Cumulia Illustrator Rendering Engine v1.1.0
A Rendering engine for industrial CAD/CAE model and optimized for greatest performance
 
Loading...
Searching...
No Matches
vector.h
1//##################################################################################################
2//
3// Copyright (c) 2024 Beijing Qiongqi Tech Co.,Ltd. All rights reserved.
4
5// This source code is confidential and proprietary to Beijing Qiongqi Tech Co.,Ltd(The Holder).
6// Any unauthorized use, copying, modification, or distribution of the code is strictly prohibited.
7// Any user shall obtain authorizaition from the Holder before modifying the source code. And the user shall not
8// sublicense, sell, distribute, or transfer the source code, whether in original or modified form, to any third party
9// without the prior written consent of the Holder.
10
11// This copyright notice and permission grant shall be included in all copies or substantial portions of the source code.
12
13// Author Cumulia Illustrator
14// Date 2024-10-31
15// Version V1.1.0
16//##################################################################################################
17
18#pragma once
19
20#include "tuple.h"
21
22#include <cmath>
23
24namespace cil
25{
26 class Vector2f;
27 class Vector3f;
28 class Vector4f;
29 class Vector2i;
30 class Vector3i;
31 class Vector4i;
32 class Matrix4f;
33
35 template<typename T, typename S>
36 class Vector : public Tuple<T>
37 {
38 public:
42 : Tuple<T>(size)
43 {
44 }
45
47 const T& operator[](int index) const
48 {
49 return this->m_data[index];
50 }
51
53 T& operator[](int index)
54 {
55 return this->m_data[index];
56 }
57
61 const T& operator[](char key) const
62 {
63 if (key == 'x')
64 return this->m_data[0];
65 if (key == 'y')
66 return this->m_data[1];
67 if (key == 'z' && this->m_size >= 3)
68 return this->m_data[2];
69 if (key == 'w' && this->m_size >= 4)
70 return this->m_data[3];
71
72 return this->m_data[0];
73 }
74
78 T& operator[](char key)
79 {
80 if (key == 'x')
81 return this->m_data[0];
82 if (key == 'y')
83 return this->m_data[1];
84 if (key == 'z' && this->m_size >= 3)
85 return this->m_data[2];
86 if (key == 'w' && this->m_size >= 4)
87 return this->m_data[3];
88
89 return this->m_data[0];
90 }
91
94 Vector& operator+=(const Vector& other)
95 {
96 int size = this->m_size;
97 for (int i = 0; i < size; i++)
98 this->m_data[i] += other.data()[i];
99
100 return *this;
101 }
105 {
106 int size = this->m_size;
107 for (int i = 0; i < size; i++)
108 this->m_data[i] -= other.data()[i];
109
110 return *this;
111 }
115 {
116 int size = this->m_size;
117 for (int i = 0; i < size; i++)
118 this->m_data[i] *= factor;
119
120 return *this;
121 }
122
126 {
127 int size = this->m_size;
128 for (int i = 0; i < size; i++)
129 this->m_data[i] /= factor;
130
131 return *this;
132 }
133
135 S operator+(const S& other) const
136 {
137 auto v = S();
138 int size = this->m_size;
139 for (int i = 0; i < size; i++)
140 v.data()[i] = this->m_data[i] + other.data()[i];
141
142 return v;
143 }
145 S operator-(const S& other) const
146 {
147 auto v = S();
148 int size = this->m_size;
149 for (int i = 0; i < size; i++)
150 v.data()[i] = this->m_data[i] - other.data()[i];
151
152 return v;
153 }
154
156 S operator-() const
157 {
158 auto v = S();
159 int size = this->m_size;
160 for (int i = 0; i < size; i++)
161 v.data()[i] = -this->m_data[i];
162
163 return v;
164 }
165
167 T operator*(const S& other) const
168 {
169 T v = 0;
170 int size = this->m_size;
171 for (int i = 0; i < size; i++)
172 v += this->m_data[i] * other.data()[i];
173
174 return v;
175 }
177 S operator*(float factor) const
178 {
179 auto v = S();
180 int size = this->m_size;
181 for (int i = 0; i < size; i++)
182 v.data()[i] = this->m_data[i] * factor;
183
184 return v;
185 }
187 S operator/(float factor) const
188 {
189 auto v = S();
190 int size = this->m_size;
191 for (int i = 0; i < size; i++)
192 v.data()[i] = this->m_data[i] / factor;
193
194 return v;
195 }
198 {
199 T v = 0;
200 int size = this->m_size;
201 for (int i = 0; i < size; i++)
202 v += this->m_data[i] * this->m_data[i];
203
204 return v;
205 }
207 float length() const
208 {
209 return sqrt(this->squaredLength());
210 }
212 bool setLength(float len)
213 {
214 auto current = this->length();
215 if (current > 0.0)
216 {
217 float factor = len / current;
218 this->operator*=(factor);
219
220 return true;
221 }
222
223 return false;
224 }
226
228 {
229 return this->setLength(1.0f);
230 }
231 };
232
234 template<typename T, typename S>
235 class Vector2 : public Vector<T, S>
236 {
237 public:
238
241 : Vector<T, S>(2)
242 {
243 }
244
246 void set(T x, T y)
247 {
248 this->m_data[0] = x;
249 this->m_data[1] = y;
250 }
251 };
252
254 class Vector2i final : public Vector2<int, Vector2i>
255 {
256 public:
258 Vector2i(int x, int y);
259 Vector2i(const Vector2i& other);
260 Vector2i(const Vector3i& other);
261 Vector2i(const Vector4i& other);
262
263 int manhattanLength() const;
264 };
265
267 class Vector2f final : public Vector2<float, Vector2f>
268 {
269 public:
271 Vector2f(float x, float y);
272 Vector2f(const Vector2f& other);
273 Vector2f(const Vector3f& other);
274 Vector2f(const Vector4f& other);
275 };
276
278 template<typename T, typename S>
279 class Vector3 : public Vector<T, S>
280 {
281 public:
282
285 : Vector<T, S>(3)
286 {
287 }
289 void set(T x, T y, T z)
290 {
291 this->m_data[0] = x;
292 this->m_data[1] = y;
293 this->m_data[2] = z;
294 }
295
297 S operator^(const S& other) const
298 {
299 auto a = this->m_data;
300 auto b = other.data();
301
302 auto x = a[1] * b[2] - a[2] * b[1];
303 auto y = a[2] * b[0] - a[0] * b[2];
304 auto z = a[0] * b[1] - a[1] * b[0];
305
306 return S(x, y, z);
307 }
308 };
309
311 class Vector3i final : public Vector3<int, Vector3i>
312 {
313 public:
315 Vector3i(int x, int y, int z);
316 Vector3i(const Vector2i& other, int z = 0);
317 Vector3i(const Vector3i& other);
318 Vector3i(const Vector4i& other);
319 };
321 class Vector3f final : public Vector3<float, Vector3f>
322 {
323 public:
325 Vector3f(float x, float y, float z);
326 Vector3f(const Vector2f& other, float z = 0);
327 Vector3f(const Vector3f& other);
328 Vector3f(const Vector4f& other);
329 };
330
332 template<typename T, typename S>
333 class Vector4 : public Vector<T, S>
334 {
335 public:
338 : Vector<T, S>(4)
339 {
340 }
342 void set(T x, T y, T z, T w)
343 {
344 this->m_data[0] = x;
345 this->m_data[1] = y;
346 this->m_data[2] = z;
347 this->m_data[3] = w;
348 }
349 };
350
352
353 class Vector4i final : public Vector4<int, Vector4i>
354 {
355 public:
357 Vector4i(int x, int y, int z, int w);
358 Vector4i(const Vector2i& other, int z = 0, int w = 0);
359 Vector4i(const Vector3i& other, int w = 0);
360 Vector4i(const Vector4i& other);
361 };
362
364
365 class Vector4f final : public Vector4<float, Vector4f>
366 {
367 public:
369 Vector4f(float x, float y, float z, float w);
370 Vector4f(const Vector2f& other, float z = 0, float w = 0);
371 Vector4f(const Vector3f& other, float w = 0);
372 Vector4f(const Vector4f& other);
373 };
374
375 using vec2 = Vector2f;
377 using vec3 = Vector3f;
379 using vec4 = Vector4f;
381}
The Tuple is a templated class inheriting from the most basic class Variant.
Definition tuple.h:27
T * data()
Gets the data array of the Tuple.
Definition tuple.h:60
int m_size
The number of elements that the data array of the Tuple holds.
Definition tuple.h:36
T * m_data
The data array of the Tuple.
Definition tuple.h:35
int size() const
Gets the number of elements of the Tuple.
Definition tuple.h:54
The class Vector2 is a templated class inheriting from the Vector class. A vector2 object holds 2 ele...
Definition vector.h:236
Vector2()
Contructs a Vector2 object with 2 elements, all initialized to 0.
Definition vector.h:240
void set(T x, T y)
Sets the values of the two elements of current Vector2 object.
Definition vector.h:246
The class Vector2f inherits from the templated class of Vector2, and the template parameters are spec...
Definition vector.h:268
Vector2f(const Vector4f &other)
Contructs a Vector2f object by copying the firt two elements of another existing vector4f object.
Vector2f(float x, float y)
Contructs a Vector2f object and sets the floats x and y to the elements.
Vector2f()
Contructs a Vector2f object, all elements initialized to 0.
Vector2f(const Vector2f &other)
Contructs a Vector2f object by copying the elements of another existing vector2f object.
Vector2f(const Vector3f &other)
Contructs a Vector2f object by copying the firt two elements of another existing vector3f object.
The class Vector2i inherits from the templated class of Vector2, and the template parameters are spec...
Definition vector.h:255
int manhattanLength() const
Vector2i(const Vector4i &other)
Contructs a Vector2i object by copying the firt two elements of another existing vector4i object.
Vector2i(const Vector3i &other)
Contructs a Vector2i object by copying the firt two elements of another existing vector3i object.
Vector2i()
Contructs a Vector2i object, all elements initialized to 0.
Vector2i(const Vector2i &other)
Contructs a Vector2i object by copying the elements of another existing vector2i object.
Vector2i(int x, int y)
Contructs a Vector2i object and sets the integers x and y to the elements.
The class Vector3 is a templated class inheriting from the Vector class. A vector3 object holds 3 ele...
Definition vector.h:280
Vector3()
Contructs a Vector3 object with 3 elements, all initialized to 0.
Definition vector.h:284
S operator^(const S &other) const
Calculates the cross product of this vector3 and another vector3, returning a new Vector3.
Definition vector.h:297
void set(T x, T y, T z)
Sets the values of the three elements of current Vector3 object.
Definition vector.h:289
The class Vector3f inherits from the templated class of Vector3, and the template parameters are spec...
Definition vector.h:322
Vector3f(const Vector4f &other)
Contructs a Vector3f object by copying the firt three elements of another existing vector4f object.
Vector3f(const Vector2f &other, float z=0)
Contructs a Vector3f object by copying the elements of another existing vector2f object,...
Vector3f(float x, float y, float z)
Contructs a Vector3f object and sets the floats x, y and z to the elements.
Vector3f()
Contructs a Vector3f object, all elements initialized to 0.
Vector3f(const Vector3f &other)
Contructs a Vector3f object by copying the elements of another existing vector3f object.
The class Vector3i inherits from the templated class of Vector3, and the template parameters are spec...
Definition vector.h:312
Vector3i(int x, int y, int z)
Contructs a Vector3i object and sets the integers x, y and z to the elements.
Vector3i(const Vector4i &other)
Contructs a Vector3i object by copying the firt three elements of another existing vector4i object.
Vector3i(const Vector3i &other)
Contructs a Vector3i object by copying the elements of another existing vector3i object.
Vector3i()
Contructs a Vector3i object, all elements initialized to 0.
Vector3i(const Vector2i &other, int z=0)
Contructs a Vector3i object by copying the elements of another existing vector2i object,...
The class Vector4 is a templated class inheriting from the Vector class. A vector4 object holds 4 ele...
Definition vector.h:334
void set(T x, T y, T z, T w)
Sets the values of the four elements of current Vector4 object.
Definition vector.h:342
Vector4()
Contructs a Vector4 object with 4 elements, all initialized to 0.
Definition vector.h:337
The class Vector4f inherits from the templated class of Vector4, and the template parameters are spec...
Definition vector.h:366
Vector4f()
Contructs a Vector4f object, all elements initialized to 0.
Vector4f(const Vector4f &other)
Contructs a Vector4f object by copying the elements of another existing vector4f object.
Vector4f(const Vector3f &other, float w=0)
Contructs a Vector4f object by copying the elements of another existing vector3f object,...
Vector4f(float x, float y, float z, float w)
Contructs a Vector4f object and sets the floats x, y, z and w to the elements.
Vector4f(const Vector2f &other, float z=0, float w=0)
Contructs a Vector4f object by copying the elements of another existing vector2f object,...
The class Vector4i inherits from the templated class of Vector4, and the template parameters are spec...
Definition vector.h:354
Vector4i()
Contructs a Vector4i object, all elements initialized to 0.
Vector4i(const Vector2i &other, int z=0, int w=0)
Contructs a Vector4i object by copying the elements of another existing vector2i object,...
Vector4i(int x, int y, int z, int w)
Contructs a Vector4i object and sets the integers x, y, z and w to the elements.
Vector4i(const Vector3i &other, int w=0)
Contructs a Vector4i object by copying the elements of another existing vector3i object,...
Vector4i(const Vector4i &other)
Contructs a Vector4i object by copying the elements of another existing vector4i object.
The Vector is a templated class inheriting from the Tuple class, and it can store a pecific number of...
Definition vector.h:37
bool normalize()
Normalize the current Vector and returns a boolean value indicating success.
Definition vector.h:227
const T & operator[](char key) const
Provides read-only access to a element within the Vector object using a character key.
Definition vector.h:61
S operator/(float factor) const
Returns a Vector object that is formed by dividing each element of the current vector by the factor.
Definition vector.h:187
const T & operator[](int index) const
Provides read-only access to the element at a specific index(position) within the Vector object.
Definition vector.h:47
S operator-(const S &other) const
Returns a Vector object that is formed by element-wise subtraction between the current Vector and ano...
Definition vector.h:145
S operator*(float factor) const
Returns a Vector object that is formed by multiplying each element of the current vector by the facto...
Definition vector.h:177
bool setLength(float len)
Changes the length of the current Vector and returns a boolean value indicating success.
Definition vector.h:212
S operator+(const S &other) const
Returns a Vector object that is formed by element-wise addition between the current Vector and anothe...
Definition vector.h:135
T squaredLength() const
Returns the squared length of the current Vector.
Definition vector.h:197
S operator-() const
Returns a Vector object that is formed by changing the sign of each element of the current vector.
Definition vector.h:156
T & operator[](char key)
Provides read-write access to a element within the Vector object using a character key.
Definition vector.h:78
Vector< T, S > & operator*=(float factor)
Performs scalar multiplication between the current Vector and a floating-point factor.
Definition vector.h:114
T operator*(const S &other) const
Returns a Vector object that is formed by element-wise multiplication between the current Vector and ...
Definition vector.h:167
Vector< T, S > & operator/=(float factor)
Performs scalar division between the current Vector and a floating-point factor.
Definition vector.h:125
float length() const
Returns the length of the current Vector.
Definition vector.h:207
Vector< T, S > & operator-=(const Vector &other)
Performs element-wise subtraction between the current Vector and another vector.
Definition vector.h:104
T & operator[](int index)
Provides read-write access to the element at a specific index(position) within the Vector object.
Definition vector.h:53
Vector(int size)
Constructs a Vector object with a specified number of elements.
Definition vector.h:41
Vector & operator+=(const Vector &other)
Performs element-wise addition between the current Vector and another vector.
Definition vector.h:94
Definition decal.h:23