use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class BoneEnvelope method isInEnvelope.
/**
* The method verifies if the given point is inside the envelope.
* @param point
* the point in 3D space (MUST be in a world coordinate space)
* @return <b>true</b> if the point is inside the envelope and <b>false</b> otherwise
*/
public boolean isInEnvelope(Vector3f point) {
Vector3f v = tail.subtract(head);
float boneLength = v.length();
v.normalizeLocal();
// computing a plane that contains 'point' and v is its normal vector
// the plane's equation is: Ax + By + Cz + D = 0, where v = [A, B, C]
float D = -v.dot(point);
// computing a point where a line that contains head and tail crosses the plane
float temp = -(v.dot(head) + D) / v.dot(v);
Vector3f p = head.add(v.x * temp, v.y * temp, v.z * temp);
// determining if the point p is on the same or other side of head than the tail point
Vector3f headToPointOnLineVector = p.subtract(head);
float headToPointLength = headToPointOnLineVector.length();
// the length of v is already = 1; cosinus should be either 1, 0 or -1
float cosinus = headToPointOnLineVector.dot(v) / headToPointLength;
if (cosinus < 0 && headToPointLength > boneHeadRadius || headToPointLength > boneLength + boneTailRadius) {
// the point is outside the anvelope
return false;
}
// now check if the point is inside and envelope
float pointDistanceFromLine = point.subtract(p).length(), maximumDistance = 0;
if (cosinus < 0) {
// checking if the distance from p to point is inside the half sphere defined by head envelope
// compute the distance from the line to the half sphere border
maximumDistance = boneHeadRadius;
} else if (headToPointLength < boneLength) {
// compute the maximum available distance
if (boneTailRadius > boneHeadRadius) {
// compute the distance from head to p
float headToPDistance = p.subtract(head).length();
// from tangens function we have
float x = headToPDistance * ((boneTailRadius - boneHeadRadius) / boneLength);
maximumDistance = x + boneHeadRadius;
} else if (boneTailRadius < boneHeadRadius) {
// compute the distance from head to p
float tailToPDistance = p.subtract(tail).length();
// from tangens function we have
float x = tailToPDistance * ((boneHeadRadius - boneTailRadius) / boneLength);
maximumDistance = x + boneTailRadius;
} else {
maximumDistance = boneTailRadius;
}
} else {
// checking if the distance from p to point is inside the half sphere defined by tail envelope
maximumDistance = boneTailRadius;
}
return pointDistanceFromLine <= maximumDistance + distance;
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class BoundingBox method containAABB.
/**
* <code>containAABB</code> creates a minimum-volume axis-aligned bounding
* box of the points, then selects the smallest enclosing sphere of the box
* with the sphere centered at the boxes center.
*
* @param points
* the list of points.
*/
public void containAABB(FloatBuffer points) {
if (points == null) {
return;
}
points.rewind();
if (// we need at least a 3 float vector
points.remaining() <= 2) {
return;
}
TempVars vars = TempVars.get();
float[] tmpArray = vars.skinPositions;
float minX = Float.POSITIVE_INFINITY, minY = Float.POSITIVE_INFINITY, minZ = Float.POSITIVE_INFINITY;
float maxX = Float.NEGATIVE_INFINITY, maxY = Float.NEGATIVE_INFINITY, maxZ = Float.NEGATIVE_INFINITY;
int iterations = (int) FastMath.ceil(points.limit() / ((float) tmpArray.length));
for (int i = iterations - 1; i >= 0; i--) {
int bufLength = Math.min(tmpArray.length, points.remaining());
points.get(tmpArray, 0, bufLength);
for (int j = 0; j < bufLength; j += 3) {
vars.vect1.x = tmpArray[j];
vars.vect1.y = tmpArray[j + 1];
vars.vect1.z = tmpArray[j + 2];
if (vars.vect1.x < minX) {
minX = vars.vect1.x;
}
if (vars.vect1.x > maxX) {
maxX = vars.vect1.x;
}
if (vars.vect1.y < minY) {
minY = vars.vect1.y;
}
if (vars.vect1.y > maxY) {
maxY = vars.vect1.y;
}
if (vars.vect1.z < minZ) {
minZ = vars.vect1.z;
}
if (vars.vect1.z > maxZ) {
maxZ = vars.vect1.z;
}
}
}
vars.release();
center.set(minX + maxX, minY + maxY, minZ + maxZ);
center.multLocal(0.5f);
xExtent = maxX - center.x;
yExtent = maxY - center.y;
zExtent = maxZ - center.z;
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class BoundingSphere method merge.
// /**
// * Merges this sphere with the given OBB.
// *
// * @param volume
// * The OBB to merge.
// * @return This sphere, after merging.
// */
// private BoundingSphere mergeOBB(OrientedBoundingBox volume) {
// // compute edge points from the obb
// if (!volume.correctCorners)
// volume.computeCorners();
// _mergeBuf.rewind();
// for (int i = 0; i < 8; i++) {
// _mergeBuf.put(volume.vectorStore[i].x);
// _mergeBuf.put(volume.vectorStore[i].y);
// _mergeBuf.put(volume.vectorStore[i].z);
// }
//
// // remember old radius and center
// float oldRadius = radius;
// Vector3f oldCenter = _compVect2.set( center );
//
// // compute new radius and center from obb points
// computeFromPoints(_mergeBuf);
// Vector3f newCenter = _compVect3.set( center );
// float newRadius = radius;
//
// // restore old center and radius
// center.set( oldCenter );
// radius = oldRadius;
//
// //merge obb points result
// merge( newRadius, newCenter, this );
//
// return this;
// }
private BoundingVolume merge(float temp_radius, Vector3f temp_center, BoundingSphere rVal) {
TempVars vars = TempVars.get();
Vector3f diff = temp_center.subtract(center, vars.vect1);
float lengthSquared = diff.lengthSquared();
float radiusDiff = temp_radius - radius;
float fRDiffSqr = radiusDiff * radiusDiff;
if (fRDiffSqr >= lengthSquared) {
if (radiusDiff <= 0.0f) {
vars.release();
return this;
}
Vector3f rCenter = rVal.center;
if (rCenter == null) {
rVal.setCenter(rCenter = new Vector3f());
}
rCenter.set(temp_center);
rVal.setRadius(temp_radius);
vars.release();
return rVal;
}
float length = (float) Math.sqrt(lengthSquared);
Vector3f rCenter = rVal.center;
if (rCenter == null) {
rVal.setCenter(rCenter = new Vector3f());
}
if (length > RADIUS_EPSILON) {
float coeff = (length + radiusDiff) / (2.0f * length);
rCenter.set(center.addLocal(diff.multLocal(coeff)));
} else {
rCenter.set(center);
}
rVal.setRadius(0.5f * (length + radius + temp_radius));
vars.release();
return rVal;
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class BoundingSphere method intersects.
/*
* (non-Javadoc)
*
* @see com.jme.bounding.BoundingVolume#intersectsOrientedBoundingBox(com.jme.bounding.OrientedBoundingBox)
*/
// public boolean intersectsOrientedBoundingBox(OrientedBoundingBox obb) {
// return obb.intersectsSphere(this);
// }
/*
* (non-Javadoc)
*
* @see com.jme.bounding.BoundingVolume#intersects(com.jme.math.Ray)
*/
public boolean intersects(Ray ray) {
assert Vector3f.isValidVector(center);
TempVars vars = TempVars.get();
Vector3f diff = vars.vect1.set(ray.getOrigin()).subtractLocal(center);
float radiusSquared = getRadius() * getRadius();
float a = diff.dot(diff) - radiusSquared;
if (a <= 0.0) {
vars.release();
// in sphere
return true;
}
// outside sphere
float b = ray.getDirection().dot(diff);
vars.release();
if (b >= 0.0) {
return false;
}
return b * b >= a;
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class Intersection method intersect.
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();
}
}
Aggregations