JavaCAD
Vector3d.java
Go to the documentation of this file.
1 
34 package eu.mihosoft.vrl.v3d;
35 
36 import static java.lang.Math.abs;
37 import static java.lang.Math.acos;
38 import static java.lang.Math.max;
39 import static java.lang.Math.min;
40 import java.util.Random;
41 
42 // TODO: Auto-generated Javadoc
48 public class Vector3d extends javax.vecmath.Vector3d{
49 
50 
54  private static final long serialVersionUID = 1878117798187075166L;
55 
57  public static final Vector3d ZERO = new Vector3d(0, 0, 0);
58 
60  public static final Vector3d UNITY = new Vector3d(1, 1, 1);
61 
63  public static final Vector3d X_ONE = new Vector3d(1, 0, 0);
64 
66  public static final Vector3d Y_ONE = new Vector3d(0, 1, 0);
67 
69  public static final Vector3d Z_ONE = new Vector3d(0, 0, 1);
70 
78  public Vector3d(double x, double y, double z) {
79  this.x = x;
80  this.y = y;
81  this.z = z;
82  }
83 
84  public Vector3d(Number x, Number y, Number z) {
85  this.x = x.doubleValue();
86  this.y = y.doubleValue();
87  this.z = z.doubleValue();
88  }
89 
90 
98  public Vector3d(double x, double y) {
99 
100  this.x = x;
101  this.y = y;
102  this.z = 0;
103  }
104 
105  public Vector3d(Number x, Number y) {
106 
107  this(x, y, (double) 0);
108 
109  }
110 
111 
119  public static Vector3d xy(double x, double y) {
120  return new Vector3d(x,y);
121  }
122  public static Vector3d xy(Number x, Number y) {
123  return xy(x.doubleValue(),y.doubleValue());
124  }
125 
134  public static Vector3d xyz(double x, double y, double z) {
135  return new Vector3d(x,y,z);
136  }
137  public static Vector3d xyz(Number x, Number y, Number z) {
138  return new Vector3d(x,y,z);
139  }
140  @Override
141  public Vector3d clone() {
142  return new Vector3d(x, y, z);
143  }
144 
152  public Vector3d negated() {
153  return new Vector3d(-x, -y, -z);
154  }
155 
165  public Vector3d plus(Vector3d v) {
166  return new Vector3d(x + v.x, y + v.y, z + v.z);
167  }
168 
178  public Vector3d minus(Vector3d v) {
179  return new Vector3d(x - v.x, y - v.y, z - v.z);
180  }
181 
191  public Vector3d times(double a) {
192  return new Vector3d(x * a, y * a, z * a);
193  }
194 
204  public Vector3d times(Vector3d a) {
205  return new Vector3d(x * a.x, y * a.y, z * a.z);
206  }
207 
217  public Vector3d dividedBy(double a) {
218  return new Vector3d(x / a, y / a, z / a);
219  }
220 
230  public double dot(Vector3d a) {
231  return this.x * a.x + this.y * a.y + this.z * a.z;
232  }
233 
245  public Vector3d lerp(Vector3d a, double t) {
246  return this.plus(a.minus(this).times(t));
247  }
248 
256  public double magnitude() {
257  return Math.sqrt(this.dot(this));
258  }
259 
267  double magnitudeSq() {
268  return this.dot(this);
269  }
270 
278  public Vector3d normalized() {
279  return this.dividedBy(this.magnitude());
280  }
281 
291  public Vector3d cross(Vector3d a) {
292  return new Vector3d(
293  this.y * a.z - this.z * a.y,
294  this.z * a.x - this.x * a.z,
295  this.x * a.y - this.y * a.x
296  );
297  }
298 
304  public String toStlString() {
305  return toStlString(new StringBuilder()).toString();
306  }
307 
314  public StringBuilder toStlString(StringBuilder sb) {
315  return sb.append(this.x).append(" ").
316  append(this.y).append(" ").
317  append(this.z);
318  }
319 
325  public String toObjString() {
326  return toObjString(new StringBuilder()).toString();
327  }
328 
335  public StringBuilder toObjString(StringBuilder sb) {
336  return sb.append(this.x).append(" ").
337  append(this.y).append(" ").
338  append(this.z);
339  }
340 
349  return transform.transform(this);
350  }
351 
362  return clone().transform(transform);
363  }
364 
372  public Vector3d transform(Transform transform, double amount) {
373  return transform.transform(this, amount);
374  }
375 
385  public Vector3d transformed(Transform transform, double amount) {
386  return clone().transform(transform, amount);
387  }
388 
389  /* (non-Javadoc)
390  * @see java.lang.Object#toString()
391  */
392  @Override
393  public String toString() {
394  return "[" + x + ", " + y + ", " + z + "]";
395  }
396 
397  /* (non-Javadoc)
398  * @see java.lang.Object#equals(java.lang.Object)
399  */
400  @Override
401  public boolean equals(Object obj) {
402  return test(obj,Plane.EPSILON_Point);
403  }
404 
405  public boolean test(Object obj, double epsilon) {
406  if (obj == null) {
407  return false;
408  }
409  if (getClass() != obj.getClass()) {
410  return false;
411  }
412  final Vector3d other = (Vector3d) obj;
413  if (abs(this.x - other.x) > epsilon) {
414  return false;
415  }
416  if (abs(this.y - other.y) > epsilon) {
417  return false;
418  }
419  if (abs(this.z - other.z) > epsilon) {
420  return false;
421  }
422  return true;
423  }
424 
431  public double angle(Vector3d v) {
432  double val = this.dot(v) / (this.magnitude() * v.magnitude());
433  return acos(max(min(val, 1), -1)); // compensate rounding errors
434  }
435 
436  /* (non-Javadoc)
437  * @see java.lang.Object#hashCode()
438  */
439  @Override
440  public int hashCode() {
441  int hash = 5;
442  hash = 97 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
443  hash = 97 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
444  hash = 97 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
445  return hash;
446  }
447 
448 // @Override
449 // public boolean equals(Object obj) {
450 // if (obj == null) {
451 // return false;
452 // }
453 // if (getClass() != obj.getClass()) {
454 // return false;
455 // }
456 // final Vector3d other = (Vector3d) obj;
457 // if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) {
458 // return false;
459 // }
460 // if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y)) {
461 // return false;
462 // }
463 // if (Double.doubleToLongBits(this.z) != Double.doubleToLongBits(other.z)) {
464 // return false;
465 // }
466 // return true;
467 // }
474  public static Vector3d x(double x) {
475  return new Vector3d(x, 0, 0);
476  }
477 
484  public static Vector3d y(double y) {
485  return new Vector3d(0, y, 0);
486  }
487 
494  public static Vector3d z(double z) {
495  return new Vector3d(0, 0, z);
496  }
497 
508  public Vector3d orthogonal() {
509 
510 // if ((this.x == Double.NaN) || (this.y == Double.NaN) || (this.z == Double.NaN)) {
511 // throw new IllegalStateException("NaN is not a valid entry for a vector.");
512 // }
513  double o1 = 0.0;
514  double o2 = 0.0;
515  double o3 = 0.0;
516 
517  Random r = new Random();
518 
519  int numberOfZeroEntries = 0;
520 
521  if (this.x == 0) {
522  numberOfZeroEntries++;
523  o1 = r.nextDouble();
524  }
525 
526  if (this.y == 0) {
527  numberOfZeroEntries++;
528  o2 = r.nextDouble();
529  }
530 
531  if (this.z == 0) {
532  numberOfZeroEntries++;
533  o3 = r.nextDouble();
534  }
535 
536  switch (numberOfZeroEntries) {
537 
538  case 0:
539  // all this_i != 0
540  //
541  //we do not want o3 to be zero
542  while (o3 == 0) {
543  o3 = r.nextDouble();
544  }
545 
546  //we do not want o2 to be zero
547  while (o2 == 0) {
548  o2 = r.nextDouble();
549  }
550  // calculate or choose randomly ??
551 // o2 = -this.z * o3 / this.y;
552 
553  o1 = (-this.y * o2 - this.z * o3) / this.x;
554 
555  break;
556 
557  case 1:
558  // this_i = 0 , i € {1,2,3}
559  // this_j != 0 != this_k , j,k € {1,2,3}\{i}
560  //
561  // choose one none zero randomly and calculate the other one
562 
563  if (this.x == 0) {
564  //we do not want o3 to be zero
565  while (o3 == 0) {
566  o3 = r.nextDouble();
567  }
568 
569  o2 = -this.z * o3 / this.y;
570 
571  } else if (this.y == 0) {
572 
573  //we do not want o3 to be zero
574  while (o3 == 0) {
575  o3 = r.nextDouble();
576  }
577 
578  o1 = -this.z * o3 / this.x;
579 
580  } else if (this.z == 0) {
581 
582  //we do not want o1 to be zero
583  while (o1 == 0) {
584  o1 = r.nextDouble();
585  }
586 
587  o2 = -this.z * o1 / this.y;
588  }
589 
590  break;
591 
592  case 2:
593  // if two parts of this are 0 we can achieve orthogonality
594  // via setting the corressponding part of the orthogonal vector
595  // to zero this is ALREADY DONE in the init (o_i = 0.0)
596  // NO CODE NEEDED
597 // if (this.x == 0) {
598 // o1 = 0;
599 // } else if (this.y == 0) {
600 // o2 = 0;
601 // } else if (this.z == 0) {
602 // o3 = 0;
603 // }
604  break;
605 
606  case 3:
607  System.err.println("This vector is equal to (0,0,0). ");
608 
609  default:
610  System.err.println("The orthogonal one is set randomly.");
611 
612  o1 = r.nextDouble();
613  o2 = r.nextDouble();
614  o3 = r.nextDouble();
615  }
616 
617  Vector3d result = new Vector3d(o1, o2, o3);
618 
619 // if ((this.x ==Double.NaN) || (this.y == Double.NaN) || (this.z == Double.NaN)) {
620 // throw new IllegalStateException("NaN is not a valid entry for a vector.");
621 // }
622 // System.out.println(" this : "+ this);
623 // System.out.println(" result : "+ result);
624  // check if the created vector is really orthogonal to this
625  // if not try one more time
626  while (this.dot(result) != 0.0) {
627  result = this.orthogonal();
628  }
629 
630  return result;
631 
632  }
633 // public double getX() {
634 // // TODO Auto-generated method stub
635 // return x;
636 // }
637 // public double getY() {
638 // // TODO Auto-generated method stub
639 // return y;
640 // }
641 // public double getZ() {
642 // // TODO Auto-generated method stub
643 // return z;
644 // }
645 
646 }
static final double EPSILON_Point
Definition: Plane.java:56
static final long serialVersionUID
Definition: Vector3d.java:54
Vector3d cross(Vector3d a)
Definition: Vector3d.java:291
boolean equals(Object obj)
Definition: Vector3d.java:401
Vector3d(double x, double y, double z)
Definition: Vector3d.java:78
StringBuilder toObjString(StringBuilder sb)
Definition: Vector3d.java:335
Vector3d transform(Transform transform)
Definition: Vector3d.java:348
static Vector3d xyz(Number x, Number y, Number z)
Definition: Vector3d.java:137
Vector3d times(double a)
Definition: Vector3d.java:191
StringBuilder toStlString(StringBuilder sb)
Definition: Vector3d.java:314
Vector3d(Number x, Number y)
Definition: Vector3d.java:105
static final Vector3d X_ONE
Definition: Vector3d.java:63
Vector3d plus(Vector3d v)
Definition: Vector3d.java:165
Vector3d transformed(Transform transform, double amount)
Definition: Vector3d.java:385
double angle(Vector3d v)
Definition: Vector3d.java:431
static Vector3d y(double y)
Definition: Vector3d.java:484
static Vector3d xyz(double x, double y, double z)
Definition: Vector3d.java:134
static Vector3d xy(double x, double y)
Definition: Vector3d.java:119
static final Vector3d UNITY
Definition: Vector3d.java:60
static final Vector3d ZERO
Definition: Vector3d.java:57
double dot(Vector3d a)
Definition: Vector3d.java:230
Vector3d minus(Vector3d v)
Definition: Vector3d.java:178
static Vector3d z(double z)
Definition: Vector3d.java:494
Vector3d transformed(Transform transform)
Definition: Vector3d.java:361
Vector3d(double x, double y)
Definition: Vector3d.java:98
Vector3d transform(Transform transform, double amount)
Definition: Vector3d.java:372
Vector3d(Number x, Number y, Number z)
Definition: Vector3d.java:84
boolean test(Object obj, double epsilon)
Definition: Vector3d.java:405
Vector3d lerp(Vector3d a, double t)
Definition: Vector3d.java:245
static Vector3d x(double x)
Definition: Vector3d.java:474
static Vector3d xy(Number x, Number y)
Definition: Vector3d.java:122
Vector3d dividedBy(double a)
Definition: Vector3d.java:217
static final Vector3d Z_ONE
Definition: Vector3d.java:69
Vector3d times(Vector3d a)
Definition: Vector3d.java:204
static final Vector3d Y_ONE
Definition: Vector3d.java:66