forked from ZelimDamian/geometry3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdgeLoop.cs
397 lines (338 loc) · 13.6 KB
/
EdgeLoop.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace g3
{
/// <summary>
/// Sequential set of vertices/edges in a mesh, that form a closed loop.
///
/// If all you have are the vertices, use EdgeLoop.VertexLoopToEdgeLoop() to construct an EdgeLoop
/// </summary>
public class EdgeLoop
{
public DMesh3 Mesh;
public int[] Vertices;
public int[] Edges;
public int[] BowtieVertices; // this may not be initialized!
public EdgeLoop(DMesh3 mesh)
{
Mesh = mesh;
}
public EdgeLoop(DMesh3 mesh, int[] vertices, int[] edges, bool bCopyArrays)
{
Mesh = mesh;
if ( bCopyArrays ) {
Vertices = new int[vertices.Length];
Array.Copy(vertices, Vertices, Vertices.Length);
Edges = new int[edges.Length];
Array.Copy(edges, Edges, Edges.Length);
} else {
Vertices = vertices;
Edges = edges;
}
}
public EdgeLoop(EdgeLoop copy)
{
Mesh = copy.Mesh;
Vertices = new int[copy.Vertices.Length];
Array.Copy(copy.Vertices, Vertices, Vertices.Length);
Edges = new int[copy.Edges.Length];
Array.Copy(copy.Edges, Edges, Edges.Length);
if (copy.BowtieVertices != null) {
BowtieVertices = new int[copy.BowtieVertices.Length];
Array.Copy(copy.BowtieVertices, BowtieVertices, BowtieVertices.Length);
}
}
/// <summary>
/// construct EdgeLoop from a list of edges of mesh
/// </summary>
public static EdgeLoop FromEdges(DMesh3 mesh, IList<int> edges)
{
int[] Edges = new int[edges.Count];
for (int i = 0; i < Edges.Length; ++i)
Edges[i] = edges[i];
int[] Vertices = new int[Edges.Length];
Index2i start_ev = mesh.GetEdgeV(Edges[0]);
Index2i prev_ev = start_ev;
for (int i = 1; i < Edges.Length; ++i) {
Index2i next_ev = mesh.GetEdgeV(Edges[i % Edges.Length]);
Vertices[i] = IndexUtil.find_shared_edge_v(ref prev_ev, ref next_ev);
prev_ev = next_ev;
}
Vertices[0] = IndexUtil.find_edge_other_v(ref start_ev, Vertices[1]);
return new EdgeLoop(mesh, Vertices, Edges, false);
}
/// <summary>
/// construct EdgeLoop from a list of vertices of mesh
/// </summary>
public static EdgeLoop FromVertices(DMesh3 mesh, IList<int> vertices)
{
int NV = vertices.Count;
int[] Vertices = new int[NV];
for (int i = 0; i < NV; ++i)
Vertices[i] = vertices[i];
int NE = NV;
int[] Edges = new int[NE];
for (int i = 0; i < NE; ++i) {
Edges[i] = mesh.FindEdge(Vertices[i], Vertices[(i + 1)%NE]);
if (Edges[i] == DMesh3.InvalidID)
throw new Exception("EdgeLoop.FromVertices: vertices are not connected by edge!");
}
return new EdgeLoop(mesh, Vertices, Edges, false);
}
/// <summary>
/// construct EdgeLoop from a list of vertices of mesh
/// if loop is a boundary edge, we can correct orientation if requested
/// </summary>
public static EdgeLoop FromVertices(DMesh3 mesh, IList<int> vertices, bool bAutoOrient = true)
{
int[] Vertices = new int[vertices.Count];
for (int i = 0; i < Vertices.Length; i++)
Vertices[i] = vertices[i];
if ( bAutoOrient ) {
int a = Vertices[0], b = Vertices[1];
int eid = mesh.FindEdge(a, b);
if (mesh.IsBoundaryEdge(eid)) {
Index2i ev = mesh.GetOrientedBoundaryEdgeV(eid);
if (ev.a == b && ev.b == a)
Array.Reverse(Vertices);
}
}
int[] Edges = new int[Vertices.Length];
for (int i = 0; i < Edges.Length; ++i) {
int a = Vertices[i], b = Vertices[(i + 1) % Vertices.Length];
Edges[i] = mesh.FindEdge(a, b);
if (Edges[i] == DMesh3.InvalidID)
throw new Exception("EdgeLoop.FromVertices: invalid edge [" + a + "," + b + "]");
}
return new EdgeLoop(mesh, Vertices, Edges, false);
}
public int VertexCount {
get { return Vertices.Length; }
}
public int EdgeCount {
get { return Edges.Length; }
}
public Vector3d GetVertex(int i) {
return Mesh.GetVertex(Vertices[i]);
}
public AxisAlignedBox3d GetBounds()
{
AxisAlignedBox3d box = AxisAlignedBox3d.Empty;
for (int i = 0; i < Vertices.Length; ++i)
box.Contain(Mesh.GetVertex(Vertices[i]));
return box;
}
public DCurve3 ToCurve(DMesh3 sourceMesh = null)
{
if (sourceMesh == null)
sourceMesh = Mesh;
DCurve3 curve = MeshUtil.ExtractLoopV(sourceMesh, Vertices);
curve.Closed = true;
return curve;
}
/// <summary>
/// if this is a border edge-loop, we can check that it is oriented correctly, and
/// if not, reverse it.
/// Returns true if we reversed orientation.
/// </summary>
public bool CorrectOrientation()
{
int a = Vertices[0], b = Vertices[1];
int eid = Mesh.FindEdge(a, b);
if (Mesh.IsBoundaryEdge(eid)) {
Index2i ev = Mesh.GetOrientedBoundaryEdgeV(eid);
if (ev.a == b && ev.b == a) {
Reverse();
return true;
}
}
return false;
}
public void Reverse()
{
Array.Reverse(Vertices);
Array.Reverse(Edges);
}
/// <summary>
/// check if all edges of this loop are internal edges (ie none on boundary)
/// </summary>
/// <returns></returns>
public bool IsInternalLoop()
{
int NV = Vertices.Length;
for (int i = 0; i < NV; ++i ) {
int eid = Mesh.FindEdge(Vertices[i], Vertices[(i + 1) % NV]);
Debug.Assert(eid != DMesh3.InvalidID);
if (Mesh.IsBoundaryEdge(eid))
return false;
}
return true;
}
/// <summary>
/// Check if all edges of this loop are boundary edges
/// </summary>
public bool IsBoundaryLoop()
{
int NV = Vertices.Length;
for (int i = 0; i < NV; ++i ) {
int eid = Mesh.FindEdge(Vertices[i], Vertices[(i + 1) % NV]);
Debug.Assert(eid != DMesh3.InvalidID);
if (Mesh.IsBoundaryEdge(eid) == false)
return false;
}
return true;
}
/// <summary>
/// find index of vertex vID in Vertices list, or -1 if not found
/// </summary>
public int FindVertexIndex(int vID)
{
int N = Vertices.Length;
for (int i = 0; i < N; ++i) {
if (Vertices[i] == vID)
return i;
}
return -1;
}
/// <summary>
/// find index of vertices of loop that is closest to point v
/// </summary>
public int FindNearestVertex(Vector3d v)
{
int iNear = -1;
double fNearSqr = double.MaxValue;
int N = Vertices.Length;
for ( int i = 0; i < N; ++i ) {
Vector3d lv = Mesh.GetVertex(Vertices[i]);
double d2 = v.DistanceSquared(lv);
if ( d2 < fNearSqr ) {
fNearSqr = d2;
iNear = i;
}
}
return iNear;
}
// count # of vertices in loop that are within tol of v
// final param returns last encountered index within tolerance, or -1 if return is 0
public int CountWithinTolerance(Vector3d v, double tol, out int last_in_tol)
{
last_in_tol = -1;
int count = 0;
int N = Vertices.Length;
for (int i = 0; i < N; ++i) {
Vector3d lv = Mesh.GetVertex(Vertices[i]);
if (v.Distance(lv) < tol) {
count++;
last_in_tol = i;
}
}
return count;
}
// Check if Loop2 is the same set of positions on another mesh.
// Does not require the indexing to be the same
// Currently doesn't handle loop-reversal
public bool IsSameLoop(EdgeLoop Loop2, bool bReverse2 = false, double tolerance = MathUtil.ZeroTolerance)
{
// find a duplicate starting vertex
int N = Vertices.Length;
int N2 = Loop2.Vertices.Length;
if (N != N2)
return false;
DMesh3 Mesh2 = Loop2.Mesh;
int start_i = 0, start_j = -1;
// try to find a unique same-vertex on each loop. Do not
// use vertices that have duplicate positions.
bool bFoundGoodStart = false;
while ( !bFoundGoodStart && start_i < N ) {
Vector3d start_v = Mesh.GetVertex(start_i);
int count = Loop2.CountWithinTolerance(start_v, tolerance, out start_j);
if (count == 1)
bFoundGoodStart = true;
else
start_i++;
}
if (!bFoundGoodStart)
return false; // no within-tolerance duplicate vtx to start at
for ( int ii = 0; ii < N; ++ii ) {
int i = (start_i + ii) % N;
int j = (bReverse2) ?
MathUtil.WrapSignedIndex(start_j - ii, N2)
: (start_j + ii) % N2;
Vector3d v = Mesh.GetVertex(Vertices[i]);
Vector3d v2 = Mesh2.GetVertex(Loop2.Vertices[j]);
if (v.Distance(v2) > tolerance)
return false;
}
return true;
}
/// <summary>
/// stores vertices [starti, starti+1, ... starti+count-1] in span, and returns span, or null if invalid range
/// </summary>
public int[] GetVertexSpan(int starti, int count, int[] span, bool reverse = false)
{
int N = Vertices.Length;
if (starti < 0 || starti >= N || count > N - 1)
return null;
if (reverse) {
for (int k = 0; k < count; ++k)
span[count-k-1] = Vertices[(starti + k) % N];
} else {
for (int k = 0; k < count; ++k)
span[k] = Vertices[(starti + k) % N];
}
return span;
}
/// <summary>
/// Exhaustively check that verts and edges of this EdgeLoop are consistent. Not for production use.
/// </summary>
public bool CheckValidity(FailMode eFailMode = FailMode.Throw)
{
bool is_ok = true;
Action<bool> CheckOrFailF = (b) => { is_ok = is_ok && b; };
if (eFailMode == FailMode.DebugAssert) {
CheckOrFailF = (b) => { Debug.Assert(b); is_ok = is_ok && b; };
} else if (eFailMode == FailMode.gDevAssert) {
CheckOrFailF = (b) => { Util.gDevAssert(b); is_ok = is_ok && b; };
} else if (eFailMode == FailMode.Throw) {
CheckOrFailF = (b) => { if (b == false) throw new Exception("EdgeLoop.CheckValidity: check failed"); };
}
CheckOrFailF(Vertices.Length == Edges.Length);
for (int ei = 0; ei < Edges.Length; ++ei) {
Index2i ev = Mesh.GetEdgeV(Edges[ei]);
CheckOrFailF(Mesh.IsVertex(ev.a));
CheckOrFailF(Mesh.IsVertex(ev.b));
CheckOrFailF(Mesh.FindEdge(ev.a, ev.b) != DMesh3.InvalidID);
CheckOrFailF(Vertices[ei] == ev.a || Vertices[ei] == ev.b);
CheckOrFailF(Vertices[(ei + 1) % Edges.Length] == ev.a || Vertices[(ei + 1) % Edges.Length] == ev.b);
}
for ( int vi = 0; vi < Vertices.Length; ++vi ) {
int a = Vertices[vi], b = Vertices[(vi + 1) % Vertices.Length];
CheckOrFailF(Mesh.IsVertex(a));
CheckOrFailF(Mesh.IsVertex(b));
CheckOrFailF(Mesh.FindEdge(a,b) != DMesh3.InvalidID);
int n = 0, edge_before_b = Edges[vi], edge_after_b = Edges[(vi + 1) % Vertices.Length];
foreach ( int nbr_e in Mesh.VtxEdgesItr(b) ) {
if (nbr_e == edge_before_b || nbr_e == edge_after_b)
n++;
}
CheckOrFailF(n == 2);
}
return is_ok;
}
/// <summary>
/// Convert a vertex loop to an edge loop. This should be somewhere else...
/// </summary>
public static int[] VertexLoopToEdgeLoop(DMesh3 mesh, int[] vertex_loop)
{
int NV = vertex_loop.Length;
int[] edges = new int[NV];
for ( int i = 0; i < NV; ++i ) {
int v0 = vertex_loop[i];
int v1 = vertex_loop[(i + 1) % NV];
edges[i] = mesh.FindEdge(v0, v1);
}
return edges;
}
}
}