42 #ifndef OPENMESH_SRC_OPENMESH_CORE_GEOMETRY_VECTOR11T_HH_ 43 #define OPENMESH_SRC_OPENMESH_CORE_GEOMETRY_VECTOR11T_HH_ 49 #include <type_traits> 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());
173 template<
typename otherScalarType,
174 typename =
typename std::enable_if<
175 std::is_convertible<otherScalarType, Scalar>::value>>
183 template<
typename OtherScalar,
184 typename =
typename std::enable_if<
185 std::is_convertible<OtherScalar, Scalar>::value>>
187 std::transform(_rhs.cbegin(), _rhs.cend(),
188 this->begin(), [](OtherScalar rhs) {
189 return static_cast<Scalar
>(std::move(rhs));
195 Scalar*
data() {
return values_.data(); }
198 const Scalar*
data()
const {
return values_.data(); }
218 return std::equal(_rhs.values_.cbegin(), _rhs.values_.cend(), values_.cbegin());
223 return !std::equal(_rhs.values_.cbegin(), _rhs.values_.cend(), values_.cbegin());
229 template<
typename OtherScalar>
231 typename std::enable_if<std::is_convertible<
232 decltype(this->values_[0] * _s), Scalar>::value,
234 for (
auto& e : *
this) {
241 template<
typename OtherScalar>
243 typename std::enable_if<std::is_convertible<
244 decltype(this->values_[0] / _s), Scalar>::value,
246 for (
auto& e : *
this) {
253 template<
typename OtherScalar>
254 typename std::enable_if<std::is_convertible<
255 decltype(std::declval<Scalar>() * std::declval<OtherScalar>()),
259 return vector_type(*
this) *= _s;
263 template<
typename OtherScalar>
264 typename std::enable_if<std::is_convertible<
265 decltype(std::declval<Scalar>() / std::declval<OtherScalar>()),
269 return vector_type(*
this) /= _s;
275 template<
typename OtherScalar>
277 typename std::enable_if<
278 sizeof(decltype(this->values_[0] * *_rhs.
data())) >= 0,
279 vector_type&>::type {
280 for (
int i = 0; i < DIM; ++i) {
281 data()[i] *= _rhs.
data()[i];
287 template<
typename OtherScalar>
289 typename std::enable_if<
290 sizeof(decltype(this->values_[0] / *_rhs.
data())) >= 0,
291 vector_type&>::type {
292 for (
int i = 0; i < DIM; ++i) {
293 data()[i] /= _rhs.
data()[i];
299 template<
typename OtherScalar>
301 typename std::enable_if<
302 sizeof(decltype(this->values_[0] - *_rhs.
data())) >= 0,
303 vector_type&>::type {
304 for (
int i = 0; i < DIM; ++i) {
305 data()[i] -= _rhs.
data()[i];
311 template<
typename OtherScalar>
313 typename std::enable_if<
314 sizeof(decltype(this->values_[0] + *_rhs.
data())) >= 0,
315 vector_type&>::type {
316 for (
int i = 0; i < DIM; ++i) {
317 data()[i] += _rhs.
data()[i];
323 template<
typename OtherScalar>
325 typename std::enable_if<
326 sizeof(decltype(this->values_[0] * *_rhs.
data())) >= 0,
328 return vector_type(*
this) *= _rhs;
332 template<
typename OtherScalar>
334 typename std::enable_if<
335 sizeof(decltype(this->values_[0] / *_rhs.
data())) >= 0,
337 return vector_type(*
this) /= _rhs;
341 template<
typename OtherScalar>
343 typename std::enable_if<
344 sizeof(decltype(this->values_[0] + *_rhs.
data())) >= 0,
346 return vector_type(*
this) += _rhs;
350 template<
typename OtherScalar>
352 typename std::enable_if<
353 sizeof(decltype(this->values_[0] - *_rhs.
data())) >= 0,
355 return vector_type(*
this) -= _rhs;
361 std::transform(values_.begin(), values_.end(), v.values_.begin(),
362 [](
const Scalar &s) {
return -s; });
368 template<
typename OtherScalar>
370 typename std::enable_if<DIM == 3,
371 VectorT<decltype((*
this)[0] * _rhs[0] -
372 (*
this)[0] * _rhs[0]), DIM>>::type {
374 values_[1] * _rhs[2] - values_[2] * _rhs[1],
375 values_[2] * _rhs[0] - values_[0] * _rhs[2],
376 values_[0] * _rhs[1] - values_[1] * _rhs[0]
382 template<
typename OtherScalar>
384 decltype(*this->data() * *_rhs.
data()) {
386 return std::inner_product(begin() + 1, begin() + DIM, _rhs.begin() + 1,
387 *begin() * *_rhs.begin());
396 template<
typename S = Scalar>
397 decltype(std::declval<S>() * std::declval<S>()) sqrnorm()
const {
398 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 399 "to be the same type. (Never override the default template " 401 typedef decltype(values_[0] * values_[0]) RESULT;
402 return std::accumulate(values_.cbegin() + 1, values_.cend(),
403 values_[0] * values_[0],
404 [](
const RESULT &l,
const Scalar &r) {
return l + r * r; });
408 template<
typename S = Scalar>
410 decltype(
std::sqrt(
std::declval<
VectorT<S, DIM>>().sqrnorm())) {
411 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 412 "to be the same type. (Never override the default template " 414 return std::sqrt(sqrnorm());
417 template<
typename S = Scalar>
419 decltype(
std::declval<
VectorT<S, DIM>>().norm()) {
420 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 421 "to be the same type. (Never override the default template " 428 template<
typename S = Scalar>
431 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 432 "to be the same type. (Never override the default template " 434 return *
this /= norm();
439 template<
typename S = Scalar>
441 decltype(*this /
std::declval<
VectorT<S, DIM>>().norm()) {
442 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 443 "to be the same type. (Never override the default template " 445 return *
this / norm();
450 template<
typename S = Scalar>
451 typename std::enable_if<
457 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 458 "to be the same type. (Never override the default template " 461 if (n !=
static_cast<decltype(norm())
>(0)) {
476 return std::accumulate(
477 values_.cbegin() + 1, values_.cend(), values_[0]);
494 return *std::max_element(values_.cbegin(), values_.cend());
500 *std::max_element(values_.cbegin(), values_.cend(),
501 [](
const Scalar &a,
const Scalar &b) {
502 return std::abs(a) < std::abs(b);
508 return *std::min_element(values_.cbegin(), values_.cend());
514 *std::min_element(values_.cbegin(), values_.cend(),
515 [](
const Scalar &a,
const Scalar &b) {
516 return std::abs(a) < std::abs(b);
522 return l1_norm()/DIM;
527 return std::accumulate(values_.cbegin() + 1, values_.cend(),
528 std::abs(values_[0]),
529 [](
const Scalar &l,
const Scalar &r) {
530 return l + std::abs(r);
536 std::transform(values_.cbegin(), values_.cend(),
537 _rhs.values_.cbegin(),
539 [](
const Scalar &l,
const Scalar &r) {
540 return std::min(l, r);
548 std::transform(values_.cbegin(), values_.cend(),
549 _rhs.values_.cbegin(),
551 [&result](
const Scalar &l,
const Scalar &r) {
564 std::transform(values_.cbegin(), values_.cend(),
565 _rhs.values_.cbegin(),
567 [](
const Scalar &l,
const Scalar &r) {
568 return std::max(l, r);
576 std::transform(values_.cbegin(), values_.cend(),
577 _rhs.values_.cbegin(),
579 [&result](
const Scalar &l,
const Scalar &r) {
591 inline vector_type
min(
const vector_type& _rhs)
const {
592 return vector_type(*this).
minimize(_rhs);
596 inline vector_type
max(
const vector_type& _rhs)
const {
597 return vector_type(*this).
maximize(_rhs);
605 template<
typename Functor>
606 inline vector_type
apply(
const Functor& _func)
const {
608 std::transform(result.values_.cbegin(), result.values_.cend(),
609 result.values_.begin(), _func);
615 std::fill(values_.begin(), values_.end(), _s);
626 return std::lexicographical_compare(
627 values_.begin(), values_.end(),
628 _rhs.values_.begin(), _rhs.values_.end());
633 noexcept(noexcept(std::swap(values_, _other.values_))) {
634 std::swap(values_, _other.values_);
642 using iterator =
typename container::iterator;
643 using const_iterator =
typename container::const_iterator;
644 using reverse_iterator =
typename container::reverse_iterator;
645 using const_reverse_iterator =
typename container::const_reverse_iterator;
647 iterator begin() noexcept {
return values_.begin(); }
648 const_iterator begin()
const noexcept {
return values_.cbegin(); }
649 const_iterator cbegin()
const noexcept {
return values_.cbegin(); }
651 iterator end() noexcept {
return values_.end(); }
652 const_iterator end()
const noexcept {
return values_.cend(); }
653 const_iterator cend()
const noexcept {
return values_.cend(); }
655 reverse_iterator rbegin() noexcept {
return values_.rbegin(); }
656 const_reverse_iterator rbegin()
const noexcept {
return values_.crbegin(); }
657 const_reverse_iterator crbegin()
const noexcept {
return values_.crbegin(); }
659 reverse_iterator rend() noexcept {
return values_.rend(); }
660 const_reverse_iterator rend()
const noexcept {
return values_.crend(); }
661 const_reverse_iterator crend()
const noexcept {
return values_.crend(); }
667 template<
typename Scalar,
int DIM,
typename OtherScalar>
669 decltype(rhs.operator*(_s)) {
675 template<
typename Scalar,
int DIM>
676 auto operator<<(std::ostream& os, const VectorT<Scalar, DIM> &_vec) ->
677 typename std::enable_if<
678 sizeof(decltype(os << _vec[0])) >= 0, std::ostream&>::type {
681 for (
int i = 1; i < DIM; ++i) {
682 os <<
" " << _vec[i];
688 template<
typename Scalar,
int DIM>
690 typename std::enable_if<
691 sizeof(decltype(is >> _vec[0])) >= 0, std::istream &>::type {
692 for (
int i = 0; i < DIM; ++i)
699 template<
typename Scalar,
int DIM>
706 template<
typename LScalar,
typename RScalar,
int DIM>
709 decltype(_v1 % _v2) {
715 template<
typename Scalar,
int DIM>
717 noexcept(noexcept(_v1.swap(_v2))) {
723 template<
typename Scalar,
int DIM>
730 template<
typename Scalar,
int DIM>
736 template<
typename Scalar,
int DIM,
typename OtherScalar>
743 template<
typename Scalar,
int DIM>
750 template<
typename Scalar,
int DIM>
757 template<
typename Scalar,
int DIM>
878 constexpr
OpenMesh::Vec4f operator"" _htmlColor(
unsigned long long raw_color) {
880 ((raw_color >> 24) & 0xFF) / 255.0f,
881 ((raw_color >> 16) & 0xFF) / 255.0f,
882 ((raw_color >> 8) & 0xFF) / 255.0f,
883 ((raw_color >> 0) & 0xFF) / 255.0f);
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
VectorT< Scalar, DIM > vector_type
type of this vector
Scalar max_abs() const
return the maximal absolute component
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
Scalar sqrnorm(const VectorT< Scalar, DIM > &_v)
auto operator|(const VectorT< OtherScalar, DIM > &_rhs) const -> decltype(*this->data() **_rhs.data())
vector_type & vectorize(const Scalar &_s)
store the same value in each component (e.g. to clear all entries)
auto norm() const -> decltype(std::sqrt(std::declval< VectorT< S, DIM >>().sqrnorm()))
compute euclidean norm
VectorT< float, 4 > Vec4f
Scalar value_type
the type of the scalar used in this template
decltype(std::declval< S >() *std::declval< S >()) sqrnorm() const
compute squared euclidean norm
Scalar norm(const VectorT< Scalar, DIM > &_v)
VectorT< Scalar, DIM > & normalize(VectorT< Scalar, DIM > &_v)
Scalar min() const
return the minimal component
auto normalized() const -> decltype(*this/std::declval< VectorT< S, DIM >>().norm())
Scalar mean_abs() const
return absolute arithmetic mean
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
void swap(VectorT< Scalar, DIM > &_v1, VectorT< Scalar, DIM > &_v2) noexcept(noexcept(_v1.swap(_v2)))
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
VectorT(const VectorT< otherScalarType, DIM > &_rhs)
copy & cast constructor (explicit)
Scalar * data()
access to Scalar array
Scalar & operator[](size_t _i)
get i'th element read-write
Scalar mean() const
return arithmetic mean
auto homogenized() const -> typename std::enable_if< D==4, VectorT< decltype(std::declval< S >()/std::declval< S >()), DIM >>::type
auto cross(const VectorT< LScalar, DIM > &_v1, const VectorT< RScalar, DIM > &_v2) -> decltype(_v1 % _v2)
bool operator!=(const vector_type &_rhs) const
component-wise comparison
Scalar dot(const VectorT< Scalar, DIM > &_v1, const VectorT< Scalar, DIM > &_v2)
bool operator<(const vector_type &_rhs) const
lexicographical comparison
const Scalar * data() const
access to const Scalar array
vector_type operator-(void) const
unary minus
vector_type &::type normalize_cond()
compute squared euclidean norm
VectorT< Scalar, DIM > & maximize(VectorT< Scalar, DIM > &_v1, VectorT< Scalar, DIM > &_v2)
void swap(VectorT &_other) noexcept(noexcept(std::swap(values_, _other.values_)))
swap with another vector
bool operator==(const vector_type &_rhs) const
component-wise comparison
bool minimized(const vector_type &_rhs)
minimize values and signalize coordinate minimization
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
vector_type min(const vector_type &_rhs) const
component-wise min
auto length() const -> decltype(std::declval< VectorT< S, DIM >>().norm())
compute squared euclidean norm
const Scalar & operator[](size_t _i) const
get i'th element read-only
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
static vector_type vectorized(const Scalar &_s)
store the same value in each component
static constexpr int dim()
returns dimension of the vector (deprecated)
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
auto operator*(const OtherScalar &_s, const VectorT< Scalar, DIM > &rhs) -> decltype(rhs.operator*(_s))
Component wise multiplication from the left.
static constexpr size_t size()
returns dimension of the vector
vector_type max(const vector_type &_rhs) const
component-wise max
VectorT(Iterator it)
construct from a value array or any other iterator
VectorT< Scalar, DIM > & vectorize(VectorT< Scalar, DIM > &_v, OtherScalar const &_val)
Scalar l1_norm() const
compute L1 (Manhattan) norm
Scalar l8_norm() const
compute l8_norm
constexpr VectorT()
default constructor creates uninitialized values.
bool maximized(const vector_type &_rhs)
maximize values and signalize coordinate maximization
vector_type & operator=(const VectorT< OtherScalar, DIM > &_rhs)
cast from vector with a different scalar type
vector_type apply(const Functor &_func) const
component-wise apply function object with Scalar operator()(Scalar).
Scalar min_abs() const
return the minimal absolute component
Scalar max() const
return the maximal component
VectorT< Scalar, DIM > & minimize(VectorT< Scalar, DIM > &_v1, VectorT< Scalar, DIM > &_v2)