11#ifndef EASY3D_CORE_VEC_H
12#define EASY3D_CORE_VEC_H
29 template <
size_t N,
class T>
37 Vec() {
for (
size_t i = 0; i < N; i++) { data_[i] = T(0); } }
41 explicit Vec(
const T& s) {
for (
size_t i = 0; i < N; i++) { data_[i] = s; } }
46 for (
size_t i = 0; i < N; i++) {
55 for (
size_t i = 0; i < N; i++) {
61 template<
class T2>
explicit Vec(
const T2* rhs) {
62 for (
size_t i = 0; i < N; i++) {
72 memcpy(data_, rhs.
data(), N*
sizeof(T));
79 static size_t size() {
return (
size_t)N; }
82 T*
data() {
return data_; }
84 const T*
data()
const {
return data_; }
88 operator const T*()
const {
return data_; }
91 operator T*() {
return data_; }
108 for (
size_t i = 0; i < N; i++) {
109 result += data_[i] * data_[i];
127 for (
size_t i = 0; i < N; i++) {
128 T val = rhs.data_[i] - data_[i];
137 s = (s > std::numeric_limits<T>::min()) ? T(1.0) / s : T(0.0);
144 for (
size_t i = 0; i < N; i++) {
145 data_[i] += v.data_[i];
152 for (
size_t i = 0; i < N; i++) {
153 data_[i] -= v.data_[i];
160 for (
size_t i = 0; i < N; i++) {
161 data_[i] *= v.data_[i];
168 for (
size_t i = 0; i < N; i++) {
169 data_[i] /= v.data_[i];
176 for (
size_t i = 0; i < N; i++) {
184 for (
size_t i = 0; i < N; i++) {
193 for (
size_t i = 0; i < N; i++) {
194 result.data_[i] += v.data_[i];
202 for (
size_t i = 0; i < N; i++) {
203 result.data_[i] -= v.data_[i];
211 for (
size_t i = 0; i < N; i++) {
212 result.data_[i] *= T(s);
220 for (
size_t i = 0; i < N; i++) {
221 result.data_[i] /= T(s);
229 for (
size_t i = 0; i < N; i++) {
230 result.data_[i] = -data_[i];
244 for (
size_t i = 0; i < N; i++) {
245 result += v1[i] * v2[i];
253 for (
size_t i = 0; i < N; i++) {
262 for (
size_t i = 0; i < N; i++) {
263 result[i] = T(s)*v[i];
271 for (
size_t i = 0; i < N; i++) {
272 result[i] = v1[i] + v2[i];
280 for (
size_t i = 0; i < N; i++) {
281 result[i] = v1[i] - v2[i];
301 s = (s > std::numeric_limits<T>::min()) ? T(1.0) / s : T(0.0);
308 return (T(1) - w) * v1 + w * v2;
326 Vec() : x(0), y(0) { }
331 Vec(T x_in, T y_in) : x(x_in), y(y_in) { }
339 explicit Vec(
const T& s) : x(s), y(s) { }
350 template<
class T2>
explicit Vec(
const T2* v)
351 : x(T(v[0])), y(T(v[1])) {}
355 T
length2()
const {
return x*x + y*y; }
359 T
length()
const {
return std::sqrt(x*x + y*y); }
371 return dx*dx + dy*dy;
378 s = (s > std::numeric_limits<T>::min()) ? T(1.0) / s : T(0.0);
436 static size_t dimension() {
return (
size_t)2; }
439 static size_t size() {
return (
size_t)2; }
443 T*
data() {
return _array; }
446 const T*
data()
const {
return _array; }
451 operator const T*()
const {
return _array; }
455 operator T*() {
return _array; }
482 return v1.x*v2.y - v1.y*v2.x;
497 return Vec<2, T>(v1.x + v2.x, v1.y + v2.y);
502 return Vec<2, T>(v1.x - v2.x, v1.y - v2.y);
521 Vec() : x(0), y(0), z(0) {}
526 explicit Vec(
const Vec<2, T>& v,
const T& s = 1) : x(v.x), y(v.y), z(s) {}
530 explicit Vec(
const Vec<4, T>& v) : x(v.x), y(v.y), z(v.z) {}
536 Vec(T x_in, T y_in, T z_in) : x(x_in), y(y_in), z(z_in) {}
540 explicit Vec(
const T& s) : x(s), y(s), z(s) { }
545 template<
class T2>
explicit Vec(
const Vec<3, T2> & v) : x(v.x), y(v.y), z(v.z) {}
550 template<
class T2>
explicit Vec(
const T2* v)
551 : x(T(v[0])), y(T(v[1])), z(T(v[2])) {}
555 T
length2()
const {
return x*x + y*y + z*z; }
558 T
length()
const {
return std::sqrt(x*x + y*y + z*z); }
569 return dx*dx + dy*dy + dz*dz;
576 s = (s > std::numeric_limits<T>::min()) ? T(1.0) / s : T(0.0);
601 template <
class T2>
thisclass&
operator*=(T2 s) { x *= T(s); y *= T(s); z *= T(s);
return *
this; }
606 template <
class T2>
thisclass&
operator/=(T2 s) { x /= T(s); y /= T(s); z /= T(s);
return *
this; }
634 static size_t dimension() {
return (
size_t)3; }
637 static size_t size() {
return (
size_t)3; }
641 T*
data() {
return _array; }
644 const T*
data()
const {
return _array; }
653 operator const T*()
const {
return _array; }
657 operator T*() {
return _array; }
674 struct { T x, y, z; };
675 struct { T r, g, b; };
681 return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
687 v1.y*v2.z - v1.z*v2.y,
688 v1.z*v2.x - v1.x*v2.z,
689 v1.x*v2.y - v1.y*v2.x
698 return Vec<3, T>(T(s)*v.x, T(s)*v.y, T(s)*v.z);
703 return Vec<3, T>(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
708 return Vec<3, T>(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
713 T absx = std::fabs(v.x);
714 T absy = std::fabs(v.y);
715 T absz = std::fabs(v.z);
717 if ((absy >= absx) && (absz >= absx))
720 if ((absx >= absy) && (absz >= absy))
741 Vec() : x(0), y(0), z(0), w(0) {}
746 explicit Vec(
const Vec<3, T>& v,
const T& s = 1) : x(v.x), y(v.y), z(v.z), w(s) {}
753 Vec(T x_in, T y_in, T z_in, T w_in) : x(x_in), y(y_in), z(z_in), w(w_in) {}
757 explicit Vec(
const T& s) : x(s), y(s), z(s), w(s) { }
763 : x(v.x), y(v.y), z(v.z), w(v.w) {}
768 template<
class T2>
explicit Vec(
const T2* v)
769 : x(v[0]), y(v[1]), z(v[2]), w(v[3]) {}
773 T
length2()
const {
return x*x + y*y + z*z + w*w; }
776 T
length()
const {
return std::sqrt(x*x + y*y + z*z + w*w); }
788 return dx*dx + dy*dy + dz*dz + dw*dw;
795 s = (s > std::numeric_limits<T>::min()) ? T(1.0) / s : T(0.0);
802 static size_t dimension() {
return (
size_t)4; }
805 static size_t size() {
return (
size_t)4; }
829 x *= T(s); y *= T(s); z *= T(s); w *= T(s);
return *
this;
836 x /= T(s); y /= T(s); z /= T(s); w /= T(s);
return *
this;
865 T*
data() {
return _array; }
868 const T*
data()
const {
return _array; }
877 operator const T*()
const {
return _array; }
881 operator T*() {
return _array; }
898 struct { T x, y, z, w; };
899 struct { T r, g, b, a; };
905 return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w;
913 return Vec<4, T>(T(s)*v.x, T(s)*v.y, T(s)*v.z, T(s)*v.w);
918 return Vec<4, T>(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
923 return Vec<4, T>(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
932 for (
size_t i = 0; i < N; i++) {
940 for (
size_t i = 0; i < N; i++) {
948 return out << v.x <<
" " << v.y;
953 return in >> v.x >> v.y;
958 return out << v.x <<
" " << v.y <<
" " << v.z;
963 return in >> v.x >> v.y >> v.z;
968 return out << v.x <<
" " << v.y <<
" " << v.z <<
" " << v.w;
973 return in >> v.x >> v.y >> v.z >> v.w;
979 template <
size_t N,
class T>
981 for (std::size_t i = 0; i < N; ++i) {
982 if (std::isnan(v[i]) || std::isinf(v[i]))
989 template <
size_t N,
class T>
991 for (
size_t i = 0; i < N; i++) {
992 if (a[i] != b[i])
return false;
998 template <
size_t N,
class T>
1000 for (
size_t i = 0; i < N; i++) {
1001 if (a[i] != b[i])
return true;
1007 template <
size_t N,
class T>
1009 for(
unsigned int i=0; i<N; ++i){
1010 if(a[i]<b[i])
return true;
1011 if(a[i]>b[i])
return false;
1017 template <
size_t N,
class T>
1020 for (
int i = 0; i < N; ++i)
1021 result[i] = v1[i] * v2[i];
1026 template <
size_t N,
class T>
1029 for (
int i = 0; i < N; ++i)
1030 result[i] = std::min(v1[i], v2[i]);
1035 template <
size_t N,
class T>
1038 for (
int i = 0; i < N; ++i)
1039 result[i] = std::max(v1[i], v2[i]);
1044 template<
size_t N,
class T>
1047 for (
unsigned int i = 1; i < N; ++i)
if (a[i] < result) result = a[i];
1052 template<
size_t N,
class T>
1055 for (
unsigned int i = 1; i < N; ++i)
if (a[i] > result) result = a[i];
1060 template<
size_t N,
class T>
1063 for (
unsigned int i = 0; i < N; ++i) result[i] = std::min(upper[i], std::max(a[i], lower[i]));
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
Vec< N, FT > thisclass
Definition vec.h:34
static size_t size()
Returns the dimension/size of this vector.
Definition vec.h:79
thisclass & operator*=(T2 s)
Compound vector-scalar multiplication.
Definition vec.h:175
thisclass & operator/=(T2 s)
Compound vector-scalar division.
Definition vec.h:183
static size_t dimension()
Returns the dimension/size of this vector.
Definition vec.h:77
thisclass & operator+=(const thisclass &v)
Compound addition with another vector.
Definition vec.h:143
T length() const
Returns the length of this vector.
Definition vec.h:115
thisclass operator/(T2 s) const
Vector-scalar division.
Definition vec.h:218
thisclass & operator*=(const thisclass &v)
Compound component-wise multiplication with another vector.
Definition vec.h:159
const T * data() const
Returns the constant memory address of the vector.
Definition vec.h:84
thisclass operator*(T2 s) const
Vector-scalar multiplication.
Definition vec.h:209
thisclass & operator/=(const thisclass &v)
Compound component-wise division with another vector.
Definition vec.h:167
Vec(const T &s)
Constructs a vector from a scalar number.
Definition vec.h:41
Vec()
Default constructor. All elements will be initialized to zero.
Definition vec.h:37
thisclass & operator-=(const thisclass &v)
Compound subtraction with another vector.
Definition vec.h:151
thisclass operator-() const
Negates this vector (i.e., adds a minus sign).
Definition vec.h:227
thisclass operator+(const thisclass &v) const
Addition with another vector.
Definition vec.h:191
Vec(const Vec< N, T2 > &rhs)
Constructs a vector from another vector of the same dimension/size.
Definition vec.h:45
Vec(const Vec< M, T2 > &rhs)
Constructs a vector from another vector of the same dimension/size.
Definition vec.h:53
T distance2(const thisclass &rhs) const
Returns the squared Euclidean distance to another vector.
Definition vec.h:125
thisclass & normalize()
Normalizes this vector.
Definition vec.h:135
T norm() const
Returns the norm (i.e., length/magnitude) of this vector.
Definition vec.h:120
thisclass & operator=(const thisclass &rhs)
Assignment operator. It assigns the value of this vector from another vector.
Definition vec.h:68
T * data()
Returns the memory address of the vector.
Definition vec.h:82
Vec(const T2 *rhs)
Constructs a vector from an array of values.
Definition vec.h:61
T length2() const
Returns the squared length of this vector.
Definition vec.h:106
Definition collider.cpp:182
Matrix< FT > operator+(const Matrix< FT > &, const FT &)
Definition matrix.h:1010
Matrix< FT > operator-(const Matrix< FT > &)
arithmetic operators
Definition matrix.h:995
bool operator!=(const Vec< N, T > &a, const Vec< N, T > &b)
Test if two vectors are strictly not identical.
Definition vec.h:999
T distance(const Vec< N, T > &v1, const Vec< N, T > &v2)
Computes the distance between two vectors/points.
Definition vec.h:295
Vec< N, T > comp_min(const Vec< N, T > &v1, const Vec< N, T > &v2)
Component-wise minimum vector.
Definition vec.h:1027
Vec< N, T > normalize(const Vec< N, T > &v)
Computes and returns the normalized vector (Note: the input vector is not modified).
Definition vec.h:299
Vec< N, T > comp_max(const Vec< N, T > &v1, const Vec< N, T > &v2)
Component-wise maximum vector.
Definition vec.h:1036
T length(const Vec< N, T > &v)
Computes the length/magnitude of a vector.
Definition vec.h:289
T clamp(T x, T lower, T upper)
Clamps a num to be within a given range.
Definition types.h:129
Vec< 3, T > cross(const Vec< 3, T > &v1, const Vec< 3, T > &v2)
Compute the cross product of two 3D vectors.
Definition vec.h:685
Vec< 3, T > orthogonal(const Vec< 3, T > &v)
Compute a vector that is orthogonal to the given vector.
Definition vec.h:712
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Output stream support for Graph::Vertex.
Definition graph.h:1300
bool operator<(const Vec< N, T > &a, const Vec< N, T > &b)
Lexicographic comparison of two vectors.
Definition vec.h:1008
T det(const Vec< 2, T > &v1, const Vec< 2, T > &v2)
Compute the determinant of the 2x2 matrix formed by the two 2D vectors as the two rows.
Definition vec.h:481
T length2(const Vec< N, T > &v)
Computes the squared length/magnitude of a vector.
Definition vec.h:293
bool operator==(const Vec< N, T > &a, const Vec< N, T > &b)
Test if two vectors are strictly identical.
Definition vec.h:990
T distance2(const Vec< N, T > &v1, const Vec< N, T > &v2)
Computes the squared distance between two vectors/points.
Definition vec.h:297
std::istream & operator>>(std::istream &is, GenericLine< DIM, FT > &line)
Input stream support for GenericLine.
Definition line.h:183
T max_coord(const Vec< N, T > &a)
The maximum coordinate of elements in a vector.
Definition vec.h:1053
Vec< N, T > mix(const Vec< N, T > &v1, const Vec< N, T > &v2, T w)
Definition vec.h:307
bool has_nan(const GenericBox< DIM, FT > &box)
Check if the representation of a box has NaN.
Definition box.h:373
T min_coord(const Vec< N, T > &a)
The minimum coordinate of elements in a vector.
Definition vec.h:1045
FT dot(const std::vector< FT > &, const std::vector< FT > &)
Inner product for vectors.
Definition matrix.h:1834
Vec< N, T > comp_product(const Vec< N, T > &v1, const Vec< N, T > &v2)
Component-wise product of two vectors.
Definition vec.h:1018
FT norm(const Matrix< FT > &)
utilities
Definition matrix.h:1455
Mat< N, M, T > operator*(T lhs, const Mat< N, M, T > &rhs)
Multiplies a scalar by a matrix.
Definition mat.h:1000