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

physics/SpatialGroup.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2003 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: SpatialGroup.cpp 1031 2004-02-11 20:46:36Z jungd $
00019   $Revision: 1.2 $
00020   $Date: 2004-02-11 15:46:36 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <physics/SpatialGroup>
00026 
00027 using physics::SpatialGroup;
00028 
00029 using base::Point3;
00030 using base::Orient;
00031 using base::Transform;
00032 
00033 
00034 SpatialGroup::SpatialGroup()
00035   : implicit(false)
00036 {
00037 }
00038 
00039 SpatialGroup::SpatialGroup(const String& name)
00040   : implicit(false)
00041 {
00042   setName(name);
00043 }
00044 
00045 SpatialGroup::SpatialGroup(const SpatialGroup& sg)
00046  : Spatial(sg), spatials(sg.spatials), implicit(sg.implicit)
00047 {
00048   if (!implicit) {
00049     position = sg.position;
00050     orient = sg.orient;
00051   }
00052   else
00053     spatial = sg.spatial;
00054     
00055 }
00056 
00057 
00058 SpatialGroup::~SpatialGroup()
00059 {
00060 }
00061 
00062 
00063 SpatialGroup& SpatialGroup::operator=(const SpatialGroup& sg)
00064 {
00065   spatials = sg.spatials;
00066   implicit = sg.implicit;
00067   if (!sg.implicit) {
00068     position = sg.position;
00069     orient = sg.orient;
00070   }
00071   else
00072     spatial = sg.spatial;
00073   
00074   setName(sg.getName());
00075   return *this;
00076 } 
00077 
00078 
00079 void SpatialGroup::setImplicitConfiguration(ref<Spatial> spatial)
00080 {
00081   implicit = true;
00082   this->spatial = spatial;
00083 }
00084 
00085 void SpatialGroup::setExplicitConfiguration(const base::Transform& configuration)
00086 {
00087   implicit = false;
00088   spatial = ref<Spatial>(0);
00089   position = configuration.getTranslation();
00090   orient = configuration.getRotation();
00091 }
00092 
00093   
00094  
00095 void SpatialGroup::add(ref<Spatial> spatial)
00096 {
00097   Assert(spatial != 0);
00098   spatials.push_back(spatial); 
00099 }
00100 
00101 void SpatialGroup::remove(ref<Spatial> spatial)
00102 {
00103   Assert(spatial != 0);
00104   spatials.remove(spatial);
00105 }
00106 
00107 
00108 void SpatialGroup::clear()
00109 {
00110   spatials.clear();
00111 }
00112 
00113 
00114 void SpatialGroup::setConfiguration(const base::Transform& configuration)
00115 {
00116   // for each Spatial, compute its relative position to us (i.e. its
00117   //  position in the coord. frame with origin (position, orient) (or spatial's configuration if implicit)
00118   // Then move it to a new world pos,orient such that its relative
00119   //  position is preserved in relation to our updated configuration
00120   Transform oldconfig( getPosition(), getOrientation() );
00121   if (configuration == oldconfig) return; // nothing to do
00122   Transform update = configuration*inverse(oldconfig);
00123   
00124   SpatialList::iterator s = spatials.begin();
00125   SpatialList::iterator end = spatials.end();
00126   while (s != end) {
00127     (*s)->setConfiguration( update * (*s)->getConfiguration() );
00128     s++;
00129   }
00130 
00131   if (!implicit) {
00132     position = configuration.getTranslation();
00133     orient = configuration.getRotation();
00134   }
00135 }
00136 
00137 
00138 void SpatialGroup::setPosition(const Point3& pos)
00139 {
00140   setConfiguration( Transform(pos, getOrientation()) );
00141 }
00142  
00143  
00144 Point3 SpatialGroup::getPosition() const
00145 {
00146   if (!implicit)
00147     return position;
00148   else
00149     return spatial->getPosition();
00150 }
00151  
00152  
00153 void SpatialGroup::setOrientation(const Orient& orient)
00154 {
00155   setConfiguration( Transform(getPosition(), orient) );
00156 }
00157  
00158  
00159 Orient SpatialGroup::getOrientation() const
00160 {
00161   if (!implicit)
00162     return orient;
00163   else
00164     return spatial->getOrientation();
00165 }
00166 
00167 
00168 void SpatialGroup::updateGroupPositionOrientation(const Point3& pos, const Orient& orient)
00169 {
00170   if (!implicit) {
00171     position = pos;
00172     this->orient = orient;
00173   }
00174   else
00175     spatial->setPositionOrientation(pos, orient);
00176 }
00177 
00178 
00179 void SpatialGroup::updateGroupConfiguration(const base::Transform& configuration)
00180 {
00181   if (!implicit) {
00182     position = configuration.getTranslation();
00183     orient = configuration.getRotation();
00184   }
00185   else
00186     spatial->setConfiguration(configuration);
00187 }
00188 
00189 

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