BlueprintManager.cpp

Go to the documentation of this file.
00001 /*************************************************************************
00002 *                                                                       *
00003 * Open Physics Abstraction Layer                                        *
00004 * Copyright (C) 2004-2005                                               *
00005 * Alan Fischer  alan.fischer@gmail.com                                  *
00006 * Andres Reinot  andres@reinot.com                                      *
00007 * Tyler Streeter  tylerstreeter@gmail.com                               *
00008 * All rights reserved.                                                  *
00009 * Web: opal.sourceforge.net                                             *
00010 *                                                                       *
00011 * This library is free software; you can redistribute it and/or         *
00012 * modify it under the terms of EITHER:                                  *
00013 *   (1) The GNU Lesser General Public License as published by the Free  *
00014 *       Software Foundation; either version 2.1 of the License, or (at  *
00015 *       your option) any later version. The text of the GNU Lesser      *
00016 *       General Public License is included with this library in the     *
00017 *       file license-LGPL.txt.                                          *
00018 *   (2) The BSD-style license that is included with this library in     *
00019 *       the file license-BSD.txt.                                       *
00020 *                                                                       *
00021 * This library is distributed in the hope that it will be useful,       *
00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00024 * license-LGPL.txt and license-BSD.txt for more details.                *
00025 *                                                                       *
00026 *************************************************************************/
00027 
00028 // class headers
00029 #include "BlueprintManager.h"
00030 
00031 // project headers
00032 #include "BoxShapeData.h"
00033 #include "SphereShapeData.h"
00034 #include "CapsuleShapeData.h"
00035 #include "PlaneShapeData.h"
00036 #include "MotorData.h"
00037 #include "AttractorMotorData.h"
00038 #include "GearedMotorData.h"
00039 #include "ServoMotorData.h"
00040 #include "ThrusterMotorData.h"
00041 #include "SpringMotorData.h"
00042 #include "SensorData.h"
00043 #include "AccelerationSensorData.h"
00044 #include "InclineSensorData.h"
00045 #include "RaycastSensorData.h"
00046 #include "VolumeSensorData.h"
00047 
00048 // system headers
00049 #include "external/tinyxml/tinyxml.h"
00050 
00051 using namespace opal;
00052 using namespace std;
00053 
00054 namespace opal
00055 {
00056         OPAL_EXPORT_FUNCTION void OPAL_CALL loadFile( Blueprint& bp,
00057                 const string& filename )
00058         {
00059                 BlueprintManager::instance().loadFile( bp, filename );
00060         }
00061 
00062         namespace blueprintManagerImpl
00063         {
00064                 BlueprintManager::BlueprintManager()
00065                 {}
00066 
00067                 BlueprintManager::~BlueprintManager()
00068                 {}
00069 
00070                 void BlueprintManager::loadFile( Blueprint& bp,
00071                                                  const string& filename )
00072                 {
00073                         // Load the file.
00074                         TiXmlDocument file;
00075                         if ( false == file.LoadFile( filename.c_str() ) )
00076                         {
00077                                 OPAL_LOGGER( "warning" ) <<
00078                                 "opal::BlueprintManager::loadFile: Cannot open " <<
00079                                 filename << endl;
00080                                 return ;
00081                         }
00082 
00083                         // Find the root element (i.e. the 'OpalBlueprint' element).
00084                         TiXmlElement* rootElement = file.RootElement();
00085                         if ( NULL == rootElement )
00086                         {
00087                                 OPAL_LOGGER( "warning" ) <<
00088                                 "opal::BlueprintManager::loadFile: Missing root element "
00089                                 << "in " << filename << ". Ignoring file." << endl;
00090                                 return ;
00091                         }
00092 
00093                         TiXmlNode* nodePtr = NULL;
00094 
00095                         // Loop over Solids.
00096                         nodePtr = NULL;
00097                         while ( ( nodePtr = rootElement->IterateChildren( "Solid", nodePtr ) ) != NULL )
00098                         {
00099                                 SolidData * data = loadSolid( nodePtr, filename );
00100                                 if ( data )
00101                                 {
00102                                         // This function makes a deep copy, so we need to destroy
00103                                         // the data.
00104                                         bp.addSolid( data );
00105                                         delete data;
00106                                 }
00107                         }
00108 
00109                         // Loop over Joints.
00110                         nodePtr = NULL;
00111                         while ( ( nodePtr = rootElement->IterateChildren( "Joint", nodePtr ) ) != NULL )
00112                         {
00113                                 JointData * data = loadJoint( nodePtr, bp, filename );
00114                                 if ( data != NULL )
00115                                 {
00116                                         // This function makes a deep copy, so we need to destroy
00117                                         // the data.
00118                                         bp.addJoint( data );
00119                                         delete data;
00120                                 }
00121                         }
00122 
00123                         // Loop over Motors.
00124                         nodePtr = NULL;
00125                         while ( ( nodePtr = rootElement->IterateChildren( "Motor", nodePtr ) ) != NULL )
00126                         {
00127                                 MotorData * data = loadMotor( nodePtr, bp, filename );
00128                                 if ( data != NULL )
00129                                 {
00130                                         // This function makes a deep copy, so we need to destroy
00131                                         // the data.
00132                                         bp.addMotor( data );
00133                                         delete data;
00134                                 }
00135                         }
00136 
00137                         // Loop over Sensors.
00138                         nodePtr = NULL;
00139                         while ( ( nodePtr = rootElement->IterateChildren(
00140                                                 "Sensor", nodePtr ) ) != NULL )
00141                         {
00142                                 SensorData * data = loadSensor( nodePtr, bp, filename );
00143                                 if ( data != NULL )
00144                                 {
00145                                         // This function makes a deep copy, so we need to destroy
00146                                         // the data.
00147                                         bp.addSensor( data );
00148                                         delete data;
00149                                 }
00150                         }
00151 
00152                         // Finalized the Blueprint.
00153                         bp.finalize();
00154                 }
00155 
00156                 SolidData* BlueprintManager::loadSolid( const TiXmlNode* nodePtr,
00157                                                         const string& filename )
00158                 {
00159                         // This data structure automatically gets initialized to
00160                         // default values in its constructor.
00161                         SolidData * data = new SolidData();
00162 
00163                         TiXmlNode* paramNodePtr = NULL;
00164 
00165                         // Load Name element if it exists.
00166                         paramNodePtr =
00167                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Name" );
00168                         if ( NULL != paramNodePtr )
00169                         {
00170                                 data->name = getAttributeString( paramNodePtr, "value" );
00171                         }
00172 
00173                         // Load Enabled element if it exists.
00174                         paramNodePtr =
00175                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Enabled" );
00176                         if ( NULL != paramNodePtr )
00177                         {
00178                                 string value = getAttributeString( paramNodePtr, "value" );
00179                                 if ( "true" == value )
00180                                 {
00181                                         data->enabled = true;
00182                                 }
00183                                 else if ( "false" == value )
00184                                 {
00185                                         data->enabled = false;
00186                                 }
00187                                 else
00188                                 {
00189                                         OPAL_LOGGER( "warning" ) <<
00190                                         "opal::BlueprintManager::loadSolid: Invalid value "
00191                                         << value << " for Solid Enabled parameter in "
00192                                         << filename << " will be ignored." << endl;
00193                                 }
00194                         }
00195 
00196                         // Load Static element if it exists.
00197                         paramNodePtr =
00198                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Static" );
00199                         if ( NULL != paramNodePtr )
00200                         {
00201                                 string value = getAttributeString( paramNodePtr, "value" );
00202                                 if ( "true" == value )
00203                                 {
00204                                         data->isStatic = true;
00205                                 }
00206                                 else if ( "false" == value )
00207                                 {
00208                                         data->isStatic = false;
00209                                 }
00210                                 else
00211                                 {
00212                                         OPAL_LOGGER( "warning" ) <<
00213                                         "opal::BlueprintManager::loadSolid: Invalid value "
00214                                         << value << " for Solid Static parameter in "
00215                                         << filename << " will be ignored." << endl;
00216                                 }
00217                         }
00218 
00219                         // Load Sleeping element if it exists.
00220                         paramNodePtr =
00221                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Sleeping" );
00222                         if ( NULL != paramNodePtr )
00223                         {
00224                                 string value = getAttributeString( paramNodePtr, "value" );
00225                                 if ( "true" == value )
00226                                 {
00227                                         data->sleeping = true;
00228                                 }
00229                                 else if ( "false" == value )
00230                                 {
00231                                         data->sleeping = false;
00232                                 }
00233                                 else
00234                                 {
00235                                         OPAL_LOGGER( "warning" ) <<
00236                                         "opal::BlueprintManager::loadSolid: Invalid value "
00237                                         << value << " for Solid Sleeping parameter in "
00238                                         << filename << " will be ignored." << endl;
00239                                 }
00240                         }
00241 
00242                         // Load Sleepiness element if it exists.
00243                         paramNodePtr =
00244                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Sleepiness" );
00245                         if ( NULL != paramNodePtr )
00246                         {
00247                                 data->sleepiness = getAttributeReal( paramNodePtr, "value" );
00248                         }
00249 
00250                         // Load LinearDamping element if it exists.
00251                         paramNodePtr =
00252                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "LinearDamping" );
00253                         if ( NULL != paramNodePtr )
00254                         {
00255                                 data->linearDamping = getAttributeReal( paramNodePtr,
00256                                                                         "value" );
00257                         }
00258 
00259                         // Load AngularDamping element if it exists.
00260                         paramNodePtr =
00261                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "AngularDamping" );
00262                         if ( NULL != paramNodePtr )
00263                         {
00264                                 data->angularDamping = getAttributeReal( paramNodePtr,
00265                                                        "value" );
00266                         }
00267 
00268                         // Load LinearVelocity element if it exists.
00269                         paramNodePtr =
00270                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "LinearVelocity" );
00271                         if ( NULL != paramNodePtr )
00272                         {
00273                                 data->globalLinearVel[ 0 ] =
00274                                     getAttributeReal( paramNodePtr, "x" );
00275                                 data->globalLinearVel[ 1 ] =
00276                                     getAttributeReal( paramNodePtr, "y" );
00277                                 data->globalLinearVel[ 2 ] =
00278                                     getAttributeReal( paramNodePtr, "z" );
00279                         }
00280 
00281                         // Load AngularVelocity element if it exists.
00282                         paramNodePtr =
00283                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "AngularVelocity" );
00284                         if ( NULL != paramNodePtr )
00285                         {
00286                                 data->globalAngularVel[ 0 ] =
00287                                     getAttributeReal( paramNodePtr, "x" );
00288                                 data->globalAngularVel[ 1 ] =
00289                                     getAttributeReal( paramNodePtr, "y" );
00290                                 data->globalAngularVel[ 2 ] =
00291                                     getAttributeReal( paramNodePtr, "z" );
00292                         }
00293 
00294                         // Load Offset element if it exists.
00295                         TiXmlNode* offsetNodePtr =
00296                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Offset" );
00297                         if ( NULL != offsetNodePtr )
00298                         {
00299                                 loadOffset( data->transform, offsetNodePtr, filename );
00300                         }
00301 
00302                         // Loop over Shapes if they exist.
00303                         TiXmlNode* shapeNodePtr = NULL;
00304                         while ( ( shapeNodePtr = const_cast<TiXmlNode*>
00305                                                  ( nodePtr ) ->IterateChildren( "Shape", shapeNodePtr ) ) != NULL )
00306                         {
00307                                 ShapeData * shapeData = loadShape( shapeNodePtr, filename );
00308                                 if ( shapeData != NULL )
00309                                 {
00310                                         data->addShape( *shapeData );
00311                                 }
00312                         }
00313 
00314                         return data;
00315                 }
00316 
00317                 JointData* BlueprintManager::loadJoint( const TiXmlNode* nodePtr,
00318                                                         const Blueprint& bp, const string& filename )
00319                 {
00320                         // This data structure automatically gets initialized to
00321                         // default values in its constructor.
00322                         JointData * data = new JointData();
00323 
00324                         TiXmlNode* paramNodePtr = NULL;
00325 
00326                         // Load Name element if it exists.
00327                         paramNodePtr =
00328                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Name" );
00329                         if ( NULL != paramNodePtr )
00330                         {
00331                                 data->name = getAttributeString( paramNodePtr, "value" );
00332                         }
00333 
00334                         // Load Type element if it exists.
00335                         string type;
00336                         paramNodePtr =
00337                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Type" );
00338                         if ( NULL != paramNodePtr )
00339                         {
00340                                 type = getAttributeString( paramNodePtr, "value" );
00341                         }
00342 
00343                         if ( "hinge" == type )
00344                         {
00345                                 data->setType( HINGE_JOINT );
00346                         }
00347                         else if ( "universal" == type )
00348                         {
00349                                 data->setType( UNIVERSAL_JOINT );
00350                         }
00351                         else if ( "ball" == type )
00352                         {
00353                                 data->setType( BALL_JOINT );
00354                         }
00355                         else if ( "slider" == type )
00356                         {
00357                                 data->setType( SLIDER_JOINT );
00358                         }
00359                         else if ( "wheel" == type )
00360                         {
00361                                 data->setType( WHEEL_JOINT );
00362                         }
00363                         else if ( "fixed" == type )
00364                         {
00365                                 data->setType( FIXED_JOINT );
00366                         }
00367                         else
00368                         {
00369                                 OPAL_LOGGER( "warning" ) <<
00370                                 "opal::BlueprintManager::loadJoint: Invalid Joint type "
00371                                 << type << " in " << filename
00372                                 << ". Ignoring the Joint." << endl;
00373                                 delete data;
00374                                 return NULL;
00375                         }
00376 
00377                         // Load References element.  If it doesn't exist, ignore this
00378                         // whole Joint.
00379                         paramNodePtr =
00380                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "References" );
00381                         if ( NULL != paramNodePtr )
00382                         {
00383                                 // Get the Solids' names.
00384                                 data->solid0BlueprintRefName = getAttributeString( paramNodePtr,
00385                                                                "solid0" );
00386                                 data->solid1BlueprintRefName = getAttributeString( paramNodePtr,
00387                                                                "solid1" );
00388                         }
00389                         else
00390                         {
00391                                 OPAL_LOGGER( "warning" ) <<
00392                                 "opal::BlueprintManager::loadJoint: Missing \
00393                                 References element in " << data->name << " in " <<
00394                                 filename << ".  Ignoring the Joint." << endl;
00395                                 delete data;
00396                                 return NULL;
00397                         }
00398 
00399                         // Load Enabled element if it exists.
00400                         paramNodePtr =
00401                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Enabled" );
00402                         if ( NULL != paramNodePtr )
00403                         {
00404                                 string value = getAttributeString( paramNodePtr, "value" );
00405                                 if ( "true" == value )
00406                                 {
00407                                         data->enabled = true;
00408                                 }
00409                                 else if ( "false" == value )
00410                                 {
00411                                         data->enabled = false;
00412                                 }
00413                                 else
00414                                 {
00415                                         OPAL_LOGGER( "warning" ) <<
00416                                         "opal::BlueprintManager::loadJoint: Invalid value "
00417                                         << value << " for Joint Enabled parameter in "
00418                                         << filename << " will be ignored." << endl;
00419                                 }
00420                         }
00421 
00422                         // Load Anchor element if it exists.
00423                         paramNodePtr =
00424                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Anchor" );
00425                         if ( NULL != paramNodePtr )
00426                         {
00427                                 data->anchor[ 0 ] = getAttributeReal( paramNodePtr, "x" );
00428                                 data->anchor[ 1 ] = getAttributeReal( paramNodePtr, "y" );
00429                                 data->anchor[ 2 ] = getAttributeReal( paramNodePtr, "z" );
00430                         }
00431 
00432                         // Load BreakMode element if it exists.
00433                         paramNodePtr =
00434                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "BreakMode" );
00435                         if ( NULL != paramNodePtr )
00436                         {
00437                                 string breakMode = getAttributeString( paramNodePtr,
00438                                                                        "value" );
00439                                 if ( "unbreakable" == breakMode )
00440                                 {
00441                                         data->breakMode = UNBREAKABLE_MODE;
00442                                 }
00443                                 else if ( "threshold" == breakMode )
00444                                 {
00445                                         data->breakMode = THRESHOLD_MODE;
00446                                 }
00447                                 else if ( "accumulated" == breakMode )
00448                                 {
00449                                         data->breakMode = ACCUMULATED_MODE;
00450                                 }
00451                                 else
00452                                 {
00453                                         OPAL_LOGGER( "warning" ) <<
00454                                         "opal::BlueprintManager::loadShape: Invalid Joint \
00455                                         break mode " << breakMode << " in " << filename
00456                                         << " will be ignored." << endl;
00457                                 }
00458                         }
00459 
00460                         // Load BreakThresh element if it exists.
00461                         paramNodePtr =
00462                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "BreakThresh" );
00463                         if ( NULL != paramNodePtr )
00464                         {
00465                                 data->breakThresh = getAttributeReal( paramNodePtr, "value" );
00466                         }
00467 
00468                         // Load AccumThresh element if it exists.
00469                         paramNodePtr =
00470                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "AccumThresh" );
00471                         if ( NULL != paramNodePtr )
00472                         {
00473                                 data->accumThresh = getAttributeReal( paramNodePtr, "value" );
00474                         }
00475 
00476                         // Load AccumDamage element if it exists.
00477                         paramNodePtr =
00478                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "AccumDamage" );
00479                         if ( NULL != paramNodePtr )
00480                         {
00481                                 data->accumDamage = getAttributeReal( paramNodePtr, "value" );
00482                         }
00483 
00484                         // Load ContactsEnabled element if it exists.
00485                         paramNodePtr =
00486                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "ContactsEnabled" );
00487                         if ( NULL != paramNodePtr )
00488                         {
00489                                 string value = getAttributeString( paramNodePtr, "value" );
00490                                 if ( "true" == value )
00491                                 {
00492                                         data->contactsEnabled = true;
00493                                 }
00494                                 else if ( "false" == value )
00495                                 {
00496                                         data->contactsEnabled = false;
00497                                 }
00498                                 else
00499                                 {
00500                                         OPAL_LOGGER( "warning" ) <<
00501                                         "opal::BlueprintManager::loadJoint: Invalid value "
00502                                         << value << " for Joint ContactsEnabled parameter in "
00503                                         << filename << " will be ignored." << endl;
00504                                 }
00505                         }
00506 
00507                         // Loop over Axis elements if they exist.
00508                         TiXmlNode* axisNodePtr = NULL;
00509                         while ( ( axisNodePtr = const_cast<TiXmlNode*>
00510                                                 ( nodePtr ) ->IterateChildren( "Axis", axisNodePtr ) ) != NULL )
00511                         {
00512                                 // Load AxisNum element.  If it doesn't exist, default to
00513                                 // Axis 0.
00514                                 int axisNum = 0;
00515                                 TiXmlNode* axisParamNodePtr =
00516                                     const_cast<TiXmlNode*>( axisNodePtr ) ->
00517                                     FirstChild( "AxisNum" );
00518                                 if ( NULL != axisParamNodePtr )
00519                                 {
00520                                         axisNum =
00521                                             ( int ) getAttributeReal( axisParamNodePtr, "value" );
00522 
00523                                         if ( axisNum < 0 || axisNum > 2 )
00524                                         {
00525                                                 OPAL_LOGGER( "warning" ) <<
00526                                                 "opal::BlueprintManager::loadJoint: Invalid value "
00527                                                 << axisNum << " for Joint AxisNum parameter \
00528                                                 in " << filename << " will be ignored." << endl;
00529                                                 axisNum = 0;
00530                                         }
00531                                 }
00532 
00533                                 // Load Direction element if it exists.
00534                                 axisParamNodePtr = const_cast<TiXmlNode*>( axisNodePtr ) ->
00535                                                    FirstChild( "Direction" );
00536                                 if ( NULL != axisParamNodePtr )
00537                                 {
00538                                         data->axis[ axisNum ].direction[ 0 ] =
00539                                             getAttributeReal( axisParamNodePtr, "x" );
00540                                         data->axis[ axisNum ].direction[ 1 ] =
00541                                             getAttributeReal( axisParamNodePtr, "y" );
00542                                         data->axis[ axisNum ].direction[ 2 ] =
00543                                             getAttributeReal( axisParamNodePtr, "z" );
00544                                 }
00545 
00546                                 // Load LimitsEnabled element if it exists.
00547                                 axisParamNodePtr = const_cast<TiXmlNode*>( axisNodePtr ) ->
00548                                                    FirstChild( "LimitsEnabled" );
00549                                 if ( NULL != axisParamNodePtr )
00550                                 {
00551                                         string value =
00552                                             getAttributeString( axisParamNodePtr, "value" );
00553                                         if ( "true" == value )
00554                                         {
00555                                                 data->axis[ axisNum ].limitsEnabled = true;
00556                                         }
00557                                         else if ( "false" == value )
00558                                         {
00559                                                 data->axis[ axisNum ].limitsEnabled = false;
00560                                         }
00561                                         else
00562                                         {
00563                                                 OPAL_LOGGER( "warning" ) <<
00564                                                 "opal::BlueprintManager::loadJoint: Invalid value "
00565                                                 << value << " for Joint Axis LimitsEnabled \
00566                                                 parameter in " << filename << " will be ignored."
00567                                                 << endl;
00568                                         }
00569                                 }
00570 
00571                                 // Load LimitLow element if it exists.
00572                                 axisParamNodePtr = const_cast<TiXmlNode*>( axisNodePtr ) ->
00573                                                    FirstChild( "LimitLow" );
00574                                 if ( NULL != axisParamNodePtr )
00575                                 {
00576                                         data->axis[ axisNum ].limits.low =
00577                                             getAttributeReal( axisParamNodePtr, "value" );
00578                                 }
00579 
00580                                 // Load LimitHigh element if it exists.
00581                                 axisParamNodePtr = const_cast<TiXmlNode*>( axisNodePtr ) ->
00582                                                    FirstChild( "LimitHigh" );
00583                                 if ( NULL != axisParamNodePtr )
00584                                 {
00585                                         data->axis[ axisNum ].limits.high =
00586                                             getAttributeReal( axisParamNodePtr, "value" );
00587                                 }
00588 
00589                                 // Load LimitHardness element if it exists.
00590                                 axisParamNodePtr = const_cast<TiXmlNode*>( axisNodePtr ) ->
00591                                                    FirstChild( "LimitHardness" );
00592                                 if ( NULL != axisParamNodePtr )
00593                                 {
00594                                         data->axis[ axisNum ].limits.hardness =
00595                                             getAttributeReal( axisParamNodePtr, "value" );
00596                                 }
00597 
00598                                 // Load LimitBounciness element if it exists.
00599                                 axisParamNodePtr = const_cast<TiXmlNode*>( axisNodePtr ) ->
00600                                                    FirstChild( "LimitBounciness" );
00601                                 if ( NULL != axisParamNodePtr )
00602                                 {
00603                                         data->axis[ axisNum ].limits.bounciness =
00604                                             getAttributeReal( axisParamNodePtr, "value" );
00605                                 }
00606                         }
00607 
00608                         return data;
00609                 }
00610 
00611                 MotorData* BlueprintManager::loadMotor( const TiXmlNode* nodePtr,
00612                                                         const Blueprint& bp, const string& filename )
00613                 {
00614                         // This data structure automatically gets initialized to
00615                         // default values in its constructor.
00616                         MotorData * data = NULL;
00617 
00618                         TiXmlNode* paramNodePtr = NULL;
00619 
00620                         // Load Type element if it exists.
00621                         string type;
00622                         paramNodePtr =
00623                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Type" );
00624                         if ( NULL != paramNodePtr )
00625                         {
00626                                 type = getAttributeString( paramNodePtr, "value" );
00627                         }
00628 
00629                         // Load data for the specific type of Motor.
00630                         if ( "attractor" == type )
00631                         {
00632                                 data = loadAttractorMotor( nodePtr, bp, filename );
00633                         }
00634                         else if ( "geared" == type )
00635                         {
00636                                 data = loadGearedMotor( nodePtr, bp, filename );
00637                         }
00638                         else if ( "servo" == type )
00639                         {
00640                                 data = loadServoMotor( nodePtr, bp, filename );
00641                         }
00642                         else if ( "spring" == type )
00643                         {
00644                                 data = loadSpringMotor( nodePtr, bp, filename );
00645                         }
00646                         else if ( "thruster" == type )
00647                         {
00648                                 data = loadThrusterMotor( nodePtr, bp, filename );
00649                         }
00650                         else
00651                         {
00652                                 OPAL_LOGGER( "warning" ) <<
00653                                 "opal::BlueprintManager::loadMotor: Invalid Motor type "
00654                                 << type << " in " << filename
00655                                 << ". Ignoring the Motor." << endl;
00656                                 return NULL;
00657                         }
00658 
00659                         // Load Name element if it exists.
00660                         paramNodePtr =
00661                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Name" );
00662                         if ( NULL != paramNodePtr )
00663                         {
00664                                 data->name = getAttributeString( paramNodePtr, "value" );
00665                         }
00666 
00667                         // Load Enabled element if it exists.
00668                         paramNodePtr =
00669                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Enabled" );
00670                         if ( NULL != paramNodePtr )
00671                         {
00672                                 string value = getAttributeString( paramNodePtr, "value" );
00673                                 if ( "true" == value )
00674                                 {
00675                                         data->enabled = true;
00676                                 }
00677                                 else if ( "false" == value )
00678                                 {
00679                                         data->enabled = false;
00680                                 }
00681                                 else
00682                                 {
00683                                         OPAL_LOGGER( "warning" ) <<
00684                                         "opal::BlueprintManager::loadMotor: Invalid value "
00685                                         << value << " for Motor Enabled parameter in "
00686                                         << filename << " will be ignored." << endl;
00687                                 }
00688                         }
00689 
00690                         return data;
00691                 }
00692 
00693                 AttractorMotorData* BlueprintManager::loadAttractorMotor(
00694                     const TiXmlNode* nodePtr, const Blueprint& bp,
00695                     const string& filename )
00696                 {
00697                         // This data structure automatically gets initialized to
00698                         // default values in its constructor.
00699                         AttractorMotorData * data = new AttractorMotorData();
00700 
00701                         TiXmlNode* paramNodePtr = NULL;
00702 
00703                         // Load Strength element if it exists.
00704                         paramNodePtr =
00705                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Strength" );
00706                         if ( NULL != paramNodePtr )
00707                         {
00708                                 data->strength = getAttributeReal( paramNodePtr, "value" );
00709                         }
00710 
00711                         // Load Exponent element if it exists.
00712                         paramNodePtr =
00713                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Exponent" );
00714                         if ( NULL != paramNodePtr )
00715                         {
00716                                 data->exponent = getAttributeReal( paramNodePtr, "value" );
00717                         }
00718 
00719                         // Load References element.  If it doesn't exist, or if there
00720                         // are problems finding the references, ignore this whole Motor.
00721                         paramNodePtr =
00722                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "References" );
00723                         if ( NULL != paramNodePtr )
00724                         {
00725                                 // Get the Solids' names.
00726                                 data->solid0BlueprintRefName = getAttributeString( paramNodePtr,
00727                                                                "solid0" );
00728                                 data->solid1BlueprintRefName = getAttributeString( paramNodePtr,
00729                                                                "solid1" );
00730                         }
00731                         else
00732                         {
00733                                 OPAL_LOGGER( "warning" ) <<
00734                                 "opal::BlueprintManager::loadAttractorMotor: Missing "
00735                                 << "References element in " << data->name << " in " <<
00736                                 filename << ".  Ignoring the Motor." << endl;
00737                                 delete data;
00738                                 return NULL;
00739                         }
00740 
00741                         return data;
00742                 }
00743 
00744                 GearedMotorData* BlueprintManager::loadGearedMotor(
00745                     const TiXmlNode* nodePtr, const Blueprint& bp,
00746                     const string& filename )
00747                 {
00748                         // This data structure automatically gets initialized to
00749                         // default values in its constructor.
00750                         GearedMotorData * data = new GearedMotorData();
00751 
00752                         TiXmlNode* paramNodePtr = NULL;
00753 
00754                         // Load JointAxisNum element if it exists.
00755                         paramNodePtr =
00756                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "JointAxisNum" );
00757                         if ( NULL != paramNodePtr )
00758                         {
00759                                 data->jointAxisNum =
00760                                     ( int ) getAttributeReal( paramNodePtr, "value" );
00761 
00762                                 if ( data->jointAxisNum < 0 || data->jointAxisNum > 2 )
00763                                 {
00764                                         OPAL_LOGGER( "warning" ) <<
00765                                         "opal::BlueprintManager::loadGearedMotor: Invalid "
00766                                         << "value " << data->jointAxisNum << " for "
00767                                         << "GearedMotor JointAxisNum parameter in "
00768                                         << filename << " will be ignored." << endl;
00769                                         data->jointAxisNum = 0;
00770                                 }
00771                         }
00772 
00773                         // Load MaxTorque element if it exists.
00774                         paramNodePtr =
00775                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "MaxTorque" );
00776                         if ( NULL != paramNodePtr )
00777                         {
00778                                 data->maxTorque = getAttributeReal( paramNodePtr, "value" );
00779                         }
00780 
00781                         // Load MaxVelocity element if it exists.
00782                         paramNodePtr =
00783                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "MaxVelocity" );
00784                         if ( NULL != paramNodePtr )
00785                         {
00786                                 data->maxVelocity = getAttributeReal( paramNodePtr, "value" );
00787                         }
00788 
00789                         // Load Throttle element if it exists.
00790                         paramNodePtr =
00791                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Throttle" );
00792                         if ( NULL != paramNodePtr )
00793                         {
00794                                 data->throttle = getAttributeReal( paramNodePtr, "value" );
00795                         }
00796 
00797                         // Load References element.  If it doesn't exist, or if there
00798                         // are problems finding the references, ignore this whole Motor.
00799                         paramNodePtr =
00800                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "References" );
00801                         if ( NULL != paramNodePtr )
00802                         {
00803                                 // Get the Joint's name.
00804                                 data->jointBlueprintRefName = getAttributeString( paramNodePtr,
00805                                                               "joint" );
00806                         }
00807                         else
00808                         {
00809                                 OPAL_LOGGER( "warning" ) <<
00810                                 "opal::BlueprintManager::loadGearedMotor: Missing "
00811                                 << "References element in " << data->name << " in " <<
00812                                 filename << ".  Ignoring the Motor." << endl;
00813                                 delete data;
00814                                 return NULL;
00815                         }
00816 
00817                         return data;
00818                 }
00819 
00820                 ServoMotorData* BlueprintManager::loadServoMotor(
00821                     const TiXmlNode* nodePtr, const Blueprint& bp,
00822                     const string& filename )
00823                 {
00824                         // This data structure automatically gets initialized to
00825                         // default values in its constructor.
00826                         ServoMotorData * data = new ServoMotorData();
00827 
00828                         TiXmlNode* paramNodePtr = NULL;
00829 
00830                         // Load JointAxisNum element if it exists.
00831                         paramNodePtr =
00832                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "JointAxisNum" );
00833                         if ( NULL != paramNodePtr )
00834                         {
00835                                 data->jointAxisNum =
00836                                     ( int ) getAttributeReal( paramNodePtr, "value" );
00837 
00838                                 if ( data->jointAxisNum < 0 || data->jointAxisNum > 2 )
00839                                 {
00840                                         OPAL_LOGGER( "warning" ) <<
00841                                         "opal::BlueprintManager::loadGearedMotor: Invalid "
00842                                         << "value " << data->jointAxisNum << " for "
00843                                         << "GearedMotor JointAxisNum parameter in "
00844                                         << filename << " will be ignored." << endl;
00845                                         data->jointAxisNum = 0;
00846                                 }
00847                         }
00848 
00849                         // Load Mode element if it exists.
00850                         paramNodePtr =
00851                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Mode" );
00852                         if ( NULL != paramNodePtr )
00853                         {
00854                                 string mode = getAttributeString( paramNodePtr,
00855                                                                   "value" );
00856                                 if ( "desiredAngle" == mode )
00857                                 {
00858                                         data->mode = DESIRED_ANGLE_MODE;
00859                                 }
00860                                 else if ( "desiredVel" == mode )
00861                                 {
00862                                         data->mode = DESIRED_VEL_MODE;
00863                                 }
00864                                 else
00865                                 {
00866                                         OPAL_LOGGER( "warning" ) <<
00867                                         "opal::BlueprintManager::loadServoMotor: Invalid "
00868                                         << "Mode " << mode << " in " << data->name <<
00869                                         " in " << filename << " will be ignored."
00870                                         << endl;
00871                                 }
00872                         }
00873 
00874                         // Load DesiredAngle element if it exists.
00875                         paramNodePtr =
00876                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "DesiredAngle" );
00877                         if ( NULL != paramNodePtr )
00878                         {
00879                                 data->desiredAngle = getAttributeReal( paramNodePtr, "value" );
00880                         }
00881 
00882                         // Load DesiredVel element if it exists.
00883                         paramNodePtr =
00884                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "DesiredVel" );
00885                         if ( NULL != paramNodePtr )
00886                         {
00887                                 data->desiredVel = getAttributeReal( paramNodePtr, "value" );
00888                         }
00889 
00890                         // Load MaxTorque element if it exists.
00891                         paramNodePtr =
00892                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "MaxTorque" );
00893                         if ( NULL != paramNodePtr )
00894                         {
00895                                 data->maxTorque = getAttributeReal( paramNodePtr, "value" );
00896                         }
00897 
00898                         // Load RestoreSpeed element if it exists.
00899                         paramNodePtr =
00900                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "RestoreSpeed" );
00901                         if ( NULL != paramNodePtr )
00902                         {
00903                                 data->restoreSpeed = getAttributeReal( paramNodePtr, "value" );
00904                         }
00905 
00906                         // Load References element.  If it doesn't exist, or if there
00907                         // are problems finding the references, ignore this whole Motor.
00908                         paramNodePtr =
00909                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "References" );
00910                         if ( NULL != paramNodePtr )
00911                         {
00912                                 // Get the Joint's name.
00913                                 data->jointBlueprintRefName = getAttributeString( paramNodePtr,
00914                                                               "joint" );
00915                         }
00916                         else
00917                         {
00918                                 OPAL_LOGGER( "warning" ) <<
00919                                 "opal::BlueprintManager::loadServoMotor: Missing "
00920                                 << "References element in " << data->name << " in " <<
00921                                 filename << ".  Ignoring the Motor." << endl;
00922                                 delete data;
00923                                 return NULL;
00924                         }
00925 
00926                         return data;
00927                 }
00928 
00929                 SpringMotorData* BlueprintManager::loadSpringMotor(
00930                     const TiXmlNode* nodePtr, const Blueprint& bp,
00931                     const string& filename )
00932                 {
00933                         // This data structure automatically gets initialized to
00934                         // default values in its constructor.
00935                         SpringMotorData * data = new SpringMotorData();
00936 
00937                         TiXmlNode* paramNodePtr = NULL;
00938 
00939                         // Load Mode element if it exists.
00940                         paramNodePtr =
00941                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Mode" );
00942                         if ( NULL != paramNodePtr )
00943                         {
00944                                 string mode = getAttributeString( paramNodePtr,
00945                                                                   "value" );
00946                                 if ( "linear" == mode )
00947                                 {
00948                                         data->mode = LINEAR_MODE;
00949                                 }
00950                                 else if ( "angular" == mode )
00951                                 {
00952                                         data->mode = ANGULAR_MODE;
00953                                 }
00954                                 else if ( "linearAndAngular" == mode )
00955                                 {
00956                                         data->mode = LINEAR_AND_ANGULAR_MODE;
00957                                 }
00958                                 else
00959                                 {
00960                                         OPAL_LOGGER( "warning" ) <<
00961                                         "opal::BlueprintManager::loadSpringMotor: Invalid "
00962                                         " Mode " << mode << " in " << data->name
00963                                         << " in " << filename << " will be ignored."
00964                                         << endl;
00965                                 }
00966                         }
00967 
00968                         // Load AttachOffset element if it exists.
00969                         paramNodePtr =
00970                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "AttachOffset" );
00971                         if ( NULL != paramNodePtr )
00972                         {
00973                                 data->attachOffset[ 0 ] = getAttributeReal( paramNodePtr, "x" );
00974                                 data->attachOffset[ 1 ] = getAttributeReal( paramNodePtr, "y" );
00975                                 data->attachOffset[ 2 ] = getAttributeReal( paramNodePtr, "z" );
00976                         }
00977 
00978                         // Load DesiredPos element if it exists.
00979                         paramNodePtr =
00980                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "DesiredPos" );
00981                         if ( NULL != paramNodePtr )
00982                         {
00983                                 data->desiredPos[ 0 ] = getAttributeReal( paramNodePtr, "x" );
00984                                 data->desiredPos[ 1 ] = getAttributeReal( paramNodePtr, "y" );
00985                                 data->desiredPos[ 2 ] = getAttributeReal( paramNodePtr, "z" );
00986                         }
00987 
00988                         // Load DesiredForward element if it exists.
00989                         paramNodePtr =
00990                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "DesiredForward" );
00991                         if ( NULL != paramNodePtr )
00992                         {
00993                                 data->desiredForward[ 0 ] = getAttributeReal( paramNodePtr, "x" );
00994                                 data->desiredForward[ 1 ] = getAttributeReal( paramNodePtr, "y" );
00995                                 data->desiredForward[ 2 ] = getAttributeReal( paramNodePtr, "z" );
00996                         }
00997 
00998                         // Load DesiredUp element if it exists.
00999                         paramNodePtr =
01000                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "DesiredUp" );
01001                         if ( NULL != paramNodePtr )
01002                         {
01003                                 data->desiredUp[ 0 ] = getAttributeReal( paramNodePtr, "x" );
01004                                 data->desiredUp[ 1 ] = getAttributeReal( paramNodePtr, "y" );
01005                                 data->desiredUp[ 2 ] = getAttributeReal( paramNodePtr, "z" );
01006                         }
01007 
01008                         // Load DesiredRight element if it exists.
01009                         paramNodePtr =
01010                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "DesiredRight" );
01011                         if ( NULL != paramNodePtr )
01012                         {
01013                                 data->desiredRight[ 0 ] = getAttributeReal( paramNodePtr, "x" );
01014                                 data->desiredRight[ 1 ] = getAttributeReal( paramNodePtr, "y" );
01015                                 data->desiredRight[ 2 ] = getAttributeReal( paramNodePtr, "z" );
01016                         }
01017 
01018                         // Load LinearKd element if it exists.
01019                         paramNodePtr =
01020                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "LinearKd" );
01021                         if ( NULL != paramNodePtr )
01022                         {
01023                                 data->linearKd = getAttributeReal( paramNodePtr, "value" );
01024                         }
01025 
01026                         // Load LinearKs element if it exists.
01027                         paramNodePtr =
01028                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "LinearKs" );
01029                         if ( NULL != paramNodePtr )
01030                         {
01031                                 data->linearKs = getAttributeReal( paramNodePtr, "value" );
01032                         }
01033 
01034                         // Load AngularKd element if it exists.
01035                         paramNodePtr =
01036                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "AngularKd" );
01037                         if ( NULL != paramNodePtr )
01038                         {
01039                                 data->angularKd = getAttributeReal( paramNodePtr, "value" );
01040                         }
01041 
01042                         // Load AngularKs element if it exists.
01043                         paramNodePtr =
01044                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "AngularKs" );
01045                         if ( NULL != paramNodePtr )
01046                         {
01047                                 data->angularKs = getAttributeReal( paramNodePtr, "value" );
01048                         }
01049 
01050                         // Load References element.  If it doesn't exist, or if there
01051                         // are problems finding the references, ignore this whole Motor.
01052                         paramNodePtr =
01053                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "References" );
01054                         if ( NULL != paramNodePtr )
01055                         {
01056                                 // Get the Solid's name.
01057                                 data->solidBlueprintRefName = getAttributeString( paramNodePtr,
01058                                                               "solid" );
01059                         }
01060                         else
01061                         {
01062                                 OPAL_LOGGER( "warning" ) <<
01063                                 "opal::BlueprintManager::loadSpringMotor: Missing "
01064                                 " References element in " << data->name << " in " <<
01065                                 filename << ".  Ignoring the Motor." << endl;
01066                                 delete data;
01067                                 return NULL;
01068                         }
01069 
01070                         return data;
01071                 }
01072 
01073                 ThrusterMotorData* BlueprintManager::loadThrusterMotor(
01074                     const TiXmlNode* nodePtr, const Blueprint& bp,
01075                     const string& filename )
01076                 {
01077                         // This data structure automatically gets initialized to
01078                         // default values in its constructor.
01079                         ThrusterMotorData * data = new ThrusterMotorData();
01080 
01081                         TiXmlNode* paramNodePtr = NULL;
01082 
01083                         // Load ForceType element if it exists.
01084                         paramNodePtr =
01085                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "ForceType" );
01086                         if ( NULL != paramNodePtr )
01087                         {
01088                                 string forceType = getAttributeString( paramNodePtr,
01089                                                                        "value" );
01090                                 if ( "localForce" == forceType )
01091                                 {
01092                                         data->force.type = LOCAL_FORCE;
01093                                 }
01094                                 else if ( "globalForce" == forceType )
01095                                 {
01096                                         data->force.type = GLOBAL_FORCE;
01097                                 }
01098                                 else if ( "localTorque" == forceType )
01099                                 {
01100                                         data->force.type = LOCAL_TORQUE;
01101                                 }
01102                                 else if ( "globalTorque" == forceType )
01103                                 {
01104                                         data->force.type = GLOBAL_TORQUE;
01105                                 }
01106                                 else if ( "localForceAtLocalPos" == forceType )
01107                                 {
01108                                         data->force.type = LOCAL_FORCE_AT_LOCAL_POS;
01109                                 }
01110                                 else if ( "localForceAtGlobalPos" == forceType )
01111                                 {
01112                                         data->force.type = LOCAL_FORCE_AT_GLOBAL_POS;
01113                                 }
01114                                 else if ( "globalForceAtLocalPos" == forceType )
01115                                 {
01116                                         data->force.type = GLOBAL_FORCE_AT_LOCAL_POS;
01117                                 }
01118                                 else if ( "globalForceAtGlobalPos" == forceType )
01119                                 {
01120                                         data->force.type = GLOBAL_FORCE_AT_GLOBAL_POS;
01121                                 }
01122                                 else
01123                                 {
01124                                         OPAL_LOGGER( "warning" ) <<
01125                                         "opal::BlueprintManager::loadThrusterMotor: Invalid "
01126                                         << "Force type " << forceType << " in " << data->name
01127                                         << " in " << filename << " will be ignored."
01128                                         << endl;
01129                                 }
01130                         }
01131 
01132                         // Load ForceVec element if it exists.
01133                         paramNodePtr =
01134                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "ForceVec" );
01135                         if ( NULL != paramNodePtr )
01136                         {
01137                                 data->force.vec[ 0 ] = getAttributeReal( paramNodePtr, "x" );
01138                                 data->force.vec[ 1 ] = getAttributeReal( paramNodePtr, "y" );
01139                                 data->force.vec[ 2 ] = getAttributeReal( paramNodePtr, "z" );
01140                         }
01141 
01142                         // Load ForcePos element if it exists.
01143                         paramNodePtr =
01144                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "ForcePos" );
01145                         if ( NULL != paramNodePtr )
01146                         {
01147                                 data->force.pos[ 0 ] = getAttributeReal( paramNodePtr, "x" );
01148                                 data->force.pos[ 1 ] = getAttributeReal( paramNodePtr, "y" );
01149                                 data->force.pos[ 2 ] = getAttributeReal( paramNodePtr, "z" );
01150                         }
01151 
01152                         // Load References element.  If it doesn't exist, or if there
01153                         // are problems finding the references, ignore this whole Motor.
01154                         paramNodePtr =
01155                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "References" );
01156                         if ( NULL != paramNodePtr )
01157                         {
01158                                 // Get the Solid's name.
01159                                 data->solidBlueprintRefName = getAttributeString( paramNodePtr,
01160                                                               "solid" );
01161                         }
01162                         else
01163                         {
01164                                 OPAL_LOGGER( "warning" ) <<
01165                                 "opal::BlueprintManager::loadSolidMotor: Missing "
01166                                 << "References element in " << data->name << " in " <<
01167                                 filename << ".  Ignoring the Motor." << endl;
01168                                 delete data;
01169                                 return NULL;
01170                         }
01171 
01172                         return data;
01173                 }
01174 
01175                 SensorData* BlueprintManager::loadSensor( const TiXmlNode* nodePtr,
01176                         const Blueprint& bp, const string& filename )
01177                 {
01178                         // This data structure automatically gets initialized to
01179                         // default values in its constructor.
01180                         SensorData * data = NULL;
01181 
01182                         TiXmlNode* paramNodePtr = NULL;
01183 
01184                         // Load Type element if it exists.
01185                         string type;
01186                         paramNodePtr =
01187                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Type" );
01188                         if ( NULL != paramNodePtr )
01189                         {
01190                                 type = getAttributeString( paramNodePtr, "value" );
01191                         }
01192 
01193                         // Load data for the specific type of Sensor.
01194                         if ( "acceleration" == type )
01195                         {
01196                                 data = loadAccelerationSensor( nodePtr, bp, filename );
01197                         }
01198                         else if ( "incline" == type )
01199                         {
01200                                 data = loadInclineSensor( nodePtr, bp, filename );
01201                         }
01202                         else if ( "raycast" == type )
01203                         {
01204                                 data = loadRaycastSensor( nodePtr, bp, filename );
01205                         }
01206                         else if ( "volume" == type )
01207                         {
01208                                 data = loadVolumeSensor( nodePtr, bp, filename );
01209                         }
01210                         else
01211                         {
01212                                 OPAL_LOGGER( "warning" ) <<
01213                                 "opal::BlueprintManager::loadSensor: Invalid Sensor \
01214                                 type " << type << " in " << filename
01215                                 << ". Ignoring the Sensor." << endl;
01216                                 return NULL;
01217                         }
01218 
01219                         // Load Name element if it exists.
01220                         paramNodePtr =
01221                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Name" );
01222                         if ( NULL != paramNodePtr )
01223                         {
01224                                 data->name = getAttributeString( paramNodePtr, "value" );
01225                         }
01226 
01227                         // Load Enabled element if it exists.
01228                         paramNodePtr =
01229                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Enabled" );
01230                         if ( NULL != paramNodePtr )
01231                         {
01232                                 string value = getAttributeString( paramNodePtr, "value" );
01233                                 if ( "true" == value )
01234                                 {
01235                                         data->enabled = true;
01236                                 }
01237                                 else if ( "false" == value )
01238                                 {
01239                                         data->enabled = false;
01240                                 }
01241                                 else
01242                                 {
01243                                         OPAL_LOGGER( "warning" ) <<
01244                                         "opal::BlueprintManager::loadSensor: Invalid value "
01245                                         << value << " for Sensor Enabled parameter in "
01246                                         << filename << " will be ignored." << endl;
01247                                 }
01248                         }
01249 
01250                         // Load Offset element if it exists.
01251                         TiXmlNode* offsetNodePtr =
01252                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Offset" );
01253                         if ( NULL != offsetNodePtr )
01254                         {
01255                                 loadOffset( data->transform, offsetNodePtr, filename );
01256                         }
01257 
01258                         // Load References element.  If it doesn't exist, just assume
01259                         // that this Sensor will not be attached to a Solid.
01260                         paramNodePtr =
01261                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "References" );
01262                         if ( NULL != paramNodePtr )
01263                         {
01264                                 // Get the Solids' names.
01265                                 data->solidBlueprintRefName = getAttributeString( paramNodePtr,
01266                                                               "solid" );
01267                         }
01268                         else
01269                         {
01270                                 data->solidBlueprintRefName = "";
01271                         }
01272 
01273                         return data;
01274                 }
01275 
01276                 AccelerationSensorData* BlueprintManager::loadAccelerationSensor(
01277                     const TiXmlNode* nodePtr, const Blueprint& bp,
01278                     const string& filename )
01279                 {
01280                         // This data structure automatically gets initialized to
01281                         // default values in its constructor.
01282                         AccelerationSensorData * data = new AccelerationSensorData();
01283 
01284                         // Nothing special to load for AccelerationSensors.
01285 
01286                         return data;
01287                 }
01288 
01289                 InclineSensorData* BlueprintManager::loadInclineSensor(
01290                     const TiXmlNode* nodePtr, const Blueprint& bp,
01291                     const string& filename )
01292                 {
01293                         // This data structure automatically gets initialized to
01294                         // default values in its constructor.
01295                         InclineSensorData * data = new InclineSensorData();
01296 
01297                         TiXmlNode* paramNodePtr = NULL;
01298 
01299                         // Load Ray element if it exists.
01300                         paramNodePtr =
01301                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Axis" );
01302                         if ( NULL != paramNodePtr )
01303                         {
01304                                 Vec3r axis;
01305                                 axis[ 0 ] = getAttributeReal( paramNodePtr, "x" );
01306                                 axis[ 1 ] = getAttributeReal( paramNodePtr, "y" );
01307                                 axis[ 2 ] = getAttributeReal( paramNodePtr, "z" );
01308                                 data->axis = axis;
01309                         }
01310 
01311                         return data;
01312                 }
01313 
01314                 RaycastSensorData* BlueprintManager::loadRaycastSensor(
01315                     const TiXmlNode* nodePtr, const Blueprint& bp,
01316                     const string& filename )
01317                 {
01318                         // This data structure automatically gets initialized to
01319                         // default values in its constructor.
01320                         RaycastSensorData * data = new RaycastSensorData();
01321 
01322                         TiXmlNode* paramNodePtr = NULL;
01323 
01324                         // Load Ray element if it exists.
01325                         paramNodePtr =
01326                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "Ray" );
01327                         if ( NULL != paramNodePtr )
01328                         {
01329                                 Point3r origin;
01330                                 Vec3r dir;
01331                                 origin[ 0 ] = getAttributeReal( paramNodePtr, "originx" );
01332                                 origin[ 1 ] = getAttributeReal( paramNodePtr, "originy" );
01333                                 origin[ 2 ] = getAttributeReal( paramNodePtr, "originz" );
01334                                 dir[ 0 ] = getAttributeReal( paramNodePtr, "dirx" );
01335                                 dir[ 1 ] = getAttributeReal( paramNodePtr, "diry" );
01336                                 dir[ 2 ] = getAttributeReal( paramNodePtr, "dirz" );
01337                                 data->ray.setOrigin( origin );
01338                                 data->ray.setDir( dir );
01339                         }
01340 
01341                         // Load ContactGroup element if it exists.
01342                         paramNodePtr =
01343                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "ContactGroup" );
01344                         if ( NULL != paramNodePtr )
01345                         {
01346                                 data->contactGroup =
01347                                     ( unsigned int ) getAttributeReal(
01348                                         paramNodePtr, "value" );
01349                         }
01350 
01351                         return data;
01352                 }
01353 
01354                 VolumeSensorData* BlueprintManager::loadVolumeSensor(
01355                     const TiXmlNode* nodePtr, const Blueprint& bp,
01356                     const string& filename )
01357                 {
01358                         // This data structure automatically gets initialized to
01359                         // default values in its constructor.
01360                         VolumeSensorData * data = new VolumeSensorData();
01361 
01362                         // Nothing special to load for VolumeSensors.
01363 
01364                         return data;
01365                 }
01366 
01367                 ShapeData* BlueprintManager::loadShape( const TiXmlNode* nodePtr,
01368                                                         const string& filename )
01369                 {
01370                         // This data structure automatically gets initialized to
01371                         // default values in its constructor.
01372                         ShapeData * data = NULL;
01373 
01374                         string type = getAttributeString( nodePtr, "type" );
01375 
01376                         // Allocate the specific ShapeData type, then load its
01377                         // dimensions.
01378                         TiXmlNode* dimensionsNodePtr = const_cast<TiXmlNode*>
01379                                                        ( nodePtr ) ->FirstChild( "Dimensions" );
01380 
01381                         if ( "box" == type )
01382                         {
01383                                 BoxShapeData * boxData = new BoxShapeData();
01384                                 data = boxData;
01385                                 boxData->dimensions[ 0 ] =
01386                                     getAttributeReal( dimensionsNodePtr, "x" );
01387                                 boxData->dimensions[ 1 ] =
01388                                     getAttributeReal( dimensionsNodePtr, "y" );
01389                                 boxData->dimensions[ 2 ] =
01390                                     getAttributeReal( dimensionsNodePtr, "z" );
01391                         }
01392                         else if ( "sphere" == type )
01393                         {
01394                                 SphereShapeData * sphereData = new SphereShapeData();
01395                                 data = sphereData;
01396                                 sphereData->radius =
01397                                     getAttributeReal( dimensionsNodePtr, "radius" );
01398                         }
01399                         else if ( "capsule" == type )
01400                         {
01401                                 CapsuleShapeData * capsuleData = new CapsuleShapeData();
01402                                 data = capsuleData;
01403                                 capsuleData->radius =
01404                                     getAttributeReal( dimensionsNodePtr, "radius" );
01405                                 capsuleData->length =
01406                                     getAttributeReal( dimensionsNodePtr, "length" );
01407                         }
01408                         else if ( "plane" == type )
01409                         {
01410                                 PlaneShapeData * planeData = new PlaneShapeData();
01411                                 data = planeData;
01412                                 planeData->abcd[ 0 ] = getAttributeReal( dimensionsNodePtr, "a" );
01413                                 planeData->abcd[ 1 ] = getAttributeReal( dimensionsNodePtr, "b" );
01414                                 planeData->abcd[ 2 ] = getAttributeReal( dimensionsNodePtr, "c" );
01415                                 planeData->abcd[ 3 ] = getAttributeReal( dimensionsNodePtr, "d" );
01416                         }
01417                         //else if ("ray" == type)
01418                         //{
01419                         //  RayShapeData* rayData = new RayShapeData();
01420                         //  data = rayData;
01421                         //  Point3r origin;
01422                         //  origin[0] = getAttributeReal(dimensionsNodePtr, "originx");
01423                         //  origin[1] = getAttributeReal(dimensionsNodePtr, "originy");
01424                         //  origin[2] = getAttributeReal(dimensionsNodePtr, "originz");
01425                         //  rayData->ray.setOrigin(origin);
01426                         //  Vec3r dir;
01427                         //  dir[0] = getAttributeReal(dimensionsNodePtr, "dirx");
01428                         //  dir[1] = getAttributeReal(dimensionsNodePtr, "diry");
01429                         //  dir[2] = getAttributeReal(dimensionsNodePtr, "dirz");
01430                         //  rayData->ray.setDir(dir);
01431                         //}
01432                         // MESH_SHAPE type not supported in OPAL XML.
01433                         else
01434                         {
01435                                 OPAL_LOGGER( "warning" ) <<
01436                                 "opal::BlueprintManager::loadShape: Invalid Shape type "
01437                                 << type << " in " << filename << " will be ignored."
01438                                 << endl;
01439                                 return NULL;
01440                         }
01441 
01442                         // Load Offset element if it exists.
01443                         TiXmlNode* offsetNodePtr = const_cast<TiXmlNode*>
01444                                                    ( nodePtr ) ->FirstChild( "Offset" );
01445                         if ( NULL != offsetNodePtr )
01446                         {
01447                                 loadOffset( data->offset, offsetNodePtr, filename );
01448                         }
01449 
01450                         // Load Material element if it exists.
01451                         TiXmlNode* materialNodePtr = const_cast<TiXmlNode*>
01452                                                      ( nodePtr ) ->FirstChild( "Material" );
01453                         if ( NULL != materialNodePtr )
01454                         {
01455                                 data->material.hardness =
01456                                     getAttributeReal( materialNodePtr, "hardness" );
01457                                 data->material.friction =
01458                                     getAttributeReal( materialNodePtr, "friction" );
01459                                 data->material.bounciness =
01460                                     getAttributeReal( materialNodePtr, "bounciness" );
01461                                 data->material.density =
01462                                     getAttributeReal( materialNodePtr, "density" );
01463                         }
01464 
01465                         // Load ContactGroup element if it exists.
01466                         TiXmlNode* contactGroupNodePtr =
01467                             const_cast<TiXmlNode*>( nodePtr ) ->FirstChild( "ContactGroup" );
01468                         if ( NULL != contactGroupNodePtr )
01469                         {
01470                                 data->contactGroup =
01471                                     ( unsigned int ) getAttributeReal(
01472                                         contactGroupNodePtr, "value" );
01473                         }
01474 
01475                         return data;
01476                 }
01477 
01478                 void BlueprintManager::loadOffset( Matrix44r& offset,
01479                                                    const TiXmlNode* nodePtr, const string& filename )
01480                 {
01481                         // Loop over all Transform sub-elements.
01482                         TiXmlNode * transformNodePtr = NULL;
01483                         while ( ( transformNodePtr = const_cast<TiXmlNode*>
01484                                                      ( nodePtr ) ->IterateChildren( "Transform", transformNodePtr ) ) != NULL )
01485                         {
01486                                 string type = getAttributeString( transformNodePtr,
01487                                                                   "type" );
01488 
01489                                 if ( "translate" == type )
01490                                 {
01491                                         real x = getAttributeReal( transformNodePtr, "x" );
01492                                         real y = getAttributeReal( transformNodePtr, "y" );
01493                                         real z = getAttributeReal( transformNodePtr, "z" );
01494                                         offset.translate( x, y, z );
01495                                 }
01496                                 else if ( "rotate" == type )
01497                                 {
01498                                         real angle = getAttributeReal( transformNodePtr, "angle" );
01499                                         real x = getAttributeReal( transformNodePtr, "x" );
01500                                         real y = getAttributeReal( transformNodePtr, "y" );
01501                                         real z = getAttributeReal( transformNodePtr, "z" );
01502                                         offset.rotate( angle, x, y, z );
01503                                 }
01504                                 else if ( "euler" == type )
01505                                 {
01506                                         real x = getAttributeReal( transformNodePtr, "x" );
01507                                         real y = getAttributeReal( transformNodePtr, "y" );
01508                                         real z = getAttributeReal( transformNodePtr, "z" );
01509 
01510                                         //Vec3r v(x, y, z);
01511                                         //real angle = v.length();
01512                                         //if (0 != angle)
01513                                         //{
01514                                         //  v.normalize();
01515                                         //  offset.rotate(angle, v[0], v[1], v[2]);
01516                                         //}
01517 
01518                                         offset.rotate( z, 0, 0, 1 );
01519                                         offset.rotate( y, 0, 1, 0 );
01520                                         offset.rotate( x, 1, 0, 0 );
01521                                 }
01522                                 else if ( "matrix" == type )
01523                                 {
01524                                         real data[ 16 ];
01525                                         data[ 0 ] = getAttributeReal( transformNodePtr, "_00" );
01526                                         data[ 1 ] = getAttributeReal( transformNodePtr, "_01" );
01527                                         data[ 2 ] = getAttributeReal( transformNodePtr, "_02" );
01528                                         data[ 3 ] = getAttributeReal( transformNodePtr, "_03" );
01529                                         data[ 4 ] = getAttributeReal( transformNodePtr, "_10" );
01530                                         data[ 5 ] = getAttributeReal( transformNodePtr, "_11" );
01531                                         data[ 6 ] = getAttributeReal( transformNodePtr, "_12" );
01532                                         data[ 7 ] = getAttributeReal( transformNodePtr, "_13" );
01533                                         data[ 8 ] = getAttributeReal( transformNodePtr, "_20" );
01534                                         data[ 9 ] = getAttributeReal( transformNodePtr, "_21" );
01535                                         data[ 10 ] = getAttributeReal( transformNodePtr, "_22" );
01536                                         data[ 11 ] = getAttributeReal( transformNodePtr, "_23" );
01537                                         data[ 12 ] = getAttributeReal( transformNodePtr, "_30" );
01538                                         data[ 13 ] = getAttributeReal( transformNodePtr, "_31" );
01539                                         data[ 14 ] = getAttributeReal( transformNodePtr, "_32" );
01540                                         data[ 15 ] = getAttributeReal( transformNodePtr, "_33" );
01541                                         offset.set( data );
01542                                 }
01543                                 else
01544                                 {
01545                                         OPAL_LOGGER( "warning" ) <<
01546                                         "opal::BlueprintManager::loadOffset: Invalid "
01547                                         << "transform type " << type << " in " << filename
01548                                         << endl;
01549                                 }
01550                         }
01551                 }
01552 
01553                 real BlueprintManager::getAttributeReal( const TiXmlNode* nodePtr,
01554                         const string& name ) const
01555                 {
01556                         TiXmlElement * elementPtr =
01557                             const_cast<TiXmlNode*>( nodePtr ) ->ToElement();
01558                         double temp = 0;
01559 
01560                         if ( NULL == elementPtr->Attribute( name.c_str(), &temp ) )
01561                         {
01562                                 OPAL_LOGGER( "warning" ) <<
01563                                 "opal::BlueprintManager::getAttributeReal: Element "
01564                                 << nodePtr->Value() << " is missing attribute '"
01565                                 << name << "'.  Parameter will be set to 0.0."
01566                                 << endl;
01567                                 return 0;
01568                         }
01569                         else
01570                         {
01571                                 return ( real ) temp;
01572                         }
01573                 }
01574 
01575                 string BlueprintManager::getAttributeString(
01576                     const TiXmlNode* nodePtr, const string& name ) const
01577                 {
01578                         TiXmlElement * elementPtr =
01579                             const_cast<TiXmlNode*>( nodePtr ) ->ToElement();
01580                         const char* temp = elementPtr->Attribute( name.c_str() );
01581 
01582                         if ( NULL == temp )
01583                         {
01584                                 OPAL_LOGGER( "warning" ) <<
01585                                 "opal::BlueprintManager::getAttributeString: Element "
01586                                 << nodePtr->Value() << " is missing attribute '"
01587                                 << name << "'.  Parameter will be set to \"\"."
01588                                 << endl;
01589                                 return "";
01590                         }
01591                         else
01592                         {
01593                                 return temp;
01594                         }
01595                 }
01596         }
01597 }

Generated on Tue May 16 17:49:50 2006 for OPAL by  doxygen 1.4.6-NO