1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.math;
14 import javax.vecmath.AxisAngle4d;
15 import javax.vecmath.Matrix3d;
16 import javax.vecmath.Matrix4d;
17 import javax.vecmath.Quat4d;
18 import javax.vecmath.Tuple2d;
19 import javax.vecmath.Tuple3d;
20 import javax.vecmath.Tuple4d;
21 import javax.vecmath.Vector2d;
22 import javax.vecmath.Vector2f;
23 import javax.vecmath.Vector3d;
25 import org.simantics.g3d.math.EulerTools.Order;
29 * Some useful geometry related math functions. Beware, methods may modify their input parameters!
31 * @author Marko Luukkainen
34 public class MathTools {
36 public static double NEAR_ZERO = 0.0000001;
37 public static double NEAR_HALF = 0.4999999;
39 public static final Vector3d Z_AXIS = new Vector3d(0.0,0.0,1.0);
40 public static final Vector3d Y_AXIS = new Vector3d(0.0,1.0,0.0);
41 public static final Vector3d X_AXIS = new Vector3d(1.0,0.0,0.0);
42 public static final Vector3d ORIGIN = new Vector3d(0.0,0.0,0.0);
44 final static double EPS = 1.0e-12;
47 public static boolean equals(double d1, double d2) {
48 return Math.abs(d1-d2) < EPS;
51 public static boolean equals(Tuple3d p1, Tuple3d p2) {
52 return distanceSquared(p1, p2) < NEAR_ZERO;
55 public static boolean equals(Tuple4d p1, Tuple4d p2) {
56 return distanceSquared(p1, p2) < NEAR_ZERO;
59 public static double distance(Tuple3d p1, Tuple3d p2) {
65 return Math.sqrt(dx*dx+dy*dy+dz*dz);
68 public static double distance(Tuple4d p1, Tuple4d p2) {
69 double dx, dy, dz, dw;
75 return Math.sqrt(dx*dx+dy*dy+dz*dz+dw*dw);
78 public static double distanceSquared(Tuple3d p1, Tuple3d p2) {
84 return dx*dx+dy*dy+dz*dz;
87 public static double distanceSquared(Tuple4d p1, Tuple4d p2) {
88 double dx, dy, dz, dw;
94 return dx*dx+dy*dy+dz*dz+dw*dw;
97 public static boolean isValid(Tuple3d t) {
98 return !(Double.isInfinite(t.x) || Double.isNaN(t.x) ||
99 Double.isInfinite(t.y) || Double.isNaN(t.y) ||
100 Double.isInfinite(t.z) || Double.isNaN(t.z));
103 public static Vector3d closestPointOnEdge(Vector3d point, Vector3d edgePoint1, Vector3d edgePoint2) {
104 point.sub(edgePoint1);
105 Vector3d v = new Vector3d(edgePoint2);
107 double t = v.dot(point);
108 t /= v.lengthSquared();
118 public static Vector3d closestPointOnStraight(Tuple3d point, Tuple3d straightPoint, Vector3d straightDir) {
119 Vector3d v = new Vector3d(point);
120 v.sub(straightPoint);
121 double t = straightDir.dot(v);
122 t /= straightDir.lengthSquared();
125 v.add(straightPoint);
129 public static Vector3d closestPointOnStraight(Tuple3d point, Tuple3d straightPoint, Vector3d straightDir, double u[]) {
130 Vector3d v = new Vector3d(point);
131 v.sub(straightPoint);
132 u[0] = straightDir.dot(v);
133 u[0] /= straightDir.lengthSquared();
136 v.add(straightPoint);
140 public static double distanceFromPlane(Vector3d point, Vector3d planeNormal, Tuple3d planePoint) {
141 point.sub(planePoint);
143 return planeNormal.dot(point);
146 public static double distanceFromPlane(Vector3d point, Vector3d planeNormal, float d) {
147 return (planeNormal.dot(point) + d);
150 public static boolean intersectStraightPlane(Tuple3d linePoint, Vector3d lineDir, Tuple3d planePoint, Vector3d planeNormal, Tuple3d intersectPoint) {
151 intersectPoint.set(planePoint);
152 intersectPoint.sub(linePoint);
153 double u = planeNormal.dot(new Vector3d(intersectPoint));
154 double v = planeNormal.dot(lineDir);
155 if (Math.abs(v) < NEAR_ZERO)
158 intersectPoint.set(lineDir);
159 intersectPoint.scale(u);
160 intersectPoint.add(linePoint);
164 public static boolean intersectStraightPlane(Tuple3d linePoint, Vector3d lineDir, Tuple3d planePoint, Vector3d planeNormal, Vector3d intersectPoint, double[] u) {
165 intersectPoint.set(planePoint);
166 intersectPoint.sub(linePoint);
167 u[0] = planeNormal.dot(intersectPoint);
168 double v = planeNormal.dot(lineDir);
169 if (Math.abs(v) < NEAR_ZERO)
172 intersectPoint.set(lineDir);
173 intersectPoint.scale(u[0]);
174 intersectPoint.add(linePoint);
178 public static boolean intersectLineLine(Tuple3d l1_start,Tuple3d l1_end,Tuple3d l2_start,Tuple3d l2_end,Tuple3d l1_pos, Tuple3d l2_pos) {
179 Vector3d p13 = new Vector3d();
180 Vector3d p43 = new Vector3d();
181 Vector3d p21 = new Vector3d();
182 double d1343,d4321,d1321,d4343,d2121;
184 p13.sub(l1_start, l2_start);
185 p43.sub(l2_end,l2_start);
186 if (Math.abs(p43.x) < NEAR_ZERO && Math.abs(p43.y) < NEAR_ZERO && Math.abs(p43.z) < NEAR_ZERO)
188 p21.sub(l1_end,l1_start);
189 if (Math.abs(p21.x) < NEAR_ZERO && Math.abs(p21.y) < NEAR_ZERO && Math.abs(p21.z) < NEAR_ZERO)
192 d1343 = p13.dot(p43);
193 d4321 = p43.dot(p21);
194 d1321 = p13.dot(p21);
195 d4343 = p43.lengthSquared();
196 d2121 = p21.lengthSquared();
198 denom = d2121 * d4343 - d4321 * d4321;
199 if (Math.abs(denom) < NEAR_ZERO)
201 numer = d1343 * d4321 - d1321 * d4343;
203 double mua = numer / denom;
204 double mub = (d1343 + d4321 * mua) / d4343;
206 l1_pos.x = l1_start.x + mua * p21.x;
207 l1_pos.y = l1_start.y + mua * p21.y;
208 l1_pos.z = l1_start.z + mua * p21.z;
209 l2_pos.x = l2_start.x + mub * p43.x;
210 l2_pos.y = l2_start.y + mub * p43.y;
211 l2_pos.z = l2_start.z + mub * p43.z;
216 public static boolean intersectStraightStraight(Tuple3d p1,Vector3d p21,Tuple3d p3,Vector3d p43,Tuple3d pa,Tuple3d pb) {
217 Vector3d p13 = new Vector3d();
219 double d1343,d4321,d1321,d4343,d2121;
223 if (Math.abs(p43.x) < NEAR_ZERO && Math.abs(p43.y) < NEAR_ZERO && Math.abs(p43.z) < NEAR_ZERO)
225 if (Math.abs(p21.x) < NEAR_ZERO && Math.abs(p21.y) < NEAR_ZERO && Math.abs(p21.z) < NEAR_ZERO)
228 d1343 = p13.dot(p43);
229 d4321 = p43.dot(p21);
230 d1321 = p13.dot(p21);
231 d4343 = p43.lengthSquared();
232 d2121 = p21.lengthSquared();
234 denom = d2121 * d4343 - d4321 * d4321;
235 if (Math.abs(denom) < NEAR_ZERO)
237 numer = d1343 * d4321 - d1321 * d4343;
239 double mua = numer / denom;
240 double mub = (d1343 + d4321 * mua) / d4343;
242 pa.x = p1.x + mua * p21.x;
243 pa.y = p1.y + mua * p21.y;
244 pa.z = p1.z + mua * p21.z;
245 pb.x = p3.x + mub * p43.x;
246 pb.y = p3.y + mub * p43.y;
247 pb.z = p3.z + mub * p43.z;
253 * Calculate the line segment PaPb that is the shortest route between
254 * two lines P1P2 and P3P4. Calculate also the values of mua and mub where
255 * Pa = P1 + mua (P2 - P1)
256 * Pb = P3 + mub (P4 - P3)
266 public static boolean intersectStraightStraight(Tuple3d p1,Vector3d p21,Tuple3d p3,Vector3d p43,Tuple3d pa,Tuple3d pb, double mu[]) {
267 Vector3d p13 = new Vector3d();
269 double d1343,d4321,d1321,d4343,d2121;
273 if (Math.abs(p43.x) < EPS && Math.abs(p43.y) < EPS && Math.abs(p43.z) < EPS)
275 if (Math.abs(p21.x) < EPS && Math.abs(p21.y) < EPS && Math.abs(p21.z) < EPS)
278 d1343 = p13.dot(p43);
279 d4321 = p43.dot(p21);
280 d1321 = p13.dot(p21);
281 d4343 = p43.lengthSquared();
282 d2121 = p21.lengthSquared();
284 denom = d2121 * d4343 - d4321 * d4321;
285 if (Math.abs(denom) < EPS)
287 numer = d1343 * d4321 - d1321 * d4343;
289 mu[0] = numer / denom;
290 mu[1] = (d1343 + d4321 * mu[0]) / d4343;
292 pa.x = p1.x + mu[0] * p21.x;
293 pa.y = p1.y + mu[0] * p21.y;
294 pa.z = p1.z + mu[0] * p21.z;
295 pb.x = p3.x + mu[1] * p43.x;
296 pb.y = p3.y + mu[1] * p43.y;
297 pb.z = p3.z + mu[1] * p43.z;
304 public static void rotate(Quat4d q, Tuple3d in, Tuple3d out) {
306 double tw = - q.x*in.x - q.y*in.y - q.z*in.z;
307 double tx = q.w*in.x + q.y*in.z - q.z*in.y;
308 double ty = q.w*in.y - q.x*in.z + q.z*in.x;
309 double tz = q.w*in.z + q.x*in.y - q.y*in.x ;
311 //temp * q' -> x = -x, y = -y z = -z
312 //out.w = tw*q.w + tx*q.x + ty*q.y + tz*q.z;
313 out.x = -tw*q.x + tx*q.w - ty*q.z + tz*q.y;
314 out.y = -tw*q.y + tx*q.z + ty*q.w - tz*q.x;
315 out.z = -tw*q.z - tx*q.y + ty*q.x + tz*q.w;
318 public static void getMatrix(Quat4d quat, Matrix3d m) {
319 m.m00 = 1.0f - 2.0 * (quat.y * quat.y + quat.z * quat.z);
320 m.m01 = 2.0 * (quat.x * quat.y + quat.w * quat.z);
321 m.m02 = 2.0 * (quat.x * quat.z - quat.w * quat.y);
322 m.m10 = 2.0 * (quat.x * quat.y - quat.w * quat.z);
323 m.m11 = 1.0 - 2.0f * (quat.x * quat.x + quat.z * quat.z);
324 m.m12 = 2.0 * (quat.y * quat.z + quat.w * quat.x);
325 m.m20 = 2.0 * (quat.x * quat.z + quat.w * quat.y);
326 m.m21 = 2.0 * (quat.y * quat.z - quat.w * quat.x);
327 m.m22 = 1.0 - 2.0f * (quat.x * quat.x + quat.y * quat.y);
332 private static double q[] = new double[3];
333 private static int nxt[] = { 1, 2, 0 };
335 * Converts Matrix to Quaternion
337 * Note: non-thread safe.
342 public static void getQuat(Matrix3d mat, Quat4d quat) {
343 double tr = mat.m00 + mat.m11 + mat.m22;
345 double s = Math.sqrt(tr + 1.0);
348 quat.x = (mat.m21 - mat.m12) * s;
349 quat.y = (mat.m02 - mat.m20) * s;
350 quat.z = (mat.m10 - mat.m01) * s;
353 if (mat.m11 > mat.m00)
355 if (mat.m22 > mat.getElement(i, i))
362 double s = Math.sqrt((mat.getElement(i, i) - (mat.getElement(j, j) + mat.getElement(k, k))) + 1.0);
366 if (Math.abs(s) > 0.001)
369 quat.w = (mat.getElement(k, j) - mat.getElement(j, k)) * s;
370 q[j] = (mat.getElement(j, i) + mat.getElement(i, j)) * s;
371 q[k] = (mat.getElement(k, i) + mat.getElement(i, k)) * s;
379 public static Quat4d getQuat(Matrix3d mat) {
380 Quat4d q = new Quat4d();
385 public static AxisAngle4d getFromPseudoEuler(Vector3d euler) {
386 AxisAngle4d aa = new AxisAngle4d();
387 aa.angle = euler.length();
388 Vector3d normal = new Vector3d(euler);
389 if (aa.angle > NEAR_ZERO) {
403 public static Vector3d getPseudoEuler(AxisAngle4d aa) {
404 Vector3d euler = new Vector3d(aa.x,aa.y,aa.z);
405 euler.scale(aa.angle);
410 public static void getQuat(Vector3d euler, Quat4d quat) {
411 Quat4d q = EulerTools.getQuatFromEuler(Order.YXZ, euler.y,euler.x,euler.z);
413 // http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Conversion_formulae_between_formalisms
414 // Using the x-convention, the 3-1-3 Euler angles phi, theta and psi (around the Z, X and again the Z-axis)
415 // quat.x = -Math.cos((euler.x - euler.z)*0.5)*Math.sin(euler.y*0.5);
416 // quat.y = -Math.sin((euler.x - euler.z)*0.5)*Math.sin(euler.y*0.5);
417 // quat.z = -Math.sin((euler.x + euler.z)*0.5)*Math.cos(euler.y*0.5);
418 // quat.w = Math.sin((euler.x + euler.z)*0.5)*Math.cos(euler.y*0.5);
420 // http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm
422 // double c1 = Math.cos(euler.y*0.5);
423 // double s1 = Math.sin(euler.y*0.5);
424 // double c2 = Math.cos(euler.z*0.5);
425 // double s2 = Math.sin(euler.z*0.5);
426 // double c3 = Math.cos(euler.x*0.5);
427 // double s3 = Math.sin(euler.x*0.5);
428 // double c1c2 = c1*c2;
429 // double s1s2 = s1*s2;
430 // quat.w =c1c2*c3 - s1s2*s3;
431 // quat.x =c1c2*s3 + s1s2*c3;
432 // quat.y =s1*c2*c3 + c1*s2*s3;
433 // quat.z =c1*s2*c3 - s1*c2*s3;
435 // Quat4d q2 = EulerTools.getQuatFromEuler(Order.YZX, euler.y,euler.z,euler.x);
436 // System.out.println("Q " + quat + " Q2 " + q2);
437 // double c1 = Math.cos(euler.y);
438 // double s1 = Math.sin(euler.y);
439 // double c2 = Math.cos(euler.z);
440 // double s2 = Math.sin(euler.z);
441 // double c3 = Math.cos(euler.x);
442 // double s3 = Math.sin(euler.x);
443 // quat.w = Math.sqrt(1.0 + c1 * c2 + c1*c3 - s1 * s2 * s3 + c2*c3) / 2.0;
444 // double w4 = (4.0 * quat.w);
445 // quat.x = (c2 * s3 + c1 * s3 + s1 * s2 * c3) / w4 ;
446 // quat.y = (s1 * c2 + s1 * c3 + c1 * s2 * s3) / w4 ;
447 // quat.z = (-s1 * s3 + c1 * s2 * c3 +s2) / w4 ;
453 public static void getEuler(Quat4d quat,Vector3d euler) {
454 Vector3d e = EulerTools.getEulerFromQuat(Order.YXZ, quat);
459 // http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Conversion_formulae_between_formalisms
460 // euler.x = Math.atan2(quat.x * quat.z + quat.y* quat.w, quat.y*quat.z - quat.x * quat.w);
461 // euler.y = Math.acos(-square(quat.x) - square(quat.y) + square(quat.z) + square(quat.w));
462 // euler.z = -Math.atan2(quat.x * quat.z - quat.y* quat.w, quat.y*quat.z + quat.x * quat.w);
464 // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
466 // double test = quat.x * quat.y + quat.z * quat.w;
467 // if (test > NEAR_HALF) {
468 // euler.y = 2.0 * Math.atan2(quat.x,quat.w);
469 // euler.z = Math.PI * 0.5;
471 // } else if (test < -NEAR_HALF) {
472 // euler.y = -2.0 * Math.atan2(quat.x,quat.w);
473 // euler.z = -Math.PI * 0.5;
476 // double sqx = square(quat.x);
477 // double sqy = square(quat.y);
478 // double sqz = square(quat.z);
479 // euler.y = Math.atan2(2.0*(quat.y*quat.w-quat.x*quat.z), 1.0 - 2.0*(sqy-sqz));
480 // euler.z = Math.asin(2.0*test);
481 // euler.x = Math.atan2(2.0*(quat.x*quat.w-quat.y*quat.z), 1.0 - 2.0*(sqx-sqz));
482 // System.out.println(euler + " " + EulerTools.getEulerFromQuat(Order.YXZ, quat) + " " + quat);
484 // double sqw = quat.w*quat.w;
485 // double sqx = quat.x*quat.x;
486 // double sqy = quat.y*quat.y;
487 // double sqz = quat.z*quat.z;
488 // double unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
489 // double test = quat.x*quat.y + quat.z*quat.w;
490 // if (test > 0.499*unit) { // singularity at north pole
491 // euler.y = 2 * Math.atan2(quat.x,quat.w);
492 // euler.z = Math.PI/2;
496 // if (test < -0.499*unit) { // singularity at south pole
497 // euler.y = -2 * Math.atan2(quat.x,quat.w);
498 // euler.z = -Math.PI/2;
502 // euler.y = Math.atan2(2*quat.y*quat.w-2*quat.x*quat.z , sqx - sqy - sqz + sqw);
503 // euler.z = Math.asin(2*test/unit);
504 // euler.x = Math.atan2(2*quat.x*quat.w-2*quat.y*quat.z , -sqx + sqy - sqz + sqw);
507 public static Quat4d getQuat(Vector3d euler) {
508 Quat4d q = new Quat4d();
514 public static Vector3d getEuler(Quat4d quat) {
515 Vector3d v = new Vector3d();
520 public static Quat4d getQuat(AxisAngle4d aa) {
521 Quat4d q = new Quat4d();
526 public static AxisAngle4d getAxisAngle(Quat4d q) {
527 AxisAngle4d aa = new AxisAngle4d();
528 double mag = q.x * q.x + q.y * q.y + q.z * q.z;
531 mag = Math.sqrt(mag);
532 aa.angle = 2.0 * Math.atan2(mag, q.w);
548 public static Quat4d getIdentityQuat() {
549 return new Quat4d(0, 0, 0, 1);
552 public static void getQuat(AxisAngle4d aa, Quat4d q) {
554 // Quat = cos(theta/2) + sin(theta/2)(roation_axis)
556 amag = Math.sqrt( aa.x*aa.x + aa.y*aa.y + aa.z*aa.z);
557 if( amag < NEAR_ZERO ) {
564 double a2 = aa.angle * 0.5;
578 private static final int IN = 0;
579 private static final int LEFT = 1;
580 private static final int RIGHT = 2;
581 private static final int BOTTOM = 4;
582 private static final int TOP = 8;
585 private static int bitcode(Vector2f p1, Vector2f min, Vector2f max) {
589 else if (p1.x > max.x)
593 else if (p1.y > max.y)
598 public static boolean clipLineRectangle(Vector2f p1,Vector2f p2, Vector2f min, Vector2f max, Vector2f r1, Vector2f r2) {
600 int o1 = bitcode(p1, min, max);
601 int o2 = bitcode(p2, min, max);
620 if ((o1 & TOP) != IN) {
621 float t = (max.y - p1.y) / (p2.y - p1.y);
622 p1.x += t * (p2.x - p1.x);
624 } else if ((o1 & BOTTOM) != IN) {
625 float t = (min.y - p1.y) / (p2.y - p1.y);
626 p1.x += t * (p2.x - p1.x);
628 } else if ((o1 & LEFT) != IN) {
629 float t = (min.x - p1.x) / (p2.x - p1.x);
630 p1.y += t * (p2.y - p1.y);
632 } else if ((o1 & RIGHT) != IN) {
633 float t = (max.x - p1.x) / (p2.x - p1.x);
634 p1.y += t * (p2.y - p1.y);
637 throw new RuntimeException("Error in clipping code");
643 public static double square(double d) {
648 public static void multiplyOrientation(AxisAngle4d aa, AxisAngle4d rot) {
649 Quat4d q1 = new Quat4d();
651 Quat4d q2 = new Quat4d();
657 public static double radToDeg(double rad) {
658 return (rad / Math.PI) * 180.0;
661 public static double degToRad(double deg) {
662 return (deg / 180.0) * Math.PI;
665 public static double clamp(double min, double max,double v) {
673 public static AxisAngle4d createRotation(Vector3d original, Vector3d rotated) {
674 AxisAngle4d result = new AxisAngle4d();
675 if (createRotation(original, rotated, result))
681 public static void setIdentity(Quat4d q) {
688 public static void setIdentity(AxisAngle4d aa) {
695 public static void set(Matrix3d mat, double m00, double m01, double m02,
696 double m10, double m11, double m12, double m20, double m21,
711 public static void set(Matrix4d mat, double[] v) {
734 public static boolean createRotation(Vector3d original, Vector3d rotated, AxisAngle4d result) {
736 if (rotated.lengthSquared() > 0.01)
740 double d = original.dot(rotated);
742 // original and rotated are parallel, pointing at the same direction
747 } else if (d < -0.9999) {
748 // original and rotated are parallel, pointing at the opposite direction
750 if (Math.abs(a.dot(original)) > 0.8 )
752 result.set(a, Math.PI);
754 double angle = original.angle(rotated);
755 Vector3d axis = new Vector3d();
756 axis.cross(original, rotated);
757 result.set(axis,angle);
762 public static boolean createRotation(Vector3d original, Vector3d rotated, Quat4d result) {
764 if (rotated.lengthSquared() > 0.01)
768 double d = original.dot(rotated);
770 // original and rotated are parallel, pointing at the same direction
775 } else if (d < -0.9999) {
776 // original and rotated are parallel, pointing at the opposite direction
778 if (Math.abs(a.dot(original)) > 0.8 )
780 getQuat(a, Math.PI, result);
783 double angle = original.angle(rotated);
784 Vector3d axis = new Vector3d();
785 axis.cross(original, rotated);
786 getQuat(axis, angle, result);
791 public static void getQuat(Vector3d axis, double angle, Quat4d q)
794 // Quat = cos(theta/2) + sin(theta/2)(roation_axis)
796 amag = Math.sqrt( axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
804 double a2 = angle*0.5;
807 q.x = axis.x*amag*mag;
808 q.y = axis.y*amag*mag;
809 q.z = axis.z*amag*mag;
815 * Linear interpolation of quaternions. Result IS set to q1.
820 public static void lip(Quat4d q1, Quat4d q2, double alpha) {
821 double s1 = 1.0 - alpha;
828 public static double dot(Quat4d q1, Quat4d q2) {
829 return q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;
832 public static void mad(Tuple3d q1, Tuple3d q2, double s2) {
838 public static void mad(Quat4d q1, Quat4d q2, double s2) {
848 * Sets results to q1. Modifies q2.
854 public static void sip(Quat4d q1, Quat4d q2, double alpha) {
855 double cosom = dot(q1,q2);
861 if (cosom > 0.9999) {
868 double theta_0 = Math.acos(cosom);
869 double theta = theta_0 * alpha;
870 Quat4d t = new Quat4d(q1);
874 t.scale(Math.sin(theta));
875 q1.scale(Math.cos(theta));
880 public static void rotate(double angle, Tuple2d v1, Tuple2d v2) {
881 // TODO : verify implementation
882 double sin = Math.sin(angle);
886 } else if (sin == -1.0) {
890 double cos = Math.cos(angle);
894 } else if (cos != 1.0) {
895 v2.x= v1.x * cos + v1.y * -sin;
896 v2.y= v1.x* sin + v1.y *cos;
901 public static Tuple3d getPosRot(double m3x2[]) {
902 Vector3d t = new Vector3d();
907 Vector2d v2 = new Vector2d(1,0);
908 Vector2d v = new Vector2d();
909 // use rotation of (1,0) to calculate the rotation component
912 double a1 = v2.angle(v);
916 t.z = Math.PI*2.0 - a1;