edu.mines.jtk.sgl.TraversalContext Maven / Gradle / Ivy
Show all versions of edu-mines-jtk Show documentation
/****************************************************************************
Copyright 2004, Colorado School of Mines and others.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/
package edu.mines.jtk.sgl;
/**
* A context for scene graph traversal. A traversal context maintains
* a current node and a list of its parent nodes. Because nodes in the
* scene graph may have multiple parents, a node may become current at
* more than time during a traversal, each time with a different list
* of parent nodes.
* @author Dave Hale, Colorado School of Mines
* @version 2005.05.26
*/
public class TraversalContext {
/**
* Returns the number of current and parent nodes in this traversal.
* @return the number of nodes.
*/
public int countNodes() {
return _nodeStack.size();
}
/**
* Gets the current node in this traversal.
* @return the current node.
*/
public Node getNode() {
return _nodeStack.peek();
}
/**
* Gets the node in this traversal with specified index. If count is the
* number of nodes returned by the method {@link #countNodes()}, then the
* current node has index count-1, and the root node has index zero.
*
* If the specified index is negative, then count is added to the index.
* Therefore, the index -1 will get the current node, the index -2 will
* get its parent node, and so on.
* @param index the index.
* @return the node.
*/
public Node getNode(int index) {
if (index<0) {
return _nodeStack.get(index+countNodes());
} else {
return _nodeStack.get(index);
}
}
/**
* Gets an array of nodes representing the state of this traversal.
* Nodes in the array are ordered from parent to child. The last node
* in the array is the current node, the next to last node is the
* current node's parent, and so on.
* @return the array of nodes.
*/
public Node[] getNodes() {
int nnode = _nodeStack.size();
Node[] nodes = new Node[nnode];
for (int inode=0; inode _nodeStack = new ArrayStack();
}