From d1b821f84a493925a7785af273ceb4a565e6813d Mon Sep 17 00:00:00 2001 From: Frederick Roy Date: Thu, 8 Sep 2022 10:40:57 +0900 Subject: [PATCH 1/3] mat: std abs only constexpr in c++23 --- Sofa/framework/Type/src/sofa/type/Mat.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sofa/framework/Type/src/sofa/type/Mat.h b/Sofa/framework/Type/src/sofa/type/Mat.h index 320c2a0eb7d..87d7f9e71b7 100644 --- a/Sofa/framework/Type/src/sofa/type/Mat.h +++ b/Sofa/framework/Type/src/sofa/type/Mat.h @@ -28,6 +28,7 @@ #include #include +#include namespace // anonymous { @@ -35,7 +36,7 @@ namespace // anonymous constexpr real rabs(const real r) { if constexpr (std::is_signed()) - return std::abs(r); + return r < 0 ? -r : r; else return r; } From e2e637c56a1fef3fb3fa56fa4e05749a376ce248 Mon Sep 17 00:00:00 2001 From: Frederick Roy Date: Thu, 8 Sep 2022 10:54:33 +0900 Subject: [PATCH 2/3] sqrt is not constexpr in std --- Sofa/framework/Type/src/sofa/type/Mat.h | 1 - Sofa/framework/Type/src/sofa/type/Vec.h | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Sofa/framework/Type/src/sofa/type/Mat.h b/Sofa/framework/Type/src/sofa/type/Mat.h index 87d7f9e71b7..149a99eb6ac 100644 --- a/Sofa/framework/Type/src/sofa/type/Mat.h +++ b/Sofa/framework/Type/src/sofa/type/Mat.h @@ -28,7 +28,6 @@ #include #include -#include namespace // anonymous { diff --git a/Sofa/framework/Type/src/sofa/type/Vec.h b/Sofa/framework/Type/src/sofa/type/Vec.h index e36a44a464d..37befa51805 100644 --- a/Sofa/framework/Type/src/sofa/type/Vec.h +++ b/Sofa/framework/Type/src/sofa/type/Vec.h @@ -42,7 +42,7 @@ namespace // anonymous constexpr real rabs(const real r) { if constexpr (std::is_signed()) - return std::abs(r); + return r < 0 ? -r : r; else return r; } @@ -441,7 +441,7 @@ class Vec : public sofa::type::fixed_array } /// Euclidean norm. - constexpr ValueType norm() const noexcept + ValueType norm() const noexcept { return ValueType(std::sqrt(norm2())); } @@ -449,7 +449,7 @@ class Vec : public sofa::type::fixed_array /// l-norm of the vector /// The type of norm is set by parameter l. /// Use l<0 for the infinite norm. - constexpr ValueType lNorm( int l ) const + ValueType lNorm( int l ) const { if( l==2 ) return norm(); // euclidian norm else if( l<0 ) // infinite norm @@ -504,21 +504,21 @@ class Vec : public sofa::type::fixed_array /// Normalize the vector. /// returns false iff the norm is too small - constexpr bool normalize(ValueType threshold=std::numeric_limits::epsilon()) noexcept + bool normalize(ValueType threshold=std::numeric_limits::epsilon()) noexcept { return normalizeWithNorm(norm(),threshold); } /// Normalize the vector with a failsafe. /// If the norm is too small, the vector becomes the failsafe. - constexpr void normalize(Vec failsafe, ValueType threshold=std::numeric_limits::epsilon()) noexcept + void normalize(Vec failsafe, ValueType threshold=std::numeric_limits::epsilon()) noexcept { if( !normalize(threshold) ) *this=failsafe; } /// Return the normalized vector. /// @warning 'this' is not normalized. - constexpr Vec normalized() const noexcept + Vec normalized() const noexcept { Vec r(*this); r.normalize(); From 7b55fef90ab393d587209b7b80d8400051d04542 Mon Sep 17 00:00:00 2001 From: Frederick Roy Date: Tue, 13 Sep 2022 10:02:15 +0900 Subject: [PATCH 3/3] make rabs non-constexpr as std::abs is not constexpr --- Sofa/framework/Type/src/sofa/type/Mat.h | 20 ++++++++++---------- Sofa/framework/Type/src/sofa/type/Vec.h | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Sofa/framework/Type/src/sofa/type/Mat.h b/Sofa/framework/Type/src/sofa/type/Mat.h index 149a99eb6ac..be6a3befa12 100644 --- a/Sofa/framework/Type/src/sofa/type/Mat.h +++ b/Sofa/framework/Type/src/sofa/type/Mat.h @@ -32,16 +32,16 @@ namespace // anonymous { template - constexpr real rabs(const real r) + real rabs(const real r) { if constexpr (std::is_signed()) - return r < 0 ? -r : r; + return std::abs(r); else return r; } template - constexpr real equalsZero(const real r, const real epsilon = std::numeric_limits::epsilon()) + real equalsZero(const real r, const real epsilon = std::numeric_limits::epsilon()) { return rabs(r) <= epsilon; } @@ -65,8 +65,8 @@ class Mat : public fixed_array, L> typedef VecNoInit LineNoInit; typedef Vec Col; - static const Size nbLines = L; - static const Size nbCols = C; + static constexpr Size nbLines = L; + static constexpr Size nbCols = C; constexpr Mat() noexcept { @@ -389,7 +389,7 @@ class Mat : public fixed_array, L> } - constexpr bool isSymmetric() const + bool isSymmetric() const { for (Size i=0; i, L> return true; } - constexpr bool isDiagonal() const noexcept + bool isDiagonal() const noexcept { for (Size i=0; i& m) noexcept // one-norm of a 3 x 3 matrix template -constexpr real oneNorm(const Mat<3,3,real>& A) +real oneNorm(const Mat<3,3,real>& A) { real norm = 0.0; for (sofa::Size i=0; i<3; i++) @@ -813,7 +813,7 @@ constexpr real oneNorm(const Mat<3,3,real>& A) // inf-norm of a 3 x 3 matrix template -constexpr real infNorm(const Mat<3,3,real>& A) +real infNorm(const Mat<3,3,real>& A) { real norm = 0.0; for (sofa::Size i=0; i<3; i++) @@ -847,7 +847,7 @@ constexpr Vec diagonal(const Mat& m) /// Matrix inversion (general case). template -[[nodiscard]] constexpr bool invertMatrix(Mat& dest, const Mat& from) +[[nodiscard]] bool invertMatrix(Mat& dest, const Mat& from) { sofa::Size i{0}, j{0}, k{0}; Vec r, c, row, col; diff --git a/Sofa/framework/Type/src/sofa/type/Vec.h b/Sofa/framework/Type/src/sofa/type/Vec.h index 37befa51805..e090a3d5e8f 100644 --- a/Sofa/framework/Type/src/sofa/type/Vec.h +++ b/Sofa/framework/Type/src/sofa/type/Vec.h @@ -39,10 +39,10 @@ namespace sofa::type namespace // anonymous { template - constexpr real rabs(const real r) + real rabs(const real r) { if constexpr (std::is_signed()) - return r < 0 ? -r : r; + return std::abs(r); else return r; } @@ -526,7 +526,7 @@ class Vec : public sofa::type::fixed_array } /// return true if norm()==1 - constexpr bool isNormalized( ValueType threshold=std::numeric_limits::epsilon()*(ValueType)10 ) const + bool isNormalized( ValueType threshold=std::numeric_limits::epsilon()*(ValueType)10 ) const { return rabs( norm2() - static_cast(1) ) <= threshold; }