JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
MinimalVec3.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright 2024 Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22#include "JPLSpatial/Core.h"
25
26#include <array>
27#include <cmath>
28#include <type_traits>
29#include <ostream>
30
31namespace JPL
32{
33 //======================================================================
37 {
38 union
39 {
40 std::array<float, 3> mVF;
41 struct
42 {
43 float X, Y, Z;
44 };
45 };
46
47 MinimalVec3() = default;
48 JPL_INLINE constexpr MinimalVec3(float x, float y, float z) noexcept : X(x), Y(y), Z(z) {}
49
50 [[nodiscard]] JPL_INLINE static constexpr MinimalVec3 Zero() noexcept { return MinimalVec3{ 0.0f, 0.0f, 0.0f }; }
51
52 [[nodiscard]] JPL_INLINE constexpr float operator [](uint32 coordinate) const { return mVF[coordinate]; }
53
54 [[nodiscard]] JPL_INLINE constexpr float LengthSquared() const noexcept { return X * X + Y * Y + Z * Z; }
55 [[nodiscard]] JPL_INLINE constexpr float Length() const noexcept { return Math::Sqrt(LengthSquared()); }
56
58 {
59 const float invLength = Math::InvSqrt(LengthSquared());
60 X *= invLength;
61 Y *= invLength;
62 Z *= invLength;
63
64 return *this;
65 }
66
67 JPL_INLINE constexpr MinimalVec3 SafeNormal(const MinimalVec3& fallbackDirection, float tolerance = JPL_FLOAT_EPS) const noexcept
68 {
69 const float length2 = LengthSquared();
71 {
72 return fallbackDirection;
73 }
74 else
75 {
76 const float invLength = Math::InvSqrt(length2);
77 return MinimalVec3{ X * invLength, Y * invLength, Z * invLength };
78 }
79 }
80
81 JPL_INLINE constexpr MinimalVec3 SafeNormal(float& outLength, const MinimalVec3& fallbackDirection, float tolerance = JPL_FLOAT_EPS) const noexcept
82 {
83 const float length2 = LengthSquared();
85 {
86 outLength = 0.0f;
87 return fallbackDirection;
88 }
89 else
90 {
92 const float invLength = 1.0f / outLength;
93 return MinimalVec3{ X * invLength, Y * invLength, Z * invLength };
94 }
95 }
96
97 [[nodiscard]] JPL_INLINE constexpr MinimalVec3 operator-() const { return MinimalVec3(-X, -Y, -Z); }
98 [[nodiscard]] JPL_INLINE constexpr MinimalVec3 operator-(const MinimalVec3& V) const { return MinimalVec3(X - V.X, Y - V.Y, Z - V.Z); }
99 [[nodiscard]] JPL_INLINE constexpr MinimalVec3 operator+(const MinimalVec3& V) const { return MinimalVec3(X + V.X, Y + V.Y, Z + V.Z); }
100
101 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
102 [[nodiscard]] JPL_INLINE constexpr MinimalVec3 operator*(Arg Scale) const noexcept { return MinimalVec3(X * (float)Scale, Y * (float)Scale, Z * (float)Scale); }
103 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
104 [[nodiscard]] JPL_INLINE constexpr MinimalVec3 operator/(Arg Scale) const noexcept { return MinimalVec3(X / (float)Scale, Y / (float)Scale, Z / (float)Scale); }
105
106 JPL_INLINE constexpr void operator+=(const MinimalVec3& V) noexcept { X += V.X; Y += V.Y; Z += V.Z; }
107 JPL_INLINE constexpr MinimalVec3& operator*=(const MinimalVec3 Other) { X *= Other.X; Y *= Other.Y; Z *= Other.Z; return *this; }
108
109 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
110 JPL_INLINE constexpr MinimalVec3& operator/=(Arg Scale) noexcept { X /= (float)Scale; Y /= (float)Scale; Z /= (float)Scale; return *this; }
111
112 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
113 JPL_INLINE constexpr MinimalVec3& operator*=(Arg Scale) noexcept { X *= (float)Scale; Y *= (float)Scale; Z *= (float)Scale; return *this; }
114
115 };
116
117 [[nodiscard]] JPL_INLINE constexpr bool operator==(const MinimalVec3& A, const MinimalVec3& B) noexcept{ return A.X == B.X && A.Y == B.Y && A.Z == B.Z; }
118
119 template<typename T> requires(std::is_arithmetic_v<T>)
120 [[nodiscard]] JPL_INLINE constexpr MinimalVec3 operator*(T Scale, const JPL::MinimalVec3& V) noexcept { return V.operator*(Scale); }
121 template<typename T> requires(std::is_arithmetic_v<T>)
122 [[nodiscard]] JPL_INLINE constexpr MinimalVec3 operator/(T Scale, const JPL::MinimalVec3& V) noexcept { return V.operator/(Scale); }
123
124 [[nodiscard]] JPL_INLINE constexpr MinimalVec3 operator*(const JPL::MinimalVec3& A, const JPL::MinimalVec3& B) noexcept { return { A.X * B.X, A.Y * B.Y, A.Z * B.Z }; }
125 [[nodiscard]] JPL_INLINE constexpr MinimalVec3 operator/(const JPL::MinimalVec3& A, const JPL::MinimalVec3& B) noexcept { return { A.X / B.X, A.Y / B.Y, A.Z / B.Z }; }
126
127 [[nodiscard]] static JPL_INLINE constexpr float LengthSquared(const MinimalVec3& V) noexcept { return V.LengthSquared(); }
128 [[nodiscard]] static JPL_INLINE constexpr float Length(const MinimalVec3& V) noexcept { return V.Length(); }
129 static JPL_INLINE constexpr MinimalVec3& Normalize(MinimalVec3& V) noexcept { return V.Normalize(); }
130 [[nodiscard]] static JPL_INLINE constexpr MinimalVec3 Normalized(const MinimalVec3& V) noexcept { return MinimalVec3(V).Normalize(); }
131 [[nodiscard]] static JPL_INLINE constexpr float DotProduct(const MinimalVec3& A, const MinimalVec3& B) noexcept{ return A.X * B.X + A.Y * B.Y + A.Z * B.Z; }
132 [[nodiscard]] static JPL_INLINE constexpr MinimalVec3 CrossProduct(const MinimalVec3& A, const MinimalVec3& B) noexcept
133 {
134 return MinimalVec3{
135 A.Y * B.Z - A.Z * B.Y,
136 A.Z * B.X - A.X * B.Z,
137 A.X * B.Y - A.Y * B.X
138 };
139 }
140
141 [[nodiscard]] static JPL_INLINE constexpr MinimalVec3 Abs(const MinimalVec3& V) noexcept { return { Math::Abs(V.X), Math::Abs(V.Y), Math::Abs(V.Z) }; }
142
143 // CVec3 interface
144 template<>
146 {
147 [[nodiscard]] static JPL_INLINE float GetX(const MinimalVec3& v) noexcept { return v.X; }
148 [[nodiscard]] static JPL_INLINE float GetY(const MinimalVec3& v) noexcept { return v.Y; }
149 [[nodiscard]] static JPL_INLINE float GetZ(const MinimalVec3& v) noexcept { return v.Z; }
150
151 static JPL_INLINE void SetX(MinimalVec3& v, float value) noexcept { v.X = value; }
152 static JPL_INLINE void SetY(MinimalVec3& v, float value) noexcept { v.Y = value; }
153 static JPL_INLINE void SetZ(MinimalVec3& v, float value) noexcept { v.Z = value; }
154 };
155
156 inline std::ostream& operator<<(std::ostream& os, const MinimalVec3& v) { os << "{ " << v.X << ", " << v.Y << ", " << v.Z << " }"; return os; }
157} // namespace JPL
JPL_INLINE constexpr T InvSqrt(T x) noexcept
Definition Math.h:283
JPL_INLINE constexpr T Sqrt(T x) noexcept
Definition Math.h:269
JPL_INLINE constexpr bool IsNearlyZero(T value, T errorTolerance=JPL_FLOAT_EPS_V< T >) noexcept
Definition Math.h:146
JPL_INLINE constexpr auto Abs(const T &value) noexcept
Standard abs is not constexpr in C++20.
Definition Math.h:87
Definition AcousticMaterial.h:36
JPL_INLINE constexpr Vec2 operator/(T Scale, const JPL::Vec2 &V) noexcept
Definition MinimalVec2.h:64
std::uint32_t uint32
Definition Core.h:311
JPL_INLINE constexpr bool operator==(const Vec2 &A, const Vec2 &B) noexcept
Definition MinimalVec2.h:59
JPL_INLINE constexpr Vec2 operator*(T Scale, const JPL::Vec2 &V) noexcept
Definition MinimalVec2.h:62
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
std::ostream & operator<<(std::ostream &os, const AcousticMaterial &v)
Definition AcousticMaterial.h:104
Definition MinimalVec3.h:37
JPL_INLINE constexpr MinimalVec3 operator-() const
Definition MinimalVec3.h:97
JPL_INLINE constexpr float Length() const noexcept
Definition MinimalVec3.h:55
JPL_INLINE constexpr MinimalVec3(float x, float y, float z) noexcept
Definition MinimalVec3.h:48
JPL_INLINE constexpr float LengthSquared() const noexcept
Definition MinimalVec3.h:54
JPL_INLINE constexpr MinimalVec3 SafeNormal(float &outLength, const MinimalVec3 &fallbackDirection, float tolerance=JPL_FLOAT_EPS) const noexcept
Definition MinimalVec3.h:81
JPL_INLINE constexpr MinimalVec3 operator-(const MinimalVec3 &V) const
Definition MinimalVec3.h:98
JPL_INLINE constexpr MinimalVec3 SafeNormal(const MinimalVec3 &fallbackDirection, float tolerance=JPL_FLOAT_EPS) const noexcept
Definition MinimalVec3.h:67
JPL_INLINE constexpr void operator+=(const MinimalVec3 &V) noexcept
Definition MinimalVec3.h:106
float Y
Definition MinimalVec3.h:43
JPL_INLINE constexpr MinimalVec3 & Normalize() noexcept
Definition MinimalVec3.h:57
JPL_INLINE constexpr MinimalVec3 operator+(const MinimalVec3 &V) const
Definition MinimalVec3.h:99
static JPL_INLINE constexpr MinimalVec3 Zero() noexcept
Definition MinimalVec3.h:50
float Z
Definition MinimalVec3.h:43
JPL_INLINE constexpr float operator[](uint32 coordinate) const
Definition MinimalVec3.h:52
JPL_INLINE constexpr MinimalVec3 & operator*=(const MinimalVec3 Other)
Definition MinimalVec3.h:107
MinimalVec3()=default
float X
Definition MinimalVec3.h:43
std::array< float, 3 > mVF
Definition MinimalVec3.h:40
static JPL_INLINE float GetY(const MinimalVec3 &v) noexcept
Definition MinimalVec3.h:148
static JPL_INLINE void SetZ(MinimalVec3 &v, float value) noexcept
Definition MinimalVec3.h:153
static JPL_INLINE void SetX(MinimalVec3 &v, float value) noexcept
Definition MinimalVec3.h:151
static JPL_INLINE float GetX(const MinimalVec3 &v) noexcept
Definition MinimalVec3.h:147
static JPL_INLINE void SetY(MinimalVec3 &v, float value) noexcept
Definition MinimalVec3.h:152
static JPL_INLINE float GetZ(const MinimalVec3 &v) noexcept
Definition MinimalVec3.h:149
Definition Vec3Traits.h:30