Vec4.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. Original file from GamePlay3D: http://gameplay3d.org
  14. This file was modified to fit the cocos2d-x project
  15. */
  16. #include "math/Vec4.h"
  17. #include <cmath>
  18. #include "math/MathUtil.h"
  19. #include "base/ccMacros.h"
  20. NS_CC_MATH_BEGIN
  21. Vec4::Vec4()
  22. : x(0.0f), y(0.0f), z(0.0f), w(0.0f)
  23. {
  24. }
  25. Vec4::Vec4(float xx, float yy, float zz, float ww)
  26. : x(xx), y(yy), z(zz), w(ww)
  27. {
  28. }
  29. Vec4::Vec4(const float* src)
  30. {
  31. set(src);
  32. }
  33. Vec4::Vec4(const Vec4& p1, const Vec4& p2)
  34. {
  35. set(p1, p2);
  36. }
  37. Vec4::Vec4(const Vec4& copy)
  38. {
  39. set(copy);
  40. }
  41. Vec4 Vec4::fromColor(unsigned int color)
  42. {
  43. float components[4];
  44. int componentIndex = 0;
  45. for (int i = 3; i >= 0; --i)
  46. {
  47. int component = (color >> i*8) & 0x000000ff;
  48. components[componentIndex++] = static_cast<float>(component) / 255.0f;
  49. }
  50. Vec4 value(components);
  51. return value;
  52. }
  53. Vec4::~Vec4()
  54. {
  55. }
  56. bool Vec4::isZero() const
  57. {
  58. return x == 0.0f && y == 0.0f && z == 0.0f && w == 0.0f;
  59. }
  60. bool Vec4::isOne() const
  61. {
  62. return x == 1.0f && y == 1.0f && z == 1.0f && w == 1.0f;
  63. }
  64. float Vec4::angle(const Vec4& v1, const Vec4& v2)
  65. {
  66. float dx = v1.w * v2.x - v1.x * v2.w - v1.y * v2.z + v1.z * v2.y;
  67. float dy = v1.w * v2.y - v1.y * v2.w - v1.z * v2.x + v1.x * v2.z;
  68. float dz = v1.w * v2.z - v1.z * v2.w - v1.x * v2.y + v1.y * v2.x;
  69. return std::atan2(std::sqrt(dx * dx + dy * dy + dz * dz) + MATH_FLOAT_SMALL, dot(v1, v2));
  70. }
  71. void Vec4::add(const Vec4& v)
  72. {
  73. x += v.x;
  74. y += v.y;
  75. z += v.z;
  76. w += v.w;
  77. }
  78. void Vec4::add(const Vec4& v1, const Vec4& v2, Vec4* dst)
  79. {
  80. GP_ASSERT(dst);
  81. dst->x = v1.x + v2.x;
  82. dst->y = v1.y + v2.y;
  83. dst->z = v1.z + v2.z;
  84. dst->w = v1.w + v2.w;
  85. }
  86. void Vec4::clamp(const Vec4& min, const Vec4& max)
  87. {
  88. GP_ASSERT(!(min.x > max.x || min.y > max.y || min.z > max.z || min.w > max.w));
  89. // Clamp the x value.
  90. if (x < min.x)
  91. x = min.x;
  92. if (x > max.x)
  93. x = max.x;
  94. // Clamp the y value.
  95. if (y < min.y)
  96. y = min.y;
  97. if (y > max.y)
  98. y = max.y;
  99. // Clamp the z value.
  100. if (z < min.z)
  101. z = min.z;
  102. if (z > max.z)
  103. z = max.z;
  104. // Clamp the z value.
  105. if (w < min.w)
  106. w = min.w;
  107. if (w > max.w)
  108. w = max.w;
  109. }
  110. void Vec4::clamp(const Vec4& v, const Vec4& min, const Vec4& max, Vec4* dst)
  111. {
  112. GP_ASSERT(dst);
  113. GP_ASSERT(!(min.x > max.x || min.y > max.y || min.z > max.z || min.w > max.w));
  114. // Clamp the x value.
  115. dst->x = v.x;
  116. if (dst->x < min.x)
  117. dst->x = min.x;
  118. if (dst->x > max.x)
  119. dst->x = max.x;
  120. // Clamp the y value.
  121. dst->y = v.y;
  122. if (dst->y < min.y)
  123. dst->y = min.y;
  124. if (dst->y > max.y)
  125. dst->y = max.y;
  126. // Clamp the z value.
  127. dst->z = v.z;
  128. if (dst->z < min.z)
  129. dst->z = min.z;
  130. if (dst->z > max.z)
  131. dst->z = max.z;
  132. // Clamp the w value.
  133. dst->w = v.w;
  134. if (dst->w < min.w)
  135. dst->w = min.w;
  136. if (dst->w > max.w)
  137. dst->w = max.w;
  138. }
  139. float Vec4::distance(const Vec4& v) const
  140. {
  141. float dx = v.x - x;
  142. float dy = v.y - y;
  143. float dz = v.z - z;
  144. float dw = v.w - w;
  145. return std::sqrt(dx * dx + dy * dy + dz * dz + dw * dw);
  146. }
  147. float Vec4::distanceSquared(const Vec4& v) const
  148. {
  149. float dx = v.x - x;
  150. float dy = v.y - y;
  151. float dz = v.z - z;
  152. float dw = v.w - w;
  153. return (dx * dx + dy * dy + dz * dz + dw * dw);
  154. }
  155. float Vec4::dot(const Vec4& v) const
  156. {
  157. return (x * v.x + y * v.y + z * v.z + w * v.w);
  158. }
  159. float Vec4::dot(const Vec4& v1, const Vec4& v2)
  160. {
  161. return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w);
  162. }
  163. float Vec4::length() const
  164. {
  165. return std::sqrt(x * x + y * y + z * z + w * w);
  166. }
  167. float Vec4::lengthSquared() const
  168. {
  169. return (x * x + y * y + z * z + w * w);
  170. }
  171. void Vec4::negate()
  172. {
  173. x = -x;
  174. y = -y;
  175. z = -z;
  176. w = -w;
  177. }
  178. void Vec4::normalize()
  179. {
  180. float n = x * x + y * y + z * z + w * w;
  181. // Already normalized.
  182. if (n == 1.0f)
  183. return;
  184. n = std::sqrt(n);
  185. // Too close to zero.
  186. if (n < MATH_TOLERANCE)
  187. return;
  188. n = 1.0f / n;
  189. x *= n;
  190. y *= n;
  191. z *= n;
  192. w *= n;
  193. }
  194. Vec4 Vec4::getNormalized() const
  195. {
  196. Vec4 v(*this);
  197. v.normalize();
  198. return v;
  199. }
  200. void Vec4::scale(float scalar)
  201. {
  202. x *= scalar;
  203. y *= scalar;
  204. z *= scalar;
  205. w *= scalar;
  206. }
  207. void Vec4::set(float xx, float yy, float zz, float ww)
  208. {
  209. this->x = xx;
  210. this->y = yy;
  211. this->z = zz;
  212. this->w = ww;
  213. }
  214. void Vec4::set(const float* array)
  215. {
  216. GP_ASSERT(array);
  217. x = array[0];
  218. y = array[1];
  219. z = array[2];
  220. w = array[3];
  221. }
  222. void Vec4::set(const Vec4& v)
  223. {
  224. this->x = v.x;
  225. this->y = v.y;
  226. this->z = v.z;
  227. this->w = v.w;
  228. }
  229. void Vec4::set(const Vec4& p1, const Vec4& p2)
  230. {
  231. x = p2.x - p1.x;
  232. y = p2.y - p1.y;
  233. z = p2.z - p1.z;
  234. w = p2.w - p1.w;
  235. }
  236. void Vec4::subtract(const Vec4& v)
  237. {
  238. x -= v.x;
  239. y -= v.y;
  240. z -= v.z;
  241. w -= v.w;
  242. }
  243. void Vec4::subtract(const Vec4& v1, const Vec4& v2, Vec4* dst)
  244. {
  245. GP_ASSERT(dst);
  246. dst->x = v1.x - v2.x;
  247. dst->y = v1.y - v2.y;
  248. dst->z = v1.z - v2.z;
  249. dst->w = v1.w - v2.w;
  250. }
  251. const Vec4 Vec4::ZERO = Vec4(0.0f, 0.0f, 0.0f, 0.0f);
  252. const Vec4 Vec4::ONE = Vec4(1.0f, 1.0f, 1.0f, 1.0f);
  253. const Vec4 Vec4::UNIT_X = Vec4(1.0f, 0.0f, 0.0f, 0.0f);
  254. const Vec4 Vec4::UNIT_Y = Vec4(0.0f, 1.0f, 0.0f, 0.0f);
  255. const Vec4 Vec4::UNIT_Z = Vec4(0.0f, 0.0f, 1.0f, 0.0f);
  256. const Vec4 Vec4::UNIT_W = Vec4(0.0f, 0.0f, 0.0f, 1.0f);
  257. NS_CC_MATH_END