jspace/jspace/State.cpp

Go to the documentation of this file.
00001 /*
00002  * Stanford Whole-Body Control Framework http://stanford-wbc.sourceforge.net/
00003  *
00004  * Copyright (C) 2010 The Board of Trustees of The Leland Stanford Junior University. All rights reserved.
00005  *
00006  * This program is free software: you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation, either version 3 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this program.  If not, see
00018  * <http://www.gnu.org/licenses/>
00019  */
00020 
00026 #include "State.hpp"
00027 #include "vector_util.hpp"
00028 #include <string.h>
00029 
00030 
00031 namespace jspace {
00032   
00033   
00034   State::
00035   State()
00036   {
00037     init(0, 0, 0);
00038   }
00039   
00040   
00041   State::
00042   State(State const & orig)
00043   {
00044     *this = orig;
00045   }
00046   
00047   
00048   State::
00049   State(size_t npos, size_t nvel, size_t nforce)
00050   {
00051     init(npos, nvel, nforce);
00052   }
00053   
00054   
00055   void State::
00056   init(size_t npos, size_t nvel, size_t nforce)
00057   {
00058     time_sec_ = 0;
00059     time_usec_ = 0;
00060     // Grrr, Eigen2 does not gracefully handle MatrixBase::Zero(0);
00061     if (0 == npos) {
00062       position_.resize(0);
00063     }
00064     else {
00065       position_ = Vector::Zero(npos);
00066     }
00067     if (0 == nvel) {
00068       velocity_.resize(0);
00069     }
00070     else {
00071       velocity_ = Vector::Zero(nvel);
00072     }
00073     if (0 == nforce) {
00074       force_.resize(0);
00075     }
00076     else {
00077       force_ = Vector::Zero(nforce);
00078     }
00079   }
00080   
00081   
00082   static void _pad(Vector & vv, size_t nn)
00083   {
00084     size_t const old(vv.size());
00085     if (old != nn) {
00086       vv.resize(nn);
00087       if (old < nn) {
00088   memset(&vv[old], 0, (nn - old) * sizeof(double));
00089       }
00090     }
00091   }
00092   
00093   
00094   void State::
00095   resizeAndPadWithZeros(size_t npos, size_t nvel, size_t nforce)
00096   {
00097     _pad(position_, npos);
00098     _pad(velocity_, nvel);
00099     _pad(force_, nforce);
00100   }
00101   
00102   
00103   State & State::
00104   operator = (State const & rhs)
00105   {
00106     if (&rhs == this) {
00107       return *this;
00108     }
00109     time_sec_ = rhs.time_sec_;
00110     time_usec_ = rhs.time_usec_;
00111     position_ = rhs.position_;
00112     velocity_ = rhs.velocity_;
00113     force_ = rhs.force_;
00114     return *this;
00115   }
00116   
00117   
00118   bool State::
00119   equal(State const & rhs, int flags, double precision) const
00120   {
00121     if (&rhs == this) {
00122       return true;
00123     }
00124     if (flags & COMPARE_ACQUISITION_TIME) {
00125       if (time_sec_ != rhs.time_sec_) {
00126   return false;
00127       }
00128       if (time_usec_ != rhs.time_usec_) {
00129   return false;
00130       }
00131     }
00132     if (flags & COMPARE_POSITION) {
00133       if ( ! compare(position_, rhs.position_, precision)) {
00134   return false;
00135       }
00136     }
00137     if (flags & COMPARE_VELOCITY) {
00138       if ( ! compare(velocity_, rhs.velocity_, precision)) {
00139   return false;
00140       }
00141     }
00142     if (flags & COMPARE_FORCE) {
00143       if ( ! compare(force_, rhs.force_, precision)) {
00144   return false;
00145       }
00146     }
00147     return true;
00148   }
00149   
00150 }

Generated on Fri Aug 26 01:31:16 2011 for Stanford Whole-Body Control Framework by  doxygen 1.5.4