propane 3.6.0-java → 3.10.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +1 -1
  3. data/.travis.yml +1 -1
  4. data/CHANGELOG.md +5 -1
  5. data/README.md +6 -13
  6. data/Rakefile +7 -6
  7. data/lib/java/japplemenubar/JAppleMenuBar.java +88 -0
  8. data/lib/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
  9. data/lib/java/monkstone/ColorUtil.java +127 -0
  10. data/lib/java/monkstone/MathToolModule.java +287 -0
  11. data/lib/java/monkstone/PropaneLibrary.java +46 -0
  12. data/lib/java/monkstone/core/LibraryProxy.java +136 -0
  13. data/lib/java/monkstone/fastmath/DegLutTables.java +111 -0
  14. data/lib/java/monkstone/fastmath/Deglut.java +71 -0
  15. data/lib/java/monkstone/fastmath/package-info.java +6 -0
  16. data/lib/java/monkstone/filechooser/Chooser.java +39 -0
  17. data/lib/java/monkstone/noise/FastTerrain.java +874 -0
  18. data/lib/java/monkstone/noise/Noise.java +90 -0
  19. data/lib/java/monkstone/noise/NoiseGenerator.java +75 -0
  20. data/lib/java/monkstone/noise/NoiseMode.java +28 -0
  21. data/lib/java/monkstone/noise/OpenSimplex2F.java +881 -0
  22. data/lib/java/monkstone/noise/OpenSimplex2S.java +1106 -0
  23. data/lib/java/monkstone/noise/SmoothTerrain.java +1099 -0
  24. data/lib/java/monkstone/slider/CustomHorizontalSlider.java +164 -0
  25. data/lib/java/monkstone/slider/CustomVerticalSlider.java +178 -0
  26. data/lib/java/monkstone/slider/SimpleHorizontalSlider.java +145 -0
  27. data/lib/java/monkstone/slider/SimpleSlider.java +166 -0
  28. data/lib/java/monkstone/slider/SimpleVerticalSlider.java +157 -0
  29. data/lib/java/monkstone/slider/Slider.java +61 -0
  30. data/lib/java/monkstone/slider/SliderBar.java +245 -0
  31. data/lib/java/monkstone/slider/SliderGroup.java +56 -0
  32. data/lib/java/monkstone/slider/WheelHandler.java +35 -0
  33. data/lib/java/monkstone/vecmath/GfxRender.java +86 -0
  34. data/lib/java/monkstone/vecmath/JRender.java +56 -0
  35. data/lib/java/monkstone/vecmath/ShapeRender.java +87 -0
  36. data/lib/java/monkstone/vecmath/package-info.java +20 -0
  37. data/lib/java/monkstone/vecmath/vec2/Vec2.java +802 -0
  38. data/lib/java/monkstone/vecmath/vec2/package-info.java +6 -0
  39. data/lib/java/monkstone/vecmath/vec3/Vec3.java +727 -0
  40. data/lib/java/monkstone/vecmath/vec3/package-info.java +6 -0
  41. data/lib/java/monkstone/videoevent/CaptureEvent.java +27 -0
  42. data/lib/java/monkstone/videoevent/MovieEvent.java +32 -0
  43. data/lib/java/monkstone/videoevent/package-info.java +20 -0
  44. data/lib/java/processing/awt/PGraphicsJava2D.java +3040 -0
  45. data/lib/java/processing/awt/PImageAWT.java +377 -0
  46. data/lib/java/processing/awt/PShapeJava2D.java +387 -0
  47. data/lib/java/processing/awt/PSurfaceAWT.java +1581 -0
  48. data/lib/java/processing/awt/ShimAWT.java +581 -0
  49. data/lib/java/processing/core/PApplet.java +15156 -0
  50. data/lib/java/processing/core/PConstants.java +523 -0
  51. data/lib/java/processing/core/PFont.java +1126 -0
  52. data/lib/java/processing/core/PGraphics.java +8600 -0
  53. data/lib/java/processing/core/PImage.java +3377 -0
  54. data/lib/java/processing/core/PMatrix.java +208 -0
  55. data/lib/java/processing/core/PMatrix2D.java +562 -0
  56. data/lib/java/processing/core/PMatrix3D.java +890 -0
  57. data/lib/java/processing/core/PShape.java +3561 -0
  58. data/lib/java/processing/core/PShapeOBJ.java +483 -0
  59. data/lib/java/processing/core/PShapeSVG.java +2016 -0
  60. data/lib/java/processing/core/PStyle.java +63 -0
  61. data/lib/java/processing/core/PSurface.java +198 -0
  62. data/lib/java/processing/core/PSurfaceNone.java +431 -0
  63. data/lib/java/processing/core/PVector.java +1066 -0
  64. data/lib/java/processing/core/ThinkDifferent.java +115 -0
  65. data/lib/java/processing/data/DoubleDict.java +850 -0
  66. data/lib/java/processing/data/DoubleList.java +928 -0
  67. data/lib/java/processing/data/FloatDict.java +847 -0
  68. data/lib/java/processing/data/FloatList.java +936 -0
  69. data/lib/java/processing/data/IntDict.java +807 -0
  70. data/lib/java/processing/data/IntList.java +936 -0
  71. data/lib/java/processing/data/JSONArray.java +1260 -0
  72. data/lib/java/processing/data/JSONObject.java +2282 -0
  73. data/lib/java/processing/data/JSONTokener.java +435 -0
  74. data/lib/java/processing/data/LongDict.java +802 -0
  75. data/lib/java/processing/data/LongList.java +937 -0
  76. data/lib/java/processing/data/Sort.java +46 -0
  77. data/lib/java/processing/data/StringDict.java +613 -0
  78. data/lib/java/processing/data/StringList.java +800 -0
  79. data/lib/java/processing/data/Table.java +4936 -0
  80. data/lib/java/processing/data/TableRow.java +198 -0
  81. data/lib/java/processing/data/XML.java +1156 -0
  82. data/lib/java/processing/dxf/RawDXF.java +404 -0
  83. data/lib/java/processing/event/Event.java +125 -0
  84. data/lib/java/processing/event/KeyEvent.java +70 -0
  85. data/lib/java/processing/event/MouseEvent.java +114 -0
  86. data/lib/java/processing/event/TouchEvent.java +57 -0
  87. data/lib/java/processing/javafx/PGraphicsFX2D.java +32 -0
  88. data/lib/java/processing/javafx/PSurfaceFX.java +173 -0
  89. data/lib/java/processing/net/Client.java +744 -0
  90. data/lib/java/processing/net/Server.java +388 -0
  91. data/lib/java/processing/opengl/FontTexture.java +378 -0
  92. data/lib/java/processing/opengl/FrameBuffer.java +513 -0
  93. data/lib/java/processing/opengl/LinePath.java +627 -0
  94. data/lib/java/processing/opengl/LineStroker.java +681 -0
  95. data/lib/java/processing/opengl/PGL.java +3483 -0
  96. data/lib/java/processing/opengl/PGraphics2D.java +615 -0
  97. data/lib/java/processing/opengl/PGraphics3D.java +281 -0
  98. data/lib/java/processing/opengl/PGraphicsOpenGL.java +13753 -0
  99. data/lib/java/processing/opengl/PJOGL.java +2008 -0
  100. data/lib/java/processing/opengl/PShader.java +1484 -0
  101. data/lib/java/processing/opengl/PShapeOpenGL.java +5269 -0
  102. data/lib/java/processing/opengl/PSurfaceJOGL.java +1385 -0
  103. data/lib/java/processing/opengl/Texture.java +1696 -0
  104. data/lib/java/processing/opengl/VertexBuffer.java +88 -0
  105. data/lib/java/processing/opengl/cursors/arrow.png +0 -0
  106. data/lib/java/processing/opengl/cursors/cross.png +0 -0
  107. data/lib/java/processing/opengl/cursors/hand.png +0 -0
  108. data/lib/java/processing/opengl/cursors/license.txt +27 -0
  109. data/lib/java/processing/opengl/cursors/move.png +0 -0
  110. data/lib/java/processing/opengl/cursors/text.png +0 -0
  111. data/lib/java/processing/opengl/cursors/wait.png +0 -0
  112. data/lib/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
  113. data/lib/java/processing/opengl/shaders/ColorVert.glsl +34 -0
  114. data/lib/java/processing/opengl/shaders/LightFrag.glsl +33 -0
  115. data/lib/java/processing/opengl/shaders/LightVert.glsl +151 -0
  116. data/lib/java/processing/opengl/shaders/LineFrag.glsl +32 -0
  117. data/lib/java/processing/opengl/shaders/LineVert.glsl +100 -0
  118. data/lib/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
  119. data/lib/java/processing/opengl/shaders/PointFrag.glsl +32 -0
  120. data/lib/java/processing/opengl/shaders/PointVert.glsl +56 -0
  121. data/lib/java/processing/opengl/shaders/TexFrag.glsl +37 -0
  122. data/lib/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
  123. data/lib/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
  124. data/lib/java/processing/opengl/shaders/TexVert.glsl +38 -0
  125. data/lib/java/processing/pdf/PGraphicsPDF.java +581 -0
  126. data/lib/java/processing/svg/PGraphicsSVG.java +378 -0
  127. data/lib/propane/app.rb +9 -10
  128. data/lib/propane/runner.rb +10 -12
  129. data/lib/propane/version.rb +1 -1
  130. data/library/pdf/pdf.rb +7 -0
  131. data/library/svg/svg.rb +7 -0
  132. data/mvnw +3 -3
  133. data/mvnw.cmd +2 -2
  134. data/pom.rb +30 -3
  135. data/pom.xml +54 -3
  136. data/propane.gemspec +7 -3
  137. data/src/main/java/monkstone/FastNoiseModuleJava.java +127 -0
  138. data/src/main/java/monkstone/MathToolModule.java +30 -30
  139. data/src/main/java/monkstone/PropaneLibrary.java +2 -0
  140. data/src/main/java/monkstone/SmoothNoiseModuleJava.java +127 -0
  141. data/src/main/java/monkstone/fastmath/DegLutTables.java +111 -0
  142. data/src/main/java/monkstone/fastmath/Deglut.java +6 -56
  143. data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
  144. data/src/main/java/monkstone/noise/OpenSimplex2F.java +813 -0
  145. data/src/main/java/monkstone/noise/OpenSimplex2S.java +1138 -0
  146. data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
  147. data/src/main/java/monkstone/vecmath/JRender.java +6 -6
  148. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +20 -19
  149. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +12 -12
  150. data/src/main/java/processing/awt/PGraphicsJava2D.java +11 -3
  151. data/src/main/java/processing/core/PApplet.java +13242 -13374
  152. data/src/main/java/processing/core/PConstants.java +155 -163
  153. data/src/main/java/processing/core/PGraphics.java +118 -111
  154. data/src/main/java/processing/opengl/PJOGL.java +6 -5
  155. data/src/main/java/processing/pdf/PGraphicsPDF.java +581 -0
  156. data/src/main/java/processing/svg/PGraphicsSVG.java +378 -0
  157. data/test/deglut_spec_test.rb +2 -2
  158. data/vendors/Rakefile +1 -1
  159. metadata +146 -17
  160. data/library/simplex_noise/simplex_noise.rb +0 -5
  161. data/src/main/java/monkstone/noise/SimplexNoise.java +0 -436
@@ -0,0 +1,6 @@
1
+ /*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+ package monkstone.vecmath.vec2;
@@ -0,0 +1,727 @@
1
+ package monkstone.vecmath.vec3;
2
+
3
+ /*
4
+ * Copyright (c) 2015-20 Martin Prout
5
+ *
6
+ * This library is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * http://creativecommons.org/licenses/LGPL/2.1/
12
+ *
13
+ * This library is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
+ * Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public
19
+ * License along with this library; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
+ */
22
+ import org.jruby.Ruby;
23
+ import org.jruby.RubyArray;
24
+ import org.jruby.RubyClass;
25
+ import org.jruby.RubyFixnum;
26
+ import org.jruby.RubyFloat;
27
+ import org.jruby.RubyObject;
28
+ import org.jruby.RubySymbol;
29
+ import org.jruby.anno.JRubyClass;
30
+ import org.jruby.anno.JRubyMethod;
31
+ import org.jruby.runtime.Block;
32
+ import org.jruby.runtime.ThreadContext;
33
+ import org.jruby.runtime.builtin.IRubyObject;
34
+ import monkstone.vecmath.JRender;
35
+ import monkstone.vecmath.vec2.Vec2;
36
+
37
+ /**
38
+ *
39
+ * @author Martin Prout
40
+ */
41
+ @JRubyClass(name = "Vec3D")
42
+ public final class Vec3 extends RubyObject {
43
+
44
+ private static final long serialVersionUID = 4563517343762009867L;
45
+
46
+ /**
47
+ *
48
+ * @param runtime Ruby
49
+ */
50
+ public static void createVec3(final Ruby runtime) {
51
+ RubyClass vec3Cls = runtime.defineClass("Vec3D", runtime.getObject(), (Ruby runtime1, RubyClass rubyClass) -> new Vec3(runtime1, rubyClass));
52
+ vec3Cls.defineAnnotatedMethods(Vec3.class);
53
+ }
54
+
55
+ static final double EPSILON = 9.999999747378752e-05; // matches processing.org EPSILON
56
+ double jx = 0;
57
+ double jy = 0;
58
+ double jz = 0;
59
+
60
+ /**
61
+ *
62
+ * @param context ThreadContext
63
+ * @param klazz IRubyObject
64
+ * @param args optional (no args jx = 0, jy = 0, jz = 0) (2 args jz = 0)
65
+ * @return new Vec3 object (ruby)
66
+ */
67
+ @JRubyMethod(name = "new", meta = true, rest = true)
68
+ public final static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject... args) {
69
+ Vec3 vec = (Vec3) ((RubyClass) klazz).allocate();
70
+ vec.init(context, args);
71
+ return vec;
72
+ }
73
+
74
+ /**
75
+ *
76
+ * @param runtime Ruby
77
+ * @param klass RubyClass
78
+ */
79
+ public Vec3(Ruby runtime, RubyClass klass) {
80
+ super(runtime, klass);
81
+ }
82
+
83
+ void init(ThreadContext context, IRubyObject... args) {
84
+ int count = args.length;
85
+ if (count >= 2) {
86
+ jx = (args[0] instanceof RubyFloat)
87
+ ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
88
+ jy = (args[1] instanceof RubyFloat)
89
+ ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
90
+ }
91
+ if (count == 3) {
92
+ jz = (args[2] instanceof RubyFloat)
93
+ ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
94
+ } // allow ruby ducktyping in constructor
95
+ if (count == 1) {
96
+ if (!(args[0].respondsTo("x"))) {
97
+ throw context.runtime.newTypeError(args[0].getType() + " doesn't respond_to :x & :y");
98
+ }
99
+ jx = ((args[0].callMethod(context, "x")) instanceof RubyFloat)
100
+ ? ((RubyFloat) args[0].callMethod(context, "x")).getValue() : ((RubyFixnum) args[0].callMethod(context, "x")).getDoubleValue();
101
+ jy = ((args[0].callMethod(context, "y")) instanceof RubyFloat)
102
+ ? ((RubyFloat) args[0].callMethod(context, "y")).getValue() : ((RubyFixnum) args[0].callMethod(context, "y")).getDoubleValue();
103
+ if (!(args[0].respondsTo("z"))) {
104
+ return;
105
+ } // allow promotion from 2D to 3D, sets jz = 0
106
+ jz = ((args[0].callMethod(context, "z")) instanceof RubyFloat) ? ((RubyFloat) args[0].callMethod(context, "z")).getValue() : ((RubyFixnum) args[0].callMethod(context, "z")).getDoubleValue();
107
+ }
108
+ }
109
+
110
+ /**
111
+ *
112
+ * @param context ThreadContext
113
+ * @return x IRubyObject
114
+ */
115
+ @JRubyMethod(name = "x")
116
+
117
+ public IRubyObject getX(ThreadContext context) {
118
+ return context.runtime.newFloat(jx);
119
+ }
120
+
121
+ /**
122
+ *
123
+ * @param context ThreadContext
124
+ * @return y IRubyObject
125
+ */
126
+ @JRubyMethod(name = "y")
127
+ public IRubyObject getY(ThreadContext context) {
128
+ return context.runtime.newFloat(jy);
129
+ }
130
+
131
+ /**
132
+ *
133
+ * @param context ThreadContext
134
+ * @return z IRubyObject
135
+ */
136
+ @JRubyMethod(name = "z")
137
+ public IRubyObject getZ(ThreadContext context
138
+ ) {
139
+ return context.runtime.newFloat(jz);
140
+ }
141
+
142
+ /**
143
+ *
144
+ * @param context ThreadContext
145
+ * @param other IRubyObject
146
+ * @return x IRubyObject
147
+ */
148
+ @JRubyMethod(name = "x=")
149
+ public IRubyObject setX(ThreadContext context, IRubyObject other) {
150
+ if (other instanceof RubyFloat) {
151
+ jx = ((RubyFloat) other).getValue();
152
+ } else {
153
+ jx = ((RubyFixnum) other).getDoubleValue();
154
+ }
155
+ return other;
156
+ }
157
+
158
+ /**
159
+ *
160
+ * @param context ThreadContext
161
+ * @param other IRubyObject
162
+ * @return y IRubyObject
163
+ */
164
+ @JRubyMethod(name = "y=")
165
+ public IRubyObject setY(ThreadContext context, IRubyObject other) {
166
+ if (other instanceof RubyFloat) {
167
+ jy = ((RubyFloat) other).getValue();
168
+ } else {
169
+ jy = ((RubyFixnum) other).getDoubleValue();
170
+ }
171
+ return other;
172
+ }
173
+
174
+ /**
175
+ *
176
+ * @param context ThreadContext
177
+ * @param other IRubyObject
178
+ * @return z IRubyObject
179
+ */
180
+ @JRubyMethod(name = "z=")
181
+ public IRubyObject setZ(ThreadContext context, IRubyObject other) {
182
+ if (other instanceof RubyFloat) {
183
+ jz = ((RubyFloat) other).getValue();
184
+ } else {
185
+ jz = ((RubyFixnum) other).getDoubleValue();
186
+ }
187
+ return other;
188
+ }
189
+
190
+ /**
191
+ *
192
+ * @param context ThreadContext
193
+ * @param key as symbol
194
+ * @return value float
195
+ */
196
+ @JRubyMethod(name = "[]", required = 1)
197
+ public IRubyObject aref(ThreadContext context, IRubyObject key) {
198
+ Ruby runtime = context.runtime;
199
+ if (key instanceof RubySymbol) {
200
+ if (key == RubySymbol.newSymbol(runtime, "x")) {
201
+ return runtime.newFloat(jx);
202
+ } else if (key == RubySymbol.newSymbol(runtime, "y")) {
203
+ return runtime.newFloat(jy);
204
+ } else if (key == RubySymbol.newSymbol(runtime, "z")) {
205
+ return runtime.newFloat(jz);
206
+ } else {
207
+ throw runtime.newIndexError("invalid key");
208
+ }
209
+ } else {
210
+ throw runtime.newIndexError("invalid key");
211
+ }
212
+ }
213
+
214
+ /**
215
+ * @param context ThreadContext
216
+ * @param key as symbol
217
+ * @param value as float
218
+ * @return value float
219
+ */
220
+ @JRubyMethod(name = "[]=")
221
+ public IRubyObject aset(ThreadContext context, IRubyObject key, IRubyObject value) {
222
+ Ruby runtime = context.runtime;
223
+ if (key instanceof RubySymbol) {
224
+ if (key == RubySymbol.newSymbol(runtime, "x")) {
225
+ jx = (value instanceof RubyFloat)
226
+ ? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue();
227
+ } else if (key == RubySymbol.newSymbol(runtime, "y")) {
228
+ jy = (value instanceof RubyFloat)
229
+ ? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue();
230
+ } else if (key == RubySymbol.newSymbol(runtime, "z")) {
231
+ jz = (value instanceof RubyFloat)
232
+ ? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue();
233
+ } else {
234
+ throw runtime.newIndexError("invalid key");
235
+ }
236
+ } else {
237
+ throw runtime.newIndexError("invalid key");
238
+ }
239
+ return value;
240
+ }
241
+
242
+ /**
243
+ *
244
+ * @param context ThreadContext
245
+ * @param other IRubyObject
246
+ * @return distance float
247
+ */
248
+ @JRubyMethod(name = "dist", required = 1)
249
+
250
+ public IRubyObject dist(ThreadContext context, IRubyObject other) {
251
+ Vec3 b = null;
252
+ if (other instanceof Vec3) {
253
+ b = (Vec3) other.toJava(Vec3.class);
254
+ } else {
255
+ throw context.runtime.newTypeError("argument should be Vec3D");
256
+ }
257
+ double result = Math.sqrt((jx - b.jx) * (jx - b.jx) + (jy - b.jy) * (jy - b.jy) + (jz - b.jz) * (jz - b.jz));
258
+ return context.runtime.newFloat(result);
259
+ }
260
+
261
+ /**
262
+ *
263
+ * @param context ThreadContext
264
+ * @param other IRubyObject
265
+ * @return distance squared float
266
+ */
267
+ @JRubyMethod(name = "dist_squared", required = 1)
268
+
269
+ public IRubyObject dist_squared(ThreadContext context, IRubyObject other) {
270
+ Vec3 b = null;
271
+ if (other instanceof Vec3) {
272
+ b = (Vec3) other.toJava(Vec3.class);
273
+ } else {
274
+ throw context.runtime.newTypeError("argument should be Vec3D");
275
+ }
276
+ double result = (jx - b.jx) * (jx - b.jx) + (jy - b.jy) * (jy - b.jy) + (jz - b.jz) * (jz - b.jz);
277
+ return context.runtime.newFloat(result);
278
+ }
279
+
280
+ /**
281
+ *
282
+ *
283
+ * @param context ThreadContext
284
+ * @param other IRubyObject
285
+ * @return cross product IRubyObject
286
+ */
287
+ @JRubyMethod(name = "cross", required = 1)
288
+
289
+ public IRubyObject cross(ThreadContext context, IRubyObject other) {
290
+ Ruby runtime = context.runtime;
291
+ Vec3 vec = null;
292
+ if (other instanceof Vec3) {
293
+ vec = (Vec3) other.toJava(Vec3.class);
294
+ } else {
295
+ throw runtime.newTypeError("argument should be Vec3D");
296
+ }
297
+ return Vec3.rbNew(context, other.getMetaClass(), new IRubyObject[]{
298
+ runtime.newFloat(jy * vec.jz - jz * vec.jy),
299
+ runtime.newFloat(jz * vec.jx - jx * vec.jz),
300
+ runtime.newFloat(jx * vec.jy - jy * vec.jx)
301
+ }
302
+ );
303
+ }
304
+
305
+ /**
306
+ *
307
+ * @param context ThreadContext
308
+ * @param other IRubyObject
309
+ * @return dot product IRubyObject
310
+ */
311
+ @JRubyMethod(name = "dot", required = 1)
312
+
313
+ public IRubyObject dot(ThreadContext context, IRubyObject other) {
314
+ Ruby runtime = context.runtime;
315
+ Vec3 b = null;
316
+ if (other instanceof Vec3) {
317
+ b = (Vec3) other.toJava(Vec3.class);
318
+ } else {
319
+ throw runtime.newTypeError("argument should be Vec3D");
320
+ }
321
+ return runtime.newFloat(jx * b.jx + jy * b.jy + jz * b.jz);
322
+ }
323
+
324
+ /**
325
+ *
326
+ * @param context ThreadContext
327
+ * @param other IRubyObject
328
+ * @return new Vec3 object (ruby)
329
+ */
330
+ @JRubyMethod(name = "+", required = 1)
331
+
332
+ public IRubyObject op_add(ThreadContext context, IRubyObject other) {
333
+ Ruby runtime = context.runtime;
334
+ Vec3 b = (Vec3) other.toJava(Vec3.class);
335
+ return Vec3.rbNew(context, other.getMetaClass(), new IRubyObject[]{
336
+ runtime.newFloat(jx + b.jx),
337
+ runtime.newFloat(jy + b.jy),
338
+ runtime.newFloat(jz + b.jz)});
339
+ }
340
+
341
+ /**
342
+ *
343
+ * @param context ThreadContext
344
+ * @param other IRubyObject
345
+ * @return new Vec3 object (ruby)
346
+ */
347
+ @JRubyMethod(name = "-")
348
+ public IRubyObject op_sub(ThreadContext context, IRubyObject other) {
349
+ Ruby runtime = context.runtime;
350
+ Vec3 b = null;
351
+ if (other instanceof Vec3) {
352
+ b = (Vec3) other.toJava(Vec3.class);
353
+ } else {
354
+ throw runtime.newTypeError("argument should be Vec3D");
355
+ }
356
+ return Vec3.rbNew(context, other.getMetaClass(), new IRubyObject[]{
357
+ runtime.newFloat(jx - b.jx),
358
+ runtime.newFloat(jy - b.jy),
359
+ runtime.newFloat(jz - b.jz)});
360
+ }
361
+
362
+ /**
363
+ *
364
+ * @param context ThreadContext
365
+ * @param scalar IRubyObject
366
+ * @return new Vec3 object (ruby)
367
+ */
368
+ @JRubyMethod(name = "*", required = 1)
369
+ public IRubyObject op_mul(ThreadContext context, IRubyObject scalar) {
370
+ Ruby runtime = context.runtime;
371
+ double multi = (scalar instanceof RubyFloat)
372
+ ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
373
+ return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
374
+ runtime.newFloat(jx * multi),
375
+ runtime.newFloat(jy * multi),
376
+ runtime.newFloat(jz * multi)});
377
+ }
378
+
379
+ /**
380
+ *
381
+ * @param context ThreadContext
382
+ * @param scalar IRubyObject
383
+ * @return new Vec3 object (ruby)
384
+ */
385
+ @JRubyMethod(name = "/", required = 1)
386
+ public IRubyObject op_div(ThreadContext context, IRubyObject scalar) {
387
+ Ruby runtime = context.runtime;
388
+ double divisor = (scalar instanceof RubyFloat)
389
+ ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
390
+ if (Math.abs(divisor) < Vec3.EPSILON) {
391
+ return this;
392
+ }
393
+ return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
394
+ runtime.newFloat(jx / divisor),
395
+ runtime.newFloat(jy / divisor),
396
+ runtime.newFloat(jz / divisor)});
397
+ }
398
+
399
+ /**
400
+ *
401
+ * @param context ThreadContext
402
+ * @return magnitude squared IRubyObject
403
+ */
404
+ @JRubyMethod(name = "mag_squared")
405
+ public IRubyObject mag_squared(ThreadContext context) {
406
+ return context.runtime.newFloat(jx * jx + jy * jy + jz * jz);
407
+ }
408
+
409
+ /**
410
+ *
411
+ * @param context ThreadContext
412
+ * @return magnitude IRubyObject
413
+ */
414
+ @JRubyMethod(name = "mag")
415
+ public IRubyObject mag(ThreadContext context) {
416
+ return context.runtime.newFloat(Math.sqrt(jx * jx + jy * jy + jz * jz));
417
+ }
418
+
419
+ /**
420
+ * Call yield if block given, do nothing if yield == false else set_mag to
421
+ * given scalar
422
+ *
423
+ * @param context ThreadContext
424
+ * @param scalar double value to set
425
+ * @param block should return a boolean (optional)
426
+ * @return magnitude IRubyObject
427
+ */
428
+ @JRubyMethod(name = "set_mag")
429
+ public IRubyObject set_mag(ThreadContext context, IRubyObject scalar, Block block) {
430
+ if (block.isGiven()) {
431
+ if (!(boolean) block.yield(context, scalar).toJava(Boolean.class)) {
432
+ return this;
433
+ }
434
+ }
435
+ double new_mag = (scalar instanceof RubyFloat)
436
+ ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
437
+ double current = Math.sqrt(jx * jx + jy * jy + jz * jz);
438
+ if (current > EPSILON) {
439
+ jx *= new_mag / current;
440
+ jy *= new_mag / current;
441
+ jz *= new_mag / current;
442
+ }
443
+ return this;
444
+ }
445
+
446
+ /**
447
+ *
448
+ * @param context ThreadContext
449
+ * @return this as a ruby object
450
+ */
451
+ @JRubyMethod(name = "normalize!")
452
+ public IRubyObject normalize_bang(ThreadContext context) {
453
+ if (Math.abs(jx) < EPSILON && Math.abs(jy) < EPSILON && Math.abs(jz) < EPSILON) {
454
+ return this;
455
+ }
456
+ double mag = Math.sqrt(jx * jx + jy * jy + jz * jz);
457
+ jx /= mag;
458
+ jy /= mag;
459
+ jz /= mag;
460
+ return this;
461
+ }
462
+
463
+ /**
464
+ *
465
+ * @param context ThreadContext
466
+ * @return new normalized Vec3D object (ruby)
467
+ */
468
+ @JRubyMethod(name = "normalize")
469
+ public IRubyObject normalize(ThreadContext context) {
470
+ Ruby runtime = context.runtime;
471
+ double mag = Math.sqrt(jx * jx + jy * jy + jz * jz);
472
+ if (mag < EPSILON) {
473
+ return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
474
+ runtime.newFloat(jx),
475
+ runtime.newFloat(jy),
476
+ runtime.newFloat(jz)});
477
+ }
478
+ return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
479
+ runtime.newFloat(jx / mag),
480
+ runtime.newFloat(jy / mag),
481
+ runtime.newFloat(jz / mag)});
482
+ }
483
+
484
+ /**
485
+ * Example of a regular ruby class method
486
+ *
487
+ * @param context ThreadContext
488
+ * @param klazz IRubyObject
489
+ * @return new random Vec3D object (ruby)
490
+ */
491
+ @JRubyMethod(name = "random", meta = true)
492
+ public static IRubyObject random_direction(ThreadContext context, IRubyObject klazz) {
493
+ Ruby runtime = context.runtime;
494
+ double angle = Math.random() * Math.PI * 2;
495
+ double vz = Math.random() * 2 - 1;
496
+ double vx = Math.sqrt(1 - vz * vz) * Math.cos(angle);
497
+ double vy = Math.sqrt(1 - vz * vz) * Math.sin(angle);
498
+
499
+ return Vec3.rbNew(context, klazz, new IRubyObject[]{
500
+ runtime.newFloat(vx),
501
+ runtime.newFloat(vy),
502
+ runtime.newFloat(vz)});
503
+ }
504
+
505
+ /**
506
+ *
507
+ * @param context ThreadContext
508
+ * @param other IRubyObject another Vec3D
509
+ * @return angle IRubyObject in radians
510
+ */
511
+ @JRubyMethod(name = "angle_between")
512
+ public IRubyObject angleBetween(ThreadContext context, IRubyObject other) {
513
+ Ruby runtime = context.runtime;
514
+ Vec3 vec = (Vec3) other.toJava(Vec3.class);
515
+ // We get NaN if we pass in a zero vector which can cause problems
516
+ // Zero seems like a reasonable angle between a (0,0,0) vector and something else
517
+ if (jx == 0 && jy == 0 && jz == 0) {
518
+ return runtime.newFloat(0.0);
519
+ }
520
+ if (vec.jx == 0 && vec.jy == 0 && vec.jz == 0) {
521
+ return runtime.newFloat(0.0);
522
+ }
523
+
524
+ double dot = jx * vec.jx + jy * vec.jy + jz * vec.jz;
525
+ double v1mag = Math.sqrt(jx * jx + jy * jy + jz * jz);
526
+ double v2mag = Math.sqrt(vec.jx * vec.jx + vec.jy * vec.jy + vec.jz * vec.jz);
527
+ // This should be a number between -1 and 1, since it's "normalized"
528
+ double amt = dot / (v1mag * v2mag);
529
+ if (amt <= -1) {
530
+ return runtime.newFloat(Math.PI);
531
+ } else if (amt >= 1) {
532
+ return runtime.newFloat(0.0);
533
+ }
534
+ return runtime.newFloat(Math.acos(amt));
535
+ }
536
+
537
+ /**
538
+ *
539
+ * @param context ThreadContext
540
+ * @return IRubyObject copy
541
+ */
542
+ @JRubyMethod(name = {"copy", "dup"})
543
+ public IRubyObject copy(ThreadContext context) {
544
+ Ruby runtime = context.runtime;
545
+ return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
546
+ runtime.newFloat(jx),
547
+ runtime.newFloat(jy),
548
+ runtime.newFloat(jz)});
549
+ }
550
+
551
+ /**
552
+ *
553
+ * @param context ThreadContext
554
+ * @return IRubyObject array of float
555
+ */
556
+ @JRubyMethod(name = "to_a")
557
+ public IRubyObject toArray(ThreadContext context) {
558
+ Ruby runtime = context.runtime;
559
+ return RubyArray.newArray(context.runtime, new IRubyObject[]{
560
+ runtime.newFloat(jx),
561
+ runtime.newFloat(jy),
562
+ runtime.newFloat(jz)});
563
+ }
564
+
565
+ /**
566
+ * To vertex
567
+ *
568
+ * @param context ThreadContext
569
+ * @param object IRubyObject vertex renderer
570
+ */
571
+ @JRubyMethod(name = "to_vertex")
572
+ public void toVertex(ThreadContext context, IRubyObject object) {
573
+ JRender renderer = (JRender) object.toJava(JRender.class);
574
+ renderer.vertex(jx, jy, jz);
575
+ }
576
+
577
+ /**
578
+ * To curve vertex
579
+ *
580
+ * @param context ThreadContext
581
+ * @param object IRubyObject vertex renderer
582
+ */
583
+ @JRubyMethod(name = "to_curve_vertex")
584
+ public void toCurveVertex(ThreadContext context, IRubyObject object) {
585
+ JRender renderer = (JRender) object.toJava(JRender.class);
586
+ renderer.curveVertex(jx, jy, jz);
587
+ }
588
+
589
+ /**
590
+ * Sends this Vec3D as a processing vertex uv
591
+ *
592
+ * @param context ThreadContext
593
+ * @param args IRubyObject[]
594
+ */
595
+ @JRubyMethod(name = "to_vertex_uv", rest = true)
596
+ public void toVertexUV(ThreadContext context, IRubyObject... args) {
597
+ int count = args.length;
598
+ double u = 0;
599
+ double v = 0;
600
+ if (count == 3) {
601
+ u = (args[1] instanceof RubyFloat)
602
+ ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
603
+ v = (args[2] instanceof RubyFloat)
604
+ ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
605
+ }
606
+ if (count == 2) {
607
+ Vec2 texture = (Vec2) args[1].toJava(Vec2.class);
608
+ u = texture.javax();
609
+ v = texture.javay();
610
+ }
611
+ JRender renderer = (JRender) args[0].toJava(JRender.class);
612
+ renderer.vertex(jx, jy, jz, u, v);
613
+ }
614
+
615
+ /**
616
+ * Sends this Vec3D as a processing normal
617
+ *
618
+ * @param context ThreadContext
619
+ * @param object IRubyObject vertex renderer
620
+ */
621
+ @JRubyMethod(name = "to_normal")
622
+ public void toNormal(ThreadContext context, IRubyObject object) {
623
+ JRender renderer = (JRender) object.toJava(JRender.class);
624
+ renderer.normal(jx, jy, jz);
625
+ }
626
+
627
+ /**
628
+ * For jruby-9000 we alias to inspect
629
+ *
630
+ * @param context ThreadContext
631
+ * @return IRubyObject to_s
632
+ */
633
+ @JRubyMethod(name = {"to_s", "inspect"})
634
+ public IRubyObject to_s(ThreadContext context) {
635
+ return context.runtime.newString(String.format("Vec3D(x = %4.4f, y = %4.4f, z = %4.4f)", jx, jy, jz));
636
+ }
637
+
638
+ /**
639
+ * Java hash
640
+ *
641
+ * @return hash int
642
+ */
643
+ @Override
644
+ public int hashCode() {
645
+ int hash = 7;
646
+ hash = 97 * hash + (int) (Double.doubleToLongBits(this.jx) ^ (Double.doubleToLongBits(this.jx) >>> 32));
647
+ hash = 97 * hash + (int) (Double.doubleToLongBits(this.jy) ^ (Double.doubleToLongBits(this.jy) >>> 32));
648
+ hash = 97 * hash + (int) (Double.doubleToLongBits(this.jz) ^ (Double.doubleToLongBits(this.jz) >>> 32));
649
+ return hash;
650
+ }
651
+
652
+ /**
653
+ * Java Equals
654
+ *
655
+ * @param obj Object
656
+ * @return result boolean
657
+ */
658
+ @Override
659
+ public boolean equals(Object obj) {
660
+ if (obj == this) {
661
+ return true;
662
+ }
663
+ if (obj instanceof Vec3) {
664
+ final Vec3 other = (Vec3) obj;
665
+ if ((Double.compare(jx, (Double) other.jx) == 0)
666
+ && (Double.compare(jy, (Double) other.jy) == 0)
667
+ && (Double.compare(jz, (Double) other.jz) == 0)) {
668
+ return true;
669
+ }
670
+
671
+ }
672
+ return false;
673
+ }
674
+
675
+ /**
676
+ *
677
+ * @param context ThreadContext
678
+ * @param other IRubyObject
679
+ * @return result IRubyObject as boolean
680
+ */
681
+ @JRubyMethod(name = "eql?", required = 1)
682
+ public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
683
+ Ruby runtime = context.runtime;
684
+ if (other == this) {
685
+ return runtime.newBoolean(true);
686
+ }
687
+ if (other instanceof Vec3) {
688
+ Vec3 v = (Vec3) other.toJava(Vec3.class);
689
+ if ((Double.compare(jx, (Double) v.jx) == 0)
690
+ && (Double.compare(jy, (Double) v.jy) == 0)
691
+ && (Double.compare(jz, (Double) v.jz) == 0)) {
692
+ return runtime.newBoolean(true);
693
+ }
694
+
695
+ }
696
+ return runtime.newBoolean(false);
697
+ }
698
+
699
+ /**
700
+ *
701
+ * @param context ThreadContext
702
+ * @param other IRubyObject
703
+ * @return result IRubyObject as boolean
704
+ */
705
+ @JRubyMethod(name = "==", required = 1)
706
+ @Override
707
+ public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
708
+ Ruby runtime = context.runtime;
709
+ if (other == this) {
710
+ return runtime.newBoolean(true);
711
+ }
712
+ if (other instanceof Vec3) {
713
+ Vec3 v = (Vec3) other.toJava(Vec3.class);
714
+ double diff = jx - v.jx;
715
+ if ((diff < 0 ? -diff : diff) > Vec3.EPSILON) {
716
+ return runtime.newBoolean(false);
717
+ }
718
+ diff = jy - v.jy;
719
+ if ((diff < 0 ? -diff : diff) > Vec3.EPSILON) {
720
+ return runtime.newBoolean(false);
721
+ }
722
+ diff = jz - v.jz;
723
+ return runtime.newBoolean((diff < 0 ? -diff : diff) < Vec3.EPSILON);
724
+ }
725
+ return runtime.newBoolean(false);
726
+ }
727
+ }