/
MeshPlaneIntersect.hpp
345 lines (317 loc) · 10.4 KB
/
MeshPlaneIntersect.hpp
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
#pragma once
#include <vector>
#include <array>
#include <map>
#include <algorithm>
#include <iterator>
template <class FloatType, class IndexType>
class MeshPlaneIntersect {
public:
typedef std::array<FloatType, 3> Vec3D;
typedef std::array<IndexType, 3> Face;
struct Plane {
Vec3D origin = { 0,0,0 };
Vec3D normal = { 0,0,1 };
};
struct Path3D {
std::vector<Vec3D> points;
bool isClosed = false;
};
class Mesh {
public:
Mesh(const std::vector<Vec3D>& vertices, const std::vector<Face>& faces) :
vertices(vertices), faces(faces) {}
std::vector<Path3D> Intersect(const Plane& plane) const {
return _Execute(*this, plane, false);
}
std::vector<Path3D> Clip(const Plane& plane) const {
return _Execute(*this, plane, true);
}
private:
const std::vector<Vec3D>& vertices;
const std::vector<Face>& faces;
typedef std::pair<int, int> Edge;
typedef std::vector<Edge> EdgePath;
static std::vector<Path3D> _Execute(const Mesh& mesh, const Plane& plane,
const bool isClip) {
const auto vertexOffsets(VertexOffsets(mesh.vertices, plane));
auto edgePaths(EdgePaths(mesh.faces, vertexOffsets));
if (isClip) {
auto freeEdges = FreeEdges(mesh.faces, vertexOffsets);
auto freeEdgePaths = FreeEdgePaths(freeEdges, vertexOffsets);
edgePaths.insert(edgePaths.end(), freeEdgePaths.begin(), freeEdgePaths.end());
}
ChainEdgePaths(edgePaths);
return ConstructGeometricPaths(mesh, edgePaths, vertexOffsets);
}
static std::vector<EdgePath> EdgePaths(const std::vector<Face>& faces,
const std::vector<FloatType>& vertexOffsets) {
auto crossingFaces = CrossingFaces(faces, vertexOffsets);
std::vector<EdgePath> edgePaths;
while (crossingFaces.size() > 0) {
edgePaths.push_back(GetEdgePath(crossingFaces));
}
return edgePaths;
}
static std::vector<Path3D> ConstructGeometricPaths(const Mesh& mesh,
const std::vector<EdgePath>& edgePaths,
const std::vector<FloatType>& vertexOffsets) {
std::vector<Path3D> paths;
for (const auto& edgePath : edgePaths) {
Path3D path;
bool skipThisPoint = path.isClosed = edgePath.front() == edgePath.back();
for (const auto& edge : edgePath) {
if (skipThisPoint) {
skipThisPoint = false;
}
else if (edge.first == edge.second) {
path.points.push_back(mesh.vertices.at(edge.first));
}
else {
const auto& offset1(vertexOffsets[edge.first]);
const auto& offset2(vertexOffsets[edge.second]);
const auto factor = offset1 / (offset1 - offset2);
const auto& edgeStart(mesh.vertices.at(edge.first));
const auto& edgeEnd(mesh.vertices.at(edge.second));
Vec3D newPoint;
for (int i(0); i < 3; ++i) {
newPoint[i] = edgeStart[i] + (edgeEnd[i] - edgeStart[i]) * factor;
}
path.points.push_back(newPoint);
}
}
paths.push_back(path);
}
return paths;
}
static const std::vector<FloatType> VertexOffsets(const std::vector<Vec3D>& vertices,
const Plane& plane) {
std::vector<FloatType> offsets;
offsets.reserve(vertices.size());
std::transform(vertices.begin(), vertices.end(), std::back_inserter(offsets),
[&plane](const auto& vertex) {
return VertexOffset(vertex, plane);
});
return offsets;
}
static FloatType VertexOffset(const Vec3D& vertex, const Plane& plane) {
FloatType offset(0);
for (int i(0); i < 3; ++i) {
offset += plane.normal[i] * (vertex[i] - plane.origin[i]);
}
return offset;
}
typedef std::map<Edge, int> CrossingFaceMap;
static CrossingFaceMap CrossingFaces(const std::vector<Face>& faces,
const std::vector<FloatType>& vertexOffsets) {
std::vector<std::pair<Edge, int>> crossingFaces;
for (const auto& face : faces) {
const bool edge1crosses = vertexOffsets[face[0]] * vertexOffsets[face[1]] < 0;
const bool edge2crosses = vertexOffsets[face[1]] * vertexOffsets[face[2]] < 0;
if (edge1crosses || edge2crosses) {
int oddVertex = edge2crosses - edge1crosses + 1;
const bool oddIsHigher = vertexOffsets[face[oddVertex]] > 0;
int v0 = oddVertex + 1 + oddIsHigher;
if (v0 > 2) {
v0 -= 3;
}
int v2 = oddVertex + 2 - oddIsHigher;
if (v2 > 2) {
v2 -= 3;
}
crossingFaces.push_back({
{ static_cast<int>(face[v0]), static_cast<int>(face[oddVertex]) },
static_cast<int>(face[v2]) });
}
}
return CrossingFaceMap(crossingFaces.begin(), crossingFaces.end());
}
static void AlignEdge(Edge& edge) {
if (edge.first > edge.second) {
std::swap(edge.first, edge.second);
}
}
static EdgePath GetEdgePath(CrossingFaceMap& crossingFaces) {
auto currentFace = crossingFaces.cbegin();
EdgePath edgePath({ currentFace->first });
int closingVertex(currentFace->second);
while (GetNextPoint(currentFace, crossingFaces)) {
edgePath.push_back(currentFace->first);
closingVertex = currentFace->second;
}
edgePath.push_back({ edgePath.back().second, closingVertex });
for (auto& edge : edgePath) {
AlignEdge(edge);
}
return edgePath;
}
static bool GetNextPoint(CrossingFaceMap::const_iterator& currentFace,
CrossingFaceMap& crossingFaces) {
auto nextKey(std::make_pair(currentFace->first.second, currentFace->second));
crossingFaces.erase(currentFace);
currentFace = crossingFaces.find(nextKey);
if (currentFace == crossingFaces.end()) {
std::swap(nextKey.first, nextKey.second);
currentFace = crossingFaces.find(nextKey);
}
return currentFace != crossingFaces.end();
}
template <typename Type>
static bool GetStartingItem(const std::vector<Type>& items,
std::vector<bool>& usedItems, Type& startItem) {
for (size_t i(0); i < items.size(); ++i) {
if (!usedItems[i]) {
startItem = items[i];
usedItems[i] = true;
return true;
}
}
return false;
}
static bool InsertConnectingEdgePath(const std::vector<EdgePath>& edgePaths,
std::vector<bool>& usedPaths, EdgePath& currentChain) {
int iPath(-1);
for (auto& path : edgePaths) {
++iPath;
if (usedPaths[iPath]) {
continue;
}
if (path.front() == currentChain.back())
currentChain.insert(currentChain.end(), path.begin() + 1, path.end());
else if (path.back() == currentChain.back())
currentChain.insert(currentChain.end(), path.rbegin() + 1, path.rend());
else if (path.back() == currentChain.front())
currentChain.insert(currentChain.begin(), path.begin(), path.end() - 1);
else if (path.front() == currentChain.front())
currentChain.insert(currentChain.begin(), path.rbegin(), path.rend() - 1);
else continue;
usedPaths[iPath] = true;
return true;
}
return false;
}
static void ChainEdgePaths(std::vector<EdgePath>& edgePaths) {
if (edgePaths.size() < 1) {
return;
}
std::vector<bool> usedPaths(edgePaths.size());
std::vector<EdgePath> chainedPaths;
EdgePath chain;
while (GetStartingItem(edgePaths, usedPaths, chain)) {
while (InsertConnectingEdgePath(edgePaths, usedPaths, chain)) {}
chainedPaths.push_back(chain);
}
edgePaths = chainedPaths;
}
static std::vector<Edge> FreeEdges(const std::vector<Face>& faces,
const std::vector<FloatType>& vertexOffsets) {
std::map<Edge, int> edgeFaceCount;
for (const auto& face : faces) {
for (int iEdge(0); iEdge < 3; ++iEdge) {
const auto& v0(face[iEdge]);
const auto& v1(face[(iEdge + 1) % 3]);
if (vertexOffsets[v0] < 0 && vertexOffsets[v1] < 0) {
continue;
}
Edge thisEdge(v0, v1);
AlignEdge(thisEdge);
edgeFaceCount[thisEdge]++;
}
}
std::vector<Edge> freeEdges;
for (const auto& edge : edgeFaceCount) {
if (edge.second == 1) {
freeEdges.push_back(edge.first);
}
}
return freeEdges;
}
struct FreeEdgePath {
Edge StartEdge, EndEdge;
std::vector<int> vertices;
bool isUsed = false;
};
static std::vector<EdgePath> FreeEdgePaths(const std::vector<Edge>& freeEdges,
const std::vector<FloatType> vertexOffsets) {
std::vector<EdgePath> freeEdgePaths;
std::vector<bool> usedEdges(freeEdges.size());
Edge edge;
while (GetStartingItem(freeEdges, usedEdges, edge)) {
FreeEdgePath path;
path.StartEdge = path.EndEdge = Edge(-1, -1);
if (vertexOffsets[edge.first] > 0) {
path.vertices.push_back(edge.first);
if (vertexOffsets[edge.second] < 0) {
path.StartEdge = edge;
}
}
if (vertexOffsets[edge.second] > 0) {
path.vertices.push_back(edge.second);
if (vertexOffsets[edge.first] < 0) {
path.EndEdge = edge;
}
}
while (ExtendFreeEdgePath(path, freeEdges, usedEdges, vertexOffsets)) {
// chain getting longer, available edges getting smaller
}
EdgePath edgePath;
if (path.StartEdge.first >= 0) {
edgePath.push_back(path.StartEdge);
}
for (const int iVert : path.vertices) {
edgePath.push_back(std::make_pair(iVert, iVert));
}
if (path.EndEdge.first >= 0) {
edgePath.push_back(path.EndEdge);
}
freeEdgePaths.push_back(edgePath);
}
return freeEdgePaths;
}
static bool ExtendFreeEdgePath(FreeEdgePath& path, const std::vector<Edge>& freeEdges,
std::vector<bool>& usedFreeEdges, const std::vector<FloatType> vertexOffsets) {
for (size_t iEdge(0); iEdge < freeEdges.size(); iEdge++) {
if (usedFreeEdges[iEdge]) {
continue;
}
const auto& edge(freeEdges[iEdge]);
const bool edgeCrosses = vertexOffsets[edge.first] * vertexOffsets[edge.second] < 0;
// try adding to the back of the chain
if (path.EndEdge.first == -1) {
const bool append(path.vertices.back() == edge.first);
const bool appendReverse(path.vertices.back() == edge.second);
if (append || appendReverse) {
if (edgeCrosses) {
path.EndEdge = edge;
}
else {
path.vertices.push_back(append ? edge.second : edge.first);
}
usedFreeEdges[iEdge] = true;
return true;
}
}
// try adding to the front of the chain
if (path.StartEdge.first == -1) {
const bool prepend(path.vertices.front() == edge.first);
const bool prependReverse(path.vertices.front() == edge.second);
if (prepend || prependReverse) {
if (edgeCrosses) {
path.StartEdge = edge;
}
else {
path.vertices.insert(path.vertices.begin(),
prepend ? edge.second : edge.first);
}
usedFreeEdges[iEdge] = true;
return true;
}
}
}
return false;
}
};
private:
// constructor is private, use the mesh class Interect and Clip methods
MeshPlaneIntersect() {};
};