float4.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 Intel Corporation.
  4. * All rights reserved.
  5. *
  6. * Based on the convexdecomposition library from
  7. * <http://codesuppository.googlecode.com> by John W. Ratcliff and Stan Melax.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. using System;
  28. namespace OpenSim.Region.Physics.ConvexDecompositionDotNet
  29. {
  30. public class float4
  31. {
  32. public float x;
  33. public float y;
  34. public float z;
  35. public float w;
  36. public float4()
  37. {
  38. x = 0;
  39. y = 0;
  40. z = 0;
  41. w = 0;
  42. }
  43. public float4(float _x, float _y, float _z, float _w)
  44. {
  45. x = _x;
  46. y = _y;
  47. z = _z;
  48. w = _w;
  49. }
  50. public float4(float3 v, float _w)
  51. {
  52. x = v.x;
  53. y = v.y;
  54. z = v.z;
  55. w = _w;
  56. }
  57. public float4(float4 f)
  58. {
  59. x = f.x;
  60. y = f.y;
  61. z = f.z;
  62. w = f.w;
  63. }
  64. public float this[int i]
  65. {
  66. get
  67. {
  68. switch (i)
  69. {
  70. case 0: return x;
  71. case 1: return y;
  72. case 2: return z;
  73. case 3: return w;
  74. }
  75. throw new ArgumentOutOfRangeException();
  76. }
  77. }
  78. public float3 xyz()
  79. {
  80. return new float3(x, y, z);
  81. }
  82. public void setxyz(float3 xyz)
  83. {
  84. x = xyz.x;
  85. y = xyz.y;
  86. z = xyz.z;
  87. }
  88. public override int GetHashCode()
  89. {
  90. return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode() ^ w.GetHashCode();
  91. }
  92. public override bool Equals(object obj)
  93. {
  94. float4 f = obj as float4;
  95. if (f == null)
  96. return false;
  97. return this == f;
  98. }
  99. public static float4 Homogenize(float3 v3)
  100. {
  101. return Homogenize(v3, 1.0f);
  102. }
  103. //C++ TO C# CONVERTER NOTE: C# does not allow default values for parameters. Overloaded methods are inserted above.
  104. //ORIGINAL LINE: float4 Homogenize(const float3 &v3, const float &w =1.0f)
  105. public static float4 Homogenize(float3 v3, float w)
  106. {
  107. return new float4(v3.x, v3.y, v3.z, w);
  108. }
  109. public static float4 cmul(float4 a, float4 b)
  110. {
  111. return new float4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
  112. }
  113. public static float4 operator +(float4 a, float4 b)
  114. {
  115. return new float4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
  116. }
  117. public static float4 operator -(float4 a, float4 b)
  118. {
  119. return new float4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
  120. }
  121. public static float4 operator *(float4 v, float4x4 m)
  122. {
  123. return v.x * m.x + v.y * m.y + v.z * m.z + v.w * m.w; // yes this actually works
  124. }
  125. public static bool operator ==(float4 a, float4 b)
  126. {
  127. // If both are null, or both are same instance, return true.
  128. if (System.Object.ReferenceEquals(a, b))
  129. return true;
  130. // If one is null, but not both, return false.
  131. if (((object)a == null) || ((object)b == null))
  132. return false;
  133. return (a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w);
  134. }
  135. public static bool operator !=(float4 a, float4 b)
  136. {
  137. return !(a == b);
  138. }
  139. public static float4 operator *(float4 v, float s)
  140. {
  141. return new float4(v.x * s, v.y * s, v.z * s, v.w * s);
  142. }
  143. public static float4 operator *(float s, float4 v)
  144. {
  145. return new float4(v.x * s, v.y * s, v.z * s, v.w * s);
  146. }
  147. }
  148. }