Skip to content

Commit 21665bb

Browse files
committed
utility function to try to improve vtx loop stitching
1 parent b375ce5 commit 21665bb

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Diff for: mesh/MeshEditor.cs

+42
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,48 @@ public virtual int[] StitchLoop(int[] vloop1, int[] vloop2, int group_id = -1)
180180

181181

182182

183+
184+
185+
/// <summary>
186+
/// Trivial back-and-forth stitch between two vertex loops with same length.
187+
/// If nearest vertices of input loops would not be matched, cycles loops so
188+
/// that this is the case.
189+
/// Loops must have appropriate orientation.
190+
/// </summary>
191+
public virtual int[] StitchVertexLoops_NearestV(int[] loop0, int[] loop1, int group_id = -1)
192+
{
193+
int N = loop0.Length;
194+
Index2i iBestPair = Index2i.Zero;
195+
double best_dist = double.MaxValue;
196+
for (int i = 0; i < N; ++i) {
197+
Vector3d v0 = Mesh.GetVertex(loop0[i]);
198+
for (int j = 0; j < N; ++j) {
199+
double dist_sqr = v0.DistanceSquared(Mesh.GetVertex(loop1[j]));
200+
if (dist_sqr < best_dist) {
201+
best_dist = dist_sqr;
202+
iBestPair = new Index2i(i, j);
203+
}
204+
}
205+
}
206+
if (iBestPair.a != iBestPair.b) {
207+
int[] newLoop0 = new int[N];
208+
int[] newLoop1 = new int[N];
209+
for (int i = 0; i < N; ++i) {
210+
newLoop0[i] = loop0[(iBestPair.a + i) % N];
211+
newLoop1[i] = loop1[(iBestPair.b + i) % N];
212+
}
213+
return StitchLoop(newLoop0, newLoop1, group_id);
214+
} else {
215+
return StitchLoop(loop0, loop1, group_id);
216+
}
217+
218+
}
219+
220+
221+
222+
223+
224+
183225
/// <summary>
184226
/// Stitch two sets of boundary edges that are provided as unordered pairs of edges, by
185227
/// adding triangulated quads between each edge pair.

0 commit comments

Comments
 (0)