43 #ifndef OPENVOLUMEMESH_SRC_OPENVOLUMEMESH_GEOMETRY_VECTOR11T_HH_ 44 #define OPENVOLUMEMESH_SRC_OPENVOLUMEMESH_GEOMETRY_VECTOR11T_HH_ 50 #include <type_traits> 64 template<
typename ... Ts>
67 template<
typename To,
typename From,
typename ... Froms>
69 static constexpr
bool value = std::is_convertible<From, To>::value
73 template<
typename To,
typename From>
77 template<
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<
125 constexpr
VectorT(T... vs) : values_ { {
static_cast<Scalar
>(vs)...} }
127 static_assert(
sizeof...(T) == DIM,
128 "Invalid number of components specified in constructor.");
130 "Not all components are convertible to Scalar.");
142 template<
typename S = Scalar,
int D = DIM>
144 typename
std::enable_if<D == 4,
145 VectorT<decltype(
std::declval<S>()/
std::declval<S>()), DIM>>::type {
146 static_assert(D == DIM,
"D and DIM need to be identical. (Never " 147 "override the default template arguments.)");
148 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 149 "to be the same type. (Never override the default template " 152 values_[0]/values_[3],
153 values_[1]/values_[3],
154 values_[2]/values_[3],
159 template<
typename Iterator,
161 *std::declval<Iterator&>(),
void(),
162 ++std::declval<Iterator&>(),
void())>
164 std::copy_n(it, DIM, values_.begin());
168 template<
typename otherScalarType,
169 typename =
typename std::enable_if<
170 std::is_convertible<otherScalarType, Scalar>::value>>
178 template<
typename OtherScalar,
179 typename =
typename std::enable_if<
180 std::is_convertible<OtherScalar, Scalar>::value>>
184 std::transform(_rhs.
data(), _rhs.
data() + DIM,
185 data(), [](OtherScalar rhs) {
186 return static_cast<Scalar
>(std::move(rhs));
192 Scalar*
data() {
return values_.data(); }
195 const Scalar *
data()
const {
return values_.data(); }
215 return std::equal(_rhs.values_.cbegin(), _rhs.values_.cend(), values_.cbegin());
220 return !std::equal(_rhs.values_.cbegin(), _rhs.values_.cend(), values_.cbegin());
226 template<
typename OtherScalar>
228 typename std::enable_if<std::is_convertible<
229 decltype(this->values_[0] * _s), Scalar>::value,
231 for (
auto& e : *
this) {
238 template<
typename OtherScalar>
240 typename std::enable_if<std::is_convertible<
241 decltype(this->values_[0] / _s), Scalar>::value,
243 for (
auto& e : *
this) {
250 template<
typename OtherScalar>
251 typename std::enable_if<std::is_convertible<
252 decltype(std::declval<Scalar>() * std::declval<OtherScalar>()),
256 return vector_type(*
this) *= _s;
260 template<
typename OtherScalar>
261 typename std::enable_if<std::is_convertible<
262 decltype(std::declval<Scalar>() / std::declval<OtherScalar>()),
266 return vector_type(*
this) /= _s;
272 template<
typename OtherScalar>
274 typename std::enable_if<
275 sizeof(decltype(this->values_[0] * *_rhs.
data())) >= 0,
276 vector_type&>::type {
277 for (
int i = 0; i < DIM; ++i) {
278 data()[i] *= _rhs.
data()[i];
284 template<
typename OtherScalar>
286 typename std::enable_if<
287 sizeof(decltype(this->values_[0] / *_rhs.
data())) >= 0,
288 vector_type&>::type {
289 for (
int i = 0; i < DIM; ++i) {
290 data()[i] /= _rhs.
data()[i];
296 template<
typename OtherScalar>
298 typename std::enable_if<
299 sizeof(decltype(this->values_[0] - *_rhs.
data())) >= 0,
300 vector_type&>::type {
301 for (
int i = 0; i < DIM; ++i) {
302 data()[i] -= _rhs.
data()[i];
308 template<
typename OtherScalar>
310 typename std::enable_if<
311 sizeof(decltype(this->values_[0] + *_rhs.
data())) >= 0,
312 vector_type&>::type {
313 for (
int i = 0; i < DIM; ++i) {
314 data()[i] += _rhs.
data()[i];
320 template<
typename OtherScalar>
322 typename std::enable_if<
323 sizeof(decltype(this->values_[0] * *_rhs.
data())) >= 0,
325 return vector_type(*
this) *= _rhs;
329 template<
typename OtherScalar>
331 typename std::enable_if<
332 sizeof(decltype(this->values_[0] / *_rhs.
data())) >= 0,
334 return vector_type(*
this) /= _rhs;
338 template<
typename OtherScalar>
340 typename std::enable_if<
341 sizeof(decltype(this->values_[0] + *_rhs.
data())) >= 0,
343 return vector_type(*
this) += _rhs;
347 template<
typename OtherScalar>
349 typename std::enable_if<
350 sizeof(decltype(this->values_[0] - *_rhs.
data())) >= 0,
352 return vector_type(*
this) -= _rhs;
358 std::transform(values_.begin(), values_.end(), v.values_.begin(),
359 [](
const Scalar &s) {
return -s; });
365 template<
typename OtherScalar>
367 typename std::enable_if<DIM == 3,
368 VectorT<decltype(this->values_[0] * _rhs[0] -
369 this->values_[0] * _rhs[0]),
372 values_[1] * _rhs[2] - values_[2] * _rhs[1],
373 values_[2] * _rhs[0] - values_[0] * _rhs[2],
374 values_[0] * _rhs[1] - values_[1] * _rhs[0]
380 template<
typename OtherScalar>
382 decltype(*this->data() * *_rhs.
data()) {
384 return std::inner_product(data() + 1, data() + DIM, _rhs.
data() + 1,
385 *data() * *_rhs.
data());
394 template<
typename S = Scalar>
395 decltype(std::declval<S>() * std::declval<S>()) sqrnorm()
const {
396 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 397 "to be the same type. (Never override the default template " 399 typedef decltype(values_[0] * values_[0]) RESULT;
400 return std::accumulate(values_.cbegin() + 1, values_.cend(),
401 values_[0] * values_[0],
402 [](
const RESULT &l,
const Scalar &r) {
return l + r * r; });
406 template<
typename S = Scalar>
408 decltype(
std::sqrt(
std::declval<
VectorT<S, DIM>>().sqrnorm())) {
409 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 410 "to be the same type. (Never override the default template " 412 return std::sqrt(sqrnorm());
415 template<
typename S = Scalar>
417 decltype(
std::declval<
VectorT<S, DIM>>().norm()) {
418 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 419 "to be the same type. (Never override the default template " 426 template<
typename S = Scalar>
429 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 430 "to be the same type. (Never override the default template " 432 return *
this /= norm();
437 template<
typename S = Scalar>
439 decltype(*this /
std::declval<
VectorT<S, DIM>>().norm()) {
440 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 441 "to be the same type. (Never override the default template " 443 return *
this / norm();
448 template<
typename S = Scalar>
449 typename std::enable_if<
455 static_assert(std::is_same<S, Scalar>::value,
"S and Scalar need " 456 "to be the same type. (Never override the default template " 459 if (n !=
static_cast<decltype(norm())
>(0)) {
474 return std::accumulate(
475 values_.cbegin() + 1, values_.cend(), values_[0]);
492 return *std::max_element(values_.cbegin(), values_.cend());
498 *std::max_element(values_.cbegin(), values_.cend(),
499 [](
const Scalar &a,
const Scalar &b) {
500 return std::abs(a) < std::abs(b);
506 return *std::min_element(values_.cbegin(), values_.cend());
512 *std::min_element(values_.cbegin(), values_.cend(),
513 [](
const Scalar &a,
const Scalar &b) {
514 return std::abs(a) < std::abs(b);
520 return l1_norm()/DIM;
525 return std::accumulate(values_.cbegin() + 1, values_.cend(),
526 std::abs(values_[0]),
527 [](
const Scalar &l,
const Scalar &r) {
528 return l + std::abs(r);
534 std::transform(values_.cbegin(), values_.cend(),
535 _rhs.values_.cbegin(),
537 [](
const Scalar &l,
const Scalar &r) {
538 return std::min(l, r);
546 std::transform(values_.cbegin(), values_.cend(),
547 _rhs.values_.cbegin(),
549 [&result](
const Scalar &l,
const Scalar &r) {
562 std::transform(values_.cbegin(), values_.cend(),
563 _rhs.values_.cbegin(),
565 [](
const Scalar &l,
const Scalar &r) {
566 return std::max(l, r);
574 std::transform(values_.cbegin(), values_.cend(),
575 _rhs.values_.cbegin(),
577 [&result](
const Scalar &l,
const Scalar &r) {
589 inline vector_type
min(
const vector_type& _rhs)
const {
590 return vector_type(*this).
minimize(_rhs);
594 inline vector_type
max(
const vector_type& _rhs)
const {
595 return vector_type(*this).
maximize(_rhs);
603 template<
typename Functor>
604 inline vector_type
apply(
const Functor& _func)
const {
606 std::transform(result.values_.begin(), result.values_.end(),
607 result.values_.begin(), _func);
613 std::fill(values_.begin(), values_.end(), _s);
624 return std::lexicographical_compare(
625 values_.begin(), values_.end(),
626 _rhs.values_.begin(), _rhs.values_.end());
631 noexcept(noexcept(std::swap(values_, _other.values_))) {
632 std::swap(values_, _other.values_);
640 using iterator =
typename container::iterator;
641 using const_iterator =
typename container::const_iterator;
642 using reverse_iterator =
typename container::reverse_iterator;
643 using const_reverse_iterator =
typename container::const_reverse_iterator;
645 iterator begin() noexcept {
return values_.begin(); }
646 const_iterator begin()
const noexcept {
return values_.cbegin(); }
647 const_iterator cbegin()
const noexcept {
return values_.cbegin(); }
649 iterator end() noexcept {
return values_.end(); }
650 const_iterator end()
const noexcept {
return values_.cend(); }
651 const_iterator cend()
const noexcept {
return values_.cend(); }
653 reverse_iterator rbegin() noexcept {
return values_.rbegin(); }
654 const_reverse_iterator rbegin()
const noexcept {
return values_.crbegin(); }
655 const_reverse_iterator crbegin()
const noexcept {
return values_.crbegin(); }
657 reverse_iterator rend() noexcept {
return values_.rend(); }
658 const_reverse_iterator rend()
const noexcept {
return values_.crend(); }
659 const_reverse_iterator crend()
const noexcept {
return values_.crend(); }
665 template<
typename Scalar,
int DIM,
typename OtherScalar>
667 decltype(rhs.operator*(_s)) {
673 template<
typename Scalar,
int DIM>
674 auto operator<<(std::ostream& os, const VectorT<Scalar, DIM> &_vec) ->
675 typename std::enable_if<
676 sizeof(decltype(os << _vec[0])) >= 0, std::ostream&>::type {
679 for (
int i = 1; i < DIM; ++i) {
680 os <<
" " << _vec[i];
686 template<
typename Scalar,
int DIM>
688 typename std::enable_if<
689 sizeof(decltype(is >> _vec[0])) >= 0, std::istream &>::type {
690 for (
int i = 0; i < DIM; ++i)
697 template<
typename Scalar,
int DIM>
704 template<
typename LScalar,
typename RScalar,
int DIM>
707 decltype(_v1 % _v2) {
713 template<
typename Scalar,
int DIM>
715 noexcept(noexcept(_v1.
swap(_v2))) {
828 using namespace Geometry;
833 template <>
const std::string typeName<Vec2f>();
834 template <>
const std::string typeName<Vec2d>();
835 template <>
const std::string typeName<Vec2i>();
836 template <>
const std::string typeName<Vec2ui>();
838 template <>
const std::string typeName<Vec3f>();
839 template <>
const std::string typeName<Vec3d>();
840 template <>
const std::string typeName<Vec3i>();
841 template <>
const std::string typeName<Vec3ui>();
843 template <>
const std::string typeName<Vec4f>();
844 template <>
const std::string typeName<Vec4d>();
845 template <>
const std::string typeName<Vec4i>();
846 template <>
const std::string typeName<Vec4ui>();
static constexpr int dim()
returns dimension of the vector (deprecated)
static constexpr size_t size()
returns dimension of the vector
auto length() const -> decltype(std::declval< VectorT< S, DIM >>().norm())
compute squared euclidean norm
vector_type & vectorize(const Scalar &_s)
store the same value in each component (e.g. to clear all entries)
void swap(VectorT &_other) noexcept(noexcept(std::swap(values_, _other.values_)))
swap with another vector
VectorT(Iterator it)
construct from a value array or any other iterator
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
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
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Scalar * data()
access to Scalar array
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
vector_type operator-(void) const
unary minus
const Scalar & operator[](size_t _i) const
get i'th element read-only
Scalar max() const
return the maximal component
auto homogenized() const -> typename std::enable_if< D==4, VectorT< decltype(std::declval< S >()/std::declval< S >()), DIM >>::type
vector_type apply(const Functor &_func) const
component-wise apply function object with Scalar operator()(Scalar).
Scalar l1_norm() const
compute L1 (Manhattan) norm
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
VectorT< Scalar, DIM > vector_type
type of this vector
Scalar mean_abs() const
return absolute arithmetic mean
bool operator!=(const vector_type &_rhs) const
component-wise comparison
static vector_type vectorized(const Scalar &_s)
store the same value in each component
Scalar min() const
return the minimal 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-division by scalar
Scalar value_type
the type of the scalar used in this template
Scalar & operator[](size_t _i)
get i'th element read-write
auto operator|(const VectorT< OtherScalar, DIM > &_rhs) const -> decltype(*this->data() **_rhs.data())
bool operator<(const vector_type &_rhs) const
lexicographical comparison
auto normalized() const -> decltype(*this/std::declval< VectorT< S, DIM >>().norm())
VectorT(const VectorT< otherScalarType, DIM > &_rhs)
copy & cast constructor (explicit)
vector_type & operator=(const VectorT< OtherScalar, DIM > &_rhs)
cast from vector with a different scalar type
auto norm() const -> decltype(std::sqrt(std::declval< VectorT< S, DIM >>().sqrnorm()))
compute euclidean norm
const Scalar * data() const
access to const Scalar array
vector_type min(const vector_type &_rhs) const
component-wise min
DLLEXPORT QString typeName(DataType _id)
Get the name of a type with given id.
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
Scalar l8_norm() const
compute l8_norm
bool operator==(const vector_type &_rhs) const
component-wise comparison
constexpr VectorT()
default constructor creates uninitialized values.
bool minimized(const vector_type &_rhs)
minimize values and signalize coordinate minimization
Scalar min_abs() const
return the minimal absolute component
Scalar mean() const
return arithmetic mean
Scalar max_abs() const
return the maximal absolute component
vector_type max(const vector_type &_rhs) const
component-wise max
bool maximized(const vector_type &_rhs)
maximize values and signalize coordinate maximization
vector_type &::type normalize_cond()
compute squared euclidean norm