mesh.cpp 61 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);}

namespace INMOST
{
Kirill Terekhov's avatar
Kirill Terekhov committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

#if defined(USE_PARALLEL_WRITE_TIME)
	static std::vector<Mesh *> allocated_meshes;


	void Mesh::AtExit(void)
	{
		while(!allocated_meshes.empty())
		{
			if( allocated_meshes.back() != NULL )
			{
				allocated_meshes.back()->FinalizeFile();
			}
			allocated_meshes.pop_back();
		}
	}
#endif //USE_PARALLEL_WRITE_TIME


Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	const char * TopologyCheckNotifyString(TopologyCheck c)
	{
		switch(c)
		{
			case THROW_EXCEPTION:        return "exception thrown";
			case PRINT_NOTIFY:           return "print notify";
			case DELETE_ON_ERROR:        return "element should be deleted on error";
			case MARK_ON_ERROR:          return "element is marked on error";
			case DUPLICATE_EDGE:         return "search for duplicate edge";
			case DUPLICATE_FACE:         return "search for duplicate face";
			case DUPLICATE_CELL:         return "search for duplicate cell";
			case DEGENERATE_EDGE:        return "TOPOLOGY ERROR: curvilinear edge found"; 
			case DEGENERATE_FACE:        return "TOPOLOGY ERROR: degenerate face found";
			case DEGENERATE_CELL:        return "TOPOLOGY ERROR: degenerate cell found";
			case FACE_ORIENTATION:       return "TOPOLOGY ERROR: bad face orientation";
			case FACE_PLANARITY:         return "TOPOLOGY ERROR: non-planar face found";
			case INTERLEAVED_FACES:      return "TOPOLOGY ERROR: interleaving faces found";
			case TRIPLE_SHARED_FACE:     return "TOPOLOGY ERROR: face have more then two neighbours"; 
			case FLATTENED_CELL:         return "TOPOLOGY ERROR: flattened cell found"; 
			case ADJACENT_DUPLICATE:     return "TOPOLOGY ERROR: duplicates in adjacent elements";
			case ADJACENT_HIDDEN:        return "TOPOLOGY ERROR: hidden element is used as adjacent"; 
			case ADJACENT_VALID:         return "TOPOLOGY ERROR: invalid handle is used as adjacent"; 
			case ADJACENT_DIMENSION:     return "TOPOLOGY ERROR: wrong dimension of adjacent elements";
			case PROHIBIT_MULTILINE:     return "TOPOLOGY ERROR: multiline is prohibited"; 
			case PROHIBIT_POLYGON:       return "TOPOLOGY ERROR: polygon is prohibited"; 
			case PROHIBIT_MULTIPOLYGON:  return "TOPOLOGY ERROR: multipolygon is prohibited"; 
			case PROHIBIT_POLYHEDRON:    return "TOPOLOGY ERROR: polyhedron is prohibited"; 
			case FACE_EDGES_ORDER:       return "TOPOLOGY ERROR: no order in face edges"; 
			case PROHIBIT_CONCAVE_FACE:  return "TOPOLOGY ERROR: concave faces are prohibited"; 
			case PROHIBIT_CONCAVE_CELL:  return "TOPOLOGY ERROR: concave cells are prohibited"; 
			case PROHIBIT_NONSTAR_FACE:  return "TOPOLOGY ERROR: non star-shaped faces are prohibited"; 
			case PROHIBIT_NONSTAR_CELL:  return "TOPOLOGY ERROR: non star-shpaed cells are prohibited"; 
			case FACE_SELF_INTERSECTION: return "TOPOLOGY ERROR: self intersection of face edges detected"; 
			case CELL_SELF_INTERSECTION: return "TOPOLOGY ERROR: self intersection of cell faces detected"; 
			case DISABLE_2D:             return "TOPOLOGY ERROR: 2d mesh support is disabled"; 
			default: return "unknown";
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
65 66

	const char * ElementTypeName(ElementType t)
Kirill Terekhov's avatar
Kirill Terekhov committed
67
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
68 69 70 71 72 73 74 75 76 77 78 79
		switch(t)
		{
			case NONE: return "NONE";
			case NODE: return "NODE";
			case EDGE: return "EDGE";
			case FACE: return "FACE";
			case CELL: return "CELL";
			case ESET: return "ESET";
			case MESH: return "MESH";
		}
		return "UNKNOWN";
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
80
		
Kirill Terekhov's avatar
Kirill Terekhov committed
81 82 83 84 85 86
	Mesh::Mesh()
	:TagManager(), Storage(NULL,ComposeHandle(MESH,0))
	{
		m_link = this;
		integer selfid = TieElement(5);
		assert(selfid == 0);
Kirill Terekhov's avatar
Kirill Terekhov committed
87 88 89 90 91 92 93
		dim = 3;
		have_global_id = NONE;
		checkset = DEFAULT_CHECK;
		errorset = 0;
		new_element = hide_element = 0;

		memset(remember,0,sizeof(remember));
Kirill Terekhov's avatar
Kirill Terekhov committed
94 95 96 97 98 99 100
		tag_coords        = CreateTag("PROTECTED_COORD",DATA_REAL, NODE,NONE,dim);
		tag_high_conn     = CreateTag("PROTECTED_HIGH_CONN",DATA_REFERENCE,ESET|CELL|FACE|EDGE|NODE,NONE);
		tag_low_conn      = CreateTag("PROTECTED_LOW_CONN",DATA_REFERENCE,ESET|CELL|FACE|EDGE|NODE,NONE);
		tag_markers       = CreateTag("PROTECTED_MARKERS",DATA_BULK,CELL|FACE|EDGE|NODE|ESET|MESH,NONE,MarkerFields);
		tag_geom_type     = CreateTag("PROTECTED_GEOM_TYPE",DATA_BULK,CELL|FACE|EDGE|NODE,NONE,1);
		tag_setname       = CreateTag("PROTECTED_SET_NAME",DATA_BULK,ESET,NONE);
		tag_setcomparator = CreateTag("PROTECTED_SET_COMPARATOR",DATA_BULK,ESET,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
101 102
		for(ElementType etype = NODE; etype <= MESH; etype = etype << 1)
			ReallocateData(ElementNum(etype),GetArrayCapacity(ElementNum(etype)));
Kirill Terekhov's avatar
Kirill Terekhov committed
103

Kirill Terekhov's avatar
Kirill Terekhov committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
		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;
Kirill Terekhov's avatar
Kirill Terekhov committed
126
		allocated_meshes.push_back(this);
Kirill Terekhov's avatar
Kirill Terekhov committed
127 128
#endif
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

	Storage::enumerator Mesh::MemoryUsage(HandleType h)
	{
		if( isValidHandle(h) )
		{
			integer etypenum = GetHandleElementNum(h);
			enumerator ret = 2*sizeof(integer); //link and address occupied
			for(Mesh::iteratorTag t = BeginTag(); t != EndTag(); ++t)
			{
				if( t->isDefinedByDim(etypenum) )
				{
					bool have_data = true;
					if( t->isSparseByDim(etypenum) )
					{
						have_data = HaveData(h,*t);
						if( have_data ) 
							ret += sizeof(sparse_sub_record); //size occupied for storage of data link
					}
					if( have_data )
					{
						ret += t->GetRecordSize(); //all utilized data for fixed data, size of support structure for variable data
						if( t->GetSize() == ENUMUNDEF )
							ret += GetDataSize(h,*t)*t->GetBytesSize(); //actually occupied size for sparse data
					}
				}
				if( !sparse_data[etypenum].empty() ) ret += sizeof(sparse_sub_type); //size needed to support sparse data
			}
			//Any additional impact of supporting huge structures over all elements may be added later
157
			return ret;
Kirill Terekhov's avatar
Kirill Terekhov committed
158 159 160
		}
		else return 0;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
161 162
	
	Mesh::Mesh(const Mesh & other)
Kirill Terekhov's avatar
Kirill Terekhov committed
163
	:TagManager(other),Storage(NULL,ComposeHandle(MESH,0))
Kirill Terekhov's avatar
Kirill Terekhov committed
164 165
	{
		
Kirill Terekhov's avatar
Kirill Terekhov committed
166 167 168 169 170 171
		m_link = this;
		integer selfid = TieElement(5);
		assert(selfid == 0);
		//TagManager constuctor copied only tags
		//copy links:
		for(int i = 0; i < 5; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
172
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
173 174 175
			links[i] = other.links[i];
			empty_links[i] = other.empty_links[i];
			empty_space[i] = other.empty_space[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
176
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
177 178
		//this should alocate space for data and copy it including markers and connections
		for(ElementType etype = NODE; etype <= MESH; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
179
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
180 181 182 183
			ReallocateData(ElementNum(etype),GetArrayCapacity(ElementNum(etype)));
			for(tag_array_type::size_type i = 0; i < tags.size(); ++i)
			{
				if( tags[i].isDefined(etype) )
Kirill Terekhov's avatar
Kirill Terekhov committed
184
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
					if( tags[i].isSparse(etype) )
					{
						for(integer lid = 0; lid < LastLocalID(etype); ++lid) if( isValidElement(etype,lid) )
						{
							HandleType h = ComposeHandle(etype,lid);
							if( other.HaveData(h,tags[i]) )
								TagManager::CopyData(tags[i],MGetLink(h,tags[i]),other.MGetLink(h,other.tags[i]));
						}
					}
					else
					{
						for(integer lid = 0; lid < LastLocalID(etype); ++lid) if( isValidElement(etype,lid) )
						{
							HandleType h = ComposeHandle(etype,lid);
							TagManager::CopyData(tags[i],MGetLink(h,tags[i]),other.MGetLink(h,other.tags[i]));
						}
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
202
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
203
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
204
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
205 206
		//setup system tags shortcuts
		dim = other.dim;
Kirill Terekhov's avatar
Kirill Terekhov committed
207 208 209 210 211 212 213
		tag_coords        = CreateTag("PROTECTED_COORD",DATA_REAL, NODE,NONE,dim);
		tag_high_conn     = CreateTag("PROTECTED_HIGH_CONN",DATA_REFERENCE,ESET|CELL|FACE|EDGE|NODE,NONE);
		tag_low_conn      = CreateTag("PROTECTED_LOW_CONN",DATA_REFERENCE,ESET|CELL|FACE|EDGE|NODE,NONE);
		tag_markers       = CreateTag("PROTECTED_MARKERS",DATA_BULK,CELL|FACE|EDGE|NODE|ESET|MESH,NONE,MarkerFields);
		tag_geom_type     = CreateTag("PROTECTED_GEOM_TYPE",DATA_BULK,CELL|FACE|EDGE|NODE,NONE,1);
		tag_setname       = CreateTag("PROTECTED_SET_NAME",DATA_BULK,ESET,NONE);
		tag_setcomparator = CreateTag("PROTECTED_SET_COMPARATOR",DATA_BULK,ESET,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
214 215 216 217 218 219
		//copy supplimentary values
		m_state = other.m_state;
		checkset = other.checkset;
		errorset = other.errorset;
		new_element = other.new_element;
		hide_element = other.hide_element;
Kirill Terekhov's avatar
Kirill Terekhov committed
220
		epsilon = other.epsilon;
Kirill Terekhov's avatar
Kirill Terekhov committed
221 222 223 224 225 226 227 228 229
		have_global_id = other.have_global_id;
		// copy communicator
		if( m_state == Mesh::Parallel ) SetCommunicator(other.comm); else comm = INMOST_MPI_COMM_WORLD;
		// reestablish geometric tags and table
		memcpy(remember,other.remember,sizeof(remember));
		RestoreGeometricTags();
		//this is not needed as it was copied with all the other data
		//recompute global ids
		//AssignGlobalID(other.have_global_id);
Kirill Terekhov's avatar
Kirill Terekhov committed
230 231 232 233 234
	}
	
	Mesh & Mesh::operator =(Mesh const & other)
	{
		if( this == &other ) return *this; //don't do anything
Kirill Terekhov's avatar
Kirill Terekhov committed
235 236
		//first delete everything
		//delete parallel vars
Kirill Terekhov's avatar
Kirill Terekhov committed
237
#if defined(USE_MPI)
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
238
#if defined(USE_MPI_P2P)
Kirill Terekhov's avatar
Kirill Terekhov committed
239 240 241 242 243 244 245
		if( m_state == Mesh::Parallel )
		{
			MPI_Free_mem(shared_space);
			MPI_Win_free(&window);
		}
#endif
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
246 247
		//clear all data fields
		for(ElementType etype = NODE; etype <= MESH; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
248
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
			for(tag_array_type::size_type i = 0; i < tags.size(); ++i)
			{
				if( tags[i].isDefined(etype) )
				{
					if( tags[i].isSparse(etype) )
					{
						for(integer lid = 0; lid < LastLocalID(etype); ++lid) 
							if( isValidElement(etype,lid) )
								DelSparseData(ComposeHandle(etype,lid),tags[i]);
					}
					else if( tags[i].GetSize() == ENUMUNDEF )
					{
						for(integer lid = 0; lid < LastLocalID(etype); ++lid) 
							if( isValidElement(etype,lid) )
								DelDenseData(ComposeHandle(etype,lid),tags[i]);
					}
				}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
267
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
268 269
		//clear links
		for(int i = 0; i < 5; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
270
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
271 272 273
			links[i].clear();
			empty_links[i].clear();
			empty_space[i].clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
274
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
275 276 277 278
		//this should copy tags, clear sparse data, set up dense links
		TagManager::operator =(other);
		//set up new links
		for(int i = 0; i < 5; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
279
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
280 281 282
			links[i] = other.links[i];
			empty_links[i] = other.empty_links[i];
			empty_space[i] = other.empty_space[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
283
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
284 285
		//this should alocate space for data and copy it including markers and connections
		for(ElementType etype = NODE; etype <= MESH; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
286
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
287 288 289 290
			ReallocateData(ElementNum(etype),GetArrayCapacity(ElementNum(etype)));
			for(tag_array_type::size_type i = 0; i < tags.size(); ++i)
			{
				if( tags[i].isDefined(etype) )
Kirill Terekhov's avatar
Kirill Terekhov committed
291
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
					if( tags[i].isSparse(etype) )
					{
						for(integer lid = 0; lid < LastLocalID(etype); ++lid) if( isValidElement(etype,lid) )
						{
							HandleType h = ComposeHandle(etype,lid);
							if( other.HaveData(h,tags[i]) )
								TagManager::CopyData(tags[i],MGetLink(h,tags[i]),other.MGetLink(h,other.tags[i]));
						}
					}
					else
					{
						for(integer lid = 0; lid < LastLocalID(etype); ++lid) if( isValidElement(etype,lid) )
						{
							HandleType h = ComposeHandle(etype,lid);
							TagManager::CopyData(tags[i],MGetLink(h,tags[i]),other.MGetLink(h,other.tags[i]));
						}
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
309
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
310
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
311
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
312 313
		//setup system tags shortcuts
		dim = other.dim;
Kirill Terekhov's avatar
Kirill Terekhov committed
314 315 316 317 318 319 320
		tag_coords        = CreateTag("PROTECTED_COORD",DATA_REAL, NODE,NONE,dim);
		tag_high_conn     = CreateTag("PROTECTED_HIGH_CONN",DATA_REFERENCE,ESET|CELL|FACE|EDGE|NODE,NONE);
		tag_low_conn      = CreateTag("PROTECTED_LOW_CONN",DATA_REFERENCE,ESET|CELL|FACE|EDGE|NODE,NONE);
		tag_markers       = CreateTag("PROTECTED_MARKERS",DATA_BULK,CELL|FACE|EDGE|NODE|ESET|MESH,NONE,MarkerFields);
		tag_geom_type     = CreateTag("PROTECTED_GEOM_TYPE",DATA_BULK,CELL|FACE|EDGE|NODE,NONE,1);
		tag_setname       = CreateTag("PROTECTED_SET_NAME",DATA_BULK,ESET,NONE);
		tag_setcomparator = CreateTag("PROTECTED_SET_COMPARATOR",DATA_BULK,ESET,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
321 322 323 324 325 326
		//copy supplimentary values
		m_state = other.m_state;
		checkset = other.checkset;
		errorset = other.errorset;
		new_element = other.new_element;
		hide_element = other.hide_element;
Kirill Terekhov's avatar
Kirill Terekhov committed
327
		epsilon = other.epsilon;
Kirill Terekhov's avatar
Kirill Terekhov committed
328 329 330 331 332 333 334 335 336
		have_global_id = other.have_global_id;
		// copy communicator
		if( m_state == Mesh::Parallel ) SetCommunicator(other.comm); else comm = INMOST_MPI_COMM_WORLD;
		// reestablish geometric tags and table
		memcpy(remember,other.remember,sizeof(remember));
		RestoreGeometricTags();
		//this is not needed as it was copied with all the other data
		//recompute global ids
		//AssignGlobalID(other.have_global_id);
Kirill Terekhov's avatar
Kirill Terekhov committed
337 338 339 340 341
		return *this;
	}
	
	Mesh::~Mesh()
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
342 343
		//clear all data fields
		for(ElementType etype = NODE; etype <= MESH; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
344
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
			for(tag_array_type::size_type i = 0; i < tags.size(); ++i)
			{
				if( tags[i].isDefined(etype) )
				{
					if( tags[i].isSparse(etype) )
					{
						for(integer lid = 0; lid < LastLocalID(etype); ++lid) 
							if( isValidElement(etype,lid) )
								DelSparseData(ComposeHandle(etype,lid),tags[i]);
					}
					else if( tags[i].GetSize() == ENUMUNDEF )
					{
						for(integer lid = 0; lid < LastLocalID(etype); ++lid) 
							if( isValidElement(etype,lid) )
								DelDenseData(ComposeHandle(etype,lid),tags[i]);
					}
				}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
363
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
364 365
		//clear links
		for(int i = 0; i < 5; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
366
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
367 368 369
			links[i].clear();
			empty_links[i].clear();
			empty_space[i].clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
370
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
371
#if defined(USE_MPI)
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
372
#if defined(USE_MPI_P2P)
Kirill Terekhov's avatar
Kirill Terekhov committed
373 374
		if( m_state == Mesh::Parallel )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
			int test = 0;
			MPI_Finalized(&test);
			if( !test )
			{
				MPI_Free_mem(shared_space);
				MPI_Win_free(&window);
			}
			else
			{
				std::cout << "Cannot release memory and window allocated by MPI" << std::endl;
				std::cout << "since MPI was already finalized. Most likely this" << std::endl;
				std::cout << "happens when you define class Mesh in main() function" << std::endl;
				std::cout << "so that destructor get called after Mesh::Finalize()" << std::endl;
				std::cout << "Please enclose area where you use Mesh class" << std::endl;
				std::cout << "with scopes, so that destructor get called when" << std::endl;
				std::cout << "execution goes out of the scope or dynamically" << std::endl;
				std::cout << "allocate and explicitly delete Mesh class." << std::endl;
				std::cout << "Thank you!" << std::endl;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
394 395
			//~ MPI_Comm_free(&comm);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
396 397
#endif //USE_MPI_P2P
#endif //USE_MPI
Kirill Terekhov's avatar
Kirill Terekhov committed
398
#if defined(USE_PARALLEL_WRITE_TIME)
Kirill Terekhov's avatar
Kirill Terekhov committed
399
		FinalizeFile();
Kirill Terekhov's avatar
Kirill Terekhov committed
400
		out_time.close();
Kirill Terekhov's avatar
Kirill Terekhov committed
401 402 403 404
		for(size_t q = 0; q < allocated_meshes.size(); ++q)
			if (allocated_meshes[q] == this)
				allocated_meshes[q] = NULL;
#endif //USE_PARALLEL_WRITE_TIME
Kirill Terekhov's avatar
Kirill Terekhov committed
405
		//arrays for data are deallocated inside ~TagManager()
Kirill Terekhov's avatar
Kirill Terekhov committed
406 407 408 409 410 411 412 413 414 415 416
	}
	
	
	
	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)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
417 418
		//deallocate data on elements
		for(ElementType etype = NODE; etype <= MESH; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
419
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
			if( (etype & type_mask) && tag.isDefined(etype) )
			{
				if( tag.isSparse(etype) )
				{
					for(integer lid = 0; lid < LastLocalID(etype); ++lid) 
						if( isValidElement(etype,lid) )
							DelSparseData(ComposeHandle(etype,lid),tag);
				}
				else if( tag.GetSize() == ENUMUNDEF )
				{
					for(integer lid = 0; lid < LastLocalID(etype); ++lid) 
						if( isValidElement(etype,lid) )
							DelDenseData(ComposeHandle(etype,lid),tag);
				}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
435 436 437 438 439 440 441
		}
		tag = TagManager::DeleteTag(tag,type_mask);
		return tag;
	}
	
	
	
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
442
	HandleType Mesh::FindSharedAdjacency(const HandleType * arr, enumerator s) const
Kirill Terekhov's avatar
Kirill Terekhov committed
443
	{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
444
		if( s == 0 ) return InvalidHandle();
Kirill Terekhov's avatar
Kirill Terekhov committed
445 446 447
		if( !HideMarker() )
		{
			{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
448
				enumerator flag0, flag1, i;
Kirill Terekhov's avatar
Kirill Terekhov committed
449 450 451
				dynarray<Element::adj_type const *, 64> hcarr(s);
				hcarr[0] = &HighConn(arr[0]);
				if( !hcarr[0]->empty() ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
452
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
453
					for(i = 1; i < s; i++) hcarr[i] = &HighConn(arr[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
454
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
455
				else return InvalidHandle();
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
456
				enumerator it, iend = static_cast<enumerator>(hcarr[0]->size()), jt, jend;
Kirill Terekhov's avatar
Kirill Terekhov committed
457
				for(it = 0; it < iend; ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
458
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
459
					flag0 = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
460
					for(i = 1; i < s; ++i)
Kirill Terekhov's avatar
Kirill Terekhov committed
461
					{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
462
						jend = static_cast<enumerator>(hcarr[i]->size());
Kirill Terekhov's avatar
Kirill Terekhov committed
463 464
						flag1 = 0;
						for(jt = 0; jt < jend; ++jt)
Kirill Terekhov's avatar
Kirill Terekhov committed
465
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
466
							if( hcarr[0]->at(it) == hcarr[i]->at(jt) )
Kirill Terekhov's avatar
Kirill Terekhov committed
467 468
							{
								flag0++;
Kirill Terekhov's avatar
Kirill Terekhov committed
469
								flag1 = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
470 471 472
								break;
							}
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
473
						if( flag1 == 0 ) break;
Kirill Terekhov's avatar
Kirill Terekhov committed
474
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
475
					if( flag0 == s-1 ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
476
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
477
						return hcarr[0]->at(it);
Kirill Terekhov's avatar
Kirill Terekhov committed
478 479
					}
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
480 481 482 483
			}
		}
		else
		{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
484 485
			enumerator ss = Count(arr,s,HideMarker()), nextk;
			enumerator k = ENUMUNDEF, i;
Kirill Terekhov's avatar
Kirill Terekhov committed
486 487 488 489 490
			k = getNext(arr,s,k,HideMarker());
			nextk = getNext(arr,s,k,HideMarker());
			Element::adj_type const & hc0 = HighConn(arr[k]);
			Element::adj_type::size_type it, iend = hc0.size(), jt, jend, flag0, flag1;
			for(it = 0; it < iend; ++it) if( !GetMarker(hc0[it],HideMarker()) )
Kirill Terekhov's avatar
Kirill Terekhov committed
491
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
492 493 494
				flag0 = 0;
				i = nextk;
				while( i < s )
Kirill Terekhov's avatar
Kirill Terekhov committed
495
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
496 497 498 499
					flag1 = 0;
					Element::adj_type const & ihc = HighConn(arr[i]);
					jend = ihc.size();
					for(jt = 0; jt < jend; ++jt) if( !GetMarker(ihc[jt],HideMarker()) )
Kirill Terekhov's avatar
Kirill Terekhov committed
500
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
501
						if( hc0[it] == ihc[jt] )
Kirill Terekhov's avatar
Kirill Terekhov committed
502 503
						{
							flag0++;
Kirill Terekhov's avatar
Kirill Terekhov committed
504
							flag1 = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
505 506 507
							break;
						}
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
508 509
					if( flag1 == 0 ) break;
					i = getNext(arr,s,i,HideMarker());
Kirill Terekhov's avatar
Kirill Terekhov committed
510
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
511
				if( flag0 == ss-1 ) return hc0[it];
Kirill Terekhov's avatar
Kirill Terekhov committed
512 513
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
514
		return InvalidHandle();
Kirill Terekhov's avatar
Kirill Terekhov committed
515 516 517
	}
	
	
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
518
	TopologyCheck Mesh::BeginTopologyCheck(ElementType etype, const HandleType * adj, enumerator s)
Kirill Terekhov's avatar
Kirill Terekhov committed
519
	{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
520
		enumerator i,j,d = ENUMUNDEF;
Kirill Terekhov's avatar
Kirill Terekhov committed
521
		TopologyCheck chk = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
522
		if( GetTopologyCheck(ADJACENT_VALID) )
Kirill Terekhov's avatar
Kirill Terekhov committed
523 524
		{
			for(i = 0; i < s; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
525
				if( !isValidHandle(adj[i]) )
Kirill Terekhov's avatar
Kirill Terekhov committed
526
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
527 528
					chk |= ADJACENT_VALID;
					if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(ADJACENT_VALID) << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
529 530
				}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
531
		if( GetTopologyCheck(ADJACENT_HIDDEN) )
Kirill Terekhov's avatar
Kirill Terekhov committed
532 533
		{
			for(i = 0; i < s; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
534
				if( GetMarker(adj[i],HideMarker()) )
Kirill Terekhov's avatar
Kirill Terekhov committed
535
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
536 537
					chk |= ADJACENT_HIDDEN;
					if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(ADJACENT_HIDDEN) << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
538 539 540 541 542
				}
		}
		if( GetTopologyCheck(ADJACENT_DUPLICATE) )
		{
			bool have_dup = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
543
			MarkerType dup = CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
544 545
			for(i = 0; i < s; i++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
546
				if( GetMarker(adj[i],dup) ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
547 548 549 550
				{
					have_dup = true; // duplication of element
					break;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
551
				else SetMarker(adj[i],dup);
Kirill Terekhov's avatar
Kirill Terekhov committed
552
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
553
			for(i = 0; i < s; i++) RemMarker(adj[i],dup);
Kirill Terekhov's avatar
Kirill Terekhov committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567
			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++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
568
				j = ElementByHandle(adj[i])->GetElementDimension();
Kirill Terekhov's avatar
Kirill Terekhov committed
569 570 571 572 573 574 575 576 577 578 579 580 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
				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++)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
690
					if( ElementByHandle(adj[i])->nbAdjElements(CELL) == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
691 692 693 694 695 696 697 698
					{
						check = true;
						break;
					}
				}
				if( check )
				{
					bool happen = true;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
699
					if( (GetTopologyCheck(DUPLICATE_CELL)) && (FindSharedAdjacency(adj,s) != InvalidHandle()) ) happen = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
700 701 702 703 704 705 706 707 708 709 710 711
					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) )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
712 713 714
				ElementArray<Node> allnodes(this);
				for(i = 0; i < s; i++) allnodes.Unite(ElementByHandle(adj[i])->getNodes());
				ElementArray<Face> faces = allnodes[0].getFaces();
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
715 716
				for(i = 1; i < static_cast<enumerator>(allnodes.size()) && !faces.empty(); i++) faces.Intersect(allnodes[i].getFaces());
				for(i = 0; i < static_cast<enumerator>(faces.size()); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
717
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
718
					if( faces[i]->nbAdjElements(NODE) != allnodes.size() )
Kirill Terekhov's avatar
Kirill Terekhov committed
719 720 721 722 723 724 725 726 727 728 729
					{
						chk |= INTERLEAVED_FACES;
						if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(INTERLEAVED_FACES) << std::endl;
					}
				}
			}
		}
		errorset |= chk;
		return chk;
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
730
	TopologyCheck Mesh::EndTopologyCheck(HandleType he)
Kirill Terekhov's avatar
Kirill Terekhov committed
731 732
	{
		TopologyCheck chk = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
733
		Element e = ElementByHandle(he);
Kirill Terekhov's avatar
Kirill Terekhov committed
734 735 736 737 738
		switch(e->GetGeometricDimension(e->GetGeometricType()))
		{
		case 3:
			if( GetTopologyCheck(FLATTENED_CELL) )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
739
				ElementArray<Face> faces = e->getFaces();
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
740 741
				enumerator num = e->nbAdjElements(NODE);
				for(enumerator i = 0; i < static_cast<enumerator>(faces.size()); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
742 743 744 745 746 747 748 749 750 751
				{
					if( faces[i].nbAdjElements(NODE) == num )
					{
						chk |= FLATTENED_CELL;
						if( GetTopologyCheck(PRINT_NOTIFY) ) std::cerr << TopologyCheckNotifyString(FLATTENED_CELL) << std::endl;
					}
				}
			}
			if( GetTopologyCheck(FACE_ORIENTATION) )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
752
				ElementArray<Face> faces = e->getFaces();
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
753
				for(enumerator i = 0; i < static_cast<enumerator>(faces.size()); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
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
				{
					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;
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
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
	Node Mesh::CreateNode(const real * coords)
	{
		integer id = TieElement(0);
		HandleType h = ComposeHandle(0,id);
		SetGeometricType(h,Element::Vertex);
		real * v = static_cast<Storage::real *>(MGetDenseLink(h,CoordsTag()));
		for(integer i = 0; i < dim; i++) v[i] = coords[i];
		SetMarker(h,NewMarker());
		return Node(this,h);
	}

	void PrintHandle(HandleType h)
	{
		std::cout << ElementTypeName(GetHandleElementType(h)) << " " << GetHandleID(h);
	}

	void PrintAdjElem(Element::adj_type const & arr)
	{
		for(Element::adj_type::size_type it = 0; it < arr.size(); ++it)
		{
			PrintHandle(arr[it]);
			std::cout << " ";
		}
		std::cout << std::endl;
	}

	void PrintHandles(const HandleType * beg, const HandleType * end)
Kirill Terekhov's avatar
Kirill Terekhov committed
846
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
847 848 849 850 851 852
		for(const HandleType * it = beg; it != end; ++it)
		{
			PrintHandle(*it);
			std::cout << " ";
		}
		std::cout << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
853
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
854 855

	std::pair<Edge,bool> Mesh::CreateEdge(const ElementArray<Node> & nodes)
Kirill Terekhov's avatar
Kirill Terekhov committed
856
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
857 858
		HandleType he = InvalidHandle();
		if( !nodes.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
859 860
		{
			TopologyCheck chk = 0;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
861
			chk |= BeginTopologyCheck(EDGE,nodes.data(),static_cast<enumerator>(nodes.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
862 863 864 865 866 867
			if( chk != 0 )
			{
				if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
			}
			if( GetTopologyCheck() & DUPLICATE_EDGE )
			{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
868
				HandleType test = FindSharedAdjacency(nodes.data(),static_cast<enumerator>(nodes.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
				if (test != InvalidHandle()) return std::make_pair(Edge(this,test),false);
			}
			integer id = TieElement(1);
			he = ComposeHandle(1,id);
			for(ElementArray<Node>::size_type i = 0; i < nodes.size(); i++)
			{
				Element::adj_type & hc = HighConn(nodes.at(i));

				//PrintHandle(nodes.at(i)); std::cout << " current: "; PrintAdjElem(hc); 

				hc.push_back(he);

				//std::cout << "add "; PrintHandle(he); std::cout << " result: "; PrintAdjElem(hc);
			}
			Element::adj_type & lc = LowConn(he);

			//PrintHandle(he); std::cout << " current: "; PrintAdjElem(lc);
			
			lc.insert(lc.end(),nodes.data(),nodes.data()+nodes.size());

			//std::cout << "add "; PrintHandles(nodes.data(),nodes.data()+nodes.size());
			//std::cout << "result: "; PrintAdjElem(lc);
			//DEBUG
			/*
			bool halt = false;
			if(!Element(this,he)->CheckElementConnectivity()) 
Kirill Terekhov's avatar
Kirill Terekhov committed
895
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
896 897
				Element(this,he)->PrintElementConnectivity();
				halt = true;
Kirill Terekhov's avatar
Kirill Terekhov committed
898
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
899 900 901 902 903 904 905 906 907 908 909 910 911
			for(ElementArray<Node>::size_type i = 0; i < nodes.size(); i++) 
				if(!nodes[i]->CheckElementConnectivity()) 
				{
					nodes[i]->PrintElementConnectivity();
					halt = true;
				}
			assert(!halt);
			*/
			//DEBUG END
			ComputeGeometricType(he);
			SetMarker(he,NewMarker());
			RecomputeGeometricData(he);
			chk |= EndTopologyCheck(he);
Kirill Terekhov's avatar
Kirill Terekhov committed
912 913
			if( chk != 0 )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
914 915
				if( GetTopologyCheck(MARK_ON_ERROR) ) Integer(he,TopologyErrorTag()) = chk;
				if( GetTopologyCheck(DELETE_ON_ERROR) ) { Destroy(he); he = InvalidHandle();}
Kirill Terekhov's avatar
Kirill Terekhov committed
916 917 918
				if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
919
		return std::make_pair(Edge(this,he),true);
Kirill Terekhov's avatar
Kirill Terekhov committed
920 921 922
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
923
	std::pair<Face,bool> Mesh::CreateFace(const ElementArray<Node> & f_nodes)
Kirill Terekhov's avatar
Kirill Terekhov committed
924
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
925 926 927
		ElementArray<Edge> f_edges(this,f_nodes.size());
		ElementArray<Node> e_nodes(this,2);
		if( f_nodes.size() == 2 ) //This is an edge for 2d!
Kirill Terekhov's avatar
Kirill Terekhov committed
928
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
929 930 931 932 933
			e_nodes.resize(1);
			e_nodes.at(0) = f_nodes.at(0);
			f_edges.at(0) = CreateEdge(e_nodes).first->GetHandle();
			e_nodes.at(0) = f_nodes.at(1);
			f_edges.at(1) = CreateEdge(e_nodes).first->GetHandle();
Kirill Terekhov's avatar
Kirill Terekhov committed
934 935 936
		}
		else
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
937
			for(ElementArray<Node>::size_type i = 0; i < f_nodes.size(); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
938
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
939 940 941
				e_nodes.at(0) = f_nodes.at(i);
				e_nodes.at(1) = f_nodes.at((i+1)%f_nodes.size());
				f_edges.at(i) = CreateEdge(e_nodes).first->GetHandle();
Kirill Terekhov's avatar
Kirill Terekhov committed
942 943
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
944
		return CreateFace(f_edges);
Kirill Terekhov's avatar
Kirill Terekhov committed
945 946
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
947
	std::pair<Face,bool> Mesh::CreateFace(const ElementArray<Edge> & f_edges)
Kirill Terekhov's avatar
Kirill Terekhov committed
948
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
949 950
		HandleType he = InvalidHandle();
		if( !f_edges.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
951 952
		{
			TopologyCheck chk = 0;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
953
			chk |= BeginTopologyCheck(FACE,f_edges.data(),static_cast<enumerator>(f_edges.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
954 955 956 957 958 959
			if( chk != 0 )
			{
				if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
			}
			if( GetTopologyCheck(DUPLICATE_FACE) )
			{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
960
				HandleType test = FindSharedAdjacency(f_edges.data(),static_cast<enumerator>(f_edges.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
961
				if (test != InvalidHandle()) return std::make_pair(Face(this,test),false);
Kirill Terekhov's avatar
Kirill Terekhov committed
962
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
			integer id = TieElement(2);
			he = ComposeHandle(2,id);
			for(ElementArray<Edge>::size_type i = 0; i < f_edges.size(); i++)
			{
				Element::adj_type & hc = HighConn(f_edges.at(i));

				//PrintHandle(f_edges.at(i)); std::cout << " current: "; PrintAdjElem(hc); 

				hc.push_back(he);

				//std::cout << "add "; PrintHandle(he); std::cout << " result: "; PrintAdjElem(hc);
			}
			Element::adj_type & lc = LowConn(he);

			//PrintHandle(he); std::cout << "current: "; PrintAdjElem(lc);

			lc.insert(lc.end(),f_edges.data(),f_edges.data()+f_edges.size());

			//std::cout << "add "; PrintHandles(f_edges.data(),f_edges.data()+f_edges.size());
			//std::cout << "result: "; PrintAdjElem(lc);
			//DEBUG
			/*
			bool halt = false;
			if(!Element(this,he)->CheckElementConnectivity())
Kirill Terekhov's avatar
Kirill Terekhov committed
987
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
988 989 990 991 992 993 994 995 996 997
				Element(this,he)->PrintElementConnectivity();
				halt = true;
			}
			for(ElementArray<Edge>::size_type i = 0; i < f_edges.size(); i++) 
			{
				if(!f_edges[i]->CheckElementConnectivity())
				{
					f_edges[i]->PrintElementConnectivity();
					halt = true;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
998
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
999 1000 1001 1002 1003 1004 1005
			assert(!halt);
			*/
			//DEBUG END
			ComputeGeometricType(he);
			SetMarker(he,NewMarker());
			RecomputeGeometricData(he);
			chk |= EndTopologyCheck(he);
Kirill Terekhov's avatar
Kirill Terekhov committed
1006 1007
			if( chk != 0 )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1008 1009
				if( GetTopologyCheck(MARK_ON_ERROR)   ) Integer(he,TopologyErrorTag()) = chk;
				if( GetTopologyCheck(DELETE_ON_ERROR) ) { Destroy(he); he = InvalidHandle();}
Kirill Terekhov's avatar
Kirill Terekhov committed
1010 1011 1012
				if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1013
		return std::make_pair(Face(this,he),true);
Kirill Terekhov's avatar
Kirill Terekhov committed
1014 1015 1016
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1017
	std::pair<Cell,bool> Mesh::CreateCell(const ElementArray<Node> & c_f_nodes, const integer * c_f_sizes, integer s, const ElementArray<Node> & suggest_nodes_order)
Kirill Terekhov's avatar
Kirill Terekhov committed
1018
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1019 1020 1021
		ElementArray<Face> c_faces(this,s);
		ElementArray<Node>::size_type j = 0;
		for(integer i = 0; i < s; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1022
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1023
			c_faces.at(i) = CreateFace(ElementArray<Node>(this, c_f_nodes.data()+j, c_f_nodes.data()+j + c_f_sizes[i])).first->GetHandle();
Kirill Terekhov's avatar
Kirill Terekhov committed
1024 1025
			j += c_f_sizes[i];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1026
		return CreateCell(c_faces,suggest_nodes_order);
Kirill Terekhov's avatar
Kirill Terekhov committed
1027 1028 1029
	}
	

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

						for(Element::adj_type::size_type i = 0; i < lc.size(); i++) if( !Hidden(lc[i]) )  //iterate over faces
						{
							Element::adj_type & ilc = LowConn(lc[i]);
							for(Element::adj_type::size_type j = 0; j < ilc.size(); j++) if( !Hidden(ilc[j]) ) //iterate over face edges
								RemMarker(ilc[j],mrk);
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1307
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1308
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1309
				for(ElementArray<Node>::size_type it = 0; it < ret.size(); it++) ret[it]->RemMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1310 1311 1312 1313 1314 1315 1316
				ReleaseMarker(mrk);
				break;
			}
		}

	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1317
	std::pair<Cell,bool> Mesh::CreateCell(const ElementArray<Face> & c_faces, const ElementArray<Node> & c_nodes)
Kirill Terekhov's avatar
Kirill Terekhov committed
1318
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1319 1320
		HandleType he = InvalidHandle();
		if( !c_faces.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1321 1322
		{
			TopologyCheck chk = 0;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1323
			chk |= BeginTopologyCheck(CELL,c_faces.data(),static_cast<enumerator>(c_faces.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
1324 1325 1326 1327 1328 1329
			if( chk != 0 )
			{
				if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
			}
			if( GetTopologyCheck(DUPLICATE_CELL) )
			{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1330
				HandleType test = FindSharedAdjacency(c_faces.data(),static_cast<enumerator>(c_faces.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
1331
				if (test != InvalidHandle()) return std::make_pair(Cell(this,test),false);
Kirill Terekhov's avatar
Kirill Terekhov committed
1332
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1333 1334 1335
			integer id = TieElement(3);
			he = ComposeHandle(3,id);
			for(ElementArray<Face>::size_type i = 0; i < c_faces.size(); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1336
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1337 1338
				Element::adj_type & hc = HighConn(c_faces.at(i));
				hc.push_back(he);
Kirill Terekhov's avatar
Kirill Terekhov committed
1339
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1340 1341 1342
			Element::adj_type & lc = LowConn(he);
			lc.insert(lc.begin(),c_faces.begin(),c_faces.end());
			ComputeGeometricType(he);		
Kirill Terekhov's avatar
Kirill Terekhov committed
1343
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1344 1345
				//bool halt = false; //DEBUG
				if( c_nodes.empty() ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1346
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
					ElementArray<Node> nodes(this);
					RestoreCellNodes(he,nodes);
					for(ElementArray<Node>::size_type k = 0; k < nodes.size(); k++)
					{
						Element::adj_type & lc = LowConn(nodes.at(k));
						lc.push_back(he);
					}
					Element::adj_type & hc = HighConn(he);
					hc.insert(hc.begin(),nodes.begin(),nodes.end());

					//DEBUG
					/*
					for(ElementArray<Node>::size_type i = 0; i < nodes.size(); i++) 
					{
						if(!nodes[i]->CheckElementConnectivity())
						{
							nodes[i]->PrintElementConnectivity();
							halt = true;
						}
					}
					*/
					//END DEBUG
Kirill Terekhov's avatar
Kirill Terekhov committed
1369
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1370
				else
Kirill Terekhov's avatar
Kirill Terekhov committed
1371
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
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
					for(ElementArray<Node>::size_type k = 0; k < c_nodes.size(); k++)
					{
						Element::adj_type & lc = LowConn(c_nodes.at(k));
						lc.push_back(he);
					}
					Element::adj_type & hc = HighConn(he);
					hc.insert(hc.begin(),c_nodes.begin(),c_nodes.end());
					//DEBUG
					/*
					for(ElementArray<Face>::size_type i = 0; i < c_nodes.size(); i++) 
					{
						if(!c_nodes[i]->CheckElementConnectivity())
						{
							c_nodes[i]->PrintElementConnectivity();
							halt = true;
						}
					}
					*/
					//END DEBUG
				}
				//DEBUG
				/*
				if(!Element(this,he)->CheckElementConnectivity())
				{
					Element(this,he)->PrintElementConnectivity();
					halt = true;
				}
				for(ElementArray<Face>::size_type i = 0; i < c_faces.size(); i++)
				{
					if( !c_faces[i]->CheckElementConnectivity() )
					{
						c_faces[i]->PrintElementConnectivity();
						halt = true;
					}
				}
				assert(!halt);
				*/
				//END DEBUG
				SetMarker(he,NewMarker());
				RecomputeGeometricData(he);
				chk |= EndTopologyCheck(he);
Kirill Terekhov's avatar
Kirill Terekhov committed
1413 1414
				if( chk != 0 )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1415 1416
					if( GetTopologyCheck(MARK_ON_ERROR)   ) Integer(he, TopologyErrorTag()) = chk;
					if( GetTopologyCheck(DELETE_ON_ERROR) ) { Destroy(he); he = InvalidHandle();}
Kirill Terekhov's avatar
Kirill Terekhov committed
1417 1418 1419
					if( GetTopologyCheck(THROW_EXCEPTION) ) throw TopologyCheckError;
				}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1420 1421 1422

			
			
Kirill Terekhov's avatar
Kirill Terekhov committed
1423
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1424
		return std::make_pair(Cell(this,he),true);
Kirill Terekhov's avatar
Kirill Terekhov committed
1425 1426 1427
	}