CGVIS Utils
Loading...
Searching...
No Matches
Vec2.h
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <string>
5#include <array>
6#include <sstream>
7#include <cmath>
8
9#include "Rand.h"
10
25
26template <typename T>
27union Vec2t {
28public:
30 std::array<T, 2> e;
31 struct { T x; T y; };
32 struct { T r; T g; };
33
38 e{0,0}
39 {}
40
46 Vec2t(T x, T y) :
47 e{x,y}
48 {}
49
54 Vec2t(const Vec2t& other) :
55 e{other.x, other.y}
56 {}
57
63 template <typename U>
64 explicit Vec2t(const Vec2t<U>& other) :
65 e{T(other.x), T(other.y)}
66 {}
67
72 const std::string toString() const {
73 std::stringstream s;
74 s << "[" << e[0] << ", " << e[1] << "]";
75 return s.str();
76 }
77
83 Vec2t operator+(const Vec2t& val) const{
84 return {e[0]+val.e[0],e[1]+val.e[1]};
85 }
86
92 Vec2t operator-(const Vec2t& val) const {
93 return {e[0]-val.e[0],e[1]-val.e[1]};
94 }
95
101 Vec2t operator*(const Vec2t& val) const {
102 return {e[0]*val.e[0],e[1]*val.e[1]};
103 }
104
110 Vec2t operator/(const Vec2t& val) const {
111 return {e[0]/val.e[0],e[1]/val.e[1]};
112 }
113
119 Vec2t operator*(const T& val) const {
120 return {e[0]*val,e[1]*val};
121 }
122
128 Vec2t operator/(const T& val) const {
129 return {e[0]/val, e[1]/val};
130 }
131
137 Vec2t operator+(const T& val) const {
138 return {e[0]+val,e[1]+val};
139 }
140
146 Vec2t operator-(const T& val) const{
147 return {e[0]-val,e[1]-val};
148 }
149
155 bool operator == ( const Vec2t& other ) const {
156 return e == other.e;
157 }
158
164 bool operator != ( const Vec2t& other ) const {
165 return e != other.e;
166 }
167
172 T length() const {
173 return sqrt(sqlength());
174 }
175
180 T sqlength() const {
181 return e[0]*e[0]+e[1]*e[1];
182 }
183
189 operator T*(void) {return e.data();}
190
195 operator const T*(void) const {return e.data();}
196
203 static Vec2t random() {
204 return Vec2t{T{staticRand.rand01()},T{staticRand.rand01()}};
205 }
206
212 static Vec2t normalize(const Vec2t& a) {
213 const float l = a.length();
214 return (l != T(0)) ? a/l : Vec2t{T(0),T(0)};
215 }
216
225 static Vec2t<float> clamp(const Vec2t& val, float minVal, float maxVal) {
226 return { std::clamp(val.x, minVal, maxVal),
227 std::clamp(val.y, minVal, maxVal)
228 };
229 }
230};
231
239template <typename T>
240std::ostream & operator<<(std::ostream & os, const Vec2t<T> & v) {
241 os << v.toString();
242 return os;
243}
244
252template <typename T>
253Vec2t<T> operator*(float scalar, const Vec2t<T>& vec) {
254 return vec * scalar;
255}
256
Random staticRand
Global PRNG instance for convenience functions.
Definition Rand.cpp:52
Simple random number utility wrapping std::mt19937 with common ranges.
Vec2t< T > operator*(float scalar, const Vec2t< T > &vec)
Scalar multiplication with scalar on the left.
Definition Vec2.h:253
Vec2t< float > Vec2
Float vector (x,y).
Definition Vec2.h:258
Vec2t< int32_t > Vec2i
32-bit signed integer vector (x,y).
Definition Vec2.h:260
std::ostream & operator<<(std::ostream &os, const Vec2t< T > &v)
Stream insertion for string representation of a vector.
Definition Vec2.h:240
Vec2t< uint32_t > Vec2ui
32-bit unsigned integer vector (x,y).
Definition Vec2.h:262
Definition Vec2.h:27
Vec2t(const Vec2t< U > &other)
Explicitly convert from another Vec2t with different component type.
Definition Vec2.h:64
Vec2t operator/(const Vec2t &val) const
Component-wise division.
Definition Vec2.h:110
T length() const
Euclidean length (magnitude).
Definition Vec2.h:172
Vec2t(T x, T y)
Construct from two component values.
Definition Vec2.h:46
Vec2t operator*(const T &val) const
Scalar multiplication.
Definition Vec2.h:119
float y
Definition Vec2.h:31
float g
Definition Vec2.h:32
T sqlength() const
Squared length (avoids sqrt).
Definition Vec2.h:180
static Vec2t normalize(const Vec2t &a)
Return the normalized (unit-length) version of a vector.
Definition Vec2.h:212
Vec2t()
Default-construct to (0, 0).
Definition Vec2.h:37
float x
Definition Vec2.h:31
const std::string toString() const
String representation "[x, y]".
Definition Vec2.h:72
Vec2t(const Vec2t &other)
Copy-construct from another vector of the same type.
Definition Vec2.h:54
static Vec2t random()
Create a vector with components sampled from [0,1).
Definition Vec2.h:203
Vec2t operator/(const T &val) const
Scalar division.
Definition Vec2.h:128
Vec2t operator+(const T &val) const
Add scalar to both components.
Definition Vec2.h:137
Vec2t operator+(const Vec2t &val) const
Component-wise addition.
Definition Vec2.h:83
bool operator!=(const Vec2t &other) const
Inequality comparison (component-wise inequality).
Definition Vec2.h:164
static Vec2t< float > clamp(const Vec2t &val, float minVal, float maxVal)
Clamp both components to [minVal, maxVal].
Definition Vec2.h:225
float r
Definition Vec2.h:32
std::array< float, 2 > e
Definition Vec2.h:30
Vec2t operator-(const Vec2t &val) const
Component-wise subtraction.
Definition Vec2.h:92
Vec2t operator*(const Vec2t &val) const
Component-wise multiplication.
Definition Vec2.h:101
Vec2t operator-(const T &val) const
Subtract scalar from both components.
Definition Vec2.h:146
bool operator==(const Vec2t &other) const
Equality comparison (component-wise equality).
Definition Vec2.h:155