From 0edb45f11cd034c4937e6941b7a3e5d9f7edbd2f Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 11 Nov 2012 02:49:48 +0100 Subject: Merge MathFloat into FloatUtil --- .../classes/com/jogamp/graph/math/Quaternion.java | 59 +++++++++++----------- .../classes/com/jogamp/graph/math/VectorUtil.java | 8 +-- src/jogl/classes/com/jogamp/opengl/FloatUtil.java | 16 ++++++ .../jogamp/graph/geom/plane/AffineTransform.java | 16 +++--- src/jogl/classes/jogamp/graph/math/MathFloat.java | 45 ----------------- .../classes/jogamp/graph/math/plane/Crossing.java | 19 +++---- 6 files changed, 68 insertions(+), 95 deletions(-) delete mode 100644 src/jogl/classes/jogamp/graph/math/MathFloat.java diff --git a/src/jogl/classes/com/jogamp/graph/math/Quaternion.java b/src/jogl/classes/com/jogamp/graph/math/Quaternion.java index 1e912457d..cbdf52dff 100755 --- a/src/jogl/classes/com/jogamp/graph/math/Quaternion.java +++ b/src/jogl/classes/com/jogamp/graph/math/Quaternion.java @@ -27,7 +27,8 @@ */ package com.jogamp.graph.math; -import jogamp.graph.math.MathFloat; +import com.jogamp.opengl.FloatUtil; + public class Quaternion { protected float x,y,z,w; @@ -49,14 +50,14 @@ public class Quaternion { */ public Quaternion(float[] vector1, float[] vector2) { - float theta = (float)MathFloat.acos(dot(vector1, vector2)); + float theta = (float)FloatUtil.acos(dot(vector1, vector2)); float[] cross = cross(vector1,vector2); cross = normalizeVec(cross); - this.x = (float)MathFloat.sin(theta/2)*cross[0]; - this.y = (float)MathFloat.sin(theta/2)*cross[1]; - this.z = (float)MathFloat.sin(theta/2)*cross[2]; - this.w = (float)MathFloat.cos(theta/2); + this.x = (float)FloatUtil.sin(theta/2)*cross[0]; + this.y = (float)FloatUtil.sin(theta/2)*cross[1]; + this.z = (float)FloatUtil.sin(theta/2)*cross[2]; + this.w = (float)FloatUtil.cos(theta/2); this.normalize(); } @@ -66,8 +67,8 @@ public class Quaternion { public float[] toAxis() { float[] vec = new float[4]; - float scale = (float)MathFloat.sqrt(x * x + y * y + z * z); - vec[0] =(float) MathFloat.acos(w) * 2.0f; + float scale = (float)FloatUtil.sqrt(x * x + y * y + z * z); + vec[0] =(float) FloatUtil.acos(w) * 2.0f; vec[1] = x / scale; vec[2] = y / scale; vec[3] = z / scale; @@ -82,7 +83,7 @@ public class Quaternion { { float[] newVector = new float[3]; - float d = MathFloat.sqrt(vector[0]*vector[0] + vector[1]*vector[1] + vector[2]*vector[2]); + float d = FloatUtil.sqrt(vector[0]*vector[0] + vector[1]*vector[1] + vector[2]*vector[2]); if(d> 0.0f) { newVector[0] = vector[0]/d; @@ -203,7 +204,7 @@ public class Quaternion { */ public void normalize() { - float norme = (float)MathFloat.sqrt(w*w + x*x + y*y + z*z); + float norme = (float)FloatUtil.sqrt(w*w + x*x + y*y + z*z); if (norme == 0.0f) { w = 1.0f; @@ -274,12 +275,12 @@ public class Quaternion { { float omega, cosom, sinom, sclp, sclq; cosom = a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w; - if ((1.0f+cosom) > MathFloat.E) { - if ((1.0f-cosom) > MathFloat.E) { - omega = (float)MathFloat.acos(cosom); - sinom = (float)MathFloat.sin(omega); - sclp = (float)MathFloat.sin((1.0f-t)*omega) / sinom; - sclq = (float)MathFloat.sin(t*omega) / sinom; + if ((1.0f+cosom) > FloatUtil.E) { + if ((1.0f-cosom) > FloatUtil.E) { + omega = (float)FloatUtil.acos(cosom); + sinom = (float)FloatUtil.sin(omega); + sclp = (float)FloatUtil.sin((1.0f-t)*omega) / sinom; + sclq = (float)FloatUtil.sin(t*omega) / sinom; } else { sclp = 1.0f - t; @@ -295,8 +296,8 @@ public class Quaternion { y = a.x; z =-a.w; w = a.z; - sclp = MathFloat.sin((1.0f-t) * MathFloat.PI * 0.5f); - sclq = MathFloat.sin(t * MathFloat.PI * 0.5f); + sclp = FloatUtil.sin((1.0f-t) * FloatUtil.PI * 0.5f); + sclq = FloatUtil.sin(t * FloatUtil.PI * 0.5f); x = sclp*a.x + sclq*b.x; y = sclp*a.y + sclq*b.y; z = sclp*a.z + sclq*b.z; @@ -330,7 +331,7 @@ public class Quaternion { public void setFromMatrix(float[] m) { float T= m[0] + m[4] + m[8] + 1; if (T>0){ - float S = 0.5f / (float)MathFloat.sqrt(T); + float S = 0.5f / (float)FloatUtil.sqrt(T); w = 0.25f / S; x = ( m[5] - m[7]) * S; y = ( m[6] - m[2]) * S; @@ -338,21 +339,21 @@ public class Quaternion { } else{ if ((m[0] > m[4])&(m[0] > m[8])) { - float S = MathFloat.sqrt( 1.0f + m[0] - m[4] - m[8] ) * 2f; // S=4*qx + float S = FloatUtil.sqrt( 1.0f + m[0] - m[4] - m[8] ) * 2f; // S=4*qx w = (m[7] - m[5]) / S; x = 0.25f * S; y = (m[3] + m[1]) / S; z = (m[6] + m[2]) / S; } else if (m[4] > m[8]) { - float S = MathFloat.sqrt( 1.0f + m[4] - m[0] - m[8] ) * 2f; // S=4*qy + float S = FloatUtil.sqrt( 1.0f + m[4] - m[0] - m[8] ) * 2f; // S=4*qy w = (m[6] - m[2]) / S; x = (m[3] + m[1]) / S; y = 0.25f * S; z = (m[7] + m[5]) / S; } else { - float S = MathFloat.sqrt( 1.0f + m[8] - m[0] - m[4] ) * 2f; // S=4*qz + float S = FloatUtil.sqrt( 1.0f + m[8] - m[0] - m[4] ) * 2f; // S=4*qz w = (m[3] - m[1]) / S; x = (m[6] + m[2]) / S; y = (m[7] + m[5]) / S; @@ -368,13 +369,13 @@ public class Quaternion { */ public boolean isRotationMatrix(float[] m) { double epsilon = 0.01; // margin to allow for rounding errors - if (MathFloat.abs(m[0]*m[3] + m[3]*m[4] + m[6]*m[7]) > epsilon) return false; - if (MathFloat.abs(m[0]*m[2] + m[3]*m[5] + m[6]*m[8]) > epsilon) return false; - if (MathFloat.abs(m[1]*m[2] + m[4]*m[5] + m[7]*m[8]) > epsilon) return false; - if (MathFloat.abs(m[0]*m[0] + m[3]*m[3] + m[6]*m[6] - 1) > epsilon) return false; - if (MathFloat.abs(m[1]*m[1] + m[4]*m[4] + m[7]*m[7] - 1) > epsilon) return false; - if (MathFloat.abs(m[2]*m[2] + m[5]*m[5] + m[8]*m[8] - 1) > epsilon) return false; - return (MathFloat.abs(determinant(m)-1) < epsilon); + if (FloatUtil.abs(m[0]*m[3] + m[3]*m[4] + m[6]*m[7]) > epsilon) return false; + if (FloatUtil.abs(m[0]*m[2] + m[3]*m[5] + m[6]*m[8]) > epsilon) return false; + if (FloatUtil.abs(m[1]*m[2] + m[4]*m[5] + m[7]*m[8]) > epsilon) return false; + if (FloatUtil.abs(m[0]*m[0] + m[3]*m[3] + m[6]*m[6] - 1) > epsilon) return false; + if (FloatUtil.abs(m[1]*m[1] + m[4]*m[4] + m[7]*m[7] - 1) > epsilon) return false; + if (FloatUtil.abs(m[2]*m[2] + m[5]*m[5] + m[8]*m[8] - 1) > epsilon) return false; + return (FloatUtil.abs(determinant(m)-1) < epsilon); } private float determinant(float[] m) { return m[0]*m[4]*m[8] + m[3]*m[7]*m[2] + m[6]*m[1]*m[5] - m[0]*m[7]*m[5] - m[3]*m[1]*m[8] - m[6]*m[4]*m[2]; diff --git a/src/jogl/classes/com/jogamp/graph/math/VectorUtil.java b/src/jogl/classes/com/jogamp/graph/math/VectorUtil.java index d51afcbab..540e901b8 100755 --- a/src/jogl/classes/com/jogamp/graph/math/VectorUtil.java +++ b/src/jogl/classes/com/jogamp/graph/math/VectorUtil.java @@ -29,9 +29,9 @@ package com.jogamp.graph.math; import java.util.ArrayList; -import jogamp.graph.math.MathFloat; import com.jogamp.graph.geom.Vertex; +import com.jogamp.opengl.FloatUtil; public class VectorUtil { @@ -64,7 +64,7 @@ public class VectorUtil { { float[] newVector = new float[3]; - float d = MathFloat.sqrt(vector[0]*vector[0] + vector[1]*vector[1] + vector[2]*vector[2]); + float d = FloatUtil.sqrt(vector[0]*vector[0] + vector[1]*vector[1] + vector[2]*vector[2]); if(d> 0.0f) { newVector[0] = vector[0]/d; @@ -181,7 +181,7 @@ public class VectorUtil { */ public static float norm(float[] vec) { - return MathFloat.sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]); + return FloatUtil.sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]); } /** Compute distance between 2 points * @param p0 a ref point on the line @@ -193,7 +193,7 @@ public class VectorUtil { { float[] w = new float[]{point[0]-p0[0],point[1]-p0[1],point[2]-p0[2]}; - float distance = MathFloat.sqrt(w[0]*w[0] + w[1]*w[1] + w[2]*w[2]); + float distance = FloatUtil.sqrt(w[0]*w[0] + w[1]*w[1] + w[2]*w[2]); return distance; } diff --git a/src/jogl/classes/com/jogamp/opengl/FloatUtil.java b/src/jogl/classes/com/jogamp/opengl/FloatUtil.java index ffd2344a6..16e8b1455 100644 --- a/src/jogl/classes/com/jogamp/opengl/FloatUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/FloatUtil.java @@ -406,5 +406,21 @@ public class FloatUtil { } return sb; } + + public static final float E = 2.7182818284590452354f; + + public static final float PI = 3.14159265358979323846f; + + public static float abs(float a) { return (float) java.lang.Math.abs(a); } + + public static float pow(float a, float b) { return (float) java.lang.Math.pow(a, b); } + + public static float sin(float a) { return (float) java.lang.Math.sin(a); } + + public static float cos(float a) { return (float) java.lang.Math.cos(a); } + + public static float acos(float a) { return (float) java.lang.Math.acos(a); } + + public static float sqrt(float a) { return (float) java.lang.Math.sqrt(a); } } \ No newline at end of file diff --git a/src/jogl/classes/jogamp/graph/geom/plane/AffineTransform.java b/src/jogl/classes/jogamp/graph/geom/plane/AffineTransform.java index fc086ebe4..123883b2f 100644 --- a/src/jogl/classes/jogamp/graph/geom/plane/AffineTransform.java +++ b/src/jogl/classes/jogamp/graph/geom/plane/AffineTransform.java @@ -22,11 +22,11 @@ package jogamp.graph.geom.plane; import java.io.IOException; import java.io.Serializable; -import jogamp.graph.math.MathFloat; // import jogamp.opengl.util.HashCode; import com.jogamp.graph.geom.Vertex; import com.jogamp.graph.geom.Vertex.Factory; +import com.jogamp.opengl.FloatUtil; public class AffineTransform implements Cloneable, Serializable { @@ -285,13 +285,13 @@ public class AffineTransform implements Cloneable, Serializable { } public void setToRotation(float angle) { - float sin = MathFloat.sin(angle); - float cos = MathFloat.cos(angle); - if (MathFloat.abs(cos) < ZERO) { + float sin = FloatUtil.sin(angle); + float cos = FloatUtil.cos(angle); + if (FloatUtil.abs(cos) < ZERO) { cos = 0.0f; sin = sin > 0.0f ? 1.0f : -1.0f; } else - if (MathFloat.abs(sin) < ZERO) { + if (FloatUtil.abs(sin) < ZERO) { sin = 0.0f; cos = cos > 0.0f ? 1.0f : -1.0f; } @@ -387,7 +387,7 @@ public class AffineTransform implements Cloneable, Serializable { public AffineTransform createInverse() throws NoninvertibleTransformException { float det = getDeterminant(); - if (MathFloat.abs(det) < ZERO) { + if (FloatUtil.abs(det) < ZERO) { throw new NoninvertibleTransformException(determinantIsZero); } return new AffineTransform( @@ -467,7 +467,7 @@ public class AffineTransform implements Cloneable, Serializable { public Vertex inverseTransform(Vertex src, Vertex dst) throws NoninvertibleTransformException { float det = getDeterminant(); - if (MathFloat.abs(det) < ZERO) { + if (FloatUtil.abs(det) < ZERO) { throw new NoninvertibleTransformException(determinantIsZero); } if (dst == null) { @@ -485,7 +485,7 @@ public class AffineTransform implements Cloneable, Serializable { throws NoninvertibleTransformException { float det = getDeterminant(); - if (MathFloat.abs(det) < ZERO) { + if (FloatUtil.abs(det) < ZERO) { throw new NoninvertibleTransformException(determinantIsZero); } diff --git a/src/jogl/classes/jogamp/graph/math/MathFloat.java b/src/jogl/classes/jogamp/graph/math/MathFloat.java deleted file mode 100644 index 82e7823a6..000000000 --- a/src/jogl/classes/jogamp/graph/math/MathFloat.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2011 JogAmp Community. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied, of JogAmp Community. - */ -package jogamp.graph.math; - -public class MathFloat { - - public static final float E = 2.7182818284590452354f; - - public static final float PI = 3.14159265358979323846f; - - public static float abs(float a) { return (float) java.lang.Math.abs(a); } - public static float pow(float a, float b) { return (float) java.lang.Math.pow(a, b); } - - public static float sin(float a) { return (float) java.lang.Math.sin(a); } - public static float cos(float a) { return (float) java.lang.Math.cos(a); } - public static float acos(float a) { return (float) java.lang.Math.acos(a); } - - public static float sqrt(float a) { return (float) java.lang.Math.sqrt(a); } - -} diff --git a/src/jogl/classes/jogamp/graph/math/plane/Crossing.java b/src/jogl/classes/jogamp/graph/math/plane/Crossing.java index 9be5978cc..51d81da54 100644 --- a/src/jogl/classes/jogamp/graph/math/plane/Crossing.java +++ b/src/jogl/classes/jogamp/graph/math/plane/Crossing.java @@ -19,9 +19,10 @@ */ package jogamp.graph.math.plane; +import com.jogamp.opengl.FloatUtil; + import jogamp.graph.geom.plane.Path2D; import jogamp.graph.geom.plane.PathIterator; -import jogamp.graph.math.MathFloat; public class Crossing { @@ -68,7 +69,7 @@ public class Crossing { if (d < 0.0) { return 0; } - d = MathFloat.sqrt(d); + d = FloatUtil.sqrt(d); res[rc++] = (- b + d) / (a * 2.0f); // d != 0.0 if (d != 0.0) { @@ -101,15 +102,15 @@ public class Crossing { float n = - a / 3.0f; if (R2 < Q3) { - float t = MathFloat.acos(R / MathFloat.sqrt(Q3)) / 3.0f; - float p = 2.0f * MathFloat.PI / 3.0f; - float m = -2.0f * MathFloat.sqrt(Q); - res[rc++] = m * MathFloat.cos(t) + n; - res[rc++] = m * MathFloat.cos(t + p) + n; - res[rc++] = m * MathFloat.cos(t - p) + n; + float t = FloatUtil.acos(R / FloatUtil.sqrt(Q3)) / 3.0f; + float p = 2.0f * FloatUtil.PI / 3.0f; + float m = -2.0f * FloatUtil.sqrt(Q); + res[rc++] = m * FloatUtil.cos(t) + n; + res[rc++] = m * FloatUtil.cos(t + p) + n; + res[rc++] = m * FloatUtil.cos(t - p) + n; } else { // Debug.println("R2 >= Q3 (" + R2 + "/" + Q3 + ")"); - float A = MathFloat.pow(MathFloat.abs(R) + MathFloat.sqrt(R2 - Q3), 1.0f / 3.0f); + float A = FloatUtil.pow(FloatUtil.abs(R) + FloatUtil.sqrt(R2 - Q3), 1.0f / 3.0f); if (R > 0.0) { A = -A; } -- cgit v1.2.3