Normand Briere
2017-05-07 314b34423070cf127464da79a53cddf6b1c38587
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
package mocap.figure;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
 
import j3d.Util;
import mocap.player.MocapPlayer;
 
/**
 * Represents an autonomous graphical figure. Contains the skeleton and
 * its own player (= motor).
 * 
 * @author Michael Kipp
 */
public class Figure
{
    private String _name;
    private Bone _skeleton; // root bone of skeleton
    private MocapPlayer _player; // drives animation
    private Point3d _offset;
    private BranchGroup _branchGroup; // scenegraph root
    private TransformGroup _tg; // can be used to give a base orientation
 
    /**
     * Creates a new figure and initializes the skeleton.
     *
     * @param name
     *            ID of the figure
     * @param skeleton
     *            Root bone of the skeleton
     */
    public Figure(String name, Bone skeleton)
    {
        _name = name;
        _skeleton = skeleton;
        _branchGroup = new BranchGroup();
        _branchGroup.setCapability(BranchGroup.ALLOW_DETACH);
        _tg = new TransformGroup();
        _tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
 
        _tg.addChild(_skeleton.getBaseTG());
        _branchGroup.addChild(_tg);
        _branchGroup.compile();
    }
 
    public double getScale()
    {
        return _skeleton.getScale();
    }
 
    /**
     * Measures the length of the skeleton by computing the max. distance between any two leaves in
     * the skeleton tree. Figure must be LIVE to allow this operation.
     */
    public double measureLength()
    {
        List<Bone> leaves = new ArrayList<Bone>();
        List<Bone> all = new ArrayList<Bone>();
        _skeleton.collectBones(all);
        for (Bone b : all) {
            if (b.getChildren().length == 0) {
                leaves.add(b);
            }
        }
        double dist = 0;
        Point3d p1 = new Point3d();
        Point3d p2 = new Point3d();
        for (Bone b1 : leaves) {
            for (Bone b2 : leaves) {
                if (!b1.equals(b2)) {
                    b1.getWorldPosition(p1);
                    b2.getWorldPosition(p2);
                    dist = Math.max(p1.distance(p2), dist);
                }
            }
        }
        System.out.println("### LENGTH IS " + dist);
        return dist;
    }
 
    /**
     * For motion data where the skeleton head point up the z-axis, use this transformation (reset
     * with resetRotation).
     */
    //    public void correctUpVector(Vector3d rotationAxis, double angle)
    //    {
    //        Transform3D t1 = new Transform3D();
    //        Transform3D t2 = new Transform3D();
    //        t1.rotX(-Math.PI / 2d);
    //        t2.rotY(-Math.PI / 2d);
    //        t2.mul(t1);
    //        _tg.setTransform(t2);
    //    }
    /**
     * For motion data where the skeleton head point up the z-axis, use this transformation (reset
     * with resetRotation).
     */
    public void setZUpRotation()
    {
        Transform3D t2 = new Transform3D();
        t2.rotX(-Math.PI / 2d);
        _tg.setTransform(t2);
    }
 
    /**
     * For motion data where the skeleton head point up the x-axis, use this transformation (reset
     * with resetRotation).
     */
    public void setXUpRotation()
    {
        Transform3D t2 = new Transform3D();
        t2.rotZ(Math.PI / 2d);
        _tg.setTransform(t2);
    }
 
    public void resetRotation()
    {
        _tg.setTransform(new Transform3D());
    }
 
    public boolean hasAnimation()
    {
        return _player != null;
    }
 
    public String getName()
    {
        return _name;
    }
 
    public MocapPlayer getPlayer()
    {
        return _player;
    }
 
    public Bone getSkeleton()
    {
        return _skeleton;
    }
 
    public void setBG(BranchGroup bg)
    {
        _branchGroup = bg;
    }
 
    /**
     * Point in the scene graph where this skeleton is attached.
     */
    public BranchGroup getBG()
    {
 
        return _branchGroup;
    }
 
    public void setAnimation(AnimData data)
    {
        _player = new MocapPlayer(_skeleton, data, _offset);
        _player.setPlaybackFps(data.getFps());
    }
 
    public void setOffset(Point3d p)
    {
        _offset = p;
    }
}