HelperTypes.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 OpenSimulator 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. using System;
  28. using System.Collections.Generic;
  29. using System.Diagnostics;
  30. using System.Globalization;
  31. using OpenMetaverse;
  32. using OpenSim.Region.Physics.Manager;
  33. using OpenSim.Region.Physics.Meshing;
  34. public class Vertex : IComparable<Vertex>
  35. {
  36. Vector3 vector;
  37. public float X
  38. {
  39. get { return vector.X; }
  40. set { vector.X = value; }
  41. }
  42. public float Y
  43. {
  44. get { return vector.Y; }
  45. set { vector.Y = value; }
  46. }
  47. public float Z
  48. {
  49. get { return vector.Z; }
  50. set { vector.Z = value; }
  51. }
  52. public Vertex(float x, float y, float z)
  53. {
  54. vector.X = x;
  55. vector.Y = y;
  56. vector.Z = z;
  57. }
  58. public Vertex normalize()
  59. {
  60. float tlength = vector.Length();
  61. if (tlength != 0f)
  62. {
  63. float mul = 1.0f / tlength;
  64. return new Vertex(vector.X * mul, vector.Y * mul, vector.Z * mul);
  65. }
  66. else
  67. {
  68. return new Vertex(0f, 0f, 0f);
  69. }
  70. }
  71. public Vertex cross(Vertex v)
  72. {
  73. return new Vertex(vector.Y * v.Z - vector.Z * v.Y, vector.Z * v.X - vector.X * v.Z, vector.X * v.Y - vector.Y * v.X);
  74. }
  75. // disable warning: mono compiler moans about overloading
  76. // operators hiding base operator but should not according to C#
  77. // language spec
  78. #pragma warning disable 0108
  79. public static Vertex operator *(Vertex v, Quaternion q)
  80. {
  81. // From http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/transforms/
  82. Vertex v2 = new Vertex(0f, 0f, 0f);
  83. v2.X = q.W * q.W * v.X +
  84. 2f * q.Y * q.W * v.Z -
  85. 2f * q.Z * q.W * v.Y +
  86. q.X * q.X * v.X +
  87. 2f * q.Y * q.X * v.Y +
  88. 2f * q.Z * q.X * v.Z -
  89. q.Z * q.Z * v.X -
  90. q.Y * q.Y * v.X;
  91. v2.Y =
  92. 2f * q.X * q.Y * v.X +
  93. q.Y * q.Y * v.Y +
  94. 2f * q.Z * q.Y * v.Z +
  95. 2f * q.W * q.Z * v.X -
  96. q.Z * q.Z * v.Y +
  97. q.W * q.W * v.Y -
  98. 2f * q.X * q.W * v.Z -
  99. q.X * q.X * v.Y;
  100. v2.Z =
  101. 2f * q.X * q.Z * v.X +
  102. 2f * q.Y * q.Z * v.Y +
  103. q.Z * q.Z * v.Z -
  104. 2f * q.W * q.Y * v.X -
  105. q.Y * q.Y * v.Z +
  106. 2f * q.W * q.X * v.Y -
  107. q.X * q.X * v.Z +
  108. q.W * q.W * v.Z;
  109. return v2;
  110. }
  111. public static Vertex operator +(Vertex v1, Vertex v2)
  112. {
  113. return new Vertex(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
  114. }
  115. public static Vertex operator -(Vertex v1, Vertex v2)
  116. {
  117. return new Vertex(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
  118. }
  119. public static Vertex operator *(Vertex v1, Vertex v2)
  120. {
  121. return new Vertex(v1.X * v2.X, v1.Y * v2.Y, v1.Z * v2.Z);
  122. }
  123. public static Vertex operator +(Vertex v1, float am)
  124. {
  125. v1.X += am;
  126. v1.Y += am;
  127. v1.Z += am;
  128. return v1;
  129. }
  130. public static Vertex operator -(Vertex v1, float am)
  131. {
  132. v1.X -= am;
  133. v1.Y -= am;
  134. v1.Z -= am;
  135. return v1;
  136. }
  137. public static Vertex operator *(Vertex v1, float am)
  138. {
  139. v1.X *= am;
  140. v1.Y *= am;
  141. v1.Z *= am;
  142. return v1;
  143. }
  144. public static Vertex operator /(Vertex v1, float am)
  145. {
  146. if (am == 0f)
  147. {
  148. return new Vertex(0f,0f,0f);
  149. }
  150. float mul = 1.0f / am;
  151. v1.X *= mul;
  152. v1.Y *= mul;
  153. v1.Z *= mul;
  154. return v1;
  155. }
  156. #pragma warning restore 0108
  157. public float dot(Vertex v)
  158. {
  159. return X * v.X + Y * v.Y + Z * v.Z;
  160. }
  161. public Vertex(Vector3 v)
  162. {
  163. vector = v;
  164. }
  165. public Vertex Clone()
  166. {
  167. return new Vertex(X, Y, Z);
  168. }
  169. public static Vertex FromAngle(double angle)
  170. {
  171. return new Vertex((float) Math.Cos(angle), (float) Math.Sin(angle), 0.0f);
  172. }
  173. public float Length()
  174. {
  175. return vector.Length();
  176. }
  177. public virtual bool Equals(Vertex v, float tolerance)
  178. {
  179. Vertex diff = this - v;
  180. float d = diff.Length();
  181. if (d < tolerance)
  182. return true;
  183. return false;
  184. }
  185. public int CompareTo(Vertex other)
  186. {
  187. if (X < other.X)
  188. return -1;
  189. if (X > other.X)
  190. return 1;
  191. if (Y < other.Y)
  192. return -1;
  193. if (Y > other.Y)
  194. return 1;
  195. if (Z < other.Z)
  196. return -1;
  197. if (Z > other.Z)
  198. return 1;
  199. return 0;
  200. }
  201. public static bool operator >(Vertex me, Vertex other)
  202. {
  203. return me.CompareTo(other) > 0;
  204. }
  205. public static bool operator <(Vertex me, Vertex other)
  206. {
  207. return me.CompareTo(other) < 0;
  208. }
  209. public String ToRaw()
  210. {
  211. // Why this stuff with the number formatter?
  212. // Well, the raw format uses the english/US notation of numbers
  213. // where the "," separates groups of 1000 while the "." marks the border between 1 and 10E-1.
  214. // The german notation uses these characters exactly vice versa!
  215. // The Float.ToString() routine is a localized one, giving different results depending on the country
  216. // settings your machine works with. Unusable for a machine readable file format :-(
  217. NumberFormatInfo nfi = new NumberFormatInfo();
  218. nfi.NumberDecimalSeparator = ".";
  219. nfi.NumberDecimalDigits = 3;
  220. String s1 = X.ToString("N2", nfi) + " " + Y.ToString("N2", nfi) + " " + Z.ToString("N2", nfi);
  221. return s1;
  222. }
  223. }
  224. public class Triangle
  225. {
  226. public Vertex v1;
  227. public Vertex v2;
  228. public Vertex v3;
  229. private float radius_square;
  230. private float cx;
  231. private float cy;
  232. public Triangle(Vertex _v1, Vertex _v2, Vertex _v3)
  233. {
  234. v1 = _v1;
  235. v2 = _v2;
  236. v3 = _v3;
  237. CalcCircle();
  238. }
  239. public bool isInCircle(float x, float y)
  240. {
  241. float dx, dy;
  242. float dd;
  243. dx = x - cx;
  244. dy = y - cy;
  245. dd = dx*dx + dy*dy;
  246. if (dd < radius_square)
  247. return true;
  248. else
  249. return false;
  250. }
  251. public bool isDegraded()
  252. {
  253. // This means, the vertices of this triangle are somewhat strange.
  254. // They either line up or at least two of them are identical
  255. return (radius_square == 0.0);
  256. }
  257. private void CalcCircle()
  258. {
  259. // Calculate the center and the radius of a circle given by three points p1, p2, p3
  260. // It is assumed, that the triangles vertices are already set correctly
  261. double p1x, p2x, p1y, p2y, p3x, p3y;
  262. // Deviation of this routine:
  263. // A circle has the general equation (M-p)^2=r^2, where M and p are vectors
  264. // this gives us three equations f(p)=r^2, each for one point p1, p2, p3
  265. // putting respectively two equations together gives two equations
  266. // f(p1)=f(p2) and f(p1)=f(p3)
  267. // bringing all constant terms to one side brings them to the form
  268. // M*v1=c1 resp.M*v2=c2 where v1=(p1-p2) and v2=(p1-p3) (still vectors)
  269. // and c1, c2 are scalars (Naming conventions like the variables below)
  270. // Now using the equations that are formed by the components of the vectors
  271. // and isolate Mx lets you make one equation that only holds My
  272. // The rest is straight forward and eaasy :-)
  273. //
  274. /* helping variables for temporary results */
  275. double c1, c2;
  276. double v1x, v1y, v2x, v2y;
  277. double z, n;
  278. double rx, ry;
  279. // Readout the three points, the triangle consists of
  280. p1x = v1.X;
  281. p1y = v1.Y;
  282. p2x = v2.X;
  283. p2y = v2.Y;
  284. p3x = v3.X;
  285. p3y = v3.Y;
  286. /* calc helping values first */
  287. c1 = (p1x*p1x + p1y*p1y - p2x*p2x - p2y*p2y)/2;
  288. c2 = (p1x*p1x + p1y*p1y - p3x*p3x - p3y*p3y)/2;
  289. v1x = p1x - p2x;
  290. v1y = p1y - p2y;
  291. v2x = p1x - p3x;
  292. v2y = p1y - p3y;
  293. z = (c1*v2x - c2*v1x);
  294. n = (v1y*v2x - v2y*v1x);
  295. if (n == 0.0) // This is no triangle, i.e there are (at least) two points at the same location
  296. {
  297. radius_square = 0.0f;
  298. return;
  299. }
  300. cy = (float) (z/n);
  301. if (v2x != 0.0)
  302. {
  303. cx = (float) ((c2 - v2y*cy)/v2x);
  304. }
  305. else if (v1x != 0.0)
  306. {
  307. cx = (float) ((c1 - v1y*cy)/v1x);
  308. }
  309. else
  310. {
  311. Debug.Assert(false, "Malformed triangle"); /* Both terms zero means nothing good */
  312. }
  313. rx = (p1x - cx);
  314. ry = (p1y - cy);
  315. radius_square = (float) (rx*rx + ry*ry);
  316. }
  317. public override String ToString()
  318. {
  319. NumberFormatInfo nfi = new NumberFormatInfo();
  320. nfi.CurrencyDecimalDigits = 2;
  321. nfi.CurrencyDecimalSeparator = ".";
  322. String s1 = "<" + v1.X.ToString(nfi) + "," + v1.Y.ToString(nfi) + "," + v1.Z.ToString(nfi) + ">";
  323. String s2 = "<" + v2.X.ToString(nfi) + "," + v2.Y.ToString(nfi) + "," + v2.Z.ToString(nfi) + ">";
  324. String s3 = "<" + v3.X.ToString(nfi) + "," + v3.Y.ToString(nfi) + "," + v3.Z.ToString(nfi) + ">";
  325. return s1 + ";" + s2 + ";" + s3;
  326. }
  327. public Vector3 getNormal()
  328. {
  329. // Vertices
  330. // Vectors for edges
  331. Vector3 e1;
  332. Vector3 e2;
  333. e1 = new Vector3(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
  334. e2 = new Vector3(v1.X - v3.X, v1.Y - v3.Y, v1.Z - v3.Z);
  335. // Cross product for normal
  336. Vector3 n = Vector3.Cross(e1, e2);
  337. // Length
  338. float l = n.Length();
  339. // Normalized "normal"
  340. n = n/l;
  341. return n;
  342. }
  343. public void invertNormal()
  344. {
  345. Vertex vt;
  346. vt = v1;
  347. v1 = v2;
  348. v2 = vt;
  349. }
  350. // Dumps a triangle in the "raw faces" format, blender can import. This is for visualisation and
  351. // debugging purposes
  352. public String ToStringRaw()
  353. {
  354. String output = v1.ToRaw() + " " + v2.ToRaw() + " " + v3.ToRaw();
  355. return output;
  356. }
  357. }