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

Kirill Terekhov's avatar
Kirill Terekhov committed
2
3
4
5
#ifndef INMOST_MESH_H_INCLUDED
#define INMOST_MESH_H_INCLUDED

#include "inmost_common.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
6
#include "inmost_data.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
7
8
9

#if defined(USE_MESH)

Kirill Terekhov's avatar
Kirill Terekhov committed
10

Kirill Terekhov's avatar
Kirill Terekhov committed
11
12
13
14
15
16
17
18
19
20
21

namespace INMOST
{
	class Mesh;
	class Storage;
	class Element;
	class TagManager;
	class Node;
	class Edge;
	class Face;
	class Cell;
Kirill Terekhov's avatar
Kirill Terekhov committed
22
	class ElementSet;
Kirill Terekhov's avatar
Kirill Terekhov committed
23

Kirill Terekhov's avatar
Kirill Terekhov committed
24

Kirill Terekhov's avatar
Kirill Terekhov committed
25
26
	
	typedef INMOST_DATA_BULK_TYPE GeometricData;
Kirill Terekhov's avatar
Kirill Terekhov committed
27
28
29
30
31
	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
32
	
Kirill Terekhov's avatar
Kirill Terekhov committed
33
	typedef INMOST_DATA_BULK_TYPE SyncBitOp; //< This type is used for marker synchronization
Kirill Terekhov's avatar
Kirill Terekhov committed
34
	static const SyncBitOp SYNC_BIT_SET = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
35
36
37
38
	static const SyncBitOp SYNC_BIT_OR  = 1;
	static const SyncBitOp SYNC_BIT_XOR = 2;
	static const SyncBitOp SYNC_BIT_AND = 3;

39
    
Kirill Terekhov's avatar
Kirill Terekhov committed
40

Kirill Terekhov's avatar
Kirill Terekhov committed
41
	///Definition for data type of topology error or event. This type may be extended later to 64 bits
Kirill Terekhov's avatar
Kirill Terekhov committed
42
43
44
45
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
	/// to accomodate more topological errors or events.
	typedef INMOST_DATA_ENUM_TYPE         TopologyCheck;
	///Throw TopologyError exception on error.
	static const TopologyCheck            THROW_EXCEPTION        = 0x00000001;
	///Print topology notify to std::cerr.
	static const TopologyCheck            PRINT_NOTIFY           = 0x00000002;
	///Element should be deleted if there is an error in EndTopologyCheck.
	static const TopologyCheck            DELETE_ON_ERROR        = 0x00000004;
	///Bitwise OR of errors is recorded to sparse integer tag of element (if not deleted), availible
	/// by TopologyErrorTag().
	static const TopologyCheck            MARK_ON_ERROR          = 0x00000008;
	///Check that edge already exists, on occurance CreateEdge silently returns existant edge.
	static const TopologyCheck            DUPLICATE_EDGE         = 0x00000010;
	///Check that face already exists, on occurance CreateFace silently returns existant face.
	static const TopologyCheck            DUPLICATE_FACE         = 0x00000020;
	///Check that cell already exists, on occurance CreateCell silently returns existant cell.
	static const TopologyCheck            DUPLICATE_CELL         = 0x00000040;
	///Check that edge have more then two nodes (no support for this kind of object).
	static const TopologyCheck            DEGENERATE_EDGE        = 0x00000080;
	///Produce error if face consists of less then 3 edges.
	static const TopologyCheck            DEGENERATE_FACE        = 0x00000100;
	///Produce error if cell consists of less then 4 faces.
	static const TopologyCheck            DEGENERATE_CELL        = 0x00000200;
	///Produce error if face have wrong orientation of edges, for star-shaped elements only.
	static const TopologyCheck            FACE_ORIENTATION       = 0x00000400;
	///Produce error if face is non planar.
	static const TopologyCheck            FACE_PLANARITY         = 0x00000800;
	///Produce error if there is another face that already uses same nodes.
	static const TopologyCheck            INTERLEAVED_FACES      = 0x00001000;
	///Check that every face have exectly two neighbours, if DUPLICATE_CELL is activated, then checks
	/// for cell duplication.
Kirill Terekhov's avatar
Kirill Terekhov committed
73
	static const TopologyCheck            TRIPLE_SHARED_FACE     = 0x00002000;
Kirill Terekhov's avatar
Kirill Terekhov committed
74
	///Produce error if one of the faces of the cell contains all the nodes of the cell.
Kirill Terekhov's avatar
Kirill Terekhov committed
75
	static const TopologyCheck            FLATTENED_CELL         = 0x00004000;
Kirill Terekhov's avatar
Kirill Terekhov committed
76
	///Produce error if provided array of elements for construction contain duplications.
Kirill Terekhov's avatar
Kirill Terekhov committed
77
	static const TopologyCheck            ADJACENT_DUPLICATE     = 0x00008000;
Kirill Terekhov's avatar
Kirill Terekhov committed
78
79
80
81
82
83
84
85
	///Hidden elements should not be used when new elements are created.
	static const TopologyCheck            ADJACENT_HIDDEN        = 0x00010000;
	///Check that all handles are valid.
	static const TopologyCheck            ADJACENT_VALID         = 0x00020000;
	///Produce error if provided array of elements have wrong geometric dimension.
	static const TopologyCheck            ADJACENT_DIMENSION     = 0x00040000;
	///Produce error if edges of faces are not closed, or face is folded.
	/// \warning This check needs NEED_TEST_CLOSURE to be activated.
Kirill Terekhov's avatar
Kirill Terekhov committed
86
	static const TopologyCheck            PROHIBIT_MULTILINE     = 0x00080000;
Kirill Terekhov's avatar
Kirill Terekhov committed
87
	/// Allow only known types of elements: Tet, Quad, Line.
Kirill Terekhov's avatar
Kirill Terekhov committed
88
	static const TopologyCheck            PROHIBIT_POLYGON       = 0x00100000;
Kirill Terekhov's avatar
Kirill Terekhov committed
89
90
91
92
93
94
95
96
97
98
99
100
	///Produce error if faces of cell are not closed, or cell is folded.
	/// \warning This check needs NEED_TEST_CLOSURE to be activated.
	static const TopologyCheck            PROHIBIT_MULTIPOLYGON  = 0x00200000;
	///Allow only known types of elements: Tet,Hex,Prism,Pyramid.
	static const TopologyCheck            PROHIBIT_POLYHEDRON    = 0x00400000;
	///Edges of the face should form one closed loop.
	static const TopologyCheck            FACE_EDGES_ORDER       = 0x00800000;
	///Don't allow concave face.
	/// \warning Not implemented.
	static const TopologyCheck            PROHIBIT_CONCAVE_FACE  = 0x01000000;
	///Don't allow concave cell.
	/// \warning Not implemented.
Kirill Terekhov's avatar
Kirill Terekhov committed
101
	static const TopologyCheck            PROHIBIT_CONCAVE_CELL  = 0x02000000;
Kirill Terekhov's avatar
Kirill Terekhov committed
102
103
	///Don't allow non-star shaped face.
	/// \warning Not implemented.
Kirill Terekhov's avatar
Kirill Terekhov committed
104
	static const TopologyCheck            PROHIBIT_NONSTAR_FACE  = 0x04000000;
Kirill Terekhov's avatar
Kirill Terekhov committed
105
106
	///Don't allow non-star shaped concave cell.
	/// \warning Not implemented.
Kirill Terekhov's avatar
Kirill Terekhov committed
107
	static const TopologyCheck            PROHIBIT_NONSTAR_CELL  = 0x08000000;
Kirill Terekhov's avatar
Kirill Terekhov committed
108
109
	///Edges of the face don't cross each other.
	/// \warning Not implemented.
Kirill Terekhov's avatar
Kirill Terekhov committed
110
	static const TopologyCheck            FACE_SELF_INTERSECTION = 0x10000000;
Kirill Terekhov's avatar
Kirill Terekhov committed
111
112
113
114
115
116
117
	///Faces of the cell don't cross each other.
	/// \warning Not implemented.
	static const TopologyCheck            CELL_SELF_INTERSECTION = 0x20000000;
	///Silent, test's for closure in ComputeGeometricType, needed to detect MultiLine and
	/// MultiPolygon.
	static const TopologyCheck            NEED_TEST_CLOSURE      = 0x40000000;
	///Don't allow 2d grids, where edges appear to be vertexes, faces are edges and cells are faces
Kirill Terekhov's avatar
Kirill Terekhov committed
118
	static const TopologyCheck            DISABLE_2D             = 0x80000000;
Kirill Terekhov's avatar
Kirill Terekhov committed
119
	///A shortcut to test for grid conformity.
Kirill Terekhov's avatar
Kirill Terekhov committed
120
121
122
123
124
	static const TopologyCheck            GRID_CONFORMITY        = NEED_TEST_CLOSURE 
                                                               | PROHIBIT_MULTILINE 
                                                               | PROHIBIT_MULTIPOLYGON  
                                                               | INTERLEAVED_FACES 
                                                               | TRIPLE_SHARED_FACE;
Kirill Terekhov's avatar
Kirill Terekhov committed
125
	///Default set of options.
Kirill Terekhov's avatar
Kirill Terekhov committed
126
127
128
129
130
	static const TopologyCheck            DEFAULT_CHECK          = THROW_EXCEPTION 
                                                               | DUPLICATE_EDGE 
                                                               | DUPLICATE_FACE 
                                                               | DUPLICATE_CELL 
                                                               | PRINT_NOTIFY;
Kirill Terekhov's avatar
Kirill Terekhov committed
131
132
	///Returns a string explaining each topology check. Implemented in mesh.cpp.
	/// @param c Single topology check.
Kirill Terekhov's avatar
Kirill Terekhov committed
133
134
135
	const char * TopologyCheckNotifyString(TopologyCheck c);
  
 
Kirill Terekhov's avatar
Kirill Terekhov committed
136
	
Kirill Terekhov's avatar
Kirill Terekhov committed
137
138
	template <typename StorageType>
	class ElementArray
Kirill Terekhov's avatar
Kirill Terekhov committed
139
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
140
	public:
141
		typedef std::vector<HandleType>      cont_t;
Kirill Terekhov's avatar
Kirill Terekhov committed
142
		typedef cont_t::size_type            size_type;
Kirill Terekhov's avatar
Kirill Terekhov committed
143
	private:
Kirill Terekhov's avatar
Kirill Terekhov committed
144
145
		Mesh *                               m_link;
		cont_t                               container;
Kirill Terekhov's avatar
Kirill Terekhov committed
146
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
147
148
149
150
151
152
153
154
155
156
157
158
		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
159
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
160
			Mesh * m_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
161
		public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
162
163
			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
164
			ptrdiff_t     operator -(const iterator & other) const {return static_cast<const cont_t::iterator>(*this)-static_cast<const cont_t::iterator>(other);}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
165
166
			iterator      operator +(size_t n) const{return iterator(m_link,cont_t::iterator::operator +(n));}
			iterator      operator -(size_t n) const{return iterator(m_link,cont_t::iterator::operator -(n));}
Kirill Terekhov's avatar
Kirill Terekhov committed
167
168
169
170
171
			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; }
172
173
			HandleType &  operator *() const { return cont_t::iterator::operator *(); }
			StorageType   operator->() const { return StorageType(m_link,&cont_t::iterator::operator *()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
174
		};
Kirill Terekhov's avatar
Kirill Terekhov committed
175
		class reverse_iterator : public cont_t::reverse_iterator
Kirill Terekhov's avatar
Kirill Terekhov committed
176
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
177
			Mesh * m_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
178
		public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
179
180
			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
181
			ptrdiff_t             operator -(const reverse_iterator & other) const {return static_cast<const cont_t::reverse_iterator>(*this)-static_cast<const cont_t::reverse_iterator>(other);}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
182
183
			reverse_iterator      operator +(size_t n) const {return reverse_iterator(m_link,cont_t::reverse_iterator::operator+(n));}
			reverse_iterator      operator -(size_t n) const {return reverse_iterator(m_link,cont_t::reverse_iterator::operator-(n));}
Kirill Terekhov's avatar
Kirill Terekhov committed
184
185
186
187
188
			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; }
189
190
			HandleType &       operator *() const { return cont_t::reverse_iterator::operator *(); }
			StorageType        operator->() const { return StorageType(m_link,&cont_t::reverse_iterator::operator *()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
191
		};
Kirill Terekhov's avatar
Kirill Terekhov committed
192
		class const_iterator : public cont_t::const_iterator
Kirill Terekhov's avatar
Kirill Terekhov committed
193
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
194
			Mesh * m_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
195
		public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
196
197
			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
198
			ptrdiff_t     operator -(const const_iterator & other) const {return static_cast<const cont_t::const_iterator>(*this)-static_cast<const cont_t::const_iterator>(other);}
Kirill Terekhov's avatar
Kirill Terekhov committed
199
200
201
202
203
			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; }
204
205
			const HandleType &  operator *() const { return cont_t::const_iterator::operator *(); }
			StorageType         operator->() const { return StorageType(m_link,cont_t::const_iterator::operator *()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
206
		};
Kirill Terekhov's avatar
Kirill Terekhov committed
207
		class const_reverse_iterator : public cont_t::const_reverse_iterator
Kirill Terekhov's avatar
Kirill Terekhov committed
208
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
209
			Mesh * m_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
210
		public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
211
212
			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
213
			ptrdiff_t     operator -(const const_reverse_iterator & other) const {return static_cast<const cont_t::const_reverse_iterator>(*this)-static_cast<const cont_t::const_reverse_iterator>(other);}
Kirill Terekhov's avatar
Kirill Terekhov committed
214
215
216
217
218
			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; }
219
220
			const HandleType &       operator *() const { return cont_t::const_reverse_iterator::operator *(); }
			StorageType              operator->() const { return StorageType(m_link,cont_t::const_reverse_iterator::operator *()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
221
		};
Kirill Terekhov's avatar
Kirill Terekhov committed
222
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
223
		template<class InputIterator>
Kirill Terekhov's avatar
Kirill Terekhov committed
224
225
226
227
228
229
230
231
232
233
234
235
		__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
236
		__INLINE StorageType      operator [] (size_type n) {assert(m_link); return StorageType(m_link,&container[n]);}
Kirill Terekhov's avatar
Kirill Terekhov committed
237
		__INLINE StorageType      operator [] (size_type n) const {assert(m_link); return StorageType(m_link,container[n]);}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
238
		__INLINE StorageType      front       () {assert(m_link); return StorageType(m_link,&container.front()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
239
		__INLINE StorageType      front       () const {assert(m_link); return StorageType(m_link,container.front()); }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
240
		__INLINE StorageType      back        ()  {assert(m_link); return StorageType(m_link,&container.back()); }
Kirill Terekhov's avatar
Kirill Terekhov committed
241
242
243
244
245
246
247
248
		__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
249
		__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
250
		//__INLINE void             push_back   (const StorageType & x) {container.push_back(x.GetHandle());}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
251
		__INLINE void             push_back   (HandleType x) {assert(m_link != NULL); container.push_back(x);}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
252
		__INLINE void             pop_back    () {container.pop_back();}
Kirill Terekhov's avatar
Kirill Terekhov committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
		__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
274
		void                      SetPrivateMarker   (MarkerType n) const;
Kirill Terekhov's avatar
Kirill Terekhov committed
275
		void                      RemPrivateMarker   (MarkerType n) const;
Kirill Terekhov's avatar
Kirill Terekhov committed
276
277
		template<typename Etype>
		ElementArray<Etype>       Convert() {return ElementArray<Etype>(m_link,container);}
Kirill Terekhov's avatar
Kirill Terekhov committed
278
279
280
281
282
283
	};
	
			
	class Element : public Storage //implemented in element.cpp
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
		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
298
299
300
		static const GeometricType Set          = 253;
		static const GeometricType MeshPart     = 254;
		static const GeometricType MaxType      = 255;
Kirill Terekhov's avatar
Kirill Terekhov committed
301
		//enum GeometricType {Unset,Vertex,Line,MultiLine,Tri,Quad,Polygon,MultiPolygon,Tet,Hex,Prism,Pyramid,Polyhedron,Set};
Kirill Terekhov's avatar
Kirill Terekhov committed
302
		static const char *       GeometricTypeName(GeometricType t);
Kirill Terekhov's avatar
Kirill Terekhov committed
303
		static integer            GetGeometricDimension(GeometricType m_type);
Kirill Terekhov's avatar
Kirill Terekhov committed
304
305
306
307
308
309
310
		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
311
		typedef inner_reference_array            adj_type;
Kirill Terekhov's avatar
Kirill Terekhov committed
312
313
314
315
		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
316
317
318
	public:
		//adj_type &                  HighConn                () const;
		//adj_type &                  LowConn                 () const;
Kirill Terekhov's avatar
Kirill Terekhov committed
319
	protected:
Kirill Terekhov's avatar
Kirill Terekhov committed
320
321
322
323
		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
324
		Element(Mesh * m, HandleType * h) : Storage(m,h) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
325
326
327
328
329
330
		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
331
		virtual ~Element() {}
Kirill Terekhov's avatar
Kirill Terekhov committed
332
	public:
Igor Konshin's avatar
Igor Konshin committed
333
		/// Retrieve number of adjacent elements.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
334
335
		/// 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
336
337
338
		/// @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
339
		/// Retrieve unordered array of adjacent elements.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
340
341
342
		/// 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
343
344
345
		/// @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
346
		/// Retrieve number of adjacent elements with marker.
Kirill Terekhov's avatar
Kirill Terekhov committed
347
348
349
350
351
352
353
		/// 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
354
		/// Retrieve unordered array of adjacent elements with marker.
Kirill Terekhov's avatar
Kirill Terekhov committed
355
356
357
358
359
		/// @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
360
361
362
363
364
		ElementArray<Element>       BridgeAdjacencies       (ElementType Bridge, ElementType Dest, MarkerType bridge_mask = 0, bool bridge_invert = false, MarkerType target_mask = 0, bool target_invert = false) const;
		ElementArray<Node>          BridgeAdjacencies2Node  (ElementType Bridge, MarkerType bridge_mask = 0, bool bridge_invert = false, MarkerType target_mask = 0, bool target_invert = false) const;
		ElementArray<Edge>          BridgeAdjacencies2Edge  (ElementType Bridge, MarkerType bridge_mask = 0, bool bridge_invert = false, MarkerType target_mask = 0, bool target_invert = false) const;
		ElementArray<Face>          BridgeAdjacencies2Face  (ElementType Bridge, MarkerType bridge_mask = 0, bool bridge_invert = false, MarkerType target_mask = 0, bool target_invert = false) const;
		ElementArray<Cell>          BridgeAdjacencies2Cell  (ElementType Bridge, MarkerType bridge_mask = 0, bool bridge_invert = false, MarkerType target_mask = 0, bool target_invert = false) const;
Igor Konshin's avatar
Igor Konshin committed
365
		/// Retrieve all the nodes of the element.
366
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
367
		/// For a node returns itself.
368
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
369
		/// For an edge returns ordered pair of nodes. The order of nodes in the edge is preserved from the first construction.
370
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
371
372
373
		/// 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.
374
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
375
376
377
378
		/// 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.
379
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
380
381
382
		/// @return array of elements
		/// @see Face::CheckNormalOrientation
		/// @see Face::FaceOrientedOutside
Kirill Terekhov's avatar
Kirill Terekhov committed
383
		virtual ElementArray<Node>  getNodes                () const; //unordered
Igor Konshin's avatar
Igor Konshin committed
384
		/// Retrieve all the edges of the element.
385
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
386
		/// For a node returns unordered set of edges.
387
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
388
		/// For an edge returns itself.
389
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
390
		/// For a face returns ordered set of edges.
391
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
392
		/// For a cell returns unordered set of edges.
393
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
394
		/// @return array of elements
Kirill Terekhov's avatar
Kirill Terekhov committed
395
		virtual ElementArray<Edge>  getEdges                () const; //unordered
Igor Konshin's avatar
Igor Konshin committed
396
		/// Retrieve all the faces of the element.
397
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
398
		/// For a node returns unordered set of faces.
399
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
400
		/// For an edge returns unordered set of faces.
401
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
402
		/// For a face returns itself.
403
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
404
		/// For a cell return ordered set of faces. The order of faces in the cell is preserved from the first construction.
405
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
406
		/// @return array of elements
Kirill Terekhov's avatar
Kirill Terekhov committed
407
		virtual ElementArray<Face>  getFaces                () const; //unordered
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
408
		/// Return all the cells of the element.
409
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
410
		/// For a node returns unordered set of cells.
411
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
412
		/// For an edge returns unordered set of cells.
413
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
414
415
		/// 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.
416
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
417
		/// For a cell returns itself.
418
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
419
420
		/// @return array of elements
		/// @see Face::FaceOrientedOutside
Kirill Terekhov's avatar
Kirill Terekhov committed
421
422
423
424
425
426
427
428
429
430
431
432
433
		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
		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
434
		//implemented in geometry.cpp
435
		void                        CastRay                 (const real * pos, const real * dir, std::map<HandleType, real> & hits) const;
436
		
Kirill Terekhov's avatar
Kirill Terekhov committed
437
438
439
440
		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;
Kirill Terekhov's avatar
Feature    
Kirill Terekhov committed
441
442
443
444
445
446
447
448
		/// Determine that the element is on the boundary.
		/// \warning This function does not involve communication
		/// for distributed mesh and works only for local partition
		/// of the mesh. It cannot correctly determine boundary elements
		/// of the global mesh. For this purpose you should use
		/// Mesh::MarkBoundaryFaces
		/// @return True if the element is on the boundary, otherwise false.
		/// @see Mesh::MarkBoundaryFaces
Kirill Terekhov's avatar
Kirill Terekhov committed
449
450
		bool                        Boundary                () const;
		bool                        Planarity               () const; // check that all nodes lay on one plane
Kirill Terekhov's avatar
Kirill Terekhov committed
451
		//implemented in modify.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
452
453
454
		/// If the function returns true then element was hidden,
		/// works only inside BeginModification and EndModification,
		/// on EndModification all Hidden elements are deleted.
Kirill Terekhov's avatar
Feature    
Kirill Terekhov committed
455
456
		/// \todo Probably have to check normal orientation after
		/// hiding a back cell for a face.
Kirill Terekhov's avatar
Kirill Terekhov committed
457
458
459
460
		bool                        Hide                    () const;
		/// If the function returns true then element was recovered
		/// from hidden state, works only inside BeginModification
		/// and EndModification.
Kirill Terekhov's avatar
Feature    
Kirill Terekhov committed
461
462
		/// \todo Probably have to check normal orientation after
		/// recovering a back cell for a face.
Kirill Terekhov's avatar
Kirill Terekhov committed
463
		bool                        Show                    () const; // if true then element was recovered
Kirill Terekhov's avatar
Kirill Terekhov committed
464
465
466
467
468
469
470
		/// Remove element from mesh.
		/// If you call this function inside modification phase, see Mesh::BeginModification and Mesh::EndModification,
		/// and the element was not created during modification phase (not marked as Element::New),
		/// then the element will not be actually destroyed but hidden. 
		/// You can restore all the hidden elements by using Mesh::ToggleModification.
		/// \warning This function will not resolve an ierarchical strucutre of ElementSet, use ElemnetSet::DeleteSet instead.
		/// @return Returns true if the element was actually destroyed. Returns false if the element was hidden.
Kirill Terekhov's avatar
Kirill Terekhov committed
471
472
473
474
		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
475
		/// Disconnect element.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
476
477
		/// 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
478
		/// Updates geometric data and cell nodes automatically.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
479
		void                        Disconnect              (const HandleType * adjacent, INMOST_DATA_ENUM_TYPE num) const;
Kirill Terekhov's avatar
Kirill Terekhov committed
480
481
482
		/// \brief Connects lower adjacencies to current element.
		/// Geometric data and cell nodes are updated automatically.
		///
483
		/// \todo
Kirill Terekhov's avatar
Kirill Terekhov committed
484
485
486
		///	  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
487
		void                        Connect                 (const HandleType * adjacent, INMOST_DATA_ENUM_TYPE num) const; 
Kirill Terekhov's avatar
Kirill Terekhov committed
488
		/// Update geometric data for element, calls RecomputeGeometricData from Mesh.
489
490
491
492
		void                        UpdateGeometricData     () const;
		/// Marks element to be sent to remote processors that current processor don't belong to.
		/// Call Mesh::ExchangeMarked to perform the exchange.
		void                        SendTo                  (std::set<Storage::integer> & procs) const; 
493
494
		void                        SendTo                  (std::vector<Storage::integer> & procs) const; 
		void                        SendTo                  (Storage::integer_array procs) const; 
Kirill Terekhov's avatar
Kirill Terekhov committed
495
496
	};
	
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
497
498
	__INLINE const Element & InvalidElement() {static Element ret(NULL,InvalidHandle()); return ret;}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
499
500
501
	class Node : public Element //implemented in node.cpp
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
502
503
504
		Node() : Element() {}
		Node(const Node & other) : Element(other) {}
		Node(Mesh * m, HandleType h) : Element(m,h) {}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
505
		Node(Mesh * m, HandleType * h) : Element(m,h) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
506
507
508
509
510
511
512
513
514
		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
515

Kirill Terekhov's avatar
Kirill Terekhov committed
516
517
518
		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
519

Kirill Terekhov's avatar
Kirill Terekhov committed
520
		Storage::real_array         Coords                  () const; 
Kirill Terekhov's avatar
Kirill Terekhov committed
521
	};
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
522
523

	__INLINE const Node & InvalidNode() {static Node ret(NULL,InvalidHandle()); return ret;}
Kirill Terekhov's avatar
Kirill Terekhov committed
524
525
526
527
	
	class Edge : public Element //implemented in edge.cpp
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
528
529
530
		Edge() : Element() {}
		Edge(const Edge & other) : Element(other) {}
		Edge(Mesh * m, HandleType h) : Element(m,h) {}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
531
		Edge(Mesh * m, HandleType * h) : Element(m,h) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
532
533
534
535
536
537
		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
538
		
Kirill Terekhov's avatar
Kirill Terekhov committed
539
540
541
		ElementArray<Node>          getNodes                () const; //ordered
		ElementArray<Face>          getFaces                () const; //unordered
		ElementArray<Cell>          getCells                () const; //unordered
Kirill Terekhov's avatar
Kirill Terekhov committed
542

Kirill Terekhov's avatar
Kirill Terekhov committed
543
544
545
		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
546

Kirill Terekhov's avatar
Kirill Terekhov committed
547
548
		Node                        getBeg                  () const;
		Node                        getEnd                  () const;
Kirill Terekhov's avatar
Kirill Terekhov committed
549
		//implemented in modify.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
550
		static Edge                 UniteEdges              (const ElementArray<Edge> & edges, MarkerType del_protect);
Kirill Terekhov's avatar
Kirill Terekhov committed
551
552
553
		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);
554
		static Node                 CollapseEdge            (Edge e);
Kirill Terekhov's avatar
Kirill Terekhov committed
555
		//implemented in geometry.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
556
		Storage::real               Length                  () const;
557
558
		///Swap positions of first node and last node
		void                        SwapEnds                ();
Kirill Terekhov's avatar
Kirill Terekhov committed
559
	};
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
560
561

	__INLINE const Edge & InvalidEdge() {static Edge ret(NULL,InvalidHandle()); return ret;}
Kirill Terekhov's avatar
Kirill Terekhov committed
562
	
Kirill Terekhov's avatar
Kirill Terekhov committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583

	/// \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
584
585
586
	class Face : public Element //implemented in face.cpp
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
587
588
589
		Face() : Element() {}
		Face(const Face & other) : Element(other) {}
		Face(Mesh * m, HandleType h) : Element(m,h) {}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
590
		Face(Mesh * m, HandleType * h) : Element(m,h) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
591
592
593
594
595
596
		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
597
		
Kirill Terekhov's avatar
Kirill Terekhov committed
598
599
600
		ElementArray<Node>          getNodes                () const; //ordered
		ElementArray<Edge>          getEdges                () const; //ordered
		ElementArray<Cell>          getCells                () const; //unordered
Kirill Terekhov's avatar
Kirill Terekhov committed
601

Kirill Terekhov's avatar
Kirill Terekhov committed
602
603
604
		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
605
606

		//this is for 2d case when the face is represented by segment
Kirill Terekhov's avatar
Kirill Terekhov committed
607
608
		Node                        getBeg                  () const;
		Node                        getEnd                  () const;
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
609
610
		/// \brief Retrieve the cell for which the normal points outwards.
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
611
		/// \warning Depending on the grid construction the normal may incorrectly point inwards.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
612
		/// You can resolve this situation by Face::FixNormalOrientation.
Kirill Terekhov's avatar
Kirill Terekhov committed
613
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
614
615
		/// @return the cell for which normal points outwards.
		/// @see Face::FixNormalOrientation
Kirill Terekhov's avatar
Kirill Terekhov committed
616
		Cell                        BackCell                () const;
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
617
618
		/// \brief Retrieve the cell for which the normal points inwards.
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
619
		/// \warning Depending on the grid construction the normal may incorrectly point outwards.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
620
		/// You can resolve this situation by Face::FixNormalOrientation.
Kirill Terekhov's avatar
Kirill Terekhov committed
621
		///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
622
623
		/// @return the cell for which normal points inwards.
		/// @see Face::FixNormalOrientation
Kirill Terekhov's avatar
Kirill Terekhov committed
624
625
626
627
628
		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
629
		//implemented in modify.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
630
		static Face                 UniteFaces              (const ElementArray<Face> & faces, MarkerType del_protect);
Kirill Terekhov's avatar
Kirill Terekhov committed
631
632
		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
Kirill Terekhov's avatar
Kirill Terekhov committed
633
634
635
636
637
		static bool                 TestSplitFace           (Face face, const ElementArray<Edge> & edges, MarkerType del_protect);
		///This function changes Face::BackCell() with Face::FrontCell().
		/// \warning Function does not modify normal orientation. You may have
		/// to call Face::FixNormalOrientation().
		void                        SwapCells               () const; 
Kirill Terekhov's avatar
Kirill Terekhov committed
638
		//implemented in geometry.cpp
Kirill Terekhov's avatar
Kirill Terekhov committed
639
640
641
642
643
		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;
644
		bool                        FixNormalOrientation    (bool allow_swap = true) const;  //returns true if orientation was corrected, otherwise returns false
Kirill Terekhov's avatar
Kirill Terekhov committed
645
646
		bool                        CheckNormalOrientation  () const; //returns true if orientation is correct, otherwise returns false
		bool                        Closure                 () const; // test integrity of polygon
647
		bool                        Inside                  (const real * point) const; //is point inside face
Kirill Terekhov's avatar
Kirill Terekhov committed
648
	};
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
649
650

	__INLINE const Face & InvalidFace() {static Face ret(NULL,InvalidHandle()); return ret;}
Kirill Terekhov's avatar
Kirill Terekhov committed
651
	
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
652
653
654
655
656
657
	//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
658
659
	/// 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
660
661
662
663
664
665
666
667
668
	///
	/// 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
669
670
671
672
	/// 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
673
	class Cell : public Element
Kirill Terekhov's avatar
Kirill Terekhov committed
674
675
	{
	public:
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
676
677
678
679
680
681
682
683
684
685
		/// \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
686
		Cell(Mesh * m, HandleType h) : Element(m,h) {}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
687
688
689
690
691
		/// \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
692
		/// The purpose of this function is to be used in various non-constant iterators
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
693
694
695
696
		/// 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
697
		Cell(Mesh * m, HandleType * h) : Element(m,h) {}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
698
699
700
701
702
		/// \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
703
		Cell(const Cell & other) : Element(other) {}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
		/// \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
724
		const Cell * operator ->() const {return this;}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
725
726
727
728
729
730
731
732
733
734
735
		/// \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
736
737
		const Cell & self() const {return *this;}
	public:
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
738
739
740
741
		/// \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
742
		/// The order of nodes will be preserved from the moment of the construction of the cell.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
		/// 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
766
767
768
769
770
771
772
		///
		/// \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
773
774
775
776
777
778
		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
779
		/// the moment of the construction of the cell.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
780
781
782
783
784
785
786
		///
		/// @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
787
		/// The order of nodes will be preserved from the moment of the construction of the cell.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
788
789
790
791
792
793
794
795
796
		/// 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
797
798
799
		///
		/// \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
800
801
802
803
804
805
806
807
808
809
810
		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
811
812
813
		///
		/// \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
814
815
816
817
818
819
		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
820
		/// the moment of the construction of the cell.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
821
822
823
824
825
		///
		/// @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 faces that compose current cell and (not) marked by marker.
Kirill Terekhov's avatar
Kirill Terekhov committed
826
827
828
		///
		/// \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
829
830
831
		ElementArray<Face>          getFaces                (MarkerType mask,bool invert_mask = false) const;
		/// \brief Check that sequence of edges form a closed loop and each edge have a node that matches one of the nodes at the next edge.
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
832
		/// This function works for cells for which Element::GetElementDimension returns 2.
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
833
834
835
836
837
838
839
		///
		/// @return True if edges form the correct closed loop.
		///
		/// \todo
		///   1. Implement.
		///   2. Use in topology check algorithms.
		///   3. Use in Element::Connect.
Kirill Terekhov's avatar
Kirill Terekhov committed
840
		bool                        CheckEdgeOrder          () const; //not implemented//2D only, returns true if edges of face form an ordered closed loop
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
841
842
843
844
845
846
847
848
849
850
851
852
		/// \brief Repair the sequence of edges so that each edge have node that matches one of the nodes at the next edge.
		///
		/// This function works for cells for which Element::GetGeometricDimension returns 2.
		///
		/// This function may fail if all the edges does not form a closed loop.
		///
		/// @return True if edges were successfully reordered to form a closed loop.
		///
		/// \todo
		///   1. Implement.
		///   2. Use in topology check algorithms.
		///   3. Use in Element::Connect.
Kirill Terekhov's avatar
Kirill Terekhov committed
853
		bool                        FixEdgeOrder            () const; //not implemented//2D only, returns true if edges were successfully reordered to form a closed loop
Kirill Terekhov's avatar
Kirill Terekhov committed
854
		//implemented in modify.cpp
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
855
856
857
858
859
860
861
862
863
		/// \brief Unite a set of given cells into one cell.
		///
		/// This will create a cell whose faces are formed by symmetric difference
		/// of faces of given cells.
		/// If you specify a nonzero marker then the procedure will fail 
		/// if any marked element have to be deleted during union.
		/// @param cells A set of cells to be united.
		/// @param del_protect A marker that protects elements from deletion. Zero means no check.
		/// @return A new cell.
Kirill Terekhov's avatar
Kirill Terekhov committed
864
		static Cell                 UniteCells              (const ElementArray<Cell> & cells, MarkerType del_protect);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
865
866
867
868
		/// \brief Test that no marked element will be deleted during union of given cells.
		/// @param cells A set of cells to be united.
		/// @param del_protect A marker that protects elements from deletion. Zero means no check.
		/// @return True if no element deleted, false otherwise.
Kirill Terekhov's avatar
Kirill Terekhov committed
869
		static bool                 TestUniteCells          (const ElementArray<Cell> & cells, MarkerType del_protect);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
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
		/// \brief Separate a cell according to the set of provided faces.
		///
		/// You should first separate all edges with new nodes by Edge::SplitEdge, then faces with new edges by Face::SplitFace
		/// and then create faces using new edges from faces of initial cell and optionally edges from inside of the cell. 
		/// All faces are supposed to be internal with respect to the current cell. 
		/// Internally this function will resolve geometry of new cells inside of the 
		/// current cell by running through adjacency graph.
		///
		/// @param cell A cell to be split.
		/// @param faces A set of faces, internal for current cell, that will be used for construction of new cells.
		/// @param del_protect Marker that may be used to protect some elements from deletion by algorithm. 
		///        Zero means no check.
		/// @return A set of new cells.
		/// @see Edge::SplitEdge
		/// @see Face::SplitFace
		///
		/// \todo
		///   1. The algorithm inside is minimizing the size of the adjacency graph for each new cell.
		///      The correct behavior is to calculate volume of the cell for each adjacency graph and choose the graph with minimal volume.
		///      This requires calculation of volume for non-convex cells. For correct calculation of volume on non-convex cells one should 
		///      find one face for which normal orientation can be clearly determined and then orient all edges of the cell with respect to
		///      the orientation of edges of this face and establish normals for all faces. Once the algorithm is implemented here it should 
		///      be implemented in geometrical services or vice verse.
		///   2. Probably the algorithm should minimize the volume and adjacency graph size altogether. Between the cells with smallest volume
		///      within some tolerance select those that have smallest adjacency graph.
Kirill Terekhov's avatar
Kirill Terekhov committed
895
		static ElementArray<Cell>   SplitCell               (Cell cell, const ElementArray<Face> & faces, MarkerType del_protect); //provide all faces, that lay inside cell
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
896
897
898
899
900
901
		/// \brief This functions checks is it possible to split the cell by the given set of faces without deleting marked elements.
		///
		/// @param cell A cell to be split.
		/// @param faces Set of faces, internal for current cell, that will be used for new cells.
		/// @param del_protect Marker that may be used to protect some elements from deletion by algorithm. Zero means no check.
		/// @return True if possible, otherwise false.
Kirill Terekhov's avatar
Kirill Terekhov committed
902
		static bool                 TestSplitCell           (Cell cell, const ElementArray<Face> & faces, MarkerType del_protect);
Kirill Terekhov's avatar
Kirill Terekhov committed
903
		//implemented in geometry.cpp
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
904
905
906
907
908
909
910
911
912
913
		/// \brief Get a cell that share a face with the current cell.
		///
		/// Don't forget to check that the returned cell is valid.
		/// It would return invalid cell for boundary face.
		///
		/// @param face A face of current cell for which neighbouring cell is determined.
		/// @return A cell that shares a given face with the current cell.
		Cell                        Neighbour               (Face face) const;
		/// \brief Get all cells that share the face with the current cell.
		/// @return Set of cells that share a face with the current cell.
Kirill Terekhov's avatar
Kirill Terekhov committed
914
		ElementArray<Cell>          NeighbouringCells       () const; // get all cells that share any face with current
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
915
916
		/// \brief Determine, if point lies inside element.
		///
Kirill Terekhov's avatar
Kirill Terekhov committed
917
918
919
920
921
922
		/// Now it works only for 3-dimensional elements, at future it will be
		/// extended to support polygons and segments.
		/// @param point coordinates of the point, it is assumed that number of the
		/// coordinates is the same as the number of dimensions in the mesh.
		/// @return returns true if points inside element, false otherwise
		/// @see Mesh::GetDimensions
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
923
924
925
926
927
928
929
930
931
		///
		/// \todo
		///   1. Should be checked or extended for 2d cells. (done, testing)
		bool                        Inside                  (const real * point) const; //is point inside cell, check for 2d case
		/// \brief Return volume of the cell.
		///
		/// Note that currently the volume for non-convex cells may be calculated incorrectly.
		/// \todo
		///   1. Geometric services should correctly resolve volume for non-convex cells.
Kirill Terekhov's avatar
Kirill Terekhov committed
932
		real                        Volume                  () const;
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
933
934
935
936
		/// \brief Test that faces of the cell form the closed set.
		///
		/// This is automatically checked for if you activate NEED_TEST_CLOSURE
		/// in Mesh::SetTopologyCheck.
Kirill Terekhov's avatar
Kirill Terekhov committed
937
		/// @return True if faces of the cell form the closed set, false otherwise.
Kirill Terekhov's avatar
Kirill Terekhov committed
938
939
940
		bool                        Closure                 () const;
		/// For each adjacent cell make me a front cell and fix normal orientation accordingly.
		void                        SwapBackCell            () const;
941
942

		bool                        CheckConvexity          () const;
Kirill Terekhov's avatar
Kirill Terekhov committed
943
944
	};

Kirill Terekhov's avatar
Kirill Terekhov committed
945
	///
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
946
947
	__INLINE const Cell & InvalidCell() {static Cell ret(NULL,InvalidHandle()); return ret;}

Kirill Terekhov's avatar
Kirill Terekhov committed
948
949
950
	class ElementSet : public Element //implemented in eset.cpp
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
951
952
953
954
955
956
		///Number of reserved positions in HighConn array.
		///The first position is the handle to parent set.
		///The second position is handle to sibling set.
		///The third position is handle to child set.
		///The fourth position is number of sorted elements in the set.
		///All the rest are positions of deleted elements.
Kirill Terekhov's avatar
Kirill Terekhov committed
957
		static const enumerator             high_conn_reserved  = 4;
Kirill Terekhov's avatar
Kirill Terekhov committed
958
		__INLINE static HandleType &        hParent             (Element::adj_type & arr) {return arr[0];}
Kirill Terekhov's avatar
Kirill Terekhov committed
959
960
961
		__INLINE static HandleType &        hSibling            (Element::adj_type & arr) {return arr[1];}
		__INLINE static HandleType &        hChild              (Element::adj_type & arr) {return arr[2];}
		__INLINE static HandleType &        hSorted             (Element::adj_type & arr) {return arr[3];}
Kirill Terekhov's avatar
Kirill Terekhov committed
962
963
964
965
		typedef INMOST_DATA_BULK_TYPE       ComparatorType;
		static const ComparatorType         UNSORTED_COMPARATOR = 0;
		static const ComparatorType         GLOBALID_COMPARATOR = 1;
		static const ComparatorType         CENTROID_COMPARATOR = 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
966
		static const ComparatorType         HIERARCHY_COMPARATOR= 3;
Kirill Terekhov's avatar
Kirill Terekhov committed
967
		static const ComparatorType         HANDLE_COMPARATOR   = 4;
968
969
970
971
		//typedef INMOST_DATA_BULK_TYPE       ExchangeType;
		//static const ExchangeType           SYNC_ELEMENTS_NONE  = 0; //elements are not synced in parallel set
		//static const ExchangeType           SYNC_ELEMENTS_ALL   = 1; //all elements are synchronized in parallel set
		//static const ExchangeType           SYNC_ELEMENTS_SHARED= 2; //only shared elements are present
Kirill Terekhov's avatar
Kirill Terekhov committed
972
973
974
975
976
977
978
979
980
		                                    ElementSet          () : Element() {}
		                                    ElementSet          (Mesh * m, HandleType h) : Element(m,h) {}
		                                    ElementSet          (Mesh * m, HandleType * h) : Element(m,h) {}
		                                    ElementSet          (const ElementSet & other) : Element(other) {}
		__INLINE ElementSet &               operator =          (ElementSet const & other) {Element::operator =(other); return *this;}
		__INLINE ElementSet *               operator ->         () {return this;}
		__INLINE const ElementSet *         operator ->         () const {return this;}
		__INLINE ElementSet &               self                ()              {return *this;}
		__INLINE const ElementSet &         self                () const        {return *this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
981
	private:
982
983
984
		/// Compute union of processors on elements.
		/// @param procs Set of processors to fill.
		/// @param dir Directon for tree traversal, 1 - downwards, 2, upwards, 3 - both directions
985
		void                                CollectProcessors   (std::set<Storage::integer> & procs, char dir) const;
986
987
988
		/// Fill Mesh::SendtoTag() to send sets to processors from procs set that currently don't have this set
		/// @param procs Set of processors to send the set to.
		/// @param dir Directon for tree traversal, 1 - downwards, 2, upwards, 3 - both directions
989
990
991
		void                                SetSendTo(std::set<Storage::integer> & procs, char dir) const;
		void                                SetSendTo(std::vector<Storage::integer> & procs, char dir) const;
		void                                SetSendTo(Storage::integer_array procs, char dir) const;
Kirill Terekhov's avatar
Kirill Terekhov committed
992
993
994
	public:
		/// Get name of the set
		std::string                 GetName() const;
Igor Konshin's avatar
Igor Konshin committed
995
		/// Retrieve parent of the set
Kirill Terekhov's avatar
Kirill Terekhov committed
996
		ElementSet                  GetParent() const;
Igor Konshin's avatar
Igor Konshin committed
997
		/// Retrieve sibling set of the set, this will be next child for the parent
Kirill Terekhov's avatar
Kirill Terekhov committed
998
		ElementSet                  GetSibling() const;
999
		/// Retrieve child set of the set.
Kirill Terekhov's avatar
Kirill Terekhov committed
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
		/// Following example shows how to iterate through children of current set:
		/// for(ElementSet it = set->GetChild(); it->isValid(); it = it->GetSibling()) ...
		ElementSet                  GetChild() const;
		
		/// This will create new child for the parent
		void                        AddSibling(const ElementSet & sibling) const;
		/// Add child to current set
		void                        AddChild(const ElementSet & child) const;

		/// This will erase sibling or parent's child
		void                        RemSibling(const ElementSet & sibling) const;
		/// This will erase my child
		void                        RemChild(const ElementSet & child) const;
		/// How many there are siblings to the right of me including me
		enumerator                  CountSiblings() const;
		/// How many children I have 
		enumerator                  CountChildren() const;

		bool                        HaveSibling() const;
		bool                        HaveParent() const;
		bool                        HaveChild() const;

		/// Direct raw access to stored elements, no copy involved.
		/// The actual pointer may change when you add new elements due to reallocation of memory.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1024
		/// Modify at your own risk.
Kirill Terekhov's avatar
Kirill Terekhov committed
1025
		HandleType *                getHandles() const;
1026
1027
		/// Retrieve number of stored handles, including invalid.
		/// If you want to get number of valid elements use ElementSet::Size
Kirill Terekhov's avatar
Kirill Terekhov committed
1028
		enumerator                  nbHandles() const;
1029
		/// Retrieve position after last sorted element. This does not correctly
Kirill Terekhov's avatar
Kirill Terekhov committed
1030
1031
		/// represent total number of sorted elements, since some of them may be deleted
		enumerator                  nbSorted() const;
Igor Konshin's avatar
Igor Konshin committed
1032
		/// Retrieve all elements by type
Kirill Terekhov's avatar
Kirill Terekhov committed
1033
1034
		enumerator                  nbAdjElements(ElementType etype) const;
		enumerator                  nbAdjElements(ElementType etype, MarkerType select, bool invert = false) const;
Igor Konshin's avatar
Igor Konshin committed
1035
		/// Retrieve all elements by type
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
1037
1038
		ElementArray<Element>       getAdjElements(ElementType etype) const;
		ElementArray<Element>       getAdjElements(ElementType etype, MarkerType select, bool invert = false) const;
		
Igor Konshin's avatar
Igor Konshin committed
1039
		/// Retrieve only nodes
Kirill Terekhov's avatar
Kirill Terekhov committed
1040
1041
		ElementArray<Node>          getNodes() const;
		ElementArray<Node>          getNodes(MarkerType select, bool invert = false) const;
Igor Konshin's avatar
Igor Konshin committed
1042
		/// Retrieve only edges
Kirill Terekhov's avatar
Kirill Terekhov committed
1043
1044
		ElementArray<Edge>          getEdges() const;
		ElementArray<Edge>          getEdges(MarkerType select, bool invert = false) const;
Igor Konshin's avatar
Igor Konshin committed
1045
		/// Retrieve only faces
Kirill Terekhov's avatar
Kirill Terekhov committed
1046
1047
		ElementArray<Face>          getFaces() const;
		ElementArray<Face>          getFaces(MarkerType select, bool invert = false) const;
Igor Konshin's avatar
Igor Konshin committed
1048
		/// Retrieve only cells
Kirill Terekhov's avatar
Kirill Terekhov committed
1049
1050
		ElementArray<Cell>          getCells() const;
		ElementArray<Cell>          getCells(MarkerType select, bool invert = false) const;
1051
1052
		/// Put one element without checking of the existance of duplicate.
		/// For sorted set new element will appear at unsorted part.
Kirill Terekhov's avatar
Kirill Terekhov committed
1053
		void                        PutElement(HandleType e) const;
1054
		/// Put one element without checking of the existance of duplicate.
Kirill Terekhov's avatar
Kirill Terekhov committed
1055
1056
1057
1058
		/// For sorted set new element will appear at unsorted part
		void                        PutElement(const Storage & e) const {PutElement(e->GetHandle());}
		/// Put multiple handles without checking of the existance of duplicate
		void                        PutElements(const HandleType * handles, enumerator num) const;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1059
1060
		/// Put multiple handles of the other set without checking of the existance of duplicate
		void                        PutElements(const ElementSet & other) const {PutElements(other->getHandles(),other->nbHandles());}
Kirill Terekhov's avatar
Kirill Terekhov committed
1061
1062
		/// Put multiple handles without checking
		template<typename EType>
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1063
		void                        PutElements(const ElementArray<EType> & elems) const {PutElements(elems.data(),static_cast<enumerator>(elems.size()));}
1064
		/// Put one element with checking of the existance of duplicate.
Kirill Terekhov's avatar
Kirill Terekhov committed
1065
		/// Preserves order for sorted set, thus may be expensive.
Kirill Terekhov's avatar
Kirill Terekhov committed
1066
		void                        AddElement(HandleType e) const;
1067
		/// Put one element with checking of the existance of duplicate.
Kirill Terekhov's avatar
Kirill Terekhov committed
1068
		/// Preserves order for sorted set, thus may be expensive.
Kirill Terekhov's avatar
Kirill Terekhov committed
1069
		void                        AddElement(const Storage & e) const {AddElement(e->GetHandle());}
1070
1071
		/// Add multiple elements with checking of the existance of duplicate.
		/// Preserves order for sorted set, thus may be expensive.
Kirill Terekhov's avatar
Kirill Terekhov committed
1072
1073
1074
		/// This will also remove any duplicates in unsorted part of the set.
		/// If you inserted duplicated elements through PutElements into previously sorted array
		/// then this operation does not guarantee that those duplications will be stored.
Kirill Terekhov's avatar
Kirill Terekhov committed
1075
		/// \todo Recheck usage of markers.
Kirill Terekhov's avatar
Kirill Terekhov committed
1076
		void                        AddElements(const HandleType * handles, enumerator num) const;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1077
1078
1079
		/// Add elements of other set
		void                        AddElements(const ElementSet & other) {Unite(other);}
		/// Add multiple elements with checking of the existance of duplicate
Kirill Terekhov's avatar
Kirill Terekhov committed
1080
1081
1082
1083
1084
1085
1086
1087
1088
		template<typename EType>
		void                        AddElements(const ElementArray<EType> & elems) const {AddElements(elems.data(),static_cast<enumerator>(elems.size()));}

		void                        RemoveElement(const Storage & e) const;
		void                        RemoveElements(const HandleType * handles, enumerator num) const;
		/// Remove multiple elements from the set
		template<typename EType>
		void                        RemoveElements(const ElementArray<EType> & elems) const {RemoveElements(elems.data(),static_cast<enumerator>(elems.size()));}

Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1089
		/// Compute and return union with other set.
Kirill Terekhov's avatar
Kirill Terekhov committed
1090
		/// Result is unordered.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1091
		/// All elements will be unique.
Kirill Terekhov's avatar
Kirill Terekhov committed
1092
		ElementArray<Element> Union(const ElementSet & other) const;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1093
		/// Compute and return union with raw handles.
Kirill Terekhov's avatar
Kirill Terekhov committed
1094
		/// Result is unordered.
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1095
		/// All elements will be unique.