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
31template <class T>
32class Vector2 : public std::array<T,2> {
33public:
34 using Base = std::array<T,2>;
35 using CompType = T;
36 static constexpr uint32 vdim = 2;
37
38 constexpr Vector2() : Base({T{},T{}}) { }
40 constexpr Vector2(const Vector2<T> &v) = default;
41 constexpr Vector2<T>& operator=(const Vector2<T>& in) = default;
42
44 constexpr Vector2(const T &x,const T &y) : Base({x,y}) { }
46 explicit constexpr Vector2(const T &t) : Base({t,t}) { }
47
50
51 // Accessorse
53 constexpr const T &x() const { return dof(0); }
55 constexpr const T &y() const { return dof(1); }
56 constexpr const T &z() const { return dof(1); }
58 constexpr const T &dof(unsigned u) const { assert(u<vdim); return Base::operator[](u); }
60 T &rx() { return rdof(0); }
62 T &ry() { return rdof(1); }
63 T &rz() { return rdof(1); }
65 T &rdof(unsigned u) { assert(u<vdim); return Base::operator[](u); }
66 template <std::size_t N> constexpr decltype(auto) get() const {
67 static_assert(N<2,"Index out of range");
68 return Base::operator[](N);
69 }
70 template <> constexpr decltype(auto) get<2>() const { return dof(1); }
71
72 // Various norms
74 constexpr T fsum() const { return (std::abs(x())+std::abs(y())); }
76 constexpr T sum() const { return x() + y(); }
78 constexpr T mag2() const { return (x()*x()+y()*y()); }
80 constexpr T mag() const { return to<T>(std::sqrt(to<double>(mag2()))); }
82 constexpr T area() const { return (x()*y()); }
84 constexpr T volume() const { return area(); }
86 constexpr T spread() const { return (y() -x()); }
88 constexpr Vector2<T> unit() const { Vector2<T> v(*this); T m(v.mag()); if (m) v/=m; return v; }
90 constexpr Vector2<T> abs() const { Vector2<T> v(std::abs(x()),std::abs(y())); return v; }
92 constexpr void fill(const T &d) { rx()=d; ry()=d; }
94 constexpr T maxComp() const { return std::max(x(),y()); }
96 constexpr T minComp() const { return std::min(x(),y()); }
98 constexpr unsigned maxCompIndex() const { return x() > y() ? 0 : 1; }
100 constexpr unsigned minCompIndex() const { return x() < y() ? 0 : 1; }
102
104 constexpr bool operator==(const Vector2<T> &v) const { return ((x()==v.x())&&(y()==v.y())); }
106 constexpr bool operator!=(const Vector2<T> &v) const { return ((x()!=v.x())||(y()!=v.y())); }
108 constexpr bool operator<(const Vector2<T> &v) const { if (x()!=v.x()) return x()<v.x(); return y()<v.y(); }
110 constexpr bool operator>(const Vector2<T> &v) const { if (x()!=v.x()) return x()>v.x(); return y()>v.y(); }
111
112 // In-place operators
114 constexpr const Vector2<T> &operator+=(const Vector2<T> &v) { rx()+=v.x(); ry()+=v.y(); return *this; }
115 constexpr const Vector2<T> &operator-=(const Vector2<T> &v) { rx()-=v.x(); ry()-=v.y(); return *this; }
116 constexpr const Vector2<T> &operator*=(const Vector2<T> &v) { rx()*=v.x(); ry()*=v.y(); return *this; }
117 constexpr const Vector2<T> &operator*=(const T &t) { rx()*=t; ry()*=t; return *this; }
118 constexpr const Vector2<T> &operator/=(const Vector2<T> &v) { rx()/=v.x(); ry()/=v.y(); return *this; }
119 constexpr const Vector2<T> &operator/=(const T &t) { rx()/=t; ry()/=t; return *this; }
120
121 // Binary operators - require temp (ugh)
123 constexpr Vector2<T> operator+(const Vector2<T> &v) const { Vector2<T> out(x()+v.x(),y()+v.y()); return out; }
124 constexpr Vector2<T> operator-(const Vector2<T> &v) const { Vector2<T> out(x()-v.x(),y()-v.y()); return out; }
125 constexpr Vector2<T> operator*(const Vector2<T> &v) const { Vector2<T> out(x()*v.x(),y()*v.y()); return out; }
126 constexpr Vector2<T> operator*(const T &t) const { Vector2<T> out(x()*t,y()*t); return out; }
127 constexpr Vector2<T> operator/(const Vector2<T> &v) const { Vector2<T> out(x()/v.x(),y()/v.y()); return out; }
128 constexpr Vector2<T> operator/(const T &t) const { Vector2<T> out(x()/t,y()/t); return out; }
129 // Cross Product (see DAVect)
131 constexpr T operator|(const Vector2<T> &v) const { return (x()*v.x()+y()*v.y()); }
132
133 // Support for use of a Vect2 as a range (min,max)
135 constexpr const Vector2<T> &expandToInclude(const T &t) { rx() = std::min(x(),t); ry() = std::max(y(),t); return *this; }
137 constexpr const Vector2<T> &expandToInclude(const Vector2<T> &v) { rx() = std::min(x(),v.minComp()); ry() = std::max(y(),v.maxComp()); return *this; }
139 constexpr Vector2<T> expandedToInclude(const T &t) const { Vector2<T> ret(*this); ret.expandToInclude(t); return ret; }
141 constexpr Vector2<T> expandedToInclude(const Vector2<T> &v) const { Vector2<T> ret(*this); ret.expandToInclude(v); return ret; }
143 constexpr bool contains(const T &t) const { if (t<x()) return false; if (t>y()) return false; return true; }
144 constexpr Vector2<T> perp() const { return Vector2<T>(y(),-x()); }
145};
146
149template <class T>
150class Vector3 : public std::array<T,3> {
151public:
152 using Base = std::array<T,3>;
153 using CompType = T;
154 static constexpr uint32 vdim = 3;
155
156 constexpr Vector3() : Base({T{},T{},T{}}) {}
157 constexpr Vector3(const Vector3<T> &v)=default;
158 constexpr Vector3<T>& operator=(const Vector3<T>& in)=default;
159
161 constexpr Vector3(const T &x,const T &y,const T &z=0) : Base({x,y,z}) { }
163 explicit constexpr Vector3(const T &t) : Base({t,t,t}) { }
164
165 // Accessors
167 constexpr const T &x() const { return dof(0); }
169 constexpr const T &y() const { return dof(1); }
171 constexpr const T &z() const { return dof(2); }
173 constexpr const T &dof(unsigned u) const { assert(u<vdim); return Base::operator[](u); }
175 T &rx() { return rdof(0); }
177 T &ry() { return rdof(1); }
179 T &rz() { return rdof(2); }
181 T &rdof(unsigned u) { assert(u<vdim); return Base::operator[](u); }
182 template <std::size_t N> constexpr T get() const {
183 static_assert(N<3,"Index out of range");
184 return Base::operator[](N);
185 }
186
187 // Various norms
189 constexpr T fsum() const { return (std::abs(x())+std::abs(y())+std::abs(z())); }
191 constexpr T sum() const { return x()+y()+z(); }
193 constexpr T mag2() const { return (x()*x()+y()*y()+z()*z()); }
195 constexpr T mag() const { return to<T>(std::sqrt(to<double>(mag2()))); }
197 constexpr T volume() const { return (x()*y()*z()); }
199 Vector3<T> unit() const { Vector3<T> v(*this); T m(v.mag()); v /= m; return v; }
201 constexpr Vector3<T> abs() const { Vector3<T> v(std::abs(x()),std::abs(y()),std::abs(z())); return v; }
203 void fill(const T &d) { rx()=d; ry()=d; rz()=d; }
205 constexpr T maxComp() const { return std::max(x(),std::max(y(),z())); }
207 constexpr T minComp() const { return std::min(x(),std::min(y(),z())); }
209 constexpr unsigned maxCompIndex() const { return x() > y() ? (x() > z() ? 0 : 2) : (y() > z() ? 1 : 2); }
211 constexpr unsigned minCompIndex() const { return x() < y() ? (x() < z() ? 0 : 2) : (y() < z() ? 1 : 2); }
212
214 constexpr bool operator==(const Vector3<T> &v) const { return ((x()==v.x())&&(y()==v.y())&&(z()==v.z())); }
216 constexpr bool operator!=(const Vector3<T> &v) const { return ((x()!=v.x())||(y()!=v.y())||(z()!=v.z())); }
218 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(); }
220 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(); }
221
222 // In-place operators
224 constexpr const Vector3<T> &operator+=(const Vector3<T> &v) { rx()+=v.x(); ry()+=v.y(); rz()+=v.z(); return *this; }
225 constexpr const Vector3<T> &operator-=(const Vector3<T> &v) { rx()-=v.x(); ry()-=v.y(); rz()-=v.z(); return *this; }
226 constexpr const Vector3<T> &operator*=(const Vector3<T> &v) { rx()*=v.x(); ry()*=v.y(); rz()*=v.z(); return *this; }
227 constexpr const Vector3<T> &operator*=(const T &t) { rx()*=t; ry()*=t; rz()*=t; return *this; }
228 constexpr const Vector3<T> &operator/=(const Vector3<T> &v) { rx()/=v.x(); ry()/=v.y(); rz()/=v.z(); return *this; }
229 constexpr const Vector3<T> &operator/=(const T &t) { rx()/=t; ry()/=t; rz()/=t; return *this; }
230
231 // Binary operators - require temp (ugh)
233 constexpr Vector3<T> operator+(const Vector3<T> &v) const { Vector3<T> out(x()+v.x(),y()+v.y(),z()+v.z()); return out; }
234 constexpr Vector3<T> operator-(const Vector3<T> &v) const { Vector3<T> out(x()-v.x(),y()-v.y(),z()-v.z()); return out; }
235 constexpr Vector3<T> operator*(const Vector3<T> &v) const { Vector3<T> out(x()*v.x(),y()*v.y(),z()*v.z()); return out; }
236 constexpr Vector3<T> operator*(const T &t) const { Vector3<T> out(x()*t,y()*t,z()*t); return out; }
237 constexpr Vector3<T> operator/(const Vector3<T> &v) const { Vector3<T> out(x()/v.x(),y()/v.y(),z()/v.z()); return out; }
238 constexpr Vector3<T> operator/(const T &t) const { Vector3<T> out(x()/t,y()/t,z()/t); return out; }
240 constexpr Vector3<T> operator&(const Vector3<T> &v) const {
241 Vector3<T> out((y()*v.z())-(z()*v.y()),(z()*v.x())-(x()*v.z()),(x()*v.y())-(y()*v.x()));
242 return out;
243 }
245 constexpr T operator|(const Vector3<T> &v) const { return (x()*v.x()+y()*v.y()+z()*v.z()); }
246};
247
248// Naming convention for most commonly used types.
249using DVect2 = Vector2<double> ;
250using FVect2 = Vector2<float> ;
251using IVect2 = Vector2<int32> ;
252using UVect2 = Vector2<uint32> ;
253using I64Vect2 = Vector2<int64> ;
254using U64Vect2 = Vector2<uint64> ;
255
256using DVect3 = Vector3<double> ;
257using FVect3 = Vector3<float> ;
258using IVect3 = Vector3<int32> ;
259using UVect3 = Vector3<uint32> ;
260using I64Vect3 = Vector3<int64> ;
261using U64Vect3 = Vector3<uint64> ;
262
263// Conversion routines.
266template <class T> inline constexpr DVect2 toDVect2(const Vector2<T> &v) { DVect2 dv2(to<double>(v.x()),to<double>(v.y())); return dv2; }
269template <class T> inline constexpr FVect2 toFVect2(const Vector2<T> &v) { FVect2 fv2(to<float>(v.x()),to<float>(v.y())); return fv2; }
272template <class T> inline constexpr IVect2 toIVect2(const Vector2<T> &v) { IVect2 iv2(to<int32>(v.x()),to<int32>(v.y())); return iv2; }
273template <class T> inline constexpr I64Vect2 toI64Vect2(const Vector2<T> &v) { I64Vect2 iv2(to<int64>(v.x()),to<int64>(v.y())); return iv2; }
276template <class T> inline constexpr UVect2 toUVect2(const Vector2<T> &v) { UVect2 uv2(to<uint32>(v.x()),to<uint32>(v.y())); return uv2; }
277template <class T> inline constexpr U64Vect2 toU64Vect2(const Vector2<T> &v) { U64Vect2 uv2(to<uint64>(v.x()),to<uint64>(v.y())); return uv2; }
278
281template <class T> inline constexpr DVect3 toDVect3(const Vector3<T> &v) {
282 DVect3 dv3(to<double>(v.x()),to<double>(v.y()),to<double>(v.z()));
283 return dv3;
284}
287template <class T> inline constexpr FVect3 toFVect3(const Vector3<T> &v) {
288 FVect3 fv3(to<float>(v.x()),to<float>(v.y()),to<float>(v.z()));
289 return fv3;
290}
293template <class T> inline constexpr IVect3 toIVect3(const Vector3<T> &v) {
294 IVect3 iv3(to<int32>(v.x()),to<int32>(v.y()),to<int32>(v.z()));
295 return iv3;
296}
297template <class T> inline I64Vect3 toI64Vect3(const Vector3<T> &v) {
298 I64Vect3 iv3(to<int64>(v.x()),to<int64>(v.y()),to<int64>(v.z()));
299 return iv3;
300}
303template <class T> inline constexpr U64Vect3 toU64Vect3(const Vector3<T> &v) {
304 U64Vect3 uv3(to<uint64>(v.x()),to<uint64>(v.y()),to<uint64>(v.z()));
305 return uv3;
306}
307
308
310template <class T> inline constexpr const Vector2<T> &toVect2(const Vector2<T> &v) { return v; }
311template <class T> inline constexpr Vector2<T> toVect2(const Vector3<T> &v) { return Vector2<T>(v.x(),v.y()); }
312template <class T> inline constexpr Vector3<T> toVect3(const Vector2<T> &v,const T &t=0) { return Vector3<T>(v.x(),v.y(),t); }
313template <class T> inline constexpr const Vector3<T> &toVect3(const Vector3<T> &v) { return v; }
314
315
318template <class T> inline constexpr Vector2<T> vmax(const Vector2<T> &v1,const Vector2<T> &v2) {
319 Vector2<T> out(std::max<T>(v1.x(),v2.x()),std::max<T>(v1.y(),v2.y()));
320 return out;
321}
324template <class T> inline constexpr Vector2<T> vmin(const Vector2<T> &v1,const Vector2<T> &v2) {
325 Vector2<T> out(std::min<T>(v1.x(),v2.x()),std::min<T>(v1.y(),v2.y()));
326 return out;
327}
328
331template <class T> inline constexpr Vector2<T> vsign(const Vector2<T> &v1,const Vector2<T> &v2) {
332 Vector2<T> out(v2.x() < 0 ? -qAbs(v1.x()) : qAbs(v1.x()) ,v2.y() < 0 ? -qAbs(v1.y()) : qAbs(v1.y()));
333 return out;
334}
335
338template <class T> inline constexpr Vector3<T> vmax(const Vector3<T> &v1,const Vector3<T> &v2) {
339 Vector3<T> out(std::max<T>(v1.x(),v2.x()),std::max<T>(v1.y(),v2.y()),std::max<T>(v1.z(),v2.z()));
340 return out;
341}
344template <class T> inline constexpr Vector3<T> vmin(const Vector3<T> &v1,const Vector3<T> &v2) {
345 Vector3<T> out(std::min<T>(v1.x(),v2.x()),std::min<T>(v1.y(),v2.y()),std::min<T>(v1.z(),v2.z()));
346 return out;
347}
348
351template <class T> inline constexpr Vector3<T> vsign(const Vector3<T> &v1,const Vector3<T> &v2) {
352 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()));
353 return out;
354}
355
358template <class T> inline Vector2<T> vceil(const Vector2<T> &v) {
359 Vector2<T> out(ceil(v.x()),ceil(v.y()));
360 return out;
361}
362
365template <class T> inline Vector3<T> vceil(const Vector3<T> &v) {
366 Vector3<T> out(ceil(v.x()),ceil(v.y()),ceil(v.z()));
367 return out;
368}
369
372template <class T> inline Vector2<T> vfloor(const Vector2<T> &v) {
373 Vector2<T> out(floor(v.x()),floor(v.y()));
374 return out;
375}
376
379template <class T> inline Vector3<T> vfloor(const Vector3<T> &v) {
380 Vector3<T> out(floor(v.x()),floor(v.y()),floor(v.z()));
381 return out;
382}
383
386template <class T> IVect2 vround(const Vector2<T> &v) {
387 IVect2 out(qRound(v.x()),qRound(v.y()));
388 return out;
389}
390
393template <class T> IVect3 constexpr vround(const Vector3<T> &v) {
394 IVect3 out(qRound(v.x()),qRound(v.y()),qRound(v.z()));
395 return out;
396}
397template <class T> I64Vect3 constexpr v64round(const Vector3<T> &v) {
398 I64Vect3 out(std::llround(v.x()),std::llround(v.y()),std::llround(v.z()));
399 return out;
400}
401
402namespace std {
404 template <class T> constexpr Vector2<T> max(const Vector2<T> &v1,const Vector2<T> &v2) { return vmax(v1,v2); }
406 template <class T> constexpr Vector2<T> min(const Vector2<T> &v1,const Vector2<T> &v2) { return vmin(v1,v2); }
408 template <class T> constexpr Vector3<T> max(const Vector3<T> &v1,const Vector3<T> &v2) { return vmax(v1,v2); }
410 template <class T> constexpr Vector3<T> min(const Vector3<T> &v1,const Vector3<T> &v2) { return vmin(v1,v2); }
411
413 template <class T> struct tuple_size<Vector2<T>> : std::integral_constant<std::size_t, 2> {};
414 template <class T> struct tuple_size<Vector3<T>> : std::integral_constant<std::size_t, 3> {};
415 template <std::size_t N,class T> struct tuple_element<N, Vector2<T>> { using type = T; };
416 template <std::size_t N,class T> struct tuple_element<N, Vector3<T>> { using type = T; };
417}
418
419namespace base {
420 // ts (tostring) support for vectors
421 // fs (fromstring) support not implemented yet.
422 namespace basehelper {
423 template <typename T>
424 string tsVect(const Vector2<T> &t, int width, char notation, int precision, char fill) {
425 if (abs(width) > 5) width = width < 0 ? -abs(abs(width) - 3) : abs(abs(width) - 3); // Subtraci (,)
426 else width = 0;
427 int w2 = std::abs(width)/2;
428 int w1 = std::abs(width) - w2;
429 if (width<0) { w1 *= -1; w2 *= -1; }
430 return "("+ts(t.x(), w1, notation, precision, fill)+","
431 +ts(t.y(), w2, notation, precision, fill)+")";
432 }
433 template <typename T>
434 string tsVect(const Vector3<T> &t, int width, char notation, int precision, char fill) {
435 if (abs(width) > 7) width = width < 0 ? -abs(abs(width) - 4) : abs(abs(width) - 4);// Subtraci (,,)
436 else width = 0;
437 int w3 = std::abs(width)/3;
438 int w2 = (std::abs(width)-w3)/2;
439 int w1 = std::abs(width) - w2 - w3;
440 if (width<0) { w1 *= -1; w2 *= -1; w3 *= -1; }
441 return "("+ts(t.x(), w1, notation, precision, fill)+","
442 +ts(t.y(), w2, notation, precision, fill)+","
443 +ts(t.z(), w3, notation, precision, fill)+")";
444 }
445 }
446
447 template <>
448 inline string ts(const DVect2 &t, int width, char notation, int precision, char fill) {
449 return basehelper::tsVect(t, width, notation, precision, fill);
450 }
451
452 template <>
453 inline string ts(const DVect3 &t, int width, char notation, int precision, char fill) {
454 return basehelper::tsVect(t, width, notation, precision, fill);
455 }
456
457 template <>
458 inline string ts(const IVect2 &t, int width, char notation, int precision, char fill) {
459 return basehelper::tsVect(t, width, notation, precision, fill);
460 }
461
462 template <>
463 inline string ts(const IVect3 &t, int width, char notation, int precision, char fill) {
464 return basehelper::tsVect(t, width, notation, precision, fill);
465 }
466
467 template <>
468 inline string ts(const I64Vect2 &t, int width, char notation, int precision, char fill) {
469 return basehelper::tsVect(t, width, notation, precision, fill);
470 }
471
472 template <>
473 inline string ts(const I64Vect3 &t, int width, char notation, int precision, char fill) {
474 return basehelper::tsVect(t, width, notation, precision, fill);
475 }
476}
477
478// This allows you to send Vector2<T> to a std::format
479// Each component is formatted as using std::format double format specification.
480// This means if W is the given width the actual width will be 2*W+3
481template <class T>
482struct std::formatter<Vector2<T>> : public std::formatter<double> {
483 template <typename ParseContext>
484 constexpr auto parse(ParseContext &ctx) { return std::formatter<double>::parse(ctx); }
485
486 // I'm sure there is a better way to do this
487 template <typename FormatContext>
488 constexpr auto format(Vector2<T> const &val, FormatContext &ctx) const {
489 format_to(ctx.out(),"(");
490 std::formatter<double>::format(val.x(),ctx);
491 format_to(ctx.out(),",");
492 std::formatter<double>::format(val.y(),ctx);
493 return format_to(ctx.out(),")");
494 }
495};
496
497// This allows you to send Vector3<T> to a std::format
498// Each component is formatted as using std::format double format specification.
499// This means if W is the given width the actual width will be 3*W+4
500template <class T>
501struct std::formatter<Vector3<T>> : public std::formatter<double> {
502 template <typename ParseContext>
503 constexpr auto parse(ParseContext &ctx) { return std::formatter<double>::parse(ctx); }
504
505 // I'm sure there is a better way to do this
506 template <typename FormatContext>
507 constexpr auto format(Vector3<T> const &val, FormatContext &ctx) const {
508 format_to(ctx.out(),"(");
509 std::formatter<double>::format(val.x(),ctx);
510 format_to(ctx.out(),",");
511 std::formatter<double>::format(val.y(),ctx);
512 format_to(ctx.out(),",");
513 std::formatter<double>::format(val.z(),ctx);
514 return format_to(ctx.out(),")");
515 }
516};
517
518template <typename T>
519constexpr T safeMag2(const Vector2<T> &v) {
520 return safeSquare(v.x()) + safeSquare(v.y());
521}
522
523template <typename T>
524constexpr T safeMag(const Vector2<T> &v) {
525 return to<T>(std::sqrt(to<double>(safeMag2(v))));
526}
527
529template <typename T>
530constexpr const Vector2<T> safeDiv(const Vector2<T> &a,const Vector2<T> &b,const Vector2<T> &safe=Vector2<T>(0)) {
531 Vector2<T> ret(::safeDiv(a.x(),b.x(),safe.x()),::safeDiv(a.y(),b.y(),safe.y()));
532 return ret;
533}
534
535template <typename T>
536constexpr const Vector2<T> safeDiv(const Vector2<T> &a,const T &v,const Vector2<T> &safe=Vector2<T>(0)) {
537 Vector2<T> ret(::safeDiv(a.x(),v,safe.x()),::safeDiv(a.y(),v,safe.y()));
538 return ret;
539}
540
541template <typename T>
542constexpr Vector2<T> safeUnit(const Vector2<T> &v) {
543 T m = safeMag(v);
544 return safeDiv(v,m);
545}
546
547template <typename T>
548constexpr bool isFinite(const Vector2<T> &a) {
549 return std::isfinite(a.x()) and std::isfinite(a.y());
550}
551
552template <typename T>
553constexpr T safeMag2(const Vector3<T> &v) {
554 return safeSquare(v.x()) + safeSquare(v.y()) + safeSquare(v.z());
555}
556
557template <typename T>
558constexpr T safeMag(const Vector3<T> &v) {
559 return to<T>(std::sqrt(to<double>(safeMag2(v))));
560}
561
563template <typename T>
564constexpr const Vector3<T> safeDiv(const Vector3<T> &a,const Vector3<T> &b,const Vector3<T> &safe=Vector3<T>(0)) {
565 Vector3<T> ret(::safeDiv(a.x(),b.x(),safe.x()),::safeDiv(a.y(),b.y(),safe.y()),::safeDiv(a.z(),b.z(),safe.z()));
566 return ret;
567}
568
569template <typename T>
570constexpr const Vector3<T> safeDiv(const Vector3<T> &a,const T &v,const Vector3<T> &safe=Vector3<T>(0)) {
571 Vector3<T> ret(::safeDiv(a.x(),v,safe.x()),::safeDiv(a.y(),v,safe.y()),::safeDiv(a.z(),v,safe.z()));
572 return ret;
573}
574
575template <typename T>
576constexpr Vector3<T> safeUnit(const Vector3<T> &v) {
577 T m = safeMag(v);
578 return safeDiv(v,m);
579}
580
581template <typename T>
582constexpr bool isFinite(const Vector3<T> &a) {
583 return std::isfinite(a.x()) and std::isfinite(a.y()) and std::isfinite(a.z());
584}
585
587// EoF
588
includes std::string and additional functions not included in the standard.
2D vector utility class.
Definition vect.h:32
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:125
constexpr T minComp() const
Returns the minimum component.
Definition vect.h:96
constexpr bool operator>(const Vector2< T > &v) const
Comparison operator, based on magnitude.
Definition vect.h:110
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:118
constexpr Vector2< T > unit() const
Unit vector - be sure vector is nonzero.
Definition vect.h:88
constexpr void fill(const T &d)
Fills all three components with value d.
Definition vect.h:92
constexpr T mag2() const
Square of the magnitude, or the dot product with itself.
Definition vect.h:78
constexpr Vector2(const Vector2< T > &v)=default
Copy constructor.
constexpr Vector2< T > expandedToInclude(const T &t) const
Returns 1D range expanded to include value t;.
Definition vect.h:139
constexpr const Vector2< T > & expandToInclude(const Vector2< T > &v)
Expands 1D range to include 1D range v.
Definition vect.h:137
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:119
constexpr const Vector2< T > & expandToInclude(const T &t)
Expands 1D range to include value t.
Definition vect.h:135
constexpr unsigned maxCompIndex() const
Returns the max component index.
Definition vect.h:98
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:114
constexpr T spread() const
Assumes vector is being used to store a 1D extent– Returns max-min. (y-x).
Definition vect.h:86
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:115
constexpr bool operator==(const Vector2< T > &v) const
"Safe" division operation - checks for zero and overflow.
Definition vect.h:104
constexpr T operator|(const Vector2< T > &v) const
Dot Product.
Definition vect.h:131
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:116
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:124
constexpr const T & x() const
X component access.
Definition vect.h:53
constexpr T volume() const
Volume of the rectangle assuming unit depth – same as area(), provided for 2D/3D compile compatibilit...
Definition vect.h:84
constexpr Vector2< T > expandedToInclude(const Vector2< T > &v) const
Returns 1D range expanded to include 1D range v.
Definition vect.h:141
constexpr T mag() const
The magnitude.
Definition vect.h:80
constexpr const T & dof(unsigned u) const
Access to degree of freedom u (0-1).
Definition vect.h:58
T & rdof(unsigned u)
Reference accesss to degree-of-freedom u (0-1).
Definition vect.h:65
constexpr T maxComp() const
Returns the maximum component.
Definition vect.h:94
constexpr bool operator<(const Vector2< T > &v) const
Comparison operator, based on magnitude.
Definition vect.h:108
constexpr bool contains(const T &t) const
True if value t falls inside this 1D range (inclusive).
Definition vect.h:143
constexpr T fsum() const
Manhattan norm.
Definition vect.h:74
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:117
constexpr Vector2< T > abs() const
Returns vector of absolute values of components.
Definition vect.h:90
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:128
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:126
static Vector2< T > nothing()
Creates an "empty" vector, useful when Vector2 is used as a 1D extent.
Definition vect.h:49
constexpr bool operator!=(const Vector2< T > &v) const
Comparison operator - no tolerance is used.
Definition vect.h:106
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:127
constexpr T area() const
Size of rectangle represented by x*y - can be negative.
Definition vect.h:82
constexpr unsigned minCompIndex() const
Returns the min component index.
Definition vect.h:100
T & rx()
Reference access to x-component.
Definition vect.h:60
constexpr T sum() const
Sum of components.
Definition vect.h:76
constexpr Vector2(const T &t)
Explicit contructor, each component is given value t.
Definition vect.h:46
constexpr const T & y() const
Y component access.
Definition vect.h:55
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:123
constexpr Vector2(const T &x, const T &y)
Explicit constructor, from two components.
Definition vect.h:44
T & ry()
Reference access to y-component.
Definition vect.h:62
3D vector utility class.
Definition vect.h:150
constexpr bool operator<(const Vector3< T > &v) const
Comparison operator, compare each DOF in order.
Definition vect.h:218
constexpr const T & y() const
The y-component of the vector.
Definition vect.h:169
T & rx()
Reference access to the x-component of the vector.
Definition vect.h:175
constexpr T maxComp() const
Returns the maximum component.
Definition vect.h:205
constexpr Vector3< T > operator&(const Vector3< T > &v) const
Cross Product.
Definition vect.h:240
constexpr T operator|(const Vector3< T > &v) const
Dot Product.
Definition vect.h:245
T & ry()
Reference access to the y-component of the vector.
Definition vect.h:177
constexpr const Vector3< T > & operator-=(const Vector3< T > &v)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:225
constexpr const Vector3< T > & operator+=(const Vector3< T > &v)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:224
constexpr unsigned minCompIndex() const
Returns the min component index.
Definition vect.h:211
constexpr Vector3< T > operator+(const Vector3< T > &v) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:233
constexpr bool operator>(const Vector3< T > &v) const
Comparison operator, compare each DOF in order.
Definition vect.h:220
constexpr const Vector3< T > & operator*=(const Vector3< T > &v)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:226
constexpr T fsum() const
Manhattan norm.
Definition vect.h:189
constexpr Vector3< T > operator/(const T &t) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:238
constexpr T minComp() const
Returns the minimum component.
Definition vect.h:207
constexpr const Vector3< T > & operator/=(const Vector3< T > &v)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:228
constexpr const Vector3< T > & operator/=(const T &t)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:229
constexpr Vector3< T > operator*(const Vector3< T > &v) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:235
constexpr const T & x() const
The x-component of the vector.
Definition vect.h:167
constexpr T mag2() const
Square of the magnitude, or the vector dotted with itself.
Definition vect.h:193
constexpr bool operator!=(const Vector3< T > &v) const
Comparison operator - no tolerance is used.
Definition vect.h:216
constexpr Vector3< T > operator/(const Vector3< T > &v) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:237
constexpr const Vector3< T > & operator*=(const T &t)
In-place mathematical operators – * and / are done on a component basis.
Definition vect.h:227
T & rdof(unsigned u)
Reference access to degree-of-freedom u (0-2) of the vector.
Definition vect.h:181
constexpr Vector3(const T &t)
Explicit constructor, all three components are inintialized to t.
Definition vect.h:163
constexpr bool operator==(const Vector3< T > &v) const
Comparison operator - no tolerance is used.
Definition vect.h:214
constexpr T volume() const
Volume of region represented by x*y*z - can be negative.
Definition vect.h:197
void fill(const T &d)
Fills all three components with value d.
Definition vect.h:203
Vector3< T > unit() const
Unit vector - be sure vector is nonzero.
Definition vect.h:199
constexpr unsigned maxCompIndex() const
Returns the max component index.
Definition vect.h:209
constexpr Vector3< T > abs() const
Returns vector of absolute values of components.
Definition vect.h:201
constexpr const T & z() const
The z-component of the vector.
Definition vect.h:171
constexpr Vector3< T > operator*(const T &t) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:236
constexpr T mag() const
The vector magnitude.
Definition vect.h:195
T & rz()
Reference access to the z-component of the vector.
Definition vect.h:179
constexpr Vector3(const T &x, const T &y, const T &z=0)
Explicit constructor, by the three components of the vector.
Definition vect.h:161
constexpr T sum() const
Sum of components.
Definition vect.h:191
constexpr const T & dof(unsigned u) const
The degree-of-freedom u (0-2) component.
Definition vect.h:173
constexpr Vector3< T > operator-(const Vector3< T > &v) const
Binary mathematical operators – * and / are done on a component basis.
Definition vect.h:234
debug checked shorthand for std::numeric_limits<T>::
Definition limit.h:25
Vector3< T > vceil(const Vector3< T > &v)
Definition vect.h:365
constexpr Vector3< T > toVect3(const Vector2< T > &v, const T &t=0)
Conversion between vectors of different dimension.
Definition vect.h:312
constexpr const Vector2< T > safeDiv(const Vector2< T > &a, const Vector2< T > &b, const Vector2< T > &safe=Vector2< T >(0))
"Safe" division operation - checks for zero and overflow.
Definition vect.h:530
constexpr U64Vect3 toU64Vect3(const Vector3< T > &v)
Definition vect.h:303
IVect2 vround(const Vector2< T > &v)
Definition vect.h:386
constexpr Vector2< T > vmax(const Vector2< T > &v1, const Vector2< T > &v2)
Definition vect.h:318
constexpr FVect3 toFVect3(const Vector3< T > &v)
Definition vect.h:287
constexpr Vector3< T > vmax(const Vector3< T > &v1, const Vector3< T > &v2)
Definition vect.h:338
IVect3 constexpr vround(const Vector3< T > &v)
Definition vect.h:393
Vector2< T > vfloor(const Vector2< T > &v)
Definition vect.h:372
constexpr IVect2 toIVect2(const Vector2< T > &v)
Definition vect.h:272
Vector3< T > vfloor(const Vector3< T > &v)
Definition vect.h:379
constexpr const Vector2< T > & toVect2(const Vector2< T > &v)
Conversion between vectors of different dimension.
Definition vect.h:310
Vector2< T > vceil(const Vector2< T > &v)
Definition vect.h:358
constexpr Vector3< T > vmin(const Vector3< T > &v1, const Vector3< T > &v2)
Definition vect.h:344
constexpr DVect3 toDVect3(const Vector3< T > &v)
Definition vect.h:281
constexpr IVect3 toIVect3(const Vector3< T > &v)
Definition vect.h:293
constexpr UVect2 toUVect2(const Vector2< T > &v)
Definition vect.h:276
constexpr Vector3< T > vsign(const Vector3< T > &v1, const Vector3< T > &v2)
Definition vect.h:351
constexpr DVect2 toDVect2(const Vector2< T > &v)
Definition vect.h:266
constexpr Vector2< T > vsign(const Vector2< T > &v1, const Vector2< T > &v2)
Definition vect.h:331
constexpr FVect2 toFVect2(const Vector2< T > &v)
Definition vect.h:269
constexpr Vector2< T > vmin(const Vector2< T > &v1, const Vector2< T > &v2)
Definition vect.h:324
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:404
constexpr Vector2< T > min(const Vector2< T > &v1, const Vector2< T > &v2)
Template specialization for max, min.
Definition vect.h:406