72f1472 Fix missing <chrono> include 54634f0 Backporting some style changes from Python fea15c3 removed namespace Passer 753de2d Fixes 9674f68 Disabled test failing on MacOS 5b89234 Made it work on MacOS d0ba29e Updated LinearAlgebra ebfb4fb Merge commit '0b63a044eb84175cc922f3f1fe0ce039d101bf7b' 0b63a04 Updated documentation, cleanup a0082bd Add direction documentation 770ce32 Merge commit '2b5f5cd175b20fa01e04b696dfe0cfbe2c4ad9da' 2b5f5cd Removed shorthand Deg/Rad aliases... (see below) d212db2 Trying to fix specialization before instantiation error e445e5b Trying to fix specialization before instantiation error f47e504 Trying to fix specialization before instantiation error b877d4d Trying to fix specialization before instantiation error 06e8623 Trying to fix specialization before instantiation error e97aee9 Trying to fix specialization before instantiation error 0e00e5d Merge branch 'main' of http://gitlab.passervr.com/passer/cpp/linear-algebra 52de55d Trying to fix specialization before instantiation error 47ba024 Trying to fix specialization before instantiation error 2402030 Trying to fix specialization before instantiation error 4e3f253 Fixed gitignore 06c70e2 Merge branch 'Experimental' 6f30334 Updated PolarOf, removed default Polar type 94a3105 Cleanup 89a5711 Fixed typo in documentation 64b7f11 Removed default Angle type 58beb36 Completed AngleOf documentation 5f58a2c Updated documentation 97d937e Fixed (copilot) unit tests 585a3b0 Removed default Spherical type d3226fd Added copilot test documentation a7aa679 Added a performance test 2c57c3f Fixed direction for spherical 28ee225 Normalize Spherical at creation 800eb75 improved rounding of discrete angles 536d6ce Add Binary support 18756ba Correct axis on quaternion from swingtwist 06e42c5 Normalized swing twist edbb4b1 Removes Angle::deg90/180 because of issues 0a07b23 SwingTwist == and Angle 9406b57 SwingTwist::AngleAxis using Direction 06da9e5 Removed AngleAxis, Sperhical will be used instead 2e61e1b Fix unit test 54d0318 Extended unit tests (plus fixes) 8286c1c Minor improvements 95a6fb3 Improved Angle quality 9eca318 Fixed inverse 0ebfce4 Spherical direction 136e44e Fixed unit tests fb5cee8 Proper angle implementation (unit tests still fail) 92d5ef0 Normalizing directions 1ea65d5 Made toQuestion const f1c70c7 Fix unit tests 2ad6a5a Fix include 11259a9 Extend functionality 3363388 Extended AngleAxis & Direction 09e25a8 Add Asin and Atan a646e93 Add ACos 407e771 Add WithDistance 69bad8f Added degrees, radians, cos, sin, and tan functions 5c3dfa6 Adde degrees/readians and poc ACos 05c61a3 Add distance between 6a1d043 Added Angle comparison 6b3bcfc Another anglebetween fix b5a6330 Fix anglebetween b975aed Added angle and rotate functions f483439 Moved Polar/Spherical to template class completely e0ef43a Merge branch 'Experimental' of http://gitlab.passervr.com/passer/cpp/linear-algebra into Experimental b460bec Added scaling to SphericalOf<T> e10e010 Added Direction to library sources dedaa31 Fix unit tests 1da93ca Fix ToVector3 9c3503f Added SphericalOf 353cb1b Add conversion from Quaternion cf86ba8 Cleanup template classes ea6894e Fix generic template functions 1ce7234 Bug fixes 38ebb34 Added Direction to replace Axis c72bba4 Change Vector3::Angle return type to AngleOf<float> 18cf653 Added scaling support 51772a1 Added add/subtract for discrete angles 49c6740 Make Angle -> float conversion explicit 608c45c Fix precision error 4591aef Added Spherical16 48828e2 Fix int/float issues 86b7c21 Added Degrees inline function a4b0491 Fix unit tests b81b77b Extend AngleOf support c70c079 Add spherical subtract 2bad384 Added spherical addition a25a8be Fixed namespace issues f8009a7 Updated namespace 91c7b20 Fix gitlab test runner 78468e3 Renamed VectorAlgebra project to LinearAlgebra 47a6368 Fix Vector3 ee62cba XYZ deprecation 62d7439 Extend ulitity functions 95713c8 Cleanup, added utility functions f7d9d97 Cleanup 66e47d7 Cleanup Vector2 and Polar e922a01 fix == operator 5693097 Initial subtractop test c411b43 Textual improvement 0c54276 equality support b1e34b6 Improved unit tests 6ddb074 Improved unit tests 5489b3c Fix test 3d971c1 Improve namespace 791bd78 namespace improvement 9bea94f Use Angle type for Polar 8a2ce21 Swapped polar constructor param order e5f8d73 namespace fix 8e7a8c6 Fix namespaces 64ca768 normalize toAngleAxis result 4385bef Add angle-axis 4b07328 Add conversion from Vector3 fe12c99 Fix Spherical reference c274c3e Add conversion from Spherical 66ff721 Fixed wrong conversion to short 264bcbc Added 32 bit angle f190dd7 Merge branch 'DiscreteAngle' into Experimental ae55a24 Further vector3 conversion fixes ec0cc47 Fix spherical to vector 89b5da8 Fix ambiguity e62fa96 Fix specialization again 430344a Fix specialization error 3e5ede0 Removed old file e62bacc First Discrete Angle functions 87d2c11 Set strict warnings 5141766 Add spherical.toVector3 test (and fixes) 8e3b9e2 Fix Polar test 7b85556 Addedfirst Polar test 395f82d Fix Spherical Unit test 0cbef79 Add first Spherical Unit test dc601a1 Removed Vector2.tofactor 159bdae Added spherical.tovector (but is is buggy) 273ebae Fixes 0468031 Bug fix 3714507 Implemented templated angles 69e7ee1 Added DiscreteAgle 2cbf259 Make negation virtual override 2b50bab Add negation for DiscreteAngle de3a647 Updated negation 254bb27 Added negation bd04285 Make superclass accessable 2bb0009 Initial implementation git-subtree-dir: LinearAlgebra git-subtree-split: 72f1472f2e1d2c9a1fe002460e47606b9aeed548
233 lines
8.4 KiB
C++
233 lines
8.4 KiB
C++
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0.If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
|
|
|
|
#ifndef VECTOR3_H
|
|
#define VECTOR3_H
|
|
|
|
#include "Vector2.h"
|
|
|
|
extern "C" {
|
|
/// <summary>
|
|
/// 3-dimensional Vector representation (C-style)
|
|
/// </summary>
|
|
/// This is a C-style implementation
|
|
/// This uses the right-handed coordinate system.
|
|
typedef struct Vec3 {
|
|
protected:
|
|
/// <summary>
|
|
/// The right axis of the vector
|
|
/// </summary>
|
|
float x;
|
|
/// <summary>
|
|
/// The upward axis of the vector
|
|
/// </summary>
|
|
float y;
|
|
/// <summary>
|
|
/// The forward axis of the vector
|
|
/// </summary>
|
|
float z;
|
|
|
|
} Vec3;
|
|
}
|
|
|
|
namespace Passer {
|
|
namespace LinearAlgebra {
|
|
|
|
template <typename T> class SphericalOf;
|
|
|
|
/// @brief A 3-dimensional vector
|
|
/// @remark This uses a right-handed carthesian coordinate system.
|
|
/// @note This implementation intentionally avoids the use of x, y and z values.
|
|
struct Vector3 : Vec3 {
|
|
friend struct Vec3;
|
|
|
|
public:
|
|
/// @brief A new 3-dimensional zero vector
|
|
Vector3();
|
|
/// @brief A new 3-dimensional vector
|
|
/// @param right The distance in the right direction in meters
|
|
/// @param up The distance in the upward direction in meters
|
|
/// @param forward The distance in the forward direction in meters
|
|
Vector3(float right, float up, float forward);
|
|
/// @brief Convert a 2-dimenstional vector to a 3-dimensional vector
|
|
/// @param v The vector to convert
|
|
Vector3(Vector2 v);
|
|
/// @brief Convert vector in spherical coordinates to 3d carthesian
|
|
/// coordinates
|
|
/// @param v The vector to convert
|
|
Vector3(SphericalOf<float> v);
|
|
|
|
/// @brief Vector3 destructor
|
|
~Vector3();
|
|
|
|
/// @brief A vector with zero for all axis
|
|
const static Vector3 zero;
|
|
/// @brief A vector with one for all axis
|
|
const static Vector3 one;
|
|
/// @brief A normalized forward-oriented vector
|
|
const static Vector3 forward;
|
|
/// @brief A normalized back-oriented vector
|
|
const static Vector3 back;
|
|
/// @brief A normalized right-oriented vector
|
|
const static Vector3 right;
|
|
/// @brief A normalized left-oriented vector
|
|
const static Vector3 left;
|
|
/// @brief A normalized up-oriented vector
|
|
const static Vector3 up;
|
|
/// @brief A normalized down-oriented vector
|
|
const static Vector3 down;
|
|
|
|
// Access functions which are intended to replace the use of XYZ
|
|
inline float Forward() const { return z; };
|
|
inline float Up() const { return y; };
|
|
inline float Right() const { return x; };
|
|
|
|
/// @brief Check if this vector to the given vector
|
|
/// @param v The vector to check against
|
|
/// @return true if it is identical to the given vector
|
|
/// @note This uses float comparison to check equality which may have strange
|
|
/// effects. Equality on floats should be avoided.
|
|
bool operator==(const Vector3 &v) const;
|
|
|
|
/// @brief The vector length
|
|
/// @param v The vector for which you need the length
|
|
/// @return The vector length
|
|
static float Magnitude(const Vector3 &v);
|
|
/// @brief The vector length
|
|
/// @return The vector length
|
|
float magnitude() const;
|
|
/// @brief The squared vector length
|
|
/// @param v The vector for which you need the length
|
|
/// @return The squared vector length
|
|
/// @remark The squared length is computationally simpler than the real
|
|
/// length. Think of Pythagoras A^2 + B^2 = C^2. This leaves out the
|
|
/// calculation of the squared root of C.
|
|
static float SqrMagnitude(const Vector3 &v);
|
|
/// @brief The squared vector length
|
|
/// @return The squared vector length
|
|
/// @remark The squared length is computationally simpler than the real
|
|
/// length. Think of Pythagoras A^2 + B^2 = C^2. This leaves out the
|
|
/// calculation of the squared root of C.
|
|
float sqrMagnitude() const;
|
|
|
|
/// @brief Convert the vector to a length of 1
|
|
/// @param v The vector to convert
|
|
/// @return The vector normalized to a length of 1
|
|
static Vector3 Normalize(const Vector3 &v);
|
|
/// @brief Convert the vector to a length of 1
|
|
/// @return The vector normalized to a length of 1
|
|
Vector3 normalized() const;
|
|
|
|
/// @brief Negate te vector such that it points in the opposite direction
|
|
/// @return The negated vector
|
|
Vector3 operator-() const;
|
|
|
|
/// @brief Subtract a vector from this vector
|
|
/// @param v The vector to subtract from this vector
|
|
/// @return The result of this subtraction
|
|
Vector3 operator-(const Vector3 &v) const;
|
|
Vector3 operator-=(const Vector3 &v);
|
|
/// @brief Add a vector to this vector
|
|
/// @param v The vector to add to this vector
|
|
/// @return The result of the addition
|
|
Vector3 operator+(const Vector3 &v) const;
|
|
Vector3 operator+=(const Vector3 &v);
|
|
|
|
/// @brief Scale the vector using another vector
|
|
/// @param v1 The vector to scale
|
|
/// @param v2 A vector with the scaling factors
|
|
/// @return The scaled vector
|
|
/// @remark Each component of the vector v1 will be multiplied with the
|
|
/// matching component from the scaling vector v2.
|
|
static Vector3 Scale(const Vector3 &v1, const Vector3 &v2);
|
|
/// @brief Scale the vector uniformly up
|
|
/// @param f The scaling factor
|
|
/// @return The scaled vector
|
|
/// @remark Each component of the vector will be multipled with the same
|
|
/// factor f.
|
|
friend Vector3 operator*(const Vector3 &v, float f) {
|
|
return Vector3(v.x * f, v.y * f, v.z * f);
|
|
}
|
|
friend Vector3 operator*(float f, const Vector3 &v) {
|
|
// return Vector3(f * v.x, f * v.y, f * v.z);
|
|
return Vector3(v.x * f, v.y * f, v.z * f);
|
|
}
|
|
Vector3 operator*=(float f);
|
|
/// @brief Scale the vector uniformly down
|
|
/// @param f The scaling factor
|
|
/// @return The scaled vector
|
|
/// @remark Each componet of the vector will be divided by the same factor.
|
|
friend Vector3 operator/(const Vector3 &v, float f) {
|
|
return Vector3(v.x / f, v.y / f, v.z / f);
|
|
}
|
|
friend Vector3 operator/(float f, const Vector3 &v) {
|
|
// return Vector3(f / v.x, f / v.y, f / v.z);
|
|
return Vector3(v.x / f, v.y / f, v.z / f);
|
|
}
|
|
Vector3 operator/=(float f);
|
|
|
|
/// @brief The distance between two vectors
|
|
/// @param v1 The first vector
|
|
/// @param v2 The second vector
|
|
/// @return The distance between the two vectors
|
|
static float Distance(const Vector3 &v1, const Vector3 &v2);
|
|
|
|
/// @brief The dot product of two vectors
|
|
/// @param v1 The first vector
|
|
/// @param v2 The second vector
|
|
/// @return The dot product of the two vectors
|
|
static float Dot(const Vector3 &v1, const Vector3 &v2);
|
|
|
|
/// @brief The cross product of two vectors
|
|
/// @param v1 The first vector
|
|
/// @param v2 The second vector
|
|
/// @return The cross product of the two vectors
|
|
static Vector3 Cross(const Vector3 &v1, const Vector3 &v2);
|
|
|
|
/// @brief Project the vector on another vector
|
|
/// @param v The vector to project
|
|
/// @param n The normal vecto to project on
|
|
/// @return The projected vector
|
|
static Vector3 Project(const Vector3 &v, const Vector3 &n);
|
|
/// @brief Project the vector on a plane defined by a normal orthogonal to the
|
|
/// plane.
|
|
/// @param v The vector to project
|
|
/// @param n The normal of the plane to project on
|
|
/// @return Teh projected vector
|
|
static Vector3 ProjectOnPlane(const Vector3 &v, const Vector3 &n);
|
|
|
|
/// @brief The angle between two vectors
|
|
/// @param v1 The first vector
|
|
/// @param v2 The second vector
|
|
/// @return The angle between the two vectors
|
|
/// @remark This reterns an unsigned angle which is the shortest distance
|
|
/// between the two vectors. Use Vector3::SignedAngle if a signed angle is
|
|
/// needed.
|
|
static AngleOf<float> Angle(const Vector3 &v1, const Vector3 &v2);
|
|
/// @brief The signed angle between two vectors
|
|
/// @param v1 The starting vector
|
|
/// @param v2 The ending vector
|
|
/// @param axis The axis to rotate around
|
|
/// @return The signed angle between the two vectors
|
|
static AngleOf<float> SignedAngle(const Vector3 &v1, const Vector3 &v2,
|
|
const Vector3 &axis);
|
|
|
|
/// @brief Lerp (linear interpolation) between two vectors
|
|
/// @param v1 The starting vector
|
|
/// @param v2 The ending vector
|
|
/// @param f The interpolation distance
|
|
/// @return The lerped vector
|
|
/// @remark The factor f is unclamped. Value 0 matches the vector *v1*, Value
|
|
/// 1 matches vector *v2*. Value -1 is vector *v1* minus the difference
|
|
/// between *v1* and *v2* etc.
|
|
static Vector3 Lerp(const Vector3 &v1, const Vector3 &v2, float f);
|
|
};
|
|
|
|
} // namespace LinearAlgebra
|
|
} // namespace Passer
|
|
using namespace Passer::LinearAlgebra;
|
|
|
|
#include "Spherical.h"
|
|
|
|
#endif |