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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Java port of Bullet (c) 2008 Martin Dvorak <jezek2@advel.cz>
 *
 * Bullet Continuous Collision Detection and Physics Library
 * Copyright (c) 2003-2008 Erwin Coumans  http://www.bulletphysics.com/
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from
 * the use of this software.
 * 
 * Permission is granted to anyone to use this software for any purpose, 
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */
 
package com.bulletphysics.linearmath;
 
import com.bulletphysics.collision.shapes.UniformScalingShape;
import cz.advel.stack.Stack;
import cz.advel.stack.StaticAlloc;
import javax.vecmath.Matrix3f;
import javax.vecmath.Matrix4f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
import javax.vecmath.Vector3d;
 
/**
 * Transform represents translation and rotation (rigid transform). Scaling and
 * shearing is not supported.<p>
 * 
 * You can use local shape scaling or {@link UniformScalingShape} for static rescaling
 * of collision objects.
 * 
 * @author jezek2
 */
public class Transform implements java.io.Serializable {
   
   //protected BulletStack stack;
 
   /** Rotation matrix of this Transform. */
   public final Matrix3f basis = new Matrix3f();
   
   /** Translation vector of this Transform. */
   public final Vector3f origin = new Vector3f();
 
//    public final Vector3f temp = new Vector3f();
        
   public Transform() {
   }
 
   public Transform(Matrix3f mat) {
       basis.set(mat);
   }
 
   public Transform(Matrix4f mat) {
       set(mat);
   }
 
   public Transform(Transform tr) {
       set(tr);
   }
   
   public void set(Transform tr) {
       basis.set(tr.basis);
       origin.set(tr.origin);
   }
   
   public void set(Matrix3f mat) {
       basis.set(mat);
       origin.set(0f, 0f, 0f);
   }
 
   public void set(Matrix4f mat) {
       mat.getRotationScale(basis);
       origin.set(mat.m03, mat.m13, mat.m23);
   }
   
   public void transform(Vector3f v) {
       basis.transform(v);
       v.add(origin);
   }
 
//    public void transform(Vector3d v)
//        {
//            temp.set((float)v.x,(float)v.y,(float)v.z);
//            basis.transform(temp);
//            temp.add(origin);
//            v.set(temp.x,temp.y,temp.z);
//    }
 
   public void setIdentity() {
       basis.setIdentity();
       origin.set(0f, 0f, 0f);
   }
   
   public void inverse() {
       basis.transpose();
       origin.scale(-1f);
       basis.transform(origin);
   }
 
   public void inverse(Transform tr) {
       set(tr);
       inverse();
   }
   
   public void mul(Transform tr) {
       Vector3f vec = (Vector3f) Stack.alloc(tr.origin);
       transform(vec);
 
       basis.mul(tr.basis);
       origin.set(vec);
   }
 
   @StaticAlloc
   public void mul(Transform tr1, Transform tr2) {
       Vector3f vec = (Vector3f) Stack.alloc(tr2.origin);
       tr1.transform(vec);
 
       basis.mul(tr1.basis, tr2.basis);
       origin.set(vec);
   }
   
   public void invXform(Vector3f inVec, Vector3f out) {
       out.sub(inVec, origin);
 
       Matrix3f mat = (Matrix3f) Stack.alloc(basis);
       mat.transpose();
       mat.transform(out);
   }
   
   public Quat4f getRotation(Quat4f out) {
       MatrixUtil.getRotation(basis, out);
       return out;
   }
   
   public void setRotation(Quat4f q) {
       MatrixUtil.setRotation(basis, q);
   }
   
   public void setFromOpenGLMatrix(float[] m) {
       MatrixUtil.setFromOpenGLSubMatrix(basis, m);
       origin.set(m[12], m[13], m[14]);
   }
 
   public void getOpenGLMatrix(float[] m) {
       MatrixUtil.getOpenGLSubMatrix(basis, m);
       m[12] = origin.x;
       m[13] = origin.y;
       m[14] = origin.z;
       m[15] = 1f;
   }
 
   public Matrix4f getMatrix(Matrix4f out) {
       out.set(basis);
       out.m03 = origin.x;
       out.m13 = origin.y;
       out.m23 = origin.z;
       return out;
   }
 
   @Override
   public boolean equals(Object obj) {
       if (obj == null || !(obj instanceof Transform)) return false;
       Transform tr = (Transform)obj;
       return basis.equals(tr.basis) && origin.equals(tr.origin);
   }
 
   @Override
   public int hashCode() {
       int hash = 3;
       hash = 41 * hash + basis.hashCode();
       hash = 41 * hash + origin.hashCode();
       return hash;
   }
   
}