33 #ifndef DART_EXAMPLE_OSG_OSGATLASSIMBICON_TINKERTOYWORLDNODE_HPP_ 34 #define DART_EXAMPLE_OSG_OSGATLASSIMBICON_TINKERTOYWORLDNODE_HPP_ 36 #include <dart/dart.hpp> 38 const double DefaultBlockLength = 0.5;
39 const double DefaultBlockWidth = 0.075;
40 const double DefaultJointRadius = 1.5 * DefaultBlockWidth / 2.0;
41 const double BalsaWoodDensity = 0.16 * 10e3;
42 const double DefaultBlockMass
43 = BalsaWoodDensity * DefaultBlockLength * pow(DefaultBlockWidth, 2);
44 const double DefaultDamping = 0.4;
46 const Eigen::Vector4d DefaultSimulationColor
47 = Eigen::Vector4d(0.5, 0.5, 1.0, 1.0);
48 const Eigen::Vector4d DefaultPausedColor
49 = Eigen::Vector4d(0xEE, 0xC9, 0x00, 0.0) / 255.0
50 + Eigen::Vector4d(0, 0, 0, 1.0);
51 const Eigen::Vector4d DefaultSelectedColor = dart::Color::Red(1.0);
52 const Eigen::Vector4d DefaultForceBodyColor = dart::Color::Fuchsia(1.0);
53 const Eigen::Vector4d DefaultForceLineColor
54 = Eigen::Vector4d(1.0, 0.63, 0.0, 1.0);
56 const double MaxForce = 200.0;
57 const double DefaultForceCoeff = 100.0;
58 const double MaxForceCoeff = 1000.0;
59 const double MinForceCoeff = 10.0;
60 const double ForceIncrement = 10.0;
68 mForceCoeff(DefaultForceCoeff),
71 mTarget = dart::gui::osg::InteractiveFrame::createShared(
72 dart::dynamics::Frame::World());
81 void setAllBodyColors(
const Eigen::Vector4d& color)
83 for (
size_t i = 0; i <
getWorld()->getNumSkeletons(); ++i)
85 const dart::dynamics::SkeletonPtr& skel =
getWorld()->getSkeleton(i);
86 for (
size_t j = 0; j < skel->getNumBodyNodes(); ++j)
89 for (
size_t k = 0; k < bn->getNumShapeNodes(); ++k)
90 bn->getShapeNode(k)->getVisualAspect()->setColor(color);
95 void setPickedNodeColor(
const Eigen::Vector4d& color)
100 for (
size_t i = 0; i < mPickedNode->getNumShapeNodes(); ++i)
101 mPickedNode->getShapeNode(i)->getVisualAspect()->setColor(color);
104 void resetForceLine()
109 mForceLine->setVertex(1, mTarget->getWorldTransform().translation());
113 mForceLine->setVertex(0, Eigen::Vector3d::Zero());
114 mForceLine->setVertex(1, Eigen::Vector3d::Zero());
122 setAllBodyColors(DefaultSimulationColor);
123 setPickedNodeColor(DefaultForceBodyColor);
127 setAllBodyColors(DefaultPausedColor);
128 setPickedNodeColor(DefaultSelectedColor);
138 Eigen::Vector3d F = mForceCoeff
139 * (mTarget->getWorldTransform().translation()
142 const double F_norm = F.norm();
143 if (F_norm > MaxForce)
144 F = MaxForce * F / F_norm;
163 = pick.position + pick.normal.normalized() * DefaultBlockWidth / 2.0;
165 mTarget->setTransform(tf);
170 mPickedNode =
nullptr;
171 mTarget->setTransform(Eigen::Isometry3d::Identity());
181 std::cout <<
" -- Please pause simulation [using the Spacebar] before " 182 <<
"attempting to delete blocks." << std::endl;
186 dart::dynamics::SkeletonPtr temporary = mPickedNode->
remove();
187 for (
size_t i = 0; i < temporary->getNumBodyNodes(); ++i)
193 getWorld()->getConstraintSolver()->getCollisionGroup()->removeShapeFramesOf(
201 createWeldJointShape();
202 createRevoluteJointShape();
203 createBallJointShape();
207 void createWeldJointShape()
210 = std::make_shared<dart::dynamics::BoxShape>(Eigen::Vector3d(
211 2.0 * DefaultJointRadius, DefaultBlockWidth, DefaultBlockWidth));
216 void createRevoluteJointShape()
218 mRevoluteJointShape = std::make_shared<dart::dynamics::CylinderShape>(
219 DefaultJointRadius, 1.5 * DefaultBlockWidth);
224 void createBallJointShape()
227 = std::make_shared<dart::dynamics::SphereShape>(DefaultJointRadius);
232 void createBlockShape()
234 mBlockShape = std::make_shared<dart::dynamics::BoxShape>(Eigen::Vector3d(
235 DefaultBlockLength, DefaultBlockWidth, DefaultBlockWidth));
239 mBlockOffset = Eigen::Isometry3d::Identity();
240 mBlockOffset.translation()[0] = DefaultBlockLength / 2.0;
243 template <
class Jo
intType>
244 std::pair<JointType*, dart::dynamics::BodyNode*> addBlock(
246 const Eigen::Isometry3d& relTf,
247 const dart::dynamics::ShapePtr& jointShape)
251 std::cout <<
" -- Please pause simulation [using the Spacebar] before " 252 <<
"attempting to add new bodies" << std::endl;
253 return std::make_pair(
nullptr,
nullptr);
256 dart::dynamics::SkeletonPtr skel;
264 "toy_#" + std::to_string(
getWorld()->getNumSkeletons() + 1));
268 auto pair = skel->createJointAndBodyNodePair<JointType>(parent);
269 JointType* joint = pair.first;
271 bn->
setName(
"block_#" + std::to_string(skel->getNumBodyNodes()));
272 joint->setName(
"joint_#" + std::to_string(skel->getNumJoints()));
274 joint->setTransformFromParentBodyNode(relTf);
275 for (
size_t i = 0; i < joint->getNumDofs(); ++i)
276 joint->getDof(i)->setDampingCoefficient(DefaultDamping);
285 dart::dynamics::CollisionAspect,
287 block->setRelativeTransform(mBlockOffset);
290 inertia.
setMass(DefaultBlockMass);
291 inertia.
setMoment(mBlockShape->computeInertia(DefaultBlockMass));
292 inertia.
setLocalCOM(DefaultBlockLength / 2.0 * Eigen::Vector3d::UnitX());
295 getWorld()->getConstraintSolver()->getCollisionGroup()->addShapeFramesOf(
300 return std::make_pair(joint, bn);
303 Eigen::Isometry3d getRelTf()
const 305 return mPickedNode ? mTarget->getTransform(mPickedNode)
306 : mTarget->getWorldTransform();
309 void addWeldJointBlock()
311 addWeldJointBlock(mPickedNode, getRelTf());
317 return addBlock<dart::dynamics::WeldJoint>(parent, relTf, mWeldJointShape)
321 void addRevoluteJointBlock()
323 addRevoluteJointBlock(mPickedNode, getRelTf());
329 auto pair = addBlock<dart::dynamics::RevoluteJoint>(
330 parent, relTf, mRevoluteJointShape);
333 pair.first->setAxis(Eigen::Vector3d::UnitZ());
338 void addBallJointBlock()
340 addBallJointBlock(mPickedNode, getRelTf());
346 return addBlock<dart::dynamics::BallJoint>(parent, relTf, mBallJointShape)
350 void createInitialToy1()
352 Eigen::Isometry3d tf(Eigen::Isometry3d::Identity());
353 tf.rotate(Eigen::AngleAxisd(
354 dart::math::toRadian(45.0), Eigen::Vector3d::UnitY()));
357 tf = Eigen::Isometry3d::Identity();
358 tf.translation()[0] = DefaultBlockLength;
359 tf.linear() = Eigen::Matrix3d::Identity();
360 tf.prerotate(Eigen::AngleAxisd(
361 dart::math::toRadian(90.0), Eigen::Vector3d::UnitX()));
362 bn = addRevoluteJointBlock(bn, tf);
364 tf = Eigen::Isometry3d::Identity();
365 tf.rotate(Eigen::AngleAxisd(
366 dart::math::toRadian(90.0), Eigen::Vector3d::UnitZ()));
367 bn = addWeldJointBlock(bn, tf);
369 tf = Eigen::Isometry3d::Identity();
370 tf.translation()[0] = DefaultBlockLength / 2.0;
371 tf.translation()[2] = DefaultBlockWidth;
372 tf.rotate(Eigen::AngleAxisd(
373 dart::math::toRadian(-30.0), Eigen::Vector3d::UnitZ()));
374 bn = addBallJointBlock(bn, tf);
377 void createInitialToy2()
379 Eigen::Isometry3d tf(Eigen::Isometry3d::Identity());
380 tf.rotate(Eigen::AngleAxisd(
381 dart::math::toRadian(90.0), Eigen::Vector3d::UnitY()));
382 tf.pretranslate(-1.0 * Eigen::Vector3d::UnitX());
385 tf = Eigen::Isometry3d::Identity();
386 tf.translation()[0] = DefaultBlockLength;
387 tf.translation()[2] = DefaultBlockLength / 2.0;
388 tf.rotate(Eigen::AngleAxisd(
389 dart::math::toRadian(90.0), Eigen::Vector3d::UnitY()));
390 bn = addWeldJointBlock(bn, tf);
392 tf = Eigen::Isometry3d::Identity();
393 tf.rotate(Eigen::AngleAxisd(
394 dart::math::toRadian(-90.0), Eigen::Vector3d::UnitX()));
395 tf.rotate(Eigen::AngleAxisd(
396 dart::math::toRadian(-90.0), Eigen::Vector3d::UnitZ()));
397 tf.translation()[2] = DefaultBlockWidth / 2.0;
398 addRevoluteJointBlock(bn, tf);
400 tf.translation()[0] = DefaultBlockLength;
401 bn = addRevoluteJointBlock(bn, tf);
403 tf = Eigen::Isometry3d::Identity();
404 tf.translation()[0] = DefaultBlockLength;
405 addBallJointBlock(bn, tf);
408 void createForceLine()
410 dart::dynamics::SimpleFramePtr lineFrame
411 = std::make_shared<dart::dynamics::SimpleFrame>(
412 dart::dynamics::Frame::World());
414 mForceLine = std::make_shared<dart::dynamics::LineSegmentShape>(
415 Eigen::Vector3d::Zero(), Eigen::Vector3d::Zero(), 3.0);
418 lineFrame->setShape(mForceLine);
419 lineFrame->createVisualAspect();
420 lineFrame->getVisualAspect()->setColor(DefaultForceLineColor);
422 getWorld()->addSimpleFrame(lineFrame);
425 void setForceCoeff(
double coeff)
429 if (mForceCoeff > MaxForceCoeff)
430 mForceCoeff = MaxForceCoeff;
431 else if (mForceCoeff < MinForceCoeff)
432 mForceCoeff = MinForceCoeff;
435 double getForceCoeff()
const 440 void incrementForceCoeff()
442 mForceCoeff += ForceIncrement;
443 if (mForceCoeff > MaxForceCoeff)
444 mForceCoeff = MaxForceCoeff;
446 std::cout <<
"[Force Coefficient: " << mForceCoeff <<
"]" << std::endl;
449 void decrementForceCoeff()
451 mForceCoeff -= ForceIncrement;
452 if (mForceCoeff < MinForceCoeff)
453 mForceCoeff = MinForceCoeff;
455 std::cout <<
"[Force Coefficient: " << mForceCoeff <<
"]" << std::endl;
458 void reorientTarget()
460 Eigen::Isometry3d tf = mTarget->getWorldTransform();
461 tf.linear() = Eigen::Matrix3d::Identity();
462 mTarget->setTransform(tf);
473 dart::dynamics::ShapePtr mWeldJointShape;
474 dart::dynamics::ShapePtr mRevoluteJointShape;
475 dart::dynamics::ShapePtr mBallJointShape;
476 dart::dynamics::ShapePtr mBlockShape;
477 Eigen::Isometry3d mBlockOffset;
480 Eigen::Vector3d mPickedPoint;
481 dart::gui::osg::InteractiveFramePtr mTarget;
483 dart::dynamics::LineSegmentShapePtr mForceLine;
488 #endif // DART_EXAMPLE_OSG_OSGATLASSIMBICON_TINKERTOYWORLDNODE_HPP_ static SkeletonPtr create(const std::string &_name="Skeleton")
Create a new Skeleton inside of a shared_ptr.
Definition: Skeleton.cpp:384
Definition: ShapeFrame.hpp:131
void customPreStep() override
If update() is not overloaded, this function will be called at the beginning of each simulation step...
Definition: TinkertoyWorldNode.hpp:134
SkeletonPtr remove(const std::string &_name="temporary")
Remove this BodyNode and all of its children (recursively) from their Skeleton.
Definition: BodyNode.cpp:782
ShapeNode * createShapeNodeWith(const ShapePtr &shape)
Create a ShapeNode with the specified Aspects and an automatically assigned name: <BodyNodeName>_Shap...
Definition: BodyNode.hpp:185
Viewer * mViewer
Viewer that this WorldNode is inside of.
Definition: WorldNode.hpp:194
void setLocalCOM(const Eigen::Vector3d &_com)
Set the center of mass with respect to the Body-fixed frame.
Definition: Inertia.cpp:132
std::shared_ptr< dart::simulation::World > getWorld() const
Get the World that this WorldNode is associated with.
Definition: WorldNode.cpp:106
void setMoment(const Eigen::Matrix3d &_moment)
Set the moment of inertia (about the center of mass).
Definition: Inertia.cpp:145
void setMass(double _mass)
Set the mass.
Definition: Inertia.cpp:119
DragAndDrop * enableDragAndDrop(dart::dynamics::Entity *_entity)
Returns a nullptr if _entity is not a type that can support the built-in drag and drop features...
Definition: Viewer.cpp:587
const std::string & setName(const std::string &_name) override
Set name.
Definition: BodyNode.cpp:422
Definition: Inertia.hpp:43
Definition: DefaultEventHandler.hpp:59
Frame * getParentFrame()
Get the parent (reference) frame of this Entity.
Definition: Entity.cpp:81
The primitive properties (such as x/y/z scaling) of the shape might change.
Definition: Shape.hpp:88
bool isSimulating() const
Returns true iff the WorldNode is stepping between render cycles.
Definition: WorldNode.cpp:161
void setInertia(const Inertia &inertia)
Set the inertia data for this BodyNode.
Definition: BodyNode.cpp:566
bool disableDragAndDrop(DragAndDrop *_dnd)
Delete a DragAndDrop object.
Definition: Viewer.cpp:705
Definition: ShapeNode.hpp:48
void setupViewer() override
Called when this world gets added to an dart::gui::osg::Viewer.
Definition: TinkertoyWorldNode.hpp:468
void customPreRefresh() override
If update() is not overloaded, this function will be called at the beginning of each rendering cycle...
Definition: TinkertoyWorldNode.hpp:118
void addExtForce(const Eigen::Vector3d &_force, const Eigen::Vector3d &_offset=Eigen::Vector3d::Zero(), bool _isForceLocal=false, bool _isOffsetLocal=true)
Add applying linear Cartesian forces to this node.
Definition: BodyNode.cpp:1213
BodyNode class represents a single node of the skeleton.
Definition: BodyNode.hpp:74
const Inertia & getInertia() const
Get the inertia data for this BodyNode.
Definition: BodyNode.cpp:584
The coloring or textures of the shape might change.
Definition: Shape.hpp:90
Definition: TinkertoyWorldNode.hpp:63
Definition: ShapeFrame.hpp:113
SkeletonPtr getSkeleton() override
Return the Skeleton that this Node is attached to.
Definition: BodyNode.cpp:861
Definition: ShapeFrame.hpp:50
Definition: RealTimeWorldNode.hpp:44
const Eigen::Isometry3d & getWorldTransform() const
Get the transform of this Frame with respect to the World Frame.
Definition: Frame.cpp:93