Skip to content
Merged
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
23 changes: 12 additions & 11 deletions Sofa/framework/DefaultType/src/sofa/defaulttype/RigidTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class StdRigidTypes<3, real>
getVCenter(c)[2] += static_cast<Real>(z);
}

static constexpr const char* Name();
static constexpr const char* Name() { return s_name.data(); }

/// Return a Deriv with random value. Each entry with magnitude smaller than the given value.
static Deriv randomDeriv(Real minMagnitude, Real maxMagnitude)
Expand Down Expand Up @@ -240,17 +240,18 @@ class StdRigidTypes<3, real>
/// create a rotation from Euler angles. For homogeneity with 2D.
static Quat rotationEuler(Real x, Real y, Real z) { return Quat::fromEuler(x, y, z); }

private:
struct Prefix { static constexpr std::string_view value = "Rigid"; };

static constexpr auto s_name = helper::makeFixedSizeTypeName<Prefix, spatial_dimensions, Real>();
};


typedef StdRigidTypes<3,double> Rigid3dTypes;
typedef StdRigidTypes<3,float> Rigid3fTypes;

/// We now use template aliases so we do not break backward compatibility.
template<> constexpr const char* Rigid3dTypes::Name() { return "Rigid3d"; }
template<> constexpr const char* Rigid3fTypes::Name() { return "Rigid3f"; }

typedef StdRigidTypes<3,SReal> Rigid3Types; ///< un-defined precision type
typedef StdRigidTypes<3,SReal> RigidTypes; ///< alias (beurk)
typedef StdRigidTypes<3,SReal> Rigid3Types;
typedef StdRigidTypes<3,SReal> RigidTypes;

static_assert(type::isRigidType<Rigid3dTypes>);
static_assert(type::isRigidType<Rigid3fTypes>);
Expand Down Expand Up @@ -289,7 +290,7 @@ class StdRigidTypes<2, real>
static constexpr const DRot& getDRot(const Deriv& d) { return getVOrientation(d); }
static constexpr void setDRot(Deriv& d, const DRot& v) { getVOrientation(d) = v; }

static constexpr const char* Name();
static constexpr const char* Name() { return s_name.data(); }

typedef type::vector<Coord> VecCoord;
typedef type::vector<Deriv> VecDeriv;
Expand Down Expand Up @@ -411,12 +412,12 @@ class StdRigidTypes<2, real>
/// create a rotation from Euler angles (only the first is used). For homogeneity with 3D.
static constexpr CRot rotationEuler( Real x, Real , Real ){ return CRot(x); }

private:
struct Prefix { static constexpr std::string_view value = "Rigid"; };

static constexpr auto s_name = helper::makeFixedSizeTypeName<Prefix, spatial_dimensions, Real>();
};

template<> constexpr const char* Rigid2dTypes::Name() { return "Rigid2d"; }
template<> constexpr const char* Rigid2fTypes::Name() { return "Rigid2f"; }

static_assert(type::isRigidType<Rigid2dTypes>);
static_assert(type::isRigidType<Rigid2fTypes>);

Expand Down
62 changes: 27 additions & 35 deletions Sofa/framework/DefaultType/src/sofa/defaulttype/VecTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class StdVectorTypes
Impl<spatial_dimensions,T>::add(c,x,y,z);
}

static constexpr const char* Name();
static constexpr const char* Name() { return s_name.data();}

static Coord interpolate(const type::vector< Coord > &ancestors, const type::vector< Real > &coefs)
{
Expand All @@ -193,50 +193,42 @@ class StdVectorTypes

return c;
}
};


/// 3D DOFs, double precision
typedef StdVectorTypes<type::Vec3d,type::Vec3d,double> Vec3dTypes;
template<> constexpr const char* Vec3dTypes::Name() { return "Vec3d"; }
private:

/// 2D DOFs, double precision
typedef StdVectorTypes<type::Vec2d, type::Vec2d,double> Vec2dTypes;
template<> constexpr const char* Vec2dTypes::Name() { return "Vec2d"; }
struct Prefix { static constexpr std::string_view value = "Vec"; };

/// 1D DOFs, double precision
typedef StdVectorTypes<type::Vec1d, type::Vec1d,double> Vec1dTypes;
template<> constexpr const char* Vec1dTypes::Name() { return "Vec1d"; }
static constexpr auto s_name = helper::makeFixedSizeTypeName<Prefix, spatial_dimensions, Real>();
};

/// 6D DOFs, double precision
typedef StdVectorTypes<type::Vec6d, type::Vec6d,double> Vec6dTypes;
template<> constexpr const char* Vec6dTypes::Name() { return "Vec6d"; }
namespace internal
{
template<sofa::Size N, class T> using VecTypes = StdVectorTypes<type::Vec<N, T>, type::Vec<N, T>, T>;
}

/// 3f DOFs, single precision
typedef StdVectorTypes<type::Vec3f, type::Vec3f,float> Vec3fTypes;
template<> constexpr const char* Vec3fTypes::Name() { return "Vec3f"; }
using Vec3dTypes = internal::VecTypes<3, double>;
using Vec2dTypes = internal::VecTypes<2, double>;
using Vec1dTypes = internal::VecTypes<1, double>;
using Vec6dTypes = internal::VecTypes<6, double>;

/// 2f DOFs, single precision
typedef StdVectorTypes<type::Vec2f, type::Vec2f,float> Vec2fTypes;
template<> constexpr const char* Vec2fTypes::Name() { return "Vec2f"; }
using Vec3fTypes = internal::VecTypes<3, float>;
using Vec2fTypes = internal::VecTypes<2, float>;
using Vec1fTypes = internal::VecTypes<1, float>;
using Vec6fTypes = internal::VecTypes<6, float>;

/// 1f DOFs, single precision
typedef StdVectorTypes<type::Vec1f, type::Vec1f,float> Vec1fTypes;
template<> constexpr const char* Vec1fTypes::Name() { return "Vec1f"; }
using Vec3Types = internal::VecTypes<3, SReal>;
using Vec2Types = internal::VecTypes<2, SReal>;
using Vec1Types = internal::VecTypes<1, SReal>;
using Vec6Types = internal::VecTypes<6, SReal>;

/// 6f DOFs, single precision
typedef StdVectorTypes<type::Vec6f, type::Vec6f,float> Vec6fTypes;
template<> constexpr const char* Vec6fTypes::Name() { return "Vec6f"; }

/// 6D DOFs, double precision (default)
typedef StdVectorTypes<type::Vec6, type::Vec6, type::Vec6::value_type> Vec6Types;
/// 3D DOFs, double precision (default)
typedef StdVectorTypes<type::Vec3, type::Vec3, type::Vec3::value_type> Vec3Types;
/// 2D DOFs, double precision (default)
typedef StdVectorTypes<type::Vec2, type::Vec2, type::Vec2::value_type> Vec2Types;
/// 1D DOFs, double precision (default)
typedef StdVectorTypes<type::Vec1, type::Vec1, type::Vec1::value_type> Vec1Types;
static_assert(std::string_view(Vec1dTypes::Name()) == "Vec1d");
static_assert(std::string_view(Vec2dTypes::Name()) == "Vec2d");
static_assert(std::string_view(Vec3dTypes::Name()) == "Vec3d");

static_assert(std::string_view(Vec1fTypes::Name()) == "Vec1f");
static_assert(std::string_view(Vec2fTypes::Name()) == "Vec2f");
static_assert(std::string_view(Vec3fTypes::Name()) == "Vec3f");

static_assert(!type::isRigidType<Vec6dTypes>);
static_assert(!type::isRigidType<Vec3dTypes>);
Expand Down
33 changes: 31 additions & 2 deletions Sofa/framework/Helper/src/sofa/helper/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
#pragma once

#include <sofa/helper/config.h>
#include <sofa/type/trait/TypeTrait.h>

#include <sstream>
#include <string>
#include <string_view>
#include <vector>
#include <sstream>


namespace sofa::helper
{
Expand Down Expand Up @@ -109,4 +109,33 @@ SOFA_HELPER_API std::string_view removeTrailingCharacter(std::string_view sv, ch
///@brief Removes specified trailing characters from a string view.
SOFA_HELPER_API std::string_view removeTrailingCharacters(std::string_view sv, std::initializer_list<char> characters);

template<std::integral T>
constexpr std::size_t numDigits(T n)
{
std::size_t d = 1;
while (n >= 10) { n /= 10; ++d; }
return d;
}

template<class Prefix, sofa::Size N, class T>
constexpr static auto makeFixedSizeTypeName()
{
constexpr std::string_view prefix = Prefix::value;
constexpr std::size_t d = sofa::helper::numDigits(N);
constexpr auto suffix = type::TypeTrait<T>::suffix;

std::array<char, prefix.size() + d + suffix.size() + 1> s{};
std::copy(prefix.begin(), prefix.end(), s.begin());
int n = N;
for (int i = d - 1; i >= 0; --i)
{
s[prefix.size() + i] = static_cast<char>('0' + n % 10);
n /= 10;
}
//suffix
std::copy(suffix.begin(), suffix.end(), s.begin() + prefix.size() + d);
s[prefix.size() + d + suffix.size()] = '\0';
return s;
}

} // namespace sofa::helper
1 change: 1 addition & 0 deletions Sofa/framework/Type/src/sofa/type/trait/TypeTrait.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
******************************************************************************/
#pragma once

#include <string>
#include <string_view>

namespace sofa::type
Expand Down
Loading