JK2eA
 All Classes Functions Variables
CurveGraph.h
00001 #pragma once
00002 
00011 #include <vector>
00012 #include <JK2lib.h>
00013 
00014 template<class T>
00015 class CCurveGraph {
00016 protected:
00017         // nodes
00018         struct GRAPH_NODE { 
00019                 float t;                
00020                 T         value;        
00021 
00022                 bool operator<(const GRAPH_NODE& r_node) const
00023                 {
00024                         return t < r_node.t;
00025                 }
00026                 bool operator>(const GRAPH_NODE& r_node) const
00027                 {
00028                         return t > r_node.t;
00029                 }
00030                 bool operator==(const GRAPH_NODE& r_node) const
00031                 {
00032                         return t == r_node.t;
00033                 }
00034                 bool operator<(const float& r_t) const 
00035                 {
00036                         return t < r_t;
00037                 }
00038                 bool operator>(const float& r_t) const 
00039                 {
00040                         return t > r_t;
00041                 }
00042                 bool operator==(const float& r_t) const 
00043                 {
00044                         return t == r_t;
00045                 }
00046                 
00047         };
00048         
00049 
00050         std::vector<GRAPH_NODE> m_nodes;        
00051 
00052         /* 
00053                 context pro interpolaci, zrychleni hledani klicu.
00054                 Predpoklada se, parametr t v Interpolate postupne roste a to
00055                 po malych skocich
00056         */
00057         size_t m_currIndex;
00058 
00059 public:
00063         CCurveGraph(); 
00067         virtual ~CCurveGraph();
00068 
00075         T*              GetValue(const size_t index);
00082         float*  GetTime(const size_t index);
00083 
00090         virtual size_t AddNode(const float t, const T& value);
00098         size_t AddNode(const size_t index, const float t, const T& value);
00099         
00104         void DelNode(const size_t index);
00105                         
00109         void Clear();
00110                         
00115         size_t GetNodeCount() const;
00116         
00122         virtual T Interpolate(float time) = NULL;
00123 
00128         void Load(FileManager *f);
00133         void Save(FileManager *f);
00134 
00140         inline bool IsEnd(const float t) 
00141         { 
00142                 if(!m_nodes.empty()) {
00143                         return t > m_nodes.back().t;
00144                 }
00145                 return true;
00146         }
00151         inline float GetTotalTime() 
00152         {
00153                 if(!m_nodes.empty()) {
00154                         return m_nodes.back().t;
00155                 }
00156                 return 0.0f;
00157         }
00158 };
00159 
00160 
00161 #define ID_TEST(ret, l_b, h_b) if(index < l_b || index >= h_b) return ret;
00162 #define ID_TEST_VOID(l_b, h_b) if(index < l_b || index >= h_b) return;
00163 
00164 template<class T>
00165 CCurveGraph<T>::CCurveGraph()
00166 {
00167 }
00168 template<class T>
00169 CCurveGraph<T>::~CCurveGraph()
00170 {
00171 }
00172 
00176 template<class T> typename
00177 T* CCurveGraph<T>::GetValue(const size_t index)
00178 {
00179         ID_TEST(NULL,0, m_nodes.size());
00180         return &m_nodes[index].value;
00181 }
00182 template<class T> typename
00183 float* CCurveGraph<T>::GetTime(const size_t index)
00184 {
00185         ID_TEST(NULL,0, m_nodes.size());
00186         return &m_nodes[index].t;
00187 }
00188 
00189 
00193 template<class T> typename
00194 size_t CCurveGraph<T>::AddNode(const float t, const T& value)
00195 {
00196         GRAPH_NODE node;
00197         node.t = t;
00198         node.value = value;
00199 
00200         if(m_nodes.empty()) {
00201                 m_nodes.push_back(node);
00202                 return 0;
00203         }
00204         // najdi misto kam patri
00205         for(size_t i = 0; i < m_nodes.size(); i++) {
00206                 if( node < m_nodes[i]) {
00207                         m_nodes.insert(m_nodes.begin() + i, node);
00208                         return i;                
00209                 } else if ( i == m_nodes.size() - 1 ) {
00210                         m_nodes.push_back(node);
00211                         return m_nodes.size() - 1;
00212                 }
00213         }
00214         return 0;
00215 }
00219 template<class T> typename
00220 size_t CCurveGraph<T>::AddNode(const size_t index, const float t, const T& value)
00221 {
00222         GRAPH_NODE node;
00223         node.t = t;
00224         node.value = value;
00225 
00226         if(index > m_nodes.size()) {
00227                 m_nodes.push_back(node);
00228                 return m_nodes.size() - 1;
00229         } else {
00230                 m_nodes.insert(m_nodes.begin() + index, node);
00231                 return index;
00232         }
00233         return 0;
00234 }
00235 
00239 template<class T>
00240 void CCurveGraph<T>::DelNode(const size_t index)
00241 {
00242         ID_TEST_VOID(0, m_nodes.size());
00243 
00244         m_nodes.erase(m_nodes.begin() + index);
00245 }
00246 
00250 template<class T>
00251 void CCurveGraph<T>::Clear()
00252 {
00253         m_nodes.clear();
00254 }
00255 
00256 template<class T>
00257 size_t CCurveGraph<T>::GetNodeCount() const
00258 {
00259         return m_nodes.size();
00260 }
00261 
00265 template<class T>
00266 void CCurveGraph<T>::Load(FileManager *f)
00267 {
00268         Clear();
00269         // nody
00270         size_t size = m_nodes.size();
00271         f->Get(&size);
00272         m_nodes.resize(size);
00273         for(size_t i = 0; i < size; i++) {
00274                 f->Get(&m_nodes[i].t);
00275                 f->Get(&m_nodes[i].value);
00276         }
00277 }
00281 template<class T>
00282 void CCurveGraph<T>::Save(FileManager *f)
00283 {
00284         // nody
00285         size_t size = m_nodes.size();
00286         f->Set(&size);
00287         for(size_t i = 0; i < size; i++) {
00288                 f->Set(&m_nodes[i].t);
00289                 f->Set(&m_nodes[i].value);
00290         }
00291 }