00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "../include/IPSA/ipsaclasses.h"
00029 #include "../include/IPSA/viewer/instructions/ListBodyContacts.h"
00030
00031 #include <sstream>
00032
00033 #ifdef ODE_SOURCE
00034 #include <ode/ode.h>
00035
00036 #ifdef ODE_0_9
00037 #include <../ode/src/joint.h>
00038 #else // for ODE >= 0.10
00039 #include <../ode/src/joints/joints.h>
00040 #endif
00041 #endif // ODE_SOURCE
00042
00043 Instruction::tExecResultType ListBodyContacts::vExecute(std::vector<std::string>& Instr, std::ostringstream& answer, unsigned int& numberOfReturnValues)
00044 {
00045 numberOfReturnValues = 0;
00046 tExecResultType result = eBODY_NOT_FOUND;
00047
00048
00049
00050
00051
00052 #ifdef ODE_SOURCE
00053 SoNode* node = SoNode::getByName(Instr[1].c_str());
00054 if (NULL == node)
00055 return eBODY_NOT_FOUND;
00056
00057 if (!node->isOfType(SoPhysics::getClassTypeId()))
00058 return eNODE_IS_NOT_A_BODY;
00059
00060 SoPhysics* phy = (SoPhysics *)node;
00061 dBodyID body = phy->getBody();
00062 if (NULL == body)
00063 return eNO_ODE_BODY_FOUND;
00064
00065 int numberOfJoints = dBodyGetNumJoints(body);
00066 int numberOfContacts = 0;
00067 result = eOK;
00068 for (int i = 0; i < numberOfJoints; i++)
00069 {
00070 dJointID joint = dBodyGetJoint(body, i);
00071 int jointType = dJointGetType(joint);
00072 if (jointType == dJointTypeContact)
00073 {
00074 numberOfContacts++;
00075 dxJointContact* jointContact = static_cast<dxJointContact*>(joint);
00076 answer << "contact ";
00077 dBodyID contactBody1 = dJointGetBody(joint,0);
00078 dBodyID contactBody2 = dJointGetBody(joint,1);
00079 dBodyID collisionBody;
00080 SoPhysics* ncb = NULL;
00081 if (contactBody1 == body)
00082 collisionBody = contactBody2;
00083 else
00084 collisionBody = contactBody1;
00085 if (NULL != collisionBody)
00086 ncb = static_cast<SoPhysics*>(dBodyGetData(collisionBody));
00087 answer << "<";
00088 if (NULL != ncb)
00089 answer << ncb->getName().getString();
00090 answer << "> x= " << jointContact->contact.geom.pos[0]
00091 << " y= " << jointContact->contact.geom.pos[1]
00092 << " z= " << jointContact->contact.geom.pos[2]
00093 << " nx= " << jointContact->contact.geom.normal[0]
00094 << " ny= " << jointContact->contact.geom.normal[1]
00095 << " nz= " << jointContact->contact.geom.normal[2]
00096 << " d= " << jointContact->contact.geom.depth;
00097 numberOfReturnValues += 7;
00098 }
00099 }
00100
00101 if (numberOfContacts == 0)
00102 answer << "no contacts found!";
00103 else answer << "END\r" << std::endl;
00104 #else
00105 printf("Command not available, define ODE_SOURCE when building ipsaViewer-library !\n");
00106 #endif // ODE_SOURCE
00107 return result;
00108 }