Blueprint.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 #include "Blueprint.h"
00029 #include "SolidData.h"
00030 #include "JointData.h"
00031 #include "MotorData.h"
00032 #include "AttractorMotorData.h"
00033 #include "GearedMotorData.h"
00034 #include "ServoMotorData.h"
00035 #include "SpringMotorData.h"
00036 #include "ThrusterMotorData.h"
00037 #include "SensorData.h"
00038 #include "AccelerationSensorData.h"
00039 #include "InclineSensorData.h"
00040 #include "RaycastSensorData.h"
00041 #include "VolumeSensorData.h"
00042 
00043 namespace opal
00044 {
00045         Blueprint::Blueprint()
00046         {
00047                 mFinalized = false;
00048         }
00049 
00050         Blueprint::~Blueprint()
00051         {
00052                 while (!mSolidList.empty())
00053                 {
00054                         delete mSolidList.back();
00055                         mSolidList.pop_back();
00056                 }
00057 
00058                 while (!mJointList.empty())
00059                 {
00060                         delete mJointList.back();
00061                         mJointList.pop_back();
00062                 }
00063 
00064                 while (!mMotorList.empty())
00065                 {
00066                         delete mMotorList.back();
00067                         mMotorList.pop_back();
00068                 }
00069 
00070                 while (!mSensorList.empty())
00071                 {
00072                         delete mSensorList.back();
00073                         mSensorList.pop_back();
00074                 }
00075         }
00076 
00077         void Blueprint::finalize()
00078         {
00079                 // Setup Joint reference indices.
00080                 size_t i;
00081                 for (i=0; i<mJointList.size();)
00082                 {
00083                         JointData* data = mJointList.at(i);
00084 
00085                         // Find the indices of the references.
00086                         int solid0Index = getSolidIndex(data->solid0BlueprintRefName);
00087                         int solid1Index = getSolidIndex(data->solid1BlueprintRefName);
00088 
00089                         // Here we allow one of the Solid indices to be -1 in case the 
00090                         // user is attaching the Joint to the static environment.
00091                         if (-1 == solid0Index && -1 == solid1Index)
00092                         {
00093                                 OPAL_LOGGER("warning") << 
00094                                         "opal::Blueprint::finalize: Invalid references "
00095                                         << "in Joint " << data->name << ". Removing the Joint "
00096                                         << "from the Blueprint." << std::endl;
00097 
00098                                 delete data;
00099                                 mJointList.at(i) = mJointList.back();
00100                                 mJointList.pop_back();
00101                         }
00102                         else
00103                         {
00104                                 data->internal_solid0Index = solid0Index;
00105                                 data->internal_solid1Index = solid1Index;
00106                                 ++i;
00107                         }
00108                 }
00109 
00110                 // Setup Motor reference indices.
00111                 for (i=0; i<mMotorList.size();)
00112                 {
00113                         MotorData* m = mMotorList.at(i);
00114                         bool error = false;
00115 
00116                         switch(m->getType())
00117                         {
00118                                 case ATTRACTOR_MOTOR:
00119                                 {
00120                                         AttractorMotorData* data = 
00121                                                 (AttractorMotorData*)mMotorList.at(i);
00122                                         int solid0Index = 
00123                                                 getSolidIndex(data->solid0BlueprintRefName);
00124                                         int solid1Index = 
00125                                                 getSolidIndex(data->solid1BlueprintRefName);
00126                                         if (-1 == solid0Index || -1 == solid1Index)
00127                                         {
00128                                                 error = true;
00129                                         }
00130                                         else
00131                                         {
00132                                                 data->internal_solid0Index = solid0Index;
00133                                                 data->internal_solid1Index = solid1Index;
00134                                         }
00135                                         break;
00136                                 }
00137                                 case GEARED_MOTOR:
00138                                 {
00139                                         GearedMotorData* data = 
00140                                                 (GearedMotorData*)mMotorList.at(i);
00141                                         int jointIndex = getJointIndex(data->jointBlueprintRefName);
00142                                         if (-1 == jointIndex)
00143                                         {
00144                                                 error = true;
00145                                         }
00146                                         else
00147                                         {
00148                                                 data->internal_jointIndex = jointIndex;
00149                                         }
00150                                         break;
00151                                 }
00152                                 case SERVO_MOTOR:
00153                                 {
00154                                         ServoMotorData* data = (ServoMotorData*)mMotorList.at(i);
00155                                         int jointIndex = getJointIndex(data->jointBlueprintRefName);
00156                                         if (-1 == jointIndex)
00157                                         {
00158                                                 error = true;
00159                                         }
00160                                         else
00161                                         {
00162                                                 data->internal_jointIndex = jointIndex;
00163                                         }
00164                                         break;
00165                                 }
00166                                 case SPRING_MOTOR:
00167                                 {
00168                                         SpringMotorData* data = 
00169                                                 (SpringMotorData*)mMotorList.at(i);
00170                                         int solidIndex = getSolidIndex(data->solidBlueprintRefName);
00171                                         if (-1 == solidIndex)
00172                                         {
00173                                                 error = true;
00174                                         }
00175                                         else
00176                                         {
00177                                                 data->internal_solidIndex = solidIndex;
00178                                         }
00179                                         break;
00180                                 }
00181                                 case THRUSTER_MOTOR:
00182                                 {
00183                                         ThrusterMotorData* data = 
00184                                                 (ThrusterMotorData*)mMotorList.at(i);
00185                                         int solidIndex = getSolidIndex(data->solidBlueprintRefName);
00186                                         if (-1 == solidIndex)
00187                                         {
00188                                                 error = true;
00189                                         }
00190                                         else
00191                                         {
00192                                                 data->internal_solidIndex = solidIndex;
00193                                         }
00194                                         break;
00195                                 }
00196                                 default:
00197                                         assert(false);
00198                         }
00199 
00200                         if (error)
00201                         {
00202                                 OPAL_LOGGER("warning") << 
00203                                         "opal::Blueprint::finalize: Invalid reference in Motor "
00204                                         << mMotorList.at(i)->name 
00205                                         << ". Removing the Motor from the Blueprint." 
00206                                         << std::endl;
00207 
00208                                 delete mMotorList.at(i);
00209                                 mMotorList.at(i) = mMotorList.back();
00210                                 mMotorList.pop_back();
00211                         }
00212                         else
00213                         {
00214                                 ++i;
00215                         }
00216                 }
00217 
00218                 // Setup Sensor reference indices.
00219                 for (i=0; i<mSensorList.size(); ++i)
00220                 {
00221                         SensorData* data = mSensorList.at(i);
00222 
00223                         // At this time we have no Sensors with special references 
00224                         // besides the standard Solid reference present in every 
00225                         // Sensor.  Thus we can just treat them all the same.
00226 
00227                         // Find the index of the Solid reference.  If the value of 
00228                         // the index is -1, the Sensor must not be attached to any 
00229                         // Solid.
00230                         int solidIndex = getSolidIndex(data->solidBlueprintRefName);
00231                         data->internal_solidIndex = solidIndex;
00232                 }
00233 
00234                 mFinalized = true;
00235         }
00236 
00237         bool Blueprint::isFinalized()const
00238         {
00239                 return mFinalized;
00240         }
00241 
00242         void Blueprint::addSolid(SolidData* data)
00243         {
00244                 if (mFinalized)
00245                 {
00246                         printFinalizedMessage();
00247                         return;
00248                 }
00249 
00250                 if (solidExists(data->name))
00251                 {
00252                         OPAL_LOGGER("warning") << "opal::Blueprint::addSolid: Solid " 
00253                                 << data->name << " already exists in this Blueprint.  \
00254 Ignoring the new one." << std::endl;
00255                 }
00256                 else
00257                 {
00258                         SolidData* newData = new SolidData(*data);
00259                         mSolidList.push_back(newData);
00260                 }
00261         }
00262 
00263         void Blueprint::addJoint(JointData* data)
00264         {
00265                 if (mFinalized)
00266                 {
00267                         printFinalizedMessage();
00268                         return;
00269                 }
00270 
00271                 if (jointExists(data->name))
00272                 {
00273                         OPAL_LOGGER("warning") << "opal::Blueprint::addJoint: Joint " 
00274                                 << data->name << " already exists in this Blueprint.  \
00275 Ignoring the new one." << std::endl;
00276                 }
00277                 else
00278                 {
00279                         JointData* newData = new JointData(*data);
00280                         mJointList.push_back(newData);
00281                 }
00282         }
00283 
00284         void Blueprint::addMotor(MotorData* data)
00285         {
00286                 if (mFinalized)
00287                 {
00288                         printFinalizedMessage();
00289                         return;
00290                 }
00291 
00292                 if (motorExists(data->name))
00293                 {
00294                         OPAL_LOGGER("warning") << "opal::Blueprint::addMotor: Motor " 
00295                                 << data->name << " already exists in this Blueprint.  \
00296 Ignoring the new one." << std::endl;
00297                 }
00298                 else
00299                 {
00300                         MotorData* newData = NULL;
00301 
00302                         // Allocate the right type of Motor.
00303                         switch(data->getType())
00304                         {
00305                                 case ATTRACTOR_MOTOR:
00306                                         newData = 
00307                                                 new AttractorMotorData(*((AttractorMotorData*)data));
00308                                         break;
00309                                 case GEARED_MOTOR:
00310                                         newData = 
00311                                                 new GearedMotorData(*((GearedMotorData*)data));
00312                                         break;
00313                                 case SERVO_MOTOR:
00314                                         newData = 
00315                                                 new ServoMotorData(*((ServoMotorData*)data));
00316                                         break;
00317                                 case SPRING_MOTOR:
00318                                         newData = 
00319                                                 new SpringMotorData(*((SpringMotorData*)data));
00320                                         break;
00321                                 case THRUSTER_MOTOR:
00322                                         newData = 
00323                                                 new ThrusterMotorData(*((ThrusterMotorData*)data));
00324                                         break;
00325                                 default:
00326                                         assert(false);
00327                         }
00328 
00329                         mMotorList.push_back(newData);
00330                 }
00331         }
00332 
00333         void Blueprint::addSensor(SensorData* data)
00334         {
00335                 if (mFinalized)
00336                 {
00337                         printFinalizedMessage();
00338                         return;
00339                 }
00340 
00341                 if (sensorExists(data->name))
00342                 {
00343                         OPAL_LOGGER("warning") << "opal::Blueprint::addSensor: Sensor " 
00344                                 << data->name << " already exists in this Blueprint.  \
00345 Ignoring the new one." << std::endl;
00346                 }
00347                 else
00348                 {
00349                         SensorData* newData = NULL;
00350 
00351                         // Allocate the right type of Sensor.
00352                         switch(data->getType())
00353                         {
00354                                 case ACCELERATION_SENSOR:
00355                                 {
00356                                         newData = 
00357                                                 new AccelerationSensorData(
00358                                                 *((AccelerationSensorData*)data));
00359                                         break;
00360                                 }
00361                                 case INCLINE_SENSOR:
00362                                 {
00363                                         newData = 
00364                                                 new InclineSensorData(*((InclineSensorData*)data));
00365                                         break;
00366                                 }
00367                                 case RAYCAST_SENSOR:
00368                                 {
00369                                         newData = 
00370                                                 new RaycastSensorData(*((RaycastSensorData*)data));
00371                                         break;
00372                                 }
00373                                 case VOLUME_SENSOR:
00374                                 {
00375                                         newData = 
00376                                                 new VolumeSensorData(*((VolumeSensorData*)data));
00377                                         break;
00378                                 }
00379                                 default:
00380                                         assert(false);
00381                         }
00382 
00383                         mSensorList.push_back(newData);
00384                 }
00385         }
00386 
00387         unsigned int Blueprint::getNumSolids()const
00388         {
00389                 return (int)(mSolidList.size());
00390         }
00391 
00392         unsigned int Blueprint::getNumJoints()const
00393         {
00394                 return (int)(mJointList.size());
00395         }
00396 
00397         unsigned int Blueprint::getNumMotors()const
00398         {
00399                 return (int)(mMotorList.size());
00400         }
00401 
00402         unsigned int Blueprint::getNumSensors()const
00403         {
00404                 return (int)(mSensorList.size());
00405         }
00406 
00407         SolidData* Blueprint::getSolidData(unsigned int i)const
00408         {
00409                 return mSolidList.at(i);
00410         }
00411 
00412         JointData* Blueprint::getJointData(unsigned int i)const
00413         {
00414                 return mJointList.at(i);
00415         }
00416 
00417         MotorData* Blueprint::getMotorData(unsigned int i)const
00418         {
00419                 return mMotorList.at(i);
00420         }
00421 
00422         SensorData* Blueprint::getSensorData(unsigned int i)const
00423         {
00424                 return mSensorList.at(i);
00425         }
00426 
00427         bool Blueprint::solidExists(const std::string& name)
00428         {
00430                 if (name.empty())
00431                 {
00432                         return false;
00433                 }
00434 
00435                 std::vector<SolidData*>::iterator iter;
00436                 for (iter = mSolidList.begin(); iter != mSolidList.end(); ++iter)
00437                 {
00438                         if ((*iter)->name == name)
00439                         {
00440                                 return true;
00441                         }
00442                 }
00443 
00444                 return false;
00445         }
00446 
00447         bool Blueprint::jointExists(const std::string& name)
00448         {
00450                 if (name.empty())
00451                 {
00452                         return false;
00453                 }
00454 
00455                 std::vector<JointData*>::iterator iter;
00456                 for (iter = mJointList.begin(); iter != mJointList.end(); ++iter)
00457                 {
00458                         if ((*iter)->name == name)
00459                         {
00460                                 return true;
00461                         }
00462                 }
00463 
00464                 return false;
00465         }
00466 
00467         bool Blueprint::motorExists(const std::string& name)
00468         {
00470                 if (name.empty())
00471                 {
00472                         return false;
00473                 }
00474 
00475                 std::vector<MotorData*>::iterator iter;
00476                 for (iter = mMotorList.begin(); iter != mMotorList.end(); ++iter)
00477                 {
00478                         if ((*iter)->name == name)
00479                         {
00480                                 return true;
00481                         }
00482                 }
00483 
00484                 return false;
00485         }
00486 
00487         bool Blueprint::sensorExists(const std::string& name)
00488         {
00490                 if (name.empty())
00491                 {
00492                         return false;
00493                 }
00494 
00495                 std::vector<SensorData*>::iterator iter;
00496                 for (iter = mSensorList.begin(); iter != mSensorList.end(); ++iter)
00497                 {
00498                         if ((*iter)->name == name)
00499                         {
00500                                 return true;
00501                         }
00502                 }
00503 
00504                 return false;
00505         }
00506 
00507         int Blueprint::getSolidIndex(const std::string& name)const
00508         {
00509                 if (name.empty())
00510                 {
00511                         return -1;
00512                 }
00513 
00514                 for (unsigned int i=0; i<mSolidList.size(); ++i)
00515                 {
00516                         if (mSolidList[i]->name == name)
00517                         {
00518                                 return (int)i;
00519                         }
00520                 }
00521 
00522                 return -1;
00523         }
00524 
00525         int Blueprint::getJointIndex(const std::string& name)const
00526         {
00527                 if (name.empty())
00528                 {
00529                         return -1;
00530                 }
00531 
00532                 for (unsigned int i=0; i<mJointList.size(); ++i)
00533                 {
00534                         if (mJointList[i]->name == name)
00535                         {
00536                                 return (int)i;
00537                         }
00538                 }
00539 
00540                 return -1;
00541         }
00542 
00543         void Blueprint::printFinalizedMessage()
00544         {
00545                 OPAL_LOGGER("warning") << "Cannot add objects to a finalized \
00546 Blueprint.  The operation will be ignored." << std::endl;
00547         }
00548 }

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