-
-
Notifications
You must be signed in to change notification settings - Fork 901
Expand file tree
/
Copy pathConversionResult.h
More file actions
427 lines (365 loc) · 13.7 KB
/
ConversionResult.h
File metadata and controls
427 lines (365 loc) · 13.7 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#ifndef CONVERSIONRESULT_H
#define CONVERSIONRESULT_H
#include "../ifcgeom/IfcGeomRenderStyles.h"
#include "../ifcgeom/ConversionSettings.h"
#include "../ifcgeom/taxonomy.h"
#include <memory>
#include <vector>
#include <unordered_map>
struct EdgeKey {
int v1, v2;
// These are not part of the hash or equality,
// but retained to easily created a directed
// graph of the original boundary edges. Since
// the boundary edges are exactly those with
// count=1 we don't need to worry about
// conflicting original vertex indices.
int ov1, ov2;
EdgeKey(int a, int b)
: ov1(a)
, ov2(b)
{
if (a < b) {
v1 = a;
v2 = b;
} else {
v1 = b;
v2 = a;
}
}
bool operator==(const EdgeKey& other) const {
return v1 == other.v1 && v2 == other.v2;
}
};
namespace std {
template <>
struct hash<EdgeKey> {
std::size_t operator()(const EdgeKey& ek) const {
return std::hash<int>()(ek.v1) ^ std::hash<int>()(ek.v2);
}
};
}
namespace IfcGeom {
namespace Representation {
class IFC_GEOM_API Triangulation;
}
template <typename T>
constexpr T add_(T a, T b) {
return a + b;
}
template <typename T>
constexpr T subtract_(T a, T b) {
return a - b;
}
template <typename T>
constexpr T multiply_(T a, T b) {
return a * b;
}
template <typename T>
constexpr T divide_(T a, T b) {
return a / b;
}
template <typename T>
constexpr bool equals_(T a, T b) {
return a == b;
}
template <typename T>
constexpr bool less_than_(T a, T b) {
return a < b;
}
template <typename T>
constexpr T negate_(T a) {
return -a;
}
class IFC_GEOM_API OpaqueNumber {
public:
virtual double to_double() const = 0;
virtual std::string to_string() const = 0;
virtual ~OpaqueNumber() {}
virtual OpaqueNumber* operator+(OpaqueNumber* other) const = 0;
virtual OpaqueNumber* operator-(OpaqueNumber* other) const = 0;
virtual OpaqueNumber* operator*(OpaqueNumber* other) const = 0;
virtual OpaqueNumber* operator/(OpaqueNumber* other) const = 0;
virtual bool operator==(OpaqueNumber* other) const = 0;
virtual bool operator<(OpaqueNumber* other) const = 0;
virtual OpaqueNumber* operator-() const = 0;
virtual OpaqueNumber* clone() const = 0;
};
// @todo this can simply be a template class, to remove the need for the NumberEpeck in CGAL kernel.
class IFC_GEOM_API NumberNativeDouble : public OpaqueNumber {
private:
double value_;
template <double (*Fn)(double, double)>
OpaqueNumber* binary_op(OpaqueNumber* other) const {
auto nnd = dynamic_cast<NumberNativeDouble*>(other);
if (nnd) {
return new NumberNativeDouble(Fn(value_, nnd->value_));
} else {
return nullptr;
}
}
template <bool(*Fn)(double, double)>
bool binary_op_bool(OpaqueNumber* other) const {
auto nnd = dynamic_cast<NumberNativeDouble*>(other);
if (nnd) {
return Fn(value_, nnd->value_);
} else {
return false;
}
}
template <double(*Fn)(double)>
OpaqueNumber* unary_op() const {
return new NumberNativeDouble(Fn(value_));
}
public:
NumberNativeDouble(double v)
: value_(v) {}
virtual double to_double() const {
return value_;
}
virtual std::string to_string() const;
virtual OpaqueNumber* operator+(OpaqueNumber* other) const {
return binary_op<add_<double>>(other);
}
virtual OpaqueNumber* operator-(OpaqueNumber* other) const {
return binary_op<subtract_<double>>(other);
}
virtual OpaqueNumber* operator*(OpaqueNumber* other) const {
return binary_op<multiply_<double>>(other);
}
virtual OpaqueNumber* operator/(OpaqueNumber* other) const {
return binary_op<divide_<double>>(other);
}
virtual bool operator==(OpaqueNumber* other) const {
return binary_op_bool<equals_<double>>(other);
}
virtual bool operator<(OpaqueNumber* other) const {
return binary_op_bool<less_than_<double>>(other);
}
virtual OpaqueNumber* operator-() const {
return unary_op<negate_<double>>();
}
virtual OpaqueNumber* clone() const {
return new NumberNativeDouble(value_);
}
};
template <size_t N>
struct IFC_GEOM_API OpaqueCoordinate {
private:
std::array<OpaqueNumber*, N> values;
static void copy_(std::array<OpaqueNumber*, N>& dest, const std::array<OpaqueNumber*, N>& src) {
for (size_t i = 0; i < N; ++i) {
dest[i] = (src[i] != nullptr) ? src[i]->clone() : nullptr;
}
}
public:
template <typename... Args>
OpaqueCoordinate(Args... args) {
static_assert(sizeof...(args) == N, "Incorrect number of arguments provided");
init_<0>(args...);
}
OpaqueCoordinate() {
for (auto it = values.begin(); it != values.end(); ++it) {
*it = nullptr;
}
}
OpaqueCoordinate(const OpaqueCoordinate& other) {
copy_(values, other.values);
}
OpaqueCoordinate& operator=(const OpaqueCoordinate& other) {
if (this != &other) {
copy_(values, other.values);
}
return *this;
}
~OpaqueCoordinate() {
for (auto it = values.begin(); it != values.end(); ++it) {
delete *it;
}
}
OpaqueNumber* get(size_t i) const {
if (i >= N) {
return nullptr;
}
return values[i];
}
void set(size_t i, OpaqueNumber* n) {
if (i < N) {
values[i] = n->clone();
}
}
private:
template <size_t Index, typename... Args>
void init_(OpaqueNumber* value, Args... args) {
values[Index] = value;
if constexpr (Index + 1 < N) {
init_<Index + 1>(args...);
}
}
};
class IFC_GEOM_API ConversionResultShape {
public:
virtual void Triangulate(ifcopenshell::geometry::Settings settings, const ifcopenshell::geometry::taxonomy::matrix4& place, Representation::Triangulation* t, int item_id, int surface_style_id) const = 0;
IfcGeom::Representation::Triangulation* Triangulate(const ifcopenshell::geometry::Settings& settings) const;
virtual void Serialize(const ifcopenshell::geometry::taxonomy::matrix4& place, std::string&) const = 0;
virtual int surface_genus() const = 0;
virtual bool is_manifold() const = 0;
virtual int num_vertices() const = 0;
virtual int num_edges() const = 0;
virtual int num_faces() const = 0;
// @todo choose one prototype
virtual double bounding_box(void*&) const = 0;
// @todo this must be something with a virtual dtor so that we can delete it.
virtual std::pair<OpaqueCoordinate<3>, OpaqueCoordinate<3>> bounding_box() const = 0;
virtual void set_box(void* b) = 0;
virtual OpaqueNumber* length() = 0;
virtual OpaqueNumber* area() = 0;
virtual OpaqueNumber* volume() = 0;
virtual OpaqueCoordinate<3> position() = 0;
virtual OpaqueCoordinate<3> axis() = 0;
virtual OpaqueCoordinate<4> plane_equation() = 0;
virtual std::vector<ConversionResultShape*> convex_decomposition() = 0;
virtual ConversionResultShape* halfspaces() = 0;
virtual ConversionResultShape* box() = 0;
virtual ConversionResultShape* solid() = 0;
virtual ConversionResultShape* wrap_in_compound() = 0;
virtual std::vector<ConversionResultShape*> vertices() = 0;
virtual std::vector<ConversionResultShape*> edges() = 0;
virtual std::vector<ConversionResultShape*> facets() = 0;
virtual ConversionResultShape* add(ConversionResultShape*) = 0;
virtual ConversionResultShape* subtract(ConversionResultShape*) = 0;
virtual ConversionResultShape* intersect(ConversionResultShape*) = 0;
virtual ConversionResultShape* concat(ConversionResultShape*) = 0;
virtual void map(OpaqueCoordinate<4>& from, OpaqueCoordinate<4>& to) = 0;
virtual void map(const std::vector<OpaqueCoordinate<4>>& from, const std::vector<OpaqueCoordinate<4>>& to) = 0;
virtual ConversionResultShape* moved(ifcopenshell::geometry::taxonomy::matrix4::ptr) const = 0;
virtual bool surface_area_along_direction(double tol, const ifcopenshell::geometry::taxonomy::matrix4::ptr&, double& along_x, double& along_y, double& along_z) const = 0;
virtual ~ConversionResultShape() {}
};
class IFC_GEOM_API ConversionResult {
private:
int id;
ifcopenshell::geometry::taxonomy::matrix4::ptr placement_;
std::shared_ptr<ConversionResultShape> shape_;
ifcopenshell::geometry::taxonomy::style::ptr style_;
public:
ConversionResult(int id, ifcopenshell::geometry::taxonomy::matrix4::ptr placement, ConversionResultShape* shape, ifcopenshell::geometry::taxonomy::style::ptr style)
: id(id), placement_(placement ? placement : ifcopenshell::geometry::taxonomy::make<ifcopenshell::geometry::taxonomy::matrix4>()), shape_(shape), style_(style)
{}
ConversionResult(int id, ifcopenshell::geometry::taxonomy::matrix4::ptr placement, ConversionResultShape* shape)
: id(id), placement_(placement ? placement : ifcopenshell::geometry::taxonomy::make<ifcopenshell::geometry::taxonomy::matrix4>()), shape_(shape)
{}
ConversionResult(int id, ConversionResultShape* shape, ifcopenshell::geometry::taxonomy::style::ptr style)
: id(id), placement_(ifcopenshell::geometry::taxonomy::make<ifcopenshell::geometry::taxonomy::matrix4>()), shape_(shape), style_(style)
{}
ConversionResult(int id, ConversionResultShape* shape)
: id(id), placement_(ifcopenshell::geometry::taxonomy::make<ifcopenshell::geometry::taxonomy::matrix4>()), shape_(shape)
{}
void append(ifcopenshell::geometry::taxonomy::matrix4::ptr trsf);
void prepend(ifcopenshell::geometry::taxonomy::matrix4::ptr trsf);
std::shared_ptr<ConversionResultShape> Shape() const { return shape_; }
ifcopenshell::geometry::taxonomy::matrix4::ptr Placement() const { return placement_; }
bool hasStyle() const { return !!style_; }
const ifcopenshell::geometry::taxonomy::style& Style() const { return *style_; }
ifcopenshell::geometry::taxonomy::style::ptr StylePtr() const { return style_; }
void setStyle(ifcopenshell::geometry::taxonomy::style::ptr newStyle) { style_ = newStyle; }
int ItemId() const { return id; }
ConversionResultShape* apply_transform(double unit_scale = 1.) const {
if (unit_scale != 1.) {
auto m = ifcopenshell::geometry::taxonomy::matrix4::ptr(placement_->clone_());
m->pre_multiply_scale(unit_scale);
return shape_->moved(m);
} else {
return shape_->moved(placement_);
}
}
};
typedef std::vector<ConversionResult> ConversionResults;
namespace util {
// @todo this is now moved to occt kernel, do we need something similar in cgal?
// bool flatten_shape_list(const IfcGeom::ConversionResults& shapes, TopoDS_Shape& result, bool fuse, double tol);
// Function to find boundary loops from triangles
template <typename NT>
std::vector<std::vector<int>> find_boundary_loops(const std::vector<NT>& positions, const std::vector<std::tuple<int, int, int>>& triangles) {
std::unordered_map<EdgeKey, int> edge_count;
// Count how many triangles each edge belongs to
for (const auto& triangle : triangles) {
int v1, v2, v3;
std::tie(v1, v2, v3) = triangle;
edge_count[{v1, v2}]++;
edge_count[{v2, v3}]++;
edge_count[{v3, v1}]++;
}
// Boundary edges have count 1
std::vector<EdgeKey> boundary_edges;
for (auto& p : edge_count) {
if (p.second == 1) {
boundary_edges.push_back(p.first);
}
}
// We retained original directed edges so we build
// a mapping out of these directed edges.
std::unordered_map<int, int> vertex_successors;
for (const auto& e : boundary_edges) {
vertex_successors[e.ov1] = e.ov2;
}
std::vector<std::vector<int>> loops;
while (!vertex_successors.empty()) {
loops.emplace_back();
auto it = vertex_successors.begin();
loops.back() = { it->first, it->second };
vertex_successors.erase(it);
int current = loops.back().back();
while (!vertex_successors.empty() && current != loops.back().front()) {
auto next = vertex_successors[current];
if (loops.back().front() != next) {
loops.back().push_back(next);
}
vertex_successors.erase(current);
current = next;
}
}
// Sort the loops by smallest x-coord of their constituent positions
// In order to put the outermost loop in front
if (loops.size() > 1) {
std::vector<std::pair<NT, size_t>> min_xs;
for (auto& l : loops) {
NT min_x = std::numeric_limits<double>::infinity();
for (auto& i : l) {
const auto& x = positions[i * 3];
if (x < min_x) {
min_x = x;
}
}
min_xs.push_back({ min_x, min_xs.size() });
}
std::sort(min_xs.begin(), min_xs.end());
decltype(loops) loops_copy;
for (auto& p : min_xs) {
loops_copy.emplace_back(std::move(loops[p.second]));
}
std::swap(loops, loops_copy);
}
return loops;
}
}
}
#endif