2015-04-16 16:55:32 +00:00
# include <stdio.h>
# include "LinearMath/btTransform.h"
# include "BulletDynamics/Featherstone/btMultiBodyDynamicsWorld.h"
# include "BulletCollision/CollisionShapes/btCompoundShape.h"
# include "BulletDynamics/Dynamics/btRigidBody.h"
# include "BulletDynamics/Featherstone/btMultiBodyLinkCollider.h"
2015-06-04 01:02:15 +00:00
# include "BulletDynamics/Featherstone/btMultiBodyJointLimitConstraint.h"
2015-04-16 16:55:32 +00:00
# include "BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h"
2016-08-29 22:09:18 +00:00
# include "URDF2Bullet.h"
2015-04-22 23:35:27 +00:00
# include "URDFImporterInterface.h"
# include "MultiBodyCreationInterface.h"
# include <string>
2015-07-04 01:54:41 +00:00
2015-04-16 16:55:32 +00:00
static int bodyCollisionFilterGroup = btBroadphaseProxy : : CharacterFilter ;
static int bodyCollisionFilterMask = btBroadphaseProxy : : AllFilter & ( ~ btBroadphaseProxy : : CharacterFilter ) ;
static bool enableConstraints = true ;
static btVector4 colors [ 4 ] =
{
btVector4 ( 1 , 0 , 0 , 1 ) ,
btVector4 ( 0 , 1 , 0 , 1 ) ,
btVector4 ( 0 , 1 , 1 , 1 ) ,
btVector4 ( 1 , 1 , 0 , 1 ) ,
} ;
2015-11-07 01:11:15 +00:00
static btVector4 selectColor2 ( )
2015-04-16 16:55:32 +00:00
{
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
static int curColor = 0 ;
btVector4 color = colors [ curColor ] ;
curColor + + ;
curColor & = 3 ;
return color ;
}
2015-04-22 23:35:27 +00:00
2015-04-16 16:55:32 +00:00
struct URDF2BulletCachedData
{
URDF2BulletCachedData ( )
2015-07-04 01:17:14 +00:00
:
m_currentMultiBodyLinkIndex ( - 1 ) ,
m_bulletMultiBody ( 0 ) ,
m_totalNumJoints1 ( 0 )
2015-04-16 16:55:32 +00:00
{
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
}
//these arrays will be initialized in the 'InitURDF2BulletCache'
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
btAlignedObjectArray < int > m_urdfLinkParentIndices ;
btAlignedObjectArray < int > m_urdfLinkIndices2BulletLinkIndices ;
btAlignedObjectArray < class btRigidBody * > m_urdfLink2rigidBodies ;
btAlignedObjectArray < btTransform > m_urdfLinkLocalInertialFrames ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
int m_currentMultiBodyLinkIndex ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
class btMultiBody * m_bulletMultiBody ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
//this will be initialized in the constructor
int m_totalNumJoints1 ;
int getParentUrdfIndex ( int linkIndex ) const
{
return m_urdfLinkParentIndices [ linkIndex ] ;
}
int getMbIndexFromUrdfIndex ( int urdfIndex ) const
{
if ( urdfIndex = = - 2 )
return - 2 ;
return m_urdfLinkIndices2BulletLinkIndices [ urdfIndex ] ;
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
void registerMultiBody ( int urdfLinkIndex , class btMultiBody * body , const btTransform & worldTransform , btScalar mass , const btVector3 & localInertiaDiagonal , const class btCompoundShape * compound , const btTransform & localInertialFrame )
{
m_urdfLinkLocalInertialFrames [ urdfLinkIndex ] = localInertialFrame ;
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
class btRigidBody * getRigidBodyFromLink ( int urdfLinkIndex )
{
return m_urdfLink2rigidBodies [ urdfLinkIndex ] ;
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
void registerRigidBody ( int urdfLinkIndex , class btRigidBody * body , const btTransform & worldTransform , btScalar mass , const btVector3 & localInertiaDiagonal , const class btCompoundShape * compound , const btTransform & localInertialFrame )
{
btAssert ( m_urdfLink2rigidBodies [ urdfLinkIndex ] = = 0 ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
m_urdfLink2rigidBodies [ urdfLinkIndex ] = body ;
m_urdfLinkLocalInertialFrames [ urdfLinkIndex ] = localInertialFrame ;
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
} ;
2015-04-22 23:35:27 +00:00
void ComputeTotalNumberOfJoints ( const URDFImporterInterface & u2b , URDF2BulletCachedData & cache , int linkIndex )
2015-04-16 16:55:32 +00:00
{
btAlignedObjectArray < int > childIndices ;
u2b . getLinkChildIndices ( linkIndex , childIndices ) ;
2015-07-04 01:54:41 +00:00
//b3Printf("link %s has %d children\n", u2b.getLinkName(linkIndex).c_str(),childIndices.size());
//for (int i=0;i<childIndices.size();i++)
//{
// b3Printf("child %d has childIndex%d=%s\n",i,childIndices[i],u2b.getLinkName(childIndices[i]).c_str());
//}
2015-04-16 16:55:32 +00:00
cache . m_totalNumJoints1 + = childIndices . size ( ) ;
for ( int i = 0 ; i < childIndices . size ( ) ; i + + )
{
int childIndex = childIndices [ i ] ;
ComputeTotalNumberOfJoints ( u2b , cache , childIndex ) ;
}
}
2015-04-22 23:35:27 +00:00
void ComputeParentIndices ( const URDFImporterInterface & u2b , URDF2BulletCachedData & cache , int urdfLinkIndex , int urdfParentIndex )
2015-04-16 16:55:32 +00:00
{
cache . m_urdfLinkParentIndices [ urdfLinkIndex ] = urdfParentIndex ;
cache . m_urdfLinkIndices2BulletLinkIndices [ urdfLinkIndex ] = cache . m_currentMultiBodyLinkIndex + + ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
btAlignedObjectArray < int > childIndices ;
u2b . getLinkChildIndices ( urdfLinkIndex , childIndices ) ;
for ( int i = 0 ; i < childIndices . size ( ) ; i + + )
{
ComputeParentIndices ( u2b , cache , childIndices [ i ] , urdfLinkIndex ) ;
}
}
2015-04-22 23:35:27 +00:00
void InitURDF2BulletCache ( const URDFImporterInterface & u2b , URDF2BulletCachedData & cache )
2015-04-16 16:55:32 +00:00
{
//compute the number of links, and compute parent indices array (and possibly other cached data?)
cache . m_totalNumJoints1 = 0 ;
int rootLinkIndex = u2b . getRootLinkIndex ( ) ;
if ( rootLinkIndex > = 0 )
{
ComputeTotalNumberOfJoints ( u2b , cache , rootLinkIndex ) ;
int numTotalLinksIncludingBase = 1 + cache . m_totalNumJoints1 ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
cache . m_urdfLinkParentIndices . resize ( numTotalLinksIncludingBase ) ;
cache . m_urdfLinkIndices2BulletLinkIndices . resize ( numTotalLinksIncludingBase ) ;
cache . m_urdfLink2rigidBodies . resize ( numTotalLinksIncludingBase ) ;
cache . m_urdfLinkLocalInertialFrames . resize ( numTotalLinksIncludingBase ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
cache . m_currentMultiBodyLinkIndex = - 1 ; //multi body base has 'link' index -1
ComputeParentIndices ( u2b , cache , rootLinkIndex , - 2 ) ;
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
}
2016-08-29 22:09:18 +00:00
void ConvertURDF2BulletInternal (
const URDFImporterInterface & u2b , MultiBodyCreationInterface & creation ,
URDF2BulletCachedData & cache , int urdfLinkIndex ,
const btTransform & parentTransformInWorldSpace , btMultiBodyDynamicsWorld * world1 ,
bool createMultiBody , const char * pathPrefix ,
int flags = 0 )
2015-04-16 16:55:32 +00:00
{
2015-07-27 20:28:47 +00:00
//b3Printf("start converting/extracting data from URDF interface\n");
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
btTransform linkTransformInWorldSpace ;
linkTransformInWorldSpace . setIdentity ( ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
int mbLinkIndex = cache . getMbIndexFromUrdfIndex ( urdfLinkIndex ) ;
int urdfParentIndex = cache . getParentUrdfIndex ( urdfLinkIndex ) ;
int mbParentIndex = cache . getMbIndexFromUrdfIndex ( urdfParentIndex ) ;
btRigidBody * parentRigidBody = 0 ;
2015-06-05 18:46:53 +00:00
2015-07-27 20:28:47 +00:00
//b3Printf("mb link index = %d\n",mbLinkIndex);
2015-04-16 16:55:32 +00:00
btTransform parentLocalInertialFrame ;
parentLocalInertialFrame . setIdentity ( ) ;
btScalar parentMass ( 1 ) ;
btVector3 parentLocalInertiaDiagonal ( 1 , 1 , 1 ) ;
if ( urdfParentIndex = = - 2 )
{
2015-07-27 20:28:47 +00:00
//b3Printf("root link has no parent\n");
2015-04-16 16:55:32 +00:00
} else
{
2015-07-27 20:28:47 +00:00
//b3Printf("urdf parent index = %d\n",urdfParentIndex);
//b3Printf("mb parent index = %d\n",mbParentIndex);
2015-04-16 16:55:32 +00:00
parentRigidBody = cache . getRigidBodyFromLink ( urdfParentIndex ) ;
u2b . getMassAndInertia ( urdfParentIndex , parentMass , parentLocalInertiaDiagonal , parentLocalInertialFrame ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
btScalar mass = 0 ;
btTransform localInertialFrame ;
localInertialFrame . setIdentity ( ) ;
btVector3 localInertiaDiagonal ( 0 , 0 , 0 ) ;
u2b . getMassAndInertia ( urdfLinkIndex , mass , localInertiaDiagonal , localInertialFrame ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
btTransform parent2joint ;
parent2joint . setIdentity ( ) ;
int jointType ;
btVector3 jointAxisInJointSpace ;
btScalar jointLowerLimit ;
btScalar jointUpperLimit ;
2016-03-17 21:54:46 +00:00
btScalar jointDamping ;
btScalar jointFriction ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
2016-05-11 22:52:50 +00:00
bool hasParentJoint = u2b . getJointInfo ( urdfLinkIndex , parent2joint , linkTransformInWorldSpace , jointAxisInJointSpace , jointType , jointLowerLimit , jointUpperLimit , jointDamping , jointFriction ) ;
2016-08-29 22:09:18 +00:00
if ( flags & CUF_USE_SDF )
2016-05-11 22:52:50 +00:00
{
parent2joint = parentTransformInWorldSpace . inverse ( ) * linkTransformInWorldSpace ;
}
else
{
linkTransformInWorldSpace = parentTransformInWorldSpace * parent2joint ;
}
2016-05-11 20:43:50 +00:00
2016-05-20 01:37:15 +00:00
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
btCompoundShape * compoundShape = u2b . convertLinkCollisionShapes ( urdfLinkIndex , pathPrefix , localInertialFrame ) ;
2016-05-20 01:37:15 +00:00
2016-06-01 05:55:13 +00:00
int graphicsIndex = u2b . convertLinkVisualShapes ( urdfLinkIndex , pathPrefix , localInertialFrame ) ;
2016-05-20 01:37:15 +00:00
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
if ( compoundShape )
{
2015-06-05 18:46:53 +00:00
2016-09-24 18:25:05 +00:00
btVector4 color = selectColor2 ( ) ;
u2b . getLinkColor ( urdfLinkIndex , color ) ;
2015-06-05 18:46:53 +00:00
/*
2015-04-16 16:55:32 +00:00
if ( visual - > material . get ( ) )
{
color . setValue ( visual - > material - > color . r , visual - > material - > color . g , visual - > material - > color . b ) ; //,visual->material->color.a);
}
*/
2016-04-19 23:52:47 +00:00
if ( mass )
{
2016-08-29 22:09:18 +00:00
if ( ! ( flags & CUF_USE_URDF_INERTIA ) )
{
compoundShape - > calculateLocalInertia ( mass , localInertiaDiagonal ) ;
}
2016-07-25 18:48:44 +00:00
URDFLinkContactInfo contactInfo ;
u2b . getLinkContactInfo ( urdfLinkIndex , contactInfo ) ;
//temporary inertia scaling until we load inertia from URDF
if ( contactInfo . m_flags & URDF_CONTACT_HAS_INERTIA_SCALING )
{
localInertiaDiagonal * = contactInfo . m_inertiaScaling ;
}
2016-04-19 23:52:47 +00:00
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
btRigidBody * linkRigidBody = 0 ;
btTransform inertialFrameInWorldSpace = linkTransformInWorldSpace * localInertialFrame ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
if ( ! createMultiBody )
{
2015-04-22 23:35:27 +00:00
btRigidBody * body = creation . allocateRigidBody ( urdfLinkIndex , mass , localInertiaDiagonal , inertialFrameInWorldSpace , compoundShape ) ;
2015-04-16 16:55:32 +00:00
linkRigidBody = body ;
2015-06-05 18:46:53 +00:00
2016-09-24 18:25:05 +00:00
world1 - > addRigidBody ( body ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
compoundShape - > setUserIndex ( graphicsIndex ) ;
2015-06-05 18:46:53 +00:00
2015-04-22 23:35:27 +00:00
creation . createRigidBodyGraphicsInstance ( urdfLinkIndex , body , color , graphicsIndex ) ;
2015-04-16 16:55:32 +00:00
cache . registerRigidBody ( urdfLinkIndex , body , inertialFrameInWorldSpace , mass , localInertiaDiagonal , compoundShape , localInertialFrame ) ;
2016-06-01 05:55:13 +00:00
//untested: u2b.convertLinkVisualShapes2(urdfLinkIndex,pathPrefix,localInertialFrame,body);
2015-04-16 16:55:32 +00:00
} else
{
if ( cache . m_bulletMultiBody = = 0 )
{
2015-11-06 05:17:46 +00:00
2015-04-16 16:55:32 +00:00
bool canSleep = false ;
bool isFixedBase = ( mass = = 0 ) ; //todo: figure out when base is fixed
int totalNumJoints = cache . m_totalNumJoints1 ;
2015-11-06 05:17:46 +00:00
cache . m_bulletMultiBody = creation . allocateMultiBody ( urdfLinkIndex , totalNumJoints , mass , localInertiaDiagonal , isFixedBase , canSleep ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
cache . registerMultiBody ( urdfLinkIndex , cache . m_bulletMultiBody , inertialFrameInWorldSpace , mass , localInertiaDiagonal , compoundShape , localInertialFrame ) ;
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
//create a joint if necessary
if ( hasParentJoint ) {
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
btTransform offsetInA , offsetInB ;
offsetInA = parentLocalInertialFrame . inverse ( ) * parent2joint ;
offsetInB = localInertialFrame . inverse ( ) ;
2016-09-02 01:12:14 +00:00
btQuaternion parentRotToThis = offsetInB . getRotation ( ) * offsetInA . inverse ( ) . getRotation ( ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
bool disableParentCollision = true ;
switch ( jointType )
{
2015-04-22 23:35:27 +00:00
case URDFFixedJoint :
2015-04-16 16:55:32 +00:00
{
if ( createMultiBody )
{
//todo: adjust the center of mass transform and pivot axis properly
cache . m_bulletMultiBody - > setupFixed ( mbLinkIndex , mass , localInertiaDiagonal , mbParentIndex ,
2016-09-02 01:12:14 +00:00
parentRotToThis , offsetInA . getOrigin ( ) , - offsetInB . getOrigin ( ) ) ;
2015-04-22 23:35:27 +00:00
creation . addLinkMapping ( urdfLinkIndex , mbLinkIndex ) ;
2015-04-16 16:55:32 +00:00
} else
{
2015-07-27 20:28:47 +00:00
//b3Printf("Fixed joint\n");
2015-06-25 06:19:00 +00:00
2016-08-18 16:44:33 +00:00
btGeneric6DofSpring2Constraint * dof6 = creation . createFixedJoint ( urdfLinkIndex , * linkRigidBody , * parentRigidBody , offsetInB , offsetInA ) ;
2015-06-25 06:19:00 +00:00
2015-04-16 16:55:32 +00:00
if ( enableConstraints )
world1 - > addConstraint ( dof6 , true ) ;
}
break ;
}
2015-04-22 23:35:27 +00:00
case URDFContinuousJoint :
case URDFRevoluteJoint :
2015-04-16 16:55:32 +00:00
{
if ( createMultiBody )
{
cache . m_bulletMultiBody - > setupRevolute ( mbLinkIndex , mass , localInertiaDiagonal , mbParentIndex ,
2016-09-02 01:12:14 +00:00
parentRotToThis , quatRotate ( offsetInB . getRotation ( ) , jointAxisInJointSpace ) , offsetInA . getOrigin ( ) , //parent2joint.getOrigin(),
2015-04-16 16:55:32 +00:00
- offsetInB . getOrigin ( ) ,
disableParentCollision ) ;
2016-09-02 01:12:14 +00:00
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_jointDamping = jointDamping ;
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_jointFriction = jointFriction ;
2015-04-22 23:35:27 +00:00
creation . addLinkMapping ( urdfLinkIndex , mbLinkIndex ) ;
2016-09-23 18:45:46 +00:00
if ( jointType = = URDFRevoluteJoint & & jointLowerLimit < = jointUpperLimit ) {
//std::string name = u2b.getLinkName(urdfLinkIndex);
//printf("create btMultiBodyJointLimitConstraint for revolute link name=%s urdf link index=%d (low=%f, up=%f)\n", name.c_str(), urdfLinkIndex, jointLowerLimit, jointUpperLimit);
btMultiBodyConstraint * con = new btMultiBodyJointLimitConstraint ( cache . m_bulletMultiBody , mbLinkIndex , jointLowerLimit , jointUpperLimit ) ;
world1 - > addMultiBodyConstraint ( con ) ;
}
2015-04-16 16:55:32 +00:00
} else
{
2015-06-25 06:19:00 +00:00
2016-08-18 16:44:33 +00:00
btGeneric6DofSpring2Constraint * dof6 = creation . createRevoluteJoint ( urdfLinkIndex , * linkRigidBody , * parentRigidBody , offsetInB , offsetInA , jointAxisInJointSpace , jointLowerLimit , jointUpperLimit ) ;
2015-06-25 06:19:00 +00:00
if ( enableConstraints )
2015-04-16 16:55:32 +00:00
world1 - > addConstraint ( dof6 , true ) ;
2015-07-27 20:28:47 +00:00
//b3Printf("Revolute/Continuous joint\n");
2015-04-16 16:55:32 +00:00
}
break ;
}
2015-04-22 23:35:27 +00:00
case URDFPrismaticJoint :
2015-04-16 16:55:32 +00:00
{
if ( createMultiBody )
{
cache . m_bulletMultiBody - > setupPrismatic ( mbLinkIndex , mass , localInertiaDiagonal , mbParentIndex ,
2016-09-02 01:12:14 +00:00
parentRotToThis , quatRotate ( offsetInB . getRotation ( ) , jointAxisInJointSpace ) , offsetInA . getOrigin ( ) , //parent2joint.getOrigin(),
2015-04-16 16:55:32 +00:00
- offsetInB . getOrigin ( ) ,
disableParentCollision ) ;
2015-04-22 23:35:27 +00:00
creation . addLinkMapping ( urdfLinkIndex , mbLinkIndex ) ;
2016-09-19 14:02:43 +00:00
if ( jointLowerLimit < = jointUpperLimit )
{
//std::string name = u2b.getLinkName(urdfLinkIndex);
//printf("create btMultiBodyJointLimitConstraint for prismatic link name=%s urdf link index=%d (low=%f, up=%f)\n", name.c_str(), urdfLinkIndex, jointLowerLimit,jointUpperLimit);
btMultiBodyConstraint * con = new btMultiBodyJointLimitConstraint ( cache . m_bulletMultiBody , mbLinkIndex , jointLowerLimit , jointUpperLimit ) ;
world1 - > addMultiBodyConstraint ( con ) ;
}
2015-06-05 18:46:53 +00:00
//printf("joint lower limit=%d, upper limit = %f\n", jointLowerLimit, jointUpperLimit);
2015-04-16 16:55:32 +00:00
} else
{
2015-06-25 06:19:00 +00:00
2016-08-18 16:44:33 +00:00
btGeneric6DofSpring2Constraint * dof6 = creation . createPrismaticJoint ( urdfLinkIndex , * linkRigidBody , * parentRigidBody , offsetInB , offsetInA , jointAxisInJointSpace , jointLowerLimit , jointUpperLimit ) ;
2015-06-25 06:19:00 +00:00
2015-04-16 16:55:32 +00:00
if ( enableConstraints )
world1 - > addConstraint ( dof6 , true ) ;
2015-06-05 18:46:53 +00:00
2015-07-27 20:28:47 +00:00
//b3Printf("Prismatic\n");
2015-04-16 16:55:32 +00:00
}
break ;
}
default :
{
2015-07-27 20:28:47 +00:00
//b3Printf("Error: unsupported joint type in URDF (%d)\n", jointType);
2015-04-16 16:55:32 +00:00
btAssert ( 0 ) ;
}
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
if ( createMultiBody )
{
2015-09-26 00:00:44 +00:00
//if (compoundShape->getNumChildShapes()>0)
2015-04-16 16:55:32 +00:00
{
2015-04-22 23:35:27 +00:00
btMultiBodyLinkCollider * col = creation . allocateMultiBodyLinkCollider ( urdfLinkIndex , mbLinkIndex , cache . m_bulletMultiBody ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
compoundShape - > setUserIndex ( graphicsIndex ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
col - > setCollisionShape ( compoundShape ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
btTransform tr ;
tr . setIdentity ( ) ;
tr = linkTransformInWorldSpace ;
2015-06-05 18:46:53 +00:00
//if we don't set the initial pose of the btCollisionObject, the simulator will do this
2015-04-16 16:55:32 +00:00
//when syncing the btMultiBody link transforms to the btMultiBodyLinkCollider
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
col - > setWorldTransform ( tr ) ;
2016-09-19 14:02:43 +00:00
//base and fixed? -> static, otherwise flag as dynamic
bool isDynamic = ( mbLinkIndex < 0 & & cache . m_bulletMultiBody - > hasFixedBase ( ) ) ? false : true ;
2015-04-16 16:55:32 +00:00
short collisionFilterGroup = isDynamic ? short ( btBroadphaseProxy : : DefaultFilter ) : short ( btBroadphaseProxy : : StaticFilter ) ;
short collisionFilterMask = isDynamic ? short ( btBroadphaseProxy : : AllFilter ) : short ( btBroadphaseProxy : : AllFilter ^ btBroadphaseProxy : : StaticFilter ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
world1 - > addCollisionObject ( col , collisionFilterGroup , collisionFilterMask ) ;
2015-06-05 18:46:53 +00:00
2015-11-07 01:11:15 +00:00
btVector4 color = selectColor2 ( ) ; //(0.0,0.0,0.5);
u2b . getLinkColor ( urdfLinkIndex , color ) ;
2015-04-22 23:35:27 +00:00
creation . createCollisionObjectGraphicsInstance ( urdfLinkIndex , col , color ) ;
2016-06-01 05:55:13 +00:00
2016-08-11 21:55:30 +00:00
u2b . convertLinkVisualShapes2 ( urdfLinkIndex , pathPrefix , localInertialFrame , col , u2b . getBodyUniqueId ( ) ) ;
2015-06-05 18:46:53 +00:00
2016-07-11 07:26:40 +00:00
URDFLinkContactInfo contactInfo ;
u2b . getLinkContactInfo ( urdfLinkIndex , contactInfo ) ;
if ( ( contactInfo . m_flags & URDF_CONTACT_HAS_LATERAL_FRICTION ) ! = 0 )
{
col - > setFriction ( contactInfo . m_lateralFriction ) ;
}
if ( ( contactInfo . m_flags & URDF_CONTACT_HAS_ROLLING_FRICTION ) ! = 0 )
{
col - > setRollingFriction ( contactInfo . m_rollingFriction ) ;
}
2016-09-15 23:04:33 +00:00
if ( ( contactInfo . m_flags & URDF_CONTACT_HAS_SPINNING_FRICTION ) ! = 0 )
{
col - > setSpinningFriction ( contactInfo . m_spinningFriction ) ;
}
2016-09-02 23:40:56 +00:00
if ( ( contactInfo . m_flags & URDF_CONTACT_HAS_STIFFNESS_DAMPING ) ! = 0 )
{
col - > setContactStiffnessAndDamping ( contactInfo . m_contactStiffness , contactInfo . m_contactDamping ) ;
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
if ( mbLinkIndex > = 0 ) //???? double-check +/- 1
{
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_collider = col ;
} else
{
cache . m_bulletMultiBody - > setBaseCollider ( col ) ;
}
}
2016-06-01 05:55:13 +00:00
} else
{
//u2b.convertLinkVisualShapes2(urdfLinkIndex,pathPrefix,localInertialFrame,compoundShape);
2015-04-16 16:55:32 +00:00
}
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
btAlignedObjectArray < int > urdfChildIndices ;
u2b . getLinkChildIndices ( urdfLinkIndex , urdfChildIndices ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
int numChildren = urdfChildIndices . size ( ) ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
for ( int i = 0 ; i < numChildren ; i + + )
{
int urdfChildLinkIndex = urdfChildIndices [ i ] ;
2015-06-05 18:46:53 +00:00
2016-08-29 22:09:18 +00:00
ConvertURDF2BulletInternal ( u2b , creation , cache , urdfChildLinkIndex , linkTransformInWorldSpace , world1 , createMultiBody , pathPrefix , flags ) ;
2015-04-16 16:55:32 +00:00
}
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
}
2016-08-29 22:09:18 +00:00
void ConvertURDF2Bullet (
const URDFImporterInterface & u2b , MultiBodyCreationInterface & creation ,
const btTransform & rootTransformInWorldSpace ,
btMultiBodyDynamicsWorld * world1 ,
bool createMultiBody , const char * pathPrefix , int flags )
2015-04-16 16:55:32 +00:00
{
URDF2BulletCachedData cache ;
InitURDF2BulletCache ( u2b , cache ) ;
int urdfLinkIndex = u2b . getRootLinkIndex ( ) ;
2016-08-29 22:09:18 +00:00
ConvertURDF2BulletInternal ( u2b , creation , cache , urdfLinkIndex , rootTransformInWorldSpace , world1 , createMultiBody , pathPrefix , flags ) ;
2015-06-05 18:46:53 +00:00
2015-04-23 22:41:17 +00:00
if ( world1 & & cache . m_bulletMultiBody )
{
btMultiBody * mb = cache . m_bulletMultiBody ;
mb - > setHasSelfCollision ( false ) ;
mb - > finalizeMultiDof ( ) ;
2016-08-18 16:44:33 +00:00
btTransform localInertialFrameRoot = cache . m_urdfLinkLocalInertialFrames [ urdfLinkIndex ] ;
mb - > setBaseWorldTransform ( rootTransformInWorldSpace * localInertialFrameRoot ) ;
2015-07-21 06:35:29 +00:00
btAlignedObjectArray < btQuaternion > scratch_q ;
btAlignedObjectArray < btVector3 > scratch_m ;
mb - > forwardKinematics ( scratch_q , scratch_m ) ;
mb - > updateCollisionObjectWorldTransforms ( scratch_q , scratch_m ) ;
2015-04-23 22:41:17 +00:00
world1 - > addMultiBody ( mb ) ;
}
2015-04-16 16:55:32 +00:00
}