Vector3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Vector3.h
  3. * RVO2-3D Library
  4. *
  5. * Copyright 2008 University of North Carolina at Chapel Hill
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * https://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * Please send all bug reports to <geom@cs.unc.edu>.
  20. *
  21. * The authors may be contacted via:
  22. *
  23. * Jur van den Berg, Stephen J. Guy, Jamie Snape, Ming C. Lin, Dinesh Manocha
  24. * Dept. of Computer Science
  25. * 201 S. Columbia St.
  26. * Frederick P. Brooks, Jr. Computer Science Bldg.
  27. * Chapel Hill, N.C. 27599-3175
  28. * United States of America
  29. *
  30. * <https://gamma.cs.unc.edu/RVO2/>
  31. */
  32. /**
  33. * \file Vector3.h
  34. * \brief Contains the Vector3 class.
  35. */
  36. #ifndef RVO3D_VECTOR3_H_
  37. #define RVO3D_VECTOR3_H_
  38. #include <cmath>
  39. #include <cstddef>
  40. #include <ostream>
  41. #define RVO3D_EXPORT
  42. namespace RVO {
  43. /**
  44. * \brief Defines a three-dimensional vector.
  45. */
  46. class RVO3D_EXPORT Vector3 {
  47. public:
  48. /**
  49. * \brief Constructs and initializes a three-dimensional vector instance to zero.
  50. */
  51. inline Vector3()
  52. {
  53. val_[0] = 0.0f;
  54. val_[1] = 0.0f;
  55. val_[2] = 0.0f;
  56. }
  57. /**
  58. * \brief Constructs and initializes a three-dimensional vector from the specified three-element array.
  59. * \param val The three-element array containing the xyz-coordinates.
  60. */
  61. inline explicit Vector3(const float val[3])
  62. {
  63. val_[0] = val[0];
  64. val_[1] = val[1];
  65. val_[2] = val[2];
  66. }
  67. /**
  68. * \brief Constructs and initializes a three-dimensional vector from the specified xyz-coordinates.
  69. * \param x The x-coordinate of the three-dimensional vector.
  70. * \param y The y-coordinate of the three-dimensional vector.
  71. * \param z The z-coordinate of the three-dimensional vector.
  72. */
  73. inline Vector3(float x, float y, float z)
  74. {
  75. val_[0] = x;
  76. val_[1] = y;
  77. val_[2] = z;
  78. }
  79. /**
  80. * \brief Returns the x-coordinate of this three-dimensional vector.
  81. * \return The x-coordinate of the three-dimensional vector.
  82. */
  83. inline float x() const { return val_[0]; }
  84. /**
  85. * \brief Returns the y-coordinate of this three-dimensional vector.
  86. * \return The y-coordinate of the three-dimensional vector.
  87. */
  88. inline float y() const { return val_[1]; }
  89. /**
  90. * \brief Returns the z-coordinate of this three-dimensional vector.
  91. * \return The z-coordinate of the three-dimensional vector.
  92. */
  93. inline float z() const { return val_[2]; }
  94. /**
  95. * \brief Returns the specified coordinate of this three-dimensional vector.
  96. * \param i The coordinate that should be returned (0 <= i < 3).
  97. * \return The specified coordinate of the three-dimensional vector.
  98. */
  99. inline float operator[](size_t i) const { return val_[i]; }
  100. /**
  101. * \brief Returns a reference to the specified coordinate of this three-dimensional vector.
  102. * \param i The coordinate to which a reference should be returned (0 <= i < 3).
  103. * \return A reference to the specified coordinate of the three-dimensional vector.
  104. */
  105. inline float &operator[](size_t i) { return val_[i]; }
  106. /**
  107. * \brief Computes the negation of this three-dimensional vector.
  108. * \return The negation of this three-dimensional vector.
  109. */
  110. inline Vector3 operator-() const
  111. {
  112. return Vector3(-val_[0], -val_[1], -val_[2]);
  113. }
  114. /**
  115. * \brief Computes the dot product of this three-dimensional vector with the specified three-dimensional vector.
  116. * \param vector The three-dimensional vector with which the dot product should be computed.
  117. * \return The dot product of this three-dimensional vector with a specified three-dimensional vector.
  118. */
  119. inline float operator*(const Vector3 &vector) const
  120. {
  121. return val_[0] * vector[0] + val_[1] * vector[1] + val_[2] * vector[2];
  122. }
  123. /**
  124. * \brief Computes the scalar multiplication of this three-dimensional vector with the specified scalar value.
  125. * \param scalar The scalar value with which the scalar multiplication should be computed.
  126. * \return The scalar multiplication of this three-dimensional vector with a specified scalar value.
  127. */
  128. inline Vector3 operator*(float scalar) const
  129. {
  130. return Vector3(val_[0] * scalar, val_[1] * scalar, val_[2] * scalar);
  131. }
  132. /**
  133. * \brief Computes the scalar division of this three-dimensional vector with the specified scalar value.
  134. * \param scalar The scalar value with which the scalar division should be computed.
  135. * \return The scalar division of this three-dimensional vector with a specified scalar value.
  136. */
  137. inline Vector3 operator/(float scalar) const
  138. {
  139. const float invScalar = 1.0f / scalar;
  140. return Vector3(val_[0] * invScalar, val_[1] * invScalar, val_[2] * invScalar);
  141. }
  142. /**
  143. * \brief Computes the vector sum of this three-dimensional vector with the specified three-dimensional vector.
  144. * \param vector The three-dimensional vector with which the vector sum should be computed.
  145. * \return The vector sum of this three-dimensional vector with a specified three-dimensional vector.
  146. */
  147. inline Vector3 operator+(const Vector3 &vector) const
  148. {
  149. return Vector3(val_[0] + vector[0], val_[1] + vector[1], val_[2] + vector[2]);
  150. }
  151. /**
  152. * \brief Computes the vector difference of this three-dimensional vector with the specified three-dimensional vector.
  153. * \param vector The three-dimensional vector with which the vector difference should be computed.
  154. * \return The vector difference of this three-dimensional vector with a specified three-dimensional vector.
  155. */
  156. inline Vector3 operator-(const Vector3 &vector) const
  157. {
  158. return Vector3(val_[0] - vector[0], val_[1] - vector[1], val_[2] - vector[2]);
  159. }
  160. /**
  161. * \brief Tests this three-dimensional vector for equality with the specified three-dimensional vector.
  162. * \param vector The three-dimensional vector with which to test for equality.
  163. * \return True if the three-dimensional vectors are equal.
  164. */
  165. inline bool operator==(const Vector3 &vector) const
  166. {
  167. return val_[0] == vector[0] && val_[1] == vector[1] && val_[2] == vector[2];
  168. }
  169. /**
  170. * \brief Tests this three-dimensional vector for inequality with the specified three-dimensional vector.
  171. * \param vector The three-dimensional vector with which to test for inequality.
  172. * \return True if the three-dimensional vectors are not equal.
  173. */
  174. inline bool operator!=(const Vector3 &vector) const
  175. {
  176. return val_[0] != vector[0] || val_[1] != vector[1] || val_[2] != vector[2];
  177. }
  178. /**
  179. * \brief Sets the value of this three-dimensional vector to the scalar multiplication of itself with the specified scalar value.
  180. * \param scalar The scalar value with which the scalar multiplication should be computed.
  181. * \return A reference to this three-dimensional vector.
  182. */
  183. inline Vector3 &operator*=(float scalar)
  184. {
  185. val_[0] *= scalar;
  186. val_[1] *= scalar;
  187. val_[2] *= scalar;
  188. return *this;
  189. }
  190. /**
  191. * \brief Sets the value of this three-dimensional vector to the scalar division of itself with the specified scalar value.
  192. * \param scalar The scalar value with which the scalar division should be computed.
  193. * \return A reference to this three-dimensional vector.
  194. */
  195. inline Vector3 &operator/=(float scalar)
  196. {
  197. const float invScalar = 1.0f / scalar;
  198. val_[0] *= invScalar;
  199. val_[1] *= invScalar;
  200. val_[2] *= invScalar;
  201. return *this;
  202. }
  203. /**
  204. * \brief Sets the value of this three-dimensional vector to the vector
  205. * sum of itself with the specified three-dimensional vector.
  206. * \param vector The three-dimensional vector with which the vector sum should be computed.
  207. * \return A reference to this three-dimensional vector.
  208. */
  209. inline Vector3 &operator+=(const Vector3 &vector)
  210. {
  211. val_[0] += vector[0];
  212. val_[1] += vector[1];
  213. val_[2] += vector[2];
  214. return *this;
  215. }
  216. /**
  217. * \brief Sets the value of this three-dimensional vector to the vector difference of itself with the specified three-dimensional vector.
  218. * \param vector The three-dimensional vector with which the vector difference should be computed.
  219. * \return A reference to this three-dimensional vector.
  220. */
  221. inline Vector3 &operator-=(const Vector3 &vector)
  222. {
  223. val_[0] -= vector[0];
  224. val_[1] -= vector[1];
  225. val_[2] -= vector[2];
  226. return *this;
  227. }
  228. private:
  229. float val_[3];
  230. };
  231. /**
  232. * \relates Vector3
  233. * \brief Computes the scalar multiplication of the specified three-dimensional vector with the specified scalar value.
  234. * \param scalar The scalar value with which the scalar multiplication should be computed.
  235. * \param vector The three-dimensional vector with which the scalar multiplication should be computed.
  236. * \return The scalar multiplication of the three-dimensional vector with the scalar value.
  237. */
  238. RVO3D_EXPORT inline Vector3 operator*(float scalar, const Vector3 &vector)
  239. {
  240. return Vector3(scalar * vector[0], scalar * vector[1], scalar * vector[2]);
  241. }
  242. /**
  243. * \relates Vector3
  244. * \brief Computes the cross product of the specified three-dimensional vectors.
  245. * \param vector1 The first vector with which the cross product should be computed.
  246. * \param vector2 The second vector with which the cross product should be computed.
  247. * \return The cross product of the two specified vectors.
  248. */
  249. RVO3D_EXPORT inline Vector3 cross(const Vector3 &vector1, const Vector3 &vector2)
  250. {
  251. return Vector3(vector1[1] * vector2[2] - vector1[2] * vector2[1], vector1[2] * vector2[0] - vector1[0] * vector2[2], vector1[0] * vector2[1] - vector1[1] * vector2[0]);
  252. }
  253. /**
  254. * \relates Vector3
  255. * \brief Inserts the specified three-dimensional vector into the specified output stream.
  256. * \param os The output stream into which the three-dimensional vector should be inserted.
  257. * \param vector The three-dimensional vector which to insert into the output stream.
  258. * \return A reference to the output stream.
  259. */
  260. RVO3D_EXPORT inline std::ostream &operator<<(std::ostream &os, const Vector3 &vector)
  261. {
  262. os << "(" << vector[0] << "," << vector[1] << "," << vector[2] << ")";
  263. return os;
  264. }
  265. /**
  266. * \relates Vector3
  267. * \brief Computes the length of a specified three-dimensional vector.
  268. * \param vector The three-dimensional vector whose length is to be computed.
  269. * \return The length of the three-dimensional vector.
  270. */
  271. RVO3D_EXPORT inline float abs(const Vector3 &vector)
  272. {
  273. return std::sqrt(vector * vector);
  274. }
  275. /**
  276. * \relates Vector3
  277. * \brief Computes the squared length of a specified three-dimensional vector.
  278. * \param vector The three-dimensional vector whose squared length is to be computed.
  279. * \return The squared length of the three-dimensional vector.
  280. */
  281. RVO3D_EXPORT inline float absSq(const Vector3 &vector)
  282. {
  283. return vector * vector;
  284. }
  285. /**
  286. * \relates Vector3
  287. * \brief Computes the normalization of the specified three-dimensional vector.
  288. * \param vector The three-dimensional vector whose normalization is to be computed.
  289. * \return The normalization of the three-dimensional vector.
  290. */
  291. RVO3D_EXPORT inline Vector3 normalize(const Vector3 &vector)
  292. {
  293. return vector / abs(vector);
  294. }
  295. }
  296. #endif