HelperTypes.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.PhysicsModules.SharedBase;
  33. using OpenSim.Region.PhysicsModule.ubODEMeshing;
  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 = 6;
  220. String s1 = X.ToString(nfi) + " " + Y.ToString(nfi) + " " + Z.ToString(nfi);
  221. return s1;
  222. }
  223. }
  224. public class Triangle
  225. {
  226. public Vertex v1;
  227. public Vertex v2;
  228. public Vertex v3;
  229. public Triangle(Vertex _v1, Vertex _v2, Vertex _v3)
  230. {
  231. v1 = _v1;
  232. v2 = _v2;
  233. v3 = _v3;
  234. }
  235. public Triangle(float _v1x,float _v1y,float _v1z,
  236. float _v2x,float _v2y,float _v2z,
  237. float _v3x,float _v3y,float _v3z)
  238. {
  239. v1 = new Vertex(_v1x, _v1y, _v1z);
  240. v2 = new Vertex(_v2x, _v2y, _v2z);
  241. v3 = new Vertex(_v3x, _v3y, _v3z);
  242. }
  243. public override String ToString()
  244. {
  245. NumberFormatInfo nfi = new NumberFormatInfo();
  246. nfi.CurrencyDecimalDigits = 2;
  247. nfi.CurrencyDecimalSeparator = ".";
  248. String s1 = "<" + v1.X.ToString(nfi) + "," + v1.Y.ToString(nfi) + "," + v1.Z.ToString(nfi) + ">";
  249. String s2 = "<" + v2.X.ToString(nfi) + "," + v2.Y.ToString(nfi) + "," + v2.Z.ToString(nfi) + ">";
  250. String s3 = "<" + v3.X.ToString(nfi) + "," + v3.Y.ToString(nfi) + "," + v3.Z.ToString(nfi) + ">";
  251. return s1 + ";" + s2 + ";" + s3;
  252. }
  253. public Vector3 getNormal()
  254. {
  255. // Vertices
  256. // Vectors for edges
  257. Vector3 e1;
  258. Vector3 e2;
  259. e1 = new Vector3(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
  260. e2 = new Vector3(v1.X - v3.X, v1.Y - v3.Y, v1.Z - v3.Z);
  261. // Cross product for normal
  262. Vector3 n = Vector3.Cross(e1, e2);
  263. // Length
  264. float l = n.Length();
  265. // Normalized "normal"
  266. n = n/l;
  267. return n;
  268. }
  269. public void invertNormal()
  270. {
  271. Vertex vt;
  272. vt = v1;
  273. v1 = v2;
  274. v2 = vt;
  275. }
  276. // Dumps a triangle in the "raw faces" format, blender can import. This is for visualisation and
  277. // debugging purposes
  278. public String ToStringRaw()
  279. {
  280. String output = v1.ToRaw() + " " + v2.ToRaw() + " " + v3.ToRaw();
  281. return output;
  282. }
  283. }