1 #ifndef __OCMECELL__ 2 #define __OCMECELL__ 3 4 /* 5 Cell store the data of the cell that will be permanently stored in memory with 6 the chain, such as the dependences from/to other cells 7 */ 8 #include "vcg_mesh.h" 9 #include <map> 10 #include <vector> 11 #include <vcg/space/box3.h> 12 #include "../utils/logging.h" 13 #include "../utils/name_access_function_bounds.h" 14 #include "../ooc_vector/ooc_chains.h" 15 #include "FBool.h" 16 #include "boolvector.h" 17 18 #include <GL/glew.h> 19 #include <wrap/gl/gl_geometry.h> 20 #include <wrap/gl/trimesh.h> 21 22 extern Logging * olgn; 23 24 struct CellToken; 25 struct Impostor ; 26 /* 27 implementation of a cell 28 */ 29 struct CellKey{ CellKeyCellKey30 CellKey(){} CellKeyCellKey31 CellKey(const int & _x, const int & _y, const int & _z, const int & _h): x(_x),y(_y),z(_z),h(_h){} CellKeyCellKey32 CellKey(const vcg::Point3f p, const int & _h): x(static_cast<int>(p[0])),y(static_cast<int>(p[1])),z(static_cast<int>(p[2])),h(_h){} 33 int x,y,z,h; P3iCellKey34 vcg::Point3i P3i() const {return vcg::Point3i(x,y,z);} P3fCellKey35 vcg::Point3f P3f() const {return vcg::Point3f(static_cast<float>(x),static_cast<float>(y),static_cast<float>(z));} GP3fCellKey36 vcg::Point3f GP3f() const { float lev = (h>=0)?(float) (1<<h): 1.f/(1<<-h); 37 return vcg::Point3f(static_cast<float>(x*lev),static_cast<float>(y*lev),static_cast<float>(z*lev));} BBox3fCellKey38 vcg::Box3f BBox3f() const {vcg::Box3f b; b.Add(GP3f()); b.Add(CellKey(x+1,y+1,z+1,h).GP3f()); return b;} 39 bool operator == (const CellKey & o) const {return (x==o.x)&&(y==o.y)&&(z==o.z)&&(h==o.h);} 40 bool operator < (const CellKey & o) const {return (x==o.x)?(y==o.y)?(z==o.z)?(h<o.h):(z<o.z):(y<o.y):(x < o.x);} 41 // h = i -> dx == dy == dz == 2^i 42 }; 43 44 /* GIndex is a global index on the hashed multigrid data structure*/ 45 struct GIndex{ GIndexGIndex46 GIndex() {SetUnassigned();} GIndexGIndex47 GIndex(const CellKey & c, const int & ii):ck(c),i(ii){} 48 CellKey ck; // cell 49 int i; // order inside cell 50 bool operator ==(const GIndex & b) const {return ( ck==b.ck) &&( i == b.i);} 51 bool operator < (const GIndex & b) const {return ( (ck == b.ck)? (i<b.i) : (ck < b.ck));} 52 SetUnassignedGIndex53 void SetUnassigned(){ i = -1; } IsUnassignedGIndex54 bool IsUnassigned() {return (i==-1);} 55 56 }; 57 58 59 /* GISet is the set of GIndex corresponding to the same vertex*/ 60 struct GISet{ 61 std::map<CellKey,unsigned int > giset; 62 unsigned int bi; 63 typedef std::map<CellKey,unsigned int >::iterator CopiesIterator; 64 typedef std::map<CellKey,unsigned int >::iterator iterator; 65 AddGISet66 void Add(GIndex gi){ giset.insert(std::make_pair(gi.ck,gi.i));} AddGISet67 void Add(std::pair <CellKey,unsigned int > p){giset.insert(p);} AddGISet68 void Add(GISet &gis){ giset.insert(gis.giset.begin(),gis.giset.end());} ClearGISet69 void Clear(){giset.clear();} IsUnassignedGISet70 bool IsUnassigned(){return giset.empty();} 71 beginGISet72 iterator begin(){return giset.begin();} endGISet73 iterator end(){return giset.end();} 74 75 const bool operator < (const GISet & o) const { 76 return giset<o.giset; 77 } 78 subGISet79 void sub(GISet & o){ 80 for(iterator i = o.begin(); i!=o.end(); ++i) 81 giset.erase((*i).first); 82 } 83 BIGISet84 unsigned int & BI(){return bi;} 85 IndexGISet86 int Index(CellKey ck){ 87 iterator gi = giset.find(ck); 88 if(gi==giset.end()) return -1; 89 else 90 return (*gi).second; 91 } 92 }; 93 94 /* 95 *** Dependence Property *** 96 97 For each face if holds that the bounding boxes of the cells that intersect the bounding box of the face 98 include the face. 99 In other terms: if cell A is crossed by the BB of face F, than the BB of cell A must contain the face F. 100 101 This must be true for all the levels BUT we use ScaleRange to limit the property to the cells that possibly 102 contain element in common with f (references to vertex of f) 103 104 */ 105 106 struct ScaleRange{ ScaleRangeScaleRange107 ScaleRange():min(1),max(-1){} ScaleRangeScaleRange108 ScaleRange( const int & _min, const int & _max):min(_min),max(_max){} AddScaleRange109 ScaleRange Add(const int & m, const int & M){ 110 if(IsNull()){min=m;max=M;} else {this->min = std::min(this->min,m); this->max = std::max(this->max,M);} return (*this);} AddScaleRange111 ScaleRange Add(const ScaleRange & sr){return Add(sr.min,sr.max);} AddScaleRange112 ScaleRange Add(const int & h){ if(this->IsNull()) {this->min = this->max = h;}else{ 113 if(h>this->max) this->max = h; else 114 if(h<this->min) this->min = h; 115 } 116 return (*this); 117 } 118 IncludeScaleRange119 bool Include(const int & h){return ( (h<=this->max) && (h >= this->min) );} IsNullScaleRange120 bool IsNull(){return min>max;} 121 bool operator ==(const ScaleRange & sr)const {return (min == sr.min) && (max==sr.max);} 122 int min,max; 123 }; 124 125 struct Box4{ Box4Box4126 Box4(){bbox3.SetNull();}; Box4Box4127 Box4(vcg::Box3f _bbox3, ScaleRange _sr):bbox3(_bbox3),sr(_sr){} AddBox4128 Box4 Add(vcg::Box3f b3, ScaleRange _sr ){ bbox3.Add(b3); sr.Add(_sr); return (*this);} AddBox4129 Box4 Add(vcg::Point3f p, int h ){ bbox3.Add(p); sr.Add(h); return (*this);} 130 vcg::Box3f bbox3; 131 ScaleRange sr; 132 bool operator ==(const Box4 & b) const {return (bbox3==b.bbox3) && (sr==b.sr);} 133 }; 134 135 struct BorderIndex{ BorderIndexBorderIndex136 BorderIndex(){} BorderIndexBorderIndex137 BorderIndex(unsigned int _vi,unsigned int _bi):vi(_vi),bi(_bi){} 138 unsigned int 139 vi, // pointer to a border vertex in the cell 140 bi; // its incremental mark 141 }; 142 143 /* per cell auxiliary data structure for edit& commit */ 144 struct EditCommitAuxData{ 145 BoolVector deleted_face; 146 BoolVector deleted_vertex; 147 BoolVector deleted_border; 148 149 FBool is_in_kernel; 150 FBool locked; 151 }; 152 153 /* per cell auxiliary data structure for rendering */ 154 struct RenderAuxData{ RenderAuxDataRenderAuxData155 RenderAuxData():renderCacheToken(0),_mesh(0){} ~RenderAuxDataRenderAuxData156 ~RenderAuxData(){if(_mesh) delete _mesh;} 157 FBool impostor_updated; 158 FBool to_render; 159 160 CellToken * renderCacheToken; 161 162 float priority; 163 MeshRenderAuxData164 vcgMesh & Mesh(){ if(_mesh==0) _mesh = new vcgMesh(); return *_mesh;} ClearMeshRenderAuxData165 void ClearMesh(){if(_mesh==0) return; _mesh->Clear(); delete _mesh; _mesh = 0;} 166 private: 167 vcgMesh * _mesh; 168 169 }; 170 171 struct Cell{ 172 CellCell173 Cell ():ecd(0),impostor(0),rd(0) {} 174 Cell(CellKey ck); 175 ~Cell(); 176 177 // position of the cell 178 CellKey key; 179 180 typedef std::map<std::string, ChainBase * > StringChainMap; 181 182 // the list of all the elements that are stored in this cell 183 // faces and vertices will be always among the elements, oher 184 // can be added (edge, string...whatever) 185 std::map<std::string, ChainBase * > elements; 186 187 // the attributes are special element that are bound to those in elements 188 // The normal per vertex, the normal per face and so on. 189 std::map<std::string, ChainBase * > perVertex_attributes; 190 std::map<std::string, ChainBase * > perFace_attributes; 191 192 193 /* Get / Set attribute values by name 194 TODO: to cache the pointer to the chain. In this implementation each single access to an attributes 195 requires a find on a map 196 */ 197 198 /// Vertex attributes 199 void GetVertexAttribute(std::string name, const unsigned int & pos, void * dst); 200 void SetVertexAttribute(std::string name, const unsigned int & pos, void * src); 201 202 /// Face attributes 203 void GetFaceAttribute(std::string name, const unsigned int & pos, void * dst); 204 void SetFaceAttribute(std::string name, const unsigned int & pos, void * src); 205 206 /// query available attributes 207 void GetPerVertexAttributeList(std::vector<std::string> & attr_list); 208 void GetPerFaceAttributeList(std::vector<std::string> & attr_list); 209 210 // remove specified faces 211 void RemoveFaces(std::vector<unsigned int> toDelete); 212 213 // remove specified vertices 214 void RemoveVertices(std::vector<unsigned int> toDelete); 215 216 // basic elements 217 /* all the faces assigned to this cell. NOTE: this is a shortcut, "face" is also in this->elements */ 218 Chain<OFace> *face; 219 220 /* all the vertices assigned to this cell. NOTE: this is a shortcut, "vert" is also in this->elements */ 221 Chain<OVertex> *vert; 222 223 /* border vertices */ 224 Chain<BorderIndex> *border; 225 226 // auxiliary data needed for edit/Commit 227 EditCommitAuxData * ecd; 228 229 // init/clear auxiliary data needed for edit/Commit 230 void InitEditCommitAuxData(); 231 void ClearEditCommitAuxData(); 232 233 234 // auxiliary data needed for rendering 235 RenderAuxData * rd; 236 237 // init/clear auxiliary data needed for rendering 238 void InitRenderAuxData(); 239 void ClearRenderAuxData(); 240 241 // return the size in RAM one the cell is loaded 242 int SizeInRAM(); 243 244 /* generic fast bool value*/ 245 FBool generic_bool; 246 247 // check if the attribute named attr_name among the elements 248 //bool HasAttribute( std::string attr_name); 249 250 // check if it is empty 251 bool IsEmpty(); 252 253 // bounding box of all the triangles that cross the cell 254 Box4 bbox; 255 256 // dependence set. All the cells that must be loaded when this one has to be edited 257 std::set<CellKey> dependence_set; 258 259 // add a face 260 int AddFace(OFace ); 261 262 // add a vertex 263 unsigned int AddVertex(OVertex ); 264 265 266 template <class VertexType> 267 void ExportVertexAttribute(std::string attr_name, const unsigned int & i, VertexType & v ); 268 269 template <class FaceType> 270 void ExportFaceAttribute(std::string attr_name, const unsigned int & i, FaceType & f ); 271 272 template <class VertexType> 273 void ImportVertexAttribute(std::string attr_name, const unsigned int & i, VertexType & v ); 274 275 template <class FaceType> 276 void ImportFaceAttribute(std::string attr_name, const unsigned int & i, FaceType & f ); 277 278 // template <class VertexType> 279 // void GetVertexAttribute(std::string name, const unsigned int & pos, VertexType & dst){ 280 // std::map<std::string, ChainBase * > ::iterator ii = perVertex_attributes.find(name); // find the chain 281 // RAssert(ii != perVertex_attributes.end()); 282 // void * dstPtr; 283 // (*ii).second->GetValue(pos,dstPtr); 284 // nafb::Get(name,dst,(*ii)) 285 // /*nafb::Call(name,dst,) 286 // (*ii).second->GetValue(pos,dst)*/; 287 // } 288 289 Impostor * impostor; 290 291 292 /* serialization */ 293 int SizeOf(); 294 char * Serialize (char * ); 295 char * DeSerialize (char * ); 296 }; 297 298 #endif 299