00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <robot/sim/SimulatedKinematicChain>
00026
00027 #include <base/Vector3>
00028 #include <base/Orient>
00029 #include <base/Transform>
00030 #include <base/Externalizer>
00031
00032 #include <gfx/Color3>
00033 #include <gfx/Line3>
00034 #include <gfx/Segment3>
00035 #include <physics/BoundingBox>
00036 #include <physics/Material>
00037 #include <physics/Box>
00038 #include <physics/Sphere>
00039 #include <physics/Cylinder>
00040 #include <physics/Solid>
00041 #include <physics/ConstraintGroup>
00042 #include <physics/Joint>
00043 #include <physics/HingeJoint>
00044 #include <physics/SliderJoint>
00045 #include <physics/Motor>
00046 #include <physics/CollisionCuller>
00047 #include <physics/CollisionDetector>
00048 #include <physics/CollisionState>
00049 #include <physics/NullCollisionResponseHandler>
00050 #include <physics/SpatialTransform>
00051
00052
00053
00054
00055 using robot::sim::SimulatedKinematicChain;
00056
00057 using base::Point3;
00058 using base::Orient;
00059 using base::Dimension3;
00060 using base::Transform;
00061 using gfx::Line3;
00062 using gfx::Segment3;
00063 using gfx::Color4;
00064 using physics::BoundingBox;
00065 using physics::Collidable;
00066 using physics::CollidableBody;
00067 using physics::CollidableGroup;
00068 using physics::CollisionState;
00069 using physics::CollisionCuller;
00070 using physics::CollisionDetector;
00071 using physics::CollisionResponseHandler;
00072 using physics::NullCollisionResponseHandler;
00073 using physics::Shape;
00074 using physics::Box;
00075 using physics::Sphere;
00076 using physics::Cylinder;
00077 using physics::Material;
00078 using physics::Solid;
00079 using physics::SpatialGroup;
00080 using physics::SpatialTransform;
00081 using physics::ConstraintGroup;
00082 using physics::Joint;
00083 using physics::HingeJoint;
00084 using physics::SliderJoint;
00085 using physics::Motor;
00086
00087
00088 using robot::KinematicChain;
00089
00090
00091
00092
00093 array<base::Dimension3> SimulatedKinematicChain::computeLinkDimensions(const array<Real>& linkRadii)
00094 {
00095
00096
00097
00098
00099
00100 Assert(chain.size() > 0);
00101
00102 array<Dimension3> dim(chain.size()+1);
00103 linkLengths.resize(chain.size()+1);
00104
00105 array<Vector> linkOrigin(chain.getLinkOrigins(zeroVector(chain.dof())));
00106
00107 for (Int l=1; l<=chain.size(); l++) {
00108 Real len;
00109
00110 if (l>1)
00111 len = base::toVector3(linkOrigin[l-1] - linkOrigin[l-2]).length();
00112 else
00113 len = base::toVector3(linkOrigin[0]).length();
00114
00115 linkLengths[l] = len;
00116
00117 Real zl=len*0.98;
00118 if (Math::equals(len,0)) zl=0.05;
00119
00120 Real yz;
00121 if (linkRadii.size() >= l+1)
00122 yz = linkRadii[l]*2.0;
00123 else
00124 yz = Math::maximum(0.05,zl*0.14);
00125
00126 dim[l] = Dimension3(yz,yz,zl);
00127 }
00128
00129 return dim;
00130 }
00131
00132
00133
00134
00135
00136 SimulatedKinematicChain::ProximityCollisionResponseHandler::ProximityCollisionResponseHandler(ref<SimulatedKinematicChain> kc,
00137 ref<CollisionDetector> collisionDetector)
00138 : CollisionResponseHandler(collisionDetector), kc(kc), resetCalled(false)
00139 {
00140 }
00141
00142
00143 void SimulatedKinematicChain::ProximityCollisionResponseHandler::reset()
00144 {
00145 Assert(kc->links.size() > 0);
00146 kc->linkProximity.clear().resize( kc->links.size() );
00147 Collider::reset();
00148
00149 resetCalled = true;
00150 }
00151
00152 const Real SimulatedKinematicChain::maxDist = Real(consts::maxInt);
00153
00154
00155 void SimulatedKinematicChain::ProximityCollisionResponseHandler::collide(ref<const physics::Collidable> collidable1, ref<const physics::Collidable> collidable2)
00156 {
00157 Assertm(resetCalled,"reset() called before collide()");
00158
00159 if ((collidable1->getUserData() == kc) || (collidable2->getUserData() == kc)) {
00160
00161 ref<const CollidableBody> body1( narrow_ref<const CollidableBody>(collidable1) );
00162 ref<const CollidableBody> body2( narrow_ref<const CollidableBody>(collidable2) );
00163 handleCollision(collisionDetector->getCollisionState(body1, body2));
00164 }
00165 else {
00166 notifyListeners(collidable1, collidable2);
00167 }
00168 }
00169
00170
00171
00172 void SimulatedKinematicChain::ProximityCollisionResponseHandler::handleCollision(ref<physics::CollisionState> collisionState)
00173 {
00174 kc->handleCollision(collisionState);
00175 }
00176