inmost_mesh.h 70.2 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
#ifndef INMOST_MESH_H_INCLUDED
#define INMOST_MESH_H_INCLUDED

#include "inmost_common.h"

#if defined(USE_MESH)

#define __NDT 3
#define __NET 6

namespace INMOST
{
	class Mesh;
	class Storage;
	class Element;
	class TagManager;
	class Node;
	class Edge;
	class Face;
	class Cell;

	typedef INMOST_DATA_BULK_TYPE ElementType;
	static const ElementType NONE = 0x00;
	static const ElementType NODE = 0x01;
	static const ElementType EDGE = 0x02;
	static const ElementType FACE = 0x04;
	static const ElementType CELL = 0x08;
	static const ElementType ESET = 0x10;
	static const ElementType MESH = 0x20;
	
	
	typedef INMOST_DATA_BULK_TYPE GeometricData;
	static const GeometricData CENTROID     = 0;
	static const GeometricData NORMAL       = 1;
	static const GeometricData ORIENTATION  = 2;
	static const GeometricData MEASURE      = 3;
	static const GeometricData BARYCENTER   = 4;
	
Kirill Terekhov's avatar
Kirill Terekhov committed
40
41
42
43
44
45
	typedef INMOST_DATA_BULK_TYPE SyncBitOp; //< This type is used for marker synchronization
	static const SyncBitOp SYNC_BIT_NEW = 0;
	static const SyncBitOp SYNC_BIT_OR  = 1;
	static const SyncBitOp SYNC_BIT_XOR = 2;
	static const SyncBitOp SYNC_BIT_AND = 3;

Kirill Terekhov's avatar
Kirill Terekhov committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	//Use topolgy checking for debug purposes
	typedef INMOST_DATA_ENUM_TYPE TopologyCheck; 
	static const TopologyCheck THROW_EXCEPTION        = 0x00000001; //done//throw TopologyError exception on error
	static const TopologyCheck PRINT_NOTIFY           = 0x00000002; //done//print topology notify to std::cerr
	static const TopologyCheck DELETE_ON_ERROR        = 0x00000004; //done//element should be deleted if there is an error in EndTopologyCheck
	static const TopologyCheck MARK_ON_ERROR          = 0x00000008; //done//bitwise OR of errors is recorded to sparse integer tag of element (if not deleted), availible by TopologyErrorTag()
	static const TopologyCheck DUPLICATE_EDGE         = 0x00000010; //done//check that edge already exists, on occurance CreateEdge silently returns existant edge
	static const TopologyCheck DUPLICATE_FACE         = 0x00000020; //done//check that face already exists, on occurance CreateFace silently returns existant face
	static const TopologyCheck DUPLICATE_CELL         = 0x00000040; //done//check that cell already exists, on occurance CreateCell silently returns existant cell
	static const TopologyCheck DEGENERATE_EDGE        = 0x00000080; //done//check that edge have more then two nodes (no support for this kind of object)
	static const TopologyCheck DEGENERATE_FACE        = 0x00000100; //done//produce error if face consists of less then 3 edges
	static const TopologyCheck DEGENERATE_CELL        = 0x00000200; //done//produce error if cell consists of less then 4 faces
	static const TopologyCheck FACE_ORIENTATION       = 0x00000400; //done//produce error if face have wrong orientation of edges, for star-shaped elements only
	static const TopologyCheck FACE_PLANARITY         = 0x00000800; //done//produce error if face is non planar
	static const TopologyCheck INTERLEAVED_FACES      = 0x00001000; //done//produce error if there is another face that already uses same nodes
	static const TopologyCheck TRIPLE_SHARED_FACE     = 0x00002000; //done//check that every face have exectly two neighbours, if DUPLICATE_CELL is activated, then checks for cell duplication
	static const TopologyCheck FLATTENED_CELL         = 0x00004000; //done//produce error if one of the faces of the cell contains all the nodes of the cell
	static const TopologyCheck ADJACENT_DUPLICATE     = 0x00008000; //done//produce error if provided array of elements for creation contain duplications
	static const TopologyCheck ADJACENT_HIDDEN        = 0x00010000; //done//hidden elements should not be used when new elements are created
	static const TopologyCheck ADJACENT_ALIEN         = 0x00020000; //done//elements that belong to another mesh should not be used
	static const TopologyCheck ADJACENT_DIMENSION     = 0x00040000; //done//produce error if provided array of elements have wrong geometric dimension
	static const TopologyCheck PROHIBIT_MULTILINE     = 0x00080000; //done//(needs NEED_TEST_CLOSURE) produce error if edges of faces are not closed, or face is folded
	static const TopologyCheck PROHIBIT_POLYGON       = 0x00100000; //done//allow only known types of elements: Tet,Quad, Line
	static const TopologyCheck PROHIBIT_MULTIPOLYGON  = 0x00200000; //done//(needs NEED_TEST_CLOSURE) produce error if faces of cell are not closed, or cell is folded
	static const TopologyCheck PROHIBIT_POLYHEDRON    = 0x00400000; //done//allow only known types of elements: Tet,Hex,Prism,Pyramid
	static const TopologyCheck FACE_EDGES_ORDER       = 0x00800000; //not implemented(implement CheckEdgeOrder,FixEdgeOrder, class Cell,Face)//edges of the face should form one closed loop
	static const TopologyCheck PROHIBIT_CONCAVE_FACE  = 0x01000000; //not implemented//don't allow concave face
	static const TopologyCheck PROHIBIT_CONCAVE_CELL  = 0x02000000; //not implemented//don't allow concave cell
	static const TopologyCheck PROHIBIT_NONSTAR_FACE  = 0x04000000; //not implemented//don't allow non-star shaped face
	static const TopologyCheck PROHIBIT_NONSTAR_CELL  = 0x08000000; //not implemented//don't allow non-star shaped concave cell
	static const TopologyCheck FACE_SELF_INTERSECTION = 0x10000000; //not implemented//edges of the face don't cross each other
	static const TopologyCheck CELL_SELF_INTERSECTION = 0x20000000; //not implemented//faces of the cell don't cross each other
	static const TopologyCheck NEED_TEST_CLOSURE      = 0x40000000; //done//silent, test's for closure in ComputeGeometricType, needed to detect MultiLine and MultiPolygon
	static const TopologyCheck DISABLE_2D             = 0x80000000; //done//don't allow 2d grids, where edges appear to be vertexes, faces are edges and cells are faces
	static const TopologyCheck GRID_CONFORMITY        = NEED_TEST_CLOSURE | PROHIBIT_MULTILINE | PROHIBIT_MULTIPOLYGON  | INTERLEAVED_FACES | TRIPLE_SHARED_FACE;
Kirill Terekhov's avatar
Kirill Terekhov committed
81
	static const TopologyCheck DEFAULT_CHECK          = THROW_EXCEPTION | DUPLICATE_EDGE | DUPLICATE_FACE | PRINT_NOTIFY;
Kirill Terekhov's avatar
Kirill Terekhov committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	
	__INLINE static 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_ALIEN:         return "TOPOLOGY ERROR: element from other mesh 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
122
123
124
	/// This function helps to determine whether one type is chosen or multiple
	__INLINE static bool OneType(ElementType t) {return t > 0 && (t & (t-1)) == 0;}

Kirill Terekhov's avatar
Kirill Terekhov committed
125
126
127
128
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
157
	__INLINE static int ElementNum(ElementType t)
	{
		unsigned int v = static_cast<unsigned int>(t);  // 32-bit value to find the log2 of 
		static const unsigned int b[] = {0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000};
		register unsigned int r = (v & b[0]) != 0;
		r |= ((v & b[4]) != 0) << 4;
		r |= ((v & b[3]) != 0) << 3;
		r |= ((v & b[2]) != 0) << 2;
		r |= ((v & b[1]) != 0) << 1;
		return static_cast<int>(r);
	}
	
	
	__INLINE static const char * ElementTypeName(ElementType t)
	{
		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";
	}
	
	
	
	typedef array<INMOST_DATA_REAL_TYPE>    inner_real_array;
	typedef array<INMOST_DATA_INTEGER_TYPE> inner_integer_array;
	typedef array<INMOST_DATA_BULK_TYPE>    inner_bulk_array;
Kirill Terekhov's avatar
Kirill Terekhov committed
158
	typedef array<Element *>                inner_reference_array;
Kirill Terekhov's avatar
Kirill Terekhov committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
		
	enum DataType
	{
		DATA_REAL      = 0, 
		DATA_INTEGER   = 1, 
		DATA_BULK      = 2,
		DATA_REFERENCE = 3
	};
	
	__INLINE static size_t DataTypeBytesSize(DataType t)
	{
		switch(t)
		{
			case DATA_BULK:      return sizeof(INMOST_DATA_BULK_TYPE);
			case DATA_INTEGER:   return sizeof(INMOST_DATA_INTEGER_TYPE);
			case DATA_REAL:      return sizeof(INMOST_DATA_REAL_TYPE);
			case DATA_REFERENCE: return sizeof(Element *);
		}
		return 0;
	}
	
	__INLINE static size_t VariableDataSize(DataType t)
	{
		switch(t)
		{
			case DATA_REAL:      return sizeof(inner_real_array);
			case DATA_INTEGER:   return sizeof(inner_integer_array);
			case DATA_BULK:      return sizeof(inner_bulk_array);
			case DATA_REFERENCE: return sizeof(inner_reference_array);
		}
		return 0;
	}
	
	__INLINE static const char * DataTypeName(DataType t)
	{
		switch(t)
		{
			case DATA_REAL:      return "REAL";
			case DATA_INTEGER:   return "INTEGER";
			case DATA_BULK:      return "BULK";
			case DATA_REFERENCE: return "REFERENCE";
		}
		return "UNKNOWN";
	}


	typedef INMOST_DATA_ENUM_TYPE MIDType;

	class TagMemory //implemented in tag.cpp
	{
	public:
		~TagMemory();
		TagMemory(Mesh * m, const TagMemory & other);
		TagMemory & operator =(TagMemory const & other);
	private:
		TagMemory();
		INMOST_DATA_ENUM_TYPE pos[__NET]; 
		DataType dtype;                 
		std::string tagname;            
		INMOST_MPI_Type bulk_data_type;   
		INMOST_DATA_ENUM_TYPE size;       
		bool sparse[__NET];             
		size_t record_size;
		TagManager * tag_manager;
		Mesh * m_link;
		friend class Tag;
	};

	class Tag //implemented in tag.cpp
	{
	private:
		TagMemory * mem;
		__INLINE size_t GetRecordSize() {return mem->record_size;}
		__INLINE void SetSize(INMOST_DATA_ENUM_TYPE size) {mem->size = size;}
		Tag(Mesh * m, std::string name, DataType _dtype, INMOST_DATA_ENUM_TYPE size); //Create me through TagManager::CreateTag
		__INLINE void SetPosition(INMOST_DATA_ENUM_TYPE pos, ElementType type) {mem->pos[ElementNum(type)] = pos;}
		__INLINE INMOST_DATA_ENUM_TYPE GetPosition(ElementType type) const {assert(mem != NULL); return mem->pos[ElementNum(type)];}
		__INLINE void SetSparse(ElementType type) {mem->sparse[ElementNum(type)] = true;}
		__INLINE void SetTagManager(TagManager * tm) {mem->tag_manager = tm;}
		__INLINE TagManager * GetTagManager() {return mem->tag_manager;}
		void ShrinkData(ElementType type, size_t new_size);
		void AllocateData(ElementType t);
		__INLINE INMOST_DATA_ENUM_TYPE GetPositionNum(INMOST_DATA_ENUM_TYPE typenum) const {assert(mem != NULL); return mem->pos[typenum];}
		__INLINE void SetBulkDataType(INMOST_MPI_Type type){assert(mem!=NULL); if( mem->dtype == DATA_BULK ) mem->bulk_data_type = type;}
	public:
		~Tag() {mem = NULL;}
		Tag() {mem = NULL;}
		Tag(const Tag & other) {mem = other.mem;}
		bool operator <(const Tag & other) const {return mem < other.mem;}
		bool operator >(const Tag & other) const {return mem > other.mem;}
		bool operator ==(const Tag & other) const {return mem == other.mem;}
		bool operator !=(const Tag & other) const {return mem != other.mem;}
		Tag & operator =(Tag const & other) {mem = other.mem; return *this;	}
		__INLINE DataType GetDataType() const {assert(mem!=NULL); return mem->dtype;}
		__INLINE INMOST_MPI_Type GetBulkDataType() const {assert(mem!=NULL); return mem->bulk_data_type;}
		__INLINE size_t GetBytesSize() const {assert(mem!=NULL); return DataTypeBytesSize(mem->dtype);}
		__INLINE INMOST_DATA_ENUM_TYPE GetSize() const {assert(mem!=NULL); return mem->size;}
		__INLINE std::string GetTagName() const {assert(mem!=NULL); return mem->tagname;}
Kirill Terekhov's avatar
Kirill Terekhov committed
257
258
		__INLINE bool isDefined(ElementType type) const {assert(mem!=NULL && OneType(type)); return GetPosition(type) != ENUMUNDEF;}
		__INLINE bool isSparse(ElementType type) const {assert(mem!=NULL && OneType(type)); return mem->sparse[ElementNum(type)];}
Kirill Terekhov's avatar
Kirill Terekhov committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
		__INLINE bool isValid() const {return mem != NULL;}
		__INLINE Mesh * GetMeshLink() const {assert(mem!=NULL); return mem->m_link;}		
		__INLINE bool isSparseNum(INMOST_DATA_ENUM_TYPE typenum) const {assert(mem!=NULL); return mem->sparse[typenum];}
		friend class TagManager;
		friend class Storage;
		friend class Mesh;
	};
	
	class TagManager //implemented in tag.cpp
	{
	protected:
		TagManager();
		TagManager(Mesh * m, const TagManager & other);
		TagManager & assign(Mesh * m, TagManager const & other);
	public:
		typedef chunk_array<INMOST_DATA_ENUM_TYPE> 	empty_data;
		typedef chunk_array<Tag> tag_array_type;
		typedef tag_array_type::iterator iteratorTag;
		typedef chunk_array<INMOST_DATA_BULK_TYPE> dense_sub_type;
		typedef chunk_array< dense_sub_type> dense_data_array_type;
		
		virtual ~TagManager();
		bool HaveTag(std::string name);
		Tag GetTag(std::string name);
		void ListTagNames(std::vector<std::string> & list) const;
		Tag CreateTag(Mesh * m, std::string name, DataType dtype, ElementType etype, ElementType sparse, INMOST_DATA_ENUM_TYPE size = ENUMUNDEF); 
		virtual Tag DeleteTag(Tag tag, ElementType mask); 
		
		__INLINE INMOST_DATA_ENUM_TYPE NumberOfTags() { return static_cast<INMOST_DATA_ENUM_TYPE>(tags.size()); }
		__INLINE iteratorTag BeginTag() {return tags.begin(); }
		__INLINE iteratorTag EndTag() {return tags.end(); }
		bool ElementDefined(Tag const & tag, ElementType etype);
	private:
		typedef tag_array_type::iterator tag_iterator;
		typedef tag_array_type::const_iterator tag_const_iterator;
		
		tag_array_type tags;
		empty_data empty_dense_data;
		dense_data_array_type dense_data;
		__INLINE dense_sub_type & GetDenseData(int pos) {return dense_data[pos];}
		friend class Storage;
		friend class Tag;
		friend class Mesh;
	};
	
304
305
306
307
308
	/// Base class for Mesh, Element, and ElementSet classes.
	/// This base class is used for the Mesh class, as well as Element classes, and ElementSet class.
	/// 
	/// Storage class is used for representing different data objects in memory.
	/// Each data object is associated with corresponding Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
	class Storage //implemented in storage.cpp
	{
	protected:
		Storage(const Storage & other);           // never use this
		Storage(Mesh * m, INMOST_DATA_ENUM_TYPE lid, const Storage & other); // use this instead
		Storage & assign(Mesh * m, INMOST_DATA_ENUM_TYPE lid, Storage const & other);
		Storage(Mesh *m, ElementType _etype);
		typedef std::pair<Tag, void *> sparse_sub_type;
		typedef array< sparse_sub_type > sparse_data_array_type;
		void CopyData(Tag t, void * adata, void * bdata);
		void DestroyVariableData(Tag t, void * adata);
		void MoveData(int new_local_id);
		void SwapData(int new_local_id);
		INMOST_DATA_ENUM_TYPE etypenum;
		INMOST_DATA_ENUM_TYPE local_id;
		Mesh * m_link;
		MIDType markers;
		sparse_data_array_type inner_data;
		__INLINE void * & GetSparseLink(Tag t) {for(sparse_data_array_type::iterator it = inner_data.begin(); it != inner_data.end(); ++it) if( it->first == t ) return it->second; inner_data.push_back(sparse_sub_type(t,(void *)NULL)); return inner_data.back().second;}
		__INLINE void * GetSparseLink(Tag t) const {for(sparse_data_array_type::const_iterator it = inner_data.begin(); it != inner_data.end(); ++it) if( it->first == t ) return it->second; return NULL;}
		__INLINE void * GetDenseLink(Tag t) const {return &(t.GetTagManager()->GetDenseData(t.GetPositionNum(etypenum))[LocalID()*t.GetRecordSize()]);}
		__INLINE void * GetLink(Tag t) {void * p; if( !t.isSparseNum(etypenum) ) p = GetDenseLink(t); else {void * & q = GetSparseLink(t); if( q == NULL ) q = calloc(1,t.GetRecordSize()); p = q;} return p;}
		__INLINE void * GetLink(Tag t) const {void * p; if( !t.isSparseNum(etypenum) ) p = GetDenseLink(t); else p = GetSparseLink(t); return p;}
	public:
333
		/// Storage type for representing real values.
Kirill Terekhov's avatar
Kirill Terekhov committed
334
		typedef INMOST_DATA_REAL_TYPE real; 
335
		/// Storage type for representing integer values.
Kirill Terekhov's avatar
Kirill Terekhov committed
336
		typedef INMOST_DATA_INTEGER_TYPE integer;
337
		/// Storage type for representing one byte of abstact data.
Kirill Terekhov's avatar
Kirill Terekhov committed
338
		typedef INMOST_DATA_BULK_TYPE bulk;
339
		/// Storage type for representing references to Element.
Kirill Terekhov's avatar
Kirill Terekhov committed
340
		typedef Element * reference;
341
		/// Storage type for representing arrays of real values.
Kirill Terekhov's avatar
Kirill Terekhov committed
342
		typedef shell<real> real_array;
343
		/// Storage type for representing arrays of integer values.
Kirill Terekhov's avatar
Kirill Terekhov committed
344
		typedef shell<integer> integer_array;
345
		/// Storage type for representing abstact data as a series of bytes.
Kirill Terekhov's avatar
Kirill Terekhov committed
346
		typedef shell<bulk> bulk_array;
347
		/// Storage type for representing arrays of Element references.
Kirill Terekhov's avatar
Kirill Terekhov committed
348
349
		typedef shell<Element *> reference_array;
		virtual ~Storage();
350
		/// Retrieve real value associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
351
		__INLINE real      & Real     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REAL     ); void * p = GetLink(tag); if( tag.GetSize() != ENUMUNDEF ) return static_cast<Storage::real     *>(p)[0]; else return static_cast<inner_real_array     *>(p)->at_safe(0);}
352
		/// Retrieve integer value associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
353
		__INLINE integer   & Integer  (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_INTEGER  ); void * p = GetLink(tag); if( tag.GetSize() != ENUMUNDEF ) return static_cast<Storage::integer  *>(p)[0]; else return static_cast<inner_integer_array  *>(p)->at_safe(0);}
354
		/// Retrieve one byte of abstract data associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
355
		__INLINE bulk      & Bulk     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_BULK     ); void * p = GetLink(tag); if( tag.GetSize() != ENUMUNDEF ) return static_cast<Storage::bulk     *>(p)[0]; else return static_cast<inner_bulk_array     *>(p)->at_safe(0);}
356
		/// Retrieve Element reference associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
357
		__INLINE reference & Reference(Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REFERENCE); void * p = GetLink(tag); if( tag.GetSize() != ENUMUNDEF ) return static_cast<Storage::reference*>(p)[0]; else return static_cast<inner_reference_array*>(p)->at_safe(0);}
358
		/// Retrieve array of real values associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
359
		__INLINE real_array      RealArray     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REAL     ); void * p = GetLink(tag); if( tag.GetSize() == ENUMUNDEF ) return Storage::real_array     (*static_cast<inner_real_array     *>(p)); else return Storage::real_array     (static_cast<Storage::real      *>(p),tag.GetSize());}
360
		/// Retrieve array of integer values associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
361
		__INLINE integer_array   IntegerArray  (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_INTEGER  ); void * p = GetLink(tag); if( tag.GetSize() == ENUMUNDEF ) return Storage::integer_array  (*static_cast<inner_integer_array  *>(p)); else return Storage::integer_array  (static_cast<Storage::integer   *>(p),tag.GetSize());}
362
		/// Retrieve abstract data associated with Tag as a series of bytes.
Kirill Terekhov's avatar
Kirill Terekhov committed
363
		__INLINE bulk_array      BulkArray     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_BULK     ); void * p = GetLink(tag); if( tag.GetSize() == ENUMUNDEF ) return Storage::bulk_array     (*static_cast<inner_bulk_array     *>(p)); else return Storage::bulk_array     (static_cast<Storage::bulk      *>(p),tag.GetSize());}
364
		/// Retrieve array of Element references associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
		__INLINE reference_array ReferenceArray(Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REFERENCE); void * p = GetLink(tag); if( tag.GetSize() == ENUMUNDEF ) return Storage::reference_array(*static_cast<inner_reference_array*>(p)); else return Storage::reference_array(static_cast<Storage::reference *>(p),tag.GetSize());}
		
		//optimized data requests for dense data with fixed size
		__INLINE real_array      RealArrayDF     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REAL      && tag.GetSize() != ENUMUNDEF && !tag.isSparse(GetElementType())); return Storage::real_array     (static_cast<Storage::real     *>(GetDenseLink(tag)),tag.GetSize());}
		__INLINE integer_array   IntegerArrayDF  (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_INTEGER   && tag.GetSize() != ENUMUNDEF && !tag.isSparse(GetElementType())); return Storage::integer_array  (static_cast<Storage::integer  *>(GetDenseLink(tag)),tag.GetSize());}
		__INLINE bulk_array      BulkArrayDF     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_BULK      && tag.GetSize() != ENUMUNDEF && !tag.isSparse(GetElementType())); return Storage::bulk_array     (static_cast<Storage::bulk     *>(GetDenseLink(tag)),tag.GetSize());}
		__INLINE reference_array ReferenceArrayDF(Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REFERENCE && tag.GetSize() != ENUMUNDEF && !tag.isSparse(GetElementType())); return Storage::reference_array(static_cast<Storage::reference*>(GetDenseLink(tag)),tag.GetSize());}
		__INLINE real      & RealDF     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REAL      && tag.GetSize() != ENUMUNDEF && !tag.isSparse(GetElementType()));  return static_cast<Storage::real     *>(GetDenseLink(tag))[0];}
		__INLINE integer   & IntegerDF  (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_INTEGER   && tag.GetSize() != ENUMUNDEF && !tag.isSparse(GetElementType()));  return static_cast<Storage::integer  *>(GetDenseLink(tag))[0];}
		__INLINE bulk      & BulkDF     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_BULK      && tag.GetSize() != ENUMUNDEF && !tag.isSparse(GetElementType()));  return static_cast<Storage::bulk     *>(GetDenseLink(tag))[0];}
		__INLINE reference & ReferenceDF(Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REFERENCE && tag.GetSize() != ENUMUNDEF && !tag.isSparse(GetElementType()));  return static_cast<Storage::reference*>(GetDenseLink(tag))[0];}
		
		//optimized data requests for dense data with variable size
		__INLINE real_array      RealArrayDV     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REAL      && tag.GetSize() == ENUMUNDEF && !tag.isSparse(GetElementType())); return Storage::real_array     (*static_cast<inner_real_array     *>(GetDenseLink(tag)));}
		__INLINE integer_array   IntegerArrayDV  (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_INTEGER   && tag.GetSize() == ENUMUNDEF && !tag.isSparse(GetElementType())); return Storage::integer_array  (*static_cast<inner_integer_array  *>(GetDenseLink(tag)));}
		__INLINE bulk_array      BulkArrayDV     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_BULK      && tag.GetSize() == ENUMUNDEF && !tag.isSparse(GetElementType())); return Storage::bulk_array     (*static_cast<inner_bulk_array     *>(GetDenseLink(tag)));}
		__INLINE reference_array ReferenceArrayDV(Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REFERENCE && tag.GetSize() == ENUMUNDEF && !tag.isSparse(GetElementType())); return Storage::reference_array(*static_cast<inner_reference_array*>(GetDenseLink(tag)));}
		__INLINE real      & RealDV     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REAL      && tag.GetSize() == ENUMUNDEF && !tag.isSparse(GetElementType()));  return static_cast<inner_real_array     *>(GetDenseLink(tag))->at_safe(0);}
		__INLINE integer   & IntegerDV  (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_INTEGER   && tag.GetSize() == ENUMUNDEF && !tag.isSparse(GetElementType()));  return static_cast<inner_integer_array  *>(GetDenseLink(tag))->at_safe(0);}
		__INLINE bulk      & BulkDV     (Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_BULK      && tag.GetSize() == ENUMUNDEF && !tag.isSparse(GetElementType()));  return static_cast<inner_bulk_array     *>(GetDenseLink(tag))->at_safe(0);}
		__INLINE reference & ReferenceDV(Tag tag) {assert(tag.isValid() && GetMeshLink() == tag.GetMeshLink() && tag.GetDataType() == DATA_REFERENCE && tag.GetSize() == ENUMUNDEF && !tag.isSparse(GetElementType()));  return static_cast<inner_reference_array*>(GetDenseLink(tag))->at_safe(0);}
		
387
388
389
		/// Return the data length associated with Tag.
		/// For abstract data return the number of bytes, otherwise return the length of associated array. 
		/// @see Storage::SetDataSize
Kirill Terekhov's avatar
Kirill Terekhov committed
390
		size_t GetDataSize(Tag tag) const; //For DATA_BULK return number of bytes, otherwise return the length of array
391
392
393
394
		/// Set the length of  data associated with Tag.
		/// @param tag Identifying Tag.
		/// @param new_size The number of bytes for abstract data, otherwise the length of the array.
		/// @see Storage::GetDataSize
Kirill Terekhov's avatar
Kirill Terekhov committed
395
		void SetDataSize(Tag tag,size_t new_size);
396
397
398
399
400
401
402
403
404
		/// Extract part of the data associated with Tag.
		/// Copy part of the associated array or data to the destination memory.
		/// @param tag Identifying Tag.
		/// @param shift Starting position of the copied data.
		/// For abstact data – number of bytes to skip, otherwise number of values to skip.
		/// @param size Number of elements to copy.
		/// For abstact data – number of bytes to copy, otherwise number of values to copy.
		/// @param data Destination position to copy data to.
		/// @see Storage::SetData
Kirill Terekhov's avatar
Kirill Terekhov committed
405
406
407
		void GetData(Tag tag,size_t shift, size_t size, void * data) const;
		void SetData(Tag tag,size_t shift, size_t size, void * data);
		void DelData(Tag tag);
408
		/// Check if any data is associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
409
410
411
412
413
414
415
416
		bool HaveData(Tag tag) const {if(tag.isSparseNum(etypenum)) { if( GetSparseLink(tag) != NULL ) return true; return false; } else {if( tag.GetPositionNum(etypenum) != ENUMUNDEF ) return true; return false;}}
		__INLINE ElementType GetElementType() const {return 1 << etypenum;}
		__INLINE INMOST_DATA_ENUM_TYPE GetElementNum() const {return etypenum;}
		__INLINE void    SetMarker(MIDType n) 
		{
#if defined(USE_OMP)
#pragma omp atomic
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
417
			//std::cout << "set " << ElementTypeName(GetElementType()) << " " << LocalID() << " " << n << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
418
419
420
421
422
423
424
425
			markers |= n;
		}
		__INLINE bool    GetMarker(MIDType n) const  {return (markers & n) != 0;}
		__INLINE void    RemMarker(MIDType n) 
		{
#if defined(USE_OMP)
#pragma omp atomic
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
426
			//std::cout << "rem " << ElementTypeName(GetElementType()) << " " << LocalID() << " " << n << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
			markers &= ~n;
		}
		__INLINE void    ClearMarkerSpace() {markers = 0;}
		__INLINE MIDType GetMarkerSpace() const {return markers;}
		__INLINE MIDType SetMarkerSpace(MIDType _markers) {return markers | (_markers);}
		__INLINE static size_t  MaxMarker() {return sizeof(MIDType)*8;}
		__INLINE INMOST_DATA_ENUM_TYPE LocalID() const {return local_id;}
		__INLINE Mesh * GetMeshLink() const {return m_link;}
		friend void SwapElement(void * pa, void * pb, void * udata);
		friend class Mesh;
	};

	
	class ElementSet : public Storage //implemented in eset.cpp
	{
	private:
		typedef std::set<Element *,bool(*)(Element *,Element *)> element_set_type;
		bool ordered;
	public:
		class iterator : public element_set_type::iterator
		{
		public:
			iterator() {}
			iterator(const element_set_type::iterator & other ) : element_set_type::iterator(other) { }
			iterator(const iterator & other ) : element_set_type::iterator(other) { }
			iterator & operator =(iterator const & other) { element_set_type::iterator::operator=(static_cast<element_set_type::iterator const &>(other)); return *this; }
			Element & operator *() { return *element_set_type::iterator::operator *(); }
			Element * operator->() { return element_set_type::iterator::operator *(); }
		};
		class reverse_iterator : public element_set_type::reverse_iterator
		{
		public:
			reverse_iterator() {};
			reverse_iterator(const element_set_type::reverse_iterator & other ) : element_set_type::reverse_iterator(other) { }
			reverse_iterator(const reverse_iterator & other ) : element_set_type::reverse_iterator(other) { }
			reverse_iterator & operator =(reverse_iterator const & other) { element_set_type::reverse_iterator::operator=(static_cast<element_set_type::reverse_iterator const &>(other)); return *this; }
			Element & operator *() { return *element_set_type::reverse_iterator::operator *(); }
			Element * operator->() { return element_set_type::reverse_iterator::operator *(); }
		};
		ElementSet(Mesh *m,  bool ordered);
		ElementSet(bool ordered = false);
		ElementSet(const ElementSet & other);
		ElementSet(Mesh *m, INMOST_DATA_ENUM_TYPE lid,const ElementSet & other);
		ElementSet & operator =(ElementSet const & other);
		~ElementSet();
		std::pair< ElementSet::iterator, bool > Insert(const Element * e);
		template<class InputIterator>
		void Insert(InputIterator first, InputIterator last) {isInputForwardIterators<Element *, InputIterator>(); eset.insert(first,last);}
Kirill Terekhov's avatar
Kirill Terekhov committed
475
		void Insert(const ElementSet & e);
Kirill Terekhov's avatar
Kirill Terekhov committed
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
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
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
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
		void Insert(std::vector<Element *> e);
		void Insert(array<Element *> e);
		bool Erase(Element * e);
		void Erase(iterator e);
		void Intersection(ElementSet other);
		void Union(ElementSet other);
		void Difference(ElementSet other);
		iterator find(Element * e);
		iterator begin();
		iterator end();
		reverse_iterator rbegin();
		reverse_iterator rend();
		size_t size() const;
		void clear();
		bool empty() const;
		void SetElementsMarker(MIDType marker);
		void RemElementsMarker(MIDType marker);
		bool isOrdered(){return ordered;}
	private:
		element_set_type eset;
	};
	
	
	template <typename AdjacentType>
	class adjacent
	{
		//typedef typename std::vector<Element *> container_type;
		typedef dynarray<Element *,64> container_type;
		container_type container;
	private:
		adjacent(const container_type & other) : container(other) {}
		container_type & get_container() {return container;};
	public:
		adjacent() {}
		template<class InputIterator>
		adjacent(InputIterator first, InputIterator last) :container(first,last) {isInputForwardIterators<Element *, InputIterator>();}
		adjacent(const adjacent & other ) {container = other.container;}
		~adjacent() {container.clear();}
		adjacent & operator=(adjacent const & other) {container = other.container; return *this;}
		class iterator : public container_type::iterator
		{
		public:
			iterator(const container_type::iterator & other ) : container_type::iterator(other) { }
			iterator(const iterator & other ) : container_type::iterator(other) { }
			iterator & operator =(iterator const & other) { container_type::iterator::operator=(static_cast<container_type::iterator const &>(other)); return *this; }
			AdjacentType & operator *() { return static_cast<AdjacentType &>(*(container_type::iterator::operator *())); }
			AdjacentType * operator->() { return static_cast<AdjacentType *>(container_type::iterator::operator *()); }
		};
		class reverse_iterator : public container_type::reverse_iterator
		{
		public:
			reverse_iterator(const container_type::reverse_iterator & other ) : container_type::reverse_iterator(other) { }
			reverse_iterator(const reverse_iterator & other ) : container_type::reverse_iterator(other) { }
			reverse_iterator & operator =(reverse_iterator const & other) { container_type::reverse_iterator::operator=(static_cast<container_type::reverse_iterator const &>(other)); return *this; }
			AdjacentType & operator *() { return static_cast<AdjacentType &>(*container_type::reverse_iterator::operator *()); }
			AdjacentType * operator->() { return static_cast<AdjacentType *>(container_type::reverse_iterator::operator *()); }
		};
		template<class InputIterator>
		void insert(iterator pos,InputIterator pbeg, InputIterator pend) {container.insert(pos,pbeg,pend);}
		iterator erase(iterator pos) {return container.erase(pos);}
		iterator begin() { return iterator(container.begin()); }
		iterator end() { return iterator(container.end()); }
		reverse_iterator rbegin() { return reverse_iterator(container.rbegin()); }
		reverse_iterator rend() { return reverse_iterator(container.rend()); }
		AdjacentType & operator [] (size_t n) {return static_cast<AdjacentType &>(*(container[n]));}
		AdjacentType & front() { return static_cast<AdjacentType &>(*(container.front())); }
		AdjacentType & back() { return static_cast<AdjacentType &>(*(container.back())); }
		AdjacentType & at(size_t n) { return static_cast<AdjacentType &>(*(container.at(n))); }
		void swap(adjacent<AdjacentType> & other) {container.swap(other.container);}
		void push_back(Element & x) {container.push_back(&x);}
		void push_back(Element * x) {container.push_back(x);}
		bool empty() {return container.empty();}
		void clear() {container.clear();}
		void reserve(size_t n) {container.reserve(n);}
		size_t size() const { return container.size(); }
		AdjacentType ** data() {return reinterpret_cast<AdjacentType **>(&container[0]);}
		void unite(const adjacent<AdjacentType>  & other);
		void substract(const adjacent<AdjacentType>  & other);
		void intersect(const adjacent<AdjacentType>  & other);
	};
	
			
	class Element : public Storage //implemented in element.cpp
	{
	public:
		enum GeometricType {Unset,Vertex,Line,MultiLine,Tri,Quad,Polygon,MultiPolygon,Tet,Hex,Prism,Pyramid,Polyhedron,Set};
		static const char * GeometricTypeName(GeometricType t);
		static unsigned int GetGeometricDimension(GeometricType m_type);
		
		typedef INMOST_DATA_BULK_TYPE Status;
		static const Status Owned  = 1;
		static const Status Shared = 2;
		static const Status Ghost  = 4;
		static const Status Any    = 0;
		static const char * StatusName(Status s);
	public:
		//typedef dynarray<Element *,16> adj_type;
		typedef array<Element *> adj_type;
		typedef adj_type::iterator adj_iterator;
		typedef adj_type::reverse_iterator adj_reverse_iterator;
	protected:
		GeometricType m_type;
		adj_type high_conn, low_conn;
		friend class Mesh;
		friend class Node;
		friend class Edge;
		friend class Face;
		friend class Cell;
		friend int CompareElementsUnique(Element * a,Element * b);
		friend int CompareElementsCentroid(Element * a,Element * b);
	protected:
		Element(Mesh * m, ElementType _etype);
		Element(const Element & other);            // don't use this
		Element(Mesh * m, INMOST_DATA_ENUM_TYPE lid,const Element & other);  // use this instead
		Element & operator =(Element const & other);
	public:
		virtual ~Element();
		INMOST_DATA_ENUM_TYPE nbAdjElements(ElementType _etype) const;
		adjacent<Element> getAdjElements(ElementType _etype) const;  //unordered
		adjacent<Element> BridgeAdjacencies(ElementType Bridge, ElementType Dest, MIDType mask = 0);
		__INLINE GeometricType GetGeometricType() const {return m_type;}
		__INLINE unsigned int GetElementDimension() const {return GetGeometricDimension(m_type);}
		Status GetStatus();
		void SetStatus(Status status);
		virtual adjacent<Node> getNodes(); //unordered
		virtual adjacent<Edge> getEdges(); //unordered
		virtual adjacent<Face> getFaces(); //unordered
		virtual adjacent<Cell> getCells(); //unordered
		Node * getAsNode(); //does dynamic conversation for you, if not a node returns NULL
		Edge * getAsEdge(); //does dynamic conversation for you, if not an edge returns NULL
		Face * getAsFace(); //does dynamic conversation for you, if not a face returns NULL
		Cell * getAsCell(); //does dynamic conversation for you, if not a cell returns NULL
		Storage::integer & GlobalID();
		bool CheckElementConnectivity();
		static bool CheckConnectivity(Mesh * m);
		//implemented in geometry.cpp
		void CastRay(Storage::real * pos, Storage::real * dir, dynarray< std::pair<Element *, Storage::real> , 16 > & hits);
		void ComputeGeometricType();
		void Centroid(Storage::real * cnt);
		void Barycenter(Storage::real * cnt);
		Storage::real Mean(Storage::real (*func)(Storage::real* x,Storage::real t),Storage::real time);
		bool Boundary();
		bool Planarity(); // check that all nodes lay on one plane
		//implemented in modify.cpp
		bool Hide(); // if true then element was hidden, works only inside BeginModification and EndModification, on EndModification all Hidden elements are deleted
		bool Show(); // if true then element was recovered
		bool Delete(); // if true then element was deleted, otherwise it was hidden
		bool Hidden();
		bool New();
		void Disconnect(bool delete_upper_adjacent);
		//void Connect(std::vector<Element *> adjacent); //connect faces to cell, edges to face, nodes to edge and recompute geometric data, not implemented
	};
	
	class Node : public Element //implemented in node.cpp
	{
	private:
		Node(Mesh * m);
		Node(const Node & other);
		Node(Mesh * m, INMOST_DATA_ENUM_TYPE lid, const Node & other);
		Node & operator =(Node const & other);
	public:
		~Node();
		
		adjacent<Edge> getEdges(); //unordered
		adjacent<Face> getFaces(); //unordered
		adjacent<Cell> getCells(); //unordered
		Storage::real_array Coords(); 
		friend class Mesh;
	};
	
	class Edge : public Element //implemented in edge.cpp
	{
	private:
		Edge(Mesh * m);
		Edge(const Edge & other);
		Edge(Mesh * m, INMOST_DATA_ENUM_TYPE lid, const Edge & other);
		Edge & operator =(Edge const & other);
	public:
		~Edge();
		
		adjacent<Node> getNodes(); //ordered
		adjacent<Face> getFaces(); //unordered
		adjacent<Cell> getCells(); //unordered
		//implemented in modify.cpp
		static Edge * UniteEdges(Edge ** edges, INMOST_DATA_ENUM_TYPE nedges, MIDType del_protect);
		static bool TestUniteEdges(Edge ** edges, INMOST_DATA_ENUM_TYPE nedges, MIDType del_protect);
		static dynarray<Edge *,32> SplitEdge(Edge * e, Node ** nodes, INMOST_DATA_ENUM_TYPE nnodes, MIDType del_protect); //provide ordered array of nodes, that lay between former nodes of the edge
		static bool TestSplitEdge(Edge * e, Node ** nodes, INMOST_DATA_ENUM_TYPE nnodes, MIDType del_protect);
		//implemented in geometry.cpp
		Storage::real Length();
		friend class Mesh;
	};
	
	class Face : public Element //implemented in face.cpp
	{
	private:
		Face(Mesh * m);
		Face(const Face & other);
		Face(Mesh * m, INMOST_DATA_ENUM_TYPE lid, const Face & other);
		Face & operator =(Face const & other);
	public:
		~Face();
		
		adjacent<Node> getNodes(); //ordered
		adjacent<Edge> getEdges(); //ordered
		adjacent<Cell> getCells(); //unordered
		Cell * BackCell();
		Cell * FrontCell();
		bool FaceOrientedOutside(Cell * c);
		void ReorderEdges();
		
		bool CheckEdgeOrder(); //not implemented// returns true if edges of face form an ordered closed loop
		bool FixEdgeOrder(); //not implemented// returns true if edges were successfully reordered to form a closed loop
		//implemented in modify.cpp
		static Face * UniteFaces(Face ** faces, INMOST_DATA_ENUM_TYPE nfaces, MIDType del_protect);
		static bool TestUniteFaces(Face ** faces, INMOST_DATA_ENUM_TYPE nfaces,  MIDType del_protect);
		static dynarray<Face *,32> SplitFace(Face * face, Edge ** edges, INMOST_DATA_ENUM_TYPE nedges, MIDType del_protect); //provide all edges that lay inside face
		static bool TestSplitFace(Face * face, Edge ** edges, INMOST_DATA_ENUM_TYPE nedges, MIDType del_protect);	
		void SwapCells(); //swap back cell and front cell
		//implemented in geometry.cpp
		Storage::real Area();
		void Normal(Storage::real * nrm);
		void UnitNormal(Storage::real * nrm);
		void OrientedNormal(Cell * c, Storage::real * nrm);
		void OrientedUnitNormal(Cell * c, Storage::real * nrm);
		bool FixNormalOrientation();  //returns true if orientation was corrected, otherwise returns false
		bool CheckNormalOrientation(); //returns true if orientation is correct, otherwise returns false
		bool Closure(); // test integrity of polygon
		friend class Mesh;
	};
	
	class Cell : public Element //implemented in cell.cpp
	{
	private:
		Cell(Mesh * m);
		Cell(const Cell & other);
		Cell(Mesh * m, INMOST_DATA_ENUM_TYPE lid, const Cell & other);
		Cell & operator =(Cell const & other);
	public:
		~Cell();
		
		adjacent<Node> getNodes(); //ordered (for known geometric types only)
		adjacent<Edge> getEdges(); //unordered
		adjacent<Face> getFaces(); //ordered (keeps order it was created in)
		
		
		bool CheckEdgeOrder(); //not implemented//2D only, returns true if edges of face form an ordered closed loop
		bool FixEdgeOrder(); //not implemented//2D only, returns true if edges were successfully reordered to form a closed loop
		//implemented in modify.cpp
		static Cell * UniteCells(Cell ** cells, INMOST_DATA_ENUM_TYPE ncells, MIDType del_protect);
		static bool TestUniteCells(Cell ** cells, INMOST_DATA_ENUM_TYPE ncells, MIDType del_protect);
		static dynarray<Cell *,32> SplitCell(Cell * cell, Face ** faces, INMOST_DATA_ENUM_TYPE nfaces, MIDType del_protect); //provide all faces, that lay inside cell
		static bool TestSplitCell(Cell * cell, Face ** faces, INMOST_DATA_ENUM_TYPE nfaces, MIDType del_protect);
		//implemented in geometry.cpp
		Cell * Neighbour(Face * f);
		adjacent<Cell> NeighbouringCells(); // get all cells that share any face with current
		bool Inside(Storage::real * point); //is point inside cell, check for 2d case
		Storage::real Volume();
		bool Closure(); // test integrity of cell
		friend class Mesh;
	};
	
	class Mesh : public TagManager, public Storage //implemented in mesh.cpp
	{
	public:
		enum MeshState {Serial, Parallel};
		typedef chunk_array<Node *> 		nodes_container;
		typedef chunk_array<Edge *> 		edges_container;
		typedef chunk_array<Face *> 		faces_container;
		typedef chunk_array<Cell *> 		cells_container;
		typedef chunk_array<ElementSet *> 	sets_container;
		typedef chunk_array<INMOST_DATA_ENUM_TYPE> 	empty_container;
	private:
		Storage::real epsilon;
		cells_container cells;
		empty_container empty_cells;
		faces_container faces;
		empty_container empty_faces;
		edges_container edges;
		empty_container empty_edges;
		nodes_container nodes;
		empty_container empty_nodes;
		sets_container  sets;
		empty_container empty_sets;
		Tag tag_global_id, tag_coords;
		MeshState m_state;
		unsigned int dim;
		Element * last_created_element;
	public:
		Mesh();
		Mesh(const Mesh & other);
		~Mesh();
		Mesh & operator =(Mesh const & other);
		MIDType CreateMarker();
		void ReleaseMarker(MIDType n);
		__INLINE void SetEpsilon(Storage::real e) {epsilon = e;}
		__INLINE Storage::real GetEpsilon() const {return epsilon;}
		void SetDimensions(unsigned int dim);
		__INLINE unsigned int GetDimensions() const {return dim;}
		__INLINE MeshState GetMeshState() {return m_state;}
		__INLINE const Tag GlobalIDTag() const {return tag_global_id;}
		__INLINE const Tag CoordsTag() const {return tag_coords;}
		Tag CreateTag(std::string name, DataType dtype, ElementType etype,ElementType sparse, INMOST_DATA_ENUM_TYPE size = ENUMUNDEF);
		Tag DeleteTag(Tag tag, ElementType mask = NODE | EDGE | FACE | CELL | ESET | MESH);
		Node * CreateNode(Storage::real * coords);
		std::pair<Edge *,bool> CreateEdge(Node ** e_nodes, INMOST_DATA_ENUM_TYPE num_e_nodes);
		std::pair<Face *,bool> CreateFace(Edge ** f_edges, INMOST_DATA_ENUM_TYPE num_f_edges);
		std::pair<Face *,bool> CreateFace(Node ** f_nodes, INMOST_DATA_ENUM_TYPE num_f_nodes);
		std::pair<Cell *,bool> CreateCell(Face ** c_faces, INMOST_DATA_ENUM_TYPE num_c_faces, Node ** suggest_nodes_order = NULL, INMOST_DATA_ENUM_TYPE numsuggest_nodes_order = 0);
		std::pair<Cell *,bool> CreateCell(Node ** c_f_nodes, const INMOST_DATA_ENUM_TYPE * c_f_numnodes, INMOST_DATA_ENUM_TYPE num_c_faces, Node ** suggest_nodes_order = NULL, INMOST_DATA_ENUM_TYPE numsuggest_nodes_order = 0);
		std::pair<Cell *, bool> CreateCell(Node ** c_nodes, const INMOST_DATA_ENUM_TYPE * c_f_nodeinds, const INMOST_DATA_ENUM_TYPE * c_f_numnodes, INMOST_DATA_ENUM_TYPE num_c_faces, Node ** suggest_nodes_order = NULL, INMOST_DATA_ENUM_TYPE numsuggest_nodes_order = 0);
		Element * ElementByLocalID(ElementType etype, INMOST_DATA_INTEGER_TYPE lid)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
789
			assert(OneType(etype));
Kirill Terekhov's avatar
Kirill Terekhov committed
790
791
792
793
794
795
796
797
798
799
800
801
802
803
			switch(etype)
			{
			case NODE: return nodes[lid];
			case EDGE: return edges[lid];
			case FACE: return faces[lid];
			case CELL: return cells[lid];
			}
			return NULL;
		}
		Node * NodeByLocalID(INMOST_DATA_INTEGER_TYPE lid) { return nodes[lid]; }
		Edge * EdgeByLocalID(INMOST_DATA_INTEGER_TYPE lid) { return edges[lid]; }
		Face * FaceByLocalID(INMOST_DATA_INTEGER_TYPE lid) {return faces[lid];}
		Cell * CellByLocalID(INMOST_DATA_INTEGER_TYPE lid) { return cells[lid]; }
		ElementSet * EsetByLocalID(INMOST_DATA_INTEGER_TYPE lid) { return sets[lid]; }
Kirill Terekhov's avatar
Kirill Terekhov committed
804
805
806
807
808
809
810

		INMOST_DATA_INTEGER_TYPE MaxLocalIDNODE() const {return static_cast<INMOST_DATA_INTEGER_TYPE>(nodes.size());}
		INMOST_DATA_INTEGER_TYPE MaxLocalIDEDGE() const {return static_cast<INMOST_DATA_INTEGER_TYPE>(edges.size());}
		INMOST_DATA_INTEGER_TYPE MaxLocalIDFACE() const {return static_cast<INMOST_DATA_INTEGER_TYPE>(faces.size());}
		INMOST_DATA_INTEGER_TYPE MaxLocalIDCELL() const {return static_cast<INMOST_DATA_INTEGER_TYPE>(cells.size());}
		INMOST_DATA_INTEGER_TYPE MaxLocalIDESET() const {return static_cast<INMOST_DATA_INTEGER_TYPE>(sets.size());}
		INMOST_DATA_INTEGER_TYPE MaxLocalID(ElementType etype) const
Kirill Terekhov's avatar
Kirill Terekhov committed
811
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
812
			assert(OneType(etype));
Kirill Terekhov's avatar
Kirill Terekhov committed
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
859
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
885
886
887
888
889
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
948
949
950
951
952
953
			switch(etype)
			{
			case NODE: return static_cast<INMOST_DATA_INTEGER_TYPE>(nodes.size());
			case EDGE: return static_cast<INMOST_DATA_INTEGER_TYPE>(edges.size());
			case FACE: return static_cast<INMOST_DATA_INTEGER_TYPE>(faces.size());
			case CELL: return static_cast<INMOST_DATA_INTEGER_TYPE>(cells.size());
			}
			return 0;
		}
		ElementSet * CreateSet();
		ElementSet * CreateOrderedSet();
		Element * FindSharedAdjacency(Element ** arr, unsigned num);
		void ReorderEmpty(ElementType reordertypes);
		void ReorderApply(Tag index, ElementType mask);
		bool isOriginal(Element * e); //don't know now what this function was for, should detect the copy-constructed element, but copy-construction is prohibited
		size_t GetArrayCapacity(ElementType etype); //This function is needed by TagManager, may be made private in future
		void RestoreCellNodes(Cell * c, dynarray<Node *,64> & ret);
	private:
		void MoveStorage(Storage * e, int new_local_id);
		void UntieElement(Storage * e);
		void TieElement(Storage * e);
		//implemented in mesh_parallel.cpp
	public:
		enum Action {AGhost, AMigrate};
		enum Prepare {UnknownSize, UnknownSource};
		typedef std::pair<int, std::vector<INMOST_DATA_BULK_TYPE> > proc_buffer_type;
		typedef std::vector< proc_buffer_type > exch_buffer_type;
		class exchange_data
		{
		public:
			std::vector<INMOST_MPI_Request> send_reqs, recv_reqs;
			exch_buffer_type send_buffers, recv_buffers;
		};
	private:
		ElementType have_global_id;
		INMOST_DATA_BIG_ENUM_TYPE parallel_mesh_unique_id;
		INMOST_MPI_Comm comm;
		Tag tag_shared, tag_owner, tag_processors, tag_layers, tag_sendto, tag_bridge, tag_redistribute;
		void ComputeSharedProcs();
		std::map< int , std::vector<Element *> > ComputeSharedSkinSet(ElementType bridge);
		void PackTagData(Tag tag, std::vector< std::vector<Element *> > & elements, ElementType mask, std::vector<INMOST_DATA_BULK_TYPE> & buffer);
		void UnpackTagData(Tag tag, std::vector< std::vector<Element *> > & elements, ElementType mask, std::vector<INMOST_DATA_BULK_TYPE> & buffer, int & position, void (*Operation)(Tag tag, Element * element,INMOST_DATA_BULK_TYPE * data, INMOST_DATA_ENUM_TYPE size));
		void PackElementsData(std::vector<Element *> & input, std::vector<INMOST_DATA_BULK_TYPE> & buffer, int destination, std::vector<std::string> tag_list);
		void UnpackElementsData(std::vector<Element *> & output, std::vector<INMOST_DATA_BULK_TYPE> & buffer, int source, std::vector<std::string> & tag_list);
		class elements_by_type
		{
		private:
			std::vector< std::vector<Element *> > container;
		public:
			elements_by_type() :container(4) {}
			elements_by_type(const elements_by_type & other){container = other.container;}
			~elements_by_type(){}
			std::vector<Element *> & operator [](int i){ return container[i]; }
			std::vector<std::vector<Element *> >::iterator begin(){ return container.begin(); }
			std::vector<std::vector<Element *> >::iterator end() { return container.end(); }
			std::vector<std::vector<Element *> > & get_container() {return container;}
		};
		typedef std::map<int, elements_by_type > parallel_storage;
#if defined(USE_PARALLEL_STORAGE)
		parallel_storage shared_elements, ghost_elements;
#endif
#if defined(USE_PARALLEL_WRITE_TIME)
		int num_exchanges;
		std::fstream out_time;
		int tab;
		int func_id;
#endif
#if defined(USE_MPI2)
		INMOST_MPI_Win window;
		unsigned * shared_space;
#endif
		int parallel_strategy, parallel_file_strategy;
		void PrepareReceiveInner(Prepare todo, exch_buffer_type & send_bufs, exch_buffer_type & recv_bufs);
		void ExchangeDataInnerBegin(std::vector<Tag> tag, parallel_storage & from, parallel_storage & to, ElementType mask, exchange_data & storage);
		void ExchangeDataInnerEnd(std::vector<Tag> tag, parallel_storage & from, parallel_storage & to, ElementType mask, void (*Operation)(Tag tag,Element * element,INMOST_DATA_BULK_TYPE * recv_data,INMOST_DATA_ENUM_TYPE recv_size), exchange_data & storage);
		void ExchangeBuffersInner(exch_buffer_type & send_bufs, exch_buffer_type & recv_bufs,std::vector<INMOST_MPI_Request> & send_reqs, std::vector<INMOST_MPI_Request> & recv_reqs);
		std::vector<int> FinishRequests(std::vector<INMOST_MPI_Request> & recv_reqs);
		void GatherParallelStorage(parallel_storage & ghost, parallel_storage & shared, ElementType mask);
		class Random // random generator to provide tag for communication
		{
		private: unsigned int n,a,c,m;
		public:
			Random(unsigned int seed = 50);
			Random(const Random & other);
			unsigned int Number();
		} randomizer;
	public:
#if defined(USE_PARALLEL_WRITE_TIME)	
		//this part is needed to test parallel performance
		void Enter();
		void Exit();
		int & GetFuncID() {return func_id;}
		std::fstream & GetStream();
		std::fstream & WriteTab(std::fstream & f);
#endif
		static void Initialize(int * argc, char *** argv);
		static void Finalize();
		void SetParallelStrategy(int strategy){if( strategy < 0 || strategy > 3 ) throw NotImplemented; parallel_strategy = strategy;}
		int GetParallelStrategy() {return parallel_strategy;}
		void SetParallelFileStrategy(int strategy){if( strategy < 0 || strategy > 1 ) throw NotImplemented; parallel_file_strategy = strategy;}
		int GetParallelFileStrategy() {return parallel_file_strategy;}
		int GetProcessorRank();
		int GetProcessorsNumber();
		INMOST_MPI_Comm GetCommunicator();
		void SetCommunicator(INMOST_MPI_Comm _comm);
		void ResolveShared();
		void RemoveGhost();
		void RemoveGhostElements(std::vector<Element *> ghost);
		void AssignGlobalID(ElementType mask);
		//exchange for tag of type DATA_REFERENCE is not implemented!
		void ExchangeData(Tag tag, ElementType mask);
		void ExchangeDataBegin(Tag tags, ElementType mask, exchange_data & storage);
		void ExchangeDataEnd(Tag tags, ElementType mask, exchange_data & storage);
		void ExchangeData(std::vector<Tag> tags, ElementType mask);
		void ExchangeDataBegin(std::vector<Tag> tags, ElementType mask, exchange_data & storage);
		void ExchangeDataEnd(std::vector<Tag> tags, ElementType mask, exchange_data & storage);
		void ReduceData(Tag tag, ElementType mask, void (*Operation)(Tag tag, Element * element, INMOST_DATA_BULK_TYPE * recv_data, INMOST_DATA_ENUM_TYPE recv_size) );
		void ReduceDataBegin(Tag tag, ElementType mask, exchange_data & storage);
		void ReduceDataEnd(Tag tag, ElementType mask, void (*Operation)(Tag tag, Element * element, INMOST_DATA_BULK_TYPE * recv_data, INMOST_DATA_ENUM_TYPE recv_size), exchange_data & storage );
		void ReduceData(std::vector<Tag> tags, ElementType mask, void (*Operation)(Tag tag, Element * element, INMOST_DATA_BULK_TYPE * recv_data, INMOST_DATA_ENUM_TYPE recv_size) );
		void ReduceDataBegin(std::vector<Tag> tags, ElementType mask, exchange_data & storage);
		void ReduceDataEnd(std::vector<Tag> tags, ElementType mask, void (*Operation)(Tag tag, Element * element, INMOST_DATA_BULK_TYPE * recv_data, INMOST_DATA_ENUM_TYPE recv_size), exchange_data & storage );
		void ExchangeMarked(enum Action action = AGhost);
		void ExchangeGhost(Storage::integer layers, ElementType bridge);
		void Redistribute();
		Storage::integer Enumerate(ElementType mask, Tag num_tag, Storage::integer start = 0);
		Storage::integer Enumerate(std::vector<Element *> elements, Tag num_tag, Storage::integer start = 0);
		Storage::integer EnumerateSet(ElementSet * set, Tag num_tag, Storage::integer start = 0);
		Storage::integer TotalNumberOf(ElementType mask);
		Storage::real Integrate(Storage::real input);
		Storage::integer Integrate(Storage::integer input);
		Storage::integer ExclusiveSum(Storage::integer input); 
		Storage::real Integrate(Tag t,ElementType mask);
		void RecomputeParallelStorage(ElementType mask);
		__INLINE const Tag SendtoTag() const {return tag_sendto;}
		__INLINE const Tag SharedTag() const {return tag_shared;}
		__INLINE const Tag OwnerTag() const {return tag_owner;}
		__INLINE const Tag LayersTag() const {return tag_layers;}
		__INLINE const Tag ProcessorsTag() const {return tag_processors;}
		__INLINE Tag RedistributeTag() {return CreateTag("TEMPORARY_NEW_OWNER",DATA_INTEGER,CELL,NONE,1);}
		
Kirill Terekhov's avatar
Kirill Terekhov committed
954
955
		ElementType SynchronizeElementType(ElementType etype);
		void SynchronizeMarker(MIDType marker, ElementType mask, SyncBitOp op);
Kirill Terekhov's avatar
Kirill Terekhov committed
956
957
958
959
960
961
962
963
964
965
966
967
968
969
		
		//for debug
		void                 BeginSequentialCode();
		void                   EndSequentialCode();
		//iterator.cpp::::::::::::::::::::::::::::::::::::::::::::::::::
	public:
		class base_iterator;
		class iteratorElement;
		class iteratorSet;
		class iteratorCell;
		class iteratorFace;
		class iteratorEdge;
		class iteratorNode;
		
Kirill Terekhov's avatar
Kirill Terekhov committed
970
971
972
973
974
		__INLINE INMOST_DATA_ENUM_TYPE NumberOfCells()   {return static_cast<INMOST_DATA_ENUM_TYPE>(cells.size() - empty_cells.size());}
		__INLINE INMOST_DATA_ENUM_TYPE NumberOfFaces()   { return static_cast<INMOST_DATA_ENUM_TYPE>(faces.size() - empty_faces.size()); }
		__INLINE INMOST_DATA_ENUM_TYPE NumberOfEdges()   { return static_cast<INMOST_DATA_ENUM_TYPE>(edges.size() - empty_edges.size()); }
		__INLINE INMOST_DATA_ENUM_TYPE NumberOfNodes()   { return static_cast<INMOST_DATA_ENUM_TYPE>(nodes.size() - empty_nodes.size()); }
		__INLINE INMOST_DATA_ENUM_TYPE NumberOfSets()    { return static_cast<INMOST_DATA_ENUM_TYPE>(sets.size() - empty_sets.size()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
975
976
		__INLINE INMOST_DATA_ENUM_TYPE NumberOfElements(){ return NumberOfCells() + NumberOfFaces() + NumberOfEdges() + NumberOfNodes(); }
		__INLINE INMOST_DATA_ENUM_TYPE NumberOfAll()     { return NumberOfSets() + NumberOfElements(); }
Kirill Terekhov's avatar
Kirill Terekhov committed
977
		INMOST_DATA_ENUM_TYPE NumberOf(ElementType t);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
1036
1037
1038
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
1094
1095
1096
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
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
		base_iterator Begin(ElementType Types);
		base_iterator End();
		iteratorElement BeginElement(ElementType Types);
		iteratorElement EndElement();
		iteratorSet  BeginSet();
		iteratorSet  EndSet();
		iteratorCell BeginCell();
		iteratorCell EndCell();
		iteratorFace BeginFace();
		iteratorFace EndFace();
		iteratorEdge BeginEdge();
		iteratorEdge EndEdge();
		iteratorNode BeginNode();
		iteratorNode EndNode();
		class base_iterator
		{
		protected:
			typedef int itenum;
			Mesh * m;
			itenum Number;
			ElementType CurrentType;
			ElementType types;
			base_iterator(ElementType Types, Mesh * mesh, bool last);
			base_iterator(Mesh * mesh) {m = mesh; CurrentType = NONE; types = NONE; Number = -1;}
		public:
			typedef Storage * pointer;
			typedef Storage & reference;
			typedef Storage value_type;
			typedef ptrdiff_t difference_type;
			typedef std::bidirectional_iterator_tag iterator_category;
			base_iterator() {Number = -1; CurrentType = NONE; m = NULL; types = NONE;}
			base_iterator(const base_iterator & other) {m = other.m; Number = other.Number;	types = other.types; CurrentType = other.CurrentType;}
			virtual ~base_iterator() {}
			base_iterator & operator ++();
			__INLINE base_iterator operator ++(int) {Mesh::base_iterator ret(*this); operator++(); return ret;}
			base_iterator & operator --();
			__INLINE base_iterator operator --(int) {Mesh::base_iterator ret(*this); operator--(); return ret;}
			__INLINE virtual reference operator *()
			{
				switch(CurrentType)
				{
					case NODE: return static_cast<Storage &>(*m->nodes[Number]); break;
					case EDGE: return static_cast<Storage &>(*m->edges[Number]); break;
					case FACE: return static_cast<Storage &>(*m->faces[Number]); break;
					case CELL: return static_cast<Storage &>(*m->cells[Number]); break;
					case ESET: return static_cast<Storage &>(*m->sets[Number]); break;
				}
				return *m;
			}
			__INLINE virtual pointer operator ->()
			{
				switch(CurrentType)
				{
					case NODE: return static_cast<Storage *>(m->nodes[Number]); break;
					case EDGE: return static_cast<Storage *>(m->edges[Number]); break;
					case FACE: return static_cast<Storage *>(m->faces[Number]); break;
					case CELL: return static_cast<Storage *>(m->cells[Number]); break;
					case ESET: return static_cast<Storage *>(m->sets[Number]); break;
				}
				return NULL;
			}
			__INLINE base_iterator & operator =(base_iterator const & other) {m = other.m; Number = other.Number; types = other.types; CurrentType = other.CurrentType; return *this;}
			__INLINE bool operator ==(const base_iterator & other) { assert( m == other.m ); if( Number == other.Number && CurrentType == other.CurrentType ) return true; return false;}
			__INLINE bool operator !=(const base_iterator & other) { assert( m == other.m ); if( Number != other.Number || CurrentType != other.CurrentType ) return true; return false;}
			__INLINE bool operator <(const base_iterator & other) { assert( m == other.m ); if( (CurrentType < other.CurrentType) || (CurrentType == other.CurrentType && Number < other.Number) ) return true; return false;}
			__INLINE bool operator >(const base_iterator & other) { assert( m == other.m ); if( (CurrentType > other.CurrentType) || (CurrentType == other.CurrentType && Number > other.Number) ) return true; return false;}
			__INLINE bool operator <=(const base_iterator & other) {assert( m != other.m ); if( (CurrentType < other.CurrentType) || (CurrentType == other.CurrentType && Number <= other.Number) ) return true; return false;}
			__INLINE bool operator >=(const base_iterator & other) {assert( m == other.m ); if( (CurrentType > other.CurrentType) || (CurrentType == other.CurrentType && Number >= other.Number) ) return true; return false;}
			void Print();
			friend base_iterator Mesh::Begin(ElementType Types);
			friend base_iterator Mesh::End();
		};
		class iteratorElement : public base_iterator
		{
		private:
			iteratorElement(const base_iterator & other) :base_iterator(other) {}
		public:
			__INLINE Element & operator  *() {return static_cast<Element &>(Mesh::base_iterator::operator *());}
			__INLINE Element * operator ->() {return static_cast<Element *>(Mesh::base_iterator::operator ->());}
			friend iteratorElement Mesh::BeginElement(ElementType Types);
			friend iteratorElement Mesh::EndElement();
		};
		class iteratorSet : public base_iterator
		{
		private:
			iteratorSet(const base_iterator & other) :base_iterator(other) {}
		public:
			__INLINE ElementSet & operator  *() {return *m->sets[Number];}
			__INLINE ElementSet * operator ->() {return m->sets[Number];}
			friend iteratorSet Mesh::BeginSet();
			friend iteratorSet Mesh::EndSet();
		};
		class iteratorCell : public base_iterator
		{
		private:
			iteratorCell(const base_iterator & other) :base_iterator(other) {}
		public:
			__INLINE Cell & operator  *() {return *m->cells[Number];}
			__INLINE Cell * operator ->() {return  m->cells[Number];}
			friend iteratorCell Mesh::BeginCell();
			friend iteratorCell Mesh::EndCell();
		};
		class iteratorFace : public base_iterator
		{
		private:
			iteratorFace(const base_iterator & other) :base_iterator(other) {}
		public:
			__INLINE Face & operator  *() {return *m->faces[Number];}
			__INLINE Face * operator ->() {return  m->faces[Number];}
			friend iteratorFace Mesh::BeginFace();
			friend iteratorFace Mesh::EndFace();
		};
		class iteratorEdge : public base_iterator
		{
		private:
			iteratorEdge(const base_iterator & other) :base_iterator(other) {}
		public:
			__INLINE Edge & operator  *() {return *m->edges[Number];} 
			__INLINE Edge * operator ->() {return  m->edges[Number];}
			friend iteratorEdge Mesh::BeginEdge();
			friend iteratorEdge Mesh::EndEdge();
		};
		class iteratorNode : public base_iterator
		{
		private:
			iteratorNode(const base_iterator & other) :base_iterator(other) {}
		public:
			__INLINE Node & operator  *() {return *m->nodes[Number];}
			__INLINE Node * operator ->() {return  m->nodes[Number];}
			friend iteratorNode Mesh::BeginNode();
			friend iteratorNode Mesh::EndNode();
		};
	private:
		int GetTypeEnd(ElementType t);
	private:
		//implemented in io.hpp
		io_converter<INMOST_DATA_INTEGER_TYPE,INMOST_DATA_REAL_TYPE> iconv;
		io_converter<INMOST_DATA_ENUM_TYPE   ,INMOST_DATA_REAL_TYPE> uconv;
	private:
		//implemented in mesh_file.cpp
		void WriteTag  (std::ostream & output, Tag X);
		Tag  ReadTag   (std::istream & input);
		void ReadData  (std::istream & input, Tag t, Storage * e,std::vector<Node *> & new_nodes,std::vector<Edge *> & new_edges,std::vector<Face *> & new_faces,std::vector<Cell *> & new_cells);
		void WriteData (std::ostream & output, Tag t, Storage & e);
		void WriteElementSet(std::ostream & output, ElementSet * X);
		ElementSet * ReadElementSet(std::istream & input,std::vector<Node *> & new_nodes,std::vector<Edge *> & new_edges,std::vector<Face *> & new_faces,std::vector<Cell *> & new_cells);
		Node * ReadNode(std::istream & input, std::vector<Node *> & old_nodes);
		void WriteNode(std::ostream & output,Node * X);
		Edge * ReadEdge(std::istream & input, std::vector<Node *> & nodes);
		void WriteEdge(std::ostream & output,Edge * X);
		Face * ReadFace(std::istream & input, std::vector<Edge *> & edges);
		void WriteFace(std::ostream & output,Face * X);
		Cell * ReadCell(std::istream & input,std::vector<Face *> & faces, std::vector<Node *> & nodes);
		void WriteCell(std::ostream & output,Cell * X);
	public:
		void Load(std::string File); // .vtk, .pvtk, .pmf
		void Save(std::string File); // .vtk, .pvtk, .gmv, .pmf
		bool isParallelFileFormat(std::string File);
	public:
		
		//implemented in geometry.cpp
	private:
		void RestoreGeometricTags();
		Tag measure_tag, centroid_tag, normal_tag, barycenter_tag, dot_tag;
		bool remember[5][3];
		bool HideGeometricData(GeometricData type, ElementType mask) {return remember[type][ElementNum(mask)-1] = false;}
		bool ShowGeometricData(GeometricData type, ElementType mask) {return remember[type][ElementNum(mask)-1] = true;}
		//~ MIDType reorient;
	public:
		typedef std::map<GeometricData, ElementType> GeomParam;
		// types for MEASURE:     EDGE | FACE | CELL   (length, area, volume)
		// types for CENTROID:    EDGE | FACE | CELL
		// types for BARYCENTER:  EDGE | FACE | CELL
		// types for NORMAL:      FACE | CELL          (may precompute normal for cells in 2d case)
		// types for ORIENTATION: FACE
		void PrepareGeometricData(GeomParam table);
		void RemoveGeometricData(GeomParam table);
		bool HaveGeometricData(GeometricData type, ElementType mask) const {return remember[type][ElementNum(mask)-1];} // requests to only one geometric and element type allowed
		void GetGeometricData(Element * e, GeometricData type, Storage::real * ret);
		void RecomputeGeometricData(Element * e); // Update all stored geometric data, runs automatically on element creation
		Tag GetGeometricTag(GeometricData type) {switch(type) {case MEASURE: return measure_tag; case CENTROID: return centroid_tag; case BARYCENTER: return barycenter_tag; case NORMAL: return normal_tag;} return Tag();}
		
		
		bool TestClosure(Element ** elements, unsigned num);

		std::vector<Face *> GatherBoundaryFaces();
		std::vector<Face *> GatherInteriorFaces();
		Storage::integer CountBoundaryFaces();
		Storage::integer CountInteriorFaces();
		Element::GeometricType ComputeGeometricType(ElementType element_type, Element ** lower_adjacent, INMOST_DATA_ENUM_TYPE lower_adjacent_size);
		//implemented in modify.cpp
	private:
		MIDType hide_element, new_element;
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
1172
		bool isMeshModified() {return new_element != 0;} //In case mesh is modified, on element creation TieElements will always place elements to the end
Kirill Terekhov's avatar
Kirill Terekhov committed
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
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
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
		MIDType HideMarker() {return hide_element;}
		MIDType NewMarker() {return new_element;}
		void SwapModification(); // swap hidden and new elements, so that old mesh is recovered
		void BeginModification();  //allow elements to be hidden
		void ApplyModification();  //modify DATA_REFERENCE tags so that links to hidden elements are converted to NULL and removed from sets
		void ResolveModification(); //resolve parallel state of newly created elements, restore ghost layers; not implemented
		void EndModification();    //delete hidden elements
		
		
		//Elements and data is only moved, copy implementation from ReorderEmpty
		//~ void BeginMoveModification();
		//~ void EndMoveModification();
		
		static INMOST_DATA_ENUM_TYPE getNext(Element ** arr, INMOST_DATA_ENUM_TYPE size, INMOST_DATA_ENUM_TYPE k, MIDType marker);
		static INMOST_DATA_ENUM_TYPE Count(Element ** arr, INMOST_DATA_ENUM_TYPE size, MIDType marker);
		//implemented in mesh.cpp
	private:
		Tag tag_topologyerror;
		TopologyCheck checkset;
		TopologyCheck errorset;
		TopologyCheck BeginTopologyCheck(ElementType etype, Element ** adj, INMOST_DATA_ENUM_TYPE num); //check provided elements
		TopologyCheck EndTopologyCheck(Element * e); //check created element
	public:
		Tag TopologyErrorTag() const {return tag_topologyerror;}
		TopologyCheck GetTopologyCheck(TopologyCheck mask = ENUMUNDEF) const {return checkset & mask;}
		void SetTopologyCheck(TopologyCheck mask) {checkset = checkset | mask;}
		void RemTopologyCheck(TopologyCheck mask) {checkset = checkset & ~mask;}

		void SetTopologyError(TopologyCheck mask) {errorset = errorset | mask;}
		TopologyCheck GetTopologyError(TopologyCheck mask = ENUMUNDEF) const {return errorset & mask;}
		void ClearTopologyError(TopologyCheck mask = ENUMUNDEF) {errorset = errorset & ~mask;}

		friend class Storage;
	};
	
	template <typename AdjacentType>
	void adjacent<AdjacentType>::unite(const adjacent<AdjacentType> & other)
	{
		if( empty() ) insert(container.end(),other.container.begin(),other.container.end());
		else
		{
			size_t s = size();
			MIDType mrk = container[0]->GetMeshLink()->CreateMarker();
			for(size_t it = 0; it < size(); it++) container[it]->SetMarker(mrk);
			for(size_t it = 0; it < other.size(); it++) if( !other.container[it]->GetMarker(mrk) ) container.push_back(other.container[it]);
			for(size_t it = 0; it < s; it++) container[it]->RemMarker(mrk);
			container[0]->GetMeshLink()->ReleaseMarker(mrk);
		}
	}
	template <typename AdjacentType>
	void adjacent<AdjacentType>::substract(const adjacent<AdjacentType> & other)
	{
		if( !empty() )
		{
			Mesh * m = container[0]->GetMeshLink();
			MIDType mrk = m->CreateMarker();
			for(size_t it = 0; it < other.size(); it++) other.container[it]->SetMarker(mrk);
			{
				size_t m = 0, n = 0;
				while( m < size() ) 
				{
					if( !container[m]->GetMarker(mrk) )
						container[n++] = container[m];
					m++;
				}
				container.resize(n);
			}
			for(size_t it = 0; it < other.size(); it++) other.container[it]->RemMarker(mrk);
			m->ReleaseMarker(mrk);
		}
	}
	template <typename AdjacentType>
	void adjacent<AdjacentType>::intersect(const adjacent<AdjacentType> & other)
	{
		if( !empty() )
		{
			Mesh * m = container[0]->GetMeshLink();
			MIDType mrk = m->CreateMarker();
			for(size_t it = 0; it < other.size(); it++) other.container[it]->SetMarker(mrk);
			{
				size_t m = 0, n = 0;
				while( m < size() ) 
				{
					if( container[m]->GetMarker(mrk) )
						container[n++] = container[m];
					m++;
				}
				container.resize(n);
			}
			for(size_t it = 0; it < other.size(); it++) other.container[it]->RemMarker(mrk);
			m->ReleaseMarker(mrk);
		}
	}
	
	bool LessElements(Element * a,Element * b); //function for std::sort and std::set based on USE_COMPARE
	int CompareElements(Element * a,Element * b); //function based on USE_COMPARE
	int CompareElementsC(const void * a,const void * b); //function for qsort based on USE_COMPARE 

	int CompareElementsGID(Element * a,Element * b);
	int CompareElementsPointer(Element * a,Element * b);
	int CompareElementsUnique(Element * a,Element * b);
	int CompareElementsCentroid(Element * a,Element * b);
	int CompareElementsHybrid(Element * a,Element * b);

	bool LessElementsGID(Element * a,Element * b);	
	bool LessElementsUnique(Element * a,Element * b);
	bool LessElementsPointer(Element * a,Element * b);
	bool LessElementsCentroid(Element * a,Element * b);
	
	int CompareElementsCGID(const void * pa, const void * pb);
	int CompareElementsCPointer(const void * pa, const void * pb);
	int CompareElementsCUnique(const void * pa, const void * pb);
	int CompareElementsCCentroid(const void * pa, const void * pb);
	
	
	//functions for algorithm cpp
	void SwapElement(void * pa, void * pb, void * udata);
	void SwapPointer(void * pa, void * pb, void * udata);
	int CompareElement(const void * pa, const void * pb, void * udata);
	int CompareElementPointer(const void * pa, const void * pb, void * udata);
	int CompareCoordSearch(const void * pa, void * udata);
	int CompareGIDSearch(const void * pa, void * udata);
	
	//algorithm.cpp
	void sort(void *pbase, unsigned num, unsigned width, int (*comp)(const void *, const void *,void *), void (*swap)(void *, void *, void *), void * user_data);
	int binary_search(void * data, unsigned num, unsigned width, int comp(const void *,void *), void * user_data);
	size_t hashfunci32(const char * pos, int n);



}

#endif

#endif // INMOST_MESH_H_INCLUDED