Normand Briere
2016-02-27 28ab4dad99d24372ea58b09a00eafbce1291c278
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
/*
 * 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.collision.shapes;
 
import com.bulletphysics.collision.broadphase.BroadphaseNativeType;
import com.bulletphysics.linearmath.MatrixUtil;
import com.bulletphysics.linearmath.Transform;
import com.bulletphysics.linearmath.VectorUtil;
import cz.advel.stack.Stack;
import javax.vecmath.Matrix3f;
import javax.vecmath.Vector3f;
 
// JAVA NOTE: ScaledBvhTriangleMeshShape from 2.73 SP1
 
/**
 * The ScaledBvhTriangleMeshShape allows to instance a scaled version of an existing
 * {@link BvhTriangleMeshShape}. Note that each {@link BvhTriangleMeshShape} still can
 * have its own local scaling, independent from this ScaledBvhTriangleMeshShape 'localScaling'.
 *
 * @author jezek2
 */
public class ScaledBvhTriangleMeshShape extends ConcaveShape {
 
   protected final Vector3f localScaling = new Vector3f();
   protected BvhTriangleMeshShape bvhTriMeshShape;
 
   public ScaledBvhTriangleMeshShape(BvhTriangleMeshShape childShape, Vector3f localScaling) {
       this.localScaling.set(localScaling);
       this.bvhTriMeshShape = childShape;
   }
 
   public BvhTriangleMeshShape getChildShape() {
       return bvhTriMeshShape;
   }
 
   @Override
   public void processAllTriangles(TriangleCallback callback, Vector3f aabbMin, Vector3f aabbMax) {
       ScaledTriangleCallback scaledCallback = new ScaledTriangleCallback(callback, localScaling);
 
       Vector3f invLocalScaling = Stack.alloc(Vector3f.class);
       invLocalScaling.set(1.f / localScaling.x, 1.f / localScaling.y, 1.f / localScaling.z);
 
       Vector3f scaledAabbMin = Stack.alloc(Vector3f.class);
       Vector3f scaledAabbMax = Stack.alloc(Vector3f.class);
 
       // support negative scaling
       scaledAabbMin.x = localScaling.x >= 0f ? aabbMin.x * invLocalScaling.x : aabbMax.x * invLocalScaling.x;
       scaledAabbMin.y = localScaling.y >= 0f ? aabbMin.y * invLocalScaling.y : aabbMax.y * invLocalScaling.y;
       scaledAabbMin.z = localScaling.z >= 0f ? aabbMin.z * invLocalScaling.z : aabbMax.z * invLocalScaling.z;
 
       scaledAabbMax.x = localScaling.x <= 0f ? aabbMin.x * invLocalScaling.x : aabbMax.x * invLocalScaling.x;
       scaledAabbMax.y = localScaling.y <= 0f ? aabbMin.y * invLocalScaling.y : aabbMax.y * invLocalScaling.y;
       scaledAabbMax.z = localScaling.z <= 0f ? aabbMin.z * invLocalScaling.z : aabbMax.z * invLocalScaling.z;
 
       bvhTriMeshShape.processAllTriangles(scaledCallback, scaledAabbMin, scaledAabbMax);
   }
 
   @Override
   public void getAabb(Transform trans, Vector3f aabbMin, Vector3f aabbMax) {
       Vector3f localAabbMin = bvhTriMeshShape.getLocalAabbMin(Stack.alloc(Vector3f.class));
       Vector3f localAabbMax = bvhTriMeshShape.getLocalAabbMax(Stack.alloc(Vector3f.class));
 
       Vector3f tmpLocalAabbMin = Stack.alloc(Vector3f.class);
       Vector3f tmpLocalAabbMax = Stack.alloc(Vector3f.class);
       VectorUtil.mul(tmpLocalAabbMin, localAabbMin, localScaling);
       VectorUtil.mul(tmpLocalAabbMax, localAabbMax, localScaling);
 
       localAabbMin.x = (localScaling.x >= 0f) ? tmpLocalAabbMin.x : tmpLocalAabbMax.x;
       localAabbMin.y = (localScaling.y >= 0f) ? tmpLocalAabbMin.y : tmpLocalAabbMax.y;
       localAabbMin.z = (localScaling.z >= 0f) ? tmpLocalAabbMin.z : tmpLocalAabbMax.z;
       localAabbMax.x = (localScaling.x <= 0f) ? tmpLocalAabbMin.x : tmpLocalAabbMax.x;
       localAabbMax.y = (localScaling.y <= 0f) ? tmpLocalAabbMin.y : tmpLocalAabbMax.y;
       localAabbMax.z = (localScaling.z <= 0f) ? tmpLocalAabbMin.z : tmpLocalAabbMax.z;
 
       Vector3f localHalfExtents = Stack.alloc(Vector3f.class);
       localHalfExtents.sub(localAabbMax, localAabbMin);
       localHalfExtents.scale(0.5f);
 
       float margin = bvhTriMeshShape.getMargin();
       localHalfExtents.x += margin;
       localHalfExtents.y += margin;
       localHalfExtents.z += margin;
 
       Vector3f localCenter = Stack.alloc(Vector3f.class);
       localCenter.add(localAabbMax, localAabbMin);
       localCenter.scale(0.5f);
 
       Matrix3f abs_b = (Matrix3f) Stack.alloc(trans.basis);
       MatrixUtil.absolute(abs_b);
 
       Vector3f center = (Vector3f) Stack.alloc(localCenter);
       trans.transform(center);
 
       Vector3f extent = Stack.alloc(Vector3f.class);
       Vector3f tmp = Stack.alloc(Vector3f.class);
       abs_b.getRow(0, tmp);
       extent.x = tmp.dot(localHalfExtents);
       abs_b.getRow(1, tmp);
       extent.y = tmp.dot(localHalfExtents);
       abs_b.getRow(2, tmp);
       extent.z = tmp.dot(localHalfExtents);
 
       aabbMin.sub(center, extent);
       aabbMax.add(center, extent);
   }
 
   @Override
   public BroadphaseNativeType getShapeType() {
       return BroadphaseNativeType.SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE;
   }
 
   @Override
   public void setLocalScaling(Vector3f scaling) {
       localScaling.set(scaling);
   }
 
   @Override
   public Vector3f getLocalScaling(Vector3f out) {
       out.set(localScaling);
       return out;
   }
 
   @Override
   public void calculateLocalInertia(float mass, Vector3f inertia) {
   }
 
   @Override
   public String getName() {
       return "SCALEDBVHTRIANGLEMESH";
   }
 
   ////////////////////////////////////////////////////////////////////////////
 
   private static class ScaledTriangleCallback extends TriangleCallback {
       private TriangleCallback originalCallback;
       private Vector3f localScaling;
       private Vector3f[] newTriangle = new Vector3f[3];
 
       public ScaledTriangleCallback(TriangleCallback originalCallback, Vector3f localScaling) {
           this.originalCallback = originalCallback;
           this.localScaling = localScaling;
           
           for (int i=0; i<newTriangle.length; i++) {
               newTriangle[i] = new Vector3f();
           }
       }
 
       public void processTriangle(Vector3f[] triangle, int partId, int triangleIndex) {
           VectorUtil.mul(newTriangle[0], triangle[0], localScaling);
           VectorUtil.mul(newTriangle[1], triangle[1], localScaling);
           VectorUtil.mul(newTriangle[2], triangle[2], localScaling);
           originalCallback.processTriangle(newTriangle, partId, triangleIndex);
       }
   }
 
}