Vector3.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include "Base.h"
  2. #include "Vector3.h"
  3. namespace gameplay
  4. {
  5. Vector3::Vector3()
  6. : x(0.0f), y(0.0f), z(0.0f)
  7. {
  8. }
  9. Vector3::Vector3(float x, float y, float z)
  10. {
  11. set(x, y, z);
  12. }
  13. Vector3::Vector3(float* array)
  14. {
  15. set(array);
  16. }
  17. Vector3::Vector3(const Vector3& p1, const Vector3& p2)
  18. {
  19. set(p1, p2);
  20. }
  21. Vector3::Vector3(const Vector3& copy)
  22. {
  23. set(copy);
  24. }
  25. Vector3 Vector3::fromColor(unsigned int color)
  26. {
  27. float components[3];
  28. int componentIndex = 0;
  29. for (int i = 2; i >= 0; --i)
  30. {
  31. int component = (color >> i*8) & 0x0000ff;
  32. components[componentIndex++] = static_cast<float>(component) / 255.0f;
  33. }
  34. Vector3 value(components);
  35. return value;
  36. }
  37. Vector3::~Vector3()
  38. {
  39. }
  40. const Vector3& Vector3::zero()
  41. {
  42. static Vector3 value(0.0f, 0.0f, 0.0f);
  43. return value;
  44. }
  45. const Vector3& Vector3::one()
  46. {
  47. static Vector3 value(1.0f, 1.0f, 1.0f);
  48. return value;
  49. }
  50. const Vector3& Vector3::unitX()
  51. {
  52. static Vector3 value(1.0f, 0.0f, 0.0f);
  53. return value;
  54. }
  55. const Vector3& Vector3::unitY()
  56. {
  57. static Vector3 value(0.0f, 1.0f, 0.0f);
  58. return value;
  59. }
  60. const Vector3& Vector3::unitZ()
  61. {
  62. static Vector3 value(0.0f, 0.0f, 1.0f);
  63. return value;
  64. }
  65. bool Vector3::isZero() const
  66. {
  67. return x == 0.0f && y == 0.0f && z == 0.0f;
  68. }
  69. bool Vector3::isOne() const
  70. {
  71. return x == 1.0f && y == 1.0f && z == 1.0f;
  72. }
  73. float Vector3::angle(const Vector3& v1, const Vector3& v2)
  74. {
  75. float dx = v1.y * v2.z - v1.z * v2.y;
  76. float dy = v1.z * v2.x - v1.x * v2.z;
  77. float dz = v1.x * v2.y - v1.y * v2.x;
  78. return atan2f(sqrt(dx * dx + dy * dy + dz * dz) + MATH_FLOAT_SMALL, dot(v1, v2));
  79. }
  80. void Vector3::add(const Vector3& v)
  81. {
  82. x += v.x;
  83. y += v.y;
  84. z += v.z;
  85. }
  86. void Vector3::add(const Vector3& v1, const Vector3& v2, Vector3* dst)
  87. {
  88. assert(dst);
  89. dst->x = v1.x + v2.x;
  90. dst->y = v1.y + v2.y;
  91. dst->z = v1.z + v2.z;
  92. }
  93. void Vector3::clamp(const Vector3& min, const Vector3& max)
  94. {
  95. assert(!( min.x > max.x || min.y > max.y || min.z > max.z));
  96. // Clamp the x value.
  97. if ( x < min.x )
  98. x = min.x;
  99. if ( x > max.x )
  100. x = max.x;
  101. // Clamp the y value.
  102. if ( y < min.y )
  103. y = min.y;
  104. if ( y > max.y )
  105. y = max.y;
  106. // Clamp the z value.
  107. if ( z < min.z )
  108. z = min.z;
  109. if ( z > max.z )
  110. z = max.z;
  111. }
  112. void Vector3::clamp(const Vector3& v, const Vector3& min, const Vector3& max, Vector3* dst)
  113. {
  114. assert(dst);
  115. assert(!( min.x > max.x || min.y > max.y || min.z > max.z));
  116. // Clamp the x value.
  117. dst->x = v.x;
  118. if ( dst->x < min.x )
  119. dst->x = min.x;
  120. if ( dst->x > max.x )
  121. dst->x = max.x;
  122. // Clamp the y value.
  123. dst->y = v.y;
  124. if ( dst->y < min.y )
  125. dst->y = min.y;
  126. if ( dst->y > max.y )
  127. dst->y = max.y;
  128. // Clamp the z value.
  129. dst->z = v.z;
  130. if ( dst->z < min.z )
  131. dst->z = min.z;
  132. if ( dst->z > max.z )
  133. dst->z = max.z;
  134. }
  135. void Vector3::cross(const Vector3& v)
  136. {
  137. float tx = (y * v.z) - (z * v.y);
  138. float ty = (z * v.x) - (x * v.z);
  139. float tz = (x * v.y) - (y * v.x);
  140. x = tx;
  141. y = ty;
  142. z = tz;
  143. }
  144. void Vector3::cross(const Vector3& v1, const Vector3& v2, Vector3* dst)
  145. {
  146. assert(dst);
  147. float x = (v1.y * v2.z) - (v1.z * v2.y);
  148. float y = (v1.z * v2.x) - (v1.x * v2.z);
  149. float z = (v1.x * v2.y) - (v1.y * v2.x);
  150. dst->x = x;
  151. dst->y = y;
  152. dst->z = z;
  153. }
  154. float Vector3::distance(const Vector3& v) const
  155. {
  156. float dx = v.x - x;
  157. float dy = v.y - y;
  158. float dz = v.z - z;
  159. return sqrt(dx * dx + dy * dy + dz * dz);
  160. }
  161. float Vector3::distanceSquared(const Vector3& v) const
  162. {
  163. float dx = v.x - x;
  164. float dy = v.y - y;
  165. float dz = v.z - z;
  166. return (dx * dx + dy * dy + dz * dz);
  167. }
  168. float Vector3::dot(const Vector3& v)
  169. {
  170. return (x * v.x + y * v.y + z * v.z);
  171. }
  172. float Vector3::dot(const Vector3& v1, const Vector3& v2)
  173. {
  174. return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
  175. }
  176. float Vector3::length() const
  177. {
  178. return sqrt(x * x + y * y + z * z);
  179. }
  180. float Vector3::lengthSquared() const
  181. {
  182. return (x * x + y * y + z * z);
  183. }
  184. void Vector3::negate()
  185. {
  186. x = -x;
  187. y = -y;
  188. z = -z;
  189. }
  190. void Vector3::normalize()
  191. {
  192. normalize(this);
  193. }
  194. void Vector3::normalize(Vector3* dst) const
  195. {
  196. assert(dst);
  197. if (dst != this)
  198. {
  199. dst->x = x;
  200. dst->y = y;
  201. dst->z = z;
  202. }
  203. float n = x * x + y * y + z * z;
  204. // Already normalized.
  205. if (n == 1.0f)
  206. return;
  207. n = sqrt(n);
  208. // Too close to zero.
  209. if (n < MATH_TOLERANCE)
  210. return;
  211. n = 1.0f / n;
  212. dst->x *= n;
  213. dst->y *= n;
  214. dst->z *= n;
  215. }
  216. void Vector3::scale(float scalar)
  217. {
  218. x *= scalar;
  219. y *= scalar;
  220. z *= scalar;
  221. }
  222. void Vector3::set(float x, float y, float z)
  223. {
  224. this->x = x;
  225. this->y = y;
  226. this->z = z;
  227. }
  228. void Vector3::set(float* array)
  229. {
  230. assert(array);
  231. x = array[0];
  232. y = array[1];
  233. z = array[2];
  234. }
  235. void Vector3::set(const Vector3& v)
  236. {
  237. this->x = v.x;
  238. this->y = v.y;
  239. this->z = v.z;
  240. }
  241. void Vector3::set(const Vector3& p1, const Vector3& p2)
  242. {
  243. x = p2.x - p1.x;
  244. y = p2.y - p1.y;
  245. z = p2.z - p1.z;
  246. }
  247. void Vector3::subtract(const Vector3& v)
  248. {
  249. x -= v.x;
  250. y -= v.y;
  251. z -= v.z;
  252. }
  253. void Vector3::subtract(const Vector3& v1, const Vector3& v2, Vector3* dst)
  254. {
  255. assert(dst);
  256. dst->x = v1.x - v2.x;
  257. dst->y = v1.y - v2.y;
  258. dst->z = v1.z - v2.z;
  259. }
  260. }