Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Vector.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
10template <uint Rows>
11class [[nodiscard]] Vector
12{
13public:
15 inline Vector() = default;
16 inline Vector(const Vector &inRHS) { *this = inRHS; }
17
19 inline uint GetRows() const { return Rows; }
20
22 inline void SetZero()
23 {
24 for (uint r = 0; r < Rows; ++r)
25 mF32[r] = 0.0f;
26 }
27
28 inline static Vector sZero() { Vector v; v.SetZero(); return v; }
29
31 template <class OtherVector>
32 void CopyPart(const OtherVector &inV, uint inSourceRow, uint inNumRows, uint inDestRow)
33 {
34 for (uint r = 0; r < inNumRows; ++r)
35 mF32[inDestRow + r] = inV[inSourceRow + r];
36 }
37
39 inline float operator [] (uint inCoordinate) const
40 {
41 JPH_ASSERT(inCoordinate < Rows);
42 return mF32[inCoordinate];
43 }
44
45 inline float & operator [] (uint inCoordinate)
46 {
47 JPH_ASSERT(inCoordinate < Rows);
48 return mF32[inCoordinate];
49 }
50
52 inline bool operator == (const Vector &inV2) const
53 {
54 for (uint r = 0; r < Rows; ++r)
55 if (mF32[r] != inV2.mF32[r])
56 return false;
57 return true;
58 }
59
60 inline bool operator != (const Vector &inV2) const
61 {
62 for (uint r = 0; r < Rows; ++r)
63 if (mF32[r] != inV2.mF32[r])
64 return true;
65 return false;
66 }
67
69 inline bool IsZero() const
70 {
71 for (uint r = 0; r < Rows; ++r)
72 if (mF32[r] != 0.0f)
73 return false;
74 return true;
75 }
76
78 inline bool IsClose(const Vector &inV2, float inMaxDistSq = 1.0e-12f)
79 {
80 return (inV2 - *this).LengthSq() <= inMaxDistSq;
81 }
82
84 inline Vector & operator = (const Vector &inV2)
85 {
86 for (uint r = 0; r < Rows; ++r)
87 mF32[r] = inV2.mF32[r];
88 return *this;
89 }
90
92 inline Vector operator * (const float inV2) const
93 {
94 Vector v;
95 for (uint r = 0; r < Rows; ++r)
96 v.mF32[r] = mF32[r] * inV2;
97 return v;
98 }
99
100 inline Vector & operator *= (const float inV2)
101 {
102 for (uint r = 0; r < Rows; ++r)
103 mF32[r] *= inV2;
104 return *this;
105 }
106
108 inline friend Vector operator * (const float inV1, const Vector &inV2)
109 {
110 return inV2 * inV1;
111 }
112
114 inline Vector operator / (float inV2) const
115 {
116 Vector v;
117 for (uint r = 0; r < Rows; ++r)
118 v.mF32[r] = mF32[r] / inV2;
119 return v;
120 }
121
122 inline Vector & operator /= (float inV2)
123 {
124 for (uint r = 0; r < Rows; ++r)
125 mF32[r] /= inV2;
126 return *this;
127 }
128
130 inline Vector operator + (const Vector &inV2) const
131 {
132 Vector v;
133 for (uint r = 0; r < Rows; ++r)
134 v.mF32[r] = mF32[r] + inV2.mF32[r];
135 return v;
136 }
137
138 inline Vector & operator += (const Vector &inV2)
139 {
140 for (uint r = 0; r < Rows; ++r)
141 mF32[r] += inV2.mF32[r];
142 return *this;
143 }
144
146 inline Vector operator - () const
147 {
148 Vector v;
149 for (uint r = 0; r < Rows; ++r)
150 v.mF32[r] = -mF32[r];
151 return v;
152 }
153
155 inline Vector operator - (const Vector &inV2) const
156 {
157 Vector v;
158 for (uint r = 0; r < Rows; ++r)
159 v.mF32[r] = mF32[r] - inV2.mF32[r];
160 return v;
161 }
162
163 inline Vector & operator -= (const Vector &inV2)
164 {
165 for (uint r = 0; r < Rows; ++r)
166 mF32[r] -= inV2.mF32[r];
167 return *this;
168 }
169
171 inline float Dot(const Vector &inV2) const
172 {
173 float dot = 0.0f;
174 for (uint r = 0; r < Rows; ++r)
175 dot += mF32[r] * inV2.mF32[r];
176 return dot;
177 }
178
180 inline float LengthSq() const
181 {
182 return Dot(*this);
183 }
184
186 inline float Length() const
187 {
188 return sqrt(LengthSq());
189 }
190
192 inline bool IsNormalized(float inToleranceSq = 1.0e-6f)
193 {
194 return abs(LengthSq() - 1.0f) <= inToleranceSq;
195 }
196
198 inline Vector Normalized() const
199 {
200 return *this / Length();
201 }
202
204 friend ostream & operator << (ostream &inStream, const Vector &inV)
205 {
206 inStream << "[";
207 for (uint i = 0; i < Rows - 1; ++i)
208 inStream << inV.mF32[i] << ", ";
209 inStream << inV.mF32[Rows - 1] << "]";
210 return inStream;
211 }
212
213 float mF32[Rows];
214};
215
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition: DVec3.inl:447
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
Templatized vector class.
Definition: Vector.h:12
void SetZero()
Vector with all zeros.
Definition: Vector.h:22
Vector Normalized() const
Normalize vector.
Definition: Vector.h:198
bool IsNormalized(float inToleranceSq=1.0e-6f)
Check if vector is normalized.
Definition: Vector.h:192
float Length() const
Length of vector.
Definition: Vector.h:186
uint GetRows() const
Dimensions.
Definition: Vector.h:19
float mF32[Rows]
Definition: Vector.h:213
bool IsZero() const
Test if vector consists of all zeros.
Definition: Vector.h:69
void CopyPart(const OtherVector &inV, uint inSourceRow, uint inNumRows, uint inDestRow)
Copy a (part) of another vector into this vector.
Definition: Vector.h:32
Vector()=default
Constructor.
float Dot(const Vector &inV2) const
Dot product.
Definition: Vector.h:171
static Vector sZero()
Definition: Vector.h:28
Vector(const Vector &inRHS)
Definition: Vector.h:16
bool IsClose(const Vector &inV2, float inMaxDistSq=1.0e-12f)
Test if two vectors are close to each other.
Definition: Vector.h:78
float LengthSq() const
Squared length of vector.
Definition: Vector.h:180