aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/OdePlugin/Meshing\/HelperTypes.cs
blob: 3d40c0451b2ec2ed9f05226587478b85cb903921 (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
using System;
using System.Globalization;
using System.Diagnostics;
using System.Collections.Generic;

using OpenSim.Region.Physics.Manager;

public class Vertex : IComparable<Vertex>
{
    public String name;
    public PhysicsVector point;

    public Vertex(String name, float x, float y, float z)
    {
        this.name = name;
        point = new PhysicsVector(x, y, z);
    }

    public int CompareTo(Vertex other)
    {
        if (point.X < other.point.X)
            return -1;

        if (point.X > other.point.X)
            return 1;

        if (point.Y < other.point.Y)
            return -1;

        if (point.Y > other.point.Y)
            return 1;

        if (point.Z < other.point.Z)
            return -1;

        if (point.Z > other.point.Z)
            return 1;

        return 0;
    }

    public static bool operator >(Vertex me, Vertex other)
    {
        return me.CompareTo(other) > 0;
    }

    public static bool operator <(Vertex me, Vertex other)
    {
        return me.CompareTo(other) < 0;
    }


}

public class Simplex : IComparable<Simplex>
{
    public Vertex v1;
    public Vertex v2;

    public Simplex(Vertex _v1, Vertex _v2)
    {
        // Presort indices to make sorting (comparing) easier
        if (_v1 > _v2)
        {
            v1 = _v1;
            v2 = _v2;
        }
        else
        {
            v1 = _v2;
            v2 = _v1;
        }
    }

    public int CompareTo(Simplex other)
    {
        if (v1 > other.v1)
        {
            return 1;
        }
        if (v1 < other.v1)
        {
            return -1;
        }

        if (v2 > other.v2)
        {
            return 1;
        }
        if (v2 < other.v2)
        {
            return -1;
        }

        return 0;
    }

};

public class Triangle
{
    public Vertex v1;
    public Vertex v2;
    public Vertex v3;

    float radius_square;
    float cx;
    float cy;

    public Triangle(Vertex _v1, Vertex _v2, Vertex _v3)
    {
        v1 = _v1;
        v2 = _v2;
        v3 = _v3;

        CalcCircle();
    }

    public bool isInCircle(float x, float y)
    {
        float dx, dy;
        float dd;

        dx = x - this.cx;
        dy = y - this.cy;

        dd = dx * dx + dy * dy;
        if (dd < this.radius_square)
            return true;
        else
            return false;
    }


    void CalcCircle()
    {
        // Calculate the center and the radius of a circle given by three points p1, p2, p3
        // It is assumed, that the triangles vertices are already set correctly
        double p1x, p2x, p1y, p2y, p3x, p3y;

        // Deviation of this routine: 
        // A circle has the general equation (M-p)^2=r^2, where M and p are vectors
        // this gives us three equations f(p)=r^2, each for one point p1, p2, p3
        // putting respectively two equations together gives two equations
        // f(p1)=f(p2) and f(p1)=f(p3)
        // bringing all constant terms to one side brings them to the form
        // M*v1=c1 resp.M*v2=c2 where v1=(p1-p2) and v2=(p1-p3) (still vectors)
        // and c1, c2 are scalars (Naming conventions like the variables below)
        // Now using the equations that are formed by the components of the vectors
        // and isolate Mx lets you make one equation that only holds My
        // The rest is straight forward and eaasy :-)
        // 

        /* helping variables for temporary results */
        double c1, c2;
        double v1x, v1y, v2x, v2y;

        double z, n;

        double rx, ry;

        // Readout the three points, the triangle consists of
        p1x = v1.point.X;
        p1y = v1.point.Y;

        p2x = v2.point.X;
        p2y = v2.point.Y;

        p3x = v3.point.X;
        p3y = v3.point.Y;

        /* calc helping values first */
        c1 = (p1x * p1x + p1y * p1y - p2x * p2x - p2y * p2y) / 2;
        c2 = (p1x * p1x + p1y * p1y - p3x * p3x - p3y * p3y) / 2;

        v1x = p1x - p2x;
        v1y = p1y - p2y;

        v2x = p1x - p3x;
        v2y = p1y - p3y;

        z = (c1 * v2x - c2 * v1x);
        n = (v1y * v2x - v2y * v1x);

        if (n == 0.0)	// This is no triangle, i.e there are (at least) two points at the same location
        {
            radius_square = 0.0f;
            return;
        }

        this.cy = (float)(z / n);

        if (v2x != 0.0)
        {
            this.cx = (float)((c2 - v2y * this.cy) / v2x);
        }
        else if (v1x != 0.0)
        {
            this.cx = (float)((c1 - v1y * this.cy) / v1x);
        }
        else
        {
            Debug.Assert(false, "Malformed triangle"); /* Both terms zero means nothing good */
        }

        rx = (p1x - this.cx);
        ry = (p1y - this.cy);

        this.radius_square = (float)(rx * rx + ry * ry);

    }

    public List<Simplex> GetSimplices()
    {
        List<Simplex> result = new List<Simplex>();
        Simplex s1 = new Simplex(v1, v2);
        Simplex s2 = new Simplex(v2, v3);
        Simplex s3 = new Simplex(v3, v1);

        result.Add(s1);
        result.Add(s2);
        result.Add(s3);

        return result;
    }

    public override String ToString()
    {

        NumberFormatInfo nfi = new NumberFormatInfo();
        nfi.CurrencyDecimalDigits = 2;
        nfi.CurrencyDecimalSeparator = ".";

        String s1 = "<" + v1.point.X.ToString(nfi) + "," + v1.point.Y.ToString(nfi) + "," + v1.point.Z.ToString(nfi) + ">";
        String s2 = "<" + v2.point.X.ToString(nfi) + "," + v2.point.Y.ToString(nfi) + "," + v2.point.Z.ToString(nfi) + ">";
        String s3 = "<" + v3.point.X.ToString(nfi) + "," + v3.point.Y.ToString(nfi) + "," + v3.point.Z.ToString(nfi) + ">";

        return s1 + ";" + s2 + ";" + s3;

    }

    public PhysicsVector getNormal()
    {
        // Vertices

        // Vectors for edges
        PhysicsVector e1;
        PhysicsVector e2;

        e1 = new PhysicsVector(v1.point.X - v2.point.X, v1.point.Y - v2.point.Y, v1.point.Z - v2.point.Z);
        e2 = new PhysicsVector(v1.point.X - v3.point.X, v1.point.Y - v3.point.Y, v1.point.Z - v3.point.Z);

        // Cross product for normal
        PhysicsVector n = new PhysicsVector();
        float nx, ny, nz;
        n.X = e1.Y * e2.Z - e1.Z * e2.Y;
        n.Y = e1.Z * e2.X - e1.X * e2.Z;
        n.Z = e1.X * e2.Y - e1.Y * e2.X;

        // Length
        float l = (float)Math.Sqrt(n.X * n.X + n.Y * n.Y + n.Z * n.Z);

        // Normalized "normal"
        n.X /= l;
        n.Y /= l;
        n.Z /= l;

        return n;
    }

    public void invertNormal()
    {
        Vertex vt;
        vt = v1;
        v1 = v2;
        v2 = vt;
    }
}