-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransformSource.pde
273 lines (224 loc) · 6.05 KB
/
TransformSource.pde
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
abstract class TransformSource
implements Transformable < TransformSource > {
static final String printformat =
"\nPosition: (%.2f, %.2f, %.2f)\n" +
"Rotation: (%.2f, %.2f, %.2f, %.2f)\n" +
"Scale: (%.2f, %.2f, %.2f)\n";
Coord position = new Coord();
Quaternion rotation = new Quaternion();
Dimension scale = new Dimension();
TransformSource() {
}
TransformSource(float tx, float ty, float tz,
float rx, float ry, float rz, float rw,
float sw, float sh, float sd) {
set(tx, ty, tz, rx, ry, rz, rw, sw, sh, sd);
}
TransformSource(Coord position, float angle, Vector axis,
Dimension scale) {
set(position, angle, axis, scale);
}
TransformSource(Coord position, Quaternion rotation,
Dimension scale) {
set(position, rotation, scale);
}
TransformSource(PMatrix2D mat3x3) {
set(mat3x3);
}
TransformSource(PMatrix3D mat4x4) {
set(mat4x4);
}
String toString() {
return String.format(printformat,
position.x, position.y, position.z,
rotation.x, rotation.y, rotation.z, rotation.w,
scale.w, scale.h, scale.d);
}
int compareTo(TransformSource t) {
return position.compareTo(t.position);
}
boolean approx(TransformSource t) {
return approx(t, EPSILON);
}
boolean approx(TransformSource t, float tolerance) {
return position.approx(t.position, tolerance) &&
rotation.approx(t.rotation, tolerance) &&
scale.approx(t.scale, tolerance);
}
abstract TransformSource draw(PGraphics3D r);
TransformSource moveBy(Vector v) {
position.add(v);
return this;
}
TransformSource moveBy(float x, float y) {
position.add(x, y);
return this;
}
TransformSource moveBy(float x, float y, float z) {
position.add(x, y, z);
return this;
}
TransformSource moveTo(Coord c) {
position.set(c);
return this;
}
TransformSource moveTo(float x, float y) {
position.set(x, y);
return this;
}
TransformSource moveTo(float x, float y, float z) {
position.set(x, y, z);
return this;
}
TransformSource reset() {
position.reset();
scale.reset();
rotation.reset();
return this;
}
TransformSource rotateBy(float angle, Vector axis) {
rotation.rotateBy(angle, axis);
return this;
}
TransformSource rotateBy(float angle, float axisx,
float axisy, float axisz) {
rotation.rotateBy(angle, axisx, axisy, axisz);
return this;
}
TransformSource rotateX(float angle) {
rotation.rotateX(angle);
return this;
}
TransformSource rotateY(float angle) {
rotation.rotateY(angle);
return this;
}
TransformSource rotateZ(float angle) {
rotation.rotateZ(angle);
return this;
}
TransformSource rotateTo(float angle, Vector axis) {
rotation.set(angle, axis);
return this;
}
TransformSource rotateTo(Quaternion q) {
rotation.set(q);
return this;
}
TransformSource scaleBy(Dimension scale) {
this.scale.add(scale);
return this;
}
TransformSource scaleBy(float scl) {
scale.add(scl);
return this;
}
TransformSource scaleBy(float w, float h) {
scale.add(w, h);
return this;
}
TransformSource scaleBy(float w, float h, float d) {
scale.add(w, h, d);
return this;
}
TransformSource scaleTo(Dimension scale) {
this.scale.set(scale);
return this;
}
TransformSource scaleTo(float scl) {
scale.set(scl, scl, scl);
return this;
}
TransformSource scaleTo(float x, float y) {
scale.set(x, y);
return this;
}
TransformSource scaleTo(float x, float y, float z) {
scale.set(x, y, z);
return this;
}
TransformSource set(float tx, float ty, float tz,
float rx, float ry, float rz, float rw,
float sw, float sh, float sd) {
position.set(tx, ty, tz);
rotation.set(rx, ry, rz, rw);
scale.set(sw, sh, sd);
return this;
}
TransformSource set(TransformSource transform) {
position.set(transform.position);
scale.set(transform.scale);
rotation.set(transform.rotation);
return this;
}
TransformSource set(Coord translation, float angle, Vector axis,
Dimension scale) {
this.position.set(translation);
this.rotation.set(angle, axis);
this.scale.set(scale);
return this;
}
TransformSource set(Coord translation, Quaternion rotation,
Dimension scale) {
this.position.set(translation);
this.rotation.set(rotation);
this.scale.set(scale);
return this;
}
TransformSource set(PMatrix2D mat3x3) {
position.set(mat3x3.m02, mat3x3.m12);
Vector i = new Vector(mat3x3.m00, mat3x3.m10);
Vector j = new Vector(mat3x3.m01, mat3x3.m11);
float imag = i.magnitude();
float jmag = j.magnitude();
if (mat3x3.determinant() < 0.0) {
jmag = -jmag;
}
scale.set(imag, jmag);
i.mult(imag != 0.0 ? 1.0 / imag : 0.0);
j.mult(jmag != 0.0 ? 1.0 / jmag : 0.0);
rotation.set(i.x, j.x, 0.0,
i.y, j.y, 0.0,
0.0, 0.0, 1.0);
return this;
}
TransformSource set(PMatrix3D mat4x4) {
position.set(mat4x4.m03, mat4x4.m13, mat4x4.m23);
Vector i = new Vector(mat4x4.m00, mat4x4.m10, mat4x4.m20);
Vector j = new Vector(mat4x4.m01, mat4x4.m11, mat4x4.m21);
Vector k = new Vector(mat4x4.m02, mat4x4.m12, mat4x4.m22);
float imag = i.magnitude();
float jmag = j.magnitude();
float kmag = k.magnitude();
if (mat4x4.determinant() < 0.0) {
jmag = -jmag;
}
scale.set(imag, jmag, kmag);
i.mult(imag != 0.0 ? 1.0 / imag : 0.0);
j.mult(jmag != 0.0 ? 1.0 / jmag : 0.0);
k.mult(kmag != 0.0 ? 1.0 / kmag : 0.0);
rotation.set(i.x, j.x, k.x,
i.y, j.y, k.y,
i.z, j.z, k.z);
return this;
}
PMatrix3D toMatrix(PMatrix3D m) {
rotation.toMatrix(m);
m.m00 *= scale.w;
m.m10 *= scale.w;
m.m20 *= scale.w;
m.m01 *= scale.h;
m.m11 *= scale.h;
m.m21 *= scale.h;
m.m02 *= scale.d;
m.m12 *= scale.d;
m.m22 *= scale.d;
m.m03 = position.x;
m.m13 = position.y;
m.m23 = position.z;
return m;
}
PMatrix3D toPMatrix3D() {
return toMatrix(new PMatrix3D());
}
}