library of assembled shared sources

http://lass.cocamware.com

spline.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @class lass::num::Spline
00046  *  @brief abstract base class of splines.
00047  *  @author Bram de Greve [BdG]
00048  */
00049 
00050 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_SPLINE_H
00051 #define LASS_GUARDIAN_OF_INCLUSION_NUM_SPLINE_H
00052 
00053 #include "num_common.h"
00054 #include "../stde/extended_iterator.h"
00055 
00056 namespace lass
00057 {
00058 namespace num
00059 {
00060 
00061 template 
00062 <
00063     typename ScalarType,
00064     typename DataType
00065 >
00066 class Spline
00067 {
00068 public:
00069 
00070     typedef ScalarType TScalar;
00071     typedef DataType TData;
00072     
00073     class ControlRange
00074     {
00075     public:
00076         ControlRange(TScalar begin, TScalar end): begin_(begin), end_(end) {}
00077         const TScalar begin() const { return begin_; }
00078         const TScalar end() const { return end_; }
00079     private:
00080         TScalar begin_;
00081         TScalar end_;
00082     };
00083 
00084     virtual ~Spline() {}
00085 
00086     virtual const TData operator()(TScalar iX) const = 0;
00087     virtual const TData derivative(TScalar iX) const = 0;
00088     virtual const TData derivative2(TScalar iX) const = 0;
00089     virtual const TData integral(TScalar iA, TScalar iB) const = 0;
00090 
00091     virtual const bool isEmpty() const = 0;
00092     virtual const ControlRange controlRange() const = 0;
00093 };
00094 
00095 
00096 
00097 
00098 
00099 #pragma LASS_TODO("below are data traits that should move to a seperate header when appropriate, [Bramz]")
00100 
00101 /** @defgroup DataTraits
00102  *  A set of trait classes on how to manipulate data
00103  *
00104  *  This is the concept each data traits class has to model:
00105  *
00106  *  @code
00107  *  struct
00108  *  {
00109  *      typedef ... TData; // type of data
00110  *      typedef ... TScalar; // type of a single scalar value in data
00111  *      static size_t dimension(const TData& iY); // returns number of scalar values in iY
00112  *      static void zero(TData& iY, size_t iDim); // resets iY to a data object with iDim zeros
00113  *      static TScalar get(const TData& iY, size_t iIndex); // return iIndex'th scalar in iY
00114  *      static void set(TData& ioY, size_t iIndex, TScalar iV); // set iIndex'th scalar of iY to iV
00115  *      static void scale(TDAta& ioAcc, TScalar iS); // scale each scalar of ioAcc by iS
00116  *      static void multiplyAccumulate(TData& ioAcc, const TData& iY, TScalar iS); // add each
00117  *          // element of iY multiplied by iS to the element in ioAcc with same index.
00118  *  };
00119  *  @endcode
00120  *
00121  *  - @ref DataTraitsSequence: concerning data acting like an STL sequence
00122  *  - @ref DataTraitsScalar: concerning scalar data
00123  *  - @ref DataTraitsDynamicVector: concerning data like num::Vector
00124  *  - @ref DataTraitsStaticVector: concerning data like prim::Vector2D
00125  */
00126 
00127 /** @ingroup DataTraits
00128  */
00129 template <typename SequenceType>
00130 struct DataTraitsSequence
00131 {
00132     typedef SequenceType TData;
00133     typedef typename SequenceType::value_type TScalar;
00134     static size_t dimension(const TData& iY) { return std::distance(iY.begin(), iY.end()); }
00135     static void zero(TData& ioY, size_t iDim) { ioY = TData(iDim); }
00136     static TScalar get(const TData& iY, size_t iIndex) { return *stde::next(iY.begin(), iIndex); }
00137     static void set(TData& ioY, size_t iIndex, TScalar iV) { *stde::next(ioY.begin(), iIndex) = iV; }
00138     static void scale(TData& ioAcc, TScalar iS) 
00139     { 
00140         std::transform(ioAcc.begin(), ioAcc.end(), ioAcc.begin(), std::bind2nd(std::multiplies<TScalar>(), iS));
00141     }
00142     static void multiplyAccumulate(TData& ioAcc, const TData& iY, TScalar iS) 
00143     {
00144         std::transform(ioAcc.begin(), ioAcc.end(), iY.begin(), ioAcc.begin(), Mac(iS));
00145     }
00146 private:
00147     class Mac
00148     {
00149         TScalar s_;
00150     public:
00151         Mac(TScalar iS): s_(iS) {}
00152         TScalar operator()(TScalar iA, TScalar iB) const { return iA + s_ * iB; }
00153     };
00154 };
00155 
00156 /** @ingroup DataTraits
00157  */
00158 template <typename ScalarType>
00159 struct DataTraitsScalar
00160 {
00161     typedef ScalarType TData;
00162     typedef ScalarType TScalar;
00163     static size_t dimension(TScalar iY) { return 1; }
00164     static void zero(TScalar& ioY, size_t iDim) { ioY = TScalar(); }
00165     static TScalar get(TScalar iY, size_t iIndex) { return iY; }
00166     static void set(TScalar& ioY, size_t iIndex, TScalar iV) { ioY = iV; }
00167     static void scale(TScalar& ioAcc, TScalar iS) { ioAcc *= iS; }
00168     static void multiplyAccumulate(TScalar& ioAcc, TScalar iY, TScalar iS) { ioAcc += iY * iS; }
00169 };
00170 
00171 
00172 /** @ingroup DataTraits
00173  */
00174 template <typename DataType>
00175 struct DataTraitsDynamicVector
00176 {
00177     typedef DataType TData;
00178     typedef typename DataType::TValue TScalar;
00179     static size_t dimension(const TData& iY) { return iY.size(); }
00180     static void zero(TData& ioY, size_t iDim) { ioY = TData(iDim); }
00181     static TScalar get(const TData& iY, size_t iIndex) { return iY[iIndex]; }
00182     static void set(TData& ioY, size_t iIndex, TScalar iV) { ioY[iIndex] = iV; }
00183     static void scale(TData& ioAcc, TScalar iS) { ioAcc *= iS; }
00184     static void multiplyAccumulate(TData& ioAcc, const TData& iY, TScalar iS)  { ioAcc += iY * iS; }
00185 };
00186 
00187 /** @ingroup DataTraits
00188  */
00189 template <typename DataType>
00190 struct DataTraitsStaticVector
00191 {
00192     typedef DataType TData;
00193     typedef typename DataType::TValue TScalar;
00194     static size_t dimension(const TData& /*iY*/) { return TData::dimension; }
00195     static void zero(TData& ioY, size_t /*iDim*/) { ioY = TData(); }
00196     static TScalar get(const TData& iY, size_t iIndex) { return iY[iIndex]; }
00197     static void set(TData& ioY, size_t iIndex, TScalar iV) { ioY[iIndex] = iV; }
00198     static void scale(TData& ioAcc, TScalar iS) { ioAcc *= iS; }
00199     static void multiplyAccumulate(TData& ioAcc, const TData& iY, TScalar iS)  { ioAcc += iY * iS; }
00200 };
00201 
00202 
00203 }
00204 
00205 }
00206 
00207 #endif
00208 
00209 // EOF

Generated on Mon Nov 10 14:21:39 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo