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 &) = default;
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 &) = default;
85
87 inline Vector operator * (const float inV2) const
88 {
89 Vector v;
90 for (uint r = 0; r < Rows; ++r)
91 v.mF32[r] = mF32[r] * inV2;
92 return v;
93 }
94
95 inline Vector & operator *= (const float inV2)
96 {
97 for (uint r = 0; r < Rows; ++r)
98 mF32[r] *= inV2;
99 return *this;
100 }
101
103 inline friend Vector operator * (const float inV1, const Vector &inV2)
104 {
105 return inV2 * inV1;
106 }
107
109 inline Vector operator / (float inV2) const
110 {
111 Vector v;
112 for (uint r = 0; r < Rows; ++r)
113 v.mF32[r] = mF32[r] / inV2;
114 return v;
115 }
116
117 inline Vector & operator /= (float inV2)
118 {
119 for (uint r = 0; r < Rows; ++r)
120 mF32[r] /= inV2;
121 return *this;
122 }
123
125 inline Vector operator + (const Vector &inV2) const
126 {
127 Vector v;
128 for (uint r = 0; r < Rows; ++r)
129 v.mF32[r] = mF32[r] + inV2.mF32[r];
130 return v;
131 }
132
133 inline Vector & operator += (const Vector &inV2)
134 {
135 for (uint r = 0; r < Rows; ++r)
136 mF32[r] += inV2.mF32[r];
137 return *this;
138 }
139
141 inline Vector operator - () const
142 {
143 Vector v;
144 for (uint r = 0; r < Rows; ++r)
145 v.mF32[r] = -mF32[r];
146 return v;
147 }
148
150 inline Vector operator - (const Vector &inV2) const
151 {
152 Vector v;
153 for (uint r = 0; r < Rows; ++r)
154 v.mF32[r] = mF32[r] - inV2.mF32[r];
155 return v;
156 }
157
158 inline Vector & operator -= (const Vector &inV2)
159 {
160 for (uint r = 0; r < Rows; ++r)
161 mF32[r] -= inV2.mF32[r];
162 return *this;
163 }
164
166 inline float Dot(const Vector &inV2) const
167 {
168 float dot = 0.0f;
169 for (uint r = 0; r < Rows; ++r)
170 dot += mF32[r] * inV2.mF32[r];
171 return dot;
172 }
173
175 inline float LengthSq() const
176 {
177 return Dot(*this);
178 }
179
181 inline float Length() const
182 {
183 return sqrt(LengthSq());
184 }
185
187 inline bool IsNormalized(float inToleranceSq = 1.0e-6f)
188 {
189 return abs(LengthSq() - 1.0f) <= inToleranceSq;
190 }
191
193 inline Vector Normalized() const
194 {
195 return *this / Length();
196 }
197
199 friend ostream & operator << (ostream &inStream, const Vector &inV)
200 {
201 inStream << "[";
202 for (uint i = 0; i < Rows - 1; ++i)
203 inStream << inV.mF32[i] << ", ";
204 inStream << inV.mF32[Rows - 1] << "]";
205 return inStream;
206 }
207
208 float mF32[Rows];
209};
210
unsigned int uint
Definition: Core.h:452
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition: DVec3.inl:451
#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:193
bool IsNormalized(float inToleranceSq=1.0e-6f)
Check if vector is normalized.
Definition: Vector.h:187
float Length() const
Length of vector.
Definition: Vector.h:181
uint GetRows() const
Dimensions.
Definition: Vector.h:19
float mF32[Rows]
Definition: Vector.h:208
Vector(const Vector &)=default
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:166
static Vector sZero()
Definition: Vector.h:28
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:175