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
|
/*
* OpenSCAD (www.openscad.at)
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#define INCLUDE_ABSTRACT_NODE_DETAILS
#include "openscad.h"
enum transform_type_e {
SCALE,
ROTATE,
TRANSLATE,
MULTMATRIX
};
class TransformModule : public AbstractModule
{
public:
transform_type_e type;
TransformModule(transform_type_e type) : type(type) { }
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstanciation *inst) const;
};
class TransformNode : public AbstractNode
{
public:
double m[16];
TransformNode(const ModuleInstanciation *mi) : AbstractNode(mi) { }
#ifdef ENABLE_CGAL
virtual CGAL_Nef_polyhedron render_cgal_nef_polyhedron() const;
#endif
virtual CSGTerm *render_csg_term(double m[16], QVector<CSGTerm*> *highlights, QVector<CSGTerm*> *background) const;
virtual QString dump(QString indent) const;
};
AbstractNode *TransformModule::evaluate(const Context *ctx, const ModuleInstanciation *inst) const
{
TransformNode *node = new TransformNode(inst);
for (int i = 0; i < 16; i++) {
node->m[i] = i % 5 == 0 ? 1.0 : 0.0;
}
QVector<QString> argnames;
QVector<Expression*> argexpr;
if (type == SCALE) {
argnames = QVector<QString>() << "v";
}
if (type == ROTATE) {
argnames = QVector<QString>() << "a" << "v";
}
if (type == TRANSLATE) {
argnames = QVector<QString>() << "v";
}
if (type == MULTMATRIX) {
argnames = QVector<QString>() << "m";
}
Context c(ctx);
c.args(argnames, argexpr, inst->argnames, inst->argvalues);
if (type == SCALE)
{
Value v = c.lookup_variable("v");
v.getnum(node->m[0]);
v.getnum(node->m[5]);
v.getnum(node->m[10]);
v.getv3(node->m[0], node->m[5], node->m[10]);
if (node->m[10] <= 0)
node->m[10] = 1;
}
if (type == ROTATE)
{
Value val_a = c.lookup_variable("a");
if (val_a.type == Value::VECTOR)
{
for (int i = 0; i < 3; i++) {
double a;
val_a.vec[i]->getnum(a);
double c = cos(a*M_PI/180.0);
double s = sin(a*M_PI/180.0);
double x = i == 0, y = i == 1, z = i == 2;
double mr[16] = {
x*x*(1-c)+c,
y*x*(1-c)+z*s,
z*x*(1-c)-y*s,
0,
x*y*(1-c)-z*s,
y*y*(1-c)+c,
z*y*(1-c)+x*s,
0,
x*z*(1-c)+y*s,
y*z*(1-c)-x*s,
z*z*(1-c)+c,
0,
0, 0, 0, 1
};
double m[16];
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
{
m[x+y*4] = 0;
for (int i = 0; i < 4; i++)
m[x+y*4] += node->m[i+y*4] * mr[x+i*4];
}
for (int i = 0; i < 16; i++)
node->m[i] = m[i];
}
}
else
{
Value val_v = c.lookup_variable("v");
double a = 0, x = 0, y = 0, z = 1;
val_a.getnum(a);
if (val_v.getv3(x, y, z)) {
if (x != 0.0 || y != 0.0 || z != 0.0) {
double sn = 1.0 / sqrt(x*x + y*y + z*z);
x *= sn, y *= sn, z *= sn;
} else {
x = 0, y = 0, z = 1;
}
}
double c = cos(a*M_PI/180.0);
double s = sin(a*M_PI/180.0);
node->m[ 0] = x*x*(1-c)+c;
node->m[ 1] = y*x*(1-c)+z*s;
node->m[ 2] = z*x*(1-c)-y*s;
node->m[ 4] = x*y*(1-c)-z*s;
node->m[ 5] = y*y*(1-c)+c;
node->m[ 6] = z*y*(1-c)+x*s;
node->m[ 8] = x*z*(1-c)+y*s;
node->m[ 9] = y*z*(1-c)-x*s;
node->m[10] = z*z*(1-c)+c;
}
}
if (type == TRANSLATE)
{
Value v = c.lookup_variable("v");
v.getv3(node->m[12], node->m[13], node->m[14]);
}
if (type == MULTMATRIX)
{
Value v = c.lookup_variable("m");
if (v.type == Value::VECTOR) {
for (int i = 0; i < 16; i++) {
int x = i / 4, y = i % 4;
if (y < v.vec.size() && v.vec[y]->type == Value::VECTOR && x < v.vec[y]->vec.size())
v.vec[y]->vec[x]->getnum(node->m[i]);
}
}
}
foreach (ModuleInstanciation *v, inst->children) {
AbstractNode *n = v->evaluate(inst->ctx);
if (n != NULL)
node->children.append(n);
}
return node;
}
#ifdef ENABLE_CGAL
CGAL_Nef_polyhedron TransformNode::render_cgal_nef_polyhedron() const
{
QString cache_id = mk_cache_id();
if (cgal_nef_cache.contains(cache_id)) {
progress_report();
return *cgal_nef_cache[cache_id];
}
bool first = true;
CGAL_Nef_polyhedron N;
foreach (AbstractNode *v, children) {
if (v->modinst->tag_background)
continue;
if (first) {
N = v->render_cgal_nef_polyhedron();
first = false;
} else if (N.dim == 2) {
N.p2 += v->render_cgal_nef_polyhedron().p2;
} else if (N.dim == 3) {
N.p3 += v->render_cgal_nef_polyhedron().p3;
}
}
if (N.dim == 2)
{
// WARNING: There must be an easier way to perform a CGAL_Aff_transformation2
// on a CGAL_Nef_polyhedron2 than this. But I haven't found the right way to do
// it yet and this solution seams to work well.
CGAL_Aff_transformation2 t(
m[0], m[4], m[12],
m[1], m[5], m[13], m[15]);
typedef std::list<CGAL_Nef_polyhedron2::Point> point_list_t;
typedef point_list_t::iterator point_list_it;
std::list< point_list_t > pdata_point_lists;
std::list < std::pair < point_list_it, point_list_it > >pdata;
typedef CGAL_Nef_polyhedron2::Explorer Explorer;
typedef Explorer::Face_const_iterator fci_t;
typedef Explorer::Halfedge_around_face_const_circulator heafcc_t;
Explorer E = N.p2.explorer();
for (fci_t fit = E.faces_begin(), fend = E.faces_end(); fit != fend; ++fit)
{
pdata_point_lists.push_back(point_list_t());
heafcc_t fcirc(E.halfedge(fit)), fend(fcirc);
CGAL_For_all(fcirc, fend) {
if (E.is_standard(E.target(fcirc))) {
Explorer::Point ep = E.point(E.target(fcirc));
CGAL_Kernel2::Point_2 tp = t.transform(CGAL_Kernel2::Point_2(ep.x(), ep.y()));
// FIXME: This to_double() calls should not be neccessary! It would be much better to reuse
// the gmpq value directly. But I haven't managed to kick CGAL hard enough to do the trick..
CGAL_Nef_polyhedron2::Point p = CGAL_Nef_polyhedron2::Point(to_double(tp.x()), to_double(tp.y()));
pdata_point_lists.back().push_back(p);
}
}
pdata.push_back(std::make_pair(pdata_point_lists.back().begin(), pdata_point_lists.back().end()));
}
N.p2 = CGAL_Nef_polyhedron2(pdata.begin(), pdata.end(), CGAL_Nef_polyhedron2::POLYGONS);
}
if (N.dim == 3) {
CGAL_Aff_transformation t(
m[0], m[4], m[ 8], m[12],
m[1], m[5], m[ 9], m[13],
m[2], m[6], m[10], m[14], m[15]);
N.p3.transform(t);
}
cgal_nef_cache.insert(cache_id, new CGAL_Nef_polyhedron(N), N.weight());
progress_report();
return N;
}
#endif /* ENABLE_CGAL */
CSGTerm *TransformNode::render_csg_term(double c[16], QVector<CSGTerm*> *highlights, QVector<CSGTerm*> *background) const
{
double x[16];
for (int i = 0; i < 16; i++)
{
int c_row = i%4;
int m_col = i/4;
x[i] = 0;
for (int j = 0; j < 4; j++)
x[i] += c[c_row + j*4] * m[m_col*4 + j];
}
CSGTerm *t1 = NULL;
foreach(AbstractNode *v, children)
{
CSGTerm *t2 = v->render_csg_term(x, highlights, background);
if (t2 && !t1) {
t1 = t2;
} else if (t2 && t1) {
t1 = new CSGTerm(CSGTerm::TYPE_UNION, t1, t2);
}
}
if (t1 && modinst->tag_highlight && highlights)
highlights->append(t1->link());
if (t1 && modinst->tag_background && background) {
background->append(t1);
return NULL;
}
return t1;
}
QString TransformNode::dump(QString indent) const
{
if (dump_cache.isEmpty()) {
QString text;
text.sprintf("n%d: multmatrix([[%f %f %f %f], [%f %f %f %f], [%f %f %f %f], [%f %f %f %f]])", idx,
m[0], m[4], m[ 8], m[12],
m[1], m[5], m[ 9], m[13],
m[2], m[6], m[10], m[14],
m[3], m[7], m[11], m[15]);
text = indent + text + " {\n";
foreach (AbstractNode *v, children)
text += v->dump(indent + QString("\t"));
((AbstractNode*)this)->dump_cache = text + indent + "}\n";
}
return dump_cache;
}
void register_builtin_transform()
{
builtin_modules["scale"] = new TransformModule(SCALE);
builtin_modules["rotate"] = new TransformModule(ROTATE);
builtin_modules["translate"] = new TransformModule(TRANSLATE);
builtin_modules["multmatrix"] = new TransformModule(MULTMATRIX);
}
|