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
|
/*
* OpenSCAD (www.openscad.org)
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
* Marius Kintel <marius@kintel.net>
*
* 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.
*
* As a special exception, you have permission to link this program
* with the CGAL library and distribute executables, as long as you
* follow the requirements of the GNU GPL in regard to all of the
* software in the executable aside from CGAL.
*
* 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
*
*/
#include "transformnode.h"
#include "module.h"
#include "evalcontext.h"
#include "polyset.h"
#include "builtin.h"
#include "value.h"
#include "printutils.h"
#include <sstream>
#include <vector>
#include <assert.h>
#include <boost/assign/std/vector.hpp>
using namespace boost::assign; // bring 'operator+=()' into scope
enum transform_type_e {
SCALE,
ROTATE,
MIRROR,
TRANSLATE,
MULTMATRIX
};
class TransformModule : public AbstractModule
{
public:
transform_type_e type;
TransformModule(transform_type_e type) : type(type) { }
virtual AbstractNode *instantiate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const;
};
AbstractNode *TransformModule::instantiate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const
{
TransformNode *node = new TransformNode(inst);
node->matrix = Transform3d::Identity();
AssignmentList args;
switch (this->type) {
case SCALE:
args += Assignment("v", NULL);
break;
case ROTATE:
args += Assignment("a", NULL), Assignment("v", NULL);
break;
case MIRROR:
args += Assignment("v", NULL);
break;
case TRANSLATE:
args += Assignment("v", NULL);
break;
case MULTMATRIX:
args += Assignment("m", NULL);
break;
default:
assert(false);
}
Context c(ctx);
c.setVariables(args, evalctx);
if (this->type == SCALE)
{
Vector3d scalevec(1,1,1);
Value v = c.lookup_variable("v");
if (!v.getVec3(scalevec[0], scalevec[1], scalevec[2], 1.0)) {
double num;
if (v.getDouble(num)) scalevec.setConstant(num);
}
node->matrix.scale(scalevec);
}
else if (this->type == ROTATE)
{
Value val_a = c.lookup_variable("a");
if (val_a.type() == Value::VECTOR)
{
Eigen::AngleAxisd rotx(0, Vector3d::UnitX());
Eigen::AngleAxisd roty(0, Vector3d::UnitY());
Eigen::AngleAxisd rotz(0, Vector3d::UnitZ());
double a;
if (val_a.toVector().size() > 0) {
val_a.toVector()[0].getDouble(a);
rotx = Eigen::AngleAxisd(a*M_PI/180, Vector3d::UnitX());
}
if (val_a.toVector().size() > 1) {
val_a.toVector()[1].getDouble(a);
roty = Eigen::AngleAxisd(a*M_PI/180, Vector3d::UnitY());
}
if (val_a.toVector().size() > 2) {
val_a.toVector()[2].getDouble(a);
rotz = Eigen::AngleAxisd(a*M_PI/180, Vector3d::UnitZ());
}
node->matrix.rotate(rotz * roty * rotx);
}
else
{
Value val_v = c.lookup_variable("v");
double a = 0;
val_a.getDouble(a);
Vector3d axis(0,0,1);
if (val_v.getVec3(axis[0], axis[1], axis[2])) {
if (axis.squaredNorm() > 0) axis.normalize();
}
if (axis.squaredNorm() > 0) {
node->matrix = Eigen::AngleAxisd(a*M_PI/180, axis);
}
}
}
else if (this->type == MIRROR)
{
Value val_v = c.lookup_variable("v");
double x = 1, y = 0, z = 0;
if (val_v.getVec3(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;
}
}
if (x != 0.0 || y != 0.0 || z != 0.0)
{
Eigen::Matrix4d m;
m << 1-2*x*x, -2*y*x, -2*z*x, 0,
-2*x*y, 1-2*y*y, -2*z*y, 0,
-2*x*z, -2*y*z, 1-2*z*z, 0,
0, 0, 0, 1;
node->matrix = m;
}
}
else if (this->type == TRANSLATE)
{
Value v = c.lookup_variable("v");
Vector3d translatevec(0,0,0);
v.getVec3(translatevec[0], translatevec[1], translatevec[2]);
node->matrix.translate(translatevec);
}
else if (this->type == MULTMATRIX)
{
Value v = c.lookup_variable("m");
if (v.type() == Value::VECTOR) {
for (int i = 0; i < 16; i++) {
size_t x = i / 4, y = i % 4;
if (y < v.toVector().size() && v.toVector()[y].type() == Value::VECTOR && x < v.toVector()[y].toVector().size())
v.toVector()[y].toVector()[x].getDouble(node->matrix(y, x));
}
}
}
std::vector<AbstractNode *> instantiatednodes = inst->instantiateChildren(evalctx);
node->children.insert(node->children.end(), instantiatednodes.begin(), instantiatednodes.end());
return node;
}
std::string TransformNode::toString() const
{
std::stringstream stream;
stream << "multmatrix([";
for (int j=0;j<4;j++) {
stream << "[";
for (int i=0;i<4;i++) {
Value v( this->matrix(j, i) );
stream << v;
if (i != 3) stream << ", ";
}
stream << "]";
if (j != 3) stream << ", ";
}
stream << "])";
return stream.str();
}
std::string TransformNode::name() const
{
return "transform";
}
void register_builtin_transform()
{
Builtins::init("scale", new TransformModule(SCALE));
Builtins::init("rotate", new TransformModule(ROTATE));
Builtins::init("mirror", new TransformModule(MIRROR));
Builtins::init("translate", new TransformModule(TRANSLATE));
Builtins::init("multmatrix", new TransformModule(MULTMATRIX));
}
|