Vec3.inl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. Original file from GamePlay3D: http://gameplay3d.org
  13. This file was modified to fit the cocos2d-x project
  14. */
  15. #include "math/Vec3.h"
  16. #include "math/Mat4.h"
  17. #include <float.h>
  18. NS_CC_MATH_BEGIN
  19. inline bool Vec3::isZero() const
  20. {
  21. return x == 0.0f && y == 0.0f && z == 0.0f;
  22. }
  23. inline bool Vec3::isOne() const
  24. {
  25. return x == 1.0f && y == 1.0f && z == 1.0f;
  26. }
  27. inline void Vec3::add(const Vec3& v)
  28. {
  29. x += v.x;
  30. y += v.y;
  31. z += v.z;
  32. }
  33. inline void Vec3::add(float xx, float yy, float zz)
  34. {
  35. x += xx;
  36. y += yy;
  37. z += zz;
  38. }
  39. inline float Vec3::length() const
  40. {
  41. return std::sqrt(x * x + y * y + z * z);
  42. }
  43. inline float Vec3::lengthSquared() const
  44. {
  45. return (x * x + y * y + z * z);
  46. }
  47. inline void Vec3::negate()
  48. {
  49. x = -x;
  50. y = -y;
  51. z = -z;
  52. }
  53. inline void Vec3::scale(float scalar)
  54. {
  55. x *= scalar;
  56. y *= scalar;
  57. z *= scalar;
  58. }
  59. inline Vec3 Vec3::lerp(const Vec3 &target, float alpha) const
  60. {
  61. return *this * (1.f - alpha) + target * alpha;
  62. }
  63. inline void Vec3::set(float xx, float yy, float zz)
  64. {
  65. this->x = xx;
  66. this->y = yy;
  67. this->z = zz;
  68. }
  69. inline void Vec3::set(const float* array)
  70. {
  71. GP_ASSERT(array);
  72. x = array[0];
  73. y = array[1];
  74. z = array[2];
  75. }
  76. inline void Vec3::set(const Vec3& v)
  77. {
  78. this->x = v.x;
  79. this->y = v.y;
  80. this->z = v.z;
  81. }
  82. inline void Vec3::set(const Vec3& p1, const Vec3& p2)
  83. {
  84. x = p2.x - p1.x;
  85. y = p2.y - p1.y;
  86. z = p2.z - p1.z;
  87. }
  88. inline void Vec3::setZero()
  89. {
  90. x = y = z = 0.0f;
  91. }
  92. inline void Vec3::subtract(const Vec3& v)
  93. {
  94. x -= v.x;
  95. y -= v.y;
  96. z -= v.z;
  97. }
  98. inline Vec3 Vec3::operator+(const Vec3& v) const
  99. {
  100. Vec3 result(*this);
  101. result.add(v);
  102. return result;
  103. }
  104. inline Vec3& Vec3::operator+=(const Vec3& v)
  105. {
  106. add(v);
  107. return *this;
  108. }
  109. inline Vec3 Vec3::operator-(const Vec3& v) const
  110. {
  111. Vec3 result(*this);
  112. result.subtract(v);
  113. return result;
  114. }
  115. inline Vec3& Vec3::operator-=(const Vec3& v)
  116. {
  117. subtract(v);
  118. return *this;
  119. }
  120. inline Vec3 Vec3::operator-() const
  121. {
  122. Vec3 result(*this);
  123. result.negate();
  124. return result;
  125. }
  126. inline Vec3 Vec3::operator*(float s) const
  127. {
  128. Vec3 result(*this);
  129. result.scale(s);
  130. return result;
  131. }
  132. inline Vec3& Vec3::operator*=(float s)
  133. {
  134. scale(s);
  135. return *this;
  136. }
  137. inline Vec3 Vec3::operator/(const float s) const
  138. {
  139. return Vec3(this->x / s, this->y / s, this->z / s);
  140. }
  141. inline bool Vec3::operator==(const Vec3& v) const
  142. {
  143. return x==v.x && y==v.y && z==v.z;
  144. }
  145. inline bool Vec3::operator!=(const Vec3& v) const
  146. {
  147. return x!=v.x || y!=v.y || z!=v.z;
  148. }
  149. /* --- --- --- --- Add by XuJJ start --- --- --- --- */
  150. inline Vec3 ScaleVec3 (const Vec3& lhs, const Vec3& rhs) { return Vec3 (lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z); }
  151. inline Vec3 Cross (const Vec3& lhs, const Vec3& rhs);
  152. inline float Dot (const Vec3& lhs, const Vec3& rhs) { return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; }
  153. inline float SqrMagnitude (const Vec3& inV) { return Dot (inV, inV); }
  154. inline float Magnitude (const Vec3& inV) {return sqrt(Dot (inV, inV));}
  155. // Normalizes a vector, asserts if the vector can be normalized
  156. inline Vec3 Normalize (const Vec3& inV) { return inV / Magnitude (inV); }
  157. inline Vec3 Cross (const Vec3& lhs, const Vec3& rhs)
  158. {
  159. return Vec3 (
  160. lhs.y * rhs.z - lhs.z * rhs.y,
  161. lhs.z * rhs.x - lhs.x * rhs.z,
  162. lhs.x * rhs.y - lhs.y * rhs.x);
  163. }
  164. // Normalizes a vector, returns default vector if it can't be normalized
  165. inline Vec3 NormalizeSafe (const Vec3& inV, const Vec3& defaultV = Vec3::ZERO);
  166. inline Vec3 NormalizeSafe (const Vec3& inV, const Vec3& defaultV)
  167. {
  168. float mag = Magnitude (inV);
  169. if (mag > FLT_EPSILON)
  170. return inV / Magnitude (inV);
  171. else
  172. return defaultV;
  173. }
  174. inline Vec3 Lerp (const Vec3& from, const Vec3& to, float t) { return to * t + from * (1.0F - t); }
  175. // Returns a vector with the smaller of every component from v0 and v1
  176. inline Vec3 Min (const Vec3& lhs, const Vec3& rhs) { return Vec3 (MIN (lhs.x, rhs.x), MIN (lhs.y, rhs.y), MIN (lhs.z, rhs.z)); }
  177. // Returns a vector with the larger of every component from v0 and v1
  178. inline Vec3 Max (const Vec3& lhs, const Vec3& rhs) { return Vec3 (MAX (lhs.x, rhs.x), MAX (lhs.y, rhs.y), MAX (lhs.z, rhs.z)); }
  179. inline bool CompareApproximately (const Vec3& inV0, const Vec3& inV1, const float inMaxDist)
  180. {
  181. return SqrMagnitude (inV1 - inV0) < inMaxDist * inMaxDist;
  182. }
  183. inline float& Vec3::operator[] (int i)
  184. {
  185. // DebugAssertIf (i < 0 || i > 2);
  186. CC_ASSERT(!(i < 0 || i > 2));
  187. return (&x)[i];
  188. }
  189. inline const float& Vec3::operator[] (int i)const
  190. {
  191. // DebugAssertIf (i < 0 || i > 2);
  192. CC_ASSERT(!(i < 0 || i > 2));
  193. return (&x)[i];
  194. }
  195. /* --- --- --- --- Add by XuJJ end --- --- --- --- */
  196. inline Vec3 operator*(float x, const Vec3& v)
  197. {
  198. Vec3 result(v);
  199. result.scale(x);
  200. return result;
  201. }
  202. NS_CC_MATH_END