Vector3.cpp 5.9 KB

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