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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
#include "PolySetCGALEvaluator.h"
#include "cgal.h"
#include "cgalutils.h"
#include "polyset.h"
#include "CGALEvaluator.h"
#include "projectionnode.h"
#include "linearextrudenode.h"
#include "rotateextrudenode.h"
#include "cgaladvnode.h"
#include "rendernode.h"
#include "dxfdata.h"
#include "dxftess.h"
#include "module.h"
#include "printutils.h"
#include "openscad.h" // get_fragments_from_r()
#include <boost/foreach.hpp>
PolySetCGALEvaluator::PolySetCGALEvaluator(CGALEvaluator &cgalevaluator)
: PolySetEvaluator(cgalevaluator.getTree()), cgalevaluator(cgalevaluator)
{
}
PolySet *PolySetCGALEvaluator::evaluatePolySet(const ProjectionNode &node)
{
// Before projecting, union all children
CGAL_Nef_polyhedron sum;
BOOST_FOREACH (AbstractNode * v, node.getChildren()) {
if (v->modinst->isBackground()) continue;
CGAL_Nef_polyhedron N = this->cgalevaluator.evaluateCGALMesh(*v);
if (N.dim == 3) {
if (sum.empty()) sum = N.copy();
else sum += N;
}
}
if (sum.empty()) return NULL;
PolySet *ps = new PolySet();
ps->convexity = node.convexity;
ps->is2d = true;
// In cut mode, the model is intersected by a large but very thin box living on the
// XY plane.
if (node.cut_mode)
{
PolySet cube;
double infval = 1e8, eps = 0.1;
double x1 = -infval, x2 = +infval, y1 = -infval, y2 = +infval, z1 = 0, z2 = eps;
cube.append_poly(); // top
cube.append_vertex(x1, y1, z2);
cube.append_vertex(x2, y1, z2);
cube.append_vertex(x2, y2, z2);
cube.append_vertex(x1, y2, z2);
cube.append_poly(); // bottom
cube.append_vertex(x1, y2, z1);
cube.append_vertex(x2, y2, z1);
cube.append_vertex(x2, y1, z1);
cube.append_vertex(x1, y1, z1);
cube.append_poly(); // side1
cube.append_vertex(x1, y1, z1);
cube.append_vertex(x2, y1, z1);
cube.append_vertex(x2, y1, z2);
cube.append_vertex(x1, y1, z2);
cube.append_poly(); // side2
cube.append_vertex(x2, y1, z1);
cube.append_vertex(x2, y2, z1);
cube.append_vertex(x2, y2, z2);
cube.append_vertex(x2, y1, z2);
cube.append_poly(); // side3
cube.append_vertex(x2, y2, z1);
cube.append_vertex(x1, y2, z1);
cube.append_vertex(x1, y2, z2);
cube.append_vertex(x2, y2, z2);
cube.append_poly(); // side4
cube.append_vertex(x1, y2, z1);
cube.append_vertex(x1, y1, z1);
cube.append_vertex(x1, y1, z2);
cube.append_vertex(x1, y2, z2);
CGAL_Nef_polyhedron Ncube = this->cgalevaluator.evaluateCGALMesh(cube);
sum *= Ncube;
// FIXME: Instead of intersecting with a thin volume, we could intersect
// with a plane. This feels like a better solution. However, as the result
// of such an intersection isn't simple, we cannot convert the resulting
// Nef polyhedron to a Polyhedron using convertToPolyset() and we need
// another way of extracting the result. kintel 20120203.
// *sum.p3 = sum.p3->intersection(CGAL_Nef_polyhedron3::Plane_3(0, 0, 1, 0),
// CGAL_Nef_polyhedron3::PLANE_ONLY);
if (!sum.p3->is_simple()) {
PRINT("WARNING: Body of projection(cut = true) isn't valid 2-manifold! Modify your design..");
goto cant_project_non_simple_polyhedron;
}
PolySet *ps3 = sum.convertToPolyset();
if (!ps3) return NULL;
// Extract polygons in the XY plane, ignoring all other polygons
// FIXME: If the polyhedron is really thin, there might be unwanted polygons
// in the XY plane, causing the resulting 2D polygon to be self-intersection
// and cause a crash in CGALEvaluator::PolyReducer. The right solution is to
// filter these polygons here. kintel 20120203.
Grid2d<unsigned int> conversion_grid(GRID_COARSE);
for (size_t i = 0; i < ps3->polygons.size(); i++) {
for (size_t j = 0; j < ps3->polygons[i].size(); j++) {
double x = ps3->polygons[i][j][0];
double y = ps3->polygons[i][j][1];
double z = ps3->polygons[i][j][2];
if (z != 0)
goto next_ps3_polygon_cut_mode;
if (conversion_grid.align(x, y) == i+1)
goto next_ps3_polygon_cut_mode;
conversion_grid.data(x, y) = i+1;
}
ps->append_poly();
for (size_t j = 0; j < ps3->polygons[i].size(); j++) {
double x = ps3->polygons[i][j][0];
double y = ps3->polygons[i][j][1];
conversion_grid.align(x, y);
ps->insert_vertex(x, y);
}
next_ps3_polygon_cut_mode:;
}
delete ps3;
}
// In projection mode all the triangles are projected manually into the XY plane
else
{
if (!sum.p3->is_simple()) {
PRINT("WARNING: Body of projection(cut = false) isn't valid 2-manifold! Modify your design..");
goto cant_project_non_simple_polyhedron;
}
PolySet *ps3 = sum.convertToPolyset();
if (!ps3) return NULL;
CGAL_Nef_polyhedron np;
for (size_t i = 0; i < ps3->polygons.size(); i++)
{
int min_x_p = -1;
double min_x_val = 0;
for (size_t j = 0; j < ps3->polygons[i].size(); j++) {
double x = ps3->polygons[i][j][0];
if (min_x_p < 0 || x < min_x_val) {
min_x_p = j;
min_x_val = x;
}
}
int min_x_p1 = (min_x_p+1) % ps3->polygons[i].size();
int min_x_p2 = (min_x_p+ps3->polygons[i].size()-1) % ps3->polygons[i].size();
double ax = ps3->polygons[i][min_x_p1][0] - ps3->polygons[i][min_x_p][0];
double ay = ps3->polygons[i][min_x_p1][1] - ps3->polygons[i][min_x_p][1];
double at = atan2(ay, ax);
double bx = ps3->polygons[i][min_x_p2][0] - ps3->polygons[i][min_x_p][0];
double by = ps3->polygons[i][min_x_p2][1] - ps3->polygons[i][min_x_p][1];
double bt = atan2(by, bx);
double eps = 0.000001;
if (fabs(at - bt) < eps || (fabs(ax) < eps && fabs(ay) < eps) ||
(fabs(bx) < eps && fabs(by) < eps)) {
// this triangle is degenerated in projection
continue;
}
std::list<CGAL_Nef_polyhedron2::Point> plist;
for (size_t j = 0; j < ps3->polygons[i].size(); j++) {
double x = ps3->polygons[i][j][0];
double y = ps3->polygons[i][j][1];
CGAL_Nef_polyhedron2::Point p = CGAL_Nef_polyhedron2::Point(x, y);
if (at > bt)
plist.push_front(p);
else
plist.push_back(p);
}
// FIXME: Should the CGAL_Nef_polyhedron2 be cached?
if (np.empty()) {
np.dim = 2;
np.p2.reset(new CGAL_Nef_polyhedron2(plist.begin(), plist.end(), CGAL_Nef_polyhedron2::INCLUDED));
}
else {
(*np.p2) += CGAL_Nef_polyhedron2(plist.begin(), plist.end(), CGAL_Nef_polyhedron2::INCLUDED);
}
}
delete ps3;
DxfData *dxf = np.convertToDxfData();
dxf_tesselate(ps, *dxf, 0, true, false, 0);
dxf_border_to_ps(ps, *dxf);
delete dxf;
}
cant_project_non_simple_polyhedron:
return ps;
}
static void add_slice(PolySet *ps, const DxfData &dxf, DxfData::Path &path, double rot1, double rot2, double h1, double h2)
{
bool splitfirst = sin(rot2 - rot1) >= 0.0;
for (size_t j = 1; j < path.indices.size(); j++)
{
int k = j - 1;
double jx1 = dxf.points[path.indices[j]][0] * cos(rot1*M_PI/180) + dxf.points[path.indices[j]][1] * sin(rot1*M_PI/180);
double jy1 = dxf.points[path.indices[j]][0] * -sin(rot1*M_PI/180) + dxf.points[path.indices[j]][1] * cos(rot1*M_PI/180);
double jx2 = dxf.points[path.indices[j]][0] * cos(rot2*M_PI/180) + dxf.points[path.indices[j]][1] * sin(rot2*M_PI/180);
double jy2 = dxf.points[path.indices[j]][0] * -sin(rot2*M_PI/180) + dxf.points[path.indices[j]][1] * cos(rot2*M_PI/180);
double kx1 = dxf.points[path.indices[k]][0] * cos(rot1*M_PI/180) + dxf.points[path.indices[k]][1] * sin(rot1*M_PI/180);
double ky1 = dxf.points[path.indices[k]][0] * -sin(rot1*M_PI/180) + dxf.points[path.indices[k]][1] * cos(rot1*M_PI/180);
double kx2 = dxf.points[path.indices[k]][0] * cos(rot2*M_PI/180) + dxf.points[path.indices[k]][1] * sin(rot2*M_PI/180);
double ky2 = dxf.points[path.indices[k]][0] * -sin(rot2*M_PI/180) + dxf.points[path.indices[k]][1] * cos(rot2*M_PI/180);
if (splitfirst)
{
ps->append_poly();
if (path.is_inner) {
ps->append_vertex(kx1, ky1, h1);
ps->append_vertex(jx1, jy1, h1);
ps->append_vertex(jx2, jy2, h2);
} else {
ps->insert_vertex(kx1, ky1, h1);
ps->insert_vertex(jx1, jy1, h1);
ps->insert_vertex(jx2, jy2, h2);
}
ps->append_poly();
if (path.is_inner) {
ps->append_vertex(kx2, ky2, h2);
ps->append_vertex(kx1, ky1, h1);
ps->append_vertex(jx2, jy2, h2);
} else {
ps->insert_vertex(kx2, ky2, h2);
ps->insert_vertex(kx1, ky1, h1);
ps->insert_vertex(jx2, jy2, h2);
}
}
else
{
ps->append_poly();
if (path.is_inner) {
ps->append_vertex(kx1, ky1, h1);
ps->append_vertex(jx1, jy1, h1);
ps->append_vertex(kx2, ky2, h2);
} else {
ps->insert_vertex(kx1, ky1, h1);
ps->insert_vertex(jx1, jy1, h1);
ps->insert_vertex(kx2, ky2, h2);
}
ps->append_poly();
if (path.is_inner) {
ps->append_vertex(jx2, jy2, h2);
ps->append_vertex(kx2, ky2, h2);
ps->append_vertex(jx1, jy1, h1);
} else {
ps->insert_vertex(jx2, jy2, h2);
ps->insert_vertex(kx2, ky2, h2);
ps->insert_vertex(jx1, jy1, h1);
}
}
}
}
PolySet *PolySetCGALEvaluator::evaluatePolySet(const LinearExtrudeNode &node)
{
DxfData *dxf;
if (node.filename.empty())
{
// Before extruding, union all (2D) children nodes
// to a single DxfData, then tesselate this into a PolySet
CGAL_Nef_polyhedron sum;
BOOST_FOREACH (AbstractNode * v, node.getChildren()) {
if (v->modinst->isBackground()) continue;
CGAL_Nef_polyhedron N = this->cgalevaluator.evaluateCGALMesh(*v);
if (N.dim != 2) {
PRINT("ERROR: linear_extrude() is not defined for 3D child objects!");
}
else {
if (sum.empty()) sum = N.copy();
else sum += N;
}
}
if (sum.empty()) return NULL;
dxf = sum.convertToDxfData();;
} else {
dxf = new DxfData(node.fn, node.fs, node.fa, node.filename, node.layername, node.origin_x, node.origin_y, node.scale);
}
PolySet *ps = extrudeDxfData(node, *dxf);
delete dxf;
return ps;
}
PolySet *PolySetCGALEvaluator::extrudeDxfData(const LinearExtrudeNode &node, DxfData &dxf)
{
PolySet *ps = new PolySet();
ps->convexity = node.convexity;
double h1, h2;
if (node.center) {
h1 = -node.height/2.0;
h2 = +node.height/2.0;
} else {
h1 = 0;
h2 = node.height;
}
bool first_open_path = true;
for (size_t i = 0; i < dxf.paths.size(); i++)
{
if (dxf.paths[i].is_closed)
continue;
if (first_open_path) {
PRINTB("WARNING: Open paths in dxf_linear_extrude(file = \"%s\", layer = \"%s\"):",
node.filename % node.layername);
first_open_path = false;
}
PRINTB(" %9.5f %10.5f ... %10.5f %10.5f",
(dxf.points[dxf.paths[i].indices.front()][0] / node.scale + node.origin_x) %
(dxf.points[dxf.paths[i].indices.front()][1] / node.scale + node.origin_y) %
(dxf.points[dxf.paths[i].indices.back()][0] / node.scale + node.origin_x) %
(dxf.points[dxf.paths[i].indices.back()][1] / node.scale + node.origin_y));
}
if (node.has_twist)
{
dxf_tesselate(ps, dxf, 0, false, true, h1);
dxf_tesselate(ps, dxf, node.twist, true, true, h2);
for (int j = 0; j < node.slices; j++)
{
double t1 = node.twist*j / node.slices;
double t2 = node.twist*(j+1) / node.slices;
double g1 = h1 + (h2-h1)*j / node.slices;
double g2 = h1 + (h2-h1)*(j+1) / node.slices;
for (size_t i = 0; i < dxf.paths.size(); i++)
{
if (!dxf.paths[i].is_closed)
continue;
add_slice(ps, dxf, dxf.paths[i], t1, t2, g1, g2);
}
}
}
else
{
dxf_tesselate(ps, dxf, 0, false, true, h1);
dxf_tesselate(ps, dxf, 0, true, true, h2);
for (size_t i = 0; i < dxf.paths.size(); i++)
{
if (!dxf.paths[i].is_closed)
continue;
add_slice(ps, dxf, dxf.paths[i], 0, 0, h1, h2);
}
}
return ps;
}
PolySet *PolySetCGALEvaluator::evaluatePolySet(const RotateExtrudeNode &node)
{
DxfData *dxf;
if (node.filename.empty())
{
// Before extruding, union all (2D) children nodes
// to a single DxfData, then tesselate this into a PolySet
CGAL_Nef_polyhedron sum;
BOOST_FOREACH (AbstractNode * v, node.getChildren()) {
if (v->modinst->isBackground()) continue;
CGAL_Nef_polyhedron N = this->cgalevaluator.evaluateCGALMesh(*v);
if (N.dim != 2) {
PRINT("ERROR: rotate_extrude() is not defined for 3D child objects!");
}
else {
if (sum.empty()) sum = N.copy();
else sum += N;
}
}
if (sum.empty()) return NULL;
dxf = sum.convertToDxfData();
} else {
dxf = new DxfData(node.fn, node.fs, node.fa, node.filename, node.layername, node.origin_x, node.origin_y, node.scale);
}
PolySet *ps = rotateDxfData(node, *dxf);
delete dxf;
return ps;
}
PolySet *PolySetCGALEvaluator::evaluatePolySet(const CgaladvNode &node)
{
CGAL_Nef_polyhedron N = this->cgalevaluator.evaluateCGALMesh(node);
PolySet *ps = NULL;
if (!N.empty()) {
ps = N.convertToPolyset();
if (ps) ps->convexity = node.convexity;
}
return ps;
}
PolySet *PolySetCGALEvaluator::evaluatePolySet(const RenderNode &node)
{
CGAL_Nef_polyhedron N = this->cgalevaluator.evaluateCGALMesh(node);
PolySet *ps = NULL;
if (!N.empty()) {
if (N.dim == 3 && !N.p3->is_simple()) {
PRINT("WARNING: Body of render() isn't valid 2-manifold!");
}
else {
ps = N.convertToPolyset();
if (ps) ps->convexity = node.convexity;
}
}
return ps;
}
PolySet *PolySetCGALEvaluator::rotateDxfData(const RotateExtrudeNode &node, DxfData &dxf)
{
PolySet *ps = new PolySet();
ps->convexity = node.convexity;
for (size_t i = 0; i < dxf.paths.size(); i++)
{
double max_x = 0;
for (size_t j = 0; j < dxf.paths[i].indices.size(); j++) {
max_x = fmax(max_x, dxf.points[dxf.paths[i].indices[j]][0]);
}
int fragments = get_fragments_from_r(max_x, node.fn, node.fs, node.fa);
double ***points;
points = new double**[fragments];
for (int j=0; j < fragments; j++) {
points[j] = new double*[dxf.paths[i].indices.size()];
for (size_t k=0; k < dxf.paths[i].indices.size(); k++)
points[j][k] = new double[3];
}
for (int j = 0; j < fragments; j++) {
double a = (j*2*M_PI) / fragments;
for (size_t k = 0; k < dxf.paths[i].indices.size(); k++) {
if (dxf.points[dxf.paths[i].indices[k]][0] == 0) {
points[j][k][0] = 0;
points[j][k][1] = 0;
} else {
points[j][k][0] = dxf.points[dxf.paths[i].indices[k]][0] * sin(a);
points[j][k][1] = dxf.points[dxf.paths[i].indices[k]][0] * cos(a);
}
points[j][k][2] = dxf.points[dxf.paths[i].indices[k]][1];
}
}
for (int j = 0; j < fragments; j++) {
int j1 = j + 1 < fragments ? j + 1 : 0;
for (size_t k = 0; k < dxf.paths[i].indices.size(); k++) {
int k1 = k + 1 < dxf.paths[i].indices.size() ? k + 1 : 0;
if (points[j][k][0] != points[j1][k][0] ||
points[j][k][1] != points[j1][k][1] ||
points[j][k][2] != points[j1][k][2]) {
ps->append_poly();
ps->append_vertex(points[j ][k ][0],
points[j ][k ][1], points[j ][k ][2]);
ps->append_vertex(points[j1][k ][0],
points[j1][k ][1], points[j1][k ][2]);
ps->append_vertex(points[j ][k1][0],
points[j ][k1][1], points[j ][k1][2]);
}
if (points[j][k1][0] != points[j1][k1][0] ||
points[j][k1][1] != points[j1][k1][1] ||
points[j][k1][2] != points[j1][k1][2]) {
ps->append_poly();
ps->append_vertex(points[j ][k1][0],
points[j ][k1][1], points[j ][k1][2]);
ps->append_vertex(points[j1][k ][0],
points[j1][k ][1], points[j1][k ][2]);
ps->append_vertex(points[j1][k1][0],
points[j1][k1][1], points[j1][k1][2]);
}
}
}
for (int j=0; j < fragments; j++) {
for (size_t k=0; k < dxf.paths[i].indices.size(); k++)
delete[] points[j][k];
delete[] points[j];
}
delete[] points;
}
return ps;
}
|