Vector4.cpp 6.1 KB

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