forked from chongyangma/LevelSyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelMath.cpp
More file actions
270 lines (251 loc) · 6.94 KB
/
Copy pathLevelMath.cpp
File metadata and controls
270 lines (251 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "LevelMath.h"
namespace level_math
{
float PointToSegmentSqDistance(const v2f& pt, const CLineBase& line)
{
if ( line.GetSqLength() < g_numericalTolerance * g_numericalTolerance )
{
return mag2(pt - line.GetPos1());
}
float d1 = mag2(pt - line.GetPos1());
float d2 = mag2(pt - line.GetPos2());
v2f pe = line.GetPos2() - line.GetPos1();
v2f pd = pt - line.GetPos1();
float dp = dot(pe, pd);
float r = dp / mag2(pe);
float d;
if ( r >= 1.f )
{
d = d2;
}
else if ( r <= 0.f )
{
d = d1;
}
else
{
v2f peNew = v2f(pe[1], -pe[0]);
d = std::abs(dot(pd, peNew) / mag(peNew));
d = d * d;
}
return d;
}
float PointToLineSqDistance(const v2f& pt, const CLineBase& line)
{
return PointToLineSqDistance(pt, line.GetPos2(), line.GetPos1());
}
float PointToLineSqDistance(const v2f& pt, const v2f& p1, const v2f& p2)
{
v2f pe = p2 - p1;
v2f peNorm = normalize(pe);
v2f pr = pt - p1;
v3f peNew = v3f(peNorm[0], peNorm[1], 0.f);
v3f prNew = v3f(pr[0], pr[1], 0.f);
v3f cp = cross(peNew, prNew);
float d = mag2(cp);
return d;
}
float RoomPerimeter(const CRoom& room1)
{
float contactArea = 0.f;
for ( int i=0; i<room1.GetNumOfEdges(); i++ )
{
CRoomEdge edge1 = room1.GetEdge(i);
contactArea += edge1.GetLength();
}
return contactArea;
}
float RoomContact(const CRoom& room1, const CRoom& room2)
{
float contactArea = 0.f;
for ( int i=0; i<room1.GetNumOfEdges(); i++ )
{
CRoomEdge edge1 = room1.GetEdge(i);
for ( int j=0; j<room2.GetNumOfEdges(); j++ )
{
CRoomEdge edge2 = room2.GetEdge(j);
if ( edge1.GetDoorFlag() == false || edge2.GetDoorFlag() == false )
{
continue;
}
float contactAreaTmp = EdgeContact(edge1, edge2);
contactArea += contactAreaTmp;
}
}
return contactArea;
}
float RoomContact(const CRoom& room1, const CRoom& room2, int& edgeIdx1, int& edgeIdx2)
{
float contactAreaMax = 0.f;
for ( int i=0; i<room1.GetNumOfEdges(); i++ )
{
CRoomEdge edge1 = room1.GetEdge(i);
for ( int j=0; j<room2.GetNumOfEdges(); j++ )
{
CRoomEdge edge2 = room2.GetEdge(j);
if ( edge1.GetDoorFlag() == false || edge2.GetDoorFlag() == false )
{
continue;
}
float contactAreaTmp = EdgeContact(edge1, edge2);
if ( contactAreaTmp > contactAreaMax )
{
contactAreaMax = contactAreaTmp;
edgeIdx1 = i;
edgeIdx2 = j;
}
}
}
return contactAreaMax;
}
float EdgeContact(const CLineBase& line1, const CLineBase& line2)
{
const float numericalTolerance = g_numericalTolerance * 100.f;
const float numericalToleranceSq = numericalTolerance * numericalTolerance;
v2f pr1 = line1.GetPos2() - line1.GetPos1();
v2f pr2 = line2.GetPos2() - line2.GetPos1();
v3f pe1 = v3f(pr1[0], pr1[1], 0.f);
v3f pe2 = v3f(pr2[0], pr2[1], 0.f);
v3f cp = cross(pe1, pe2);
if ( mag2(cp) > numericalTolerance )
{
return 0.f;
}
v2f posMin1 = min_union(line1.GetPos1(), line1.GetPos2());
v2f posMax1 = max_union(line1.GetPos1(), line1.GetPos2());
v2f posMin2 = min_union(line2.GetPos1(), line2.GetPos2());
v2f posMax2 = max_union(line2.GetPos1(), line2.GetPos2());
for ( int j=0; j<2; j++ )
{
if ( posMax1[j] < posMin2[j] - numericalTolerance || posMin1[j] > posMax2[j] + numericalTolerance )
{
return 0.f;
}
}
float d1 = PointToLineSqDistance(line2.GetPos1(), line1);
float d2 = PointToLineSqDistance(line2.GetPos2(), line1);
if ( d1 > numericalToleranceSq || d2 > numericalToleranceSq )
{
return 0.f;
}
// Now the two edges should in the same line anyway...
float len1 = mag(pe1);
float len2 = mag(pe2);
float d11 = mag2(line1.GetPos1() - line2.GetPos1());
float d21 = mag2(line1.GetPos2() - line2.GetPos1());
float d12 = mag2(line1.GetPos1() - line2.GetPos2());
float d22 = mag2(line1.GetPos2() - line2.GetPos2());
float dMax = sqrt(max(max(d11, d21), max(d12, d22)));
dMax = max(dMax, max(len1, len2));
float contactArea = len1 + len2 - dMax;
contactArea = max(contactArea, 0.f);
return contactArea;
}
float RoomDistance(const CRoom& room1, const CRoom& room2)
{
float d = 1e10;
for ( int i=0; i<room1.GetNumOfVertices(); i++ )
{
v2f pt = room1.GetVertex(i);
for ( int j=0; j<room2.GetNumOfEdges(); j++ )
{
CRoomEdge edge = room2.GetEdge(j);
float dTmp = PointToSegmentSqDistance(pt, edge);
d = min(d, dTmp);
}
}
d = sqrt(d);
return d;
}
bool SegmentIntersection(v2f pa, v2f pb, v2f pc, v2f pd, v2f& pi)
{
return SegmentIntersection(pa[0], pa[1], pb[0], pb[1], pc[0], pc[1], pd[0], pd[1], pi[0], pi[1]);
}
// Based on the example under http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
bool SegmentIntersection(float Ax, float Ay, float Bx, float By, float Cx, float Cy, float Dx, float Dy, float& Ix, float& Iy)
{
float Rx = Bx - Ax;
float Ry = By - Ay;
float Sx = Dx - Cx;
float Sy = Dy - Cy;
float QPx = Cx - Ax;
float QPy = Cy - Ay;
float rs = Rx * Sy - Ry * Sx;
if ( rs == 0.f )
{
return false;
}
float t = (QPx * Sy - QPy * Sx) / rs;
float u = (QPx * Ry - QPy * Rx) / rs;
if ( t >= 0.f && t <= 1.f && u >= 0.f && u <= 1.f )
{
Ix = Ax + t * Rx;
Iy = Ay + t * Ry;
return true;
}
else
{
return false;
}
}
bool LineIntersection(v2f pa, v2f pb, v2f pc, v2f pd, v2f& pi)
{
return LineIntersection(pa[0], pa[1], pb[0], pb[1], pc[0], pc[1], pd[0], pd[1], pi[0], pi[1]);
}
bool LineIntersection(float Ax, float Ay, float Bx, float By, float Cx, float Cy, float Dx, float Dy, float& Ix, float& Iy)
{
float Rx = Bx - Ax;
float Ry = By - Ay;
float Sx = Dx - Cx;
float Sy = Dy - Cy;
float QPx = Cx - Ax;
float QPy = Cy - Ay;
float rs = Rx * Sy - Ry * Sx;
if ( rs == 0.f )
{
return false;
}
float t = (QPx * Sy - QPy * Sx) / rs;
float u = (QPx * Ry - QPy * Rx) / rs;
Ix = Ax + t * Rx;
Iy = Ay + t * Ry;
return true;
}
bool ComparePrSmallerFirst(const PrSort& pr1, const PrSort& pr2)
{
return (pr1.m_dp < pr2.m_dp);
}
void SortVecPr(std::vector<v2f>& vecPr)
{
if ( vecPr.size() < 2 )
{
return;
}
v2f pd = vecPr[1] - vecPr[0];
std::vector<PrSort> vecPrSort(vecPr.size());
for ( int i=0; i<int(vecPrSort.size()); i++ )
{
vecPrSort[i].m_pr = vecPr[i];
vecPrSort[i].m_dp = dot(pd, vecPr[i] - vecPr[0]);
}
sort(vecPrSort.begin(), vecPrSort.end(), ComparePrSmallerFirst);
for ( int i=0; i<int(vecPrSort.size()); i++ )
{
vecPr[i] = vecPrSort[i].m_pr;
}
}
v3f randomColorFromIndex(int idx)
{
static std::vector<v3f> clrs;
if ( clrs.empty() )
{
clrs.resize(256);
for ( int c=0; c<clrs.size(); c++ )
{
clrs[c] = v3f(rand(), rand(), rand());
clrs[c] = clrs[c] / max(clrs[c]);
}
}
return (clrs[idx&255]);
}
}