picrate 0.0.2-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +47 -0
- data/.mvn/extensions.xml +9 -0
- data/.mvn/wrapper/maven-wrapper.properties +1 -0
- data/.travis.yml +10 -0
- data/CHANGELOG.md +4 -0
- data/LICENSE.md +165 -0
- data/README.md +51 -0
- data/Rakefile +59 -0
- data/bin/picrate +8 -0
- data/docs/.gitignore +6 -0
- data/docs/_config.yml +30 -0
- data/docs/_includes/footer.html +38 -0
- data/docs/_includes/head.html +16 -0
- data/docs/_includes/header.html +27 -0
- data/docs/_includes/icon-github.html +1 -0
- data/docs/_includes/icon-github.svg +1 -0
- data/docs/_includes/icon-twitter.html +1 -0
- data/docs/_includes/icon-twitter.svg +1 -0
- data/docs/_includes/navigation.html +24 -0
- data/docs/_layouts/default.html +20 -0
- data/docs/_layouts/page.html +14 -0
- data/docs/_layouts/post.html +15 -0
- data/docs/_posts/2018-05-06-getting_started.md +8 -0
- data/docs/_posts/2018-05-06-install_jruby.md +35 -0
- data/docs/_sass/_base.scss +206 -0
- data/docs/_sass/_layout.scss +242 -0
- data/docs/_sass/_syntax-highlighting.scss +71 -0
- data/docs/about.md +10 -0
- data/docs/css/main.scss +38 -0
- data/docs/favicon.ico +0 -0
- data/docs/feed.xml +30 -0
- data/docs/index.html +38 -0
- data/lib/picrate.rb +10 -0
- data/lib/picrate/app.rb +187 -0
- data/lib/picrate/creators/sketch_class.rb +57 -0
- data/lib/picrate/creators/sketch_factory.rb +12 -0
- data/lib/picrate/creators/sketch_writer.rb +21 -0
- data/lib/picrate/helper_methods.rb +214 -0
- data/lib/picrate/helpers/numeric.rb +9 -0
- data/lib/picrate/library.rb +69 -0
- data/lib/picrate/library_loader.rb +53 -0
- data/lib/picrate/native_folder.rb +35 -0
- data/lib/picrate/native_loader.rb +27 -0
- data/lib/picrate/runner.rb +81 -0
- data/lib/picrate/version.rb +4 -0
- data/library/boids/boids.rb +209 -0
- data/library/chooser/chooser.rb +19 -0
- data/library/control_panel/control_panel.rb +182 -0
- data/library/library_proxy/README.md +99 -0
- data/library/library_proxy/library_proxy.rb +14 -0
- data/library/slider/slider.rb +42 -0
- data/library/vector_utils/vector_utils.rb +69 -0
- data/library/video_event/video_event.rb +3 -0
- data/license.txt +508 -0
- data/picrate.gemspec +35 -0
- data/pom.rb +122 -0
- data/pom.xml +214 -0
- data/src/main/java/japplemenubar/JAppleMenuBar.java +88 -0
- data/src/main/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
- data/src/main/java/monkstone/ColorUtil.java +115 -0
- data/src/main/java/monkstone/MathToolModule.java +236 -0
- data/src/main/java/monkstone/PicrateLibrary.java +47 -0
- data/src/main/java/monkstone/core/LibraryProxy.java +127 -0
- data/src/main/java/monkstone/fastmath/Deglut.java +122 -0
- data/src/main/java/monkstone/fastmath/package-info.java +6 -0
- data/src/main/java/monkstone/filechooser/Chooser.java +48 -0
- data/src/main/java/monkstone/noise/SimplexNoise.java +465 -0
- data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +168 -0
- data/src/main/java/monkstone/slider/CustomVerticalSlider.java +182 -0
- data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +149 -0
- data/src/main/java/monkstone/slider/SimpleSlider.java +196 -0
- data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +163 -0
- data/src/main/java/monkstone/slider/Slider.java +67 -0
- data/src/main/java/monkstone/slider/SliderBar.java +277 -0
- data/src/main/java/monkstone/slider/SliderGroup.java +78 -0
- data/src/main/java/monkstone/slider/WheelHandler.java +35 -0
- data/src/main/java/monkstone/vecmath/AppRender.java +87 -0
- data/src/main/java/monkstone/vecmath/JRender.java +56 -0
- data/src/main/java/monkstone/vecmath/ShapeRender.java +87 -0
- data/src/main/java/monkstone/vecmath/package-info.java +20 -0
- data/src/main/java/monkstone/vecmath/vec2/Vec2.java +757 -0
- data/src/main/java/monkstone/vecmath/vec2/package-info.java +6 -0
- data/src/main/java/monkstone/vecmath/vec3/Vec3.java +727 -0
- data/src/main/java/monkstone/vecmath/vec3/package-info.java +6 -0
- data/src/main/java/monkstone/videoevent/VideoInterface.java +42 -0
- data/src/main/java/monkstone/videoevent/package-info.java +20 -0
- data/src/main/java/processing/awt/PGraphicsJava2D.java +3098 -0
- data/src/main/java/processing/awt/PShapeJava2D.java +401 -0
- data/src/main/java/processing/awt/PSurfaceAWT.java +1660 -0
- data/src/main/java/processing/core/PApplet.java +17647 -0
- data/src/main/java/processing/core/PConstants.java +1033 -0
- data/src/main/java/processing/core/PFont.java +1250 -0
- data/src/main/java/processing/core/PGraphics.java +9614 -0
- data/src/main/java/processing/core/PImage.java +3608 -0
- data/src/main/java/processing/core/PMatrix.java +347 -0
- data/src/main/java/processing/core/PMatrix2D.java +694 -0
- data/src/main/java/processing/core/PMatrix3D.java +1153 -0
- data/src/main/java/processing/core/PShape.java +4332 -0
- data/src/main/java/processing/core/PShapeOBJ.java +544 -0
- data/src/main/java/processing/core/PShapeSVG.java +1987 -0
- data/src/main/java/processing/core/PStyle.java +208 -0
- data/src/main/java/processing/core/PSurface.java +242 -0
- data/src/main/java/processing/core/PSurfaceNone.java +479 -0
- data/src/main/java/processing/core/PVector.java +1140 -0
- data/src/main/java/processing/data/FloatDict.java +829 -0
- data/src/main/java/processing/data/FloatList.java +912 -0
- data/src/main/java/processing/data/IntDict.java +796 -0
- data/src/main/java/processing/data/IntList.java +913 -0
- data/src/main/java/processing/data/JSONArray.java +1260 -0
- data/src/main/java/processing/data/JSONObject.java +2282 -0
- data/src/main/java/processing/data/JSONTokener.java +435 -0
- data/src/main/java/processing/data/Sort.java +46 -0
- data/src/main/java/processing/data/StringDict.java +601 -0
- data/src/main/java/processing/data/StringList.java +775 -0
- data/src/main/java/processing/data/Table.java +4923 -0
- data/src/main/java/processing/data/TableRow.java +198 -0
- data/src/main/java/processing/data/XML.java +1149 -0
- data/src/main/java/processing/event/Event.java +108 -0
- data/src/main/java/processing/event/KeyEvent.java +70 -0
- data/src/main/java/processing/event/MouseEvent.java +149 -0
- data/src/main/java/processing/event/TouchEvent.java +57 -0
- data/src/main/java/processing/javafx/PGraphicsFX2D.java +354 -0
- data/src/main/java/processing/opengl/FontTexture.java +379 -0
- data/src/main/java/processing/opengl/FrameBuffer.java +503 -0
- data/src/main/java/processing/opengl/LinePath.java +623 -0
- data/src/main/java/processing/opengl/LineStroker.java +685 -0
- data/src/main/java/processing/opengl/PGL.java +3366 -0
- data/src/main/java/processing/opengl/PGraphics2D.java +615 -0
- data/src/main/java/processing/opengl/PGraphics3D.java +281 -0
- data/src/main/java/processing/opengl/PGraphicsOpenGL.java +13634 -0
- data/src/main/java/processing/opengl/PJOGL.java +1966 -0
- data/src/main/java/processing/opengl/PShader.java +1478 -0
- data/src/main/java/processing/opengl/PShapeOpenGL.java +5234 -0
- data/src/main/java/processing/opengl/PSurfaceJOGL.java +1315 -0
- data/src/main/java/processing/opengl/Texture.java +1670 -0
- data/src/main/java/processing/opengl/VertexBuffer.java +88 -0
- data/src/main/java/processing/opengl/cursors/arrow.png +0 -0
- data/src/main/java/processing/opengl/cursors/cross.png +0 -0
- data/src/main/java/processing/opengl/cursors/hand.png +0 -0
- data/src/main/java/processing/opengl/cursors/license.txt +27 -0
- data/src/main/java/processing/opengl/cursors/move.png +0 -0
- data/src/main/java/processing/opengl/cursors/text.png +0 -0
- data/src/main/java/processing/opengl/cursors/wait.png +0 -0
- data/src/main/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/ColorVert.glsl +34 -0
- data/src/main/java/processing/opengl/shaders/LightFrag.glsl +33 -0
- data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +154 -0
- data/src/main/java/processing/opengl/shaders/LightVert.glsl +151 -0
- data/src/main/java/processing/opengl/shaders/LineFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/LineVert.glsl +100 -0
- data/src/main/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
- data/src/main/java/processing/opengl/shaders/PointFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/PointVert.glsl +56 -0
- data/src/main/java/processing/opengl/shaders/TexFrag.glsl +37 -0
- data/src/main/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
- data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +160 -0
- data/src/main/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
- data/src/main/java/processing/opengl/shaders/TexVert.glsl +38 -0
- data/src/main/resources/icon/icon-1024.png +0 -0
- data/src/main/resources/icon/icon-128.png +0 -0
- data/src/main/resources/icon/icon-16.png +0 -0
- data/src/main/resources/icon/icon-256.png +0 -0
- data/src/main/resources/icon/icon-32.png +0 -0
- data/src/main/resources/icon/icon-48.png +0 -0
- data/src/main/resources/icon/icon-512.png +0 -0
- data/src/main/resources/icon/icon-64.png +0 -0
- data/src/main/resources/license.txt +508 -0
- data/test/create_test.rb +68 -0
- data/test/deglut_spec_test.rb +24 -0
- data/test/helper_methods_test.rb +58 -0
- data/test/math_tool_test.rb +75 -0
- data/test/respond_to_test.rb +215 -0
- data/test/sketches/key_event.rb +37 -0
- data/test/sketches/library/my_library/my_library.rb +32 -0
- data/test/test_helper.rb +3 -0
- data/test/vecmath_spec_test.rb +522 -0
- data/vendors/Rakefile +127 -0
- metadata +289 -0
@@ -0,0 +1,1153 @@
|
|
1
|
+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
2
|
+
|
3
|
+
/*
|
4
|
+
Part of the Processing project - http://processing.org
|
5
|
+
|
6
|
+
Copyright (c) 2005-12 Ben Fry and Casey Reas
|
7
|
+
|
8
|
+
This library is free software; you can redistribute it and/or
|
9
|
+
modify it under the terms of the GNU Lesser General Public
|
10
|
+
License version 2.1 as published by the Free Software Foundation.
|
11
|
+
|
12
|
+
This library is distributed in the hope that it will be useful,
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15
|
+
Lesser General Public License for more details.
|
16
|
+
|
17
|
+
You should have received a copy of the GNU Lesser General
|
18
|
+
Public License along with this library; if not, write to the
|
19
|
+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
20
|
+
Boston, MA 02111-1307 USA
|
21
|
+
*/
|
22
|
+
|
23
|
+
package processing.core;
|
24
|
+
|
25
|
+
|
26
|
+
/**
|
27
|
+
* 4x4 matrix implementation.
|
28
|
+
* Matrices are used to describe a transformation; see {@link PMatrix} for a
|
29
|
+
* general description. This matrix looks like the following when multiplying
|
30
|
+
* a vector (x, y, z, w) in {@code mult()}.
|
31
|
+
* <pre>
|
32
|
+
* [m00 m01 m02 m03][x] [m00*x + m01*y + m02*z + m03*w] [x']
|
33
|
+
* [m10 m11 m12 m13][y] = [m10*x + m11*y + m12*z + m13*w] = [y']
|
34
|
+
* [m20 m21 m22 m23][z] [m20*x + m21*y + m22*z + m23*w] [z']
|
35
|
+
* [m30 m31 m32 m33][w] [m30*x + m31*y + m32*z + m33*w] [w']</pre>
|
36
|
+
* (x', y', z', w') is returned. The values in the matrix determine the
|
37
|
+
* transformation. They are modified by the various transformation functions.
|
38
|
+
*
|
39
|
+
* To transform 3D coordinates, w is set to 1, amd w' is made to be 1 by
|
40
|
+
* setting the bottom row of the matrix to <code>[0 0 0 1]</code>. The
|
41
|
+
* resulting point is then (x', y', z').
|
42
|
+
*/
|
43
|
+
public final class PMatrix3D implements PMatrix /*, PConstants*/ {
|
44
|
+
|
45
|
+
/**
|
46
|
+
*
|
47
|
+
*/
|
48
|
+
public float m00,
|
49
|
+
|
50
|
+
/**
|
51
|
+
*
|
52
|
+
*/
|
53
|
+
m01,
|
54
|
+
|
55
|
+
/**
|
56
|
+
*
|
57
|
+
*/
|
58
|
+
m02,
|
59
|
+
|
60
|
+
/**
|
61
|
+
*
|
62
|
+
*/
|
63
|
+
m03;
|
64
|
+
|
65
|
+
/**
|
66
|
+
*
|
67
|
+
*/
|
68
|
+
public float m10,
|
69
|
+
|
70
|
+
/**
|
71
|
+
*
|
72
|
+
*/
|
73
|
+
m11,
|
74
|
+
|
75
|
+
/**
|
76
|
+
*
|
77
|
+
*/
|
78
|
+
m12,
|
79
|
+
|
80
|
+
/**
|
81
|
+
*
|
82
|
+
*/
|
83
|
+
m13;
|
84
|
+
|
85
|
+
/**
|
86
|
+
*
|
87
|
+
*/
|
88
|
+
public float m20,
|
89
|
+
|
90
|
+
/**
|
91
|
+
*
|
92
|
+
*/
|
93
|
+
m21,
|
94
|
+
|
95
|
+
/**
|
96
|
+
*
|
97
|
+
*/
|
98
|
+
m22, m23;
|
99
|
+
|
100
|
+
/**
|
101
|
+
*
|
102
|
+
*/
|
103
|
+
public float m30,
|
104
|
+
|
105
|
+
/**
|
106
|
+
*
|
107
|
+
*/
|
108
|
+
m31,
|
109
|
+
|
110
|
+
/**
|
111
|
+
*
|
112
|
+
*/
|
113
|
+
m32,
|
114
|
+
|
115
|
+
/**
|
116
|
+
*
|
117
|
+
*/
|
118
|
+
m33;
|
119
|
+
|
120
|
+
|
121
|
+
// locally allocated version to avoid creating new memory
|
122
|
+
|
123
|
+
/**
|
124
|
+
*
|
125
|
+
*/
|
126
|
+
protected PMatrix3D inverseCopy;
|
127
|
+
|
128
|
+
/**
|
129
|
+
*
|
130
|
+
*/
|
131
|
+
public PMatrix3D() {
|
132
|
+
reset();
|
133
|
+
}
|
134
|
+
|
135
|
+
/**
|
136
|
+
*
|
137
|
+
* @param m00
|
138
|
+
* @param m01
|
139
|
+
* @param m02
|
140
|
+
* @param m10
|
141
|
+
* @param m11
|
142
|
+
* @param m12
|
143
|
+
*/
|
144
|
+
public PMatrix3D(float m00, float m01, float m02,
|
145
|
+
float m10, float m11, float m12) {
|
146
|
+
set(m00, m01, m02, 0,
|
147
|
+
m10, m11, m12, 0,
|
148
|
+
0, 0, 1, 0,
|
149
|
+
0, 0, 0, 1);
|
150
|
+
}
|
151
|
+
|
152
|
+
/**
|
153
|
+
*
|
154
|
+
* @param m00
|
155
|
+
* @param m01
|
156
|
+
* @param m02
|
157
|
+
* @param m03
|
158
|
+
* @param m10
|
159
|
+
* @param m11
|
160
|
+
* @param m12
|
161
|
+
* @param m13
|
162
|
+
* @param m20
|
163
|
+
* @param m21
|
164
|
+
* @param m22
|
165
|
+
* @param m23
|
166
|
+
* @param m30
|
167
|
+
* @param m31
|
168
|
+
* @param m32
|
169
|
+
* @param m33
|
170
|
+
*/
|
171
|
+
public PMatrix3D(float m00, float m01, float m02, float m03,
|
172
|
+
float m10, float m11, float m12, float m13,
|
173
|
+
float m20, float m21, float m22, float m23,
|
174
|
+
float m30, float m31, float m32, float m33) {
|
175
|
+
set(m00, m01, m02, m03,
|
176
|
+
m10, m11, m12, m13,
|
177
|
+
m20, m21, m22, m23,
|
178
|
+
m30, m31, m32, m33);
|
179
|
+
}
|
180
|
+
|
181
|
+
/**
|
182
|
+
*
|
183
|
+
* @param matrix
|
184
|
+
*/
|
185
|
+
public PMatrix3D(PMatrix matrix) {
|
186
|
+
set(matrix);
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
public void reset() {
|
191
|
+
set(1, 0, 0, 0,
|
192
|
+
0, 1, 0, 0,
|
193
|
+
0, 0, 1, 0,
|
194
|
+
0, 0, 0, 1);
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
/**
|
199
|
+
* Returns a copy of this PMatrix.
|
200
|
+
* @return
|
201
|
+
*/
|
202
|
+
public PMatrix3D get() {
|
203
|
+
PMatrix3D outgoing = new PMatrix3D();
|
204
|
+
outgoing.set(this);
|
205
|
+
return outgoing;
|
206
|
+
}
|
207
|
+
|
208
|
+
|
209
|
+
/**
|
210
|
+
* Copies the matrix contents into a 16 entry float array.
|
211
|
+
* If target is null (or not the correct size), a new array will be created.
|
212
|
+
* @param target
|
213
|
+
* @return
|
214
|
+
*/
|
215
|
+
public float[] get(float[] target) {
|
216
|
+
if ((target == null) || (target.length != 16)) {
|
217
|
+
target = new float[16];
|
218
|
+
}
|
219
|
+
target[0] = m00;
|
220
|
+
target[1] = m01;
|
221
|
+
target[2] = m02;
|
222
|
+
target[3] = m03;
|
223
|
+
|
224
|
+
target[4] = m10;
|
225
|
+
target[5] = m11;
|
226
|
+
target[6] = m12;
|
227
|
+
target[7] = m13;
|
228
|
+
|
229
|
+
target[8] = m20;
|
230
|
+
target[9] = m21;
|
231
|
+
target[10] = m22;
|
232
|
+
target[11] = m23;
|
233
|
+
|
234
|
+
target[12] = m30;
|
235
|
+
target[13] = m31;
|
236
|
+
target[14] = m32;
|
237
|
+
target[15] = m33;
|
238
|
+
|
239
|
+
return target;
|
240
|
+
}
|
241
|
+
|
242
|
+
|
243
|
+
public void set(PMatrix matrix) {
|
244
|
+
if (matrix instanceof PMatrix3D) {
|
245
|
+
PMatrix3D src = (PMatrix3D) matrix;
|
246
|
+
set(src.m00, src.m01, src.m02, src.m03,
|
247
|
+
src.m10, src.m11, src.m12, src.m13,
|
248
|
+
src.m20, src.m21, src.m22, src.m23,
|
249
|
+
src.m30, src.m31, src.m32, src.m33);
|
250
|
+
} else {
|
251
|
+
PMatrix2D src = (PMatrix2D) matrix;
|
252
|
+
set(src.m00, src.m01, 0, src.m02,
|
253
|
+
src.m10, src.m11, 0, src.m12,
|
254
|
+
0, 0, 1, 0,
|
255
|
+
0, 0, 0, 1);
|
256
|
+
}
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
public void set(float[] source) {
|
261
|
+
if (source.length == 6) {
|
262
|
+
set(source[0], source[1], source[2],
|
263
|
+
source[3], source[4], source[5]);
|
264
|
+
|
265
|
+
} else if (source.length == 16) {
|
266
|
+
m00 = source[0];
|
267
|
+
m01 = source[1];
|
268
|
+
m02 = source[2];
|
269
|
+
m03 = source[3];
|
270
|
+
|
271
|
+
m10 = source[4];
|
272
|
+
m11 = source[5];
|
273
|
+
m12 = source[6];
|
274
|
+
m13 = source[7];
|
275
|
+
|
276
|
+
m20 = source[8];
|
277
|
+
m21 = source[9];
|
278
|
+
m22 = source[10];
|
279
|
+
m23 = source[11];
|
280
|
+
|
281
|
+
m30 = source[12];
|
282
|
+
m31 = source[13];
|
283
|
+
m32 = source[14];
|
284
|
+
m33 = source[15];
|
285
|
+
}
|
286
|
+
}
|
287
|
+
|
288
|
+
|
289
|
+
public void set(float m00, float m01, float m02,
|
290
|
+
float m10, float m11, float m12) {
|
291
|
+
set(m00, m01, 0, m02,
|
292
|
+
m10, m11, 0, m12,
|
293
|
+
0, 0, 1, 0,
|
294
|
+
0, 0, 0, 1);
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
public void set(float m00, float m01, float m02, float m03,
|
299
|
+
float m10, float m11, float m12, float m13,
|
300
|
+
float m20, float m21, float m22, float m23,
|
301
|
+
float m30, float m31, float m32, float m33) {
|
302
|
+
this.m00 = m00; this.m01 = m01; this.m02 = m02; this.m03 = m03;
|
303
|
+
this.m10 = m10; this.m11 = m11; this.m12 = m12; this.m13 = m13;
|
304
|
+
this.m20 = m20; this.m21 = m21; this.m22 = m22; this.m23 = m23;
|
305
|
+
this.m30 = m30; this.m31 = m31; this.m32 = m32; this.m33 = m33;
|
306
|
+
}
|
307
|
+
|
308
|
+
/**
|
309
|
+
*
|
310
|
+
* @param tx
|
311
|
+
* @param ty
|
312
|
+
*/
|
313
|
+
public void translate(float tx, float ty) {
|
314
|
+
translate(tx, ty, 0);
|
315
|
+
}
|
316
|
+
|
317
|
+
// public void invTranslate(float tx, float ty) {
|
318
|
+
// invTranslate(tx, ty, 0);
|
319
|
+
// }
|
320
|
+
|
321
|
+
/**
|
322
|
+
*
|
323
|
+
* @param tx
|
324
|
+
* @param ty
|
325
|
+
* @param tz
|
326
|
+
*/
|
327
|
+
public void translate(float tx, float ty, float tz) {
|
328
|
+
m03 += tx*m00 + ty*m01 + tz*m02;
|
329
|
+
m13 += tx*m10 + ty*m11 + tz*m12;
|
330
|
+
m23 += tx*m20 + ty*m21 + tz*m22;
|
331
|
+
m33 += tx*m30 + ty*m31 + tz*m32;
|
332
|
+
}
|
333
|
+
|
334
|
+
/**
|
335
|
+
*
|
336
|
+
* @param angle
|
337
|
+
*/
|
338
|
+
public void rotate(float angle) {
|
339
|
+
rotateZ(angle);
|
340
|
+
}
|
341
|
+
|
342
|
+
/**
|
343
|
+
*
|
344
|
+
* @param angle
|
345
|
+
*/
|
346
|
+
public void rotateX(float angle) {
|
347
|
+
float c = cos(angle);
|
348
|
+
float s = sin(angle);
|
349
|
+
apply(1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1);
|
350
|
+
}
|
351
|
+
|
352
|
+
/**
|
353
|
+
*
|
354
|
+
* @param angle
|
355
|
+
*/
|
356
|
+
public void rotateY(float angle) {
|
357
|
+
float c = cos(angle);
|
358
|
+
float s = sin(angle);
|
359
|
+
apply(c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1);
|
360
|
+
}
|
361
|
+
|
362
|
+
/**
|
363
|
+
*
|
364
|
+
* @param angle
|
365
|
+
*/
|
366
|
+
public void rotateZ(float angle) {
|
367
|
+
float c = cos(angle);
|
368
|
+
float s = sin(angle);
|
369
|
+
apply(c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
|
370
|
+
}
|
371
|
+
|
372
|
+
/**
|
373
|
+
*
|
374
|
+
* @param angle
|
375
|
+
* @param v0
|
376
|
+
* @param v1
|
377
|
+
* @param v2
|
378
|
+
*/
|
379
|
+
public void rotate(float angle, float v0, float v1, float v2) {
|
380
|
+
float norm2 = v0 * v0 + v1 * v1 + v2 * v2;
|
381
|
+
if (norm2 < PConstants.EPSILON) {
|
382
|
+
// The vector is zero, cannot apply rotation.
|
383
|
+
return;
|
384
|
+
}
|
385
|
+
|
386
|
+
if (Math.abs(norm2 - 1) > PConstants.EPSILON) {
|
387
|
+
// The rotation vector is not normalized.
|
388
|
+
float norm = PApplet.sqrt(norm2);
|
389
|
+
v0 /= norm;
|
390
|
+
v1 /= norm;
|
391
|
+
v2 /= norm;
|
392
|
+
}
|
393
|
+
|
394
|
+
float c = cos(angle);
|
395
|
+
float s = sin(angle);
|
396
|
+
float t = 1.0f - c;
|
397
|
+
|
398
|
+
apply((t*v0*v0) + c, (t*v0*v1) - (s*v2), (t*v0*v2) + (s*v1), 0,
|
399
|
+
(t*v0*v1) + (s*v2), (t*v1*v1) + c, (t*v1*v2) - (s*v0), 0,
|
400
|
+
(t*v0*v2) - (s*v1), (t*v1*v2) + (s*v0), (t*v2*v2) + c, 0,
|
401
|
+
0, 0, 0, 1);
|
402
|
+
}
|
403
|
+
|
404
|
+
/**
|
405
|
+
*
|
406
|
+
* @param s
|
407
|
+
*/
|
408
|
+
public void scale(float s) {
|
409
|
+
//apply(s, 0, 0, 0, 0, s, 0, 0, 0, 0, s, 0, 0, 0, 0, 1);
|
410
|
+
scale(s, s, s);
|
411
|
+
}
|
412
|
+
|
413
|
+
/**
|
414
|
+
*
|
415
|
+
* @param sx
|
416
|
+
* @param sy
|
417
|
+
*/
|
418
|
+
public void scale(float sx, float sy) {
|
419
|
+
//apply(sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
|
420
|
+
scale(sx, sy, 1);
|
421
|
+
}
|
422
|
+
|
423
|
+
/**
|
424
|
+
*
|
425
|
+
* @param x
|
426
|
+
* @param y
|
427
|
+
* @param z
|
428
|
+
*/
|
429
|
+
public void scale(float x, float y, float z) {
|
430
|
+
//apply(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
|
431
|
+
m00 *= x; m01 *= y; m02 *= z;
|
432
|
+
m10 *= x; m11 *= y; m12 *= z;
|
433
|
+
m20 *= x; m21 *= y; m22 *= z;
|
434
|
+
m30 *= x; m31 *= y; m32 *= z;
|
435
|
+
}
|
436
|
+
|
437
|
+
/**
|
438
|
+
*
|
439
|
+
* @param angle
|
440
|
+
*/
|
441
|
+
public void shearX(float angle) {
|
442
|
+
float t = (float) Math.tan(angle);
|
443
|
+
apply(1, t, 0, 0,
|
444
|
+
0, 1, 0, 0,
|
445
|
+
0, 0, 1, 0,
|
446
|
+
0, 0, 0, 1);
|
447
|
+
}
|
448
|
+
|
449
|
+
/**
|
450
|
+
*
|
451
|
+
* @param angle
|
452
|
+
*/
|
453
|
+
public void shearY(float angle) {
|
454
|
+
float t = (float) Math.tan(angle);
|
455
|
+
apply(1, 0, 0, 0,
|
456
|
+
t, 1, 0, 0,
|
457
|
+
0, 0, 1, 0,
|
458
|
+
0, 0, 0, 1);
|
459
|
+
}
|
460
|
+
|
461
|
+
|
462
|
+
public void apply(PMatrix source) {
|
463
|
+
if (source instanceof PMatrix2D) {
|
464
|
+
apply((PMatrix2D) source);
|
465
|
+
} else if (source instanceof PMatrix3D) {
|
466
|
+
apply((PMatrix3D) source);
|
467
|
+
}
|
468
|
+
}
|
469
|
+
|
470
|
+
|
471
|
+
public void apply(PMatrix2D source) {
|
472
|
+
apply(source.m00, source.m01, 0, source.m02,
|
473
|
+
source.m10, source.m11, 0, source.m12,
|
474
|
+
0, 0, 1, 0,
|
475
|
+
0, 0, 0, 1);
|
476
|
+
}
|
477
|
+
|
478
|
+
|
479
|
+
public void apply(PMatrix3D source) {
|
480
|
+
apply(source.m00, source.m01, source.m02, source.m03,
|
481
|
+
source.m10, source.m11, source.m12, source.m13,
|
482
|
+
source.m20, source.m21, source.m22, source.m23,
|
483
|
+
source.m30, source.m31, source.m32, source.m33);
|
484
|
+
}
|
485
|
+
|
486
|
+
|
487
|
+
public void apply(float n00, float n01, float n02,
|
488
|
+
float n10, float n11, float n12) {
|
489
|
+
apply(n00, n01, 0, n02,
|
490
|
+
n10, n11, 0, n12,
|
491
|
+
0, 0, 1, 0,
|
492
|
+
0, 0, 0, 1);
|
493
|
+
}
|
494
|
+
|
495
|
+
|
496
|
+
public void apply(float n00, float n01, float n02, float n03,
|
497
|
+
float n10, float n11, float n12, float n13,
|
498
|
+
float n20, float n21, float n22, float n23,
|
499
|
+
float n30, float n31, float n32, float n33) {
|
500
|
+
|
501
|
+
float r00 = m00*n00 + m01*n10 + m02*n20 + m03*n30;
|
502
|
+
float r01 = m00*n01 + m01*n11 + m02*n21 + m03*n31;
|
503
|
+
float r02 = m00*n02 + m01*n12 + m02*n22 + m03*n32;
|
504
|
+
float r03 = m00*n03 + m01*n13 + m02*n23 + m03*n33;
|
505
|
+
|
506
|
+
float r10 = m10*n00 + m11*n10 + m12*n20 + m13*n30;
|
507
|
+
float r11 = m10*n01 + m11*n11 + m12*n21 + m13*n31;
|
508
|
+
float r12 = m10*n02 + m11*n12 + m12*n22 + m13*n32;
|
509
|
+
float r13 = m10*n03 + m11*n13 + m12*n23 + m13*n33;
|
510
|
+
|
511
|
+
float r20 = m20*n00 + m21*n10 + m22*n20 + m23*n30;
|
512
|
+
float r21 = m20*n01 + m21*n11 + m22*n21 + m23*n31;
|
513
|
+
float r22 = m20*n02 + m21*n12 + m22*n22 + m23*n32;
|
514
|
+
float r23 = m20*n03 + m21*n13 + m22*n23 + m23*n33;
|
515
|
+
|
516
|
+
float r30 = m30*n00 + m31*n10 + m32*n20 + m33*n30;
|
517
|
+
float r31 = m30*n01 + m31*n11 + m32*n21 + m33*n31;
|
518
|
+
float r32 = m30*n02 + m31*n12 + m32*n22 + m33*n32;
|
519
|
+
float r33 = m30*n03 + m31*n13 + m32*n23 + m33*n33;
|
520
|
+
|
521
|
+
m00 = r00; m01 = r01; m02 = r02; m03 = r03;
|
522
|
+
m10 = r10; m11 = r11; m12 = r12; m13 = r13;
|
523
|
+
m20 = r20; m21 = r21; m22 = r22; m23 = r23;
|
524
|
+
m30 = r30; m31 = r31; m32 = r32; m33 = r33;
|
525
|
+
}
|
526
|
+
|
527
|
+
|
528
|
+
/**
|
529
|
+
* Apply the 3D equivalent of the 2D matrix supplied to the left of this one.
|
530
|
+
* @param left
|
531
|
+
*/
|
532
|
+
public void preApply(PMatrix2D left) {
|
533
|
+
preApply(left.m00, left.m01, 0, left.m02,
|
534
|
+
left.m10, left.m11, 0, left.m12,
|
535
|
+
0, 0, 1, 0,
|
536
|
+
0, 0, 0, 1);
|
537
|
+
}
|
538
|
+
|
539
|
+
|
540
|
+
/**
|
541
|
+
* Apply another matrix to the left of this one.
|
542
|
+
* @param source
|
543
|
+
*/
|
544
|
+
public void preApply(PMatrix source) {
|
545
|
+
if (source instanceof PMatrix2D) {
|
546
|
+
preApply((PMatrix2D) source);
|
547
|
+
} else if (source instanceof PMatrix3D) {
|
548
|
+
preApply((PMatrix3D) source);
|
549
|
+
}
|
550
|
+
}
|
551
|
+
|
552
|
+
|
553
|
+
/**
|
554
|
+
* Apply another matrix to the left of this one.
|
555
|
+
* @param left
|
556
|
+
*/
|
557
|
+
public void preApply(PMatrix3D left) {
|
558
|
+
preApply(left.m00, left.m01, left.m02, left.m03,
|
559
|
+
left.m10, left.m11, left.m12, left.m13,
|
560
|
+
left.m20, left.m21, left.m22, left.m23,
|
561
|
+
left.m30, left.m31, left.m32, left.m33);
|
562
|
+
}
|
563
|
+
|
564
|
+
|
565
|
+
/**
|
566
|
+
* Apply the 3D equivalent of the 2D matrix supplied to the left of this one.
|
567
|
+
* @param n00
|
568
|
+
* @param n01
|
569
|
+
* @param n10
|
570
|
+
* @param n02
|
571
|
+
* @param n11
|
572
|
+
* @param n12
|
573
|
+
*/
|
574
|
+
public void preApply(float n00, float n01, float n02,
|
575
|
+
float n10, float n11, float n12) {
|
576
|
+
preApply(n00, n01, 0, n02,
|
577
|
+
n10, n11, 0, n12,
|
578
|
+
0, 0, 1, 0,
|
579
|
+
0, 0, 0, 1);
|
580
|
+
}
|
581
|
+
|
582
|
+
|
583
|
+
/**
|
584
|
+
* Apply another matrix to the left of this one.
|
585
|
+
* @param n00
|
586
|
+
* @param n01
|
587
|
+
* @param n03
|
588
|
+
* @param n02
|
589
|
+
* @param n32
|
590
|
+
* @param n10
|
591
|
+
* @param n12
|
592
|
+
* @param n23
|
593
|
+
* @param n20
|
594
|
+
* @param n11
|
595
|
+
* @param n21
|
596
|
+
* @param n13
|
597
|
+
* @param n31
|
598
|
+
* @param n22
|
599
|
+
* @param n30
|
600
|
+
* @param n33
|
601
|
+
*/
|
602
|
+
public void preApply(float n00, float n01, float n02, float n03,
|
603
|
+
float n10, float n11, float n12, float n13,
|
604
|
+
float n20, float n21, float n22, float n23,
|
605
|
+
float n30, float n31, float n32, float n33) {
|
606
|
+
|
607
|
+
float r00 = n00*m00 + n01*m10 + n02*m20 + n03*m30;
|
608
|
+
float r01 = n00*m01 + n01*m11 + n02*m21 + n03*m31;
|
609
|
+
float r02 = n00*m02 + n01*m12 + n02*m22 + n03*m32;
|
610
|
+
float r03 = n00*m03 + n01*m13 + n02*m23 + n03*m33;
|
611
|
+
|
612
|
+
float r10 = n10*m00 + n11*m10 + n12*m20 + n13*m30;
|
613
|
+
float r11 = n10*m01 + n11*m11 + n12*m21 + n13*m31;
|
614
|
+
float r12 = n10*m02 + n11*m12 + n12*m22 + n13*m32;
|
615
|
+
float r13 = n10*m03 + n11*m13 + n12*m23 + n13*m33;
|
616
|
+
|
617
|
+
float r20 = n20*m00 + n21*m10 + n22*m20 + n23*m30;
|
618
|
+
float r21 = n20*m01 + n21*m11 + n22*m21 + n23*m31;
|
619
|
+
float r22 = n20*m02 + n21*m12 + n22*m22 + n23*m32;
|
620
|
+
float r23 = n20*m03 + n21*m13 + n22*m23 + n23*m33;
|
621
|
+
|
622
|
+
float r30 = n30*m00 + n31*m10 + n32*m20 + n33*m30;
|
623
|
+
float r31 = n30*m01 + n31*m11 + n32*m21 + n33*m31;
|
624
|
+
float r32 = n30*m02 + n31*m12 + n32*m22 + n33*m32;
|
625
|
+
float r33 = n30*m03 + n31*m13 + n32*m23 + n33*m33;
|
626
|
+
|
627
|
+
m00 = r00; m01 = r01; m02 = r02; m03 = r03;
|
628
|
+
m10 = r10; m11 = r11; m12 = r12; m13 = r13;
|
629
|
+
m20 = r20; m21 = r21; m22 = r22; m23 = r23;
|
630
|
+
m30 = r30; m31 = r31; m32 = r32; m33 = r33;
|
631
|
+
}
|
632
|
+
|
633
|
+
|
634
|
+
//////////////////////////////////////////////////////////////
|
635
|
+
|
636
|
+
|
637
|
+
/**
|
638
|
+
* Multiply source by this matrix, and return the result.
|
639
|
+
* The result will be stored in target if target is non-null, and target
|
640
|
+
* will then be the matrix returned. This improves performance if you reuse
|
641
|
+
* target, so it's recommended if you call this many times in draw().
|
642
|
+
* @param source
|
643
|
+
* @param target
|
644
|
+
* @return
|
645
|
+
*/
|
646
|
+
public PVector mult(PVector source, PVector target) {
|
647
|
+
if (target == null) {
|
648
|
+
target = new PVector();
|
649
|
+
}
|
650
|
+
target.set(m00*source.x + m01*source.y + m02*source.z + m03,
|
651
|
+
m10*source.x + m11*source.y + m12*source.z + m13,
|
652
|
+
m20*source.x + m21*source.y + m22*source.z + m23);
|
653
|
+
// float tw = m30*source.x + m31*source.y + m32*source.z + m33;
|
654
|
+
// if (tw != 0 && tw != 1) {
|
655
|
+
// target.div(tw);
|
656
|
+
// }
|
657
|
+
return target;
|
658
|
+
}
|
659
|
+
|
660
|
+
|
661
|
+
/*
|
662
|
+
public PVector cmult(PVector source, PVector target) {
|
663
|
+
if (target == null) {
|
664
|
+
target = new PVector();
|
665
|
+
}
|
666
|
+
target.x = m00*source.x + m10*source.y + m20*source.z + m30;
|
667
|
+
target.y = m01*source.x + m11*source.y + m21*source.z + m31;
|
668
|
+
target.z = m02*source.x + m12*source.y + m22*source.z + m32;
|
669
|
+
float tw = m03*source.x + m13*source.y + m23*source.z + m33;
|
670
|
+
if (tw != 0 && tw != 1) {
|
671
|
+
target.div(tw);
|
672
|
+
}
|
673
|
+
return target;
|
674
|
+
}
|
675
|
+
*/
|
676
|
+
|
677
|
+
|
678
|
+
/**
|
679
|
+
* Multiply a three or four element vector against this matrix. If out is
|
680
|
+
* null or not length 3 or 4, a new float array (length 3) will be returned.
|
681
|
+
* Supplying and recycling a target array improves performance, so it's
|
682
|
+
* recommended if you call this many times in draw.
|
683
|
+
* @param source
|
684
|
+
* @param target
|
685
|
+
* @return
|
686
|
+
*/
|
687
|
+
public float[] mult(float[] source, float[] target) {
|
688
|
+
if (target == null || target.length < 3) {
|
689
|
+
target = new float[3];
|
690
|
+
}
|
691
|
+
if (source == target) {
|
692
|
+
throw new RuntimeException("The source and target vectors used in " +
|
693
|
+
"PMatrix3D.mult() cannot be identical.");
|
694
|
+
}
|
695
|
+
if (target.length == 3) {
|
696
|
+
target[0] = m00*source[0] + m01*source[1] + m02*source[2] + m03;
|
697
|
+
target[1] = m10*source[0] + m11*source[1] + m12*source[2] + m13;
|
698
|
+
target[2] = m20*source[0] + m21*source[1] + m22*source[2] + m23;
|
699
|
+
//float w = m30*source[0] + m31*source[1] + m32*source[2] + m33;
|
700
|
+
//if (w != 0 && w != 1) {
|
701
|
+
// target[0] /= w; target[1] /= w; target[2] /= w;
|
702
|
+
//}
|
703
|
+
} else if (target.length > 3) {
|
704
|
+
target[0] = m00*source[0] + m01*source[1] + m02*source[2] + m03*source[3];
|
705
|
+
target[1] = m10*source[0] + m11*source[1] + m12*source[2] + m13*source[3];
|
706
|
+
target[2] = m20*source[0] + m21*source[1] + m22*source[2] + m23*source[3];
|
707
|
+
target[3] = m30*source[0] + m31*source[1] + m32*source[2] + m33*source[3];
|
708
|
+
}
|
709
|
+
return target;
|
710
|
+
}
|
711
|
+
|
712
|
+
|
713
|
+
/**
|
714
|
+
* Returns the x-coordinate of the result of multiplying the point (x, y)
|
715
|
+
* by this matrix.
|
716
|
+
* @param x
|
717
|
+
* @param y
|
718
|
+
* @return
|
719
|
+
*/
|
720
|
+
public float multX(float x, float y) {
|
721
|
+
return m00*x + m01*y + m03;
|
722
|
+
}
|
723
|
+
|
724
|
+
|
725
|
+
/**
|
726
|
+
* Returns the y-coordinate of the result of multiplying the point (x, y)
|
727
|
+
* by this matrix.
|
728
|
+
* @param x
|
729
|
+
* @param y
|
730
|
+
* @return
|
731
|
+
*/
|
732
|
+
public float multY(float x, float y) {
|
733
|
+
return m10*x + m11*y + m13;
|
734
|
+
}
|
735
|
+
|
736
|
+
|
737
|
+
/**
|
738
|
+
* Returns the x-coordinate of the result of multiplying the point (x, y, z)
|
739
|
+
* by this matrix.
|
740
|
+
* @param x
|
741
|
+
* @param y
|
742
|
+
* @param z
|
743
|
+
* @return
|
744
|
+
*/
|
745
|
+
public float multX(float x, float y, float z) {
|
746
|
+
return m00*x + m01*y + m02*z + m03;
|
747
|
+
}
|
748
|
+
|
749
|
+
|
750
|
+
/**
|
751
|
+
* Returns the y-coordinate of the result of multiplying the point (x, y, z)
|
752
|
+
* by this matrix.
|
753
|
+
* @param x
|
754
|
+
* @param y
|
755
|
+
* @param z
|
756
|
+
* @return
|
757
|
+
*/
|
758
|
+
public float multY(float x, float y, float z) {
|
759
|
+
return m10*x + m11*y + m12*z + m13;
|
760
|
+
}
|
761
|
+
|
762
|
+
|
763
|
+
/**
|
764
|
+
* Returns the z-coordinate of the result of multiplying the point (x, y, z)
|
765
|
+
* by this matrix.
|
766
|
+
* @param x
|
767
|
+
* @param y
|
768
|
+
* @param z
|
769
|
+
* @return
|
770
|
+
*/
|
771
|
+
public float multZ(float x, float y, float z) {
|
772
|
+
return m20*x + m21*y + m22*z + m23;
|
773
|
+
}
|
774
|
+
|
775
|
+
|
776
|
+
/**
|
777
|
+
* Returns the fourth element of the result of multiplying the vector
|
778
|
+
* (x, y, z) by this matrix. (Acts as if w = 1 was supplied.)
|
779
|
+
* @param x
|
780
|
+
* @param y
|
781
|
+
* @param z
|
782
|
+
* @return
|
783
|
+
*/
|
784
|
+
public float multW(float x, float y, float z) {
|
785
|
+
return m30*x + m31*y + m32*z + m33;
|
786
|
+
}
|
787
|
+
|
788
|
+
|
789
|
+
/**
|
790
|
+
* Returns the x-coordinate of the result of multiplying the vector
|
791
|
+
* (x, y, z, w) by this matrix.
|
792
|
+
* @param x
|
793
|
+
* @param y
|
794
|
+
* @param z
|
795
|
+
* @param w
|
796
|
+
* @return
|
797
|
+
*/
|
798
|
+
public float multX(float x, float y, float z, float w) {
|
799
|
+
return m00*x + m01*y + m02*z + m03*w;
|
800
|
+
}
|
801
|
+
|
802
|
+
|
803
|
+
/**
|
804
|
+
* Returns the y-coordinate of the result of multiplying the vector
|
805
|
+
* (x, y, z, w) by this matrix.
|
806
|
+
* @param x
|
807
|
+
* @param w
|
808
|
+
* @param y
|
809
|
+
* @param z
|
810
|
+
* @return
|
811
|
+
*/
|
812
|
+
public float multY(float x, float y, float z, float w) {
|
813
|
+
return m10*x + m11*y + m12*z + m13*w;
|
814
|
+
}
|
815
|
+
|
816
|
+
|
817
|
+
/**
|
818
|
+
* Returns the z-coordinate of the result of multiplying the vector
|
819
|
+
* (x, y, z, w) by this matrix.
|
820
|
+
* @param x
|
821
|
+
* @param y
|
822
|
+
* @param z
|
823
|
+
* @param w
|
824
|
+
* @return
|
825
|
+
*/
|
826
|
+
public float multZ(float x, float y, float z, float w) {
|
827
|
+
return m20*x + m21*y + m22*z + m23*w;
|
828
|
+
}
|
829
|
+
|
830
|
+
|
831
|
+
/**
|
832
|
+
* Returns the w-coordinate of the result of multiplying the vector
|
833
|
+
* (x, y, z, w) by this matrix.
|
834
|
+
* @param x
|
835
|
+
* @param y
|
836
|
+
* @param z
|
837
|
+
* @param w
|
838
|
+
* @return
|
839
|
+
*/
|
840
|
+
public float multW(float x, float y, float z, float w) {
|
841
|
+
return m30*x + m31*y + m32*z + m33*w;
|
842
|
+
}
|
843
|
+
|
844
|
+
|
845
|
+
/**
|
846
|
+
* Transpose this matrix; rows become columns and columns rows.
|
847
|
+
*/
|
848
|
+
public void transpose() {
|
849
|
+
float temp;
|
850
|
+
temp = m01; m01 = m10; m10 = temp;
|
851
|
+
temp = m02; m02 = m20; m20 = temp;
|
852
|
+
temp = m03; m03 = m30; m30 = temp;
|
853
|
+
temp = m12; m12 = m21; m21 = temp;
|
854
|
+
temp = m13; m13 = m31; m31 = temp;
|
855
|
+
temp = m23; m23 = m32; m32 = temp;
|
856
|
+
}
|
857
|
+
|
858
|
+
|
859
|
+
/**
|
860
|
+
* Invert this matrix. Will not necessarily succeed, because some matrices
|
861
|
+
* map more than one point to the same image point, and so are irreversible.
|
862
|
+
* @return true if successful
|
863
|
+
*/
|
864
|
+
public boolean invert() {
|
865
|
+
float determinant = determinant();
|
866
|
+
if (determinant == 0) {
|
867
|
+
return false;
|
868
|
+
}
|
869
|
+
|
870
|
+
// first row
|
871
|
+
float t00 = determinant3x3(m11, m12, m13, m21, m22, m23, m31, m32, m33);
|
872
|
+
float t01 = -determinant3x3(m10, m12, m13, m20, m22, m23, m30, m32, m33);
|
873
|
+
float t02 = determinant3x3(m10, m11, m13, m20, m21, m23, m30, m31, m33);
|
874
|
+
float t03 = -determinant3x3(m10, m11, m12, m20, m21, m22, m30, m31, m32);
|
875
|
+
|
876
|
+
// second row
|
877
|
+
float t10 = -determinant3x3(m01, m02, m03, m21, m22, m23, m31, m32, m33);
|
878
|
+
float t11 = determinant3x3(m00, m02, m03, m20, m22, m23, m30, m32, m33);
|
879
|
+
float t12 = -determinant3x3(m00, m01, m03, m20, m21, m23, m30, m31, m33);
|
880
|
+
float t13 = determinant3x3(m00, m01, m02, m20, m21, m22, m30, m31, m32);
|
881
|
+
|
882
|
+
// third row
|
883
|
+
float t20 = determinant3x3(m01, m02, m03, m11, m12, m13, m31, m32, m33);
|
884
|
+
float t21 = -determinant3x3(m00, m02, m03, m10, m12, m13, m30, m32, m33);
|
885
|
+
float t22 = determinant3x3(m00, m01, m03, m10, m11, m13, m30, m31, m33);
|
886
|
+
float t23 = -determinant3x3(m00, m01, m02, m10, m11, m12, m30, m31, m32);
|
887
|
+
|
888
|
+
// fourth row
|
889
|
+
float t30 = -determinant3x3(m01, m02, m03, m11, m12, m13, m21, m22, m23);
|
890
|
+
float t31 = determinant3x3(m00, m02, m03, m10, m12, m13, m20, m22, m23);
|
891
|
+
float t32 = -determinant3x3(m00, m01, m03, m10, m11, m13, m20, m21, m23);
|
892
|
+
float t33 = determinant3x3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
|
893
|
+
|
894
|
+
// transpose and divide by the determinant
|
895
|
+
m00 = t00 / determinant;
|
896
|
+
m01 = t10 / determinant;
|
897
|
+
m02 = t20 / determinant;
|
898
|
+
m03 = t30 / determinant;
|
899
|
+
|
900
|
+
m10 = t01 / determinant;
|
901
|
+
m11 = t11 / determinant;
|
902
|
+
m12 = t21 / determinant;
|
903
|
+
m13 = t31 / determinant;
|
904
|
+
|
905
|
+
m20 = t02 / determinant;
|
906
|
+
m21 = t12 / determinant;
|
907
|
+
m22 = t22 / determinant;
|
908
|
+
m23 = t32 / determinant;
|
909
|
+
|
910
|
+
m30 = t03 / determinant;
|
911
|
+
m31 = t13 / determinant;
|
912
|
+
m32 = t23 / determinant;
|
913
|
+
m33 = t33 / determinant;
|
914
|
+
|
915
|
+
return true;
|
916
|
+
}
|
917
|
+
|
918
|
+
|
919
|
+
/**
|
920
|
+
* Calculate the determinant of a 3x3 matrix.
|
921
|
+
* @return result
|
922
|
+
*/
|
923
|
+
private float determinant3x3(float t00, float t01, float t02,
|
924
|
+
float t10, float t11, float t12,
|
925
|
+
float t20, float t21, float t22) {
|
926
|
+
return (t00 * (t11 * t22 - t12 * t21) +
|
927
|
+
t01 * (t12 * t20 - t10 * t22) +
|
928
|
+
t02 * (t10 * t21 - t11 * t20));
|
929
|
+
}
|
930
|
+
|
931
|
+
|
932
|
+
/**
|
933
|
+
* @return the determinant of the matrix
|
934
|
+
*/
|
935
|
+
public float determinant() {
|
936
|
+
float f =
|
937
|
+
m00
|
938
|
+
* ((m11 * m22 * m33 + m12 * m23 * m31 + m13 * m21 * m32)
|
939
|
+
- m13 * m22 * m31
|
940
|
+
- m11 * m23 * m32
|
941
|
+
- m12 * m21 * m33);
|
942
|
+
f -= m01
|
943
|
+
* ((m10 * m22 * m33 + m12 * m23 * m30 + m13 * m20 * m32)
|
944
|
+
- m13 * m22 * m30
|
945
|
+
- m10 * m23 * m32
|
946
|
+
- m12 * m20 * m33);
|
947
|
+
f += m02
|
948
|
+
* ((m10 * m21 * m33 + m11 * m23 * m30 + m13 * m20 * m31)
|
949
|
+
- m13 * m21 * m30
|
950
|
+
- m10 * m23 * m31
|
951
|
+
- m11 * m20 * m33);
|
952
|
+
f -= m03
|
953
|
+
* ((m10 * m21 * m32 + m11 * m22 * m30 + m12 * m20 * m31)
|
954
|
+
- m12 * m21 * m30
|
955
|
+
- m10 * m22 * m31
|
956
|
+
- m11 * m20 * m32);
|
957
|
+
return f;
|
958
|
+
}
|
959
|
+
|
960
|
+
|
961
|
+
//////////////////////////////////////////////////////////////
|
962
|
+
|
963
|
+
// REVERSE VERSIONS OF MATRIX OPERATIONS
|
964
|
+
|
965
|
+
// These functions should not be used, as they will be removed in the future.
|
966
|
+
|
967
|
+
/**
|
968
|
+
*
|
969
|
+
* @param tx
|
970
|
+
* @param ty
|
971
|
+
* @param tz
|
972
|
+
*/
|
973
|
+
|
974
|
+
|
975
|
+
protected void invTranslate(float tx, float ty, float tz) {
|
976
|
+
preApply(1, 0, 0, -tx,
|
977
|
+
0, 1, 0, -ty,
|
978
|
+
0, 0, 1, -tz,
|
979
|
+
0, 0, 0, 1);
|
980
|
+
}
|
981
|
+
|
982
|
+
/**
|
983
|
+
*
|
984
|
+
* @param angle
|
985
|
+
*/
|
986
|
+
protected void invRotateX(float angle) {
|
987
|
+
float c = cos(-angle);
|
988
|
+
float s = sin(-angle);
|
989
|
+
preApply(1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1);
|
990
|
+
}
|
991
|
+
|
992
|
+
/**
|
993
|
+
*
|
994
|
+
* @param angle
|
995
|
+
*/
|
996
|
+
protected void invRotateY(float angle) {
|
997
|
+
float c = cos(-angle);
|
998
|
+
float s = sin(-angle);
|
999
|
+
preApply(c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1);
|
1000
|
+
}
|
1001
|
+
|
1002
|
+
/**
|
1003
|
+
*
|
1004
|
+
* @param angle
|
1005
|
+
*/
|
1006
|
+
protected void invRotateZ(float angle) {
|
1007
|
+
float c = cos(-angle);
|
1008
|
+
float s = sin(-angle);
|
1009
|
+
preApply(c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
|
1010
|
+
}
|
1011
|
+
|
1012
|
+
/**
|
1013
|
+
*
|
1014
|
+
* @param angle
|
1015
|
+
* @param v0
|
1016
|
+
* @param v1
|
1017
|
+
* @param v2
|
1018
|
+
*/
|
1019
|
+
protected void invRotate(float angle, float v0, float v1, float v2) {
|
1020
|
+
//TODO should make sure this vector is normalized
|
1021
|
+
|
1022
|
+
float c = cos(-angle);
|
1023
|
+
float s = sin(-angle);
|
1024
|
+
float t = 1.0f - c;
|
1025
|
+
|
1026
|
+
preApply((t*v0*v0) + c, (t*v0*v1) - (s*v2), (t*v0*v2) + (s*v1), 0,
|
1027
|
+
(t*v0*v1) + (s*v2), (t*v1*v1) + c, (t*v1*v2) - (s*v0), 0,
|
1028
|
+
(t*v0*v2) - (s*v1), (t*v1*v2) + (s*v0), (t*v2*v2) + c, 0,
|
1029
|
+
0, 0, 0, 1);
|
1030
|
+
}
|
1031
|
+
|
1032
|
+
/**
|
1033
|
+
*
|
1034
|
+
* @param x
|
1035
|
+
* @param y
|
1036
|
+
* @param z
|
1037
|
+
*/
|
1038
|
+
protected void invScale(float x, float y, float z) {
|
1039
|
+
preApply(1/x, 0, 0, 0, 0, 1/y, 0, 0, 0, 0, 1/z, 0, 0, 0, 0, 1);
|
1040
|
+
}
|
1041
|
+
|
1042
|
+
/**
|
1043
|
+
*
|
1044
|
+
* @param n00
|
1045
|
+
* @param n01
|
1046
|
+
* @param n02
|
1047
|
+
* @param n03
|
1048
|
+
* @param n10
|
1049
|
+
* @param n11
|
1050
|
+
* @param n12
|
1051
|
+
* @param n13
|
1052
|
+
* @param n20
|
1053
|
+
* @param n21
|
1054
|
+
* @param n22
|
1055
|
+
* @param n23
|
1056
|
+
* @param n30
|
1057
|
+
* @param n31
|
1058
|
+
* @param n32
|
1059
|
+
* @param n33
|
1060
|
+
* @return
|
1061
|
+
*/
|
1062
|
+
protected boolean invApply(float n00, float n01, float n02, float n03,
|
1063
|
+
float n10, float n11, float n12, float n13,
|
1064
|
+
float n20, float n21, float n22, float n23,
|
1065
|
+
float n30, float n31, float n32, float n33) {
|
1066
|
+
if (inverseCopy == null) {
|
1067
|
+
inverseCopy = new PMatrix3D();
|
1068
|
+
}
|
1069
|
+
inverseCopy.set(n00, n01, n02, n03,
|
1070
|
+
n10, n11, n12, n13,
|
1071
|
+
n20, n21, n22, n23,
|
1072
|
+
n30, n31, n32, n33);
|
1073
|
+
if (!inverseCopy.invert()) {
|
1074
|
+
return false;
|
1075
|
+
}
|
1076
|
+
preApply(inverseCopy);
|
1077
|
+
return true;
|
1078
|
+
}
|
1079
|
+
|
1080
|
+
|
1081
|
+
//////////////////////////////////////////////////////////////
|
1082
|
+
|
1083
|
+
/**
|
1084
|
+
*
|
1085
|
+
*/
|
1086
|
+
|
1087
|
+
|
1088
|
+
public void print() {
|
1089
|
+
/*
|
1090
|
+
System.out.println(m00 + " " + m01 + " " + m02 + " " + m03 + "\n" +
|
1091
|
+
m10 + " " + m11 + " " + m12 + " " + m13 + "\n" +
|
1092
|
+
m20 + " " + m21 + " " + m22 + " " + m23 + "\n" +
|
1093
|
+
m30 + " " + m31 + " " + m32 + " " + m33 + "\n");
|
1094
|
+
*/
|
1095
|
+
int big = (int) Math.abs(max(max(max(max(abs(m00), abs(m01)),
|
1096
|
+
max(abs(m02), abs(m03))),
|
1097
|
+
max(max(abs(m10), abs(m11)),
|
1098
|
+
max(abs(m12), abs(m13)))),
|
1099
|
+
max(max(max(abs(m20), abs(m21)),
|
1100
|
+
max(abs(m22), abs(m23))),
|
1101
|
+
max(max(abs(m30), abs(m31)),
|
1102
|
+
max(abs(m32), abs(m33))))));
|
1103
|
+
|
1104
|
+
int digits = 1;
|
1105
|
+
if (Float.isNaN(big) || Float.isInfinite(big)) { // avoid infinite loop
|
1106
|
+
digits = 5;
|
1107
|
+
} else {
|
1108
|
+
while ((big /= 10) != 0) digits++; // cheap log()
|
1109
|
+
}
|
1110
|
+
|
1111
|
+
System.out.println(PApplet.nfs(m00, digits, 4) + " " +
|
1112
|
+
PApplet.nfs(m01, digits, 4) + " " +
|
1113
|
+
PApplet.nfs(m02, digits, 4) + " " +
|
1114
|
+
PApplet.nfs(m03, digits, 4));
|
1115
|
+
|
1116
|
+
System.out.println(PApplet.nfs(m10, digits, 4) + " " +
|
1117
|
+
PApplet.nfs(m11, digits, 4) + " " +
|
1118
|
+
PApplet.nfs(m12, digits, 4) + " " +
|
1119
|
+
PApplet.nfs(m13, digits, 4));
|
1120
|
+
|
1121
|
+
System.out.println(PApplet.nfs(m20, digits, 4) + " " +
|
1122
|
+
PApplet.nfs(m21, digits, 4) + " " +
|
1123
|
+
PApplet.nfs(m22, digits, 4) + " " +
|
1124
|
+
PApplet.nfs(m23, digits, 4));
|
1125
|
+
|
1126
|
+
System.out.println(PApplet.nfs(m30, digits, 4) + " " +
|
1127
|
+
PApplet.nfs(m31, digits, 4) + " " +
|
1128
|
+
PApplet.nfs(m32, digits, 4) + " " +
|
1129
|
+
PApplet.nfs(m33, digits, 4));
|
1130
|
+
|
1131
|
+
System.out.println();
|
1132
|
+
}
|
1133
|
+
|
1134
|
+
|
1135
|
+
//////////////////////////////////////////////////////////////
|
1136
|
+
|
1137
|
+
|
1138
|
+
static private final float max(float a, float b) {
|
1139
|
+
return (a > b) ? a : b;
|
1140
|
+
}
|
1141
|
+
|
1142
|
+
static private final float abs(float a) {
|
1143
|
+
return (a < 0) ? -a : a;
|
1144
|
+
}
|
1145
|
+
|
1146
|
+
static private final float sin(float angle) {
|
1147
|
+
return (float) Math.sin(angle);
|
1148
|
+
}
|
1149
|
+
|
1150
|
+
static private final float cos(float angle) {
|
1151
|
+
return (float) Math.cos(angle);
|
1152
|
+
}
|
1153
|
+
}
|