/*1* Vector3.h2* RVO2-3D Library3*4* Copyright 2008 University of North Carolina at Chapel Hill5*6* Licensed under the Apache License, Version 2.0 (the "License");7* you may not use this file except in compliance with the License.8* You may obtain a copy of the License at9*10* https://www.apache.org/licenses/LICENSE-2.011*12* Unless required by applicable law or agreed to in writing, software13* distributed under the License is distributed on an "AS IS" BASIS,14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15* See the License for the specific language governing permissions and16* limitations under the License.17*18* Please send all bug reports to <[email protected]>.19*20* The authors may be contacted via:21*22* Jur van den Berg, Stephen J. Guy, Jamie Snape, Ming C. Lin, Dinesh Manocha23* Dept. of Computer Science24* 201 S. Columbia St.25* Frederick P. Brooks, Jr. Computer Science Bldg.26* Chapel Hill, N.C. 27599-317527* United States of America28*29* <https://gamma.cs.unc.edu/RVO2/>30*/3132/**33* \file Vector3.h34* \brief Contains the Vector3 class.35*/36#ifndef RVO3D_VECTOR3_H_37#define RVO3D_VECTOR3_H_3839#include <cmath>40#include <cstddef>41#include <ostream>4243namespace RVO3D {44/**45* \brief Defines a three-dimensional vector.46*/47class Vector3 {48public:49/**50* \brief Constructs and initializes a three-dimensional vector instance to zero.51*/52inline Vector3()53{54val_[0] = 0.0f;55val_[1] = 0.0f;56val_[2] = 0.0f;57}5859/**60* \brief Constructs and initializes a three-dimensional vector from the specified three-dimensional vector.61* \param vector The three-dimensional vector containing the xyz-coordinates.62*/63inline Vector3(const Vector3 &vector)64{65val_[0] = vector[0];66val_[1] = vector[1];67val_[2] = vector[2];68}6970/**71* \brief Constructs and initializes a three-dimensional vector from the specified three-element array.72* \param val The three-element array containing the xyz-coordinates.73*/74inline explicit Vector3(const float val[3])75{76val_[0] = val[0];77val_[1] = val[1];78val_[2] = val[2];79}8081/**82* \brief Constructs and initializes a three-dimensional vector from the specified xyz-coordinates.83* \param x The x-coordinate of the three-dimensional vector.84* \param y The y-coordinate of the three-dimensional vector.85* \param z The z-coordinate of the three-dimensional vector.86*/87inline Vector3(float x, float y, float z)88{89val_[0] = x;90val_[1] = y;91val_[2] = z;92}9394/**95* \brief Returns the x-coordinate of this three-dimensional vector.96* \return The x-coordinate of the three-dimensional vector.97*/98inline float x() const { return val_[0]; }99100/**101* \brief Returns the y-coordinate of this three-dimensional vector.102* \return The y-coordinate of the three-dimensional vector.103*/104inline float y() const { return val_[1]; }105106/**107* \brief Returns the z-coordinate of this three-dimensional vector.108* \return The z-coordinate of the three-dimensional vector.109*/110inline float z() const { return val_[2]; }111112/**113* \brief Returns the specified coordinate of this three-dimensional vector.114* \param i The coordinate that should be returned (0 <= i < 3).115* \return The specified coordinate of the three-dimensional vector.116*/117inline float operator[](size_t i) const { return val_[i]; }118119/**120* \brief Returns a reference to the specified coordinate of this three-dimensional vector.121* \param i The coordinate to which a reference should be returned (0 <= i < 3).122* \return A reference to the specified coordinate of the three-dimensional vector.123*/124inline float &operator[](size_t i) { return val_[i]; }125126/**127* \brief Computes the negation of this three-dimensional vector.128* \return The negation of this three-dimensional vector.129*/130inline Vector3 operator-() const131{132return Vector3(-val_[0], -val_[1], -val_[2]);133}134135/**136* \brief Computes the dot product of this three-dimensional vector with the specified three-dimensional vector.137* \param vector The three-dimensional vector with which the dot product should be computed.138* \return The dot product of this three-dimensional vector with a specified three-dimensional vector.139*/140inline float operator*(const Vector3 &vector) const141{142return val_[0] * vector[0] + val_[1] * vector[1] + val_[2] * vector[2];143}144145/**146* \brief Computes the scalar multiplication of this three-dimensional vector with the specified scalar value.147* \param scalar The scalar value with which the scalar multiplication should be computed.148* \return The scalar multiplication of this three-dimensional vector with a specified scalar value.149*/150inline Vector3 operator*(float scalar) const151{152return Vector3(val_[0] * scalar, val_[1] * scalar, val_[2] * scalar);153}154155/**156* \brief Computes the scalar division of this three-dimensional vector with the specified scalar value.157* \param scalar The scalar value with which the scalar division should be computed.158* \return The scalar division of this three-dimensional vector with a specified scalar value.159*/160inline Vector3 operator/(float scalar) const161{162const float invScalar = 1.0f / scalar;163164return Vector3(val_[0] * invScalar, val_[1] * invScalar, val_[2] * invScalar);165}166167/**168* \brief Computes the vector sum of this three-dimensional vector with the specified three-dimensional vector.169* \param vector The three-dimensional vector with which the vector sum should be computed.170* \return The vector sum of this three-dimensional vector with a specified three-dimensional vector.171*/172inline Vector3 operator+(const Vector3 &vector) const173{174return Vector3(val_[0] + vector[0], val_[1] + vector[1], val_[2] + vector[2]);175}176177/**178* \brief Computes the vector difference of this three-dimensional vector with the specified three-dimensional vector.179* \param vector The three-dimensional vector with which the vector difference should be computed.180* \return The vector difference of this three-dimensional vector with a specified three-dimensional vector.181*/182inline Vector3 operator-(const Vector3 &vector) const183{184return Vector3(val_[0] - vector[0], val_[1] - vector[1], val_[2] - vector[2]);185}186187/**188* \brief Tests this three-dimensional vector for equality with the specified three-dimensional vector.189* \param vector The three-dimensional vector with which to test for equality.190* \return True if the three-dimensional vectors are equal.191*/192inline bool operator==(const Vector3 &vector) const193{194return val_[0] == vector[0] && val_[1] == vector[1] && val_[2] == vector[2];195}196197/**198* \brief Tests this three-dimensional vector for inequality with the specified three-dimensional vector.199* \param vector The three-dimensional vector with which to test for inequality.200* \return True if the three-dimensional vectors are not equal.201*/202inline bool operator!=(const Vector3 &vector) const203{204return val_[0] != vector[0] || val_[1] != vector[1] || val_[2] != vector[2];205}206207/**208* \brief Sets the value of this three-dimensional vector to the scalar multiplication of itself with the specified scalar value.209* \param scalar The scalar value with which the scalar multiplication should be computed.210* \return A reference to this three-dimensional vector.211*/212inline Vector3 &operator*=(float scalar)213{214val_[0] *= scalar;215val_[1] *= scalar;216val_[2] *= scalar;217218return *this;219}220221/**222* \brief Sets the value of this three-dimensional vector to the scalar division of itself with the specified scalar value.223* \param scalar The scalar value with which the scalar division should be computed.224* \return A reference to this three-dimensional vector.225*/226inline Vector3 &operator/=(float scalar)227{228const float invScalar = 1.0f / scalar;229230val_[0] *= invScalar;231val_[1] *= invScalar;232val_[2] *= invScalar;233234return *this;235}236237/**238* \brief Sets the value of this three-dimensional vector to the vector239* sum of itself with the specified three-dimensional vector.240* \param vector The three-dimensional vector with which the vector sum should be computed.241* \return A reference to this three-dimensional vector.242*/243inline Vector3 &operator+=(const Vector3 &vector)244{245val_[0] += vector[0];246val_[1] += vector[1];247val_[2] += vector[2];248249return *this;250}251252/**253* \brief Sets the value of this three-dimensional vector to the vector difference of itself with the specified three-dimensional vector.254* \param vector The three-dimensional vector with which the vector difference should be computed.255* \return A reference to this three-dimensional vector.256*/257inline Vector3 &operator-=(const Vector3 &vector)258{259val_[0] -= vector[0];260val_[1] -= vector[1];261val_[2] -= vector[2];262263return *this;264}265266inline Vector3 &operator=(const Vector3 &vector)267{268val_[0] = vector[0];269val_[1] = vector[1];270val_[2] = vector[2];271272return *this;273}274275private:276float val_[3];277};278279280/**281* \relates Vector3282* \brief Computes the scalar multiplication of the specified three-dimensional vector with the specified scalar value.283* \param scalar The scalar value with which the scalar multiplication should be computed.284* \param vector The three-dimensional vector with which the scalar multiplication should be computed.285* \return The scalar multiplication of the three-dimensional vector with the scalar value.286*/287inline Vector3 operator*(float scalar, const Vector3 &vector)288{289return Vector3(scalar * vector[0], scalar * vector[1], scalar * vector[2]);290}291292/**293* \relates Vector3294* \brief Computes the cross product of the specified three-dimensional vectors.295* \param vector1 The first vector with which the cross product should be computed.296* \param vector2 The second vector with which the cross product should be computed.297* \return The cross product of the two specified vectors.298*/299inline Vector3 cross(const Vector3 &vector1, const Vector3 &vector2)300{301return 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]);302}303304/**305* \relates Vector3306* \brief Inserts the specified three-dimensional vector into the specified output stream.307* \param os The output stream into which the three-dimensional vector should be inserted.308* \param vector The three-dimensional vector which to insert into the output stream.309* \return A reference to the output stream.310*/311inline std::ostream &operator<<(std::ostream &os, const Vector3 &vector)312{313os << "(" << vector[0] << "," << vector[1] << "," << vector[2] << ")";314315return os;316}317318/**319* \relates Vector3320* \brief Computes the length of a specified three-dimensional vector.321* \param vector The three-dimensional vector whose length is to be computed.322* \return The length of the three-dimensional vector.323*/324inline float abs(const Vector3 &vector)325{326return std::sqrt(vector * vector);327}328329/**330* \relates Vector3331* \brief Computes the squared length of a specified three-dimensional vector.332* \param vector The three-dimensional vector whose squared length is to be computed.333* \return The squared length of the three-dimensional vector.334*/335inline float absSq(const Vector3 &vector)336{337return vector * vector;338}339340/**341* \relates Vector3342* \brief Computes the normalization of the specified three-dimensional vector.343* \param vector The three-dimensional vector whose normalization is to be computed.344* \return The normalization of the three-dimensional vector.345*/346inline Vector3 normalize(const Vector3 &vector)347{348return vector / abs(vector);349}350}351352#endif353354355