1 
2 #include "../utils/memory_debug.h"
3 // TODO include must be taken out of the way.....
4 #ifdef SIMPLE_DB
5 #include "../ooc_vector/io/ooc_chains.hpp"
6 #else
7 #include "../ooc_vector/io/ooc_chains_kcdb.hpp"
8 #endif
9 //...............................................
10 
11 #include "cell.h"
12 #include "impostor_definition.h"
13 
~Cell()14 Cell::~Cell(){
15 		if(impostor)
16 			delete impostor;
17 
18 #ifndef _TEMP_BUILDER_MODE_
19 		ClearEditCommitAuxData();
20 		ClearRenderAuxData();
21 #endif
22 }
Cell(CellKey ck)23 Cell::Cell(CellKey ck):key(ck),impostor(0),rd(0),ecd(0){
24 		int mem;
25 		MemDbg::SetPoint(1);
26 		impostor = new Impostor();
27 		mem = MemDbg::MemFromPoint(1);
28 #ifndef _TEMP_BUILDER_MODE_
29 		InitEditCommitAuxData();
30 		InitRenderAuxData();
31 #endif
32 }
33 
InitEditCommitAuxData()34 void Cell::InitEditCommitAuxData() { if(!ecd) ecd = new EditCommitAuxData();}
ClearEditCommitAuxData()35 void Cell::ClearEditCommitAuxData(){ if(ecd) delete ecd; ecd = 0;}
InitRenderAuxData()36 void Cell::InitRenderAuxData()     { if(!rd) rd = new RenderAuxData();}
ClearRenderAuxData()37 void Cell::ClearRenderAuxData()    { if(rd) delete rd; rd = 0;}
SizeInRAM()38 int	 Cell::SizeInRAM()			   {
39 									int siz = 0;
40 									siz += face->Size()*sizeof(vcgFace) + vert->Size() * sizeof(vcgVertex);
41 									return siz;
42 									}
43 
44 /// Vertex attributes
45 
GetVertexAttribute(std::string name,const unsigned int & pos,void * dst)46 void Cell::GetVertexAttribute(std::string name, const unsigned int & pos, void * dst){
47     std::map<std::string, ChainBase *  > ::iterator ii = perVertex_attributes.find(name); // find the chain
48     RAssert(ii != perVertex_attributes.end());
49     (*ii).second->GetValue(pos,dst);
50 }
SetVertexAttribute(std::string name,const unsigned int & pos,void * src)51 void Cell::SetVertexAttribute(std::string name, const unsigned int & pos, void * src){
52     std::map<std::string, ChainBase *  > ::iterator ii = perVertex_attributes.find(name); // find the chain
53     RAssert(ii != perVertex_attributes.end());
54     (*ii).second->SetValue(pos,src);
55 }
56 
57 /// Face attributes
GetFaceAttribute(std::string name,const unsigned int & pos,void * dst)58 void Cell::GetFaceAttribute(std::string name, const unsigned int & pos, void * dst){
59     std::map<std::string, ChainBase *  > ::iterator ii = perFace_attributes.find(name); // find the chain
60     RAssert(ii != perFace_attributes.end());
61     (*ii).second->GetValue(pos,dst);
62 }
SetFaceAttribute(std::string name,const unsigned int & pos,void * src)63 void Cell::SetFaceAttribute(std::string name, const unsigned int & pos, void * src){
64     std::map<std::string, ChainBase *  > ::iterator ii = perFace_attributes.find(name); // find the chain
65     RAssert(ii != perFace_attributes.end());
66     (*ii).second->SetValue(pos,src);
67 }
68 
GetPerVertexAttributeList(std::vector<std::string> & attr_list)69 void Cell::GetPerVertexAttributeList(std::vector<std::string> & attr_list){
70 	std::map<std::string, ChainBase *  >::iterator fai;
71 	for(fai = perVertex_attributes.begin(); fai != perVertex_attributes.end(); ++fai) attr_list.push_back((*fai).first);
72 }
GetPerFaceAttributeList(std::vector<std::string> & attr_list)73 void Cell::GetPerFaceAttributeList(std::vector<std::string> & attr_list){
74 	std::map<std::string, ChainBase *  >::iterator fai;
75 	for(fai = perFace_attributes.begin(); fai != perFace_attributes.end(); ++fai) attr_list.push_back((*fai).first);
76 }
77 
78 
RemoveFaces(std::vector<unsigned int> toDelete)79 void Cell::RemoveFaces(std::vector<unsigned int> toDelete){
80 	std::map<std::string, ChainBase *  >::iterator ai;
81 	face->Compact(toDelete);
82 	for(ai = perVertex_attributes.begin(); ai != perVertex_attributes.end();++ai) (*ai).second->Compact(toDelete);
83 }
84 
RemoveVertices(std::vector<unsigned int> toDelete)85 void Cell::RemoveVertices(std::vector<unsigned int> toDelete){
86 	std::map<std::string, ChainBase *  >::iterator ai;
87 	vert->Compact(toDelete);
88 	for(ai = perFace_attributes.begin(); ai != perFace_attributes.end();++ai) (*ai).second->Compact(toDelete);
89 }
90 
91 
SizeOf()92 int Cell::SizeOf(){
93 	std::map<std::string, ChainBase *  >::iterator ai;
94 
95 	int size = sizeof(CellKey);
96 	size+= sizeof( Box4);
97 
98 	size+= sizeof(unsigned int );						// size of the dependence set
99 	size+= sizeof(CellKey) * dependence_set.size();		//dependence set
100 
101 	size+= sizeof(int); // elements.size
102 	for(ai = elements.begin(); ai != elements.end(); ++ai){
103 		size += ::SizeOf((*ai).first);
104 	}
105 	size+= sizeof(int); // perVertex_attributes.size
106 	for(ai = perVertex_attributes.begin(); ai != perVertex_attributes.end(); ++ai){
107 		size += ::SizeOf((*ai).first);
108 	}
109 	size+= sizeof(int); // perFace_attributes.size
110 	for(ai = perFace_attributes.begin(); ai != perFace_attributes.end(); ++ai){
111 		size += ::SizeOf((*ai).first);
112 	}
113 
114 	return size;
115 }
Serialize(char * ptr)116 char * Cell::Serialize (char * ptr){
117 	std::map<std::string, ChainBase *  >::iterator ai;
118 	std::set<CellKey >::iterator ci;
119 
120 	memcpy(ptr,&(this->key),sizeof(int)*4);					ptr+=sizeof(int)	*4	;
121 	memcpy(ptr,& this->bbox,sizeof(Box4) );					ptr+=sizeof(Box4);
122 
123 	*(unsigned int* )ptr =  dependence_set.size();			ptr+=sizeof(unsigned int);
124 	for(ci = dependence_set.begin(); ci != dependence_set.end(); ++ci)
125 	{memcpy(ptr,&(*ci),sizeof(CellKey));	ptr+=sizeof(CellKey);}
126 
127 	*((int*)ptr) = (int) elements.size();				ptr+=sizeof(int);
128 	for(ai = elements.begin(); ai != elements.end(); ++ai ){
129 		ptr = ::Serialize((*ai).first,ptr);}
130 
131 	*((int*)ptr) = (int) perVertex_attributes.size();	ptr+=sizeof(int);
132 	for(ai = perVertex_attributes.begin(); ai != perVertex_attributes.end(); ++ai ){
133 		ptr = ::Serialize((*ai).first,ptr);}
134 
135 	*((int*)ptr) = (int) perFace_attributes.size();		ptr+=sizeof(int);
136 	for(ai = perFace_attributes.begin(); ai != perFace_attributes.end(); ++ai ){
137 		ptr = ::Serialize((*ai).first,ptr);}
138 
139 	return ptr;
140 }
DeSerialize(char * buffer)141 char * Cell::DeSerialize (char * buffer ){
142 	std::string attr_name;
143 	char * ptr = buffer;
144 	memcpy(&(this->key),ptr,sizeof(int)*4);					ptr+=sizeof(int)	*4	;
145 	memcpy(&(this->bbox),ptr,sizeof(Box4));					ptr+=sizeof(Box4)		;
146 
147 	unsigned int ds_size = *(unsigned int* )ptr;			ptr+=sizeof(unsigned int);
148 	for(unsigned int i = 0; i < ds_size; ++i){
149 		CellKey ck;
150 		memcpy(&ck,ptr,sizeof(CellKey));
151 		dependence_set.insert(ck);
152 		ptr+=sizeof(CellKey);
153 	}
154 
155 	int n_attr = *((int*)ptr);								ptr+=sizeof(int);
156 	for(int i = 0; i < n_attr; ++i){
157 		ptr = ::DeSerialize(ptr,attr_name);	// read the attribute name
158 		elements.insert( std::pair<std::string, ChainBase *>(attr_name,NULL) );
159 	}
160 
161 	n_attr = *((int*)ptr);									ptr+=sizeof(int);
162 	for(int i = 0; i < n_attr; ++i){
163 		ptr = ::DeSerialize(ptr,attr_name);	// read the attribute name
164 		perVertex_attributes.insert( std::pair<std::string, ChainBase *>(attr_name,NULL) );
165 	}
166 
167 	n_attr = *((int*)ptr);									ptr+=sizeof(int);
168 	for(int i = 0; i < n_attr; ++i){
169 		ptr = ::DeSerialize(ptr,attr_name);	// read the attribute name
170 		perFace_attributes.insert( std::pair<std::string, ChainBase *>(attr_name,NULL) );
171 	}
172 	return ptr;
173 }
174 
IsEmpty()175 bool Cell::IsEmpty(){
176 	for(std::map<std::string, ChainBase *  >::iterator mi =  this->elements.begin(); mi != this->elements.end();++mi)
177 				if( (*mi).second->Size() > 0) return false;
178 	return true;
179 }
180 
AddFace(OFace f)181 int Cell::AddFace(OFace  f){
182 	// for each attribute add an element to the corresponding chain
183 	for(StringChainMap::iterator i = perFace_attributes.begin();  i != perFace_attributes.end(); ++i)
184 		(*i).second->Resize(vert->Size() + 1 );
185 
186 	return face->AddElem(f);
187 }
188 
AddVertex(OVertex v)189 unsigned int Cell::AddVertex(OVertex  v){
190 	bbox.bbox3.Add(v.P());
191 
192 	// for each attribute add an element to the corresponding chain
193 	for(StringChainMap::iterator i = perVertex_attributes.begin();  i != perVertex_attributes.end(); ++i)
194 		(*i).second->Resize(vert->Size() + 1 );
195 
196 	return vert->AddElem(v);
197 
198 }
199