Itasca C++ Interface
Loading...
Searching...
No Matches
avect.h
Go to the documentation of this file.
1#pragma once
9#include "vect.h"
10
12
29namespace AVector2Util {
30 static const double nullValue_ = 0.0;
31}
32
34
43template <class T> class AVector2 {
44public:
45 // Creators
47 constexpr AVector2()=default;
48 constexpr AVector2(const AVector2 &av)=default;
49 constexpr AVector2<T> &operator=(const AVector2<T> &v) =default;
50
52 explicit AVector2(const T &t) : z_(t) { }
54 explicit AVector2(const Vector2<T> &) : z_(0) { }
56 explicit AVector2(const Vector3<T> &v) : z_(v.z_) { }
57
59 const T &x() const { return AVector2Util::nullValue_; }
61 const T &y() const { return AVector2Util::nullValue_; }
63 const T &z() const { return z_; }
65 const T &dof(uint32 u) const {
66#ifdef _DEBUG
67 if (u>2) throw std::runtime_error("Vector index out of range.");
68#endif
69 if (u==2) return z_;
70 return AVector2Util::nullValue_;
71 }
72
74 T &rz() { return z_; }
76 T &rdof([[maybe_unused]] uint32 u) {
77#ifdef _DEBUG
78 if (u!=2) throw std::runtime_error("Vector index out of range.");
79#endif
80 return z_;
81 }
82
83 const T &operator[](uint32 u) const { return dof(u); }
84 T &operator[](uint32 u) { return rdof(u); }
85
86 T fsum() const { return std::abs(z_); }
87 T mag2() const { return z_*z_; }
88 constexpr T mag() const { return std::abs(z_); }
89 T area() const { return 0; }
90 T volume() const { return 0; }
91 constexpr AVector2<T> unit() const { AVector2<T> v(*this); if (z_) { v.z_ = z_ < static_cast<T>(0) ? static_cast<T>(-1) : static_cast<T>(1); } return v; }
92 AVector2<T> abs() const { return std::abs(z_); }
93 void fill(const T &d) { z_=d; }
94 constexpr T maxComp() const { return std::max(0.0,z_); }
95 constexpr T minComp() const { return std::min(0.0,z_); }
96
97 bool operator==(const AVector2<T> &av) const { return z_==av.z_; }
98 bool operator!=(const AVector2<T> &av) const { return z_!=av.z_; }
99 bool operator<(const AVector2<T> &av) const { return z_ < av.z_; }
100 bool operator>(const AVector2<T> &av) const { return z_ > av.z_; }
101
102 const AVector2<T> &operator+=(const AVector2<T> &v) { z_ += v.z_; return *this; }
103 const AVector2<T> &operator-=(const AVector2<T> &v) { z_ -= v.z_; return *this; }
104 const AVector2<T> &operator*=(const AVector2<T> &v) { z_ *= v.z_; return *this; }
105 const AVector2<T> &operator*=(const T &t) { z_ *= t; return *this; }
106 const AVector2<T> &operator/=(const AVector2<T> &v) { z_ /= v.z_; return *this; }
107 const AVector2<T> &operator/=(const T &t) { z_ /= t; return *this; }
108
109 AVector2<T> operator+(const AVector2<T> &av) const { AVector2<T> out(z_+av.z_); return out; }
110 AVector2<T> operator-(const AVector2<T> &av) const { AVector2<T> out(z_-av.z_); return out; }
111 AVector2<T> operator*(const AVector2<T> &av) const { AVector2<T> out(z_*av.z_); return out; }
112 AVector2<T> operator*(const T &t) const { AVector2<T> out(z_*t); return out; }
113 AVector2<T> operator/(const AVector2<T> &av) const { AVector2<T> out(z_/av.z_); return out; }
114 AVector2<T> operator/(const T &t) const { AVector2<T> out(z_/t); return out; }
115
116 T operator&(const AVector2<T> &) const { return 0; }
117 Vector2<T> operator&(const Vector2<T> &v) const { Vector2<T> out(-z_*v.y(),z_*v.x()); return out; }
118 Vector3<T> operator&(const Vector3<T> &v) const { Vector3<T> out(-z_*v.y(),z_*v.x(),0); return out; }
119
120 T operator|(const AVector2<T> &v) const { return z_*v.z(); }
121 T operator|(const Vector2<T> &) const { return 0; }
122 T operator|(const Vector3<T> &v) const { return z_*v.z(); }
123
124 Vector2<T> toVector2() const { return Vector2<T>(0); }
125 Vector3<T> toVector3() const { return Vector3<T>(0,0,z_); }
126
127private:
128 T z_=T{};
129};
130
133template <class T> inline Vector2<T> operator&(const Vector2<T> &v,const AVector2<T> &av) {
134 Vector2<T> out(v.y()*av.z(),-v.x()*av.z());
135 return out;
136}
137
140template <class T> inline Vector3<T> operator&(const Vector3<T> &v,const AVector2<T> &av) {
141 Vector3<T> out(v.y()*av.z(),-v.x()*av.z(),0);
142 return out;
143}
144
147template <class T> inline AVector2<T> operator&(const Vector2<T> &v1,const Vector2<T> &v2) {
148 AVector2<T> out((v1.x()*v2.y()) - (v1.y()*v2.x()));
149 return out;
150}
151
154template <class T> inline T operator|(const Vector2<T> &,const AVector2<T> &) { return 0; }
155
156
157
158
160
169template <class T> class AVector3 : public Vector3<T> {
170public:
171 using Vector3<T>::Vector3;
172 constexpr AVector3()=default;
173 constexpr AVector3(const AVector3<T> &)=default;
174 constexpr AVector3<T> &operator=(const AVector3<T> &)=default;
175
176 AVector3(const Vector3<T> &v) : Vector3<T>(v) { }
178 explicit AVector3(const AVector2<T> &v) : Vector3<T>(0,0,v.z()) { }
179
181 Vector2<T> toVector2() const { return Vector2<T>(this->x(),this->y()); }
183 const Vector3<T> &toVector3() const { return *this; }
184};
185
186// Naming convention for most commonly used types.
187using DAVect2 = AVector2<double>;
188using FAVect2 = AVector2<float>;
189using IAVect2 = AVector2<int32>;
190using UAVect2 = AVector2<uint32>;
191using DAVect3 = AVector3<double>;
192using FAVect3 = AVector3<float>;
193using IAVect3 = AVector3<int32>;
194using UAVect3 = AVector3<uint32> ;
195
196// Conversion routines.
199template <class T> inline DAVect2 toDAVect2(const AVector2<T> &v) { DAVect2 dv2(to<double>(v.z())); return dv2; }
202template <class T> inline FAVect2 toFAVect2(const AVector2<T> &v) { FAVect2 fv2(to<float>(v.z())); return fv2; }
205template <class T> inline IAVect2 toIAVect2(const AVector2<T> &v) { IAVect2 iv2(to<int32>(v.z())); return iv2; }
208template <class T> inline UAVect2 toUAVect2(const AVector2<T> &v) { UAVect2 uv2(to<uint32>(v.z())); return uv2; }
209
212template <class T> inline DAVect3 toDAVect3(const Vector3<T> &v) {
213 DAVect3 dv3(to<double>(v.x()),to<double>(v.y()),to<double>(v.z()));
214 return dv3;
215}
216
219template <class T> inline FAVect3 toFAVect3(const Vector3<T> &v) {
220 FAVect3 fv3(to<float>(v.x()),to<float>(v.y()),to<float>(v.z()));
221 return fv3;
222}
223
226template <class T> inline IAVect3 toAIVect3(const Vector3<T> &v) {
227 IAVect3 iv3(to<int32>(v.x()),to<int32>(v.y()),to<int32>(v.z()));
228 return iv3;
229}
230
233template <class T> inline UAVect3 toAUVect3(const Vector3<T> &v) {
234 UAVect3 uv3(to<uint32>(v.x()),to<uint32>(v.y()),to<uint32>(v.z()));
235 return uv3;
236}
237
240template <class T> inline Vector2<T> toVect2(const AVector2<T> &) { return Vector2<T>(0); }
243template <class T> inline Vector3<T> toVect3(const AVector2<T> &v) { return Vector3<T>(0,0,v.z()); }
246template <class T> inline Vector2<T> toVect2(const AVector3<T> &v) { return Vector2<T>(v.x(),v.y()); }
249template <class T> inline const Vector3<T> &toVect3(const AVector3<T> &v) { return v; }
250
253template <class T> inline const AVector2<T> &toAVect2(const AVector2<T> &v) { return v; }
257template <class T> inline AVector2<T> toAVect2(const AVector3<T> &v) { return AVector2<T>(v.z()); }
261template <class T> inline AVector3<T> toAVect3(const AVector2<T> &v,const T &x=0,const T &y=0) { return Vector3<T>(x,y,v.z()); }
264template <class T> inline const AVector3<T> &toAVect3(const AVector3<T> &v) { return v; }
265
268template <class T> inline AVector3<T> vmax(const AVector3<T> &v1,const AVector3<T> &v2) {
269 AVector3<T> out(std::max<T>(v1.x(),v2.x()),std::max<T>(v1.y(),v2.y()),std::max<T>(v1.z(),v2.z()));
270 return out;
271}
272
275template <class T> inline AVector3<T> vmin(const AVector3<T> &v1,const AVector3<T> &v2) {
276 AVector3<T> out(std::min<T>(v1.x(),v2.x()),std::min<T>(v1.y(),v2.y()),std::min<T>(v1.z(),v2.z()));
277 return out;
278}
279
282template <class T> inline AVector3<T> vsign(const AVector3<T> &v1,const AVector3<T> &v2) {
283 AVector3<T> out(v2.x() < 0 ? -qAbs(v1.x()) : qAbs(v1.x()), v2.y() < 0 ? -qAbs(v1.y()) : qAbs(v1.y()), v2.z() < 0 ? -qAbs(v1.z()) : qAbs(v1.z()));
284 return out;
285}
286
289template <class T> inline AVector3<T> vceil(const AVector3<T> &v) {
290 AVector3<T> out(ceil(v.x()),ceil(v.y()),ceil(v.z()));
291 return out;
292}
293
296template <class T> inline AVector2<T> vmax(const AVector2<T> &v1,const AVector2<T> &v2) {
297 AVector2<T> out(std::max<T>(v1.z(),v2.z()));
298 return out;
299}
300
303template <class T> inline AVector2<T> vmin(const AVector2<T> &v1,const AVector2<T> &v2) {
304 AVector2<T> out(std::min<T>(v1.z(),v2.z()));
305 return out;
306}
307
310template <class T> inline AVector2<T> vsign(const AVector2<T> &v1,const AVector2<T> &v2) {
311 AVector2<T> out(v2.z() < 0 ? -qAbs(v1.z()) : qAbs(v1.z()));
312 return out;
313}
314
317template <class T> inline AVector2<T> vceil(const AVector2<T> &v) {
318 AVector2<T> out(ceil(v.z()));
319 return out;
320}
321
322template <typename T>
323constexpr T safeMag2(const AVector2<T> &v) { return ::safeSquare(v.z()); }
324
325template <typename T>
326constexpr T safeMag(const AVector2<T> &v) {
327 return std::sqrt(safeMag2(v));
328}
329
330template <typename T>
331constexpr AVector2<T> safeUnit(const AVector2<T> &v) {
332 return v.unit();
333}
334
335template <typename T>
336constexpr const AVector2<T> safeDiv(const AVector2<T> &a,const AVector2<T> &b,const AVector2<T> &safe=AVector2<T>(0)) {
337 T ret(::safeDiv(a.z(),b.z(),safe.z()));
338 return AVector2<T>(ret);
339}
340
341template <typename T>
342constexpr const AVector2<T> safeDiv(const AVector2<T> &a,const T &v,const AVector2<T> &safe=AVector2<T>(0)) {
343 T ret(::safeDiv(a.z(),v,safe.z()));
344 return AVector2<T>(ret);
345}
346
347template <typename T>
348constexpr bool isFinite(const AVector2<T> &a) {
349 return std::isfinite(a.z());
350}
351
352namespace base {
353 template <class T>
354 inline string ts(const AVector2<T> &t, int width=0, char notation = '\0', int precision = -1, char fill = ' ') {
355 return ts(t.z(), width, notation, precision, fill);
356 }
357
358 template <class T>
359 inline string ts(const AVector3<T> &t, int width=0, char notation = '\0', int precision = -1, char fill = ' ') {
360 return ts(toVect3(t), width, notation, precision, fill);
361 }
362}
363
365// EOF
2D Angular vector class.
Definition avect.h:43
Vector2< T > operator&(const Vector2< T > &v) const
Cross product with a Vector2 produces a Vector2.
Definition avect.h:117
T operator|(const Vector3< T > &v) const
Dot product with a Vector3.
Definition avect.h:122
constexpr T maxComp() const
Returns the maximum component (assuming x and y are 0.0)
Definition avect.h:94
T operator|(const Vector2< T > &) const
Dot product with a Vector2 returns 0.0.
Definition avect.h:121
T & operator[](uint32 u)
Returns a reference to the z component if u=2, any other value of u is an error.
Definition avect.h:84
T fsum() const
The Manhatten norm.
Definition avect.h:86
AVector2< T > operator-(const AVector2< T > &av) const
Binary math operator - require temp (till C++0x)
Definition avect.h:110
bool operator>(const AVector2< T > &av) const
comparison operator
Definition avect.h:100
constexpr AVector2< T > unit() const
returns a unit vector.
Definition avect.h:91
AVector2(const T &t)
Explicit conversion constructor, the z component is initialized to t.
Definition avect.h:52
const T & y() const
Member access - the x and y values are always uniquely 0 for a 2D angular vector.
Definition avect.h:61
constexpr T minComp() const
Returns the minimum component (assuming x and y are 0.0)
Definition avect.h:95
constexpr AVector2()=default
Default constructor, no data initialization.
const AVector2< T > & operator*=(const T &t)
in-place math operator
Definition avect.h:105
bool operator==(const AVector2< T > &av) const
comparison operator
Definition avect.h:97
const T & x() const
Member access - the x and y values are always uniquely 0 for a 2D angular vector.
Definition avect.h:59
bool operator<(const AVector2< T > &av) const
comparison operator
Definition avect.h:99
AVector2(const Vector2< T > &)
Explicit conversion constructor, the z component is set to 0, the x and y values of v are lost.
Definition avect.h:54
T operator|(const AVector2< T > &v) const
Dot product.
Definition avect.h:120
const AVector2< T > & operator-=(const AVector2< T > &v)
in-place math operator
Definition avect.h:103
T & rdof(uint32 u)
Returns a reference to the z component if u=2, any other value of u is an error.
Definition avect.h:76
const AVector2< T > & operator/=(const AVector2< T > &v)
in-place math operator
Definition avect.h:106
AVector2(const Vector3< T > &v)
Explicit convertion contructor, the z component or v is copied but he x and y values are lost.
Definition avect.h:56
bool operator!=(const AVector2< T > &av) const
comparison operator
Definition avect.h:98
const AVector2< T > & operator*=(const AVector2< T > &v)
in-place math operator
Definition avect.h:104
const AVector2< T > & operator/=(const T &t)
in-place math operator
Definition avect.h:107
T mag2() const
The square of the magnitude, or the vector dotted with itself.
Definition avect.h:87
const AVector2< T > & operator+=(const AVector2< T > &v)
in-place math operator
Definition avect.h:102
void fill(const T &d)
Fill all components with d.
Definition avect.h:93
T & rz()
Returns a reference to the z component, cannot reference access x or y.
Definition avect.h:74
AVector2< T > operator+(const AVector2< T > &av) const
Binary math operator - require temp (till C++0x)
Definition avect.h:109
AVector2< T > operator*(const T &t) const
Binary math operator - require temp (till C++0x)
Definition avect.h:112
const T & z() const
Member access - returns the z component of the 2D angular vector.
Definition avect.h:63
T operator&(const AVector2< T > &) const
Cross product with another AVector2 will always return 0.0.
Definition avect.h:116
T volume() const
Volume represented by x*y*z, always returns 0.0.
Definition avect.h:90
AVector2< T > operator/(const T &t) const
Binary math operator - require temp (till C++0x)
Definition avect.h:114
const T & operator[](uint32 u) const
General degree-of-freedom access. If u is 2 returns the z component, otherwise returns 0....
Definition avect.h:83
AVector2< T > abs() const
Returns a vector of absolute values of components.
Definition avect.h:92
Vector3< T > operator&(const Vector3< T > &v) const
Cross product with a Vector3 produces a Vector3 (z=0)
Definition avect.h:118
constexpr T mag() const
The magnitude of the vector.
Definition avect.h:88
AVector2< T > operator/(const AVector2< T > &av) const
Binary math operator - require temp (till C++0x)
Definition avect.h:113
T area() const
Area represented by x*y, always returns 0.0.
Definition avect.h:89
const T & dof(uint32 u) const
General degree-of-freedom access. If u is 2 returns the z component, otherwise returns 0....
Definition avect.h:65
AVector2< T > operator*(const AVector2< T > &av) const
Binary math operator - require temp (till C++0x)
Definition avect.h:111
Vector2< T > toVector2() const
Converts to a Vector2(0,0)
Definition avect.h:124
Vector3< T > toVector3() const
Converts to a Vector3(0,0,z)
Definition avect.h:125
3D Angular vector class.
Definition avect.h:169
const Vector3< T > & toVector3() const
Converts to a Vector3 directly.
Definition avect.h:183
Vector2< T > toVector2() const
Converts to a Vector2, the z component is lost.
Definition avect.h:181
AVector3(const AVector2< T > &v)
Explicit conversion contructor from an AVector2, x=0, y=0, z=v.z.
Definition avect.h:178
2D vector utility class.
Definition vect.h:32
constexpr const T & x() const
X component access.
Definition vect.h:53
constexpr const T & y() const
Y component access.
Definition vect.h:55
3D vector utility class.
Definition vect.h:150
constexpr const T & y() const
The y-component of the vector.
Definition vect.h:169
constexpr const T & x() const
The x-component of the vector.
Definition vect.h:167
constexpr const T & z() const
The z-component of the vector.
Definition vect.h:171
Vector2< T > toVect2(const AVector3< T > &v)
Definition avect.h:246
AVector2< T > vceil(const AVector2< T > &v)
Definition avect.h:317
constexpr Vector3< T > toVect3(const Vector2< T > &v, const T &t=0)
Conversion between vectors of different dimension.
Definition vect.h:312
IAVect2 toIAVect2(const AVector2< T > &v)
Definition avect.h:205
DAVect3 toDAVect3(const Vector3< T > &v)
Definition avect.h:212
AVector3< T > vsign(const AVector3< T > &v1, const AVector3< T > &v2)
Definition avect.h:282
const AVector2< T > & toAVect2(const AVector2< T > &v)
Definition avect.h:253
UAVect2 toUAVect2(const AVector2< T > &v)
Definition avect.h:208
AVector2< T > vsign(const AVector2< T > &v1, const AVector2< T > &v2)
Definition avect.h:310
UAVect3 toAUVect3(const Vector3< T > &v)
Definition avect.h:233
FAVect2 toFAVect2(const AVector2< T > &v)
Definition avect.h:202
Vector3< T > operator&(const Vector3< T > &v, const AVector2< T > &av)
Definition avect.h:140
AVector3< T > vceil(const AVector3< T > &v)
Definition avect.h:289
AVector3< T > toAVect3(const AVector2< T > &v, const T &x=0, const T &y=0)
Definition avect.h:261
Vector2< T > operator&(const Vector2< T > &v, const AVector2< T > &av)
Definition avect.h:133
AVector2< T > vmin(const AVector2< T > &v1, const AVector2< T > &v2)
Definition avect.h:303
DAVect2 toDAVect2(const AVector2< T > &v)
Definition avect.h:199
FAVect3 toFAVect3(const Vector3< T > &v)
Definition avect.h:219
AVector2< T > toAVect2(const AVector3< T > &v)
Definition avect.h:257
Vector2< T > toVect2(const AVector2< T > &)
Definition avect.h:240
AVector2< T > vmax(const AVector2< T > &v1, const AVector2< T > &v2)
Definition avect.h:296
AVector3< T > vmax(const AVector3< T > &v1, const AVector3< T > &v2)
Definition avect.h:268
Vector3< T > toVect3(const AVector2< T > &v)
Definition avect.h:243
const Vector3< T > & toVect3(const AVector3< T > &v)
Definition avect.h:249
IAVect3 toAIVect3(const Vector3< T > &v)
Definition avect.h:226
AVector3< T > vmin(const AVector3< T > &v1, const AVector3< T > &v2)
Definition avect.h:275
const AVector3< T > & toAVect3(const AVector3< T > &v)
Definition avect.h:264
T operator|(const Vector2< T > &, const AVector2< T > &)
Definition avect.h:154
AVector2< T > operator&(const Vector2< T > &v1, const Vector2< T > &v2)
Definition avect.h:147
2D Angular vector class.
Definition avect.h:29
2D and 3D vector utility classes.