Normand Briere
2019-10-05 8768a855af9ccc482a9520ce708ef32e0e6e0e7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Bone.java
 *
 *
 *  The Salamander Project - 2D and 3D graphics libraries in Java
 *  Copyright (C) 2004 Mark McKay
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
 *  projects can be found at http://www.kitfox.com
 *
 * Created on March 10, 2004, 8:54 AM
 */
 
import javax.media.j3d.*;
//import javax.vecmath.*;
 
import java.util.*;
 
/**
 * <p>Bones define a skeleton that is linked to a SmoothMesh.  By repositioning the
 * bone joins, one can deform it's associated mesh.</p>
 *
 * <p>A bone coordinate system can be thought of as similar to a regular TransformGroup
 * hierarchy within the scene graph, with the exception that mesh verticies are
 * transformed instead of instanced geometry and that a vertex can be influenced
 * by more than one bone.  To transform a mesh from it's bind pose (starting position)
 * to it's animated pose, you need to perform a weighted sum of the effects that
 * each bone contributes to the point.</p>
 *
 * <p>A single bone transforms points from the local bind coordinate system (static mesh)
 * to the local target coordinated system (animated mesh).  Given:</p>
 * <ul>
 * <li/>ltp - local target point
 * <li/>lbp - local bind point
 * <li/>B - bind matrix
 * <li/>A - current joint orientation matrix
 * </ul>
 *
 * <p>then <code>ltp = A B^-1 lbp</code></p>
 *
 *
 * <p>To determine the true transformation a bone contributes to a point, one must
 * take into account the transformations of it's parents too.  So if this bone
 * is at level n and n - p indicates it's pth parent, then the true
 * transformation matrix is</p>
 *
 * <code>M = A(0) B^-1(0) ... A(n - 1) B^-1(n - 1) A(n) B^-1(n)</code>
 *
 * <p>The position of the final animated mesh is a weighted sum over all bones.</p>
 *
 * <code>
 * tp = 0 <br/>
 * foreach (joint j) <br/>
 * { <br/>
 *    tp += w(bp, j) * M(j) * bp <br/>
 * } <br/>
 * </code>
 *
 * <p>where
 * <ul>
 * <li/>bp - bind point
 * <li/>M(j) - transform of bone j
 * <li/>w(bp, j) - weight for point bp on bone j
 * </ul>
 * </p>
 *
 * @author  kitfox
 */
public class Bone extends TransformGroup {
    /** Bind transfrom of this bone */
    final Transform3D bindXform = new Transform3D();
    /** Inverse bind transfrom of this bone */
    final Transform3D bindInvXform = new Transform3D();
 
    /** Current local pose position of this bone */
    final Transform3D animXform = new Transform3D();
 
    final Vector<Bone> children = new Vector<Bone>();
 
    /** Creates a new instance of Bone */
    public Bone(Transform3D bind)
    {
        super(bind);
        bindXform.set(bind);
        bindInvXform.invert(bind);
        animXform.set(bind);
 
        setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    }
 
    /**
     * Adds a child bone to this one.
     * @return - index of added child
     */
    public int addChildBone(Bone child)
    {
        if (children.contains(child)) return children.indexOf(child);
 
        super.addChild(child);
        children.add(child);
        return children.size() - 1;
    }
 
 
    public void addChild(Node child)
    {
        if (child instanceof Bone)
        {
            addChildBone((Bone)child);
        }
        else super.addChild(child);
    }
 
    public int getNumChildBones() { return children.size(); }
 
    public Bone getChildBone(int idx)
    {
        return (Bone)children.get(idx);
    }
 
    /**
     * Returns cached bone xform.  Do not write to this matrix!  Instead, call
     * setTransform to update this bone's orientation
     */
    public void getTransform(Transform3D xform)
    {
        xform.set(animXform);
    }
 
    public void getBindTransform(Transform3D xform)
    {
        xform.set(bindXform);
    }
 
    /**
     * Updates the animated local transformation of this bone
     */
    public void setTransform(Transform3D xform)
    {
        super.setTransform(xform);
        animXform.set(xform);
//        sceneXformDirty = true;
    }
 
    public void setToBindTransform()
    {
        super.setTransform(bindXform);
        animXform.set(bindXform);
//        sceneXformDirty = true;
    }
 
    public Node cloneNode(boolean forceDuplicate)
    {
        Bone bone = new Bone(bindXform);
        bone.duplicateNode(this, forceDuplicate);
        return bone;
    }
 
    /*
    public void duplicateNode(Node originalNode, boolean forceDuplicate)
    {
        Bone src = (Bone)originalNode;
    }
     */
}