Panda3D
|
00001 // Filename: lerpfunctor.h 00002 // Created by: frang (26May00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #ifndef __LERPFUNCTOR_H__ 00016 #define __LERPFUNCTOR_H__ 00017 00018 #include "pandabase.h" 00019 #include "typedReferenceCount.h" 00020 00021 class EXPCL_PANDA_LERP LerpFunctor : public TypedReferenceCount { 00022 public: 00023 LerpFunctor() {} 00024 LerpFunctor(const LerpFunctor&); 00025 virtual ~LerpFunctor(); 00026 LerpFunctor& operator=(const LerpFunctor&); 00027 virtual void operator()(float) = 0; 00028 00029 PUBLISHED: 00030 static TypeHandle get_class_type() { 00031 return _type_handle; 00032 } 00033 00034 public: 00035 static void init_type() { 00036 TypedReferenceCount::init_type(); 00037 register_type(_type_handle, "LerpFunctor", 00038 TypedReferenceCount::get_class_type()); 00039 } 00040 virtual TypeHandle get_type() const { 00041 return get_class_type(); 00042 } 00043 virtual TypeHandle force_init_type() { 00044 init_type(); 00045 return get_class_type(); 00046 } 00047 private: 00048 static TypeHandle _type_handle; 00049 }; 00050 00051 template <class value> 00052 class SimpleLerpFunctor : public LerpFunctor { 00053 protected: 00054 value _start; 00055 value _end; 00056 value _diff_cache; 00057 00058 SimpleLerpFunctor(value start, value end) : LerpFunctor(), _start(start), 00059 _end(end), _diff_cache(end-start) 00060 {} 00061 SimpleLerpFunctor(const SimpleLerpFunctor<value>&); 00062 public: 00063 virtual ~SimpleLerpFunctor(); 00064 SimpleLerpFunctor<value>& operator=(const SimpleLerpFunctor<value>&); 00065 virtual void operator()(float); 00066 00067 PUBLISHED: 00068 value interpolate(float); 00069 INLINE const value &get_start() const { return _start; } 00070 INLINE const value &get_end() const { return _end; } 00071 00072 public: 00073 static TypeHandle get_class_type() { 00074 return _type_handle; 00075 } 00076 static void init_type() { 00077 LerpFunctor::init_type(); 00078 do_init_type(value); 00079 ostringstream os; 00080 os << "SimpleLerpFunctor<" << get_type_handle(value).get_name() << ">"; 00081 register_type(_type_handle, os.str(), LerpFunctor::get_class_type()); 00082 } 00083 virtual TypeHandle get_type() const { 00084 return get_class_type(); 00085 } 00086 virtual TypeHandle force_init_type() { 00087 init_type(); 00088 return get_class_type(); 00089 } 00090 private: 00091 static TypeHandle _type_handle; 00092 }; 00093 00094 template <class value> 00095 TypeHandle SimpleLerpFunctor<value>::_type_handle; 00096 00097 template <class value> 00098 class SimpleQueryLerpFunctor : public SimpleLerpFunctor<value> { 00099 private: 00100 value _save; 00101 protected: 00102 /* 00103 SimpleQueryLerpFunctor(const SimpleQueryLerpFucntor<value>& c); 00104 */ 00105 public: 00106 SimpleQueryLerpFunctor(value start, value end) 00107 : SimpleLerpFunctor<value>(start, end) {} 00108 virtual ~SimpleQueryLerpFunctor(); 00109 SimpleQueryLerpFunctor<value>& operator=(const SimpleQueryLerpFunctor<value>&); 00110 virtual void operator()(float); 00111 PUBLISHED: 00112 value get_value(); 00113 00114 static TypeHandle get_class_type() { 00115 return _type_handle; 00116 } 00117 public: 00118 static void init_type() { 00119 SimpleLerpFunctor<value>::init_type(); 00120 ostringstream os; 00121 os << "SimpleQueryLerpFunctor<" << get_type_handle(value).get_name() 00122 << ">"; 00123 register_type(_type_handle, os.str(), 00124 SimpleLerpFunctor<value>::get_class_type()); 00125 } 00126 virtual TypeHandle get_type() const { 00127 return get_class_type(); 00128 } 00129 virtual TypeHandle force_init_type() { 00130 init_type(); 00131 return get_class_type(); 00132 } 00133 private: 00134 static TypeHandle _type_handle; 00135 }; 00136 00137 template <class value> 00138 TypeHandle SimpleQueryLerpFunctor<value>::_type_handle; 00139 00140 #include "luse.h" 00141 00142 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<int>) 00143 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<float>) 00144 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<LPoint2f>) 00145 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<LPoint3f>) 00146 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<LPoint4f>) 00147 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<LVecBase2f>) 00148 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<LVecBase3f>) 00149 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<LVecBase4f>) 00150 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<LVector2f>) 00151 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<LVector3f>) 00152 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleLerpFunctor<LVector4f>) 00153 00154 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<int>) 00155 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<float>) 00156 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<LPoint2f>) 00157 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<LPoint3f>) 00158 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<LPoint4f>) 00159 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<LVecBase2f>) 00160 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<LVecBase3f>) 00161 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<LVecBase4f>) 00162 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<LVector2f>) 00163 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<LVector3f>) 00164 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_LERP, EXPTP_PANDA_LERP, SimpleQueryLerpFunctor<LVector4f>) 00165 00166 typedef SimpleLerpFunctor<int> IntLerpFunctor; 00167 typedef SimpleLerpFunctor<float> FloatLerpFunctor; 00168 typedef SimpleLerpFunctor<LPoint2f> LPoint2fLerpFunctor; 00169 typedef SimpleLerpFunctor<LPoint3f> LPoint3fLerpFunctor; 00170 typedef SimpleLerpFunctor<LPoint4f> LPoint4fLerpFunctor; 00171 typedef SimpleLerpFunctor<LVecBase2f> LVecBase2fLerpFunctor; 00172 typedef SimpleLerpFunctor<LVecBase3f> LVecBase3fLerpFunctor; 00173 typedef SimpleLerpFunctor<LVecBase4f> LVecBase4fLerpFunctor; 00174 typedef SimpleLerpFunctor<LVector2f> LVector2fLerpFunctor; 00175 typedef SimpleLerpFunctor<LVector3f> LVector3fLerpFunctor; 00176 typedef SimpleLerpFunctor<LVector4f> LVector4fLerpFunctor; 00177 00178 typedef SimpleQueryLerpFunctor<int> IntQueryLerpFunctor; 00179 typedef SimpleQueryLerpFunctor<float> FloatQueryLerpFunctor; 00180 typedef SimpleQueryLerpFunctor<LPoint2f> LPoint2fQueryLerpFunctor; 00181 typedef SimpleQueryLerpFunctor<LPoint3f> LPoint3fQueryLerpFunctor; 00182 typedef SimpleQueryLerpFunctor<LPoint4f> LPoint4fQueryLerpFunctor; 00183 typedef SimpleQueryLerpFunctor<LVecBase2f> LVecBase2fQueryLerpFunctor; 00184 typedef SimpleQueryLerpFunctor<LVecBase3f> LVecBase3fQueryLerpFunctor; 00185 typedef SimpleQueryLerpFunctor<LVecBase4f> LVecBase4fQueryLerpFunctor; 00186 typedef SimpleQueryLerpFunctor<LVector2f> LVector2fQueryLerpFunctor; 00187 typedef SimpleQueryLerpFunctor<LVector3f> LVector3fQueryLerpFunctor; 00188 typedef SimpleQueryLerpFunctor<LVector4f> LVector4fQueryLerpFunctor; 00189 00190 #include "pset.h" 00191 #include "pointerTo.h" 00192 00193 class EXPCL_PANDA_LERP MultiLerpFunctor : public LerpFunctor { 00194 private: 00195 typedef phash_set< PT(LerpFunctor) > Functors; 00196 Functors _funcs; 00197 public: 00198 MultiLerpFunctor() {} 00199 MultiLerpFunctor(const MultiLerpFunctor&); 00200 virtual ~MultiLerpFunctor(); 00201 MultiLerpFunctor& operator=(const MultiLerpFunctor&); 00202 virtual void operator()(float); 00203 void add_functor(LerpFunctor*); 00204 void remove_functor(LerpFunctor*); 00205 00206 PUBLISHED: 00207 static TypeHandle get_class_type() { 00208 return _type_handle; 00209 } 00210 00211 public: 00212 static void init_type() { 00213 LerpFunctor::init_type(); 00214 register_type(_type_handle, "MultiLerpFunctor", 00215 LerpFunctor::get_class_type()); 00216 } 00217 virtual TypeHandle get_type() const { 00218 return get_class_type(); 00219 } 00220 virtual TypeHandle force_init_type() { 00221 init_type(); 00222 return get_class_type(); 00223 } 00224 private: 00225 static TypeHandle _type_handle; 00226 }; 00227 00228 // 00229 // template implementation 00230 // 00231 00232 template <class value> 00233 SimpleLerpFunctor<value>::SimpleLerpFunctor(const SimpleLerpFunctor<value>& c) 00234 : LerpFunctor(c), _start(c._start), _end(c._end), _diff_cache(c._diff_cache) 00235 {} 00236 00237 template <class value> 00238 SimpleLerpFunctor<value>::~SimpleLerpFunctor() 00239 { 00240 } 00241 00242 template <class value> 00243 SimpleLerpFunctor<value>& 00244 SimpleLerpFunctor<value>::operator=(const SimpleLerpFunctor& c) { 00245 _start = c._start; 00246 _end = c._end; 00247 _diff_cache = c._diff_cache; 00248 LerpFunctor::operator=(c); 00249 return *this; 00250 } 00251 00252 template <class value> 00253 void SimpleLerpFunctor<value>::operator()(float) { 00254 // should not be here 00255 } 00256 00257 template <class value> 00258 value SimpleLerpFunctor<value>::interpolate(float t) { 00259 return ((t * _diff_cache) + _start); 00260 } 00261 00262 /* 00263 template <class value> 00264 SimpleQueryLerpFunctor<value>::SimpleQueryLerpFunctor(const SimpleQueryLerpFunctor& c) : SimpleLerpFunctor<value>(c), _save(c._save) {} 00265 */ 00266 00267 template <class value> 00268 SimpleQueryLerpFunctor<value>::~SimpleQueryLerpFunctor() 00269 { 00270 } 00271 00272 template <class value> 00273 SimpleQueryLerpFunctor<value>& 00274 SimpleQueryLerpFunctor<value>::operator=(const SimpleQueryLerpFunctor& c) { 00275 _save = c._save; 00276 SimpleLerpFunctor<value>::operator=(c); 00277 return *this; 00278 } 00279 00280 template <class value> 00281 void SimpleQueryLerpFunctor<value>::operator()(float t) { 00282 _save = this->interpolate(t); 00283 } 00284 00285 template <class value> 00286 value SimpleQueryLerpFunctor<value>::get_value() { 00287 return _save; 00288 } 00289 00290 #endif /* __LERPFUNCTOR_H__ */