Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

robot/sim/SimulatedKinematicChain.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2002 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018   $Id: SimulatedKinematicChain.cpp 1033 2004-02-11 20:47:52Z jungd $
00019   $Revision: 1.2 $
00020   $Date: 2004-02-11 15:47:52 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
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   // If no explicit geometry if provided for links, a Box will be used
00096   //  The dimensions are based on the link lengths as calculated via
00097   //  a line segment joining the origins of consecutive links is
00098   //  calculated here for this purpose
00099   
00100   Assert(chain.size() > 0);
00101   
00102   array<Dimension3> dim(chain.size()+1); // link dimensions (1..#links)
00103   linkLengths.resize(chain.size()+1);
00104   
00105   array<Vector> linkOrigin(chain.getLinkOrigins(zeroVector(chain.dof()))); //!!! use home pos?
00106   
00107   for (Int l=1; l<=chain.size(); l++) { // for each link
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; // some links are zero length so make them very small instead
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); // make x&y-dims smaller than length (z-dim)
00125     
00126     dim[l] = Dimension3(yz,yz,zl);
00127   }
00128  
00129   return dim; 
00130 }
00131 
00132 
00133 
00134 
00135 // ProximityCollisionResponseHandler
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() ); //  all elements now default
00147   Collider::reset(); // pass it on
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); // pass through
00167   }
00168 }
00169 
00170 
00171 
00172 void SimulatedKinematicChain::ProximityCollisionResponseHandler::handleCollision(ref<physics::CollisionState> collisionState) 
00173 {
00174   kc->handleCollision(collisionState);
00175 }
00176 

Generated on Thu Jul 29 15:56:40 2004 for OpenSim by doxygen 1.3.6