JavaCAD
Transform.java
Go to the documentation of this file.
1 
34 package eu.mihosoft.vrl.v3d;
35 
36 import javax.vecmath.Matrix4d;
37 import javax.vecmath.Quat4d;
38 
39 // TODO: Auto-generated Javadoc
60 public class Transform {
61 
65  private final Matrix4d m;
66 
72  public Transform() {
73  m = new Matrix4d();
74  getInternalMatrix().m00 = 1;
75  getInternalMatrix().m11 = 1;
76  getInternalMatrix().m22 = 1;
77  getInternalMatrix().m33 = 1;
78  }
79 
85  public static Transform unity() {
86  return new Transform();
87  }
88 
95  public Transform(Matrix4d m) {
96  this.m = m;
97  }
98 
106  public Transform rotX(double degrees) {
107  double radians = degrees * Math.PI * (1.0 / 180.0);
108  double cos = Math.cos(radians);
109  double sin = Math.sin(radians);
110  double elemenents[] = { 1, 0, 0, 0, 0, cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1 };
111  getInternalMatrix().mul(new Matrix4d(elemenents));
112  return this;
113  }
114 
123  public Transform rotY(double degrees) {
124  double radians = degrees * Math.PI * (1.0 / 180.0);
125  double cos = Math.cos(radians);
126  double sin = Math.sin(radians);
127  double elemenents[] = { cos, 0, -sin, 0, 0, 1, 0, 0, sin, 0, cos, 0, 0, 0, 0, 1 };
128  getInternalMatrix().mul(new Matrix4d(elemenents));
129  return this;
130  }
131 
140  public Transform rotZ(double degrees) {
141  double radians = degrees * Math.PI * (1.0 / 180.0);
142  double cos = Math.cos(radians);
143  double sin = Math.sin(radians);
144  double elemenents[] = { cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
145  getInternalMatrix().mul(new Matrix4d(elemenents));
146  return this;
147  }
148 
161  public Transform rot(double x, double y, double z) {
162  return rotX(x).rotY(y).rotZ(z);
163  }
164 
173  public Transform rot(Vector3d vec) {
174 
175  // TODO: use quaternions
176  return rotX(vec.x).rotY(vec.y).rotZ(vec.z);
177  }
178 
188  return translate(vec.x, vec.y, vec.z);
189  }
190 
203  public Transform translate(double x, double y, double z) {
204  double elemenents[] = { 1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1 };
205  getInternalMatrix().mul(new Matrix4d(elemenents));
206  return this;
207  }
208 
217  public Transform translateX(double value) {
218  double elemenents[] = { 1, 0, 0, value, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
219  getInternalMatrix().mul(new Matrix4d(elemenents));
220  return this;
221  }
222 
223  // rotations
224  public double getQuataurionX() {
225  Matrix4d rotation = getInternalMatrix();
226  Quat4d q1 = new Quat4d();
227  rotation.get(q1);
228 
229  return q1.x;
230  }
231 
232  public double getQuataurionY() {
233  Matrix4d rotation = getInternalMatrix();
234  Quat4d q1 = new Quat4d();
235  rotation.get(q1);
236 
237  return q1.y;
238  }
239 
240  public double getQuataurionZ() {
241  Matrix4d rotation = getInternalMatrix();
242  Quat4d q1 = new Quat4d();
243  rotation.get(q1);
244 
245  return q1.z;
246  }
247 
248  public double getQuataurionW() {
249  Matrix4d rotation = getInternalMatrix();
250  Quat4d q1 = new Quat4d();
251  rotation.get(q1);
252 
253  return q1.w;
254  }
255 
256  // translations
257  public double getX() {
258  javax.vecmath.Vector3d t1 = new javax.vecmath.Vector3d();
259  getInternalMatrix().get(t1);
260  return t1.x;
261  }
262 
263  public double getY() {
264  javax.vecmath.Vector3d t1 = new javax.vecmath.Vector3d();
265  getInternalMatrix().get(t1);
266  return t1.y;
267  }
268 
269  public double getZ() {
270  javax.vecmath.Vector3d t1 = new javax.vecmath.Vector3d();
271  getInternalMatrix().get(t1);
272  return t1.z;
273  }
274 
275  /*
276  * (non-Javadoc)
277  *
278  * @see java.lang.Object#toString()
279  */
280  @Override
281  public String toString() {
282  javax.vecmath.Vector3d t1 = new javax.vecmath.Vector3d();
283  getInternalMatrix().get(t1);
284  Quat4d q1 = new Quat4d();
285  getInternalMatrix().get(q1);
286 
287  return "X="+t1.x+" Y="+t1.y+" Z="+t1.z+" Qx="+ q1.x+" Qy="+q1.y+" Qz="+q1.z+" Qw="+q1.w;
288  }
289 
290 
299  public Transform translateY(double value) {
300  double elemenents[] = { 1, 0, 0, 0, 0, 1, 0, value, 0, 0, 1, 0, 0, 0, 0, 1 };
301  getInternalMatrix().mul(new Matrix4d(elemenents));
302  return this;
303  }
304 
313  public Transform translateZ(double value) {
314  double elemenents[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, value, 0, 0, 0, 1 };
315  getInternalMatrix().mul(new Matrix4d(elemenents));
316  return this;
317  }
318 
327  public Transform mirror(Plane plane) {
328 
329  System.err.println("WARNING: I'm too dumb to implement the mirror() operation correctly. Please fix me!");
330 
331  double nx = plane.normal.x;
332  double ny = plane.normal.y;
333  double nz = plane.normal.z;
334  double w = plane.dist;
335  double elemenents[] = { (1.0 - 2.0 * nx * nx), (-2.0 * ny * nx), (-2.0 * nz * nx), 0, (-2.0 * nx * ny),
336  (1.0 - 2.0 * ny * ny), (-2.0 * nz * ny), 0, (-2.0 * nx * nz), (-2.0 * ny * nz), (1.0 - 2.0 * nz * nz),
337  0, (-2.0 * nx * w), (-2.0 * ny * w), (-2.0 * nz * w), 1 };
338  getInternalMatrix().mul(new Matrix4d(elemenents));
339  return this;
340  }
341 
350  public Transform scale(Vector3d vec) {
351 
352  if (vec.x == 0 || vec.y == 0 || vec.z == 0) {
353  throw new IllegalArgumentException("scale by 0 not allowed!");
354  }
355 
356  double elemenents[] = { vec.x, 0, 0, 0, 0, vec.y, 0, 0, 0, 0, vec.z, 0, 0, 0, 0, 1 };
357  getInternalMatrix().mul(new Matrix4d(elemenents));
358  return this;
359  }
360 
373  public Transform scale(double x, double y, double z) {
374 
375  if (x == 0 || y == 0 || z == 0) {
376  throw new IllegalArgumentException("scale by 0 not allowed!");
377  }
378 
379  double elemenents[] = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 };
380  getInternalMatrix().mul(new Matrix4d(elemenents));
381  return this;
382  }
383 
392  public Transform scale(double s) {
393 
394  if (s == 0) {
395  throw new IllegalArgumentException("scale by 0 not allowed!");
396  }
397 
398  double elemenents[] = { s, 0, 0, 0, 0, s, 0, 0, 0, 0, s, 0, 0, 0, 0, 1 };
399  getInternalMatrix().mul(new Matrix4d(elemenents));
400  return this;
401  }
402 
411  public Transform scaleX(double s) {
412 
413  if (s == 0) {
414  throw new IllegalArgumentException("scale by 0 not allowed!");
415  }
416 
417  double elemenents[] = { s, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
418  getInternalMatrix().mul(new Matrix4d(elemenents));
419  return this;
420  }
421 
430  public Transform scaleY(double s) {
431 
432  if (s == 0) {
433  throw new IllegalArgumentException("scale by 0 not allowed!");
434  }
435 
436  double elemenents[] = { 1, 0, 0, 0, 0, s, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
437  getInternalMatrix().mul(new Matrix4d(elemenents));
438  return this;
439  }
440 
449  public Transform scaleZ(double s) {
450 
451  if (s == 0) {
452  throw new IllegalArgumentException("scale by 0 not allowed!");
453  }
454 
455  double elemenents[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, s, 0, 0, 0, 0, 1 };
456  getInternalMatrix().mul(new Matrix4d(elemenents));
457  return this;
458  }
459 
469  double x, y;
470  x = getInternalMatrix().m00 * vec.x + getInternalMatrix().m01 * vec.y + getInternalMatrix().m02 * vec.z
471  + getInternalMatrix().m03;
472  y = getInternalMatrix().m10 * vec.x + getInternalMatrix().m11 * vec.y + getInternalMatrix().m12 * vec.z
473  + getInternalMatrix().m13;
474  vec.z = getInternalMatrix().m20 * vec.x + getInternalMatrix().m21 * vec.y + getInternalMatrix().m22 * vec.z
475  + getInternalMatrix().m23;
476  vec.x = x;
477  vec.y = y;
478 
479  return vec;
480  }
481 
492  public Vector3d transform(Vector3d vec, double amount) {
493 
494  double prevX = vec.x;
495  double prevY = vec.y;
496  double prevZ = vec.z;
497 
498  final double x, y;
499  x = getInternalMatrix().m00 * vec.x + getInternalMatrix().m01 * vec.y + getInternalMatrix().m02 * vec.z
500  + getInternalMatrix().m03;
501  y = getInternalMatrix().m10 * vec.x + getInternalMatrix().m11 * vec.y + getInternalMatrix().m12 * vec.z
502  + getInternalMatrix().m13;
503  vec.z = getInternalMatrix().m20 * vec.x + getInternalMatrix().m21 * vec.y + getInternalMatrix().m22 * vec.z
504  + getInternalMatrix().m23;
505  vec.x = x;
506  vec.y = y;
507 
508  double diffX = vec.x - prevX;
509  double diffY = vec.y - prevY;
510  double diffZ = vec.z - prevZ;
511 
512  vec.x = prevX + (diffX) * amount;
513  vec.y = prevY + (diffY) * amount;
514  vec.z = prevZ + (diffZ) * amount;
515 
516  return vec;
517  }
518 
519  // // Multiply a CSG.Vector3D (interpreted as 3 column, 1 row) by this matrix
520  // // (result = v*M)
521  // // Fourth element is taken as 1
522  // leftMultiply1x3Vector: function(v) {
523  // var v0 = v._x;
524  // var v1 = v._y;
525  // var v2 = v._z;
526  // var v3 = 1;
527  // var x = v0 * this.elements[0] + v1 * this.elements[4] + v2 * this.elements[8]
528  // + v3 * this.elements[12];
529  // var y = v0 * this.elements[1] + v1 * this.elements[5] + v2 * this.elements[9]
530  // + v3 * this.elements[13];
531  // var z = v0 * this.elements[2] + v1 * this.elements[6] + v2 *
532  // this.elements[10] + v3 * this.elements[14];
533  // var w = v0 * this.elements[3] + v1 * this.elements[7] + v2 *
534  // this.elements[11] + v3 * this.elements[15];
535  // // scale such that fourth element becomes 1:
536  // if(w != 1) {
537  // var invw = 1.0 / w;
538  // x *= invw;
539  // y *= invw;
540  // z *= invw;
541  // }
542  // return new CSG.Vector3D(x, y, z);
543  // },
553  public double getScale() {
554  return getInternalMatrix().getScale();
555  }
556 
564  public boolean isMirror() {
565  return getInternalMatrix().determinant() < 0;
566  }
567 
578  return this;
579  }
580 
581 
582  public Matrix4d getInternalMatrix() {
583  return m;
584  }
585 
591  public Transform inverse() {
592  Transform tr = new Transform().apply(this);
593 
594  tr.getInternalMatrix().invert();
595 
596  return tr;
597  }
598 
604  public Transform invert() {
605  getInternalMatrix().invert();
606 
607  return this;
608  }
609 
610  public Transform move(Number x, Number y, Number z) {
611  return new Transform().translate(x.doubleValue(),y.doubleValue(),z.doubleValue()).apply(this);
612  }
613  public Transform move(Vertex v) {
614  return new Transform().translate(v.getX(),v.getY(),v.getZ()).apply(this);
615  }
616  public Transform move(Vector3d v) {
617  return new Transform().translate(v.x,v.y,v.z).apply(this);
618  }
619  public Transform move(Number[] posVector) {
620  return move(posVector[0], posVector[1], posVector[2]);
621  }
622 
630  // Helper/wrapper functions for movement
631  public Transform movey(Number howFarToMove) {
632  return new Transform().translateY(howFarToMove.doubleValue()).apply(this);
633  }
634 
642  public Transform movez(Number howFarToMove) {
643  return new Transform().translateZ(howFarToMove.doubleValue()).apply(this);
644  }
645 
653  public Transform movex(Number howFarToMove) {
654  return new Transform().translateX(howFarToMove.doubleValue()).apply(this);
655  }
656 
663  // Helper/wrapper functions for movement
664  public Transform mirrory() {
665  return this.scaleY(-1);
666  }
667 
673  public Transform mirrorz() {
674  return this.scaleZ(-1);
675  }
676 
682  public Transform mirrorx() {
683  return this.scaleX(-1);
684  }
685 
693  // Rotation function, rotates the object
694  public Transform rotz(Number degreesToRotate) {
695  return new Transform().rotZ(degreesToRotate.doubleValue()).apply(this);
696  }
697 
705  public Transform roty(Number degreesToRotate) {
706  return new Transform().rotY(degreesToRotate.doubleValue()).apply(this);
707  }
708 
716  public Transform rotx(Number degreesToRotate) {
717  return new Transform().rotX(degreesToRotate.doubleValue()).apply(this);
718  }
719 
720 }
Transform scaleX(double s)
Definition: Transform.java:411
Transform apply(Transform t)
Definition: Transform.java:576
Transform rot(double x, double y, double z)
Definition: Transform.java:161
Transform rot(Vector3d vec)
Definition: Transform.java:173
Transform move(Vertex v)
Definition: Transform.java:613
Vector3d transform(Vector3d vec, double amount)
Definition: Transform.java:492
Transform movey(Number howFarToMove)
Definition: Transform.java:631
Transform roty(Number degreesToRotate)
Definition: Transform.java:705
Transform rotZ(double degrees)
Definition: Transform.java:140
Transform scaleY(double s)
Definition: Transform.java:430
Transform rotx(Number degreesToRotate)
Definition: Transform.java:716
Transform translate(double x, double y, double z)
Definition: Transform.java:203
Transform mirror(Plane plane)
Definition: Transform.java:327
Transform rotX(double degrees)
Definition: Transform.java:106
Transform translateZ(double value)
Definition: Transform.java:313
Transform rotz(Number degreesToRotate)
Definition: Transform.java:694
Vector3d transform(Vector3d vec)
Definition: Transform.java:468
Transform move(Number[] posVector)
Definition: Transform.java:619
Transform translate(Vector3d vec)
Definition: Transform.java:187
Transform scaleZ(double s)
Definition: Transform.java:449
Transform scale(double x, double y, double z)
Definition: Transform.java:373
Transform rotY(double degrees)
Definition: Transform.java:123
Transform move(Number x, Number y, Number z)
Definition: Transform.java:610
Transform scale(double s)
Definition: Transform.java:392
Transform movez(Number howFarToMove)
Definition: Transform.java:642
Transform translateX(double value)
Definition: Transform.java:217
static Transform unity()
Definition: Transform.java:85
Transform scale(Vector3d vec)
Definition: Transform.java:350
Transform translateY(double value)
Definition: Transform.java:299
Transform movex(Number howFarToMove)
Definition: Transform.java:653
Transform move(Vector3d v)
Definition: Transform.java:616
static Vector3d y(double y)
Definition: Vector3d.java:484
static Vector3d z(double z)
Definition: Vector3d.java:494
static Vector3d x(double x)
Definition: Vector3d.java:474