aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/PhysicsModules/ConvexDecompositionDotNet/SplitPlane.cs
blob: 9f06a9a23f826a9d3450c2ad724686fe4f8371c6 (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
/* 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;
using System.Collections.Generic;

namespace OpenSim.Region.Physics.ConvexDecompositionDotNet
{
    public class Rect3d
    {
        public float[] mMin = new float[3];
        public float[] mMax = new float[3];

        public Rect3d()
        {
        }

        public Rect3d(float[] bmin, float[] bmax)
        {
            mMin[0] = bmin[0];
            mMin[1] = bmin[1];
            mMin[2] = bmin[2];

            mMax[0] = bmax[0];
            mMax[1] = bmax[1];
            mMax[2] = bmax[2];
        }

        public void SetMin(float[] bmin)
        {
            mMin[0] = bmin[0];
            mMin[1] = bmin[1];
            mMin[2] = bmin[2];
        }

        public void SetMax(float[] bmax)
        {
            mMax[0] = bmax[0];
            mMax[1] = bmax[1];
            mMax[2] = bmax[2];
        }

        public void SetMin(float x, float y, float z)
        {
            mMin[0] = x;
            mMin[1] = y;
            mMin[2] = z;
        }

        public void SetMax(float x, float y, float z)
        {
            mMax[0] = x;
            mMax[1] = y;
            mMax[2] = z;
        }
    }

    public static class SplitPlane
    {
        public static bool computeSplitPlane(List<float3> vertices, List<int> indices, ref float4 plane)
        {
            float[] bmin = { Single.MaxValue, Single.MaxValue, Single.MaxValue };
            float[] bmax = { Single.MinValue, Single.MinValue, Single.MinValue };

            for (int i = 0; i < vertices.Count; i++)
            {
                float3 p = vertices[i];

                if (p[0] < bmin[0])
                    bmin[0] = p[0];
                if (p[1] < bmin[1])
                    bmin[1] = p[1];
                if (p[2] < bmin[2])
                    bmin[2] = p[2];

                if (p[0] > bmax[0])
                    bmax[0] = p[0];
                if (p[1] > bmax[1])
                    bmax[1] = p[1];
                if (p[2] > bmax[2])
                    bmax[2] = p[2];
            }

            float dx = bmax[0] - bmin[0];
            float dy = bmax[1] - bmin[1];
            float dz = bmax[2] - bmin[2];

            float laxis = dx;

            int axis = 0;

            if (dy > dx)
            {
                axis = 1;
                laxis = dy;
            }

            if (dz > dx && dz > dy)
            {
                axis = 2;
                laxis = dz;
            }

            float[] p1 = new float[3];
            float[] p2 = new float[3];
            float[] p3 = new float[3];

            p3[0] = p2[0] = p1[0] = bmin[0] + dx * 0.5f;
            p3[1] = p2[1] = p1[1] = bmin[1] + dy * 0.5f;
            p3[2] = p2[2] = p1[2] = bmin[2] + dz * 0.5f;

            Rect3d b = new Rect3d(bmin, bmax);

            Rect3d b1 = new Rect3d();
            Rect3d b2 = new Rect3d();

            splitRect(axis, b, b1, b2, p1);

            switch (axis)
            {
                case 0:
                    p2[1] = bmin[1];
                    p2[2] = bmin[2];

                    if (dz > dy)
                    {
                        p3[1] = bmax[1];
                        p3[2] = bmin[2];
                    }
                    else
                    {
                        p3[1] = bmin[1];
                        p3[2] = bmax[2];
                    }

                    break;
                case 1:
                    p2[0] = bmin[0];
                    p2[2] = bmin[2];

                    if (dx > dz)
                    {
                        p3[0] = bmax[0];
                        p3[2] = bmin[2];
                    }
                    else
                    {
                        p3[0] = bmin[0];
                        p3[2] = bmax[2];
                    }

                    break;
                case 2:
                    p2[0] = bmin[0];
                    p2[1] = bmin[1];

                    if (dx > dy)
                    {
                        p3[0] = bmax[0];
                        p3[1] = bmin[1];
                    }
                    else
                    {
                        p3[0] = bmin[0];
                        p3[1] = bmax[1];
                    }

                    break;
            }

            computePlane(p1, p2, p3, plane);

            return true;
        }

        internal static void computePlane(float[] A, float[] B, float[] C, float4 plane)
        {
            float vx = (B[0] - C[0]);
            float vy = (B[1] - C[1]);
            float vz = (B[2] - C[2]);

            float wx = (A[0] - B[0]);
            float wy = (A[1] - B[1]);
            float wz = (A[2] - B[2]);

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

            float 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;
            }

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

            float D = 0.0f - ((x * A[0]) + (y * A[1]) + (z * A[2]));

            plane.x = x;
            plane.y = y;
            plane.z = z;
            plane.w = D;
        }

        public static void splitRect(int axis, Rect3d source, Rect3d b1, Rect3d b2, float[] midpoint)
        {
            switch (axis)
            {
                case 0:
                    b1.SetMin(source.mMin);
                    b1.SetMax(midpoint[0], source.mMax[1], source.mMax[2]);

                    b2.SetMin(midpoint[0], source.mMin[1], source.mMin[2]);
                    b2.SetMax(source.mMax);
                    break;
                case 1:
                    b1.SetMin(source.mMin);
                    b1.SetMax(source.mMax[0], midpoint[1], source.mMax[2]);

                    b2.SetMin(source.mMin[0], midpoint[1], source.mMin[2]);
                    b2.SetMax(source.mMax);
                    break;
                case 2:
                    b1.SetMin(source.mMin);
                    b1.SetMax(source.mMax[0], source.mMax[1], midpoint[2]);

                    b2.SetMin(source.mMin[0], source.mMin[1], midpoint[2]);
                    b2.SetMax(source.mMax);
                    break;
            }
        }
    }
}