14 template <
class T> T abs(
const T &t) {
18 template <
class T> T max0(
const T &t1,
const T &t2) {
22 template <
class T> T min0(
const T &t1,
const T &t2) {
34 using Base = std::array<T,2>;
36 static constexpr uint32 vdim = 2;
38 constexpr Vector2() : Base({T{},T{}}) { }
46 explicit constexpr Vector2(
const T &t) : Base({t,t}) { }
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); }
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);
70 template <>
constexpr decltype(
auto) get<2>()
const {
return dof(1); }
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()); }
86 constexpr T
spread()
const {
return (
y() -
x()); }
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()); }
143 constexpr bool contains(
const T &t)
const {
if (t<
x())
return false;
if (t>
y())
return false;
return true; }
152 using Base = std::array<T,3>;
154 static constexpr uint32 vdim = 3;
156 constexpr Vector3() : Base({T{},T{},T{}}) {}
163 explicit constexpr Vector3(
const T &t) : Base({t,t,t}) { }
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); }
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);
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()))); }
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); }
273template <
class T>
inline constexpr I64Vect2 toI64Vect2(
const Vector2<T> &v) { I64Vect2 iv2(to<int64>(v.
x()),to<int64>(v.
y()));
return iv2; }
277template <
class T>
inline constexpr U64Vect2 toU64Vect2(
const Vector2<T> &v) { U64Vect2 uv2(to<uint64>(v.
x()),to<uint64>(v.
y()));
return uv2; }
282 DVect3 dv3(to<double>(v.
x()),to<double>(v.
y()),to<double>(v.
z()));
288 FVect3 fv3(to<float>(v.
x()),to<float>(v.
y()),to<float>(v.
z()));
294 IVect3 iv3(to<int32>(v.
x()),to<int32>(v.
y()),to<int32>(v.
z()));
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()));
304 U64Vect3 uv3(to<uint64>(v.
x()),to<uint64>(v.
y()),to<uint64>(v.
z()));
319 Vector2<T> out(std::max<T>(v1.
x(),v2.
x()),std::max<T>(v1.
y(),v2.
y()));
325 Vector2<T> out(std::min<T>(v1.
x(),v2.
x()),std::min<T>(v1.
y(),v2.
y()));
332 Vector2<T> out(v2.
x() < 0 ? -qAbs(v1.
x()) : qAbs(v1.
x()) ,v2.
y() < 0 ? -qAbs(v1.
y()) : qAbs(v1.
y()));
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()));
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()));
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()));
387 IVect2 out(qRound(v.
x()),qRound(v.
y()));
394 IVect3 out(qRound(v.
x()),qRound(v.
y()),qRound(v.
z()));
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()));
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; };
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);
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)+
")";
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);
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)+
")";
448 inline string ts(
const DVect2 &t,
int width,
char notation,
int precision,
char fill) {
449 return basehelper::tsVect(t, width, notation, precision, fill);
453 inline string ts(
const DVect3 &t,
int width,
char notation,
int precision,
char fill) {
454 return basehelper::tsVect(t, width, notation, precision, fill);
458 inline string ts(
const IVect2 &t,
int width,
char notation,
int precision,
char fill) {
459 return basehelper::tsVect(t, width, notation, precision, fill);
463 inline string ts(
const IVect3 &t,
int width,
char notation,
int precision,
char fill) {
464 return basehelper::tsVect(t, width, notation, precision, fill);
468 inline string ts(
const I64Vect2 &t,
int width,
char notation,
int precision,
char fill) {
469 return basehelper::tsVect(t, width, notation, precision, fill);
473 inline string ts(
const I64Vect3 &t,
int width,
char notation,
int precision,
char fill) {
474 return basehelper::tsVect(t, width, notation, precision, fill);
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); }
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(),
")");
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); }
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(),
")");
520 return safeSquare(v.
x()) + safeSquare(v.
y());
525 return to<T>(std::sqrt(to<double>(safeMag2(v))));
549 return std::isfinite(a.
x()) and std::isfinite(a.y());
553constexpr T safeMag2(const
Vector3<T> &v) {
554 return safeSquare(v.x()) + safeSquare(v.y()) + safeSquare(v.z());
559 return to<T>(std::sqrt(to<double>(safeMag2(v))));
583 return std::isfinite(a.
x()) and std::isfinite(a.y()) and std::isfinite(a.z());
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