propane 0.6.0-java → 0.7.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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;
@@ -1,31 +1,31 @@
1
1
  package monkstone.vecmath.vec3;
2
- /*
2
+
3
+ /*
3
4
  * Copyright (c) 2015-16 Martin Prout
4
- *
5
+ *
5
6
  * This library is free software; you can redistribute it and/or
6
7
  * modify it under the terms of the GNU Lesser General Public
7
8
  * License as published by the Free Software Foundation; either
8
9
  * version 2.1 of the License, or (at your option) any later version.
9
- *
10
+ *
10
11
  * http://creativecommons.org/licenses/LGPL/2.1/
11
- *
12
+ *
12
13
  * This library is distributed in the hope that it will be useful,
13
14
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
15
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
16
  * Lesser General Public License for more details.
16
- *
17
+ *
17
18
  * You should have received a copy of the GNU Lesser General Public
18
19
  * License along with this library; if not, write to the Free Software
19
20
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- */
21
-
21
+ */
22
22
  import org.jruby.Ruby;
23
23
  import org.jruby.RubyArray;
24
- import org.jruby.RubyBoolean;
25
24
  import org.jruby.RubyClass;
25
+ import org.jruby.RubyFixnum;
26
+ import org.jruby.RubyFloat;
26
27
  import org.jruby.RubyObject;
27
28
  import org.jruby.RubySymbol;
28
- import org.jruby.runtime.ObjectAllocator;
29
29
  import org.jruby.anno.JRubyClass;
30
30
  import org.jruby.anno.JRubyMethod;
31
31
  import org.jruby.runtime.Arity;
@@ -36,668 +36,703 @@ import monkstone.vecmath.JRender;
36
36
  import monkstone.vecmath.vec2.Vec2;
37
37
 
38
38
  /**
39
- *
40
- * @author Martin Prout
41
- */
39
+ *
40
+ * @author Martin Prout
41
+ */
42
42
  @JRubyClass(name = "Vec3D")
43
43
  public final class Vec3 extends RubyObject {
44
-
45
- private static final long serialVersionUID = 4563517343762009867L;
46
-
47
- /**
48
- *
49
- * @param runtime Ruby
50
- */
51
- public static void createVec3(final Ruby runtime) {
52
- RubyClass vec3Cls = runtime.defineClass("Vec3D", runtime.getObject(), (Ruby runtime1, RubyClass rubyClass) -> new Vec3(runtime1, rubyClass));
53
- vec3Cls.defineAnnotatedMethods(Vec3.class);
54
- }
55
-
56
- static final double EPSILON = 9.999999747378752e-05; // matches processing.org EPSILON
57
- private double jx = 0;
58
- private double jy = 0;
59
- private double jz = 0;
60
-
61
- /**
62
- *
63
- * @param context ThreadContext
64
- * @param klazz IRubyObject
65
- * @param args optional (no args jx = 0, jy = 0, jz = 0) (2 args jz = 0)
66
- * @return new Vec3 object (ruby)
67
- */
68
- @JRubyMethod(name = "new", meta = true, rest = true)
69
- public final static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
70
- Vec3 vec = (Vec3) ((RubyClass) klazz).allocate();
71
- vec.init(context, args);
72
- return vec;
73
- }
74
-
75
- /**
76
- *
77
- * @param runtime Ruby
78
- * @param klass RubyClass
79
- */
80
- public Vec3(Ruby runtime, RubyClass klass) {
81
- super(runtime, klass);
82
- }
83
-
84
- void init(ThreadContext context, IRubyObject[] args) {
85
- int count = Arity.checkArgumentCount(context.getRuntime(), args, Arity.OPTIONAL.getValue(), 3);
86
- if (count >= 2) {
87
- jx = (Double) args[0].toJava(Double.class);
88
- jy = (Double) args[1].toJava(Double.class);
89
- }
90
- if (count == 3) {
91
- jz = (Double) args[2].toJava(Double.class);
92
- }
93
- }
94
-
95
- /**
96
- *
97
- * @param context ThreadContext
98
- * @return x IRubyObject
99
- */
100
- @JRubyMethod(name = "x")
101
-
102
- public IRubyObject getX(ThreadContext context) {
103
- return context.getRuntime().newFloat(jx);
104
- }
105
-
106
- /**
107
- *
108
- * @param context ThreadContext
109
- * @return y IRubyObject
110
- */
111
- @JRubyMethod(name = "y")
112
-
113
- public IRubyObject getY(ThreadContext context) {
114
- return context.getRuntime().newFloat(jy);
115
- }
116
-
117
- /**
118
- *
119
- * @param context ThreadContext
120
- * @return z IRubyObject
121
- */
122
- @JRubyMethod(name = "z")
123
- public IRubyObject getZ(ThreadContext context) {
124
- return context.getRuntime().newFloat(jz);
125
- }
126
-
127
- /**
128
- *
129
- * @param context ThreadContext
130
- * @param other IRubyObject
131
- * @return x IRubyObject
132
- */
133
- @JRubyMethod(name = "x=")
134
-
135
- public IRubyObject setX(ThreadContext context, IRubyObject other) {
136
- jx = (Double) other.toJava(Double.class);
137
- return context.getRuntime().newFloat(jx);
138
- }
139
-
140
- /**
141
- *
142
- * @param context ThreadContext
143
- * @param other IRubyObject
144
- * @return y IRubyObject
145
- */
146
- @JRubyMethod(name = "y=")
147
-
148
- public IRubyObject setY(ThreadContext context, IRubyObject other) {
149
- jy = (Double) other.toJava(Double.class);
150
- return context.getRuntime().newFloat(jy);
151
- }
152
-
153
- /**
154
- *
155
- * @param context ThreadContext
156
- * @param other IRubyObject
157
- * @return z IRubyObject
158
- */
159
- @JRubyMethod(name = "z=")
160
- public IRubyObject setZ(ThreadContext context, IRubyObject other) {
161
- jz = (Double) other.toJava(Double.class);
162
- return context.getRuntime().newFloat(jz);
163
- }
164
-
165
- /**
166
- *
167
- * @param context ThreadContext
168
- * @param key as symbol
169
- * @return value float
170
- */
171
- @JRubyMethod(name = "[]", required = 1)
172
-
173
- public IRubyObject aref(ThreadContext context, IRubyObject key) {
174
- Ruby runtime = context.getRuntime();
175
- if (key instanceof RubySymbol) {
176
- if (key == RubySymbol.newSymbol(runtime, "x")) {
177
- return runtime.newFloat(jx);
178
- } else if (key == RubySymbol.newSymbol(runtime, "y")) {
179
- return runtime.newFloat(jy);
180
- } else if (key == RubySymbol.newSymbol(runtime, "z")) {
181
- return runtime.newFloat(jz);
182
- } else {
183
- throw runtime.newIndexError("invalid key");
184
- }
185
- } else {
186
- throw runtime.newIndexError("invalid key");
187
- }
188
- }
189
-
190
- /**
191
- * @param context ThreadContext
192
- * @param key as symbol
193
- * @param value as float
194
- * @return value float
195
- */
196
- @JRubyMethod(name = "[]=")
197
-
198
- public IRubyObject aset(ThreadContext context, IRubyObject key, IRubyObject value) {
199
- Ruby runtime = context.getRuntime();
200
- if (key instanceof RubySymbol) {
201
- if (key == RubySymbol.newSymbol(runtime, "x")) {
202
- jx = (Double) value.toJava(Double.class);
203
- } else if (key == RubySymbol.newSymbol(runtime, "y")) {
204
- jy = (Double) value.toJava(Double.class);
205
- } else if (key == RubySymbol.newSymbol(runtime, "z")) {
206
- jz = (Double) value.toJava(Double.class);
207
- } else {
208
- throw runtime.newIndexError("invalid key");
209
- }
210
- } else {
211
- throw runtime.newIndexError("invalid key");
212
- }
213
- return value;
214
- }
215
-
216
- /**
217
- *
218
- * @param context ThreadContext
219
- * @param other IRubyObject
220
- * @return distance float
221
- */
222
- @JRubyMethod(name = "dist", required = 1)
223
-
224
- public IRubyObject dist(ThreadContext context, IRubyObject other) {
225
- Vec3 b = null;
226
- if (other instanceof Vec3) {
227
- b = (Vec3) other.toJava(Vec3.class);
228
- } else {
229
- throw context.runtime.newTypeError("argument should be Vec3D");
230
- }
231
- double result = Math.sqrt((jx - b.jx) * (jx - b.jx) + (jy - b.jy) * (jy - b.jy) + (jz - b.jz) * (jz - b.jz));
232
- return context.getRuntime().newFloat(result);
233
- }
234
-
235
- /**
236
- *
237
- * @param context ThreadContext
238
- * @param other IRubyObject
239
- * @return distance squared float
240
- */
241
- @JRubyMethod(name = "dist_squared", required = 1)
242
-
243
- public IRubyObject dist_squared(ThreadContext context, IRubyObject other) {
244
- Vec3 b = null;
245
- if (other instanceof Vec3) {
246
- b = (Vec3) other.toJava(Vec3.class);
247
- } else {
248
- throw context.runtime.newTypeError("argument should be Vec3D");
249
- }
250
- double result = (jx - b.jx) * (jx - b.jx) + (jy - b.jy) * (jy - b.jy) + (jz - b.jz) * (jz - b.jz);
251
- return context.getRuntime().newFloat(result);
252
- }
253
-
254
- /**
255
- *
256
- *
257
- * @param context ThreadContext
258
- * @param other IRubyObject
259
- * @return cross product IRubyObject
260
- */
261
-
262
- @JRubyMethod(name = "cross", required = 1)
263
-
264
- public IRubyObject cross(ThreadContext context, IRubyObject other) {
265
- Ruby runtime = context.getRuntime();
266
- Vec3 vec = null;
267
- if (other instanceof Vec3) {
268
- vec = (Vec3) other.toJava(Vec3.class);
269
- } else {
270
- throw runtime.newTypeError("argument should be Vec3D");
271
- }
272
- return Vec3.rbNew(context, other.getMetaClass(), new IRubyObject[]{
273
- runtime.newFloat(jy * vec.jz - jz * vec.jy),
274
- runtime.newFloat(jz * vec.jx - jx * vec.jz),
275
- runtime.newFloat(jx * vec.jy - jy * vec.jx)
276
- }
277
- );
278
- }
279
-
280
- /**
281
- *
282
- * @param context ThreadContext
283
- * @param other IRubyObject
284
- * @return dot product IRubyObject
285
- */
286
- @JRubyMethod(name = "dot", required = 1)
287
-
288
- public IRubyObject dot(ThreadContext context, IRubyObject other) {
289
- Ruby runtime = context.getRuntime();
290
- Vec3 b = null;
291
- if (other instanceof Vec3) {
292
- b = (Vec3) other.toJava(Vec3.class);
293
- } else {
294
- throw runtime.newTypeError("argument should be Vec3D");
295
- }
296
- return runtime.newFloat(jx * b.jx + jy * b.jy + jz * b.jz);
297
- }
298
-
299
- /**
300
- *
301
- * @param context ThreadContext
302
- * @param other IRubyObject
303
- * @return new Vec3 object (ruby)
304
- */
305
- @JRubyMethod(name = "+", required = 1)
306
-
307
- public IRubyObject op_add(ThreadContext context, IRubyObject other) {
308
- Ruby runtime = context.getRuntime();
309
- Vec3 b = (Vec3) other.toJava(Vec3.class);
310
- return Vec3.rbNew(context, other.getMetaClass(), new IRubyObject[]{
311
- runtime.newFloat(jx + b.jx),
312
- runtime.newFloat(jy + b.jy),
313
- runtime.newFloat(jz + b.jz)});
314
- }
315
-
316
- /**
317
- *
318
- * @param context ThreadContext
319
- * @param other IRubyObject
320
- * @return new Vec3 object (ruby)
321
- */
322
- @JRubyMethod(name = "-")
323
-
324
- public IRubyObject op_sub(ThreadContext context, IRubyObject other) {
325
- Ruby runtime = context.getRuntime();
326
- Vec3 b = null;
327
- if (other instanceof Vec3) {
328
- b = (Vec3) other.toJava(Vec3.class);
329
- } else {
330
- throw runtime.newTypeError("argument should be Vec3D");
331
- }
332
- return Vec3.rbNew(context, other.getMetaClass(), new IRubyObject[]{
333
- runtime.newFloat(jx - b.jx),
334
- runtime.newFloat(jy - b.jy),
335
- runtime.newFloat(jz - b.jz)});
336
- }
337
-
338
- /**
339
- *
340
- * @param context ThreadContext
341
- * @param other IRubyObject
342
- * @return new Vec3 object (ruby)
343
- */
344
- @JRubyMethod(name = "*", required = 1)
345
-
346
- public IRubyObject op_mul(ThreadContext context, IRubyObject other) {
347
- Ruby runtime = context.getRuntime();
348
- double scalar = (Double) other.toJava(Double.class);
349
- return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
350
- runtime.newFloat(jx * scalar),
351
- runtime.newFloat(jy * scalar),
352
- runtime.newFloat(jz * scalar)});
353
- }
354
-
355
- /**
356
- *
357
- * @param context ThreadContext
358
- * @param other IRubyObject
359
- * @return new Vec3 object (ruby)
360
- */
361
- @JRubyMethod(name = "/", required = 1)
362
-
363
- public IRubyObject op_div(ThreadContext context, IRubyObject other) {
364
- Ruby runtime = context.getRuntime();
365
- double scalar = (Double) other.toJava(Double.class);
366
- if (Math.abs(scalar) < Vec3.EPSILON) {
367
- return this;
368
- }
369
- return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
370
- runtime.newFloat(jx / scalar),
371
- runtime.newFloat(jy / scalar),
372
- runtime.newFloat(jz / scalar)});
373
- }
374
-
375
- /**
376
- *
377
- * @param context ThreadContext
378
- * @return magnitude squared IRubyObject
379
- */
380
- @JRubyMethod(name = "mag_squared")
381
-
382
- public IRubyObject mag_squared(ThreadContext context) {
383
- return context.getRuntime().newFloat(jx * jx + jy * jy + jz * jz);
384
- }
385
-
386
- /**
387
- *
388
- * @param context ThreadContext
389
- * @return magnitude IRubyObject
390
- */
391
- @JRubyMethod(name = "mag")
392
-
393
- public IRubyObject mag(ThreadContext context) {
394
- return context.getRuntime().newFloat(Math.sqrt(jx * jx + jy * jy + jz * jz));
395
- }
396
-
397
- /**
398
- * Call yield if block given, do nothing if yield == false else set_mag to
399
- * given scalar
400
- *
401
- * @param context ThreadContext
402
- * @param scalar double value to set
403
- * @param block should return a boolean (optional)
404
- * @return magnitude IRubyObject
405
- */
406
- @JRubyMethod(name = "set_mag")
407
-
408
- public IRubyObject set_mag(ThreadContext context, IRubyObject scalar, Block block) {
409
- if (block.isGiven()) {
410
- if (!(boolean) block.yield(context, scalar).toJava(Boolean.class)) {
44
+
45
+ private static final long serialVersionUID = 4563517343762009867L;
46
+
47
+ /**
48
+ *
49
+ * @param runtime Ruby
50
+ */
51
+ public static void createVec3(final Ruby runtime) {
52
+ RubyClass vec3Cls = runtime.defineClass("Vec3D", runtime.getObject(), (Ruby runtime1, RubyClass rubyClass) -> new Vec3(runtime1, rubyClass));
53
+ vec3Cls.defineAnnotatedMethods(Vec3.class);
54
+ }
55
+
56
+ static final double EPSILON = 9.999999747378752e-05; // matches processing.org EPSILON
57
+ double jx = 0;
58
+ double jy = 0;
59
+ double jz = 0;
60
+
61
+ /**
62
+ *
63
+ * @param context ThreadContext
64
+ * @param klazz IRubyObject
65
+ * @param args optional (no args jx = 0, jy = 0, jz = 0) (2 args jz = 0)
66
+ * @return new Vec3 object (ruby)
67
+ */
68
+ @JRubyMethod(name = "new", meta = true, rest = true)
69
+ public final static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
70
+ Vec3 vec = (Vec3) ((RubyClass) klazz).allocate();
71
+ vec.init(context, args);
72
+ return vec;
73
+ }
74
+
75
+ /**
76
+ *
77
+ * @param runtime Ruby
78
+ * @param klass RubyClass
79
+ */
80
+ public Vec3(Ruby runtime, RubyClass klass) {
81
+ super(runtime, klass);
82
+ }
83
+
84
+ void init(ThreadContext context, IRubyObject[] args) {
85
+ int count = Arity.checkArgumentCount(context.runtime, args, Arity.OPTIONAL.getValue(), 3);
86
+ if (count >= 2) {
87
+ jx = (args[0] instanceof RubyFloat)
88
+ ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
89
+ jy = (args[1] instanceof RubyFloat)
90
+ ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
91
+ }
92
+ if (count == 3) {
93
+ jz = (args[2] instanceof RubyFloat)
94
+ ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
95
+ }
96
+ }
97
+
98
+ /**
99
+ *
100
+ * @param context ThreadContext
101
+ * @return x IRubyObject
102
+ */
103
+ @JRubyMethod(name = "x")
104
+
105
+ public IRubyObject getX(ThreadContext context) {
106
+ return context.runtime.newFloat(jx);
107
+ }
108
+
109
+ /**
110
+ *
111
+ * @param context ThreadContext
112
+ * @return y IRubyObject
113
+ */
114
+ @JRubyMethod(name = "y")
115
+
116
+ public IRubyObject getY(ThreadContext context) {
117
+ return context.runtime.newFloat(jy);
118
+ }
119
+
120
+ /**
121
+ *
122
+ * @param context ThreadContext
123
+ * @return z IRubyObject
124
+ */
125
+ @JRubyMethod(name = "z")
126
+ public IRubyObject getZ(ThreadContext context) {
127
+ return context.runtime.newFloat(jz);
128
+ }
129
+
130
+ /**
131
+ *
132
+ * @param context ThreadContext
133
+ * @param other IRubyObject
134
+ * @return x IRubyObject
135
+ */
136
+ @JRubyMethod(name = "x=")
137
+
138
+ public IRubyObject setX(ThreadContext context, IRubyObject other) {
139
+ if (other instanceof RubyFloat) {
140
+ jx = ((RubyFloat) other).getValue();
141
+ } else {
142
+ jx = ((RubyFixnum) other).getDoubleValue();
143
+ }
144
+ return other;
145
+ }
146
+
147
+ /**
148
+ *
149
+ * @param context ThreadContext
150
+ * @param other IRubyObject
151
+ * @return y IRubyObject
152
+ */
153
+ @JRubyMethod(name = "y=")
154
+
155
+ public IRubyObject setY(ThreadContext context, IRubyObject other) {
156
+ if (other instanceof RubyFloat) {
157
+ jy = ((RubyFloat) other).getValue();
158
+ } else {
159
+ jy = ((RubyFixnum) other).getDoubleValue();
160
+ }
161
+ return other;
162
+ }
163
+
164
+ /**
165
+ *
166
+ * @param context ThreadContext
167
+ * @param other IRubyObject
168
+ * @return z IRubyObject
169
+ */
170
+ @JRubyMethod(name = "z=")
171
+ public IRubyObject setZ(ThreadContext context, IRubyObject other) {
172
+ if (other instanceof RubyFloat) {
173
+ jz = ((RubyFloat) other).getValue();
174
+ } else {
175
+ jz = ((RubyFixnum) other).getDoubleValue();
176
+ }
177
+ return other;
178
+ }
179
+
180
+ /**
181
+ *
182
+ * @param context ThreadContext
183
+ * @param key as symbol
184
+ * @return value float
185
+ */
186
+ @JRubyMethod(name = "[]", required = 1)
187
+
188
+ public IRubyObject aref(ThreadContext context, IRubyObject key) {
189
+ Ruby runtime = context.runtime;
190
+ if (key instanceof RubySymbol) {
191
+ if (key == RubySymbol.newSymbol(runtime, "x")) {
192
+ return runtime.newFloat(jx);
193
+ } else if (key == RubySymbol.newSymbol(runtime, "y")) {
194
+ return runtime.newFloat(jy);
195
+ } else if (key == RubySymbol.newSymbol(runtime, "z")) {
196
+ return runtime.newFloat(jz);
197
+ } else {
198
+ throw runtime.newIndexError("invalid key");
199
+ }
200
+ } else {
201
+ throw runtime.newIndexError("invalid key");
202
+ }
203
+ }
204
+
205
+ /**
206
+ * @param context ThreadContext
207
+ * @param key as symbol
208
+ * @param value as float
209
+ * @return value float
210
+ */
211
+ @JRubyMethod(name = "[]=")
212
+
213
+ public IRubyObject aset(ThreadContext context, IRubyObject key, IRubyObject value) {
214
+ Ruby runtime = context.runtime;
215
+ if (key instanceof RubySymbol) {
216
+ if (key == RubySymbol.newSymbol(runtime, "x")) {
217
+ jx = (value instanceof RubyFloat)
218
+ ? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue();
219
+ } else if (key == RubySymbol.newSymbol(runtime, "y")) {
220
+ jy = (value instanceof RubyFloat)
221
+ ? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue();
222
+ } else if (key == RubySymbol.newSymbol(runtime, "z")) {
223
+ jz = (value instanceof RubyFloat)
224
+ ? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue();
225
+ } else {
226
+ throw runtime.newIndexError("invalid key");
227
+ }
228
+ } else {
229
+ throw runtime.newIndexError("invalid key");
230
+ }
231
+ return value;
232
+ }
233
+
234
+ /**
235
+ *
236
+ * @param context ThreadContext
237
+ * @param other IRubyObject
238
+ * @return distance float
239
+ */
240
+ @JRubyMethod(name = "dist", required = 1)
241
+
242
+ public IRubyObject dist(ThreadContext context, IRubyObject other) {
243
+ Vec3 b = null;
244
+ if (other instanceof Vec3) {
245
+ b = (Vec3) other.toJava(Vec3.class);
246
+ } else {
247
+ throw context.runtime.newTypeError("argument should be Vec3D");
248
+ }
249
+ double result = Math.sqrt((jx - b.jx) * (jx - b.jx) + (jy - b.jy) * (jy - b.jy) + (jz - b.jz) * (jz - b.jz));
250
+ return context.runtime.newFloat(result);
251
+ }
252
+
253
+ /**
254
+ *
255
+ * @param context ThreadContext
256
+ * @param other IRubyObject
257
+ * @return distance squared float
258
+ */
259
+ @JRubyMethod(name = "dist_squared", required = 1)
260
+
261
+ public IRubyObject dist_squared(ThreadContext context, IRubyObject other) {
262
+ Vec3 b = null;
263
+ if (other instanceof Vec3) {
264
+ b = (Vec3) other.toJava(Vec3.class);
265
+ } else {
266
+ throw context.runtime.newTypeError("argument should be Vec3D");
267
+ }
268
+ double result = (jx - b.jx) * (jx - b.jx) + (jy - b.jy) * (jy - b.jy) + (jz - b.jz) * (jz - b.jz);
269
+ return context.runtime.newFloat(result);
270
+ }
271
+
272
+ /**
273
+ *
274
+ *
275
+ * @param context ThreadContext
276
+ * @param other IRubyObject
277
+ * @return cross product IRubyObject
278
+ */
279
+ @JRubyMethod(name = "cross", required = 1)
280
+
281
+ public IRubyObject cross(ThreadContext context, IRubyObject other) {
282
+ Ruby runtime = context.runtime;
283
+ Vec3 vec = null;
284
+ if (other instanceof Vec3) {
285
+ vec = (Vec3) other.toJava(Vec3.class);
286
+ } else {
287
+ throw runtime.newTypeError("argument should be Vec3D");
288
+ }
289
+ return Vec3.rbNew(context, other.getMetaClass(), new IRubyObject[]{
290
+ runtime.newFloat(jy * vec.jz - jz * vec.jy),
291
+ runtime.newFloat(jz * vec.jx - jx * vec.jz),
292
+ runtime.newFloat(jx * vec.jy - jy * vec.jx)
293
+ }
294
+ );
295
+ }
296
+
297
+ /**
298
+ *
299
+ * @param context ThreadContext
300
+ * @param other IRubyObject
301
+ * @return dot product IRubyObject
302
+ */
303
+ @JRubyMethod(name = "dot", required = 1)
304
+
305
+ public IRubyObject dot(ThreadContext context, IRubyObject other) {
306
+ Ruby runtime = context.runtime;
307
+ Vec3 b = null;
308
+ if (other instanceof Vec3) {
309
+ b = (Vec3) other.toJava(Vec3.class);
310
+ } else {
311
+ throw runtime.newTypeError("argument should be Vec3D");
312
+ }
313
+ return runtime.newFloat(jx * b.jx + jy * b.jy + jz * b.jz);
314
+ }
315
+
316
+ /**
317
+ *
318
+ * @param context ThreadContext
319
+ * @param other IRubyObject
320
+ * @return new Vec3 object (ruby)
321
+ */
322
+ @JRubyMethod(name = "+", required = 1)
323
+
324
+ public IRubyObject op_add(ThreadContext context, IRubyObject other) {
325
+ Ruby runtime = context.runtime;
326
+ Vec3 b = (Vec3) other.toJava(Vec3.class);
327
+ return Vec3.rbNew(context, other.getMetaClass(), new IRubyObject[]{
328
+ runtime.newFloat(jx + b.jx),
329
+ runtime.newFloat(jy + b.jy),
330
+ runtime.newFloat(jz + b.jz)});
331
+ }
332
+
333
+ /**
334
+ *
335
+ * @param context ThreadContext
336
+ * @param other IRubyObject
337
+ * @return new Vec3 object (ruby)
338
+ */
339
+ @JRubyMethod(name = "-")
340
+
341
+ public IRubyObject op_sub(ThreadContext context, IRubyObject other) {
342
+ Ruby runtime = context.runtime;
343
+ Vec3 b = null;
344
+ if (other instanceof Vec3) {
345
+ b = (Vec3) other.toJava(Vec3.class);
346
+ } else {
347
+ throw runtime.newTypeError("argument should be Vec3D");
348
+ }
349
+ return Vec3.rbNew(context, other.getMetaClass(), new IRubyObject[]{
350
+ runtime.newFloat(jx - b.jx),
351
+ runtime.newFloat(jy - b.jy),
352
+ runtime.newFloat(jz - b.jz)});
353
+ }
354
+
355
+ /**
356
+ *
357
+ * @param context ThreadContext
358
+ * @param scalar IRubyObject
359
+ * @return new Vec3 object (ruby)
360
+ */
361
+ @JRubyMethod(name = "*", required = 1)
362
+
363
+ public IRubyObject op_mul(ThreadContext context, IRubyObject scalar) {
364
+ Ruby runtime = context.runtime;
365
+ double multi = (scalar instanceof RubyFloat)
366
+ ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
367
+ return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
368
+ runtime.newFloat(jx * multi),
369
+ runtime.newFloat(jy * multi),
370
+ runtime.newFloat(jz * multi)});
371
+ }
372
+
373
+ /**
374
+ *
375
+ * @param context ThreadContext
376
+ * @param scalar IRubyObject
377
+ * @return new Vec3 object (ruby)
378
+ */
379
+ @JRubyMethod(name = "/", required = 1)
380
+
381
+ public IRubyObject op_div(ThreadContext context, IRubyObject scalar) {
382
+ Ruby runtime = context.runtime;
383
+ double divisor = (scalar instanceof RubyFloat)
384
+ ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
385
+ if (Math.abs(divisor) < Vec3.EPSILON) {
386
+ return this;
387
+ }
388
+ return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
389
+ runtime.newFloat(jx / divisor),
390
+ runtime.newFloat(jy / divisor),
391
+ runtime.newFloat(jz / divisor)});
392
+ }
393
+
394
+ /**
395
+ *
396
+ * @param context ThreadContext
397
+ * @return magnitude squared IRubyObject
398
+ */
399
+ @JRubyMethod(name = "mag_squared")
400
+
401
+ public IRubyObject mag_squared(ThreadContext context) {
402
+ return context.runtime.newFloat(jx * jx + jy * jy + jz * jz);
403
+ }
404
+
405
+ /**
406
+ *
407
+ * @param context ThreadContext
408
+ * @return magnitude IRubyObject
409
+ */
410
+ @JRubyMethod(name = "mag")
411
+
412
+ public IRubyObject mag(ThreadContext context) {
413
+ return context.runtime.newFloat(Math.sqrt(jx * jx + jy * jy + jz * jz));
414
+ }
415
+
416
+ /**
417
+ * Call yield if block given, do nothing if yield == false else set_mag to
418
+ * given scalar
419
+ *
420
+ * @param context ThreadContext
421
+ * @param scalar double value to set
422
+ * @param block should return a boolean (optional)
423
+ * @return magnitude IRubyObject
424
+ */
425
+ @JRubyMethod(name = "set_mag")
426
+
427
+ public IRubyObject set_mag(ThreadContext context, IRubyObject scalar, Block block) {
428
+ if (block.isGiven()) {
429
+ if (!(boolean) block.yield(context, scalar).toJava(Boolean.class)) {
430
+ return this;
431
+ }
432
+ }
433
+ double new_mag = (scalar instanceof RubyFloat)
434
+ ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
435
+ double current = Math.sqrt(jx * jx + jy * jy + jz * jz);
436
+ if (current > EPSILON) {
437
+ jx *= new_mag / current;
438
+ jy *= new_mag / current;
439
+ jz *= new_mag / current;
440
+ }
411
441
  return this;
412
- }
413
- }
414
- double new_mag = (Double) scalar.toJava(Double.class);
415
- double current = Math.sqrt(jx * jx + jy * jy + jz * jz);
416
- if (current > EPSILON) {
417
- jx *= new_mag / current;
418
- jy *= new_mag / current;
419
- jz *= new_mag / current;
420
- }
421
- return this;
422
- }
423
-
424
- /**
425
- *
426
- * @param context ThreadContext
427
- * @return this as a ruby object
428
- */
429
- @JRubyMethod(name = "normalize!")
430
-
431
- public IRubyObject normalize_bang(ThreadContext context) {
432
- if (Math.abs(jx) < EPSILON && Math.abs(jy) < EPSILON && Math.abs(jz) < EPSILON) {
433
- return this;
434
- }
435
- double mag = Math.sqrt(jx * jx + jy * jy + jz * jz);
436
- jx /= mag;
437
- jy /= mag;
438
- jz /= mag;
439
- return this;
440
- }
441
-
442
- /**
443
- *
444
- * @param context ThreadContext
445
- * @return new normalized Vec3D object (ruby)
446
- */
447
- @JRubyMethod(name = "normalize")
448
-
449
- public IRubyObject normalize(ThreadContext context) {
450
- Ruby runtime = context.getRuntime();
451
- double mag = Math.sqrt(jx * jx + jy * jy + jz * jz);
452
- if (mag < EPSILON) {
453
- return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
454
- runtime.newFloat(jx),
455
- runtime.newFloat(jy),
456
- runtime.newFloat(jz)});
457
- }
458
- return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
459
- runtime.newFloat(jx / mag),
460
- runtime.newFloat(jy / mag),
461
- runtime.newFloat(jz / mag)});
462
- }
463
-
464
- /**
465
- * Example of a regular ruby class method
466
- *
467
- * @param context ThreadContext
468
- * @param klazz IRubyObject
469
- * @return new random Vec3D object (ruby)
470
- */
471
- @JRubyMethod(name = "random", meta = true)
472
-
473
- public static IRubyObject random_direction(ThreadContext context, IRubyObject klazz) {
474
- Ruby runtime = context.getRuntime();
475
- double angle = Math.random() * Math.PI * 2;
476
- double vz = Math.random() * 2 - 1;
477
- double vx = Math.sqrt(1 - vz * vz) * Math.cos(angle);
478
- double vy = Math.sqrt(1 - vz * vz) * Math.sin(angle);
479
-
480
- return Vec3.rbNew(context, klazz, new IRubyObject[]{
481
- runtime.newFloat(vx),
482
- runtime.newFloat(vy),
483
- runtime.newFloat(vz)});
484
- }
485
-
486
- /**
487
- *
488
- * @param context ThreadContext
489
- * @param other IRubyObject another Vec3D
490
- * @return angle IRubyObject in radians
491
- */
492
- @JRubyMethod(name = "angle_between")
493
-
494
- public IRubyObject angleBetween(ThreadContext context, IRubyObject other) {
495
- Ruby runtime = context.getRuntime();
496
- Vec3 vec = (Vec3) other.toJava(Vec3.class);
497
- // We get NaN if we pass in a zero vector which can cause problems
498
- // Zero seems like a reasonable angle between a (0,0,0) vector and something else
499
- if (jx == 0 && jy == 0 && jz == 0) {
500
- return runtime.newFloat(0.0);
501
- }
502
- if (vec.jx == 0 && vec.jy == 0 && vec.jz == 0) {
503
- return runtime.newFloat(0.0);
504
- }
505
-
506
- double dot = jx * vec.jx + jy * vec.jy + jz * vec.jz;
507
- double v1mag = Math.sqrt(jx * jx + jy * jy + jz * jz);
508
- double v2mag = Math.sqrt(vec.jx * vec.jx + vec.jy * vec.jy + vec.jz * vec.jz);
509
- // This should be a number between -1 and 1, since it's "normalized"
510
- double amt = dot / (v1mag * v2mag);
511
- if (amt <= -1) {
512
- return runtime.newFloat(Math.PI);
513
- } else if (amt >= 1) {
514
- return runtime.newFloat(0.0);
515
- }
516
- return runtime.newFloat(Math.acos(amt));
517
- }
518
-
519
- /**
520
- *
521
- * @param context ThreadContext
522
- * @return IRubyObject copy
523
- */
524
- @JRubyMethod(name = {"copy", "dup"})
525
-
526
- public IRubyObject copy(ThreadContext context) {
527
- Ruby runtime = context.getRuntime();
528
- return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
529
- runtime.newFloat(jx),
530
- runtime.newFloat(jy),
531
- runtime.newFloat(jz)});
532
- }
533
-
534
- /**
535
- *
536
- * @param context ThreadContext
537
- * @return IRubyObject array of float
538
- */
539
- @JRubyMethod(name = "to_a")
540
-
541
- public IRubyObject toArray(ThreadContext context) {
542
- Ruby runtime = context.getRuntime();
543
- return RubyArray.newArray(context.runtime, new IRubyObject[]{
544
- runtime.newFloat(jx),
545
- runtime.newFloat(jy),
546
- runtime.newFloat(jz)});
547
- }
548
-
549
- /**
550
- * To vertex
551
- * @param context ThreadContext
552
- * @param object IRubyObject vertex renderer
553
- */
554
- @JRubyMethod(name = "to_vertex")
555
-
556
- public void toVertex(ThreadContext context, IRubyObject object) {
557
- JRender renderer = (JRender) object.toJava(JRender.class);
558
- renderer.vertex(jx, jy, jz);
559
- }
560
-
561
- /**
562
- * To curve vertex
563
- * @param context ThreadContext
564
- * @param object IRubyObject vertex renderer
565
- */
566
- @JRubyMethod(name = "to_curve_vertex")
567
-
568
- public void toCurveVertex(ThreadContext context, IRubyObject object) {
569
- JRender renderer = (JRender) object.toJava(JRender.class);
570
- renderer.curveVertex(jx, jy, jz);
571
- }
572
-
573
- /**
574
- * Sends this Vec3D as a processing vertex uv
575
- * @param context ThreadContext
576
- * @param args IRubyObject[]
577
- */
578
- @JRubyMethod(name = "to_vertex_uv", rest = true)
579
-
580
- public void toVertexUV(ThreadContext context, IRubyObject[] args) {
581
- int count = Arity.checkArgumentCount(context.getRuntime(), args, Arity.OPTIONAL.getValue(), 3);
582
- double u = 0;
583
- double v = 0;
584
- if (count == 3)
585
- {
586
- u = (Double) args[1].toJava(Double.class);
587
- v = (Double) args[2].toJava(Double.class);
588
- }
589
- if (count == 2){
590
- Vec2 texture = (Vec2) args[1].toJava(Vec2.class);
591
- u = texture.javax();
592
- v = texture.javay();
593
- }
594
- JRender renderer = (JRender) args[0].toJava(JRender.class);
595
- renderer.vertex(jx, jy, jz, u, v);
596
- }
597
-
598
- /**
599
- * Sends this Vec3D as a processing normal
600
- * @param context ThreadContext
601
- * @param object IRubyObject vertex renderer
602
- */
603
- @JRubyMethod(name = "to_normal")
604
-
605
- public void toNormal(ThreadContext context, IRubyObject object) {
606
- JRender renderer = (JRender) object.toJava(JRender.class);
607
- renderer.normal(jx, jy, jz);
608
- }
609
-
610
- /**
611
- * For jruby-9000 we alias to inspect
612
- *
613
- * @param context ThreadContext
614
- * @return IRubyObject to_s
615
- */
616
- @JRubyMethod(name = {"to_s", "inspect"})
617
-
618
- public IRubyObject to_s(ThreadContext context) {
619
- return context.getRuntime().newString(String.format("Vec3D(x = %4.4f, y = %4.4f, z = %4.4f)", jx, jy, jz));
620
- }
621
-
622
- /**
623
- * Java hash
624
- * @return hash int
625
- */
626
- @Override
627
- public int hashCode() {
628
- int hash = 7;
629
- hash = 97 * hash + (int) (Double.doubleToLongBits(this.jx) ^ (Double.doubleToLongBits(this.jx) >>> 32));
630
- hash = 97 * hash + (int) (Double.doubleToLongBits(this.jy) ^ (Double.doubleToLongBits(this.jy) >>> 32));
631
- hash = 97 * hash + (int) (Double.doubleToLongBits(this.jz) ^ (Double.doubleToLongBits(this.jz) >>> 32));
632
- return hash;
633
- }
634
-
635
- /**
636
- * Java Equals
637
- * @param obj Object
638
- * @return result boolean
639
- */
640
- @Override
641
- public boolean equals(Object obj) {
642
- if (obj instanceof Vec3) {
643
- final Vec3 other = (Vec3) obj;
644
- if (!((Double) this.jx).equals(other.jx)) {
645
- return false;
646
- }
647
- if (!((Double) this.jy).equals(other.jy)) {
442
+ }
443
+
444
+ /**
445
+ *
446
+ * @param context ThreadContext
447
+ * @return this as a ruby object
448
+ */
449
+ @JRubyMethod(name = "normalize!")
450
+
451
+ public IRubyObject normalize_bang(ThreadContext context) {
452
+ if (Math.abs(jx) < EPSILON && Math.abs(jy) < EPSILON && Math.abs(jz) < EPSILON) {
453
+ return this;
454
+ }
455
+ double mag = Math.sqrt(jx * jx + jy * jy + jz * jz);
456
+ jx /= mag;
457
+ jy /= mag;
458
+ jz /= mag;
459
+ return this;
460
+ }
461
+
462
+ /**
463
+ *
464
+ * @param context ThreadContext
465
+ * @return new normalized Vec3D object (ruby)
466
+ */
467
+ @JRubyMethod(name = "normalize")
468
+
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
+
493
+ public static IRubyObject random_direction(ThreadContext context, IRubyObject klazz) {
494
+ Ruby runtime = context.runtime;
495
+ double angle = Math.random() * Math.PI * 2;
496
+ double vz = Math.random() * 2 - 1;
497
+ double vx = Math.sqrt(1 - vz * vz) * Math.cos(angle);
498
+ double vy = Math.sqrt(1 - vz * vz) * Math.sin(angle);
499
+
500
+ return Vec3.rbNew(context, klazz, new IRubyObject[]{
501
+ runtime.newFloat(vx),
502
+ runtime.newFloat(vy),
503
+ runtime.newFloat(vz)});
504
+ }
505
+
506
+ /**
507
+ *
508
+ * @param context ThreadContext
509
+ * @param other IRubyObject another Vec3D
510
+ * @return angle IRubyObject in radians
511
+ */
512
+ @JRubyMethod(name = "angle_between")
513
+
514
+ public IRubyObject angleBetween(ThreadContext context, IRubyObject other) {
515
+ Ruby runtime = context.runtime;
516
+ Vec3 vec = (Vec3) other.toJava(Vec3.class);
517
+ // We get NaN if we pass in a zero vector which can cause problems
518
+ // Zero seems like a reasonable angle between a (0,0,0) vector and something else
519
+ if (jx == 0 && jy == 0 && jz == 0) {
520
+ return runtime.newFloat(0.0);
521
+ }
522
+ if (vec.jx == 0 && vec.jy == 0 && vec.jz == 0) {
523
+ return runtime.newFloat(0.0);
524
+ }
525
+
526
+ double dot = jx * vec.jx + jy * vec.jy + jz * vec.jz;
527
+ double v1mag = Math.sqrt(jx * jx + jy * jy + jz * jz);
528
+ double v2mag = Math.sqrt(vec.jx * vec.jx + vec.jy * vec.jy + vec.jz * vec.jz);
529
+ // This should be a number between -1 and 1, since it's "normalized"
530
+ double amt = dot / (v1mag * v2mag);
531
+ if (amt <= -1) {
532
+ return runtime.newFloat(Math.PI);
533
+ } else if (amt >= 1) {
534
+ return runtime.newFloat(0.0);
535
+ }
536
+ return runtime.newFloat(Math.acos(amt));
537
+ }
538
+
539
+ /**
540
+ *
541
+ * @param context ThreadContext
542
+ * @return IRubyObject copy
543
+ */
544
+ @JRubyMethod(name = {"copy", "dup"})
545
+
546
+ public IRubyObject copy(ThreadContext context) {
547
+ Ruby runtime = context.runtime;
548
+ return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{
549
+ runtime.newFloat(jx),
550
+ runtime.newFloat(jy),
551
+ runtime.newFloat(jz)});
552
+ }
553
+
554
+ /**
555
+ *
556
+ * @param context ThreadContext
557
+ * @return IRubyObject array of float
558
+ */
559
+ @JRubyMethod(name = "to_a")
560
+
561
+ public IRubyObject toArray(ThreadContext context) {
562
+ Ruby runtime = context.runtime;
563
+ return RubyArray.newArray(context.runtime, new IRubyObject[]{
564
+ runtime.newFloat(jx),
565
+ runtime.newFloat(jy),
566
+ runtime.newFloat(jz)});
567
+ }
568
+
569
+ /**
570
+ * To vertex
571
+ *
572
+ * @param context ThreadContext
573
+ * @param object IRubyObject vertex renderer
574
+ */
575
+ @JRubyMethod(name = "to_vertex")
576
+
577
+ public void toVertex(ThreadContext context, IRubyObject object) {
578
+ JRender renderer = (JRender) object.toJava(JRender.class);
579
+ renderer.vertex(jx, jy, jz);
580
+ }
581
+
582
+ /**
583
+ * To curve vertex
584
+ *
585
+ * @param context ThreadContext
586
+ * @param object IRubyObject vertex renderer
587
+ */
588
+ @JRubyMethod(name = "to_curve_vertex")
589
+
590
+ public void toCurveVertex(ThreadContext context, IRubyObject object) {
591
+ JRender renderer = (JRender) object.toJava(JRender.class);
592
+ renderer.curveVertex(jx, jy, jz);
593
+ }
594
+
595
+ /**
596
+ * Sends this Vec3D as a processing vertex uv
597
+ *
598
+ * @param context ThreadContext
599
+ * @param args IRubyObject[]
600
+ */
601
+ @JRubyMethod(name = "to_vertex_uv", rest = true)
602
+
603
+ public void toVertexUV(ThreadContext context, IRubyObject[] args) {
604
+ int count = Arity.checkArgumentCount(context.runtime, args, Arity.OPTIONAL.getValue(), 3);
605
+ double u = 0;
606
+ double v = 0;
607
+ if (count == 3) {
608
+ u = (args[1] instanceof RubyFloat)
609
+ ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
610
+ v = (args[2] instanceof RubyFloat)
611
+ ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
612
+ }
613
+ if (count == 2) {
614
+ Vec2 texture = (Vec2) args[1].toJava(Vec2.class);
615
+ u = texture.javax();
616
+ v = texture.javay();
617
+ }
618
+ JRender renderer = (JRender) args[0].toJava(JRender.class);
619
+ renderer.vertex(jx, jy, jz, u, v);
620
+ }
621
+
622
+ /**
623
+ * Sends this Vec3D as a processing normal
624
+ *
625
+ * @param context ThreadContext
626
+ * @param object IRubyObject vertex renderer
627
+ */
628
+ @JRubyMethod(name = "to_normal")
629
+
630
+ public void toNormal(ThreadContext context, IRubyObject object) {
631
+ JRender renderer = (JRender) object.toJava(JRender.class);
632
+ renderer.normal(jx, jy, jz);
633
+ }
634
+
635
+ /**
636
+ * For jruby-9000 we alias to inspect
637
+ *
638
+ * @param context ThreadContext
639
+ * @return IRubyObject to_s
640
+ */
641
+ @JRubyMethod(name = {"to_s", "inspect"})
642
+
643
+ public IRubyObject to_s(ThreadContext context) {
644
+ return context.runtime.newString(String.format("Vec3D(x = %4.4f, y = %4.4f, z = %4.4f)", jx, jy, jz));
645
+ }
646
+
647
+ /**
648
+ * Java hash
649
+ *
650
+ * @return hash int
651
+ */
652
+ @Override
653
+ public int hashCode() {
654
+ int hash = 7;
655
+ hash = 97 * hash + (int) (Double.doubleToLongBits(this.jx) ^ (Double.doubleToLongBits(this.jx) >>> 32));
656
+ hash = 97 * hash + (int) (Double.doubleToLongBits(this.jy) ^ (Double.doubleToLongBits(this.jy) >>> 32));
657
+ hash = 97 * hash + (int) (Double.doubleToLongBits(this.jz) ^ (Double.doubleToLongBits(this.jz) >>> 32));
658
+ return hash;
659
+ }
660
+
661
+ /**
662
+ * Java Equals
663
+ *
664
+ * @param obj Object
665
+ * @return result boolean
666
+ */
667
+ @Override
668
+ public boolean equals(Object obj) {
669
+ if (obj == this) {
670
+ return true;
671
+ }
672
+ if (obj instanceof Vec3) {
673
+ final Vec3 other = (Vec3) obj;
674
+ if ((Double.compare(jx, (Double) other.jx) == 0)
675
+ && (Double.compare(jy, (Double) other.jy) == 0)
676
+ && (Double.compare(jz, (Double) other.jz) == 0)) {
677
+ return true;
678
+ }
679
+
680
+ }
648
681
  return false;
649
- }
650
- return ((Double) this.jz).equals(other.jz);
651
- }
652
- return false;
653
- }
654
-
655
- /**
656
- *
657
- * @param context ThreadContext
658
- * @param other IRubyObject
659
- * @return result IRubyObject as boolean
660
- */
661
- @JRubyMethod(name = "eql?", required = 1)
662
-
663
- public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
664
- if (other instanceof Vec3) {
665
- Vec3 v = (Vec3) other.toJava(Vec3.class);
666
- if (!((Double) this.jx).equals(v.jx)) {
667
- return RubyBoolean.newBoolean(context.runtime, false);
668
- }
669
- if (!((Double) this.jy).equals(v.jy)) {
670
- return RubyBoolean.newBoolean(context.runtime, false);
671
- }
672
- return RubyBoolean.newBoolean(context.runtime, ((Double) this.jz).equals(v.jz));
673
- }
674
- return RubyBoolean.newBoolean(context.runtime, false);
675
- }
676
-
677
- /**
678
- *
679
- * @param context ThreadContext
680
- * @param other IRubyObject
681
- * @return result IRubyObject as boolean
682
- */
683
- @JRubyMethod(name = "==", required = 1)
684
-
685
- @Override
686
- public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
687
- if (other instanceof Vec3) {
688
- Vec3 v = (Vec3) other.toJava(Vec3.class);
689
- double diff = jx - v.jx;
690
- if ((diff < 0 ? -diff : diff) > Vec3.EPSILON) {
691
- return RubyBoolean.newBoolean(context.runtime, false);
692
- }
693
- diff = jy - v.jy;
694
- if ((diff < 0 ? -diff : diff) > Vec3.EPSILON) {
695
- return RubyBoolean.newBoolean(context.runtime, false);
696
- }
697
- diff = jz - v.jz;
698
- boolean result = ((diff < 0 ? -diff : diff) < Vec3.EPSILON);
699
- return RubyBoolean.newBoolean(context.runtime, result);
700
- }
701
- return RubyBoolean.newBoolean(context.runtime, false);
702
- }
682
+ }
683
+
684
+ /**
685
+ *
686
+ * @param context ThreadContext
687
+ * @param other IRubyObject
688
+ * @return result IRubyObject as boolean
689
+ */
690
+ @JRubyMethod(name = "eql?", required = 1)
691
+
692
+ public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
693
+ Ruby runtime = context.runtime;
694
+ if (other == this) {
695
+ return runtime.newBoolean(true);
696
+ }
697
+ if (other instanceof Vec3) {
698
+ Vec3 v = (Vec3) other.toJava(Vec3.class);
699
+ if ((Double.compare(jx, (Double) v.jx) == 0)
700
+ && (Double.compare(jy, (Double) v.jy) == 0)
701
+ && (Double.compare(jz, (Double) v.jz) == 0)) {
702
+ return runtime.newBoolean(true);
703
+ }
704
+
705
+ }
706
+ return runtime.newBoolean(false);
707
+ }
708
+
709
+ /**
710
+ *
711
+ * @param context ThreadContext
712
+ * @param other IRubyObject
713
+ * @return result IRubyObject as boolean
714
+ */
715
+ @JRubyMethod(name = "==", required = 1)
716
+
717
+ @Override
718
+ public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
719
+ Ruby runtime = context.runtime;
720
+ if (other == this) {
721
+ return runtime.newBoolean(true);
722
+ }
723
+ if (other instanceof Vec3) {
724
+ Vec3 v = (Vec3) other.toJava(Vec3.class);
725
+ double diff = jx - v.jx;
726
+ if ((diff < 0 ? -diff : diff) > Vec3.EPSILON) {
727
+ return runtime.newBoolean(false);
728
+ }
729
+ diff = jy - v.jy;
730
+ if ((diff < 0 ? -diff : diff) > Vec3.EPSILON) {
731
+ return runtime.newBoolean(false);
732
+ }
733
+ diff = jz - v.jz;
734
+ return runtime.newBoolean((diff < 0 ? -diff : diff) < Vec3.EPSILON);
735
+ }
736
+ return runtime.newBoolean(false);
737
+ }
703
738
  }