net.java.dev.joode.geom
Class TriMesh

java.lang.Object
  extended by net.java.dev.joode.geom.Geom
      extended by net.java.dev.joode.geom.TriMesh
All Implemented Interfaces:
java.io.Serializable, SimState

public class TriMesh
extends Geom

Represents a geom as a collection of triangular facets.

Following the Xith3d and Java3d conventions, vertices are ordered counterclockwise as viewed from the front of the face.

Following the GIMPACT and ODE conventions, an oriented plane is represented by the coordinates (a,b,c,d) such that a x + b y + c z - d = 0 is satisfied by a point (x,y,z) on the plane, and (a,b,c) is a normal vector pointing in the direction the plane is facing. Note that elsewhere, the convention a x + b y + c z + d = 0 (i.e., opposite sign for d) is sometimes used; not here. Plane coordinates a, b, c, d are stored in a Vector4f as members x, y, z, and w, respectively.

This implementation is derived from GIMPACT, a software library for geometry processing and collision detection. GIMPACT source code is used under the following license.

Copyright (c) 2006, Francisco Le�n.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Author:
Art Pope
See Also:
Serialized Form

Nested Class Summary
static class TriMesh.TriangleData
          Encapsulates working information about a triangle.
static class TriMesh.TrianglePlanes
          Represents several planes associated with a some triangle.
 
Field Summary
 
Fields inherited from class net.java.dev.joode.geom.Geom
aabb, AABB_MAX_X, AABB_MAX_Y, AABB_MAX_Z, AABB_MIN_X, AABB_MIN_Y, AABB_MIN_Z, body, category_bits, collide_bits, dBoxClass, dCapsuleClass, dCircle, dConvex, dCylinderClass, dFirstSpaceClass, dFirstUserClass, dGeomNumClasses, dGeomTransformClass, dHashSpaceClass, dLastSpaceClass, dLastUserClass, dMaxUserClasses, dOctTreeSpaceClass, dPlaneClass, dQuadTreeSpaceClass, dRayClass, dRectangle, dSimpleSpaceClass, dSphereClass, dTriMeshClass, GEOM_AABB_BAD, GEOM_DIRTY, GEOM_ENABLED, GEOM_PLACEABLE, gflags, lpos, parent_space, pos, R, RAY_BACKFACECULL, RAY_CLOSEST_HIT, RAY_FIRSTCONTACT, tmpq, type
 
Constructor Summary
TriMesh(Space space, boolean placeable, Point3f[] vertices, int[] indices)
          Constructs a TriMesh with specified sets of vertices and triangles.
 
Method Summary
 TriMesh cloneState(ClonedReferences util)
           deep clones the state object, any references to other SimState objects should be cloned and assigned to the clone via the ClonedReferences util object.
 void computeAABB()
          Updates the AABB for the whole mesh.
static boolean crossesPlane(Point3f[] vertices, Vector4f plane)
          Tests whether a triangle crosses a plane.
static float distanceFromPlane(Point3f point, Vector4f plane)
          Computes the signed distance of a point from a plane.
 AABBSet getAABBSet()
          Gets the set of per-triangle AABBs.
 int[] getIndices()
          Gets the indices of the triangle vertices.
 void getTriangleData(int triangle, TriMesh.TriangleData data)
          Gets information about a particular triangle.
 Point3f[] getVertices()
          Gets the array of triangle vertex coordinates.
static boolean lineIntersectsPlane(Point3f s1, Point3f s2, Vector4f plane, Point3f intersection)
          Tests whether a line intersects a plane.
 java.lang.Iterable<Point3f[]> triangles()
          Gets an Iterable for iterating over triangles of the mesh.
 
Methods inherited from class net.java.dev.joode.geom.Geom
AABBTest, clonePartialGeom, destroy, getAABB, getUserData, isEnabled, recomputeAABB, setBody, setPosition, setQuaternion, setQuaternion, setRotation, setUserData
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TriMesh

public TriMesh(Space space,
               boolean placeable,
               Point3f[] vertices,
               int[] indices)
Constructs a TriMesh with specified sets of vertices and triangles.

Parameters:
space - a space to which this geom will be added, or nullplaceable - whether the geom is to be dynamically repositioned by means of its Geom.pos and Geom.R members.
vertices - the coordinates of each triangle vertex. The triangles themselves are identified by indices using indices into this array.
indices - identifies the vertices of each triangle. Each group of three consecutive integers in this array identifies, by index, the three vertices in the vertices array that make up one triangle .
Method Detail

getVertices

public Point3f[] getVertices()
Gets the array of triangle vertex coordinates.

Returns:
the array of triangle vertex coordinates.
See Also:
getIndices()

getIndices

public int[] getIndices()
Gets the indices of the triangle vertices.

Returns:
an array of vertex indices. Each group of three consecutive elements identifies, by index, the three vertices in the vertices array that make up one triangle.
See Also:
getVertices()

triangles

public java.lang.Iterable<Point3f[]> triangles()
Gets an Iterable for iterating over triangles of the mesh. Note that the iterator returns the triangles with their original vertex positions, not the ones as transformed by Geom.pos and Geom.R.

 for (Point3f [] triangle : mesh.triangles()) {
     // vertices are triangle[0], triangle1], triangle[2]
 }
 


getAABBSet

public AABBSet getAABBSet()
Gets the set of per-triangle AABBs.


computeAABB

public void computeAABB()
Updates the AABB for the whole mesh.

As a side-effect, it also re-applies the translation and rotation transforms to the triangle vertices in case they have changed, and clears the cache of triangle plane information.

Specified by:
computeAABB in class Geom

getTriangleData

public void getTriangleData(int triangle,
                            TriMesh.TriangleData data)
Gets information about a particular triangle.

Parameters:
triangle - the index of the triangle of interest.
data - a caller-supplied TriangleData object that this method will fill with information about the triangle.

distanceFromPlane

public static float distanceFromPlane(Point3f point,
                                      Vector4f plane)
Computes the signed distance of a point from a plane.

Parameters:
point - the point
plane - the coordinates of the plane
Returns:
the signed perpendicular distance of the point from the plane. It is negative if the point lies behind the plane as determined by the plane's normal vector.

crossesPlane

public static boolean crossesPlane(Point3f[] vertices,
                                   Vector4f plane)
Tests whether a triangle crosses a plane.

Parameters:
vertices - the triangle's three vertices
plane - the coordinates of the plane
Returns:
true iff vertices of the triangle lie on both sides of the plane, or at least one vertex lies in the plane

lineIntersectsPlane

public static boolean lineIntersectsPlane(Point3f s1,
                                          Point3f s2,
                                          Vector4f plane,
                                          Point3f intersection)
Tests whether a line intersects a plane.

Parameters:
s1 - one point on the line
s2 - another point on the line
plane - the coordinates of the plane
intersection - returns the coordinates of the point of intersection
Returns:
true if the line containing the points s1 and s2 intersects the plane

cloneState

public TriMesh cloneState(ClonedReferences util)
Description copied from interface: SimState

deep clones the state object, any references to other SimState objects should be cloned and assigned to the clone via the ClonedReferences util object. The util object ensures that multiple clones of the same object are not performed during a clone operation.

Parameters:
util - instanciate a new one if top level cloning