Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions Sofa/framework/Type/src/sofa/type/Vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <cmath>
#include <array>
#include <cassert>
#include <numeric>

#define EQUALITY_THRESHOLD 1e-6

Expand Down Expand Up @@ -257,8 +258,7 @@ class Vec
// assign one value to all elements
constexpr void assign(const ValueType& value) noexcept
{
for (size_type i = 0; i < N; i++)
elems[i] = value;
std::fill_n(this->elems.data(), N, value);
}

/// Sets every element to 0.
Expand Down Expand Up @@ -472,6 +472,10 @@ class Vec
/// Squared norm.
constexpr ValueType norm2() const noexcept
{
// The STL function is slower when not compiling in full optimization.
// Therefore, the debugging would be slower.
// return std::inner_product(elems.begin(), elems.end(), elems.begin(), ValueType{});

ValueType r = this->elems[0]*this->elems[0];
for (Size i=1; i<N; i++)
r += this->elems[i]*this->elems[i];
Expand Down Expand Up @@ -583,7 +587,10 @@ class Vec
/// sum of all elements of the vector
constexpr ValueType sum() const noexcept
{
ValueType sum = ValueType(0.0);
// The STL function is slower when not compiling in full optimization.
// Therefore, the debugging would be slower.
// return std::accumulate(elems.begin(), elems.end(), ValueType{});
ValueType sum = static_cast<ValueType>(0.0);
for (Size i=0; i<N; i++)
sum += this->elems[i];
return sum;
Expand All @@ -597,8 +604,8 @@ class Vec
{
if constexpr (std::is_floating_point_v<ValueType>)
{
return std::equal(this->elems.begin(), this->elems.end(), b.elems.begin(),
[](auto x, auto y) { return std::abs(x - y) < EQUALITY_THRESHOLD; });
constexpr auto equalTest = [](auto x, auto y) { return std::abs(x - y) < EQUALITY_THRESHOLD; };
return std::equal(this->elems.begin(), this->elems.end(), b.elems.begin(), equalTest );
}
else
{
Expand Down Expand Up @@ -680,19 +687,19 @@ class Vec
return elems.end();
}

constexpr reference front()
constexpr reference front() noexcept
{
return elems[0];
}
constexpr const_reference front() const
constexpr const_reference front() const noexcept
{
return elems[0];
}
constexpr reference back()
constexpr reference back() noexcept
{
return elems[N - 1];
}
constexpr const_reference back() const
constexpr const_reference back() const noexcept
{
return elems[N - 1];
}
Expand All @@ -717,7 +724,7 @@ class VecNoInit : public Vec<N,real>
{}

constexpr VecNoInit(Vec<N,real>&& v) noexcept
: Vec<N,real>(v)
: Vec<N,real>(std::move(v))
{}

using Vec<N,real>::Vec;
Expand Down
24 changes: 9 additions & 15 deletions Sofa/framework/Type/src/sofa/type/fixed_array_algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,25 @@
******************************************************************************/
#pragma once

namespace sofa::type::pairwise
{
#include <algorithm>

/// @brief clamp a single value. This function should be removed when std::clamp will be available
template<class T>
const T& stdclamp( const T& v, const T& lo, const T& hi )
namespace sofa::type::pairwise
{
assert( !(hi < lo) );
return (v < lo) ? lo : (hi < v) ? hi : v;
}

/// @brief clamp all the values of a fixed_array to be within a given interval.
template<class T, class TT=typename T::value_type, size_t TN=T::static_size>
T clamp(const T& in, const TT& minValue, const TT& maxValue)
template<class T, size_t TN=T::static_size>
T clamp(const T& in, const typename T::value_type& minValue, const typename T::value_type& maxValue)
{
T result {};
for(typename T::size_type i=0; i < typename T::size_type(TN); ++i)
{
result[i] = stdclamp(in[i], minValue, maxValue);
result[i] = std::clamp(in[i], minValue, maxValue);
}
return result;
}

/// @brief pairwise add of two fixed_array
template<class T, class TT=typename T::value_type, size_t TN=T::static_size>
template<class T, size_t TN=T::static_size>
constexpr T operator+(const T& l, const T& r)
{
T result {};
Expand All @@ -57,7 +51,7 @@ constexpr T operator+(const T& l, const T& r)
}

/// @brief pairwise subtract of two fixed_array
template<class T, class TT=typename T::value_type, size_t TN=T::static_size>
template<class T, size_t TN=T::static_size>
constexpr T operator-(const T& l, const T& r)
{
T result {};
Expand All @@ -69,7 +63,7 @@ constexpr T operator-(const T& l, const T& r)
}

/// @brief multiply from l the r components.
template<class T, class TT=typename T::value_type, size_t TN=T::static_size>
template<class T, size_t TN=T::static_size>
T operator*(const T& r, const typename T::value_type& f)
{
T result {};
Expand All @@ -81,7 +75,7 @@ T operator*(const T& r, const typename T::value_type& f)
}

/// @brief multiply from l the r components.
template<class T, class TT=typename T::value_type, size_t TN=T::static_size>
template<class T, size_t TN=T::static_size>
T operator/(const T& r, const typename T::value_type& f)
{
T result {};
Expand Down
Loading
Loading