3 #include <assimp/Importer.hpp>
4 #include <assimp/postprocess.h>
5 #include <assimp/scene.h>
7 #include <geometric_shapes/shape_operations.h>
9 #include <dart/dynamics/BodyNode.hpp>
10 #include <dart/dynamics/WeldJoint.hpp>
27 : name_(name), skeleton_(dart::dynamics::Skeleton::create(name_)), acm_(
std::make_shared<
ACM>(this))
34 dart::dynamics::WeldJoint::Properties properties;
35 dart::dynamics::BodyNode::Properties node;
37 node.mName = properties.mName =
"root";
40 skeleton_->createJointAndBodyNodePair<dart::dynamics::WeldJoint>(
nullptr, properties, node);
41 const auto &root = pair.second;
43 const auto &objects =
scene->getCollisionObjects();
44 for (
const auto &
object : objects)
46 const auto &geometry =
scene->getObjectGeometry(
object);
47 const auto &pose =
scene->getObjectPose(
object);
49 dart::dynamics::FreeJoint::Properties joint;
57 setColor(pair.second, dart::Color::Blue(0.2));
60 const auto &acm =
scene->getACMConst();
62 acm.getAllEntryNames(names);
65 for (
const auto &name1 : names)
66 for (
const auto &name2 : names)
67 if (acm.getEntry(name1, name2, type))
70 acm_->enableCollision(name1, name2);
72 acm_->disableCollision(name1, name2);
78 auto structure = std::make_shared<Structure>(newName);
79 structure->setSkeleton(
skeleton_->cloneSkeleton(newName));
81 for (
const auto &pair :
acm_->getDisabledPairsConst())
82 structure->getACM()->disableCollision(pair.first, pair.second);
124 for (
const auto &node :
skeleton_->getBodyNodes())
127 const std::size_t index = node->getIndexInSkeleton();
129 out << sname <<
"_" << index <<
"[label=\"" << name <<
"\"]" <<
std::endl;
131 const auto &joint = node->getParentJoint();
135 const auto &parent = joint->getParentBodyNode();
138 const auto &pindex = parent->getIndexInSkeleton();
139 out << sname <<
"_" << pindex <<
"->"
140 << sname <<
"_" << index
155 body->createShapeNodeWith<dart::dynamics::VisualAspect,
156 dart::dynamics::CollisionAspect,
157 dart::dynamics::DynamicsAspect>(shape);
159 dart::dynamics::Inertia inertia;
164 inertia.setMass(mass);
165 inertia.setMoment(shape->computeInertia(mass));
167 if (not inertia.verify(
false))
168 inertia = dart::dynamics::Inertia(mass);
170 body->setInertia(inertia);
173 auto *joint = body->getParentJoint();
175 for (
std::size_t i = 0; i < joint->getNumDofs(); ++i)
181 const dart::dynamics::ShapePtr &shape,
182 dart::dynamics::BodyNode *parent)
184 dart::dynamics::BodyNode::Properties node;
185 node.mName = properties.mName;
188 skeleton_->createJointAndBodyNodePair<dart::dynamics::RevoluteJoint>(parent, properties, node);
191 pair.first->setPositionLimitEnforced(
true);
197 const dart::dynamics::ShapePtr &shape,
198 dart::dynamics::BodyNode *parent)
200 dart::dynamics::BodyNode::Properties node;
201 node.mName = properties.mName;
204 skeleton_->createJointAndBodyNodePair<dart::dynamics::PrismaticJoint>(parent, properties, node);
207 pair.first->setPositionLimitEnforced(
true);
213 const dart::dynamics::ShapePtr &shape,
214 dart::dynamics::BodyNode *parent)
216 dart::dynamics::BodyNode::Properties node;
217 node.mName = properties.mName;
219 auto pair =
skeleton_->createJointAndBodyNodePair<dart::dynamics::FreeJoint>(parent, properties, node);
222 pair.first->setPositionLimitEnforced(
true);
228 const dart::dynamics::ShapePtr &shape,
229 dart::dynamics::BodyNode *parent)
231 dart::dynamics::BodyNode::Properties node;
232 node.mName = properties.mName;
234 auto pair =
skeleton_->createJointAndBodyNodePair<dart::dynamics::WeldJoint>(parent, properties, node);
242 const double thickness = 0.01;
244 dart::dynamics::WeldJoint::Properties joint;
245 joint.mName =
"ground";
246 joint.mT_ParentBodyToJoint.translation() = Eigen::Vector3d(0, 0, z - thickness);
248 auto ground =
makeBox(radius, radius, thickness);
251 setColor(pair.second, dart::Color::Blue(0.2));
256 setJoint(name, Eigen::VectorXd::Constant(1, value));
262 joint->setPositions(value);
270 return ik->solveAndApply(
true);
275 skeleton_->getDof(index)->setPosition(value);
281 for (
const auto *joint :
skeleton_->getJoints())
303 tf = child->getTransform(
frame);
305 tf = child->getWorldTransform();
307 dart::dynamics::FreeJoint::Properties joint;
308 joint.mName = child->getName();
309 auto *jt = child->moveTo<dart::dynamics::FreeJoint>(
skeleton_,
frame, joint);
317 if (joint ==
nullptr)
318 RBX_ERROR(
"Cannot find joint named %s to set TF!", name);
320 joint->setTransformFromParentBodyNode(tf);
326 auto nodes =
skeleton_->getBodyNodes(name);
331 dart::dynamics::FreeJoint::Properties joint;
339 setColor(pair.second, dart::Color::Blue(0.2));
350 const auto &dimensions = geometry->getDimensions();
352 switch (geometry->getType())
372 return std::make_shared<dart::dynamics::BoxShape>(v);
377 return makeBox(Eigen::Vector3d{x, y, z});
382 return std::make_shared<dart::dynamics::CylinderShape>(radius, height);
387 return std::make_shared<dart::dynamics::SphereShape>(radius);
393 Eigen::Vector3d dimensions = geometry->getDimensions();
397 const auto &temp_file_name =
".robowflex_tmp.stl";
399 auto shape = std::dynamic_pointer_cast<shapes::Mesh>(geometry->getShape());
402 shapes::writeSTLBinary(shape.get(), buffer);
405 out.
open(temp_file_name, std::ofstream::out | std::ofstream::binary);
409 uri = temp_file_name;
413 const auto &aiscene = dart::dynamics::MeshShape::loadMesh(file);
415 auto mesh = std::make_shared<dart::dynamics::MeshShape>(dimensions, aiscene);
427 if (inner_radius > outer_radius)
433 const double interval = high - low;
434 const double step = interval / resolution;
436 auto *mesh =
new aiMesh;
437 mesh->mMaterialIndex = (
unsigned int)(-1);
446 mesh->mNumVertices = n_vert;
447 mesh->mVertices =
new aiVector3D[n_vert];
448 mesh->mNormals =
new aiVector3D[n_vert];
449 mesh->mColors[0] =
new aiColor4D[n_vert];
454 const aiVector3D fnormal(1.0f, 0.0f, 0.0f);
455 const aiVector3D bnormal(-1.0f, 0.0f, 0.0f);
456 const aiColor4D color(0.0f, 0.0f, 0.0f, 1.0f);
461 const double theta = low + step * i;
464 const double yi = inner_radius *
std::cos(theta);
465 const double zi = inner_radius *
std::sin(theta);
466 vertex.Set(vx, yi, zi);
470 mesh->mVertices[ifv] = vertex;
471 mesh->mNormals[ifv] = fnormal;
472 mesh->mColors[0][ifv] = color;
476 mesh->mVertices[ibv] = vertex;
477 mesh->mNormals[ibv] = bnormal;
478 mesh->mColors[0][ibv] = color;
481 const double yo = outer_radius *
std::cos(theta);
482 const double zo = outer_radius *
std::sin(theta);
483 vertex.Set(vx, yo, zo);
487 mesh->mVertices[ofv] = vertex;
488 mesh->mNormals[ofv] = fnormal;
489 mesh->mColors[0][ofv] = color;
493 mesh->mVertices[obv] = vertex;
494 mesh->mNormals[obv] = bnormal;
495 mesh->mColors[0][obv] = color;
498 mesh->mNumFaces = n_face;
499 mesh->mFaces =
new aiFace[n_face];
510 aiFace *fface = &mesh->mFaces[i - 1];
511 fface->mNumIndices = 4;
512 fface->mIndices =
new unsigned int[4];
513 fface->mIndices[0] = ifv;
514 fface->mIndices[1] = ifvp;
515 fface->mIndices[2] = ofvp;
516 fface->mIndices[3] = ofv;
524 aiFace *bface = &mesh->mFaces[i + resolution - 1];
525 bface->mNumIndices = 4;
526 bface->mIndices =
new unsigned int[4];
527 bface->mIndices[0] = obv;
528 bface->mIndices[1] = obvp;
529 bface->mIndices[2] = ibvp;
530 bface->mIndices[3] = ibv;
533 auto *node =
new aiNode;
534 node->mNumMeshes = 1;
535 node->mMeshes =
new unsigned int[1];
536 node->mMeshes[0] = 0;
538 auto *
scene =
new aiScene;
539 scene->mNumMeshes = 1;
540 scene->mMeshes =
new aiMesh *[1];
541 scene->mMeshes[0] = mesh;
542 scene->mRootNode = node;
544 auto shape = std::make_shared<dart::dynamics::MeshShape>(Eigen::Vector3d::Ones(),
scene);
545 shape->setColorMode(dart::dynamics::MeshShape::SHAPE_COLOR);
552 for (
const auto &shape : node->getShapeNodes())
553 shape->getVisualAspect()->setColor(color);
A shared pointer wrapper for robowflex::Geometry.
@ CYLINDER
Solid primitive cylinder. Uses two dimensions (height, radius).
@ MESH
Mesh. Dimensions scale along x, y, z.
@ BOX
Solid primitive box. Uses three dimensions (x, y, z).
@ SPHERE
Solid primitive sphere. Uses one dimension (radius).
A const shared pointer wrapper for robowflex::Scene.
A shared pointer wrapper for robowflex::darts::ACM.
Allowable collision matrix for robowflex::darts::Structure.
A shared pointer wrapper for robowflex::darts::Structure.
Wrapper class for a dart::dynamics::Skeleton.
void dumpGraphViz(std::ostream &out, bool standalone=true)
Dumps the structure of the skeleton to a GraphViz file.
StructurePtr cloneStructure(const std::string &newName) const
Clones this structure with a new name.
void setJoint(const std::string &name, double value)
Set the value of a 1-DoF joint in the structure.
void setJointParentTransform(const std::string &name, const RobotPose &tf)
Set the transform from a joint to its parent.
bool solveIK()
Solve the current whole-body IK problem imposed on the structure.
Structure(const std::string &name)
Create an empty structure.
ACMPtr getACM()
Get the ACM for the structure.
void updateCollisionObject(const std::string &name, const GeometryPtr &geometry, const robowflex::RobotPose &pose)
Update or add a collision object.
std::pair< dart::dynamics::PrismaticJoint *, dart::dynamics::BodyNode * > addPrismaticFrame(const dart::dynamics::PrismaticJoint::Properties &properties, const dart::dynamics::ShapePtr &shape, dart::dynamics::BodyNode *parent=nullptr)
Add a new frame attached to a prismatic joint to this structure.
const std::string & getName() const
Get the name of this structure.
std::vector< std::string > getJointNames() const
Get the joint names for this structure.
void createShapeNode(dart::dynamics::BodyNode *body, const dart::dynamics::ShapePtr &shape)
Create a shape node on a body.
std::pair< dart::dynamics::FreeJoint *, dart::dynamics::BodyNode * > addFreeFrame(const dart::dynamics::FreeJoint::Properties &properties, const dart::dynamics::ShapePtr &shape, dart::dynamics::BodyNode *parent=nullptr)
Add a new frame attached to a free joint to this structure.
ACMPtr acm_
ACM for structure.
void reparentFreeFrame(dart::dynamics::BodyNode *child, const std::string &parent="")
Reparents the child node to the parent node.
std::pair< dart::dynamics::WeldJoint *, dart::dynamics::BodyNode * > addWeldedFrame(const dart::dynamics::WeldJoint::Properties &properties, const dart::dynamics::ShapePtr &shape, dart::dynamics::BodyNode *parent=nullptr)
Add a new frame attached to a fixed joint to this structure.
const dart::dynamics::SkeletonPtr & getSkeletonConst() const
Get the underlying skeleton for the structure.
void setSkeleton(const dart::dynamics::SkeletonPtr &skeleton)
Set the skeleton for the structure.
dart::dynamics::SkeletonPtr skeleton_
Underlying skeleton.
const std::string name_
Name of the structure.
void addGround(double z=0., double radius=10.)
Add a ground plane.
dart::dynamics::SkeletonPtr & getSkeleton()
Get the underlying skeleton for the structure.
std::pair< dart::dynamics::RevoluteJoint *, dart::dynamics::BodyNode * > addRevoluteFrame(const dart::dynamics::RevoluteJoint::Properties &properties, const dart::dynamics::ShapePtr &shape, dart::dynamics::BodyNode *parent=nullptr)
Add a new frame attached to a revolute joint to this structure.
void setDof(unsigned int index, double value)
Set the DoF at index to value.
dart::dynamics::BodyNode * getRootFrame() const
Get the root frame of this structure.
dart::dynamics::BodyNode * getFrame(const std::string &name="") const
Get a body node within the structure.
const ACMPtr & getACMConst() const
Get the ACM for the structure.
dart::dynamics::Joint * getJoint(const std::string &joint_name) const
Get a reference to the joint in the structure.
T emplace_back(T... args)
#define RBX_ERROR(fmt,...)
Output a error logging message.
std::string resolvePackage(const std::string &path)
Resolves package:// URLs to their canonical form. The path does not need to exist,...
RobotPose identity()
Creates the Identity pose.
static const double DEFAULT_RESTITUTION
static const double DEFAULT_DAMPING
static const double DEFAULT_DENSITY
DART-based robot modeling and planning.
std::shared_ptr< dart::dynamics::CylinderShape > makeCylinder(double radius, double height)
Create a cylinder.
void setColor(dart::dynamics::BodyNode *node, const Eigen::Vector4d &color)
Sets the color of the shapes on a body node.
std::shared_ptr< dart::dynamics::MeshShape > makeArcsegment(double low, double high, double inner_radius, double outer_radius, std::size_t resolution=32)
Create a circle's arcsector from one angle to another, with a specified radius.
std::shared_ptr< dart::dynamics::SphereShape > makeSphere(double radius)
Create a sphere.
dart::dynamics::ShapePtr makeGeometry(const GeometryPtr &geometry)
Convert a robowflex::Geometry to a Dart Shape.
std::shared_ptr< dart::dynamics::MeshShape > makeMesh(const GeometryPtr &geometry)
Create a mesh from a robowflex::Geometry that contains a mesh.
std::shared_ptr< dart::dynamics::BoxShape > makeBox(const Eigen::Ref< const Eigen::Vector3d > &v)
Create a box.
std::decay< decltype(std::declval< moveit::core::Transforms >().getTransform("")) >::type RobotPose
A pose (point in SE(3)) used in various functions. Defined from what MoveIt! uses.
Functions for loading and animating scenes in Blender.