-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscad.cc
More file actions
454 lines (390 loc) · 11.8 KB
/
Copy pathscad.cc
File metadata and controls
454 lines (390 loc) · 11.8 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
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
#include "scad.h"
// Windows!
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include <math.h>
#include <cstdio>
#include <memory>
#include <string>
#include <vector>
namespace scad {
const char* BoolStr(bool b) {
return b ? "true" : "false";
}
void WriteIndent(std::FILE* file, int indent_level) {
for (int i = 0; i < indent_level * kTabSize; ++i) {
fputc(' ', file);
}
}
void WriteComposite(std::FILE* file,
const std::function<void(std::FILE*)>& write_name,
const std::vector<Shape>& shapes,
int indent_level) {
WriteIndent(file, indent_level);
write_name(file);
fprintf(file, " {\n");
for (const Shape& s : shapes) {
s.AppendScad(file, indent_level + 1);
}
WriteIndent(file, indent_level);
fprintf(file, "}\n");
}
Shape Shape::Composite(const std::function<void(std::FILE*)>& write_name,
const std::vector<Shape>& shapes) {
return Shape([=](std::FILE* file, int indent_level) {
WriteComposite(file, write_name, shapes, indent_level);
});
}
Shape Shape::LiteralComposite(const std::string& name, const std::vector<Shape>& shapes) {
return Shape([=](std::FILE* file, int indent_level) {
WriteComposite(
file, [=](std::FILE*) { fprintf(file, "%s", name.c_str()); }, shapes, indent_level);
});
}
Shape Shape::Primitive(const std::function<void(std::FILE*)>& scad_writer) {
return Shape([=](std::FILE* file, int indent_level) {
WriteIndent(file, indent_level);
scad_writer(file);
fprintf(file, "\n");
});
}
Shape Shape::LiteralPrimitive(const std::string& primitive) {
return Primitive([=](std::FILE* file) { fprintf(file, "%s", primitive.c_str()); });
}
Shape Cube(const CubeParams& params) {
return Shape::Primitive([=](std::FILE* file) {
fprintf(file,
"cube (size = [ %.3f, %.3f, %.3f], center = %s);",
params.x,
params.y,
params.z,
BoolStr(params.center));
});
}
Shape Cube(double x, double y, double z, bool center) {
CubeParams params;
params.x = x;
params.y = y;
params.z = z;
params.center = center;
return Cube(params);
}
Shape Cube(double size, bool center) {
return Cube(size, size, size, center);
}
Shape Square(const SquareParams& params) {
return Shape::Primitive([=](std::FILE* file) {
fprintf(file,
"square (size = [%.3f, %.3f], center = %s);",
params.x,
params.y,
BoolStr(params.center));
});
}
Shape Square(double x, double y, bool center) {
SquareParams params;
params.x = x;
params.y = y;
params.center = center;
return Square(params);
}
Shape Square(double size, bool center) {
return Square(size, size, center);
}
Shape Sphere(const SphereParams& params) {
return Shape::Primitive([=](std::FILE* file) {
fprintf(file, "sphere (r = %.3f", params.r);
if (params.fs.has_value()) {
fprintf(file, ", $fs = %.3f", params.fs.value());
}
if (params.fn.has_value()) {
fprintf(file, ", $fn = %.3f", params.fn.value());
}
if (params.fa.has_value()) {
fprintf(file, ", $fa = %.3f", params.fa.value());
}
fprintf(file, ");");
});
}
Shape Sphere(double radius) {
SphereParams params;
params.r = radius;
return Sphere(params);
}
Shape Sphere(double radius, double fn) {
SphereParams params;
params.r = radius;
params.fn = fn;
return Sphere(params);
}
Shape Circle(const CircleParams& params) {
return Shape::Primitive([=](std::FILE* file) {
fprintf(file, "circle (r = %.3f", params.r);
if (params.fs.has_value()) {
fprintf(file, ", $fs = %.3f", params.fs.value());
}
if (params.fn.has_value()) {
fprintf(file, ", $fn = %.3f", params.fn.value());
}
if (params.fa.has_value()) {
fprintf(file, ", $fa = %.3f", params.fa.value());
}
fprintf(file, ");");
});
}
Shape Circle(double radius) {
CircleParams params;
params.r = radius;
return Circle(params);
}
Shape Circle(double radius, double fn) {
CircleParams params;
params.r = radius;
params.fn = fn;
return Circle(params);
}
Shape Cylinder(const CylinderParams& params) {
return Shape::Primitive([=](std::FILE* file) {
fprintf(file,
"cylinder(h = %.3f, r1 = %.3f, r2 = %.3f, center = %s",
params.h,
params.r1,
params.r2,
BoolStr(params.center));
if (params.fn.has_value()) {
fprintf(file, ", $fn = %.3f", params.fn.value());
}
fprintf(file, ");");
});
}
Shape Cylinder(double height, double radius, Optional<double> fn) {
CylinderParams params;
params.h = height;
params.r1 = radius;
params.r2 = radius;
params.fn = fn;
return Cylinder(params);
}
Shape Polygon(const std::vector<Point2d>& points) {
return Shape::Primitive([=](std::FILE* file) {
fprintf(file, "polygon (points = [");
for (size_t i = 0; i < points.size(); ++i) {
const Point2d& p = points[i];
if (i != 0) {
fputc(',', file);
}
fprintf(file, "[%.3f, %.3f]", p.x, p.y);
}
fprintf(file, "]);");
});
}
Shape RegularPolygon(int n, double r) {
std::vector<Point2d> points;
for (int i = 0; i < n; ++i) {
double step = (2.0 * M_PI) / n;
points.push_back({r * sin(step * i), r * cos(step * i)});
}
return Polygon(points);
}
Shape Polyhedron(const std::vector<Point3d>& points,
const std::vector<std::vector<int>>& faces,
int convexity) {
return Shape::Primitive([=](std::FILE* file) {
fprintf(file, "polyhedron (points = [");
for (size_t i = 0; i < points.size(); ++i) {
const Point3d& p = points[i];
if (i > 0) {
fputc(',', file);
}
fprintf(file, "[%.3f, %.3f, %.3f]", p.x, p.y, p.z);
}
fprintf(file, "], faces = [");
for (size_t i = 0; i < faces.size(); ++i) {
if (i > 0) {
fputc(',', file);
}
const auto& face = faces[i];
fprintf(file, "[");
for (size_t f = 0; f < face.size(); ++f) {
if (f != 0) {
fputc(',', file);
}
fprintf(file, "%d", face[f]);
}
fprintf(file, "]");
}
fprintf(file, "], convexity = %d);", convexity);
});
}
Shape HullAll(const std::vector<Shape>& shapes) {
return Shape::LiteralComposite("hull ()", shapes);
}
Shape UnionAll(const std::vector<Shape>& shapes) {
return Shape::LiteralComposite("union ()", shapes);
}
Shape DifferenceAll(const std::vector<Shape>& shapes) {
return Shape::LiteralComposite("difference ()", shapes);
}
Shape IntersectionAll(const std::vector<Shape>& shapes) {
return Shape::LiteralComposite("intersection ()", shapes);
}
Shape Shape::Translate(double x, double y, double z) const {
auto write_name = [=](std::FILE* file) {
fprintf(file, "translate ([%.3f, %.3f, %.3f])", x, y, z);
};
return Shape::Composite(write_name, {*this});
}
Shape Shape::TranslateX(double x) const {
return Translate(x, 0, 0);
}
Shape Shape::TranslateY(double y) const {
return Translate(0, y, 0);
}
Shape Shape::TranslateZ(double z) const {
return Translate(0, 0, z);
}
Shape Shape::Mirror(double x, double y, double z) const {
auto write_name = [=](std::FILE* file) { fprintf(file, "mirror ([%.3f, %.3f, %.3f])", x, y, z); };
return Shape::Composite(write_name, {*this});
}
Shape Shape::Rotate(double rx, double ry, double rz) const {
auto write_name = [=](std::FILE* file) {
fprintf(file, "rotate ([%.3f, %.3f, %.3f])", rx, ry, rz);
};
return Shape::Composite(write_name, {*this});
}
Shape Shape::Rotate(double degrees, double x, double y, double z) const {
auto write_name = [=](std::FILE* file) {
fprintf(file, "rotate (a = %.3f, v = [%.3f, %.3f, %.3f])", degrees, x, y, z);
};
return Shape::Composite(write_name, {*this});
}
Shape Shape::RotateX(double degrees) const {
return Rotate(degrees, 1, 0, 0);
}
Shape Shape::RotateY(double degrees) const {
return Rotate(degrees, 0, 1, 0);
}
Shape Shape::RotateZ(double degrees) const {
return Rotate(degrees, 0, 0, 1);
}
Shape Shape::LinearExtrude(const LinearExtrudeParams& params) const {
auto write_name = [=](std::FILE* file) {
fprintf(file,
"linear_extrude (height = %.3f, center = %s, convexity = %.3f, "
"twist = %.3f, slices = %d, scale = %.3f)",
params.height,
BoolStr(params.center),
params.convexity,
params.twist,
params.slices,
params.scale);
};
return Shape::Composite(write_name, {*this});
}
Shape Shape::LinearExtrude(double height) const {
LinearExtrudeParams params;
params.height = height;
return LinearExtrude(params);
}
Shape Shape::Color(double r, double g, double b, double a) const {
auto write_name = [=](std::FILE* file) {
fprintf(file, "color (c = [%.3f, %.3f, %.3f, %.3f])", r, g, b, a);
};
return Shape::Composite(write_name, {*this});
}
Shape Shape::Color(const std::string& color, double a) const {
auto write_name = [=](std::FILE* file) { fprintf(file, "color (\"%s\", %f)", color.c_str(), a); };
return Shape::Composite(write_name, {*this});
}
Shape Shape::Alpha(double a) const {
auto write_name = [=](std::FILE* file) { fprintf(file, "color (alpha = %.3f)", a); };
return Shape::Composite(write_name, {*this});
}
Shape Shape::Scale(double x, double y, double z) const {
auto write_name = [=](std::FILE* file) { fprintf(file, "scale ([%.3f, %.3f, %.3f])", x, y, z); };
return Shape::Composite(write_name, {*this});
}
Shape Shape::Scale(double s) const {
return Scale(s, s, s);
}
Shape Shape::OffsetRadius(double r, bool chamfer) const {
auto write_name = [=](std::FILE* file) {
fprintf(file, "offset (r = %.3f, chamfer = %s)", r, BoolStr(chamfer));
};
return Shape::Composite(write_name, {*this});
}
Shape Shape::OffsetDelta(double delta, bool chamfer) const {
auto write_name = [=](std::FILE* file) {
fprintf(file, "offset (delta = %.3f, chamfer = %s)", delta, BoolStr(chamfer));
};
return Shape::Composite(write_name, {*this});
}
Shape Shape::Subtract(const Shape& other) const {
return Difference(*this, other);
}
Shape Shape::operator-(const Shape& other) const {
return Subtract(other);
}
Shape& Shape::operator-=(const Shape& other) {
*this = Subtract(other);
return *this;
}
Shape Shape::Add(const Shape& other) const {
return Union(*this, other);
}
Shape Shape::operator+(const Shape& other) const {
return Add(other);
}
Shape& Shape::operator+=(const Shape& other) {
*this = Add(other);
return *this;
}
Shape Shape::Comment(const std::string& comment) const {
Shape shape_copy = *this;
return Shape([=](std::FILE* file, int indent_level) {
WriteIndent(file, indent_level);
fprintf(file, "/* %s */\n", comment.c_str());
shape_copy.AppendScad(file, indent_level);
});
}
Shape Shape::Projection(bool cut) const {
auto write_name = [=](std::FILE* file) { fprintf(file, "projection (cut = %s)", BoolStr(cut)); };
return Shape::Composite(write_name, {*this});
}
void Shape::AppendScad(std::FILE* file, int indent_level) const {
if (!scad_) {
return;
}
(*scad_)(file, indent_level);
}
void Shape::WriteToFile(const std::string& file_name) const {
std::FILE* file = nullptr;
bool opened = false;
#ifdef _WIN32
opened = fopen_s(&file, file_name.c_str(), "w") == 0;
#else
file = std::fopen(file_name.c_str(), "w");
opened = file != nullptr;
#endif
if (!opened || file == nullptr) {
fprintf(stderr, "Could not open file %s\n", file_name.c_str());
return;
}
AppendScad(file, 0);
std::fclose(file);
}
Shape Import(const std::string& file_name, int convexity) {
return Shape::Primitive([=](std::FILE* file) {
if (convexity > 0) {
fprintf(file, "import (file = \"%s\", convexity = %d);", file_name.c_str(), convexity);
} else {
fprintf(file, "import (file = \"%s\");", file_name.c_str());
}
});
}
Shape Minkowski(const Shape& first, const Shape& second) {
return Shape::LiteralComposite("minkowski ()", {first, second});
}
} // namespace scad