vhacdVector.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  4. 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  5. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  6. 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
  7. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8. */
  9. #pragma once
  10. #ifndef VHACD_VECTOR_H
  11. #define VHACD_VECTOR_H
  12. #include <iostream>
  13. #include <math.h>
  14. namespace VHACD {
  15. //! Vector dim 3.
  16. template <typename T>
  17. class Vec3 {
  18. public:
  19. T& operator[](size_t i) { return m_data[i]; }
  20. const T& operator[](size_t i) const { return m_data[i]; }
  21. T& X();
  22. T& Y();
  23. T& Z();
  24. const T& X() const;
  25. const T& Y() const;
  26. const T& Z() const;
  27. void Normalize();
  28. T GetNorm() const;
  29. void operator=(const Vec3& rhs);
  30. void operator+=(const Vec3& rhs);
  31. void operator-=(const Vec3& rhs);
  32. void operator-=(T a);
  33. void operator+=(T a);
  34. void operator/=(T a);
  35. void operator*=(T a);
  36. Vec3 operator^(const Vec3& rhs) const;
  37. T operator*(const Vec3& rhs) const;
  38. Vec3 operator+(const Vec3& rhs) const;
  39. Vec3 operator-(const Vec3& rhs) const;
  40. Vec3 operator-() const;
  41. Vec3 operator*(T rhs) const;
  42. Vec3 operator/(T rhs) const;
  43. bool operator<(const Vec3& rhs) const;
  44. bool operator>(const Vec3& rhs) const;
  45. Vec3();
  46. Vec3(T a);
  47. Vec3(T x, T y, T z);
  48. Vec3(const Vec3& rhs);
  49. /*virtual*/ ~Vec3(void);
  50. // Compute the center of this bounding box and return the diagonal length
  51. T GetCenter(const Vec3 &bmin, const Vec3 &bmax)
  52. {
  53. X() = (bmin.X() + bmax.X())*0.5;
  54. Y() = (bmin.Y() + bmax.Y())*0.5;
  55. Z() = (bmin.Z() + bmax.Z())*0.5;
  56. T dx = bmax.X() - bmin.X();
  57. T dy = bmax.Y() - bmin.Y();
  58. T dz = bmax.Z() - bmin.Z();
  59. T diagonal = T(sqrt(dx*dx + dy*dy + dz*dz));
  60. return diagonal;
  61. }
  62. // Update the min/max values relative to this point
  63. void UpdateMinMax(Vec3 &bmin,Vec3 &bmax) const
  64. {
  65. if (X() < bmin.X())
  66. {
  67. bmin.X() = X();
  68. }
  69. if (Y() < bmin.Y())
  70. {
  71. bmin.Y() = Y();
  72. }
  73. if (Z() < bmin.Z())
  74. {
  75. bmin.Z() = Z();
  76. }
  77. if (X() > bmax.X())
  78. {
  79. bmax.X() = X();
  80. }
  81. if (X() > bmax.X())
  82. {
  83. bmax.X() = X();
  84. }
  85. if (Y() > bmax.Y())
  86. {
  87. bmax.Y() = Y();
  88. }
  89. if (Z() > bmax.Z())
  90. {
  91. bmax.Z() = Z();
  92. }
  93. }
  94. // Returns the squared distance between these two points
  95. T GetDistanceSquared(const Vec3 &p) const
  96. {
  97. T dx = X() - p.X();
  98. T dy = Y() - p.Y();
  99. T dz = Z() - p.Z();
  100. return dx*dx + dy*dy + dz*dz;
  101. }
  102. T GetDistance(const Vec3 &p) const
  103. {
  104. return sqrt(GetDistanceSquared(p));
  105. }
  106. // Returns the raw vector data as a pointer
  107. T* GetData(void)
  108. {
  109. return m_data;
  110. }
  111. private:
  112. T m_data[3];
  113. };
  114. //! Vector dim 2.
  115. template <typename T>
  116. class Vec2 {
  117. public:
  118. T& operator[](size_t i) { return m_data[i]; }
  119. const T& operator[](size_t i) const { return m_data[i]; }
  120. T& X();
  121. T& Y();
  122. const T& X() const;
  123. const T& Y() const;
  124. void Normalize();
  125. T GetNorm() const;
  126. void operator=(const Vec2& rhs);
  127. void operator+=(const Vec2& rhs);
  128. void operator-=(const Vec2& rhs);
  129. void operator-=(T a);
  130. void operator+=(T a);
  131. void operator/=(T a);
  132. void operator*=(T a);
  133. T operator^(const Vec2& rhs) const;
  134. T operator*(const Vec2& rhs) const;
  135. Vec2 operator+(const Vec2& rhs) const;
  136. Vec2 operator-(const Vec2& rhs) const;
  137. Vec2 operator-() const;
  138. Vec2 operator*(T rhs) const;
  139. Vec2 operator/(T rhs) const;
  140. Vec2();
  141. Vec2(T a);
  142. Vec2(T x, T y);
  143. Vec2(const Vec2& rhs);
  144. /*virtual*/ ~Vec2(void);
  145. private:
  146. T m_data[2];
  147. };
  148. template <typename T>
  149. const bool Colinear(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c);
  150. template <typename T>
  151. const T ComputeVolume4(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c, const Vec3<T>& d);
  152. }
  153. #include "vhacdVector.inl" // template implementation
  154. #endif