CGVIS Utils
Loading...
Searching...
No Matches
Vec4.h
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <string>
5#include <array>
6
7#include "Vec2.h"
8#include "Vec3.h"
9
23
24template <typename T>
25union Vec4t {
26public:
28 std::array<T, 4> e;
29 struct { T x; T y; T z; T w;};
30 struct { T r; T g; T b; T a;};
33
36 e{0,0,0,0}
37 {}
38
46 Vec4t(T x, T y, T z, T w):
47 e{x,y,z,w}
48 {}
49
54 Vec4t(const Vec4t& other) :
55 e{other.x, other.y, other.z, other.w}
56 {}
57
63 Vec4t(const Vec3t<T>& other, T w):
64 e{other.x, other.y, other.z, w}
65 {}
66
73 Vec4t(const Vec2t<T>& other, T z, T w) :
74 e{other.x, other.y, z, w}
75 {}
76
82 template <typename U>
83 explicit Vec4t(const Vec4t<U>& other) :
84 e{other.x, other.y, other.z, other.w}
85 {}
86
93 template <typename U>
94 explicit Vec4t(const Vec3t<U>& other, T w):
95 e{other.x, other.y, other.z}
96 {}
97
105 template <typename U>
106 explicit Vec4t(const Vec2t<U>& other, T z, T w) :
107 e{other.x, other.y, z, w}
108 {}
109
114 const std::string toString() const {
115 std::stringstream s;
116 s << "[" << e[0] << ", " << e[1] << ", " << e[2] << ", " << e[3] << "]";
117 return s.str();
118 }
119
125 Vec4t operator+(const Vec4t& val) const {
126 return {e[0]+val.e[0],
127 e[1]+val.e[1],
128 e[2]+val.e[2],
129 e[3]+val.e[3]};
130 }
131
137 Vec4t operator-(const Vec4t& val) const {
138 return {e[0]-val.e[0],
139 e[1]-val.e[1],
140 e[2]-val.e[2],
141 e[3]-val.e[3]};
142 }
143
149 Vec4t operator*(const Vec4t& val) const {
150 return {e[0]*val.e[0],
151 e[1]*val.e[1],
152 e[2]*val.e[2],
153 e[3]*val.e[3]};
154 }
155
161 Vec4t operator/(const Vec4t& val) const {
162 return {e[0]/val.e[0],
163 e[1]/val.e[1],
164 e[2]/val.e[2],
165 e[3]/val.e[3]};
166 }
167
173 Vec4t operator*(const T& val) const {
174 return {e[0]*val, e[1]*val, e[2]*val, e[3]*val};
175 }
176
182 Vec4t operator/(const T& val) const {
183 return {e[0]/val, e[1]/val, e[2]/val, e[3]/val};
184 }
185
191 Vec4t operator+(const T& val) const {
192 return {e[0]+val,e[1]+val,e[2]+val,e[3]+val};
193 }
194
200 Vec4t operator-(const T& val) const{
201 return {e[0]-val,e[1]-val,e[2]-val,e[3]-val};
202 }
203
209 bool operator == ( const Vec4t& other ) const {
210 return e[0] == other.e[0] &&
211 e[1] == other.e[1] &&
212 e[2] == other.e[2] &&
213 e[3] == other.e[3];
214 }
215
221 bool operator != ( const Vec4t& other ) const {
222 return e[0] != other.e[0] ||
223 e[1] != other.e[1] ||
224 e[2] != other.e[2] ||
225 e[3] != other.e[3];
226 }
227
232 T length() const {
233 return sqrt(sqlength());
234 }
235
240 T sqlength() const {
241 return e[0]*e[0]+e[1]*e[1]+e[2]*e[2]+e[3]*e[3];
242 }
243
248 Vec3t<T> vec3() const {
249 return {e[0], e[1], e[2]};
250 }
251
257 operator T*(void) {return e.data();}
258
263 operator const T*(void) const {return e.data();}
264
271 static T dot(const Vec4t& a, const Vec4t& b) {
272 return a.e[0]*b.e[0]+a.e[1]*b.e[1]+a.e[2]*b.e[2]+a.e[3]*b.e[3];
273 }
274
281 static Vec4t normalize(const Vec4t& a) {
282 return a/a.length();
283 }
284
292 return {staticRand.rand01(),staticRand.rand01(),staticRand.rand01(),staticRand.rand01()};
293 }
294
302 static Vec4t<float> clamp(const Vec4t& val, float minVal, float maxVal) {
303 return { std::clamp(val.x, minVal, maxVal),
304 std::clamp(val.y, minVal, maxVal),
305 std::clamp(val.z, minVal, maxVal),
306 std::clamp(val.w, minVal, maxVal)
307 };
308 }
309};
310
318template <typename T>
319std::ostream & operator<<(std::ostream & os, const Vec4t<T> & v) {
320 os << v.toString();
321 return os;
322}
323
331template <typename T>
332Vec4t<T> operator*(float scalar, const Vec4t<T>& vec) {
333 return vec * scalar;
334}
335
Random staticRand
Global PRNG instance for convenience functions.
Definition Rand.cpp:52
2D vector template with arithmetic, length utilities, and helpers.
3D vector template with arithmetic, geometry helpers, and RNG utilities.
Vec4t< int32_t > Vec4i
4D 32-bit signed integer vector (x,y,z,w).
Definition Vec4.h:339
Vec4t< T > operator*(float scalar, const Vec4t< T > &vec)
Scalar multiplication with scalar on the left.
Definition Vec4.h:332
Vec4t< float > Vec4
4D float vector (x,y,z,w).
Definition Vec4.h:337
std::ostream & operator<<(std::ostream &os, const Vec4t< T > &v)
Stream insertion for string representation of a vector.
Definition Vec4.h:319
Vec4t< uint32_t > Vec4ui
4D 32-bit unsigned integer vector (x,y,z,w).
Definition Vec4.h:341
Definition Vec2.h:27
Definition Vec3.h:29
Definition Vec4.h:25
Vec4t(T x, T y, T z, T w)
Construct from four component values.
Definition Vec4.h:46
T length() const
Euclidean length (magnitude).
Definition Vec4.h:232
const std::string toString() const
String representation "[x, y, z, w]".
Definition Vec4.h:114
Vec3t< float > xyz
Definition Vec4.h:32
Vec4t(const Vec3t< U > &other, T w)
Explicitly convert from a 3D vector of a different type and a w value.
Definition Vec4.h:94
Vec4t operator/(const Vec4t &val) const
Component-wise division.
Definition Vec4.h:161
T sqlength() const
Squared length (avoids sqrt).
Definition Vec4.h:240
static T dot(const Vec4t &a, const Vec4t &b)
Dot product of two 4D vectors.
Definition Vec4.h:271
Vec3t< T > vec3() const
Drop the last component and return a 3D vector (x,y,z).
Definition Vec4.h:248
Vec4t operator-(const T &val) const
Subtract scalar from all components.
Definition Vec4.h:200
float z
Definition Vec4.h:29
float w
Definition Vec4.h:29
Vec4t(const Vec4t &other)
Copy-construct from the same type.
Definition Vec4.h:54
Vec4t(const Vec2t< T > &other, T z, T w)
Construct from a 2D vector and z, w values.
Definition Vec4.h:73
static Vec4t< float > random()
Create a vector with components sampled from [0,1).
Definition Vec4.h:291
float y
Definition Vec4.h:29
float g
Definition Vec4.h:30
Vec4t operator*(const T &val) const
Scalar multiplication.
Definition Vec4.h:173
Vec4t operator/(const T &val) const
Scalar division.
Definition Vec4.h:182
Vec4t operator+(const T &val) const
Add scalar to all components.
Definition Vec4.h:191
Vec4t operator+(const Vec4t &val) const
Component-wise addition.
Definition Vec4.h:125
bool operator==(const Vec4t &other) const
Equality comparison (component-wise equality).
Definition Vec4.h:209
bool operator!=(const Vec4t &other) const
Inequality comparison (component-wise inequality).
Definition Vec4.h:221
Vec4t operator-(const Vec4t &val) const
Component-wise subtraction.
Definition Vec4.h:137
float a
Definition Vec4.h:30
float r
Definition Vec4.h:30
std::array< float, 4 > e
Definition Vec4.h:28
Vec4t(const Vec2t< U > &other, T z, T w)
Explicitly convert from a 2D vector of a different type and z, w values.
Definition Vec4.h:106
Vec4t(const Vec3t< T > &other, T w)
Construct from a 3D vector and a w value.
Definition Vec4.h:63
Vec4t operator*(const Vec4t &val) const
Component-wise multiplication.
Definition Vec4.h:149
float b
Definition Vec4.h:30
static Vec4t< float > clamp(const Vec4t &val, float minVal, float maxVal)
Clamp each component to [minVal, maxVal].
Definition Vec4.h:302
Vec4t(const Vec4t< U > &other)
Explicitly convert from another Vec4t with different component type.
Definition Vec4.h:83
Vec2t< float > xy
Definition Vec4.h:31
static Vec4t normalize(const Vec4t &a)
Return the normalized (unit-length) version of a vector.
Definition Vec4.h:281
Vec4t()
Default-construct to (0,0,0,0).
Definition Vec4.h:35
float x
Definition Vec4.h:29