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