aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/PhysicsModules/ConvexDecompositionDotNet/float3.cs
blob: fde9b32224b77c3fdfbd5a9c9b2dd1afeaf8621b (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* The MIT License
 * 
 * Copyright (c) 2010 Intel Corporation.
 * All rights reserved.
 *
 * Based on the convexdecomposition library from 
 * <http://codesuppository.googlecode.com> by John W. Ratcliff and Stan Melax.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;

namespace OpenSim.Region.PhysicsModule.ConvexDecompositionDotNet
{
    public class float3 : IEquatable<float3>
    {
        public float x;
        public float y;
        public float z;

        public float3()
        {
            x = 0;
            y = 0;
            z = 0;
        }

        public float3(float _x, float _y, float _z)
        {
            x = _x;
            y = _y;
            z = _z;
        }

        public float3(float3 f)
        {
            x = f.x;
            y = f.y;
            z = f.z;
        }

        public float this[int i]
        {
            get
            {
                switch (i)
                {
                    case 0: return x;
                    case 1: return y;
                    case 2: return z;
                }
                throw new ArgumentOutOfRangeException();
            }
        }

        public float Distance(float3 a)
        {
            float3 d = new float3(a.x - x, a.y - y, a.z - z);
            return d.Length();
        }

        public float Distance2(float3 a)
        {
            float dx = a.x - x;
            float dy = a.y - y;
            float dz = a.z - z;
            return dx * dx + dy * dy + dz * dz;
        }

        public float Length()
        {
            return (float)Math.Sqrt(x * x + y * y + z * z);
        }

        public float Area(float3 p1, float3 p2)
        {
            float A = Partial(p1);
            A += p1.Partial(p2);
            A += p2.Partial(this);
            return A * 0.5f;
        }

        public float Partial(float3 p)
        {
            return (x * p.y) - (p.x * y);
        }

        // Given a point and a line (defined by two points), compute the closest point
        // in the line.  (The line is treated as infinitely long.)
        public void NearestPointInLine(float3 point, float3 line0, float3 line1)
        {
            float3 nearestPoint = new float3();
            float3 lineDelta = line1 - line0;

            // Handle degenerate lines
            if (lineDelta == float3.Zero)
            {
                nearestPoint = line0;
            }
            else
            {
                float delta = float3.dot(point - line0, lineDelta) / float3.dot(lineDelta, lineDelta);
                nearestPoint = line0 + lineDelta * delta;
            }

            this.x = nearestPoint.x;
            this.y = nearestPoint.y;
            this.z = nearestPoint.z;
        }

        // Given a point and a line segment (defined by two points), compute the closest point
        // in the line.  Cap the point at the endpoints of the line segment.
        public void NearestPointInLineSegment(float3 point, float3 line0, float3 line1)
        {
            float3 nearestPoint = new float3();
            float3 lineDelta = line1 - line0;

            // Handle degenerate lines
            if (lineDelta == Zero)
            {
                nearestPoint = line0;
            }
            else
            {
                float delta = float3.dot(point - line0, lineDelta) / float3.dot(lineDelta, lineDelta);

                // Clamp the point to conform to the segment's endpoints
                if (delta < 0)
                    delta = 0;
                else if (delta > 1)
                    delta = 1;

                nearestPoint = line0 + lineDelta * delta;
            }

            this.x = nearestPoint.x;
            this.y = nearestPoint.y;
            this.z = nearestPoint.z;
        }

        // Given a point and a triangle (defined by three points), compute the closest point
        // in the triangle.  Clamp the point so it's confined to the area of the triangle.
        public void NearestPointInTriangle(float3 point, float3 triangle0, float3 triangle1, float3 triangle2)
        {
            float3 nearestPoint = new float3();

            float3 lineDelta0 = triangle1 - triangle0;
            float3 lineDelta1 = triangle2 - triangle0;

            // Handle degenerate triangles
            if ((lineDelta0 == Zero) || (lineDelta1 == Zero))
            {
                nearestPoint.NearestPointInLineSegment(point, triangle1, triangle2);
            }
            else if (lineDelta0 == lineDelta1)
            {
                nearestPoint.NearestPointInLineSegment(point, triangle0, triangle1);
            }
            else
            {
                float3[] axis = new float3[3] { new float3(), new float3(), new float3() };
                axis[0].NearestPointInLine(triangle0, triangle1, triangle2);
                axis[1].NearestPointInLine(triangle1, triangle0, triangle2);
                axis[2].NearestPointInLine(triangle2, triangle0, triangle1);

                float3 axisDot = new float3();
                axisDot.x = dot(triangle0 - axis[0], point - axis[0]);
                axisDot.y = dot(triangle1 - axis[1], point - axis[1]);
                axisDot.z = dot(triangle2 - axis[2], point - axis[2]);

                bool bForce = true;
                float bestMagnitude2 = 0;
                float closeMagnitude2;
                float3 closePoint = new float3();

                if (axisDot.x < 0f)
                {
                    closePoint.NearestPointInLineSegment(point, triangle1, triangle2);
                    closeMagnitude2 = point.Distance2(closePoint);
                    if (bForce || (bestMagnitude2 > closeMagnitude2))
                    {
                        bForce = false;
                        bestMagnitude2 = closeMagnitude2;
                        nearestPoint = closePoint;
                    }
                }
                if (axisDot.y < 0f)
                {
                    closePoint.NearestPointInLineSegment(point, triangle0, triangle2);
                    closeMagnitude2 = point.Distance2(closePoint);
                    if (bForce || (bestMagnitude2 > closeMagnitude2))
                    {
                        bForce = false;
                        bestMagnitude2 = closeMagnitude2;
                        nearestPoint = closePoint;
                    }
                }
                if (axisDot.z < 0f)
                {
                    closePoint.NearestPointInLineSegment(point, triangle0, triangle1);
                    closeMagnitude2 = point.Distance2(closePoint);
                    if (bForce || (bestMagnitude2 > closeMagnitude2))
                    {
                        bForce = false;
                        bestMagnitude2 = closeMagnitude2;
                        nearestPoint = closePoint;
                    }
                }

                // If bForce is true at this point, it means the nearest point lies
                // inside the triangle; use the nearest-point-on-a-plane equation
                if (bForce)
                {
                    float3 normal;

                    // Get the normal of the polygon (doesn't have to be a unit vector)
                    normal = float3.cross(lineDelta0, lineDelta1);

                    float3 pointDelta = point - triangle0;
                    float delta = float3.dot(normal, pointDelta) / float3.dot(normal, normal);

                    nearestPoint = point - normal * delta;
                }
            }

            this.x = nearestPoint.x;
            this.y = nearestPoint.y;
            this.z = nearestPoint.z;
        }

        public static float3 operator +(float3 a, float3 b)
        {
            return new float3(a.x + b.x, a.y + b.y, a.z + b.z);
        }

        public static float3 operator -(float3 a, float3 b)
        {
            return new float3(a.x - b.x, a.y - b.y, a.z - b.z);
        }

        public static float3 operator -(float3 a, float s)
        {
            return new float3(a.x - s, a.y - s, a.z - s);
        }

        public static float3 operator -(float3 v)
        {
            return new float3(-v.x, -v.y, -v.z);
        }

        public static float3 operator *(float3 v, float s)
        {
            return new float3(v.x * s, v.y * s, v.z * s);
        }

        public static float3 operator *(float s, float3 v)
        {
            return new float3(v.x * s, v.y * s, v.z * s);
        }

        public static float3 operator *(float3 v, float3x3 m)
        {
            return new float3((m.x.x * v.x + m.y.x * v.y + m.z.x * v.z), (m.x.y * v.x + m.y.y * v.y + m.z.y * v.z), (m.x.z * v.x + m.y.z * v.y + m.z.z * v.z));
        }

        public static float3 operator *(float3x3 m, float3 v)
        {
            return new float3(dot(m.x, v), dot(m.y, v), dot(m.z, v));
        }

        public static float3 operator /(float3 v, float s)
        {
            float sinv = 1.0f / s;
            return new float3(v.x * sinv, v.y * sinv, v.z * sinv);
        }

        public bool Equals(float3 other)
        {
            return this == other;
        }

        public override bool Equals(object obj)
        {
            float3 f = obj as float3;
            if (f == null)
                return false;

            return this == f;
        }

        public override int GetHashCode()
        {
            return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
        }

        public static bool operator ==(float3 a, float3 b)
        {
            // If both are null, or both are same instance, return true.
            if (System.Object.ReferenceEquals(a, b))
                return true;
            // If one is null, but not both, return false.
            if (((object)a == null) || ((object)b == null))
                return false;

            return (a.x == b.x && a.y == b.y && a.z == b.z);
        }

        public static bool operator !=(float3 a, float3 b)
        {
            return (a.x != b.x || a.y != b.y || a.z != b.z);
        }

        public static float dot(float3 a, float3 b)
        {
            return a.x * b.x + a.y * b.y + a.z * b.z;
        }

        public static float3 cmul(float3 v1, float3 v2)
        {
            return new float3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
        }

        public static float3 cross(float3 a, float3 b)
        {
            return new float3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
        }

        public static float3 Interpolate(float3 v0, float3 v1, float alpha)
        {
            return v0 * (1 - alpha) + v1 * alpha;
        }

        public static float3 Round(float3 a, int digits)
        {
            return new float3((float)Math.Round(a.x, digits), (float)Math.Round(a.y, digits), (float)Math.Round(a.z, digits));
        }

        public static float3 VectorMax(float3 a, float3 b)
        {
            return new float3(Math.Max(a.x, b.x), Math.Max(a.y, b.y), Math.Max(a.z, b.z));
        }

        public static float3 VectorMin(float3 a, float3 b)
        {
            return new float3(Math.Min(a.x, b.x), Math.Min(a.y, b.y), Math.Min(a.z, b.z));
        }

        public static float3 vabs(float3 v)
        {
            return new float3(Math.Abs(v.x), Math.Abs(v.y), Math.Abs(v.z));
        }

        public static float magnitude(float3 v)
        {
            return (float)Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
        }

        public static float3 normalize(float3 v)
        {
            float d = magnitude(v);
            if (d == 0)
                d = 0.1f;
            d = 1 / d;
            return new float3(v.x * d, v.y * d, v.z * d);
        }

        public static float3 safenormalize(float3 v)
        {
            if (magnitude(v) <= 0.0f)
                return new float3(1, 0, 0);
            else
                return normalize(v);
        }

        public static float Yaw(float3 v)
        {
            return (v.y == 0.0 && v.x == 0.0) ? 0.0f : (float)Math.Atan2(-v.x, v.y) * (180.0f / 3.14159264f);
        }

        public static float Pitch(float3 v)
        {
            return (float)Math.Atan2(v.z, Math.Sqrt(v.x * v.x + v.y * v.y)) * (180.0f / 3.14159264f);
        }

        public float ComputePlane(float3 A, float3 B, float3 C)
        {
            float vx, vy, vz, wx, wy, wz, vw_x, vw_y, vw_z, mag;

            vx = (B.x - C.x);
            vy = (B.y - C.y);
            vz = (B.z - C.z);

            wx = (A.x - B.x);
            wy = (A.y - B.y);
            wz = (A.z - B.z);

            vw_x = vy * wz - vz * wy;
            vw_y = vz * wx - vx * wz;
            vw_z = vx * wy - vy * wx;

            mag = (float)Math.Sqrt((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));

            if (mag < 0.000001f)
            {
                mag = 0;
            }
            else
            {
                mag = 1.0f / mag;
            }

            x = vw_x * mag;
            y = vw_y * mag;
            z = vw_z * mag;

            float D = 0.0f - ((x * A.x) + (y * A.y) + (z * A.z));
            return D;
        }

        public override string ToString()
        {
            return String.Format("<{0}, {1}, {2}>", x, y, z);
        }

        public static readonly float3 Zero = new float3();
    }
}