Vector4.cpp 6.1 KB

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