43#ifndef OPENVOLUMEMESH_SRC_OPENVOLUMEMESH_GEOMETRY_VECTOR11T_HH_
44#define OPENVOLUMEMESH_SRC_OPENVOLUMEMESH_GEOMETRY_VECTOR11T_HH_
57namespace OpenVolumeMesh {
64template<
typename ... Ts>
67template<
typename To,
typename From,
typename ... Froms>
69 static constexpr bool value = std::is_convertible<From, To>::value
73template<
typename To,
typename From>
77template<
typename Scalar,
int DIM>
80 static_assert(DIM >= 1,
"VectorT requires positive dimensionality.");
83 using container = std::array<Scalar, DIM>;
97 static constexpr int dim() {
102 static constexpr size_t size() {
106 static constexpr const size_t size_ = DIM;
120 template<
typename ... T,
121 typename =
typename std::enable_if<
sizeof...(T) == DIM>::type,
122 typename =
typename std::enable_if<
124 constexpr VectorT(T... vs) : values_ { {
static_cast<Scalar
>(vs)...} } {
125 static_assert(
sizeof...(T) == DIM,
126 "Invalid number of components specified in constructor.");
127 static_assert(are_convertible_to<Scalar, T...>::value,
128 "Not all components are convertible to Scalar.");
140 template<
typename S = Scalar,
int D = DIM>
142 typename std::enable_if<D == 4,
143 VectorT<decltype(std::declval<S>()/std::declval<S>()), DIM>>::type {
144 static_assert(D == DIM,
"D and DIM need to be identical. (Never "
145 "override the default template arguments.)");
146 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
147 "to be the same type. (Never override the default template "
150 values_[0]/values_[3],
151 values_[1]/values_[3],
152 values_[2]/values_[3],
157 template<
typename Iterator,
159 *std::declval<Iterator&>(), void(),
160 ++std::declval<Iterator&>(), void())>
162 std::copy_n(it, DIM, values_.begin());
166 template<
typename otherScalarType,
167 typename =
typename std::enable_if<
168 std::is_convertible<otherScalarType, Scalar>::value>>
176 template<
typename OtherScalar,
177 typename =
typename std::enable_if<
178 std::is_convertible<OtherScalar, Scalar>::value>>
180 std::transform(_rhs.
data(), _rhs.
data() + DIM,
181 data(), [](OtherScalar rhs) {
182 return static_cast<Scalar
>(std::move(rhs));
188 Scalar*
data() {
return values_.data(); }
191 const Scalar *
data()
const {
return values_.data(); }
211 return std::equal(_rhs.values_.cbegin(), _rhs.values_.cend(), values_.cbegin());
216 return !std::equal(_rhs.values_.cbegin(), _rhs.values_.cend(), values_.cbegin());
222 template<
typename OtherScalar>
224 typename std::enable_if<std::is_convertible<
225 decltype(this->values_[0] * _s), Scalar>::value,
227 for (
auto& e : *
this) {
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>
247 typename std::enable_if<std::is_convertible<
248 decltype(std::declval<Scalar>() * std::declval<OtherScalar>()),
251 operator*(
const OtherScalar& _s)
const {
256 template<
typename OtherScalar>
257 typename std::enable_if<std::is_convertible<
258 decltype(std::declval<Scalar>() / std::declval<OtherScalar>()),
261 operator/(
const OtherScalar& _s)
const {
268 template<
typename OtherScalar>
270 typename std::enable_if<
271 sizeof(
decltype(this->values_[0] * *_rhs.data())) >= 0,
273 for (
int i = 0; i < DIM; ++i) {
274 data()[i] *= _rhs.data()[i];
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,
325 template<
typename OtherScalar>
327 typename std::enable_if<
328 sizeof(
decltype(this->values_[0] / *_rhs.data())) >= 0,
334 template<
typename OtherScalar>
336 typename std::enable_if<
337 sizeof(
decltype(this->values_[0] + *_rhs.data())) >= 0,
343 template<
typename OtherScalar>
345 typename std::enable_if<
346 sizeof(
decltype(this->values_[0] - *_rhs.data())) >= 0,
354 std::transform(values_.begin(), values_.end(), v.values_.begin(),
355 [](
const Scalar &s) { return -s; });
361 template<
typename OtherScalar>
363 typename std::enable_if<DIM == 3,
364 VectorT<
decltype(this->values_[0] * _rhs[0] -
365 this->values_[0] * _rhs[0]),
368 values_[1] * _rhs[2] - values_[2] * _rhs[1],
369 values_[2] * _rhs[0] - values_[0] * _rhs[2],
370 values_[0] * _rhs[1] - values_[1] * _rhs[0]
376 template<
typename OtherScalar>
378 decltype(*this->
data() * *_rhs.data()) {
380 return std::inner_product(
data() + 1,
data() + DIM, _rhs.data() + 1,
381 *
data() * *_rhs.data());
390 template<
typename S = Scalar>
391 decltype(std::declval<S>() * std::declval<S>())
sqrnorm()
const {
392 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
393 "to be the same type. (Never override the default template "
395 typedef decltype(values_[0] * values_[0]) RESULT;
396 return std::accumulate(values_.cbegin() + 1, values_.cend(),
397 values_[0] * values_[0],
398 [](
const RESULT &l,
const Scalar &r) { return l + r * r; });
402 template<
typename S = Scalar>
405 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
406 "to be the same type. (Never override the default template "
411 template<
typename S = Scalar>
412 auto length() const ->
414 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
415 "to be the same type. (Never override the default template "
422 template<
typename S = Scalar>
424 decltype(*
this /= std::declval<VectorT<S, DIM>>().norm()) {
425 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
426 "to be the same type. (Never override the default template "
428 return *
this /=
norm();
433 template<
typename S = Scalar>
435 decltype(*this / std::declval<
VectorT<S, DIM>>().
norm()) {
436 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
437 "to be the same type. (Never override the default template "
439 return *
this /
norm();
444 template<
typename S = Scalar>
445 typename std::enable_if<
448 std::declval<VectorT<S, DIM>>().norm())) >= 0,
451 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need "
452 "to be the same type. (Never override the default template "
455 if (n !=
static_cast<decltype(
norm())
>(0)) {
470 return std::accumulate(
471 values_.cbegin() + 1, values_.cend(), values_[0]);
488 return *std::max_element(values_.cbegin(), values_.cend());
494 *std::max_element(values_.cbegin(), values_.cend(),
495 [](
const Scalar &a,
const Scalar &b) {
496 return std::abs(a) < std::abs(b);
502 return *std::min_element(values_.cbegin(), values_.cend());
508 *std::min_element(values_.cbegin(), values_.cend(),
509 [](
const Scalar &a,
const Scalar &b) {
510 return std::abs(a) < std::abs(b);
521 return std::accumulate(values_.cbegin() + 1, values_.cend(),
522 std::abs(values_[0]),
523 [](
const Scalar &l,
const Scalar &r) {
524 return l + std::abs(r);
530 std::transform(values_.cbegin(), values_.cend(),
531 _rhs.values_.cbegin(),
533 [](
const Scalar &l,
const Scalar &r) {
534 return std::min(l, r);
542 std::transform(values_.cbegin(), values_.cend(),
543 _rhs.values_.cbegin(),
545 [&result](
const Scalar &l,
const Scalar &r) {
558 std::transform(values_.cbegin(), values_.cend(),
559 _rhs.values_.cbegin(),
561 [](
const Scalar &l,
const Scalar &r) {
562 return std::max(l, r);
570 std::transform(values_.cbegin(), values_.cend(),
571 _rhs.values_.cbegin(),
573 [&result](
const Scalar &l,
const Scalar &r) {
599 template<
typename Functor>
602 std::transform(result.values_.begin(), result.values_.end(),
603 result.values_.begin(), _func);
609 std::fill(values_.begin(), values_.end(), _s);
620 return std::lexicographical_compare(
621 values_.begin(), values_.end(),
622 _rhs.values_.begin(), _rhs.values_.end());
627 noexcept(
noexcept(std::swap(values_, _other.values_))) {
628 std::swap(values_, _other.values_);
636 using iterator =
typename container::iterator;
638 using reverse_iterator =
typename container::reverse_iterator;
639 using const_reverse_iterator =
typename container::const_reverse_iterator;
641 iterator begin() noexcept {
return values_.begin(); }
642 const_iterator begin() const noexcept {
return values_.cbegin(); }
643 const_iterator cbegin() const noexcept {
return values_.cbegin(); }
645 iterator end() noexcept {
return values_.end(); }
649 reverse_iterator rbegin() noexcept {
return values_.rbegin(); }
650 const_reverse_iterator rbegin() const noexcept {
return values_.crbegin(); }
651 const_reverse_iterator crbegin() const noexcept {
return values_.crbegin(); }
653 reverse_iterator rend() noexcept {
return values_.rend(); }
654 const_reverse_iterator rend() const noexcept {
return values_.crend(); }
655 const_reverse_iterator crend() const noexcept {
return values_.crend(); }
661template<
typename Scalar,
int DIM,
typename OtherScalar>
662auto operator*(
const OtherScalar& _s,
const VectorT<Scalar, DIM> &rhs) ->
663 decltype(rhs.operator*(_s)) {
669template<
typename Scalar,
int DIM>
670auto operator<<(std::ostream& os,
const VectorT<Scalar, DIM> &_vec) ->
671 typename std::enable_if<
672 sizeof(
decltype(os << _vec[0])) >= 0, std::ostream&>::type {
675 for (
int i = 1; i < DIM; ++i) {
676 os <<
" " << _vec[i];
682template<
typename Scalar,
int DIM>
683auto operator>> (std::istream& is, VectorT<Scalar, DIM> &_vec) ->
684 typename std::enable_if<
685 sizeof(
decltype(is >> _vec[0])) >= 0, std::istream &>::type {
686 for (
int i = 0; i < DIM; ++i)
693template<
typename Scalar,
int DIM>
694Scalar dot(
const VectorT<Scalar, DIM>& _v1,
const VectorT<Scalar, DIM>& _v2) {
700template<
typename LScalar,
typename RScalar,
int DIM>
702cross(
const VectorT<LScalar, DIM>& _v1,
const VectorT<RScalar, DIM>& _v2) ->
703 decltype(_v1 % _v2) {
709template<
typename Scalar,
int DIM>
710void swap(VectorT<Scalar, DIM>& _v1, VectorT<Scalar, DIM>& _v2)
711noexcept(
noexcept(_v1.swap(_v2))) {
718typedef VectorT<signed char,1> Vec1c;
720typedef VectorT<unsigned char,1> Vec1uc;
722typedef VectorT<signed short int,1> Vec1s;
724typedef VectorT<unsigned short int,1> Vec1us;
726typedef VectorT<signed int,1> Vec1i;
728typedef VectorT<unsigned int,1> Vec1ui;
730typedef VectorT<float,1> Vec1f;
732typedef VectorT<double,1> Vec1d;
735typedef VectorT<signed char,2> Vec2c;
737typedef VectorT<unsigned char,2> Vec2uc;
739typedef VectorT<signed short int,2> Vec2s;
741typedef VectorT<unsigned short int,2> Vec2us;
743typedef VectorT<signed int,2> Vec2i;
745typedef VectorT<unsigned int,2> Vec2ui;
747typedef VectorT<float,2> Vec2f;
749typedef VectorT<double,2> Vec2d;
752typedef VectorT<signed char,3> Vec3c;
754typedef VectorT<unsigned char,3> Vec3uc;
756typedef VectorT<signed short int,3> Vec3s;
758typedef VectorT<unsigned short int,3> Vec3us;
760typedef VectorT<signed int,3> Vec3i;
762typedef VectorT<unsigned int,3> Vec3ui;
764typedef VectorT<float,3> Vec3f;
766typedef VectorT<double,3> Vec3d;
768typedef VectorT<bool,3> Vec3b;
771typedef VectorT<signed char,4> Vec4c;
773typedef VectorT<unsigned char,4> Vec4uc;
775typedef VectorT<signed short int,4> Vec4s;
777typedef VectorT<unsigned short int,4> Vec4us;
779typedef VectorT<signed int,4> Vec4i;
781typedef VectorT<unsigned int,4> Vec4ui;
783typedef VectorT<float,4> Vec4f;
785typedef VectorT<double,4> Vec4d;
788typedef VectorT<signed char, 5> Vec5c;
790typedef VectorT<unsigned char, 5> Vec5uc;
792typedef VectorT<signed short int, 5> Vec5s;
794typedef VectorT<unsigned short int, 5> Vec5us;
796typedef VectorT<signed int, 5> Vec5i;
798typedef VectorT<unsigned int, 5> Vec5ui;
800typedef VectorT<float, 5> Vec5f;
802typedef VectorT<double, 5> Vec5d;
805typedef VectorT<signed char,6> Vec6c;
807typedef VectorT<unsigned char,6> Vec6uc;
809typedef VectorT<signed short int,6> Vec6s;
811typedef VectorT<unsigned short int,6> Vec6us;
813typedef VectorT<signed int,6> Vec6i;
815typedef VectorT<unsigned int,6> Vec6ui;
817typedef VectorT<float,6> Vec6f;
819typedef VectorT<double,6> Vec6d;
824using namespace Geometry;
827const std::string typeName();
829template <>
const std::string typeName<Vec2f>();
830template <>
const std::string typeName<Vec2d>();
831template <>
const std::string typeName<Vec2i>();
832template <>
const std::string typeName<Vec2ui>();
834template <>
const std::string typeName<Vec3f>();
835template <>
const std::string typeName<Vec3d>();
836template <>
const std::string typeName<Vec3i>();
837template <>
const std::string typeName<Vec3ui>();
839template <>
const std::string typeName<Vec4f>();
840template <>
const std::string typeName<Vec4d>();
841template <>
const std::string typeName<Vec4i>();
842template <>
const std::string typeName<Vec4ui>();
Definition Vector11T.hh:78
static constexpr int dim()
returns dimension of the vector (deprecated)
Definition Vector11T.hh:97
auto operator|(const VectorT< OtherScalar, DIM > &_rhs) const -> decltype(*this->data() **_rhs.data())
Definition Vector11T.hh:377
Scalar * data()
access to Scalar array
Definition Vector11T.hh:188
std::enable_if< sizeof(decltype(static_cast< S >(0), std::declval< VectorT< S, DIM > >().norm()))>=0, vector_type & >::type normalize_cond()
Definition Vector11T.hh:450
auto operator+=(const VectorT< OtherScalar, DIM > &_rhs) -> typename std::enable_if< sizeof(decltype(this->values_[0]+ *_rhs.data())) >=0
vector self-addition
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:235
const Scalar & operator[](size_t _i) const
get i'th element read-only
Definition Vector11T.hh:202
VectorT(const VectorT< otherScalarType, DIM > &_rhs)
copy & cast constructor (explicit)
Definition Vector11T.hh:169
vector_type & operator=(const VectorT< OtherScalar, DIM > &_rhs)
cast from vector with a different scalar type
Definition Vector11T.hh:179
Scalar min_abs() const
return the minimal absolute component
Definition Vector11T.hh:506
Scalar mean() const
return arithmetic mean
Definition Vector11T.hh:515
bool operator==(const vector_type &_rhs) const
component-wise comparison
Definition Vector11T.hh:210
auto normalized() const -> decltype(*this/std::declval< VectorT< S, DIM > >().norm())
Definition Vector11T.hh:434
void swap(VectorT &_other) noexcept(noexcept(std::swap(values_, _other.values_)))
swap with another vector
Definition Vector11T.hh:626
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition Vector11T.hh:529
auto operator*(const VectorT< OtherScalar, DIM > &_rhs) const -> typename std::enable_if< sizeof(decltype(this->values_[0] **_rhs.data())) >=0
component-wise vector multiplication
vector_type apply(const Functor &_func) const
component-wise apply function object with Scalar operator()(Scalar).
Definition Vector11T.hh:600
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 & vectorize(const Scalar &_s)
store the same value in each component (e.g. to clear all entries)
Definition Vector11T.hh:608
bool operator<(const vector_type &_rhs) const
lexicographical comparison
Definition Vector11T.hh:619
auto operator-=(const VectorT< OtherScalar, DIM > &_rhs) -> typename std::enable_if< sizeof(decltype(this->values_[0] - *_rhs.data())) >=0
vector difference from this
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM > >().norm())
Definition Vector11T.hh:423
Scalar value_type
the type of the scalar used in this template
Definition Vector11T.hh:91
bool operator!=(const vector_type &_rhs) const
component-wise comparison
Definition Vector11T.hh:215
auto operator+(const VectorT< OtherScalar, DIM > &_rhs) const -> typename std::enable_if< sizeof(decltype(this->values_[0]+ *_rhs.data())) >=0
component-wise vector addition
Scalar mean_abs() const
return absolute arithmetic mean
Definition Vector11T.hh:520
Scalar max_abs() const
return the maximal absolute component
Definition Vector11T.hh:492
vector_type operator-(void) const
unary minus
Definition Vector11T.hh:352
static constexpr size_t size()
returns dimension of the vector
Definition Vector11T.hh:102
Scalar min() const
return the minimal component
Definition Vector11T.hh:501
auto homogenized() const -> typename std::enable_if< D==4, VectorT< decltype(std::declval< S >()/std::declval< S >()), DIM > >::type
Definition Vector11T.hh:141
auto operator/=(const VectorT< OtherScalar, DIM > &_rhs) -> typename std::enable_if< sizeof(decltype(this->values_[0]/*_rhs.data())) >=0
component-wise self-division
decltype(std::declval< S >() *std::declval< S >()) sqrnorm() const
compute squared euclidean norm
Definition Vector11T.hh:391
auto operator*=(const VectorT< OtherScalar, DIM > &_rhs) -> typename std::enable_if< sizeof(decltype(this->values_[0] **_rhs.data())) >=0
component-wise self-multiplication
VectorT(const Scalar &v)
Definition Vector11T.hh:116
static vector_type vectorized(const Scalar &_s)
store the same value in each component
Definition Vector11T.hh:614
vector_type max(const vector_type &_rhs) const
component-wise max
Definition Vector11T.hh:590
VectorT< Scalar, DIM > vector_type
type of this vector
Definition Vector11T.hh:94
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:223
vector_type min(const vector_type &_rhs) const
component-wise min
Definition Vector11T.hh:585
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition Vector11T.hh:557
auto operator-(const VectorT< OtherScalar, DIM > &_rhs) const -> typename std::enable_if< sizeof(decltype(this->values_[0] - *_rhs.data())) >=0
component-wise vector difference
Scalar l1_norm() const
compute L1 (Manhattan) norm
Definition Vector11T.hh:469
bool maximized(const vector_type &_rhs)
maximize values and signalize coordinate maximization
Definition Vector11T.hh:568
Scalar l8_norm() const
compute l8_norm
Definition Vector11T.hh:475
VectorT(Iterator it)
construct from a value array or any other iterator
Definition Vector11T.hh:161
Scalar max() const
return the maximal component
Definition Vector11T.hh:487
auto operator%(const VectorT< OtherScalar, DIM > &_rhs) const -> typename std::enable_if< DIM==3, VectorT< decltype(this->values_[0] *_rhs[0] - this->values_[0] *_rhs[0]), DIM > >::type
Definition Vector11T.hh:362
Scalar & operator[](size_t _i)
get i'th element read-write
Definition Vector11T.hh:196
auto norm() const -> decltype(std::sqrt(std::declval< VectorT< S, DIM > >().sqrnorm()))
compute euclidean norm
Definition Vector11T.hh:403
constexpr VectorT()
default constructor creates uninitialized values.
Definition Vector11T.hh:111
const Scalar * data() const
access to const Scalar array
Definition Vector11T.hh:191
bool minimized(const vector_type &_rhs)
minimize values and signalize coordinate minimization
Definition Vector11T.hh:540
Definition Vector11T.hh:65