001: /*
002: * Apollo - Motion capture and animation system
003: * Copyright (c) 2005 Apollo
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: * http://www.gnu.org/copyleft/gpl.html
020: *
021: * @author Giovane.Kuhn - brain@netuno.com.br
022: *
023: */
024: package org.apollo.bvh;
025:
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.List;
029:
030: import javax.vecmath.Vector3d;
031:
032: import org.apollo.Visitor;
033:
034: /**
035: * Represents a joint in a skeleton model.<br>
036: * It's each node in the hierarchy.
037: *
038: * @author Giovane.Kuhn on 18/04/2005
039: */
040: public class Joint {
041:
042: /** Joint name */
043: protected final String name;
044:
045: /** Joint's parent */
046: protected final Joint parent;
047:
048: /** Channels defines parameters that will be animating */
049: protected List<Channel> channels;
050:
051: /** Initial X position */
052: protected Double x;
053:
054: /** Initial Y position */
055: protected Double y;
056:
057: /** Initial Z position */
058: protected Double z;
059:
060: /** List with children of this joint */
061: protected List<Joint> children;
062:
063: public Joint(String name, Joint parent) {
064: this .name = name;
065: this .parent = parent;
066: }
067:
068: public String getName() {
069: return name;
070: }
071:
072: public Joint getParent() {
073: return parent;
074: }
075:
076: public List<Channel> getChannels() {
077: if (channels == null) {
078: return Collections.emptyList();
079: }
080: return channels;
081: }
082:
083: public boolean addChannel(Channel channel) {
084: if (channels == null) {
085: channels = new ArrayList<Channel>();
086: }
087: return channels.add(channel);
088: }
089:
090: public void removeAllChannels() {
091: channels = null;
092: }
093:
094: public List<Joint> getChildren() {
095: if (children == null) {
096: return Collections.emptyList();
097: }
098: return children;
099: }
100:
101: public boolean addChildren(Joint child) {
102: if (children == null) {
103: children = new ArrayList<Joint>();
104: }
105: return children.add(child);
106: }
107:
108: public Double getX() {
109: return x;
110: }
111:
112: public void setX(Double x) {
113: this .x = x;
114: }
115:
116: public Double getY() {
117: return y;
118: }
119:
120: public void setY(Double y) {
121: this .y = y;
122: }
123:
124: public Double getZ() {
125: return z;
126: }
127:
128: public void setZ(Double z) {
129: this .z = z;
130: }
131:
132: public Vector3d getVector3d() {
133: return new Vector3d( //
134: x == null ? Double.NaN : x, //
135: y == null ? Double.NaN : y, //
136: z == null ? Double.NaN : z //
137: );
138: }
139:
140: public void visit(Visitor<Joint> visitor) {
141: if (visitor.accept(this )) {
142: for (Joint child : getChildren()) {
143: child.visit(visitor);
144: }
145: }
146: }
147:
148: public void inverseVisit(Visitor<Joint> visitor) {
149: if (visitor.accept(this )) {
150: if (parent != null) {
151: parent.visit(visitor);
152: }
153: }
154: }
155:
156: public boolean isRoot() {
157: return parent == null;
158: }
159:
160: public boolean isEndSite() {
161: return children == null || children.isEmpty();
162: }
163:
164: public boolean isTranslated() {
165: if (channels == null) {
166: return false;
167: }
168: boolean ret = false;
169: for (Channel c : channels) {
170: if (c == Channel.XPOS || c == Channel.YPOS
171: || c == Channel.ZPOS) {
172: ret = true;
173: break;
174: }
175: }
176: if (!ret) {
177: return ret;
178: }
179: assert channels.size() >= 3;
180: assert channels.get(0) == Channel.XPOS;
181: assert channels.get(1) == Channel.YPOS;
182: assert channels.get(2) == Channel.ZPOS;
183: return true;
184: }
185:
186: public String toString() {
187: return name;
188: }
189: }
|