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

robot/sim/TestBasicEnvironment.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2003 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: TestBasicEnvironment.cpp 1033 2004-02-11 20:47:52Z jungd $
00019   $Revision: 1.6 $
00020   $Date: 2004-02-11 15:47:52 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <robot/sim/TestBasicEnvironment>
00026 
00027 #include <base/Externalizer>
00028 #include <base/externalization_error>
00029 #include <base/Application>
00030 #include <base/VFile>
00031 
00032 using robot::sim::TestBasicEnvironment;
00033 
00034 using base::Externalizer;
00035 using base::externalization_error;
00036 using base::Orient;
00037 using base::Dimension3;
00038 using base::Application;
00039 using base::VFile;
00040 using base::PathName;
00041 using base::dom::DOMNode;
00042 using base::dom::DOMElement;
00043 using robot::TestRobot;
00044 using robot::sim::BasicEnvironment;
00045 
00046 
00047 
00048 TestBasicEnvironment::TestBasicEnvironment(ref<base::VFileSystem> fs, ref<base::Cache> cache, const String& name)
00049   : BasicEnvironment(fs, cache, name)
00050 {
00051   Logln("Warning: class TestBasicEnvironment will be deprecated in favour of SimulatedBasicEnvironment in future");
00052 }
00053 
00054 /*
00055 ref<robot::Robot> TestBasicEnvironment::addRobot(ref<base::VFile> robotSpecification,
00056                                                  const base::Point3& position, 
00057                                                  const base::Orient& orientation,
00058                                                  bool anchored)
00059 {
00060   ref<TestRobot> robot(NewObj TestRobot(robotSpecification,
00061                                         position, orientation));
00062   robots.push_back(robot);
00063   return robot;
00064 }
00065 */
00066 
00067 ref<robot::Robot> TestBasicEnvironment::addRobot(ref<const robot::RobotDescription> robotDescription,
00068                                                  const base::Point3& position, 
00069                                                  const base::Orient& orientation,
00070                                                  bool anchored)
00071 {
00072   ref<TestRobot> robot(NewObj TestRobot(robotDescription,
00073                        position, orientation));
00074   robots.push_back(robot);
00075   return robot;
00076 }
00077 
00078 
00079 void TestBasicEnvironment::removeRobot(ref<robot::Robot> robot)
00080 {
00081   if (instanceof(*robot, TestRobot)) {
00082     ref<TestRobot> testRobot( narrow_ref<TestRobot>(robot));
00083     robots.remove(testRobot);
00084   }
00085   else
00086     throw std::invalid_argument(Exception("unknown robot"));
00087 }
00088 
00089 
00090 ref<BasicEnvironment::Tool> TestBasicEnvironment::addTool(ref<const robot::ToolDescription> toolDescription,
00091                                                           const base::Point3& position, 
00092                                                           const base::Orient& orientation)
00093 {
00094   ref<Tool> tool(NewObj Tool(toolDescription->getName(),toolDescription,
00095                              position, orientation));
00096   tools.push_back(tool);
00097   return tool;
00098 }
00099 
00100 
00101 void TestBasicEnvironment::removeTool(ref<BasicEnvironment::Tool> tool)
00102 {
00103   tools.remove(tool);
00104 }
00105 
00106 
00107 void TestBasicEnvironment::placeToolInProximity(ref<Tool> tool, ref<Robot> robot, Int manipulatorIndex)
00108 {
00109    if (instanceof(*robot, TestRobot)) {
00110      ref<const ToolDescription> toolDescription(tool->getToolDescription());
00111 
00112     ref<TestRobot> testRobot( narrow_ref<TestRobot>(robot));
00113     testRobot->placeToolInProximity(toolDescription, manipulatorIndex);
00114   }
00115   else
00116     throw std::invalid_argument(Exception("unknown robot"));
00117 }
00118 
00119 
00120 
00121 ref<BasicEnvironment::Obstacle> TestBasicEnvironment::addBoxObstacle(base::Dimension3 dim, 
00122                                                                      const base::Point3& position, 
00123                                                                      const base::Orient& orientation,
00124                                                                      const String& name)
00125 {
00126   ref<Obstacle> obstacle(NewObj Obstacle(dim,position,orientation));
00127   obstacles.push_back(obstacle);
00128   return obstacle;
00129 }
00130 
00131 
00132 ref<BasicEnvironment::Obstacle> TestBasicEnvironment::addSphereObstacle(Real radius,
00133                                                                         const base::Point3& position, 
00134                                                                         const base::Orient& orientation,
00135                                                                         const String& name)
00136 {
00137   ref<Obstacle> obstacle(NewObj Obstacle(radius,position,orientation));
00138   obstacles.push_back(obstacle);
00139   return obstacle;
00140 }
00141 
00142 
00143 void TestBasicEnvironment::removeObstacle(ref<BasicEnvironment::Obstacle> obstacle)
00144 {
00145   obstacles.remove(obstacle);
00146 }
00147 
00148 
00149 
00150 void TestBasicEnvironment::preSimulate()
00151 {
00152 }
00153 
00154 
00155 void TestBasicEnvironment::simulateForSimTime(const base::Time& dt)
00156 {
00157 }
00158 
00159 
00160 bool TestBasicEnvironment::formatSupported(const String format, Real version, ExternalizationType type) const
00161 {
00162   return ( (format=="xml") && (version==1.0) ); 
00163 }
00164 
00165 
00166 void TestBasicEnvironment::externalize(base::Externalizer& e, const String format, Real version)
00167 {
00168   if (!formatSupported(format,version,e.ioType()))
00169     throw std::invalid_argument(Exception(String("format ")+format+" v"+base::realToString(version)+" unsupported"));
00170   
00171   if (format == "xml") {
00172 
00173     if (e.isOutput()) {
00174 
00175       DOMElement* envElem = e.createElement("environment");
00176       e.setElementAttribute(envElem,"type","basic");
00177 
00178       e.pushContext(envElem);
00179 
00180       e.appendBreak(envElem);
00181 
00182       // externalize the robots
00183       RobotList::const_iterator r = robots.begin();
00184       RobotList::const_iterator rend = robots.end();
00185      if (r != rend)
00186        e.appendComment(envElem,"robots   (description, position, and orientation (Quat) )");
00187       while (r != rend) {
00188         ref<TestRobot> robot(*r);
00189         
00190         if (robot->isDescriptionProvided()) 
00191           robot->getRobotDescription()->externalize(e, format, version);
00192         else
00193           throw externalization_error(Exception("can't externalize a Robot with no description"));
00194         
00195         DOMElement* robotElem = e.lastAppendedElement();
00196         Point3 position(robot->getPosition());
00197         DOMElement* posElem = e.createElement("position",false);
00198         e.appendText(posElem, e.toString(position) );
00199         e.appendNode(robotElem, posElem);
00200         e.appendBreak(robotElem);
00201         
00202         Orient orientation(robot->getOrientation());
00203         DOMElement* orientElem = e.createElement("orientation",false);
00204         Vector v( orientation.getVector(Orient::Quat) );
00205         e.appendText(orientElem, e.toString(v,true));
00206         e.appendNode(robotElem, orientElem);
00207         e.appendBreak(robotElem);
00208         
00209         ++r;
00210         e.appendBreak(envElem);
00211       }
00212       e.appendBreak(envElem);
00213 
00214 
00215       // externalize tools
00216       ToolList::const_iterator t = tools.begin();
00217       ToolList::const_iterator tend = tools.end();
00218       if (t != tend)
00219         e.appendComment(envElem,"tools   (position, orientation (Quat) and description)");
00220         while (t != tend) {
00221           ref<Tool> tool(*t);
00222           tool->getToolDescription()->externalize(e, format, version);
00223           
00224           DOMElement* toolElem = e.lastAppendedElement();
00225           
00226           Point3& position(tool->getPosition()); 
00227           DOMElement* posElem = e.createElement("position",false);
00228           e.appendText(posElem, e.toString(position) );
00229           e.appendNode(toolElem, posElem);
00230           e.appendBreak(toolElem);
00231           
00232           Orient& orientation(tool->getOrientation()); 
00233           DOMElement* orientElem = e.createElement("orientation",false);
00234           Vector v( orientation.getVector(Orient::Quat) );
00235           e.appendText(orientElem, e.toString(v,true));
00236           e.appendNode(toolElem, orientElem);
00237           e.appendBreak(toolElem);
00238           
00239           ++t;
00240           e.appendBreak(envElem);
00241       }
00242       e.appendBreak(envElem);
00243 
00244 
00245       // externalize obstacles
00246       ObstacleList::const_iterator o = obstacles.begin();
00247       ObstacleList::const_iterator oend = obstacles.end();
00248       if (o != oend)
00249         e.appendComment(envElem,"obstacles (position, orientation (Quat) and description)");
00250       while (o != oend) {
00251         ref<Obstacle> obstacle(*o);
00252         
00253         DOMElement* obstElem = e.createElement("obstacle");
00254         
00255         Point3& position(obstacle->position); 
00256         DOMElement* posElem = e.createElement("position",false);
00257         e.appendText(posElem, e.toString(position) );
00258         e.appendNode(obstElem, posElem);
00259         e.appendBreak(obstElem);
00260         
00261         Orient& orientation(obstacle->orientation); 
00262         DOMElement* orientElem = e.createElement("orientation",false);
00263         Vector v( orientation.getVector(Orient::Quat) );
00264         e.appendText(orientElem, e.toString(v,true));
00265         e.appendNode(obstElem, orientElem);
00266         e.appendBreak(obstElem);
00267         
00268         if (obstacle->type == Obstacle::BoxObstacle) {
00269           e.setElementAttribute(obstElem,"type","box");
00270           DOMElement* dElem = e.createElement("dimensions",false);
00271           e.appendText( dElem, e.toString(obstacle->dims) );
00272           e.appendNode(obstElem,dElem);
00273           e.appendBreak(obstElem);
00274         }
00275         else if (obstacle->type == Obstacle::SphereObstacle) {
00276           e.setElementAttribute(obstElem,"type","sphere");
00277           DOMElement* rElem = e.createElement("radius",false);
00278           e.appendText(rElem, base::realToString(obstacle->radius));
00279           e.appendBreak(obstElem);
00280           e.appendNode(obstElem,rElem);
00281         }
00282         else
00283           throw externalization_error(Exception("unsupported obstacle type"));
00284         
00285         e.appendNode(envElem, obstElem);
00286         
00287         ++o;
00288         e.appendBreak(envElem);
00289       }
00290 
00291       e.popContext();
00292 
00293       e.appendElement(envElem);
00294 
00295     }
00296     else { // input
00297       // first clear the current environment
00298       while (!robots.empty()) removeRobot( robots.front() );
00299       while (!tools.empty()) removeTool( tools.front() );
00300       while (!obstacles.empty()) removeObstacle( obstacles.front() );
00301 
00302       // now load the new one
00303       DOMNode* context = e.context();
00304     
00305       DOMElement* envElem = e.getFirstElement(context, "environment");
00306 
00307       // handle link
00308       String link = e.getElementAttribute(envElem,"link",false);
00309       if (link != "") {
00310         
00311         ref<VFile> linkFile = Application::getInstance()->universe()->cache()->findFile(link,e.getArchivePath());
00312         load(linkFile,format,version);
00313       }
00314       else {
00315         
00316         if ( e.getElementAttribute(envElem, "type") != "basic" )
00317           throw externalization_error(Exception("unsupported environment type"));
00318         
00319         e.pushContext(envElem);
00320         
00321         // read in robots
00322         DOMElement* robotElem = e.getFirstChildElement(envElem, "robot",false);
00323         while (robotElem) {
00324           // get position & orientation
00325           DOMElement* posElem = e.getFirstChildElement(robotElem,"position");
00326           String posText = e.getContainedText(posElem);
00327           Point3 position( e.toVector3(posText) );
00328           
00329           DOMElement* orientElem = e.getFirstChildElement(robotElem, "orientation");
00330           String orientText = e.getContainedText(orientElem);
00331           Vector v( e.toVector(orientText,true) );
00332           if (v.size() != 4)
00333             throw externalization_error(Exception("orientation must be a Quat (4 elements)"));
00334           Orient orientation(v, Orient::Quat);
00335           
00336           ref<RobotDescription> rd(NewObj RobotDescription());
00337           rd->externalize(e, format, version);
00338           
00339           addRobot(rd, position, orientation);
00340           
00341           robotElem = e.getFirstChildElement(envElem, "robot",false);
00342         }
00343         
00344         
00345         // read in tools
00346         DOMElement* toolElem = e.getFirstChildElement(envElem, "tool",false);
00347         while (toolElem) {
00348           // get position & orientation
00349           DOMElement* posElem = e.getFirstChildElement(toolElem,"position");
00350           String posText = e.getContainedText(posElem);
00351           Point3 position( e.toVector3(posText) );
00352           
00353           DOMElement* orientElem = e.getFirstChildElement(toolElem, "orientation");
00354           String orientText = e.getContainedText(orientElem);
00355           Vector v( e.toVector(orientText,true) );
00356           if (v.size() != 4)
00357             throw externalization_error(Exception("orientation must be a Quat (4 elements)"));
00358           Orient orientation(v, Orient::Quat);
00359           
00360           ref<ToolDescription> td(NewObj ToolDescription());
00361           td->externalize(e, format, version);
00362           
00363           addTool(td, position, orientation);
00364           
00365           toolElem = e.getFirstChildElement(envElem, "tool",false);
00366         }
00367         
00368         
00369         // read in obstacles
00370         DOMElement* obstElem = e.getFirstChildElement(envElem, "obstacle",false);
00371         while (obstElem) {
00372           String name( e.getDefaultedElementAttribute(envElem, "name", "obstacle") );
00373           
00374           // read in position & orientation
00375           DOMElement* posElem = e.getFirstChildElement(obstElem, "position");
00376           String posText = e.getContainedText(posElem);
00377           Point3 position( e.toVector3(posText) );
00378           
00379           DOMElement* orientElem = e.getFirstChildElement(obstElem, "orientation");
00380           String orientText = e.getContainedText(orientElem);
00381           Vector v( e.toVector(orientText,true) );
00382           if (v.size() != 4)
00383             throw externalization_error(Exception("orientation must be a Quat (4 elements)"));
00384           Orient orientation(v, Orient::Quat);  
00385           
00386           // get obstacle parameters & construct obstacles
00387           ref<Obstacle> obstacle;
00388           Obstacle::ObstacleType type;
00389           if ( e.getElementAttribute(obstElem, "type") == "box" ) {
00390             type = Obstacle::BoxObstacle;
00391             
00392             DOMElement* dElem = e.getFirstChildElement(obstElem, "dimensions");
00393             String dimText = e.getContainedText(dElem);
00394             Dimension3 dims( e.toVector3( dimText ) );
00395             
00396             addBoxObstacle(dims,position,orientation,name);
00397           }
00398           else if ( e.getElementAttribute(obstElem, "type") == "sphere" ) {
00399             type = Obstacle::SphereObstacle;
00400             
00401             DOMElement* rElem = e.getFirstChildElement(obstElem, "radius",false);
00402             Real radius=1.0;
00403             if (rElem) 
00404               radius = base::stringToReal( e.getContainedText(rElem) );
00405             
00406             addSphereObstacle(radius, position,orientation,name);
00407           }
00408           else
00409             throw externalization_error(Exception("unsupported obstacle type"));
00410           
00411           e.removeElement(obstElem);
00412           
00413           obstElem = e.getFirstChildElement(envElem, "obstacle",false);
00414         }
00415         
00416         
00417         e.popContext();
00418       }
00419 
00420       e.removeElement(envElem);
00421     }
00422 
00423   }
00424 
00425 }
00426 

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