Skip to content

Commit

Permalink
[mc_rtc/Schema] Add support for arbitrary vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
gergondet committed Jan 11, 2024
1 parent 69397dc commit 4205ce8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
16 changes: 15 additions & 1 deletion include/mc_rtc/Schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ inline constexpr bool is_std_map_schema_v = []()
else { return false; }
}();

/** Type-trait to detect if something is an Eigen::VectorNd */
template<typename T>
struct is_eigen_vector : public std::false_type
{
};

template<typename Scalar, int Rows, int Options, int MaxRows>
struct is_eigen_vector<Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, 1>> : public std::true_type
{
};

template<typename T>
inline constexpr bool is_eigen_vector_v = is_eigen_vector<T>::value;

template<typename T, bool IsRequired, bool IsInteractive, bool HasChoices = false, bool IsStatic = false>
void addValueToForm(const T & value,
const std::string & description,
Expand Down Expand Up @@ -182,7 +196,7 @@ void addValueToForm(const T & value,
{
form.addElement(mc_rtc::gui::FormTransformInput(description, IsRequired, get_value, IsInteractive));
}
else if constexpr(std::is_same_v<T, sva::ForceVecd>)
else if constexpr(std::is_same_v<T, sva::ForceVecd> || details::is_eigen_vector_v<T>)
{
form.addElement(mc_rtc::gui::FormArrayInput(description, IsRequired, get_value));
}
Expand Down
17 changes: 17 additions & 0 deletions include/mc_rtc/gui/details/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ inline constexpr bool has_compatible_signature_v = std::is_convertible_v<Callbac
/** Given a type provides appropriate labels.
*
* The following types are supported:
* - Eigen::Vector2d -> {"x", "y"}
* - Eigen::Vector3d -> {"x", "y", "z"}
* - Eigen::Vector4d -> {"x", "y", "z", "w"}
* - Eigen::Quaterniond -> {"w", "x", "y", "z"}
* - Eigen::Quaterniond -> {"w", "x", "y", "z"}
* - sva::MotionVecd -> {"wx", "wy", "wz", "vx", "vy", "vz"}
* - sva::ForceVecd -> {"cx", "cy", "cz", "fx", "fy", "fz"}
Expand All @@ -119,13 +122,27 @@ struct Labels
inline static const std::vector<std::string> labels = {};
};

template<>
struct Labels<Eigen::Vector2d>
{
static constexpr bool has_labels = true;
inline static const std::vector<std::string> labels = {"x", "y"};
};

template<>
struct Labels<Eigen::Vector3d>
{
static constexpr bool has_labels = true;
inline static const std::vector<std::string> labels = {"x", "y", "z"};
};

template<>
struct Labels<Eigen::Vector4d>
{
static constexpr bool has_labels = true;
inline static const std::vector<std::string> labels = {"x", "y", "z", "w"};
};

template<>
struct Labels<Eigen::Quaterniond>
{
Expand Down
2 changes: 2 additions & 0 deletions tests/samples_Schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ struct SimpleSchema
MEMBER(MapType, jointValues, "Some joint values")
MEMBER(sva::ForceVecd, wrench, "Target wrench")
MEMBER(sva::PTransformd, pt, "Some transform")
MEMBER(Eigen::Vector2d, v2d, "Some 2d setting")
MEMBER(Eigen::Vector4d, v4d, "Some 4d setting")
#undef MEMBER
};

0 comments on commit 4205ce8

Please sign in to comment.