aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/OdePlugin/Meshing\/HelperTypes.cs
diff options
context:
space:
mode:
authordan miller2007-09-29 04:08:33 +0000
committerdan miller2007-09-29 04:08:33 +0000
commitc1d3e93fbb3284b38b85294d43189001bfce5214 (patch)
treeb79de3dec16f268822463165040c3263b65bea66 /OpenSim/Region/Physics/OdePlugin/Meshing\/HelperTypes.cs
parentHollow prims (box only), thanks Gerard! Enjoy (diff)
downloadopensim-SC_OLD-c1d3e93fbb3284b38b85294d43189001bfce5214.zip
opensim-SC_OLD-c1d3e93fbb3284b38b85294d43189001bfce5214.tar.gz
opensim-SC_OLD-c1d3e93fbb3284b38b85294d43189001bfce5214.tar.bz2
opensim-SC_OLD-c1d3e93fbb3284b38b85294d43189001bfce5214.tar.xz
Hollow prims (box only), thanks Gerard! Enjoy
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/Physics/OdePlugin/Meshing\/HelperTypes.cs279
1 files changed, 279 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/OdePlugin/Meshing\/HelperTypes.cs b/OpenSim/Region/Physics/OdePlugin/Meshing\/HelperTypes.cs
new file mode 100644
index 0000000..3d40c04
--- /dev/null
+++ b/OpenSim/Region/Physics/OdePlugin/Meshing\/HelperTypes.cs
@@ -0,0 +1,279 @@
1using System;
2using System.Globalization;
3using System.Diagnostics;
4using System.Collections.Generic;
5
6using OpenSim.Region.Physics.Manager;
7
8public class Vertex : IComparable<Vertex>
9{
10 public String name;
11 public PhysicsVector point;
12
13 public Vertex(String name, float x, float y, float z)
14 {
15 this.name = name;
16 point = new PhysicsVector(x, y, z);
17 }
18
19 public int CompareTo(Vertex other)
20 {
21 if (point.X < other.point.X)
22 return -1;
23
24 if (point.X > other.point.X)
25 return 1;
26
27 if (point.Y < other.point.Y)
28 return -1;
29
30 if (point.Y > other.point.Y)
31 return 1;
32
33 if (point.Z < other.point.Z)
34 return -1;
35
36 if (point.Z > other.point.Z)
37 return 1;
38
39 return 0;
40 }
41
42 public static bool operator >(Vertex me, Vertex other)
43 {
44 return me.CompareTo(other) > 0;
45 }
46
47 public static bool operator <(Vertex me, Vertex other)
48 {
49 return me.CompareTo(other) < 0;
50 }
51
52
53}
54
55public class Simplex : IComparable<Simplex>
56{
57 public Vertex v1;
58 public Vertex v2;
59
60 public Simplex(Vertex _v1, Vertex _v2)
61 {
62 // Presort indices to make sorting (comparing) easier
63 if (_v1 > _v2)
64 {
65 v1 = _v1;
66 v2 = _v2;
67 }
68 else
69 {
70 v1 = _v2;
71 v2 = _v1;
72 }
73 }
74
75 public int CompareTo(Simplex other)
76 {
77 if (v1 > other.v1)
78 {
79 return 1;
80 }
81 if (v1 < other.v1)
82 {
83 return -1;
84 }
85
86 if (v2 > other.v2)
87 {
88 return 1;
89 }
90 if (v2 < other.v2)
91 {
92 return -1;
93 }
94
95 return 0;
96 }
97
98};
99
100public class Triangle
101{
102 public Vertex v1;
103 public Vertex v2;
104 public Vertex v3;
105
106 float radius_square;
107 float cx;
108 float cy;
109
110 public Triangle(Vertex _v1, Vertex _v2, Vertex _v3)
111 {
112 v1 = _v1;
113 v2 = _v2;
114 v3 = _v3;
115
116 CalcCircle();
117 }
118
119 public bool isInCircle(float x, float y)
120 {
121 float dx, dy;
122 float dd;
123
124 dx = x - this.cx;
125 dy = y - this.cy;
126
127 dd = dx * dx + dy * dy;
128 if (dd < this.radius_square)
129 return true;
130 else
131 return false;
132 }
133
134
135 void CalcCircle()
136 {
137 // Calculate the center and the radius of a circle given by three points p1, p2, p3
138 // It is assumed, that the triangles vertices are already set correctly
139 double p1x, p2x, p1y, p2y, p3x, p3y;
140
141 // Deviation of this routine:
142 // A circle has the general equation (M-p)^2=r^2, where M and p are vectors
143 // this gives us three equations f(p)=r^2, each for one point p1, p2, p3
144 // putting respectively two equations together gives two equations
145 // f(p1)=f(p2) and f(p1)=f(p3)
146 // bringing all constant terms to one side brings them to the form
147 // M*v1=c1 resp.M*v2=c2 where v1=(p1-p2) and v2=(p1-p3) (still vectors)
148 // and c1, c2 are scalars (Naming conventions like the variables below)
149 // Now using the equations that are formed by the components of the vectors
150 // and isolate Mx lets you make one equation that only holds My
151 // The rest is straight forward and eaasy :-)
152 //
153
154 /* helping variables for temporary results */
155 double c1, c2;
156 double v1x, v1y, v2x, v2y;
157
158 double z, n;
159
160 double rx, ry;
161
162 // Readout the three points, the triangle consists of
163 p1x = v1.point.X;
164 p1y = v1.point.Y;
165
166 p2x = v2.point.X;
167 p2y = v2.point.Y;
168
169 p3x = v3.point.X;
170 p3y = v3.point.Y;
171
172 /* calc helping values first */
173 c1 = (p1x * p1x + p1y * p1y - p2x * p2x - p2y * p2y) / 2;
174 c2 = (p1x * p1x + p1y * p1y - p3x * p3x - p3y * p3y) / 2;
175
176 v1x = p1x - p2x;
177 v1y = p1y - p2y;
178
179 v2x = p1x - p3x;
180 v2y = p1y - p3y;
181
182 z = (c1 * v2x - c2 * v1x);
183 n = (v1y * v2x - v2y * v1x);
184
185 if (n == 0.0) // This is no triangle, i.e there are (at least) two points at the same location
186 {
187 radius_square = 0.0f;
188 return;
189 }
190
191 this.cy = (float)(z / n);
192
193 if (v2x != 0.0)
194 {
195 this.cx = (float)((c2 - v2y * this.cy) / v2x);
196 }
197 else if (v1x != 0.0)
198 {
199 this.cx = (float)((c1 - v1y * this.cy) / v1x);
200 }
201 else
202 {
203 Debug.Assert(false, "Malformed triangle"); /* Both terms zero means nothing good */
204 }
205
206 rx = (p1x - this.cx);
207 ry = (p1y - this.cy);
208
209 this.radius_square = (float)(rx * rx + ry * ry);
210
211 }
212
213 public List<Simplex> GetSimplices()
214 {
215 List<Simplex> result = new List<Simplex>();
216 Simplex s1 = new Simplex(v1, v2);
217 Simplex s2 = new Simplex(v2, v3);
218 Simplex s3 = new Simplex(v3, v1);
219
220 result.Add(s1);
221 result.Add(s2);
222 result.Add(s3);
223
224 return result;
225 }
226
227 public override String ToString()
228 {
229
230 NumberFormatInfo nfi = new NumberFormatInfo();
231 nfi.CurrencyDecimalDigits = 2;
232 nfi.CurrencyDecimalSeparator = ".";
233
234 String s1 = "<" + v1.point.X.ToString(nfi) + "," + v1.point.Y.ToString(nfi) + "," + v1.point.Z.ToString(nfi) + ">";
235 String s2 = "<" + v2.point.X.ToString(nfi) + "," + v2.point.Y.ToString(nfi) + "," + v2.point.Z.ToString(nfi) + ">";
236 String s3 = "<" + v3.point.X.ToString(nfi) + "," + v3.point.Y.ToString(nfi) + "," + v3.point.Z.ToString(nfi) + ">";
237
238 return s1 + ";" + s2 + ";" + s3;
239
240 }
241
242 public PhysicsVector getNormal()
243 {
244 // Vertices
245
246 // Vectors for edges
247 PhysicsVector e1;
248 PhysicsVector e2;
249
250 e1 = new PhysicsVector(v1.point.X - v2.point.X, v1.point.Y - v2.point.Y, v1.point.Z - v2.point.Z);
251 e2 = new PhysicsVector(v1.point.X - v3.point.X, v1.point.Y - v3.point.Y, v1.point.Z - v3.point.Z);
252
253 // Cross product for normal
254 PhysicsVector n = new PhysicsVector();
255 float nx, ny, nz;
256 n.X = e1.Y * e2.Z - e1.Z * e2.Y;
257 n.Y = e1.Z * e2.X - e1.X * e2.Z;
258 n.Z = e1.X * e2.Y - e1.Y * e2.X;
259
260 // Length
261 float l = (float)Math.Sqrt(n.X * n.X + n.Y * n.Y + n.Z * n.Z);
262
263 // Normalized "normal"
264 n.X /= l;
265 n.Y /= l;
266 n.Z /= l;
267
268 return n;
269 }
270
271 public void invertNormal()
272 {
273 Vertex vt;
274 vt = v1;
275 v1 = v2;
276 v2 = vt;
277 }
278}
279