-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscad.h
More file actions
226 lines (180 loc) · 6.98 KB
/
Copy pathscad.h
File metadata and controls
226 lines (180 loc) · 6.98 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
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <vector>
#if defined(__GNUC__) || defined(__GNUG__)
#define SCAD_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
#define SCAD_WARN_UNUSED_RESULT
#endif
namespace scad {
const int kTabSize = 2;
using ScadWriter = std::function<void(std::FILE* file, int indent_level)>;
template <typename T>
class Optional {
public:
Optional() {
}
Optional(T value) : value_(value), has_value_(true) {
}
T value() const {
return value_;
}
bool has_value() const {
return has_value_;
}
private:
T value_;
bool has_value_ = false;
};
struct LinearExtrudeParams {
double height = 0;
double twist = 0;
double convexity = 10;
int slices = 20;
double scale = 1;
bool center = true;
};
class Shape {
public:
Shape() {
}
explicit Shape(std::shared_ptr<ScadWriter> scad) : scad_(std::move(scad)) {
}
explicit Shape(ScadWriter scad) : scad_(std::make_shared<ScadWriter>(std::move(scad))) {
}
static Shape Composite(const std::function<void(std::FILE*)>& write_name,
const std::vector<Shape>& shapes);
static Shape LiteralComposite(const std::string& name, const std::vector<Shape>& shapes);
static Shape Primitive(const std::function<void(std::FILE*)>& scad_writer);
static Shape LiteralPrimitive(const std::string& primitive);
void WriteToFile(const std::string& file_name) const;
void AppendScad(std::FILE* file, int indent_level) const;
Shape SCAD_WARN_UNUSED_RESULT Translate(double x, double y, double z) const;
Shape SCAD_WARN_UNUSED_RESULT TranslateX(double x) const;
Shape SCAD_WARN_UNUSED_RESULT TranslateY(double y) const;
Shape SCAD_WARN_UNUSED_RESULT TranslateZ(double z) const;
template <typename Vec3>
Shape SCAD_WARN_UNUSED_RESULT Translate(const Vec3& v) const {
return Translate(v.x, v.y, v.z);
}
Shape SCAD_WARN_UNUSED_RESULT Mirror(double x, double y, double z) const;
Shape SCAD_WARN_UNUSED_RESULT MirrorY() const {
return Mirror(0, 1, 0);
}
Shape SCAD_WARN_UNUSED_RESULT MirrorX() const {
return Mirror(1, 0, 0);
}
Shape SCAD_WARN_UNUSED_RESULT Rotate(double rx, double ry, double rz) const;
Shape SCAD_WARN_UNUSED_RESULT Rotate(double degrees, double x, double y, double z) const;
Shape SCAD_WARN_UNUSED_RESULT RotateX(double degrees) const;
Shape SCAD_WARN_UNUSED_RESULT RotateY(double degrees) const;
Shape SCAD_WARN_UNUSED_RESULT RotateZ(double degrees) const;
Shape SCAD_WARN_UNUSED_RESULT LinearExtrude(const LinearExtrudeParams& params) const;
Shape SCAD_WARN_UNUSED_RESULT LinearExtrude(double height) const;
Shape SCAD_WARN_UNUSED_RESULT Color(double r, double g, double b, double a = 1.0) const;
Shape SCAD_WARN_UNUSED_RESULT Color(const std::string& color, double a = 1) const;
Shape SCAD_WARN_UNUSED_RESULT Alpha(double a) const;
Shape SCAD_WARN_UNUSED_RESULT Subtract(const Shape& other) const;
Shape operator-(const Shape& other) const;
Shape& operator-=(const Shape& other);
Shape SCAD_WARN_UNUSED_RESULT Add(const Shape& other) const;
Shape operator+(const Shape& other) const;
Shape& operator+=(const Shape& other);
Shape SCAD_WARN_UNUSED_RESULT Scale(double x, double y, double z) const;
Shape SCAD_WARN_UNUSED_RESULT Scale(double s) const;
Shape SCAD_WARN_UNUSED_RESULT OffsetRadius(double r, bool chamfer = false) const;
Shape SCAD_WARN_UNUSED_RESULT OffsetDelta(double delta, bool chamfer = false) const;
Shape SCAD_WARN_UNUSED_RESULT Comment(const std::string& comment) const;
Shape SCAD_WARN_UNUSED_RESULT Projection(bool cut = false) const;
private:
std::shared_ptr<const ScadWriter> scad_;
};
struct CubeParams {
double x = 1;
double y = 1;
double z = 1;
bool center = true;
};
Shape SCAD_WARN_UNUSED_RESULT Cube(const CubeParams& params);
Shape SCAD_WARN_UNUSED_RESULT Cube(double x, double y, double z, bool center = true);
Shape SCAD_WARN_UNUSED_RESULT Cube(double size, bool center = true);
struct SphereParams {
double r = 1;
Optional<double> fn;
Optional<double> fa;
Optional<double> fs;
};
Shape SCAD_WARN_UNUSED_RESULT Sphere(const SphereParams& params);
Shape SCAD_WARN_UNUSED_RESULT Sphere(double radius);
Shape SCAD_WARN_UNUSED_RESULT Sphere(double radius, double fn);
struct CircleParams {
double r = 1;
Optional<double> fn;
Optional<double> fa;
Optional<double> fs;
};
Shape SCAD_WARN_UNUSED_RESULT Circle(const CircleParams& params);
Shape SCAD_WARN_UNUSED_RESULT Circle(double radius);
Shape SCAD_WARN_UNUSED_RESULT Circle(double radius, double fn);
struct CylinderParams {
double h = 1;
double r1 = 1;
double r2 = 1;
Optional<double> fn;
bool center = true;
};
Shape SCAD_WARN_UNUSED_RESULT Cylinder(const CylinderParams& params);
Shape SCAD_WARN_UNUSED_RESULT Cylinder(double height, double radius, Optional<double> fn = {});
struct SquareParams {
double x = 1;
double y = 1;
bool center = true;
};
Shape SCAD_WARN_UNUSED_RESULT Square(const CubeParams& params);
Shape SCAD_WARN_UNUSED_RESULT Square(double x, double y, bool center = true);
Shape SCAD_WARN_UNUSED_RESULT Square(double size, bool center = true);
struct Point2d {
double x = 0;
double y = 0;
};
Shape SCAD_WARN_UNUSED_RESULT Polygon(const std::vector<Point2d>& points);
Shape SCAD_WARN_UNUSED_RESULT RegularPolygon(int n, double radius);
struct Point3d {
double x = 0;
double y = 0;
double z = 0;
};
Shape SCAD_WARN_UNUSED_RESULT Polyhedron(const std::vector<Point3d>& points,
const std::vector<std::vector<int>>& faces,
int convexity = 1);
Shape SCAD_WARN_UNUSED_RESULT HullAll(const std::vector<Shape>& shapes);
template <typename... Shapes>
Shape SCAD_WARN_UNUSED_RESULT Hull(const Shape& shape, const Shapes&... more_shapes) {
return HullAll({shape, more_shapes...});
}
Shape SCAD_WARN_UNUSED_RESULT UnionAll(const std::vector<Shape>& shapes);
template <typename... Shapes>
Shape SCAD_WARN_UNUSED_RESULT Union(const Shape& shape, const Shapes&... more_shapes) {
return UnionAll({shape, more_shapes...});
}
Shape SCAD_WARN_UNUSED_RESULT DifferenceAll(const std::vector<Shape>& shapes);
template <typename... Shapes>
Shape SCAD_WARN_UNUSED_RESULT Difference(const Shape& shape, const Shapes&... more_shapes) {
return DifferenceAll({shape, more_shapes...});
}
Shape SCAD_WARN_UNUSED_RESULT IntersectionAll(const std::vector<Shape>& shapes);
template <typename... Shapes>
Shape SCAD_WARN_UNUSED_RESULT Intersection(const Shape& shape, const Shapes&... more_shapes) {
return IntersectionAll({shape, more_shapes...});
}
Shape SCAD_WARN_UNUSED_RESULT Import(const std::string& file_name, int convexity = -1);
Shape SCAD_WARN_UNUSED_RESULT Minkowski(const Shape& first, const Shape& second);
const char* BoolStr(bool b);
void WriteIndent(std::FILE* file, int indent_level);
void WriteComposite(std::FILE* file,
const std::function<void(std::FILE*)>& write_name,
const std::vector<Shape>& shapes,
int indent_level);
} // namespace scad