| 1 | /* |
| 2 | * Vector2.h |
| 3 | * RVO2 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 | * http://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 | * <http://gamma.cs.unc.edu/RVO2/> |
| 31 | */ |
| 32 | |
| 33 | #ifndef RVO_VECTOR2_H_ |
| 34 | #define RVO_VECTOR2_H_ |
| 35 | |
| 36 | /** |
| 37 | * \file Vector2.h |
| 38 | * \brief Contains the Vector2 class. |
| 39 | */ |
| 40 | |
| 41 | #include <cmath> |
| 42 | #include <ostream> |
| 43 | |
| 44 | namespace RVO2D { |
| 45 | /** |
| 46 | * \brief Defines a two-dimensional vector. |
| 47 | */ |
| 48 | class Vector2 { |
| 49 | public: |
| 50 | /** |
| 51 | * \brief Constructs and initializes a two-dimensional vector instance |
| 52 | * to (0.0, 0.0). |
| 53 | */ |
| 54 | inline Vector2() : x_(0.0f), y_(0.0f) { } |
| 55 | |
| 56 | /** |
| 57 | * \brief Constructs and initializes a two-dimensional vector from |
| 58 | * the specified xy-coordinates. |
| 59 | * \param x The x-coordinate of the two-dimensional |
| 60 | * vector. |
| 61 | * \param y The y-coordinate of the two-dimensional |
| 62 | * vector. |
| 63 | */ |
| 64 | inline Vector2(float x, float y) : x_(x), y_(y) { } |
| 65 | |
| 66 | inline Vector2(const Vector2 &vector) |
| 67 | { |
| 68 | x_ = vector.x(); |
| 69 | y_ = vector.y(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * \brief Returns the x-coordinate of this two-dimensional vector. |
| 74 | * \return The x-coordinate of the two-dimensional vector. |
| 75 | */ |
| 76 | inline float x() const { return x_; } |
| 77 | |
| 78 | /** |
| 79 | * \brief Returns the y-coordinate of this two-dimensional vector. |
| 80 | * \return The y-coordinate of the two-dimensional vector. |
| 81 | */ |
| 82 | inline float y() const { return y_; } |
| 83 | |
| 84 | /** |
| 85 | * \brief Computes the negation of this two-dimensional vector. |
| 86 | * \return The negation of this two-dimensional vector. |
| 87 | */ |
| 88 | inline Vector2 operator-() const |
| 89 | { |
| 90 | return Vector2(-x_, -y_); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * \brief Computes the dot product of this two-dimensional vector with |
| 95 | * the specified two-dimensional vector. |
| 96 | * \param vector The two-dimensional vector with which the |
| 97 | * dot product should be computed. |
| 98 | * \return The dot product of this two-dimensional vector with a |
| 99 | * specified two-dimensional vector. |
| 100 | */ |
| 101 | inline float operator*(const Vector2 &vector) const |
| 102 | { |
| 103 | return x_ * vector.x() + y_ * vector.y(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * \brief Computes the scalar multiplication of this |
| 108 | * two-dimensional vector with the specified scalar value. |
| 109 | * \param s The scalar value with which the scalar |
| 110 | * multiplication should be computed. |
| 111 | * \return The scalar multiplication of this two-dimensional vector |
| 112 | * with a specified scalar value. |
| 113 | */ |
| 114 | inline Vector2 operator*(float s) const |
| 115 | { |
| 116 | return Vector2(x_ * s, y_ * s); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * \brief Computes the scalar division of this two-dimensional vector |
| 121 | * with the specified scalar value. |
| 122 | * \param s The scalar value with which the scalar |
| 123 | * division should be computed. |
| 124 | * \return The scalar division of this two-dimensional vector with a |
| 125 | * specified scalar value. |
| 126 | */ |
| 127 | inline Vector2 operator/(float s) const |
| 128 | { |
| 129 | const float invS = 1.0f / s; |
| 130 | |
| 131 | return Vector2(x_ * invS, y_ * invS); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * \brief Computes the vector sum of this two-dimensional vector with |
| 136 | * the specified two-dimensional vector. |
| 137 | * \param vector The two-dimensional vector with which the |
| 138 | * vector sum should be computed. |
| 139 | * \return The vector sum of this two-dimensional vector with a |
| 140 | * specified two-dimensional vector. |
| 141 | */ |
| 142 | inline Vector2 operator+(const Vector2 &vector) const |
| 143 | { |
| 144 | return Vector2(x_ + vector.x(), y_ + vector.y()); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * \brief Computes the vector difference of this two-dimensional |
| 149 | * vector with the specified two-dimensional vector. |
| 150 | * \param vector The two-dimensional vector with which the |
| 151 | * vector difference should be computed. |
| 152 | * \return The vector difference of this two-dimensional vector with a |
| 153 | * specified two-dimensional vector. |
| 154 | */ |
| 155 | inline Vector2 operator-(const Vector2 &vector) const |
| 156 | { |
| 157 | return Vector2(x_ - vector.x(), y_ - vector.y()); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * \brief Tests this two-dimensional vector for equality with the |
| 162 | * specified two-dimensional vector. |
| 163 | * \param vector The two-dimensional vector with which to |
| 164 | * test for equality. |
| 165 | * \return True if the two-dimensional vectors are equal. |
| 166 | */ |
| 167 | inline bool operator==(const Vector2 &vector) const |
| 168 | { |
| 169 | return x_ == vector.x() && y_ == vector.y(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * \brief Tests this two-dimensional vector for inequality with the |
| 174 | * specified two-dimensional vector. |
| 175 | * \param vector The two-dimensional vector with which to |
| 176 | * test for inequality. |
| 177 | * \return True if the two-dimensional vectors are not equal. |
| 178 | */ |
| 179 | inline bool operator!=(const Vector2 &vector) const |
| 180 | { |
| 181 | return x_ != vector.x() || y_ != vector.y(); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * \brief Sets the value of this two-dimensional vector to the scalar |
| 186 | * multiplication of itself with the specified scalar value. |
| 187 | * \param s The scalar value with which the scalar |
| 188 | * multiplication should be computed. |
| 189 | * \return A reference to this two-dimensional vector. |
| 190 | */ |
| 191 | inline Vector2 &operator*=(float s) |
| 192 | { |
| 193 | x_ *= s; |
| 194 | y_ *= s; |
| 195 | |
| 196 | return *this; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * \brief Sets the value of this two-dimensional vector to the scalar |
| 201 | * division of itself with the specified scalar value. |
| 202 | * \param s The scalar value with which the scalar |
| 203 | * division should be computed. |
| 204 | * \return A reference to this two-dimensional vector. |
| 205 | */ |
| 206 | inline Vector2 &operator/=(float s) |
| 207 | { |
| 208 | const float invS = 1.0f / s; |
| 209 | x_ *= invS; |
| 210 | y_ *= invS; |
| 211 | |
| 212 | return *this; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * \brief Sets the value of this two-dimensional vector to the vector |
| 217 | * sum of itself with the specified two-dimensional vector. |
| 218 | * \param vector The two-dimensional vector with which the |
| 219 | * vector sum should be computed. |
| 220 | * \return A reference to this two-dimensional vector. |
| 221 | */ |
| 222 | inline Vector2 &operator+=(const Vector2 &vector) |
| 223 | { |
| 224 | x_ += vector.x(); |
| 225 | y_ += vector.y(); |
| 226 | |
| 227 | return *this; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * \brief Sets the value of this two-dimensional vector to the vector |
| 232 | * difference of itself with the specified two-dimensional |
| 233 | * vector. |
| 234 | * \param vector The two-dimensional vector with which the |
| 235 | * vector difference should be computed. |
| 236 | * \return A reference to this two-dimensional vector. |
| 237 | */ |
| 238 | inline Vector2 &operator-=(const Vector2 &vector) |
| 239 | { |
| 240 | x_ -= vector.x(); |
| 241 | y_ -= vector.y(); |
| 242 | |
| 243 | return *this; |
| 244 | } |
| 245 | |
| 246 | inline Vector2 &operator=(const Vector2 &vector) |
| 247 | { |
| 248 | x_ = vector.x(); |
| 249 | y_ = vector.y(); |
| 250 | |
| 251 | return *this; |
| 252 | } |
| 253 | |
| 254 | private: |
| 255 | float x_; |
| 256 | float y_; |
| 257 | }; |
| 258 | |
| 259 | /** |
| 260 | * \relates Vector2 |
| 261 | * \brief Computes the scalar multiplication of the specified |
| 262 | * two-dimensional vector with the specified scalar value. |
| 263 | * \param s The scalar value with which the scalar |
| 264 | * multiplication should be computed. |
| 265 | * \param vector The two-dimensional vector with which the scalar |
| 266 | * multiplication should be computed. |
| 267 | * \return The scalar multiplication of the two-dimensional vector with the |
| 268 | * scalar value. |
| 269 | */ |
| 270 | inline Vector2 operator*(float s, const Vector2 &vector) |
| 271 | { |
| 272 | return Vector2(s * vector.x(), s * vector.y()); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * \relates Vector2 |
| 277 | * \brief Inserts the specified two-dimensional vector into the specified |
| 278 | * output stream. |
| 279 | * \param os The output stream into which the two-dimensional |
| 280 | * vector should be inserted. |
| 281 | * \param vector The two-dimensional vector which to insert into |
| 282 | * the output stream. |
| 283 | * \return A reference to the output stream. |
| 284 | */ |
| 285 | inline std::ostream &operator<<(std::ostream &os, const Vector2 &vector) |
| 286 | { |
| 287 | os << "(" << vector.x() << "," << vector.y() << ")" ; |
| 288 | |
| 289 | return os; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * \relates Vector2 |
| 294 | * \brief Computes the length of a specified two-dimensional vector. |
| 295 | * \param vector The two-dimensional vector whose length is to be |
| 296 | * computed. |
| 297 | * \return The length of the two-dimensional vector. |
| 298 | */ |
| 299 | inline float abs(const Vector2 &vector) |
| 300 | { |
| 301 | return std::sqrt(vector * vector); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * \relates Vector2 |
| 306 | * \brief Computes the squared length of a specified two-dimensional |
| 307 | * vector. |
| 308 | * \param vector The two-dimensional vector whose squared length |
| 309 | * is to be computed. |
| 310 | * \return The squared length of the two-dimensional vector. |
| 311 | */ |
| 312 | inline float absSq(const Vector2 &vector) |
| 313 | { |
| 314 | return vector * vector; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * \relates Vector2 |
| 319 | * \brief Computes the determinant of a two-dimensional square matrix with |
| 320 | * rows consisting of the specified two-dimensional vectors. |
| 321 | * \param vector1 The top row of the two-dimensional square |
| 322 | * matrix. |
| 323 | * \param vector2 The bottom row of the two-dimensional square |
| 324 | * matrix. |
| 325 | * \return The determinant of the two-dimensional square matrix. |
| 326 | */ |
| 327 | inline float det(const Vector2 &vector1, const Vector2 &vector2) |
| 328 | { |
| 329 | return vector1.x() * vector2.y() - vector1.y() * vector2.x(); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * \relates Vector2 |
| 334 | * \brief Computes the normalization of the specified two-dimensional |
| 335 | * vector. |
| 336 | * \param vector The two-dimensional vector whose normalization |
| 337 | * is to be computed. |
| 338 | * \return The normalization of the two-dimensional vector. |
| 339 | */ |
| 340 | inline Vector2 normalize(const Vector2 &vector) |
| 341 | { |
| 342 | return vector / abs(vector); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | #endif /* RVO_VECTOR2_H_ */ |
| 347 | |