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
|
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "catmullclark_coefficients.h"
namespace embree
{
class __aligned(32) HalfEdge
{
friend class SubdivMesh;
public:
enum PatchType : char {
BILINEAR_PATCH = 0, //!< a bilinear patch
REGULAR_QUAD_PATCH = 1, //!< a regular quad patch can be represented as a B-Spline
IRREGULAR_QUAD_PATCH = 2, //!< an irregular quad patch can be represented as a Gregory patch
COMPLEX_PATCH = 3 //!< these patches need subdivision and cannot be processed by the above fast code paths
};
enum VertexType : char {
REGULAR_VERTEX = 0, //!< regular vertex
NON_MANIFOLD_EDGE_VERTEX = 1, //!< vertex of a non-manifold edge
};
__forceinline friend PatchType max( const PatchType& ty0, const PatchType& ty1) {
return (PatchType) max((int)ty0,(int)ty1);
}
struct Edge
{
/*! edge constructor */
__forceinline Edge(const uint32_t v0, const uint32_t v1)
: v0(v0), v1(v1) {}
/*! create an 64 bit identifier that is unique for the not oriented edge */
__forceinline operator uint64_t() const
{
uint32_t p0 = v0, p1 = v1;
if (p0<p1) std::swap(p0,p1);
return (((uint64_t)p0) << 32) | (uint64_t)p1;
}
public:
uint32_t v0,v1; //!< start and end vertex of the edge
};
HalfEdge ()
: vtx_index(-1), next_half_edge_ofs(0), prev_half_edge_ofs(0), opposite_half_edge_ofs(0), edge_crease_weight(0),
vertex_crease_weight(0), edge_level(0), patch_type(COMPLEX_PATCH), vertex_type(REGULAR_VERTEX)
{
static_assert(sizeof(HalfEdge) == 32, "invalid half edge size");
}
__forceinline bool hasOpposite() const { return opposite_half_edge_ofs != 0; }
__forceinline void setOpposite(HalfEdge* opposite) { opposite_half_edge_ofs = int(opposite-this); }
__forceinline HalfEdge* next() { assert( next_half_edge_ofs != 0 ); return &this[next_half_edge_ofs]; }
__forceinline const HalfEdge* next() const { assert( next_half_edge_ofs != 0 ); return &this[next_half_edge_ofs]; }
__forceinline HalfEdge* prev() { assert( prev_half_edge_ofs != 0 ); return &this[prev_half_edge_ofs]; }
__forceinline const HalfEdge* prev() const { assert( prev_half_edge_ofs != 0 ); return &this[prev_half_edge_ofs]; }
__forceinline HalfEdge* opposite() { assert( opposite_half_edge_ofs != 0 ); return &this[opposite_half_edge_ofs]; }
__forceinline const HalfEdge* opposite() const { assert( opposite_half_edge_ofs != 0 ); return &this[opposite_half_edge_ofs]; }
__forceinline HalfEdge* rotate() { return opposite()->next(); }
__forceinline const HalfEdge* rotate() const { return opposite()->next(); }
__forceinline unsigned int getStartVertexIndex() const { return vtx_index; }
__forceinline unsigned int getEndVertexIndex () const { return next()->vtx_index; }
__forceinline Edge getEdge () const { return Edge(getStartVertexIndex(),getEndVertexIndex()); }
/*! tests if the start vertex of the edge is regular */
__forceinline PatchType vertexType() const
{
const HalfEdge* p = this;
size_t face_valence = 0;
bool hasBorder = false;
do
{
/* we need subdivision to handle edge creases */
if (p->hasOpposite() && p->edge_crease_weight > 0.0f)
return COMPLEX_PATCH;
face_valence++;
/* test for quad */
const HalfEdge* pp = p;
pp = pp->next(); if (pp == p) return COMPLEX_PATCH;
pp = pp->next(); if (pp == p) return COMPLEX_PATCH;
pp = pp->next(); if (pp == p) return COMPLEX_PATCH;
pp = pp->next(); if (pp != p) return COMPLEX_PATCH;
/* continue with next face */
p = p->prev();
if (likely(p->hasOpposite()))
p = p->opposite();
/* if there is no opposite go the long way to the other side of the border */
else
{
face_valence++;
hasBorder = true;
p = this;
while (p->hasOpposite())
p = p->rotate();
}
} while (p != this);
/* calculate vertex type */
if (face_valence == 2 && hasBorder) {
if (vertex_crease_weight == 0.0f ) return REGULAR_QUAD_PATCH;
else if (vertex_crease_weight == float(inf)) return REGULAR_QUAD_PATCH;
else return COMPLEX_PATCH;
}
else if (vertex_crease_weight != 0.0f) return COMPLEX_PATCH;
else if (face_valence == 3 && hasBorder) return REGULAR_QUAD_PATCH;
else if (face_valence == 4 && !hasBorder) return REGULAR_QUAD_PATCH;
else return IRREGULAR_QUAD_PATCH;
}
/*! tests if this edge is part of a bilinear patch */
__forceinline bool bilinearVertex() const {
return vertex_crease_weight == float(inf) && edge_crease_weight == float(inf);
}
/*! calculates the type of the patch */
__forceinline PatchType patchType() const
{
const HalfEdge* p = this;
PatchType ret = REGULAR_QUAD_PATCH;
bool bilinear = true;
ret = max(ret,p->vertexType());
bilinear &= p->bilinearVertex();
if ((p = p->next()) == this) return COMPLEX_PATCH;
ret = max(ret,p->vertexType());
bilinear &= p->bilinearVertex();
if ((p = p->next()) == this) return COMPLEX_PATCH;
ret = max(ret,p->vertexType());
bilinear &= p->bilinearVertex();
if ((p = p->next()) == this) return COMPLEX_PATCH;
ret = max(ret,p->vertexType());
bilinear &= p->bilinearVertex();
if ((p = p->next()) != this) return COMPLEX_PATCH;
if (bilinear) return BILINEAR_PATCH;
return ret;
}
/*! tests if the face is a regular b-spline face */
__forceinline bool isRegularFace() const {
return patch_type == REGULAR_QUAD_PATCH;
}
/*! tests if the face can be diced (using bspline or gregory patch) */
__forceinline bool isGregoryFace() const {
return patch_type == IRREGULAR_QUAD_PATCH || patch_type == REGULAR_QUAD_PATCH;
}
/*! tests if the base vertex of this half edge is a corner vertex */
__forceinline bool isCorner() const {
return !hasOpposite() && !prev()->hasOpposite();
}
/*! tests if the vertex is attached to any border */
__forceinline bool vertexHasBorder() const
{
const HalfEdge* p = this;
do {
if (!p->hasOpposite()) return true;
p = p->rotate();
} while (p != this);
return false;
}
/*! tests if the face this half edge belongs to has some border */
__forceinline bool faceHasBorder() const
{
const HalfEdge* p = this;
do {
if (p->vertexHasBorder() && (p->vertex_type != HalfEdge::NON_MANIFOLD_EDGE_VERTEX)) return true;
p = p->next();
} while (p != this);
return false;
}
/*! calculates conservative bounds of a catmull clark subdivision face */
__forceinline BBox3fa bounds(const BufferView<Vec3fa>& vertices) const
{
BBox3fa bounds = this->get1RingBounds(vertices);
for (const HalfEdge* p=this->next(); p!=this; p=p->next())
bounds.extend(p->get1RingBounds(vertices));
return bounds;
}
/*! tests if this is a valid patch */
__forceinline bool valid(const BufferView<Vec3fa>& vertices) const
{
size_t N = 1;
if (!this->validRing(vertices)) return false;
for (const HalfEdge* p=this->next(); p!=this; p=p->next(), N++) {
if (!p->validRing(vertices)) return false;
}
return N >= 3 && N <= MAX_PATCH_VALENCE;
}
/*! counts number of polygon edges */
__forceinline unsigned int numEdges() const
{
unsigned int N = 1;
for (const HalfEdge* p=this->next(); p!=this; p=p->next(), N++);
return N;
}
/*! calculates face and edge valence */
__forceinline void calculateFaceValenceAndEdgeValence(size_t& faceValence, size_t& edgeValence) const
{
faceValence = 0;
edgeValence = 0;
const HalfEdge* p = this;
do
{
/* calculate bounds of current face */
unsigned int numEdges = p->numEdges();
assert(numEdges >= 3);
edgeValence += numEdges-2;
faceValence++;
p = p->prev();
/* continue with next face */
if (likely(p->hasOpposite()))
p = p->opposite();
/* if there is no opposite go the long way to the other side of the border */
else {
faceValence++;
edgeValence++;
p = this;
while (p->hasOpposite())
p = p->opposite()->next();
}
} while (p != this);
}
/*! stream output */
friend __forceinline std::ostream &operator<<(std::ostream &o, const HalfEdge &h)
{
return o << "{ " <<
"vertex = " << h.vtx_index << ", " << //" -> " << h.next()->vtx_index << ", " <<
"prev = " << h.prev_half_edge_ofs << ", " <<
"next = " << h.next_half_edge_ofs << ", " <<
"opposite = " << h.opposite_half_edge_ofs << ", " <<
"edge_crease = " << h.edge_crease_weight << ", " <<
"vertex_crease = " << h.vertex_crease_weight << ", " <<
//"edge_level = " << h.edge_level <<
" }";
}
private:
/*! calculates the bounds of the face associated with the half-edge */
__forceinline BBox3fa getFaceBounds(const BufferView<Vec3fa>& vertices) const
{
BBox3fa b = vertices[getStartVertexIndex()];
for (const HalfEdge* p = next(); p!=this; p=p->next()) {
b.extend(vertices[p->getStartVertexIndex()]);
}
return b;
}
/*! calculates the bounds of the 1-ring associated with the vertex of the half-edge */
__forceinline BBox3fa get1RingBounds(const BufferView<Vec3fa>& vertices) const
{
BBox3fa bounds = empty;
const HalfEdge* p = this;
do
{
/* calculate bounds of current face */
bounds.extend(p->getFaceBounds(vertices));
p = p->prev();
/* continue with next face */
if (likely(p->hasOpposite()))
p = p->opposite();
/* if there is no opposite go the long way to the other side of the border */
else {
p = this;
while (p->hasOpposite())
p = p->opposite()->next();
}
} while (p != this);
return bounds;
}
/*! tests if this is a valid face */
__forceinline bool validFace(const BufferView<Vec3fa>& vertices, size_t& N) const
{
const Vec3fa v = vertices[getStartVertexIndex()];
if (!isvalid(v)) return false;
size_t n = 1;
for (const HalfEdge* p = next(); p!=this; p=p->next(), n++) {
const Vec3fa v = vertices[p->getStartVertexIndex()];
if (!isvalid(v)) return false;
}
N += n-2;
return n >= 3 && n <= MAX_PATCH_VALENCE;
}
/*! tests if this is a valid ring */
__forceinline bool validRing(const BufferView<Vec3fa>& vertices) const
{
size_t faceValence = 0;
size_t edgeValence = 0;
const HalfEdge* p = this;
do
{
/* calculate bounds of current face */
if (!p->validFace(vertices,edgeValence))
return false;
faceValence++;
p = p->prev();
/* continue with next face */
if (likely(p->hasOpposite()))
p = p->opposite();
/* if there is no opposite go the long way to the other side of the border */
else {
faceValence++;
edgeValence++;
p = this;
while (p->hasOpposite())
p = p->opposite()->next();
}
} while (p != this);
return faceValence <= MAX_RING_FACE_VALENCE && edgeValence <= MAX_RING_EDGE_VALENCE;
}
private:
unsigned int vtx_index; //!< index of edge start vertex
int next_half_edge_ofs; //!< relative offset to next half edge of face
int prev_half_edge_ofs; //!< relative offset to previous half edge of face
int opposite_half_edge_ofs; //!< relative offset to opposite half edge
public:
float edge_crease_weight; //!< crease weight attached to edge
float vertex_crease_weight; //!< crease weight attached to start vertex
float edge_level; //!< subdivision factor for edge
PatchType patch_type; //!< stores type of subdiv patch
VertexType vertex_type; //!< stores type of the start vertex
char align[2];
};
}
|