forked from NeuronRobotics/JCSG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3d.java
More file actions
574 lines (517 loc) · 14.1 KB
/
Vector3d.java
File metadata and controls
574 lines (517 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/**
* Vector3d.java
*
* Copyright 2014-2014 Michael Hoffer info@michaelhoffer.de. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Michael Hoffer info@michaelhoffer.de "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Michael Hoffer info@michaelhoffer.de OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of Michael Hoffer
* info@michaelhoffer.de.
*/
package eu.mihosoft.vrl.v3d;
import static java.lang.Math.abs;
import static java.lang.Math.acos;
import static java.lang.Math.max;
import static java.lang.Math.min;
import java.util.Locale;
// Auto-generated Javadoc
/**
* 3D Vector3d.
*
* @author Michael Hoffer <info@michaelhoffer.de>
*/
public class Vector3d extends javax.vecmath.Vector3d {
private static String exportString = "%.10f";
private static double EXPORTEPSILON = 1.0e-10;
/**
*
*/
private static final long serialVersionUID = 1878117798187075166L;
/** The Constant ZERO. */
public static final Vector3d ZERO = new Vector3d(0, 0, 0);
/** The Constant UNITY. */
public static final Vector3d UNITY = new Vector3d(1, 1, 1);
/** The Constant X_ONE. */
public static final Vector3d X_ONE = new Vector3d(1, 0, 0);
/** The Constant Y_ONE. */
public static final Vector3d Y_ONE = new Vector3d(0, 1, 0);
/** The Constant Z_ONE. */
public static final Vector3d Z_ONE = new Vector3d(0, 0, 1);
/**
* Creates a new vector.
*
* @param x
* x value
* @param y
* y value
* @param z
* z value
*/
public Vector3d(double x, double y, double z) {
// if(!Double.isFinite(x)||!Double.isFinite(y)||!Double.isFinite(z)) {
// throw new NumberFormatException("Vectors must be real "+x+" "+y+" "+z);
// }
this.x = x;
this.y = y;
this.z = z;
}
public Vector3d(Number x, Number y, Number z) {
this(x.doubleValue(), y.doubleValue(), z.doubleValue());
}
/**
* Creates a new vector with specified {@code x}, {@code y} and {@code z = 0}.
*
* @param x
* x value
* @param y
* y value
*/
public Vector3d(double x, double y) {
this(x, y, (double) 0);
}
public Vector3d(Number x, Number y) {
this(x, y, (double) 0);
}
/**
* Creates a new vector with specified {@code x}, {@code y} and {@code z = 0}.
*
* @param x
* x value
* @param y
* y value
*/
public static Vector3d xy(double x, double y) {
return new Vector3d(x, y);
}
public static Vector3d xy(Number x, Number y) {
return xy(x.doubleValue(), y.doubleValue());
}
/**
* Creates a new vector with specified {@code x}, {@code y} and {@code z}.
*
* @param x
* x value
* @param y
* y value
* @param z
* z value
*/
public static Vector3d xyz(double x, double y, double z) {
return new Vector3d(x, y, z);
}
public static Vector3d xyz(Number x, Number y, Number z) {
return new Vector3d(x, y, z);
}
@Override
public Vector3d clone() {
return new Vector3d(x, y, z);
}
/**
* Returns a negated copy of this vector.
*
* Note: this vector is not modified.
*
* @return a negated copy of this vector
*/
public Vector3d negated() {
return new Vector3d(-x, -y, -z);
}
/**
* Returns the sum of this vector and the specified vector.
*
* @param v
* the vector to add
*
* Note: this vector is not modified.
*
* @return the sum of this vector and the specified vector
*/
public Vector3d plus(Vector3d v) {
return new Vector3d(x + v.x, y + v.y, z + v.z);
}
/**
* Returns the difference of this vector and the specified vector.
*
* @param v
* the vector to subtract
*
* Note: this vector is not modified.
*
* @return the difference of this vector and the specified vector
*/
public Vector3d minus(Vector3d v) {
return new Vector3d(x - v.x, y - v.y, z - v.z);
}
/**
* Returns the product of this vector and the specified value.
*
* @param a
* the value
*
* Note: this vector is not modified.
*
* @return the product of this vector and the specified value
*/
public Vector3d times(double a) {
return new Vector3d(x * a, y * a, z * a);
}
/**
* Returns the product of this vector and the specified vector.
*
* @param a
* the vector
*
* Note: this vector is not modified.
*
* @return the product of this vector and the specified vector
*/
public Vector3d times(Vector3d a) {
return new Vector3d(x * a.x, y * a.y, z * a.z);
}
/**
* Returns this vector devided by the specified value.
*
* @param a
* the value
*
* Note: this vector is not modified.
*
* @return this vector devided by the specified value
*/
public Vector3d dividedBy(double a) {
return new Vector3d(x / a, y / a, z / a);
}
/**
* Returns the dot product of this vector and the specified vector.
*
* Note: this vector is not modified.
*
* @param a
* the second vector
*
* @return the dot product of this vector and the specified vector
*/
public double dot(Vector3d a) {
return this.x * a.x + this.y * a.y + this.z * a.z;
}
/**
* Linearly interpolates between this and the specified vector.
*
* Note: this vector is not modified.
*
* @param a
* vector
* @param t
* interpolation value
*
* @return copy of this vector if {@code t = 0}; copy of a if {@code t = 1}; the
* point midway between this and the specified vector if {@code t = 0.5}
*/
public Vector3d lerp(Vector3d a, double t) {
return this.plus(a.minus(this).times(t));
}
/**
* Returns the magnitude of this vector.
*
* Note: this vector is not modified.
*
* @return the magnitude of this vector
*/
public double magnitude() {
return Math.sqrt(this.dot(this));
}
/**
* Returns the squared magnitude of this vector (<code>this.dot(this)</code>).
*
* Note: this vector is not modified.
*
* @return the squared magnitude of this vector
*/
double magnitudeSq() {
return this.dot(this);
}
/**
* Returns a normalized copy of this vector with length {@code 1}.
*
* Note: this vector is not modified.
*
* @return a normalized copy of this vector with length {@code 1}
*/
public Vector3d normalized() {
return this.dividedBy(this.magnitude());
}
/**
* Returns the cross product of this vector and the specified vector.
*
* Note: this vector is not modified.
*
* @param a
* the vector
*
* @return the cross product of this vector and the specified vector.
*/
public Vector3d cross(Vector3d a) {
return new Vector3d(this.y * a.z - this.z * a.y, this.z * a.x - this.x * a.z, this.x * a.y - this.y * a.x);
}
// Minimize ASCII STL size by removing trailing zeros
private static String stripTrailingZeros(String formatted) {
if ((formatted == null) || formatted.isEmpty())
return formatted;
int decPos = formatted.indexOf('.');
if (decPos == -1)
return formatted; // Nothing to do
int i = formatted.length() - 1;
while ((i > decPos) && (formatted.charAt(i) == '0'))
i--;
// Remove decimal point if needed
if (i == decPos)
i--;
return formatted.substring(0, i + 1);
}
/**
* Returns this vector in STL string format.
*
* @return this vector in STL string format
*/
public String toStlString() {
return toStlString(new StringBuilder()).toString();
}
/**
* Returns this vector in STL string format.
*
* @param sb
* string builder
* @return the specified string builder
*/
public StringBuilder toStlString(StringBuilder sb) {
double ep = getEXPORTEPSILON();
return sb.append(stripTrailingZeros(roundedValue(x, ep))).append(" ")
.append(stripTrailingZeros(roundedValue(y, ep))).append(" ")
.append(stripTrailingZeros(roundedValue(z, ep)));
}
public Vector3d roundToEpsilon(double ep) {
x = roundToEpsilon(x, ep);
y = roundToEpsilon(y, ep);
z = roundToEpsilon(z, ep);
return this;
}
/**
* Returns this vector in OBJ string format.
*
* @return this vector in OBJ string format
*/
public String toObjString() {
return toObjString(new StringBuilder()).toString();
}
/**
* Returns this vector in OBJ string format.
*
* @param sb
* string builder
* @return the specified string builder
*/
public StringBuilder toObjString(StringBuilder sb) {
double ep = getEXPORTEPSILON();
return sb.append(roundedValue(x, ep)).append(" ").append(roundedValue(y, ep)).append(" ")
.append(roundedValue(z, ep));
}
/**
* Rounds a double value to the nearest multiple of epsilon.
*
* @param value
* The value to round
* @return The rounded value
*/
private double roundToEpsilon(double value, double epsilon) {
// Round to nearest multiple of epsilon
return ((double) Math.round(value / epsilon)) * epsilon;
}
private String roundedValue(double v, double ep) {
return String.format(Locale.US, getExportString(), roundToEpsilon(v, ep));
}
/**
* Applies the specified transformation to this vector.
*
* @param transform
* the transform to apply
*
* @return this vector
*/
public Vector3d transform(Transform transform) {
return transform.transform(this);
}
/**
* Returns a transformed copy of this vector.
*
* @param transform
* the transform to apply
*
* Note: this vector is not modified.
*
* @return a transformed copy of this vector
*/
public Vector3d transformed(Transform transform) {
return clone().transform(transform);
}
/**
* Applies the specified transformation to this vector.
*
* @param transform
* the transform to apply
* @param amount
* the amount
* @return this vector
*/
public Vector3d transform(Transform transform, double amount) {
return transform.transform(this, amount);
}
/**
* Returns a transformed copy of this vector.
*
* @param transform
* the transform to apply
*
* Note: this vector is not modified.
* @param amount
* the amount
* @return a transformed copy of this vector
*/
public Vector3d transformed(Transform transform, double amount) {
return clone().transform(transform, amount);
}
public String toString() {
return "[" + toStlString().replaceAll(" ", " , ") + "]";
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
// if(Vertex.class.isInstance(obj)) {
// return equals(((Vertex)obj).pos);
// }
if (!Vector3d.class.isInstance(obj)) {
System.err.println("Test fail, " + obj.getClass() + " is not a Vector3d");
return false;
}
return test((Vector3d) obj, Plane.getEPSILON());
}
double distance(Vector3d v) {
Vector3d diff = v.minus(this);
return diff.magnitude();
}
public boolean test(Vector3d obj) {
return test(obj, Plane.getEPSILON());
}
public boolean test(Vector3d obj, double epsilon) {
if (this == obj)
return true;
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Vector3d other = (Vector3d) obj;
Vector3d diff = other.minus(this);
double distance = diff.magnitude();
double abs = Math.abs(distance);
if (abs > epsilon)
return false;
return true;
}
/**
* Returns the angle between this and the specified vector.
*
* @param v
* vector
* @return angle in radians
*/
public double angle(Vector3d v) {
double val = this.dot(v) / (this.magnitude() * v.magnitude());
return acos(max(min(val, 1), -1)); // compensate rounding errors
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int hash = 5;
hash = 97 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
hash = 97 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
hash = 97 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
return hash;
}
/**
* Creates a new vector with specified {@code x}.
*
* @param x
* x value
* @return a new vector {@code [x,0,0]}
*/
public static Vector3d x(double x) {
return new Vector3d(x, 0, 0);
}
/**
* Creates a new vector with specified {@code y}.
*
* @param y
* y value
* @return a new vector {@code [0,y,0]}
*/
public static Vector3d y(double y) {
return new Vector3d(0, y, 0);
}
/**
* Creates a new vector with specified {@code z}.
*
* @param z
* z value
* @return a new vector {@code [0,0,z]}
*/
public static Vector3d z(double z) {
return new Vector3d(0, 0, z);
}
public static String getExportString() {
return exportString;
}
static void setExportString(String exportString) {
Vector3d.exportString = exportString;
}
public static double getEXPORTEPSILON() {
return EXPORTEPSILON;
}
static void setEXPORTEPSILON(double eXPORTEPSILON) {
if (eXPORTEPSILON < 1.0e-5)
EXPORTEPSILON = eXPORTEPSILON;
}
}