Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/LinearInterpolator.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef LinearInterpolator_INCLUDE_ONCE
00033 #define LinearInterpolator_INCLUDE_ONCE
00034 
00035 #include <vlCore/Interpolator.hpp>
00036 
00037 namespace vl
00038 {
00043   template<typename T>
00044   class LinearInterpolator: public Object
00045   {
00046     VL_INSTRUMENT_CLASS(vl::LinearInterpolator<typename T>, Object)
00047 
00048   public:
00049     LinearInterpolator() 
00050     {
00051       VL_DEBUG_SET_OBJECT_NAME()
00052     }
00053 
00054     LinearInterpolator(const std::vector<T>& path): mPath(path) {}
00055 
00057     T computePoint(float t) const
00058     {
00059       t = clamp(t,0.0f,1.0f);
00060       if (t == 0.0f)
00061         return mPath[0];
00062       if (t == 1.0f)
00063         return mPath.back();
00064       int p0 = int((mPath.size()-1)*t);
00065       int p1 = p0+1;
00066       if (p1 > (int)mPath.size()-1)
00067         p1 = (int)mPath.size()-1;
00068       float tt = (mPath.size()-1)*t - p0/*int((mPath.size()-1)*t)*/;
00069       return mPath[p0]*(1.0f-tt) + mPath[p1]*tt;
00070     }
00071 
00075     void setPath(const std::vector<T>& path) { mPath = path; }
00077     const std::vector<T>& path() const { return mPath; }
00079     std::vector<T>& path() { return mPath; }
00080 
00081   protected:
00082     std::vector<T> mPath;
00083     std::vector<T> mLinearSpline;
00084   };
00085 
00086   typedef LinearInterpolator<float>     LinearInterpolatorFloat_T;
00087   typedef LinearInterpolator<fvec2> LinearInterpolatorFVec2_T;
00088   typedef LinearInterpolator<fvec3> LinearInterpolatorFVec3_T;
00089   typedef LinearInterpolator<fvec4> LinearInterpolatorFVec4_T;
00090   typedef LinearInterpolator<double>    LinearInterpolatorDouble_T;
00091   typedef LinearInterpolator<dvec2> LinearInterpolatorDVec2_T;
00092   typedef LinearInterpolator<dvec3> LinearInterpolatorDVec3_T;
00093   typedef LinearInterpolator<dvec4> LinearInterpolatorDVec4_T;
00094 
00096   class LinearInterpolatorFVec4: public InterpolatorFVec4
00097   {
00098     VL_INSTRUMENT_CLASS(vl::LinearInterpolatorFVec4, InterpolatorFVec4)
00099   public:
00100     LinearInterpolatorFVec4(): mInterpolator( new LinearInterpolatorFVec4_T ) {}
00101     LinearInterpolatorFVec4(const std::vector<fvec4>& path): mInterpolator( new LinearInterpolatorFVec4_T(path) ) {}
00102     fvec4 computePoint(float t) const { return interpolator()->computePoint(t); }
00103     LinearInterpolatorFVec4_T* interpolator() { return mInterpolator.get(); }
00104     const LinearInterpolatorFVec4_T* interpolator() const { return mInterpolator.get(); }
00105     void setInterpolator(LinearInterpolatorFVec4_T* interpolator) { mInterpolator = interpolator; }
00106   protected:
00107     ref<LinearInterpolatorFVec4_T> mInterpolator;
00108   };
00110   class LinearInterpolatorFVec3: public InterpolatorFVec3
00111   {
00112     VL_INSTRUMENT_CLASS(vl::LinearInterpolatorFVec3, InterpolatorFVec3)
00113   public:
00114     LinearInterpolatorFVec3(): mInterpolator( new LinearInterpolatorFVec3_T ) {}
00115     LinearInterpolatorFVec3(const std::vector<fvec3>& path): mInterpolator( new LinearInterpolatorFVec3_T(path) ) {}
00116     fvec3 computePoint(float t) const { return interpolator()->computePoint(t); }
00117     LinearInterpolatorFVec3_T* interpolator() { return mInterpolator.get(); }
00118     const LinearInterpolatorFVec3_T* interpolator() const { return mInterpolator.get(); }
00119     void setInterpolator(LinearInterpolatorFVec3_T* interpolator) { mInterpolator = interpolator; }
00120   protected:
00121     ref<LinearInterpolatorFVec3_T> mInterpolator;
00122   };
00124   class LinearInterpolatorFVec2: public InterpolatorFVec2
00125   {
00126     VL_INSTRUMENT_CLASS(vl::LinearInterpolatorFVec2, InterpolatorFVec2)
00127   public:
00128     LinearInterpolatorFVec2(): mInterpolator( new LinearInterpolatorFVec2_T ) {}
00129     LinearInterpolatorFVec2(const std::vector<fvec2>& path): mInterpolator( new LinearInterpolatorFVec2_T(path) ) {}
00130     fvec2 computePoint(float t) const { return interpolator()->computePoint(t); }
00131     LinearInterpolatorFVec2_T* interpolator() { return mInterpolator.get(); }
00132     const LinearInterpolatorFVec2_T* interpolator() const { return mInterpolator.get(); }
00133     void setInterpolator(LinearInterpolatorFVec2_T* interpolator) { mInterpolator = interpolator; }
00134   protected:
00135     ref<LinearInterpolatorFVec2_T> mInterpolator;
00136   };
00138   class LinearInterpolatorFloat: public InterpolatorFloat
00139   {
00140     VL_INSTRUMENT_CLASS(vl::LinearInterpolatorFloat, InterpolatorFloat)
00141   public:
00142     LinearInterpolatorFloat(): mInterpolator( new LinearInterpolatorFloat_T ) {}
00143     LinearInterpolatorFloat(const std::vector<float>& path): mInterpolator( new LinearInterpolatorFloat_T(path) ) {}
00144     float computePoint(float t) const { return interpolator()->computePoint(t); }
00145     LinearInterpolatorFloat_T* interpolator() { return mInterpolator.get(); }
00146     const LinearInterpolatorFloat_T* interpolator() const { return mInterpolator.get(); }
00147     void setInterpolator(LinearInterpolatorFloat_T* interpolator) { mInterpolator = interpolator; }
00148   protected:
00149     ref<LinearInterpolatorFloat_T> mInterpolator;
00150   };
00152   class LinearInterpolatorDVec4: public InterpolatorDVec4
00153   {
00154     VL_INSTRUMENT_CLASS(vl::LinearInterpolatorDVec4, InterpolatorDVec4)
00155   public:
00156     LinearInterpolatorDVec4(): mInterpolator( new LinearInterpolatorDVec4_T ) {}
00157     LinearInterpolatorDVec4(const std::vector<dvec4>& path): mInterpolator( new LinearInterpolatorDVec4_T(path) ) {}
00158     dvec4 computePoint(float t) const { return interpolator()->computePoint(t); }
00159     LinearInterpolatorDVec4_T* interpolator() { return mInterpolator.get(); }
00160     const LinearInterpolatorDVec4_T* interpolator() const { return mInterpolator.get(); }
00161     void setInterpolator(LinearInterpolatorDVec4_T* interpolator) { mInterpolator = interpolator; }
00162   protected:
00163     ref<LinearInterpolatorDVec4_T> mInterpolator;
00164   };
00166   class LinearInterpolatorDVec3: public InterpolatorDVec3
00167   {
00168     VL_INSTRUMENT_CLASS(vl::LinearInterpolatorDVec3, InterpolatorDVec3)
00169   public:
00170     LinearInterpolatorDVec3(): mInterpolator( new LinearInterpolatorDVec3_T ) {}
00171     LinearInterpolatorDVec3(const std::vector<dvec3>& path): mInterpolator( new LinearInterpolatorDVec3_T(path) ) {}
00172     dvec3 computePoint(float t) const { return interpolator()->computePoint(t); }
00173     LinearInterpolatorDVec3_T* interpolator() { return mInterpolator.get(); }
00174     const LinearInterpolatorDVec3_T* interpolator() const { return mInterpolator.get(); }
00175     void setInterpolator(LinearInterpolatorDVec3_T* interpolator) { mInterpolator = interpolator; }
00176   protected:
00177     ref<LinearInterpolatorDVec3_T> mInterpolator;
00178   };
00180   class LinearInterpolatorDVec2: public InterpolatorDVec2
00181   {
00182     VL_INSTRUMENT_CLASS(vl::LinearInterpolatorDVec2, InterpolatorDVec2)
00183   public:
00184     LinearInterpolatorDVec2(): mInterpolator( new LinearInterpolatorDVec2_T ) {}
00185     LinearInterpolatorDVec2(const std::vector<dvec2>& path): mInterpolator( new LinearInterpolatorDVec2_T(path) ) {}
00186     dvec2 computePoint(float t) const { return interpolator()->computePoint(t); }
00187     LinearInterpolatorDVec2_T* interpolator() { return mInterpolator.get(); }
00188     const LinearInterpolatorDVec2_T* interpolator() const { return mInterpolator.get(); }
00189     void setInterpolator(LinearInterpolatorDVec2_T* interpolator) { mInterpolator = interpolator; }
00190   protected:
00191     ref<LinearInterpolatorDVec2_T> mInterpolator;
00192   };
00194   class LinearInterpolatorDouble: public InterpolatorDouble
00195   {
00196     VL_INSTRUMENT_CLASS(vl::LinearInterpolatorDouble, InterpolatorDouble)
00197   public:
00198     LinearInterpolatorDouble(): mInterpolator( new LinearInterpolatorDouble_T ) {}
00199     LinearInterpolatorDouble(const std::vector<double>& path): mInterpolator( new LinearInterpolatorDouble_T(path) ) {}
00200     double computePoint(float t) const { return interpolator()->computePoint(t); }
00201     LinearInterpolatorDouble_T* interpolator() { return mInterpolator.get(); }
00202     const LinearInterpolatorDouble_T* interpolator() const { return mInterpolator.get(); }
00203     void setInterpolator(LinearInterpolatorDouble_T* interpolator) { mInterpolator = interpolator; }
00204   protected:
00205     ref<LinearInterpolatorDouble_T> mInterpolator;
00206   };
00207 }
00208 
00209 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:29.
Permission is granted to use this page to write and publish articles regarding Visualization Library.