Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Vector3.cpp
1/*
2Copyright (C) 2015-2019,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#include "aeongames/Vector3.hpp"
17#include "aeongames/Plane.hpp"
18#include "3DMath.h"
19
20namespace AeonGames
21{
23 {
24 memset ( mVector, 0, sizeof ( float ) * 3 );
25 }
26
27 Vector3::Vector3 ( const float* const aVector )
28 {
29 SetVector3 ( aVector );
30 }
31
32 void Vector3::SetVector3 ( const float* const aVector )
33 {
34 memcpy ( mVector, aVector, sizeof ( float ) * 3 );
35 }
36
37 Vector3::Vector3 ( const void* const aVector, const uint32_t aStride )
38 {
39 SetVector3 ( aVector, aStride );
40 }
41
42 void Vector3::SetVector3 ( const void* const aVector, const uint32_t aStride )
43 {
44 mVector[0] = * ( reinterpret_cast<const float*> ( aVector ) );
45 mVector[1] = * ( reinterpret_cast<const float*> ( reinterpret_cast<const uint8_t*> ( aVector ) + ( aStride ) ) );
46 mVector[2] = * ( reinterpret_cast<const float*> ( reinterpret_cast<const uint8_t*> ( aVector ) + ( aStride * 2 ) ) );
47 }
48
50 {
51 return Dot ( *this, *this );
52 }
53
54 float Vector3::GetLenght() const
55 {
56 return sqrtf ( GetLenghtSquared() );
57 }
58
60 {
61 return
62 ( mVector[0] > mVector[1] ) ?
63 ( ( mVector[0] > mVector[2] ) ? 0 : 2 ) :
64 ( ( mVector[2] > mVector[1] ) ? 2 : 1 );
65 }
66
68 {
69 return
70 ( mVector[0] < mVector[1] ) ?
71 ( ( mVector[0] < mVector[2] ) ? 0 : 2 ) :
72 ( ( mVector[2] < mVector[1] ) ? 2 : 1 );
73 }
74
76 {
77 return mVector[GetMaxAxisIndex()];
78 }
79
81 {
82 return mVector[GetMinAxisIndex()];
83 }
84
85 float Vector3::GetDistanceToPlane ( const Plane & aPlane ) const
86 {
87 return Dot ( aPlane.GetNormal(), *this ) - aPlane.GetDistance();
88 }
89
90 Vector3::Vector3 ( float aX, float aY, float aZ )
91 {
92 mVector[0] = aX;
93 mVector[1] = aY;
94 mVector[2] = aZ;
95 }
96
97 void Vector3::Get ( float* aData ) const
98 {
99 memcpy ( aData, mVector, sizeof ( float ) * 3 );
100 }
101
102 const float* const Vector3::GetVector3() const
103 {
104 return mVector;
105 }
106
107 const float& Vector3::GetX() const
108 {
109 return mVector[0];
110 }
111
112 const float& Vector3::GetY() const
113 {
114 return mVector[1];
115 }
116
117
118 const float& Vector3::GetZ() const
119 {
120 return mVector[2];
121 }
122
123 const Vector3 Cross ( const Vector3& aLhs, const Vector3& aRhs )
124 {
125 return Vector3 (
126 ( aLhs.GetY() * aRhs.GetZ() ) - ( aLhs.GetZ() * aRhs.GetY() ),
127 ( aLhs.GetZ() * aRhs.GetX() ) - ( aLhs.GetX() * aRhs.GetZ() ),
128 ( aLhs.GetX() * aRhs.GetY() ) - ( aLhs.GetY() * aRhs.GetX() ) );
129 }
130
131 float Vector3::operator [] ( const size_t aIndex ) const
132 {
133 assert ( aIndex < 3 );
134 return mVector[aIndex];
135 }
136
137 float& Vector3::operator [] ( const size_t aIndex )
138 {
139 assert ( aIndex < 3 );
140 return mVector[aIndex];
141 }
142
143 Vector3& Vector3::operator= ( const float* aLhs )
144 {
145 SetVector3 ( aLhs );
146 return *this;
147 }
148
150 {
151 mVector[0] -= aLhs.mVector[0];
152 mVector[1] -= aLhs.mVector[1];
153 mVector[2] -= aLhs.mVector[2];
154 return *this;
155 }
156
157
158 const Vector3 operator- ( const Vector3& aLhs, const Vector3& aRhs )
159 {
160 /* Here Matrix4x4(lhs) *MAY* mean cast away constness
161 rather tan create a temporary object in some compilers,
162 we want the temporary, NOT the cast.*/
163 return Vector3 ( aLhs ) -= aRhs;
164 }
165
167 {
168 mVector[0] += aLhs.mVector[0];
169 mVector[1] += aLhs.mVector[1];
170 mVector[2] += aLhs.mVector[2];
171 return *this;
172 }
173 const Vector3 operator+ ( const Vector3& aLhs, const Vector3& aRhs )
174 {
175 /* Here Matrix4x4(lhs) *MAY* mean cast away constness
176 rather tan create a temporary object in some compilers,
177 we want the temporary, NOT the cast.*/
178 return Vector3 ( aLhs ) += aRhs;
179 }
180
181 Vector3& Vector3::operator *= ( const float aLhs )
182 {
183 mVector[0] *= aLhs;
184 mVector[1] *= aLhs;
185 mVector[2] *= aLhs;
186 return *this;
187 }
188
190 {
191 mVector[0] *= aLhs[0];
192 mVector[1] *= aLhs[1];
193 mVector[2] *= aLhs[2];
194 return *this;
195 }
196
197 Vector3 & Vector3::operator/= ( const float aLhs )
198 {
199 mVector[0] /= aLhs;
200 mVector[1] /= aLhs;
201 mVector[2] /= aLhs;
202 return *this;
203 }
204
205 bool Vector3::IsZero() const
206 {
207 return mVector[0] == 0.0f && mVector[1] == 0.0f && mVector[2] == 0.0f;
208 }
209
210 const Vector3 operator* ( const Vector3& aLhs, const float aRhs )
211 {
212 /* Here Matrix4x4(lhs) *MAY* mean cast away constness
213 rather tan create a temporary object in some compilers,
214 we want the temporary, NOT the cast.*/
215 return Vector3 ( aLhs ) *= aRhs;
216 }
217
218 const Vector3 operator/ ( const Vector3 & aLhs, const float aRhs )
219 {
220 /* Here Matrix4x4(lhs) *MAY* mean cast away constness
221 rather tan create a temporary object in some compilers,
222 we want the temporary, NOT the cast.*/
223 return Vector3 ( aLhs ) /= aRhs;
224 }
225
226 const Vector3 operator* ( const float aLhs, const Vector3& aRhs )
227 {
228 /* Here Matrix4x4(lhs) *MAY* mean cast away constness
229 rather tan create a temporary object in some compilers,
230 we want the temporary, NOT the cast.*/
231 return Vector3 ( aRhs ) *= aLhs;
232 }
233
234 const Vector3 operator* ( const Vector3 & aLhs, const Vector3 & aRhs )
235 {
236 return Vector3 ( aRhs ) *= aLhs;
237 }
238
239 bool operator!= ( const Vector3& aLhs, const Vector3& aRhs )
240 {
241 return memcmp ( aLhs.GetVector3(), aRhs.GetVector3(), sizeof ( float ) * 3 ) != 0;
242 }
243
244 bool operator== ( const Vector3 & aLhs, const Vector3 & aRhs )
245 {
246 return memcmp ( aLhs.GetVector3(), aRhs.GetVector3(), sizeof ( float ) * 3 ) == 0;
247 }
248
249 const float Dot ( const Vector3& aLhs, const Vector3& aRhs )
250 {
251 return
252 aLhs.GetX() * aRhs.GetX() +
253 aLhs.GetY() * aRhs.GetY() +
254 aLhs.GetZ() * aRhs.GetZ();
255 }
256
257 const Vector3 Normalize ( const Vector3& aVector )
258 {
259 float magnitude = sqrtf ( Dot ( aVector, aVector ) );
260 assert ( magnitude != 0.0f );
261 return Vector3 (
262 aVector.GetX() / magnitude,
263 aVector.GetY() / magnitude,
264 aVector.GetZ() / magnitude );
265 }
266 const Vector3 Abs ( const Vector3 & aVector )
267 {
268 return Vector3
269 {
270 std::abs ( aVector[0] ),
271 std::abs ( aVector[1] ),
272 std::abs ( aVector[2] )
273 };
274 }
275 const Vector3 Spline ( const Vector3 & p0, const Vector3 & p1, const Vector3 & p2, const Vector3 & p3, double interpolation )
276 {
277 double i2 = interpolation * interpolation;
278 double i3 = i2 * interpolation;
279 double t0[3] =
280 {
281 ( p2[0] - p0[0] ) / 2.0,
282 ( p2[1] - p0[1] ) / 2.0,
283 ( p2[2] - p0[2] ) / 2.0
284 };
285
286 double t1[3] =
287 {
288 ( p3[0] - p1[0] ) / 2.0,
289 ( p3[1] - p1[1] ) / 2.0,
290 ( p3[2] - p1[2] ) / 2.0
291 };
292 return Vector3
293 {
294 static_cast<float> ( ( 2 * i3 - 3 * i2 + 1 ) * p1[0] + ( -2 * i3 + 3 * i2 ) * p2[0] + ( i3 - 2 * i2 + interpolation ) * t0[0] + ( i3 - i2 ) * t1[0] ),
295 static_cast<float> ( ( 2 * i3 - 3 * i2 + 1 ) * p1[1] + ( -2 * i3 + 3 * i2 ) * p2[1] + ( i3 - 2 * i2 + interpolation ) * t0[1] + ( i3 - i2 ) * t1[1] ),
296 static_cast<float> ( ( 2 * i3 - 3 * i2 + 1 ) * p1[2] + ( -2 * i3 + 3 * i2 ) * p2[2] + ( i3 - 2 * i2 + interpolation ) * t0[2] + ( i3 - i2 ) * t1[2] )
297 };
298 }
299}
Inline functions related to 3D Math.
Header for the plane class.
Header for the 3D vector class.
Plane class.
Definition Plane.hpp:33
DLL const float & GetDistance() const
Get the distance from the origin to the plane.
Definition Plane.cpp:45
DLL const Vector3 & GetNormal() const
Get the plane normal vector.
Definition Plane.cpp:40
3D vector class.
Definition Vector3.hpp:32
DLL float GetLenghtSquared() const
Get the squared length of the vector.
Definition Vector3.cpp:49
DLL const float *const GetVector3() const
Get a pointer to the internal vector data.
Definition Vector3.cpp:102
DLL float operator[](const size_t aIndex) const
Access a component by index (const).
Definition Vector3.cpp:131
DLL float GetMaxAxisLenght() const
Get the absolute value of the largest axis component.
Definition Vector3.cpp:75
DLL const float & GetZ() const
Get the Z component.
Definition Vector3.cpp:118
DLL float GetMinAxisLenght() const
Get the absolute value of the smallest axis component.
Definition Vector3.cpp:80
DLL size_t GetMaxAxisIndex() const
Get the index of the axis with the largest absolute value.
Definition Vector3.cpp:59
DLL float GetDistanceToPlane(const Plane &aPlane) const
Get the signed distance from this point to a plane.
Definition Vector3.cpp:85
DLL float GetLenght() const
Get the length of the vector.
Definition Vector3.cpp:54
DLL Vector3 & operator=(const float *aLhs)
Assign from a raw float array.
Definition Vector3.cpp:143
DLL Vector3 & operator*=(const float aLhs)
Scale this vector by a scalar.
Definition Vector3.cpp:181
DLL Vector3 & operator/=(const float aLhs)
Divide this vector by a scalar.
Definition Vector3.cpp:197
DLL void SetVector3(const float *const aVector)
Set the vector from a float array.
Definition Vector3.cpp:32
DLL bool IsZero() const
Check whether all components are zero.
Definition Vector3.cpp:205
DLL Vector3 & operator-=(const Vector3 &aLhs)
Subtract a vector from this vector.
Definition Vector3.cpp:149
DLL const float & GetY() const
Get the Y component.
Definition Vector3.cpp:112
DLL Vector3()
Default constructor.
Definition Vector3.cpp:22
float mVector[3]
X,Y,Z.
Definition Vector3.hpp:135
DLL Vector3 & operator+=(const Vector3 &aLhs)
Add a vector to this vector.
Definition Vector3.cpp:166
DLL const float & GetX() const
Get the X component.
Definition Vector3.cpp:107
DLL void Get(float *aData) const
Copy vector data to an external float array.
Definition Vector3.cpp:97
DLL size_t GetMinAxisIndex() const
Get the index of the axis with the smallest absolute value.
Definition Vector3.cpp:67
<- This is here just for the literals
Definition AABB.hpp:31
DLL const Vector3 Spline(const Vector3 &p0, const Vector3 &p1, const Vector3 &p2, const Vector3 &p3, double interpolation)
Catmull-Rom spline interpolation between four control points.
Definition Vector3.cpp:275
DLL bool operator!=(const Vector3 &aLhs, const Vector3 &aRhs)
Inequality comparison operator.
Definition Vector3.cpp:239
DLL const Vector3 operator/(const Vector3 &aLhs, const float aRhs)
Scalar division operator.
Definition Vector3.cpp:218
DLL const Matrix3x3 operator*(const Matrix3x3 &lhs, const Matrix3x3 &rhs)
Multiplies two 3x3 matrices. Multiplies two 3x3 matrices.
DLL const float Dot(const Vector3 &aLhs, const Vector3 &aRhs)
Compute the dot product of two vectors.
Definition Vector3.cpp:249
DLL const Vector3 operator+(const Vector3 &aLhs, const Vector3 &aRhs)
Addition operator.
Definition Vector3.cpp:173
DLL const Matrix3x3 Abs(const Matrix3x3 &aMatrix3x3)
Compute the element-wise absolute value of a matrix.
DLL const Vector3 Cross(const Vector3 &aLhs, const Vector3 &aRhs)
Compute the cross product of two vectors.
Definition Vector3.cpp:123
DLL const Vector3 operator-(const Vector3 &aLhs, const Vector3 &aRhs)
Subtraction operator.
Definition Vector3.cpp:158
DLL const bool operator==(const Matrix3x3 &lhs, const Matrix3x3 &rhs)
Compare two 3x3 matrices for equality.
DLL const Vector3 Normalize(const Vector3 &aVector)
Return a normalized (unit length) copy of the vector.
Definition Vector3.cpp:257