diff options
author | Sven Gothel <[email protected]> | 2014-03-05 03:23:44 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-03-05 03:23:44 +0100 |
commit | 79156e080ef919857f1624543e37b62794fb5a64 (patch) | |
tree | 8c7b3eeea7bb85b5af05356c7adce1f69606be3a /src/jogl/classes | |
parent | fe3daea00da48c90a4e0c90cf37514a3ab7093d6 (diff) |
Bug 801: VectorUtil: Pass result vector, allowing caller to manage memory (performance, reduce temp objects)
Diffstat (limited to 'src/jogl/classes')
8 files changed, 127 insertions, 129 deletions
diff --git a/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java b/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java index 44a8e7384..cb99fbfa4 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java +++ b/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java @@ -135,6 +135,10 @@ public class OutlineShape implements Comparable<OutlineShape> { private float sharpness; + private final float[] tmpV1 = new float[3]; + private final float[] tmpV2 = new float[3]; + private final float[] tmpV3 = new float[3]; + /** Create a new Outline based Shape */ public OutlineShape(Vertex.Factory<? extends Vertex> factory) { @@ -428,16 +432,16 @@ public class OutlineShape implements Comparable<OutlineShape> { } private void subdivideTriangle(final Outline outline, Vertex a, Vertex b, Vertex c, int index){ - float[] v1 = VectorUtil.mid(a.getCoord(), b.getCoord()); - float[] v3 = VectorUtil.mid(b.getCoord(), c.getCoord()); - float[] v2 = VectorUtil.mid(v1, v3); + VectorUtil.mid(tmpV1, a.getCoord(), b.getCoord()); + VectorUtil.mid(tmpV3, b.getCoord(), c.getCoord()); + VectorUtil.mid(tmpV2, tmpV1, tmpV3); //drop off-curve vertex to image on the curve - b.setCoord(v2, 0, 3); + b.setCoord(tmpV2, 0, 3); b.setOnCurve(true); - outline.addVertex(index, vertexFactory.create(v1, 0, 3, false)); - outline.addVertex(index+2, vertexFactory.create(v3, 0, 3, false)); + outline.addVertex(index, vertexFactory.create(tmpV1, 0, 3, false)); + outline.addVertex(index+2, vertexFactory.create(tmpV3, 0, 3, false)); } /** @@ -491,10 +495,6 @@ public class OutlineShape implements Comparable<OutlineShape> { }while(!overlaps.isEmpty()); } - private final float[] tempVecAC = new float[3]; - private final float[] tempVecAB = new float[3]; - private final float[] tempVecAP = new float[3]; - private Vertex checkTriOverlaps(Vertex a, Vertex b, Vertex c) { int count = getOutlineNumber(); for (int cc = 0; cc < count; cc++) { @@ -515,7 +515,7 @@ public class OutlineShape implements Comparable<OutlineShape> { if( VectorUtil.vertexInTriangle3(a.getCoord(), b.getCoord(), c.getCoord(), current.getCoord(), nextV.getCoord(), prevV.getCoord(), - tempVecAC, tempVecAB, tempVecAP) ) { + tmpV1, tmpV2, tmpV3) ) { return current; } if(VectorUtil.testTri2SegIntersection(a, b, c, prevV, current) || @@ -538,9 +538,8 @@ public class OutlineShape implements Comparable<OutlineShape> { final Vertex currentVertex = outline.getVertex(i); final Vertex nextVertex = outline.getVertex((i+1)%vertexCount); if ( !currentVertex.isOnCurve() && !nextVertex.isOnCurve() ) { - final float[] newCoords = VectorUtil.mid(currentVertex.getCoord(), - nextVertex.getCoord()); - final Vertex v = vertexFactory.create(newCoords, 0, 3, true); + VectorUtil.mid(tmpV1, currentVertex.getCoord(), nextVertex.getCoord()); + final Vertex v = vertexFactory.create(tmpV1, 0, 3, true); i++; vertexCount++; outline.addVertex(i, v); diff --git a/src/jogl/classes/com/jogamp/graph/font/Font.java b/src/jogl/classes/com/jogamp/graph/font/Font.java index 2f21fd214..ac7a904e7 100644 --- a/src/jogl/classes/com/jogamp/graph/font/Font.java +++ b/src/jogl/classes/com/jogamp/graph/font/Font.java @@ -84,7 +84,12 @@ public interface Font { float getLineGap(float pixelSize); float getMaxExtend(float pixelSize); float getScale(float pixelSize); - AABBox getBBox(float pixelSize); + /** + * @param pixelSize + * @param tmpV3 caller provided temporary 3-component vector + * @return + */ + AABBox getBBox(float pixelSize, float[] tmpV3); } /** @@ -106,7 +111,12 @@ public interface Font { public short getID(); public AABBox getBBox(); public float getScale(float pixelSize); - public AABBox getBBox(float pixelSize); + /** + * @param pixelSize + * @param tmpV3 caller provided temporary 3-component vector + * @return + */ + public AABBox getBBox(float pixelSize, float[] tmpV3); public float getAdvance(float pixelSize, boolean useFrationalMetrics); public OutlineShape getShape(); public int hashCode(); diff --git a/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java b/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java index 52a59c599..3c3510b7f 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java @@ -56,7 +56,8 @@ public class Quaternion { */ public Quaternion(float[] vector1, float[] vector2) { final float theta = FloatUtil.acos(VectorUtil.dot(vector1, vector2)); - final float[] cross = VectorUtil.cross(vector1, vector2); + final float[] cross = new float[3]; + VectorUtil.cross(cross, vector1, vector2); fromAxis(cross, theta); } @@ -79,7 +80,7 @@ public class Quaternion { public void fromAxis(float[] vector, float angle) { final float halfangle = angle * 0.5f; final float sin = FloatUtil.sin(halfangle); - final float[] nv = VectorUtil.normalize(vector); + final float[] nv = VectorUtil.normalize(vector, vector); x = (nv[0] * sin); y = (nv[1] * sin); z = (nv[2] * sin); @@ -220,7 +221,7 @@ public class Quaternion { * Normalize a quaternion required if to be used as a rotational quaternion */ public void normalize() { - final float norme = (float) FloatUtil.sqrt(w * w + x * x + y * y + z * z); + final float norme = FloatUtil.sqrt(w * w + x * x + y * y + z * z); if (norme == 0.0f) { setIdentity(); } else { @@ -355,7 +356,7 @@ public class Quaternion { public void setFromMatrix(float[] m) { final float T = m[0] + m[4] + m[8] + 1; if (T > 0) { - final float S = 0.5f / (float) FloatUtil.sqrt(T); + final float S = 0.5f / FloatUtil.sqrt(T); w = 0.25f / S; x = (m[5] - m[7]) * S; y = (m[6] - m[2]) * S; diff --git a/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java b/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java index 053876b56..734b7459b 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java @@ -52,41 +52,26 @@ public class VectorUtil { { return (vec1[0]*vec2[0] + vec1[1]*vec2[1] + vec1[2]*vec2[2]); } - /** Normalize a vector + + /** + * Normalize a vector * @param vector input vector * @return normalized vector */ - public static float[] normalize(float[] vector) + public static float[] normalize(final float[] result, float[] vector) { - final float[] newVector = new float[3]; - final 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; - newVector[1] = vector[1]/d; - newVector[2] = vector[2]/d; + result[0] = vector[0]/d; + result[1] = vector[1]/d; + result[2] = vector[2]/d; } - return newVector; - } - - /** Scales a vector by param creating a new float[] for the result! - * @param vector input vector - * @param scale constant to scale by - * @return new scaled vector - * @deprecated Use {@link #scale(float[], float[], float)} - */ - public static float[] scale(float[] vector, float scale) - { - final float[] newVector = new float[3]; - - newVector[0] = vector[0] * scale; - newVector[1] = vector[1] * scale; - newVector[2] = vector[2] * scale; - return newVector; + return result; } - /** Scales a vector by param using given result float[] + /** + * Scales a vector by param using given result float[] * @param result vector for the result * @param vector input vector * @param scale single scale constant for all vector components @@ -113,67 +98,61 @@ public class VectorUtil { return result; } - /** Adds to vectors + /** + * Adds to vectors * @param v1 vector 1 * @param v2 vector 2 * @return v1 + v2 */ - public static float[] vectorAdd(float[] v1, float[] v2) + public static float[] vectorAdd(float[] result, float[] v1, float[] v2) { - final float[] newVector = new float[3]; - - newVector[0] = v1[0] + v2[0]; - newVector[1] = v1[1] + v2[1]; - newVector[2] = v1[2] + v2[2]; - return newVector; + result[0] = v1[0] + v2[0]; + result[1] = v1[1] + v2[1]; + result[2] = v1[2] + v2[2]; + return result; } - /** cross product vec1 x vec2 + /** + * cross product vec1 x vec2 * @param vec1 vector 1 * @param vec2 vecttor 2 * @return the resulting vector */ - public static float[] cross(float[] vec1, float[] vec2) + public static float[] cross(final float[] result, float[] vec1, float[] vec2) { - final float[] out = new float[3]; + result[0] = vec2[2]*vec1[1] - vec2[1]*vec1[2]; + result[1] = vec2[0]*vec1[2] - vec2[2]*vec1[0]; + result[2] = vec2[1]*vec1[0] - vec2[0]*vec1[1]; - out[0] = vec2[2]*vec1[1] - vec2[1]*vec1[2]; - out[1] = vec2[0]*vec1[2] - vec2[2]*vec1[0]; - out[2] = vec2[1]*vec1[0] - vec2[0]*vec1[1]; - - return out; + return result; } /** Column Matrix Vector multiplication * @param colMatrix column matrix (4x4) * @param vec vector(x,y,z) - * @return result new float[3] + * @return result */ - public static float[] colMatrixVectorMult(float[] colMatrix, float[] vec) + public static float[] colMatrixVectorMult(final float[] result, float[] colMatrix, float[] vec) { - final float[] out = new float[3]; - - out[0] = vec[0]*colMatrix[0] + vec[1]*colMatrix[4] + vec[2]*colMatrix[8] + colMatrix[12]; - out[1] = vec[0]*colMatrix[1] + vec[1]*colMatrix[5] + vec[2]*colMatrix[9] + colMatrix[13]; - out[2] = vec[0]*colMatrix[2] + vec[1]*colMatrix[6] + vec[2]*colMatrix[10] + colMatrix[14]; + result[0] = vec[0]*colMatrix[0] + vec[1]*colMatrix[4] + vec[2]*colMatrix[8] + colMatrix[12]; + result[1] = vec[0]*colMatrix[1] + vec[1]*colMatrix[5] + vec[2]*colMatrix[9] + colMatrix[13]; + result[2] = vec[0]*colMatrix[2] + vec[1]*colMatrix[6] + vec[2]*colMatrix[10] + colMatrix[14]; - return out; + return result; } /** Matrix Vector multiplication * @param rawMatrix column matrix (4x4) * @param vec vector(x,y,z) - * @return result new float[3] + * @return result */ - public static float[] rowMatrixVectorMult(float[] rawMatrix, float[] vec) + public static float[] rowMatrixVectorMult(final float[] result, float[] rawMatrix, float[] vec) { - final float[] out = new float[3]; - - out[0] = vec[0]*rawMatrix[0] + vec[1]*rawMatrix[1] + vec[2]*rawMatrix[2] + rawMatrix[3]; - out[1] = vec[0]*rawMatrix[4] + vec[1]*rawMatrix[5] + vec[2]*rawMatrix[6] + rawMatrix[7]; - out[2] = vec[0]*rawMatrix[8] + vec[1]*rawMatrix[9] + vec[2]*rawMatrix[10] + rawMatrix[11]; + result[0] = vec[0]*rawMatrix[0] + vec[1]*rawMatrix[1] + vec[2]*rawMatrix[2] + rawMatrix[3]; + result[1] = vec[0]*rawMatrix[4] + vec[1]*rawMatrix[5] + vec[2]*rawMatrix[6] + rawMatrix[7]; + result[2] = vec[0]*rawMatrix[8] + vec[1]*rawMatrix[9] + vec[2]*rawMatrix[10] + rawMatrix[11]; - return out; + return result; } /** Calculate the midpoint of two values @@ -186,19 +165,19 @@ public class VectorUtil { return (p1+p2)/2.0f; } - /** Calculate the midpoint of two points + /** + * Calculate the midpoint of two points * @param p1 first point * @param p2 second point * @return midpoint */ - public static float[] mid(float[] p1, float[] p2) + public static float[] mid(final float[] result, float[] p1, float[] p2) { - final float[] midPoint = new float[3]; - midPoint[0] = (p1[0] + p2[0])*0.5f; - midPoint[1] = (p1[1] + p2[1])*0.5f; - midPoint[2] = (p1[2] + p2[2])*0.5f; + result[0] = (p1[0] + p2[0])*0.5f; + result[1] = (p1[1] + p2[1])*0.5f; + result[2] = (p1[2] + p2[2])*0.5f; - return midPoint; + return result; } /** Compute the norm of a vector @@ -357,7 +336,8 @@ public class VectorUtil { return (u >= 0) && (v >= 0) && (u + v < 1); } - /** Check if one of three vertices are in triangle using + /** + * Check if one of three vertices are in triangle using * barycentric coordinates computation. * @param a first triangle vertex * @param b second triangle vertex @@ -365,26 +345,29 @@ public class VectorUtil { * @param p1 the vertex in question * @param p2 the vertex in question * @param p3 the vertex in question + * @param tmpAC + * @param tmpAB + * @param tmpAP * @return true if p1 or p2 or p3 is in triangle (a, b, c), false otherwise. */ public static boolean vertexInTriangle3(float[] a, float[] b, float[] c, float[] p1, float[] p2, float[] p3, - float[] ac, float[] ab, float[] ap){ + float[] tmpAC, float[] tmpAB, float[] tmpAP){ // Compute vectors - computeVector(ac, a, c); //v0 - computeVector(ab, a, b); //v1 + computeVector(tmpAC, a, c); //v0 + computeVector(tmpAB, a, b); //v1 // Compute dot products - final float dotAC_AC = dot(ac, ac); - final float dotAC_AB = dot(ac, ab); - final float dotAB_AB = dot(ab, ab); + final float dotAC_AC = dot(tmpAC, tmpAC); + final float dotAC_AB = dot(tmpAC, tmpAB); + final float dotAB_AB = dot(tmpAB, tmpAB); // Compute barycentric coordinates final float invDenom = 1 / (dotAC_AC * dotAB_AB - dotAC_AB * dotAC_AB); { - computeVector(ap, a, p1); //v2 - final float dotAC_AP1 = dot(ac, ap); - final float dotAB_AP1 = dot(ab, ap); + computeVector(tmpAP, a, p1); //v2 + final float dotAC_AP1 = dot(tmpAC, tmpAP); + final float dotAB_AP1 = dot(tmpAB, tmpAP); final float u1 = (dotAB_AB * dotAC_AP1 - dotAC_AB * dotAB_AP1) * invDenom; final float v1 = (dotAC_AC * dotAB_AP1 - dotAC_AB * dotAC_AP1) * invDenom; @@ -395,9 +378,9 @@ public class VectorUtil { } { - computeVector(ap, a, p2); //v2 - final float dotAC_AP2 = dot(ac, ap); - final float dotAB_AP2 = dot(ab, ap); + computeVector(tmpAP, a, p2); //v2 + final float dotAC_AP2 = dot(tmpAC, tmpAP); + final float dotAB_AP2 = dot(tmpAB, tmpAP); final float u = (dotAB_AB * dotAC_AP2 - dotAC_AB * dotAB_AP2) * invDenom; final float v = (dotAC_AC * dotAB_AP2 - dotAC_AB * dotAC_AP2) * invDenom; @@ -408,9 +391,9 @@ public class VectorUtil { } { - computeVector(ap, a, p3); //v2 - final float dotAC_AP3 = dot(ac, ap); - final float dotAB_AP3 = dot(ab, ap); + computeVector(tmpAP, a, p3); //v2 + final float dotAC_AP3 = dot(tmpAC, tmpAP); + final float dotAB_AP3 = dot(tmpAB, tmpAP); final float u = (dotAB_AB * dotAC_AP3 - dotAC_AB * dotAB_AP3) * invDenom; final float v = (dotAC_AC * dotAB_AP3 - dotAC_AB * dotAC_AP3) * invDenom; @@ -473,10 +456,9 @@ public class VectorUtil { * @param b vertex 2 of first segment * @param c vertex 1 of second segment * @param d vertex 2 of second segment - * @return the intersection coordinates if the segments intersect, otherwise - * returns null + * @return the intersection coordinates if the segments intersect, otherwise returns null */ - public static float[] seg2SegIntersection(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d) { + public static float[] seg2SegIntersection(final float[] result, Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d) { final float determinant = (a.getX()-b.getX())*(c.getY()-d.getY()) - (a.getY()-b.getY())*(c.getX()-d.getX()); if (determinant == 0) @@ -492,7 +474,10 @@ public class VectorUtil { if(gamma <= 0 || gamma >= 1) return null; if(gamma1 <= 0 || gamma1 >= 1) return null; - return new float[]{xi,yi,0}; + result[0] = xi; + result[1] = yi; + result[2] = 0; + return result; } /** Compute intersection between two segments @@ -535,7 +520,7 @@ public class VectorUtil { * @return the intersection coordinates if the lines intersect, otherwise * returns null */ - public static float[] line2lineIntersection(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d) { + public static float[] line2lineIntersection(final float[] result, Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d) { final float determinant = (a.getX()-b.getX())*(c.getY()-d.getY()) - (a.getY()-b.getY())*(c.getX()-d.getX()); if (determinant == 0) @@ -546,7 +531,10 @@ public class VectorUtil { final float xi = ((c.getX()-d.getX())*alpha-(a.getX()-b.getX())*beta)/determinant; final float yi = ((c.getY()-d.getY())*alpha-(a.getY()-b.getY())*beta)/determinant; - return new float[]{xi,yi,0}; + result[0] = xi; + result[1] = yi; + result[2] = 0; + return result; } /** Check if a segment intersects with a triangle diff --git a/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java b/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java index 5fbc28c60..81928888c 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java +++ b/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java @@ -37,9 +37,9 @@ import com.jogamp.opengl.math.VectorUtil; * */ public class AABBox implements Cloneable { - private float[] low = new float[3]; - private float[] high = new float[3]; - private float[] center = new float[3]; + private final float[] low = new float[3]; + private final float[] high = new float[3]; + private final float[] center = new float[3]; /** Create a Axis Aligned bounding box (AABBox) * where the low and and high MAX float Values. @@ -275,26 +275,25 @@ public class AABBox implements Cloneable { return center; } - /** Scale the AABBox by a constant + /** + * Scale the AABBox by a constant * @param size a constant float value + * @param tmpV3 caller provided temporary 3-component vector */ - public final void scale(float size) { - float[] diffH = new float[3]; - diffH[0] = high[0] - center[0]; - diffH[1] = high[1] - center[1]; - diffH[2] = high[2] - center[2]; - - diffH = VectorUtil.scale(diffH, size); + public final void scale(float size, float[] tmpV3) { + tmpV3[0] = high[0] - center[0]; + tmpV3[1] = high[1] - center[1]; + tmpV3[2] = high[2] - center[2]; - float[] diffL = new float[3]; - diffL[0] = low[0] - center[0]; - diffL[1] = low[1] - center[1]; - diffL[2] = low[2] - center[2]; + VectorUtil.scale(tmpV3, tmpV3, size); // in-place scale + VectorUtil.vectorAdd(high, center, tmpV3); - diffL = VectorUtil.scale(diffL, size); + tmpV3[0] = low[0] - center[0]; + tmpV3[1] = low[1] - center[1]; + tmpV3[2] = low[2] - center[2]; - high = VectorUtil.vectorAdd(center, diffH); - low = VectorUtil.vectorAdd(center, diffL); + VectorUtil.scale(tmpV3, tmpV3, size); // in-place scale + VectorUtil.vectorAdd(low, center, tmpV3); } public final float getMinX() { diff --git a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java index ae9c43ec5..3cd9ab7c1 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java @@ -53,6 +53,7 @@ class TypecastFont implements Font { private final int cmapentries; private final IntObjectHashMap char2Glyph; private final TypecastHMetrics metrics; + private final float[] tmpV3 = new float[3]; // FIXME: Add cache size to limit memory usage ?? public TypecastFont(final OTFontCollection fontset) { @@ -260,7 +261,7 @@ class TypecastFont implements Font { final char character = string.charAt(i); if (character != ' ') { final Glyph glyph = getGlyph(character); - AABBox bbox = glyph.getBBox(pixelSize); + AABBox bbox = glyph.getBBox(pixelSize, tmpV3); height = (int)Math.ceil(Math.max(bbox.getHeight(), height)); } } diff --git a/src/jogl/classes/jogamp/graph/font/typecast/TypecastGlyph.java b/src/jogl/classes/jogamp/graph/font/typecast/TypecastGlyph.java index 82971848e..b0e283278 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/TypecastGlyph.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/TypecastGlyph.java @@ -188,10 +188,10 @@ public class TypecastGlyph implements Font.Glyph { } @Override - public final AABBox getBBox(float pixelSize) { + public final AABBox getBBox(float pixelSize, float[] tmpV3) { final float size = getScale(pixelSize); AABBox newBox = getBBox().clone(); - newBox.scale(size); + newBox.scale(size, tmpV3); return newBox; } diff --git a/src/jogl/classes/jogamp/graph/font/typecast/TypecastHMetrics.java b/src/jogl/classes/jogamp/graph/font/typecast/TypecastHMetrics.java index 7efad5fb0..4064e6463 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/TypecastHMetrics.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/TypecastHMetrics.java @@ -82,9 +82,9 @@ class TypecastHMetrics implements Metrics { return pixelSize * unitsPerEM_Inv; } @Override - public final AABBox getBBox(float pixelSize) { + public final AABBox getBBox(float pixelSize, float[] tmpV3) { AABBox res = new AABBox(bbox.getLow(), bbox.getHigh()); - res.scale(getScale(pixelSize)); + res.scale(getScale(pixelSize), tmpV3); return res; } }
\ No newline at end of file |