-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStructure.cs
459 lines (367 loc) · 12.9 KB
/
DataStructure.cs
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MyDataStructure
{
public struct Triangle
{
public Vector2 v1;
public Vector2 v2;
public Vector2 v3;
public Triangle(Vector2 v1, Vector2 v2, Vector2 v3)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
SetOrientation();
}
/// <summary>
/// Switch clockwise = counter-clockwise
/// </summary>
public void ChangeOrientation()
{
Vector2 temp = v1;
v1 = v2;
v2 = temp;
}
public float MinX()
{
return Mathf.Min(v1.x, Mathf.Min(v2.x, v3.x));
}
public float MaxX()
{
return Mathf.Max(v1.x, Mathf.Max(v2.x, v3.x));
}
public float MinY()
{
return Mathf.Min(v1.y, Mathf.Min(v2.y, v3.y));
}
public float MaxY()
{
return Mathf.Max(v1.y, Mathf.Max(v2.y, v3.y));
}
public void SetOrientation()
{
if (!MathUtility.isTriangleOrientedClockWise(v1, v2, v3))
{
ChangeOrientation();
}
}
}
public class HalfEdge
{
//The vertex it points to
public HalfEdgeVertex v;
//The face it belongs to
public HalfEdgeFace face;
//The next half-edge inside the face (ordered clockwise)
//The document says counter-clockwise but clockwise is easier because that's how Unity is displaying triangles
public HalfEdge nextEdge;
//The opposite half-edge belonging to the neighbor
public HalfEdge oppositeEdge;
//(optionally) the previous halfedge in the face
//If we assume the face is closed, then we could identify this edge by walking forward
//until we reach it
public HalfEdge prevEdge;
public HalfEdge(HalfEdgeVertex v)
{
this.v = v;
}
}
public class HalfEdgeFace
{
public HalfEdge edge;
public float circumRadius;
public Vector2 circumCenter;
public Vector2 centerPosition;
public HalfEdgeFace(HalfEdge edge)
{
this.edge = edge;
Vector2 a = edge.v.position;
Vector2 b = edge.nextEdge.v.position;
Vector2 c = edge.prevEdge.v.position;
LinearEquation lineAB = new LinearEquation(a, b);
LinearEquation lineBC = new LinearEquation(b, c);
var perpendicularAB = lineAB.PerpendicularLineAt(Vector2.Lerp(a, b, .5f));
var perpendicularBC = lineBC.PerpendicularLineAt(Vector2.Lerp(b, c, .5f));
this.circumCenter = GetCrossingPoint(perpendicularAB, perpendicularBC);
this.circumRadius = Vector2.Distance(circumCenter, a);
}
public void RecalculateCircums()
{
Vector2 a = edge.v.position;
Vector2 b = edge.nextEdge.v.position;
Vector2 c = edge.prevEdge.v.position;
LinearEquation lineAB = new LinearEquation(a, b);
LinearEquation lineBC = new LinearEquation(b, c);
var perpendicularAB = lineAB.PerpendicularLineAt(Vector2.Lerp(a, b, .5f));
var perpendicularBC = lineBC.PerpendicularLineAt(Vector2.Lerp(b, c, .5f));
circumCenter = GetCrossingPoint(perpendicularAB, perpendicularBC);
circumRadius = Vector2.Distance(circumCenter, a);
centerPosition = (a + b + c) / 3;
}
static Vector2 GetCrossingPoint(LinearEquation line1, LinearEquation line2)
{
float A1 = line1._A;
float A2 = line2._A;
float B1 = line1._B;
float B2 = line2._B;
float C1 = line1._C;
float C2 = line2._C;
//Cramer's rule
float Determinant = A1 * B2 - A2 * B1;
float DeterminantX = C1 * B2 - C2 * B1;
float DeterminantY = A1 * C2 - A2 * C1;
float x = DeterminantX / Determinant;
float y = DeterminantY / Determinant;
return new Vector2(x, y);
}
public bool IsInside(Vector3 p)
{
Vector2 p1 = edge.v.position;
Vector2 p2 = edge.nextEdge.v.position;
Vector2 p3 = edge.prevEdge.v.position;
bool isWithinTriangle = false;
//Based on Barycentric coordinates
float denominator = ((p2.y - p3.y) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.y - p3.y));
float a = ((p2.y - p3.y) * (p.x - p3.x) + (p3.x - p2.x) * (p.y - p3.y)) / denominator;
float b = ((p3.y - p1.y) * (p.x - p3.x) + (p1.x - p3.x) * (p.y - p3.y)) / denominator;
float c = 1 - a - b;
//The point is within the triangle or on the border if 0 <= a <= 1 and 0 <= b <= 1 and 0 <= c <= 1
//if (a >= 0f && a <= 1f && b >= 0f && b <= 1f && c >= 0f && c <= 1f)
//{
// isWithinTriangle = true;
//}
//The point is within the triangle
if (a > 0f && a < 1f && b > 0f && b < 1f && c > 0f && c < 1f)
{
isWithinTriangle = true;
}
return isWithinTriangle;
}
}
public class HalfEdgeVertex
{
public Vector2 position;
// 이 edge의 시작점은 이 vertex다.
public HalfEdge edge;
public HalfEdgeVertex(Vector2 position_)
{
position = position_;
}
}
public class Bin
{
public List<HalfEdgeVertex> points;
public Bin()
{
points = new List<HalfEdgeVertex>();
}
public void Add(HalfEdgeVertex point)
{
points.Add(point);
}
}
public class HalfEdgeData
{
public List<HalfEdgeVertex> vertices;
public List<HalfEdgeFace> faces;
public List<HalfEdge> edges;
public Bin[,] bins;
public HalfEdgeData()
{
this.vertices = new List<HalfEdgeVertex>();
this.faces = new List<HalfEdgeFace>();
this.edges = new List<HalfEdge>();
}
public void AddPointToBin(Bounds bound, List<Vector2> allPoints)
{
int M = (int)Mathf.Pow(allPoints.Count, 1f / 4f);
bins = new Bin[M, M];
for (int i = 0; i < allPoints.Count; i++)
{
var point = allPoints[i];
int x = Mathf.RoundToInt(0.99f * M * point.x / bound.size.x);
int y = Mathf.RoundToInt(0.99f * M * point.y / bound.size.y);
// bins[x, y] = point;
}
}
public void Update(List<Triangle> triangles)
{
foreach (var tri in triangles)
{
HalfEdgeVertex v1 = new HalfEdgeVertex(tri.v1);
HalfEdgeVertex v2 = new HalfEdgeVertex(tri.v2);
HalfEdgeVertex v3 = new HalfEdgeVertex(tri.v3);
HalfEdge he1 = new HalfEdge(v1);
HalfEdge he2 = new HalfEdge(v2);
HalfEdge he3 = new HalfEdge(v3);
he1.nextEdge = he2;
he2.nextEdge = he3;
he3.nextEdge = he1;
he1.prevEdge = he3;
he2.prevEdge = he1;
he3.prevEdge = he2;
v1.edge = he2;
v2.edge = he3;
v3.edge = he1;
HalfEdgeFace face = new HalfEdgeFace(he1);
// he1.face = he2.face = he3.face = face;
he1.face = face;
he2.face = face;
he3.face = face;
edges.Add(he1);
edges.Add(he2);
edges.Add(he3);
faces.Add(face);
vertices.Add(v1);
vertices.Add(v2);
vertices.Add(v3);
}
SetOpposite();
}
public void SetOpposite()
{
foreach (var e in edges)
{
var curEdgeVertex = e.v;
var nextEdgeVertex = e.nextEdge.v;
foreach (HalfEdge other in edges)
{
if (e == other) // 해당 edge의 opposite edge를 찾는거니 자기 자신을 참조할 필요는 없다
{
continue;
}
if (curEdgeVertex.position.Equals(other.nextEdge.v.position) && nextEdgeVertex.position.Equals(other.v.position))
{
e.oppositeEdge = other;
break;
}
}
}
}
public HashSet<Triangle> HalfEdge2Triangle()
{
HashSet<Triangle> triangles = new HashSet<Triangle>();
foreach (var face in faces)
{
var v1 = face.edge.v.position;
var v2 = face.edge.nextEdge.v.position;
var v3 = face.edge.prevEdge.v.position;
Triangle tri = new Triangle(v1, v2, v3);
triangles.Add(tri);
}
return triangles;
}
}
[System.Serializable]
public class LinearEquation
{
public float _A;
public float _B;
public float _C;
public LinearEquation() { }
//Ax+By=C
public LinearEquation(Vector2 pointA, Vector2 pointB)
{
float deltaX = pointB.x - pointA.x;
float deltaY = pointB.y - pointA.y;
_A = deltaY; //y2-y1
_B = -deltaX; //x1-x2
_C = _A * pointA.x + _B * pointA.y;
}
public LinearEquation PerpendicularLineAt(Vector3 point)
{
LinearEquation newLine = new LinearEquation();
newLine._A = -_B;
newLine._B = _A;
newLine._C = newLine._A * point.x + newLine._B * point.y;
return newLine;
}
}
public class Normalizer2
{
float dMax;
AABB2 boundingBox;
public Normalizer2(List<Vector2> points)
{
this.boundingBox = new AABB2(points);
this.dMax = CalculateDMax(boundingBox);
}
public float CalculateDMax(AABB2 boundingBox)
{
float dX = boundingBox.max.x - boundingBox.min.x;
float dY = boundingBox.max.y - boundingBox.min.y;
float dMax = Mathf.Max(dX, dY);
return dMax;
}
public Vector2 Normalize(Vector2 point)
{
float x = (point.x - boundingBox.min.x) / dMax;
float y = (point.y - boundingBox.min.y) / dMax;
return new Vector2(x, y);
}
public Vector2 UnNormalize(Vector2 point)
{
float x = (point.x * dMax) + boundingBox.min.x;
float y = (point.y * dMax) + boundingBox.min.y;
return new Vector2(x, y);
}
public List<Vector2> Normalize(List<Vector2> points)
{
List<Vector2> result = new List<Vector2>();
foreach (var p in points)
{
result.Add(Normalize(p));
}
return result;
}
public void UnNormalize(ref HalfEdgeData data)
{
foreach (var v in data.vertices)
{
Vector2 unNormalize = UnNormalize(v.position);
v.position = unNormalize;
}
}
}
public struct AABB2
{
public Vector2 min;
public Vector2 max;
public AABB2(Vector2 min, Vector2 max)
{
this.min = min;
this.max = max;
}
public AABB2(List<Vector2> points)
{
float minX = float.MaxValue;
float minY = float.MaxValue;
float maxX = float.MinValue;
float maxY = float.MinValue;
for (int i = 0; i < points.Count; i++)
{
var point = points[i];
if (point.x < minX)
{
minX = point.x;
}
else if (point.x > maxX)
{
maxX = point.x;
}
if (point.y < minY)
{
minY = point.y;
}
else if (point.y > maxY)
{
maxY = point.y;
}
}
this.min = new Vector2(minX, minY);
this.max = new Vector2(maxX, maxY);
}
}
}