11#ifndef EASY3D_CORE_VEC_H
12#define EASY3D_CORE_VEC_H
33 template <
size_t N,
class T>
40 Vec() {
for (
size_t i = 0; i < N; i++) { data_[i] = T(0); } }
43 explicit Vec(
const T& s) {
for (
size_t i = 0; i < N; i++) { data_[i] = s; } }
48 for (
size_t i = 0; i < N; i++) {
57 for (
size_t i = 0; i < N; i++) {
63 template<
class T2>
explicit Vec(
const T2* rhs) {
64 for (
size_t i = 0; i < N; i++) {
71 memcpy(data_, rhs.
data(), N*
sizeof(T));
81 T*
data() {
return data_; }
83 const T*
data()
const {
return data_; }
87 operator const T*()
const {
return data_; }
90 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;
325 Vec() : x(0), y(0) { }
326 Vec(T x_in, T y_in) : x(x_in), y(y_in) { }
329 explicit Vec(
const T& s) : x(s), y(s) { }
334 template<
class T2>
explicit Vec(
const T2* v)
335 : x(T(v[0])), y(T(v[1])) {}
338 inline T
length2()
const {
return x*x + y*y; }
339 inline T
length()
const {
return std::sqrt(x*x + y*y); }
344 return dx*dx + dy*dy;
349 s = (s > std::numeric_limits<T>::min()) ? T(1.0) / s : T(0.0);
359 template <
class T2>
inline thisclass&
operator*=(T2 s) { x *= T(s); y *= T(s);
return *
this; }
360 template <
class T2>
inline thisclass&
operator/=(T2 s) { x /= T(s); y /= T(s);
return *
this; }
368 size_t dimension()
const {
return (
size_t)2; }
371 T*
data() {
return _array; }
372 const T*
data()
const {
return _array; }
377 operator const T*()
const {
return _array; }
378 operator T*() {
return _array; }
406 return v1.x*v2.y - v1.y*v2.x;
421 return Vec<2, T>(v1.x + v2.x, v1.y + v2.y);
426 return Vec<2, T>(v1.x - v2.x, v1.y - v2.y);
444 Vec() : x(0), y(0), z(0) {}
445 explicit Vec(
const Vec<2, T>& v,
const T& s = 1) : x(v.x), y(v.y), z(s) {}
446 explicit Vec(
const Vec<4, T>& v) : x(v.x), y(v.y), z(v.z) {}
448 Vec(T x_in, T y_in, T z_in) : x(x_in), y(y_in), z(z_in) {}
450 explicit Vec(
const T& s) : x(s), y(s), z(s) { }
452 template<
class T2>
explicit Vec(
const Vec<3, T2> & v) : x(v.x), y(v.y), z(v.z) {}
453 template<
class T2>
explicit Vec(
const T2* v)
454 : x(T(v[0])), y(T(v[1])), z(T(v[2])) {}
456 inline T
length2()
const {
return x*x + y*y + z*z; }
457 inline T
length()
const {
return std::sqrt(x*x + y*y + z*z); }
463 return dx*dx + dy*dy + dz*dz;
468 s = (s > std::numeric_limits<T>::min()) ? T(1.0) / s : T(0.0);
478 template <
class T2>
inline thisclass&
operator*=(T2 s) { x *= T(s); y *= T(s); z *= T(s);
return *
this; }
479 template <
class T2>
inline thisclass&
operator/=(T2 s) { x /= T(s); y /= T(s); z /= T(s);
return *
this; }
488 size_t dimension()
const {
return (
size_t)3; }
491 T*
data() {
return _array; }
492 const T*
data()
const {
return _array; }
504 operator const T*()
const {
return _array; }
505 operator T*() {
return _array; }
523 struct { T x, y, z; };
524 struct { T r, g, b; };
530 return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
536 v1.y*v2.z - v1.z*v2.y,
537 v1.z*v2.x - v1.x*v2.z,
538 v1.x*v2.y - v1.y*v2.x
547 return Vec<3, T>(T(s)*v.x, T(s)*v.y, T(s)*v.z);
552 return Vec<3, T>(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
557 return Vec<3, T>(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
562 T absx = std::fabs(v.x);
563 T absy = std::fabs(v.y);
564 T absz = std::fabs(v.z);
566 if ((absy >= absx) && (absz >= absx))
569 if ((absx >= absy) && (absz >= absy))
589 Vec() : x(0), y(0), z(0), w(0) {}
590 explicit Vec(
const Vec<3, T>& v,
const T& s = 1) : x(v.x), y(v.y), z(v.z), w(s) {}
591 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) {}
593 explicit Vec(
const T& s) : x(s), y(s), z(s), w(s) { }
596 : x(v.x), y(v.y), z(v.z), w(v.w) {}
597 template<
class T2>
explicit Vec(
const T2* v)
598 : x(v[0]), y(v[1]), z(v[2]), w(v[3]) {}
600 inline T
length2()
const {
return x*x + y*y + z*z + w*w; }
601 inline T
length()
const {
return std::sqrt(x*x + y*y + z*z + w*w); }
608 return dx*dx + dy*dy + dz*dz + dw*dw;
613 s = (s > std::numeric_limits<T>::min()) ? T(1.0) / s : T(0.0);
618 size_t dimension()
const {
return (
size_t)4; }
627 x *= T(s); y *= T(s); z *= T(s); w *= T(s);
return *
this;
630 x /= T(s); y /= T(s); z /= T(s); w /= T(s);
return *
this;
638 T*
data() {
return _array; }
639 const T*
data()
const {
return _array; }
647 operator const T*()
const {
return _array; }
648 operator T*() {
return _array; }
666 struct { T x, y, z, w; };
667 struct { T r, g, b, a; };
673 return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w;
681 return Vec<4, T>(T(s)*v.x, T(s)*v.y, T(s)*v.z, T(s)*v.w);
686 return Vec<4, T>(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
691 return Vec<4, T>(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
700 for (
size_t i = 0; i < N; i++) {
708 for (
size_t i = 0; i < N; i++) {
716 return out << v.x <<
" " << v.y;
721 return in >> v.x >> v.y;
726 return out << v.x <<
" " << v.y <<
" " << v.z;
731 return in >> v.x >> v.y >> v.z;
736 return out << v.x <<
" " << v.y <<
" " << v.z <<
" " << v.w;
741 return in >> v.x >> v.y >> v.z >> v.w;
747 template <
size_t N,
class T>
749 for (std::size_t i = 0; i < N; ++i) {
750 if (std::isnan(v[i]) || std::isinf(v[i]))
757 template <
size_t N,
class T>
759 bool t = (a[0] == b[0]);
762 t = t && (a[i]==b[i]);
769 template <
size_t N,
class T>
771 bool t = (a[0] != b[0]);
774 t = t || (a[i]!=b[i]);
781 template <
size_t N,
class T>
783 for(
unsigned int i=0; i<N; ++i){
784 if(a[i]<b[i])
return true;
785 if(a[i]>b[i])
return false;
791 template <
size_t N,
class T>
794 for (
int i = 0; i < N; ++i)
795 result[i] = v1[i] * v2[i];
800 template <
size_t N,
class T>
803 for (
int i = 0; i < N; ++i)
804 result[i] = std::min(v1[i], v2[i]);
809 template <
size_t N,
class T>
812 for (
int i = 0; i < N; ++i)
813 result[i] = std::max(v1[i], v2[i]);
818 template<
size_t N,
class T>
821 for (
unsigned int i = 1; i < N; ++i)
if (a[i] < result) result = a[i];
826 template<
size_t N,
class T>
829 for (
unsigned int i = 1; i < N; ++i)
if (a[i] > result) result = a[i];
834 template<
size_t N,
class T>
837 for (
unsigned int i = 0; i < N; ++i) result[i] = std::min(upper[i], std::max(a[i], lower[i]));
844 template <
size_t DIM,
typename T>
struct Vec {
845 Vec() {
for (
size_t i=DIM; i--; data_[i] = T()); }
846 T& operator[](
const size_t i) { assert(i<DIM);
return data_[i]; }
847 const T& operator[](
const size_t i)
const { assert(i<DIM);
return data_[i]; }
852 typedef Vec<2, float>
vec2;
853 typedef Vec<3, float>
vec3;
854 typedef Vec<3, int > vec3i;
855 typedef Vec<4, float>
vec4;
857 template <
typename T>
struct Vec<2,T> {
858 Vec() : x(T()), y(T()) {}
859 Vec(T X, T Y) : x(X), y(Y) {}
860 template <
class U> Vec<2,T>(
const Vec<2,U> &v);
861 T& operator[](
const size_t i) { assert(i<2);
return i<=0 ? x : y; }
862 const T& operator[](
const size_t i)
const { assert(i<2);
return i<=0 ? x : y; }
866 template <
typename T>
struct Vec<3,T> {
867 Vec() : x(T()), y(T()), z(T()) {}
868 Vec(T X, T Y, T Z) : x(X), y(Y), z(Z) {}
869 T& operator[](
const size_t i) { assert(i<3);
return i<=0 ? x : (1==i ? y : z); }
870 const T& operator[](
const size_t i)
const { assert(i<3);
return i<=0 ? x : (1==i ? y : z); }
871 float norm() {
return std::sqrt(x*x+y*y+z*z); }
872 Vec<3,T> &
normalize(T l=1) { *
this = (*this)*(l/
norm());
return *
this; }
876 template <
typename T>
struct Vec<4,T> {
877 Vec() : x(T()), y(T()), z(T()), w(T()) {}
878 Vec(T X, T Y, T Z, T W) : x(X), y(Y), z(Z), w(W) {}
879 T& operator[](
const size_t i) { assert(i<4);
return i<=0 ? x : (1==i ? y : (2==i ? z : w)); }
880 const T& operator[](
const size_t i)
const { assert(i<4);
return i<=0 ? x : (1==i ? y : (2==i ? z : w)); }
884 template<
size_t DIM,
typename T> T
operator*(
const Vec<DIM,T>& lhs,
const Vec<DIM,T>& rhs) {
886 for (
size_t i=DIM; i--; ret+=lhs[i]*rhs[i]);
890 template<
size_t DIM,
typename T>Vec<DIM,T>
operator+(Vec<DIM,T> lhs,
const Vec<DIM,T>& rhs) {
891 for (
size_t i=DIM; i--; lhs[i]+=rhs[i]);
895 template<
size_t DIM,
typename T>Vec<DIM,T>
operator-(Vec<DIM,T> lhs,
const Vec<DIM,T>& rhs) {
896 for (
size_t i=DIM; i--; lhs[i]-=rhs[i]);
900 template<
size_t DIM,
typename T,
typename U> Vec<DIM,T>
operator*(
const Vec<DIM,T> &lhs,
const U& rhs) {
902 for (
size_t i=DIM; i--; ret[i]=lhs[i]*rhs);
906 template<
size_t DIM,
typename T> Vec<DIM,T>
operator-(
const Vec<DIM,T> &lhs) {
911 template<
size_t DIM,
typename T> T
dot(
const Vec<DIM,T>& lhs,
const Vec<DIM,T>& rhs) {
913 for (
size_t i=DIM; i--; ret+=lhs[i]*rhs[i]);
917 template <
typename T> Vec<3,T>
cross(Vec<3,T> v1, Vec<3,T> v2) {
918 return Vec<3,T>(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x);
921 template <
size_t DIM,
typename T> std::ostream&
operator<<(std::ostream& out,
const Vec<DIM,T>& v) {
922 for(
unsigned int i=0; i<DIM; i++) out << v[i] <<
" " ;
A 2D vector (used for representing 2D points or vectors).
Definition: vec.h:320
A 3D vector (used for representing 3D points or vectors).
Definition: vec.h:439
A 4D vector (used for representing 3D points or vectors in homogeneous coordinates).
Definition: vec.h:584
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition: vec.h:34
thisclass & operator*=(T2 s)
Compound vector-scalar multiplication.
Definition: vec.h:175
size_t size() const
Returns the dimension/size of this vector.
Definition: vec.h:78
thisclass & operator/=(T2 s)
Compound vector-scalar division.
Definition: vec.h:183
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:83
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 s. All elements will be initialized to this value.
Definition: vec.h:43
size_t dimension() const
Returns the dimension/size of this vector.
Definition: vec.h:76
Vec()
Default constructor. All elements will be initialized to zero.
Definition: vec.h:40
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:47
Vec(const Vec< M, T2 > &rhs)
Constructs a vector from another vector of the same dimension/size.
Definition: vec.h:55
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/magnitude0 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:70
T * data()
Returns the memory address of the vector.
Definition: vec.h:81
Vec(const T2 *rhs)
Constructs a vector from an array of values.
Definition: vec.h:63
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:990
Matrix< FT > operator-(const Matrix< FT > &)
arithmetic operators
Definition: matrix.h:975
bool operator<(const Vec< N, T > &a, const Vec< N, T > &b)
Lexicographic comparison of two vectors.
Definition: vec.h:782
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition: types.h:45
Vec< N, T > comp_product(const Vec< N, T > &v1, const Vec< N, T > &v2)
Component-wise product of two vectors.
Definition: vec.h:792
T distance(const Vec< N, T > &v1, const Vec< N, T > &v2)
Computes the distance between two vectors/points.
Definition: vec.h:295
bool has_nan(const GenericBox< DIM, FT > &box)
Definition: box.h:284
T max_coord(const Vec< N, T > &a)
The maximum coordinate of elements in a vector.
Definition: vec.h:827
T min_coord(const Vec< N, T > &a)
The minimum coordinate of elements in a vector.
Definition: vec.h:819
T clamp(T x, T lower, T upper)
Clamps a num to be within a given range.
Definition: types.h:130
Vec< N, T > mix(const Vec< N, T > &v1, const Vec< N, T > &v2, T w)
Definition: vec.h:307
std::istream & operator>>(std::istream &is, GenericLine< DIM, FT > &line)
Definition: line.h:133
T length(const Vec< N, T > &v)
Computes the length/magnitude of a vector.
Definition: vec.h:289
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Definition: graph.h:920
Vec< 3, T > orthogonal(const Vec< 3, T > &v)
Compute a vector that is orthogonal to the given vector.
Definition: vec.h:561
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:405
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition: types.h:47
bool operator==(const Vec< N, T > &a, const Vec< N, T > &b)
Test if two vectors are strictly identical.
Definition: vec.h:758
Vec< 2, float > vec2
A 2D point/vector of float type.
Definition: types.h:43
Vec< N, T > comp_min(const Vec< N, T > &v1, const Vec< N, T > &v2)
Component-wise minimum vector.
Definition: vec.h:801
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:534
Vec< N, T > comp_max(const Vec< N, T > &v1, const Vec< N, T > &v2)
Component-wise maximum vector.
Definition: vec.h:810
T distance2(const Vec< N, T > &v1, const Vec< N, T > &v2)
Computes the squared distance between two vectors/points.
Definition: vec.h:297
FT dot(const std::vector< FT > &, const std::vector< FT > &)
Inner product for vectors.
Definition: matrix.h:1803
bool operator!=(const Vec< N, T > &a, const Vec< N, T > &b)
Test if two vectors are strictly not identical.
Definition: vec.h:770
FT norm(const Matrix< FT > &)
utilities
Definition: matrix.h:1424
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
Mat< N, M, T > operator*(T lhs, const Mat< N, M, T > &rhs)
Component-wise scalar-matrix multiplication.
Definition: mat.h:787
T length2(const Vec< N, T > &v)
Computes the squared length/magnitude of a vector.
Definition: vec.h:293