hmi.graphics.scenegraph
Class GMesh

java.lang.Object
  extended by hmi.xml.XMLStructureAdapter
      extended by hmi.graphics.scenegraph.GMesh
All Implemented Interfaces:
XMLStructure
Direct Known Subclasses:
GSkinnedMesh, GSphere

public class GMesh
extends XMLStructureAdapter

A GMesh object defines methods for defining and retrieving mesh data arrays and index arrays, as well as methods for converting between various representation schemes. A GMesh is not dependend on any particular render technology, nor on any particular graphics file format. Rather, it is intended to be an intermediate internal format, for easy and efficient exchange of graphical mesh data. The following schemes can be used to specify mesh data: 1) specify just float data arrays d0, d1, ... dn, without index data. 2) specify float data arrays d0, d1, ..., dn and a single index array i, 3) specify float data arrays d0, d1, ..., dn and similar index arrays i0, i1, ..., in. By the size of a data array for some named attribute, we mean the length of that array divided by the attribute size. For instance, a float array of length 300 defines a data array of size 100 if the attribute size would be 3, like for instance for 3D coordinates or normals. For texture coordinates with attribute size 2, on the other hand, the size would be 300/2=150. (For index arrays, there is no distinction between array length and size of the data.) For case 1) and case 2) above, we require that the sizes of all the data arrays are the same. For case 3), we require that the size of all the index arrays is the same, and of course, the values of the indices within ik should be smaller than the size of dk. The mesh topology can be defined in several ways: the default is to assume a triangulated mesh, where every three consecutive indices (or consecutive attributes for case 1) form a triangle. An alternative is work with polygons, by specifying the vcounts data. The latter is an array specifying, for each polygon, the number of consecutive indices. polygons with "holes" are not supported. Addition for morph targets: In addition to a "base" mesh we can have a number vof named morph targets, each with its own set of attributes. It is allowed and expected that morph targets introduce new vertex positions and normals, but not new tex coords etc.

Author:
Job Zwiers

Nested Class Summary
static class GMesh.MeshType
          MeshType enumerates the legal mesh geometry, like Triangles, Tristrips, Polygons, etc.
 
Field Summary
private  ArrayList<VertexAttribute> attributeList
           
private static int ATTRIBUTELIST_SIZE
           
private static String[] attrOrder
           
private  String id
           
private  int[] indexData
           
private static int INDICESPERLINE
           
private static org.slf4j.Logger logger
           
private  GMesh.MeshType meshType
           
private  ArrayList<ArrayList<VertexAttribute>> morphAttributeLists
           
private  String[] morphTargets
           
private  int nrOfVertices
           
private static boolean showGMeshData
           
private static double TUPLEINDEX_GROW_GUESTIMATE
           
private  boolean unifiedIndexData
           
private  int[] vcounts
           
private static int VERTEXCOORD_SIZE
           
private static String XMLTAG
           
 
Fields inherited from class hmi.xml.XMLStructureAdapter
ATTRIBUTE_TOKEN_DELIMITERS, COMMA_SEPARATOR, DECODEDARRAYSIZE, DEFAULT_RECOVER_MODE, NEWLINE, SYSTEMNEWLINE, TAB, TAB_STRING
 
Constructor Summary
GMesh()
          Default constructor
GMesh(GMesh base)
          Creates a clone of the specified base GMesh.
GMesh(XMLTokenizer tokenizer)
          Creates a new GMesh and reads the data from the XMLTokenizer.
 
Method Summary
private  void addVertexAttribute(int morphTarget, VertexAttribute attr)
           
 void affineTransform(float[] mat4x4)
          Transforms the mesh attributes with specific names: VertexCoord, Normal Assumption: transformMatrix is a 4x4 matrix, in row major order.
 StringBuilder appendAttributeString(StringBuilder buf, XMLFormatting fmt)
          appends the XML attributes to buf.
 StringBuilder appendContent(StringBuilder buf, XMLFormatting fmt)
          Appends content part of XML encoding
private  int attrOrd(String attrName)
           
protected  int[] calculateTuples()
          calculates tuples for all new indices, together with the data remapping tables.
 boolean checkIndexIntegrity()
          scales the mesh attributes with specific names: VertexCoord, Normal
 boolean checkMorphTargetConsistency(int morphTarget, String attrName)
           
 boolean checkMorphTargetConsistency(String attrName)
           
 boolean checkTriangleIntegrity(float minSize)
           
 void cleanupTriangles(float minSize)
           
 void decodeAttributes(HashMap<String,String> attrMap, XMLTokenizer tokenizer)
          decodes the XML attributes.
 void decodeContent(XMLTokenizer tokenizer)
          Decodes content part of XML encoding
 int[] getAttributeIndexData(int morphTarget, String attributeName)
          Returns the index int array for a named attribute.
 int[] getAttributeIndexData(String attributeName)
          Returns the index int array for a named attribute.
 String getId()
          returns the GMesh id, possibly null
 int[] getIndexData()
          Returns the common index data array, which could be null.
 GMesh.MeshType getMeshType()
          Returns the GMesh.MeshType of this GMesh.
 float[][] getMorphData(String semantic)
          returns the morph data arrays for a specified attribute name.
 String[] getMorphTargets()
          Returns the String array with morph target names, or null when no morph targets have been defined.
 int getNrOfAttributes()
          Returns the number of vertex attributes
 int getNrOfIndices()
          Returns the length of the number of common indices, i.e. the length of the indexData array.
 int getNrOfVertices()
          Returns the number of distinct vertices.
 int[] getVCountData()
          Returns the polgon/polylist vertex counts data: Each array element specifies the number of vertices for one polygon.
 VertexAttribute getVertexAttribute(int morphTarget, String attributeName)
          Retrieves a named attribute; this could be null if the attribute is not defined for this GMesh.
 VertexAttribute getVertexAttribute(String attributeName)
          Retrieves a named attribute; this could be null if the attribute is not defined for this GMesh.
 ArrayList<VertexAttribute> getVertexAttributeList()
          Returns the list with VertexAttributes for this GMesh
 ArrayList<VertexAttribute> getVertexAttributeList(int morphTarget)
          Returns the list with VertexAttributes for this GMesh
 List<String> getVertexAttributeNameList(int morphTarget)
          Returns the list with VertexAttribute names for this GMesh
 float[] getVertexData(int morphTarget, String attributeName)
          Returns the vertex data float array for a named attribute.
 float[] getVertexData(String attributeName)
          Returns the vertex data float array for a named attribute.
 String getXMLTag()
          returns the XML Stag for XML encoding
 boolean hasUnifiedIndexData()
          returns true when a single, i.e. shared, index is used for all vertexattributes.
 void linearTransform(float[] mat3x3)
          Transforms the mesh attributes with specific names: VertexCoord, Normal Assumption: transformMatrix is a 4x4 matrix, in row major order.
 int morphListSize()
          returns the number of morph targets, or 0 when no morph targets have been defined
private  VertexAttribute requestVertexAttribute(int morphTarget, String attributeName)
           
 void setId(String id)
          Defines the GMesh id
 void setIndexData(int[] indexData)
          Defines the common index data array.
 void setIndexedVertexData(int morphTarget, String attributeName, int dataElementSize, float[] vertexData, int[] vertexIndexData)
          Defines the indexed vertex data for a named attribute.
 void setIndexedVertexData(String attributeName, int dataElementSize, float[] vertexData, int[] indexData)
          Defines the indexed vertex data for a named attribute.
 void setMeshType(GMesh.MeshType t)
          Sets the GMesh.MeshType of this GMesh, like Undefined, Triangles, etc.
 void setMorphTargets(String[] morphTargets)
          Defines the names of the morph targets for this GMesh and allocates (empty) vertex attribute lists for each of these.
 void setVCountData(int[] counts)
          Defines the polgon/polylist vertex counts data: Each array element specifies the number of vertices for one polygon.
 void setVertexData(int morphTarget, String attributeName, int dataElementSize, float[] vertexData)
          Defines the (non-indexed) vertex data for a named attribute.
 void setVertexData(String attributeName, int dataElementSize, float[] vertexData)
          Defines the (non-indexed) vertex data for a named attribute.
static void showGMeshData(boolean show)
           
 void triangulate()
          Ensures that all polgons are transformed into triangles.
 void unifyIndices()
          Removes all attribute-specific indices, and replaces them by a single, common, indexData array.
static String xmlTag()
          The XML Stag for XML encoding
 
Methods inherited from class hmi.xml.XMLStructureAdapter
appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttribute, appendAttributes, appendAttributes, appendAttributeString, appendBooleans, appendCloseEmptyTag, appendCloseSTag, appendComment, appendContent, appendDoubleElement, appendEmptyTag, appendEmptyTag, appendEmptyTag, appendEmptyTag, appendEmptyTag, appendEmptyTag, appendETag, appendETag, appendFloatArrayElement, appendFloatElement, appendFloats, appendIntArrayElement, appendIntElement, appendInts, appendLongElement, appendNewLine, appendNewLine, appendNewLine, appendOpenSTag, appendOptionalDoubleElement, appendOptionalFloatElement, appendOptionalIntElement, appendOptionalLongElement, appendSpaces, appendSpaces, appendSTag, appendSTag, appendSTag, appendStringArrayElement, appendStrings, appendSystemNewLine, appendTab, appendTextElement, appendXML, appendXML, appendXML, appendXML, appendXML, appendXMLStructure, appendXMLStructureList, appendXMLTextElementList, countTokens, countTokens, decodeAttribute, decodeAttribute, decodeBoolean, decodeBooleanArray, decodeBooleanArray, decodeBooleanArray, decodeBooleanArray, decodeDouble, decodeDoubleArray, decodeDoubleArray, decodeDoubleArray, decodeDoubleArray, decodeFloat, decodeFloatArray, decodeFloatArray, decodeFloatArray, decodeFloatArray, decodeInt, decodeIntArray, decodeIntArray, decodeIntArray, decodeIntArray, decodeLong, decodeStringArray, decodeStringArray, decodeStringArray, decodeStringArray, decodeXMLStructureList, decodeXMLValueElement, getNamespace, getOptionalAttribute, getOptionalAttribute, getOptionalBooleanAttribute, getOptionalDoubleAttribute, getOptionalFloatAttribute, getOptionalIntAttribute, getOptionalLongAttribute, getRequiredAttribute, getRequiredBooleanAttribute, getRequiredDoubleAttribute, getRequiredFloatAttribute, getRequiredIntAttribute, getRequiredLongAttribute, getTagLine, hasContent, parseXMLElement, postProcess, preProcess, readXML, readXML, readXML, setConsoleAttributeEnabled, setDefaultRecoverMode, setRecoverMode, spaces, toString, toXMLString, toXMLString, toXMLString, toXMLString, toXMLString, toXMLString, writeXML, writeXML, writeXML, writeXML
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ATTRIBUTELIST_SIZE

private static final int ATTRIBUTELIST_SIZE
See Also:
Constant Field Values

id

private String id

meshType

private GMesh.MeshType meshType

attributeList

private ArrayList<VertexAttribute> attributeList

morphTargets

private String[] morphTargets

morphAttributeLists

private ArrayList<ArrayList<VertexAttribute>> morphAttributeLists

vcounts

private int[] vcounts

indexData

private int[] indexData

nrOfVertices

private int nrOfVertices

unifiedIndexData

private boolean unifiedIndexData

logger

private static org.slf4j.Logger logger

attrOrder

private static String[] attrOrder

VERTEXCOORD_SIZE

private static final int VERTEXCOORD_SIZE
See Also:
Constant Field Values

TUPLEINDEX_GROW_GUESTIMATE

private static final double TUPLEINDEX_GROW_GUESTIMATE
See Also:
Constant Field Values

showGMeshData

private static boolean showGMeshData

INDICESPERLINE

private static final int INDICESPERLINE
See Also:
Constant Field Values

XMLTAG

private static final String XMLTAG
See Also:
Constant Field Values
Constructor Detail

GMesh

public GMesh()
Default constructor


GMesh

public GMesh(XMLTokenizer tokenizer)
      throws IOException
Creates a new GMesh and reads the data from the XMLTokenizer.

Throws:
IOException

GMesh

public GMesh(GMesh base)
Creates a clone of the specified base GMesh. Data is not copied, rather this GMesh and the specified base Gmesh will share data arrays.

Method Detail

setId

public void setId(String id)
Defines the GMesh id


getId

public String getId()
returns the GMesh id, possibly null


setMeshType

public void setMeshType(GMesh.MeshType t)
Sets the GMesh.MeshType of this GMesh, like Undefined, Triangles, etc.


getMeshType

public GMesh.MeshType getMeshType()
Returns the GMesh.MeshType of this GMesh.


setMorphTargets

public void setMorphTargets(String[] morphTargets)
Defines the names of the morph targets for this GMesh and allocates (empty) vertex attribute lists for each of these.


getMorphData

public float[][] getMorphData(String semantic)
returns the morph data arrays for a specified attribute name. So getMorphData("mcPosition")[2] is the float array with vertex data for the "mcPosition" attribute of the morph target with index 2.


getMorphTargets

public String[] getMorphTargets()
Returns the String array with morph target names, or null when no morph targets have been defined.


morphListSize

public int morphListSize()
returns the number of morph targets, or 0 when no morph targets have been defined


requestVertexAttribute

private VertexAttribute requestVertexAttribute(int morphTarget,
                                               String attributeName)

addVertexAttribute

private void addVertexAttribute(int morphTarget,
                                VertexAttribute attr)

attrOrd

private int attrOrd(String attrName)

getVertexAttribute

public VertexAttribute getVertexAttribute(String attributeName)
Retrieves a named attribute; this could be null if the attribute is not defined for this GMesh. This method can be used for fixed-function OpenGL style attributes as well as for user-defined attributes.


getVertexAttribute

public VertexAttribute getVertexAttribute(int morphTarget,
                                          String attributeName)
Retrieves a named attribute; this could be null if the attribute is not defined for this GMesh. This method can be used for fixed-function OpenGL style attributes as well as for user-defined attributes.


getVertexAttributeList

public ArrayList<VertexAttribute> getVertexAttributeList()
Returns the list with VertexAttributes for this GMesh


getVertexAttributeList

public ArrayList<VertexAttribute> getVertexAttributeList(int morphTarget)
Returns the list with VertexAttributes for this GMesh


getVertexAttributeNameList

public List<String> getVertexAttributeNameList(int morphTarget)
Returns the list with VertexAttribute names for this GMesh


checkMorphTargetConsistency

public boolean checkMorphTargetConsistency(String attrName)

checkMorphTargetConsistency

public boolean checkMorphTargetConsistency(int morphTarget,
                                           String attrName)

setVertexData

public void setVertexData(String attributeName,
                          int dataElementSize,
                          float[] vertexData)
Defines the (non-indexed) vertex data for a named attribute. This includes the floating point data as well attribute size, but no index data The attribute size specifies the number of floats per attribute.


setVertexData

public void setVertexData(int morphTarget,
                          String attributeName,
                          int dataElementSize,
                          float[] vertexData)
Defines the (non-indexed) vertex data for a named attribute. This includes the floating point data as well attribute size, but no index data The attribute size specifies the number of floats per attribute.


setIndexedVertexData

public void setIndexedVertexData(String attributeName,
                                 int dataElementSize,
                                 float[] vertexData,
                                 int[] indexData)
Defines the indexed vertex data for a named attribute. (Assumes that global index data is not being used.) This includes the floating point data as well as index data and attribute size. The attribute size specifies the number of floats per attribute.


setIndexedVertexData

public void setIndexedVertexData(int morphTarget,
                                 String attributeName,
                                 int dataElementSize,
                                 float[] vertexData,
                                 int[] vertexIndexData)
Defines the indexed vertex data for a named attribute. (Assumes that global index data is not being used.) This includes the floating point data as well as index data and attribute size. The attribute size specifies the number of floats per attribute. The morphtarget parameter can be -1, in which case there is no morph target. Similarly, the vertex attribute set can be -1, to denote absence of any set assignment.


getVertexData

public float[] getVertexData(String attributeName)
Returns the vertex data float array for a named attribute. Returns null if either the attribute was not defined, or no vertexData for that attribute was defined.


getVertexData

public float[] getVertexData(int morphTarget,
                             String attributeName)
Returns the vertex data float array for a named attribute. Returns null if either the attribute was not defined, or no vertexData for that attribute was defined.


getAttributeIndexData

public int[] getAttributeIndexData(String attributeName)
Returns the index int array for a named attribute. Returns null if either the attribute was not defined at all, or when no indices for the attribute are defined.


getAttributeIndexData

public int[] getAttributeIndexData(int morphTarget,
                                   String attributeName)
Returns the index int array for a named attribute. Returns null if either the attribute was not defined at all, or when no indices for the attribute are defined.


getIndexData

public int[] getIndexData()
Returns the common index data array, which could be null. It is assumed that unified i ndices are being used.


setIndexData

public void setIndexData(int[] indexData)
Defines the common index data array. (Assumes that attribute index data is not being used.)


getNrOfAttributes

public int getNrOfAttributes()
Returns the number of vertex attributes


getNrOfIndices

public int getNrOfIndices()
Returns the length of the number of common indices, i.e. the length of the indexData array. If the latter is null, -1 is returned.


getNrOfVertices

public int getNrOfVertices()
Returns the number of distinct vertices. If individual attribute indices have been used, the GMesh will first apply a unifyIndices operation.


setVCountData

public void setVCountData(int[] counts)
Defines the polgon/polylist vertex counts data: Each array element specifies the number of vertices for one polygon.


getVCountData

public int[] getVCountData()
Returns the polgon/polylist vertex counts data: Each array element specifies the number of vertices for one polygon. Returns null if no VCount data has been set, or if the mesh has been triangulated.


affineTransform

public void affineTransform(float[] mat4x4)
Transforms the mesh attributes with specific names: VertexCoord, Normal Assumption: transformMatrix is a 4x4 matrix, in row major order. The translation part of it will be applied to * the vertex coordinates, but not to the normals. Assumption two: the transform matrix is a rotation and/or translation Scaling is ok, but will result in normal vectors that are no longer unit length.


linearTransform

public void linearTransform(float[] mat3x3)
Transforms the mesh attributes with specific names: VertexCoord, Normal Assumption: transformMatrix is a 4x4 matrix, in row major order. The translation part of it will be applied to * the vertex coordinates, but not to the normals. Assumption two: the transform matrix is a rotation and/or translation Scaling is ok, but will result in normal vectors that are no longer unit length.


checkIndexIntegrity

public boolean checkIndexIntegrity()
scales the mesh attributes with specific names: VertexCoord, Normal


checkTriangleIntegrity

public boolean checkTriangleIntegrity(float minSize)

cleanupTriangles

public void cleanupTriangles(float minSize)

triangulate

public void triangulate()
Ensures that all polgons are transformed into triangles. So, the type of the mesh is modified from PolyList to Triangles. Polygons, that could include holes, are not allowed. Polgons need not be convex, however. It is assumed that common indices are used. Else, if individual attribute indices have been used, the GMesh will first apply a unifyIndices operation.


hasUnifiedIndexData

public boolean hasUnifiedIndexData()
returns true when a single, i.e. shared, index is used for all vertexattributes.


unifyIndices

public void unifyIndices()
Removes all attribute-specific indices, and replaces them by a single, common, indexData array. This implies that the vertexData arrays for individual attributes has to be modified as well, and will usually grow in size, by replicating parts of the existing attribute data.


calculateTuples

protected int[] calculateTuples()
calculates tuples for all new indices, together with the data remapping tables. The remapping for vertexData is returned (for usage within the GSkinnedMesh class)


showGMeshData

public static void showGMeshData(boolean show)

appendAttributeString

public StringBuilder appendAttributeString(StringBuilder buf,
                                           XMLFormatting fmt)
appends the XML attributes to buf.

Overrides:
appendAttributeString in class XMLStructureAdapter

decodeAttributes

public void decodeAttributes(HashMap<String,String> attrMap,
                             XMLTokenizer tokenizer)
decodes the XML attributes.

Overrides:
decodeAttributes in class XMLStructureAdapter

appendContent

public StringBuilder appendContent(StringBuilder buf,
                                   XMLFormatting fmt)
Appends content part of XML encoding

Overrides:
appendContent in class XMLStructureAdapter

decodeContent

public void decodeContent(XMLTokenizer tokenizer)
                   throws IOException
Decodes content part of XML encoding

Overrides:
decodeContent in class XMLStructureAdapter
Throws:
IOException

xmlTag

public static String xmlTag()
The XML Stag for XML encoding


getXMLTag

public String getXMLTag()
returns the XML Stag for XML encoding

Specified by:
getXMLTag in interface XMLStructure
Overrides:
getXMLTag in class XMLStructureAdapter