Itasca C++ Interface
Loading...
Searching...
No Matches
vect.h
Go to the documentation of this file.
1#pragma once
7#include "basestring.h"
8#include "to.h"
9#include <cmath>
10#include <array>
11
12#ifdef _LINUX
13namespace std {
14 template <class T> T abs(const T &t) {
15 if (t<0) return -t;
16 return t;
17 }
18 template <class T> T max0(const T &t1,const T &t2) {
19 if (t2>t1) return t2;
20 return t1;
21 }
22 template <class T> T min0(const T &t1,const T &t2) {
23 if (t2<t1) return t1;
24 return t1;
25 }
26} // namespace std
27#endif
28
31PUSHWARNING
32VSWARNING(26594)
33VSWARNING(4702) // Unreachable code warnings in release (?)
34template <class T> class Vector2 {
35 public:
36 using CompType = T;
37 static constexpr uint32 vdim = 2;
38
39 // Creators
41#ifdef _DEBUG
42 constexpr Vector2() : a_({initVal<T>(),initVal<T>()}) { }
43#else
44 Vector2() { }
45#endif
47 constexpr Vector2(const Vector2<T> &v) : a_(v.a_) { }
49 constexpr Vector2(const T &x,const T &y) : a_({x,y}) { }
51 explicit constexpr Vector2(const T &t) : a_({t,t}) { }
52 constexpr const Vector2<T> &operator=(const Vector2<T> &in) { a_ = in.a_; return *this; }
55
56 // Accessorse
58 constexpr const T &x() const { return a_[0]; }
60 constexpr const T &y() const { return a_[1]; }
62 constexpr const T &dof(unsigned u) const { return a_[u]; }
64 T &rx() { return a_[0]; }
66 T &ry() { return a_[1]; }
68 T &rdof(unsigned u) { return a_[u]; }
70 constexpr const T &operator[](unsigned u) const { return dof(u); }
71 T & operator[](unsigned u) { return rdof(u); }
72 template <std::size_t N> constexpr decltype(auto) get() const {
73 static_assert(N<2,"Index out of range");
74 return a_[N];
75 }
76
77 // Various norms
79 constexpr T fsum() const { return (std::abs(x())+std::abs(y())); }
81 constexpr T sum() const { return x() + y(); }
83 constexpr T mag2() const { return (x()*x()+y()*y()); }
85 constexpr T mag() const { return to<T>(std::sqrt(to<double>(mag2()))); }
87 constexpr T area() const { return (x()*y()); }
89 constexpr T volume() const { return area(); }
91 constexpr T spread() const { return (y() -x()); }
93 constexpr Vector2<T> unit() const { Vector2<T> v(*this); T m(v.mag()); if (m) v/=m; return v; }
95 constexpr Vector2<T> abs() const { Vector2<T> v(std::abs(x()),std::abs(y())); return v; }
97 constexpr void fill(const T &d) { rx()=d; ry()=d; }
99 constexpr T maxComp() const { return std::max(x(),y()); }
101 constexpr T minComp() const { return std::min(x(),y()); }
103 constexpr unsigned maxCompIndex() const { return x() > y() ? 0 : 1; }
105 constexpr unsigned minCompIndex() const { return x() < y() ? 0 : 1; }
107 constexpr const Vector2<T> &safeDivE(const Vector2<T> &v) { rx()=::safeDiv(x(),v.x()); ry() = ::safeDiv(y(),v.y()); return *this; }
109 constexpr const Vector2<T> safeDiv(const Vector2<T> &v) { Vector2<T> ret(::safeDiv(x(),v.x()),::safeDiv(y(),v.y())); return ret; }
110
112 constexpr bool operator==(const Vector2<T> &v) const { return ((x()==v.x())&&(y()==v.y())); }
114 constexpr bool operator!=(const Vector2<T> &v) const { return ((x()!=v.x())||(y()!=v.y())); }
116 constexpr bool operator<(const Vector2<T> &v) const { if (x()!=v.x()) return x()<v.x(); return y()<v.y(); }
118 constexpr bool operator>(const Vector2<T> &v) const { if (x()!=v.x()) return x()>v.x(); return y()>v.y(); }
119
120 // In-place operators
122 constexpr const Vector2<T> &operator+=(const Vector2<T> &v) { rx()+=v.x(); ry()+=v.y(); return *this; }
123 constexpr const Vector2<T> &operator-=(const Vector2<T> &v) { rx()-=v.x(); ry()-=v.y(); return *this; }
124 constexpr const Vector2<T> &operator*=(const Vector2<T> &v) { rx()*=v.x(); ry()*=v.y(); return *this; }
125 constexpr const Vector2<T> &operator*=(const T &t) { rx()*=t; ry()*=t; return *this; }
126 constexpr const Vector2<T> &operator/=(const Vector2<T> &v) { rx()/=v.x(); ry()/=v.y(); return *this; }
127 constexpr const Vector2<T> &operator/=(const T &t) { rx()/=t; ry()/=t; return *this; }
128
129 // Binary operators - require temp (ugh)
131 constexpr Vector2<T> operator+(const Vector2<T> &v) const { Vector2<T> out(x()+v.x(),y()+v.y()); return out; }
132 constexpr Vector2<T> operator-(const Vector2<T> &v) const { Vector2<T> out(x()-v.x(),y()-v.y()); return out; }
133 constexpr Vector2<T> operator*(const Vector2<T> &v) const { Vector2<T> out(x()*v.x(),y()*v.y()); return out; }
134 constexpr Vector2<T> operator*(const T &t) const { Vector2<T> out(x()*t,y()*t); return out; }
135 constexpr Vector2<T> operator/(const Vector2<T> &v) const { Vector2<T> out(x()/v.x(),y()/v.y()); return out; }
136 constexpr Vector2<T> operator/(const T &t) const { Vector2<T> out(x()/t,y()/t); return out; }
137 // Cross Product (see DAVect)
139 constexpr T operator|(const Vector2<T> &v) const { return (x()*v.x()+y()*v.y()); }
140
141 // Support for use of a Vect2 as a range (min,max)
143 constexpr const Vector2<T> &expandToInclude(const T &t) { rx() = std::min(x(),t); ry() = std::max(y(),t); return *this; }
145 constexpr const Vector2<T> &expandToInclude(const Vector2<T> &v) { rx() = std::min(x(),v.minComp()); ry() = std::max(y(),v.maxComp()); return *this; }
147 constexpr Vector2<T> expandedToInclude(const T &t) const { Vector2<T> ret(*this); ret.expandToInclude(t); return ret; }
149 constexpr Vector2<T> expandedToInclude(const Vector2<T> &v) const { Vector2<T> ret(*this); ret.expandToInclude(v); return ret; }
151 constexpr bool contains(const T &t) const { if (t<x()) return false; if (t>y()) return false; return true; }
152
153private:
154 std::array<T,2> a_;
155};
156POPWARNING
157
160PUSHWARNING
161VSWARNING(26495) // Uninitialize in VS code analysis - on purpose
162VSWARNING(4702) // Weird unreachable code warnings in release
163template <class T> class Vector3 {
164 public:
165 using CompType = T;
166 static constexpr uint32 vdim = 3;
167 // Creators
169#ifdef _DEBUG
170 constexpr Vector3() : a_({initVal<T>(),initVal<T>(),initVal<T>()}) { }
171#else
173#endif
175 constexpr Vector3(const Vector3<T> &v) : a_(v.a_) { }
177 constexpr Vector3(const T &x,const T &y,const T &z=0) : a_({x,y,z}) { }
179 explicit constexpr Vector3(const T &t) : a_({t,t,t}) { }
180 constexpr const Vector3<T> &operator=(const Vector3<T> &in) { a_ = in.a_; return *this; }
181
182 // Accessors
184 constexpr const T &x() const { return a_[0]; }
186 constexpr const T &y() const { return a_[1]; }
188 constexpr const T &z() const { return a_[2]; }
190 constexpr const T &dof(unsigned u) const { return a_[u]; }
192 T &rx() { return a_[0]; }
194 T &ry() { return a_[1]; }
196 T &rz() { return a_[2]; }
198 T &rdof(unsigned u) { return a_[u]; }
200 constexpr const T &operator[](unsigned u) const { return dof(u); }
202 T &operator[](unsigned u) { return rdof(u); }
203 template <std::size_t N> constexpr T get() const {
204 static_assert(N<3,"Index out of range");
205 return a_[N];
206 }
207
208 // Various norms
210 constexpr T fsum() const { return (std::abs(x())+std::abs(y())+std::abs(z())); }
212 constexpr T sum() const { return x()+y()+z(); }
214 constexpr T mag2() const { return (x()*x()+y()*y()+z()*z()); }
216 constexpr T mag() const { return to<T>(std::sqrt(to<double>(mag2()))); }
218 constexpr T volume() const { return (x()*y()*z()); }
220 Vector3<T> unit() const { Vector3<T> v(*this); T m(v.mag()); if (m) v/=m; return v; }
222 constexpr Vector3<T> abs() const { Vector3<T> v(std::abs(x()),std::abs(y()),std::abs(z())); return v; }
224 void fill(const T &d) { rx()=d; ry()=d; rz()=d; }
226 constexpr T maxComp() const { return std::max(x(),std::max(y(),z())); }
228 constexpr T minComp() const { return std::min(x(),std::min(y(),z())); }
230 constexpr unsigned maxCompIndex() const { return x() > y() ? (x() > z() ? 0 : 2) : (y() > z() ? 1 : 2); }
232 constexpr unsigned minCompIndex() const { return x() < y() ? (x() < z() ? 0 : 2) : (y() < z() ? 1 : 2); }
234 constexpr const Vector3<T> &safeDivE(const Vector3<T> &v) { rx()=::safeDiv(x(),v.x()); ry() = ::safeDiv(y(),v.y()); rz() = ::safeDiv(z(),v.z()); return *this; }
236 constexpr const Vector3<T> safeDiv(const Vector3<T> &v) { Vector3<T> ret(::safeDiv(x(),v.x()),::safeDiv(y(),v.y()),::safeDiv(z(),v.z())); return ret; }
237
239 constexpr bool operator==(const Vector3<T> &v) const { return ((x()==v.x())&&(y()==v.y())&&(z()==v.z())); }
241 constexpr bool operator!=(const Vector3<T> &v) const { return ((x()!=v.x())||(y()!=v.y())||(z()!=v.z())); }
243 constexpr bool operator<(const Vector3<T> &v) const { if (x()!=v.x()) return x()<v.x(); if (y()!=v.y()) return y()<v.y(); return z()<v.z(); }
245 constexpr bool operator>(const Vector3<T> &v) const { if (x()!=v.x()) return x()>v.x(); if (y()!=v.y()) return y()>v.y(); return z()>v.z(); }
246
247 // In-place operators
249 constexpr const Vector3<T> &operator+=(const Vector3<T> &v) { rx()+=v.x(); ry()+=v.y(); rz()+=v.z(); return *this; }
250 constexpr const Vector3<T> &operator-=(const Vector3<T> &v) { rx()-=v.x(); ry()-=v.y(); rz()-=v.z(); return *this; }
251 constexpr const Vector3<T> &operator*=(const Vector3<T> &v) { rx()*=v.x(); ry()*=v.y(); rz()*=v.z(); return *this; }
252 constexpr const Vector3<T> &operator*=(const T &t) { rx()*=t; ry()*=t; rz()*=t; return *this; }
253 constexpr const Vector3<T> &operator/=(const Vector3<T> &v) { rx()/=v.x(); ry()/=v.y(); rz()/=v.z(); return *this; }
254 constexpr const Vector3<T> &operator/=(const T &t) { rx()/=t; ry()/=t; rz()/=t; return *this; }
255
256 // Binary operators - require temp (ugh)
258 constexpr Vector3<T> operator+(const Vector3<T> &v) const { Vector3<T> out(x()+v.x(),y()+v.y(),z()+v.z()); return out; }
259 constexpr Vector3<T> operator-(const Vector3<T> &v) const { Vector3<T> out(x()-v.x(),y()-v.y(),z()-v.z()); return out; }
260 constexpr Vector3<T> operator*(const Vector3<T> &v) const { Vector3<T> out(x()*v.x(),y()*v.y(),z()*v.z()); return out; }
261 constexpr Vector3<T> operator*(const T &t) const { Vector3<T> out(x()*t,y()*t,z()*t); return out; }
262 constexpr Vector3<T> operator/(const Vector3<T> &v) const { Vector3<T> out(x()/v.x(),y()/v.y(),z()/v.z()); return out; }
263 constexpr Vector3<T> operator/(const T &t) const { Vector3<T> out(x()/t,y()/t,z()/t); return out; }
265 constexpr Vector3<T> operator&(const Vector3<T> &v) const {
266 Vector3<T> out((y()*v.z())-(z()*v.y()),(z()*v.x())-(x()*v.z()),(x()*v.y())-(y()*v.x()));
267 return out;
268 }
270 constexpr T operator|(const Vector3<T> &v) const { return (x()*v.x()+y()*v.y()+z()*v.z()); }
271
272private:
273 std::array<T,3> a_;
274};
275POPWARNING
276
277// Naming convention for most commonly used types.
278typedef Vector2<double> DVect2;
279typedef Vector2<float> FVect2;
280typedef Vector2<int32> IVect2;
281typedef Vector2<uint32> UVect2;
282typedef Vector2<int64> I64Vect2;
283typedef Vector2<uint64> U64Vect2;
284
285typedef Vector3<double> DVect3;
286typedef Vector3<float> FVect3;
287typedef Vector3<int32> IVect3;
288typedef Vector3<uint32> UVect3;
289typedef Vector3<int64> I64Vect3;
290typedef Vector3<uint64> U64Vect3;
291
292// Conversion routines.
295template <class T> inline constexpr DVect2 toDVect2(const Vector2<T> &v) { DVect2 dv2(to<double>(v.x()),to<double>(v.y())); return dv2; }
298template <class T> inline constexpr FVect2 toFVect2(const Vector2<T> &v) { FVect2 fv2(to<float>(v.x()),to<float>(v.y())); return fv2; }
301template <class T> inline constexpr IVect2 toIVect2(const Vector2<T> &v) { IVect2 iv2(to<int32>(v.x()),to<int32>(v.y())); return iv2; }
302template <class T> inline constexpr I64Vect2 toI64Vect2(const Vector2<T> &v) { I64Vect2 iv2(to<int64>(v.x()),to<int64>(v.y())); return iv2; }
305template <class T> inline constexpr UVect2 toUVect2(const Vector2<T> &v) { UVect2 uv2(to<uint32>(v.x()),to<uint32>(v.y())); return uv2; }
306template <class T> inline constexpr U64Vect2 toU64Vect2(const Vector2<T> &v) { U64Vect2 uv2(to<uint64>(v.x()),to<uint64>(v.y())); return uv2; }
307
310template <class T> inline constexpr DVect3 toDVect3(const Vector3<T> &v) {
311 DVect3 dv3(to<double>(v.x()),to<double>(v.y()),to<double>(v.z()));
312 return dv3;
313}
316template <class T> inline constexpr FVect3 toFVect3(const Vector3<T> &v) {
317 FVect3 fv3(to<float>(v.x()),to<float>(v.y()),to<float>(v.z()));
318 return fv3;
319}
322template <class T> inline constexpr IVect3 toIVect3(const Vector3<T> &v) {
323 IVect3 iv3(to<int32>(v.x()),to<int32>(v.y()),to<int32>(v.z()));
324 return iv3;
325}
326template <class T> inline I64Vect3 toI64Vect3(const Vector3<T> &v) {
327 I64Vect3 iv3(to<int64>(v.x()),to<int64>(v.y()),to<int64>(v.z()));
328 return iv3;
329}
332template <class T> inline constexpr U64Vect3 toU64Vect3(const Vector3<T> &v) {
333 U64Vect3 uv3(to<uint64>(v.x()),to<uint64>(v.y()),to<uint64>(v.z()));
334 return uv3;
335}
336
337
339template <class T> inline constexpr const Vector2<T> &toVect2(const Vector2<T> &v) { return v; }
340template <class T> inline constexpr Vector2<T> toVect2(const Vector3<T> &v) { return Vector2<T>(v.x(),v.y()); }
341template <class T> inline constexpr Vector3<T> toVect3(const Vector2<T> &v,const T &t=0) { return Vector3<T>(v.x(),v.y(),t); }
342template <class T> inline constexpr const Vector3<T> &toVect3(const Vector3<T> &v) { return v; }
343
344
347template <class T> inline constexpr Vector2<T> vmax(const Vector2<T> &v1,const Vector2<T> &v2) {
348 Vector2<T> out(std::max<T>(v1.x(),v2.x()),std::max<T>(v1.y(),v2.y()));
349 return out;
350}
353template <class T> inline constexpr Vector2<T> vmin(const Vector2<T> &v1,const Vector2<T> &v2) {
354 Vector2<T> out(std::min<T>(v1.x(),v2.x()),std::min<T>(v1.y(),v2.y()));
355 return out;
356}
357
360template <class T> inline constexpr Vector2<T> vsign(const Vector2<T> &v1,const Vector2<T> &v2) {
361 Vector2<T> out(v2.x() < 0 ? -qAbs(v1.x()) : qAbs(v1.x()) ,v2.y() < 0 ? -qAbs(v1.y()) : qAbs(v1.y()));
362 return out;
363}
364
367template <class T> inline constexpr Vector3<T> vmax(const Vector3<T> &v1,const Vector3<T> &v2) {
368 Vector3<T> out(std::max<T>(v1.x(),v2.x()),std::max<T>(v1.y(),v2.y()),std::max<T>(v1.z(),v2.z()));
369 return out;
370}
373template <class T> inline constexpr Vector3<T> vmin(const Vector3<T> &v1,const Vector3<T> &v2) {
374 Vector3<T> out(std::min<T>(v1.x(),v2.x()),std::min<T>(v1.y(),v2.y()),std::min<T>(v1.z(),v2.z()));
375 return out;
376}
377
380template <class T> inline constexpr Vector3<T> vsign(const Vector3<T> &v1,const Vector3<T> &v2) {
381 Vector3<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()));
382 return out;
383}
384
387template <class T> inline Vector2<T> vceil(const Vector2<T> &v) {
388 Vector2<T> out(ceil(v.x()),ceil(v.y()));
389 return out;
390}
391
394template <class T> inline Vector3<T> vceil(const Vector3<T> &v) {
395 Vector3<T> out(ceil(v.x()),ceil(v.y()),ceil(v.z()));
396 return out;
397}
398
401template <class T> inline Vector2<T> vfloor(const Vector2<T> &v) {
402 Vector2<T> out(floor(v.x()),floor(v.y()));
403 return out;
404}
405
408template <class T> inline Vector3<T> vfloor(const Vector3<T> &v) {
409 Vector3<T> out(floor(v.x()),floor(v.y()),floor(v.z()));
410 return out;
411}
412
415template <class T> IVect2 vround(const Vector2<T> &v) {
416 IVect2 out(qRound(v.x()),qRound(v.y()));
417 return out;
418}
419
422template <class T> IVect3 constexpr vround(const Vector3<T> &v) {
423 IVect3 out(qRound(v.x()),qRound(v.y()),qRound(v.z()));
424 return out;
425}
426template <class T> I64Vect3 constexpr v64round(const Vector3<T> &v) {
427 I64Vect3 out(std::llround(v.x()),std::llround(v.y()),std::llround(v.z()));
428 return out;
429}
430
431namespace std {
433 template <class T> constexpr Vector2<T> max(const Vector2<T> &v1,const Vector2<T> &v2) { return vmax(v1,v2); }
435 template <class T> constexpr Vector2<T> min(const Vector2<T> &v1,const Vector2<T> &v2) { return vmin(v1,v2); }
437 template <class T> constexpr Vector3<T> max(const Vector3<T> &v1,const Vector3<T> &v2) { return vmax(v1,v2); }
439 template <class T> constexpr Vector3<T> min(const Vector3<T> &v1,const Vector3<T> &v2) { return vmin(v1,v2); }
440
442 template <class T> struct tuple_size<Vector2<T>> : std::integral_constant<std::size_t, 2> {};
443 template <class T> struct tuple_size<Vector3<T>> : std::integral_constant<std::size_t, 3> {};
444 template <std::size_t N,class T> struct tuple_element<N, Vector2<T>> { using type = T; };
445 template <std::size_t N,class T> struct tuple_element<N, Vector3<T>> { using type = T; };
446}
447
448namespace base {
449 // ts (tostring) support for vectors
450 // fs (fromstring) support not implemented yet.
451 namespace basehelper {
452 template <typename T>
453 string tsVect(const Vector2<T> &t, int width, char notation, int precision, char fill) {
454 if (abs(width) > 5) width = width < 0 ? -abs(abs(width) - 3) : abs(abs(width) - 3); // Subtraci (,)
455 else width = 0;
456 int w2 = std::abs(width)/2;
457 int w1 = std::abs(width) - w2;
458 if (width<0) { w1 *= -1; w2 *= -1; }
459 return "("+ts(t.x(), w1, notation, precision, fill)+","
460 +ts(t.y(), w2, notation, precision, fill)+")";
461 }
462 template <typename T>
463 string tsVect(const Vector3<T> &t, int width, char notation, int precision, char fill) {
464 if (abs(width) > 7) width = width < 0 ? -abs(abs(width) - 4) : abs(abs(width) - 4);// Subtraci (,,)
465 else width = 0;
466 int w3 = std::abs(width)/3;
467 int w2 = (std::abs(width)-w3)/2;
468 int w1 = std::abs(width) - w2 - w3;
469 if (width<0) { w1 *= -1; w2 *= -1; w3 *= -1; }
470 return "("+ts(t.x(), w1, notation, precision, fill)+","
471 +ts(t.y(), w2, notation, precision, fill)+","
472 +ts(t.z(), w3, notation, precision, fill)+")";
473 }
474 }
475
476 template <>
477 inline string ts(const DVect2 &t, int width, char notation, int precision, char fill) {
478 return basehelper::tsVect(t, width, notation, precision, fill);
479 }
480
481 template <>
482 inline string ts(const DVect3 &t, int width, char notation, int precision, char fill) {
483 return basehelper::tsVect(t, width, notation, precision, fill);
484 }
485
486 template <>
487 inline string ts(const IVect2 &t, int width, char notation, int precision, char fill) {
488 return basehelper::tsVect(t, width, notation, precision, fill);
489 }
490
491 template <>
492 inline string ts(const IVect3 &t, int width, char notation, int precision, char fill) {
493 return basehelper::tsVect(t, width, notation, precision, fill);
494 }
495
496 template <>
497 inline string ts(const I64Vect2 &t, int width, char notation, int precision, char fill) {
498 return basehelper::tsVect(t, width, notation, precision, fill);
499 }
500
501 template <>
502 inline string ts(const I64Vect3 &t, int width, char notation, int precision, char fill) {
503 return basehelper::tsVect(t, width, notation, precision, fill);
504 }
505}
506
507// This allows you to send Vector2<T> to a fmt::format
508// Each component is formatted as using fmt::format double format specification.
509// This means if W is the given width the actual width will be 2*W+3
510template <class T>
511struct fmt::formatter<Vector2<T>> : public fmt::formatter<double> {
512 template <typename ParseContext>
513 constexpr auto parse(ParseContext &ctx) { return fmt::formatter<double>::parse(ctx); }
514
515 // I'm sure there is a better way to do this
516 template <typename FormatContext>
517 auto format(Vector2<T> const &val, FormatContext &ctx) {
518 format_to(ctx.out(),"(");
519 fmt::formatter<double>::format(val.x(),ctx);
520 format_to(ctx.out(),",");
521 fmt::formatter<double>::format(val.y(),ctx);
522 return format_to(ctx.out(),")");
523 }
524};
525
526// This allows you to send Vector3<T> to a fmt::format
527// Each component is formatted as using fmt::format double format specification.
528// This means if W is the given width the actual width will be 3*W+4
529template <class T>
530struct fmt::formatter<Vector3<T>> : public fmt::formatter<double> {
531 template <typename ParseContext>
532 constexpr auto parse(ParseContext &ctx) { return fmt::formatter<double>::parse(ctx); }
533
534 // I'm sure there is a better way to do this
535 template <typename FormatContext>
536 auto format(Vector3<T> const &val, FormatContext &ctx) {
537 format_to(ctx.out(),"(");
538 fmt::formatter<double>::format(val.x(),ctx);
539 format_to(ctx.out(),",");
540 fmt::formatter<double>::format(val.y(),ctx);
541 format_to(ctx.out(),",");
542 fmt::formatter<double>::format(val.z(),ctx);
543 return format_to(ctx.out(),")");
544 }
545};
547// EoF
548
QString helper functions, plus some additions.
2D vector utility class.
Definition vect.h:34
constexpr Vector2< T > operator*(const Vector2< T > &v) const
Binary mathematical operators using vectors and scalars – Note that * and / of Vector2 types are done...
Definition vect.h:133
constexpr T minComp() const
Returns the minimum component.
Definition vect.h:101
constexpr bool operator>(const Vector2< T > &v) const
Comparison operator, based on magnitude.
Definition vect.h:118
constexpr const Vector2< T > & operator/=(const Vector2< T > &v)
In place mathematical operators using vectors and scalars – Note that * and / of Vector2 types are do...
Definition vect.h:126
constexpr Vector2< T > unit() const
Unit vector - be sure vector is nonzero.
Definition vect.h:93
constexpr void fill(const T &d)
Fills all three components with value d.
Definition vect.h:97
constexpr T mag2() const
Square of the magnitude, or the dot product with itself.
Definition vect.h:83
constexpr Vector2(const Vector2< T > &v)
Copy constructor.
Definition vect.h:47
constexpr Vector2< T > expandedToInclude(const T &t) const
Returns 1D range expanded to include value t;.
Definition vect.h:147
T & operator[](unsigned u)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition vect.h:71
constexpr const Vector2< T > & expandToInclude(const Vector2< T > &v)
Expands 1D range to include 1D range v.
Definition vect.h:145
constexpr const Vector2< T > & operator/=(const T &t)
In place mathematical operators using vectors and scalars – Note that * and / of Vector2 types are do...
Definition vect.h:127
constexpr const Vector2< T > & expandToInclude(const T &t)
Expands 1D range to include value t.
Definition vect.h:143
constexpr unsigned maxCompIndex() const
Returns the max component index.
Definition vect.h:103
constexpr const Vector2< T > & operator+=(const Vector2< T > &v)
In place mathematical operators using vectors and scalars – Note that * and / of Vector2 types are do...
Definition vect.h:122
constexpr T spread() const
Assumes vector is being used to store a 1D extent– Returns max-min. (y-x).
Definition vect.h:91
constexpr const Vector2< T > & operator-=(const Vector2< T > &v)
In place mathematical operators using vectors and scalars – Note that * and / of Vector2 types are do...
Definition vect.h:123
constexpr const Vector2< T > safeDiv(const Vector2< T > &v)
"Safe" division operation - checks for zero and overflow.
Definition vect.h:109
constexpr bool operator==(const Vector2< T > &v) const
Comparison operator - no tolerance is used.
Definition vect.h:112
constexpr T operator|(const Vector2< T > &v) const
Dot Product.
Definition vect.h:139
constexpr const Vector2< T > & operator*=(const Vector2< T > &v)
In place mathematical operators using vectors and scalars – Note that * and / of Vector2 types are do...
Definition vect.h:124
constexpr const T & operator[](unsigned u) const
Allow array like access by degree of freedom (0-1)
Definition vect.h:70
constexpr Vector2< T > operator-(const Vector2< T > &v) const
Binary mathematical operators using vectors and scalars – Note that * and / of Vector2 types are done...
Definition vect.h:132
constexpr const T & x() const
X component access.
Definition vect.h:58
constexpr T volume() const
Volume of the rectangle assuming unit depth – same as area(), provided for 2D/3D compile compatibilit...
Definition vect.h:89
constexpr Vector2< T > expandedToInclude(const Vector2< T > &v) const
Returns 1D range expanded to include 1D range v.
Definition vect.h:149
constexpr T mag() const
The magnitude.
Definition vect.h:85
constexpr const T & dof(unsigned u) const
Access to degree of freedom u (0-1).
Definition vect.h:62
T & rdof(unsigned u)
Reference accesss to degree-of-freedom u (0-1).
Definition vect.h:68
constexpr T maxComp() const
Returns the maximum component.
Definition vect.h:99
constexpr bool operator<(const Vector2< T > &v) const
Comparison operator, based on magnitude.
Definition vect.h:116
constexpr bool contains(const T &t) const
True if value t falls inside this 1D range (inclusive).
Definition vect.h:151
constexpr T fsum() const
Manhattan norm.
Definition vect.h:79
constexpr const Vector2< T > & operator*=(const T &t)
In place mathematical operators using vectors and scalars – Note that * and / of Vector2 types are do...
Definition vect.h:125
constexpr Vector2< T > abs() const
Returns vector of absolute values of components.
Definition vect.h:95
constexpr Vector2< T > operator/(const T &t) const
Binary mathematical operators using vectors and scalars – Note that * and / of Vector2 types are done...
Definition vect.h:136
constexpr Vector2< T > operator*(const T &t) const
Binary mathematical operators using vectors and scalars – Note that * and / of Vector2 types are done...
Definition vect.h:134
static Vector2< T > nothing()
Creates an "empty" vector, useful when Vector2 is used as a 1D extent.
Definition vect.h:54
constexpr const Vector2< T > & safeDivE(const Vector2< T > &v)
"Safe" division operation - checks for zero and overflow.
Definition vect.h:107
constexpr bool operator!=(const Vector2< T > &v) const
Comparison operator - no tolerance is used.
Definition vect.h:114
constexpr Vector2< T > operator/(const Vector2< T > &v) const
Binary mathematical operators using vectors and scalars – Note that * and / of Vector2 types are done...
Definition vect.h:135
constexpr T area() const
Size of rectangle represented by x*y - can be negative.
Definition vect.h:87
constexpr unsigned minCompIndex() const
Returns the min component index.
Definition vect.h:105
T & rx()
Reference access to x-component.
Definition vect.h:64
constexpr T sum() const
Sum of components.
Definition vect.h:81
Vector2()
Default constructor, no data initialization.
Definition vect.h:44
constexpr Vector2(const T &t)
Explicit contructor, each component is given value t.
Definition vect.h:51
constexpr const T & y() const
Y component access.
Definition vect.h:60
constexpr Vector2< T > operator+(const Vector2< T > &v) const
Binary mathematical operators using vectors and scalars – Note that * and / of Vector2 types are done...
Definition vect.h:131
constexpr Vector2(const T &x, const T &y)
Explicit constructor, from two components.
Definition vect.h:49
T & ry()
Reference access to y-component.
Definition vect.h:66
3D vector utility class.
Definition vect.h:163
constexpr const Vector3< T > safeDiv(const Vector3< T > &v)
"Safe" division operation - checks for zero and overflow.
Definition vect.h:236
constexpr bool operator<(const Vector3< T > &v) const
Comparison operator, compare each DOF in order.
Definition vect.h:243
constexpr const T & y() const
The y-component of the vector.
Definition vect.h:186
T & rx()
Reference access to the x-component of the vector.
Definition vect.h:192
constexpr T maxComp() const
Returns the maximum component.
Definition vect.h:226
constexpr Vector3< T > operator&(const Vector3< T > &v) const
Cross Product.
Definition vect.h:265
constexpr const Vector3< T > & safeDivE(const Vector3< T > &v)
"Safe" division operation - checks for zero and overflow.
Definition vect.h:234
constexpr T operator|(const Vector3< T > &v) const
Dot Product.
Definition vect.h:270
T & ry()
Reference access to the y-component of the vector.
Definition vect.h:194
constexpr const Vector3< T > & operator-=(const Vector3< T > &v)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:250
constexpr const Vector3< T > & operator+=(const Vector3< T > &v)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:249
constexpr unsigned minCompIndex() const
Returns the min component index.
Definition vect.h:232
constexpr Vector3< T > operator+(const Vector3< T > &v) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:258
constexpr bool operator>(const Vector3< T > &v) const
Comparison operator, compare each DOF in order.
Definition vect.h:245
constexpr const Vector3< T > & operator*=(const Vector3< T > &v)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:251
constexpr T fsum() const
Manhattan norm.
Definition vect.h:210
constexpr Vector3< T > operator/(const T &t) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:263
constexpr Vector3(const Vector3< T > &v)
Copy constructor.
Definition vect.h:175
Vector3()
Default constructor - no data initialization.
Definition vect.h:172
constexpr T minComp() const
Returns the minimum component.
Definition vect.h:228
constexpr const Vector3< T > & operator/=(const Vector3< T > &v)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:253
constexpr const Vector3< T > & operator/=(const T &t)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:254
constexpr Vector3< T > operator*(const Vector3< T > &v) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:260
constexpr const T & x() const
The x-component of the vector.
Definition vect.h:184
constexpr T mag2() const
Square of the magnitude, or the vector dotted with itself.
Definition vect.h:214
constexpr bool operator!=(const Vector3< T > &v) const
Comparison operator - no tolerance is used.
Definition vect.h:241
constexpr Vector3< T > operator/(const Vector3< T > &v) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:262
constexpr const Vector3< T > & operator*=(const T &t)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:252
T & rdof(unsigned u)
Reference access to degree-of-freedom u (0-2) of the vector.
Definition vect.h:198
constexpr Vector3(const T &t)
Explicit constructor, all three components are inintialized to t.
Definition vect.h:179
constexpr bool operator==(const Vector3< T > &v) const
Comparison operator - no tolerance is used.
Definition vect.h:239
constexpr T volume() const
Volume of region represented by x*y*z - can be negative.
Definition vect.h:218
void fill(const T &d)
Fills all three components with value d.
Definition vect.h:224
Vector3< T > unit() const
Unit vector - be sure vector is nonzero.
Definition vect.h:220
constexpr unsigned maxCompIndex() const
Returns the max component index.
Definition vect.h:230
constexpr Vector3< T > abs() const
Returns vector of absolute values of components.
Definition vect.h:222
constexpr const T & z() const
The z-component of the vector.
Definition vect.h:188
constexpr Vector3< T > operator*(const T &t) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:261
constexpr T mag() const
The vector magnitude.
Definition vect.h:216
T & rz()
Reference access to the z-component of the vector.
Definition vect.h:196
T & operator[](unsigned u)
Allows array like access to degrees-of-freedom.
Definition vect.h:202
constexpr const T & operator[](unsigned u) const
Allows array like access to degrees-of-freedom.
Definition vect.h:200
constexpr Vector3(const T &x, const T &y, const T &z=0)
Explicit constructor, by the three components of the vector.
Definition vect.h:177
constexpr T sum() const
Sum of components.
Definition vect.h:212
constexpr const T & dof(unsigned u) const
The degree-of-freedom u (0-2) component.
Definition vect.h:190
constexpr Vector3< T > operator-(const Vector3< T > &v) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:259
debug checked shorthand for std::numeric_limits<T>::
Definition limit.h:25
Vector3< T > vceil(const Vector3< T > &v)
Definition vect.h:394
constexpr Vector3< T > toVect3(const Vector2< T > &v, const T &t=0)
Conversion between vectors of different dimension.
Definition vect.h:341
constexpr U64Vect3 toU64Vect3(const Vector3< T > &v)
Definition vect.h:332
IVect2 vround(const Vector2< T > &v)
Definition vect.h:415
constexpr Vector2< T > vmax(const Vector2< T > &v1, const Vector2< T > &v2)
Definition vect.h:347
constexpr FVect3 toFVect3(const Vector3< T > &v)
Definition vect.h:316
constexpr Vector3< T > vmax(const Vector3< T > &v1, const Vector3< T > &v2)
Definition vect.h:367
IVect3 constexpr vround(const Vector3< T > &v)
Definition vect.h:422
Vector2< T > vfloor(const Vector2< T > &v)
Definition vect.h:401
constexpr IVect2 toIVect2(const Vector2< T > &v)
Definition vect.h:301
Vector3< T > vfloor(const Vector3< T > &v)
Definition vect.h:408
constexpr const Vector2< T > & toVect2(const Vector2< T > &v)
Conversion between vectors of different dimension.
Definition vect.h:339
Vector2< T > vceil(const Vector2< T > &v)
Definition vect.h:387
constexpr Vector3< T > vmin(const Vector3< T > &v1, const Vector3< T > &v2)
Definition vect.h:373
constexpr DVect3 toDVect3(const Vector3< T > &v)
Definition vect.h:310
constexpr IVect3 toIVect3(const Vector3< T > &v)
Definition vect.h:322
constexpr const T safeDiv(const T num, const T denom)
This function provids "safe" division operation, checks explicitly for zero.
Definition to.h:117
constexpr UVect2 toUVect2(const Vector2< T > &v)
Definition vect.h:305
constexpr Vector3< T > vsign(const Vector3< T > &v1, const Vector3< T > &v2)
Definition vect.h:380
constexpr DVect2 toDVect2(const Vector2< T > &v)
Definition vect.h:295
constexpr Vector2< T > vsign(const Vector2< T > &v1, const Vector2< T > &v2)
Definition vect.h:360
constexpr FVect2 toFVect2(const Vector2< T > &v)
Definition vect.h:298
constexpr Vector2< T > vmin(const Vector2< T > &v1, const Vector2< T > &v2)
Definition vect.h:353
A overflow checked shorthand for static_cast<T>().
constexpr Vector2< T > max(const Vector2< T > &v1, const Vector2< T > &v2)
Template specialization for max, min.
Definition vect.h:433
constexpr Vector2< T > min(const Vector2< T > &v1, const Vector2< T > &v2)
Template specialization for max, min.
Definition vect.h:435