Main repository of MikuMikuStudio
Revision | f1ae54d573fc066c2b5a23fef02ad76de3387988 (tree) |
---|---|
Time | 2013-07-07 23:55:10 |
Author | kobayasi <kobayasi@pscn...> |
Commiter | kobayasi |
add Converter
copy from jbullet library.
@@ -0,0 +1,283 @@ | ||
1 | +/* | |
2 | + * Copyright (c) 2009-2010 jMonkeyEngine | |
3 | + * All rights reserved. | |
4 | + * | |
5 | + * Redistribution and use in source and binary forms, with or without | |
6 | + * modification, are permitted provided that the following conditions are | |
7 | + * met: | |
8 | + * | |
9 | + * * Redistributions of source code must retain the above copyright | |
10 | + * notice, this list of conditions and the following disclaimer. | |
11 | + * | |
12 | + * * Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * | |
16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
17 | + * may be used to endorse or promote products derived from this software | |
18 | + * without specific prior written permission. | |
19 | + * | |
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | + */ | |
32 | +package com.jme3.bullet.util; | |
33 | + | |
34 | +//import com.bulletphysics.collision.shapes.IndexedMesh; | |
35 | +//import com.bulletphysics.dom.HeightfieldTerrainShape; | |
36 | +import com.jme3.math.FastMath; | |
37 | +import com.jme3.scene.mesh.IndexBuffer; | |
38 | +import com.jme3.scene.Mesh; | |
39 | +import com.jme3.scene.VertexBuffer.Type; | |
40 | +import com.jme3.scene.mesh.WrappedIndexBuffer; | |
41 | +import com.jme3.util.BufferUtils; | |
42 | +import java.nio.ByteBuffer; | |
43 | +import java.nio.FloatBuffer; | |
44 | + | |
45 | +/** | |
46 | + * Nice convenience methods for conversion between javax.vecmath and com.jme3.math | |
47 | + * Objects, also some jme to jbullet mesh conversion. | |
48 | + * @author normenhansen | |
49 | + */ | |
50 | +public class Converter { | |
51 | + | |
52 | + private Converter() { | |
53 | + } | |
54 | + | |
55 | + public static com.jme3.math.Vector3f convert(javax.vecmath.Vector3f oldVec) { | |
56 | + com.jme3.math.Vector3f newVec = new com.jme3.math.Vector3f(); | |
57 | + convert(oldVec, newVec); | |
58 | + return newVec; | |
59 | + } | |
60 | + | |
61 | + public static com.jme3.math.Vector3f convert(javax.vecmath.Vector3f oldVec, com.jme3.math.Vector3f newVec) { | |
62 | + newVec.x = oldVec.x; | |
63 | + newVec.y = oldVec.y; | |
64 | + newVec.z = oldVec.z; | |
65 | + return newVec; | |
66 | + } | |
67 | + | |
68 | + public static javax.vecmath.Vector3f convert(com.jme3.math.Vector3f oldVec) { | |
69 | + javax.vecmath.Vector3f newVec = new javax.vecmath.Vector3f(); | |
70 | + convert(oldVec, newVec); | |
71 | + return newVec; | |
72 | + } | |
73 | + | |
74 | + public static javax.vecmath.Vector3f convert(com.jme3.math.Vector3f oldVec, javax.vecmath.Vector3f newVec) { | |
75 | + newVec.x = oldVec.x; | |
76 | + newVec.y = oldVec.y; | |
77 | + newVec.z = oldVec.z; | |
78 | + return newVec; | |
79 | + } | |
80 | + | |
81 | + public static javax.vecmath.Quat4f convert(com.jme3.math.Quaternion oldQuat, javax.vecmath.Quat4f newQuat) { | |
82 | + newQuat.w = oldQuat.getW(); | |
83 | + newQuat.x = oldQuat.getX(); | |
84 | + newQuat.y = oldQuat.getY(); | |
85 | + newQuat.z = oldQuat.getZ(); | |
86 | + return newQuat; | |
87 | + } | |
88 | + | |
89 | + public static javax.vecmath.Quat4f convert(com.jme3.math.Quaternion oldQuat) { | |
90 | + javax.vecmath.Quat4f newQuat = new javax.vecmath.Quat4f(); | |
91 | + convert(oldQuat, newQuat); | |
92 | + return newQuat; | |
93 | + } | |
94 | + | |
95 | + public static com.jme3.math.Quaternion convert(javax.vecmath.Quat4f oldQuat, com.jme3.math.Quaternion newQuat) { | |
96 | + newQuat.set(oldQuat.x, oldQuat.y, oldQuat.z, oldQuat.w); | |
97 | + return newQuat; | |
98 | + } | |
99 | + | |
100 | + public static com.jme3.math.Quaternion convert(javax.vecmath.Quat4f oldQuat) { | |
101 | + com.jme3.math.Quaternion newQuat = new com.jme3.math.Quaternion(); | |
102 | + convert(oldQuat, newQuat); | |
103 | + return newQuat; | |
104 | + } | |
105 | + | |
106 | + public static com.jme3.math.Quaternion convert(javax.vecmath.Matrix3f oldMatrix, com.jme3.math.Quaternion newQuaternion) { | |
107 | + // the trace is the sum of the diagonal elements; see | |
108 | + // http://mathworld.wolfram.com/MatrixTrace.html | |
109 | + float t = oldMatrix.m00 + oldMatrix.m11 + oldMatrix.m22; | |
110 | + float w, x, y, z; | |
111 | + // we protect the division by s by ensuring that s>=1 | |
112 | + if (t >= 0) { // |w| >= .5 | |
113 | + float s = FastMath.sqrt(t + 1); // |s|>=1 ... | |
114 | + w = 0.5f * s; | |
115 | + s = 0.5f / s; // so this division isn't bad | |
116 | + x = (oldMatrix.m21 - oldMatrix.m12) * s; | |
117 | + y = (oldMatrix.m02 - oldMatrix.m20) * s; | |
118 | + z = (oldMatrix.m10 - oldMatrix.m01) * s; | |
119 | + } else if ((oldMatrix.m00 > oldMatrix.m11) && (oldMatrix.m00 > oldMatrix.m22)) { | |
120 | + float s = FastMath.sqrt(1.0f + oldMatrix.m00 - oldMatrix.m11 - oldMatrix.m22); // |s|>=1 | |
121 | + x = s * 0.5f; // |x| >= .5 | |
122 | + s = 0.5f / s; | |
123 | + y = (oldMatrix.m10 + oldMatrix.m01) * s; | |
124 | + z = (oldMatrix.m02 + oldMatrix.m20) * s; | |
125 | + w = (oldMatrix.m21 - oldMatrix.m12) * s; | |
126 | + } else if (oldMatrix.m11 > oldMatrix.m22) { | |
127 | + float s = FastMath.sqrt(1.0f + oldMatrix.m11 - oldMatrix.m00 - oldMatrix.m22); // |s|>=1 | |
128 | + y = s * 0.5f; // |y| >= .5 | |
129 | + s = 0.5f / s; | |
130 | + x = (oldMatrix.m10 + oldMatrix.m01) * s; | |
131 | + z = (oldMatrix.m21 + oldMatrix.m12) * s; | |
132 | + w = (oldMatrix.m02 - oldMatrix.m20) * s; | |
133 | + } else { | |
134 | + float s = FastMath.sqrt(1.0f + oldMatrix.m22 - oldMatrix.m00 - oldMatrix.m11); // |s|>=1 | |
135 | + z = s * 0.5f; // |z| >= .5 | |
136 | + s = 0.5f / s; | |
137 | + x = (oldMatrix.m02 + oldMatrix.m20) * s; | |
138 | + y = (oldMatrix.m21 + oldMatrix.m12) * s; | |
139 | + w = (oldMatrix.m10 - oldMatrix.m01) * s; | |
140 | + } | |
141 | + return newQuaternion.set(x, y, z, w); | |
142 | + } | |
143 | + | |
144 | + public static javax.vecmath.Matrix3f convert(com.jme3.math.Quaternion oldQuaternion, javax.vecmath.Matrix3f newMatrix) { | |
145 | + float norm = oldQuaternion.getW() * oldQuaternion.getW() + oldQuaternion.getX() * oldQuaternion.getX() + oldQuaternion.getY() * oldQuaternion.getY() + oldQuaternion.getZ() * oldQuaternion.getZ(); | |
146 | + float s = (norm == 1f) ? 2f : (norm > 0f) ? 2f / norm : 0; | |
147 | + | |
148 | + // compute xs/ys/zs first to save 6 multiplications, since xs/ys/zs | |
149 | + // will be used 2-4 times each. | |
150 | + float xs = oldQuaternion.getX() * s; | |
151 | + float ys = oldQuaternion.getY() * s; | |
152 | + float zs = oldQuaternion.getZ() * s; | |
153 | + float xx = oldQuaternion.getX() * xs; | |
154 | + float xy = oldQuaternion.getX() * ys; | |
155 | + float xz = oldQuaternion.getX() * zs; | |
156 | + float xw = oldQuaternion.getW() * xs; | |
157 | + float yy = oldQuaternion.getY() * ys; | |
158 | + float yz = oldQuaternion.getY() * zs; | |
159 | + float yw = oldQuaternion.getW() * ys; | |
160 | + float zz = oldQuaternion.getZ() * zs; | |
161 | + float zw = oldQuaternion.getW() * zs; | |
162 | + | |
163 | + // using s=2/norm (instead of 1/norm) saves 9 multiplications by 2 here | |
164 | + newMatrix.m00 = 1 - (yy + zz); | |
165 | + newMatrix.m01 = (xy - zw); | |
166 | + newMatrix.m02 = (xz + yw); | |
167 | + newMatrix.m10 = (xy + zw); | |
168 | + newMatrix.m11 = 1 - (xx + zz); | |
169 | + newMatrix.m12 = (yz - xw); | |
170 | + newMatrix.m20 = (xz - yw); | |
171 | + newMatrix.m21 = (yz + xw); | |
172 | + newMatrix.m22 = 1 - (xx + yy); | |
173 | + | |
174 | + return newMatrix; | |
175 | + } | |
176 | + | |
177 | + public static com.jme3.math.Matrix3f convert(javax.vecmath.Matrix3f oldMatrix) { | |
178 | + com.jme3.math.Matrix3f newMatrix = new com.jme3.math.Matrix3f(); | |
179 | + convert(oldMatrix, newMatrix); | |
180 | + return newMatrix; | |
181 | + } | |
182 | + | |
183 | + public static com.jme3.math.Matrix3f convert(javax.vecmath.Matrix3f oldMatrix, com.jme3.math.Matrix3f newMatrix) { | |
184 | + newMatrix.set(0, 0, oldMatrix.m00); | |
185 | + newMatrix.set(0, 1, oldMatrix.m01); | |
186 | + newMatrix.set(0, 2, oldMatrix.m02); | |
187 | + newMatrix.set(1, 0, oldMatrix.m10); | |
188 | + newMatrix.set(1, 1, oldMatrix.m11); | |
189 | + newMatrix.set(1, 2, oldMatrix.m12); | |
190 | + newMatrix.set(2, 0, oldMatrix.m20); | |
191 | + newMatrix.set(2, 1, oldMatrix.m21); | |
192 | + newMatrix.set(2, 2, oldMatrix.m22); | |
193 | + return newMatrix; | |
194 | + } | |
195 | + | |
196 | + public static javax.vecmath.Matrix3f convert(com.jme3.math.Matrix3f oldMatrix) { | |
197 | + javax.vecmath.Matrix3f newMatrix = new javax.vecmath.Matrix3f(); | |
198 | + convert(oldMatrix, newMatrix); | |
199 | + return newMatrix; | |
200 | + } | |
201 | + | |
202 | + public static javax.vecmath.Matrix3f convert(com.jme3.math.Matrix3f oldMatrix, javax.vecmath.Matrix3f newMatrix) { | |
203 | + newMatrix.m00 = oldMatrix.get(0, 0); | |
204 | + newMatrix.m01 = oldMatrix.get(0, 1); | |
205 | + newMatrix.m02 = oldMatrix.get(0, 2); | |
206 | + newMatrix.m10 = oldMatrix.get(1, 0); | |
207 | + newMatrix.m11 = oldMatrix.get(1, 1); | |
208 | + newMatrix.m12 = oldMatrix.get(1, 2); | |
209 | + newMatrix.m20 = oldMatrix.get(2, 0); | |
210 | + newMatrix.m21 = oldMatrix.get(2, 1); | |
211 | + newMatrix.m22 = oldMatrix.get(2, 2); | |
212 | + return newMatrix; | |
213 | + } | |
214 | + | |
215 | +// public static com.bulletphysics.linearmath.Transform convert(com.jme3.math.Transform in, com.bulletphysics.linearmath.Transform out) { | |
216 | +// convert(in.getTranslation(), out.origin); | |
217 | +// convert(in.getRotation(), out.basis); | |
218 | +// return out; | |
219 | +// } | |
220 | +// | |
221 | +// public static com.jme3.math.Transform convert(com.bulletphysics.linearmath.Transform in, com.jme3.math.Transform out) { | |
222 | +// convert(in.origin, out.getTranslation()); | |
223 | +// convert(in.basis, out.getRotation()); | |
224 | +// return out; | |
225 | +// } | |
226 | +// | |
227 | +// public static IndexedMesh convert(Mesh mesh) { | |
228 | +// IndexedMesh jBulletIndexedMesh = new IndexedMesh(); | |
229 | +// jBulletIndexedMesh.triangleIndexBase = ByteBuffer.allocate(mesh.getTriangleCount() * 3 * 4); | |
230 | +// jBulletIndexedMesh.vertexBase = ByteBuffer.allocate(mesh.getVertexCount() * 3 * 4); | |
231 | +// | |
232 | +// IndexBuffer indices = mesh.getIndicesAsList(); | |
233 | +// | |
234 | +// FloatBuffer vertices = mesh.getFloatBuffer(Type.Position); | |
235 | +// vertices.rewind(); | |
236 | +// | |
237 | +// int verticesLength = mesh.getVertexCount() * 3; | |
238 | +// jBulletIndexedMesh.numVertices = mesh.getVertexCount(); | |
239 | +// jBulletIndexedMesh.vertexStride = 12; //3 verts * 4 bytes per. | |
240 | +// for (int i = 0; i < verticesLength; i++) { | |
241 | +// float tempFloat = vertices.get(); | |
242 | +// jBulletIndexedMesh.vertexBase.putFloat(tempFloat); | |
243 | +// } | |
244 | +// | |
245 | +// int indicesLength = mesh.getTriangleCount() * 3; | |
246 | +// jBulletIndexedMesh.numTriangles = mesh.getTriangleCount(); | |
247 | +// jBulletIndexedMesh.triangleIndexStride = 12; //3 index entries * 4 bytes each. | |
248 | +// for (int i = 0; i < indicesLength; i++) { | |
249 | +// jBulletIndexedMesh.triangleIndexBase.putInt(indices.get(i)); | |
250 | +// } | |
251 | +// vertices.rewind(); | |
252 | +// vertices.clear(); | |
253 | +// | |
254 | +// return jBulletIndexedMesh; | |
255 | +// } | |
256 | +// | |
257 | +// public static Mesh convert(IndexedMesh mesh) { | |
258 | +// Mesh jmeMesh = new Mesh(); | |
259 | +// | |
260 | +// jmeMesh.setBuffer(Type.Index, 3, BufferUtils.createShortBuffer(mesh.numTriangles * 3)); | |
261 | +// jmeMesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(mesh.numVertices * 3)); | |
262 | +// | |
263 | +// IndexBuffer indicess = jmeMesh.getIndexBuffer(); | |
264 | +// FloatBuffer vertices = jmeMesh.getFloatBuffer(Type.Position); | |
265 | +// | |
266 | +// for (int i = 0; i < mesh.numTriangles * 3; i++) { | |
267 | +// indicess.put(i, mesh.triangleIndexBase.getInt(i * 4)); | |
268 | +// } | |
269 | +// | |
270 | +// for (int i = 0; i < mesh.numVertices * 3; i++) { | |
271 | +// vertices.put(i, mesh.vertexBase.getFloat(i * 4)); | |
272 | +// } | |
273 | +// jmeMesh.updateCounts(); | |
274 | +// jmeMesh.updateBound(); | |
275 | +// jmeMesh.getFloatBuffer(Type.Position).clear(); | |
276 | +// | |
277 | +// return jmeMesh; | |
278 | +// } | |
279 | +// | |
280 | +// public static Mesh convert(HeightfieldTerrainShape heightfieldShape) { | |
281 | +// return null; //TODO!! | |
282 | +// } | |
283 | +} |