inmost_mesh.h 203 KB
Newer Older
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1

Kirill Terekhov's avatar
Kirill Terekhov committed
2
3
4
5
6
7
8
9
10
11
#ifndef INMOST_MESH_H_INCLUDED
#define INMOST_MESH_H_INCLUDED

#include "inmost_common.h"

#if defined(USE_MESH)

#define __NDT 3
#define __NET 6

Kirill Terekhov's avatar
Kirill Terekhov committed
12

Kirill Terekhov's avatar
Kirill Terekhov committed
13
14
namespace INMOST
{
Kirill Terekhov's avatar
Kirill Terekhov committed
15
16


Kirill Terekhov's avatar
Kirill Terekhov committed
17
18
19
20
21
22
23
24
	class Mesh;
	class Storage;
	class Element;
	class TagManager;
	class Node;
	class Edge;
	class Face;
	class Cell;
Kirill Terekhov's avatar
Kirill Terekhov committed
25
	class ElementSet;
Kirill Terekhov's avatar
Kirill Terekhov committed
26
27
28
29
30
31
32
33
34

	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;
Kirill Terekhov's avatar
Kirill Terekhov committed
35
36
37
38
39

	typedef INMOST_DATA_ENUM_TYPE HandleType;
	static const INMOST_DATA_ENUM_TYPE handle_etype_bits  = 3;
	static const INMOST_DATA_ENUM_TYPE handle_etype_shift = sizeof(HandleType)*8-handle_etype_bits;
	static const INMOST_DATA_ENUM_TYPE handle_id_mask     = (1 << handle_etype_shift)-1;
Kirill Terekhov's avatar
Kirill Terekhov committed
40
41
42
43
44
45
46
47
	
	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
48
	typedef INMOST_DATA_BULK_TYPE SyncBitOp; //< This type is used for marker synchronization
Kirill Terekhov's avatar
Kirill Terekhov committed
49
	static const SyncBitOp SYNC_BIT_SET = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
50
51
52
53
	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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
	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;
	typedef array<HandleType>               inner_reference_array;

	enum DataType
	{
		DATA_REAL      = 0, 
		DATA_INTEGER   = 1, 
		DATA_BULK      = 2,
		DATA_REFERENCE = 3
	};

	typedef INMOST_DATA_ENUM_TYPE MarkerType; // low 8 bits - marker mask, rest high bits - position of marker
	static const INMOST_DATA_ENUM_TYPE MarkerFields = 16;   // number of chars to hold all markers, total number (MarkerFields * bits_per_char)
	static const INMOST_DATA_ENUM_TYPE MarkerMask   = static_cast<INMOST_DATA_BULK_TYPE>(-1); // bit mask to obtain marker mask within MarkerType
	static const INMOST_DATA_ENUM_TYPE MarkerShift  = sizeof(INMOST_DATA_BULK_TYPE)*8;    // sizeof(char) * bits_per_char
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
71
	__INLINE static MarkerType InvalidMarker() {return ENUMUNDEF;}
Kirill Terekhov's avatar
Kirill Terekhov committed
72
73
74
75
76
77
78


	static const INMOST_DATA_ENUM_TYPE chunk_bits_elems       = 13;
	static const INMOST_DATA_ENUM_TYPE chunk_bits_empty       = 8;
	static const INMOST_DATA_ENUM_TYPE chunk_bits_tags        = 6;
	static const INMOST_DATA_ENUM_TYPE chunk_bits_dense_array = 6;

Kirill Terekhov's avatar
Kirill Terekhov committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
	//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
Kirill Terekhov's avatar
Kirill Terekhov committed
96
	static const TopologyCheck ADJACENT_DUPLICATE     = 0x00008000; //done//produce error if provided array of elements for construction contain duplications
Kirill Terekhov's avatar
Kirill Terekhov committed
97
	static const TopologyCheck ADJACENT_HIDDEN        = 0x00010000; //done//hidden elements should not be used when new elements are created
Kirill Terekhov's avatar
Kirill Terekhov committed
98
	static const TopologyCheck ADJACENT_VALID         = 0x00020000; //done//check that all handles are valid
Kirill Terekhov's avatar
Kirill Terekhov committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	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
Fixes    
Kirill Terekhov committed
114
	static const TopologyCheck DEFAULT_CHECK          = THROW_EXCEPTION | DUPLICATE_EDGE | DUPLICATE_FACE | DUPLICATE_CELL | PRINT_NOTIFY;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
115
116
117
118
119
120
121
122
123
124
	const char *                             TopologyCheckNotifyString(TopologyCheck c); //mesh.cpp
	const char *                             DataTypeName         (DataType t); //tag.cpp
	__INLINE bool                            OneType              (ElementType t) {return t > 0 && (t & (t-1)) == 0;}
	__INLINE ElementType                     FirstElementType     () {return NODE;}
	__INLINE ElementType                     LastElementType      () {return MESH << 1;}
	__INLINE ElementType                     NextElementType      (ElementType etype) {return etype << 1;}
	__INLINE ElementType                     PrevElementType      (ElementType etype) {return etype >> 1;}
	__INLINE ElementType                     ElementTypeFromDim   (INMOST_DATA_INTEGER_TYPE dim) {return 1 << dim;}
	const char *                             ElementTypeName      (ElementType t); //mesh.cpp
	__INLINE INMOST_DATA_INTEGER_TYPE        ElementNum           (ElementType t)
Kirill Terekhov's avatar
Kirill Terekhov committed
125
126
	{
		unsigned int v = static_cast<unsigned int>(t);  // 32-bit value to find the log2 of 
Kirill Terekhov's avatar
Kirill Terekhov committed
127
128
		//static const unsigned int b[] = {0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000};
		static const unsigned int b[] = {0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0};
Kirill Terekhov's avatar
Kirill Terekhov committed
129
		register unsigned int r = (v & b[0]) != 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
130
131
		//r |= ((v & b[4]) != 0) << 4;
		//r |= ((v & b[3]) != 0) << 3;
Kirill Terekhov's avatar
Kirill Terekhov committed
132
133
		r |= ((v & b[2]) != 0) << 2;
		r |= ((v & b[1]) != 0) << 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
134
		return static_cast<INMOST_DATA_INTEGER_TYPE>(r);
Kirill Terekhov's avatar
Kirill Terekhov committed
135
	}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
136
137
138
139
140
141
142
	__INLINE HandleType                      InvalidHandle        () {return 0;}
	__INLINE INMOST_DATA_INTEGER_TYPE        GetHandleID          (HandleType h) {return (h & handle_id_mask)-1;}
	__INLINE INMOST_DATA_INTEGER_TYPE        GetHandleElementNum  (HandleType h) {return h >> handle_etype_shift;}
	__INLINE ElementType                     GetHandleElementType (HandleType h) {return 1 << GetHandleElementNum(h);}
	__INLINE HandleType                      ComposeHandle        (ElementType etype, INMOST_DATA_INTEGER_TYPE ID) {return ID == -1 ? InvalidHandle() : ((ElementNum(etype) << handle_etype_shift) + (1+ID));}
	__INLINE HandleType                      ComposeHandle        (INMOST_DATA_INTEGER_TYPE etypenum, INMOST_DATA_INTEGER_TYPE ID) {return ID == -1 ? InvalidHandle() : ((etypenum << handle_etype_shift) + (1+ID));}
	__INLINE bool                            isValidHandle        (HandleType h) {return h != 0;}
Kirill Terekhov's avatar
Kirill Terekhov committed
143
	
Kirill Terekhov's avatar
Kirill Terekhov committed
144

Kirill Terekhov's avatar
Kirill Terekhov committed
145
	
Kirill Terekhov's avatar
Kirill Terekhov committed
146
147
148
149
150
151
152
153
154

	class TagMemory //implemented in tag.cpp
	{
	public:
		~TagMemory();
		TagMemory(Mesh * m, const TagMemory & other);
		TagMemory & operator =(TagMemory const & other);
	private:
		TagMemory();
Kirill Terekhov's avatar
Kirill Terekhov committed
155
156
157
158
		INMOST_DATA_ENUM_TYPE  pos[__NET]; 
		DataType               dtype;                 
		std::string            tagname;            
		INMOST_MPI_Type        bulk_data_type;   
Kirill Terekhov's avatar
Kirill Terekhov committed
159
		INMOST_DATA_ENUM_TYPE  size, bytes_size;
Kirill Terekhov's avatar
Kirill Terekhov committed
160
		bool                   sparse[__NET];
Kirill Terekhov's avatar
Kirill Terekhov committed
161
		INMOST_DATA_ENUM_TYPE  record_size;
Kirill Terekhov's avatar
Kirill Terekhov committed
162
		Mesh *                 m_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
163
		friend class Tag;
Kirill Terekhov's avatar
Kirill Terekhov committed
164
		friend class Storage; //for debug
Kirill Terekhov's avatar
Kirill Terekhov committed
165
166
167
168
169
170
171
	};

	class Tag //implemented in tag.cpp
	{
	private:
		TagMemory * mem;
		Tag(Mesh * m, std::string name, DataType _dtype, INMOST_DATA_ENUM_TYPE size); //Create me through TagManager::CreateTag
Kirill Terekhov's avatar
Kirill Terekhov committed
172
		__INLINE INMOST_DATA_ENUM_TYPE  GetRecordSize() const {return mem->record_size;}
Kirill Terekhov's avatar
Kirill Terekhov committed
173
174
175
176
		__INLINE void                   SetSize(INMOST_DATA_ENUM_TYPE size) {mem->size = size;}
		__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;}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
177
		__INLINE INMOST_DATA_ENUM_TYPE  GetPositionByDim(INMOST_DATA_ENUM_TYPE typenum) const {return mem->pos[typenum];}
Kirill Terekhov's avatar
Kirill Terekhov committed
178
179
180
181
182
183
184
185
186
	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;	}
Kirill Terekhov's avatar
Kirill Terekhov committed
187
188
		__INLINE DataType               GetDataType() const {assert(mem!=NULL); return mem->dtype;}
		__INLINE INMOST_MPI_Type        GetBulkDataType() const {assert(mem!=NULL); return mem->bulk_data_type;}
Kirill Terekhov's avatar
Kirill Terekhov committed
189
		__INLINE INMOST_DATA_ENUM_TYPE  GetBytesSize() const {assert(mem!=NULL); return mem->bytes_size;}
Kirill Terekhov's avatar
Kirill Terekhov committed
190
191
192
193
194
195
		__INLINE INMOST_DATA_ENUM_TYPE  GetSize() const {assert(mem!=NULL); return mem->size;}
		__INLINE std::string            GetTagName() const {assert(mem!=NULL); return mem->tagname;}
		__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)];}
		__INLINE bool                   isValid() const {return mem != NULL;}
		__INLINE Mesh *                 GetMeshLink() const {assert(mem!=NULL); return mem->m_link;}		
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
196
197
		__INLINE bool                   isSparseByDim(INMOST_DATA_INTEGER_TYPE typenum) const {assert(mem!=NULL); return mem->sparse[typenum];}
		__INLINE bool                   isDefinedByDim(INMOST_DATA_INTEGER_TYPE typenum) const {assert(mem!=NULL); return GetPositionByDim(typenum) != ENUMUNDEF;}
Kirill Terekhov's avatar
Kirill Terekhov committed
198
		__INLINE void                   SetBulkDataType(INMOST_MPI_Type type) {assert(mem!=NULL && mem->dtype == DATA_BULK ); mem->bulk_data_type = type;}
Kirill Terekhov's avatar
Kirill Terekhov committed
199
200
201
202
203
204
205
206
207
		friend class TagManager;
		friend class Storage;
		friend class Mesh;
	};
	
	class TagManager //implemented in tag.cpp
	{
	protected:
		TagManager();
Kirill Terekhov's avatar
Kirill Terekhov committed
208
209
210
211
212
213
214
215
216
217
218
219
220
		TagManager(const TagManager & other);
		TagManager & operator = (TagManager const & other);
		typedef chunk_array<INMOST_DATA_ENUM_TYPE,chunk_bits_empty>    empty_data;
		typedef chunk_array<Tag, chunk_bits_tags>                      tag_array_type;
		typedef chunk_bulk_array<chunk_bits_elems>                     dense_sub_type;
		typedef chunk_array<dense_sub_type,chunk_bits_dense_array>     dense_data_array_type;
		typedef struct{void * tag, * rec;}                             sparse_sub_record;
		typedef array< sparse_sub_record >                             sparse_sub_type;
		typedef chunk_array< sparse_sub_type,chunk_bits_elems>         sparse_data_array_type;
		typedef chunk_array<INMOST_DATA_INTEGER_TYPE,chunk_bits_elems> back_links_type;
		//typedef std::vector<INMOST_DATA_INTEGER_TYPE>                  back_links_type;
	public:
		typedef tag_array_type::iterator                               iteratorTag;
Kirill Terekhov's avatar
Kirill Terekhov committed
221
222
	public:
		virtual ~TagManager();
Kirill Terekhov's avatar
Kirill Terekhov committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
		bool                             HaveTag            (std::string name) const;
		Tag                              GetTag             (std::string name) const;
		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); 
		bool                             ElementDefined     (Tag const & tag, ElementType etype) const;
	protected:
		void                             ReallocateData     (const Tag & t, INMOST_DATA_INTEGER_TYPE etypenum,INMOST_DATA_ENUM_TYPE new_size);
		void                             ReallocateData     (INMOST_DATA_INTEGER_TYPE etypenum, INMOST_DATA_ENUM_TYPE new_size);
		__INLINE sparse_sub_type const & GetSparseData      (int etypenum, int local_id) const {return sparse_data[etypenum][local_id];}
		__INLINE sparse_sub_type &       GetSparseData      (int etypenum, int local_id) {return sparse_data[etypenum][local_id];}
		__INLINE dense_sub_type const &  GetDenseData       (int pos) const {return dense_data[pos];}
		__INLINE dense_sub_type &        GetDenseData       (int pos) {return dense_data[pos];}
		static void                      CopyData           (const Tag & t, void * adata, const void * bdata);
		static void                      DestroyVariableData(const Tag & t, void * adata);
Kirill Terekhov's avatar
Kirill Terekhov committed
238
	protected:
Kirill Terekhov's avatar
Kirill Terekhov committed
239
240
		typedef tag_array_type::iterator            tag_iterator;
		typedef tag_array_type::const_iterator      tag_const_iterator;
Kirill Terekhov's avatar
Kirill Terekhov committed
241
	protected:
Kirill Terekhov's avatar
Kirill Terekhov committed
242
243
244
245
		tag_array_type                              tags;
		empty_data                                  empty_dense_data;
		dense_data_array_type                       dense_data;
		sparse_data_array_type                      sparse_data[6];
Kirill Terekhov's avatar
Kirill Terekhov committed
246
		back_links_type                             back_links[6];
Kirill Terekhov's avatar
Kirill Terekhov committed
247
248
	};
	
249
250
251
252
253
	/// 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
254
255
256
	class Storage //implemented in storage.cpp
	{
	public:
257
		/// Storage type for representing real values.
Kirill Terekhov's avatar
Kirill Terekhov committed
258
		typedef INMOST_DATA_REAL_TYPE     real; 
259
		/// Storage type for representing integer values.
Kirill Terekhov's avatar
Kirill Terekhov committed
260
		typedef INMOST_DATA_INTEGER_TYPE  integer;
261
		/// Storage type for representing one byte of abstact data.
Kirill Terekhov's avatar
Kirill Terekhov committed
262
		typedef INMOST_DATA_BULK_TYPE     bulk;
Kirill Terekhov's avatar
Kirill Terekhov committed
263
264
		/// type for representing unsigned integer values.
		typedef INMOST_DATA_ENUM_TYPE     enumerator;
265
		/// Storage type for representing references to Element.
Kirill Terekhov's avatar
Kirill Terekhov committed
266
		typedef HandleType                reference;
267
		/// Storage type for representing arrays of real values.
Kirill Terekhov's avatar
Kirill Terekhov committed
268
		typedef shell<real>               real_array;
269
		/// Storage type for representing arrays of integer values.
Kirill Terekhov's avatar
Kirill Terekhov committed
270
		typedef shell<integer>            integer_array;
271
		/// Storage type for representing abstact data as a series of bytes.
Kirill Terekhov's avatar
Kirill Terekhov committed
272
		typedef shell<bulk>               bulk_array;
273
		/// Storage type for representing arrays of Element references.
Kirill Terekhov's avatar
Kirill Terekhov committed
274
275
276
277
		class reference_array : public shell<HandleType>
		{
			Mesh * m;
		public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
278
			reference_array() : m(NULL) {}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
279
280
			reference_array(Mesh * m, inner_reference_array & arr) : shell<reference>(arr), m(m) {} 
			reference_array(Mesh * m, reference * arr, size_type size) : shell<reference>(arr,size), m(m) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
281
282
			~reference_array() {}
			void push_back(const Storage & elem);
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
283
284
			void push_back(HandleType h) {shell<reference>::push_back(h);} //is it needed?
			Element operator[] (size_type n);
Kirill Terekhov's avatar
Kirill Terekhov committed
285
286
287
288
289
			Element operator[] (size_type n) const;
			class iterator : public shell<HandleType>::iterator
			{
				Mesh * m;
			public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
290
291
292
				iterator(Mesh * m, const shell<HandleType>::iterator & other) : shell<HandleType>::iterator(other), m(m) {}
				iterator(const iterator & other) : shell<HandleType>::iterator(other), m(other.m) {}
				iterator & operator =(iterator const & other) {m = other.m; shell<HandleType>::iterator::operator=(other); return *this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
293
294
295
296
297
298
299
300
301
302
				iterator & operator ++() {shell<HandleType>::iterator::operator++(); return *this;}
				iterator   operator ++(int) {iterator ret(*this); shell<HandleType>::iterator::operator++(); return ret;}
				iterator & operator --() {shell<HandleType>::iterator::operator--(); return *this;}
				iterator   operator --(int) {iterator ret(*this); shell<HandleType>::iterator::operator--(); return ret;}
				Element operator->();
			};
			class const_iterator : public shell<HandleType>::const_iterator
			{
				Mesh * m;
			public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
303
304
305
				const_iterator(Mesh * m, const shell<HandleType>::const_iterator & other) : shell<HandleType>::const_iterator(other) , m(m) {}
				const_iterator(const const_iterator & other) : shell<HandleType>::const_iterator(other), m(other.m) {}
				const_iterator & operator =(const_iterator const & other) {m = other.m; shell<HandleType>::const_iterator::operator=(other); return *this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
306
307
308
309
310
311
312
313
314
315
				const_iterator & operator ++() {shell<HandleType>::const_iterator::operator++(); return *this;}
				const_iterator   operator ++(int) {const_iterator ret(*this); shell<HandleType>::const_iterator::operator++(); return ret;}
				const_iterator & operator --() {shell<HandleType>::const_iterator::operator--(); return *this;}
				const_iterator   operator --(int) {const_iterator ret(*this); shell<HandleType>::const_iterator::operator--(); return ret;}
				Element operator->();
			};
			class reverse_iterator : public shell<HandleType>::reverse_iterator
			{
				Mesh * m;
			public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
316
317
318
				reverse_iterator(Mesh * m, const shell<HandleType>::reverse_iterator & other) : shell<HandleType>::reverse_iterator(other), m(m) {}
				reverse_iterator(const reverse_iterator & other) : shell<HandleType>::reverse_iterator(other), m(other.m)  {}
				reverse_iterator & operator =(reverse_iterator const & other) {m = other.m; shell<HandleType>::reverse_iterator::operator=(other); return *this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
319
320
321
322
323
324
325
326
327
328
				reverse_iterator & operator ++() {shell<HandleType>::reverse_iterator::operator++(); return *this;}
				reverse_iterator   operator ++(int) {reverse_iterator ret(*this); shell<HandleType>::reverse_iterator::operator++(); return ret;}
				reverse_iterator & operator --() {shell<HandleType>::reverse_iterator::operator--(); return *this;}
				reverse_iterator   operator --(int) {reverse_iterator ret(*this); shell<HandleType>::reverse_iterator::operator--(); return ret;}
				Element operator->();
			};
			class const_reverse_iterator : public shell<HandleType>::const_reverse_iterator
			{
				Mesh * m;
			public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
329
330
331
				const_reverse_iterator(Mesh * m, const shell<HandleType>::const_reverse_iterator & other) : shell<HandleType>::const_reverse_iterator(other), m(m) {}
				const_reverse_iterator(const const_reverse_iterator & other) : shell<HandleType>::const_reverse_iterator(other), m(other.m) {}
				const_reverse_iterator & operator =(const_reverse_iterator const & other) {m = other.m; shell<HandleType>::const_reverse_iterator::operator=(other); return *this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
332
333
334
335
336
337
				const_reverse_iterator & operator ++() {shell<HandleType>::const_reverse_iterator::operator++(); return *this;}
				const_reverse_iterator   operator ++(int) {const_reverse_iterator ret(*this); shell<HandleType>::const_reverse_iterator::operator++(); return ret;}
				const_reverse_iterator & operator --() {shell<HandleType>::const_reverse_iterator::operator--(); return *this;}
				const_reverse_iterator   operator --(int) {const_reverse_iterator ret(*this); shell<HandleType>::const_reverse_iterator::operator--(); return ret;}
				Element operator->();
			};
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
338
339
340
341
342
343
344
345
			iterator begin() {return iterator(m,shell<HandleType>::begin());}
			const_iterator begin() const {return const_iterator(m,shell<HandleType>::begin());}
			reverse_iterator rbegin() {return reverse_iterator(m,shell<HandleType>::rbegin());}
			const_reverse_iterator rbegin() const {return const_reverse_iterator(m,shell<HandleType>::rbegin());}
			iterator end() {return iterator(m,shell<HandleType>::end());}
			const_iterator end() const {return const_iterator(m,shell<HandleType>::end());}
			reverse_iterator rend() {return reverse_iterator(m,shell<HandleType>::rend());}
			const_reverse_iterator rend() const {return const_reverse_iterator(m,shell<HandleType>::rend());}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
346
			void SetMeshLink(Mesh * new_m) {m = new_m;}
Kirill Terekhov's avatar
Kirill Terekhov committed
347
348
349
350
		};
		//typedef shell<reference>          reference_array;
	protected:
		HandleType                        handle;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
351
		HandleType *                      handle_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
352
353
354
	private:
		Mesh *                            m_link;
	public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
355
356
357
358
359
360
361
362
363
		Storage(const Storage & other) : handle(other.handle), handle_link(other.handle_link), m_link(other.m_link) {}
		Storage(Mesh * mesh, HandleType handle) : handle(handle), handle_link(NULL), m_link(mesh) {}
		/// This constructor allows for remote handle modification
		Storage(Mesh * mesh, HandleType * handle) : handle(*handle), handle_link(handle), m_link(mesh) {}
		/// If there is a link to handle provided (automatically by ElementArray and reference_array),
		/// then remote handle value will be modified
		Storage & operator =(Storage const & other); 
		bool      operator ==(const Storage & other) {return handle == other.handle;} //m_link may be NULL if Invalidxxx is used
		bool      operator !=(const Storage & other) {return handle != other.handle;} //m_link may be NULL if Invalidxxx is used
Kirill Terekhov's avatar
Kirill Terekhov committed
364
365
366
367
		Storage * operator ->() {return this;}
		const Storage * operator->() const {return this;}
		Storage & self() {return *this;}
		const Storage & self() const {return *this;}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
368
		virtual ~Storage() {}
Kirill Terekhov's avatar
Kirill Terekhov committed
369
	public:
370
		/// Retrieve real value associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
371
		real      &              Real            (const Tag & tag) const;
372
		/// Retrieve integer value associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
373
		integer   &              Integer         (const Tag & tag) const;
374
		/// Retrieve one byte of abstract data associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
375
		bulk      &              Bulk            (const Tag & tag) const;
376
		/// Retrieve Element reference associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
377
		reference &              Reference       (const Tag & tag) const;
378
		/// Retrieve array of real values associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
379
		real_array               RealArray       (const Tag & tag) const;
380
		/// Retrieve array of integer values associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
381
		integer_array            IntegerArray    (const Tag & tag) const;
382
		/// Retrieve abstract data associated with Tag as a series of bytes.
Kirill Terekhov's avatar
Kirill Terekhov committed
383
		bulk_array               BulkArray       (const Tag & tag) const;
384
		/// Retrieve array of Element references associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
385
		reference_array          ReferenceArray  (const Tag & tag) const;
Kirill Terekhov's avatar
Kirill Terekhov committed
386
387
		
		//optimized data requests for dense data with fixed size
Kirill Terekhov's avatar
Kirill Terekhov committed
388
389
390
391
392
393
394
395
		real_array               RealArrayDF     (const Tag & tag) const;
		integer_array            IntegerArrayDF  (const Tag & tag) const;
		bulk_array               BulkArrayDF     (const Tag & tag) const;
		reference_array          ReferenceArrayDF(const Tag & tag) const;
		real      &              RealDF          (const Tag & tag) const;
		integer   &              IntegerDF       (const Tag & tag) const;
		bulk      &              BulkDF          (const Tag & tag) const;
		reference &              ReferenceDF     (const Tag & tag) const;
Kirill Terekhov's avatar
Kirill Terekhov committed
396
397
		
		//optimized data requests for dense data with variable size
Kirill Terekhov's avatar
Kirill Terekhov committed
398
399
400
401
402
403
404
405
		real_array               RealArrayDV     (const Tag & tag) const;
		integer_array            IntegerArrayDV  (const Tag & tag) const;
		bulk_array               BulkArrayDV     (const Tag & tag) const;
		reference_array          ReferenceArrayDV(const Tag & tag) const;
		real      &              RealDV          (const Tag & tag) const;
		integer   &              IntegerDV       (const Tag & tag) const;
		bulk      &              BulkDV          (const Tag & tag) const;
		reference &              ReferenceDV     (const Tag & tag) const;
Kirill Terekhov's avatar
Kirill Terekhov committed
406
		
407
408
409
		/// 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
410
		INMOST_DATA_ENUM_TYPE    GetDataSize     (const Tag & tag) const; //For DATA_BULK return number of bytes, otherwise return the length of array
411
412
413
414
		/// 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
415
		void                     SetDataSize     (const Tag & tag,INMOST_DATA_ENUM_TYPE new_size) const;
416
417
418
419
420
421
422
423
424
		/// 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
425
426
427
428
429
430
431
		void                     GetData         (const Tag & tag, INMOST_DATA_ENUM_TYPE shift, INMOST_DATA_ENUM_TYPE size, void * data) const;
		void                     SetData         (const Tag & tag, INMOST_DATA_ENUM_TYPE shift, INMOST_DATA_ENUM_TYPE size, const void * data) const;
		void                     DelData         (const Tag & tag) const;
		/// Deallocates space allocated for sparse data, frees variable array if necessary
		void                     DelSparseData   (const Tag & tag) const;
		/// Frees variable array or fills field with zeroes
		void                     DelDenseData    (const Tag & tag) const;
432
		/// Check if any data is associated with Tag.
Kirill Terekhov's avatar
Kirill Terekhov committed
433
434
435
436
437
438
439
440
441
442
		bool                     HaveData        (const Tag & tag) const;
		__INLINE ElementType     GetElementType  () const {return GetHandleElementType(handle);}
		__INLINE integer         GetElementNum   () const {return GetHandleElementNum(handle);}
		void                     SetMarker       (MarkerType n) const;
		bool                     GetMarker       (MarkerType n) const;
		void                     RemMarker       (MarkerType n) const;
		void                     ClearMarkerSpace() const;
		void                     GetMarkerSpace  (bulk copy[MarkerFields]) const;
		void                     SetMarkerSpace  (bulk source[MarkerFields]) const;
		__INLINE integer         LocalID         () const {return GetHandleID(handle);}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
443
444
445
		/// This number is guaranteed to be between 0 and Mesh::NumberOf(type of element)
		/// after Mesh::ReorderEmpty
		integer                  DataLocalID     () const;
Kirill Terekhov's avatar
Kirill Terekhov committed
446
447
448
		bool                     isValid         () const;
		__INLINE Mesh *          GetMeshLink     () const {return m_link;}
		__INLINE HandleType      GetHandle       () const {return handle;}
Kirill Terekhov's avatar
Kirill Terekhov committed
449
450
451
		friend class Mesh;
	};
	
Kirill Terekhov's avatar
Kirill Terekhov committed
452
453
	template <typename StorageType>
	class ElementArray
Kirill Terekhov's avatar
Kirill Terekhov committed
454
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
455
456
457
	public:
		typedef dynarray<HandleType,64>      cont_t;
		typedef cont_t::size_type            size_type;
Kirill Terekhov's avatar
Kirill Terekhov committed
458
	private:
Kirill Terekhov's avatar
Kirill Terekhov committed
459
460
		Mesh *                               m_link;
		cont_t                               container;
Kirill Terekhov's avatar
Kirill Terekhov committed
461
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
462
463
464
465
466
467
468
469
470
471
472
473
		const cont_t & GetContainer() {return container;}
		~ElementArray() {}
		ElementArray() : m_link(NULL) {}
		ElementArray(Mesh * m_link) : m_link(m_link) {}
		ElementArray(Mesh * m_link, size_type n, HandleType h = InvalidHandle())  : m_link(m_link), container(n,h) {}
		ElementArray(Mesh * m_link, const cont_t & c)  : m_link(m_link), container(c) {}
		ElementArray(const ElementArray & other) : m_link(other.m_link), container(other.container) {}
		template<class InputIterator>
		ElementArray(Mesh * m_link, InputIterator first, InputIterator last) :m_link(m_link), container(first,last) {}
		ElementArray & operator=(ElementArray const & other) {m_link = other.m_link; container = other.container; return *this;}
	public:
		class iterator : public cont_t::iterator
Kirill Terekhov's avatar
Kirill Terekhov committed
474
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
475
			Mesh * m_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
476
		public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
477
478
			iterator(Mesh * m, const cont_t::iterator & other ) : cont_t::iterator(other) , m_link(m){assert(m_link);}
			iterator(const iterator & other ) : cont_t::iterator(other), m_link(other.m_link) {assert(m_link);}
Kirill Terekhov's avatar
Kirill Terekhov committed
479
480
481
482
483
484
			iterator &    operator ++() {cont_t::iterator::operator++(); return *this;}
			iterator      operator ++(int) {iterator ret(*this); cont_t::iterator::operator++(); return ret;}
			iterator &    operator --() {cont_t::iterator::operator--(); return *this;}
			iterator      operator --(int) {iterator ret(*this); cont_t::iterator::operator--(); return ret;}
			iterator &    operator =(iterator const & other) {m_link = other.m_link; cont_t::iterator::operator=(static_cast<cont_t::iterator const &>(other)); return *this; }
			HandleType &  operator *() { return cont_t::iterator::operator *(); }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
485
			StorageType   operator->() { return StorageType(m_link,&cont_t::iterator::operator *()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
486
		};
Kirill Terekhov's avatar
Kirill Terekhov committed
487
		class reverse_iterator : public cont_t::reverse_iterator
Kirill Terekhov's avatar
Kirill Terekhov committed
488
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
489
			Mesh * m_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
490
		public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
491
492
			reverse_iterator(Mesh * m, const cont_t::reverse_iterator & other ) : cont_t::reverse_iterator(other), m_link(m) {assert(m_link);}
			reverse_iterator(const reverse_iterator & other ) : cont_t::reverse_iterator(other), m_link(other.m_link) {assert(m_link);}
Kirill Terekhov's avatar
Kirill Terekhov committed
493
494
495
496
497
498
			reverse_iterator &    operator ++() {cont_t::reverse_iterator::operator++(); return *this;}
			reverse_iterator      operator ++(int) {reverse_iterator ret(*this); cont_t::reverse_iterator::operator++(); return ret;}
			reverse_iterator &    operator --() {cont_t::reverse_iterator::operator--(); return *this;}
			reverse_iterator      operator --(int) {reverse_iterator ret(*this); cont_t::reverse_iterator::operator--(); return ret;}
			reverse_iterator & operator =(reverse_iterator const & other) {m_link = other.m_link; cont_t::reverse_iterator::operator=(static_cast<cont_t::reverse_iterator const &>(other)); return *this; }
			HandleType &       operator *() { return cont_t::reverse_iterator::operator *(); }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
499
			StorageType        operator->() { return StorageType(m_link,&cont_t::reverse_iterator::operator *()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
500
		};
Kirill Terekhov's avatar
Kirill Terekhov committed
501
		class const_iterator : public cont_t::const_iterator
Kirill Terekhov's avatar
Kirill Terekhov committed
502
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
503
			Mesh * m_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
504
		public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
505
506
			const_iterator(Mesh * m, const cont_t::const_iterator & other ) : cont_t::const_iterator(other), m_link(m) {assert(m_link);}
			const_iterator(const const_iterator & other ) : cont_t::const_iterator(other), m_link(other.m_link) {assert(m_link);}
Kirill Terekhov's avatar
Kirill Terekhov committed
507
508
509
510
511
512
513
			const_iterator &    operator ++() {cont_t::const_iterator::operator++(); return *this;}
			const_iterator      operator ++(int) {const_iterator ret(*this); cont_t::const_iterator::operator++(); return ret;}
			const_iterator &    operator --() {cont_t::const_iterator::operator--(); return *this;}
			const_iterator      operator --(int) {const_iterator ret(*this); cont_t::const_iterator::operator--(); return ret;}
			const_iterator &    operator =(const_iterator const & other) {m_link = other.m_link; cont_t::const_iterator::operator=(static_cast<cont_t::const_iterator const &>(other)); return *this; }
			const HandleType &  operator *() { return cont_t::const_iterator::operator *(); }
			StorageType         operator->() { return StorageType(m_link,cont_t::const_iterator::operator *()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
514
		};
Kirill Terekhov's avatar
Kirill Terekhov committed
515
		class const_reverse_iterator : public cont_t::const_reverse_iterator
Kirill Terekhov's avatar
Kirill Terekhov committed
516
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
517
			Mesh * m_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
518
		public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
519
520
			const_reverse_iterator(Mesh * m, const cont_t::const_reverse_iterator & other) : cont_t::const_reverse_iterator(other), m_link(m) {assert(m_link);}
			const_reverse_iterator(const const_reverse_iterator & other ) : cont_t::const_reverse_iterator(other), m_link(other.m_link) {assert(m_link);}
Kirill Terekhov's avatar
Kirill Terekhov committed
521
522
523
524
525
526
527
			const_reverse_iterator &    operator ++() {cont_t::const_reverse_iterator::operator++(); return *this;}
			const_reverse_iterator      operator ++(int) {const_reverse_iterator ret(*this); cont_t::const_reverse_iterator::operator++(); return ret;}
			const_reverse_iterator &    operator --() {cont_t::const_reverse_iterator::operator--(); return *this;}
			const_reverse_iterator      operator --(int) {const_reverse_iterator ret(*this); cont_t::const_reverse_iterator::operator--(); return ret;}
			const_reverse_iterator & operator =(const_reverse_iterator const & other) { cont_t::const_reverse_iterator::operator=(static_cast<cont_t::const_reverse_iterator const &>(other)); return *this; }
			const HandleType &       operator *() { return cont_t::const_reverse_iterator::operator *(); }
			StorageType              operator->() { return StorageType(m_link,cont_t::const_reverse_iterator::operator *()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
528
		};
Kirill Terekhov's avatar
Kirill Terekhov committed
529
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
530
		template<class InputIterator>
Kirill Terekhov's avatar
Kirill Terekhov committed
531
532
533
534
535
536
537
538
539
540
541
542
		__INLINE void             insert      (iterator pos,InputIterator pbeg, InputIterator pend) {container.insert(static_cast<cont_t::iterator>(pos),pbeg,pend);}
		__INLINE iterator         erase       (iterator pos) {return iterator(m_link,container.erase(static_cast<cont_t::iterator>(pos)));}

		__INLINE iterator         begin       () { return iterator(m_link,container.begin()); }
		__INLINE iterator         end         () { return iterator(m_link,container.end()); }
		__INLINE reverse_iterator rbegin      () { return reverse_iterator(m_link,container.rbegin()); }
		__INLINE reverse_iterator rend        () { return reverse_iterator(m_link,container.rend()); }
		__INLINE const_iterator         begin () const { return const_iterator(m_link,container.begin()); }
		__INLINE const_iterator         end   () const { return const_iterator(m_link,container.end()); }
		__INLINE const_reverse_iterator rbegin() const { return const_reverse_iterator(m_link,container.rbegin()); }
		__INLINE const_reverse_iterator rend  () const { return const_reverse_iterator(m_link,container.rend()); }
		
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
543
		__INLINE StorageType      operator [] (size_type n) {assert(m_link); return StorageType(m_link,&container[n]);}
Kirill Terekhov's avatar
Kirill Terekhov committed
544
		__INLINE StorageType      operator [] (size_type n) const {assert(m_link); return StorageType(m_link,container[n]);}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
545
		__INLINE StorageType      front       () {assert(m_link); return StorageType(m_link,&container.front()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
546
		__INLINE StorageType      front       () const {assert(m_link); return StorageType(m_link,container.front()); }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
547
		__INLINE StorageType      back        ()  {assert(m_link); return StorageType(m_link,&container.back()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
548
549
550
551
552
553
554
555
		__INLINE StorageType      back        () const {assert(m_link); return StorageType(m_link,container.back()); }
		__INLINE HandleType       atfront     () const { return container.front(); }
		__INLINE HandleType       atback      () const { return container.back(); }
		__INLINE HandleType &     atfront     () { return container.front(); }
		__INLINE HandleType &     atback      () { return container.back(); }
		__INLINE HandleType &     at          (size_type n) { return container.at(n); }
		__INLINE HandleType       at          (size_type n) const { return container.at(n); }
		__INLINE void             swap        (ElementArray<StorageType> & other) {Mesh * t = m_link; m_link = other.m_link; other.m_link = t; container.swap(other.container);}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
556
		__INLINE void             push_back   (const Storage & x) {if( m_link == NULL ) m_link = x.GetMeshLink(); assert(x.GetMeshLink() == m_link); container.push_back(x.GetHandle());}
Kirill Terekhov's avatar
Kirill Terekhov committed
557
		//__INLINE void             push_back   (const StorageType & x) {container.push_back(x.GetHandle());}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
558
		__INLINE void             push_back   (HandleType x) {assert(m_link != NULL); container.push_back(x);}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
559
		__INLINE void             pop_back    () {container.pop_back();}
Kirill Terekhov's avatar
Kirill Terekhov committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
		__INLINE void             resize      (size_type n) {container.resize(n);}
		__INLINE bool             empty       () const {return container.empty();}
		__INLINE void             clear       () {container.clear();}
		__INLINE void             reserve     (size_type n) {container.reserve(n);}
		__INLINE size_type        size        () const {return container.size(); }
		__INLINE HandleType *     data        () {return container.data();}
		__INLINE const HandleType*data        () const {return container.data();}
		__INLINE Mesh *           GetMeshLink () const {assert(m_link); return m_link;}
		__INLINE void             SetMeshLink (Mesh * m) {m_link = m;}
		//implemented in mesh_array.cpp
		void                      Unite       (const HandleType * h, INMOST_DATA_ENUM_TYPE num);
		void                      Subtract    (const HandleType * h, INMOST_DATA_ENUM_TYPE num);
		void                      Intersect   (const HandleType * h, INMOST_DATA_ENUM_TYPE num);
		template<typename EType>
		void                      Unite       (const ElementArray<EType> & other) {Unite(other.data(),static_cast<INMOST_DATA_ENUM_TYPE>(other.size()));}
		template<typename EType>
		void                      Subtract    (const ElementArray<EType> & other) {Subtract(other.data(),static_cast<INMOST_DATA_ENUM_TYPE>(other.size()));}
		template<typename EType>
		void                      Intersect   (const ElementArray<EType> & other) {Intersect(other.data(),static_cast<INMOST_DATA_ENUM_TYPE>(other.size()));}
		void                      SetMarker   (MarkerType n) const;
		void                      RemMarker   (MarkerType n) const;
Kirill Terekhov's avatar
Kirill Terekhov committed
581
582
583
584
585
586
	};
	
			
	class Element : public Storage //implemented in element.cpp
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
587
588
589
590
591
592
593
594
595
596
597
598
599
600
		typedef INMOST_DATA_BULK_TYPE GeometricType;
		static const GeometricType Unset        = 0;
		static const GeometricType Vertex       = 1;
		static const GeometricType Line         = 2;
		static const GeometricType MultiLine    = 3;
		static const GeometricType Tri          = 4;
		static const GeometricType Quad         = 5;
		static const GeometricType Polygon      = 6;
		static const GeometricType MultiPolygon = 7;
		static const GeometricType Tet          = 8;
		static const GeometricType Hex          = 9;
		static const GeometricType Prism        = 10;
		static const GeometricType Pyramid      = 11;
		static const GeometricType Polyhedron   = 12;
Kirill Terekhov's avatar
Kirill Terekhov committed
601
602
603
		static const GeometricType Set          = 253;
		static const GeometricType MeshPart     = 254;
		static const GeometricType MaxType      = 255;
Kirill Terekhov's avatar
Kirill Terekhov committed
604
		//enum GeometricType {Unset,Vertex,Line,MultiLine,Tri,Quad,Polygon,MultiPolygon,Tet,Hex,Prism,Pyramid,Polyhedron,Set};
Kirill Terekhov's avatar
Kirill Terekhov committed
605
		static const char *       GeometricTypeName(GeometricType t);
Kirill Terekhov's avatar
Kirill Terekhov committed
606
		static integer            GetGeometricDimension(GeometricType m_type);
Kirill Terekhov's avatar
Kirill Terekhov committed
607
608
609
610
611
612
613
		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:
Kirill Terekhov's avatar
Kirill Terekhov committed
614
		typedef inner_reference_array            adj_type;
Kirill Terekhov's avatar
Kirill Terekhov committed
615
616
617
618
		typedef adj_type::iterator               adj_iterator;
		typedef adj_type::const_iterator         const_adj_iterator;
		typedef adj_type::reverse_iterator       adj_reverse_iterator;
		typedef adj_type::const_reverse_iterator const_adj_reverse_iterator;
Kirill Terekhov's avatar
Kirill Terekhov committed
619
620
621
	public:
		//adj_type &                  HighConn                () const;
		//adj_type &                  LowConn                 () const;
Kirill Terekhov's avatar
Kirill Terekhov committed
622
	protected:
Kirill Terekhov's avatar
Kirill Terekhov committed
623
624
625
626
		void                        SetGeometricType        (GeometricType t);
	public:
		Element() : Storage(NULL,InvalidHandle()) {}
		Element(Mesh * m, HandleType h) : Storage(m,h) {}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
627
		Element(Mesh * m, HandleType * h) : Storage(m,h) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
628
629
630
631
632
633
		Element(const Element & other) : Storage(other) {}
		Element & operator =(Element const & other) {Storage::operator =(other); return *this;}
		Element * operator ->() {return this;}
		const Element * operator ->() const {return this;}
		Element & self() {return *this;}
		const Element & self() const {return *this;}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
634
		virtual ~Element() {}
Kirill Terekhov's avatar
Kirill Terekhov committed
635
	public:
Igor Konshin's avatar
Igor Konshin committed
636
		/// Retrieve number of adjacent elements.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
637
638
		/// For etype you can either pass one type as CELL,
		/// or several types as bitwise mask: NODE | CELL.
Kirill Terekhov's avatar
Kirill Terekhov committed
639
640
641
		/// @param etype bitwise mask of element types
		/// @return number of adjacent elements.
		virtual enumerator                nbAdjElements           (ElementType etype) const;
Igor Konshin's avatar
Igor Konshin committed
642
		/// Retrieve unordered array of adjacent elements.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
643
644
645
		/// If you care about orderness of nodes for face you should you Face::getNodes() instead.
		/// If you want faster access you may use direct access to handles stored in memory
		/// through Mesh::HighConn for upper adjacencies and Mesh::LowConn for lower adjacencies.
Kirill Terekhov's avatar
Kirill Terekhov committed
646
647
648
		/// @param etype bitwise mask of element types
		/// @return array of elements
		virtual ElementArray<Element>     getAdjElements          (ElementType etype) const;  //unordered
Igor Konshin's avatar
Igor Konshin committed
649
		/// Retrieve number of adjacent elements with marker.
Kirill Terekhov's avatar
Kirill Terekhov committed
650
651
652
653
654
655
656
		/// As etype you can either pass one type as CELL,
		/// or several types as bitwise mask: NODE | CELL
		/// @param etype bitwise mask of element types
		/// @param mask marker to be set 
		/// @param invert_mask if true then those are selected on wich marker is not set
		/// @return number of adjacent elements.
		virtual enumerator                nbAdjElements           (ElementType etype, MarkerType mask, bool invert_mask = false) const;
Igor Konshin's avatar
Igor Konshin committed
657
		/// Retrieve unordered array of adjacent elements with marker.
Kirill Terekhov's avatar
Kirill Terekhov committed
658
659
660
661
662
		/// @param etype bitwise mask of element types
		/// @param mask marker to be set 
		/// @param invert_mask if true then those are selected on wich marker is not set
		/// @return array of elements
		virtual ElementArray<Element>     getAdjElements          (ElementType etype, MarkerType mask, bool invert_mask = false) const;  //unordered
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
663
664
665
666
667
		ElementArray<Element>       BridgeAdjacencies       (ElementType Bridge, ElementType Dest, MarkerType mask = 0, bool invert_mask = false) const;
		ElementArray<Node>          BridgeAdjacencies2Node  (ElementType Bridge, MarkerType mask = 0, bool invert_mask = false) const;
		ElementArray<Edge>          BridgeAdjacencies2Edge  (ElementType Bridge, MarkerType mask = 0, bool invert_mask = false) const;
		ElementArray<Face>          BridgeAdjacencies2Face  (ElementType Bridge, MarkerType mask = 0, bool invert_mask = false) const;
		ElementArray<Cell>          BridgeAdjacencies2Cell  (ElementType Bridge, MarkerType mask = 0, bool invert_mask = false) const;
Igor Konshin's avatar
Igor Konshin committed
668
		/// Retrieve all the nodes of the element.
669
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
670
		/// For a node returns itself.
671
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
672
		/// For an edge returns ordered pair of nodes. The order of nodes in the edge is preserved from the first construction.
673
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
674
675
676
		/// For a face returns ordered set of nodes. In the case Face::CheckNormalOrientation returns true the
		/// order of nodes will follow right hand side rule with respect to normal vector, otherwise it follows
		/// left hand side rule with respect to normal vector.
677
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
678
679
680
681
		/// For a cell returns the same order that was provided through suggest_nodes_oreder in Mesh::CreateCell.
		/// In the case suggest_nodes_order was not provided, the order of nodes follows VTK format for known types
		/// of elements such as Element::Tet, Element::Prism, Element::Hex, Element::Pyramid. For a general polyhedron
		/// the order is unspecified.
682
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
683
684
685
		/// @return array of elements
		/// @see Face::CheckNormalOrientation
		/// @see Face::FaceOrientedOutside
Kirill Terekhov's avatar
Kirill Terekhov committed
686
		virtual ElementArray<Node>  getNodes                () const; //unordered
Igor Konshin's avatar
Igor Konshin committed
687
		/// Retrieve all the edges of the element.
688
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
689
		/// For a node returns unordered set of edges.
690
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
691
		/// For an edge returns itself.
692
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
693
		/// For a face returns ordered set of edges.
694
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
695
		/// For a cell returns unordered set of edges.
696
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
697
		/// @return array of elements
Kirill Terekhov's avatar
Kirill Terekhov committed
698
		virtual ElementArray<Edge>  getEdges                () const; //unordered
Igor Konshin's avatar
Igor Konshin committed
699
		/// Retrieve all the faces of the element.
700
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
701
		/// For a node returns unordered set of faces.
702
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
703
		/// For an edge returns unordered set of faces.
704
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
705
		/// For a face returns itself.
706
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
707
		/// For a cell return ordered set of faces. The order of faces in the cell is preserved from the first construction.
708
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
709
		/// @return array of elements
Kirill Terekhov's avatar
Kirill Terekhov committed
710
		virtual ElementArray<Face>  getFaces                () const; //unordered
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
711
		/// Return all the cells of the element.
712
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
713
		/// For a node returns unordered set of cells.
714
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
715
		/// For an edge returns unordered set of cells.
716
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
717
718
		/// For a face returns a pair of cells. In the case Face::CheckNormalOrientation returns true
		/// then the normal points from the first cell to the second and in oppisite direction otherwise.
719
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
720
		/// For a cell returns itself.
721
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
722
723
		/// @return array of elements
		/// @see Face::FaceOrientedOutside
Kirill Terekhov's avatar
Kirill Terekhov committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
		virtual ElementArray<Cell>  getCells                () const; //unordered
		virtual ElementArray<Node>  getNodes                (MarkerType mask,bool invert_mask = false) const; //unordered
		virtual ElementArray<Edge>  getEdges                (MarkerType mask,bool invert_mask = false) const; //unordered
		virtual ElementArray<Face>  getFaces                (MarkerType mask,bool invert_mask = false) const; //unordered
		virtual ElementArray<Cell>  getCells                (MarkerType mask,bool invert_mask = false) const; //unordered
		Node                        getAsNode               () const;
		Edge                        getAsEdge               () const;
		Face                        getAsFace               () const;
		Cell                        getAsCell               () const;
		ElementSet                  getAsSet                () const;
		GeometricType               GetGeometricType        () const;
		unsigned int                GetElementDimension     () const {return GetGeometricDimension(GetGeometricType());}
		Status                      GetStatus               () const;
		void                        SetStatus               (Status status) const;
		Storage::integer &          GlobalID                () const;
		bool                        CheckElementConnectivity() const;
		void                        PrintElementConnectivity() const;
		static bool                 CheckConnectivity       (Mesh * m);
Kirill Terekhov's avatar
Kirill Terekhov committed
742
		//implemented in geometry.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
743
744
745
746
747
748
749
		void                        CastRay                 (real * pos, real * dir, dynarray< std::pair<Element, real> , 16 > & hits) const;
		void                        ComputeGeometricType    () const;
		void                        Centroid                (real * cnt) const;
		void                        Barycenter              (real * cnt) const;
		Storage::real               Mean                    (real (*func)(real* x,real t),real time) const;
		bool                        Boundary                () const;
		bool                        Planarity               () const; // check that all nodes lay on one plane
Kirill Terekhov's avatar
Kirill Terekhov committed
750
		//implemented in modify.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
751
752
753
754
755
756
		bool                        Hide                    () const; // if true then element was hidden, works only inside BeginModification and EndModification, on EndModification all Hidden elements are deleted
		bool                        Show                    () const; // if true then element was recovered
		bool                        Delete                  (); // if true then element was deleted, otherwise it was hidden
		bool                        Hidden                  () const;
		bool                        New                     () const;
		void                        Disconnect              (bool delete_upper_adjacent) const; //disconnect all elements, delete upper dependent
757
		/// Disconnect element.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
758
759
		/// Disconnects nodes from this edge, edges from this face, faces from this cell, cannot disconnect cells from this node; \n
		/// Disconnects edges from this node, faces from this edge, cells from this face, cannot disconnect nodes from this cell; \n
Kirill Terekhov's avatar
Kirill Terekhov committed
760
		/// Updates geometric data and cell nodes automatically.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
761
		void                        Disconnect              (const HandleType * adjacent, INMOST_DATA_ENUM_TYPE num) const;
Kirill Terekhov's avatar
Kirill Terekhov committed
762
763
764
		/// \brief Connects lower adjacencies to current element.
		/// Geometric data and cell nodes are updated automatically.
		///
765
		/// \todo
Kirill Terekhov's avatar
Kirill Terekhov committed
766
767
768
		///	  1. Asserts in this function should be replaced by Topography checks.
		///	  2. This function should be used for construction of elements instead of current implementation.
		///	  3. Should correctly account for order of edges (may be implemented through CheckEdgeOrder, FixEdgeOrder).
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
769
		void                        Connect                 (const HandleType * adjacent, INMOST_DATA_ENUM_TYPE num) const; 
Kirill Terekhov's avatar
Kirill Terekhov committed
770
		/// Update geometric data for element, calls RecomputeGeometricData from Mesh.
Kirill Terekhov's avatar
Kirill Terekhov committed
771
		void                        UpdateGeometricData     () const; 
Kirill Terekhov's avatar
Kirill Terekhov committed
772
773
	};
	
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
774
775
	__INLINE const Element & InvalidElement() {static Element ret(NULL,InvalidHandle()); return ret;}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
776
777
778
	class Node : public Element //implemented in node.cpp
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
779
780
781
		Node() : Element() {}
		Node(const Node & other) : Element(other) {}
		Node(Mesh * m, HandleType h) : Element(m,h) {}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
782
		Node(Mesh * m, HandleType * h) : Element(m,h) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
783
784
785
786
787
788
789
790
791
		Node & operator =(Node const & other) {Element::operator =(other); return *this;}
		Node * operator ->() {return this;}
		const Node * operator ->() const {return this;}
		Node & self() {return *this;}
		const Node & self() const {return *this;}
	public:
		ElementArray<Edge>          getEdges                () const; //unordered
		ElementArray<Face>          getFaces                () const; //unordered
		ElementArray<Cell>          getCells                () const; //unordered
Kirill Terekhov's avatar
Kirill Terekhov committed
792

Kirill Terekhov's avatar
Kirill Terekhov committed
793
794
795
		ElementArray<Edge>          getEdges                (MarkerType mask,bool invert_mask = false) const; //unordered
		ElementArray<Face>          getFaces                (MarkerType mask,bool invert_mask = false) const; //unordered
		ElementArray<Cell>          getCells                (MarkerType mask,bool invert_mask = false) const; //unordered
Kirill Terekhov's avatar
Kirill Terekhov committed
796

Kirill Terekhov's avatar
Kirill Terekhov committed
797
		Storage::real_array         Coords                  () const; 
Kirill Terekhov's avatar
Kirill Terekhov committed
798
	};
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
799
800

	__INLINE const Node & InvalidNode() {static Node ret(NULL,InvalidHandle()); return ret;}
Kirill Terekhov's avatar
Kirill Terekhov committed
801
802
803
804
	
	class Edge : public Element //implemented in edge.cpp
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
805
806
807
		Edge() : Element() {}
		Edge(const Edge & other) : Element(other) {}
		Edge(Mesh * m, HandleType h) : Element(m,h) {}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
808
		Edge(Mesh * m, HandleType * h) : Element(m,h) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
809
810
811
812
813
814
		Edge & operator =(Edge const & other) {Element::operator =(other); return *this;}
		Edge * operator ->() {return this;}
		const Edge * operator ->() const {return this;}
		Edge & self() {return *this;}
		const Edge & self() const {return *this;}
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
815
		
Kirill Terekhov's avatar
Kirill Terekhov committed
816
817
818
		ElementArray<Node>          getNodes                () const; //ordered
		ElementArray<Face>          getFaces                () const; //unordered
		ElementArray<Cell>          getCells                () const; //unordered
Kirill Terekhov's avatar
Kirill Terekhov committed
819

Kirill Terekhov's avatar
Kirill Terekhov committed
820
821
822
		ElementArray<Node>          getNodes                (MarkerType mask,bool invert_mask = false) const; //ordered
		ElementArray<Face>          getFaces                (MarkerType mask,bool invert_mask = false) const; //unordered
		ElementArray<Cell>          getCells                (MarkerType mask,bool invert_mask = false) const; //unordered
Kirill Terekhov's avatar
Kirill Terekhov committed
823

Kirill Terekhov's avatar
Kirill Terekhov committed
824
825
		Node                        getBeg                  () const;
		Node                        getEnd                  () const;
Kirill Terekhov's avatar
Kirill Terekhov committed
826
		//implemented in modify.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
827
828
829
830
		static Edge                 UniteEdges              (ElementArray<Edge> & edges, MarkerType del_protect);
		static bool                 TestUniteEdges          (const ElementArray<Edge> & edges, MarkerType del_protect);
		static ElementArray<Edge>   SplitEdge               (Edge e, const ElementArray<Node> & nodes, MarkerType del_protect); //provide ordered array of nodes, that lay between former nodes of the edge
		static bool                 TestSplitEdge           (Edge e, const ElementArray<Node> & nodes, MarkerType del_protect);
Kirill Terekhov's avatar
Kirill Terekhov committed
831
		//implemented in geometry.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
832
		Storage::real               Length                  () const;
Kirill Terekhov's avatar
Kirill Terekhov committed
833
	};
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
834
835

	__INLINE const Edge & InvalidEdge() {static Edge ret(NULL,InvalidHandle()); return ret;}
Kirill Terekhov's avatar
Kirill Terekhov committed
836
	
Kirill Terekhov's avatar
Kirill Terekhov committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857

	/// \brief An interface for elements of type FACE.
	///
	/// This interface carry the link to the mesh and the element's handle that
	/// represents position of the element's data in the mesh. 
	///
	/// Interface provides some operations that can be done uniquely on the object of class 
	/// Element for which Element::GetElementType retruns FACE.
	///
	/// For the basic set of operations on all of the elements check class Element.
	///
	/// For the basic set of operations on the data of the element check class Storage.
	///
	/// You can obtain object of class Face from Mesh::iteratorFace,
	/// in this case obtained object is always valid.
	/// Also you can get it through Mesh::FaceByLocalID, check with Element::isValid to see whether you
	/// have obtained a valid object.
	/// You can convert an object of class Element into an object of class Face by Element::getAsFace. In debug
	/// mode it will internally check that the element is of type FACE.
	/// You may compose an object of class Face by using constructor and specifing pointer to the mesh
	/// and providing element's handle. You can make a handle with ComposeHandle(FACE,local_id) function.
Kirill Terekhov's avatar
Kirill Terekhov committed
858
859
860
	class Face : public Element //implemented in face.cpp
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
861
862
863
		Face() : Element() {}
		Face(const Face & other) : Element(other) {}
		Face(Mesh * m, HandleType h) : Element(m,h) {}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
864
		Face(Mesh * m, HandleType * h) : Element(m,h) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
865
866
867
868
869
870
		Face & operator =(Face const & other) {Element::operator =(other); return *this;}
		Face * operator ->() {return this;}
		const Face * operator ->() const {return this;}
		Face & self() {return *this;}
		const Face & self() const {return *this;}
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
871
		
Kirill Terekhov's avatar
Kirill Terekhov committed
872
873
874
		ElementArray<Node>          getNodes                () const; //ordered
		ElementArray<Edge>          getEdges                () const; //ordered
		ElementArray<Cell>          getCells                () const; //unordered
Kirill Terekhov's avatar
Kirill Terekhov committed
875

Kirill Terekhov's avatar
Kirill Terekhov committed
876
877
878
		ElementArray<Node>          getNodes                (MarkerType mask,bool invert_mask = false) const; //ordered
		ElementArray<Edge>          getEdges                (MarkerType mask,bool invert_mask = false) const; //ordered
		ElementArray<Cell>          getCells                (MarkerType mask,bool invert_mask = false) const; //unordered
Kirill Terekhov's avatar
Kirill Terekhov committed
879
880

		//this is for 2d case when the face is represented by segment
Kirill Terekhov's avatar
Kirill Terekhov committed
881
882
		Node                        getBeg                  () const;
		Node                        getEnd                  () const;
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
883
884
		/// \brief Retrieve the cell for which the normal points outwards.
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
885
		/// \warning Depending on the grid construction the normal may incorrectly point inwards.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
886
		/// You can resolve this situation by Face::FixNormalOrientation.
Kirill Terekhov's avatar
Kirill Terekhov committed
887
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
888
889
		/// @return the cell for which normal points outwards.
		/// @see Face::FixNormalOrientation
Kirill Terekhov's avatar
Kirill Terekhov committed
890
		Cell                        BackCell                () const;
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
891
892
		/// \brief Retrieve the cell for which the normal points inwards.
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
893
		/// \warning Depending on the grid construction the normal may incorrectly point outwards.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
894
		/// You can resolve this situation by Face::FixNormalOrientation.
Kirill Terekhov's avatar
Kirill Terekhov committed
895
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
896
897
		/// @return the cell for which normal points inwards.
		/// @see Face::FixNormalOrientation
Kirill Terekhov's avatar
Kirill Terekhov committed
898
899
900
901
902
		Cell                        FrontCell               () const;
		bool                        FaceOrientedOutside     (Cell c) const;
		void                        ReorderEdges            () const;
		bool                        CheckEdgeOrder          () const; //not implemented// returns true if edges of face form an ordered closed loop
		bool                        FixEdgeOrder            () const; //not implemented// returns true if edges were successfully reordered to form a closed loop
Kirill Terekhov's avatar
Kirill Terekhov committed
903
		//implemented in modify.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
904
905
906
907
908
		static Face                 UniteFaces              (ElementArray<Face> & faces, MarkerType del_protect);
		static bool                 TestUniteFaces          (const ElementArray<Face> & faces,  MarkerType del_protect);
		static ElementArray<Face>   SplitFace               (Face face, const ElementArray<Edge> & edges, MarkerType del_protect); //provide all edges that lay inside face
		static bool                 TestSplitFace           (Face face, const ElementArray<Edge> & edges, MarkerType del_protect);	
		void                        SwapCells               (); //swap back cell and front cell
Kirill Terekhov's avatar
Kirill Terekhov committed
909
		//implemented in geometry.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
910
911
912
913
914
915
916
917
		Storage::real               Area                    () const;
		void                        Normal                  (real * nrm) const;
		void                        UnitNormal              (real * nrm) const;
		void                        OrientedNormal          (Cell c, real * nrm) const;
		void                        OrientedUnitNormal      (Cell c, real * nrm) const;
		bool                        FixNormalOrientation    () const;  //returns true if orientation was corrected, otherwise returns false
		bool                        CheckNormalOrientation  () const; //returns true if orientation is correct, otherwise returns false
		bool                        Closure                 () const; // test integrity of polygon
Kirill Terekhov's avatar
Kirill Terekhov committed
918
	};
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
919
920

	__INLINE const Face & InvalidFace() {static Face ret(NULL,InvalidHandle()); return ret;}
Kirill Terekhov's avatar
Kirill Terekhov committed
921
	
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
922
923
924
925
926
927
	//implemented in cell.cpp
	/// \brief An interface for elements of type CELL.
	///
	/// This interface carry the link to the mesh and the element's handle that
	/// represents position of the element's data in the mesh. 
	///
Kirill Terekhov's avatar
Kirill Terekhov committed
928
929
	/// Interface provides some operations that can be done uniquely on the object of class 
	/// Element for which Element::GetElementType retruns CELL.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
930
931
932
933
934
935
936
937
938
	///
	/// For the basic set of operations on all of the elements check class Element.
	///
	/// For the basic set of operations on the data of the element check class Storage.
	///
	/// You can obtain object of class Cell from Mesh::iteratorCell,
	/// in this case obtained object is always valid.
	/// Also you can get it through Mesh::CellByLocalID, check with Element::isValid to see whether you
	/// have obtained valid object.
Kirill Terekhov's avatar
Kirill Terekhov committed
939
940
941
942
	/// You can convert an object of class Element into an object of class Cell by Element::getAsCell. In debug
	/// mode it will internally check that the element is of type CELL.
	/// You may compose an object of class Cell by using constructor and specifing pointer to the mesh
	/// and providing element's handle. You can make a handle with ComposeHandle(CELL,local_id) function.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
943
	class Cell : public Element
Kirill Terekhov's avatar
Kirill Terekhov committed
944
945
	{
	public:
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
946
947
948
949
950
951
952
953
954
955
		/// \brief Basic constructor.
		///
		/// Constructs invalid cell.
		Cell() : Element() {} 
		/// \brief Basic constructor with fixed handle.
		///
		/// When constructed this way, only handle within object may change.
		///
		/// @param m Pointer to the mesh to which the element belongs.
		/// @param h Unique handle that describes position of the element within the mesh.
Kirill Terekhov's avatar
Kirill Terekhov committed
956
		Cell(Mesh * m, HandleType h) : Element(m,h) {}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
957
958
959
960
961
		/// \brief Basic constructor with an assignable handle.
		///
		/// When constructed this way, the provided memory location for handle
		/// will be modified on assignment.
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
962
		/// The purpose of this function is to be used in various non-constant iterators
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
963
964
965
966
		/// of containers that allow underlaying contents to be changed.
		///
		/// @param m Pointer to the mesh to which the element belongs.
		/// @param h Pointer to the handle that describes position of the element within mesh.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
967
		Cell(Mesh * m, HandleType * h) : Element(m,h) {}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
968
969
970
971
972
		/// \brief Copy constructor.
		///
		/// New object will inherit the assignment behavior of the initial object.
		///
		/// @param other Object of type Cell to be duplicated.
Kirill Terekhov's avatar
Kirill Terekhov committed
973
		Cell(const Cell & other) : Element(other) {}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
		/// \brief Assignment operator
		///
		/// Assigned object will inherit the asignment behavior of the initial object only
		/// in case it was constructed without pointer to a handle. Otherwise it will
		/// only modify the memory location to which it points.
		///
		/// @param other Object of type Cell to be duplicated.
		/// @return Reference to the current object.
		Cell & operator =(Cell const & other) {Element::operator =(other); return *this;} ///< Assignment Operator.
		/// \brief Operator of dereference to pointer.
		///
		/// This is needed for iterators to work properly.
		///
		/// @return Pointer to the current object.
		Cell * operator ->() {return this;} 
		/// \brief Operator of dereference to constant pointer. 
		///
		/// This is needed for const_iterators to work properly.
		///
		/// @return Constant pointer to the current object.
Kirill Terekhov's avatar
Kirill Terekhov committed
994
		const Cell * operator ->() const {return this;}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
995
996
997
998
999
1000
1001
1002
1003
1004
1005
		/// \brief Get self-reference.
		///
		/// Main purpose is to convert iterators into elements and to pass them as arguments of functions.
		///
		/// @return Constant reference to the current object.
		Cell & self() {return *this;} 
		/// \brief Get constant self-reference.
		///
		/// Main purpose is to convert iterators into elements and to pass them as arguments of functions.
		///
		/// @return Reference to the current object.
Kirill Terekhov's avatar
Kirill Terekhov committed
1006
1007
		const Cell & self() const {return *this;}
	public:
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1008
1009
1010
1011
		/// \brief Get all the nodes of the current cell.
		///
		/// This function traverses up the adjacency graph by one level.
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
1012
		/// The order of nodes will be preserved from the moment of the construction of the cell.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
		/// When suggest_nodes array is not supplied into the Mesh::CreateCell functions, then
		/// for known types the order of nodes follows VTK convention. For polyhedral cells
		/// the order is unspecified. When suggest_nodes_order was provided into Mesh::CreateCell
		/// then the order follows provided order.
		///
		/// @return Set of nodes that compose current cell.
		ElementArray<Node>          getNodes                () const;
		/// \brief Get all the edges of the current cell.
		///
		/// This function traverses down the adjacency graph by two levels.
		///
		/// The order of the edges is unspecified.
		///
		/// @return Set of edges that compose current cell.
		///
		/// \todo
		/// One should thoroughly check three scenarios of function execution in shared parallel environment
		/// for different types of cells (simple tet/hex cells as well as complex polyhedral cells) and
		/// draw a conclusion on the best scenario for each condition. One of the development versions
		/// contains all the algorithms, ask for the files.
		///  1. Use of markers (current wariant).
		///  2. Put all elements into array with duplications, then run std::sort and std::unique.
		///  3. Put all elements into array, check for duplication by running through array.
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
1037
1038
1039
1040
1041
1042
		///
		/// \warning Note that this function uses markers to check for duplication of edges
		/// in output array. This allows for faster algorithm, but will lead to penalties
		/// in shared parallel execution, since operations on markers are declared atomic.
		/// For atomic declaration to work you have to define USE_OMP during CMake configuration
		/// or in inmost_common.h. If you attempt to use this function in shared parallel execution
		/// without USE_OMP you may expect side effects.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1043
1044
1045
1046
1047
1048
		ElementArray<Edge>          getEdges                () const;
		/// \brief Get all the faces of the current cell.
		///
		/// This function traverses down the adjacency graph by one level.
		///
		/// The order of the faces returned by this function is preserved from 
Kirill Terekhov's avatar
Kirill Terekhov committed
1049
		/// the moment of the construction of the cell.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1050
1051
1052
1053
1054
1055
1056
		///
		/// @return Set of faces that compose current cell.
		ElementArray<Face>          getFaces                () const;
		/// \brief Get the subset of the nodes of the current cell that are (not) marked by provided marker.
		///
		/// This function traverses up the adjacency graph by one level.
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
1057
		/// The order of nodes will be preserved from the moment of the construction of the cell.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1058
1059
1060
1061
1062
1063
1064
1065
1066
		/// When suggest_nodes array is not supplied into the Mesh::CreateCell functions, then
		/// for known types the order of nodes follows VTK convention. For polyhedral cells
		/// the order is unspecified. When suggest_nodes_order was provided into Mesh::CreateCell
		/// then the order follows provided order.
		///
		/// @param mask Marker that should be used to filter elements.
		/// @param invert_mask If false then those elements that are marked will be taken, otherwise
		///                    elements that are not marked will be taken.
		/// @return Set of the nodes that compose current cell and (not) marked by marker.
Kirill Terekhov's avatar
Kirill Terekhov committed
1067
1068
1069
		///
		/// \warning To work correctly in shared parallel environment this function requires
		/// USE_OMP to be defined in CMake or in inmost_common.h.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
		ElementArray<Node>          getNodes                (MarkerType mask,bool invert_mask = false) const;
		/// \brief Get the subset of the edges of the current cell that are (not) marked by provided marker.
		///
		/// This function traverses down the adjacency graph by two levels.
		///
		/// The order of the edges is unspecified.
		///
		/// @param mask Marker that should be used to filter elements.
		/// @param invert_mask If false then those elements that are marked will be taken, otherwise
		///                    elements that are not marked will be taken.
		/// @return Set of the edges that compose current cell and (not) marked by marker.
Kirill Terekhov's avatar
Kirill Terekhov committed
1081
1082
1083
		///
		/// \warning To work correctly in shared parallel environment this function requires
		/// USE_OMP to be defined in CMake or in inmost_common.h.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1084
1085
1086
1087
1088
1089
		ElementArray<Edge>          getEdges                (MarkerType mask,bool invert_mask = false) const;
		/// \brief Get the subset of the faces of the current cell that are (not) marked by provided marker.
		///
		/// This function traverses down the adjacency graph by one level.
		///
		/// The order of the faces returned by this function is preserved from 
Kirill Terekhov's avatar
Kirill Terekhov committed
1090
		/// the moment of the construction of the cell.