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

base/ParametricTrajectoryRep.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: ParametricTrajectoryRep.cpp 1029 2004-02-11 20:45:54Z jungd $
00019   $Revision: 1.1 $
00020   $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <base/ParametricTrajectoryRep>
00026 
00027 #include <base/Serializer>
00028 
00029 using base::ParametricTrajectoryRep;
00030 
00031 using base::Time;
00032 
00033 ParametricTrajectoryRep::ParametricTrajectoryRep(const ExpressionVector& p)
00034 {
00035   Assert( (p.size() == 4) || (p.size() == 7) || (p.size() == 8) );
00036 
00037   if (p.size() != 4)
00038     flags = HasOrient;
00039 
00040   v.resize(p.size()-1);
00041   for(Int i=0; i<v.size(); i++)
00042     v[i] = p[i];
00043   
00044   // the function f, t = f(s) is required to be linear, so reformulate it
00045   //  by fitting a line to it, then test the fit and complain if it is bad
00046   Expression f = p[p.size()-1];
00047 
00048   Real a,b;
00049   times = f;
00050   linearFit(a,b);
00051 
00052   if (a <= 0.0)
00053     throw std::invalid_argument(Exception("the function t(s) must be linear and monotonically increasing"));
00054 
00055   // make a new f, lf(s) = sa + b
00056   Expression lf = Expression::p[0]*a + b;
00057   lf.simplify();
00058   
00059   // check fit
00060   Vector param(1);
00061   for(Real s=0; s<=1.0; s+=1/10.0) {
00062     param[0]=s;
00063     if (!Math::equals( lf.evaluate(param), f.evaluate(param) ) )
00064       throw std::invalid_argument(Exception("the function t(s) must be linear"));
00065   }
00066   
00067   times = lf;
00068   
00069 }
00070 
00071 
00072 void ParametricTrajectoryRep::linearFit(Real& a, Real &b) const
00073 {
00074   // determine a,b s.t. t = as + b
00075   Real t0 = eval(0); // t0 = times(0)
00076   Real t1 = eval(1); // t1 = times(1)
00077   
00078   a = t1-t0;
00079   b = t0;
00080 }
00081 
00082 
00083 Time ParametricTrajectoryRep::time(Real s) const
00084 {
00085   Math::bound<Real>(s,0,1);
00086   return eval(s);
00087 }
00088 
00089 
00090 void ParametricTrajectoryRep::shiftTime(const Time& dt)
00091 {
00092   times = times + Expression(dt.seconds());
00093   times.simplify();
00094 }
00095 
00096 
00097 void ParametricTrajectoryRep::scaleTime(Real s)
00098 {
00099   times = Expression(s)*times;
00100   times.simplify();
00101 }
00102 
00103 
00104 Real ParametricTrajectoryRep::gets(const Time& t) const
00105 {
00106   // fit times = as + b, then compute s = (t-b)/a
00107 
00108   Real t0 = eval(0);
00109   Real t1 = eval(1);
00110   Real bt(t.seconds());
00111   Math::bound<Real>(bt,t0,t1);
00112 
00113   Real a,b;
00114   linearFit(a,b);
00115   
00116   if (a==0) return 0;
00117   
00118   return(bt-b)/a;
00119 }
00120 
00121 
00122 void ParametricTrajectoryRep::serialize(Serializer& s)
00123 {
00124   ParametricPathRep::serialize(s);
00125   s(times,"times");
00126 }
00127 
00128 

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