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.000000001;
37 public static double NEAR_HALF = 0.499999999;
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 Vector3d projectToPlane(Vector3d v, Vector3d planeNormal) {
152 //planeNormal.normalize();
153 Vector3d t = new Vector3d();
154 t.cross(v,planeNormal);
155 t.cross(planeNormal, t);
160 public static boolean intersectStraightPlane(Tuple3d linePoint, Vector3d lineDir, Tuple3d planePoint, Vector3d planeNormal, Tuple3d intersectPoint) {
161 intersectPoint.set(planePoint);
162 intersectPoint.sub(linePoint);
163 double u = planeNormal.dot(new Vector3d(intersectPoint));
164 double v = planeNormal.dot(lineDir);
165 if (Math.abs(v) < NEAR_ZERO)
168 intersectPoint.set(lineDir);
169 intersectPoint.scale(u);
170 intersectPoint.add(linePoint);
174 public static boolean intersectStraightPlane(Tuple3d linePoint, Vector3d lineDir, Tuple3d planePoint, Vector3d planeNormal, Vector3d intersectPoint, double[] u) {
175 intersectPoint.set(planePoint);
176 intersectPoint.sub(linePoint);
177 u[0] = planeNormal.dot(intersectPoint);
178 double v = planeNormal.dot(lineDir);
179 if (Math.abs(v) < NEAR_ZERO)
182 intersectPoint.set(lineDir);
183 intersectPoint.scale(u[0]);
184 intersectPoint.add(linePoint);
188 public static boolean intersectLineLine(Tuple3d l1_start,Tuple3d l1_end,Tuple3d l2_start,Tuple3d l2_end,Tuple3d l1_pos, Tuple3d l2_pos) {
189 Vector3d p13 = new Vector3d();
190 Vector3d p43 = new Vector3d();
191 Vector3d p21 = new Vector3d();
192 double d1343,d4321,d1321,d4343,d2121;
194 p13.sub(l1_start, l2_start);
195 p43.sub(l2_end,l2_start);
196 if (Math.abs(p43.x) < NEAR_ZERO && Math.abs(p43.y) < NEAR_ZERO && Math.abs(p43.z) < NEAR_ZERO)
198 p21.sub(l1_end,l1_start);
199 if (Math.abs(p21.x) < NEAR_ZERO && Math.abs(p21.y) < NEAR_ZERO && Math.abs(p21.z) < NEAR_ZERO)
202 d1343 = p13.dot(p43);
203 d4321 = p43.dot(p21);
204 d1321 = p13.dot(p21);
205 d4343 = p43.lengthSquared();
206 d2121 = p21.lengthSquared();
208 denom = d2121 * d4343 - d4321 * d4321;
209 if (Math.abs(denom) < NEAR_ZERO)
211 numer = d1343 * d4321 - d1321 * d4343;
213 double mua = numer / denom;
214 double mub = (d1343 + d4321 * mua) / d4343;
216 l1_pos.x = l1_start.x + mua * p21.x;
217 l1_pos.y = l1_start.y + mua * p21.y;
218 l1_pos.z = l1_start.z + mua * p21.z;
219 l2_pos.x = l2_start.x + mub * p43.x;
220 l2_pos.y = l2_start.y + mub * p43.y;
221 l2_pos.z = l2_start.z + mub * p43.z;
226 public static boolean intersectStraightStraight(Tuple3d p1,Vector3d p21,Tuple3d p3,Vector3d p43,Tuple3d pa,Tuple3d pb) {
227 Vector3d p13 = new Vector3d();
229 double d1343,d4321,d1321,d4343,d2121;
233 if (Math.abs(p43.x) < NEAR_ZERO && Math.abs(p43.y) < NEAR_ZERO && Math.abs(p43.z) < NEAR_ZERO)
235 if (Math.abs(p21.x) < NEAR_ZERO && Math.abs(p21.y) < NEAR_ZERO && Math.abs(p21.z) < NEAR_ZERO)
238 d1343 = p13.dot(p43);
239 d4321 = p43.dot(p21);
240 d1321 = p13.dot(p21);
241 d4343 = p43.lengthSquared();
242 d2121 = p21.lengthSquared();
244 denom = d2121 * d4343 - d4321 * d4321;
245 if (Math.abs(denom) < NEAR_ZERO)
247 numer = d1343 * d4321 - d1321 * d4343;
249 double mua = numer / denom;
250 double mub = (d1343 + d4321 * mua) / d4343;
252 pa.x = p1.x + mua * p21.x;
253 pa.y = p1.y + mua * p21.y;
254 pa.z = p1.z + mua * p21.z;
255 pb.x = p3.x + mub * p43.x;
256 pb.y = p3.y + mub * p43.y;
257 pb.z = p3.z + mub * p43.z;
263 * Calculate the line segment PaPb that is the shortest route between
264 * two lines P1P2 and P3P4. Calculate also the values of mua and mub where
265 * Pa = P1 + mua (P2 - P1)
266 * Pb = P3 + mub (P4 - P3)
276 public static boolean intersectStraightStraight(Tuple3d p1,Vector3d p21,Tuple3d p3,Vector3d p43,Tuple3d pa,Tuple3d pb, double mu[]) {
277 Vector3d p13 = new Vector3d();
279 double d1343,d4321,d1321,d4343,d2121;
283 if (Math.abs(p43.x) < EPS && Math.abs(p43.y) < EPS && Math.abs(p43.z) < EPS)
285 if (Math.abs(p21.x) < EPS && Math.abs(p21.y) < EPS && Math.abs(p21.z) < EPS)
288 d1343 = p13.dot(p43);
289 d4321 = p43.dot(p21);
290 d1321 = p13.dot(p21);
291 d4343 = p43.lengthSquared();
292 d2121 = p21.lengthSquared();
294 denom = d2121 * d4343 - d4321 * d4321;
295 if (Math.abs(denom) < EPS)
297 numer = d1343 * d4321 - d1321 * d4343;
299 mu[0] = numer / denom;
300 mu[1] = (d1343 + d4321 * mu[0]) / d4343;
302 pa.x = p1.x + mu[0] * p21.x;
303 pa.y = p1.y + mu[0] * p21.y;
304 pa.z = p1.z + mu[0] * p21.z;
305 pb.x = p3.x + mu[1] * p43.x;
306 pb.y = p3.y + mu[1] * p43.y;
307 pb.z = p3.z + mu[1] * p43.z;
314 public static void rotate(Quat4d q, Tuple3d in, Tuple3d out) {
316 double tw = - q.x*in.x - q.y*in.y - q.z*in.z;
317 double tx = q.w*in.x + q.y*in.z - q.z*in.y;
318 double ty = q.w*in.y - q.x*in.z + q.z*in.x;
319 double tz = q.w*in.z + q.x*in.y - q.y*in.x ;
321 //temp * q' -> x = -x, y = -y z = -z
322 //out.w = tw*q.w + tx*q.x + ty*q.y + tz*q.z;
323 out.x = -tw*q.x + tx*q.w - ty*q.z + tz*q.y;
324 out.y = -tw*q.y + tx*q.z + ty*q.w - tz*q.x;
325 out.z = -tw*q.z - tx*q.y + ty*q.x + tz*q.w;
328 public static void getMatrix(Quat4d quat, Matrix3d m) {
329 m.m00 = 1.0f - 2.0 * (quat.y * quat.y + quat.z * quat.z);
330 m.m01 = 2.0 * (quat.x * quat.y + quat.w * quat.z);
331 m.m02 = 2.0 * (quat.x * quat.z - quat.w * quat.y);
332 m.m10 = 2.0 * (quat.x * quat.y - quat.w * quat.z);
333 m.m11 = 1.0 - 2.0f * (quat.x * quat.x + quat.z * quat.z);
334 m.m12 = 2.0 * (quat.y * quat.z + quat.w * quat.x);
335 m.m20 = 2.0 * (quat.x * quat.z + quat.w * quat.y);
336 m.m21 = 2.0 * (quat.y * quat.z - quat.w * quat.x);
337 m.m22 = 1.0 - 2.0f * (quat.x * quat.x + quat.y * quat.y);
341 public static void getMatrix(Quat4d quat, Matrix4d m) {
343 m.m00 = 1.0f - 2.0 * (quat.y * quat.y + quat.z * quat.z);
344 m.m01 = 2.0 * (quat.x * quat.y + quat.w * quat.z);
345 m.m02 = 2.0 * (quat.x * quat.z - quat.w * quat.y);
346 m.m10 = 2.0 * (quat.x * quat.y - quat.w * quat.z);
347 m.m11 = 1.0 - 2.0f * (quat.x * quat.x + quat.z * quat.z);
348 m.m12 = 2.0 * (quat.y * quat.z + quat.w * quat.x);
349 m.m20 = 2.0 * (quat.x * quat.z + quat.w * quat.y);
350 m.m21 = 2.0 * (quat.y * quat.z - quat.w * quat.x);
351 m.m22 = 1.0 - 2.0f * (quat.x * quat.x + quat.y * quat.y);
355 private static double q[] = new double[3];
356 private static int nxt[] = { 1, 2, 0 };
358 * Converts Matrix to Quaternion
360 * Note: non-thread safe.
365 public static void getQuat(Matrix3d mat, Quat4d quat) {
366 double tr = mat.m00 + mat.m11 + mat.m22;
368 double s = Math.sqrt(tr + 1.0);
371 quat.x = (mat.m21 - mat.m12) * s;
372 quat.y = (mat.m02 - mat.m20) * s;
373 quat.z = (mat.m10 - mat.m01) * s;
376 if (mat.m11 > mat.m00)
378 if (mat.m22 > mat.getElement(i, i))
385 double s = Math.sqrt((mat.getElement(i, i) - (mat.getElement(j, j) + mat.getElement(k, k))) + 1.0);
389 if (Math.abs(s) > 0.001)
392 quat.w = (mat.getElement(k, j) - mat.getElement(j, k)) * s;
393 q[j] = (mat.getElement(j, i) + mat.getElement(i, j)) * s;
394 q[k] = (mat.getElement(k, i) + mat.getElement(i, k)) * s;
402 public static Quat4d getQuat(Matrix3d mat) {
403 Quat4d q = new Quat4d();
408 public static AxisAngle4d getFromPseudoEuler(Vector3d euler) {
409 AxisAngle4d aa = new AxisAngle4d();
410 aa.angle = euler.length();
411 Vector3d normal = new Vector3d(euler);
412 if (aa.angle > NEAR_ZERO) {
426 public static Vector3d getPseudoEuler(AxisAngle4d aa) {
427 Vector3d euler = new Vector3d(aa.x,aa.y,aa.z);
428 euler.scale(aa.angle);
433 public static void getQuat(Vector3d euler, Quat4d quat) {
434 Quat4d q = EulerTools.getQuatFromEuler(Order.YXZ, euler.y,euler.x,euler.z);
436 // http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Conversion_formulae_between_formalisms
437 // Using the x-convention, the 3-1-3 Euler angles phi, theta and psi (around the Z, X and again the Z-axis)
438 // quat.x = -Math.cos((euler.x - euler.z)*0.5)*Math.sin(euler.y*0.5);
439 // quat.y = -Math.sin((euler.x - euler.z)*0.5)*Math.sin(euler.y*0.5);
440 // quat.z = -Math.sin((euler.x + euler.z)*0.5)*Math.cos(euler.y*0.5);
441 // quat.w = Math.sin((euler.x + euler.z)*0.5)*Math.cos(euler.y*0.5);
443 // http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm
445 // double c1 = Math.cos(euler.y*0.5);
446 // double s1 = Math.sin(euler.y*0.5);
447 // double c2 = Math.cos(euler.z*0.5);
448 // double s2 = Math.sin(euler.z*0.5);
449 // double c3 = Math.cos(euler.x*0.5);
450 // double s3 = Math.sin(euler.x*0.5);
451 // double c1c2 = c1*c2;
452 // double s1s2 = s1*s2;
453 // quat.w =c1c2*c3 - s1s2*s3;
454 // quat.x =c1c2*s3 + s1s2*c3;
455 // quat.y =s1*c2*c3 + c1*s2*s3;
456 // quat.z =c1*s2*c3 - s1*c2*s3;
458 // Quat4d q2 = EulerTools.getQuatFromEuler(Order.YZX, euler.y,euler.z,euler.x);
459 // System.out.println("Q " + quat + " Q2 " + q2);
460 // double c1 = Math.cos(euler.y);
461 // double s1 = Math.sin(euler.y);
462 // double c2 = Math.cos(euler.z);
463 // double s2 = Math.sin(euler.z);
464 // double c3 = Math.cos(euler.x);
465 // double s3 = Math.sin(euler.x);
466 // quat.w = Math.sqrt(1.0 + c1 * c2 + c1*c3 - s1 * s2 * s3 + c2*c3) / 2.0;
467 // double w4 = (4.0 * quat.w);
468 // quat.x = (c2 * s3 + c1 * s3 + s1 * s2 * c3) / w4 ;
469 // quat.y = (s1 * c2 + s1 * c3 + c1 * s2 * s3) / w4 ;
470 // quat.z = (-s1 * s3 + c1 * s2 * c3 +s2) / w4 ;
476 public static void getEuler(Quat4d quat,Vector3d euler) {
477 Vector3d e = EulerTools.getEulerFromQuat(Order.YXZ, quat);
482 // http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Conversion_formulae_between_formalisms
483 // euler.x = Math.atan2(quat.x * quat.z + quat.y* quat.w, quat.y*quat.z - quat.x * quat.w);
484 // euler.y = Math.acos(-square(quat.x) - square(quat.y) + square(quat.z) + square(quat.w));
485 // euler.z = -Math.atan2(quat.x * quat.z - quat.y* quat.w, quat.y*quat.z + quat.x * quat.w);
487 // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
489 // double test = quat.x * quat.y + quat.z * quat.w;
490 // if (test > NEAR_HALF) {
491 // euler.y = 2.0 * Math.atan2(quat.x,quat.w);
492 // euler.z = Math.PI * 0.5;
494 // } else if (test < -NEAR_HALF) {
495 // euler.y = -2.0 * Math.atan2(quat.x,quat.w);
496 // euler.z = -Math.PI * 0.5;
499 // double sqx = square(quat.x);
500 // double sqy = square(quat.y);
501 // double sqz = square(quat.z);
502 // euler.y = Math.atan2(2.0*(quat.y*quat.w-quat.x*quat.z), 1.0 - 2.0*(sqy-sqz));
503 // euler.z = Math.asin(2.0*test);
504 // euler.x = Math.atan2(2.0*(quat.x*quat.w-quat.y*quat.z), 1.0 - 2.0*(sqx-sqz));
505 // System.out.println(euler + " " + EulerTools.getEulerFromQuat(Order.YXZ, quat) + " " + quat);
507 // double sqw = quat.w*quat.w;
508 // double sqx = quat.x*quat.x;
509 // double sqy = quat.y*quat.y;
510 // double sqz = quat.z*quat.z;
511 // double unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
512 // double test = quat.x*quat.y + quat.z*quat.w;
513 // if (test > 0.499*unit) { // singularity at north pole
514 // euler.y = 2 * Math.atan2(quat.x,quat.w);
515 // euler.z = Math.PI/2;
519 // if (test < -0.499*unit) { // singularity at south pole
520 // euler.y = -2 * Math.atan2(quat.x,quat.w);
521 // euler.z = -Math.PI/2;
525 // euler.y = Math.atan2(2*quat.y*quat.w-2*quat.x*quat.z , sqx - sqy - sqz + sqw);
526 // euler.z = Math.asin(2*test/unit);
527 // euler.x = Math.atan2(2*quat.x*quat.w-2*quat.y*quat.z , -sqx + sqy - sqz + sqw);
530 public static Quat4d getQuat(Vector3d euler) {
531 Quat4d q = new Quat4d();
537 public static Vector3d getEuler(Quat4d quat) {
538 Vector3d v = new Vector3d();
543 public static Quat4d getQuat(AxisAngle4d aa) {
544 Quat4d q = new Quat4d();
549 public static AxisAngle4d getAxisAngle(Quat4d q) {
550 AxisAngle4d aa = new AxisAngle4d();
551 double mag = q.x * q.x + q.y * q.y + q.z * q.z;
554 mag = Math.sqrt(mag);
555 aa.angle = 2.0 * Math.atan2(mag, q.w);
571 public static Quat4d getIdentityQuat() {
572 return new Quat4d(0, 0, 0, 1);
575 public static void getQuat(AxisAngle4d aa, Quat4d q) {
577 // Quat = cos(theta/2) + sin(theta/2)(roation_axis)
579 amag = Math.sqrt( aa.x*aa.x + aa.y*aa.y + aa.z*aa.z);
580 if( amag < NEAR_ZERO ) {
587 double a2 = aa.angle * 0.5;
601 private static final int IN = 0;
602 private static final int LEFT = 1;
603 private static final int RIGHT = 2;
604 private static final int BOTTOM = 4;
605 private static final int TOP = 8;
608 private static int bitcode(Vector2f p1, Vector2f min, Vector2f max) {
612 else if (p1.x > max.x)
616 else if (p1.y > max.y)
621 public static boolean clipLineRectangle(Vector2f p1,Vector2f p2, Vector2f min, Vector2f max, Vector2f r1, Vector2f r2) {
623 int o1 = bitcode(p1, min, max);
624 int o2 = bitcode(p2, min, max);
643 if ((o1 & TOP) != IN) {
644 float t = (max.y - p1.y) / (p2.y - p1.y);
645 p1.x += t * (p2.x - p1.x);
647 } else if ((o1 & BOTTOM) != IN) {
648 float t = (min.y - p1.y) / (p2.y - p1.y);
649 p1.x += t * (p2.x - p1.x);
651 } else if ((o1 & LEFT) != IN) {
652 float t = (min.x - p1.x) / (p2.x - p1.x);
653 p1.y += t * (p2.y - p1.y);
655 } else if ((o1 & RIGHT) != IN) {
656 float t = (max.x - p1.x) / (p2.x - p1.x);
657 p1.y += t * (p2.y - p1.y);
660 throw new RuntimeException("Error in clipping code");
666 public static double square(double d) {
671 public static void multiplyOrientation(AxisAngle4d aa, AxisAngle4d rot) {
672 Quat4d q1 = new Quat4d();
674 Quat4d q2 = new Quat4d();
680 public static double radToDeg(double rad) {
681 return (rad / Math.PI) * 180.0;
684 public static double degToRad(double deg) {
685 return (deg / 180.0) * Math.PI;
688 public static double clamp(double min, double max,double v) {
696 public static AxisAngle4d createRotation(Vector3d original, Vector3d rotated) {
697 AxisAngle4d result = new AxisAngle4d();
698 if (createRotation(original, rotated, result))
704 public static void setIdentity(Quat4d q) {
711 public static void setIdentity(AxisAngle4d aa) {
718 public static void set(Matrix3d mat, double m00, double m01, double m02,
719 double m10, double m11, double m12, double m20, double m21,
734 public static void set(Matrix4d mat, double[] v) {
757 public static boolean createRotation(Vector3d original, Vector3d rotated, AxisAngle4d result) {
759 if (rotated.lengthSquared() > 0.01)
763 double d = original.dot(rotated);
765 // original and rotated are parallel, pointing at the same direction
770 } else if (d < -0.9999) {
771 // original and rotated are parallel, pointing at the opposite direction
773 if (Math.abs(a.dot(original)) > 0.8 )
775 result.set(a, Math.PI);
777 double angle = original.angle(rotated);
778 Vector3d axis = new Vector3d();
779 axis.cross(original, rotated);
780 result.set(axis,angle);
785 public static boolean createRotation(Vector3d original, Vector3d rotated, Quat4d result) {
787 if (rotated.lengthSquared() > 0.01)
791 double d = original.dot(rotated);
793 // original and rotated are parallel, pointing at the same direction
798 } else if (d < -0.9999) {
799 // original and rotated are parallel, pointing at the opposite direction
801 if (Math.abs(a.dot(original)) > 0.8 )
803 getQuat(a, Math.PI, result);
806 double angle = original.angle(rotated);
807 Vector3d axis = new Vector3d();
808 axis.cross(original, rotated);
809 getQuat(axis, angle, result);
814 public static boolean createRotation(Vector3d original, Vector3d rotated, Vector3d axis, AxisAngle4d result) {
816 if (rotated.lengthSquared() > 0.01)
820 if (original.lengthSquared() > 0.01)
821 original.normalize();
824 if (axis.lengthSquared() > 0.01)
828 double d = original.dot(rotated);
830 // original and rotated are parallel, pointing at the same direction
835 } else if (d < -0.9999) {
836 // original and rotated are parallel, pointing at the opposite direction
837 result.angle = Math.PI;
842 // Project vectors to Axis plane
843 Vector3d p1 = projectToPlane(original, axis);
844 Vector3d p2 = projectToPlane(rotated, axis);
845 // Create vectors where z-axis is plane normal
846 Quat4d q = getQuat(createRotation(axis, Z_AXIS));
847 Vector3d t1 = new Vector3d();
848 Vector3d t2 = new Vector3d();
851 // Calculate angles on z-axis plane.
852 double a1 = Math.atan2(t1.y, t1.x);
853 double a2 = Math.atan2(t2.y, t2.x);
854 result.set(axis,a2-a1);
860 public static void getQuat(Vector3d axis, double angle, Quat4d q)
863 // Quat = cos(theta/2) + sin(theta/2)(roation_axis)
865 amag = Math.sqrt( axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
873 double a2 = angle*0.5;
876 q.x = axis.x*amag*mag;
877 q.y = axis.y*amag*mag;
878 q.z = axis.z*amag*mag;
884 * Linear interpolation of quaternions. Result IS set to q1.
889 public static void lip(Quat4d q1, Quat4d q2, double alpha) {
890 double s1 = 1.0 - alpha;
897 public static double dot(Quat4d q1, Quat4d q2) {
898 return q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;
901 public static void mad(Tuple3d q1, Tuple3d q2, double s2) {
907 public static void mad(Quat4d q1, Quat4d q2, double s2) {
917 * Sets results to q1. Modifies q2.
923 public static void sip(Quat4d q1, Quat4d q2, double alpha) {
924 double cosom = dot(q1,q2);
930 if (cosom > 0.9999) {
937 double theta_0 = Math.acos(cosom);
938 double theta = theta_0 * alpha;
939 Quat4d t = new Quat4d(q1);
943 t.scale(Math.sin(theta));
944 q1.scale(Math.cos(theta));
949 public static void rotate(double angle, Tuple2d v1, Tuple2d v2) {
950 // TODO : verify implementation
951 double sin = Math.sin(angle);
955 } else if (sin == -1.0) {
959 double cos = Math.cos(angle);
963 } else if (cos != 1.0) {
964 v2.x= v1.x * cos + v1.y * -sin;
965 v2.y= v1.x* sin + v1.y *cos;
970 public static Tuple3d getPosRot(double m3x2[]) {
971 Vector3d t = new Vector3d();
976 Vector2d v2 = new Vector2d(1,0);
977 Vector2d v = new Vector2d();
978 // use rotation of (1,0) to calculate the rotation component
981 double a1 = v2.angle(v);
985 t.z = Math.PI*2.0 - a1;
990 public static Matrix4d glFrustum(double l, double r, double b, double t, double n, double f) {
991 Matrix4d mat = new Matrix4d();
992 mat.m00 = 2.0 * n / (r - l);
993 mat.m11 = 2.0 * n / (t - b);
994 mat.m02 = (r+l) / (r-l);
995 mat.m12 = (t+b) / (t-b);
996 mat.m22 = -(f+n) / (f-n);
997 mat.m23 = -(2.0 *f * n) / (f-n);
1002 public static Matrix4d glOrtho(double l, double r, double b, double t, double n, double f) {
1003 Matrix4d mat = new Matrix4d();
1004 mat.m00 = 2.0 / (r - l);
1005 mat.m11 = 2.0 / (t - b);
1006 mat.m22 = -2.0 / (f-n);
1008 mat.m03 = -(r+l)/(r-l);
1009 mat.m13 = -(t+b)/(t-b);
1010 mat.m23 = -(f+n)/(f-n);