6 #ifndef LIB_ALMATH_SCENEGRAPH_URDF_H
7 #define LIB_ALMATH_SCENEGRAPH_URDF_H
9 #include <almath/api.h>
14 #include <boost/property_tree/ptree.hpp>
15 #include <boost/optional.hpp>
16 #include <boost/variant.hpp>
17 #include <boost/range/adaptor/map.hpp>
18 #include <boost/range/adaptor/filtered.hpp>
19 #include <boost/function/function_fwd.hpp>
20 #include <boost/ref.hpp>
21 #include <boost/tokenizer.hpp>
22 #include <boost/multi_index_container.hpp>
23 #include <boost/multi_index/ordered_index.hpp>
24 #include <boost/multi_index/identity.hpp>
25 #include <boost/multi_index/mem_fun.hpp>
26 #include <boost/multi_index/global_fun.hpp>
27 #include <boost/multi_index/sequenced_index.hpp>
30 #include <type_traits>
31 #include <boost/property_tree/stream_translator.hpp>
34 namespace property_tree {
40 template <
typename Ch,
typename Traits>
41 struct customize_stream<Ch, Traits, double, void> {
42 static void insert(std::basic_ostream<Ch, Traits> &s,
const double &e) {
43 s.precision(std::numeric_limits<double>::max_digits10);
46 static void extract(std::basic_istream<Ch, Traits> &s,
double &e) {
69 return (a[0] == 0) && (a[1] == 0) && (a[2] == 0);
73 return (a[0] == 1) && (a[1] == 1) && (a[2] == 1);
79 ALMATH_API std::string
name(
const ptree &pt);
134 void operator=(
RobotTree const &other) =
delete;
136 const ptree &link(
const std::string &
name)
const;
137 ptree &link(
const std::string &name);
139 const ptree &joint(
const std::string &name)
const;
140 ptree &joint(
const std::string &name);
142 const std::string &root_link()
const;
147 void rm_root_joint();
153 void rm_leaf_joint(
const std::string &name);
178 void transport_root_link_frame(
const Pose &pose);
240 void define_as_root_link(
const std::string &name);
259 bool is_mimic_tree_flat()
const;
262 std::unique_ptr<detail::RobotTreeP> _p;
271 template <
typename Scalar, std::
size_t N>
275 static_assert(std::is_scalar<Scalar>::value,
276 "ScalarArrayTranslator requires a scalar");
283 boost::tokenizer<> tok(
284 str, boost::char_delimiters_separator<char>(
false,
"",
" \t\n\v\f\r"));
285 boost::optional<Scalar> d;
288 boost::property_tree::translator_between<internal_type, Scalar>::type tr;
290 boost::tokenizer<>::iterator beg = tok.begin();
292 for (; beg != tok.end() && i < N; ++beg, ++i) {
293 d = tr.get_value(*beg);
294 if (!d)
return boost::optional<external_type>();
297 if (i != N || beg != tok.end())
298 return boost::optional<external_type>();
299 return boost::optional<external_type>(x);
305 boost::property_tree::translator_between<internal_type, Scalar>::type tr;
306 std::ostringstream ss;
308 ss << *tr.put_value(v[0]);
309 for (
auto i=1u; i < N; ++i) {
310 ss <<
" " << *tr.put_value(v[i]);
323 const Array3d &xyz()
const {
return _xyz; }
324 const Array3d &rpy()
const {
return _rpy; }
326 ALMATH_API
friend Pose
operator*(
const Pose &lhs,
const Pose &rhs);
328 static Pose from_ptree(
const ptree &pt);
329 static Pose from_ptree(
const boost::optional<const ptree &> &pt);
330 boost::optional<ptree> to_ptree()
const;
338 return (lhs.xyz() == rhs.xyz()) && (lhs.rpy() == rhs.rpy());
341 return !(lhs == rhs);
353 std::string joint()
const;
354 double multiplier()
const;
355 double offset()
const;
361 enum Type { revolute, continuous, prismatic, fixed, floating, planar };
364 std::string
name()
const;
378 boost::optional<std::pair<double, double>> limit_lower_upper()
const;
379 boost::optional<double> limit_effort()
const;
380 boost::optional<double> limit_velocity()
const;
381 boost::optional<Mimic> mimic()
const;
426 std::string filename()
const;
431 using Geometry = boost::variant<Box, Cylinder, Sphere, Mesh>;
440 static bool is_visual(
const ptree::value_type &val);
448 std::string
name()
const;
449 boost::optional<Inertial> inertial()
const;
453 inline auto _visual_ptrees() const
454 -> boost::select_second_const_range<
455 decltype(boost::adaptors::filter(pt,
Visual::is_visual))> {
456 return boost::adaptors::values(
465 -> boost::transformed_range<
466 decltype(&_makeVisual),
467 const decltype(_visual_ptrees())> {
468 return boost::adaptors::transform(_visual_ptrees(), &_makeVisual);
516 std::function<
bool(
const ptree &joint)> pred);
530 bool discover(
const ptree &joint);
531 void finish(
const ptree &joint);
548 ptree &robot, std::function<std::string(std::string)> op);
std::array< Scalar, N > external_type
boost::optional< internal_type > put_value(const external_type &v)
ALMATH_API void squashJointMass(RobotTree &parser, const std::string &name)
ALMATH_API std::string name(const ptree &pt)
ALMATH_API void put_name(ptree &pt, const std::string &name)
ALMATH_API std::vector< std::string > makeMasslessJointsFixed(RobotTree &parser)
ALMATH_API void squashFixedJointsMass(RobotTree &parser)
bool operator==(const Pose &lhs, const Pose &rhs)
boost::variant< Box, Cylinder, Sphere, Mesh > Geometry
std::array< double, 3 > Array3d
ALMATH_API std::string parent_link(const ptree &pt)
bool is_identity(const Pose &p)
ALMATH_API std::vector< std::string > removeSubTreeIfJoint(RobotTree &parser, std::function< bool(const ptree &joint)> pred)
static bool is_visual(const ptree::value_type &val)
bool operator!=(const Pose &lhs, const Pose &rhs)
virtual void finish(ptree &)
boost::optional< external_type > get_value(const internal_type &str)
Transform inverse(const Transform &tf)
static void extract(std::basic_istream< Ch, Traits > &s, double &e)
RobotTree::JointVisitor JointVisitor
virtual bool discover(ptree &)
ALMATH_API std::string child_link(const ptree &pt)
virtual bool discover(const ptree &)
static void insert(std::basic_ostream< Ch, Traits > &s, const double &e)
ALMATH_API void makeJointFloating(RobotTree &parser, const std::string &name)
ALMATH_API void makeJointFixed(RobotTree &parser, const std::string &name)
Transform operator*(const Transform &lhs, const Transform &rhs)
virtual void finish(const ptree &)
ALMATH_API std::vector< std::string > makeContinuousJointsFixed(RobotTree &parser)
ALMATH_API void transform_filenames(ptree &robot, std::function< std::string(std::string)> op)
boost::property_tree::ptree ptree
Pose(const Array3d &xyz, const Array3d &rpy)
bool is_ones(const Array3d &a)
std::string internal_type
RobotTree::JointConstVisitor JointConstVisitor
bool is_zero(const Array3d &a)
auto visuals() const -> boost::transformed_range< decltype(&_makeVisual), const decltype(_visual_ptrees())>