User Manual, Developers Guide and API Documentation

NLinear.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef _NLINEAR_HPP
00029 #define _NLINEAR_HPP
00030 
00031 #include <cmath>
00032 #include <list>
00033 #include <WNS/Interpolation.hpp>
00034 
00035 namespace wns
00036 {
00068     template<typename T,
00069          InterpolationDefs::SizeType N,
00070          InterpolationDefs::SizeType M = N>
00071     class NLinear : public Interpolation<T, N>
00072     {
00076         typedef Interpolation<T, N> SuperType;
00077     public:
00081         typedef typename SuperType::CoordType CoordType;
00086         typedef typename SuperType::DiscreteContainer DiscreteContainer;
00087 
00091         typedef typename SuperType::InitType InitType;
00092 
00096         typedef const NLinear<T, N-1, M> HyperplaneType;
00097 
00101         typedef const HyperplaneType& ConstReference;
00102 
00106         typedef std::list<CoordType> CoordList;
00107 
00133         NLinear(const DiscreteContainer& discreteValues,
00134             const void *const universe = 0,
00135             CoordList *const coords = 0)
00136             : SuperType(discreteValues),
00137               coords((coords == 0) ? new CoordList() : coords),
00138               deleteCoords(coords == 0),
00139               //  what value this has actually doesn't matter
00140               hyperplane(discreteValues[0],
00141                      (universe == 0) ? &discreteValues : universe,
00142                      this->coords)      // because we use this ^
00143         {}
00144 
00148         virtual ~NLinear()
00149         {
00150             if (deleteCoords) delete coords;
00151         }
00152 
00164         virtual ConstReference operator[](const CoordType& coord) const
00165         {
00166             coords->push_back(coord);
00167             return hyperplane;
00168         }
00169 
00170     private:
00177         CoordList* const coords;
00178 
00183         const bool deleteCoords;
00184 
00189         HyperplaneType hyperplane;
00190     };
00191 
00195     template<typename T,
00196          InterpolationDefs::SizeType M>
00197     class NLinear<T, 1, M> : public Interpolation<T, 1>
00198     {
00199         typedef Interpolation<T, 1> SuperType;
00200     public:
00201         typedef typename SuperType::ValueType ValueType;
00202         typedef typename SuperType::CoordType CoordType;
00203         typedef typename SuperType::DiscreteContainer DiscreteContainer;
00204         typedef typename SuperType::InitType InitType;
00205         typedef typename DiscreteContainer::IndexType IndexType;
00206 
00207         typedef std::list<CoordType> CoordList;
00208 
00209         NLinear(const DiscreteContainer& discreteValues,
00210             const void *const universe = 0,
00211             CoordList *const coords = 0)
00212             : SuperType(discreteValues),
00213               universe((universe == 0) ? &discreteValues : universe),
00214               coords((coords == 0) ? new CoordList() : coords),
00215               deleteCoords(coords == 0)
00216         {}
00217 
00218         virtual ~NLinear()
00219         {
00220             if (deleteCoords) delete coords;
00221         }
00222 
00223         virtual ValueType operator[](const CoordType& coord) const
00224         {
00225             coords->push_back(coord);
00226             const ValueType v = this->interpolate(*coords);
00227             coords->clear();
00228             return v;
00229         }
00230 
00231     private:
00238         typedef typename Interpolation<T, M>::DiscreteContainer UniverseType;
00239         typedef std::list<IndexType> IndexList;
00240 
00241         const void *const universe;
00242         CoordList *const coords;
00243         const bool deleteCoords;
00244 
00263         ValueType interpolate(const CoordList& coords,
00264                       const IndexList& indices = IndexList()) const
00265         {
00266             if (coords.empty()) {
00267                 // If coords is empty, there is nothing to interpolate.
00268                 // So just return the values in the discreteContainer.
00269                 return static_cast<const UniverseType *const>(universe)->at(indices.begin(), indices.end());
00270             }
00271             else {
00272                 // calculate new indices
00273                 const CoordType& x = coords.back();
00274                 const IndexType x1 = (IndexType)(std::floor(x));
00275                 const IndexType x2 = (IndexType)(std::ceil(x));
00276                 // shorten coordlist
00277                 CoordList c = CoordList(coords);
00278                 c.pop_back();
00279                 // construct new index lists
00280                 IndexList i1 = IndexList(indices); i1.push_front(x1);
00281                 IndexList i2 = IndexList(indices); i2.push_front(x2);
00282                 // recursivly get values to interpolate
00283                 const ValueType f1 = interpolate(c, i1);
00284                 const ValueType f2 = interpolate(c, i2);
00285                 // interpolate and return
00286                 return f1 + (f2 - f1) * (x - x1);
00287             }
00288         }
00289     };
00290 
00291 }
00292 
00293 #endif // _NLINEAR_HPP
00294 

Generated on Fri May 25 03:31:48 2012 for openWNS by  doxygen 1.5.5