Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
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
/*
 * 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.BulletGlobals;
import com.bulletphysics.linearmath.VectorUtil;
import cz.advel.stack.Stack;
import javax.vecmath.Vector3f;
import javax.vecmath.Vector4f;
 
/**
 *
 * @author jezek2
 */
class GeometryOperations {
 
   public static final float PLANEDIREPSILON = 0.0000001f;
   public static final float PARALELENORMALS = 0.000001f;
   
   public static final float CLAMP(float number, float minval, float maxval) {
       return (number < minval? minval : (number > maxval? maxval : number));
   }
 
   /**
    * Calc a plane from a triangle edge an a normal.
    */
   public static void edge_plane(Vector3f e1, Vector3f e2, Vector3f normal, Vector4f plane) {
       Vector3f planenormal = Stack.alloc(Vector3f.class);
       planenormal.sub(e2, e1);
       planenormal.cross(planenormal, normal);
       planenormal.normalize();
 
       plane.set(planenormal);
       plane.w = e2.dot(planenormal);
   }
   
   /**
    * Finds the closest point(cp) to (v) on a segment (e1,e2).
    */
   public static void closest_point_on_segment(Vector3f cp, Vector3f v, Vector3f e1, Vector3f e2) {
       Vector3f n = Stack.alloc(Vector3f.class);
       n.sub(e2, e1);
       cp.sub(v, e1);
       float _scalar = cp.dot(n) / n.dot(n);
       if (_scalar < 0.0f) {
           cp = e1;
       }
       else if (_scalar > 1.0f) {
           cp = e2;
       }
       else {
           cp.scaleAdd(_scalar, n, e1);
       }
   }
   
   /**
    * Line plane collision.
    * 
    * @return -0 if the ray never intersects, -1 if the ray collides in front, -2 if the ray collides in back
    */
   public static int line_plane_collision(Vector4f plane, Vector3f vDir, Vector3f vPoint, Vector3f pout, float[] tparam, float tmin, float tmax) {
       float _dotdir = VectorUtil.dot3(vDir, plane);
 
       if (Math.abs(_dotdir) < PLANEDIREPSILON) {
           tparam[0] = tmax;
           return 0;
       }
 
       float _dis = ClipPolygon.distance_point_plane(plane, vPoint);
       int returnvalue = _dis < 0.0f ? 2 : 1;
       tparam[0] = -_dis / _dotdir;
 
       if (tparam[0] < tmin) {
           returnvalue = 0;
           tparam[0] = tmin;
       }
       else if (tparam[0] > tmax) {
           returnvalue = 0;
           tparam[0] = tmax;
       }
       pout.scaleAdd(tparam[0], vDir, vPoint);
       return returnvalue;
   }
   
   /**
    * Find closest points on segments.
    */
   public static void segment_collision(Vector3f vA1, Vector3f vA2, Vector3f vB1, Vector3f vB2, Vector3f vPointA, Vector3f vPointB) {
       Vector3f AD = Stack.alloc(Vector3f.class);
       AD.sub(vA2, vA1);
 
       Vector3f BD = Stack.alloc(Vector3f.class);
       BD.sub(vB2, vB1);
 
       Vector3f N = Stack.alloc(Vector3f.class);
       N.cross(AD, BD);
       float[] tp = new float[] { N.lengthSquared() };
 
       Vector4f _M = Stack.alloc(Vector4f.class);//plane
 
       if (tp[0] < BulletGlobals.SIMD_EPSILON)//ARE PARALELE
       {
           // project B over A
           boolean invert_b_order = false;
           _M.x = vB1.dot(AD);
           _M.y = vB2.dot(AD);
 
           if (_M.x > _M.y) {
               invert_b_order = true;
               //BT_SWAP_NUMBERS(_M[0],_M[1]);
               _M.x = _M.x + _M.y;
               _M.y = _M.x - _M.y;
               _M.x = _M.x - _M.y;
           }
           _M.z = vA1.dot(AD);
           _M.w = vA2.dot(AD);
           // mid points
           N.x = (_M.x + _M.y) * 0.5f;
           N.y = (_M.z + _M.w) * 0.5f;
 
           if (N.x < N.y) {
               if (_M.y < _M.z) {
                   vPointB = invert_b_order ? vB1 : vB2;
                   vPointA = vA1;
               }
               else if (_M.y < _M.w) {
                   vPointB = invert_b_order ? vB1 : vB2;
                   closest_point_on_segment(vPointA, vPointB, vA1, vA2);
               }
               else {
                   vPointA = vA2;
                   closest_point_on_segment(vPointB, vPointA, vB1, vB2);
               }
           }
           else {
               if (_M.w < _M.x) {
                   vPointB = invert_b_order ? vB2 : vB1;
                   vPointA = vA2;
               }
               else if (_M.w < _M.y) {
                   vPointA = vA2;
                   closest_point_on_segment(vPointB, vPointA, vB1, vB2);
               }
               else {
                   vPointB = invert_b_order ? vB1 : vB2;
                   closest_point_on_segment(vPointA, vPointB, vA1, vA2);
               }
           }
           return;
       }
 
       N.cross(N, BD);
       _M.set(N.x, N.y, N.z, vB1.dot(N));
 
       // get point A as the plane collision point
       line_plane_collision(_M, AD, vA1, vPointA, tp, 0f, 1f);
 
       /*Closest point on segment*/
       vPointB.sub(vPointA, vB1);
       tp[0] = vPointB.dot(BD);
       tp[0] /= BD.dot(BD);
       tp[0] = CLAMP(tp[0], 0.0f, 1.0f);
 
       vPointB.scaleAdd(tp[0], BD, vB1);
   }
   
}