-
Notifications
You must be signed in to change notification settings - Fork 2
/
HalfEdge.cs
394 lines (356 loc) · 14.2 KB
/
HalfEdge.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* See flipcode.com/archives/The_Half-Edge_Data_Structure.shtml
* @author Edvin von Platen
*/
namespace KaplaCSG
{
public enum PlaneConfig
{
On,
Left, // positive dot
Right // Negative dot
}
public class HEVertex
{
public Vector3 v;
public short heIndex = -1;// One of the half-edges emantating from the vertex
public PlaneConfig config;
}
public class HEFace
{
public short heIndex = -1; // Store one halfedge bordering each face.
public Vector3 normal; // TODO LÄS HÖGS UP
public HEFace Copy()
{
HEFace c = new HEFace
{
heIndex = this.heIndex,
normal = new Vector3(normal.x, normal.y, normal.z)
};
return c;
}
}
public class HalfEdge
{
// Half edge represented by indices.
// ^
// | next half edge (around the vertex
// |
// vert --------> OppositeVert
public short verIndex = -1; // Index to vertex at the end of the half-edge
public short oppositeIndex = -1; // Index to oppositely oriented adjacent half-edge
public short faceIndex = -1; // Index to the face (triangle) the half-edge borders
public short nextIndex = -1; // Index to the next half-edge around the face
public short index = -1; // Index to where in the halfedge list it is
public HalfEdge Copy()
{
HalfEdge c = new HalfEdge
{
verIndex = this.verIndex,
oppositeIndex = this.oppositeIndex,
faceIndex = this.faceIndex,
nextIndex = this.nextIndex,
index = this.index
};
return c;
}
/*
* Create a pair of half-edges with a new vertex in between two other half-edges.
*
*/
public static HalfEdge[] CreateFromTwo(HalfEdge left, HalfEdge right, short index, short vertexIndex)
{
// Creates two new halfedged emaniating from vertexIndex which lies on the left and right edge.
// i.e. left.oppositeIndex = right.oppositeIndex
// he_0 extends left and he_1 extends right
HalfEdge[] res = new HalfEdge[2];
HalfEdge he_0 = new HalfEdge();
HalfEdge he_1 = new HalfEdge();
he_0.index = index;
he_1.index = (short)(index + 1);
// Lets start with left. I.e. goes left -> right updated to left -> new vertex;
// he_1 goes from new vertex -> left
left.oppositeIndex = he_1.index;
he_1.oppositeIndex = left.index;
he_1.verIndex = vertexIndex;
he_1.faceIndex = right.faceIndex;
he_1.nextIndex = right.nextIndex;
// right and he_0
right.oppositeIndex = he_0.index;
he_0.oppositeIndex = right.index;
he_0.verIndex = vertexIndex;
he_0.faceIndex = left.faceIndex;
he_0.nextIndex = left.nextIndex;
// Update left / right next
left.nextIndex = he_0.index;
right.nextIndex = he_1.index;
res[1] = he_1;
res[0] = he_0;
return res;
}
// Get the half-edges bordering a face
public static List<short> FaceHalfEdges(HEFace face, List<HalfEdge> halfEdges)
{
List<short> halfEdgeIndices = new List<short>();
HalfEdge he = halfEdges[face.heIndex];
short first = face.heIndex;
short next = he.nextIndex;
halfEdgeIndices.Add(first);
// Walk the next index until we return to the first.
while (first != next)
{
halfEdgeIndices.Add(next);
next = halfEdges[next].nextIndex;
}
return halfEdgeIndices;
}
// Get all half-edges going out from a vertex
public static List<short> VertexHalfEdges(HEVertex vertex, List<HalfEdge> halfEdges)
{
List<short> halfEdgeIndices = new List<short>();
short first = vertex.heIndex;
short next = halfEdges[halfEdges[first].oppositeIndex].nextIndex;
halfEdgeIndices.Add(first);
while (next != first)
{
halfEdgeIndices.Add(next);
next = halfEdges[halfEdges[next].oppositeIndex].nextIndex;
}
return halfEdgeIndices;
}
public static void CreateStructureFromMesh(Mesh mesh, List<HalfEdge> halfEdges, List<HEFace> faces, List<HEVertex> vertices)
{
int[] meshTriangles = mesh.triangles;
Vector3[] meshVertices = mesh.vertices;
Vector3[] meshNormals = mesh.normals;
// One face per triangle. Add normals
for (int i = 0; i < meshTriangles.Length; i += 3)
{
HEFace f = new HEFace();
// Take the mean of the triangle normals
Vector3 n = meshNormals[meshTriangles[i]] + meshNormals[meshTriangles[i+1]] + meshNormals[meshTriangles[i+2]];
n = n / 3.0f;
f.normal = n.normalized;
faces.Add(f);
}
// Simplify mesh vertices i.e. remove duplicates.
// There are duplicate vertices since they have different normals
// Use a dictionary to store the vertices and their new indices
Dictionary<Vector3, int> d = new Dictionary<Vector3, int>();
List<Vector3> newVerts = new List<Vector3>();
List<int> newTris = new List<int>();
int newIdx = 0;
for (int i = 0; i < meshVertices.Length; i++)
{
if (!d.ContainsKey(meshVertices[i]))
{
d[meshVertices[i]] = newIdx;
newVerts.Add(meshVertices[i]);
newIdx++;
}
}
// Update the triangle indices to the new vertex indices.
for (int i = 0; i < meshTriangles.Length; i++)
{
newTris.Add(d[meshVertices[meshTriangles[i]]]);
}
// Set to updated values
meshVertices = newVerts.ToArray();
meshTriangles = newTris.ToArray();
// Init the vertices with vertex data, but not half-edge
for (int i = 0; i < meshVertices.Length; i++)
{
HEVertex vert = new HEVertex
{
v = meshVertices[i]
};
vertices.Add(vert);
}
// Dictionary for opposite lookup. Build in first loop over triangles, used in the second.
// key is v0v1
Dictionary<string, List<short>> edgeMap = new Dictionary<string, List<short>>();
// Start with creating the halfedges and filling in everything except oppositite
for (int i = 0; i < meshTriangles.Length; i += 3)
{
// Stored 3-by-3 indices. e.g. 0,1,2 forms the first triangle.
short i0 = (short)i;
short i1 = (short)(i + 1);
short i2 = (short)(i + 2);
// Triangles are in clockwise order in Unity
HalfEdge h0 = new HalfEdge();
HalfEdge h1 = new HalfEdge();
HalfEdge h2 = new HalfEdge();
h0.index = i0;
h1.index = i1;
h2.index = i2;
h0.faceIndex = i0;
h1.faceIndex = i0;
h2.faceIndex = i0;
h0.verIndex = (short)meshTriangles[i];
h1.verIndex = (short)meshTriangles[i + 1];
h2.verIndex = (short)meshTriangles[i + 2];
// Set the vertices halfedge index. It doesn't matter if we overwrite previously set values.
vertices[meshTriangles[i]].heIndex = i0;
vertices[meshTriangles[i + 1]].heIndex = i1;
vertices[meshTriangles[i + 2]].heIndex = i2;
// next entries. Unity wants clockwise order of vertices
h0.nextIndex = h1.index;
h1.nextIndex = h2.index;
h2.nextIndex = h0.index;
// Add them!
halfEdges.Add(h0);
halfEdges.Add(h1);
halfEdges.Add(h2);
// set face HalfEdge index. i0 is a multiple of 3
faces[i0 / 3].heIndex = i0;
// Fill edgemap, three times :)
string sum;
// This check is required so the ordering is consistent
if (h0.verIndex >= h1.verIndex)
{
sum = h0.verIndex.ToString() + h1.verIndex.ToString();
} else
{
sum = h1.verIndex.ToString() + h0.verIndex.ToString();
}
if (!edgeMap.ContainsKey(sum))
{
List<short> l = new List<short>();
// add the face index
l.Add(i0);
edgeMap.Add(sum, l);
} else
{
edgeMap[sum].Add(i0);
}
if (h1.verIndex >= h2.verIndex)
{
sum = h1.verIndex.ToString() + h2.verIndex.ToString();
} else
{
sum = h2.verIndex.ToString() + h1.verIndex.ToString();
}
if (!edgeMap.ContainsKey(sum))
{
List<short> l = new List<short>();
// add the face index
l.Add(i1);
edgeMap.Add(sum, l);
}
else
{
edgeMap[sum].Add(i1);
}
if (h0.verIndex >= h2.verIndex)
{
sum = h0.verIndex.ToString() + h2.verIndex.ToString();
} else
{
sum = h2.verIndex.ToString() + h0.verIndex.ToString();
}
if (!edgeMap.ContainsKey(sum))
{
List<short> l = new List<short>();
// add the face index
l.Add(i2);
edgeMap.Add(sum, l);
}
else
{
edgeMap[sum].Add(i2);
}
}
// Fill the opposite entries
for (int i = 0; i < meshTriangles.Length; i += 3)
{
// each edge is shared between exactly two faces
// Stored 3-by-3 indices. e.g. 0,1,2 forms the first triangle.
short i0 = (short)i;
short i1 = (short)(i + 1);
short i2 = (short)(i + 2);
HalfEdge h0 = halfEdges[i0];
HalfEdge h1 = halfEdges[i1];
HalfEdge h2 = halfEdges[i2];
// First edge
// Use our edgeMap to find opposites
string sum;
if (h0.verIndex >= h1.verIndex)
{
sum = h0.verIndex.ToString() + h1.verIndex.ToString();
}
else
{
sum = h1.verIndex.ToString() + h0.verIndex.ToString();
}
List<short> l = edgeMap[sum];
short h0Idx = l[0];
short h1Idx = l[1];
halfEdges[h0Idx].oppositeIndex = h1Idx;
halfEdges[h1Idx].oppositeIndex = h0Idx;
// Second edge
if (h1.verIndex >= h2.verIndex)
{
sum = h1.verIndex.ToString() + h2.verIndex.ToString();
}
else
{
sum = h2.verIndex.ToString() + h1.verIndex.ToString();
}
l = edgeMap[sum];
h0Idx = l[0];
h1Idx = l[1];
halfEdges[h0Idx].oppositeIndex = h1Idx;
halfEdges[h1Idx].oppositeIndex = h0Idx;
// Third edge
if (h0.verIndex >= h2.verIndex)
{
sum = h0.verIndex.ToString() + h2.verIndex.ToString();
}
else
{
sum = h2.verIndex.ToString() + h0.verIndex.ToString();
}
l = edgeMap[sum];
h0Idx = l[0];
h1Idx = l[1];
halfEdges[h0Idx].oppositeIndex = h1Idx;
halfEdges[h1Idx].oppositeIndex = h0Idx;
}
}
// Reacreate a unity mesh with vertices, triangles, and normals.
// Assume all faces are triangles, call Triangulate() before use if not.
public static Mesh CreateMeshFromHalfEdge(List<HEFace> faces, List<HEVertex> vertices, List<HalfEdge> halfEdges)
{
Mesh meshRes = new Mesh();
List<int> meshTriangles = new List<int>();
List<Vector3> meshVertices = new List<Vector3>();
List<Vector3> meshNormals = new List<Vector3>();
// Foreach face. Walk it and save the vertex and normal data toghether with triangle indices.
int triIdx = 0;
foreach (HEFace face in faces)
{
// Each face has a normal and an index into halfEdges
// Get the face halfedges
List<short> faceHalfEdges = HalfEdge.FaceHalfEdges(face, halfEdges);
if (faceHalfEdges.Count != 3)
{
Debug.Assert(faceHalfEdges.Count == 3);
}
foreach (short i in faceHalfEdges)
{
meshVertices.Add(vertices[halfEdges[i].verIndex].v);
meshNormals.Add(face.normal);
meshTriangles.Add(triIdx);
triIdx++;
}
}
meshRes.vertices = meshVertices.ToArray();
meshRes.triangles = meshTriangles.ToArray();
meshRes.normals = meshNormals.ToArray();
return meshRes;
}
}
}