]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java
Converting variable angle turn turn to fixed angle could fail.
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / math / MathTools.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g3d.math;
13
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;
24
25 import org.simantics.g3d.math.EulerTools.Order;
26
27
28 /**
29  * Some useful geometry related math functions. Beware, methods may modify their input parameters!
30  * 
31  * @author Marko Luukkainen
32  *
33  */
34 public class MathTools {
35         
36         public static double NEAR_ZERO = 0.000000001;
37         public static double NEAR_HALF = 0.499999999;
38         
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);
43         
44         final static double EPS = 1.0e-12;
45         
46         
47         public static boolean equals(double d1, double d2) {
48                 return Math.abs(d1-d2) < EPS;
49         }
50         
51         public static boolean equals(Tuple3d p1, Tuple3d p2) {
52                 return distanceSquared(p1, p2) < NEAR_ZERO;
53         }
54         
55         public static boolean equals(Tuple4d p1, Tuple4d p2) {
56                 return distanceSquared(p1, p2) < NEAR_ZERO;
57         }
58         
59         public static double distance(Tuple3d p1, Tuple3d p2) {
60                 double dx, dy, dz;
61
62                 dx = p2.x - p1.x;
63                 dy = p2.y - p1.y;
64                 dz = p2.z - p1.z;
65                 return Math.sqrt(dx*dx+dy*dy+dz*dz);
66         }
67         
68         public static double distance(Tuple4d p1, Tuple4d p2) {
69                 double dx, dy, dz, dw;
70
71                 dx = p2.x - p1.x;
72                 dy = p2.y - p1.y;
73                 dz = p2.z - p1.z;
74                 dw = p2.w - p1.w;
75                 return Math.sqrt(dx*dx+dy*dy+dz*dz+dw*dw);
76         }
77         
78         public static double distanceSquared(Tuple3d p1, Tuple3d p2) {
79                 double dx, dy, dz;
80
81                 dx = p2.x - p1.x;
82                 dy = p2.y - p1.y;
83                 dz = p2.z - p1.z;
84                 return dx*dx+dy*dy+dz*dz;
85         }
86         
87         public static double distanceSquared(Tuple4d p1, Tuple4d p2) {
88                 double dx, dy, dz, dw;
89
90                 dx = p2.x - p1.x;
91                 dy = p2.y - p1.y;
92                 dz = p2.z - p1.z;
93                 dw = p2.w - p1.w;
94                 return dx*dx+dy*dy+dz*dz+dw*dw;
95         }
96         
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));
101         }
102         
103         public static Vector3d closestPointOnEdge(Vector3d point, Vector3d edgePoint1, Vector3d edgePoint2) {
104                 point.sub(edgePoint1);
105                 Vector3d v = new Vector3d(edgePoint2);
106                 v.sub(edgePoint1);
107                 double t = v.dot(point);
108                 t /= v.lengthSquared();
109                 if (t <= 0.0f)
110                         return edgePoint1;
111                 if (t >= 1.0f)
112                         return edgePoint2;
113                 v.scale(t);
114                 v.add(edgePoint1);
115                 return v;   
116         }
117         
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();
123                 v.set(straightDir);
124                 v.scale(t);
125                 v.add(straightPoint);
126                 return v;   
127         }
128         
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();
134                 v.set(straightDir);
135                 v.scale(u[0]);
136                 v.add(straightPoint);
137                 return v;   
138         }
139         
140         public static double distanceFromPlane(Vector3d point, Vector3d planeNormal, Tuple3d planePoint) {
141                 point.sub(planePoint);
142                 
143                 return planeNormal.dot(point);
144         }
145         
146         public static double distanceFromPlane(Vector3d point, Vector3d planeNormal, float d) {
147                 return (planeNormal.dot(point) + d);
148         }
149         
150         public static Vector3d projectToPlane(Vector3d v, Vector3d planeNormal) {
151             //v.normalize();
152             //planeNormal.normalize();
153             Vector3d t = new Vector3d();
154             t.cross(v,planeNormal);
155             t.cross(planeNormal, t);
156             return t;
157             
158         }
159         
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)
166                         return false;
167                 u /= v;
168                 intersectPoint.set(lineDir);
169                 intersectPoint.scale(u);
170                 intersectPoint.add(linePoint);
171                 return true;
172         }
173         
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)
180                         return false;
181                 u[0] /= v;
182                 intersectPoint.set(lineDir);
183                 intersectPoint.scale(u[0]);
184                 intersectPoint.add(linePoint);
185                 return true;
186         }
187         
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;
193                         double numer,denom;
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)
197                                 return false;
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)
200                                 return false;
201
202                         d1343 = p13.dot(p43);
203                         d4321 = p43.dot(p21);
204                         d1321 = p13.dot(p21);
205                         d4343 = p43.lengthSquared();
206                         d2121 = p21.lengthSquared();
207
208                         denom = d2121 * d4343 - d4321 * d4321;
209                         if (Math.abs(denom) < NEAR_ZERO)
210                                 return false;
211                         numer = d1343 * d4321 - d1321 * d4343;
212
213                         double mua = numer / denom;
214                         double mub = (d1343 + d4321 * mua) / d4343;
215
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;
222
223                         return true;
224         }
225         
226         public static boolean intersectStraightStraight(Tuple3d p1,Vector3d p21,Tuple3d p3,Vector3d p43,Tuple3d pa,Tuple3d pb) {
227                 Vector3d p13 = new Vector3d();
228
229                 double d1343,d4321,d1321,d4343,d2121;
230                 double numer,denom;
231                 
232                 p13.sub(p1, p3);
233                 if (Math.abs(p43.x)  < NEAR_ZERO && Math.abs(p43.y)  < NEAR_ZERO && Math.abs(p43.z)  < NEAR_ZERO)
234                         return false;
235                 if (Math.abs(p21.x)  < NEAR_ZERO && Math.abs(p21.y)  < NEAR_ZERO && Math.abs(p21.z)  < NEAR_ZERO)
236                         return false;
237
238                 d1343 = p13.dot(p43);
239                 d4321 = p43.dot(p21);
240                 d1321 = p13.dot(p21);
241                 d4343 = p43.lengthSquared();
242                 d2121 = p21.lengthSquared();
243
244                 denom = d2121 * d4343 - d4321 * d4321;
245                 if (Math.abs(denom) < NEAR_ZERO)
246                         return false;
247                 numer = d1343 * d4321 - d1321 * d4343;
248
249                 double mua = numer / denom;
250                 double mub = (d1343 + d4321 * mua) / d4343;
251
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;
258
259                 return true;
260         }
261         
262         /**
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)
267          * @param p1
268          * @param p21
269          * @param p3
270          * @param p43
271          * @param pa
272          * @param pb
273          * @param mu
274          * @return
275          */
276         public static boolean intersectStraightStraight(Tuple3d p1,Vector3d p21,Tuple3d p3,Vector3d p43,Tuple3d pa,Tuple3d pb, double mu[]) {
277                 Vector3d p13 = new Vector3d();
278
279                 double d1343,d4321,d1321,d4343,d2121;
280                 double numer,denom;
281                 double EPS = 0.001;
282                 p13.sub(p1, p3);
283                 if (Math.abs(p43.x)  < EPS && Math.abs(p43.y)  < EPS && Math.abs(p43.z)  < EPS)
284                         return false;
285                 if (Math.abs(p21.x)  < EPS && Math.abs(p21.y)  < EPS && Math.abs(p21.z)  < EPS)
286                         return false;
287
288                 d1343 = p13.dot(p43);
289                 d4321 = p43.dot(p21);
290                 d1321 = p13.dot(p21);
291                 d4343 = p43.lengthSquared();
292                 d2121 = p21.lengthSquared();
293
294                 denom = d2121 * d4343 - d4321 * d4321;
295                 if (Math.abs(denom) < EPS)
296                         return false;
297                 numer = d1343 * d4321 - d1321 * d4343;
298
299                 mu[0] = numer / denom;
300                 mu[1] = (d1343 + d4321 * mu[0]) / d4343;
301
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;
308
309                 return true;
310         }
311         
312
313         
314         public static void rotate(Quat4d q, Tuple3d in, Tuple3d out) {
315                 // p' = q * p * q'
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           ;
320                 
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;  
326         }
327         
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);
338
339         }
340         
341         public static void getMatrix(Quat4d quat, Matrix4d m) {
342                 m.setZero();
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);
352                 m.m33 = 1.0;
353         }
354         
355         private static double q[] = new double[3];
356         private static int nxt[] = { 1, 2, 0 };
357         /**
358          * Converts Matrix to Quaternion
359          * 
360          * Note: non-thread safe.
361          * 
362          * @param mat
363          * @param quat
364          */
365         public static void getQuat(Matrix3d mat, Quat4d quat) {
366                 double tr = mat.m00 + mat.m11 + mat.m22;
367                 if (tr > 0.0) {
368                         double s = Math.sqrt(tr + 1.0);
369                         quat.w = 0.5 * s;
370                         s = 0.5 / s;
371                         quat.x = (mat.m21 - mat.m12) * s;
372                         quat.y = (mat.m02 - mat.m20) * s;
373                         quat.z = (mat.m10 - mat.m01) * s;
374                 } else {
375                         int i = 0, j, k;
376                         if (mat.m11 > mat.m00)
377                                 i = 1;
378                         if (mat.m22 > mat.getElement(i, i))
379                                 i = 2;
380                         
381
382                         j = nxt[i];
383                         k = nxt[j];
384
385                         double s = Math.sqrt((mat.getElement(i, i) - (mat.getElement(j, j) + mat.getElement(k, k))) + 1.0);
386
387                         q[i] = s * 0.5;
388
389                         if (Math.abs(s) > 0.001)
390                                 s = 0.5 / s;
391
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;
395
396                         quat.x = q[0];
397                         quat.y = q[1];
398                         quat.z = q[2];
399                 }
400         }
401         
402         public static Quat4d getQuat(Matrix3d mat) {
403                 Quat4d q = new Quat4d();
404                 getQuat(mat, q);
405                 return q;
406         }
407         
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) {
413                         normal.normalize();
414                         aa.x = normal.x;
415                         aa.y = normal.y;
416                         aa.z = normal.z;
417                 } else {
418                         aa.x = 1.0;
419                         aa.y = 0.0;
420                         aa.z = 0.0;
421                 }
422                 
423                 return aa;
424         }
425         
426         public static Vector3d getPseudoEuler(AxisAngle4d aa) {
427                 Vector3d euler = new Vector3d(aa.x,aa.y,aa.z);
428                 euler.scale(aa.angle);
429                 return euler;
430         }
431         
432         
433         public static void getQuat(Vector3d euler, Quat4d quat)  {
434                 Quat4d q = EulerTools.getQuatFromEuler(Order.YXZ, euler.y,euler.x,euler.z);
435                 quat.set(q);
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);
442                 
443                 // http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm
444                 // Y, Z, X order
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;
457
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 ;
471         }
472         
473         
474
475         
476         public static void getEuler(Quat4d quat,Vector3d euler)  {
477                 Vector3d e = EulerTools.getEulerFromQuat(Order.YXZ, quat);
478                 euler.x = e.y;
479                 euler.y = e.x;
480                 euler.z = e.z;
481                 
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);
486                 
487                 // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
488         // Y, Z, X order
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;
493 //                 euler.x = 0.0;
494 //         } else if (test < -NEAR_HALF) {
495 //                 euler.y = -2.0 * Math.atan2(quat.x,quat.w);
496 //                 euler.z = -Math.PI * 0.5;
497 //                 euler.x = 0.0;
498 //         } else {
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);
506 //         }
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;
516 //                      euler.x = 0;
517 //                      return;
518 //              }
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;
522 //                      euler.x = 0;
523 //                      return;
524 //              }
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);
528         }
529         
530         public static Quat4d getQuat(Vector3d euler) {
531                 Quat4d q = new Quat4d();
532                 getQuat(euler,q);
533                 return q;
534         }
535         
536         
537         public static Vector3d getEuler(Quat4d quat) {
538                 Vector3d v = new Vector3d();
539                 getEuler(quat, v);
540                 return v;
541         }
542         
543         public static Quat4d getQuat(AxisAngle4d aa) {
544                 Quat4d q = new Quat4d();
545                 getQuat(aa, q);
546                 return q;
547         }
548         
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;
552
553                 if (mag > EPS) {
554                         mag = Math.sqrt(mag);
555                         aa.angle = 2.0 * Math.atan2(mag, q.w);
556                         mag = 1.0 / mag;
557                         aa.x = q.x * mag;
558                         aa.y = q.y * mag;
559                         aa.z = q.z * mag;
560                         
561                 } else {
562                         aa.x = 0.0;
563                         aa.y = 1.0;
564                         aa.z = 0.0;
565                         aa.angle = 0.0;
566                 }
567                 // aa.set(q);
568                 return aa;
569         }
570         
571         public static Quat4d getIdentityQuat() {
572                 return new Quat4d(0, 0, 0, 1);
573         }
574         
575         public static void getQuat(AxisAngle4d aa, Quat4d q) {
576                 double mag,amag;
577                 // Quat = cos(theta/2) + sin(theta/2)(roation_axis) 
578                 
579                 amag = Math.sqrt( aa.x*aa.x + aa.y*aa.y + aa.z*aa.z);
580                 if( amag < NEAR_ZERO ) {
581                         q.w = 1.0;
582                         q.x = 0.0;
583                         q.y = 0.0;
584                         q.z = 0.0;
585                 } else {  
586                         amag = 1.0/amag; 
587                         double a2 = aa.angle * 0.5;
588                         mag = Math.sin(a2);
589                         q.w = Math.cos(a2);
590                         q.x = aa.x*amag*mag;
591                         q.y = aa.y*amag*mag;
592                         q.z = aa.z*amag*mag;
593                 }
594         }
595         
596         
597         /*
598          * Cohen-Sutherland
599          */
600         
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;
606         
607         
608         private static int bitcode(Vector2f p1, Vector2f min, Vector2f max) {
609                 int code = IN;
610                 if (p1.x < min.x)
611                         code |= LEFT;
612                 else if (p1.x > max.x)
613                         code |= RIGHT;
614                 if (p1.y < min.y)
615                         code |= BOTTOM;
616                 else if (p1.y > max.y)
617                         code |= TOP;
618                 return code;
619         }
620         
621         public static boolean clipLineRectangle(Vector2f p1,Vector2f p2, Vector2f min, Vector2f max, Vector2f r1, Vector2f r2) {
622                 while (true) {
623                         int o1 = bitcode(p1, min, max);
624                         int o2 = bitcode(p2, min, max);
625                         int and = o1 & o2;
626                         int or = o1 | o2;
627                         if (and != IN) {
628                                 return false;
629                         }
630                         if (or == IN) {
631                                 r1.set(p1);
632                                 r2.set(p2);
633                                 return true;
634                         }
635                         if (o1 == IN) {
636                                 Vector2f t = p1;
637                                 p1 = p2;
638                                 p2 = t;
639                                 int t2 = o1;
640                                 o1 = o2;
641                                 o2 = t2;
642                         }
643                         if ((o1 & TOP) != IN) {
644                                 float t = (max.y - p1.y) / (p2.y - p1.y);
645                                 p1.x += t * (p2.x - p1.x);
646                                 p1.y = max.y;
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);
650                                 p1.y = min.y;
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);
654                                 p1.x = min.x;
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);
658                                 p1.x = max.x;
659                         } else {
660                                 throw new RuntimeException("Error in clipping code");
661                         }
662                 }
663                 
664         }
665         
666         public static double square(double d) {
667                 return d * d;
668         }
669         
670         
671         public static void multiplyOrientation(AxisAngle4d aa, AxisAngle4d rot)  {
672                 Quat4d q1 = new Quat4d();
673                 getQuat(aa, q1);
674                 Quat4d q2 = new Quat4d();
675                 getQuat(rot, q2);
676                 q2.mul(q1);
677                 rot.set(q2);
678         }
679         
680         public static double radToDeg(double rad) {
681                 return (rad / Math.PI) * 180.0;
682         }
683         
684         public static double degToRad(double deg) {
685                 return (deg / 180.0) * Math.PI;
686         }
687         
688         public static double clamp(double min, double max,double v) {
689                 if (v < min)
690                         return min;
691                 if (v > max)
692                         return max;
693                 return v;
694         }
695         
696         public static AxisAngle4d createRotation(Vector3d original, Vector3d rotated) { 
697                 AxisAngle4d result = new AxisAngle4d();
698                 if (createRotation(original, rotated, result))
699                         return result;
700                 return null;
701         }
702         
703         
704         public static void setIdentity(Quat4d q) {
705                 q.w = 1.0;
706                 q.x = 0.0;
707                 q.y = 0.0;
708                 q.z = 0.0;
709         }
710         
711         public static void setIdentity(AxisAngle4d aa) {
712                 aa.angle = 0.0;
713                 aa.x = 0.0;
714                 aa.y = 1.0;
715                 aa.z = 0.0;
716         }
717         
718         public static void set(Matrix3d mat, double m00, double m01, double m02,
719                         double m10, double m11, double m12, double m20, double m21,
720                         double m22) {
721                 mat.m00 = m00;
722                 mat.m01 = m01;
723                 mat.m02 = m02;
724
725                 mat.m10 = m10;
726                 mat.m11 = m11;
727                 mat.m12 = m12;
728
729                 mat.m20 = m20;
730                 mat.m21 = m21;
731                 mat.m22 = m22;
732         }
733         
734         public static void set(Matrix4d mat, double[] v) {
735                 mat.m00 = v[0];
736                 mat.m01 = v[1];
737                 mat.m02 = v[2];
738                 mat.m03 = v[3];
739
740                 mat.m10 = v[4];
741                 mat.m11 = v[5];
742                 mat.m12 = v[6];
743                 mat.m13 = v[7];
744
745                 mat.m20 = v[8];
746                 mat.m21 = v[9];
747                 mat.m22 = v[10];
748                 mat.m23 = v[11];
749
750                 mat.m30 = v[12];
751                 mat.m31 = v[13];
752                 mat.m32 = v[14];
753                 mat.m33 = v[15];
754
755         }
756         
757         public static boolean createRotation(Vector3d original, Vector3d rotated, AxisAngle4d result) {
758                 
759                 if (rotated.lengthSquared() > 0.01)
760                         rotated.normalize();
761                 else
762                         return false;
763                 double d = original.dot(rotated);
764                 if (d > 0.9999) {
765                         // original and rotated are parallel, pointing at the same direction
766                         result.angle = 0.0;
767                         result.x = 0.0;
768                         result.y = 1.0;
769                         result.z = 0.0;
770                 } else if (d < -0.9999) {
771                         // original and rotated are parallel, pointing at the opposite direction 
772                         Vector3d a = Z_AXIS;
773                         if (Math.abs(a.dot(original)) > 0.8 )
774                                 a = Y_AXIS;
775                         result.set(a, Math.PI);
776                 } else {
777                         double angle = original.angle(rotated);
778                         Vector3d axis = new Vector3d();
779                         axis.cross(original, rotated);
780                         result.set(axis,angle);
781                 }
782                 return true;
783         }
784         
785         public static boolean createRotation(Vector3d original, Vector3d rotated, Quat4d result) {
786                 
787                 if (rotated.lengthSquared() > 0.01)
788                         rotated.normalize();
789                 else
790                         return false;
791                 double d = original.dot(rotated);
792                 if (d > 0.9999) {
793                         // original and rotated are parallel, pointing at the same direction
794                         result.w = 1.0;
795                         result.x = 0.0;
796                         result.y = 0.0;
797                         result.z = 0.0;
798                 } else if (d < -0.9999) {
799                         // original and rotated are parallel, pointing at the opposite direction 
800                         Vector3d a = Z_AXIS;
801                         if (Math.abs(a.dot(original)) > 0.8 )
802                                 a = Y_AXIS;
803                         getQuat(a, Math.PI, result);
804                         
805                 } else {
806                         double angle = original.angle(rotated);
807                         Vector3d axis = new Vector3d();
808                         axis.cross(original, rotated);
809                         getQuat(axis, angle, result);
810                 }
811                 return true;
812         }
813         
814         public static boolean createRotation(Vector3d original, Vector3d rotated, Vector3d axis, AxisAngle4d result) {
815         
816         if (rotated.lengthSquared() > 0.01)
817             rotated.normalize();
818         else
819             return false;
820         if (original.lengthSquared() > 0.01)
821             original.normalize();
822         else
823             return false;
824         if (axis.lengthSquared() > 0.01)
825             axis.normalize();
826         else
827             return false;
828         double d = original.dot(rotated);
829         if (d > 0.9999) {
830             // original and rotated are parallel, pointing at the same direction
831             result.angle = 0.0;
832             result.x = axis.x;
833             result.y = axis.y;
834             result.z = axis.z;
835         } else if (d < -0.9999) {
836             // original and rotated are parallel, pointing at the opposite direction 
837             result.angle = Math.PI;
838             result.x = axis.x;
839             result.y = axis.y;
840             result.z = axis.z;
841         } else {
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();
849             rotate(q, p1, t1);
850             rotate(q, p2, t2);
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);
855             
856         }
857         return true;
858     }
859         
860         public static void getQuat(Vector3d axis, double angle, Quat4d q)
861         {
862                 double mag,amag;
863                 // Quat = cos(theta/2) + sin(theta/2)(roation_axis) 
864                 
865                 amag = Math.sqrt( axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
866                 if( amag < EPS ) {
867                         q.w = 1.0;
868                         q.x = 0.0;
869                         q.y = 0.0;
870                         q.z = 0.0;
871                 } else {  
872                         amag = 1.0/amag; 
873                         double a2 = angle*0.5;
874                         mag = Math.sin(a2);
875                         q.w = Math.cos(a2);
876                         q.x = axis.x*amag*mag;
877                         q.y = axis.y*amag*mag;
878                         q.z = axis.z*amag*mag;
879                 }
880         
881         }
882         
883         /**
884          * Linear interpolation of quaternions. Result IS set to q1.
885          * @param q1
886          * @param q2
887          * @param alpha
888          */
889         public static void lip(Quat4d q1, Quat4d q2, double alpha) {
890                 double s1 = 1.0 - alpha;
891                 double s2 = alpha;
892                 q1.scale(s1);
893                 mad(q1,q2,s2);
894                 q1.normalize();
895         }
896         
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;
899         }
900         
901         public static void mad(Tuple3d q1, Tuple3d q2, double s2) {
902                 q1.x += q2.x * s2;
903                 q1.y += q2.y * s2;
904                 q1.z += q2.z * s2;
905         }
906         
907         public static void mad(Quat4d q1, Quat4d q2, double s2) {
908                 q1.x += q2.x * s2;
909                 q1.y += q2.y * s2;
910                 q1.z += q2.z * s2;
911                 q1.w += q2.w * s2;
912         }
913         
914         /**
915          * Slerp
916          * 
917          * Sets results to q1. Modifies q2.
918          * 
919          * @param q1
920          * @param q2
921          * @param alpha
922          */
923         public static void sip(Quat4d q1, Quat4d q2, double alpha) {
924                 double cosom = dot(q1,q2);
925                 if (cosom < 0.0) {
926                         cosom = -cosom;
927                         q2.negate();
928                 }
929                 
930                 if (cosom > 0.9999) {
931                         q2.sub(q1);
932                         q2.scale(alpha);
933                         q1.add(q2);
934                         q1.normalize();
935                         return;
936                 }
937                 double theta_0 = Math.acos(cosom);
938                 double theta = theta_0 * alpha;
939                 Quat4d t = new Quat4d(q1);
940                 t.scale(-cosom);
941                 t.add(q2);
942                 t.normalize();
943                 t.scale(Math.sin(theta));
944                 q1.scale(Math.cos(theta));
945                 q1.add(t);
946         }
947         
948         
949         public static void rotate(double angle, Tuple2d v1, Tuple2d v2) {
950                 // TODO : verify implementation
951                 double sin = Math.sin(angle);
952                 if (sin == 1.0) {
953                         v2.x = v1.y;
954                         v2.y = -v1.x;
955                 } else if (sin == -1.0) {
956                         v2.x = -v1.y;
957                         v2.y = v1.x;
958                 } else {
959                         double cos = Math.cos(angle);
960                         if (cos == -1.0) {
961                                 v2.x = -v1.x;
962                                 v2.y = -v1.y;
963                         } else if (cos != 1.0) {
964                                 v2.x= v1.x * cos + v1.y * -sin;
965                                 v2.y= v1.x* sin + v1.y *cos;
966                         }
967                 }       
968         }
969         
970         public static Tuple3d getPosRot(double m3x2[]) {
971                 Vector3d t = new Vector3d();
972                 t.x = m3x2[4];
973                 t.y = m3x2[5];
974                 
975                 
976                 Vector2d v2 = new Vector2d(1,0);
977                 Vector2d v = new Vector2d();
978                 // use rotation of (1,0) to calculate the rotation component
979                 v.x  =  m3x2[0];
980                 v.y  =  m3x2[2];
981                 double a1 = v2.angle(v);
982                 if (v.y < 0) {
983                         t.z = a1;
984                 } else {
985                         t.z = Math.PI*2.0 - a1;
986                 }
987                 return t;
988         }
989         
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);
998                 mat.m32 = -1.0;
999                 return mat;
1000         }
1001         
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);
1007                 mat.m33 =  1.0;
1008                 mat.m03 = -(r+l)/(r-l);
1009                 mat.m13 = -(t+b)/(t-b);
1010                 mat.m23 = -(f+n)/(f-n);
1011                 return mat;
1012         }
1013 }