NASA World Wind

gov.nasa.worldwind.ogc.collada
Class ColladaRoot

java.lang.Object
  extended by gov.nasa.worldwind.util.xml.AbstractXMLEventParser
      extended by gov.nasa.worldwind.ogc.collada.ColladaAbstractObject
          extended by gov.nasa.worldwind.ogc.collada.ColladaRoot
All Implemented Interfaces:
ColladaRenderable, Highlightable, XMLEventParser

public class ColladaRoot
extends ColladaAbstractObject
implements ColladaRenderable, Highlightable

Parses a COLLADA document and provides access to its contents. Instructions for parsing COLLADA files and streams are given in the Description section of gov.nasa.worldwind.ogc.collada.


Field Summary
protected  int altitudeMode
          This shape's altitude mode.
protected  ColladaDoc colladaDoc
          Reference to the ColladaDoc representing the COLLADA file.
protected  XMLEventReader eventReader
          The event reader used to parse the document's XML.
protected  InputStream eventStream
          The input stream underlying the event reader.
protected  Angle heading
          This shape's heading, positive values are clockwise from north.
protected  boolean highlighted
          Indicates whether or not the COLLADA model is highlighted.
protected  Matrix matrix
          Transform matrix computed from the document's scale and orientation.
protected  Vec4 modelScale
          A scale to apply to the model.
protected  ColladaParserContext parserContext
          The parser context for the document.
protected  Angle pitch
          This shape's pitch (often called tilt), its rotation about the model's X axis.
protected  Position position
          This shape's geographic location.
protected  ColladaResourceResolver resourceResolver
          Resource resolver to resolve relative file paths.
protected  Angle roll
          This shape's roll, its rotation about the model's Y axis.
protected  double scale
          Scale applied to the model.
protected  boolean scaleFetched
          Flag to indicate that the scale has been computed.
protected  ColladaScene scene
          Cached COLLADA scene.
protected  boolean sceneFetched
          Flag to indicate that the scene has been retrieved from the hash map.
 
Fields inherited from class gov.nasa.worldwind.util.xml.AbstractXMLEventParser
CHARACTERS_CONTENT, fields, namespaceURI, parent
 
Constructor Summary
ColladaRoot(ColladaDoc docSource)
          Create a new ColladaRoot for a ColladaDoc instance.
ColladaRoot(File docSource)
          Create a new ColladaRoot for a File.
ColladaRoot(InputStream docSource)
          Create a new ColladaRoot for a InputStream.
ColladaRoot(URL docSource)
          Create a new ColladaRoot for a URL.
 
Method Summary
protected  boolean canParseContentType(String mimeType)
          Determines if a MIME type can be parsed as COLLADA.
protected  void closeEventStream()
          Closes the event stream associated with this context's XML event reader.
protected  double computeScale()
          Indicates the scale defined by the asset/unit element.
static ColladaRoot create(Object docSource)
          Creates a Collada root for an untyped source.
static ColladaRoot createAndParse(Object docSource)
          Creates and parses a Collada root for an untyped source.
protected  ColladaParserContext createParserContext(XMLEventReader reader)
          Invoked during initialize() to create the parser context.
protected  XMLEventReader createReader(Object docSource)
          Creates the event reader.
 int getAltitudeMode()
          Returns this shape's altitude mode.
 ColladaAsset getAsset()
          Indicates the asset field of this document.
protected  ColladaDoc getColladaDoc()
          Indicates the document that is the source of this root.
 Angle getHeading()
          Indicates this shape's heading, its rotation clockwise from north.
 Object getItemByID(String id)
          Finds a named element in the document.
 Box getLocalExtent(ColladaTraversalContext tc)
          Returns this renderable's model coordinate extent.
protected  Matrix getMatrix()
          Indicates the transform matrix applied to this document.
 Vec4 getModelScale()
          Indicates this shape's scale, if any.
protected  XMLEventParserContext getParserContext()
          Indicates the parser context used by this document.
 Angle getPitch()
          Indicates this shape's pitch -- often referred to as tilt -- the angle to rotate this shape's model about its X axis.
 Position getPosition()
          Indicates this shape's geographic position.
 ColladaResourceResolver getResourceResolver()
          Indicates the resource resolver used to resolve relative file paths.
 Angle getRoll()
          Indicates this shape's roll, the angle to rotate this shape's model about its Y axis.
protected  double getScale()
          Indicates the scale factored applied to this document.
 ColladaScene getScene()
          Indicates the scene contained in this document.
 String getSupportFilePath(String link)
          Determines the path of a supporting file (such an image).
protected  void initialize()
          Called just before the constructor returns.
 boolean isHighlighted()
          Indicates whether to highlight the shape.
 ColladaRoot parse(Object... args)
          Starts document parsing.
protected  ColladaRoot parseCachedColladaFile(URL url, String linkBase)
          Open and parse the specified file expressed as a file: URL..
 void preRender(ColladaTraversalContext tc, DrawContext dc)
          Pre-Render this element.
 void render(ColladaTraversalContext tc, DrawContext dc)
          Render this element.
protected  void reset()
          Clear cached values.
protected  Object resolveLocalReference(String linkBase, String linkRef)
          Resolves a reference to a local element identified by address and identifier, where linkBase identifies a document, including the current document, and linkRef is the id of the desired element.
 Object resolveReference(String link)
          Resolves a reference to a local or remote file or element.
 Object resolveRemoteReference(String linkBase, String linkRef)
          Resolves a reference to a remote element identified by address and identifier, where linkBase identifies a remote document, and linkRef is the id of the desired element.
 void setAltitudeMode(int altitudeMode)
          Specifies this shape's altitude mode, one of WorldWind.ABSOLUTE, WorldWind.RELATIVE_TO_GROUND or WorldWind.CLAMP_TO_GROUND.
 void setHeading(Angle heading)
          Specifies this shape's heading, its rotation clockwise from north.
 void setHighlighted(boolean highlighted)
          Specifies whether to highlight the shape.
 void setModelScale(Vec4 modelScale)
          Specifies this shape's scale.
 void setPitch(Angle pitch)
          Specifies this shape's pitch -- often referred to as tilt -- the angle to rotate this shape's model about its X axis.
 void setPosition(Position position)
          Specifies this shape's geographic position.
 void setResourceResolver(ColladaResourceResolver resourceResolver)
          Specifies a resource resolver to resolve relative file paths.
 void setRoll(Angle roll)
          Specifies this shape's roll, the angle to rotate this shape's model about its Y axis.
 
Methods inherited from class gov.nasa.worldwind.ogc.collada.ColladaAbstractObject
getRoot
 
Methods inherited from class gov.nasa.worldwind.util.xml.AbstractXMLEventParser
allocate, doAddCharacters, doAddEventAttribute, doAddEventContent, doParseEventAttributes, doParseEventContent, freeResources, getAConstructor, getCharacters, getField, getField, getFields, getNamespaceURI, getParent, hasField, hasField, hasFields, mergeFields, newInstance, overrideFields, parse, parseCharacterContent, removeField, setField, setField, setFields, setParent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

altitudeMode

protected int altitudeMode
This shape's altitude mode. May be one of WorldWind.CLAMP_TO_GROUND, WorldWind.RELATIVE_TO_GROUND, or WorldWind.ABSOLUTE.


colladaDoc

protected ColladaDoc colladaDoc
Reference to the ColladaDoc representing the COLLADA file.


eventReader

protected XMLEventReader eventReader
The event reader used to parse the document's XML.


eventStream

protected InputStream eventStream
The input stream underlying the event reader.


heading

protected Angle heading
This shape's heading, positive values are clockwise from north. Null is an allowed value.


highlighted

protected boolean highlighted
Indicates whether or not the COLLADA model is highlighted.


matrix

protected Matrix matrix
Transform matrix computed from the document's scale and orientation. This matrix is computed and cached during when the document is rendered.


modelScale

protected Vec4 modelScale
A scale to apply to the model. Null is an allowed value.


parserContext

protected ColladaParserContext parserContext
The parser context for the document.


pitch

protected Angle pitch
This shape's pitch (often called tilt), its rotation about the model's X axis. Positive values are clockwise. Null is an allowed value.


position

protected Position position
This shape's geographic location. The altitude is relative to this shapes altitude mode.


resourceResolver

protected ColladaResourceResolver resourceResolver
Resource resolver to resolve relative file paths.


roll

protected Angle roll
This shape's roll, its rotation about the model's Y axis. Positive values are clockwise. Null is an allowed Value.


scale

protected double scale
Scale applied to the model. Determined by the COLLADA/asset/unit element.


scaleFetched

protected boolean scaleFetched
Flag to indicate that the scale has been computed.


scene

protected ColladaScene scene
Cached COLLADA scene.


sceneFetched

protected boolean sceneFetched
Flag to indicate that the scene has been retrieved from the hash map.

Constructor Detail

ColladaRoot

public ColladaRoot(ColladaDoc docSource)
            throws IOException
Create a new ColladaRoot for a ColladaDoc instance. A ColladaDoc represents COLLADA files from either files or input streams.

Parameters:
docSource - the ColladaDoc instance representing the COLLADA document.
Throws:
IllegalArgumentException - if the document source is null.
IOException - if an error occurs while reading the COLLADA document.

ColladaRoot

public ColladaRoot(File docSource)
            throws IOException
Create a new ColladaRoot for a File.

Parameters:
docSource - the File containing the document.
Throws:
IllegalArgumentException - if the document source is null.
IOException - if an error occurs while reading the Collada document.

ColladaRoot

public ColladaRoot(InputStream docSource)
            throws IOException
Create a new ColladaRoot for a InputStream.

Parameters:
docSource - the URL of the document.
Throws:
IllegalArgumentException - if the document source is null.
IOException - if an error occurs while reading the Collada document.

ColladaRoot

public ColladaRoot(URL docSource)
            throws IOException
Create a new ColladaRoot for a URL.

Parameters:
docSource - the URL of the document.
Throws:
IllegalArgumentException - if the document source is null.
IOException - if an error occurs while reading the Collada document.
Method Detail

canParseContentType

protected boolean canParseContentType(String mimeType)
Determines if a MIME type can be parsed as COLLADA. Parsable types are the COLLADA MIME type, as well as "text/plain" and "text/xml".

Parameters:
mimeType - Type to test. May be null.
Returns:
true if mimeType can be parsed as COLLADA.

closeEventStream

protected void closeEventStream()
Closes the event stream associated with this context's XML event reader.


computeScale

protected double computeScale()
Indicates the scale defined by the asset/unit element. This scale converts the document's units to meters.

Returns:
Scale for this document, or 1.0 if no scale is defined.

create

public static ColladaRoot create(Object docSource)
                          throws IOException
Creates a Collada root for an untyped source. The source must be either a File or a String identifying either a file path or a URL. Null is returned if the source type is not recognized.

Parameters:
docSource - either a File or a String identifying a file path or URL.
Returns:
a new ColladaRoot for the specified source, or null if the source type is not supported.
Throws:
IllegalArgumentException - if the source is null.
IOException - if an error occurs while reading the source.

createAndParse

public static ColladaRoot createAndParse(Object docSource)
                                  throws IOException,
                                         XMLStreamException
Creates and parses a Collada root for an untyped source. The source must be either a File or a String identifying either a file path or a URL. Null is returned if the source type is not recognized.

Parameters:
docSource - either a File or a String identifying a file path or URL.
Returns:
a new ColladaRoot for the specified source, or null if the source type is not supported.
Throws:
IllegalArgumentException - if the source is null.
IOException - if an error occurs while reading the source.
XMLStreamException

createParserContext

protected ColladaParserContext createParserContext(XMLEventReader reader)
Invoked during initialize() to create the parser context. The parser context is created by the global XMLEventParserContextFactory.

Parameters:
reader - the reader to associate with the parser context.
Returns:
a new parser context.

createReader

protected XMLEventReader createReader(Object docSource)
Creates the event reader. Called from the constructor.

Parameters:
docSource - the document source to create a reader for. The type can be any of those supported by WWXML.openEventReader(Object).
Returns:
a new event reader, or null if the source type cannot be determined.

getAltitudeMode

public int getAltitudeMode()
Returns this shape's altitude mode.

Returns:
this shape's altitude mode.
See Also:
setAltitudeMode(int)

getAsset

public ColladaAsset getAsset()
Indicates the asset field of this document.

Returns:
The asset field, or null if the field has not been set.

getColladaDoc

protected ColladaDoc getColladaDoc()
Indicates the document that is the source of this root.

Returns:
The source of the COLLADA content.

getHeading

public Angle getHeading()
Indicates this shape's heading, its rotation clockwise from north.

Returns:
this shape's heading, or null if no heading has been specified.

getItemByID

public Object getItemByID(String id)
Finds a named element in the document.

Parameters:
id - the element's identifier. If null, null is returned.
Returns:
the element requested, or null if there is no corresponding element in the document.

getLocalExtent

public Box getLocalExtent(ColladaTraversalContext tc)
Description copied from class: ColladaAbstractObject
Returns this renderable's model coordinate extent.

Specified by:
getLocalExtent in interface ColladaRenderable
Overrides:
getLocalExtent in class ColladaAbstractObject
Parameters:
tc - The traversal context to use when determining the extent.
Returns:
The model coordinate extent.

getMatrix

protected Matrix getMatrix()
Indicates the transform matrix applied to this document.

Returns:
Transform matrix.

getModelScale

public Vec4 getModelScale()
Indicates this shape's scale, if any.

Returns:
this shape's scale, or null if no scale has been specified.

getParserContext

protected XMLEventParserContext getParserContext()
Indicates the parser context used by this document.

Returns:
The parser context used to parse the document.

getPitch

public Angle getPitch()
Indicates this shape's pitch -- often referred to as tilt -- the angle to rotate this shape's model about its X axis.

Returns:
this shape's pitch, or null if no pitch has been specified. Positive values are clockwise as observed looking along the model's X axis toward the model's origin.

getPosition

public Position getPosition()
Indicates this shape's geographic position.

Returns:
this shape's geographic position. The position's altitude is relative to this shape's altitude mode.

getResourceResolver

public ColladaResourceResolver getResourceResolver()
Indicates the resource resolver used to resolve relative file paths.

Returns:
The resource resolver, or null if none is set.

getRoll

public Angle getRoll()
Indicates this shape's roll, the angle to rotate this shape's model about its Y axis.

Returns:
this shape's roll, or null if no roll has been specified. Positive values are clockwise as observed looking along the model's Y axis toward the origin.

getScale

protected double getScale()
Indicates the scale factored applied to this document. The scale is specified by the asset/unit element.

Returns:
Scale applied to the document. Returns 1.0 if the document does not specify a scale.

getScene

public ColladaScene getScene()
Indicates the scene contained in this document.

Returns:
The COLLADA scene, or null if there is no scene.

getSupportFilePath

public String getSupportFilePath(String link)
                          throws IOException
Determines the path of a supporting file (such an image). If a resource resolver has been specified, the resource resolver will be invoked to determine the file path. Otherwise, the path will be resolved relative to the COLLADA document's file path or URL.

Parameters:
link - Relative path to resolve.
Returns:
Absolute path of the resource, or null if the resource cannot be resolved.
Throws:
IOException - If an error occurs while attempting to resolve the resource.

initialize

protected void initialize()
                   throws IOException
Called just before the constructor returns. If overriding this method be sure to invoke super.initialize().

Throws:
IOException - if an I/O error occurs attempting to open the document source.

isHighlighted

public boolean isHighlighted()
Indicates whether to highlight the shape.

Specified by:
isHighlighted in interface Highlightable
Returns:
true to highlight the shape, otherwise false.

parse

public ColladaRoot parse(Object... args)
                  throws XMLStreamException
Starts document parsing. This method initiates parsing of the COLLADA document and returns when the full document has been parsed.

Parameters:
args - optional arguments to pass to parsers of sub-elements.
Returns:
this if parsing is successful, otherwise null.
Throws:
XMLStreamException - if an exception occurs while attempting to read the event stream.

parseCachedColladaFile

protected ColladaRoot parseCachedColladaFile(URL url,
                                             String linkBase)
                                      throws IOException,
                                             XMLStreamException
Open and parse the specified file expressed as a file: URL..

Parameters:
url - the URL of the file to open, expressed as a URL with a scheme of "file".
linkBase - the original address of the document if the file is a retrieved and cached file.
Returns:
A ColladaRoot representing the file's COLLADA contents.
Throws:
IOException - if an I/O error occurs during opening and parsing.
XMLStreamException - if a server parsing error is encountered.

preRender

public void preRender(ColladaTraversalContext tc,
                      DrawContext dc)
Pre-Render this element. Renders the scene contained in this document.

Specified by:
preRender in interface ColladaRenderable
Parameters:
tc - the current COLLADA traversal context.
dc - the current draw context.

render

public void render(ColladaTraversalContext tc,
                   DrawContext dc)
Render this element. Renders the scene contained in this document.

Specified by:
render in interface ColladaRenderable
Parameters:
tc - the current COLLADA traversal context.
dc - the current draw context.

reset

protected void reset()
Clear cached values. Values will be recomputed the next time this document is rendered.


resolveLocalReference

protected Object resolveLocalReference(String linkBase,
                                       String linkRef)
Resolves a reference to a local element identified by address and identifier, where linkBase identifies a document, including the current document, and linkRef is the id of the desired element.

If linkBase refers to a local COLLADA file and linkRef is non-null, the return value is the element identified by linkRef. If linkRef is null, the return value is a parsed ColladaRoot for the COLLADA file identified by linkBase. Otherwise, linkBase is returned.

Parameters:
linkBase - the address of the document containing the requested element.
linkRef - the element's identifier.
Returns:
the requested element, or null if the element is not found.
Throws:
IllegalArgumentException - if the address is null.

resolveReference

public Object resolveReference(String link)
Resolves a reference to a local or remote file or element. If the link refers to an element in the current document, this method returns that element. If the link refers to a remote document, this method will initiate asynchronous retrieval of the document, and return a URL of the downloaded document in the file cache, if it is available locally. If the link identifies a COLLADA document, the document will be returned as a parsed ColladaRoot.

Parameters:
link - the address of the document or element to resolve. This may be a full URL, a URL fragment that identifies an element in the current document ("#myElement"), or a URL and a fragment identifier ("http://server.com/model.dae#myElement").
Returns:
the requested element, or null if the element is not found.
Throws:
IllegalArgumentException - if the address is null.

resolveRemoteReference

public Object resolveRemoteReference(String linkBase,
                                     String linkRef)
Resolves a reference to a remote element identified by address and identifier, where linkBase identifies a remote document, and linkRef is the id of the desired element. This method retrieves resources asynchronously using the FileStore.

The return value is null if the file is not yet available in the FileStore. If linkBase refers to a COLLADA file and linkRef is non-null, the return value is the element identified by linkRef. If linkBase refers to a COLLADA file and linkRef is null, the return value is a parsed ColladaRoot for the COLLADA file identified by linkBase. Otherwise the return value is a URL to the file in the file cache.

Parameters:
linkBase - the address of the document containing the requested element.
linkRef - the element's identifier.
Returns:
URL to the requested file, parsed ColladaRoot, or COLLADA element. Returns null if the document is not yet available in the FileStore.
Throws:
IllegalArgumentException - if the linkBase is null.

setAltitudeMode

public void setAltitudeMode(int altitudeMode)
Specifies this shape's altitude mode, one of WorldWind.ABSOLUTE, WorldWind.RELATIVE_TO_GROUND or WorldWind.CLAMP_TO_GROUND.

Note: If the altitude mode is unrecognized, WorldWind.ABSOLUTE is used.

Note: Subclasses may recognize additional altitude modes or may not recognize the ones described above.

Parameters:
altitudeMode - the altitude mode. The default value is WorldWind.ABSOLUTE.

setHeading

public void setHeading(Angle heading)
Specifies this shape's heading, its rotation clockwise from north.

Parameters:
heading - this shape's heading. May be null.

setHighlighted

public void setHighlighted(boolean highlighted)
Specifies whether to highlight the shape. Setting root COLLADA root highlighted causes all parts of the COLLADA model to highlight.

Specified by:
setHighlighted in interface Highlightable
Parameters:
highlighted - true to highlight the shape, otherwise false.

setModelScale

public void setModelScale(Vec4 modelScale)
Specifies this shape's scale. The scale is applied to the shape's model definition in the model's coordinate system prior to oriented and positioning the model.

Parameters:
modelScale - this shape's scale. May be null, in which case no scaling is applied.

setPitch

public void setPitch(Angle pitch)
Specifies this shape's pitch -- often referred to as tilt -- the angle to rotate this shape's model about its X axis.

Parameters:
pitch - this shape's pitch. Positive values are clockwise as observed looking along the model's X axis toward the model's origin. May be null.

setPosition

public void setPosition(Position position)
Specifies this shape's geographic position. The position's altitude is relative to this shape's altitude mode.

Parameters:
position - this shape's geographic position.
Throws:
IllegalArgumentException - if the position is null.

setResourceResolver

public void setResourceResolver(ColladaResourceResolver resourceResolver)
Specifies a resource resolver to resolve relative file paths.

Parameters:
resourceResolver - New resource resolver. May be null.

setRoll

public void setRoll(Angle roll)
Specifies this shape's roll, the angle to rotate this shape's model about its Y axis.

Parameters:
roll - this shape's roll. May be null. Positive values are clockwise as observed looking along the model's Y axis toward the origin.

NASA World Wind