Normand Briere
2018-05-22 42107f9a01652cb2f47228d20c1148a2a22f6a63
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
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Java port of Bullet (c) 2008 Martin Dvorak <jezek2@advel.cz>
 *
 * This source file is part of GIMPACT Library.
 *
 * For the latest info, see http://gimpact.sourceforge.net/
 *
 * Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
 * email: projectileman@yahoo.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.extras.gimpact;
 
import com.bulletphysics.linearmath.Transform;
import com.bulletphysics.util.ObjectArrayList;
import cz.advel.stack.Stack;
import javax.vecmath.Vector3f;
import javax.vecmath.Vector4f;
 
/**
 *
 * @author jezek2
 */
public class PrimitiveTriangle {
 
   private final ObjectArrayList<Vector3f> tmpVecList1 = new ObjectArrayList<Vector3f>(TriangleContact.MAX_TRI_CLIPPING);
   private final ObjectArrayList<Vector3f> tmpVecList2 = new ObjectArrayList<Vector3f>(TriangleContact.MAX_TRI_CLIPPING);
   private final ObjectArrayList<Vector3f> tmpVecList3 = new ObjectArrayList<Vector3f>(TriangleContact.MAX_TRI_CLIPPING);
   
   {
       for (int i=0; i<TriangleContact.MAX_TRI_CLIPPING; i++) {
           tmpVecList1.add(new Vector3f());
           tmpVecList2.add(new Vector3f());
           tmpVecList3.add(new Vector3f());
       }
   }
   
   public final Vector3f[] vertices = new Vector3f[3];
   public final Vector4f plane = new Vector4f();
   public float margin = 0.01f;
 
   public PrimitiveTriangle() {
       for (int i=0; i<vertices.length; i++) {
           vertices[i] = new Vector3f();
       }
   }
   
   public void set(PrimitiveTriangle tri) {
       throw new UnsupportedOperationException();
   }
   
   public void buildTriPlane() {
       Vector3f tmp1 = Stack.alloc(Vector3f.class);
       Vector3f tmp2 = Stack.alloc(Vector3f.class);
 
       Vector3f normal = Stack.alloc(Vector3f.class);
       tmp1.sub(vertices[1], vertices[0]);
       tmp2.sub(vertices[2], vertices[0]);
       normal.cross(tmp1, tmp2);
       normal.normalize();
 
       plane.set(normal.x, normal.y, normal.z, vertices[0].dot(normal));
   }
 
   /**
    * Test if triangles could collide.
    */
   public boolean overlap_test_conservative(PrimitiveTriangle other) {
       float total_margin = margin + other.margin;
       // classify points on other triangle
       float dis0 = ClipPolygon.distance_point_plane(plane, other.vertices[0]) - total_margin;
 
       float dis1 = ClipPolygon.distance_point_plane(plane, other.vertices[1]) - total_margin;
 
       float dis2 = ClipPolygon.distance_point_plane(plane, other.vertices[2]) - total_margin;
 
       if (dis0 > 0.0f && dis1 > 0.0f && dis2 > 0.0f) {
           return false; // classify points on this triangle
       }
       
       dis0 = ClipPolygon.distance_point_plane(other.plane, vertices[0]) - total_margin;
 
       dis1 = ClipPolygon.distance_point_plane(other.plane, vertices[1]) - total_margin;
 
       dis2 = ClipPolygon.distance_point_plane(other.plane, vertices[2]) - total_margin;
 
       if (dis0 > 0.0f && dis1 > 0.0f && dis2 > 0.0f) {
           return false;
       }
       return true;
   }
   
   /**
    * Calcs the plane which is paralele to the edge and perpendicular to the triangle plane.
    * This triangle must have its plane calculated.
    */
   public void get_edge_plane(int edge_index, Vector4f plane) {
       Vector3f e0 = vertices[edge_index];
       Vector3f e1 = vertices[(edge_index + 1) % 3];
 
       Vector3f tmp = Stack.alloc(Vector3f.class);
       tmp.set(this.plane.x, this.plane.y, this.plane.z);
 
       GeometryOperations.edge_plane(e0, e1, tmp, plane);
   }
 
   public void applyTransform(Transform t) {
       t.transform(vertices[0]);
       t.transform(vertices[1]);
       t.transform(vertices[2]);
   }
   
   /**
    * Clips the triangle against this.
    * 
    * @param clipped_points must have MAX_TRI_CLIPPING size, and this triangle must have its plane calculated.
    * @return the number of clipped points
    */
   public int clip_triangle(PrimitiveTriangle other, ObjectArrayList<Vector3f> clipped_points) {
       // edge 0
       ObjectArrayList<Vector3f> temp_points = tmpVecList1;
 
       Vector4f edgeplane = Stack.alloc(Vector4f.class);
 
       get_edge_plane(0, edgeplane);
 
       int clipped_count = ClipPolygon.plane_clip_triangle(edgeplane, other.vertices[0], other.vertices[1], other.vertices[2], temp_points);
 
       if (clipped_count == 0) {
           return 0;
       }
       ObjectArrayList<Vector3f> temp_points1 = tmpVecList2;
 
       // edge 1
       get_edge_plane(1, edgeplane);
 
       clipped_count = ClipPolygon.plane_clip_polygon(edgeplane, temp_points, clipped_count, temp_points1);
 
       if (clipped_count == 0) {
           return 0; // edge 2
       }
       get_edge_plane(2, edgeplane);
 
       clipped_count = ClipPolygon.plane_clip_polygon(edgeplane, temp_points1, clipped_count, clipped_points);
 
       return clipped_count;
   }
   
   /**
    * Find collision using the clipping method.
    * This triangle and other must have their triangles calculated.
    */
   public boolean find_triangle_collision_clip_method(PrimitiveTriangle other, TriangleContact contacts) {
       float margin = this.margin + other.margin;
 
       ObjectArrayList<Vector3f> clipped_points = tmpVecList3;
 
       int clipped_count;
       //create planes
       // plane v vs U points
 
       TriangleContact contacts1 = Stack.alloc(TriangleContact.class);
 
       contacts1.separating_normal.set(plane);
 
       clipped_count = clip_triangle(other, clipped_points);
 
       if (clipped_count == 0) {
           return false; // Reject
       }
 
       // find most deep interval face1
       contacts1.merge_points(contacts1.separating_normal, margin, clipped_points, clipped_count);
       if (contacts1.point_count == 0) {
           return false; // too far
       // Normal pointing to this triangle
       }
       contacts1.separating_normal.x *= -1.f;
       contacts1.separating_normal.y *= -1.f;
       contacts1.separating_normal.z *= -1.f;
 
       // Clip tri1 by tri2 edges
       TriangleContact contacts2 = Stack.alloc(TriangleContact.class);
       contacts2.separating_normal.set(other.plane);
 
       clipped_count = other.clip_triangle(this, clipped_points);
 
       if (clipped_count == 0) {
           return false; // Reject
       }
 
       // find most deep interval face1
       contacts2.merge_points(contacts2.separating_normal, margin, clipped_points, clipped_count);
       if (contacts2.point_count == 0) {
           return false; // too far
 
       // check most dir for contacts
       }
       if (contacts2.penetration_depth < contacts1.penetration_depth) {
           contacts.copy_from(contacts2);
       }
       else {
           contacts.copy_from(contacts1);
       }
       return true;
   }
   
}