Implemented a new TempVars system, temp vars can now be requested without conflict.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7692 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 14 years ago
parent dfeff3c6af
commit 413f2b1e48
  1. 10
      engine/src/bullet/com/jme3/bullet/control/KinematicRagdollControl.java
  2. 3
      engine/src/bullet/com/jme3/bullet/util/DebugShapeFactory.java
  3. 4
      engine/src/core/com/jme3/animation/Bone.java
  4. 4
      engine/src/core/com/jme3/animation/Skeleton.java
  5. 4
      engine/src/core/com/jme3/animation/SkeletonControl.java
  6. 141
      engine/src/core/com/jme3/bounding/BoundingBox.java
  7. 64
      engine/src/core/com/jme3/bounding/BoundingSphere.java
  8. 50
      engine/src/core/com/jme3/bounding/Intersection.java
  9. 48
      engine/src/core/com/jme3/collision/bih/BIHNode.java
  10. 55
      engine/src/core/com/jme3/collision/bih/BIHTree.java
  11. 18
      engine/src/core/com/jme3/effect/ParticleEmitter.java
  12. 9
      engine/src/core/com/jme3/math/Line.java
  13. 12
      engine/src/core/com/jme3/math/LineSegment.java
  14. 202
      engine/src/core/com/jme3/math/Matrix3f.java
  15. 9
      engine/src/core/com/jme3/math/Matrix4f.java
  16. 3
      engine/src/core/com/jme3/math/Quaternion.java
  17. 32
      engine/src/core/com/jme3/math/Ray.java
  18. 21
      engine/src/core/com/jme3/renderer/Camera.java
  19. 5
      engine/src/core/com/jme3/renderer/RenderManager.java
  20. 33
      engine/src/core/com/jme3/scene/Geometry.java
  21. 16
      engine/src/core/com/jme3/scene/Spatial.java
  22. 3
      engine/src/core/com/jme3/scene/control/CameraControl.java
  23. 8
      engine/src/core/com/jme3/scene/control/LightControl.java
  24. 10
      engine/src/core/com/jme3/scene/debug/SkeletonDebugger.java
  25. 15
      engine/src/core/com/jme3/scene/shape/Dome.java
  26. 61
      engine/src/core/com/jme3/scene/shape/Sphere.java
  27. 151
      engine/src/core/com/jme3/util/BufferUtils.java
  28. 173
      engine/src/core/com/jme3/util/TempVars.java
  29. 5
      engine/src/desktop-fx/com/jme3/water/WaterFilter.java
  30. 12
      engine/src/jbullet/com/jme3/bullet/control/KinematicRagdollControl.java
  31. 8
      engine/src/jbullet/com/jme3/bullet/util/DebugShapeFactory.java
  32. 79
      engine/src/test/jme3test/app/TestTempVars.java

@ -165,7 +165,7 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
return; return;
} }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Quaternion tmpRot1 = vars.quat1; Quaternion tmpRot1 = vars.quat1;
Quaternion tmpRot2 = vars.quat2; Quaternion tmpRot2 = vars.quat2;
@ -264,7 +264,7 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
} }
} }
} }
assert vars.unlock(); vars.release();
} }
@ -682,7 +682,6 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
baseRigidBody.setKinematic(mode == Mode.Kinetmatic); baseRigidBody.setKinematic(mode == Mode.Kinetmatic);
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
for (PhysicsBoneLink link : boneLinks.values()) { for (PhysicsBoneLink link : boneLinks.values()) {
link.rigidBody.setKinematic(mode == Mode.Kinetmatic); link.rigidBody.setKinematic(mode == Mode.Kinetmatic);
if (mode == Mode.Ragdoll) { if (mode == Mode.Ragdoll) {
@ -693,7 +692,7 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
} }
} }
assert vars.unlock(); vars.release();
for (Bone bone : skeleton.getRoots()) { for (Bone bone : skeleton.getRoots()) {
RagdollUtils.setUserControl(bone, mode == Mode.Ragdoll); RagdollUtils.setUserControl(bone, mode == Mode.Ragdoll);
@ -718,7 +717,6 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
for (PhysicsBoneLink link : boneLinks.values()) { for (PhysicsBoneLink link : boneLinks.values()) {
Vector3f p = link.rigidBody.getMotionState().getWorldLocation(); Vector3f p = link.rigidBody.getMotionState().getWorldLocation();
@ -737,7 +735,7 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
link.startBlendingRot.set(q2); link.startBlendingRot.set(q2);
link.rigidBody.setKinematic(true); link.rigidBody.setKinematic(true);
} }
assert vars.unlock(); vars.release();
for (Bone bone : skeleton.getRoots()) { for (Bone bone : skeleton.getRoots()) {
RagdollUtils.setUserControl(bone, false); RagdollUtils.setUserControl(bone, false);

@ -80,14 +80,13 @@ public class DebugShapeFactory {
// apply rotation // apply rotation
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Matrix3f tempRot = vars.tempMat3; Matrix3f tempRot = vars.tempMat3;
tempRot.set(geometry.getLocalRotation()); tempRot.set(geometry.getLocalRotation());
childCollisionShape.rotation.mult(tempRot, tempRot); childCollisionShape.rotation.mult(tempRot, tempRot);
geometry.setLocalRotation(tempRot); geometry.setLocalRotation(tempRot);
assert vars.unlock(); vars.release();
node.attachChild(geometry); node.attachChild(geometry);
} }

@ -524,7 +524,7 @@ public final class Bone implements Savable {
} }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock(); // assert vars.lock();
Vector3f tmpV = vars.vect1; Vector3f tmpV = vars.vect1;
Vector3f tmpV2 = vars.vect2; Vector3f tmpV2 = vars.vect2;
@ -545,7 +545,7 @@ public final class Bone implements Savable {
} }
assert vars.unlock(); vars.release();
} }
/** /**

@ -54,7 +54,6 @@ public final class Skeleton implements Savable {
private Bone[] rootBones; private Bone[] rootBones;
private Bone[] boneList; private Bone[] boneList;
/** /**
* Contains the skinning matrices, multiplying it by a vertex effected by a bone * Contains the skinning matrices, multiplying it by a vertex effected by a bone
* will cause it to go to the animated position. * will cause it to go to the animated position.
@ -251,11 +250,10 @@ public final class Skeleton implements Savable {
*/ */
public Matrix4f[] computeSkinningMatrices() { public Matrix4f[] computeSkinningMatrices() {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
for (int i = 0; i < boneList.length; i++) { for (int i = 0; i < boneList.length; i++) {
boneList[i].getOffsetTransform(skinningMatrixes[i], vars.quat1, vars.vect1, vars.vect2, vars.tempMat3); boneList[i].getOffsetTransform(skinningMatrixes[i], vars.quat1, vars.vect1, vars.vect2, vars.tempMat3);
} }
assert vars.unlock(); vars.release();
return skinningMatrixes; return skinningMatrixes;
} }

@ -227,7 +227,7 @@ public class SkeletonControl extends AbstractControl implements Savable, Cloneab
int idxWeights = 0; int idxWeights = 0;
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
float[] posBuf = vars.skinPositions; float[] posBuf = vars.skinPositions;
float[] normBuf = vars.skinNormals; float[] normBuf = vars.skinNormals;
@ -284,7 +284,7 @@ public class SkeletonControl extends AbstractControl implements Savable, Cloneab
fnb.put(normBuf, 0, bufLength); fnb.put(normBuf, 0, bufLength);
} }
assert vars.unlock(); vars.release();
vb.updateData(fvb); vb.updateData(fvb);
nb.updateData(fnb); nb.updateData(fnb);

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.bounding; package com.jme3.bounding;
import com.jme3.collision.Collidable; import com.jme3.collision.Collidable;
@ -130,7 +129,7 @@ public class BoundingBox extends BoundingVolume {
} }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f min = vars.vect1.set(new Vector3f(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY)); Vector3f min = vars.vect1.set(new Vector3f(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
Vector3f max = vars.vect2.set(new Vector3f(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY)); Vector3f max = vars.vect2.set(new Vector3f(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));
@ -151,7 +150,7 @@ public class BoundingBox extends BoundingVolume {
yExtent = max.y - center.y; yExtent = max.y - center.y;
zExtent = max.z - center.z; zExtent = max.z - center.z;
assert vars.unlock(); vars.release();
} }
public void computeFromTris(int[] indices, Mesh mesh, int start, int end) { public void computeFromTris(int[] indices, Mesh mesh, int start, int end) {
@ -160,7 +159,7 @@ public class BoundingBox extends BoundingVolume {
} }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f vect1 = vars.vect1; Vector3f vect1 = vars.vect1;
Vector3f vect2 = vars.vect2; Vector3f vect2 = vars.vect2;
Triangle triangle = vars.triangle; Triangle triangle = vars.triangle;
@ -186,23 +185,29 @@ public class BoundingBox extends BoundingVolume {
yExtent = max.y - center.y; yExtent = max.y - center.y;
zExtent = max.z - center.z; zExtent = max.z - center.z;
assert vars.unlock(); vars.release();
} }
public static final void checkMinMax(Vector3f min, Vector3f max, Vector3f point) { public static final void checkMinMax(Vector3f min, Vector3f max, Vector3f point) {
if (point.x < min.x) if (point.x < min.x) {
min.x = point.x; min.x = point.x;
if (point.x > max.x) }
if (point.x > max.x) {
max.x = point.x; max.x = point.x;
if (point.y < min.y) }
if (point.y < min.y) {
min.y = point.y; min.y = point.y;
if (point.y > max.y) }
if (point.y > max.y) {
max.y = point.y; max.y = point.y;
if (point.z < min.z) }
if (point.z < min.z) {
min.z = point.z; min.z = point.z;
if (point.z > max.z) }
if (point.z > max.z) {
max.z = point.z; max.z = point.z;
} }
}
/** /**
* <code>containAABB</code> creates a minimum-volume axis-aligned bounding * <code>containAABB</code> creates a minimum-volume axis-aligned bounding
@ -213,15 +218,18 @@ public class BoundingBox extends BoundingVolume {
* the list of points. * the list of points.
*/ */
public void containAABB(FloatBuffer points) { public void containAABB(FloatBuffer points) {
if (points == null) if (points == null) {
return; return;
}
points.rewind(); points.rewind();
if (points.remaining() <= 2) // we need at least a 3 float vector if (points.remaining() <= 2) // we need at least a 3 float vector
{
return; return;
}
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
BufferUtils.populateFromBuffer(vars.vect1, points, 0); BufferUtils.populateFromBuffer(vars.vect1, points, 0);
float minX = vars.vect1.x, minY = vars.vect1.y, minZ = vars.vect1.z; float minX = vars.vect1.x, minY = vars.vect1.y, minZ = vars.vect1.z;
float maxX = vars.vect1.x, maxY = vars.vect1.y, maxZ = vars.vect1.z; float maxX = vars.vect1.x, maxY = vars.vect1.y, maxZ = vars.vect1.z;
@ -229,23 +237,26 @@ public class BoundingBox extends BoundingVolume {
for (int i = 1, len = points.remaining() / 3; i < len; i++) { for (int i = 1, len = points.remaining() / 3; i < len; i++) {
BufferUtils.populateFromBuffer(vars.vect1, points, i); BufferUtils.populateFromBuffer(vars.vect1, points, i);
if (vars.vect1.x < minX) if (vars.vect1.x < minX) {
minX = vars.vect1.x; minX = vars.vect1.x;
else if (vars.vect1.x > maxX) } else if (vars.vect1.x > maxX) {
maxX = vars.vect1.x; maxX = vars.vect1.x;
}
if (vars.vect1.y < minY) if (vars.vect1.y < minY) {
minY = vars.vect1.y; minY = vars.vect1.y;
else if (vars.vect1.y > maxY) } else if (vars.vect1.y > maxY) {
maxY = vars.vect1.y; maxY = vars.vect1.y;
}
if (vars.vect1.z < minZ) if (vars.vect1.z < minZ) {
minZ = vars.vect1.z; minZ = vars.vect1.z;
else if (vars.vect1.z > maxZ) } else if (vars.vect1.z > maxZ) {
maxZ = vars.vect1.z; maxZ = vars.vect1.z;
} }
}
assert vars.unlock(); vars.release();
center.set(minX + maxX, minY + maxY, minZ + maxZ); center.set(minX + maxX, minY + maxY, minZ + maxZ);
center.multLocal(0.5f); center.multLocal(0.5f);
@ -278,7 +289,7 @@ public class BoundingBox extends BoundingVolume {
box.center.addLocal(trans.getTranslation()); box.center.addLocal(trans.getTranslation());
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Matrix3f transMatrix = vars.tempMat3; Matrix3f transMatrix = vars.tempMat3;
transMatrix.set(trans.getRotation()); transMatrix.set(trans.getRotation());
// Make the rotation matrix all positive to get the maximum x/y/z extent // Make the rotation matrix all positive to get the maximum x/y/z extent
@ -292,7 +303,7 @@ public class BoundingBox extends BoundingVolume {
box.yExtent = FastMath.abs(vars.vect2.getY()); box.yExtent = FastMath.abs(vars.vect2.getY());
box.zExtent = FastMath.abs(vars.vect2.getZ()); box.zExtent = FastMath.abs(vars.vect2.getZ());
assert vars.unlock(); vars.release();
return box; return box;
} }
@ -305,7 +316,7 @@ public class BoundingBox extends BoundingVolume {
box = (BoundingBox) store; box = (BoundingBox) store;
} }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
float w = trans.multProj(center, box.center); float w = trans.multProj(center, box.center);
box.center.divideLocal(w); box.center.divideLocal(w);
@ -324,7 +335,7 @@ public class BoundingBox extends BoundingVolume {
box.yExtent = FastMath.abs(vars.vect1.getY()); box.yExtent = FastMath.abs(vars.vect1.getY());
box.zExtent = FastMath.abs(vars.vect1.getZ()); box.zExtent = FastMath.abs(vars.vect1.getZ());
assert vars.unlock(); vars.release();
return box; return box;
} }
@ -471,7 +482,6 @@ public class BoundingBox extends BoundingVolume {
// zExtent = max.z - center.z; // zExtent = max.z - center.z;
// return this; // return this;
// } // }
/** /**
* <code>merge</code> combines this bounding box with another box which is * <code>merge</code> combines this bounding box with another box which is
* defined by the center, x, y, z extents. * defined by the center, x, y, z extents.
@ -492,26 +502,32 @@ public class BoundingBox extends BoundingVolume {
float boxZ, BoundingBox rVal) { float boxZ, BoundingBox rVal) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
vars.vect1.x = center.x - xExtent; vars.vect1.x = center.x - xExtent;
if (vars.vect1.x > boxCenter.x - boxX) if (vars.vect1.x > boxCenter.x - boxX) {
vars.vect1.x = boxCenter.x - boxX; vars.vect1.x = boxCenter.x - boxX;
}
vars.vect1.y = center.y - yExtent; vars.vect1.y = center.y - yExtent;
if (vars.vect1.y > boxCenter.y - boxY) if (vars.vect1.y > boxCenter.y - boxY) {
vars.vect1.y = boxCenter.y - boxY; vars.vect1.y = boxCenter.y - boxY;
}
vars.vect1.z = center.z - zExtent; vars.vect1.z = center.z - zExtent;
if (vars.vect1.z > boxCenter.z - boxZ) if (vars.vect1.z > boxCenter.z - boxZ) {
vars.vect1.z = boxCenter.z - boxZ; vars.vect1.z = boxCenter.z - boxZ;
}
vars.vect2.x = center.x + xExtent; vars.vect2.x = center.x + xExtent;
if (vars.vect2.x < boxCenter.x + boxX) if (vars.vect2.x < boxCenter.x + boxX) {
vars.vect2.x = boxCenter.x + boxX; vars.vect2.x = boxCenter.x + boxX;
}
vars.vect2.y = center.y + yExtent; vars.vect2.y = center.y + yExtent;
if (vars.vect2.y < boxCenter.y + boxY) if (vars.vect2.y < boxCenter.y + boxY) {
vars.vect2.y = boxCenter.y + boxY; vars.vect2.y = boxCenter.y + boxY;
}
vars.vect2.z = center.z + zExtent; vars.vect2.z = center.z + zExtent;
if (vars.vect2.z < boxCenter.z + boxZ) if (vars.vect2.z < boxCenter.z + boxZ) {
vars.vect2.z = boxCenter.z + boxZ; vars.vect2.z = boxCenter.z + boxZ;
}
center.set(vars.vect2).addLocal(vars.vect1).multLocal(0.5f); center.set(vars.vect2).addLocal(vars.vect1).multLocal(0.5f);
@ -519,7 +535,7 @@ public class BoundingBox extends BoundingVolume {
yExtent = vars.vect2.y - center.y; yExtent = vars.vect2.y - center.y;
zExtent = vars.vect2.z - center.z; zExtent = vars.vect2.z - center.z;
assert vars.unlock(); vars.release();
return rVal; return rVal;
} }
@ -585,8 +601,9 @@ public class BoundingBox extends BoundingVolume {
&& FastMath.abs(center.y - bs.center.y) < bs.getRadius() && FastMath.abs(center.y - bs.center.y) < bs.getRadius()
+ yExtent + yExtent
&& FastMath.abs(center.z - bs.center.z) < bs.getRadius() && FastMath.abs(center.z - bs.center.z) < bs.getRadius()
+ zExtent) + zExtent) {
return true; return true;
}
return false; return false;
} }
@ -602,17 +619,18 @@ public class BoundingBox extends BoundingVolume {
assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bb.center); assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bb.center);
if (center.x + xExtent < bb.center.x - bb.xExtent if (center.x + xExtent < bb.center.x - bb.xExtent
|| center.x - xExtent > bb.center.x + bb.xExtent) || center.x - xExtent > bb.center.x + bb.xExtent) {
return false; return false;
else if (center.y + yExtent < bb.center.y - bb.yExtent } else if (center.y + yExtent < bb.center.y - bb.yExtent
|| center.y - yExtent > bb.center.y + bb.yExtent) || center.y - yExtent > bb.center.y + bb.yExtent) {
return false; return false;
else if (center.z + zExtent < bb.center.z - bb.zExtent } else if (center.z + zExtent < bb.center.z - bb.zExtent
|| center.z - zExtent > bb.center.z + bb.zExtent) || center.z - zExtent > bb.center.z + bb.zExtent) {
return false; return false;
else } else {
return true; return true;
} }
}
/** /**
* determines if this bounding box intersects with a given oriented bounding * determines if this bounding box intersects with a given oriented bounding
@ -623,7 +641,6 @@ public class BoundingBox extends BoundingVolume {
// public boolean intersectsOrientedBoundingBox(OrientedBoundingBox obb) { // public boolean intersectsOrientedBoundingBox(OrientedBoundingBox obb) {
// return obb.intersectsBoundingBox(this); // return obb.intersectsBoundingBox(this);
// } // }
/** /**
* determines if this bounding box intersects with a given ray object. If an * determines if this bounding box intersects with a given ray object. If an
* intersection has occurred, true is returned, otherwise false is returned. * intersection has occurred, true is returned, otherwise false is returned.
@ -636,7 +653,7 @@ public class BoundingBox extends BoundingVolume {
float rhs; float rhs;
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f diff = ray.origin.subtract(getCenter(vars.vect2), vars.vect1); Vector3f diff = ray.origin.subtract(getCenter(vars.vect2), vars.vect1);
final float[] fWdU = vars.fWdU; final float[] fWdU = vars.fWdU;
@ -650,7 +667,7 @@ public class BoundingBox extends BoundingVolume {
fDdU[0] = diff.dot(Vector3f.UNIT_X); fDdU[0] = diff.dot(Vector3f.UNIT_X);
fADdU[0] = FastMath.abs(fDdU[0]); fADdU[0] = FastMath.abs(fDdU[0]);
if (fADdU[0] > xExtent && fDdU[0] * fWdU[0] >= 0.0) { if (fADdU[0] > xExtent && fDdU[0] * fWdU[0] >= 0.0) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -659,7 +676,7 @@ public class BoundingBox extends BoundingVolume {
fDdU[1] = diff.dot(Vector3f.UNIT_Y); fDdU[1] = diff.dot(Vector3f.UNIT_Y);
fADdU[1] = FastMath.abs(fDdU[1]); fADdU[1] = FastMath.abs(fDdU[1]);
if (fADdU[1] > yExtent && fDdU[1] * fWdU[1] >= 0.0) { if (fADdU[1] > yExtent && fDdU[1] * fWdU[1] >= 0.0) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -668,7 +685,7 @@ public class BoundingBox extends BoundingVolume {
fDdU[2] = diff.dot(Vector3f.UNIT_Z); fDdU[2] = diff.dot(Vector3f.UNIT_Z);
fADdU[2] = FastMath.abs(fDdU[2]); fADdU[2] = FastMath.abs(fDdU[2]);
if (fADdU[2] > zExtent && fDdU[2] * fWdU[2] >= 0.0) { if (fADdU[2] > zExtent && fDdU[2] * fWdU[2] >= 0.0) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -677,25 +694,25 @@ public class BoundingBox extends BoundingVolume {
fAWxDdU[0] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_X)); fAWxDdU[0] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_X));
rhs = yExtent * fAWdU[2] + zExtent * fAWdU[1]; rhs = yExtent * fAWdU[2] + zExtent * fAWdU[1];
if (fAWxDdU[0] > rhs) { if (fAWxDdU[0] > rhs) {
assert vars.unlock(); vars.release();
return false; return false;
} }
fAWxDdU[1] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Y)); fAWxDdU[1] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Y));
rhs = xExtent * fAWdU[2] + zExtent * fAWdU[0]; rhs = xExtent * fAWdU[2] + zExtent * fAWdU[0];
if (fAWxDdU[1] > rhs) { if (fAWxDdU[1] > rhs) {
assert vars.unlock(); vars.release();
return false; return false;
} }
fAWxDdU[2] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Z)); fAWxDdU[2] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Z));
rhs = xExtent * fAWdU[1] + yExtent * fAWdU[0]; rhs = xExtent * fAWdU[1] + yExtent * fAWdU[0];
if (fAWxDdU[2] > rhs) { if (fAWxDdU[2] > rhs) {
assert vars.unlock(); vars.release();
return false; return false;
} }
assert vars.unlock(); vars.release();
return true; return true;
} }
@ -704,7 +721,7 @@ public class BoundingBox extends BoundingVolume {
*/ */
private int collideWithRay(Ray ray, CollisionResults results) { private int collideWithRay(Ray ray, CollisionResults results) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f diff = vars.vect1.set(ray.origin).subtractLocal(center); Vector3f diff = vars.vect1.set(ray.origin).subtractLocal(center);
Vector3f direction = vars.vect2.set(ray.direction); Vector3f direction = vars.vect2.set(ray.direction);
@ -717,7 +734,7 @@ public class BoundingBox extends BoundingVolume {
&& clip(-direction.y, +diff.y - yExtent, t) && clip(-direction.y, +diff.y - yExtent, t)
&& clip(+direction.z, -diff.z - zExtent, t) && clip(+direction.z, -diff.z - zExtent, t)
&& clip(-direction.z, +diff.z - zExtent, t); && clip(-direction.z, +diff.z - zExtent, t);
assert vars.unlock(); vars.release();
if (notEntirelyClipped && (t[0] != saveT0 || t[1] != saveT1)) { if (notEntirelyClipped && (t[0] != saveT0 || t[1] != saveT1)) {
if (t[1] > t[0]) { if (t[1] > t[0]) {
@ -844,16 +861,20 @@ public class BoundingBox extends BoundingVolume {
// plane. Otherwise 'false' is returned in which case the line segment // plane. Otherwise 'false' is returned in which case the line segment
// is entirely clipped. // is entirely clipped.
if (denom > 0.0f) { if (denom > 0.0f) {
if (numer > denom * t[1]) if (numer > denom * t[1]) {
return false; return false;
if (numer > denom * t[0]) }
if (numer > denom * t[0]) {
t[0] = numer / denom; t[0] = numer / denom;
}
return true; return true;
} else if (denom < 0.0f) { } else if (denom < 0.0f) {
if (numer > denom * t[0]) if (numer > denom * t[0]) {
return false; return false;
if (numer > denom * t[1]) }
if (numer > denom * t[1]) {
t[1] = numer / denom; t[1] = numer / denom;
}
return true; return true;
} else { } else {
return numer <= 0.0; return numer <= 0.0;
@ -888,22 +909,25 @@ public class BoundingBox extends BoundingVolume {
} }
public void setXExtent(float xExtent) { public void setXExtent(float xExtent) {
if (xExtent < 0) if (xExtent < 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
}
this.xExtent = xExtent; this.xExtent = xExtent;
} }
public void setYExtent(float yExtent) { public void setYExtent(float yExtent) {
if (yExtent < 0) if (yExtent < 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
}
this.yExtent = yExtent; this.yExtent = yExtent;
} }
public void setZExtent(float zExtent) { public void setZExtent(float zExtent) {
if (zExtent < 0) if (zExtent < 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
}
this.zExtent = zExtent; this.zExtent = zExtent;
} }
@ -954,4 +978,3 @@ public class BoundingBox extends BoundingVolume {
return (8 * xExtent * yExtent * zExtent); return (8 * xExtent * yExtent * zExtent);
} }
} }

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.bounding; package com.jme3.bounding;
import com.jme3.collision.Collidable; import com.jme3.collision.Collidable;
@ -69,9 +68,7 @@ public class BoundingSphere extends BoundingVolume {
private static final Logger logger = private static final Logger logger =
Logger.getLogger(BoundingSphere.class.getName()); Logger.getLogger(BoundingSphere.class.getName());
float radius; float radius;
private static final float RADIUS_EPSILON = 1f + 0.00001f; private static final float RADIUS_EPSILON = 1f + 0.00001f;
/** /**
@ -190,8 +187,9 @@ public class BoundingSphere extends BoundingVolume {
* The points to calculate the minimum bounds from. * The points to calculate the minimum bounds from.
*/ */
public void calcWelzl(FloatBuffer points) { public void calcWelzl(FloatBuffer points) {
if (center == null) if (center == null) {
center = new Vector3f(); center = new Vector3f();
}
FloatBuffer buf = BufferUtils.createFloatBuffer(points.limit()); FloatBuffer buf = BufferUtils.createFloatBuffer(points.limit());
points.rewind(); points.rewind();
buf.put(points); buf.put(points);
@ -216,7 +214,7 @@ public class BoundingSphere extends BoundingVolume {
*/ */
private void recurseMini(FloatBuffer points, int p, int b, int ap) { private void recurseMini(FloatBuffer points, int p, int b, int ap) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f tempA = vars.vect1; Vector3f tempA = vars.vect1;
Vector3f tempB = vars.vect2; Vector3f tempB = vars.vect2;
Vector3f tempC = vars.vect3; Vector3f tempC = vars.vect3;
@ -248,7 +246,7 @@ public class BoundingSphere extends BoundingVolume {
BufferUtils.populateFromBuffer(tempC, points, ap - 3); BufferUtils.populateFromBuffer(tempC, points, ap - 3);
BufferUtils.populateFromBuffer(tempD, points, ap - 4); BufferUtils.populateFromBuffer(tempD, points, ap - 4);
setSphere(tempA, tempB, tempC, tempD); setSphere(tempA, tempB, tempC, tempD);
assert vars.unlock(); vars.release();
return; return;
} }
for (int i = 0; i < p; i++) { for (int i = 0; i < p; i++) {
@ -260,12 +258,12 @@ public class BoundingSphere extends BoundingVolume {
BufferUtils.setInBuffer(tempC, points, j + ap); BufferUtils.setInBuffer(tempC, points, j + ap);
BufferUtils.setInBuffer(tempB, points, j - 1 + ap); BufferUtils.setInBuffer(tempB, points, j - 1 + ap);
} }
assert vars.unlock(); vars.release();
recurseMini(points, i, b + 1, ap + 1); recurseMini(points, i, b + 1, ap + 1);
assert vars.lock();
} }
} }
assert vars.unlock(); vars.release();
} }
/** /**
@ -327,9 +325,7 @@ public class BoundingSphere extends BoundingVolume {
radius = 0; radius = 0;
} else { } else {
Vector3f o = acrossB.cross(a).multLocal(b.lengthSquared()) Vector3f o = acrossB.cross(a).multLocal(b.lengthSquared()).addLocal(b.cross(acrossB).multLocal(a.lengthSquared())).divideLocal(Denominator);
.addLocal(b.cross(acrossB).multLocal(a.lengthSquared()))
.divideLocal(Denominator);
radius = o.length() * RADIUS_EPSILON; radius = o.length() * RADIUS_EPSILON;
O.add(o, center); O.add(o, center);
} }
@ -431,13 +427,15 @@ public class BoundingSphere extends BoundingVolume {
float z = FastMath.abs(scale.z); float z = FastMath.abs(scale.z);
if (x >= y) { if (x >= y) {
if (x >= z) if (x >= z) {
return x; return x;
}
return z; return z;
} }
if (y >= z) if (y >= z) {
return y; return y;
}
return z; return z;
} }
@ -531,12 +529,12 @@ public class BoundingSphere extends BoundingVolume {
case AABB: { case AABB: {
BoundingBox box = (BoundingBox) volume; BoundingBox box = (BoundingBox) volume;
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector3f radVect = TempVars.get().vect1; Vector3f radVect = vars.vect1;
radVect.set(box.xExtent, box.yExtent, box.zExtent); radVect.set(box.xExtent, box.yExtent, box.zExtent);
Vector3f temp_center = box.center; Vector3f temp_center = box.center;
float len = radVect.length(); float len = radVect.length();
assert TempVars.get().unlock(); vars.release();
return merge(len, temp_center, this); return merge(len, temp_center, this);
} }
@ -585,11 +583,10 @@ public class BoundingSphere extends BoundingVolume {
// //
// return this; // return this;
// } // }
private BoundingVolume merge(float temp_radius, Vector3f temp_center, private BoundingVolume merge(float temp_radius, Vector3f temp_center,
BoundingSphere rVal) { BoundingSphere rVal) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f diff = temp_center.subtract(center, vars.vect1); Vector3f diff = temp_center.subtract(center, vars.vect1);
float lengthSquared = diff.lengthSquared(); float lengthSquared = diff.lengthSquared();
float radiusDiff = temp_radius - radius; float radiusDiff = temp_radius - radius;
@ -598,7 +595,7 @@ public class BoundingSphere extends BoundingVolume {
if (fRDiffSqr >= lengthSquared) { if (fRDiffSqr >= lengthSquared) {
if (radiusDiff <= 0.0f) { if (radiusDiff <= 0.0f) {
assert vars.unlock(); vars.release();
return this; return this;
} }
@ -608,7 +605,7 @@ public class BoundingSphere extends BoundingVolume {
} }
rCenter.set(temp_center); rCenter.set(temp_center);
rVal.setRadius(temp_radius); rVal.setRadius(temp_radius);
assert vars.unlock(); vars.release();
return rVal; return rVal;
} }
@ -626,7 +623,7 @@ public class BoundingSphere extends BoundingVolume {
} }
rVal.setRadius(0.5f * (length + radius + temp_radius)); rVal.setRadius(0.5f * (length + radius + temp_radius));
assert vars.unlock(); vars.release();
return rVal; return rVal;
} }
@ -685,11 +682,11 @@ public class BoundingSphere extends BoundingVolume {
assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bs.center); assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bs.center);
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f diff = center.subtract(bs.center, vars.vect1); Vector3f diff = center.subtract(bs.center, vars.vect1);
float rsum = getRadius() + bs.getRadius(); float rsum = getRadius() + bs.getRadius();
boolean eq = (diff.dot(diff) <= rsum * rsum); boolean eq = (diff.dot(diff) <= rsum * rsum);
assert vars.unlock(); vars.release();
return eq; return eq;
} }
@ -706,8 +703,9 @@ public class BoundingSphere extends BoundingVolume {
&& FastMath.abs(bb.center.y - center.y) < getRadius() && FastMath.abs(bb.center.y - center.y) < getRadius()
+ bb.yExtent + bb.yExtent
&& FastMath.abs(bb.center.z - center.z) < getRadius() && FastMath.abs(bb.center.z - center.z) < getRadius()
+ bb.zExtent) + bb.zExtent) {
return true; return true;
}
return false; return false;
} }
@ -730,9 +728,8 @@ public class BoundingSphere extends BoundingVolume {
assert Vector3f.isValidVector(center); assert Vector3f.isValidVector(center);
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f diff = vars.vect1.set(ray.getOrigin()) Vector3f diff = vars.vect1.set(ray.getOrigin()).subtractLocal(center);
.subtractLocal(center);
float radiusSquared = getRadius() * getRadius(); float radiusSquared = getRadius() * getRadius();
float a = diff.dot(diff) - radiusSquared; float a = diff.dot(diff) - radiusSquared;
if (a <= 0.0) { if (a <= 0.0) {
@ -742,7 +739,7 @@ public class BoundingSphere extends BoundingVolume {
// outside sphere // outside sphere
float b = ray.getDirection().dot(diff); float b = ray.getDirection().dot(diff);
assert vars.unlock(); vars.release();
if (b >= 0.0) { if (b >= 0.0) {
return false; return false;
} }
@ -756,7 +753,7 @@ public class BoundingSphere extends BoundingVolume {
*/ */
public int collideWithRay(Ray ray, CollisionResults results) { public int collideWithRay(Ray ray, CollisionResults results) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f diff = vars.vect1.set(ray.getOrigin()).subtractLocal( Vector3f diff = vars.vect1.set(ray.getOrigin()).subtractLocal(
center); center);
float a = diff.dot(diff) - (getRadius() * getRadius()); float a = diff.dot(diff) - (getRadius() * getRadius());
@ -776,16 +773,15 @@ public class BoundingSphere extends BoundingVolume {
} }
a1 = ray.direction.dot(diff); a1 = ray.direction.dot(diff);
assert vars.unlock(); vars.release();
if (a1 >= 0.0) { if (a1 >= 0.0) {
return 0; return 0;
} }
discr = a1 * a1 - a; discr = a1 * a1 - a;
if (discr < 0.0) if (discr < 0.0) {
return 0; return 0;
} else if (discr >= FastMath.ZERO_TOLERANCE) {
else if (discr >= FastMath.ZERO_TOLERANCE) {
root = FastMath.sqrt(discr); root = FastMath.sqrt(discr);
float dist = -a1 - root; float dist = -a1 - root;
Vector3f point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin); Vector3f point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin);

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.bounding; package com.jme3.bounding;
import com.jme3.util.TempVars; import com.jme3.util.TempVars;
@ -49,14 +48,21 @@ public class Intersection {
private static final void findMinMax(float x0, float x1, float x2, Vector3f minMax) { private static final void findMinMax(float x0, float x1, float x2, Vector3f minMax) {
minMax.set(x0, x0, 0); minMax.set(x0, x0, 0);
if (x1 < minMax.x) minMax.setX(x1); if (x1 < minMax.x) {
if (x1 > minMax.y) minMax.setY(x1); minMax.setX(x1);
if (x2 < minMax.x) minMax.setX(x2); }
if (x2 > minMax.y) minMax.setY(x2); if (x1 > minMax.y) {
minMax.setY(x1);
}
if (x2 < minMax.x) {
minMax.setX(x2);
}
if (x2 > minMax.y) {
minMax.setY(x2);
}
} }
// private boolean axisTest(float a, float b, float fa, float fb, Vector3f v0, Vector3f v1, ) // private boolean axisTest(float a, float b, float fa, float fb, Vector3f v0, Vector3f v1, )
// private boolean axisTestX01(float a, float b, float fa, float fb, // private boolean axisTestX01(float a, float b, float fa, float fb,
// Vector3f center, Vector3f ext, // Vector3f center, Vector3f ext,
// Vector3f v1, Vector3f v2, Vector3f v3){ // Vector3f v1, Vector3f v2, Vector3f v3){
@ -73,7 +79,6 @@ public class Intersection {
// if(min > rad || max < -rad) // if(min > rad || max < -rad)
// return false; // return false;
// } // }
public static boolean intersect(BoundingBox bbox, Vector3f v1, Vector3f v2, Vector3f v3) { public static boolean intersect(BoundingBox bbox, Vector3f v1, Vector3f v2, Vector3f v3) {
// use separating axis theorem to test overlap between triangle and box // use separating axis theorem to test overlap between triangle and box
// need to test for overlap in these directions: // need to test for overlap in these directions:
@ -84,7 +89,7 @@ public class Intersection {
// this gives 3x3=9 more tests // this gives 3x3=9 more tests
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f tmp0 = vars.vect1, Vector3f tmp0 = vars.vect1,
tmp1 = vars.vect2, tmp1 = vars.vect2,
@ -128,7 +133,7 @@ public class Intersection {
max = max(p0, p2); max = max(p0, p2);
rad = fez * extent.y + fey * extent.z; rad = fez * extent.y + fey * extent.z;
if (min > rad || max < -rad) { if (min > rad || max < -rad) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -139,7 +144,7 @@ public class Intersection {
max = max(p0, p2); max = max(p0, p2);
rad = fez * extent.x + fex * extent.z; rad = fez * extent.x + fex * extent.z;
if (min > rad || max < -rad) { if (min > rad || max < -rad) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -150,7 +155,7 @@ public class Intersection {
max = max(p1, p2); max = max(p1, p2);
rad = fey * extent.x + fex * extent.y; rad = fey * extent.x + fex * extent.y;
if (min > rad || max < -rad) { if (min > rad || max < -rad) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -165,7 +170,7 @@ public class Intersection {
max = max(p0, p2); max = max(p0, p2);
rad = fez * extent.y + fey * extent.z; rad = fez * extent.y + fey * extent.z;
if (min > rad || max < -rad) { if (min > rad || max < -rad) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -176,7 +181,7 @@ public class Intersection {
max = max(p0, p2); max = max(p0, p2);
rad = fez * extent.x + fex * extent.z; rad = fez * extent.x + fex * extent.z;
if (min > rad || max < -rad) { if (min > rad || max < -rad) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -187,7 +192,7 @@ public class Intersection {
max = max(p0, p1); max = max(p0, p1);
rad = fey * extent.x + fex * extent.y; rad = fey * extent.x + fex * extent.y;
if (min > rad || max < -rad) { if (min > rad || max < -rad) {
assert vars.unlock(); vars.release();
return false; return false;
} }
// //
@ -202,7 +207,7 @@ public class Intersection {
max = max(p0, p1); max = max(p0, p1);
rad = fez * extent.y + fey * extent.z; rad = fez * extent.y + fey * extent.z;
if (min > rad || max < -rad) { if (min > rad || max < -rad) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -213,7 +218,7 @@ public class Intersection {
max = max(p0, p1); max = max(p0, p1);
rad = fez * extent.x + fex * extent.y; rad = fez * extent.x + fex * extent.y;
if (min > rad || max < -rad) { if (min > rad || max < -rad) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -224,7 +229,7 @@ public class Intersection {
max = max(p1, p2); max = max(p1, p2);
rad = fey * extent.x + fex * extent.y; rad = fey * extent.x + fex * extent.y;
if (min > rad || max < -rad) { if (min > rad || max < -rad) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -240,21 +245,21 @@ public class Intersection {
// test in X-direction // test in X-direction
findMinMax(tmp0.x, tmp1.x, tmp2.x, minMax); findMinMax(tmp0.x, tmp1.x, tmp2.x, minMax);
if (minMax.x > extent.x || minMax.y < -extent.x) { if (minMax.x > extent.x || minMax.y < -extent.x) {
assert vars.unlock(); vars.release();
return false; return false;
} }
// test in Y-direction // test in Y-direction
findMinMax(tmp0.y, tmp1.y, tmp2.y, minMax); findMinMax(tmp0.y, tmp1.y, tmp2.y, minMax);
if (minMax.x > extent.y || minMax.y < -extent.y) { if (minMax.x > extent.y || minMax.y < -extent.y) {
assert vars.unlock(); vars.release();
return false; return false;
} }
// test in Z-direction // test in Z-direction
findMinMax(tmp0.z, tmp1.z, tmp2.z, minMax); findMinMax(tmp0.z, tmp1.z, tmp2.z, minMax);
if (minMax.x > extent.z || minMax.y < -extent.z) { if (minMax.x > extent.z || minMax.y < -extent.z) {
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -267,15 +272,14 @@ public class Intersection {
p.setPlanePoints(v1, v2, v3); p.setPlanePoints(v1, v2, v3);
if (bbox.whichSide(p) == Plane.Side.Negative) { if (bbox.whichSide(p) == Plane.Side.Negative) {
assert vars.unlock(); vars.release();
return false; return false;
} }
// //
// if(!planeBoxOverlap(normal,v0,boxhalfsize)) return false; // if(!planeBoxOverlap(normal,v0,boxhalfsize)) return false;
assert vars.unlock(); vars.release();
return true; /* box and triangle overlaps */ return true; /* box and triangle overlaps */
} }
} }

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.collision.bih; package com.jme3.collision.bih;
import com.jme3.bounding.BoundingBox; import com.jme3.bounding.BoundingBox;
@ -62,7 +61,6 @@ import static java.lang.Math.max;
public final class BIHNode implements Savable { public final class BIHNode implements Savable {
private int leftIndex, rightIndex; private int leftIndex, rightIndex;
private BIHNode left; private BIHNode left;
private BIHNode right; private BIHNode right;
private float leftPlane; private float leftPlane;
@ -146,7 +144,6 @@ public final class BIHNode implements Savable {
this.min = min; this.min = min;
this.max = max; this.max = max;
} }
} }
public final int intersectWhere(Collidable col, public final int intersectWhere(Collidable col,
@ -155,7 +152,8 @@ public final class BIHNode implements Savable {
BIHTree tree, BIHTree tree,
CollisionResults results) { CollisionResults results) {
ArrayList<BIHStackData> stack = TempVars.get().bihStack; TempVars vars = TempVars.get();
ArrayList<BIHStackData> stack = vars.bihStack;
stack.clear(); stack.clear();
float[] minExts = {box.getCenter().x - box.getXExtent(), float[] minExts = {box.getCenter().x - box.getXExtent(),
@ -171,7 +169,8 @@ public final class BIHNode implements Savable {
Triangle t = new Triangle(); Triangle t = new Triangle();
int cols = 0; int cols = 0;
stackloop: while (stack.size() > 0){ stackloop:
while (stack.size() > 0) {
BIHNode node = stack.remove(stack.size() - 1).node; BIHNode node = stack.remove(stack.size() - 1).node;
while (node.axis != 3) { while (node.axis != 3) {
@ -184,9 +183,10 @@ public final class BIHNode implements Savable {
// means there's a gap in the middle // means there's a gap in the middle
// if the box is in that gap, we stop there // if the box is in that gap, we stop there
if (minExt > node.leftPlane if (minExt > node.leftPlane
&& maxExt < node.rightPlane) && maxExt < node.rightPlane) {
continue stackloop; continue stackloop;
} }
}
if (maxExt < node.rightPlane) { if (maxExt < node.rightPlane) {
node = node.left; node = node.left;
@ -230,7 +230,7 @@ public final class BIHNode implements Savable {
} }
} }
} }
vars.release();
return cols; return cols;
} }
@ -248,15 +248,18 @@ public final class BIHNode implements Savable {
int cols = 0; int cols = 0;
ArrayList<BIHStackData> stack = TempVars.get().bihStack; TempVars vars = TempVars.get();
ArrayList<BIHStackData> stack = vars.bihStack;
stack.clear(); stack.clear();
stack.add(new BIHStackData(this, 0, 0)); stack.add(new BIHStackData(this, 0, 0));
stackloop: while (stack.size() > 0){ stackloop:
while (stack.size() > 0) {
BIHStackData data = stack.remove(stack.size() - 1); BIHStackData data = stack.remove(stack.size() - 1);
BIHNode node = data.node; BIHNode node = data.node;
leafloop: while (node.axis != 3){ // while node is not a leaf leafloop:
while (node.axis != 3) { // while node is not a leaf
BIHNode nearNode, farNode; BIHNode nearNode, farNode;
nearNode = node.left; nearNode = node.left;
farNode = node.right; farNode = node.right;
@ -278,9 +281,7 @@ public final class BIHNode implements Savable {
float t = r.intersects(v1, v2, v3); float t = r.intersects(v1, v2, v3);
if (t < tHit) { if (t < tHit) {
tHit = t; tHit = t;
Vector3f contactPoint = new Vector3f(r.direction) Vector3f contactPoint = new Vector3f(r.direction).multLocal(tHit).addLocal(r.origin);
.multLocal(tHit)
.addLocal(r.origin);
CollisionResult cr = new CollisionResult(contactPoint, tHit); CollisionResult cr = new CollisionResult(contactPoint, tHit);
cr.setTriangleIndex(tree.getTriangleIndex(i)); cr.setTriangleIndex(tree.getTriangleIndex(i));
results.addCollision(cr); results.addCollision(cr);
@ -288,7 +289,7 @@ public final class BIHNode implements Savable {
} }
} }
} }
vars.release();
return cols; return cols;
} }
@ -299,7 +300,8 @@ public final class BIHNode implements Savable {
float sceneMax, float sceneMax,
CollisionResults results) { CollisionResults results) {
ArrayList<BIHStackData> stack = TempVars.get().bihStack; TempVars vars = TempVars.get();
ArrayList<BIHStackData> stack = vars.bihStack;
stack.clear(); stack.clear();
// float tHit = Float.POSITIVE_INFINITY; // float tHit = Float.POSITIVE_INFINITY;
@ -331,17 +333,20 @@ public final class BIHNode implements Savable {
int cols = 0; int cols = 0;
stack.add(new BIHStackData(this, sceneMin, sceneMax)); stack.add(new BIHStackData(this, sceneMin, sceneMax));
stackloop: while (stack.size() > 0){ stackloop:
while (stack.size() > 0) {
BIHStackData data = stack.remove(stack.size() - 1); BIHStackData data = stack.remove(stack.size() - 1);
BIHNode node = data.node; BIHNode node = data.node;
float tMin = data.min, float tMin = data.min,
tMax = data.max; tMax = data.max;
if (tMax < tMin) if (tMax < tMin) {
continue; continue;
}
leafloop: while (node.axis != 3){ // while node is not a leaf leafloop:
while (node.axis != 3) { // while node is not a leaf
int a = node.axis; int a = node.axis;
// find the origin and direction value for the given axis // find the origin and direction value for the given axis
@ -405,9 +410,7 @@ public final class BIHNode implements Savable {
} }
Vector3f contactNormal = Triangle.computeTriangleNormal(v1, v2, v3, null); Vector3f contactNormal = Triangle.computeTriangleNormal(v1, v2, v3, null);
Vector3f contactPoint = new Vector3f(d) Vector3f contactPoint = new Vector3f(d).multLocal(t).addLocal(o);
.multLocal(t)
.addLocal(o);
float worldSpaceDist = o.distance(contactPoint); float worldSpaceDist = o.distance(contactPoint);
CollisionResult cr = new CollisionResult(contactPoint, worldSpaceDist); CollisionResult cr = new CollisionResult(contactPoint, worldSpaceDist);
@ -418,11 +421,10 @@ public final class BIHNode implements Savable {
} }
} }
} }
vars.release();
r.setOrigin(o); r.setOrigin(o);
r.setDirection(d); r.setDirection(d);
return cols; return cols;
} }
} }

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.collision.bih; package com.jme3.collision.bih;
import com.jme3.scene.mesh.VirtualIndexBuffer; import com.jme3.scene.mesh.VirtualIndexBuffer;
@ -64,18 +63,14 @@ public class BIHTree implements CollisionData {
public static final int MAX_TREE_DEPTH = 100; public static final int MAX_TREE_DEPTH = 100;
public static final int MAX_TRIS_PER_NODE = 21; public static final int MAX_TRIS_PER_NODE = 21;
private Mesh mesh; private Mesh mesh;
private BIHNode root; private BIHNode root;
private int maxTrisPerNode; private int maxTrisPerNode;
private int numTris; private int numTris;
private float[] pointData; private float[] pointData;
private int[] triIndices; private int[] triIndices;
private transient CollisionResults boundResults = new CollisionResults(); private transient CollisionResults boundResults = new CollisionResults();
private transient float[] bihSwapTmp; private transient float[] bihSwapTmp;
private static final TriangleAxisComparator[] comparators = new TriangleAxisComparator[3]; private static final TriangleAxisComparator[] comparators = new TriangleAxisComparator[3];
static { static {
@ -105,16 +100,18 @@ public class BIHTree implements CollisionData {
} }
triIndices = new int[numTris]; triIndices = new int[numTris];
for (int i = 0; i < numTris; i++) for (int i = 0; i < numTris; i++) {
triIndices[i] = i; triIndices[i] = i;
} }
}
public BIHTree(Mesh mesh, int maxTrisPerNode) { public BIHTree(Mesh mesh, int maxTrisPerNode) {
this.mesh = mesh; this.mesh = mesh;
this.maxTrisPerNode = maxTrisPerNode; this.maxTrisPerNode = maxTrisPerNode;
if (maxTrisPerNode < 1 || mesh == null) if (maxTrisPerNode < 1 || mesh == null) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
}
bihSwapTmp = new float[9]; bihSwapTmp = new float[9];
@ -144,7 +141,7 @@ public class BIHTree implements CollisionData {
private BoundingBox createBox(int l, int r) { private BoundingBox createBox(int l, int r) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f min = vars.vect1.set(new Vector3f(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY)); Vector3f min = vars.vect1.set(new Vector3f(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
Vector3f max = vars.vect2.set(new Vector3f(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY)); Vector3f max = vars.vect2.set(new Vector3f(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));
@ -160,7 +157,7 @@ public class BIHTree implements CollisionData {
} }
BoundingBox bbox = new BoundingBox(min, max); BoundingBox bbox = new BoundingBox(min, max);
assert vars.unlock(); vars.release();
return bbox; return bbox;
} }
@ -173,7 +170,7 @@ public class BIHTree implements CollisionData {
int j = r; int j = r;
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f v1 = vars.vect1, Vector3f v1 = vars.vect1,
v2 = vars.vect2, v2 = vars.vect2,
v3 = vars.vect3; v3 = vars.vect3;
@ -189,7 +186,7 @@ public class BIHTree implements CollisionData {
} }
} }
assert vars.unlock(); vars.release();
pivot = (pivot == l && j < pivot) ? j : pivot; pivot = (pivot == l && j < pivot) ? j : pivot;
return pivot; return pivot;
} }
@ -198,20 +195,22 @@ public class BIHTree implements CollisionData {
Vector3f min = bbox.getMin(null); Vector3f min = bbox.getMin(null);
Vector3f max = bbox.getMax(null); Vector3f max = bbox.getMax(null);
if (doMin) if (doMin) {
min.set(axis, value); min.set(axis, value);
else } else {
max.set(axis, value); max.set(axis, value);
}
bbox.setMinMax(min, max); bbox.setMinMax(min, max);
} }
private float getMinMax(BoundingBox bbox, boolean doMin, int axis) { private float getMinMax(BoundingBox bbox, boolean doMin, int axis) {
if (doMin) if (doMin) {
return bbox.getMin(null).get(axis); return bbox.getMin(null).get(axis);
else } else {
return bbox.getMax(null).get(axis); return bbox.getMax(null).get(axis);
} }
}
// private BIHNode createNode2(int l, int r, BoundingBox nodeBbox, int depth){ // private BIHNode createNode2(int l, int r, BoundingBox nodeBbox, int depth){
// if ((r - l) < maxTrisPerNode || depth > 100) // if ((r - l) < maxTrisPerNode || depth > 100)
@ -290,7 +289,6 @@ public class BIHTree implements CollisionData {
// //
// return node; // return node;
// } // }
private BIHNode createNode(int l, int r, BoundingBox nodeBbox, int depth) { private BIHNode createNode(int l, int r, BoundingBox nodeBbox, int depth) {
if ((r - l) < maxTrisPerNode || depth > MAX_TREE_DEPTH) { if ((r - l) < maxTrisPerNode || depth > MAX_TREE_DEPTH) {
return new BIHNode(l, r); return new BIHNode(l, r);
@ -304,24 +302,28 @@ public class BIHTree implements CollisionData {
int axis = 0; int axis = 0;
if (exteriorExt.x > exteriorExt.y) { if (exteriorExt.x > exteriorExt.y) {
if (exteriorExt.x > exteriorExt.z) if (exteriorExt.x > exteriorExt.z) {
axis = 0; axis = 0;
else } else {
axis = 2; axis = 2;
}
} else { } else {
if (exteriorExt.y > exteriorExt.z) if (exteriorExt.y > exteriorExt.z) {
axis = 1; axis = 1;
else } else {
axis = 2; axis = 2;
} }
if (exteriorExt.equals(Vector3f.ZERO)) }
if (exteriorExt.equals(Vector3f.ZERO)) {
axis = 0; axis = 0;
}
// Arrays.sort(tris, l, r, comparators[axis]); // Arrays.sort(tris, l, r, comparators[axis]);
float split = currentBox.getCenter().get(axis); float split = currentBox.getCenter().get(axis);
int pivot = sortTriangles(l, r, split, axis); int pivot = sortTriangles(l, r, split, axis);
if (pivot == l || pivot == r) if (pivot == l || pivot == r) {
pivot = (r + l) / 2; pivot = (r + l) / 2;
}
//If one of the partitions is empty, continue with recursion: same level but different bbox //If one of the partitions is empty, continue with recursion: same level but different bbox
if (pivot < l) { if (pivot < l) {
@ -403,13 +405,15 @@ public class BIHTree implements CollisionData {
float tMin = boundResults.getClosestCollision().getDistance(); float tMin = boundResults.getClosestCollision().getDistance();
float tMax = boundResults.getFarthestCollision().getDistance(); float tMax = boundResults.getFarthestCollision().getDistance();
if (tMax <= 0) if (tMax <= 0) {
tMax = Float.POSITIVE_INFINITY; tMax = Float.POSITIVE_INFINITY;
else if (tMin == tMax) } else if (tMin == tMax) {
tMin = 0; tMin = 0;
}
if (tMin <= 0) if (tMin <= 0) {
tMin = 0; tMin = 0;
}
if (r.getLimit() < Float.POSITIVE_INFINITY) { if (r.getLimit() < Float.POSITIVE_INFINITY) {
tMax = Math.min(tMax, r.getLimit()); tMax = Math.min(tMax, r.getLimit());
@ -473,5 +477,4 @@ public class BIHTree implements CollisionData {
pointData = ic.readFloatArray("points", null); pointData = ic.readFloatArray("points", null);
triIndices = ic.readIntArray("indices", null); triIndices = ic.readIntArray("indices", null);
} }
} }

@ -75,7 +75,6 @@ public class ParticleEmitter extends Geometry {
private static final EmitterShape DEFAULT_SHAPE = new EmitterPointShape(Vector3f.ZERO); private static final EmitterShape DEFAULT_SHAPE = new EmitterPointShape(Vector3f.ZERO);
private static final ParticleInfluencer DEFAULT_INFLUENCER = new DefaultParticleInfluencer(); private static final ParticleInfluencer DEFAULT_INFLUENCER = new DefaultParticleInfluencer();
private ParticleEmitterControl control = new ParticleEmitterControl(); private ParticleEmitterControl control = new ParticleEmitterControl();
private EmitterShape shape = DEFAULT_SHAPE; private EmitterShape shape = DEFAULT_SHAPE;
private ParticleMesh particleMesh; private ParticleMesh particleMesh;
@ -139,7 +138,6 @@ public class ParticleEmitter extends Geometry {
public void read(JmeImporter im) throws IOException { public void read(JmeImporter im) throws IOException {
// the data is not written here // the data is not written here
} }
} }
@Override @Override
@ -199,7 +197,6 @@ public class ParticleEmitter extends Geometry {
this.shape = shape; this.shape = shape;
} }
public EmitterShape getShape() { public EmitterShape getShape() {
return shape; return shape;
} }
@ -838,7 +835,7 @@ public class ParticleEmitter extends Geometry {
this.getWorldTransform(); this.getWorldTransform();
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
BoundingBox bbox = (BoundingBox) this.getMesh().getBound(); BoundingBox bbox = (BoundingBox) this.getMesh().getBound();
@ -862,7 +859,7 @@ public class ParticleEmitter extends Geometry {
bbox.setMinMax(min, max); bbox.setMinMax(min, max);
this.setBoundRefresh(); this.setBoundRefresh();
assert vars.unlock(); vars.release();
} }
/** /**
@ -909,7 +906,7 @@ public class ParticleEmitter extends Geometry {
this.getWorldTransform(); this.getWorldTransform();
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f min = vars.vect1.set(Vector3f.POSITIVE_INFINITY); Vector3f min = vars.vect1.set(Vector3f.POSITIVE_INFINITY);
Vector3f max = vars.vect2.set(Vector3f.NEGATIVE_INFINITY); Vector3f max = vars.vect2.set(Vector3f.NEGATIVE_INFINITY);
@ -979,7 +976,7 @@ public class ParticleEmitter extends Geometry {
bbox.setMinMax(min, max); bbox.setMinMax(min, max);
this.setBoundRefresh(); this.setBoundRefresh();
assert vars.unlock(); vars.release();
} }
/** /**
@ -1033,14 +1030,15 @@ public class ParticleEmitter extends Geometry {
} }
Matrix3f inverseRotation = Matrix3f.IDENTITY; Matrix3f inverseRotation = Matrix3f.IDENTITY;
TempVars vars = null;
if (!worldSpace) { if (!worldSpace) {
TempVars vars = TempVars.get(); vars = TempVars.get();
assert vars.lock();
inverseRotation = this.getWorldRotation().toRotationMatrix(vars.tempMat3).invertLocal(); inverseRotation = this.getWorldRotation().toRotationMatrix(vars.tempMat3).invertLocal();
} }
particleMesh.updateParticleData(particles, cam, inverseRotation); particleMesh.updateParticleData(particles, cam, inverseRotation);
if (!worldSpace) { if (!worldSpace) {
assert TempVars.get().unlock(); vars.release();
} }
} }

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.math; package com.jme3.math;
import com.jme3.export.InputCapsule; import com.jme3.export.InputCapsule;
@ -113,7 +112,7 @@ public class Line implements Savable, Cloneable {
public float distanceSquared(Vector3f point) { public float distanceSquared(Vector3f point) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f compVec1 = vars.vect1; Vector3f compVec1 = vars.vect1;
Vector3f compVec2 = vars.vect2; Vector3f compVec2 = vars.vect2;
@ -122,7 +121,7 @@ public class Line implements Savable, Cloneable {
origin.add(direction.mult(lineParameter, compVec2), compVec2); origin.add(direction.mult(lineParameter, compVec2), compVec2);
compVec2.subtract(point, compVec1); compVec2.subtract(point, compVec1);
float len = compVec1.lengthSquared(); float len = compVec1.lengthSquared();
assert vars.unlock(); vars.release();
return len; return len;
} }
@ -136,7 +135,7 @@ public class Line implements Savable, Cloneable {
} }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f compVec1 = vars.vect1; Vector3f compVec1 = vars.vect1;
Vector3f compVec2 = vars.vect2; Vector3f compVec2 = vars.vect2;
Matrix3f compMat1 = vars.tempMat3; Matrix3f compMat1 = vars.tempMat3;
@ -185,7 +184,7 @@ public class Line implements Savable, Cloneable {
compEigen1.calculateEigen(compMat1); compEigen1.calculateEigen(compMat1);
direction = compEigen1.getEigenVector(0); direction = compEigen1.getEigenVector(0);
assert vars.unlock(); vars.release();
} }
/** /**

@ -110,8 +110,8 @@ public class LineSegment implements Cloneable, Savable {
} }
public float distanceSquared(Vector3f point) { public float distanceSquared(Vector3f point) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector3f compVec1 = TempVars.get().vect1; Vector3f compVec1 = vars.vect1;
point.subtract(origin, compVec1); point.subtract(origin, compVec1);
float segmentParameter = direction.dot(compVec1); float segmentParameter = direction.dot(compVec1);
@ -129,20 +129,20 @@ public class LineSegment implements Cloneable, Savable {
compVec1.subtractLocal(point); compVec1.subtractLocal(point);
float len = compVec1.lengthSquared(); float len = compVec1.lengthSquared();
assert TempVars.get().unlock(); vars.release();
return len; return len;
} }
public float distanceSquared(LineSegment test) { public float distanceSquared(LineSegment test) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector3f compVec1 = TempVars.get().vect1; Vector3f compVec1 = vars.vect1;
origin.subtract(test.getOrigin(), compVec1); origin.subtract(test.getOrigin(), compVec1);
float negativeDirectionDot = -(direction.dot(test.getDirection())); float negativeDirectionDot = -(direction.dot(test.getDirection()));
float diffThisDot = compVec1.dot(direction); float diffThisDot = compVec1.dot(direction);
float diffTestDot = -(compVec1.dot(test.getDirection())); float diffTestDot = -(compVec1.dot(test.getDirection()));
float lengthOfDiff = compVec1.lengthSquared(); float lengthOfDiff = compVec1.lengthSquared();
assert TempVars.get().unlock(); vars.release();
float determinant = FastMath.abs(1.0f - negativeDirectionDot float determinant = FastMath.abs(1.0f - negativeDirectionDot
* negativeDirectionDot); * negativeDirectionDot);
float s0, s1, squareDistance, extentDeterminant0, extentDeterminant1, tempS0, tempS1; float s0, s1, squareDistance, extentDeterminant0, extentDeterminant1, tempS0, tempS1;

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.math; package com.jme3.math;
import com.jme3.export.InputCapsule; import com.jme3.export.InputCapsule;
@ -55,11 +54,9 @@ import java.util.logging.Logger;
public final class Matrix3f implements Savable, Cloneable { public final class Matrix3f implements Savable, Cloneable {
private static final Logger logger = Logger.getLogger(Matrix3f.class.getName()); private static final Logger logger = Logger.getLogger(Matrix3f.class.getName());
protected float m00, m01, m02; protected float m00, m01, m02;
protected float m10, m11, m12; protected float m10, m11, m12;
protected float m20, m21, m22; protected float m20, m21, m22;
public static final Matrix3f ZERO = new Matrix3f(0, 0, 0, 0, 0, 0, 0, 0, 0); public static final Matrix3f ZERO = new Matrix3f(0, 0, 0, 0, 0, 0, 0, 0, 0);
public static final Matrix3f IDENTITY = new Matrix3f(); public static final Matrix3f IDENTITY = new Matrix3f();
@ -176,21 +173,30 @@ public final class Matrix3f implements Savable, Cloneable {
switch (i) { switch (i) {
case 0: case 0:
switch (j) { switch (j) {
case 0: return m00; case 0:
case 1: return m01; return m00;
case 2: return m02; case 1:
return m01;
case 2:
return m02;
} }
case 1: case 1:
switch (j) { switch (j) {
case 0: return m10; case 0:
case 1: return m11; return m10;
case 2: return m12; case 1:
return m11;
case 2:
return m12;
} }
case 2: case 2:
switch (j) { switch (j) {
case 0: return m20; case 0:
case 1: return m21; return m20;
case 2: return m22; case 1:
return m21;
case 2:
return m22;
} }
} }
@ -220,8 +226,7 @@ public final class Matrix3f implements Savable, Cloneable {
data[6] = m20; data[6] = m20;
data[7] = m21; data[7] = m21;
data[8] = m22; data[8] = m22;
} } else {
else {
data[0] = m00; data[0] = m00;
data[1] = m10; data[1] = m10;
data[2] = m20; data[2] = m20;
@ -232,8 +237,7 @@ public final class Matrix3f implements Savable, Cloneable {
data[7] = m12; data[7] = m12;
data[8] = m22; data[8] = m22;
} }
} } else if (data.length == 16) {
else if (data.length == 16) {
if (rowMajor) { if (rowMajor) {
data[0] = m00; data[0] = m00;
data[1] = m01; data[1] = m01;
@ -244,8 +248,7 @@ public final class Matrix3f implements Savable, Cloneable {
data[8] = m20; data[8] = m20;
data[9] = m21; data[9] = m21;
data[10] = m22; data[10] = m22;
} } else {
else {
data[0] = m00; data[0] = m00;
data[1] = m10; data[1] = m10;
data[2] = m20; data[2] = m20;
@ -256,8 +259,7 @@ public final class Matrix3f implements Savable, Cloneable {
data[9] = m12; data[9] = m12;
data[10] = m22; data[10] = m22;
} }
} } else {
else {
throw new IndexOutOfBoundsException("Array size must be 9 or 16 in Matrix3f.get()."); throw new IndexOutOfBoundsException("Array size must be 9 or 16 in Matrix3f.get().");
} }
} }
@ -286,7 +288,9 @@ public final class Matrix3f implements Savable, Cloneable {
* @return the column specified by the index. * @return the column specified by the index.
*/ */
public Vector3f getColumn(int i, Vector3f store) { public Vector3f getColumn(int i, Vector3f store) {
if (store == null) store = new Vector3f(); if (store == null) {
store = new Vector3f();
}
switch (i) { switch (i) {
case 0: case 0:
store.x = m00; store.x = m00;
@ -334,7 +338,9 @@ public final class Matrix3f implements Savable, Cloneable {
* @return the row specified by the index. * @return the row specified by the index.
*/ */
public Vector3f getRow(int i, Vector3f store) { public Vector3f getRow(int i, Vector3f store) {
if (store == null) store = new Vector3f(); if (store == null) {
store = new Vector3f();
}
switch (i) { switch (i) {
case 0: case 0:
store.x = m00; store.x = m00;
@ -396,12 +402,12 @@ public final class Matrix3f implements Savable, Cloneable {
// } // }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
fillFloatArray(vars.matrixWrite, columnMajor); fillFloatArray(vars.matrixWrite, columnMajor);
fb.put(vars.matrixWrite, 0, 9); fb.put(vars.matrixWrite, 0, 9);
assert vars.unlock(); vars.release();
return fb; return fb;
} }
@ -470,7 +476,6 @@ public final class Matrix3f implements Savable, Cloneable {
return this; return this;
} }
/** /**
* *
* <code>setRow</code> sets a particular row of this matrix to that * <code>setRow</code> sets a particular row of this matrix to that
@ -529,21 +534,39 @@ public final class Matrix3f implements Savable, Cloneable {
switch (i) { switch (i) {
case 0: case 0:
switch (j) { switch (j) {
case 0: m00 = value; return this; case 0:
case 1: m01 = value; return this; m00 = value;
case 2: m02 = value; return this; return this;
case 1:
m01 = value;
return this;
case 2:
m02 = value;
return this;
} }
case 1: case 1:
switch (j) { switch (j) {
case 0: m10 = value; return this; case 0:
case 1: m11 = value; return this; m10 = value;
case 2: m12 = value; return this; return this;
case 1:
m11 = value;
return this;
case 2:
m12 = value;
return this;
} }
case 2: case 2:
switch (j) { switch (j) {
case 0: m20 = value; return this; case 0:
case 1: m21 = value; return this; m20 = value;
case 2: m22 = value; return this; return this;
case 1:
m21 = value;
return this;
case 2:
m22 = value;
return this;
} }
} }
@ -563,8 +586,10 @@ public final class Matrix3f implements Savable, Cloneable {
* @return this * @return this
*/ */
public Matrix3f set(float[][] matrix) { public Matrix3f set(float[][] matrix) {
if (matrix.length != 3 || matrix[0].length != 3) { throw new IllegalArgumentException( if (matrix.length != 3 || matrix[0].length != 3) {
"Array must be of size 9."); } throw new IllegalArgumentException(
"Array must be of size 9.");
}
m00 = matrix[0][0]; m00 = matrix[0][0];
m01 = matrix[0][1]; m01 = matrix[0][1];
@ -626,8 +651,10 @@ public final class Matrix3f implements Savable, Cloneable {
* @return this * @return this
*/ */
public Matrix3f set(float[] matrix, boolean rowMajor) { public Matrix3f set(float[] matrix, boolean rowMajor) {
if (matrix.length != 9) throw new IllegalArgumentException( if (matrix.length != 9) {
throw new IllegalArgumentException(
"Array must be of size 9."); "Array must be of size 9.");
}
if (rowMajor) { if (rowMajor) {
m00 = matrix[0]; m00 = matrix[0];
@ -681,10 +708,9 @@ public final class Matrix3f implements Savable, Cloneable {
* @return true if this matrix is identity * @return true if this matrix is identity
*/ */
public boolean isIdentity() { public boolean isIdentity() {
return return (m00 == 1 && m01 == 0 && m02 == 0)
(m00 == 1 && m01 == 0 && m02 == 0) && && (m10 == 0 && m11 == 1 && m12 == 0)
(m10 == 0 && m11 == 1 && m12 == 0) && && (m20 == 0 && m21 == 0 && m22 == 1);
(m20 == 0 && m21 == 0 && m22 == 1);
} }
/** /**
@ -766,7 +792,9 @@ public final class Matrix3f implements Savable, Cloneable {
float temp10, temp11, temp12; float temp10, temp11, temp12;
float temp20, temp21, temp22; float temp20, temp21, temp22;
if (product == null) product = new Matrix3f(); if (product == null) {
product = new Matrix3f();
}
temp00 = m00 * mat.m00 + m01 * mat.m10 + m02 * mat.m20; temp00 = m00 * mat.m00 + m01 * mat.m10 + m02 * mat.m20;
temp01 = m00 * mat.m01 + m01 * mat.m11 + m02 * mat.m21; temp01 = m00 * mat.m01 + m01 * mat.m11 + m02 * mat.m21;
temp02 = m00 * mat.m02 + m01 * mat.m12 + m02 * mat.m22; temp02 = m00 * mat.m02 + m01 * mat.m12 + m02 * mat.m22;
@ -862,7 +890,9 @@ public final class Matrix3f implements Savable, Cloneable {
* @return The passed vector after multiplication * @return The passed vector after multiplication
*/ */
public Vector3f multLocal(Vector3f vec) { public Vector3f multLocal(Vector3f vec) {
if (vec == null) return null; if (vec == null) {
return null;
}
float x = vec.x; float x = vec.x;
float y = vec.y; float y = vec.y;
vec.x = m00 * x + m01 * y + m02 * vec.z; vec.x = m00 * x + m01 * y + m02 * vec.z;
@ -925,11 +955,14 @@ public final class Matrix3f implements Savable, Cloneable {
* @return The store * @return The store
*/ */
public Matrix3f invert(Matrix3f store) { public Matrix3f invert(Matrix3f store) {
if (store == null) store = new Matrix3f(); if (store == null) {
store = new Matrix3f();
}
float det = determinant(); float det = determinant();
if ( FastMath.abs(det) <= FastMath.FLT_EPSILON ) if (FastMath.abs(det) <= FastMath.FLT_EPSILON) {
return store.zero(); return store.zero();
}
store.m00 = m11 * m22 - m12 * m21; store.m00 = m11 * m22 - m12 * m21;
store.m01 = m02 * m21 - m01 * m22; store.m01 = m02 * m21 - m01 * m22;
@ -952,8 +985,9 @@ public final class Matrix3f implements Savable, Cloneable {
*/ */
public Matrix3f invertLocal() { public Matrix3f invertLocal() {
float det = determinant(); float det = determinant();
if ( FastMath.abs(det) <= FastMath.FLT_EPSILON ) if (FastMath.abs(det) <= FastMath.FLT_EPSILON) {
return zero(); return zero();
}
float f00 = m11 * m22 - m12 * m21; float f00 = m11 * m22 - m12 * m21;
float f01 = m02 * m21 - m01 * m22; float f01 = m02 * m21 - m01 * m22;
@ -996,7 +1030,9 @@ public final class Matrix3f implements Savable, Cloneable {
* @return store * @return store
*/ */
public Matrix3f adjoint(Matrix3f store) { public Matrix3f adjoint(Matrix3f store) {
if (store == null) store = new Matrix3f(); if (store == null) {
store = new Matrix3f();
}
store.m00 = m11 * m22 - m12 * m21; store.m00 = m11 * m22 - m12 * m21;
store.m01 = m02 * m21 - m01 * m22; store.m01 = m02 * m21 - m01 * m22;
@ -1138,17 +1174,35 @@ public final class Matrix3f implements Savable, Cloneable {
} }
Matrix3f comp = (Matrix3f) o; Matrix3f comp = (Matrix3f) o;
if (Float.compare(m00,comp.m00) != 0) return false; if (Float.compare(m00, comp.m00) != 0) {
if (Float.compare(m01,comp.m01) != 0) return false; return false;
if (Float.compare(m02,comp.m02) != 0) return false; }
if (Float.compare(m01, comp.m01) != 0) {
return false;
}
if (Float.compare(m02, comp.m02) != 0) {
return false;
}
if (Float.compare(m10,comp.m10) != 0) return false; if (Float.compare(m10, comp.m10) != 0) {
if (Float.compare(m11,comp.m11) != 0) return false; return false;
if (Float.compare(m12,comp.m12) != 0) return false; }
if (Float.compare(m11, comp.m11) != 0) {
return false;
}
if (Float.compare(m12, comp.m12) != 0) {
return false;
}
if (Float.compare(m20,comp.m20) != 0) return false; if (Float.compare(m20, comp.m20) != 0) {
if (Float.compare(m21,comp.m21) != 0) return false; return false;
if (Float.compare(m22,comp.m22) != 0) return false; }
if (Float.compare(m21, comp.m21) != 0) {
return false;
}
if (Float.compare(m22, comp.m22) != 0) {
return false;
}
return true; return true;
} }
@ -1290,18 +1344,36 @@ public final class Matrix3f implements Savable, Cloneable {
} }
static boolean equalIdentity(Matrix3f mat) { static boolean equalIdentity(Matrix3f mat) {
if (Math.abs(mat.m00 - 1) > 1e-4) return false; if (Math.abs(mat.m00 - 1) > 1e-4) {
if (Math.abs(mat.m11 - 1) > 1e-4) return false; return false;
if (Math.abs(mat.m22 - 1) > 1e-4) return false; }
if (Math.abs(mat.m11 - 1) > 1e-4) {
return false;
}
if (Math.abs(mat.m22 - 1) > 1e-4) {
return false;
}
if (Math.abs(mat.m01) > 1e-4) return false; if (Math.abs(mat.m01) > 1e-4) {
if (Math.abs(mat.m02) > 1e-4) return false; return false;
}
if (Math.abs(mat.m02) > 1e-4) {
return false;
}
if (Math.abs(mat.m10) > 1e-4) return false; if (Math.abs(mat.m10) > 1e-4) {
if (Math.abs(mat.m12) > 1e-4) return false; return false;
}
if (Math.abs(mat.m12) > 1e-4) {
return false;
}
if (Math.abs(mat.m20) > 1e-4) return false; if (Math.abs(mat.m20) > 1e-4) {
if (Math.abs(mat.m21) > 1e-4) return false; return false;
}
if (Math.abs(mat.m21) > 1e-4) {
return false;
}
return true; return true;
} }

@ -159,7 +159,6 @@ public final class Matrix4f implements Savable, Cloneable {
loadIdentity(); loadIdentity();
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f f = vars.vect1.set(direction); Vector3f f = vars.vect1.set(direction);
Vector3f s = vars.vect2.set(f).crossLocal(up); Vector3f s = vars.vect2.set(f).crossLocal(up);
@ -192,14 +191,14 @@ public final class Matrix4f implements Savable, Cloneable {
// m22 = -direction.z; // m22 = -direction.z;
// //
Matrix4f transMatrix = TempVars.get().tempMat4; Matrix4f transMatrix = vars.tempMat4;
transMatrix.loadIdentity(); transMatrix.loadIdentity();
transMatrix.m03 = -location.x; transMatrix.m03 = -location.x;
transMatrix.m13 = -location.y; transMatrix.m13 = -location.y;
transMatrix.m23 = -location.z; transMatrix.m23 = -location.z;
this.multLocal(transMatrix); this.multLocal(transMatrix);
assert TempVars.get().unlock(); vars.release();
// transMatrix.multLocal(this); // transMatrix.multLocal(this);
@ -746,12 +745,12 @@ public final class Matrix4f implements Savable, Cloneable {
// } // }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
fillFloatArray(vars.matrixWrite, columnMajor); fillFloatArray(vars.matrixWrite, columnMajor);
fb.put(vars.matrixWrite, 0, 16); fb.put(vars.matrixWrite, 0, 16);
assert vars.unlock(); vars.release();
return fb; return fb;
} }

@ -1276,12 +1276,11 @@ public final class Quaternion implements Savable, Cloneable {
*/ */
public void lookAt(Vector3f direction, Vector3f up) { public void lookAt(Vector3f direction, Vector3f up) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
vars.vect3.set(direction).normalizeLocal(); vars.vect3.set(direction).normalizeLocal();
vars.vect1.set(up).crossLocal(direction).normalizeLocal(); vars.vect1.set(up).crossLocal(direction).normalizeLocal();
vars.vect2.set(direction).crossLocal(vars.vect1).normalizeLocal(); vars.vect2.set(direction).crossLocal(vars.vect1).normalizeLocal();
fromAxes(vars.vect1, vars.vect2, vars.vect3); fromAxes(vars.vect1, vars.vect2, vars.vect3);
assert vars.unlock(); vars.release();
} }
public void write(JmeExporter e) throws IOException { public void write(JmeExporter e) throws IOException {

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.math; package com.jme3.math;
import com.jme3.bounding.BoundingVolume; import com.jme3.bounding.BoundingVolume;
@ -45,7 +44,6 @@ import com.jme3.export.Savable;
import com.jme3.util.TempVars; import com.jme3.util.TempVars;
import java.io.IOException; import java.io.IOException;
/** /**
* <code>Ray</code> defines a line segment which has an origin and a direction. * <code>Ray</code> defines a line segment which has an origin and a direction.
* That is, a point and an infinite ray is cast from this point. The ray is * That is, a point and an infinite ray is cast from this point. The ray is
@ -57,19 +55,16 @@ import java.io.IOException;
public final class Ray implements Savable, Cloneable, Collidable { public final class Ray implements Savable, Cloneable, Collidable {
//todo: merge with Line? //todo: merge with Line?
/** The ray's begining point. */ /** The ray's begining point. */
public Vector3f origin; public Vector3f origin;
/** The direction of the ray. */ /** The direction of the ray. */
public Vector3f direction; public Vector3f direction;
public float limit = Float.POSITIVE_INFINITY; public float limit = Float.POSITIVE_INFINITY;
// protected static final Vector3f tempVa=new Vector3f(); // protected static final Vector3f tempVa=new Vector3f();
// protected static final Vector3f tempVb=new Vector3f(); // protected static final Vector3f tempVb=new Vector3f();
// protected static final Vector3f tempVc=new Vector3f(); // protected static final Vector3f tempVc=new Vector3f();
// protected static final Vector3f tempVd=new Vector3f(); // protected static final Vector3f tempVd=new Vector3f();
/** /**
* Constructor instantiates a new <code>Ray</code> object. As default, the * Constructor instantiates a new <code>Ray</code> object. As default, the
* origin is (0,0,0) and the direction is (0,0,0). * origin is (0,0,0) and the direction is (0,0,0).
@ -99,7 +94,6 @@ public final class Ray implements Savable, Cloneable, Collidable {
// public boolean intersect(Triangle t) { // public boolean intersect(Triangle t) {
// return intersect(t.get(0), t.get(1), t.get(2)); // return intersect(t.get(0), t.get(1), t.get(2));
// } // }
/** /**
* <code>intersect</code> determines if the Ray intersects a triangle * <code>intersect</code> determines if the Ray intersects a triangle
* defined by the specified points. * defined by the specified points.
@ -115,7 +109,6 @@ public final class Ray implements Savable, Cloneable, Collidable {
// public boolean intersect(Vector3f v0,Vector3f v1,Vector3f v2){ // public boolean intersect(Vector3f v0,Vector3f v1,Vector3f v2){
// return intersectWhere(v0, v1, v2, null); // return intersectWhere(v0, v1, v2, null);
// } // }
/** /**
* <code>intersectWhere</code> determines if the Ray intersects a triangle. It then * <code>intersectWhere</code> determines if the Ray intersects a triangle. It then
* stores the point of intersection in the given loc vector * stores the point of intersection in the given loc vector
@ -209,7 +202,6 @@ public final class Ray implements Savable, Cloneable, Collidable {
private boolean intersects(Vector3f v0, Vector3f v1, Vector3f v2, private boolean intersects(Vector3f v0, Vector3f v1, Vector3f v2,
Vector3f store, boolean doPlanar, boolean quad) { Vector3f store, boolean doPlanar, boolean quad) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f tempVa = vars.vect1, Vector3f tempVa = vars.vect1,
tempVb = vars.vect2, tempVb = vars.vect2,
@ -230,7 +222,7 @@ public final class Ray implements Savable, Cloneable, Collidable {
dirDotNorm = -dirDotNorm; dirDotNorm = -dirDotNorm;
} else { } else {
// ray and triangle/quad are parallel // ray and triangle/quad are parallel
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -244,12 +236,13 @@ public final class Ray implements Savable, Cloneable, Collidable {
float diffDotNorm = -sign * diff.dot(norm); float diffDotNorm = -sign * diff.dot(norm);
if (diffDotNorm >= 0.0f) { if (diffDotNorm >= 0.0f) {
// this method always returns // this method always returns
assert vars.unlock(); vars.release();
// ray intersects triangle // ray intersects triangle
// if storage vector is null, just return true, // if storage vector is null, just return true,
if (store == null) if (store == null) {
return true; return true;
}
// else fill in. // else fill in.
float inv = 1f / dirDotNorm; float inv = 1f / dirDotNorm;
@ -273,7 +266,7 @@ public final class Ray implements Savable, Cloneable, Collidable {
} }
} }
} }
assert vars.unlock(); vars.release();
return false; return false;
} }
@ -377,15 +370,15 @@ public final class Ray implements Savable, Cloneable, Collidable {
public boolean intersectsWherePlane(Plane p, Vector3f loc) { public boolean intersectsWherePlane(Plane p, Vector3f loc) {
float denominator = p.getNormal().dot(direction); float denominator = p.getNormal().dot(direction);
if (denominator > -FastMath.FLT_EPSILON && denominator < FastMath.FLT_EPSILON) if (denominator > -FastMath.FLT_EPSILON && denominator < FastMath.FLT_EPSILON) {
return false; // coplanar return false; // coplanar
}
float numerator = -(p.getNormal().dot(origin) - p.getConstant()); float numerator = -(p.getNormal().dot(origin) - p.getConstant());
float ratio = numerator / denominator; float ratio = numerator / denominator;
if (ratio < FastMath.FLT_EPSILON) if (ratio < FastMath.FLT_EPSILON) {
return false; // intersects behind origin return false; // intersects behind origin
}
loc.set(direction).multLocal(ratio).addLocal(origin); loc.set(direction).multLocal(ratio).addLocal(origin);
return true; return true;
@ -398,8 +391,9 @@ public final class Ray implements Savable, Cloneable, Collidable {
} else if (other instanceof AbstractTriangle) { } else if (other instanceof AbstractTriangle) {
AbstractTriangle tri = (AbstractTriangle) other; AbstractTriangle tri = (AbstractTriangle) other;
float d = intersects(tri.get1(), tri.get2(), tri.get3()); float d = intersects(tri.get1(), tri.get2(), tri.get3());
if (Float.isInfinite(d) || Float.isNaN(d)) if (Float.isInfinite(d) || Float.isNaN(d)) {
return 0; return 0;
}
Vector3f point = new Vector3f(direction).multLocal(d).addLocal(origin); Vector3f point = new Vector3f(direction).multLocal(d).addLocal(origin);
results.addCollision(new CollisionResult(point, d)); results.addCollision(new CollisionResult(point, d));
@ -411,7 +405,6 @@ public final class Ray implements Savable, Cloneable, Collidable {
public float distanceSquared(Vector3f point) { public float distanceSquared(Vector3f point) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f tempVa = vars.vect1, Vector3f tempVa = vars.vect1,
tempVb = vars.vect2; tempVb = vars.vect2;
@ -427,7 +420,7 @@ public final class Ray implements Savable, Cloneable, Collidable {
tempVb.subtract(point, tempVa); tempVb.subtract(point, tempVa);
float len = tempVa.lengthSquared(); float len = tempVa.lengthSquared();
assert vars.unlock(); vars.release();
return len; return len;
} }
@ -527,4 +520,3 @@ public final class Ray implements Savable, Cloneable, Collidable {
} }
} }
} }

@ -98,7 +98,6 @@ public class Camera implements Savable, Cloneable {
*/ */
Intersects; Intersects;
} }
/** /**
* LEFT_PLANE represents the left plane of the camera frustum. * LEFT_PLANE represents the left plane of the camera frustum.
*/ */
@ -131,7 +130,6 @@ public class Camera implements Savable, Cloneable {
* MAX_WORLD_PLANES holds the maximum planes allowed by the system. * MAX_WORLD_PLANES holds the maximum planes allowed by the system.
*/ */
private static final int MAX_WORLD_PLANES = 6; private static final int MAX_WORLD_PLANES = 6;
/** /**
* Camera's location * Camera's location
*/ */
@ -164,14 +162,12 @@ public class Camera implements Savable, Cloneable {
* Distance from camera to bottom frustum plane. * Distance from camera to bottom frustum plane.
*/ */
protected float frustumBottom; protected float frustumBottom;
//Temporary values computed in onFrustumChange that are needed if a //Temporary values computed in onFrustumChange that are needed if a
//call is made to onFrameChange. //call is made to onFrameChange.
protected float[] coeffLeft; protected float[] coeffLeft;
protected float[] coeffRight; protected float[] coeffRight;
protected float[] coeffBottom; protected float[] coeffBottom;
protected float[] coeffTop; protected float[] coeffTop;
//view port coordinates //view port coordinates
/** /**
* Percent value on display where horizontal viewing starts for this camera. * Percent value on display where horizontal viewing starts for this camera.
@ -197,13 +193,11 @@ public class Camera implements Savable, Cloneable {
* Array holding the planes that this camera will check for culling. * Array holding the planes that this camera will check for culling.
*/ */
protected Plane[] worldPlane; protected Plane[] worldPlane;
/** /**
* A mask value set during contains() that allows fast culling of a Node's * A mask value set during contains() that allows fast culling of a Node's
* children. * children.
*/ */
private int planeState; private int planeState;
protected int width; protected int width;
protected int height; protected int height;
protected boolean viewportChanged = true; protected boolean viewportChanged = true;
@ -758,10 +752,9 @@ public class Camera implements Savable, Cloneable {
*/ */
public void lookAt(Vector3f pos, Vector3f worldUpVector) { public void lookAt(Vector3f pos, Vector3f worldUpVector) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock(); Vector3f newDirection = vars.vect1;
Vector3f newDirection = TempVars.get().vect1; Vector3f newUp = vars.vect2;
Vector3f newUp = TempVars.get().vect2; Vector3f newLeft = vars.vect3;
Vector3f newLeft = TempVars.get().vect3;
newDirection.set(pos).subtractLocal(location).normalizeLocal(); newDirection.set(pos).subtractLocal(location).normalizeLocal();
@ -783,7 +776,7 @@ public class Camera implements Savable, Cloneable {
this.rotation.fromAxes(newLeft, newUp, newDirection); this.rotation.fromAxes(newLeft, newUp, newDirection);
this.rotation.normalizeLocal(); this.rotation.normalizeLocal();
assert vars.unlock(); vars.release();
onFrameChange(); onFrameChange();
} }
@ -1285,15 +1278,15 @@ public class Camera implements Savable, Cloneable {
store = new Vector3f(); store = new Vector3f();
} }
// assert TempVars.get().lock(); // TempVars vars = vars.lock();
// Quaternion tmp_quat = TempVars.get().quat1; // Quaternion tmp_quat = vars.quat1;
// tmp_quat.set( worldPosition.x, worldPosition.y, worldPosition.z, 1 ); // tmp_quat.set( worldPosition.x, worldPosition.y, worldPosition.z, 1 );
// viewProjectionMatrix.mult(tmp_quat, tmp_quat); // viewProjectionMatrix.mult(tmp_quat, tmp_quat);
// tmp_quat.multLocal( 1.0f / tmp_quat.getW() ); // tmp_quat.multLocal( 1.0f / tmp_quat.getW() );
// store.x = ( ( tmp_quat.getX() + 1 ) * ( viewPortRight - viewPortLeft ) / 2 + viewPortLeft ) * getWidth(); // store.x = ( ( tmp_quat.getX() + 1 ) * ( viewPortRight - viewPortLeft ) / 2 + viewPortLeft ) * getWidth();
// store.y = ( ( tmp_quat.getY() + 1 ) * ( viewPortTop - viewPortBottom ) / 2 + viewPortBottom ) * getHeight(); // store.y = ( ( tmp_quat.getY() + 1 ) * ( viewPortTop - viewPortBottom ) / 2 + viewPortBottom ) * getHeight();
// store.z = ( tmp_quat.getZ() + 1 ) / 2; // store.z = ( tmp_quat.getZ() + 1 ) / 2;
// assert TempVars.get().unlock(); // vars.release();
float w = viewProjectionMatrix.multProj(worldPosition, store); float w = viewProjectionMatrix.multProj(worldPosition, store);
store.divideLocal(w); store.divideLocal(w);

@ -75,7 +75,6 @@ import java.util.logging.Logger;
public class RenderManager { public class RenderManager {
private static final Logger logger = Logger.getLogger(RenderManager.class.getName()); private static final Logger logger = Logger.getLogger(RenderManager.class.getName());
private Renderer renderer; private Renderer renderer;
private Timer timer; private Timer timer;
private ArrayList<ViewPort> preViewPorts = new ArrayList<ViewPort>(); private ArrayList<ViewPort> preViewPorts = new ArrayList<ViewPort>();
@ -344,7 +343,6 @@ public class RenderManager {
public void updateUniformBindings(List<Uniform> params) { public void updateUniformBindings(List<Uniform> params) {
// assums worldMatrix is properly set. // assums worldMatrix is properly set.
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Matrix4f tempMat4 = vars.tempMat4; Matrix4f tempMat4 = vars.tempMat4;
Matrix3f tempMat3 = vars.tempMat3; Matrix3f tempMat3 = vars.tempMat3;
@ -465,7 +463,7 @@ public class RenderManager {
} }
} }
assert vars.unlock(); vars.release();
} }
/** /**
@ -1167,5 +1165,4 @@ public class RenderManager {
renderViewPort(postViewPorts.get(i), tpf); renderViewPort(postViewPorts.get(i), tpf);
} }
} }
} }

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.scene; package com.jme3.scene;
import com.jme3.asset.AssetNotFoundException; import com.jme3.asset.AssetNotFoundException;
@ -60,18 +59,13 @@ import java.util.logging.Logger;
public class Geometry extends Spatial { public class Geometry extends Spatial {
private static final Logger logger = Logger.getLogger(Geometry.class.getName()); private static final Logger logger = Logger.getLogger(Geometry.class.getName());
protected Mesh mesh; protected Mesh mesh;
protected transient int lodLevel = 0; protected transient int lodLevel = 0;
protected Material material; protected Material material;
/** /**
* When true, the geometry's transform will not be applied. * When true, the geometry's transform will not be applied.
*/ */
protected boolean ignoreTransform = false; protected boolean ignoreTransform = false;
protected transient Matrix4f cachedWorldMat = new Matrix4f(); protected transient Matrix4f cachedWorldMat = new Matrix4f();
/** /**
@ -101,8 +95,9 @@ public class Geometry extends Spatial {
*/ */
public Geometry(String name, Mesh mesh) { public Geometry(String name, Mesh mesh) {
this(name); this(name);
if (mesh == null) if (mesh == null) {
throw new NullPointerException(); throw new NullPointerException();
}
this.mesh = mesh; this.mesh = mesh;
} }
@ -133,11 +128,13 @@ public class Geometry extends Spatial {
*/ */
@Override @Override
public void setLodLevel(int lod) { public void setLodLevel(int lod) {
if (mesh.getNumLodLevels() == 0) if (mesh.getNumLodLevels() == 0) {
throw new IllegalStateException("LOD levels are not set on this mesh"); throw new IllegalStateException("LOD levels are not set on this mesh");
}
if (lod < 0 || lod >= mesh.getNumLodLevels()) if (lod < 0 || lod >= mesh.getNumLodLevels()) {
throw new IllegalArgumentException("LOD level is out of range: " + lod); throw new IllegalArgumentException("LOD level is out of range: " + lod);
}
lodLevel = lod; lodLevel = lod;
} }
@ -181,8 +178,9 @@ public class Geometry extends Spatial {
* @throws IllegalArgumentException If mesh is null * @throws IllegalArgumentException If mesh is null
*/ */
public void setMesh(Mesh mesh) { public void setMesh(Mesh mesh) {
if (mesh == null) if (mesh == null) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
}
this.mesh = mesh; this.mesh = mesh;
setBoundRefresh(); setBoundRefresh();
@ -246,8 +244,9 @@ public class Geometry extends Spatial {
@Override @Override
protected void updateWorldBound() { protected void updateWorldBound() {
super.updateWorldBound(); super.updateWorldBound();
if (mesh == null) if (mesh == null) {
throw new NullPointerException("Geometry: " + getName() + " has null mesh"); throw new NullPointerException("Geometry: " + getName() + " has null mesh");
}
if (mesh.getBound() != null) { if (mesh.getBound() != null) {
if (ignoreTransform) { if (ignoreTransform) {
@ -283,12 +282,12 @@ public class Geometry extends Spatial {
cachedWorldMat.setRotationQuaternion(worldTransform.getRotation()); cachedWorldMat.setRotationQuaternion(worldTransform.getRotation());
cachedWorldMat.setTranslation(worldTransform.getTranslation()); cachedWorldMat.setTranslation(worldTransform.getTranslation());
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Matrix4f scaleMat = TempVars.get().tempMat4; Matrix4f scaleMat = vars.tempMat4;
scaleMat.loadIdentity(); scaleMat.loadIdentity();
scaleMat.scale(worldTransform.getScale()); scaleMat.scale(worldTransform.getScale());
cachedWorldMat.multLocal(scaleMat); cachedWorldMat.multLocal(scaleMat);
assert TempVars.get().unlock(); vars.release();
} }
/** /**
@ -362,11 +361,12 @@ public class Geometry extends Spatial {
Geometry geomClone = (Geometry) super.clone(cloneMaterial); Geometry geomClone = (Geometry) super.clone(cloneMaterial);
geomClone.cachedWorldMat = cachedWorldMat.clone(); geomClone.cachedWorldMat = cachedWorldMat.clone();
if (material != null) { if (material != null) {
if (cloneMaterial) if (cloneMaterial) {
geomClone.material = material.clone(); geomClone.material = material.clone();
else } else {
geomClone.material = material; geomClone.material = material;
} }
}
if (mesh != null && mesh.getBuffer(Type.BindPosePosition) != null) { if (mesh != null && mesh.getBuffer(Type.BindPosePosition) != null) {
geomClone.mesh = mesh.cloneForAnim(); geomClone.mesh = mesh.cloneForAnim();
@ -436,5 +436,4 @@ public class Geometry extends Spatial {
} }
ignoreTransform = ic.readBoolean("ignoreTransform", false); ignoreTransform = ic.readBoolean("ignoreTransform", false);
} }
} }

@ -340,7 +340,6 @@ public abstract class Spatial implements Savable, Cloneable, Collidable {
*/ */
public void rotateUpTo(Vector3f newUp) { public void rotateUpTo(Vector3f newUp) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f compVecA = vars.vect1; Vector3f compVecA = vars.vect1;
Quaternion q = vars.quat1; Quaternion q = vars.quat1;
@ -360,7 +359,7 @@ public abstract class Spatial implements Savable, Cloneable, Collidable {
q.fromAngleNormalAxis(angle, rotAxis); q.fromAngleNormalAxis(angle, rotAxis);
q.mult(rot, rot); q.mult(rot, rot);
assert vars.unlock(); vars.release();
setTransformRefresh(); setTransformRefresh();
} }
@ -382,9 +381,9 @@ public abstract class Spatial implements Savable, Cloneable, Collidable {
Vector3f worldTranslation = getWorldTranslation(); Vector3f worldTranslation = getWorldTranslation();
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f compVecA = vars.vect4; Vector3f compVecA = vars.vect4;
assert vars.unlock(); vars.release();
compVecA.set(position).subtractLocal(worldTranslation); compVecA.set(position).subtractLocal(worldTranslation);
getLocalRotation().lookAt(compVecA, upVector); getLocalRotation().lookAt(compVecA, upVector);
@ -448,7 +447,6 @@ public abstract class Spatial implements Savable, Cloneable, Collidable {
refreshFlags &= ~RF_TRANSFORM; refreshFlags &= ~RF_TRANSFORM;
} else { } else {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Spatial[] stack = vars.spatialStack; Spatial[] stack = vars.spatialStack;
Spatial rootNode = this; Spatial rootNode = this;
@ -472,7 +470,7 @@ public abstract class Spatial implements Savable, Cloneable, Collidable {
i++; i++;
} }
assert vars.unlock(); vars.release();
for (int j = i; j >= 0; j--) { for (int j = i; j >= 0; j--) {
rootNode = stack[j]; rootNode = stack[j];
@ -978,11 +976,11 @@ public abstract class Spatial implements Savable, Cloneable, Collidable {
* @return The spatial on which this method is called, e.g <code>this</code>. * @return The spatial on which this method is called, e.g <code>this</code>.
*/ */
public Spatial rotate(float yaw, float roll, float pitch) { public Spatial rotate(float yaw, float roll, float pitch) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Quaternion q = TempVars.get().quat1; Quaternion q = vars.quat1;
q.fromAngles(yaw, roll, pitch); q.fromAngles(yaw, roll, pitch);
rotate(q); rotate(q);
assert TempVars.get().unlock(); vars.release();
return this; return this;
} }

@ -116,7 +116,6 @@ public class CameraControl extends AbstractControl {
// set the localtransform, so that the worldtransform would be equal to the camera's transform. // set the localtransform, so that the worldtransform would be equal to the camera's transform.
// Location: // Location:
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f vecDiff = vars.vect1.set(camera.getLocation()).subtractLocal(spatial.getWorldTranslation()); Vector3f vecDiff = vars.vect1.set(camera.getLocation()).subtractLocal(spatial.getWorldTranslation());
spatial.setLocalTranslation(vecDiff.addLocal(spatial.getLocalTranslation())); spatial.setLocalTranslation(vecDiff.addLocal(spatial.getLocalTranslation()));
@ -124,7 +123,7 @@ public class CameraControl extends AbstractControl {
// Rotation: // Rotation:
Quaternion worldDiff = vars.quat1.set(camera.getRotation()).subtractLocal(spatial.getWorldRotation()); Quaternion worldDiff = vars.quat1.set(camera.getRotation()).subtractLocal(spatial.getWorldRotation());
spatial.setLocalRotation(worldDiff.addLocal(spatial.getLocalRotation())); spatial.setLocalRotation(worldDiff.addLocal(spatial.getLocalRotation()));
assert vars.unlock(); vars.release();
break; break;
} }
} }

@ -126,12 +126,11 @@ public class LightControl extends AbstractControl {
((PointLight) light).setPosition(spatial.getWorldTranslation()); ((PointLight) light).setPosition(spatial.getWorldTranslation());
} }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
if (light instanceof DirectionalLight) { if (light instanceof DirectionalLight) {
((DirectionalLight) light).setDirection(vars.vect1.set(spatial.getWorldTranslation()).multLocal(-1.0f)); ((DirectionalLight) light).setDirection(vars.vect1.set(spatial.getWorldTranslation()).multLocal(-1.0f));
} }
assert vars.unlock(); vars.release();
//TODO add code for Spot light here when it's done //TODO add code for Spot light here when it's done
// if( light instanceof SpotLight){ // if( light instanceof SpotLight){
// ((SpotLight)light).setPosition(spatial.getWorldTranslation()); // ((SpotLight)light).setPosition(spatial.getWorldTranslation());
@ -142,15 +141,12 @@ public class LightControl extends AbstractControl {
private void lightToSpatial(Light light) { private void lightToSpatial(Light light) {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
if (light instanceof PointLight) { if (light instanceof PointLight) {
PointLight pLight = (PointLight) light; PointLight pLight = (PointLight) light;
Vector3f vecDiff = vars.vect1.set(pLight.getPosition()).subtractLocal(spatial.getWorldTranslation()); Vector3f vecDiff = vars.vect1.set(pLight.getPosition()).subtractLocal(spatial.getWorldTranslation());
spatial.setLocalTranslation(vecDiff.addLocal(spatial.getLocalTranslation())); spatial.setLocalTranslation(vecDiff.addLocal(spatial.getLocalTranslation()));
assert vars.unlock();
} }
if (light instanceof DirectionalLight) { if (light instanceof DirectionalLight) {
@ -159,7 +155,7 @@ public class LightControl extends AbstractControl {
Vector3f vecDiff = vars.vect1.subtractLocal(spatial.getWorldTranslation()); Vector3f vecDiff = vars.vect1.subtractLocal(spatial.getWorldTranslation());
spatial.setLocalTranslation(vecDiff.addLocal(spatial.getLocalTranslation())); spatial.setLocalTranslation(vecDiff.addLocal(spatial.getLocalTranslation()));
} }
assert vars.unlock(); vars.release();
//TODO add code for Spot light here when it's done //TODO add code for Spot light here when it's done

@ -67,4 +67,14 @@ public class SkeletonDebugger extends Node {
wires.updateGeometry(); wires.updateGeometry();
points.updateGeometry(); points.updateGeometry();
} }
public SkeletonPoints getPoints() {
return points;
}
public SkeletonWire getWires() {
return wires;
}
} }

@ -29,11 +29,9 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
// $Id: Dome.java 4131 2009-03-19 20:15:28Z blaine.dev $ // $Id: Dome.java 4131 2009-03-19 20:15:28Z blaine.dev $
package com.jme3.scene.shape; package com.jme3.scene.shape;
import com.jme3.scene.*; import com.jme3.scene.*;
import com.jme3.export.JmeImporter; import com.jme3.export.JmeImporter;
import com.jme3.export.InputCapsule; import com.jme3.export.InputCapsule;
@ -58,15 +56,11 @@ import java.nio.ShortBuffer;
public class Dome extends Mesh { public class Dome extends Mesh {
private int planes; private int planes;
private int radialSamples; private int radialSamples;
/** The radius of the dome */ /** The radius of the dome */
private float radius; private float radius;
/** The center of the dome */ /** The center of the dome */
private Vector3f center; private Vector3f center;
private boolean outsideView = true; private boolean outsideView = true;
/** /**
@ -220,7 +214,6 @@ public class Dome extends Mesh {
} }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f tempVc = vars.vect3; Vector3f tempVc = vars.vect3;
Vector3f tempVb = vars.vect2; Vector3f tempVb = vars.vect2;
Vector3f tempVa = vars.vect1; Vector3f tempVa = vars.vect1;
@ -251,10 +244,11 @@ public class Dome extends Mesh {
BufferUtils.populateFromBuffer(tempVa, vb, i); BufferUtils.populateFromBuffer(tempVa, vb, i);
kNormal = tempVa.subtractLocal(center); kNormal = tempVa.subtractLocal(center);
kNormal.normalizeLocal(); kNormal.normalizeLocal();
if (outsideView) if (outsideView) {
nb.put(kNormal.x).put(kNormal.y).put(kNormal.z); nb.put(kNormal.x).put(kNormal.y).put(kNormal.z);
else } else {
nb.put(-kNormal.x).put(-kNormal.y).put(-kNormal.z); nb.put(-kNormal.x).put(-kNormal.y).put(-kNormal.z);
}
tb.put(fRadialFraction).put(fYFraction); tb.put(fRadialFraction).put(fYFraction);
} }
@ -263,7 +257,7 @@ public class Dome extends Mesh {
tb.put(1.0f).put(fYFraction); tb.put(1.0f).put(fYFraction);
} }
assert vars.unlock(); vars.release();
// pole // pole
vb.put(center.x).put(center.y + radius).put(center.z); vb.put(center.x).put(center.y + radius).put(center.z);
@ -321,5 +315,4 @@ public class Dome extends Mesh {
capsule.write(radius, "radius", 0); capsule.write(radius, "radius", 0);
capsule.write(center, "center", Vector3f.ZERO); capsule.write(center, "center", Vector3f.ZERO);
} }
} }

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
// $Id: Sphere.java 4163 2009-03-25 01:14:55Z matt.yellen $ // $Id: Sphere.java 4163 2009-03-25 01:14:55Z matt.yellen $
package com.jme3.scene.shape; package com.jme3.scene.shape;
@ -47,7 +46,6 @@ import java.io.IOException;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.nio.ShortBuffer; import java.nio.ShortBuffer;
/** /**
* <code>Sphere</code> represents a 3D object with all points equidistance * <code>Sphere</code> represents a 3D object with all points equidistance
* from a center point. * from a center point.
@ -58,6 +56,7 @@ import java.nio.ShortBuffer;
public class Sphere extends Mesh { public class Sphere extends Mesh {
public enum TextureMode { public enum TextureMode {
/** /**
* Wrap texture radially and along z-axis * Wrap texture radially and along z-axis
*/ */
@ -72,22 +71,14 @@ public class Sphere extends Mesh {
*/ */
Polar Polar
} }
protected int vertCount; protected int vertCount;
protected int triCount; protected int triCount;
protected int zSamples; protected int zSamples;
protected int radialSamples; protected int radialSamples;
protected boolean useEvenSlices; protected boolean useEvenSlices;
protected boolean interior; protected boolean interior;
/** the distance from the center point each point falls on */ /** the distance from the center point each point falls on */
public float radius; public float radius;
protected TextureMode textureMode = TextureMode.Original; protected TextureMode textureMode = TextureMode.Original;
/** /**
@ -185,7 +176,6 @@ public class Sphere extends Mesh {
afCos[radialSamples] = afCos[0]; afCos[radialSamples] = afCos[0];
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Vector3f tempVa = vars.vect1; Vector3f tempVa = vars.vect1;
Vector3f tempVb = vars.vect2; Vector3f tempVb = vars.vect2;
Vector3f tempVc = vars.vect3; Vector3f tempVc = vars.vect3;
@ -195,11 +185,11 @@ public class Sphere extends Mesh {
for (int iZ = 1; iZ < (zSamples - 1); iZ++) { for (int iZ = 1; iZ < (zSamples - 1); iZ++) {
float fAFraction = FastMath.HALF_PI * (-1.0f + fZFactor * iZ); // in (-pi/2, pi/2) float fAFraction = FastMath.HALF_PI * (-1.0f + fZFactor * iZ); // in (-pi/2, pi/2)
float fZFraction; float fZFraction;
if (useEvenSlices) if (useEvenSlices) {
fZFraction = -1.0f + fZFactor * iZ; // in (-1, 1) fZFraction = -1.0f + fZFactor * iZ; // in (-1, 1)
else } else {
fZFraction = FastMath.sin(fAFraction); // in (-1,1) fZFraction = FastMath.sin(fAFraction); // in (-1,1)
}
float fZ = radius * fZFraction; float fZ = radius * fZFraction;
// compute center of slice // compute center of slice
@ -225,21 +215,22 @@ public class Sphere extends Mesh {
kNormal = tempVa; kNormal = tempVa;
kNormal.normalizeLocal(); kNormal.normalizeLocal();
if (!interior) // allow interior texture vs. exterior if (!interior) // allow interior texture vs. exterior
{
normBuf.put(kNormal.x).put(kNormal.y).put( normBuf.put(kNormal.x).put(kNormal.y).put(
kNormal.z); kNormal.z);
else } else {
normBuf.put(-kNormal.x).put(-kNormal.y).put( normBuf.put(-kNormal.x).put(-kNormal.y).put(
-kNormal.z); -kNormal.z);
}
if (textureMode == TextureMode.Original) if (textureMode == TextureMode.Original) {
texBuf.put(fRadialFraction).put( texBuf.put(fRadialFraction).put(
0.5f * (fZFraction + 1.0f)); 0.5f * (fZFraction + 1.0f));
else if (textureMode == TextureMode.Projected) } else if (textureMode == TextureMode.Projected) {
texBuf.put(fRadialFraction).put( texBuf.put(fRadialFraction).put(
FastMath.INV_PI FastMath.INV_PI
* (FastMath.HALF_PI + FastMath * (FastMath.HALF_PI + FastMath.asin(fZFraction)));
.asin(fZFraction))); } else if (textureMode == TextureMode.Polar) {
else if (textureMode == TextureMode.Polar) {
float r = (FastMath.HALF_PI - FastMath.abs(fAFraction)) / FastMath.PI; float r = (FastMath.HALF_PI - FastMath.abs(fAFraction)) / FastMath.PI;
float u = r * afCos[iR] + 0.5f; float u = r * afCos[iR] + 0.5f;
float v = r * afSin[iR] + 0.5f; float v = r * afSin[iR] + 0.5f;
@ -252,16 +243,14 @@ public class Sphere extends Mesh {
BufferUtils.copyInternalVector3(posBuf, iSave, i); BufferUtils.copyInternalVector3(posBuf, iSave, i);
BufferUtils.copyInternalVector3(normBuf, iSave, i); BufferUtils.copyInternalVector3(normBuf, iSave, i);
if (textureMode == TextureMode.Original) if (textureMode == TextureMode.Original) {
texBuf.put(1.0f).put( texBuf.put(1.0f).put(
0.5f * (fZFraction + 1.0f)); 0.5f * (fZFraction + 1.0f));
else if (textureMode == TextureMode.Projected) } else if (textureMode == TextureMode.Projected) {
texBuf.put(1.0f) texBuf.put(1.0f).put(
.put(
FastMath.INV_PI FastMath.INV_PI
* (FastMath.HALF_PI + FastMath * (FastMath.HALF_PI + FastMath.asin(fZFraction)));
.asin(fZFraction))); } else if (textureMode == TextureMode.Polar) {
else if (textureMode == TextureMode.Polar) {
float r = (FastMath.HALF_PI - FastMath.abs(fAFraction)) / FastMath.PI; float r = (FastMath.HALF_PI - FastMath.abs(fAFraction)) / FastMath.PI;
texBuf.put(r + 0.5f).put(0.5f); texBuf.put(r + 0.5f).put(0.5f);
} }
@ -269,26 +258,26 @@ public class Sphere extends Mesh {
i++; i++;
} }
assert vars.unlock(); vars.release();
// south pole // south pole
posBuf.position(i * 3); posBuf.position(i * 3);
posBuf.put(0f).put(0f).put(-radius); posBuf.put(0f).put(0f).put(-radius);
normBuf.position(i * 3); normBuf.position(i * 3);
if (!interior) if (!interior) {
normBuf.put(0).put(0).put(-1); // allow for inner normBuf.put(0).put(0).put(-1); // allow for inner
// texture orientation } // texture orientation
// later. // later.
else else {
normBuf.put(0).put(0).put(1); normBuf.put(0).put(0).put(1);
}
texBuf.position(i * 2); texBuf.position(i * 2);
if (textureMode == TextureMode.Polar) { if (textureMode == TextureMode.Polar) {
texBuf.put(0.5f).put(0.5f); texBuf.put(0.5f).put(0.5f);
} } else {
else {
texBuf.put(0.5f).put(0.0f); texBuf.put(0.5f).put(0.0f);
} }
@ -297,10 +286,11 @@ public class Sphere extends Mesh {
// north pole // north pole
posBuf.put(0).put(0).put(radius); posBuf.put(0).put(0).put(radius);
if (!interior) if (!interior) {
normBuf.put(0).put(0).put(1); normBuf.put(0).put(0).put(1);
else } else {
normBuf.put(0).put(0).put(-1); normBuf.put(0).put(0).put(-1);
}
if (textureMode == TextureMode.Polar) { if (textureMode == TextureMode.Polar) {
texBuf.put(0.5f).put(0.5f); texBuf.put(0.5f).put(0.5f);
@ -427,5 +417,4 @@ public class Sphere extends Mesh {
capsule.write(textureMode, "textureMode", TextureMode.Original); capsule.write(textureMode, "textureMode", TextureMode.Original);
capsule.write(interior, "interior", false); capsule.write(interior, "interior", false);
} }
} }

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.util; package com.jme3.util;
import com.jme3.math.ColorRGBA; import com.jme3.math.ColorRGBA;
@ -48,7 +47,6 @@ import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.WeakHashMap; import java.util.WeakHashMap;
/** /**
* <code>BufferUtils</code> is a helper class for generating nio buffers from * <code>BufferUtils</code> is a helper class for generating nio buffers from
* jME data classes such as Vectors and ColorRGBA. * jME data classes such as Vectors and ColorRGBA.
@ -62,14 +60,12 @@ public final class BufferUtils {
// private static final Vector2f _tempVec2 = new Vector2f(); // private static final Vector2f _tempVec2 = new Vector2f();
// private static final Vector3f _tempVec3 = new Vector3f(); // private static final Vector3f _tempVec3 = new Vector3f();
// private static final ColorRGBA _tempColor = new ColorRGBA(); // private static final ColorRGBA _tempColor = new ColorRGBA();
//// -- TRACKER HASH -- //// //// -- TRACKER HASH -- ////
private static final Map<Buffer, Object> trackingHash = Collections.synchronizedMap(new WeakHashMap<Buffer, Object>()); private static final Map<Buffer, Object> trackingHash = Collections.synchronizedMap(new WeakHashMap<Buffer, Object>());
private static final Object ref = new Object(); private static final Object ref = new Object();
private static final boolean trackDirectMemory = false; private static final boolean trackDirectMemory = false;
//// -- GENERIC CLONE -- //// //// -- GENERIC CLONE -- ////
/** /**
* Creates a clone of the given buffer. The clone's capacity is * Creates a clone of the given buffer. The clone's capacity is
* equal to the given buffer's limit. * equal to the given buffer's limit.
@ -93,9 +89,7 @@ public final class BufferUtils {
} }
} }
//// -- VECTOR3F METHODS -- //// //// -- VECTOR3F METHODS -- ////
/** /**
* Generate a new FloatBuffer using the given array of Vector3f objects. * Generate a new FloatBuffer using the given array of Vector3f objects.
* The FloatBuffer will be 3 * data.length long and contain the vector data * The FloatBuffer will be 3 * data.length long and contain the vector data
@ -104,14 +98,17 @@ public final class BufferUtils {
* @param data array of Vector3f objects to place into a new FloatBuffer * @param data array of Vector3f objects to place into a new FloatBuffer
*/ */
public static FloatBuffer createFloatBuffer(Vector3f... data) { public static FloatBuffer createFloatBuffer(Vector3f... data) {
if (data == null) return null; if (data == null) {
return null;
}
FloatBuffer buff = createFloatBuffer(3 * data.length); FloatBuffer buff = createFloatBuffer(3 * data.length);
for (int x = 0; x < data.length; x++) { for (int x = 0; x < data.length; x++) {
if (data[x] != null) if (data[x] != null) {
buff.put(data[x].x).put(data[x].y).put(data[x].z); buff.put(data[x].x).put(data[x].y).put(data[x].z);
else } else {
buff.put(0).put(0).put(0); buff.put(0).put(0).put(0);
} }
}
buff.flip(); buff.flip();
return buff; return buff;
} }
@ -123,15 +120,17 @@ public final class BufferUtils {
* @param data array of Quaternion objects to place into a new FloatBuffer * @param data array of Quaternion objects to place into a new FloatBuffer
*/ */
public static FloatBuffer createFloatBuffer(Quaternion... data) { public static FloatBuffer createFloatBuffer(Quaternion... data) {
if (data == null) return null; if (data == null) {
return null;
}
FloatBuffer buff = createFloatBuffer(4 * data.length); FloatBuffer buff = createFloatBuffer(4 * data.length);
for (int x = 0; x < data.length; x++) { for (int x = 0; x < data.length; x++) {
if (data[x] != null) if (data[x] != null) {
buff.put(data[x].getX()).put(data[x].getY()) buff.put(data[x].getX()).put(data[x].getY()).put(data[x].getZ()).put(data[x].getW());
.put(data[x].getZ()).put(data[x].getW()); } else {
else
buff.put(0).put(0).put(0); buff.put(0).put(0).put(0);
} }
}
buff.flip(); buff.flip();
return buff; return buff;
} }
@ -141,7 +140,9 @@ public final class BufferUtils {
* @param data array of float primitives to place into a new FloatBuffer * @param data array of float primitives to place into a new FloatBuffer
*/ */
public static FloatBuffer createFloatBuffer(float... data) { public static FloatBuffer createFloatBuffer(float... data) {
if (data == null) return null; if (data == null) {
return null;
}
FloatBuffer buff = createFloatBuffer(data.length); FloatBuffer buff = createFloatBuffer(data.length);
buff.clear(); buff.clear();
buff.put(data); buff.put(data);
@ -311,12 +312,12 @@ public final class BufferUtils {
* to normalize * to normalize
*/ */
public static void normalizeVector3(FloatBuffer buf, int index) { public static void normalizeVector3(FloatBuffer buf, int index) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector3f tempVec3 = TempVars.get().vect1; Vector3f tempVec3 = vars.vect1;
populateFromBuffer(tempVec3, buf, index); populateFromBuffer(tempVec3, buf, index);
tempVec3.normalizeLocal(); tempVec3.normalizeLocal();
setInBuffer(tempVec3, buf, index); setInBuffer(tempVec3, buf, index);
assert TempVars.get().unlock(); vars.release();
} }
/** /**
@ -331,12 +332,12 @@ public final class BufferUtils {
* to add to * to add to
*/ */
public static void addInBuffer(Vector3f toAdd, FloatBuffer buf, int index) { public static void addInBuffer(Vector3f toAdd, FloatBuffer buf, int index) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector3f tempVec3 = TempVars.get().vect1; Vector3f tempVec3 = vars.vect1;
populateFromBuffer(tempVec3, buf, index); populateFromBuffer(tempVec3, buf, index);
tempVec3.addLocal(toAdd); tempVec3.addLocal(toAdd);
setInBuffer(tempVec3, buf, index); setInBuffer(tempVec3, buf, index);
assert TempVars.get().unlock(); vars.release();
} }
/** /**
@ -351,12 +352,12 @@ public final class BufferUtils {
* to multiply * to multiply
*/ */
public static void multInBuffer(Vector3f toMult, FloatBuffer buf, int index) { public static void multInBuffer(Vector3f toMult, FloatBuffer buf, int index) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector3f tempVec3 = TempVars.get().vect1; Vector3f tempVec3 = vars.vect1;
populateFromBuffer(tempVec3, buf, index); populateFromBuffer(tempVec3, buf, index);
tempVec3.multLocal(toMult); tempVec3.multLocal(toMult);
setInBuffer(tempVec3, buf, index); setInBuffer(tempVec3, buf, index);
assert TempVars.get().unlock(); vars.release();
} }
/** /**
@ -373,16 +374,15 @@ public final class BufferUtils {
* @return * @return
*/ */
public static boolean equals(Vector3f check, FloatBuffer buf, int index) { public static boolean equals(Vector3f check, FloatBuffer buf, int index) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector3f tempVec3 = TempVars.get().vect1; Vector3f tempVec3 = vars.vect1;
populateFromBuffer(tempVec3, buf, index); populateFromBuffer(tempVec3, buf, index);
boolean eq = tempVec3.equals(check); boolean eq = tempVec3.equals(check);
assert TempVars.get().unlock(); vars.release();
return eq; return eq;
} }
// // -- VECTOR2F METHODS -- //// // // -- VECTOR2F METHODS -- ////
/** /**
* Generate a new FloatBuffer using the given array of Vector2f objects. * Generate a new FloatBuffer using the given array of Vector2f objects.
* The FloatBuffer will be 2 * data.length long and contain the vector data * The FloatBuffer will be 2 * data.length long and contain the vector data
@ -391,14 +391,17 @@ public final class BufferUtils {
* @param data array of Vector2f objects to place into a new FloatBuffer * @param data array of Vector2f objects to place into a new FloatBuffer
*/ */
public static FloatBuffer createFloatBuffer(Vector2f... data) { public static FloatBuffer createFloatBuffer(Vector2f... data) {
if (data == null) return null; if (data == null) {
return null;
}
FloatBuffer buff = createFloatBuffer(2 * data.length); FloatBuffer buff = createFloatBuffer(2 * data.length);
for (int x = 0; x < data.length; x++) { for (int x = 0; x < data.length; x++) {
if (data[x] != null) if (data[x] != null) {
buff.put(data[x].x).put(data[x].y); buff.put(data[x].x).put(data[x].y);
else } else {
buff.put(0).put(0); buff.put(0).put(0);
} }
}
buff.flip(); buff.flip();
return buff; return buff;
} }
@ -514,12 +517,12 @@ public final class BufferUtils {
* to normalize * to normalize
*/ */
public static void normalizeVector2(FloatBuffer buf, int index) { public static void normalizeVector2(FloatBuffer buf, int index) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector2f tempVec2 = TempVars.get().vect2d; Vector2f tempVec2 = vars.vect2d;
populateFromBuffer(tempVec2, buf, index); populateFromBuffer(tempVec2, buf, index);
tempVec2.normalizeLocal(); tempVec2.normalizeLocal();
setInBuffer(tempVec2, buf, index); setInBuffer(tempVec2, buf, index);
assert TempVars.get().unlock(); vars.release();
} }
/** /**
@ -534,12 +537,12 @@ public final class BufferUtils {
* to add to * to add to
*/ */
public static void addInBuffer(Vector2f toAdd, FloatBuffer buf, int index) { public static void addInBuffer(Vector2f toAdd, FloatBuffer buf, int index) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector2f tempVec2 = TempVars.get().vect2d; Vector2f tempVec2 = vars.vect2d;
populateFromBuffer(tempVec2, buf, index); populateFromBuffer(tempVec2, buf, index);
tempVec2.addLocal(toAdd); tempVec2.addLocal(toAdd);
setInBuffer(tempVec2, buf, index); setInBuffer(tempVec2, buf, index);
assert TempVars.get().unlock(); vars.release();
} }
/** /**
@ -554,12 +557,12 @@ public final class BufferUtils {
* to multiply * to multiply
*/ */
public static void multInBuffer(Vector2f toMult, FloatBuffer buf, int index) { public static void multInBuffer(Vector2f toMult, FloatBuffer buf, int index) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector2f tempVec2 = TempVars.get().vect2d; Vector2f tempVec2 = vars.vect2d;
populateFromBuffer(tempVec2, buf, index); populateFromBuffer(tempVec2, buf, index);
tempVec2.multLocal(toMult); tempVec2.multLocal(toMult);
setInBuffer(tempVec2, buf, index); setInBuffer(tempVec2, buf, index);
assert TempVars.get().unlock(); vars.release();
} }
/** /**
@ -576,17 +579,15 @@ public final class BufferUtils {
* @return * @return
*/ */
public static boolean equals(Vector2f check, FloatBuffer buf, int index) { public static boolean equals(Vector2f check, FloatBuffer buf, int index) {
assert TempVars.get().lock(); TempVars vars = TempVars.get();
Vector2f tempVec2 = TempVars.get().vect2d; Vector2f tempVec2 = vars.vect2d;
populateFromBuffer(tempVec2, buf, index); populateFromBuffer(tempVec2, buf, index);
boolean eq = tempVec2.equals(check); boolean eq = tempVec2.equals(check);
assert TempVars.get().unlock(); vars.release();
return eq; return eq;
} }
//// -- INT METHODS -- //// //// -- INT METHODS -- ////
/** /**
* Generate a new IntBuffer using the given array of ints. The IntBuffer * Generate a new IntBuffer using the given array of ints. The IntBuffer
* will be data.length long and contain the int data as data[0], data[1]... * will be data.length long and contain the int data as data[0], data[1]...
@ -596,7 +597,9 @@ public final class BufferUtils {
* array of ints to place into a new IntBuffer * array of ints to place into a new IntBuffer
*/ */
public static IntBuffer createIntBuffer(int... data) { public static IntBuffer createIntBuffer(int... data) {
if (data == null) return null; if (data == null) {
return null;
}
IntBuffer buff = createIntBuffer(data.length); IntBuffer buff = createIntBuffer(data.length);
buff.clear(); buff.clear();
buff.put(data); buff.put(data);
@ -613,7 +616,9 @@ public final class BufferUtils {
* @return a new int array populated from the IntBuffer * @return a new int array populated from the IntBuffer
*/ */
public static int[] getIntArray(IntBuffer buff) { public static int[] getIntArray(IntBuffer buff) {
if (buff == null) return null; if (buff == null) {
return null;
}
buff.clear(); buff.clear();
int[] inds = new int[buff.limit()]; int[] inds = new int[buff.limit()];
for (int x = 0; x < inds.length; x++) { for (int x = 0; x < inds.length; x++) {
@ -631,7 +636,9 @@ public final class BufferUtils {
* @return a new float array populated from the FloatBuffer * @return a new float array populated from the FloatBuffer
*/ */
public static float[] getFloatArray(FloatBuffer buff) { public static float[] getFloatArray(FloatBuffer buff) {
if (buff == null) return null; if (buff == null) {
return null;
}
buff.clear(); buff.clear();
float[] inds = new float[buff.limit()]; float[] inds = new float[buff.limit()];
for (int x = 0; x < inds.length; x++) { for (int x = 0; x < inds.length; x++) {
@ -640,9 +647,7 @@ public final class BufferUtils {
return inds; return inds;
} }
//// -- GENERAL DOUBLE ROUTINES -- //// //// -- GENERAL DOUBLE ROUTINES -- ////
/** /**
* Create a new DoubleBuffer of the specified size. * Create a new DoubleBuffer of the specified size.
* *
@ -691,7 +696,9 @@ public final class BufferUtils {
* @return the copy * @return the copy
*/ */
public static DoubleBuffer clone(DoubleBuffer buf) { public static DoubleBuffer clone(DoubleBuffer buf) {
if (buf == null) return null; if (buf == null) {
return null;
}
buf.rewind(); buf.rewind();
DoubleBuffer copy; DoubleBuffer copy;
@ -705,10 +712,7 @@ public final class BufferUtils {
return copy; return copy;
} }
//// -- GENERAL FLOAT ROUTINES -- //// //// -- GENERAL FLOAT ROUTINES -- ////
/** /**
* Create a new FloatBuffer of the specified size. * Create a new FloatBuffer of the specified size.
* *
@ -756,7 +760,9 @@ public final class BufferUtils {
* @return the copy * @return the copy
*/ */
public static FloatBuffer clone(FloatBuffer buf) { public static FloatBuffer clone(FloatBuffer buf) {
if (buf == null) return null; if (buf == null) {
return null;
}
buf.rewind(); buf.rewind();
FloatBuffer copy; FloatBuffer copy;
@ -770,9 +776,7 @@ public final class BufferUtils {
return copy; return copy;
} }
//// -- GENERAL INT ROUTINES -- //// //// -- GENERAL INT ROUTINES -- ////
/** /**
* Create a new IntBuffer of the specified size. * Create a new IntBuffer of the specified size.
* *
@ -821,7 +825,9 @@ public final class BufferUtils {
* @return the copy * @return the copy
*/ */
public static IntBuffer clone(IntBuffer buf) { public static IntBuffer clone(IntBuffer buf) {
if (buf == null) return null; if (buf == null) {
return null;
}
buf.rewind(); buf.rewind();
IntBuffer copy; IntBuffer copy;
@ -835,9 +841,7 @@ public final class BufferUtils {
return copy; return copy;
} }
//// -- GENERAL BYTE ROUTINES -- //// //// -- GENERAL BYTE ROUTINES -- ////
/** /**
* Create a new ByteBuffer of the specified size. * Create a new ByteBuffer of the specified size.
* *
@ -901,7 +905,9 @@ public final class BufferUtils {
* @return the copy * @return the copy
*/ */
public static ByteBuffer clone(ByteBuffer buf) { public static ByteBuffer clone(ByteBuffer buf) {
if (buf == null) return null; if (buf == null) {
return null;
}
buf.rewind(); buf.rewind();
ByteBuffer copy; ByteBuffer copy;
@ -915,9 +921,7 @@ public final class BufferUtils {
return copy; return copy;
} }
//// -- GENERAL SHORT ROUTINES -- //// //// -- GENERAL SHORT ROUTINES -- ////
/** /**
* Create a new ShortBuffer of the specified size. * Create a new ShortBuffer of the specified size.
* *
@ -956,7 +960,9 @@ public final class BufferUtils {
} }
public static ShortBuffer createShortBuffer(short... data) { public static ShortBuffer createShortBuffer(short... data) {
if (data == null) return null; if (data == null) {
return null;
}
ShortBuffer buff = createShortBuffer(data.length); ShortBuffer buff = createShortBuffer(data.length);
buff.clear(); buff.clear();
buff.put(data); buff.put(data);
@ -975,7 +981,9 @@ public final class BufferUtils {
* @return the copy * @return the copy
*/ */
public static ShortBuffer clone(ShortBuffer buf) { public static ShortBuffer clone(ShortBuffer buf) {
if (buf == null) return null; if (buf == null) {
return null;
}
buf.rewind(); buf.rewind();
ShortBuffer copy; ShortBuffer copy;
@ -1068,25 +1076,20 @@ public final class BufferUtils {
dBufs++; dBufs++;
} }
} }
long heapMem = Runtime.getRuntime().totalMemory() - long heapMem = Runtime.getRuntime().totalMemory()
Runtime.getRuntime().freeMemory(); - Runtime.getRuntime().freeMemory();
boolean printStout = store == null; boolean printStout = store == null;
if (store == null) { if (store == null) {
store = new StringBuilder(); store = new StringBuilder();
} }
store.append("Existing buffers: ").append(bufs.size()).append("\n"); store.append("Existing buffers: ").append(bufs.size()).append("\n");
store.append("(b: ").append(bBufs).append(" f: ").append(fBufs) store.append("(b: ").append(bBufs).append(" f: ").append(fBufs).append(" i: ").append(iBufs).append(" s: ").append(sBufs).append(" d: ").append(dBufs).append(")").append("\n");
.append(" i: ").append(iBufs).append(" s: ").append(sBufs)
.append(" d: ").append(dBufs).append(")").append("\n");
store.append("Total heap memory held: ").append(heapMem / 1024).append("kb\n"); store.append("Total heap memory held: ").append(heapMem / 1024).append("kb\n");
store.append("Total direct memory held: ").append(totalHeld / 1024).append("kb\n"); store.append("Total direct memory held: ").append(totalHeld / 1024).append("kb\n");
store.append("(b: ").append(bBufsM/1024).append("kb f: ").append(fBufsM/1024) store.append("(b: ").append(bBufsM / 1024).append("kb f: ").append(fBufsM / 1024).append("kb i: ").append(iBufsM / 1024).append("kb s: ").append(sBufsM / 1024).append("kb d: ").append(dBufsM / 1024).append("kb)").append("\n");
.append("kb i: ").append(iBufsM/1024).append("kb s: ").append(sBufsM/1024)
.append("kb d: ").append(dBufsM/1024).append("kb)").append("\n");
if (printStout) { if (printStout) {
System.out.println(store.toString()); System.out.println(store.toString());
} }
} }
} }

@ -29,7 +29,6 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.util; package com.jme3.util;
import com.jme3.collision.bih.BIHNode.BIHStackData; import com.jme3.collision.bih.BIHNode.BIHStackData;
@ -45,94 +44,148 @@ import com.jme3.scene.Spatial;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* Temporary variables assigned to each thread. Engine classes may access * Temporary variables assigned to each thread. Engine classes may access
* these temp variables with TempVars.get(). A method using temp vars with this * these temp variables with TempVars.get().
* class is not allowed to make calls to other methods using the class otherwise * This returns an available instance of the TempVar class ensuring this particular instance is never used elsewhere in the mean time.
* memory corruption will occur. A locking mechanism may be implemented
* in the future to prevent the occurance of such situation.
*/ */
public class TempVars { public class TempVars {
private static final ThreadLocal<TempVars> varsLocal private static final Logger log = Logger.getLogger(TempVars.class.getName());
= new ThreadLocal<TempVars>(){ //default array size
private static final int arraySize = 30;
//one array per thread
private static final ThreadLocal<TempVars[]> varsLocal = new ThreadLocal<TempVars[]>() {
@Override @Override
public TempVars initialValue(){ public TempVars[] initialValue() {
return new TempVars(); TempVars[] l = new TempVars[arraySize];
//the array is initialized with one instance (should be enough in most cases)
l[0] = new TempVars();
return l;
} }
}; };
//the current index in the stack
private static int stackLevel = 0;
//number of instanced TempVar, just to raise a warning if too much are instanced
private static int instanceCount = 1;
/**
* Returns an available instance of the TempVar class
* Warning, you have to release the instance once used by calling the release() method
* @return a TempVar instance
*/
public static TempVars get() { public static TempVars get() {
return varsLocal.get();
}
private TempVars(){ TempVars vars = null;
TempVars[] array = varsLocal.get();
//if the stack level is > 0 it means we have some instances in the array, we can return one of them
if (stackLevel >= 0) {
//getting the lastTempVar instance
vars = array[stackLevel];
//removing the reference to it in the array so if it's never released it will be garbage collected.
array[stackLevel] = null;
//decreasing the stack level since we have one less instance
stackLevel--;
//In some cases (when lauching jme threads from another thread), the instance at 0 can be null
//so we check that to avoid NPE
if (vars==null){
vars = new TempVars();
//increasing the count to keep track
instanceCount++;
} }
} else {
private boolean locked = false; //if the stack level is <0 it means the stack is empty, we have to create a new instance
private StackTraceElement[] lockerStack; //there
vars = new TempVars();
public final boolean lock(){ //increasing the count to keep track
if (locked){ instanceCount++;
System.err.println("INTERNAL ERROR"); //raising a warning if the instance count is to high, because it might be due to bad usage
System.err.println("Offending trace: "); if (instanceCount == array.length) {
log.log(Level.WARNING, "TempVars has been requested {0} times, maybe you forgot to call release()?", instanceCount);
StackTraceElement[] stack = new Throwable().getStackTrace();
for (int i = 1; i < stack.length; i++){
System.err.println("\tat "+stack[i].toString());
} }
System.err.println("Attempted to aquire TempVars lock owned by");
for (int i = 1; i < lockerStack.length; i++){
System.err.println("\tat "+lockerStack[i].toString());
} }
System.exit(1); return vars;
return false;
} }
lockerStack = new Throwable().getStackTrace(); private TempVars() {
locked = true;
return true;
} }
// private boolean locked = false;
public final boolean unlock(){ // private StackTraceElement[] lockerStack;
if (!locked){
System.err.println("INTERNAL ERROR"); // public final boolean lock() {
System.err.println("Attempted to release non-existent lock: "); // if (locked) {
// System.err.println("INTERNAL ERROR");
StackTraceElement[] stack = new Throwable().getStackTrace(); // System.err.println("Offending trace: ");
for (int i = 1; i < stack.length; i++){ //
System.err.println("\tat "+stack[i].toString()); // StackTraceElement[] stack = new Throwable().getStackTrace();
// for (int i = 1; i < stack.length; i++) {
// System.err.println("\tat " + stack[i].toString());
// }
//
// System.err.println("Attempted to aquire TempVars lock owned by");
// for (int i = 1; i < lockerStack.length; i++) {
// System.err.println("\tat " + lockerStack[i].toString());
// }
// System.exit(1);
// return false;
// }
//
// lockerStack = new Throwable().getStackTrace();
// locked = true;
// return true;
// }
//
// public final boolean unlock() {
// if (!locked) {
// System.err.println("INTERNAL ERROR");
// System.err.println("Attempted to release non-existent lock: ");
//
// StackTraceElement[] stack = new Throwable().getStackTrace();
// for (int i = 1; i < stack.length; i++) {
// System.err.println("\tat " + stack[i].toString());
// }
//
// System.exit(1);
// return false;
// }
//
// lockerStack = null;
// locked = false;
// return true;
// }
/**
* Release this instance of TempVar, allowing it to be reused later.
*/
public final void release() {
//we only keep as much instances as we can,
//but if too much are instanced, we just don't readd them, they'll be garbage collected
//This can happen only in case of recursive calls that are instancing the TempVar
if (stackLevel < arraySize - 1) {
stackLevel++;
varsLocal.get()[stackLevel] = this;
} }
System.exit(1);
return false;
} }
lockerStack = null;
locked = false;
return true;
}
/** /**
* For interfacing with OpenGL in Renderer. * For interfacing with OpenGL in Renderer.
*/ */
public final IntBuffer intBuffer1 = BufferUtils.createIntBuffer(1); public final IntBuffer intBuffer1 = BufferUtils.createIntBuffer(1);
public final IntBuffer intBuffer16 = BufferUtils.createIntBuffer(16); public final IntBuffer intBuffer16 = BufferUtils.createIntBuffer(16);
public final FloatBuffer floatBuffer16 = BufferUtils.createFloatBuffer(16); public final FloatBuffer floatBuffer16 = BufferUtils.createFloatBuffer(16);
/** /**
* Skinning buffers * Skinning buffers
*/ */
public final float[] skinPositions = new float[512 * 3]; public final float[] skinPositions = new float[512 * 3];
public final float[] skinNormals = new float[512 * 3]; public final float[] skinNormals = new float[512 * 3];
/** /**
* Fetching triangle from mesh * Fetching triangle from mesh
*/ */
public final Triangle triangle = new Triangle(); public final Triangle triangle = new Triangle();
/** /**
* General vectors. * General vectors.
*/ */
@ -143,42 +196,36 @@ public class TempVars {
public final Vector3f vect5 = new Vector3f(); public final Vector3f vect5 = new Vector3f();
public final Vector3f vect6 = new Vector3f(); public final Vector3f vect6 = new Vector3f();
public final Vector3f vect7 = new Vector3f(); public final Vector3f vect7 = new Vector3f();
//seems the maximum number of vector used is 7 in com.jme3.bounding.java
public final Vector3f vect8 = new Vector3f(); public final Vector3f vect8 = new Vector3f();
public final Vector3f vect9 = new Vector3f(); public final Vector3f vect9 = new Vector3f();
public final Vector3f vect10 = new Vector3f(); public final Vector3f vect10 = new Vector3f();
public final Vector3f[] tri = {new Vector3f(), public final Vector3f[] tri = {new Vector3f(),
new Vector3f(), new Vector3f(),
new Vector3f()}; new Vector3f()};
/** /**
* 2D vector * 2D vector
*/ */
public final Vector2f vect2d = new Vector2f(); public final Vector2f vect2d = new Vector2f();
public final Vector2f vect2d2 = new Vector2f(); public final Vector2f vect2d2 = new Vector2f();
/** /**
* General matrices. * General matrices.
*/ */
public final Matrix3f tempMat3 = new Matrix3f(); public final Matrix3f tempMat3 = new Matrix3f();
public final Matrix4f tempMat4 = new Matrix4f(); public final Matrix4f tempMat4 = new Matrix4f();
/** /**
* General quaternions. * General quaternions.
*/ */
public final Quaternion quat1 = new Quaternion(); public final Quaternion quat1 = new Quaternion();
public final Quaternion quat2 = new Quaternion(); public final Quaternion quat2 = new Quaternion();
/** /**
* Eigen * Eigen
*/ */
public final Eigen3f eigen = new Eigen3f(); public final Eigen3f eigen = new Eigen3f();
/** /**
* Plane * Plane
*/ */
public final Plane plane = new Plane(); public final Plane plane = new Plane();
/** /**
* BoundingBox ray collision * BoundingBox ray collision
*/ */
@ -187,18 +234,14 @@ public class TempVars {
public final float[] fDdU = new float[3]; public final float[] fDdU = new float[3];
public final float[] fADdU = new float[3]; public final float[] fADdU = new float[3];
public final float[] fAWxDdU = new float[3]; public final float[] fAWxDdU = new float[3];
/** /**
* Maximum tree depth .. 32 levels?? * Maximum tree depth .. 32 levels??
*/ */
public final Spatial[] spatialStack = new Spatial[32]; public final Spatial[] spatialStack = new Spatial[32];
public final float[] matrixWrite = new float[16]; public final float[] matrixWrite = new float[16];
/** /**
* BIHTree * BIHTree
*/ */
public final float[] bihSwapTmp = new float[9]; public final float[] bihSwapTmp = new float[9];
public final ArrayList<BIHStackData> bihStack = new ArrayList<BIHStackData>(); public final ArrayList<BIHStackData> bihStack = new ArrayList<BIHStackData>();
} }

@ -109,7 +109,6 @@ public class WaterFilter extends Filter {
private boolean useCaustics = true; private boolean useCaustics = true;
private boolean useRefraction = true; private boolean useRefraction = true;
private float time = 0; private float time = 0;
private float savedTpf = 0;
private float reflectionDisplace = 30; private float reflectionDisplace = 30;
private float foamIntensity = 0.5f; private float foamIntensity = 0.5f;
private boolean underWater; private boolean underWater;
@ -168,7 +167,7 @@ public class WaterFilter extends Filter {
sceneCam.getFrustumTop(), sceneCam.getFrustumTop(),
sceneCam.getFrustumBottom()); sceneCam.getFrustumBottom());
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
vars.vect1.set(sceneCam.getLocation()).addLocal(sceneCam.getUp()); vars.vect1.set(sceneCam.getLocation()).addLocal(sceneCam.getUp());
float planeDistance = plane.pseudoDistance(vars.vect1); float planeDistance = plane.pseudoDistance(vars.vect1);
@ -176,8 +175,8 @@ public class WaterFilter extends Filter {
vars.vect3.set(vars.vect1.subtractLocal(vars.vect2)).subtractLocal(loc).normalizeLocal().negateLocal(); vars.vect3.set(vars.vect1.subtractLocal(vars.vect2)).subtractLocal(loc).normalizeLocal().negateLocal();
reflectionCam.lookAt(targetLocation, vars.vect3); reflectionCam.lookAt(targetLocation, vars.vect3);
vars.release();
assert vars.unlock();
if (inv) { if (inv) {
reflectionCam.setAxes(reflectionCam.getLeft().negateLocal(), reflectionCam.getUp(), reflectionCam.getDirection().negateLocal()); reflectionCam.setAxes(reflectionCam.getLeft().negateLocal(), reflectionCam.getUp(), reflectionCam.getDirection().negateLocal());
} }

@ -165,7 +165,7 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
return; return;
} }
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Quaternion tmpRot1 = vars.quat1; Quaternion tmpRot1 = vars.quat1;
Quaternion tmpRot2 = vars.quat2; Quaternion tmpRot2 = vars.quat2;
@ -264,7 +264,7 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
} }
} }
} }
assert vars.unlock(); vars.release();
} }
@ -682,7 +682,7 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
baseRigidBody.setKinematic(mode == Mode.Kinetmatic); baseRigidBody.setKinematic(mode == Mode.Kinetmatic);
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
for (PhysicsBoneLink link : boneLinks.values()) { for (PhysicsBoneLink link : boneLinks.values()) {
link.rigidBody.setKinematic(mode == Mode.Kinetmatic); link.rigidBody.setKinematic(mode == Mode.Kinetmatic);
if (mode == Mode.Ragdoll) { if (mode == Mode.Ragdoll) {
@ -693,7 +693,7 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
} }
} }
assert vars.unlock(); vars.release();
for (Bone bone : skeleton.getRoots()) { for (Bone bone : skeleton.getRoots()) {
RagdollUtils.setUserControl(bone, mode == Mode.Ragdoll); RagdollUtils.setUserControl(bone, mode == Mode.Ragdoll);
@ -716,9 +716,7 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
animControl.setEnabled(true); animControl.setEnabled(true);
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
for (PhysicsBoneLink link : boneLinks.values()) { for (PhysicsBoneLink link : boneLinks.values()) {
Vector3f p = link.rigidBody.getMotionState().getWorldLocation(); Vector3f p = link.rigidBody.getMotionState().getWorldLocation();
@ -737,7 +735,7 @@ public class KinematicRagdollControl implements PhysicsControl, PhysicsCollision
link.startBlendingRot.set(q2); link.startBlendingRot.set(q2);
link.rigidBody.setKinematic(true); link.rigidBody.setKinematic(true);
} }
assert vars.unlock(); vars.release();
for (Bone bone : skeleton.getRoots()) { for (Bone bone : skeleton.getRoots()) {
RagdollUtils.setUserControl(bone, false); RagdollUtils.setUserControl(bone, false);

@ -90,14 +90,14 @@ public class DebugShapeFactory {
// apply rotation // apply rotation
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
Matrix3f tempRot = vars.tempMat3; Matrix3f tempRot = vars.tempMat3;
tempRot.set(geometry.getLocalRotation()); tempRot.set(geometry.getLocalRotation());
childCollisionShape.rotation.mult(tempRot, tempRot); childCollisionShape.rotation.mult(tempRot, tempRot);
geometry.setLocalRotation(tempRot); geometry.setLocalRotation(tempRot);
assert vars.unlock(); vars.release();
node.attachChild(geometry); node.attachChild(geometry);
} }
@ -126,9 +126,7 @@ public class DebugShapeFactory {
mesh = new Mesh(); mesh = new Mesh();
mesh.setBuffer(Type.Position, 3, getVertices((ConvexShape) shape.getCShape())); mesh.setBuffer(Type.Position, 3, getVertices((ConvexShape) shape.getCShape()));
mesh.getFloatBuffer(Type.Position).clear(); mesh.getFloatBuffer(Type.Position).clear();
} } else if (shape.getCShape() instanceof ConcaveShape) {
else if(shape.getCShape() instanceof ConcaveShape)
{
mesh = new Mesh(); mesh = new Mesh();
mesh.setBuffer(Type.Position, 3, getVertices((ConcaveShape) shape.getCShape())); mesh.setBuffer(Type.Position, 3, getVertices((ConcaveShape) shape.getCShape()));
mesh.getFloatBuffer(Type.Position).clear(); mesh.getFloatBuffer(Type.Position).clear();

@ -29,51 +29,82 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package jme3test.app; package jme3test.app;
import com.jme3.util.TempVars; import com.jme3.util.TempVars;
import java.util.Date;
import java.util.Iterator;
public class TestTempVars { public class TestTempVars {
public static void methodThatUsesTempVars(){
TempVars vars = TempVars.get();
assert vars.lock();
{
vars.vect1.set(123,999,-55);
}
assert vars.unlock();
}
public static void main(String[] args) { public static void main(String[] args) {
System.err.println("NOTE: This test simulates a data corruption attempt\n" +
" in the engine. If assertions are enabled (-ea VM flag), the \n" +
"data corruption will be detected and displayed below.");
Date d,d2;
System.err.println("NOTE: This test simulates a data corruption attempt\n"
+ " in the engine. If assertions are enabled (-ea VM flag), the \n"
+ "data corruption will be detected and displayed below.");
//get the vars
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
assert vars.lock();
{
// do something with temporary vars // do something with temporary vars
vars.vect1.addLocal(vars.vect2); vars.vect1.addLocal(vars.vect2);
//release the vars
vars.release();
//Perf tests
//100 million calls
d = new Date();
for (int i = 0; i < 100000000; i++) {
methodThatUsesTempVars();
} }
assert vars.unlock(); d2 = new Date();
System.out.println("100 million calls : " +(d2.getTime() - d.getTime())+" ms");
//recursive Method
d = new Date();
recursiveMethod();
d2 = new Date();
System.out.println("100 recursive calls : " +(d2.getTime() - d.getTime())+" ms");
assert vars.lock(); d = new Date();
{ for (int i = 0; i < 1000000; i++) {
// set a value methodThatUsesTempVarsWithNoRelease();
vars.vect1.set(1,1,1); }
d2 = new Date();
System.out.println("1 million calls with no release : " +(d2.getTime() - d.getTime())+" ms");
// method that currupts the value }
methodThatUsesTempVars(); static int recurse = 0;
public static void recursiveMethod(){
TempVars vars = TempVars.get();
vars.vect1.set(123, 999, -55);
recurse++;
if(recurse<100){
recursiveMethod();
}
// read the value vars.release();
System.out.println(vars.vect1); }
public static void methodThatUsesTempVars() {
TempVars vars = TempVars.get();
{
vars.vect1.set(123, 999, -55);
} }
assert vars.unlock(); vars.release();
}
public static void methodThatUsesTempVarsWithNoRelease() {
TempVars vars = TempVars.get();
{
vars.vect1.set(123, 999, -55);
} }
} }
}

Loading…
Cancel
Save