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
package j3d;
 
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
 
/**
 *
 * @author Michael Kipp
 */
public class Util
{
 
    private Util()
    {
    }
 
    /**
     * Computes the full transform from root to the given transform group,
     * including the transform *in* the TG.
     *
     * The resulting transform can be used to find the origin of the TG by
     * applying the transform to (0,0,0).
     *
     * @param tg The TG in question
     * @param tf Resulting transform is returned here (old transform is overwritten)
     */
    public static void getFullTransform(TransformGroup tg, Transform3D tf)
    {
        Transform3D tf2 = new Transform3D();
        tf.setIdentity();
        tg.getTransform(tf2);
        tg.getLocalToVworld(tf);
        tf.mul(tf2);
    }
 
    /**
     * Computes the transform that takes a point that is *local* in locationTG
     * to the frame of reference of frameTG.
     *
     * If one applies the resulting transform to (0,0,0) one obtains the location
     * of the origin of locationTG in the frame of reference of frameTG.
     *
     * @param locationTG The TG in question, must be (grand)child of frameTG
     * @param frameTG The new frame of reference, must be parent of locationTG
     * @param tf Resulting transform is returned here (old transform is overwritten)
     */
    public static void getRelativeTransform(TransformGroup locationTG, TransformGroup frameTG, Transform3D tf)
    {
        Transform3D tf2 = new Transform3D();
        getFullTransform(locationTG, tf);
        getFullTransform(frameTG, tf2);
        tf2.invert();
        tf.mul(tf2, tf);
    }
}