Vec3.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/MathUtil.h"
  17. #include "base/ccMacros.h"
  18. NS_CC_MATH_BEGIN
  19. Vec3::Vec3()
  20. : x(0.0f), y(0.0f), z(0.0f)
  21. {
  22. }
  23. Vec3::Vec3(float xx, float yy, float zz)
  24. : x(xx), y(yy), z(zz)
  25. {
  26. }
  27. Vec3::Vec3(const float* array)
  28. {
  29. set(array);
  30. }
  31. Vec3::Vec3(const Vec3& p1, const Vec3& p2)
  32. {
  33. set(p1, p2);
  34. }
  35. Vec3::Vec3(const Vec3& copy)
  36. {
  37. set(copy);
  38. }
  39. Vec3 Vec3::fromColor(unsigned int color)
  40. {
  41. float components[3];
  42. int componentIndex = 0;
  43. for (int i = 2; i >= 0; --i)
  44. {
  45. int component = (color >> i*8) & 0x0000ff;
  46. components[componentIndex++] = static_cast<float>(component) / 255.0f;
  47. }
  48. Vec3 value(components);
  49. return value;
  50. }
  51. Vec3::~Vec3()
  52. {
  53. }
  54. float Vec3::angle(const Vec3& v1, const Vec3& v2)
  55. {
  56. float dx = v1.y * v2.z - v1.z * v2.y;
  57. float dy = v1.z * v2.x - v1.x * v2.z;
  58. float dz = v1.x * v2.y - v1.y * v2.x;
  59. return std::atan2(std::sqrt(dx * dx + dy * dy + dz * dz) + MATH_FLOAT_SMALL, dot(v1, v2));
  60. }
  61. void Vec3::add(const Vec3& v1, const Vec3& v2, Vec3* dst)
  62. {
  63. GP_ASSERT(dst);
  64. dst->x = v1.x + v2.x;
  65. dst->y = v1.y + v2.y;
  66. dst->z = v1.z + v2.z;
  67. }
  68. void Vec3::clamp(const Vec3& min, const Vec3& max)
  69. {
  70. GP_ASSERT(!(min.x > max.x || min.y > max.y || min.z > max.z));
  71. // Clamp the x value.
  72. if (x < min.x)
  73. x = min.x;
  74. if (x > max.x)
  75. x = max.x;
  76. // Clamp the y value.
  77. if (y < min.y)
  78. y = min.y;
  79. if (y > max.y)
  80. y = max.y;
  81. // Clamp the z value.
  82. if (z < min.z)
  83. z = min.z;
  84. if (z > max.z)
  85. z = max.z;
  86. }
  87. void Vec3::clamp(const Vec3& v, const Vec3& min, const Vec3& max, Vec3* dst)
  88. {
  89. GP_ASSERT(dst);
  90. GP_ASSERT(!(min.x > max.x || min.y > max.y || min.z > max.z));
  91. // Clamp the x value.
  92. dst->x = v.x;
  93. if (dst->x < min.x)
  94. dst->x = min.x;
  95. if (dst->x > max.x)
  96. dst->x = max.x;
  97. // Clamp the y value.
  98. dst->y = v.y;
  99. if (dst->y < min.y)
  100. dst->y = min.y;
  101. if (dst->y > max.y)
  102. dst->y = max.y;
  103. // Clamp the z value.
  104. dst->z = v.z;
  105. if (dst->z < min.z)
  106. dst->z = min.z;
  107. if (dst->z > max.z)
  108. dst->z = max.z;
  109. }
  110. void Vec3::cross(const Vec3& v)
  111. {
  112. cross(*this, v, this);
  113. }
  114. void Vec3::cross(const Vec3& v1, const Vec3& v2, Vec3* dst)
  115. {
  116. GP_ASSERT(dst);
  117. // NOTE: This code assumes Vec3 struct members are contiguous floats in memory.
  118. // We might want to revisit this (and other areas of code that make this assumption)
  119. // later to guarantee 100% safety/compatibility.
  120. MathUtil::crossVec3(&v1.x, &v2.x, &dst->x);
  121. }
  122. float Vec3::distance(const Vec3& v) const
  123. {
  124. float dx = v.x - x;
  125. float dy = v.y - y;
  126. float dz = v.z - z;
  127. return std::sqrt(dx * dx + dy * dy + dz * dz);
  128. }
  129. float Vec3::distanceSquared(const Vec3& v) const
  130. {
  131. float dx = v.x - x;
  132. float dy = v.y - y;
  133. float dz = v.z - z;
  134. return (dx * dx + dy * dy + dz * dz);
  135. }
  136. float Vec3::dot(const Vec3& v) const
  137. {
  138. return (x * v.x + y * v.y + z * v.z);
  139. }
  140. float Vec3::dot(const Vec3& v1, const Vec3& v2)
  141. {
  142. return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
  143. }
  144. void Vec3::normalize()
  145. {
  146. float n = x * x + y * y + z * z;
  147. // Already normalized.
  148. if (n == 1.0f)
  149. return;
  150. n = std::sqrt(n);
  151. // Too close to zero.
  152. if (n < MATH_TOLERANCE)
  153. return;
  154. n = 1.0f / n;
  155. x *= n;
  156. y *= n;
  157. z *= n;
  158. }
  159. Vec3 Vec3::getNormalized() const
  160. {
  161. Vec3 v(*this);
  162. v.normalize();
  163. return v;
  164. }
  165. void Vec3::subtract(const Vec3& v1, const Vec3& v2, Vec3* dst)
  166. {
  167. GP_ASSERT(dst);
  168. dst->x = v1.x - v2.x;
  169. dst->y = v1.y - v2.y;
  170. dst->z = v1.z - v2.z;
  171. }
  172. void Vec3::smooth(const Vec3& target, float elapsedTime, float responseTime)
  173. {
  174. if (elapsedTime > 0)
  175. {
  176. *this += (target - *this) * (elapsedTime / (elapsedTime + responseTime));
  177. }
  178. }
  179. const Vec3 Vec3::ZERO(0.0f, 0.0f, 0.0f);
  180. const Vec3 Vec3::ONE(1.0f, 1.0f, 1.0f);
  181. const Vec3 Vec3::UNIT_X(1.0f, 0.0f, 0.0f);
  182. const Vec3 Vec3::UNIT_Y(0.0f, 1.0f, 0.0f);
  183. const Vec3 Vec3::UNIT_Z(0.0f, 0.0f, 1.0f);
  184. NS_CC_MATH_END