42 #ifndef OPENMESH_SRC_OPENMESH_CORE_GEOMETRY_VECTOR11T_HH_
43 #define OPENMESH_SRC_OPENMESH_CORE_GEOMETRY_VECTOR11T_HH_
49 #include <type_traits>
58 #include <OpenMesh/Core/System/config.h>
66 template<
typename ... Ts>
67 struct are_convertible_to;
69 template<
typename To,
typename From,
typename ... Froms>
70 struct are_convertible_to<To, From, Froms...> {
71 static constexpr
bool value = std::is_convertible<From, To>::value
72 && are_convertible_to<To, Froms...>::value;
75 template<
typename To,
typename From>
76 struct are_convertible_to<To, From> :
public std::is_convertible<From, To> {
82 template<
typename Scalar,
int DIM>
85 static_assert(DIM >= 1,
"VectorT requires positive dimensionality.");
88 using container = std::array<Scalar, DIM>;
102 static constexpr
int dim() {
107 static constexpr
size_t size() {
111 static constexpr
const size_t size_ = DIM;
117 template<
typename T,
typename ... Ts,
118 typename =
typename std::enable_if<
sizeof...(Ts)+1 == DIM>::type,
119 typename =
typename std::enable_if<
120 are_convertible_to<Scalar, T, Ts...>::value>::type>
121 constexpr
VectorT(T v, Ts... vs) : values_ { {
static_cast<Scalar
>(v),
static_cast<Scalar
>(vs)...} } {
122 static_assert(
sizeof...(Ts)+1 == DIM,
123 "Invalid number of components specified in constructor.");
124 static_assert(are_convertible_to<Scalar, T, Ts...>::value,
125 "Not all components are convertible to Scalar.");
147 template<
typename S = Scalar,
int D = DIM>
149 typename std::enable_if<D == 4,
150 VectorT<decltype(std::declval<S>()/std::declval<S>()), DIM>>::type {
151 static_assert(D == DIM,
"D and DIM need to be identical. (Never "
152 "override the default template arguments.)");
153 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
154 "to be the same type. (Never override the default template "
157 values_[0]/values_[3],
158 values_[1]/values_[3],
159 values_[2]/values_[3],
164 template<
typename Iterator,
166 *std::declval<Iterator&>(),
void(),
167 ++std::declval<Iterator&>(),
void())>
169 std::copy_n(it, DIM, values_.begin());
178 template<
typename otherScalarType,
179 typename =
typename std::enable_if<
180 std::is_convertible<otherScalarType, Scalar>::value>>
188 template<
typename OtherScalar,
189 typename =
typename std::enable_if<
190 std::is_convertible<OtherScalar, Scalar>::value>>
192 std::transform(_rhs.cbegin(), _rhs.cend(),
193 this->begin(), [](OtherScalar rhs) {
194 return static_cast<Scalar>(std::move(rhs));
200 Scalar*
data() {
return values_.data(); }
203 const Scalar*
data()
const {
return values_.data(); }
223 return std::equal(_rhs.values_.cbegin(), _rhs.values_.cend(), values_.cbegin());
228 return !std::equal(_rhs.values_.cbegin(), _rhs.values_.cend(), values_.cbegin());
234 template<
typename OtherScalar>
236 typename std::enable_if<std::is_convertible<
237 decltype(this->values_[0] * _s), Scalar>::value,
239 for (
auto& e : *
this) {
246 template<
typename OtherScalar>
248 typename std::enable_if<std::is_convertible<
249 decltype(this->values_[0] / _s), Scalar>::value,
251 for (
auto& e : *
this) {
258 template<
typename OtherScalar>
259 typename std::enable_if<std::is_convertible<
260 decltype(std::declval<Scalar>() * std::declval<OtherScalar>()),
268 template<
typename OtherScalar>
269 typename std::enable_if<std::is_convertible<
270 decltype(std::declval<Scalar>() / std::declval<OtherScalar>()),
280 template<
typename OtherScalar>
282 typename std::enable_if<
283 sizeof(decltype(this->values_[0] * *_rhs.data())) >= 0,
285 for (
int i = 0; i < DIM; ++i) {
286 data()[i] *= _rhs.data()[i];
292 template<
typename OtherScalar>
294 typename std::enable_if<
295 sizeof(decltype(this->values_[0] / *_rhs.data())) >= 0,
297 for (
int i = 0; i < DIM; ++i) {
298 data()[i] /= _rhs.data()[i];
304 template<
typename OtherScalar>
306 typename std::enable_if<
307 sizeof(decltype(this->values_[0] - *_rhs.data())) >= 0,
309 for (
int i = 0; i < DIM; ++i) {
310 data()[i] -= _rhs.data()[i];
316 template<
typename OtherScalar>
318 typename std::enable_if<
319 sizeof(decltype(this->values_[0] + *_rhs.data())) >= 0,
321 for (
int i = 0; i < DIM; ++i) {
322 data()[i] += _rhs.data()[i];
328 template<
typename OtherScalar>
330 typename std::enable_if<
331 sizeof(decltype(this->values_[0] * *_rhs.data())) >= 0,
337 template<
typename OtherScalar>
339 typename std::enable_if<
340 sizeof(decltype(this->values_[0] / *_rhs.data())) >= 0,
346 template<
typename OtherScalar>
348 typename std::enable_if<
349 sizeof(decltype(this->values_[0] + *_rhs.data())) >= 0,
355 template<
typename OtherScalar>
357 typename std::enable_if<
358 sizeof(decltype(this->values_[0] - *_rhs.data())) >= 0,
366 std::transform(values_.begin(), values_.end(), v.values_.begin(),
367 [](
const Scalar &s) { return -s; });
373 template<
typename OtherScalar>
375 typename std::enable_if<DIM == 3,
376 VectorT<decltype((*
this)[0] * _rhs[0] -
377 (*
this)[0] * _rhs[0]), DIM>>::type {
379 values_[1] * _rhs[2] - values_[2] * _rhs[1],
380 values_[2] * _rhs[0] - values_[0] * _rhs[2],
381 values_[0] * _rhs[1] - values_[1] * _rhs[0]
387 template<
typename OtherScalar>
389 decltype(*
this % _rhs)
397 template<
typename OtherScalar>
399 decltype(*this->
data() * *_rhs.data()) {
401 return std::inner_product(begin() + 1, begin() + DIM, _rhs.begin() + 1,
402 *begin() * *_rhs.begin());
407 template<
typename OtherScalar>
409 decltype(*
this | _rhs)
420 template<
typename S = Scalar>
421 decltype(std::declval<S>() * std::declval<S>())
sqrnorm()
const {
422 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
423 "to be the same type. (Never override the default template "
425 typedef decltype(values_[0] * values_[0]) RESULT;
426 return std::accumulate(values_.cbegin() + 1, values_.cend(),
427 values_[0] * values_[0],
428 [](
const RESULT &l,
const Scalar &r) { return l + r * r; });
432 template<
typename S = Scalar>
435 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
436 "to be the same type. (Never override the default template "
441 template<
typename S = Scalar>
444 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
445 "to be the same type. (Never override the default template "
452 template<
typename S = Scalar>
455 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
456 "to be the same type. (Never override the default template "
458 return *
this /=
norm();
463 template<
typename S = Scalar>
465 decltype(*this / std::declval<
VectorT<S, DIM>>().
norm()) {
466 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
467 "to be the same type. (Never override the default template "
469 return *
this /
norm();
474 template<
typename S = Scalar>
475 typename std::enable_if<
481 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
482 "to be the same type. (Never override the default template "
485 if (n !=
static_cast<decltype(
norm())
>(0)) {
500 return std::accumulate(
501 values_.cbegin() + 1, values_.cend(), values_[0]);
518 return *std::max_element(values_.cbegin(), values_.cend());
524 *std::max_element(values_.cbegin(), values_.cend(),
525 [](
const Scalar &a,
const Scalar &b) {
526 return std::abs(a) < std::abs(b);
532 return *std::min_element(values_.cbegin(), values_.cend());
538 *std::min_element(values_.cbegin(), values_.cend(),
539 [](
const Scalar &a,
const Scalar &b) {
540 return std::abs(a) < std::abs(b);
551 return std::accumulate(values_.cbegin() + 1, values_.cend(),
552 std::abs(values_[0]),
553 [](
const Scalar &l,
const Scalar &r) {
554 return l + std::abs(r);
560 std::transform(values_.cbegin(), values_.cend(),
561 _rhs.values_.cbegin(),
563 [](
const Scalar &l,
const Scalar &r) {
564 return std::min(l, r);
572 std::transform(values_.cbegin(), values_.cend(),
573 _rhs.values_.cbegin(),
575 [&result](
const Scalar &l,
const Scalar &r) {
588 std::transform(values_.cbegin(), values_.cend(),
589 _rhs.values_.cbegin(),
591 [](
const Scalar &l,
const Scalar &r) {
592 return std::max(l, r);
600 std::transform(values_.cbegin(), values_.cend(),
601 _rhs.values_.cbegin(),
603 [&result](
const Scalar &l,
const Scalar &r) {
629 template<
typename Functor>
632 std::transform(result.values_.cbegin(), result.values_.cend(),
633 result.values_.begin(), _func);
639 std::fill(values_.begin(), values_.end(), _s);
650 return std::lexicographical_compare(
651 values_.begin(), values_.end(),
652 _rhs.values_.begin(), _rhs.values_.end());
657 noexcept(noexcept(std::swap(values_, _other.values_))) {
658 std::swap(values_, _other.values_);
666 using iterator =
typename container::iterator;
667 using const_iterator =
typename container::const_iterator;
668 using reverse_iterator =
typename container::reverse_iterator;
669 using const_reverse_iterator =
typename container::const_reverse_iterator;
671 iterator begin() noexcept {
return values_.begin(); }
672 const_iterator begin() const noexcept {
return values_.cbegin(); }
673 const_iterator cbegin() const noexcept {
return values_.cbegin(); }
675 iterator end() noexcept {
return values_.end(); }
676 const_iterator end() const noexcept {
return values_.cend(); }
677 const_iterator cend() const noexcept {
return values_.cend(); }
679 reverse_iterator rbegin() noexcept {
return values_.rbegin(); }
680 const_reverse_iterator rbegin() const noexcept {
return values_.crbegin(); }
681 const_reverse_iterator crbegin() const noexcept {
return values_.crbegin(); }
683 reverse_iterator rend() noexcept {
return values_.rend(); }
684 const_reverse_iterator rend() const noexcept {
return values_.crend(); }
685 const_reverse_iterator crend() const noexcept {
return values_.crend(); }
691 template<
typename Scalar,
int DIM,
typename OtherScalar>
693 decltype(rhs.operator*(_s)) {
699 template<
typename Scalar,
int DIM>
701 typename std::enable_if<
702 sizeof(decltype(os << _vec[0])) >= 0, std::ostream&>::type {
705 for (
int i = 1; i < DIM; ++i) {
706 os <<
" " << _vec[i];
712 template<
typename Scalar,
int DIM>
714 typename std::enable_if<
715 sizeof(decltype(is >> _vec[0])) >= 0, std::istream &>::type {
716 for (
int i = 0; i < DIM; ++i)
723 template<
typename Scalar,
int DIM>
730 template<
typename LScalar,
typename RScalar,
int DIM>
733 decltype(_v1 % _v2) {
739 template<
typename Scalar,
int DIM>
741 noexcept(noexcept(_v1.swap(_v2))) {
747 template<
typename Scalar,
int DIM>
754 template<
typename Scalar,
int DIM>
760 template<
typename Scalar,
int DIM,
typename OtherScalar>
767 template<
typename Scalar,
int DIM>
774 template<
typename Scalar,
int DIM>
781 template<
typename Scalar,
int DIM>
788 template<
typename Scalar,
int DIM>
795 template<
typename Scalar,
int DIM>
917 constexpr
OpenMesh::Vec4f operator"" _htmlColor(
unsigned long long raw_color) {
919 ((raw_color >> 24) & 0xFF) / 255.0f,
920 ((raw_color >> 16) & 0xFF) / 255.0f,
921 ((raw_color >> 8) & 0xFF) / 255.0f,
922 ((raw_color >> 0) & 0xFF) / 255.0f);
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
auto operator*(const OtherScalar &_s, const VectorT< Scalar, DIM > &rhs) -> decltype(rhs.operator*(_s))
Component wise multiplication from the left.
Definition: Vector11T.hh:692
VectorT< float, 4 > Vec4f
4-float vector
Definition: Vector11T.hh:869
Definition: Vector11T.hh:83
auto norm() const -> decltype(std::sqrt(std::declval< VectorT< S, DIM >>().sqrnorm()))
compute euclidean norm
Definition: Vector11T.hh:433
VectorT< Scalar, DIM > & normalize(VectorT< Scalar, DIM > &_v)
non-member normalize
Definition: Vector11T.hh:768
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: Vector11T.hh:559
VectorT(Iterator it)
construct from a value array or any other iterator
Definition: Vector11T.hh:168
std::enable_if< std::is_convertible< decltype(std::declval< Scalar >) *std::declval< OtherScalar >)), Scalar >::value, VectorT< Scalar, DIM > >::type operator*(const OtherScalar &_s) const
component-wise multiplication with scalar
Definition: Vector11T.hh:263
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
normalize vector, return normalized vector
Definition: Vector11T.hh:453
auto cross(const VectorT< OtherScalar, DIM > &_rhs) const -> decltype(*this % _rhs)
cross product: only defined for Vec3* as specialization
Definition: Vector11T.hh:388
vector_type &::type normalize_cond()
compute squared euclidean norm
Definition: Vector11T.hh:480
Scalar dot(const VectorT< Scalar, DIM > &_v1, const VectorT< Scalar, DIM > &_v2)
symmetric version of the dot product
Definition: Vector11T.hh:724
std::enable_if< std::is_convertible< decltype(std::declval< Scalar >)/std::declval< OtherScalar >)), Scalar >::value, VectorT< Scalar, DIM > >::type operator/(const OtherScalar &_s) const
component-wise division by with scalar
Definition: Vector11T.hh:273
auto operator*(const VectorT< OtherScalar, DIM > &_rhs) const -> typename std::enable_if< sizeof(decltype(this->values_[0] **_rhs.data())) >=0
component-wise vector multiplication
VectorT< Scalar, DIM > & minimize(VectorT< Scalar, DIM > &_v1, VectorT< Scalar, DIM > &_v2)
non-member minimize
Definition: Vector11T.hh:782
auto operator+(const VectorT< OtherScalar, DIM > &_rhs) const -> typename std::enable_if< sizeof(decltype(this->values_[0]+ *_rhs.data())) >=0
component-wise vector addition
VectorT< Scalar, DIM > vector_type
type of this vector
Definition: Vector11T.hh:99
static constexpr int dim()
returns dimension of the vector (deprecated)
Definition: Vector11T.hh:102
auto homogenized() const -> typename std::enable_if< D==4, VectorT< decltype(std::declval< S >()/std::declval< S >()), DIM >>::type
Only for 4-component vectors with division operator on their Scalar: Dehomogenization.
Definition: Vector11T.hh:148
static constexpr size_t size()
returns dimension of the vector
Definition: Vector11T.hh:107
VectorT< Scalar, DIM > & vectorize(VectorT< Scalar, DIM > &_v, OtherScalar const &_val)
non-member vectorize
Definition: Vector11T.hh:761
void swap(VectorT &_other) noexcept(noexcept(std::swap(values_, _other.values_)))
swap with another vector
Definition: Vector11T.hh:656
auto operator%(const VectorT< OtherScalar, DIM > &_rhs) const -> typename std::enable_if< DIM==3, VectorT< decltype((*this)[0] *_rhs[0] -(*this)[0] *_rhs[0]), DIM >>::type
cross product: only defined for Vec3* as specialization
Definition: Vector11T.hh:374
Scalar max_abs() const
return the maximal absolute component
Definition: Vector11T.hh:522
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: Vector11T.hh:587
vector_type min(const vector_type &_rhs) const
component-wise min
Definition: Vector11T.hh:615
VectorT< Scalar, DIM > min(const VectorT< Scalar, DIM > &_v1, const VectorT< Scalar, DIM > &_v2)
non-member min
Definition: Vector11T.hh:796
Scalar sqrnorm(const VectorT< Scalar, DIM > &_v)
non-member sqrnorm
Definition: Vector11T.hh:755
auto cross(const VectorT< LScalar, DIM > &_v1, const VectorT< RScalar, DIM > &_v2) -> decltype(_v1 % _v2)
symmetric version of the cross product
Definition: Vector11T.hh:732
auto operator|(const VectorT< OtherScalar, DIM > &_rhs) const -> decltype(*this->data() **_rhs.data())
compute scalar product
Definition: Vector11T.hh:398
auto operator-(const VectorT< OtherScalar, DIM > &_rhs) const -> typename std::enable_if< sizeof(decltype(this->values_[0] - *_rhs.data())) >=0
component-wise vector difference
VectorT< Scalar, DIM > & maximize(VectorT< Scalar, DIM > &_v1, VectorT< Scalar, DIM > &_v2)
non-member maximize
Definition: Vector11T.hh:775
void swap(VectorT< Scalar, DIM > &_v1, VectorT< Scalar, DIM > &_v2) noexcept(noexcept(_v1.swap(_v2)))
non-member swap
Definition: Vector11T.hh:740
VectorT< Scalar, DIM > max(const VectorT< Scalar, DIM > &_v1, const VectorT< Scalar, DIM > &_v2)
non-member max
Definition: Vector11T.hh:789
auto operator-=(const VectorT< OtherScalar, DIM > &_rhs) -> typename std::enable_if< sizeof(decltype(this->values_[0] - *_rhs.data())) >=0
vector difference from this
auto operator*=(const VectorT< OtherScalar, DIM > &_rhs) -> typename std::enable_if< sizeof(decltype(this->values_[0] **_rhs.data())) >=0
component-wise self-multiplication
auto operator/=(const OtherScalar &_s) -> typename std::enable_if< std::is_convertible< decltype(this->values_[0]/_s), Scalar >::value, VectorT< Scalar, DIM > & >::type
component-wise self-division by scalar
Definition: Vector11T.hh:247
vector_type max(const vector_type &_rhs) const
component-wise max
Definition: Vector11T.hh:620
auto operator/=(const VectorT< OtherScalar, DIM > &_rhs) -> typename std::enable_if< sizeof(decltype(this->values_[0]/*_rhs.data())) >=0
component-wise self-division
static vector_type vectorized(const Scalar &_s)
store the same value in each component
Definition: Vector11T.hh:644
Scalar l8_norm() const
compute l8_norm
Definition: Vector11T.hh:505
Scalar min_abs() const
return the minimal absolute component
Definition: Vector11T.hh:536
VectorT(const Scalar &v)
Creates a vector with all components set to v.
Definition: Vector11T.hh:134
auto operator/(const VectorT< OtherScalar, DIM > &_rhs) const -> typename std::enable_if< sizeof(decltype(this->values_[0]/*_rhs.data())) >=0
component-wise vector division
vector_type operator-(void) const
unary minus
Definition: Vector11T.hh:364
bool operator<(const vector_type &_rhs) const
lexicographical comparison
Definition: Vector11T.hh:649
const Scalar & operator[](size_t _i) const
get i'th element read-only
Definition: Vector11T.hh:214
bool minimized(const vector_type &_rhs)
minimize values and signalize coordinate minimization
Definition: Vector11T.hh:570
Scalar mean_abs() const
return absolute arithmetic mean
Definition: Vector11T.hh:550
Scalar l1_norm() const
compute L1 (Manhattan) norm
Definition: Vector11T.hh:499
Scalar value_type
the type of the scalar used in this template
Definition: Vector11T.hh:96
auto dot(const VectorT< OtherScalar, DIM > &_rhs) const -> decltype(*this|_rhs)
compute scalar product
Definition: Vector11T.hh:408
VectorT(const VectorT< otherScalarType, DIM > &_rhs)
copy & cast constructor (explicit)
Definition: Vector11T.hh:181
Scalar mean() const
return arithmetic mean
Definition: Vector11T.hh:545
vector_type & vectorize(const Scalar &_s)
store the same value in each component (e.g. to clear all entries)
Definition: Vector11T.hh:638
auto operator*=(const OtherScalar &_s) -> typename std::enable_if< std::is_convertible< decltype(this->values_[0] *_s), Scalar >::value, VectorT< Scalar, DIM > & >::type
component-wise self-multiplication with scalar
Definition: Vector11T.hh:235
auto normalized() const -> decltype(*this/std::declval< VectorT< S, DIM >>().norm())
return normalized vector
Definition: Vector11T.hh:464
auto operator+=(const VectorT< OtherScalar, DIM > &_rhs) -> typename std::enable_if< sizeof(decltype(this->values_[0]+ *_rhs.data())) >=0
vector self-addition
constexpr VectorT()
default constructor creates uninitialized values.
Definition: Vector11T.hh:129
vector_type apply(const Functor &_func) const
component-wise apply function object with Scalar operator()(Scalar).
Definition: Vector11T.hh:630
bool operator==(const vector_type &_rhs) const
component-wise comparison
Definition: Vector11T.hh:222
Scalar * data()
access to Scalar array
Definition: Vector11T.hh:200
vector_type & operator=(const VectorT< OtherScalar, DIM > &_rhs)
cast from vector with a different scalar type
Definition: Vector11T.hh:191
bool operator!=(const vector_type &_rhs) const
component-wise comparison
Definition: Vector11T.hh:227
Scalar max() const
return the maximal component
Definition: Vector11T.hh:517
const Scalar * data() const
access to const Scalar array
Definition: Vector11T.hh:203
bool maximized(const vector_type &_rhs)
maximize values and signalize coordinate maximization
Definition: Vector11T.hh:598
Scalar & operator[](size_t _i)
get i'th element read-write
Definition: Vector11T.hh:208
auto length() const -> decltype(std::declval< VectorT< S, DIM >>().norm())
compute squared euclidean norm
Definition: Vector11T.hh:442
Scalar min() const
return the minimal component
Definition: Vector11T.hh:531
decltype(std::declval< S >() *std::declval< S >()) sqrnorm() const
compute squared euclidean norm
Definition: Vector11T.hh:421
Scalar norm(const VectorT< Scalar, DIM > &_v)
non-member norm
Definition: Vector11T.hh:748
VectorT(container &&_array)
construct from an array
Definition: Vector11T.hh:173
std::ostream & operator<<(std::ostream &_o, const Timer &_t)
Write seconds to output stream.
Definition: Timer.hh:199