From ba8853981721d9cc0f23b446750030b5e9ef0e0a Mon Sep 17 00:00:00 2001 From: "sha..rd" Date: Sun, 19 Jun 2011 19:17:14 +0000 Subject: [PATCH] * Improved performance of Matrix3/4f.fillFloatBuffer() by writing to an array first * Initial javadoc for com.jme3.collision git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7672 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../core/com/jme3/collision/Collidable.java | 17 ++++--- .../com/jme3/collision/CollisionResults.java | 14 ++++++ .../UnsupportedCollisionException.java | 7 ++- engine/src/core/com/jme3/math/Matrix3f.java | 50 ++++++++++++++++--- engine/src/core/com/jme3/math/Matrix4f.java | 31 ++++++++---- engine/src/core/com/jme3/util/TempVars.java | 2 + 6 files changed, 94 insertions(+), 27 deletions(-) diff --git a/engine/src/core/com/jme3/collision/Collidable.java b/engine/src/core/com/jme3/collision/Collidable.java index 5dd823067..65c063135 100644 --- a/engine/src/core/com/jme3/collision/Collidable.java +++ b/engine/src/core/com/jme3/collision/Collidable.java @@ -33,16 +33,21 @@ package com.jme3.collision; /** - * Interface for collidable objects. - * @author Kirill + * Interface for Collidable objects. + * Classes that implement this interface are marked as collidable, meaning + * they support collision detection between other objects that are also + * collidable. + * + * @author Kirill Vainer */ public interface Collidable { /** - * Check collision with another collidable - * @param other - * @param results - * @return how many collisions were found + * Check collision with another Collidable. + * + * @param other The object to check collision against + * @param results Will contain the list of {@link CollisionResult}s. + * @return how many collisions were found between this and other */ public int collideWith(Collidable other, CollisionResults results) throws UnsupportedCollisionException; } diff --git a/engine/src/core/com/jme3/collision/CollisionResults.java b/engine/src/core/com/jme3/collision/CollisionResults.java index 84585934f..ec691c102 100644 --- a/engine/src/core/com/jme3/collision/CollisionResults.java +++ b/engine/src/core/com/jme3/collision/CollisionResults.java @@ -36,15 +36,29 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; +/** + * CollisionResults is a collection returned as a result of a + * collision detection operation done by {@link Collidable}. + * + * @author Kirill Vainer + */ public class CollisionResults implements Iterable { private final ArrayList results = new ArrayList(); private boolean sorted = true; + /** + * Clears all collision results added to this list + */ public void clear(){ results.clear(); } + /** + * Iterator for iterating over the collision results. + * + * @return the iterator + */ public Iterator iterator() { if (!sorted){ Collections.sort(results); diff --git a/engine/src/core/com/jme3/collision/UnsupportedCollisionException.java b/engine/src/core/com/jme3/collision/UnsupportedCollisionException.java index 68192a1be..ddd6779eb 100644 --- a/engine/src/core/com/jme3/collision/UnsupportedCollisionException.java +++ b/engine/src/core/com/jme3/collision/UnsupportedCollisionException.java @@ -33,8 +33,11 @@ package com.jme3.collision; /** - * - * @author Kirill + * Thrown by {@link Collidable} when the requested collision query could not + * be completed because one of the collidables does not support colliding with + * the other. + * + * @author Kirill Vainer */ public class UnsupportedCollisionException extends UnsupportedOperationException { diff --git a/engine/src/core/com/jme3/math/Matrix3f.java b/engine/src/core/com/jme3/math/Matrix3f.java index 3ab9d0abc..073b9f0a1 100644 --- a/engine/src/core/com/jme3/math/Matrix3f.java +++ b/engine/src/core/com/jme3/math/Matrix3f.java @@ -38,6 +38,7 @@ import com.jme3.export.JmeImporter; import com.jme3.export.OutputCapsule; import com.jme3.export.Savable; import com.jme3.util.BufferUtils; +import com.jme3.util.TempVars; import java.io.IOException; import java.nio.FloatBuffer; import java.util.logging.Logger; @@ -384,16 +385,49 @@ public final class Matrix3f implements Savable, Cloneable { * limit set is not changed). */ public FloatBuffer fillFloatBuffer(FloatBuffer fb, boolean columnMajor) { - if (columnMajor){ - fb.put(m00).put(m10).put(m20); - fb.put(m01).put(m11).put(m21); - fb.put(m02).put(m12).put(m22); +// if (columnMajor){ +// fb.put(m00).put(m10).put(m20); +// fb.put(m01).put(m11).put(m21); +// fb.put(m02).put(m12).put(m22); +// }else{ +// fb.put(m00).put(m01).put(m02); +// fb.put(m10).put(m11).put(m12); +// fb.put(m20).put(m21).put(m22); +// } + + TempVars vars = TempVars.get(); + assert vars.lock(); + + fillFloatArray(vars.matrixWrite, columnMajor); + fb.put(vars.matrixWrite, 0, 9); + + assert vars.unlock(); + + return fb; + } + + public void fillFloatArray(float[] f, boolean columnMajor){ + if (columnMajor) { + f[ 0] = m00; + f[ 1] = m10; + f[ 2] = m20; + f[ 3] = m01; + f[ 4] = m11; + f[ 5] = m21; + f[ 6] = m02; + f[ 7] = m12; + f[ 8] = m22; }else{ - fb.put(m00).put(m01).put(m02); - fb.put(m10).put(m11).put(m12); - fb.put(m20).put(m21).put(m22); + f[ 0] = m00; + f[ 1] = m01; + f[ 2] = m02; + f[ 3] = m10; + f[ 4] = m11; + f[ 5] = m12; + f[ 6] = m20; + f[ 7] = m21; + f[ 8] = m22; } - return fb; } /** diff --git a/engine/src/core/com/jme3/math/Matrix4f.java b/engine/src/core/com/jme3/math/Matrix4f.java index abd329d2b..fdd5ae468 100644 --- a/engine/src/core/com/jme3/math/Matrix4f.java +++ b/engine/src/core/com/jme3/math/Matrix4f.java @@ -733,17 +733,26 @@ public final class Matrix4f implements Savable, Cloneable { * limit set is not changed). */ public FloatBuffer fillFloatBuffer(FloatBuffer fb, boolean columnMajor) { - if (columnMajor) { - fb.put(m00).put(m10).put(m20).put(m30); - fb.put(m01).put(m11).put(m21).put(m31); - fb.put(m02).put(m12).put(m22).put(m32); - fb.put(m03).put(m13).put(m23).put(m33); - } else { - fb.put(m00).put(m01).put(m02).put(m03); - fb.put(m10).put(m11).put(m12).put(m13); - fb.put(m20).put(m21).put(m22).put(m23); - fb.put(m30).put(m31).put(m32).put(m33); - } +// if (columnMajor) { +// fb.put(m00).put(m10).put(m20).put(m30); +// fb.put(m01).put(m11).put(m21).put(m31); +// fb.put(m02).put(m12).put(m22).put(m32); +// fb.put(m03).put(m13).put(m23).put(m33); +// } else { +// fb.put(m00).put(m01).put(m02).put(m03); +// fb.put(m10).put(m11).put(m12).put(m13); +// fb.put(m20).put(m21).put(m22).put(m23); +// fb.put(m30).put(m31).put(m32).put(m33); +// } + + TempVars vars = TempVars.get(); + assert vars.lock(); + + fillFloatArray(vars.matrixWrite, columnMajor); + fb.put(vars.matrixWrite, 0, 16); + + assert vars.unlock(); + return fb; } diff --git a/engine/src/core/com/jme3/util/TempVars.java b/engine/src/core/com/jme3/util/TempVars.java index 7bc569c53..c96508c80 100644 --- a/engine/src/core/com/jme3/util/TempVars.java +++ b/engine/src/core/com/jme3/util/TempVars.java @@ -193,6 +193,8 @@ public class TempVars { */ public final Spatial[] spatialStack = new Spatial[32]; + public final float[] matrixWrite = new float[16]; + /** * BIHTree */