HelperTypes.cs 11 KB

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