Visualization LibraryA lightweight C++ OpenGL middleware for 2D/3D graphics |
[Home] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://www.visualizationlibrary.com */ 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 public: 00047 virtual const char* className() { return "vl::LinearInterpolator"; } 00048 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 = vl::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<vl::fvec2> LinearInterpolatorFVec2_T; 00088 typedef LinearInterpolator<vl::fvec3> LinearInterpolatorFVec3_T; 00089 typedef LinearInterpolator<vl::fvec4> LinearInterpolatorFVec4_T; 00090 typedef LinearInterpolator<double> LinearInterpolatorDouble_T; 00091 typedef LinearInterpolator<vl::dvec2> LinearInterpolatorDVec2_T; 00092 typedef LinearInterpolator<vl::dvec3> LinearInterpolatorDVec3_T; 00093 typedef LinearInterpolator<vl::dvec4> LinearInterpolatorDVec4_T; 00094 00096 class LinearInterpolatorFVec4: public vl::InterpolatorFVec4 00097 { 00098 public: 00099 LinearInterpolatorFVec4(): mInterpolator( new LinearInterpolatorFVec4_T ) {} 00100 LinearInterpolatorFVec4(const std::vector<vl::fvec4>& path): mInterpolator( new LinearInterpolatorFVec4_T(path) ) {} 00101 vl::fvec4 computePoint(float t) const { return interpolator()->computePoint(t); } 00102 LinearInterpolatorFVec4_T* interpolator() { return mInterpolator.get(); } 00103 const LinearInterpolatorFVec4_T* interpolator() const { return mInterpolator.get(); } 00104 void setInterpolator(LinearInterpolatorFVec4_T* interpolator) { mInterpolator = interpolator; } 00105 protected: 00106 vl::ref<LinearInterpolatorFVec4_T> mInterpolator; 00107 }; 00109 class LinearInterpolatorFVec3: public vl::InterpolatorFVec3 00110 { 00111 public: 00112 LinearInterpolatorFVec3(): mInterpolator( new LinearInterpolatorFVec3_T ) {} 00113 LinearInterpolatorFVec3(const std::vector<vl::fvec3>& path): mInterpolator( new LinearInterpolatorFVec3_T(path) ) {} 00114 vl::fvec3 computePoint(float t) const { return interpolator()->computePoint(t); } 00115 LinearInterpolatorFVec3_T* interpolator() { return mInterpolator.get(); } 00116 const LinearInterpolatorFVec3_T* interpolator() const { return mInterpolator.get(); } 00117 void setInterpolator(LinearInterpolatorFVec3_T* interpolator) { mInterpolator = interpolator; } 00118 protected: 00119 vl::ref<LinearInterpolatorFVec3_T> mInterpolator; 00120 }; 00122 class LinearInterpolatorFVec2: public vl::InterpolatorFVec2 00123 { 00124 public: 00125 LinearInterpolatorFVec2(): mInterpolator( new LinearInterpolatorFVec2_T ) {} 00126 LinearInterpolatorFVec2(const std::vector<vl::fvec2>& path): mInterpolator( new LinearInterpolatorFVec2_T(path) ) {} 00127 vl::fvec2 computePoint(float t) const { return interpolator()->computePoint(t); } 00128 LinearInterpolatorFVec2_T* interpolator() { return mInterpolator.get(); } 00129 const LinearInterpolatorFVec2_T* interpolator() const { return mInterpolator.get(); } 00130 void setInterpolator(LinearInterpolatorFVec2_T* interpolator) { mInterpolator = interpolator; } 00131 protected: 00132 vl::ref<LinearInterpolatorFVec2_T> mInterpolator; 00133 }; 00135 class LinearInterpolatorFloat: public vl::InterpolatorFloat 00136 { 00137 public: 00138 LinearInterpolatorFloat(): mInterpolator( new LinearInterpolatorFloat_T ) {} 00139 LinearInterpolatorFloat(const std::vector<float>& path): mInterpolator( new LinearInterpolatorFloat_T(path) ) {} 00140 float computePoint(float t) const { return interpolator()->computePoint(t); } 00141 LinearInterpolatorFloat_T* interpolator() { return mInterpolator.get(); } 00142 const LinearInterpolatorFloat_T* interpolator() const { return mInterpolator.get(); } 00143 void setInterpolator(LinearInterpolatorFloat_T* interpolator) { mInterpolator = interpolator; } 00144 protected: 00145 vl::ref<LinearInterpolatorFloat_T> mInterpolator; 00146 }; 00148 class LinearInterpolatorDVec4: public vl::InterpolatorDVec4 00149 { 00150 public: 00151 LinearInterpolatorDVec4(): mInterpolator( new LinearInterpolatorDVec4_T ) {} 00152 LinearInterpolatorDVec4(const std::vector<vl::dvec4>& path): mInterpolator( new LinearInterpolatorDVec4_T(path) ) {} 00153 vl::dvec4 computePoint(float t) const { return interpolator()->computePoint(t); } 00154 LinearInterpolatorDVec4_T* interpolator() { return mInterpolator.get(); } 00155 const LinearInterpolatorDVec4_T* interpolator() const { return mInterpolator.get(); } 00156 void setInterpolator(LinearInterpolatorDVec4_T* interpolator) { mInterpolator = interpolator; } 00157 protected: 00158 vl::ref<LinearInterpolatorDVec4_T> mInterpolator; 00159 }; 00161 class LinearInterpolatorDVec3: public vl::InterpolatorDVec3 00162 { 00163 public: 00164 LinearInterpolatorDVec3(): mInterpolator( new LinearInterpolatorDVec3_T ) {} 00165 LinearInterpolatorDVec3(const std::vector<vl::dvec3>& path): mInterpolator( new LinearInterpolatorDVec3_T(path) ) {} 00166 vl::dvec3 computePoint(float t) const { return interpolator()->computePoint(t); } 00167 LinearInterpolatorDVec3_T* interpolator() { return mInterpolator.get(); } 00168 const LinearInterpolatorDVec3_T* interpolator() const { return mInterpolator.get(); } 00169 void setInterpolator(LinearInterpolatorDVec3_T* interpolator) { mInterpolator = interpolator; } 00170 protected: 00171 vl::ref<LinearInterpolatorDVec3_T> mInterpolator; 00172 }; 00174 class LinearInterpolatorDVec2: public vl::InterpolatorDVec2 00175 { 00176 public: 00177 LinearInterpolatorDVec2(): mInterpolator( new LinearInterpolatorDVec2_T ) {} 00178 LinearInterpolatorDVec2(const std::vector<vl::dvec2>& path): mInterpolator( new LinearInterpolatorDVec2_T(path) ) {} 00179 vl::dvec2 computePoint(float t) const { return interpolator()->computePoint(t); } 00180 LinearInterpolatorDVec2_T* interpolator() { return mInterpolator.get(); } 00181 const LinearInterpolatorDVec2_T* interpolator() const { return mInterpolator.get(); } 00182 void setInterpolator(LinearInterpolatorDVec2_T* interpolator) { mInterpolator = interpolator; } 00183 protected: 00184 vl::ref<LinearInterpolatorDVec2_T> mInterpolator; 00185 }; 00187 class LinearInterpolatorDouble: public vl::InterpolatorDouble 00188 { 00189 public: 00190 LinearInterpolatorDouble(): mInterpolator( new LinearInterpolatorDouble_T ) {} 00191 LinearInterpolatorDouble(const std::vector<double>& path): mInterpolator( new LinearInterpolatorDouble_T(path) ) {} 00192 double computePoint(float t) const { return interpolator()->computePoint(t); } 00193 LinearInterpolatorDouble_T* interpolator() { return mInterpolator.get(); } 00194 const LinearInterpolatorDouble_T* interpolator() const { return mInterpolator.get(); } 00195 void setInterpolator(LinearInterpolatorDouble_T* interpolator) { mInterpolator = interpolator; } 00196 protected: 00197 vl::ref<LinearInterpolatorDouble_T> mInterpolator; 00198 }; 00199 } 00200 00201 #endif