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/ListBody2BodyContacts.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 ListBody2BodyContacts::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 SoNode * node2 = SoNode::getByName(Instr[2].c_str());
00055 int forceflag = atoi(Instr[3].c_str());
00056 if ((NULL == node) || (NULL == node2))
00057 return eBODY_NOT_FOUND;
00058
00059 if ( !(node->isOfType(SoPhysics::getClassTypeId())) || !(node2->isOfType(SoPhysics::getClassTypeId())))
00060 return eNODE_IS_NOT_A_BODY;
00061
00062 SoPhysics* phy = static_cast<SoPhyiscs*>(node);
00063 dBodyID body1 = phy->getBody();
00064 SoPhysics* phy2 = static_cast<SoPhyiscs*>(node2);
00065 dBodyID body2 = phy2->getBody();
00066 if ((NULL == body1) || (NULL == body2))
00067 return eNO_ODE_BODY_FOUND;
00068
00069 int numberOfJoints = dBodyGetNumJoints(body1);
00070 int numberOfContacts = 0;
00071 result = eOK;
00072 for (int i = 0; i < numberOfJoints; i++)
00073 {
00074 dJointID joint = dBodyGetJoint(body1, i);
00075 dBodyID contactBody1 = dJointGetBody(joint, 0);
00076 dBodyID contactBody2 = dJointGetBody(joint, 1);
00077 int jointType = dJointGetType(joint);
00078 if ((jointType == dJointTypeContact) && (contactBody2 == body2))
00079 {
00080 numberOfContacts++;
00081 dxJointContact* jc = static_cast<dxJointContact*>(joint);
00082 answer << "contact ";
00083
00084 dBodyID collidingBody;
00085 SoPhysics* ncb = NULL;
00086 if (contactBody1 == body1)
00087 collidingBody = contactBody2;
00088 else
00089 collidingBody = contactBody1;
00090 if (NULL != collidingBody)
00091 ncb = static_cast<SoPhysics*>(dBodyGetData(collidingBody));
00092
00093 answer << "<";
00094 if (NULL != ncb)
00095 answer << ncb->getName().getString();
00096 answer << "> x= " << jc->contact.geom.pos[0]
00097 << " y= " << jc->contact.geom.pos[1]
00098 << " z= " << jc->contact.geom.pos[2]
00099 << " nx= " << jc->contact.geom.normal[0]
00100 << " ny= " << jc->contact.geom.normal[1]
00101 << " nz= " << jc->contact.geom.normal[2]
00102 << " d= " << jc->contact.geom.depth;
00103 numberOfReturnValues += 7;
00104
00105 if (forceflag)
00106 {
00107 dJointFeedback *jointFeedback = dJointGetFeedback(joint);
00108 if (NULL != jointFeedback)
00109 {
00110 answer << " fx= " << jointFeedback->f1[0] << " fy= " << jointFeedback->f1[1] << " fz= " << jointFeedback->f1[2];
00111 numberOfReturnValues += 3;
00112 }
00113 else
00114 {
00115 answer << "Body1 provides no force feedback! ";
00116 result = eUNKNOWN_ERROR;
00117 numberOfReturnValues = 0;
00118 }
00119 }
00120
00121 answer << "\r" << std::endl;
00122 }
00123 }
00124
00125 if (numberOfContacts == 0)
00126 answer << "no contacts found\r"<< std::endl;
00127 answer << "END\r"<< std::endl;
00128 #else
00129 printf("Command not available, define ODE_SOURCE when building ipsaViewer-library !\n");
00130 #endif // ODE_SOURCE
00131 return result;
00132 }