Vector3.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. namespace RVO3D {
  42. /**
  43. * \brief Defines a three-dimensional vector.
  44. */
  45. class Vector3 {
  46. public:
  47. /**
  48. * \brief Constructs and initializes a three-dimensional vector instance to zero.
  49. */
  50. inline Vector3()
  51. {
  52. val_[0] = 0.0f;
  53. val_[1] = 0.0f;
  54. val_[2] = 0.0f;
  55. }
  56. /**
  57. * \brief Constructs and initializes a three-dimensional vector from the specified three-dimensional vector.
  58. * \param vector The three-dimensional vector containing the xyz-coordinates.
  59. */
  60. inline Vector3(const Vector3 &vector)
  61. {
  62. val_[0] = vector[0];
  63. val_[1] = vector[1];
  64. val_[2] = vector[2];
  65. }
  66. /**
  67. * \brief Constructs and initializes a three-dimensional vector from the specified three-element array.
  68. * \param val The three-element array containing the xyz-coordinates.
  69. */
  70. inline explicit Vector3(const float val[3])
  71. {
  72. val_[0] = val[0];
  73. val_[1] = val[1];
  74. val_[2] = val[2];
  75. }
  76. /**
  77. * \brief Constructs and initializes a three-dimensional vector from the specified xyz-coordinates.
  78. * \param x The x-coordinate of the three-dimensional vector.
  79. * \param y The y-coordinate of the three-dimensional vector.
  80. * \param z The z-coordinate of the three-dimensional vector.
  81. */
  82. inline Vector3(float x, float y, float z)
  83. {
  84. val_[0] = x;
  85. val_[1] = y;
  86. val_[2] = z;
  87. }
  88. /**
  89. * \brief Returns the x-coordinate of this three-dimensional vector.
  90. * \return The x-coordinate of the three-dimensional vector.
  91. */
  92. inline float x() const { return val_[0]; }
  93. /**
  94. * \brief Returns the y-coordinate of this three-dimensional vector.
  95. * \return The y-coordinate of the three-dimensional vector.
  96. */
  97. inline float y() const { return val_[1]; }
  98. /**
  99. * \brief Returns the z-coordinate of this three-dimensional vector.
  100. * \return The z-coordinate of the three-dimensional vector.
  101. */
  102. inline float z() const { return val_[2]; }
  103. /**
  104. * \brief Returns the specified coordinate of this three-dimensional vector.
  105. * \param i The coordinate that should be returned (0 <= i < 3).
  106. * \return The specified coordinate of the three-dimensional vector.
  107. */
  108. inline float operator[](size_t i) const { return val_[i]; }
  109. /**
  110. * \brief Returns a reference to the specified coordinate of this three-dimensional vector.
  111. * \param i The coordinate to which a reference should be returned (0 <= i < 3).
  112. * \return A reference to the specified coordinate of the three-dimensional vector.
  113. */
  114. inline float &operator[](size_t i) { return val_[i]; }
  115. /**
  116. * \brief Computes the negation of this three-dimensional vector.
  117. * \return The negation of this three-dimensional vector.
  118. */
  119. inline Vector3 operator-() const
  120. {
  121. return Vector3(-val_[0], -val_[1], -val_[2]);
  122. }
  123. /**
  124. * \brief Computes the dot product of this three-dimensional vector with the specified three-dimensional vector.
  125. * \param vector The three-dimensional vector with which the dot product should be computed.
  126. * \return The dot product of this three-dimensional vector with a specified three-dimensional vector.
  127. */
  128. inline float operator*(const Vector3 &vector) const
  129. {
  130. return val_[0] * vector[0] + val_[1] * vector[1] + val_[2] * vector[2];
  131. }
  132. /**
  133. * \brief Computes the scalar multiplication of this three-dimensional vector with the specified scalar value.
  134. * \param scalar The scalar value with which the scalar multiplication should be computed.
  135. * \return The scalar multiplication of this three-dimensional vector with a specified scalar value.
  136. */
  137. inline Vector3 operator*(float scalar) const
  138. {
  139. return Vector3(val_[0] * scalar, val_[1] * scalar, val_[2] * scalar);
  140. }
  141. /**
  142. * \brief Computes the scalar division of this three-dimensional vector with the specified scalar value.
  143. * \param scalar The scalar value with which the scalar division should be computed.
  144. * \return The scalar division of this three-dimensional vector with a specified scalar value.
  145. */
  146. inline Vector3 operator/(float scalar) const
  147. {
  148. const float invScalar = 1.0f / scalar;
  149. return Vector3(val_[0] * invScalar, val_[1] * invScalar, val_[2] * invScalar);
  150. }
  151. /**
  152. * \brief Computes the vector sum of this three-dimensional vector with the specified three-dimensional vector.
  153. * \param vector The three-dimensional vector with which the vector sum should be computed.
  154. * \return The vector sum 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 Computes the vector difference of this three-dimensional vector with the specified three-dimensional vector.
  162. * \param vector The three-dimensional vector with which the vector difference should be computed.
  163. * \return The vector difference of this three-dimensional vector with a specified three-dimensional vector.
  164. */
  165. inline Vector3 operator-(const Vector3 &vector) const
  166. {
  167. return Vector3(val_[0] - vector[0], val_[1] - vector[1], val_[2] - vector[2]);
  168. }
  169. /**
  170. * \brief Tests this three-dimensional vector for equality with the specified three-dimensional vector.
  171. * \param vector The three-dimensional vector with which to test for equality.
  172. * \return True if the three-dimensional vectors are 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 Tests this three-dimensional vector for inequality with the specified three-dimensional vector.
  180. * \param vector The three-dimensional vector with which to test for inequality.
  181. * \return True if the three-dimensional vectors are not equal.
  182. */
  183. inline bool operator!=(const Vector3 &vector) const
  184. {
  185. return val_[0] != vector[0] || val_[1] != vector[1] || val_[2] != vector[2];
  186. }
  187. /**
  188. * \brief Sets the value of this three-dimensional vector to the scalar multiplication of itself with the specified scalar value.
  189. * \param scalar The scalar value with which the scalar multiplication should be computed.
  190. * \return A reference to this three-dimensional vector.
  191. */
  192. inline Vector3 &operator*=(float scalar)
  193. {
  194. val_[0] *= scalar;
  195. val_[1] *= scalar;
  196. val_[2] *= scalar;
  197. return *this;
  198. }
  199. /**
  200. * \brief Sets the value of this three-dimensional vector to the scalar division of itself with the specified scalar value.
  201. * \param scalar The scalar value with which the scalar division should be computed.
  202. * \return A reference to this three-dimensional vector.
  203. */
  204. inline Vector3 &operator/=(float scalar)
  205. {
  206. const float invScalar = 1.0f / scalar;
  207. val_[0] *= invScalar;
  208. val_[1] *= invScalar;
  209. val_[2] *= invScalar;
  210. return *this;
  211. }
  212. /**
  213. * \brief Sets the value of this three-dimensional vector to the vector
  214. * sum of itself with the specified three-dimensional vector.
  215. * \param vector The three-dimensional vector with which the vector sum should be computed.
  216. * \return A reference to this three-dimensional vector.
  217. */
  218. inline Vector3 &operator+=(const Vector3 &vector)
  219. {
  220. val_[0] += vector[0];
  221. val_[1] += vector[1];
  222. val_[2] += vector[2];
  223. return *this;
  224. }
  225. /**
  226. * \brief Sets the value of this three-dimensional vector to the vector difference of itself with the specified three-dimensional vector.
  227. * \param vector The three-dimensional vector with which the vector difference should be computed.
  228. * \return A reference to this three-dimensional vector.
  229. */
  230. inline Vector3 &operator-=(const Vector3 &vector)
  231. {
  232. val_[0] -= vector[0];
  233. val_[1] -= vector[1];
  234. val_[2] -= vector[2];
  235. return *this;
  236. }
  237. inline Vector3 &operator=(const Vector3 &vector)
  238. {
  239. val_[0] = vector[0];
  240. val_[1] = vector[1];
  241. val_[2] = vector[2];
  242. return *this;
  243. }
  244. private:
  245. float val_[3];
  246. };
  247. /**
  248. * \relates Vector3
  249. * \brief Computes the scalar multiplication of the specified three-dimensional vector with the specified scalar value.
  250. * \param scalar The scalar value with which the scalar multiplication should be computed.
  251. * \param vector The three-dimensional vector with which the scalar multiplication should be computed.
  252. * \return The scalar multiplication of the three-dimensional vector with the scalar value.
  253. */
  254. inline Vector3 operator*(float scalar, const Vector3 &vector)
  255. {
  256. return Vector3(scalar * vector[0], scalar * vector[1], scalar * vector[2]);
  257. }
  258. /**
  259. * \relates Vector3
  260. * \brief Computes the cross product of the specified three-dimensional vectors.
  261. * \param vector1 The first vector with which the cross product should be computed.
  262. * \param vector2 The second vector with which the cross product should be computed.
  263. * \return The cross product of the two specified vectors.
  264. */
  265. inline Vector3 cross(const Vector3 &vector1, const Vector3 &vector2)
  266. {
  267. 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]);
  268. }
  269. /**
  270. * \relates Vector3
  271. * \brief Inserts the specified three-dimensional vector into the specified output stream.
  272. * \param os The output stream into which the three-dimensional vector should be inserted.
  273. * \param vector The three-dimensional vector which to insert into the output stream.
  274. * \return A reference to the output stream.
  275. */
  276. inline std::ostream &operator<<(std::ostream &os, const Vector3 &vector)
  277. {
  278. os << "(" << vector[0] << "," << vector[1] << "," << vector[2] << ")";
  279. return os;
  280. }
  281. /**
  282. * \relates Vector3
  283. * \brief Computes the length of a specified three-dimensional vector.
  284. * \param vector The three-dimensional vector whose length is to be computed.
  285. * \return The length of the three-dimensional vector.
  286. */
  287. inline float abs(const Vector3 &vector)
  288. {
  289. return std::sqrt(vector * vector);
  290. }
  291. /**
  292. * \relates Vector3
  293. * \brief Computes the squared length of a specified three-dimensional vector.
  294. * \param vector The three-dimensional vector whose squared length is to be computed.
  295. * \return The squared length of the three-dimensional vector.
  296. */
  297. inline float absSq(const Vector3 &vector)
  298. {
  299. return vector * vector;
  300. }
  301. /**
  302. * \relates Vector3
  303. * \brief Computes the normalization of the specified three-dimensional vector.
  304. * \param vector The three-dimensional vector whose normalization is to be computed.
  305. * \return The normalization of the three-dimensional vector.
  306. */
  307. inline Vector3 normalize(const Vector3 &vector)
  308. {
  309. return vector / abs(vector);
  310. }
  311. }
  312. #endif