📄 boundingbox.java
字号:
center.z - zExtent);
Vector3f max = _compVect2.set(center.x + xExtent, center.y + yExtent,
center.z + zExtent);
for (int i = 1; i < volume.vectorStore.length; i++) {
Vector3f temp = volume.vectorStore[i];
if (temp.x < min.x)
min.x = temp.x;
else if (temp.x > max.x)
max.x = temp.x;
if (temp.y < min.y)
min.y = temp.y;
else if (temp.y > max.y)
max.y = temp.y;
if (temp.z < min.z)
min.z = temp.z;
else if (temp.z > max.z)
max.z = temp.z;
}
center.set(min.addLocal(max));
center.multLocal(0.5f);
xExtent = max.x - center.x;
yExtent = max.y - center.y;
zExtent = max.z - center.z;
return this;
}
/**
* <code>merge</code> combines this bounding box with another box which is
* defined by the center, x, y, z extents.
*
* @param boxCenter
* the center of the box to merge with
* @param boxX
* the x extent of the box to merge with.
* @param boxY
* the y extent of the box to merge with.
* @param boxZ
* the z extent of the box to merge with.
* @param rVal
* the resulting merged box.
* @return the resulting merged box.
*/
private BoundingBox merge(Vector3f boxCenter, float boxX, float boxY,
float boxZ, BoundingBox rVal) {
_compVect1.x = center.x - xExtent;
if (_compVect1.x > boxCenter.x - boxX)
_compVect1.x = boxCenter.x - boxX;
_compVect1.y = center.y - yExtent;
if (_compVect1.y > boxCenter.y - boxY)
_compVect1.y = boxCenter.y - boxY;
_compVect1.z = center.z - zExtent;
if (_compVect1.z > boxCenter.z - boxZ)
_compVect1.z = boxCenter.z - boxZ;
_compVect2.x = center.x + xExtent;
if (_compVect2.x < boxCenter.x + boxX)
_compVect2.x = boxCenter.x + boxX;
_compVect2.y = center.y + yExtent;
if (_compVect2.y < boxCenter.y + boxY)
_compVect2.y = boxCenter.y + boxY;
_compVect2.z = center.z + zExtent;
if (_compVect2.z < boxCenter.z + boxZ)
_compVect2.z = boxCenter.z + boxZ;
center.set(_compVect2).addLocal(_compVect1).multLocal(0.5f);
xExtent = _compVect2.x - center.x;
yExtent = _compVect2.y - center.y;
zExtent = _compVect2.z - center.z;
return rVal;
}
/**
* <code>clone</code> creates a new BoundingBox object containing the same
* data as this one.
*
* @param store
* where to store the cloned information. if null or wrong class,
* a new store is created.
* @return the new BoundingBox
*/
public BoundingVolume clone(BoundingVolume store) {
if (store != null && store.getType() == Type.AABB) {
BoundingBox rVal = (BoundingBox) store;
rVal.center.set(center);
rVal.xExtent = xExtent;
rVal.yExtent = yExtent;
rVal.zExtent = zExtent;
rVal.checkPlane = checkPlane;
return rVal;
}
BoundingBox rVal = new BoundingBox(center.clone(),
xExtent, yExtent, zExtent);
return rVal;
}
/**
* <code>toString</code> returns the string representation of this object.
* The form is: "Radius: RRR.SSSS Center: <Vector>".
*
* @return the string representation of this.
*/
public String toString() {
return "com.jme.scene.BoundingBox [Center: " + center + " xExtent: "
+ xExtent + " yExtent: " + yExtent + " zExtent: " + zExtent
+ "]";
}
/**
* intersects determines if this Bounding Box intersects with another given
* bounding volume. If so, true is returned, otherwise, false is returned.
*
* @see com.jme.bounding.BoundingVolume#intersects(com.jme.bounding.BoundingVolume)
*/
public boolean intersects(BoundingVolume bv) {
if (bv == null)
return false;
return bv.intersectsBoundingBox(this);
}
/**
* determines if this bounding box intersects a given bounding sphere.
*
* @see com.jme.bounding.BoundingVolume#intersectsSphere(com.jme.bounding.BoundingSphere)
*/
public boolean intersectsSphere(BoundingSphere bs) {
if (!Vector3f.isValidVector(center) || !Vector3f.isValidVector(bs.center)) return false;
if (FastMath.abs(center.x - bs.getCenter().x) < bs.getRadius()
+ xExtent
&& FastMath.abs(center.y - bs.getCenter().y) < bs.getRadius()
+ yExtent
&& FastMath.abs(center.z - bs.getCenter().z) < bs.getRadius()
+ zExtent)
return true;
return false;
}
/**
* determines if this bounding box intersects a given bounding box. If the
* two boxes intersect in any way, true is returned. Otherwise, false is
* returned.
*
* @see com.jme.bounding.BoundingVolume#intersectsBoundingBox(com.jme.bounding.BoundingBox)
*/
public boolean intersectsBoundingBox(BoundingBox bb) {
if (!Vector3f.isValidVector(center) || !Vector3f.isValidVector(bb.center)) return false;
if (center.x + xExtent < bb.center.x - bb.xExtent
|| center.x - xExtent > bb.center.x + bb.xExtent)
return false;
else if (center.y + yExtent < bb.center.y - bb.yExtent
|| center.y - yExtent > bb.center.y + bb.yExtent)
return false;
else if (center.z + zExtent < bb.center.z - bb.zExtent
|| center.z - zExtent > bb.center.z + bb.zExtent)
return false;
else
return true;
}
/**
* determines if this bounding box intersects with a given oriented bounding
* box.
*
* @see com.jme.bounding.BoundingVolume#intersectsOrientedBoundingBox(com.jme.bounding.OrientedBoundingBox)
*/
public boolean intersectsOrientedBoundingBox(OrientedBoundingBox obb) {
return obb.intersectsBoundingBox(this);
}
/**
* determines if this bounding box intersects with a given bounding capsule.
*
* @see com.jme.bounding.BoundingVolume#intersectsCapsule(BoundingCapsule)
*/
public boolean intersectsCapsule(BoundingCapsule bc) {
return bc.intersectsBoundingBox(this);
}
/**
* determines if this bounding box intersects with a given ray object. If an
* intersection has occurred, true is returned, otherwise false is returned.
*
* @see com.jme.bounding.BoundingVolume#intersects(com.jme.math.Ray)
*/
public boolean intersects(Ray ray) {
if (!Vector3f.isValidVector(center)) return false;
float rhs;
Vector3f diff = ray.origin.subtract(getCenter(_compVect2), _compVect1);
fWdU[0] = ray.getDirection().dot(Vector3f.UNIT_X);
fAWdU[0] = FastMath.abs(fWdU[0]);
fDdU[0] = diff.dot(Vector3f.UNIT_X);
fADdU[0] = FastMath.abs(fDdU[0]);
if (fADdU[0] > xExtent && fDdU[0] * fWdU[0] >= 0.0) {
return false;
}
fWdU[1] = ray.getDirection().dot(Vector3f.UNIT_Y);
fAWdU[1] = FastMath.abs(fWdU[1]);
fDdU[1] = diff.dot(Vector3f.UNIT_Y);
fADdU[1] = FastMath.abs(fDdU[1]);
if (fADdU[1] > yExtent && fDdU[1] * fWdU[1] >= 0.0) {
return false;
}
fWdU[2] = ray.getDirection().dot(Vector3f.UNIT_Z);
fAWdU[2] = FastMath.abs(fWdU[2]);
fDdU[2] = diff.dot(Vector3f.UNIT_Z);
fADdU[2] = FastMath.abs(fDdU[2]);
if (fADdU[2] > zExtent && fDdU[2] * fWdU[2] >= 0.0) {
return false;
}
Vector3f wCrossD = ray.getDirection().cross(diff, _compVect2);
fAWxDdU[0] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_X));
rhs = yExtent * fAWdU[2] + zExtent * fAWdU[1];
if (fAWxDdU[0] > rhs) {
return false;
}
fAWxDdU[1] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Y));
rhs = xExtent * fAWdU[2] + zExtent * fAWdU[0];
if (fAWxDdU[1] > rhs) {
return false;
}
fAWxDdU[2] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Z));
rhs = xExtent * fAWdU[1] + yExtent * fAWdU[0];
if (fAWxDdU[2] > rhs) {
return false;
}
return true;
}
/**
* @see com.jme.bounding.BoundingVolume#intersectsWhere(com.jme.math.Ray)
*/
public IntersectionRecord intersectsWhere(Ray ray) {
Vector3f diff = _compVect1.set(ray.origin).subtractLocal(center);
Vector3f direction = _compVect2.set(ray.direction);
float[] t = { 0f, Float.POSITIVE_INFINITY };
float saveT0 = t[0], saveT1 = t[1];
boolean notEntirelyClipped = clip(+direction.x, -diff.x - xExtent, t)
&& clip(-direction.x, +diff.x - xExtent, t)
&& 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);
if (notEntirelyClipped && (t[0] != saveT0 || t[1] != saveT1)) {
if (t[1] > t[0]) {
float[] distances = t;
Vector3f[] points = new Vector3f[] {
new Vector3f(ray.direction).multLocal(distances[0]).addLocal(ray.origin),
new Vector3f(ray.direction).multLocal(distances[1]).addLocal(ray.origin)
};
IntersectionRecord record = new IntersectionRecord(distances, points);
return record;
}
float[] distances = new float[] { t[0] };
Vector3f[] points = new Vector3f[] {
new Vector3f(ray.direction).multLocal(distances[0]).addLocal(ray.origin),
};
IntersectionRecord record = new IntersectionRecord(distances, points);
return record;
}
return new IntersectionRecord();
}
@Override
public boolean contains(Vector3f point) {
return FastMath.abs(center.x - point.x) < xExtent
&& FastMath.abs(center.y - point.y) < yExtent
&& FastMath.abs(center.z - point.z) < zExtent;
}
public float distanceToEdge(Vector3f point) {
// compute coordinates of point in box coordinate system
Vector3f closest = point.subtract(center);
// project test point onto box
float sqrDistance = 0.0f;
float delta;
if (closest.x < -xExtent) {
delta = closest.x + xExtent;
sqrDistance += delta * delta;
closest.x = -xExtent;
} else if (closest.x > xExtent) {
delta = closest.x - xExtent;
sqrDistance += delta * delta;
closest.x = xExtent;
}
if (closest.y < -yExtent) {
delta = closest.y + yExtent;
sqrDistance += delta * delta;
closest.y = -yExtent;
} else if (closest.y > yExtent) {
delta = closest.y - yExtent;
sqrDistance += delta * delta;
closest.y = yExtent;
}
if (closest.z < -zExtent) {
delta = closest.z + zExtent;
sqrDistance += delta * delta;
closest.z = -zExtent;
} else if (closest.z > zExtent) {
delta = closest.z - zExtent;
sqrDistance += delta * delta;
closest.z = zExtent;
}
return FastMath.sqrt(sqrDistance);
}
/**
* <code>clip</code> determines if a line segment intersects the current
* test plane.
*
* @param denom
* the denominator of the line segment.
* @param numer
* the numerator of the line segment.
* @param t
* test values of the plane.
* @return true if the line segment intersects the plane, false otherwise.
*/
private boolean clip(float denom, float numer, float[] t) {
// Return value is 'true' if line segment intersects the current test
// plane. Otherwise 'false' is returned in which case the line segment
// is entirely clipped.
if (denom > 0.0f) {
if (numer > denom * t[1])
return false;
if (numer > denom * t[0])
t[0] = numer / denom;
return true;
} else if (denom < 0.0f) {
if (numer > denom * t[0])
return false;
if (numer > denom * t[1])
t[1] = numer / denom;
return true;
} else {
return numer <= 0.0;
}
}
/**
* Query extent.
*
* @param store
* where extent gets stored - null to return a new vector
* @return store / new vector
*/
public Vector3f getExtent(Vector3f store) {
if (store == null) {
store = new Vector3f();
}
store.set(xExtent, yExtent, zExtent);
return store;
}
public void write(JMEExporter e) throws IOException {
super.write(e);
OutputCapsule capsule = e.getCapsule(this);
capsule.write(xExtent, "xExtent", 0);
capsule.write(yExtent, "yExtent", 0);
capsule.write(zExtent, "zExtent", 0);
}
public void read(JMEImporter e) throws IOException {
super.read(e);
InputCapsule capsule = e.getCapsule(this);
xExtent = capsule.readFloat("xExtent", 0);
yExtent = capsule.readFloat("yExtent", 0);
zExtent = capsule.readFloat("zExtent", 0);
}
@Override
public float getVolume() {
return (8*xExtent*yExtent*zExtent);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -