bounding: move intersection algorithms to shared class
These algorithms are to be shared with the light filter.
This commit is contained in:
parent
45f8893f13
commit
1fa6c4ac11
@ -593,18 +593,7 @@ public class BoundingBox extends BoundingVolume {
|
|||||||
* @see BoundingVolume#intersectsSphere(com.jme3.bounding.BoundingSphere)
|
* @see BoundingVolume#intersectsSphere(com.jme3.bounding.BoundingSphere)
|
||||||
*/
|
*/
|
||||||
public boolean intersectsSphere(BoundingSphere bs) {
|
public boolean intersectsSphere(BoundingSphere bs) {
|
||||||
assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bs.center);
|
return bs.intersectsBoundingBox(this);
|
||||||
|
|
||||||
if (FastMath.abs(center.x - bs.center.x) < bs.getRadius()
|
|
||||||
+ xExtent
|
|
||||||
&& FastMath.abs(center.y - bs.center.y) < bs.getRadius()
|
|
||||||
+ yExtent
|
|
||||||
&& FastMath.abs(center.z - bs.center.z) < bs.getRadius()
|
|
||||||
+ zExtent) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -670,15 +670,7 @@ public class BoundingSphere extends BoundingVolume {
|
|||||||
* @see com.jme.bounding.BoundingVolume#intersectsSphere(com.jme.bounding.BoundingSphere)
|
* @see com.jme.bounding.BoundingVolume#intersectsSphere(com.jme.bounding.BoundingSphere)
|
||||||
*/
|
*/
|
||||||
public boolean intersectsSphere(BoundingSphere bs) {
|
public boolean intersectsSphere(BoundingSphere bs) {
|
||||||
assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bs.center);
|
return Intersection.intersect(bs, center, radius);
|
||||||
|
|
||||||
TempVars vars = TempVars.get();
|
|
||||||
|
|
||||||
Vector3f diff = center.subtract(bs.center, vars.vect1);
|
|
||||||
float rsum = getRadius() + bs.getRadius();
|
|
||||||
boolean eq = (diff.dot(diff) <= rsum * rsum);
|
|
||||||
vars.release();
|
|
||||||
return eq;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -687,18 +679,7 @@ public class BoundingSphere extends BoundingVolume {
|
|||||||
* @see com.jme.bounding.BoundingVolume#intersectsBoundingBox(com.jme.bounding.BoundingBox)
|
* @see com.jme.bounding.BoundingVolume#intersectsBoundingBox(com.jme.bounding.BoundingBox)
|
||||||
*/
|
*/
|
||||||
public boolean intersectsBoundingBox(BoundingBox bb) {
|
public boolean intersectsBoundingBox(BoundingBox bb) {
|
||||||
assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bb.center);
|
return Intersection.intersect(bb, center, radius);
|
||||||
|
|
||||||
if (FastMath.abs(bb.center.x - center.x) < getRadius()
|
|
||||||
+ bb.xExtent
|
|
||||||
&& FastMath.abs(bb.center.y - center.y) < getRadius()
|
|
||||||
+ bb.yExtent
|
|
||||||
&& FastMath.abs(bb.center.z - center.z) < getRadius()
|
|
||||||
+ bb.zExtent) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -41,10 +41,56 @@ import static java.lang.Math.min;
|
|||||||
/**
|
/**
|
||||||
* This class includes some utility methods for computing intersection
|
* This class includes some utility methods for computing intersection
|
||||||
* between bounding volumes and triangles.
|
* between bounding volumes and triangles.
|
||||||
|
*
|
||||||
* @author Kirill
|
* @author Kirill
|
||||||
*/
|
*/
|
||||||
public class Intersection {
|
public final class Intersection {
|
||||||
|
|
||||||
|
private Intersection() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean intersect(BoundingSphere sphere, Vector3f center, float radius) {
|
||||||
|
assert Vector3f.isValidVector(center) && Vector3f.isValidVector(sphere.center);
|
||||||
|
|
||||||
|
TempVars vars = TempVars.get();
|
||||||
|
try {
|
||||||
|
Vector3f diff = center.subtract(sphere.center, vars.vect1);
|
||||||
|
float rsum = sphere.getRadius() + radius;
|
||||||
|
return (diff.dot(diff) <= rsum * rsum);
|
||||||
|
} finally {
|
||||||
|
vars.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean intersect(BoundingBox bbox, Vector3f center, float radius) {
|
||||||
|
assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bbox.center);
|
||||||
|
|
||||||
|
// Arvo's algorithm
|
||||||
|
float distSqr = radius * radius;
|
||||||
|
|
||||||
|
float minX = bbox.center.x - bbox.xExtent;
|
||||||
|
float maxX = bbox.center.x + bbox.xExtent;
|
||||||
|
|
||||||
|
float minY = bbox.center.y - bbox.yExtent;
|
||||||
|
float maxY = bbox.center.y + bbox.yExtent;
|
||||||
|
|
||||||
|
float minZ = bbox.center.z - bbox.zExtent;
|
||||||
|
float maxZ = bbox.center.z + bbox.zExtent;
|
||||||
|
|
||||||
|
if (center.x < minX) distSqr -= FastMath.sqr(center.x - minX);
|
||||||
|
else if (center.x > maxX) distSqr -= FastMath.sqr(center.x - maxX);
|
||||||
|
|
||||||
|
|
||||||
|
if (center.y < minY) distSqr -= FastMath.sqr(center.y - minY);
|
||||||
|
else if (center.y > maxY) distSqr -= FastMath.sqr(center.y - maxY);
|
||||||
|
|
||||||
|
|
||||||
|
if (center.z < minZ) distSqr -= FastMath.sqr(center.z - minZ);
|
||||||
|
else if (center.z > maxZ) distSqr -= FastMath.sqr(center.z - maxZ);
|
||||||
|
|
||||||
|
return distSqr > 0;
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
if (x1 < minMax.x) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user