diff options
Diffstat (limited to 'OpenSim/Region/Physics/Meshing/HelperTypes.cs')
-rw-r--r-- | OpenSim/Region/Physics/Meshing/HelperTypes.cs | 306 |
1 files changed, 306 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/Meshing/HelperTypes.cs b/OpenSim/Region/Physics/Meshing/HelperTypes.cs new file mode 100644 index 0000000..9e75826 --- /dev/null +++ b/OpenSim/Region/Physics/Meshing/HelperTypes.cs | |||
@@ -0,0 +1,306 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | using System; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Diagnostics; | ||
32 | using System.Globalization; | ||
33 | using OpenSim.Framework.Console; | ||
34 | using OpenSim.Region.Physics.Manager; | ||
35 | |||
36 | using OpenSim.Region.Physics.Meshing; | ||
37 | |||
38 | public class Vertex : PhysicsVector, IComparable<Vertex> | ||
39 | { | ||
40 | public Vertex(float x, float y, float z) | ||
41 | : base(x, y, z) | ||
42 | { | ||
43 | } | ||
44 | |||
45 | public Vertex(PhysicsVector v) | ||
46 | : base(v.X, v.Y, v.Z) | ||
47 | { | ||
48 | } | ||
49 | |||
50 | public Vertex Clone() | ||
51 | { | ||
52 | return new Vertex(X, Y, Z); | ||
53 | } | ||
54 | |||
55 | public static Vertex FromAngle(double angle) | ||
56 | { | ||
57 | return new Vertex((float)Math.Cos(angle), (float)Math.Sin(angle), 0.0f); | ||
58 | } | ||
59 | |||
60 | |||
61 | public virtual bool Equals(Vertex v, float tolerance) | ||
62 | { | ||
63 | PhysicsVector diff = this - v; | ||
64 | float d = diff.length(); | ||
65 | if (d < tolerance) | ||
66 | return true; | ||
67 | |||
68 | return false; | ||
69 | } | ||
70 | |||
71 | |||
72 | public int CompareTo(Vertex other) | ||
73 | { | ||
74 | if (X < other.X) | ||
75 | return -1; | ||
76 | |||
77 | if (X > other.X) | ||
78 | return 1; | ||
79 | |||
80 | if (Y < other.Y) | ||
81 | return -1; | ||
82 | |||
83 | if (Y > other.Y) | ||
84 | return 1; | ||
85 | |||
86 | if (Z < other.Z) | ||
87 | return -1; | ||
88 | |||
89 | if (Z > other.Z) | ||
90 | return 1; | ||
91 | |||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | public static bool operator >(Vertex me, Vertex other) | ||
96 | { | ||
97 | return me.CompareTo(other) > 0; | ||
98 | } | ||
99 | |||
100 | public static bool operator <(Vertex me, Vertex other) | ||
101 | { | ||
102 | return me.CompareTo(other) < 0; | ||
103 | } | ||
104 | public String ToRaw() | ||
105 | { | ||
106 | // Why this stuff with the number formatter? | ||
107 | // Well, the raw format uses the english/US notation of numbers | ||
108 | // where the "," separates groups of 1000 while the "." marks the border between 1 and 10E-1. | ||
109 | // The german notation uses these characters exactly vice versa! | ||
110 | // The Float.ToString() routine is a localized one, giving different results depending on the country | ||
111 | // settings your machine works with. Unusable for a machine readable file format :-( | ||
112 | NumberFormatInfo nfi = new NumberFormatInfo(); | ||
113 | nfi.NumberDecimalSeparator = "."; | ||
114 | nfi.NumberDecimalDigits = 3; | ||
115 | |||
116 | String s1 = X.ToString("N2", nfi) + " " + Y.ToString("N2", nfi) + " " + Z.ToString("N2", nfi); | ||
117 | |||
118 | return s1; | ||
119 | } | ||
120 | |||
121 | } | ||
122 | |||
123 | public class Triangle | ||
124 | { | ||
125 | public Vertex v1; | ||
126 | public Vertex v2; | ||
127 | public Vertex v3; | ||
128 | |||
129 | private float radius_square; | ||
130 | private float cx; | ||
131 | private float cy; | ||
132 | |||
133 | public Triangle(Vertex _v1, Vertex _v2, Vertex _v3) | ||
134 | { | ||
135 | v1 = _v1; | ||
136 | v2 = _v2; | ||
137 | v3 = _v3; | ||
138 | |||
139 | CalcCircle(); | ||
140 | } | ||
141 | |||
142 | public bool isInCircle(float x, float y) | ||
143 | { | ||
144 | float dx, dy; | ||
145 | float dd; | ||
146 | |||
147 | dx = x - cx; | ||
148 | dy = y - cy; | ||
149 | |||
150 | dd = dx*dx + dy*dy; | ||
151 | if (dd < radius_square) | ||
152 | return true; | ||
153 | else | ||
154 | return false; | ||
155 | } | ||
156 | |||
157 | public bool isDegraded() | ||
158 | { | ||
159 | // This means, the vertices of this triangle are somewhat strange. | ||
160 | // They either line up or at least two of them are identical | ||
161 | return (radius_square == 0.0); | ||
162 | } | ||
163 | |||
164 | private void CalcCircle() | ||
165 | { | ||
166 | // Calculate the center and the radius of a circle given by three points p1, p2, p3 | ||
167 | // It is assumed, that the triangles vertices are already set correctly | ||
168 | double p1x, p2x, p1y, p2y, p3x, p3y; | ||
169 | |||
170 | // Deviation of this routine: | ||
171 | // A circle has the general equation (M-p)^2=r^2, where M and p are vectors | ||
172 | // this gives us three equations f(p)=r^2, each for one point p1, p2, p3 | ||
173 | // putting respectively two equations together gives two equations | ||
174 | // f(p1)=f(p2) and f(p1)=f(p3) | ||
175 | // bringing all constant terms to one side brings them to the form | ||
176 | // M*v1=c1 resp.M*v2=c2 where v1=(p1-p2) and v2=(p1-p3) (still vectors) | ||
177 | // and c1, c2 are scalars (Naming conventions like the variables below) | ||
178 | // Now using the equations that are formed by the components of the vectors | ||
179 | // and isolate Mx lets you make one equation that only holds My | ||
180 | // The rest is straight forward and eaasy :-) | ||
181 | // | ||
182 | |||
183 | /* helping variables for temporary results */ | ||
184 | double c1, c2; | ||
185 | double v1x, v1y, v2x, v2y; | ||
186 | |||
187 | double z, n; | ||
188 | |||
189 | double rx, ry; | ||
190 | |||
191 | // Readout the three points, the triangle consists of | ||
192 | p1x = v1.X; | ||
193 | p1y = v1.Y; | ||
194 | |||
195 | p2x = v2.X; | ||
196 | p2y = v2.Y; | ||
197 | |||
198 | p3x = v3.X; | ||
199 | p3y = v3.Y; | ||
200 | |||
201 | /* calc helping values first */ | ||
202 | c1 = (p1x*p1x + p1y*p1y - p2x*p2x - p2y*p2y)/2; | ||
203 | c2 = (p1x*p1x + p1y*p1y - p3x*p3x - p3y*p3y)/2; | ||
204 | |||
205 | v1x = p1x - p2x; | ||
206 | v1y = p1y - p2y; | ||
207 | |||
208 | v2x = p1x - p3x; | ||
209 | v2y = p1y - p3y; | ||
210 | |||
211 | z = (c1*v2x - c2*v1x); | ||
212 | n = (v1y*v2x - v2y*v1x); | ||
213 | |||
214 | if (n == 0.0) // This is no triangle, i.e there are (at least) two points at the same location | ||
215 | { | ||
216 | radius_square = 0.0f; | ||
217 | return; | ||
218 | } | ||
219 | |||
220 | cy = (float) (z/n); | ||
221 | |||
222 | if (v2x != 0.0) | ||
223 | { | ||
224 | cx = (float) ((c2 - v2y*cy)/v2x); | ||
225 | } | ||
226 | else if (v1x != 0.0) | ||
227 | { | ||
228 | cx = (float) ((c1 - v1y*cy)/v1x); | ||
229 | } | ||
230 | else | ||
231 | { | ||
232 | Debug.Assert(false, "Malformed triangle"); /* Both terms zero means nothing good */ | ||
233 | } | ||
234 | |||
235 | rx = (p1x - cx); | ||
236 | ry = (p1y - cy); | ||
237 | |||
238 | radius_square = (float) (rx*rx + ry*ry); | ||
239 | } | ||
240 | |||
241 | public List<Simplex> GetSimplices() | ||
242 | { | ||
243 | List<Simplex> result = new List<Simplex>(); | ||
244 | Simplex s1 = new Simplex(v1, v2); | ||
245 | Simplex s2 = new Simplex(v2, v3); | ||
246 | Simplex s3 = new Simplex(v3, v1); | ||
247 | |||
248 | result.Add(s1); | ||
249 | result.Add(s2); | ||
250 | result.Add(s3); | ||
251 | |||
252 | return result; | ||
253 | } | ||
254 | |||
255 | public override String ToString() | ||
256 | { | ||
257 | NumberFormatInfo nfi = new NumberFormatInfo(); | ||
258 | nfi.CurrencyDecimalDigits = 2; | ||
259 | nfi.CurrencyDecimalSeparator = "."; | ||
260 | |||
261 | String s1 = "<" + v1.X.ToString(nfi) + "," + v1.Y.ToString(nfi) + "," + v1.Z.ToString(nfi) + ">"; | ||
262 | String s2 = "<" + v2.X.ToString(nfi) + "," + v2.Y.ToString(nfi) + "," + v2.Z.ToString(nfi) + ">"; | ||
263 | String s3 = "<" + v3.X.ToString(nfi) + "," + v3.Y.ToString(nfi) + "," + v3.Z.ToString(nfi) + ">"; | ||
264 | |||
265 | return s1 + ";" + s2 + ";" + s3; | ||
266 | } | ||
267 | |||
268 | public PhysicsVector getNormal() | ||
269 | { | ||
270 | // Vertices | ||
271 | |||
272 | // Vectors for edges | ||
273 | PhysicsVector e1; | ||
274 | PhysicsVector e2; | ||
275 | |||
276 | e1 = new PhysicsVector(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z); | ||
277 | e2 = new PhysicsVector(v1.X - v3.X, v1.Y - v3.Y, v1.Z - v3.Z); | ||
278 | |||
279 | // Cross product for normal | ||
280 | PhysicsVector n = PhysicsVector.cross(e1, e2); | ||
281 | |||
282 | // Length | ||
283 | float l = n.length(); | ||
284 | |||
285 | // Normalized "normal" | ||
286 | n = n / l; | ||
287 | |||
288 | return n; | ||
289 | } | ||
290 | |||
291 | public void invertNormal() | ||
292 | { | ||
293 | Vertex vt; | ||
294 | vt = v1; | ||
295 | v1 = v2; | ||
296 | v2 = vt; | ||
297 | } | ||
298 | |||
299 | // Dumps a triangle in the "raw faces" format, blender can import. This is for visualisation and | ||
300 | // debugging purposes | ||
301 | public String ToStringRaw() | ||
302 | { | ||
303 | String output = v1.ToRaw() + " " + v2.ToRaw() + " " +v3.ToRaw(); | ||
304 | return output; | ||
305 | } | ||
306 | } \ No newline at end of file | ||