mesh.cpp 65.2 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
4
5
6
7
#include "inmost.h"
#include <sstream>
#if defined(USE_MESH)
#define WAITNL 	{char c;scanf("%c",&c);}

//#define HEAVY_DUPLICATE_CHECK //depricated, left for future debug

Kirill Terekhov's avatar
Kirill Terekhov committed
8
#include <unordered_map>
Kirill Terekhov's avatar
Kirill Terekhov committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

namespace INMOST
{
	int CompareElementsCPointer(const void * pa, const void * pb);
	
	//~ void Mesh::GetElementsBySparseTag(Tag t, ElementType types, std::vector<Element *> & ret) const
	//~ {
		//~ for(ElementType mask = NODE; mask <= CELL; mask = mask << 1)
			//~ if( mask & types && t.isDefined(mask) && t.isSparse(mask) )
			//~ {
				//~ TagManager::sparse_sub_type & s = t.GetTagManager()->GetSparseData(t.GetPosition(mask));
				//~ for(TagManager::sparse_sub_type::iterator it = s.begin(); it != s.end(); ++it)
					//~ ret.push_back(reinterpret_cast<Element *>(it->first));
			//~ }
	//~ }
	
	Mesh::Mesh()
Kirill Terekhov's avatar
Kirill Terekhov committed
26
27
28
29
30
31
32
33
	:TagManager(),Storage(this,MESH)
	//for chunk_array
	,
	cells(1),empty_cells(1),
	faces(1),empty_faces(1),
	edges(1),empty_edges(1),
	nodes(1),empty_nodes(1),
	sets(1), empty_sets(1)
Kirill Terekhov's avatar
Kirill Terekhov committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
	{
		
		
		
		dim = 3;
		have_global_id = NONE;
		checkset = DEFAULT_CHECK;
		errorset = 0;
		new_element = hide_element = 0;

		memset(remember,0,sizeof(remember));
		
		//~ nodes.reserve(2048);
		//~ edges.reserve(4096);
		//~ faces.reserve(4096);
		//~ cells.reserve(1024);
		
		//tag_global_id = CreateTag("GLOBAL_ID",DATA_INTEGER, ESET | CELL | FACE | EDGE | NODE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
52
		tag_coords        = CreateTag("COORD",DATA_REAL, NODE,NONE,dim);
Kirill Terekhov's avatar
Kirill Terekhov committed
53
		tag_topologyerror = CreateTag("TOPOLOGY_ERROR_TAG",DATA_INTEGER,CELL | FACE | EDGE,CELL | FACE | EDGE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
54
55
56
57
58
59
60
61
#if defined(NEW_CONNECTIONS)
		tag_high_conn     = CreateTag("HIGH_CONN",DATA_REFERENCE,CELL|FACE|EDGE|NODE,NONE);
		tag_low_conn      = CreateTag("LOW_CONN",DATA_REFERENCE,CELL|FACE|EDGE|NODE,NONE);
#endif
#if defined(NEW_MARKERS)
		tag_markers       = CreateTag("MARKERS",DATA_BULK,CELL|FACE|EDGE|NODE|ESET|MESH,NONE,MarkerFields);
#endif
		tag_geom_type     = CreateTag("GEOM_TYPE",DATA_BULK,CELL|FACE|EDGE|NODE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
62
63
		//tag_shared_elems = CreateTag("SHARED_ELEMS_TAG",DATA_INTEGER,CELL|FACE|EDGE|NODE,NONE,1);

Kirill Terekhov's avatar
Kirill Terekhov committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
		epsilon = 1.0e-8;
		m_state = Mesh::Serial;

#if defined(USE_MPI)
		{
			int test;
			MPI_Initialized(&test);
			if( test == 0 ) MPI_Init(NULL,NULL);
			comm = INMOST_MPI_COMM_WORLD;
		}
#endif

#if defined(USE_PARALLEL_WRITE_TIME)
		num_exchanges = 0;
		std::stringstream temp;
		temp << "time_" << GetProcessorRank() << ".xml";
		out_time.open(temp.str().c_str(),std::ios::out);
		out_time << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" << std::endl;
		out_time << "<?xml-stylesheet type=\"text/xsl\" href=\"style.xsl\"?>" << std::endl;
		out_time << "<Debug>" << std::endl;
		tab = 1;
		func_id = 0;
#endif
	}
	
	Mesh::Mesh(const Mesh & other)
	:TagManager(this,other),Storage(this,0,other),
	empty_cells(other.empty_cells),empty_faces(other.empty_faces),
Kirill Terekhov's avatar
Kirill Terekhov committed
92
93
94
95
96
97
98
99
	empty_edges(other.empty_edges),empty_nodes(other.empty_nodes),
	empty_sets(other.empty_sets)
	,
	cells(1),
	faces(1),
	edges(1),
	nodes(1),
	sets(1) 
Kirill Terekhov's avatar
Kirill Terekhov committed
100
101
	{
		INMOST_DATA_ENUM_TYPE i;
Kirill Terekhov's avatar
Kirill Terekhov committed
102
103
104
105
106
#if defined(NEW_MARKERS)
		Storage::bulk marker_space[MarkerFields];
		other.GetMarkerSpace(marker_space);
		SetMarkerSpace(marker_space);
#else
Kirill Terekhov's avatar
Kirill Terekhov committed
107
		markers = other.GetMarkerSpace();
Kirill Terekhov's avatar
Kirill Terekhov committed
108
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
109
110
111
112
113
114
115
116
117
118
119
120
121
		m_state = other.m_state;
		
		checkset = other.checkset;
		errorset = other.errorset;

		new_element = other.new_element;
		hide_element = other.hide_element;
		
		memcpy(remember,other.remember,sizeof(remember));
		
		dim = other.dim;

		
Kirill Terekhov's avatar
Kirill Terekhov committed
122
		tag_coords        = CreateTag("COORD",DATA_REAL, NODE,NONE,dim);
Kirill Terekhov's avatar
Kirill Terekhov committed
123
		tag_topologyerror = CreateTag("TOPOLOGY_ERROR_TAG",DATA_INTEGER,CELL | FACE | EDGE,CELL | FACE | EDGE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
124
125
126
127
128
129
130
131
#if defined(NEW_CONNECTIONS)
		tag_high_conn     = CreateTag("HIGH_CONN",DATA_REFERENCE,CELL|FACE|EDGE|NODE,NONE);
		tag_low_conn      = CreateTag("LOW_CONN",DATA_REFERENCE,CELL|FACE|EDGE|NODE,NONE);
#endif
#if defined(NEW_MARKERS)
		tag_markers       = CreateTag("MARKERS",DATA_BULK,CELL|FACE|EDGE|NODE|ESET|MESH,NONE,MarkerFields);
#endif
		tag_geom_type     = CreateTag("GEOM_TYPE",DATA_BULK,CELL|FACE|EDGE|NODE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
132
		//tag_shared_elems = CreateTag("SHARED_ELEMS_TAG",DATA_INTEGER,CELL|FACE|EDGE|NODE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
133
134
135
136
137
138
139
140
141
142
143
		
		if( m_state == Mesh::Parallel ) SetCommunicator(other.comm);
		else comm = INMOST_MPI_COMM_WORLD;
		
		RestoreGeometricTags();

		//~ nodes.reserve(other.nodes.size());
		//~ edges.reserve(other.edges.size());
		//~ faces.reserve(other.faces.size());
		//~ cells.reserve(other.cells.size());
		//~ sets.reserve(other.sets.size());
Kirill Terekhov's avatar
Kirill Terekhov committed
144
145
		for(ElementType etype = NODE; etype <= MESH; etype = etype << 1 )
			ReallocateData(etype);
Kirill Terekhov's avatar
Kirill Terekhov committed
146

Kirill Terekhov's avatar
Kirill Terekhov committed
147
		/*
Kirill Terekhov's avatar
Kirill Terekhov committed
148
149
150
151
		for(iteratorTag t = BeginTag(); t != EndTag(); t++)
			for(ElementType etype = NODE; etype <= MESH; etype = etype << 1 )
				if( t->isDefined(etype) && !t->isSparse(etype) )
					t->AllocateData(etype);
Kirill Terekhov's avatar
Kirill Terekhov committed
152
		*/
Kirill Terekhov's avatar
Kirill Terekhov committed
153
154
155
156
157
158
159
160
161
		
		for(nodes_container::const_iterator it = other.nodes.begin(); it != other.nodes.end(); it++) if( (*it) != NULL ) nodes.push_back(new Node(this,(*it)->LocalID(),*(*it))); else nodes.push_back(NULL);
		for(edges_container::const_iterator it = other.edges.begin(); it != other.edges.end(); it++) if( (*it) != NULL ) edges.push_back(new Edge(this,(*it)->LocalID(),*(*it))); else edges.push_back(NULL);
		for(faces_container::const_iterator it = other.faces.begin(); it != other.faces.end(); it++) if( (*it) != NULL ) faces.push_back(new Face(this,(*it)->LocalID(),*(*it))); else faces.push_back(NULL);
		for(cells_container::const_iterator it = other.cells.begin(); it != other.cells.end(); it++) if( (*it) != NULL ) cells.push_back(new Cell(this,(*it)->LocalID(),*(*it))); else cells.push_back(NULL);
		for(sets_container::const_iterator it = other.sets.begin(); it != other.sets.end(); it++) if( (*it) != NULL ) sets.push_back(new ElementSet(this,(*it)->LocalID(),*(*it))); else sets.push_back(NULL);
		i = 0;
		for(nodes_container::const_iterator it = other.nodes.begin(); it != other.nodes.end(); it++, i++) if( (*it) != NULL )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
162
163
			for(Element::adj_iterator jt = (*it)->LowConn().begin(); jt != (*it)->LowConn().end(); jt++) nodes[i]->LowConn().push_back(cells[(*jt)->LocalID()]);
			for(Element::adj_iterator jt = (*it)->HighConn().begin(); jt != (*it)->HighConn().end(); jt++) nodes[i]->HighConn().push_back(edges[(*jt)->LocalID()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
164
165
166
167
		}
		i = 0;
		for(edges_container::const_iterator it = other.edges.begin(); it != other.edges.end(); it++, i++) if( (*it) != NULL )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
168
169
			for(Element::adj_iterator jt = (*it)->LowConn().begin(); jt != (*it)->LowConn().end(); jt++) edges[i]->LowConn().push_back(nodes[(*jt)->LocalID()]);
			for(Element::adj_iterator jt = (*it)->HighConn().begin(); jt != (*it)->HighConn().end(); jt++) edges[i]->HighConn().push_back(faces[(*jt)->LocalID()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
170
171
172
173
		}
		i = 0;
		for(faces_container::const_iterator it = other.faces.begin(); it != other.faces.end(); it++, i++) if( (*it) != NULL )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
174
175
			for(Element::adj_iterator jt = (*it)->LowConn().begin(); jt != (*it)->LowConn().end(); jt++) faces[i]->LowConn().push_back(edges[(*jt)->LocalID()]);
			for(Element::adj_iterator jt = (*it)->HighConn().begin(); jt != (*it)->HighConn().end(); jt++) faces[i]->HighConn().push_back(cells[(*jt)->LocalID()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
176
177
178
179
		}
		i = 0;
		for(cells_container::const_iterator it = other.cells.begin(); it != other.cells.end(); it++, i++) if( (*it) != NULL )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
180
181
			for(Element::adj_iterator jt = (*it)->LowConn().begin(); jt != (*it)->LowConn().end(); jt++) cells[i]->LowConn().push_back(faces[(*jt)->LocalID()]);
			for(Element::adj_iterator jt = (*it)->HighConn().begin(); jt != (*it)->HighConn().end(); jt++) cells[i]->HighConn().push_back(nodes[(*jt)->LocalID()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
		}
		i = 0;
		for(sets_container::const_iterator it = other.sets.begin(); it != other.sets.end(); it++, i++) if( (*it) != NULL )
		{
			for(ElementSet::iterator jt = (*it)->begin(); jt != (*it)->end(); jt++)
				switch(jt->GetElementType())
				{
					case NODE: sets[i]->Insert(nodes[jt->LocalID()]); break;
					case EDGE: sets[i]->Insert(edges[jt->LocalID()]); break;
					case FACE: sets[i]->Insert(faces[jt->LocalID()]); break;
					case CELL: sets[i]->Insert(cells[jt->LocalID()]); break;
				}
		}
		epsilon = other.epsilon;
		AssignGlobalID(other.have_global_id);
	}
	
	Mesh & Mesh::operator =(Mesh const & other)
	{
		INMOST_DATA_ENUM_TYPE i;
		if( this == &other ) return *this; //don't do anything
		//first delete everithing
		
		for(sets_container::iterator it = sets.begin(); it != sets.end(); it++) if( *it != NULL ) delete *it;
		for(cells_container::iterator it = cells.begin(); it != cells.end(); it++) if( *it != NULL ) delete *it;
		for(faces_container::iterator it = faces.begin(); it != faces.end(); it++) if( *it != NULL ) delete *it;
		for(edges_container::iterator it = edges.begin(); it != edges.end(); it++) if( *it != NULL ) delete *it;
		for(nodes_container::iterator it = nodes.begin(); it != nodes.end(); it++) if( *it != NULL ) delete *it;
#if defined(USE_MPI)
#if defined(USE_MPI2)
		if( m_state == Mesh::Parallel )
		{
			MPI_Free_mem(shared_space);
			MPI_Win_free(&window);
		}
#endif
#endif
		nodes.clear();
		edges.clear();
		faces.clear();
		cells.clear();
		sets.clear();
		empty_cells.clear();
		empty_faces.clear();
		empty_edges.clear();
		empty_nodes.clear();
		empty_sets.clear();

		
		TagManager::assign(this,other);
		Storage::assign(this,0,other);
Kirill Terekhov's avatar
Kirill Terekhov committed
233

Kirill Terekhov's avatar
Kirill Terekhov committed
234
235
236
237
238
239
240
		m_state = other.m_state;
		dim = other.dim;
		checkset = other.checkset;
		errorset = other.errorset;
		memcpy(remember,other.remember,sizeof(remember));

		
Kirill Terekhov's avatar
Kirill Terekhov committed
241
		tag_coords        = CreateTag("COORD",DATA_REAL, NODE,NONE,dim);
Kirill Terekhov's avatar
Kirill Terekhov committed
242
		tag_topologyerror = CreateTag("TOPOLOGY_ERROR_TAG",DATA_INTEGER,CELL | FACE | EDGE,CELL | FACE | EDGE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
243
244
245
246
247
248
249
250
#if defined(NEW_CONNECTIONS)
		tag_high_conn     = CreateTag("HIGH_CONN",DATA_REFERENCE,CELL|FACE|EDGE|NODE,NONE);
		tag_low_conn      = CreateTag("LOW_CONN",DATA_REFERENCE,CELL|FACE|EDGE|NODE,NONE);
#endif
#if defined(NEW_MARKERS)
		tag_markers       = CreateTag("MARKERS",DATA_BULK,CELL|FACE|EDGE|NODE|ESET|MESH,NONE,MarkerFields);
#endif
		tag_geom_type     = CreateTag("GEOM_TYPE",DATA_BULK,CELL|FACE|EDGE|NODE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
251
		//tag_shared_elems = CreateTag("SHARED_ELEMS_TAG",DATA_INTEGER,CELL|FACE|EDGE|NODE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
		
		
		if( m_state == Mesh::Parallel ) SetCommunicator(other.comm);
		else comm = INMOST_MPI_COMM_WORLD;

		RestoreGeometricTags();

		
		empty_cells = other.empty_cells;
		empty_faces = other.empty_faces;
		empty_edges = other.empty_edges;
		empty_nodes = other.empty_nodes;
		empty_sets = other.empty_sets; 

		//~ nodes.reserve(other.nodes.size());
		//~ edges.reserve(other.edges.size());
		//~ faces.reserve(other.faces.size());
		//~ cells.reserve(other.cells.size());
		//~ sets.reserve(other.sets.size());

Kirill Terekhov's avatar
Kirill Terekhov committed
272
273
274
		for(ElementType etype = NODE; etype <= MESH; etype = etype << 1 )
			ReallocateData(etype);
		/*
Kirill Terekhov's avatar
Kirill Terekhov committed
275
276
277
278
		for(iteratorTag t = BeginTag(); t != EndTag(); t++)
			for(ElementType etype = NODE; etype <= MESH; etype = etype << 1 )
				if( t->isDefined(etype) && !t->isSparse(etype) )
					t->AllocateData(etype);
Kirill Terekhov's avatar
Kirill Terekhov committed
279
		*/
Kirill Terekhov's avatar
Kirill Terekhov committed
280
281
282
283
284
285
286
287
		for(nodes_container::const_iterator it = other.nodes.begin(); it != other.nodes.end(); it++) if( (*it) != NULL ) nodes.push_back(new Node(this,(*it)->LocalID(),*(*it))); else nodes.push_back(NULL);
		for(edges_container::const_iterator it = other.edges.begin(); it != other.edges.end(); it++) if( (*it) != NULL ) edges.push_back(new Edge(this,(*it)->LocalID(),*(*it))); else edges.push_back(NULL);
		for(faces_container::const_iterator it = other.faces.begin(); it != other.faces.end(); it++) if( (*it) != NULL ) faces.push_back(new Face(this,(*it)->LocalID(),*(*it))); else faces.push_back(NULL);
		for(cells_container::const_iterator it = other.cells.begin(); it != other.cells.end(); it++) if( (*it) != NULL ) cells.push_back(new Cell(this,(*it)->LocalID(),*(*it))); else cells.push_back(NULL);
		for(sets_container::const_iterator it = other.sets.begin(); it != other.sets.end(); it++) if( (*it) != NULL ) sets.push_back(new ElementSet(this,(*it)->LocalID(),*(*it))); else sets.push_back(NULL);
		i = 0;
		for(nodes_container::const_iterator it = other.nodes.begin(); it != other.nodes.end(); it++, i++) if( (*it) != NULL )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
288
289
			for(Element::adj_iterator jt = (*it)->LowConn().begin(); jt != (*it)->LowConn().end(); jt++) nodes[i]->LowConn().push_back(cells[(*jt)->LocalID()]);
			for(Element::adj_iterator jt = (*it)->HighConn().begin(); jt != (*it)->HighConn().end(); jt++) nodes[i]->HighConn().push_back(edges[(*jt)->LocalID()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
290
291
292
293
		}
		i = 0;
		for(edges_container::const_iterator it = other.edges.begin(); it != other.edges.end(); it++, i++) if( (*it) != NULL )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
294
295
			for(Element::adj_iterator jt = (*it)->LowConn().begin(); jt != (*it)->LowConn().end(); jt++) edges[i]->LowConn().push_back(nodes[(*jt)->LocalID()]);
			for(Element::adj_iterator jt = (*it)->HighConn().begin(); jt != (*it)->HighConn().end(); jt++) edges[i]->HighConn().push_back(faces[(*jt)->LocalID()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
296
297
298
299
		}
		i = 0;
		for(faces_container::const_iterator it = other.faces.begin(); it != other.faces.end(); it++, i++) if( (*it) != NULL )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
300
301
			for(Element::adj_iterator jt = (*it)->LowConn().begin(); jt != (*it)->LowConn().end(); jt++) faces[i]->LowConn().push_back(edges[(*jt)->LocalID()]);
			for(Element::adj_iterator jt = (*it)->HighConn().begin(); jt != (*it)->HighConn().end(); jt++) faces[i]->HighConn().push_back(cells[(*jt)->LocalID()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
302
303
304
305
		}
		i = 0;
		for(cells_container::const_iterator it = other.cells.begin(); it != other.cells.end(); it++, i++) if( (*it) != NULL )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
306
307
			for(Element::adj_iterator jt = (*it)->LowConn().begin(); jt != (*it)->LowConn().end(); jt++) cells[i]->LowConn().push_back(faces[(*jt)->LocalID()]);
			for(Element::adj_iterator jt = (*it)->HighConn().begin(); jt != (*it)->HighConn().end(); jt++) cells[i]->HighConn().push_back(nodes[(*jt)->LocalID()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
		}
		i = 0;
		for(sets_container::const_iterator it = other.sets.begin(); it != other.sets.end(); it++, i++) if( (*it) != NULL )
		{
			for(ElementSet::iterator jt = (*it)->begin(); jt != (*it)->end(); jt++)
				switch(jt->GetElementType())
				{
					case NODE: sets[i]->Insert(nodes[jt->LocalID()]); break;
					case EDGE: sets[i]->Insert(edges[jt->LocalID()]); break;
					case FACE: sets[i]->Insert(faces[jt->LocalID()]); break;
					case CELL: sets[i]->Insert(cells[jt->LocalID()]); break;
				}
		}
		epsilon = other.epsilon;
		AssignGlobalID(other.have_global_id);
		return *this;
	}
	
	Mesh::~Mesh()
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
328
329
		//Mesh::iteratorTag t = BeginTag();
		//while(t != EndTag()) DeleteTag(*t);
Kirill Terekhov's avatar
Kirill Terekhov committed
330
		for(sets_container::iterator it = sets.begin(); it != sets.end(); it++) if( *it != NULL ) delete *it;
Kirill Terekhov's avatar
Kirill Terekhov committed
331
332
		for(cells_container::iterator it = cells.begin(); it != cells.end(); it++) if( *it != NULL ) 
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
333
334
			(*it)->HighConn().clear();
			(*it)->LowConn().clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
335
336
337
338
			delete *it;
		}
		for(faces_container::iterator it = faces.begin(); it != faces.end(); it++) if( *it != NULL ) 
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
339
340
			(*it)->HighConn().clear();
			(*it)->LowConn().clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
341
342
343
344
			delete *it;
		}
		for(edges_container::iterator it = edges.begin(); it != edges.end(); it++) if( *it != NULL ) 
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
345
346
			(*it)->HighConn().clear();
			(*it)->LowConn().clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
347
348
349
350
			delete *it;
		}
		for(nodes_container::iterator it = nodes.begin(); it != nodes.end(); it++) if( *it != NULL ) 
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
351
352
			(*it)->HighConn().clear();
			(*it)->LowConn().clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
353
354
			delete *it;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#if defined(USE_MPI)
#if defined(USE_MPI2)
		if( m_state == Mesh::Parallel )
		{
			MPI_Free_mem(shared_space);
			MPI_Win_free(&window);
			//~ MPI_Comm_free(&comm);
		}
#endif
#endif
#if defined(USE_PARALLEL_WRITE_TIME)
		out_time << "</Debug>" << std::endl;
		out_time.close();
#endif
	}
	
	
	
	Tag Mesh::CreateTag(std::string name, DataType dtype, ElementType etype,ElementType sparse, INMOST_DATA_ENUM_TYPE size)
	{
		Tag ret = TagManager::CreateTag(this,name,dtype,etype,sparse,size);
		return ret;
	}
	Tag Mesh::DeleteTag(Tag tag, ElementType type_mask)
	{
		for(ElementType mask = NODE; mask <= MESH; mask = mask << 1 ) if( mask & type_mask )
		{
			if( tag.isDefined(mask) && (tag.isSparse(mask) || tag.GetSize() == ENUMUNDEF) )
				for(iteratorElement it = BeginElement(mask); it != EndElement(); it++)
					it->DelData(tag);
		}
		tag = TagManager::DeleteTag(tag,type_mask);
		return tag;
	}
	
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
392
	Element * Mesh::FindSharedAdjacency(Element * const * arr, unsigned s) 
Kirill Terekhov's avatar
Kirill Terekhov committed
393
394
395
396
397
	{
		if( s == 0 ) return NULL;
		if( !HideMarker() )
		{
			/*
Kirill Terekhov's avatar
Kirill Terekhov committed
398
399
			Element::adj_type const & hc = arr[0]->HighConn();
			dynarray<Element *, 64> inter(hc.data(),hc.data()+hc.size());
Kirill Terekhov's avatar
Kirill Terekhov committed
400
			MarkerType mrk = CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
401
402
			for(unsigned i = 1; i < s; i++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
403
404
				Element::adj_type const & ihc = arr[i]->HighConn();
				for(Element::adj_type::enumerator jt = 0; jt < ihc.size(); ++jt) ihc[jt]->SetMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
405
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
406
					int m = 0, n = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
407
408
409
410
411
412
413
414
					while( m < inter.size() ) 
					{
						if( inter[m]->GetMarker(mrk) )
							inter[n++] = inter[m];
						m++;
					}
					inter.resize(n);
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
415
				for(Element::adj_type::enumerator jt = 0; jt < ihc.size(); ++jt) ihc[jt]->RemMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
416
417
418
419
420
421
422
423
424
425
				if( inter.empty() )
				{
					ReleaseMarker(mrk);
					return NULL;
				}
			}	
			ReleaseMarker(mrk);
			return inter[0];
			*/
			/*
Kirill Terekhov's avatar
Kirill Terekhov committed
426
			if( arr[0]->GetElementType() == NODE )// this is edge
Kirill Terekhov's avatar
Kirill Terekhov committed
427
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
				//there are 2 nodes
				if( s == 2 )
				{
					//check that any of edges of current node links to the other node
					Element::adj_type const & hc = arr[0]->HighConn();
					int i, iend = hc.size(), ss = s;
					for(i = 0; i < iend; ++i)
					{
						Element::adj_type const & lc = hc[i]->LowConn();
						//assert(lc.size() == 2);
						if( (lc[0] == arr[1] || lc[1] == arr[1]) ) return hc[i];
					}
				}
				else if( s == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
442
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
443
444
					Element::adj_type const & hc = arr[0]->HighConn();
					if( !hc.empty() ) return hc.front();
Kirill Terekhov's avatar
Kirill Terekhov committed
445
446
447
448
449
450
				}
				return NULL;
			}
			else
			*/
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
451
452
453
454
455
				int flag0, flag1, i , ss = s;
				dynarray<Element::adj_type const *, 64> hcarr(ss);
				for(i = 0; i < ss; i++) hcarr[i] = &arr[i]->HighConn();
				int it, iend = hcarr[0]->size(), jt, jend;
				for(it = 0; it < iend; ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
456
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
457
458
					flag0 = 0;
					for(i = 1; i < ss; ++i)
Kirill Terekhov's avatar
Kirill Terekhov committed
459
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
460
461
462
						jend = hcarr[i]->size();
						flag1 = 0;
						for(jt = 0; jt < jend; ++jt)
Kirill Terekhov's avatar
Kirill Terekhov committed
463
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
464
							if( hcarr[0]->at(it) == hcarr[i]->at(jt) )
Kirill Terekhov's avatar
Kirill Terekhov committed
465
466
							{
								flag0++;
Kirill Terekhov's avatar
Kirill Terekhov committed
467
								flag1 = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
468
469
470
								break;
							}
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
471
						if( flag1 == 0 ) break;
Kirill Terekhov's avatar
Kirill Terekhov committed
472
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
473
					if( flag0 == ss-1 ) return hcarr[0]->at(it);
Kirill Terekhov's avatar
Kirill Terekhov committed
474
475
476
477
478
479
480
481
482
483
484
485
				}
			}
			
			/*
			{
				//typedef std::map<Element *,int> set_type;
				//typedef tiny_map<Element *,int,64> set_type;
				//typedef small_hash<unsigned,int,64> set_type;
				typedef std::unordered_map<unsigned,int> set_type;
				set_type visits;
				for(unsigned i = 0; i < s; ++i)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
486
					unsigned jend = arr[i]->HighConn().size();
Kirill Terekhov's avatar
Kirill Terekhov committed
487
					for(unsigned jt = 0; jt < jend; ++jt)
Kirill Terekhov's avatar
Kirill Terekhov committed
488
						visits[reinterpret_cast<unsigned>(arr[i]->HighConn()[jt])]++;
Kirill Terekhov's avatar
Kirill Terekhov committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
				}
				for(set_type::iterator it = visits.begin(); it != visits.end(); ++it)
					if( it->second == s ) return reinterpret_cast<Element *>(it->first);
			}
			*/
			/*
			{
				//typedef std::map<Element *,int> set_type;
				//typedef tiny_map<Element *,int,64> set_type;
				//typedef small_hash<unsigned,int,64> set_type;
				Element * ret = NULL;
				unsigned q = s;
				for(unsigned i = 0; i < s && !ret; ++i)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
503
					unsigned jend = arr[i]->HighConn().size();
Kirill Terekhov's avatar
Kirill Terekhov committed
504
505
					for(unsigned jt = 0; jt < jend && !ret; ++jt)
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
506
						if( ++arr[i]->HighConn()[jt]->RealDF(tag_shared_elems) == s)
Kirill Terekhov's avatar
Kirill Terekhov committed
507
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
508
							ret = arr[i]->HighConn()[jt];
Kirill Terekhov's avatar
Kirill Terekhov committed
509
510
511
512
513
514
							q = i+1;
						}
					}
				}
				for(unsigned i = 0; i < q; ++i)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
515
					unsigned jend = arr[i]->HighConn().size();
Kirill Terekhov's avatar
Kirill Terekhov committed
516
					for(unsigned jt = 0; jt < jend; ++jt)
Kirill Terekhov's avatar
Kirill Terekhov committed
517
						arr[i]->HighConn()[jt]->RealDF(tag_shared_elems) = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
518
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
519
				return ret;
Kirill Terekhov's avatar
Kirill Terekhov committed
520
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
521
			*/
Kirill Terekhov's avatar
Kirill Terekhov committed
522
523
524
525
		}
		else
		{
			unsigned ss = Mesh::Count(arr,s,HideMarker());
Kirill Terekhov's avatar
Kirill Terekhov committed
526
527
528
			Element::adj_type & const hc0 = arr[0]->HighConn();
			unsigned iend = hc0.size();
			for(unsigned it = 0; it < iend; ++it) if( !hc0[it]->Hidden() )
Kirill Terekhov's avatar
Kirill Terekhov committed
529
530
531
532
533
			{
				unsigned int flag0 = 0;
				for(unsigned i = 1; i < s; i++)
				{
					bool flag1 = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
534
535
536
					Element::adj_type & const hci = arr[i]->HighConn();
					unsigned jend = hci.size();
					for(unsigned jt = 0; jt < jend; ++jt) if( !hci[jt]->Hidden() )
Kirill Terekhov's avatar
Kirill Terekhov committed
537
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
538
						if( hc0[it] == hci[jt] )
Kirill Terekhov's avatar
Kirill Terekhov committed
539
540
541
542
543
544
545
546
547
						{
							flag0++;
							flag1 = true;
							break;
						}
					}
					if( !flag1 )
						break;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
548
				if( flag0 == ss-1 ) return hc0[it];
Kirill Terekhov's avatar
Kirill Terekhov committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
			}
		}
		return NULL;
	}
	
	
	TopologyCheck Mesh::BeginTopologyCheck(ElementType etype, Element ** adj, INMOST_DATA_ENUM_TYPE s)
	{
		INMOST_DATA_ENUM_TYPE i,j,d = ENUMUNDEF;
		TopologyCheck chk = 0;
		if( GetTopologyCheck(ADJACENT_HIDDEN) )
		{
			for(i = 0; i < s; i++)
				if( adj[i]->GetMarker(HideMarker()) )
				{
					chk |= ADJACENT_HIDDEN;
					if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(ADJACENT_HIDDEN) << std::endl;
				}
		}
		if( GetTopologyCheck(ADJACENT_ALIEN) )
		{
			for(i = 0; i < s; i++)
				if( adj[i]->GetMeshLink() != this )
				{
					chk |= ADJACENT_ALIEN;
					if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(ADJACENT_ALIEN) << std::endl;
				}
		}
		if( GetTopologyCheck(ADJACENT_DUPLICATE) )
		{
			bool have_dup = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
580
			MarkerType dup = CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
			for(i = 0; i < s; i++)
			{
				if( adj[i]->GetMarker(dup) ) 
				{
					have_dup = true; // duplication of element
					break;
				}
				else adj[i]->SetMarker(dup);
			}
			for(i = 0; i < s; i++) adj[i]->RemMarker(dup);
			ReleaseMarker(dup);	
			
			if( have_dup )
			{
				chk |= ADJACENT_DUPLICATE;
				if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(ADJACENT_DUPLICATE) << std::endl;
			}
		}
		
		if( GetTopologyCheck(ADJACENT_DIMENSION | DEGENERATE_CELL | DEGENERATE_FACE | DEGENERATE_EDGE | DISABLE_2D) )
		{
			bool happen = false;
			for(i = 0; i < s; i++)
			{
				j = adj[i]->GetElementDimension();
				if( j == ENUMUNDEF ) 
				{
					chk |= ADJACENT_DIMENSION;
					if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(ADJACENT_DIMENSION) << std::endl;
					happen = true;
					break;
				}
				if( d == ENUMUNDEF ) d = j;
				if( d != j ) 
				{
					chk |= ADJACENT_DIMENSION;
					if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(ADJACENT_DIMENSION) << std::endl;
					happen = true;
					break;
				}
			}
			if(!happen) switch(etype) //if happen = true, we cannot be sure about value in d
			{
				case EDGE: 
					if( GetTopologyCheck(DISABLE_2D) )
					{
						if( s == 1 && d == 0 ) 
						{
							chk |= DISABLE_2D;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(DISABLE_2D) << std::endl;
						}
					}
					if( GetTopologyCheck(ADJACENT_DIMENSION) )
					{
						if( d > 0 ) 
						{
							chk |= ADJACENT_DIMENSION;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(ADJACENT_DIMENSION) << std::endl;
						}
					}
					if( GetTopologyCheck(DEGENERATE_EDGE) )
					{
						if( s > 2 )
						{
							chk |= DEGENERATE_EDGE;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(DEGENERATE_EDGE) << std::endl;
						}
					}
				break;
				case FACE:
					if( GetTopologyCheck(DISABLE_2D) )
					{
						if( s == 2 && d == 0 ) 
						{
							chk |= DISABLE_2D;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(DISABLE_2D) << std::endl;
						}
					}
					if( GetTopologyCheck(ADJACENT_DIMENSION) )
					{
						if( d > 1 ) 
						{
							chk |= ADJACENT_DIMENSION;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(ADJACENT_DIMENSION) << std::endl;
						}
					}
					if( GetTopologyCheck(DEGENERATE_EDGE) )
					{
						if(d == 0 && s != 2) //This should be Edge (2D)
						{
							chk |= DEGENERATE_EDGE;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(DEGENERATE_EDGE) << std::endl;
						}
					}
					if( GetTopologyCheck() & DEGENERATE_FACE )
					{
						if(d == 1 && s < 3) // Should not be less then 3 Line or Curve
						{
							chk |= DEGENERATE_FACE;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(DEGENERATE_FACE) << std::endl;
						}					
					}
				break;
				case CELL:
					if( GetTopologyCheck(DISABLE_2D) )
					{
						if( d == 1 ) 
						{
							chk |= DISABLE_2D;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(DISABLE_2D) << std::endl;
						}
					}
					if( GetTopologyCheck(ADJACENT_DIMENSION) )
					{
						if( d > 2 || d == 0 ) 
						{
							chk |= ADJACENT_DIMENSION;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(ADJACENT_DIMENSION) << std::endl;
						}
					}
					if( GetTopologyCheck(DEGENERATE_FACE) )
					{
						if( d == 1 && s < 3 ) //Should not be less then 3 Line
						{
							chk |= DEGENERATE_FACE;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(DEGENERATE_FACE) << std::endl;
						} 
					}
					if( GetTopologyCheck(DEGENERATE_CELL) )
					{
						if( d == 2 && s < 4 ) //Should not be less then 4 Tri, Quad, Polygon
						{
							chk |= DEGENERATE_CELL;
							if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(DEGENERATE_CELL) << std::endl;
						} 
					}
				break;
			}
		}
		if( etype == CELL )
		{
			if( GetTopologyCheck(TRIPLE_SHARED_FACE) )
			{
				bool check = false;
				for(i = 0; i < s; i++)
				{
					if( adj[i]->nbAdjElements(CELL) == 2 )
					{
						check = true;
						break;
					}
				}
				if( check )
				{
					bool happen = true;
					if( (GetTopologyCheck(DUPLICATE_CELL)) && (FindSharedAdjacency(adj,s) != NULL) ) happen = false;
					if( happen )
					{
						chk |= TRIPLE_SHARED_FACE;
						if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(TRIPLE_SHARED_FACE) << std::endl;
					}
				}
			}
		}
		else if( etype == FACE )
		{
			if( GetTopologyCheck(INTERLEAVED_FACES) )
			{
				adjacent<Node> allnodes;
				for(i = 0; i < s; i++) allnodes.unite(adj[i]->getNodes());
				adjacent<Face> faces = allnodes[0].getFaces();
				for(i = 1; i < allnodes.size() && !faces.empty(); i++) faces.intersect(allnodes[i].getFaces());
				for(i = 0; i < faces.size(); i++)
				{
					if( faces[i].nbAdjElements(NODE) != allnodes.size() )
					{
						chk |= INTERLEAVED_FACES;
						if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(INTERLEAVED_FACES) << std::endl;
					}
				}
			}
		}
		errorset |= chk;
		return chk;
	}
	
	TopologyCheck Mesh::EndTopologyCheck(Element * e)
	{
		TopologyCheck chk = 0;
		switch(e->GetGeometricDimension(e->GetGeometricType()))
		{
		case 3:
			if( GetTopologyCheck(FLATTENED_CELL) )
			{
				adjacent<Face> faces = e->getFaces();
				INMOST_DATA_ENUM_TYPE num = e->nbAdjElements(NODE);
				for(INMOST_DATA_ENUM_TYPE i = 0; i < faces.size(); i++)
				{
					if( faces[i].nbAdjElements(NODE) == num )
					{
						chk |= FLATTENED_CELL;
						if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(FLATTENED_CELL) << std::endl;
					}
				}
			}
			if( GetTopologyCheck(FACE_ORIENTATION) )
			{
				adjacent<Face> faces = e->getFaces();
				for(INMOST_DATA_ENUM_TYPE i = 0; i < faces.size(); i++)
				{
					if( faces[i].nbAdjElements(CELL) == 1 && !faces[i].CheckNormalOrientation() )
					{
						chk |= FACE_ORIENTATION;
						if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(FACE_ORIENTATION) << std::endl;
						break;
					}
				}
			}
			break;
		case 2:
			if( GetTopologyCheck(FACE_PLANARITY) )
			{
				if( (e->GetElementType() == FACE && !e->getAsFace()->Planarity()) || (e->GetElementType() == CELL && !e->getAsCell()->Planarity()) )
				{
					chk |= FACE_PLANARITY;
					if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(FACE_PLANARITY) << std::endl;
				}
			}
			if( GetTopologyCheck(FACE_EDGES_ORDER) )
			{
				if( (e->GetElementType() == FACE && !e->getAsFace()->CheckEdgeOrder()) || (e->GetElementType() == CELL && !e->getAsCell()->CheckEdgeOrder()) )
				{
					chk |= FACE_EDGES_ORDER;
					if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(FACE_EDGES_ORDER) << std::endl;
				}
			}
			break;
		}
		if( GetTopologyCheck(PROHIBIT_MULTILINE) )
		{
			if( e->GetGeometricType() == Element::MultiLine )
			{
				chk |= PROHIBIT_MULTILINE;
				if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(PROHIBIT_MULTILINE) << std::endl;
			}
		}
		if( GetTopologyCheck(PROHIBIT_MULTIPOLYGON) )
		{
			if( e->GetGeometricType() == Element::MultiPolygon )
			{
				chk |= PROHIBIT_MULTIPOLYGON;
				if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(PROHIBIT_MULTIPOLYGON) << std::endl;
			}
		}
		if( GetTopologyCheck(PROHIBIT_POLYGON) )
		{
			if( e->GetGeometricType() == Element::Polygon )
			{
				chk |= PROHIBIT_POLYGON;
				if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(PROHIBIT_POLYGON) << std::endl;
			}
		}
		if( GetTopologyCheck(PROHIBIT_POLYHEDRON) )
		{
			if( e->GetGeometricType() == Element::Polyhedron )
			{
				chk |= PROHIBIT_POLYHEDRON;
				if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(PROHIBIT_POLYHEDRON) << std::endl;
			}
		}
		errorset |= chk;
		return chk;
	}
	
	Node * Mesh::CreateNode(Storage::real * coords)
	{
		INMOST_DATA_ENUM_TYPE i = 0;
		Node * e = new Node(this);
Kirill Terekhov's avatar
Kirill Terekhov committed
859
		e->SetGeometricType(Element::Vertex);
Kirill Terekhov's avatar
Kirill Terekhov committed
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
		Storage::real_array v = e->RealArray(tag_coords);
		for(i = 0; i < dim; i++) v[i] = coords[i];
		e->SetMarker(NewMarker());
		return e;
	}
	std::pair<Edge *,bool> Mesh::CreateEdge(Node ** e_nodes, INMOST_DATA_ENUM_TYPE s)
	{
		INMOST_DATA_ENUM_TYPE i;
		Edge * e = NULL;
		if( s > 0 )
		{
			TopologyCheck chk = 0;
			chk |= BeginTopologyCheck(EDGE,reinterpret_cast<Element **>(&e_nodes[0]),s);
			if( chk != 0 )
			{
				if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
			}
			if( GetTopologyCheck() & DUPLICATE_EDGE )
			{
				Element * test = FindSharedAdjacency(reinterpret_cast<Element **>(&e_nodes[0]),s);
				if (test != NULL) return std::pair<Edge *, bool>(static_cast<Edge *>(test),false);
			}		
			e = new Edge(this);
			for(i = 0; i < s; i++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
885
886
				e_nodes[i]->HighConn().push_back(e);
				//e->LowConn().push_back(e_nodes[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
887
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
888
889
			Element::adj_type & lc = e->LowConn();
			lc.insert(lc.end(),reinterpret_cast<Element **>(e_nodes),reinterpret_cast<Element **>(e_nodes+s));
Kirill Terekhov's avatar
Kirill Terekhov committed
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
			e->ComputeGeometricType();
			e->SetMarker(NewMarker());
			RecomputeGeometricData(e);
			chk |= EndTopologyCheck(e);
			if( chk != 0 )
			{
				if( GetTopologyCheck(MARK_ON_ERROR) ) e->Integer(TopologyErrorTag()) = chk;
				if( GetTopologyCheck(DELETE_ON_ERROR) ) { delete e; e = NULL;}
				if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
			}
		}
		return std::pair<Edge *, bool>(e,true);
	}
	
	
	std::pair<Face *,bool> Mesh::CreateFace(Node ** f_nodes, INMOST_DATA_ENUM_TYPE s)
	{
		dynarray<Edge *,64> f_edges(s);
		Node * e_nodes[2];
		if( s == 2 ) //This is an edge for 2d!
		{
			e_nodes[0] = f_nodes[0];
			f_edges[0] = CreateEdge(e_nodes,1).first;
			e_nodes[0] = f_nodes[1];
			f_edges[1] = CreateEdge(e_nodes,1).first;
		}
		else
		{
			for(unsigned int i = 0; i < s; i++)
			{
				e_nodes[0] = f_nodes[i];
				e_nodes[1] = f_nodes[(i+1)%s];
				f_edges[i] = CreateEdge(e_nodes,2).first;
			}
		}
		return CreateFace(&f_edges[0],s);
	}
	
	std::pair<Face *,bool> Mesh::CreateFace(Edge ** f_edges, INMOST_DATA_ENUM_TYPE s)
	{
		INMOST_DATA_ENUM_TYPE i;
		Face * e = NULL;
		if( s > 0 )
		{
			TopologyCheck chk = 0;
			chk |= BeginTopologyCheck(FACE,reinterpret_cast<Element **>(&f_edges[0]),s);
			if( chk != 0 )
			{
				if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
			}
			if( GetTopologyCheck(DUPLICATE_FACE) )
			{
				Element * test = FindSharedAdjacency(reinterpret_cast<Element **>(&f_edges[0]),s);
				if (test != NULL) return std::pair<Face *, bool>(static_cast<Face *>(test),false);
			}
			e = new Face(this);
			for(i = 0; i < s; i++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
948
949
				f_edges[i]->HighConn().push_back(e);
				//e->LowConn().push_back(f_edges[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
950
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
951
952
			Element::adj_type & lc = e->LowConn();
			lc.insert(lc.end(),reinterpret_cast<Element **>(f_edges),reinterpret_cast<Element **>(f_edges+s));
Kirill Terekhov's avatar
Kirill Terekhov committed
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
			e->ComputeGeometricType();
			e->SetMarker(NewMarker());
			RecomputeGeometricData(e);
			chk |= EndTopologyCheck(e);
			if( chk != 0 )
			{
				if( GetTopologyCheck(MARK_ON_ERROR)   ) e->Integer(TopologyErrorTag()) = chk;
				if( GetTopologyCheck(DELETE_ON_ERROR) ) { delete e; e = NULL;}
				if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
			}
		}
		return std::pair<Face *, bool>(e,true);
	}
	
	
	std::pair<Cell *,bool> Mesh::CreateCell(Node ** c_f_nodes, const INMOST_DATA_ENUM_TYPE * c_f_sizes, INMOST_DATA_ENUM_TYPE s, Node ** suggest_nodes_order, INMOST_DATA_ENUM_TYPE sn)
	{
		INMOST_DATA_ENUM_TYPE i,j = 0;
		dynarray<Face *,64> c_faces;
		c_faces.resize(s);
		for(i = 0; i < s; i++)
		{
			c_faces[i] = CreateFace(&c_f_nodes[j], c_f_sizes[i]).first;
			j += c_f_sizes[i];
		}
		return CreateCell(&c_faces[0],s,suggest_nodes_order,sn);
	}
	

	std::pair<Cell *, bool> Mesh::CreateCell(Node ** c_f_nodes, const INMOST_DATA_ENUM_TYPE * c_f_nodeinds, const INMOST_DATA_ENUM_TYPE * c_f_numnodes, INMOST_DATA_ENUM_TYPE s, Node ** suggest_nodes_order, INMOST_DATA_ENUM_TYPE sn)
	{
		INMOST_DATA_ENUM_TYPE i,k,j = 0; //, sn = 0;
		dynarray<Node *,64> temp;
		dynarray<Face *,64> c_faces;
		c_faces.resize(s);
		for(i = 0; i < s; i++)
		{
			for(k = j; k < j+c_f_numnodes[i]; k++)
			{
				//if( c_f_nodeinds[k]+1 > sn ) sn = c_f_nodeinds[k]+1;
				temp.push_back(c_f_nodes[c_f_nodeinds[k]]);
			}
			c_faces[i] = CreateFace(&temp[0], c_f_numnodes[i]).first;
			j += c_f_numnodes[i];
			temp.clear();
		}
		return CreateCell(&c_faces[0],s,suggest_nodes_order,sn);
	}
	
	
	void Mesh::RestoreCellNodes(Cell * c, dynarray<Node *,64> & ret)
	{
		ret.clear();
		switch(c->GetGeometricType())
		{
			case Element::Vertex:
			case Element::Line:
			{
				throw CorruptedIerarchy;
			}
			case Element::MultiLine:
			case Element::Tri:
			case Element::Quad:
			case Element::Polygon:
			{
				adjacent<Edge> edges = c->getEdges();
				ret.reserve(edges.size());
				for(adjacent<Edge>::iterator it = edges.begin(); it != edges.end(); it++)
				{
					adjacent<Node> nodes = it->getNodes();
					if( nodes.size() != 1 ) throw CorruptedIerarchy;
					ret.push_back(nodes.data()[0]);
				}
				break;
			}
				/*
				 6 7
				 4 5
				 2 3
				 0 1
				 */
			case Element::Hex:
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
1037
1038
				MarkerType mrk = CreateMarker();
				MarkerType cemrk = CreateMarker();
				MarkerType femrk = CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
				//printf("%lx %lx %lx\n",mrk,cemrk,femrk);
				adjacent<Face> faces = c->getFaces();
				Face * face = &faces[0];
				ret.reserve(8);
				adjacent<Node> verts = face->getNodes();
				if( face->BackCell() == c )
					for(adjacent<Node>::iterator it = verts.begin(); it != verts.end(); it++)
					{
						ret.push_back(&*it);
						it->SetMarker(mrk);
					}
				else
					for(adjacent<Node>::reverse_iterator it = verts.rbegin(); it != verts.rend(); it++)
					{
						ret.push_back(&*it);
						it->SetMarker(mrk);
					}
				adjacent<Edge> c_edges = c->getEdges();
				for(adjacent<Edge>::iterator it = c_edges.begin(); it != c_edges.end(); it++)
					it->SetMarker(cemrk);
				adjacent<Edge> f_edges = face->getEdges();
				for(adjacent<Edge>::iterator it = f_edges.begin(); it != f_edges.end(); it++)
					it->SetMarker(femrk);
				for(unsigned int k = 0; k < 4; k++)
				{
					adjacent<Edge> v_edges = ret[k]->getEdges();
					for(adjacent<Edge>::iterator it = v_edges.begin(); it != v_edges.end(); it++)
					{
						if( it->GetMarker(cemrk) && !it->GetMarker(femrk) )
						{
							adjacent<Node> nn = it->getNodes();
							if( nn[0].GetMarker(mrk) )
								ret.push_back(&nn[1]);
							else
								ret.push_back(&nn[0]);
							break;
						}
					}
					ret[k]->RemMarker(mrk);
				}
				for(adjacent<Edge>::iterator it = c_edges.begin(); it != c_edges.end(); it++) it->RemMarker(cemrk);
				for(adjacent<Edge>::iterator it = f_edges.begin(); it != f_edges.end(); it++) it->RemMarker(femrk);
				ReleaseMarker(mrk);
				ReleaseMarker(cemrk);
				ReleaseMarker(femrk);
				break;
			}
				/*
				 5
				 3 4
				 2 
				 0 1
				 */
			case Element::Prism:
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1094
1095
1096
				MarkerType mrk = CreateMarker();
				MarkerType cemrk = CreateMarker();
				MarkerType femrk = CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
				ret.reserve(6);
				Face * face;
				adjacent<Face> faces = c->getFaces();
				for(unsigned int i = 0 ; i < faces.size(); i++) //iterate over faces
					if( faces[i].nbAdjElements(EDGE) == 3 ) //number of edges in i-th face
					{
						face = &faces[i];
						break;
					}
				adjacent<Node> verts = face->getNodes();
				if( face->BackCell() == c )
					for(adjacent<Node>::iterator it = verts.begin(); it != verts.end(); it++)
					{
						ret.push_back(&*it);
						it->SetMarker(mrk);
					}
				else
					for(adjacent<Node>::reverse_iterator it = verts.rbegin(); it != verts.rend(); it++)
					{
						ret.push_back(&*it);
						it->SetMarker(mrk);
					}
				adjacent<Edge> c_edges = c->getEdges();
				for(adjacent<Edge>::iterator it = c_edges.begin(); it != c_edges.end(); it++) it->SetMarker(cemrk);
				adjacent<Edge> f_edges = face->getEdges();
				for(adjacent<Edge>::iterator it = f_edges.begin(); it != f_edges.end(); it++) it->SetMarker(femrk);
				for(unsigned int k = 0; k < 3; k++)
				{
					adjacent<Edge> v_edges = ret[k]->getEdges();
					for(adjacent<Edge>::iterator it = v_edges.begin(); it != v_edges.end(); it++)
						if( it->GetMarker(cemrk) && !it->GetMarker(femrk) )
						{
							adjacent<Node> nn = it->getNodes();
							if( nn[0].GetMarker(mrk) )
								ret.push_back(&nn[1]);
							else
								ret.push_back(&nn[0]);
							break;
						}
					ret[k]->RemMarker(mrk);
				}
				for(adjacent<Edge>::iterator it = c_edges.begin(); it != c_edges.end(); it++) it->RemMarker(cemrk);
				for(adjacent<Edge>::iterator it = f_edges.begin(); it != f_edges.end(); it++) it->RemMarker(femrk);
				ReleaseMarker(mrk);
				ReleaseMarker(cemrk);
				ReleaseMarker(femrk);
				break;
			}
				/*
				  4
				 2 3
				 0 1
				 */
			case Element::Pyramid:
			{
				ret.reserve(5);
				Face * quad, * triangle;
Kirill Terekhov's avatar
Kirill Terekhov committed
1154
				MarkerType mrk = CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
				adjacent<Face> faces = c->getFaces();
				for(unsigned int i = 0; i < faces.size(); i++) //go over faces
					if( faces[i].nbAdjElements(EDGE) == 4 ) //check if number of edges = 4
					{
						quad = &faces[i];
						break;
					}
				for(unsigned int i = 0; i < faces.size(); i++) //go over faces
					if( faces[i].nbAdjElements(EDGE) == 3 ) //check if number of edges = 3
					{
						triangle = &faces[i];
						break;
					}
				adjacent<Node> base_nodes = quad->getNodes();
				if( quad->BackCell() == c )
					for(adjacent<Node>::iterator it = base_nodes.begin(); it != base_nodes.end(); it++)
					{
						ret.push_back(&*it);
						it->SetMarker(mrk);
					}
				else
					for(adjacent<Node>::reverse_iterator it = base_nodes.rbegin(); it != base_nodes.rend(); it++)
					{
						ret.push_back(&*it);
						it->SetMarker(mrk);
					}
				adjacent<Node> tri_nodes = triangle->getNodes();
				for(adjacent<Node>::iterator it = tri_nodes.begin(); it != tri_nodes.end(); it++)
				{
					if( !it->GetMarker(mrk) )
					{
						ret.push_back(&*it);
						break;
					}
				}
				for(dynarray<Node *,64>::iterator it = ret.begin(); it != ret.end(); it++)
					(*it)->RemMarker(mrk);
				ReleaseMarker(mrk);
				break;
			}
			default: //Tet, MultiPolygon, Polyhedron
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1197
				MarkerType mrk = CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
1198
1199
				if( !HideMarker() )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
					Element::adj_type & lc = c->LowConn();
					for(Element::adj_type::enumerator i = 0; i < lc.size(); i++) //iterate over faces
					{
						Element::adj_type & ilc = lc[i]->LowConn();
						for(Element::adj_type::enumerator j = 0; j < ilc.size(); j++) //iterate over face edges
						{
							Element::adj_type & jlc = ilc[j]->LowConn();
							for(Element::adj_type::enumerator k = 0; k < jlc.size(); k++) //iterator over edge nodes
							{
								if( !jlc[k]->GetMarker(mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1210
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
1211
1212
									jlc[k]->SetMarker(mrk);
									ret.push_back(static_cast<Node *>(jlc[k]));
Kirill Terekhov's avatar
Kirill Terekhov committed
1213
								}
Kirill Terekhov's avatar
Kirill Terekhov committed
1214
1215
1216
							}
						}
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1217
1218
1219
				}
				else
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
					Element::adj_type & lc = c->LowConn();
					for(Element::adj_type::enumerator i = 0; i < lc.size(); i++) if( !lc[i]->Hidden() )  //iterate over faces
					{
						Element::adj_type & ilc = lc[i]->LowConn();
						for(Element::adj_type::enumerator j = 0; j < ilc.size(); j++) if( !ilc[j]->Hidden() ) //iterate over face edges
						{
							Element::adj_type & jlc = ilc[j]->LowConn();
							for(Element::adj_type::enumerator k = 0; k < jlc.size(); k++) if( !jlc[k]->Hidden() ) //iterator over edge nodes
							{
								if( !jlc[k]->GetMarker(mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1230
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
1231
1232
									jlc[k]->SetMarker(mrk);
									ret.push_back(static_cast<Node *>(jlc[k]));
Kirill Terekhov's avatar
Kirill Terekhov committed
1233
								}
Kirill Terekhov's avatar
Kirill Terekhov committed
1234
1235
1236
							}
						}
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1237
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1238
				for(dynarray<Node *,64>::enumerator it = 0; it < ret.size(); it++) ret[it]->RemMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
				ReleaseMarker(mrk);
				break;
			}
		}

	}
	
	std::pair<Cell *,bool> Mesh::CreateCell(Face ** c_faces, INMOST_DATA_ENUM_TYPE s, Node ** c_nodes, INMOST_DATA_ENUM_TYPE sn)
	{
		INMOST_DATA_ENUM_TYPE i;
		Cell * e = NULL;
		if( s > 0 )
		{
			TopologyCheck chk = 0;
			chk |= BeginTopologyCheck(CELL,reinterpret_cast<Element **>(&c_faces[0]),s);
			if( chk != 0 )
			{
				if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
			}
			if( GetTopologyCheck(DUPLICATE_CELL) )
			{
				Element * test = FindSharedAdjacency(reinterpret_cast<Element **>(&c_faces[0]),s);
				if (test != NULL) return std::pair<Cell *, bool>(static_cast<Cell *>(test),false);
			}
			e = new Cell(this);		
			for(i = 0; i < s; i++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1266
1267
				c_faces[i]->HighConn().push_back(e);
				//e->LowConn().push_back(c_faces[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1268
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1269
1270
			Element::adj_type & lc = e->LowConn();
			lc.insert(lc.begin(),reinterpret_cast<Element **>(c_faces),reinterpret_cast<Element **>(c_faces+s));
Kirill Terekhov's avatar
Kirill Terekhov committed
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
			e->ComputeGeometricType();		
			{
				dynarray<Node *,64> temp_nodes;
				if( sn == 0 ) 
				{
					RestoreCellNodes(e,temp_nodes);
					c_nodes = &temp_nodes[0];
					sn = temp_nodes.size();
				}
				for(INMOST_DATA_ENUM_TYPE k = 0; k < sn; k++)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1282
1283
					c_nodes[k]->LowConn().push_back(static_cast<Element *>(e));
					//e->HighConn().push_back(static_cast<Element *>(c_nodes[k]));
Kirill Terekhov's avatar
Kirill Terekhov committed
1284
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1285
1286
				Element::adj_type & hc = e->HighConn();
				hc.insert(hc.begin(),reinterpret_cast<Element **>(c_nodes),reinterpret_cast<Element **>(c_nodes+sn));
Kirill Terekhov's avatar
Kirill Terekhov committed
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
				e->SetMarker(NewMarker());
				RecomputeGeometricData(e);
				chk |= EndTopologyCheck(e);
				if( chk != 0 )
				{
					if( GetTopologyCheck(MARK_ON_ERROR)   ) e->Integer(TopologyErrorTag()) = chk;
					if( GetTopologyCheck(DELETE_ON_ERROR) ) { delete e; e = NULL;}
					if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
				}
			}
		}
		return std::pair<Cell *, bool>(e,true);
	}
	
	
	ElementSet * Mesh::CreateSet()
	{
		ElementSet * e = new ElementSet(this,false);
		return e;
	}
	
	ElementSet * Mesh::CreateOrderedSet()
	{
		ElementSet * e = new ElementSet(this,true);
		return e;
	}
	
	
	void Mesh::SetDimensions(unsigned int dims)
	{
		if( dim == dims ) return;
		if( dims < 2 || dims > 3 ) throw DimensionIsNotSupportedByGeometry;
Kirill Terekhov's avatar
Kirill Terekhov committed
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360

		if( NumberOfNodes() > 0)
		{
			Storage::real * temp = new Storage::real[dims*NumberOfNodes()];
			Storage::integer j = 0;
			for(Storage::integer k = 0; k < MaxLocalIDNODE(); ++k)
			{
				Node * n = NodeByLocalID(k);
				if( n != NULL )
				{
					Storage::real_array c = n->Coords();
					for(Storage::integer i = 0; i < dims; i++)
					{
						temp[j+i] = c[i];
					}
					j+=dims;
				}
			}
			DeleteTag(tag_coords);
			tag_coords = CreateTag("COORD",DATA_REAL,NODE,NONE,dims);
			j = 0;
			for(Storage::integer k = 0; k < MaxLocalIDNODE(); ++k)
			{
				Node * n = NodeByLocalID(k);
				if( n != NULL )
				{
					Storage::real_array c = n->Coords();
					for(Storage::integer i = 0; i < dims; i++)
					{
						c[i] = temp[j+i];
					}
					j+=dims;
				}
			}
			dim = dims;
		}
		else
		{
			dim = dims;
			DeleteTag(tag_coords);
			tag_coords = CreateTag("COORD",DATA_REAL, NODE,NONE,dim);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
	}
	
	
	bool LessElements(Element * a, Element * b)
	{
		return USE_COMPARE(a,b) < 0;
	}
	int CompareElementsC(const void * pa, const void * pb)
	{
		Element ** a = (Element **)pa;
		Element ** b = (Element **)pb;
		return USE_COMPARE(*a,*b);
	}
	int CompareElements(Element *a, Element * b)
	{
		return USE_COMPARE(a,b);
	}
	
	int CompareElementsCCentroid(const void * pa, const void * pb)
	{
		Element ** a = (Element **)pa;
		Element ** b = (Element **)pb;
		return CompareElementsCentroid(*a,*b);
	}
	
	int CompareElementsCGID(const void * pa, const void * pb)
	{
		Element ** a = (Element **)pa;
		Element ** b = (Element **)pb;
		return CompareElementsGID(*a,*b);
	}

	
	int CompareElementsCUnique(const void * pa, const void * pb)
	{
		Element ** a = (Element **)pa;
		Element ** b = (Element **)pb;
		return CompareElementsUnique(*a,*b);
	}
	
	int CompareElementsCPointer(const void * pa, const void * pb)
	{
		Element ** a = (Element **)pa;
		Element ** b = (Element **)pb;
		return CompareElementsPointer(*a,*b);
	}
	
	bool LessElementsGID(Element * a, Element * b)
	{
		return CompareElementsGID(a,b) < 0;
	}
	
	bool LessElementsCentroid(Element * a, Element * b)
	{
		return CompareElementsCentroid(a,b) < 0;
	}
	
	bool LessElementsUnique(Element * a, Element * b)
	{
		return CompareElementsUnique(a,b) < 0;
	}

	bool LessElementsPointer(Element * a, Element * b)
	{
		return CompareElementsPointer(a,b) < 0;
	}

	
	int CompareElementsUnique(Element * a,Element * b)
	{
		Mesh * ma = a->GetMeshLink(), * mb = b->GetMeshLink();
		assert(ma == mb);
		if( a == b ) return 0;
		if( a == NULL ) return 1;
		if( b == NULL ) return -1;
		if( a->GetGeometricType() == b->GetGeometricType() )
		{
			if( a->GetElementType() != b->GetElementType() )
			{
				if( a->GetElementType() < b->GetElementType() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1441
					return CompareElementsUnique(a,b->LowConn()[0]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1442
				else
Kirill Terekhov's avatar
Kirill Terekhov committed
1443
					return CompareElementsUnique(a->LowConn()[0],b);
Kirill Terekhov's avatar
Kirill Terekhov committed
1444
1445
1446
1447
1448
1449
			}
			switch( a->GetGeometricType() )
			{
				case Element::Vertex:
				{
					if( a->GetElementType() > NODE )
Kirill Terekhov's avatar
Kirill Terekhov committed
1450
						return CompareElementsUnique(a->LowConn()[0],b->LowConn()[0]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
					Storage::real_array 
						ca = a->RealArray(ma->CoordsTag()),
						cb = b->RealArray(mb->CoordsTag());
					Storage::real e = std::max(ma->GetEpsilon(),mb->GetEpsilon());
					if( ca.size() != cb.size() ) return ca.size()-cb.size();
					for(unsigned int i = 0; i < ca.size(); i++)
						if( fabs(ca[i]-cb[i]) > e )
						{
							if( ca[i] > cb[i] ) return 1;
							else return -1;
						}
					return 0;
				}
				case Element::Line:
				{
					if( a->GetElementType() > EDGE )
Kirill Terekhov's avatar
Kirill Terekhov committed
1467
						return CompareElementsUnique(a->LowConn()[0],b->LowConn()[0]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1468
					Element::adj_type ca, cb;
Kirill Terekhov's avatar
Kirill Terekhov committed
1469
1470
					if( CompareElementsUnique(a->LowConn().front(),a->LowConn().back()) <= 0 )
						ca.insert(ca.begin(),a->LowConn().begin(),a->LowConn().end());
Kirill Terekhov's avatar
Kirill Terekhov committed
1471
					else
Kirill Terekhov's avatar
Kirill Terekhov committed
1472
1473
1474
						ca.insert(ca.begin(),a->LowConn().rbegin(),a->LowConn().rend());
					if( CompareElementsUnique(b->LowConn().front(),b->LowConn().back()) <= 0 )
						cb.insert(cb.begin(),b->LowConn().begin(),b->LowConn().end());
Kirill Terekhov's avatar
Kirill Terekhov committed
1475
					else
Kirill Terekhov's avatar
Kirill Terekhov committed
1476
						cb.insert(cb.begin(),b->LowConn().rbegin(),b->LowConn().rend());
Kirill Terekhov's avatar
Kirill Terekhov committed
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
					for(unsigned int i = 0; i < ca.size(); i++)
					{
						int test = CompareElementsUnique(ca[i],cb[i]);
						if( test != 0 ) return test;
					}
					/*
					 for(int i = 0; i < ca.size(); i++)
					 if( ca[i] != cb[i] )
					 {
					 if( ca[i] > cb[i] ) return 1;
					 else return -1;
					 }
					 */
					return 0;
				}
				case Element::MultiLine:
				case Element::Tri:
				case Element::Quad:
				case Element::Polygon:
				{
					if( a->GetElementType() > FACE )
Kirill Terekhov's avatar
Kirill Terekhov committed
1498
1499
1500
1501
						return CompareElementsUnique(a->LowConn()[0],b->LowConn()[0]);
					if( a->LowConn().size() != b->LowConn().size() )
						return a->LowConn().size() - b->LowConn().size();
					Element::adj_type ca(a->LowConn()), cb(b->LowConn());
Kirill Terekhov's avatar
Kirill Terekhov committed
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
#if defined(USE_QSORT)
					qsort(&ca[0],ca.size(),sizeof(Element *),CompareElementsCUnique);
					qsort(&cb[0],cb.size(),sizeof(Element *),CompareElementsCUnique);
#else
					std::sort(ca.begin(),ca.end(),LessElementsUnique);
					std::sort(cb.begin(),cb.end(),LessElementsUnique);
#endif
					for(unsigned int i = 0; i < ca.size(); i++)
					{
						int test = CompareElementsUnique(ca[i],cb[i]);
						if( test != 0 ) return test;
					}
					return 0;
				}
				case Element::MultiPolygon:
				case Element::Tet:
				case Element::Hex:
				case Element::Prism:
				case Element::Pyramid:
				case Element::Polyhedron:
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1523
1524
1525
					if( a->LowConn().size() != b->LowConn().size() )
						return a->LowConn().size() - b->LowConn().size();
					Element::adj_type ca(a->LowConn()), cb(b->LowConn());
Kirill Terekhov's avatar
Kirill Terekhov committed
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
#if defined(USE_QSORT)
					qsort(&ca[0],ca.size(),sizeof(Element *),CompareElementsCUnique);
					qsort(&cb[0],cb.size(),sizeof(Element *),CompareElementsCUnique);
#else
					std::sort(ca.begin(),ca.end(),LessElementsUnique);
					std::sort(cb.begin(),cb.end(),LessElementsUnique);
#endif
					for(unsigned int i = 0; i < ca.size(); i++)
					{
						int test = CompareElementsUnique(ca[i],cb[i]);
						if( test != 0 ) return test;
					}
					return 0;
				}
				default: throw Impossible;
			}
			throw Impossible;
		}
		else return a->GetGeometricType() - b->GetGeometricType();
	}
	
	int CompareElementsCentroid(Element * a,Element * b)
	{
		Mesh * ma = a->GetMeshLink(), * mb = b->GetMeshLink();
		assert(ma == mb);
		if( a == b ) return 0;
		if( a == NULL ) return 1;
		if( b == NULL ) return -1;
		if( a->GetGeometricType() == b->GetGeometricType() )
		{
			if( a->GetElementType() != b->GetElementType() )
			{
				if( a->GetElementType() < b->GetElementType() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1559
					return CompareElementsCentroid(a,b->LowConn()[0]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1560
				else
Kirill Terekhov's avatar
Kirill Terekhov committed
1561
					return CompareElementsCentroid(a->LowConn()[0],b);
Kirill Terekhov's avatar
Kirill Terekhov committed
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
			}
			Storage::real ca[3] = {0,0,0};
			Storage::real cb[3] = {0,0,0};
			a->Centroid(ca);
			b->Centroid(cb);
			Storage::real e = std::max(ma->GetEpsilon(),mb->GetEpsilon());
			for(unsigned int i = 0; i < 3; i++)
				if( fabs(ca[i]-cb[i]) > e )
				{
					if( ca[i] > cb[i] ) return 1;
					else return -1;
				}
			return 0;
		}
		else return a->GetGeometricType() - b->GetGeometricType();
	}
	
	int CompareElementsGID(Element * a,Element * b)
	{
		if( a == b ) return 0;
		if( a == NULL ) return 1;
		if( b == NULL ) return -1;
		if( a->GetGeometricType() == b->GetGeometricType() )
			return a->GlobalID()-b->GlobalID();
		else return a->GetGeometricType() - b->GetGeometricType();
	}
	
	int CompareElementsPointer(Element * a, Element * b)
	{
		//printf("%ld\n",a-b);
		if( a > b ) return 1;
		if( a < b ) return -1;
		return 0;
	}
	
	int CompareElementsHybrid(Element * a,Element * b)
	{
		int test = CompareElementsCentroid(a,b);
		if( test == 0 ) test = CompareElementsUnique(a,b);
		return test;
	}
	
	
	void Mesh::UntieElement(Storage * e)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1607
1608
1609
#if defined(USE_OMP)
#pragma omp critical [storage_interraction]
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1610
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
			INMOST_DATA_ENUM_TYPE pos = e->LocalID();
			switch(e->GetElementType())
			{
				case ESET:
					if( pos >= sets.size() || sets[pos] != e ) return;
					empty_sets.push_back(pos);
					sets[pos] = NULL;
					break;
				case CELL:
					if( pos >= cells.size() || cells[pos] != e ) return;
					empty_cells.push_back(pos);
					cells[pos] = NULL;
					break;
				case FACE:
					if( pos >= faces.size() || faces[pos] != e ) return;
					empty_faces.push_back(pos);
					faces[pos] = NULL;
					break;
				case EDGE:
					if( pos >= edges.size() || edges[pos] != e ) return;
					empty_edges.push_back(pos);
					edges[pos] = NULL;
					break;
				case NODE:
					if( pos >= nodes.size() || nodes[pos] != e ) return;
					empty_nodes.push_back(pos);
					nodes[pos] = NULL;
					break;
				default:
					throw Impossible;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1642
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1643
1644
1645
		// This should be done through BeginModification - ApplyModification - EndModification
		//if( e->GetElementType() & (CELL | FACE | EDGE | NODE) )
		//	for(unsigned int i = 0; i < sets.size(); i++) if( sets[i] != NULL ) sets[i]->Erase(static_cast<Element *>(e));
Kirill Terekhov's avatar
Kirill Terekhov committed
1646
1647
1648
1649
1650
	}
	
	
	void Mesh::TieElement(Storage * e)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1651
1652
1653
#if defined(USE_OMP)
#pragma omp critical [storage_interraction]
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1654
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1655
			ElementType etype = e->GetElementType();
Kirill Terekhov's avatar
Kirill Terekhov committed
1656
			INMOST_DATA_ENUM_TYPE old_size = GetArrayCapacity(etype);
Kirill Terekhov's avatar
Kirill Terekhov committed
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
			switch(e->GetElementType())
			{
				case ESET:
					if( !empty_sets.empty() && !isMeshModified() )
					{
						e->local_id = empty_sets.back();
						sets[empty_sets.back()] = static_cast<ElementSet *>(e);
						empty_sets.pop_back();
					}
					else
					{
						e->local_id = sets.size();
						sets.push_back(static_cast<ElementSet *>(e));
					}
					break;
				case NODE:
					if( !empty_nodes.empty() && !isMeshModified() )
					{
						e->local_id = empty_nodes.back();
						nodes[empty_nodes.back()] = static_cast<Node *>(e);
						empty_nodes.pop_back();
					}
					else 
					{
						e->local_id = nodes.size();
						nodes.push_back(static_cast<Node *>(e));
					}
					break;
				case EDGE:
					if( !empty_edges.empty() && !isMeshModified() )
					{
						e->local_id = empty_edges.back();
						edges[empty_edges.back()] = static_cast<Edge *>(e);
						empty_edges.pop_back();
					}
					else 
					{
						e->local_id = edges.size();
						edges.push_back(static_cast<Edge *>(e));
					}
					break;
				case FACE:
					if( !empty_faces.empty() && !isMeshModified() )
					{
						e->local_id = empty_faces.back();
						faces[empty_faces.back()] = static_cast<Face *>(e);
						empty_faces.pop_back();
					}
					else 
					{
						e->local_id = faces.size();
						faces.push_back(static_cast<Face *>(e));
					}
					break;
				case CELL:
					if( !empty_cells.empty() && !isMeshModified() )
					{
						e->local_id = empty_cells.back();
						cells[empty_cells.back()] = static_cast<Cell *>(e);
						empty_cells.pop_back();
					}
					else 
					{
						e->local_id = cells.size();
						cells.push_back(static_cast<Cell *>(e));
					}
					break;
				case MESH:
					{
						e->local_id = 0;
					}
					break;
			}
			if( GetArrayCapacity(etype) != old_size )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1732
1733
				ReallocateData(etype);
				/*
Kirill Terekhov's avatar
Kirill Terekhov committed
1734
1735
1736
				for(Mesh::iteratorTag t = BeginTag(); t != EndTag(); ++t)
					if( t->isDefined(etype) && !t->isSparse(etype) )
						t->AllocateData(etype);
Kirill Terekhov's avatar
Kirill Terekhov committed
1737
				*/
Kirill Terekhov's avatar
Kirill Terekhov committed
1738
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
		}
	}
	
	
	void Mesh::MoveStorage(Storage * e, int new_local_id)
	{
		e->MoveData(new_local_id);
		switch(e->GetElementType())
		{
			case CELL: cells[e->local_id] = NULL; cells[new_local_id] = static_cast<Cell *>(e); break;
			case FACE: faces[e->local_id] = NULL; faces[new_local_id] = static_cast<Face *>(e); break;
			case EDGE: edges[e->local_id] = NULL; edges[new_local_id] = static_cast<Edge *>(e); break;
			case NODE: nodes[e->local_id] = NULL; nodes[new_local_id] = static_cast<Node *>(e); break;
			case ESET: sets[e->local_id] = NULL;  sets[new_local_id] = static_cast<ElementSet *>(e); break;
		}
		e->local_id = new_local_id;
	}
	
#define DOWNSIZE_FACTOR 2

Kirill Terekhov's avatar
Kirill Terekhov committed
1759
1760
1761
1762
1763
1764
	int CompareElementsForReorderApply(const void * a, const void * b, void * udata)
	{
		return (*(Storage **)a)->Integer(*(Tag *)udata) - (*(Storage **)b)->Integer(*(Tag *)udata);

	}

Kirill Terekhov's avatar
Kirill Terekhov committed
1765
1766
1767
	
	void Mesh::ReorderApply(Tag index, ElementType mask)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786