Normand Briere
2018-07-01 89c1ad67bc65d24ceadfa9e95f8c5515283f1e97
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * 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.BulletGlobals;
import com.bulletphysics.collision.broadphase.BroadphaseNativeType;
import com.bulletphysics.linearmath.VectorUtil;
import com.bulletphysics.util.ObjectArrayList;
import cz.advel.stack.Stack;
import javax.vecmath.Vector3f;
 
/**
 * ConvexHullShape implements an implicit convex hull of an array of vertices.
 * Bullet provides a general and fast collision detector for convex shapes based
 * on GJK and EPA using localGetSupportingVertex.
 * 
 * @author jezek2
 */
public class ConvexHullShape extends PolyhedralConvexShape {
 
   private final ObjectArrayList<Vector3f> points = new ObjectArrayList<Vector3f>();
   
   /**
    * TODO: This constructor optionally takes in a pointer to points. Each point is assumed to be 3 consecutive float (x,y,z), the striding defines the number of bytes between each point, in memory.
    * It is easier to not pass any points in the constructor, and just add one point at a time, using addPoint.
    * ConvexHullShape make an internal copy of the points.
    */
   // TODO: make better constuctors (ByteBuffer, etc.)
   public ConvexHullShape(ObjectArrayList<Vector3f> points) {
       // JAVA NOTE: rewritten
       
       for (int i=0; i<points.size(); i++) {
           this.points.add(new Vector3f(points.getQuick(i)));
       }
       
       recalcLocalAabb();
   }
 
   @Override
   public void setLocalScaling(Vector3f scaling) {
       localScaling.set(scaling);
       recalcLocalAabb();
   }
   
   public void addPoint(Vector3f point) {
       points.add(new Vector3f(point));
       recalcLocalAabb();
   }
 
   public ObjectArrayList<Vector3f> getPoints() {
       return points;
   }
 
   public int getNumPoints() {
       return points.size();
   }
 
   @Override
   public Vector3f localGetSupportingVertexWithoutMargin(Vector3f vec0, Vector3f out) {
       Vector3f supVec = out;
       supVec.set(0f, 0f, 0f);
       float newDot, maxDot = -1e30f;
 
       Vector3f vec = (Vector3f) Stack.alloc(vec0);
       float lenSqr = vec.lengthSquared();
       if (lenSqr < 0.0001f) {
           vec.set(1f, 0f, 0f);
       }
       else {
           float rlen = 1f / (float) Math.sqrt(lenSqr);
           vec.scale(rlen);
       }
 
 
       Vector3f vtx = Stack.alloc(Vector3f.class);
       for (int i = 0; i < points.size(); i++) {
           VectorUtil.mul(vtx, points.getQuick(i), localScaling);
 
           newDot = vec.dot(vtx);
           if (newDot > maxDot) {
               maxDot = newDot;
               supVec.set(vtx);
           }
       }
       return out;
   }
 
   @Override
   public void batchedUnitVectorGetSupportingVertexWithoutMargin(Vector3f[] vectors, Vector3f[] supportVerticesOut, int numVectors) {
       float newDot;
 
       // JAVA NOTE: rewritten as code used W coord for temporary usage in Vector3
       // TODO: optimize it
       float[] wcoords = new float[numVectors];
 
       // use 'w' component of supportVerticesOut?
       {
           for (int i = 0; i < numVectors; i++) {
               //supportVerticesOut[i][3] = btScalar(-1e30);
               wcoords[i] = -1e30f;
           }
       }
       Vector3f vtx = Stack.alloc(Vector3f.class);
       for (int i = 0; i < points.size(); i++) {
           VectorUtil.mul(vtx, points.getQuick(i), localScaling);
 
           for (int j = 0; j < numVectors; j++) {
               Vector3f vec = vectors[j];
 
               newDot = vec.dot(vtx);
               //if (newDot > supportVerticesOut[j][3])
               if (newDot > wcoords[j]) {
                   // WARNING: don't swap next lines, the w component would get overwritten!
                   supportVerticesOut[j].set(vtx);
                   //supportVerticesOut[j][3] = newDot;
                   wcoords[j] = newDot;
               }
           }
       }
   }
 
   @Override
   public Vector3f localGetSupportingVertex(Vector3f vec, Vector3f out) {
       Vector3f supVertex = localGetSupportingVertexWithoutMargin(vec, out);
 
       if (getMargin() != 0f) {
           Vector3f vecnorm = (Vector3f) Stack.alloc(vec);
           if (vecnorm.lengthSquared() < (BulletGlobals.FLT_EPSILON * BulletGlobals.FLT_EPSILON)) {
               vecnorm.set(-1f, -1f, -1f);
           }
           vecnorm.normalize();
           supVertex.scaleAdd(getMargin(), vecnorm, supVertex);
       }
       return out;
   }
 
   /**
    * Currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection.
    * Please note that you can debug-draw ConvexHullShape with the Raytracer Demo.
    */
   @Override
   public int getNumVertices() {
       return points.size();
   }
 
   @Override
   public int getNumEdges() {
       return points.size();
   }
 
   @Override
   public void getEdge(int i, Vector3f pa, Vector3f pb) {
       int index0 = i % points.size();
       int index1 = (i + 1) % points.size();
       VectorUtil.mul(pa, points.getQuick(index0), localScaling);
       VectorUtil.mul(pb, points.getQuick(index1), localScaling);
   }
 
   @Override
   public void getVertex(int i, Vector3f vtx) {
       VectorUtil.mul(vtx, points.getQuick(i), localScaling);
   }
 
   @Override
   public int getNumPlanes() {
       return 0;
   }
 
   @Override
   public void getPlane(Vector3f planeNormal, Vector3f planeSupport, int i) {
       assert false;
   }
 
   @Override
   public boolean isInside(Vector3f pt, float tolerance) {
       assert false;
       return false;
   }
 
   @Override
   public BroadphaseNativeType getShapeType() {
       return BroadphaseNativeType.CONVEX_HULL_SHAPE_PROXYTYPE;
   }
 
   @Override
   public String getName() {
       return "Convex";
   }
 
}