GEL  2
GEL is a library for Geometry and Linear Algebra
/Users/jab/Documents/Teaching/02585/GEL2_and_demos/GEL/src/Geometry/Cell.h
00001 #ifndef __GEOMETRY_CELL_H
00002 #define __GEOMETRY_CELL_H
00003 
00004 #include <vector>
00005 #include "CGLA/Vec3i.h"
00006 #include "CGLA/BitMask.h"
00007 #include "RGrid.h"
00008 
00009 namespace Geometry 
00010 {
00024         template<class T, int CELL_DIM, class ChildT>
00025         class Cell
00026         {
00027         public:
00028                 typedef T DataType;
00029 
00030         private:
00031                 bool touched;
00032 
00034                 std::vector<T> data;
00035                 
00047                 static const CGLA::BitMask& get_bit_mask()
00048                 {
00049                         static const CGLA::BitMask bit_mask(CELL_DIM);
00050                         return bit_mask;
00051                 }
00052 
00053         public:
00054 
00057                 static int get_dim() 
00058                 {
00059                         return CELL_DIM;
00060                 }
00061 
00062         private:
00063                 
00067                 int get_idx(const CGLA::Vec3i& idx) const
00068                 {
00069                         CGLA::Vec3i bot_idx = get_bit_mask().mask(idx);
00070                         return (bot_idx[2]*get_dim()+bot_idx[1])*get_dim()+bot_idx[0];
00071                 }
00072 
00073         protected:
00074 
00078                 void store_priv(const CGLA::Vec3i& p, const T& new_val) 
00079                 {
00080                         if(is_coalesced()) split();
00081                         touched = true;
00082                         data[get_idx(p)] = new_val;
00083                 }
00084 
00085                 
00086         public:
00087         
00090                 Cell(const T& val): data(1,val), touched(false)
00091                 {
00092                         assert(CELL_DIM==get_dim());
00093                 }
00094 
00097                 void coalesce(const T& val) 
00098                 {
00099                         data=std::vector<T>(1,val);
00100                         touched = true;
00101                 }
00102 
00104                 void split()
00105                 {
00106                         T val = data[0];
00107                         data.resize(CGLA::qbe(CELL_DIM), val);
00108                         touched = true;
00109                 }
00110 
00112                 bool is_coalesced() const 
00113                 {
00114                         // We rely on size() being constant time below.
00115                         // Probably this function should be changed.... it would be bad
00116                         // if it was slow ... very bad....
00117                         return data.size()==1;
00118                 }
00119 
00120                 
00124                 const T& operator[](const CGLA::Vec3i& p) const 
00125                 {
00126                         if(is_coalesced()) return data[0];
00127                         return *(&data[get_idx(p)]);
00128                 }
00129 
00130                 void store(const CGLA::Vec3i& p, const T& new_val) 
00131                 {
00132                         return static_cast<ChildT&>(*this).store(p,new_val);
00133                 }
00134                 
00135         
00138                 const T* get() const {return &data[0];}
00139 
00142                 T* get() 
00143                 {
00144                         touched = true;
00145                         return &data[0];
00146                 }
00147 
00148                 void untouch() {touched=false;}
00149                 void touch() {touched=true;}
00150                 bool is_touched() const {return touched;}
00151 
00152         };
00153 
00154         
00155         template<class T, int CELL_DIM>
00156         class DefaultCell: public Cell<T,CELL_DIM,DefaultCell<T, CELL_DIM> >
00157         {
00158         public:
00159                 DefaultCell(const T& val): Cell<T,CELL_DIM,DefaultCell>(val){}
00160                 void store(const CGLA::Vec3i& p, const T& new_val) 
00161                 {
00162                         store_priv(p, new_val);
00163                 }
00164                 
00165         };
00166 
00167 
00168 }
00169 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations