use of org.rajawali3d.bounds.BoundingSphere in project Rajawali by Rajawali.
the class A_nAABBTree method grow.
/**
* Grows the tree.
*/
protected void grow() {
RajLog.d("[" + this.getClass().getName() + "] Growing tree: " + this);
Vector3 min = new Vector3(Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE);
Vector3 max = new Vector3(-Float.MAX_VALUE, -Float.MAX_VALUE, -Float.MAX_VALUE);
//Get a full list of all the members, including members in the children
ArrayList<IGraphNodeMember> members = getAllMembersRecursively(true);
int members_count = members.size();
for (int i = 0; i < members_count; ++i) {
IBoundingVolume volume = members.get(i).getTransformedBoundingVolume();
Vector3 test_against_min = null;
Vector3 test_against_max = null;
if (volume == null) {
ATransformable3D object = (ATransformable3D) members.get(i);
test_against_min = object.getPosition();
test_against_max = test_against_min;
} else {
if (volume instanceof BoundingBox) {
BoundingBox bb = (BoundingBox) volume;
test_against_min = bb.getTransformedMin();
test_against_max = bb.getTransformedMax();
} else if (volume instanceof BoundingSphere) {
BoundingSphere bs = (BoundingSphere) volume;
Vector3 bs_position = bs.getPosition();
double radius = bs.getScaledRadius();
Vector3 rad = new Vector3();
rad.setAll(radius, radius, radius);
test_against_min = Vector3.subtractAndCreate(bs_position, rad);
test_against_max = Vector3.addAndCreate(bs_position, rad);
} else {
RajLog.e("[" + this.getClass().getName() + "] Received a bounding box of unknown type.");
throw new IllegalArgumentException("Received a bounding box of unknown type.");
}
}
if (test_against_min != null && test_against_max != null) {
if (test_against_min.x < min.x)
min.x = test_against_min.x;
if (test_against_min.y < min.y)
min.y = test_against_min.y;
if (test_against_min.z < min.z)
min.z = test_against_min.z;
if (test_against_max.x > max.x)
max.x = test_against_max.x;
if (test_against_max.y > max.y)
max.y = test_against_max.y;
if (test_against_max.z > max.z)
max.z = test_against_max.z;
}
}
mMin.setAll(min);
mMax.setAll(max);
mTransformedMin.setAll(min);
mTransformedMax.setAll(max);
calculatePoints();
calculateChildSideLengths();
if (mSplit) {
for (int i = 0; i < CHILD_COUNT; ++i) {
((Octree) mChildren[i]).setChildRegion(i, mChildLengths);
}
}
for (int i = 0; i < members_count; ++i) {
internalAddObject(members.get(i));
}
}
use of org.rajawali3d.bounds.BoundingSphere in project Rajawali by Rajawali.
the class A_nAABBTree method setBounds.
/**
* Sets the bounding volume of this node. This should only be called
* for a root node with no children. This sets the initial root node
* to have a volume ~8x the member, centered on the member.
*
* @param object IGraphNodeMember the member we will be basing
* our bounds on.
*/
protected void setBounds(IGraphNodeMember member) {
//RajLog.d("[" + this.getClass().getName() + "] Setting bounds based on member: " + member);
if (mMembers.size() != 0 && mParent != null) {
return;
}
IBoundingVolume volume = member.getTransformedBoundingVolume();
BoundingBox bcube = null;
BoundingSphere bsphere = null;
Vector3 position = member.getScenePosition();
double span_y = 0;
double span_x = 0;
double span_z = 0;
if (volume == null) {
span_x = 5.0;
span_y = 5.0;
span_z = 5.0;
} else {
if (volume instanceof BoundingBox) {
bcube = (BoundingBox) volume;
Vector3 min = bcube.getTransformedMin();
Vector3 max = bcube.getTransformedMax();
span_x = (max.x - min.x);
span_y = (max.y - min.y);
span_z = (max.z - min.z);
} else if (volume instanceof BoundingSphere) {
bsphere = (BoundingSphere) volume;
span_x = 2.0 * bsphere.getScaledRadius();
span_y = span_x;
span_z = span_x;
}
}
mMin.x = (float) (position.x - span_x);
mMin.y = (float) (position.y - span_y);
mMin.z = (float) (position.z - span_z);
mMax.x = (float) (position.x + span_x);
mMax.y = (float) (position.y + span_y);
mMax.z = (float) (position.z + span_z);
mTransformedMin.setAll(mMin);
mTransformedMax.setAll(mMax);
calculatePoints();
calculateChildSideLengths();
}
use of org.rajawali3d.bounds.BoundingSphere in project Rajawali by Rajawali.
the class RayPickingVisitor method apply.
public void apply(INode node) {
if (node instanceof Object3D) {
Object3D o = (Object3D) node;
if (!o.isVisible() || !o.isInFrustum())
return;
if (o.getGeometry().hasBoundingSphere()) {
BoundingSphere bsphere = o.getGeometry().getBoundingSphere();
bsphere.calculateBounds(o.getGeometry());
bsphere.transform(o.getModelMatrix());
if (intersectsWith(bsphere)) {
if (mPickedObject == null || (mPickedObject != null && o.getPosition().z < mPickedObject.getPosition().z))
mPickedObject = o;
}
} else {
// Assume bounding box if no bounding sphere found.
BoundingBox bbox = o.getGeometry().getBoundingBox();
bbox.calculateBounds(o.getGeometry());
bbox.transform(o.getModelMatrix());
if (intersectsWith(bbox)) {
if (mPickedObject == null || (mPickedObject != null && o.getPosition().z < mPickedObject.getPosition().z))
mPickedObject = o;
}
}
}
}
Aggregations