21template <
typename T, std::
size_t N>
class Vec :
public std::array<T, N> {
23 static_assert(
N > 0,
"Vec size must be greater than 0");
28 using Base = std::array<T, N>;
55 for (
const T &
e :
l) {
65 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
67 }(std::make_index_sequence<N>{});
74 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
76 }(std::make_index_sequence<N>{});
87 ret[index++] =
static_cast<T>(
e);
99 assert(
t.sizes() == at::IntArrayRef({N}));
100 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
102 }(std::make_index_sequence<N>{});
113 static_assert(std::is_same_v<std::remove_const_t<
decltype(T::dimensionality)>, std::size_t>);
114 static_assert(T::dimensionality ==
N);
115 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
116 return {{[&]() ->
T {
121 }(std::make_index_sequence<N>{});
133 static_assert(std::is_same_v<std::remove_const_t<
decltype(T::dimensionality)>, std::size_t>);
134 assert(
t.sizes() == at::IntArrayRef({N, T::dimensionality}));
135 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
136 return {{T::FromTensor(
t.slice(1,
indices,
indices + 1).squeeze(1))...}};
137 }(std::make_index_sequence<N>{});
144 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> at::IntArrayRef {
145 return at::IntArrayRef({(*this)[
indices]...});
146 }(std::make_index_sequence<N>{});
162 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
163 return {{(*this)[
N - 1 -
indices]...}};
164 }(std::make_index_sequence<N>{});
172 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) ->
T {
173 return ((*
this)[
indices] + ...);
174 }(std::make_index_sequence<N>{});
183 [&]<std::size_t...
indices>(std::index_sequence<
indices...>) ->
void {
187 }(std::make_index_sequence<
N - 1>{});
197 [&]<std::size_t...
indices>(std::index_sequence<
indices...>) ->
void {
201 }(std::make_index_sequence<
N - 1>{});
211 std::pair<T, T>
ret = {(*this)[0], (*this)[0]};
212 [&]<std::size_t...
indices>(std::index_sequence<
indices...>) ->
void {
217 }(std::make_index_sequence<
N - 1>{});
228 static_assert(std::is_floating_point_v<T>,
"Can only de-homogenise floating-point vectors");
229 static_assert(
N == 4,
"Can only de-homogenise 4-length vectors");
238 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<newT, N> {
240 }(std::make_index_sequence<N>{});
258 static_assert(std::is_integral_v<T>,
"Only integral types are supported for boolean 'all' operation");
259 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) ->
bool {
260 return ((*
this)[
indices] && ...);
261 }(std::make_index_sequence<N>{});
271 static_assert(std::is_integral_v<T>,
"Only integral types are supported for boolean 'any' operation");
272 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) ->
bool {
273 return ((*
this)[
indices] || ...);
274 }(std::make_index_sequence<N>{});
286 const std::function<
newT(
T)> &
f)
const {
287 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<newT, N> {
289 }(std::make_index_sequence<N>{});
299 const std::function<
newT(
const T &)> &
f)
const {
300 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<newT, N> {
302 }(std::make_index_sequence<N>{});
312 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<newT, N> {
314 }(std::make_index_sequence<N>{});
325 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<newT, N> {
327 }(std::make_index_sequence<N>{});
338 }(std::make_index_sequence<N>{});
348 }(std::make_index_sequence<N>{});
358 }(std::make_index_sequence<N>{});
368 }(std::make_index_sequence<N>{});
378 }(std::make_index_sequence<N>{});
388 }(std::make_index_sequence<N>{});
398 }(std::make_index_sequence<N>{});
408 }(std::make_index_sequence<N>{});
428 static_assert(
N > 1,
"Vec size must be greater than 1 to access element Y");
436 static_assert(
N > 1,
"Vec size must be greater than 1 to access element Y");
444 static_assert(
N > 2,
"Vec size must be greater than 2 to access element Z");
452 static_assert(
N > 2,
"Vec size must be greater than 2 to access element Z");
460 static_assert(
N > 3,
"Vec size must be greater than 3 to access element W");
468 static_assert(
N > 3,
"Vec size must be greater than 3 to access element W");
491 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
493 }(std::make_index_sequence<N>{});
502 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
504 }(std::make_index_sequence<N>{});
513 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
515 }(std::make_index_sequence<N>{});
526 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
528 }(std::make_index_sequence<N>{});
537 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
539 }(std::make_index_sequence<N>{});
548 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
550 }(std::make_index_sequence<N>{});
561 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
563 }(std::make_index_sequence<N>{});
572 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
574 }(std::make_index_sequence<N>{});
583 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
585 }(std::make_index_sequence<N>{});
596 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
598 }(std::make_index_sequence<N>{});
607 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
609 }(std::make_index_sequence<N>{});
618 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
620 }(std::make_index_sequence<N>{});
632 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
634 }(std::make_index_sequence<N>{});
643 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
645 }(std::make_index_sequence<N>{});
654 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<T, N> {
656 }(std::make_index_sequence<N>{});
668 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) ->
T {
670 }(std::make_index_sequence<N>{});
686 }(std::make_index_sequence<N2>{});
699 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
701 }(std::make_index_sequence<N>{});
710 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
712 }(std::make_index_sequence<N>{});
721 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
723 }(std::make_index_sequence<N>{});
734 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
736 }(std::make_index_sequence<N>{});
745 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
747 }(std::make_index_sequence<N>{});
756 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
758 }(std::make_index_sequence<N>{});
769 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
771 }(std::make_index_sequence<N>{});
781 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
783 }(std::make_index_sequence<N>{});
793 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
795 }(std::make_index_sequence<N>{});
806 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
808 }(std::make_index_sequence<N>{});
818 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
820 }(std::make_index_sequence<N>{});
830 return [&]<std::size_t...
indices>(std::index_sequence<
indices...>) -> std::array<bool, N> {
832 }(std::make_index_sequence<N>{});
849 }(std::make_index_sequence<Ns>{},
vecs);
864 }(std::make_index_sequence<N>{});
879 }(std::make_index_sequence<N>{});
897 return [&]<std::size_t...
cols>(std::index_sequence<
cols...>) ->
T {
899 }(std::make_index_sequence<C>{});
901 return [&]<std::size_t...
rows>(std::index_sequence<
rows...>) -> std::array<T, R> {
903 }(std::make_index_sequence<R>{});
#define __host__
Definition Global.h:17
#define __device__
Definition Global.h:22
A simple vector class derived from std::array<T, N>, providing overrides for all useful operators.
Definition Vec.h:21
__host__ __device__ constexpr Vec< newT, N > Apply(newT(*f)(const T &)) const
Map all elements with a common C-style mapping function.
Definition Vec.h:324
__host__ __device__ constexpr Vec< newT, N > StaticCast() const
Construct a Vec with the elements form this one cast to a new type.
Definition Vec.h:237
__host__ __device__ constexpr Vec< newT, N > Apply(const std::function< newT(T)> &f) const
Map all elements with a common std::function mapping function.
Definition Vec.h:285
__host__ __device__ Vec & operator*=(const Vec &other)
Element-wise multiplication by another Vec.
Definition Vec.h:375
__host__ __device__ constexpr T & Z()
Get the third element.
Definition Vec.h:451
__host__ static __device__ constexpr Vec FromIntArrayRef(const at::IntArrayRef &v)
Construct a Vec from an at::IntArrayRef (this is the type of at::Tensor::sizes())
Definition Vec.h:82
__host__ static __device__ constexpr Vec Range(const T &start=T{0}, const T &step=T{1})
Construct a Vec where the ith element is start + i * step
Definition Vec.h:73
__host__ __device__ constexpr at::IntArrayRef ToIntArrayRef() const
Convert to an at::IntArrayRef (this is the type of at::Tensor::sizes())
Definition Vec.h:143
__host__ __device__ Vec & operator*=(const scalar_t &scalar)
Element-wise multiplication by a scalar.
Definition Vec.h:385
__host__ __device__ constexpr T Max() const
Find the largest element.
Definition Vec.h:195
__host__ __device__ Vec & operator-=(const Vec &other)
Element-wise subtraction of another Vec.
Definition Vec.h:355
__host__ __device__ constexpr const T & X() const
Get a constant reference to the first element.
Definition Vec.h:417
__host__ __device__ Vec & operator+=(const Vec &other)
Element-wise addition of another Vec.
Definition Vec.h:335
__host__ __device__ constexpr const T & Z() const
Get a constant reference to the third element.
Definition Vec.h:443
static __host__ Vec FromTensor(const at::Tensor &t)
Construct a Vec from a 1D PyTorch tensor.
Definition Vec.h:98
__host__ __device__ constexpr const T & Y() const
Get a constant reference to the second element.
Definition Vec.h:427
static constexpr std::size_t dimensionality
Definition Vec.h:26
__host__ __device__ constexpr Vec< T, 3 > DeHomogenise() const
Compute the 3-vector for this homogeneous vector, = (x, y, z) / w.
Definition Vec.h:227
__host__ __device__ constexpr const T & W() const
Get a constant reference to the fourth element.
Definition Vec.h:459
__host__ __device__ constexpr T & Y()
Get the second element.
Definition Vec.h:435
__host__ __device__ constexpr T & X()
Get the first element.
Definition Vec.h:422
static __host__ constexpr Vec Identity()
Construct an identity matrix.
Definition Vec.h:112
__host__ __device__ Vec()
Default construct all elements.
Definition Vec.h:33
__host__ __device__ Vec & operator/=(const scalar_t &scalar)
Element-wise division by a scalar.
Definition Vec.h:405
__host__ __device__ constexpr Vec< newT, N > Apply(newT(*f)(T)) const
Map all elements with a common C-style mapping function.
Definition Vec.h:311
__host__ __device__ constexpr T Length() const
Compute the L^2 norm.
Definition Vec.h:247
T ElementType
Definition Vec.h:25
__host__ __device__ constexpr T Sum() const
Compute the sum of all elements.
Definition Vec.h:171
std::array< T, N > Base
Definition Vec.h:28
__host__ __device__ Vec & operator-=(const scalar_t &scalar)
Element-wise subtraction of a scalar.
Definition Vec.h:365
static __host__ Vec FromTensor2D(const at::Tensor &t)
Construct a matrix from a 2D PyTorch tensor.
Definition Vec.h:132
__host__ static __device__ constexpr Vec Full(const T &value)
Construct a Vec, copying the given value into every element.
Definition Vec.h:64
__host__ __device__ constexpr Vec Flipped() const
Construct a Vec with the same elements, but in the reverse order.
Definition Vec.h:161
__host__ __device__ constexpr bool BooleanAny() const
Compute the combination of all elements using the OR operator.
Definition Vec.h:270
__host__ __device__ Vec & operator/=(const Vec &other)
Element-wise division by another Vec.
Definition Vec.h:395
__host__ __device__ Vec & operator+=(const scalar_t &scalar)
Element-wise addition of a scalar.
Definition Vec.h:345
__host__ __device__ constexpr std::pair< T, T > MinMax() const
Find the smallest and largest elements.
Definition Vec.h:210
__host__ __device__ constexpr T Min() const
Find the smallest element.
Definition Vec.h:181
__host__ __device__ Vec(std::initializer_list< T > l)
Construct from a series of values.
Definition Vec.h:53
__host__ at::Tensor ToTensor(at::TensorOptions options={}, bool nonOwning=false) const
Convert to an at::Tensor
Definition Vec.h:152
__host__ __device__ constexpr Vec< T, 3 > XYZ() const
Construct a Vec from the first three elements.
Definition Vec.h:480
__host__ __device__ constexpr Vec< T, 2 > XY() const
Construct a Vec from the first two elements.
Definition Vec.h:475
__host__ __device__ Vec(Base array)
Construct from a std::array (this is the base class of Vec)
Definition Vec.h:42
__host__ __device__ constexpr T & W()
Get the fourth element.
Definition Vec.h:467
__host__ __device__ constexpr Vec< newT, N > Apply(const std::function< newT(const T &)> &f) const
Map all elements with a common std::function mapping function.
Definition Vec.h:298
__host__ __device__ constexpr bool BooleanAll() const
Compute the combination of all elements using the AND operator.
Definition Vec.h:257
__host__ __device__ constexpr Vec< T, N > operator-(const Vec< T, N > &lhs, const Vec< T, N > &rhs)
reg23::Vec element-wise subtraction
Definition Vec.h:524
__host__ __device__ constexpr Vec< T, N > operator/(const Vec< T, N > &lhs, const Vec< T, N > &rhs)
reg23::Vec element-wise division
Definition Vec.h:594
__host__ __device__ constexpr Vec< bool, N > operator>=(const Vec< T, N > &lhs, const Vec< T, N > &rhs)
reg23::Vec element-wise greater-than-or-equal-to
Definition Vec.h:732
__host__ __device__ constexpr Vec< T,(Ns+...)> VecCat(const Vec< T, Ns > &... vecs)
reg23::Vec concatenation of any number of vectors
Definition Vec.h:841
__host__ __device__ constexpr Vec< T, N > operator*(const Vec< T, N > &lhs, const Vec< T, N > &rhs)
reg23::Vec element-wise multiplication
Definition Vec.h:559
__host__ __device__ constexpr Vec< bool, N > operator<=(const Vec< T, N > &lhs, const Vec< T, N > &rhs)
reg23::Vec element-wise less-than-or-equal-to
Definition Vec.h:804
__host__ __device__ constexpr Vec< bool, N > operator>(const Vec< T, N > &lhs, const Vec< T, N > &rhs)
reg23::Vec element-wise greater-than
Definition Vec.h:697
__host__ __device__ constexpr Vec< bool, N > operator<(const Vec< T, N > &lhs, const Vec< T, N > &rhs)
reg23::Vec element-wise less-than
Definition Vec.h:767
__host__ __device__ constexpr Vec< T, N > operator%(const Vec< T, N > &lhs, const Vec< T, N > &rhs)
reg23::Vec element-wise modulo
Definition Vec.h:630
__host__ __device__ constexpr T VecDot(const Vec< T, N > &lhs, const Vec< T, N > &rhs)
reg23::Vec dot product
Definition Vec.h:667
__host__ __device__ constexpr Vec< Vec< T, N1 >, N2 > VecOuter(const Vec< T, N1 > &lhs, const Vec< T, N2 > &rhs)
reg23::Vec outer product; returns a column major matrix of size N1 x N2.
Definition Vec.h:682
__host__ __device__ constexpr Vec< T, R > MatMul(const Vec< Vec< T, R >, C > &lhs, const Vec< T, C > &rhs)
Matrix-vector multiplication of the Vec struct.
Definition Vec.h:894
__host__ __device__ constexpr Vec< T, N > operator+(const Vec< T, N > &lhs, const Vec< T, N > &rhs)
reg23::Vec element-wise addition
Definition Vec.h:489
Vec< TextureAddressMode, DIMENSIONALITY > StringsToAddressModes(const std::array< std::string_view, DIMENSIONALITY > &strings)
Definition Texture.h:44
Definition GridSample3DCPU.cpp:6