net.jinx.video.model
Class VertexUtil

java.lang.Object
  extended by net.jinx.video.model.VertexUtil

public class VertexUtil
extends java.lang.Object

Provides methods for manipulating raw vertex data as two dimensional float arrays. The first index for the array is the index of a particular vertex. The second index is the index of a coordinate in a vertex. Basically, it all boils down to vertex arrays being arrays of length three float arrays.

API Stability: Stable.


Field Summary
static int PORTION_ALPHA
          Indicates that the alpha portion of an image should be used.
static int PORTION_BLUE
          Indicates that the blue portion of an image should be used.
static int PORTION_GREEN
          Indicates that the green portion of an image should be used.
static int PORTION_LUMINANCE
          Indicates that the red, green and blue portions of an image should be used to produce luminance values based on the standard NTSC luminance conversion.
static int PORTION_RED
          Indicates that the red portion of an image should be used.
 
Method Summary
static float[][] buildCylinder(int slices, int stacks)
          Constructs a cylinder approximation mesh with a radius of one.
static float[][] buildHeightMapMesh(java.awt.image.BufferedImage map, int portion, float xDist, float yDist, float hFactor)
          Produces a Quads vertex array from a BufferedImage used as a height map.
static float[][] buildHeightMapTexCoord(java.awt.image.BufferedImage map)
           
static float[][] buildIcosahedron()
          Constructs an icosahedron mesh for a Triangles object.
static float[][] buildOctahedron()
          Constructs an octahedron mesh for a Triangles object.
static float[][] buildPlane(int slices, int stacks)
          Constructs an XZ planar mesh with unit width.
static float[][] buildPlaneTexCoord(int slices, int stacks)
          Constructs a planar texture coordinate array cooresponding to any of the vertex arrays produced by the build* methods.
static float[][] buildSphere(int slices, int stacks)
          Constructs a sphere approximation mesh with a radius of one.
static float[][] buildSphereMapMesh(java.awt.image.BufferedImage map, int portion)
          Builds a mesh for a height-mapped sphere.
static float[][] buildSphereMapMesh(java.awt.image.BufferedImage map, int portion, float hr)
          Builds a mesh for a height-mapped sphere.
static float[][] buildSphereMapMesh(java.awt.image.BufferedImage map, int portion, float hr, float pr)
          Builds a mesh for a height-mapped sphere.
static float[][] buildSphereMapTexCoord(java.awt.image.BufferedImage map)
           
static float[][] buildSphereTexCoord(int slices, int stacks)
          Constructs a texture coordinate array for the result of a call to buildSphere(int,int).
static float[][] buildTetrahedron()
          Constructs a tetrahedron mesh for a Triangles object.
static float[][] center(float[][] va)
          Calculates the center of va and adjusts it (by creating a new vertex array) so it is centered on the origin.
static float[][] copy(float[][] va)
           
static void copy(float[][] src, int srcPos, float[][] dst, int dstPos, int len)
          Makes a deep copy of a portion of a vertex array.
static float[] getCenter(float[][] va)
          Calculates and returns the center point of a vertex array.
static float getRadius(float[][] va, float[] center)
          Gets the minimum radius of sphere that will enclose the object described by the vertex array va.
static float[][] join(float[][] va1, float[][] va2)
          Constructs a new vertex array from all the vertices in both va1 and va2.
static float[][] join(float[][] va1, float[][] va2, float[][] va3)
          Constructs a new vertex array from all the vertices in both va1, va2 and va3.
static void normalize(float[][] va)
          Normalizes all the vertices in va.
static float[][] quadsToTriangles(float[][] quads)
          Converts a vertex array intended for use with a Quads object into one for use with a Triangles object.
static float[][] quadsToTrianglesTexCoord(float[][] tc, float[][] quads)
          Converts a texture coordinate array intended for use with a Quads object for use with a Triangles object.
static int reduce(float[][] va)
          Deprecated. This is unneeded, unless you really want to reduce memory usage as much as possible. This method simply takes too long to be worth it except on the most memory constrained hardware.
static float[][] reverseTriangles(float[][] va)
          Switches a vertex array containing data for a Triangles MeshElement from counter-clockwise to clockwise or vice-versa.
static float[][] rotate(float x, float y, float z, float[][] va)
          Rotates the given vertex array by the given values and returns the result.
static float[][] scale(float[] scale, float[][] va)
          Scales the given vertex array by the given values.
static float[][] scale(float r, float[][] va)
          Scales the given vertex array by the same value in all directions.
static float[][] scale(float xs, float ys, float zs, float[][] va)
          Scales the given vertex array by the given values.
static float[][] subdivideTriangles(float[][] va)
          Divides each of the triangles in va into 4 smaller triangles and returns the result.
static float[][] swapXY(float[][] va)
          Constructs a new vertex array from va with the X and Y axes swapped.
static float[][] swapXZ(float[][] va)
          Constructs a new vertex array from va with the X and Z axes swapped.
static float[][] swapYZ(float[][] va)
          Constructs a new vertex array from va with the Y and Z axes swapped.
static float[][] translate(float[] trans, float[][] va)
          Translates (shifts) the given vertex array by the given values.
static float[][] translate(float xt, float yt, float zt, float[][] va)
          Translates (shifts) the given vertex array by the given values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PORTION_RED

public static final int PORTION_RED
Indicates that the red portion of an image should be used.

See Also:
Constant Field Values

PORTION_GREEN

public static final int PORTION_GREEN
Indicates that the green portion of an image should be used.

See Also:
Constant Field Values

PORTION_BLUE

public static final int PORTION_BLUE
Indicates that the blue portion of an image should be used.

See Also:
Constant Field Values

PORTION_ALPHA

public static final int PORTION_ALPHA
Indicates that the alpha portion of an image should be used.

See Also:
Constant Field Values

PORTION_LUMINANCE

public static final int PORTION_LUMINANCE
Indicates that the red, green and blue portions of an image should be used to produce luminance values based on the standard NTSC luminance conversion.

See Also:
Constant Field Values
Method Detail

quadsToTrianglesTexCoord

public static float[][] quadsToTrianglesTexCoord(float[][] tc,
                                                 float[][] quads)
Converts a texture coordinate array intended for use with a Quads object for use with a Triangles object. NOTE: This method removes the texture coordinates associated with invalid triangles.

Parameters:
tc - the Quads-based texture coordinate array.
quads - the Quads-based vertex array.

quadsToTriangles

public static float[][] quadsToTriangles(float[][] quads)
Converts a vertex array intended for use with a Quads object into one for use with a Triangles object. NOTE: This method removes the resulting invalid triangles, if any exist. This normally only happens when a quad is pinched into a triangle. In this case, the linear, non-triangle portion of the quad is removed.

Parameters:
quads - the Quads-based vertex array.

copy

public static float[][] copy(float[][] va)

copy

public static void copy(float[][] src,
                        int srcPos,
                        float[][] dst,
                        int dstPos,
                        int len)
Makes a deep copy of a portion of a vertex array.


buildSphereTexCoord

public static float[][] buildSphereTexCoord(int slices,
                                            int stacks)
Constructs a texture coordinate array for the result of a call to buildSphere(int,int).

Parameters:
slices - the number of slices the sphere should have.
stacks - the number of stacks the sphere should have.

normalize

public static void normalize(float[][] va)
Normalizes all the vertices in va.

Parameters:
va - the vertex array. NOTE: va is modified directly by this method.

subdivideTriangles

public static float[][] subdivideTriangles(float[][] va)
Divides each of the triangles in va into 4 smaller triangles and returns the result.

Parameters:
va - a vertex array intended for a Triangles object.
Throws:
java.lang.IllegalArgumentException - if you pass in a vertex array whose length is not a multiple of 3.

buildTetrahedron

public static float[][] buildTetrahedron()
Constructs a tetrahedron mesh for a Triangles object.

Returns:
the vertex array for the mesh.

buildOctahedron

public static float[][] buildOctahedron()
Constructs an octahedron mesh for a Triangles object.

Returns:
the vertex array for the mesh.

buildIcosahedron

public static float[][] buildIcosahedron()
Constructs an icosahedron mesh for a Triangles object.

Returns:
the vertex array for the mesh.

buildSphere

public static float[][] buildSphere(int slices,
                                    int stacks)
Constructs a sphere approximation mesh with a radius of one. The mesh is constructed for a Quads object.

Parameters:
slices - the number of slices the sphere should have.
stacks - the number of stacks the sphere should have.

buildCylinder

public static float[][] buildCylinder(int slices,
                                      int stacks)
Constructs a cylinder approximation mesh with a radius of one. The mesh is constructed for a Quads object.

Parameters:
slices - the number of slices the cylinder should have.
stacks - the number of stacks the cylinder should have.

buildPlaneTexCoord

public static float[][] buildPlaneTexCoord(int slices,
                                           int stacks)
Constructs a planar texture coordinate array cooresponding to any of the vertex arrays produced by the build* methods. In short, the result of this method is a set of texture coordinates that are appropriate to a Quads object. The result of this method can be used to texture map almost any quadric-like object, as long as the slices and stacks match.

Parameters:
slices - the number of slices the plane should have.
stacks - the number of stacks the plane should have.

buildPlane

public static float[][] buildPlane(int slices,
                                   int stacks)
Constructs an XZ planar mesh with unit width. The mesh is constructed for a Quads object.

Parameters:
slices - the number of slices the plane should have.
stacks - the number of stacks the plane should have.

buildSphereMapTexCoord

public static float[][] buildSphereMapTexCoord(java.awt.image.BufferedImage map)

buildSphereMapMesh

public static float[][] buildSphereMapMesh(java.awt.image.BufferedImage map,
                                           int portion)
Builds a mesh for a height-mapped sphere. This builds a Quads mesh.

Parameters:
portion - the PORTION_* constant denoting which portion of the image to use for the height values.
map - the BufferedImage to use to get height values from.

buildSphereMapMesh

public static float[][] buildSphereMapMesh(java.awt.image.BufferedImage map,
                                           int portion,
                                           float hr)
Builds a mesh for a height-mapped sphere. This builds a Quads mesh.

Parameters:
portion - the PORTION_* constant denoting which portion of the image to use for the height values.
map - the BufferedImage to use to get height values from.
hr - the height ratio; this determines the maximum depth for the darker areas of the map. This should be a value between zero and one.
Throws:
java.lang.IllegalArgumentException - if hr is greater than one or less than zero.

buildSphereMapMesh

public static float[][] buildSphereMapMesh(java.awt.image.BufferedImage map,
                                           int portion,
                                           float hr,
                                           float pr)
Builds a mesh for a height-mapped sphere. This builds a Quads mesh.

Parameters:
portion - the PORTION_* constant denoting which portion of the image to use for the height values.
map - the BufferedImage to use to get height values from.
hr - the height ratio; this determines the maximum depth for the darker areas of the map. This should be a value between zero and one.
pr - the polar rolloff factor; anything this distance away from the equator along the z axis on the unit sphere will begin to use a weighted average of the average of the pixels in the map on that row and the particular pixel that's been mapped (for height). The weighting of the two is based on distance between the rolloff point and the pole, with the rolloff point being 100% the pixel and the pole being 100% the average of the row. The top and bottom points on the mapped sphere will always use the average of the points on the row.
Throws:
java.lang.IllegalArgumentException - if hr is greater than one or less than zero.

buildHeightMapTexCoord

public static float[][] buildHeightMapTexCoord(java.awt.image.BufferedImage map)

buildHeightMapMesh

public static float[][] buildHeightMapMesh(java.awt.image.BufferedImage map,
                                           int portion,
                                           float xDist,
                                           float yDist,
                                           float hFactor)
Produces a Quads vertex array from a BufferedImage used as a height map. This can be used to produce terrain from an image.

Parameters:
map - a BufferedImage that is to be used as a height map.
portion - indicates which portion of the image to use for height data. This can be any one of the PORTION_* constants.
xDist - the distance in the X direction between each vertex.
yDist - the distance in the Y direction between each vertex.
hFactor - the height multiplier.
Returns:
the resulting vertex array of triangles.

reverseTriangles

public static float[][] reverseTriangles(float[][] va)
Switches a vertex array containing data for a Triangles MeshElement from counter-clockwise to clockwise or vice-versa. This works by duplicating the array, so the original is untouched.
For every three vertices, the second and third vertex will be swapped in the resulting array. If the end of the array (after all the available sets of three are complete) is only one or two vertices, then they'll be left alone.

Parameters:
va - the vertex array to use for source data.
Returns:
a new vertex array produced from the data in the source array.

scale

public static float[][] scale(float r,
                              float[][] va)
Scales the given vertex array by the same value in all directions. The original array not modified.

Parameters:
r - the value to resize by.
va - the vertex array.
Returns:
the scaled version.

scale

public static float[][] scale(float[] scale,
                              float[][] va)
Scales the given vertex array by the given values. The original array not modified.

Parameters:
scale - a length three float array to use for scaling values.
va - the vertex array.
Returns:
a new vertex array constructed from the scaled values.

scale

public static float[][] scale(float xs,
                              float ys,
                              float zs,
                              float[][] va)
Scales the given vertex array by the given values. The original array not modified.

Parameters:
xs - the value to scale the vertices by along the x axis.
ys - the value to scale the vertices by along the y axis.
zs - the value to scale the vertices by along the z axis.
va - the vertex array.
Returns:
a new vertex array constructed from the scaled values.

translate

public static float[][] translate(float[] trans,
                                  float[][] va)
Translates (shifts) the given vertex array by the given values. The original array is not modified.

Parameters:
trans - the vector to add to each vertex.
va - the vertex array.
Returns:
a new vertex array constructed from the translated values.

center

public static float[][] center(float[][] va)
Calculates the center of va and adjusts it (by creating a new vertex array) so it is centered on the origin.

Parameters:
va - the vertex array.

translate

public static float[][] translate(float xt,
                                  float yt,
                                  float zt,
                                  float[][] va)
Translates (shifts) the given vertex array by the given values. The original array is not modified.

Parameters:
xt - the value to add to all x values in the vertex array.
yt - the value to add to all y values in the vertex array.
zt - the value to add to all z values in the vertex array.
va - the vertex array.
Returns:
a new vertex array constructed from the translated values.

rotate

public static float[][] rotate(float x,
                               float y,
                               float z,
                               float[][] va)
Rotates the given vertex array by the given values and returns the result. The original array is not modified.

Parameters:
x - the angle around the x axis by which to rotate.
y - the angle around the y axis by which to rotate.
z - the angle around the z axis by which to rotate.
va - the vertex array.
Returns:
a new vertex array constructed from the rotated values.

join

public static float[][] join(float[][] va1,
                             float[][] va2)
Constructs a new vertex array from all the vertices in both va1 and va2.

Parameters:
va1 - the first vertex array.
va2 - the second vertex array.
Returns:
the nex vertex array.

join

public static float[][] join(float[][] va1,
                             float[][] va2,
                             float[][] va3)
Constructs a new vertex array from all the vertices in both va1, va2 and va3.

Parameters:
va1 - the first vertex array.
va2 - the second vertex array.
va3 - the second vertex array
Returns:
the nex vertex array.

reduce

public static int reduce(float[][] va)
Deprecated. This is unneeded, unless you really want to reduce memory usage as much as possible. This method simply takes too long to be worth it except on the most memory constrained hardware.

Reduces the number of vertices in a vertex array by comparing them with each other and replacing duplicates with references to the first version in the array. This should reduce the overall memory required for the array, but this operation can take a great deal of time! If you're not that worried about RAM usage, I suggest you not bother with this.

Parameters:
va - the vertex array to reduce.
Returns:
the number of vertices that were reduced.

swapXY

public static float[][] swapXY(float[][] va)
Constructs a new vertex array from va with the X and Y axes swapped.

Parameters:
va - the vertex array.
Returns:
the new vertex array.

swapXZ

public static float[][] swapXZ(float[][] va)
Constructs a new vertex array from va with the X and Z axes swapped.

Parameters:
va - the vertex array.
Returns:
the new vertex array.

swapYZ

public static float[][] swapYZ(float[][] va)
Constructs a new vertex array from va with the Y and Z axes swapped.

Parameters:
va - the vertex array.
Returns:
the new vertex array.

getRadius

public static float getRadius(float[][] va,
                              float[] center)
Gets the minimum radius of sphere that will enclose the object described by the vertex array va. The center is assumed to be at the origin.

Parameters:
va - the vertex array.
Returns:
the radius of the sphere.

getCenter

public static float[] getCenter(float[][] va)
Calculates and returns the center point of a vertex array. This uses the mid-point equation to determine the center.

Parameters:
va - the vertex array.
Returns:
a length three float array that represents the center of the object.