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"
2018-02-17 03:44:33 +00:00
# include "BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h"
2015-04-16 16:55:32 +00:00
# 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>
2017-10-06 20:46:24 +00:00
# include "Bullet3Common/b3Logging.h"
2015-07-04 01:54:41 +00:00
2017-01-16 06:26:11 +00:00
//static int bodyCollisionFilterGroup=btBroadphaseProxy::CharacterFilter;
//static int bodyCollisionFilterMask=btBroadphaseProxy::AllFilter&(~btBroadphaseProxy::CharacterFilter);
2015-04-16 16:55:32 +00:00
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
2017-02-16 22:19:09 +00:00
void registerMultiBody ( int urdfLinkIndex , class btMultiBody * body , const btTransform & worldTransform , btScalar mass , const btVector3 & localInertiaDiagonal , const class btCollisionShape * compound , const btTransform & localInertialFrame )
2015-04-16 16:55:32 +00:00
{
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
2017-02-16 22:19:09 +00:00
void registerRigidBody ( int urdfLinkIndex , class btRigidBody * body , const btTransform & worldTransform , btScalar mass , const btVector3 & localInertiaDiagonal , const class btCollisionShape * compound , const btTransform & localInertialFrame )
2015-04-16 16:55:32 +00:00
{
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-12-01 06:24:20 +00:00
void processContactParameters ( const URDFLinkContactInfo & contactInfo , btCollisionObject * col )
{
if ( ( contactInfo . m_flags & URDF_CONTACT_HAS_LATERAL_FRICTION ) ! = 0 )
{
col - > setFriction ( contactInfo . m_lateralFriction ) ;
}
if ( ( contactInfo . m_flags & URDF_CONTACT_HAS_RESTITUTION ) ! = 0 )
{
col - > setRestitution ( contactInfo . m_restitution ) ;
}
if ( ( contactInfo . m_flags & URDF_CONTACT_HAS_ROLLING_FRICTION ) ! = 0 )
{
col - > setRollingFriction ( contactInfo . m_rollingFriction ) ;
}
if ( ( contactInfo . m_flags & URDF_CONTACT_HAS_SPINNING_FRICTION ) ! = 0 )
{
col - > setSpinningFriction ( contactInfo . m_spinningFriction ) ;
}
if ( ( contactInfo . m_flags & URDF_CONTACT_HAS_STIFFNESS_DAMPING ) ! = 0 )
{
col - > setContactStiffnessAndDamping ( contactInfo . m_contactStiffness , contactInfo . m_contactDamping ) ;
}
2017-03-20 17:58:07 +00:00
if ( ( contactInfo . m_flags & URDF_CONTACT_HAS_FRICTION_ANCHOR ) ! = 0 )
{
col - > setCollisionFlags ( col - > getCollisionFlags ( ) | btCollisionObject : : CF_HAS_FRICTION_ANCHOR ) ;
}
2016-12-01 06:24:20 +00:00
}
2017-08-14 21:59:41 +00:00
btScalar tmpUrdfScaling = 2 ;
2016-12-01 06:24:20 +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 ,
2018-05-23 04:22:22 +00:00
int flags = 0 , UrdfVisualShapeCache * cachedLinkGraphicsShapesIn = 0 , UrdfVisualShapeCache * cachedLinkGraphicsShapesOut = 0 )
2015-04-16 16:55:32 +00:00
{
2017-10-06 20:46:24 +00:00
B3_PROFILE ( " ConvertURDF2BulletInternal2 " ) ;
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 ) ;
2018-05-03 21:24:16 +00:00
u2b . getMassAndInertia2 ( urdfParentIndex , parentMass , parentLocalInertiaDiagonal , parentLocalInertialFrame , flags ) ;
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 ) ;
2018-05-03 21:24:16 +00:00
u2b . getMassAndInertia2 ( urdfLinkIndex , mass , localInertiaDiagonal , localInertialFrame , flags ) ;
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 ;
2017-03-26 20:06:46 +00:00
btScalar jointMaxForce ;
btScalar jointMaxVelocity ;
2015-06-05 18:46:53 +00:00
2015-04-16 16:55:32 +00:00
2017-03-26 20:06:46 +00:00
bool hasParentJoint = u2b . getJointInfo2 ( urdfLinkIndex , parent2joint , linkTransformInWorldSpace , jointAxisInJointSpace , jointType , jointLowerLimit , jointUpperLimit , jointDamping , jointFriction , jointMaxForce , jointMaxVelocity ) ;
2017-01-15 19:22:24 +00:00
std : : string linkName = u2b . getLinkName ( urdfLinkIndex ) ;
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
{
2016-12-31 02:32:57 +00:00
if ( flags & CUF_USE_MJCF )
{
linkTransformInWorldSpace = parentTransformInWorldSpace * linkTransformInWorldSpace ;
} else
{
linkTransformInWorldSpace = parentTransformInWorldSpace * parent2joint ;
}
2016-05-11 22:52:50 +00:00
}
2016-05-11 20:43:50 +00:00
2016-05-20 01:37:15 +00:00
2015-06-05 18:46:53 +00:00
2017-02-16 22:19:09 +00:00
btCompoundShape * tmpShape = u2b . convertLinkCollisionShapes ( urdfLinkIndex , pathPrefix , localInertialFrame ) ;
btCollisionShape * compoundShape = tmpShape ;
if ( tmpShape - > getNumChildShapes ( ) = = 1 & & tmpShape - > getChildTransform ( 0 ) = = btTransform : : getIdentity ( ) )
{
compoundShape = tmpShape - > getChildShape ( 0 ) ;
}
2016-05-20 01:37:15 +00:00
2017-10-06 20:46:24 +00:00
int graphicsIndex ;
{
B3_PROFILE ( " convertLinkVisualShapes " ) ;
2018-05-23 04:22:22 +00:00
if ( cachedLinkGraphicsShapesIn & & cachedLinkGraphicsShapesIn - > m_cachedUrdfLinkVisualShapeIndices . size ( ) > ( mbLinkIndex + 1 ) )
2018-05-23 03:26:00 +00:00
{
2018-05-23 04:22:22 +00:00
graphicsIndex = cachedLinkGraphicsShapesIn - > m_cachedUrdfLinkVisualShapeIndices [ mbLinkIndex + 1 ] ;
UrdfMaterialColor matColor = cachedLinkGraphicsShapesIn - > m_cachedUrdfLinkColors [ mbLinkIndex + 1 ] ;
2018-05-23 03:26:00 +00:00
u2b . setLinkColor2 ( urdfLinkIndex , matColor ) ;
}
else
{
graphicsIndex = u2b . convertLinkVisualShapes ( urdfLinkIndex , pathPrefix , localInertialFrame ) ;
2018-05-23 04:22:22 +00:00
if ( cachedLinkGraphicsShapesOut )
{
cachedLinkGraphicsShapesOut - > m_cachedUrdfLinkVisualShapeIndices . push_back ( graphicsIndex ) ;
UrdfMaterialColor matColor ;
u2b . getLinkColor2 ( urdfLinkIndex , matColor ) ;
cachedLinkGraphicsShapesOut - > m_cachedUrdfLinkColors . push_back ( matColor ) ;
}
2018-05-23 03:26:00 +00:00
}
2017-10-06 20:46:24 +00:00
}
2016-06-01 05:55:13 +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
if ( compoundShape )
{
2015-06-05 18:46:53 +00:00
2017-06-01 19:32:44 +00:00
UrdfMaterialColor matColor ;
btVector4 color2 = selectColor2 ( ) ;
btVector3 specular ( 0.5 , 0.5 , 0.5 ) ;
if ( u2b . getLinkColor2 ( urdfLinkIndex , matColor ) )
{
color2 = matColor . m_rgbaColor ;
specular = matColor . m_specularColor ;
}
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 ) ;
2017-05-06 00:15:57 +00:00
btAssert ( localInertiaDiagonal [ 0 ] < 1e10 ) ;
btAssert ( localInertiaDiagonal [ 1 ] < 1e10 ) ;
btAssert ( localInertiaDiagonal [ 2 ] < 1e10 ) ;
2016-08-29 22:09:18 +00:00
}
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 ) ;
2018-05-31 04:14:35 +00:00
bool canSleep = ( flags & CUF_ENABLE_SLEEPING ) ! = 0 ;
if ( ! canSleep )
{
body - > forceActivationState ( DISABLE_DEACTIVATION ) ;
}
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
2016-12-01 06:24:20 +00:00
2015-04-16 16:55:32 +00:00
compoundShape - > setUserIndex ( graphicsIndex ) ;
2015-06-05 18:46:53 +00:00
2016-12-01 06:24:20 +00:00
URDFLinkContactInfo contactInfo ;
u2b . getLinkContactInfo ( urdfLinkIndex , contactInfo ) ;
processContactParameters ( contactInfo , body ) ;
2017-06-01 19:32:44 +00:00
creation . createRigidBodyGraphicsInstance2 ( urdfLinkIndex , body , color2 , specular , 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
2016-12-01 06:24:20 +00:00
2017-03-16 23:09:55 +00:00
//untested: u2b.convertLinkVisualShapes2(linkIndex,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
2018-05-31 01:37:08 +00:00
bool canSleep = ( flags & CUF_ENABLE_SLEEPING ) ! = 0 ;
2015-04-16 16:55:32 +00:00
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 ) ;
2018-01-10 19:16:50 +00:00
if ( flags & CUF_GLOBAL_VELOCITIES_MB )
2018-01-10 06:47:56 +00:00
{
2018-01-10 19:16:50 +00:00
cache . m_bulletMultiBody - > useGlobalVelocities ( true ) ;
2018-01-10 06:47:56 +00:00
}
2017-01-15 19:22:24 +00:00
if ( flags & CUF_USE_MJCF )
{
cache . m_bulletMultiBody - > setBaseWorldTransform ( linkTransformInWorldSpace ) ;
}
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 ;
2017-03-27 03:05:48 +00:00
if ( createMultiBody & & cache . m_bulletMultiBody )
{
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_jointDamping = jointDamping ;
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_jointFriction = jointFriction ;
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_jointLowerLimit = jointLowerLimit ;
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_jointUpperLimit = jointUpperLimit ;
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_jointMaxForce = jointMaxForce ;
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_jointMaxVelocity = jointMaxVelocity ;
}
2015-04-16 16:55:32 +00:00
switch ( jointType )
{
2017-05-25 20:02:20 +00:00
case URDFFloatingJoint :
case URDFPlanarJoint :
2015-04-22 23:35:27 +00:00
case URDFFixedJoint :
2015-04-16 16:55:32 +00:00
{
2017-05-25 20:02:20 +00:00
if ( ( jointType = = URDFFloatingJoint ) | | ( jointType = = URDFPlanarJoint ) )
{
printf ( " Warning: joint unsupported, creating a fixed joint instead. " ) ;
}
2017-08-30 02:14:27 +00:00
creation . addLinkMapping ( urdfLinkIndex , mbLinkIndex ) ;
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 ( ) ) ;
2017-08-30 02:14:27 +00:00
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
2017-09-07 18:40:11 +00:00
btGeneric6DofSpring2Constraint * dof6 = 0 ;
//backward compatibility
2017-09-07 21:27:00 +00:00
if ( flags & CUF_RESERVED )
2017-09-07 18:40:11 +00:00
{
dof6 = creation . createFixedJoint ( urdfLinkIndex , * parentRigidBody , * linkRigidBody , offsetInA , offsetInB ) ;
} else
{
dof6 = creation . createFixedJoint ( urdfLinkIndex , * linkRigidBody , * parentRigidBody , offsetInB , offsetInA ) ;
}
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
{
2017-08-30 02:14:27 +00:00
creation . addLinkMapping ( urdfLinkIndex , mbLinkIndex ) ;
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 ) ;
2017-08-30 02:14:27 +00:00
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
2017-09-07 18:40:11 +00:00
btGeneric6DofSpring2Constraint * dof6 = 0 ;
//backwards compatibility
2017-09-07 21:27:00 +00:00
if ( flags & CUF_RESERVED )
2017-09-07 18:40:11 +00:00
{
dof6 = creation . createRevoluteJoint ( urdfLinkIndex , * parentRigidBody , * linkRigidBody , offsetInA , offsetInB , jointAxisInJointSpace , jointLowerLimit , jointUpperLimit ) ;
} else
{
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
{
2017-08-30 02:14:27 +00:00
creation . addLinkMapping ( urdfLinkIndex , mbLinkIndex ) ;
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 ) ;
2017-08-30 02:14:27 +00:00
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
2017-08-30 02:14:27 +00:00
btGeneric6DofSpring2Constraint * dof6 = creation . createPrismaticJoint ( urdfLinkIndex , * parentRigidBody , * linkRigidBody , offsetInA , offsetInB , 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
2018-02-17 03:44:33 +00:00
if ( compoundShape - > getShapeType ( ) = = TRIANGLE_MESH_SHAPE_PROXYTYPE )
{
btBvhTriangleMeshShape * trimeshShape = ( btBvhTriangleMeshShape * ) compoundShape ;
if ( trimeshShape - > getTriangleInfoMap ( ) )
{
col - > setCollisionFlags ( col - > getCollisionFlags ( ) | btCollisionObject : : CF_CUSTOM_MATERIAL_CALLBACK ) ;
}
}
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 ;
2017-01-16 06:26:11 +00:00
int collisionFilterGroup = isDynamic ? int ( btBroadphaseProxy : : DefaultFilter ) : int ( btBroadphaseProxy : : StaticFilter ) ;
int collisionFilterMask = isDynamic ? int ( btBroadphaseProxy : : AllFilter ) : int ( btBroadphaseProxy : : AllFilter ^ btBroadphaseProxy : : StaticFilter ) ;
2015-06-05 18:46:53 +00:00
2017-01-15 19:22:24 +00:00
int colGroup = 0 , colMask = 0 ;
2017-05-10 22:01:25 +00:00
int collisionFlags = u2b . getCollisionGroupAndMask ( urdfLinkIndex , colGroup , colMask ) ;
if ( collisionFlags & URDF_HAS_COLLISION_GROUP )
2017-01-15 19:22:24 +00:00
{
collisionFilterGroup = colGroup ;
}
2017-05-10 22:01:25 +00:00
if ( collisionFlags & URDF_HAS_COLLISION_MASK )
2017-01-15 19:22:24 +00:00
{
collisionFilterMask = colMask ;
}
2015-04-16 16:55:32 +00:00
world1 - > addCollisionObject ( col , collisionFilterGroup , collisionFilterMask ) ;
2015-06-05 18:46:53 +00:00
2017-06-01 19:32:44 +00:00
btVector4 color2 = selectColor2 ( ) ; //(0.0,0.0,0.5);
btVector3 specularColor ( 1 , 1 , 1 ) ;
UrdfMaterialColor matCol ;
if ( u2b . getLinkColor2 ( urdfLinkIndex , matCol ) )
{
color2 = matCol . m_rgbaColor ;
specularColor = matCol . m_specularColor ;
}
2017-10-06 20:46:24 +00:00
{
B3_PROFILE ( " createCollisionObjectGraphicsInstance2 " ) ;
creation . createCollisionObjectGraphicsInstance2 ( urdfLinkIndex , col , color2 , specularColor ) ;
}
{
B3_PROFILE ( " convertLinkVisualShapes2 " ) ;
u2b . convertLinkVisualShapes2 ( mbLinkIndex , urdfLinkIndex , pathPrefix , localInertialFrame , col , u2b . getBodyUniqueId ( ) ) ;
}
2016-07-11 07:26:40 +00:00
URDFLinkContactInfo contactInfo ;
u2b . getLinkContactInfo ( urdfLinkIndex , contactInfo ) ;
2016-12-01 06:24:20 +00:00
processContactParameters ( contactInfo , col ) ;
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 ;
2017-05-10 22:01:25 +00:00
if ( flags & CUF_USE_SELF_COLLISION_EXCLUDE_PARENT )
{
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_flags | = BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION ;
}
if ( flags & CUF_USE_SELF_COLLISION_EXCLUDE_ALL_PARENTS )
{
cache . m_bulletMultiBody - > getLink ( mbLinkIndex ) . m_flags | = BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION ;
}
2015-04-16 16:55:32 +00:00
} else
{
2018-06-11 18:41:33 +00:00
if ( cache . m_bulletMultiBody - > getBaseMass ( ) = = 0 & & cache . m_bulletMultiBody - > getNumDofs ( ) = = 0 )
2018-06-05 19:20:42 +00:00
{
//col->setCollisionFlags(btCollisionObject::CF_KINEMATIC_OBJECT);
col - > setCollisionFlags ( btCollisionObject : : CF_STATIC_OBJECT ) ;
}
2015-04-16 16:55:32 +00:00
cache . m_bulletMultiBody - > setBaseCollider ( col ) ;
}
}
2016-06-01 05:55:13 +00:00
} else
{
2018-01-17 01:58:19 +00:00
int mbLinkIndex = cache . getMbIndexFromUrdfIndex ( urdfLinkIndex ) ;
//u2b.convertLinkVisualShapes2(mbLinkIndex, urdfLinkIndex, pathPrefix, localInertialFrame, col, u2b.getBodyUniqueId());
u2b . convertLinkVisualShapes2 ( - 1 , urdfLinkIndex , pathPrefix , localInertialFrame , linkRigidBody , u2b . getBodyUniqueId ( ) ) ;
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
2018-05-23 03:26:00 +00:00
ConvertURDF2BulletInternal ( u2b , creation , cache , urdfChildLinkIndex , linkTransformInWorldSpace , world1 , createMultiBody , pathPrefix , flags , cachedLinkGraphicsShapesIn , cachedLinkGraphicsShapesOut ) ;
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 ,
2018-05-23 04:22:22 +00:00
bool createMultiBody , const char * pathPrefix , int flags , UrdfVisualShapeCache * cachedLinkGraphicsShapes )
2015-04-16 16:55:32 +00:00
{
2018-05-23 03:26:00 +00:00
URDF2BulletCachedData cache ;
2015-04-16 16:55:32 +00:00
InitURDF2BulletCache ( u2b , cache ) ;
int urdfLinkIndex = u2b . getRootLinkIndex ( ) ;
2017-10-06 20:46:24 +00:00
B3_PROFILE ( " ConvertURDF2Bullet " ) ;
2018-05-23 03:26:00 +00:00
UrdfVisualShapeCache cachedLinkGraphicsShapesOut ;
2018-05-23 04:22:22 +00:00
ConvertURDF2BulletInternal ( u2b , creation , cache , urdfLinkIndex , rootTransformInWorldSpace , world1 , createMultiBody , pathPrefix , flags , cachedLinkGraphicsShapes , & cachedLinkGraphicsShapesOut ) ;
if ( cachedLinkGraphicsShapes & & cachedLinkGraphicsShapesOut . m_cachedUrdfLinkVisualShapeIndices . size ( ) > cachedLinkGraphicsShapes - > m_cachedUrdfLinkVisualShapeIndices . size ( ) )
2018-05-23 03:26:00 +00:00
{
2018-05-23 04:22:22 +00:00
* cachedLinkGraphicsShapes = cachedLinkGraphicsShapesOut ;
2018-05-23 03:26:00 +00:00
}
2015-06-05 18:46:53 +00:00
2015-04-23 22:41:17 +00:00
if ( world1 & & cache . m_bulletMultiBody )
{
2017-10-06 20:46:24 +00:00
B3_PROFILE ( " Post process " ) ;
2015-04-23 22:41:17 +00:00
btMultiBody * mb = cache . m_bulletMultiBody ;
2017-05-10 22:01:25 +00:00
mb - > setHasSelfCollision ( ( flags & CUF_USE_SELF_COLLISION ) ! = 0 ) ;
2015-04-23 22:41:17 +00:00
mb - > finalizeMultiDof ( ) ;
2016-08-18 16:44:33 +00:00
btTransform localInertialFrameRoot = cache . m_urdfLinkLocalInertialFrames [ urdfLinkIndex ] ;
2017-01-15 19:22:24 +00:00
if ( flags & CUF_USE_MJCF )
{
} else
{
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
}