PhysicsVector.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. namespace OpenSim.Region.Physics.Manager
  29. {
  30. /*public class PhysicsVector
  31. {
  32. public float X;
  33. public float Y;
  34. public float Z;
  35. public Vector3()
  36. {
  37. }
  38. public Vector3(float x, float y, float z)
  39. {
  40. X = x;
  41. Y = y;
  42. Z = z;
  43. }
  44. public Vector3(Vector3 pv) : this(pv.X, pv.Y, pv.Z)
  45. {
  46. }
  47. public void setValues(float x, float y, float z)
  48. {
  49. X = x;
  50. Y = y;
  51. Z = z;
  52. }
  53. public static readonly PhysicsVector Zero = new PhysicsVector(0f, 0f, 0f);
  54. public override string ToString()
  55. {
  56. return "<" + X + "," + Y + "," + Z + ">";
  57. }
  58. /// <summary>
  59. /// These routines are the easiest way to store XYZ values in an Vector3 without requiring 3 calls.
  60. /// </summary>
  61. /// <returns></returns>
  62. public byte[] GetBytes()
  63. {
  64. byte[] byteArray = new byte[12];
  65. Buffer.BlockCopy(BitConverter.GetBytes(X), 0, byteArray, 0, 4);
  66. Buffer.BlockCopy(BitConverter.GetBytes(Y), 0, byteArray, 4, 4);
  67. Buffer.BlockCopy(BitConverter.GetBytes(Z), 0, byteArray, 8, 4);
  68. if (!BitConverter.IsLittleEndian)
  69. {
  70. Array.Reverse(byteArray, 0, 4);
  71. Array.Reverse(byteArray, 4, 4);
  72. Array.Reverse(byteArray, 8, 4);
  73. }
  74. return byteArray;
  75. }
  76. public void FromBytes(byte[] byteArray, int pos)
  77. {
  78. byte[] conversionBuffer = null;
  79. if (!BitConverter.IsLittleEndian)
  80. {
  81. // Big endian architecture
  82. if (conversionBuffer == null)
  83. conversionBuffer = new byte[12];
  84. Buffer.BlockCopy(byteArray, pos, conversionBuffer, 0, 12);
  85. Array.Reverse(conversionBuffer, 0, 4);
  86. Array.Reverse(conversionBuffer, 4, 4);
  87. Array.Reverse(conversionBuffer, 8, 4);
  88. X = BitConverter.ToSingle(conversionBuffer, 0);
  89. Y = BitConverter.ToSingle(conversionBuffer, 4);
  90. Z = BitConverter.ToSingle(conversionBuffer, 8);
  91. }
  92. else
  93. {
  94. // Little endian architecture
  95. X = BitConverter.ToSingle(byteArray, pos);
  96. Y = BitConverter.ToSingle(byteArray, pos + 4);
  97. Z = BitConverter.ToSingle(byteArray, pos + 8);
  98. }
  99. }
  100. // Operations
  101. public static PhysicsVector operator +(Vector3 a, Vector3 b)
  102. {
  103. return new PhysicsVector(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
  104. }
  105. public static PhysicsVector operator -(Vector3 a, Vector3 b)
  106. {
  107. return new PhysicsVector(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
  108. }
  109. public static PhysicsVector cross(Vector3 a, Vector3 b)
  110. {
  111. return new PhysicsVector(a.Y*b.Z - a.Z*b.Y, a.Z*b.X - a.X*b.Z, a.X*b.Y - a.Y*b.X);
  112. }
  113. public float length()
  114. {
  115. return (float) Math.Sqrt(X*X + Y*Y + Z*Z);
  116. }
  117. public static float GetDistanceTo(Vector3 a, Vector3 b)
  118. {
  119. float dx = a.X - b.X;
  120. float dy = a.Y - b.Y;
  121. float dz = a.Z - b.Z;
  122. return (float) Math.Sqrt(dx * dx + dy * dy + dz * dz);
  123. }
  124. public static PhysicsVector operator /(Vector3 v, float f)
  125. {
  126. return new PhysicsVector(v.X/f, v.Y/f, v.Z/f);
  127. }
  128. public static PhysicsVector operator *(Vector3 v, float f)
  129. {
  130. return new PhysicsVector(v.X*f, v.Y*f, v.Z*f);
  131. }
  132. public static PhysicsVector operator *(float f, Vector3 v)
  133. {
  134. return v*f;
  135. }
  136. public static bool isFinite(Vector3 v)
  137. {
  138. if (v == null)
  139. return false;
  140. if (Single.IsInfinity(v.X) || Single.IsNaN(v.X))
  141. return false;
  142. if (Single.IsInfinity(v.Y) || Single.IsNaN(v.Y))
  143. return false;
  144. if (Single.IsInfinity(v.Z) || Single.IsNaN(v.Z))
  145. return false;
  146. return true;
  147. }
  148. public virtual bool IsIdentical(Vector3 v, float tolerance)
  149. {
  150. PhysicsVector diff = this - v;
  151. float d = diff.length();
  152. if (d <= tolerance)
  153. return true;
  154. return false;
  155. }
  156. }*/
  157. }