geometry.cpp 52.9 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
#include "inmost.h"
#if defined(USE_MESH)
Kirill Terekhov's avatar
Kirill Terekhov committed
3
#include <deque>
Kirill Terekhov's avatar
Kirill Terekhov committed
4
using namespace std;
Kirill Terekhov's avatar
Kirill Terekhov committed
5

Kirill Terekhov's avatar
Kirill Terekhov committed
6
7
8
9
10
const std::string normal_name = "PROTECTED_GEOM_UTIL_NORMAL";
const std::string measure_name = "PROTECTED_GEOM_UTIL_MEASURE";
const std::string centroid_name = "PROTECTED_GEOM_UTIL_CENTROID";
const std::string barycenter_name = "PROTECTED_GEOM_UTIL_BARYCENTER";

Kirill Terekhov's avatar
Kirill Terekhov committed
11
12
namespace INMOST
{
Kirill Terekhov's avatar
Kirill Terekhov committed
13
14
15
16
17
18
19
20
21
22
	typedef struct orient_face_t
	{
		Edge bridge;
		Node first;
		Face face;
		orient_face_t(Edge _bridge, Node _first, Face _face)
		:bridge(_bridge),first(_first),face(_face)
		{
		}
	} orient_face;
Kirill Terekhov's avatar
Kirill Terekhov committed
23

Kirill Terekhov's avatar
Kirill Terekhov committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	__INLINE static void vec_diff(const Storage::real * vecin1, const Storage::real * vecin2, Storage::real * vecout, unsigned int size)
	{
		for(unsigned int i = 0; i < size; i++)
			vecout[i] = vecin1[i] - vecin2[i];
	}
	
	__INLINE static void vec_cross_product(const Storage::real * vecin1, const Storage::real * vecin2, Storage::real * vecout)
	{
		Storage::real temp[3];
		temp[0] = vecin1[1]*vecin2[2] - vecin1[2]*vecin2[1];
		temp[1] = vecin1[2]*vecin2[0] - vecin1[0]*vecin2[2];
		temp[2] = vecin1[0]*vecin2[1] - vecin1[1]*vecin2[0];
		vecout[0] = temp[0];
		vecout[1] = temp[1];
		vecout[2] = temp[2];
	}
	
	__INLINE static Storage::real vec_dot_product(const Storage::real * vecin1,const Storage::real * vecin2, unsigned int size)
	{
		Storage::real ret = 0;
		for(unsigned int i = 0; i < size; i++)
			ret += vecin1[i]*vecin2[i];
		return ret;
	}
	
	__INLINE static Storage::real vec_len2(const Storage::real * vecin, unsigned int size)
	{
		return vec_dot_product(vecin,vecin,size);
	}
	
	__INLINE static Storage::real vec_len(const Storage::real * vecin, unsigned int size)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
56
		return ::sqrt(vec_len2(vecin,size));
Kirill Terekhov's avatar
Kirill Terekhov committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
	}
	
	__INLINE static Storage::real det3d(Storage::real a, Storage::real b, Storage::real c,
	                         Storage::real d, Storage::real e, Storage::real f,
	                         Storage::real g, Storage::real h, Storage::real i ) 
	{
		return a*e*i - c*e*g + b*f*g - a*f*h + c*d*h - b*d*i;
	}
	
	__INLINE static Storage::real det3v(const Storage::real * x,const Storage::real * y,const Storage::real * z) 
	{
		return det3d(x[0], x[1], x[2],  y[0], y[1], y[2],  z[0], z[1], z[2]);
	}
	
	__INLINE static Storage::real det4v(const Storage::real * w, const Storage::real * x, const Storage::real * y, const Storage::real * z) 
	{
		return det3d(x[0]-w[0], x[1]-w[1], x[2]-w[2],  y[0]-w[0], y[1]-w[1], y[2]-w[2],  z[0]-w[0], z[1]-w[1], z[2]-w[2]);
	}	
	
	
	__INLINE static Storage::real vec_normalize(Storage::real * vecin, unsigned int size)
	{
		Storage::real len = 0;
		for(unsigned int i = 0; i < size; i++)
			len += vecin[i]*vecin[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
82
		len = ::sqrt(len);
Kirill Terekhov's avatar
Kirill Terekhov committed
83
84
85
86
87
88
		for(unsigned int i = 0; i < size; i++)
			vecin[i] /= len;
		return len;
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
89
	ElementArray<Cell> Cell::NeighbouringCells() const
Kirill Terekhov's avatar
Kirill Terekhov committed
90
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
91
92
93
		ElementArray<Cell> ret(GetMeshLink());
		ElementArray<Face> faces = getFaces();
		for(ElementArray<Face>::iterator f = faces.begin(); f != faces.end(); f++)
Kirill Terekhov's avatar
Kirill Terekhov committed
94
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
95
96
			Cell c = Neighbour(f->self());
			if( c.isValid() ) ret.push_back(c);
Kirill Terekhov's avatar
Kirill Terekhov committed
97
98
99
100
101
		}
		return ret;
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
102
	Cell Cell::Neighbour(Face f) const
Kirill Terekhov's avatar
Kirill Terekhov committed
103
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
104
105
		Cell b = f->BackCell();
		if( b == self() )
Kirill Terekhov's avatar
Kirill Terekhov committed
106
107
108
109
			return f->FrontCell();
		return b;
	}
	
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
110
	bool Cell::Inside(const Storage::real * point) const//check for 2d case
Kirill Terekhov's avatar
Kirill Terekhov committed
111
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
112
		Mesh * mesh = GetMeshLink();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
113
114
115
		integer dim = GetElementDimension();
		if( dim == 3 )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
116
117
118
119
120
121
122
123
124
125
126
			/*
			tiny_map<HandleType,real,16> hits;
			real ray[3];
			ray[0] = rand()/(real)RAND_MAX;
			ray[1] = rand()/(real)RAND_MAX;
			ray[2] = rand()/(real)RAND_MAX;
			CastRay(point,ray,hits);
			if( hits.size()%2 == 0 ) return false;
			return true;
			 */
			assert(mesh->GetDimensions() == 3);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
127
128
129
			integer vp = 0;
			integer vm = 0;
			integer vz = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
			real eps = mesh->GetEpsilon();
			real c,d, fcnt[3];
			real_array v1,v2;
			ElementArray<Face> data = getFaces();
			Face cur = data[0];
			MarkerType mrk = mesh->CreatePrivateMarker();
			MarkerType rev = mesh->CreatePrivateMarker(); //reverse orientation
			data.SetPrivateMarker(mrk); //0-th face orientation is default
			cur->RemPrivateMarker(mrk);
			Node n1,n2; //to retrive edge
			bool reverse = false; //reverse orientation in considered face
			std::deque< orient_face > stack; //edge and first node and face for visiting
			//todo: can do faster by retriving edges and going over their nodes
			//should not use FindSharedAdjacency
			ElementArray<Edge> edges = cur->getEdges();
			do
			{
				//figure out starting node order
				if( edges[0]->getBeg() == edges[1]->getBeg() ||
				   edges[0]->getBeg() == edges[1]->getEnd() )
				{
					n1 = edges[0]->getEnd();
					n2 = edges[0]->getBeg();
				}
				else
				{
					n1 = edges[0]->getBeg();
					n2 = edges[0]->getEnd();
				}
				//schedule unvisited adjacent faces
				for(unsigned j = 0; j < edges.size(); j++)
				{
					//schedule face adjacent to considered edge
					ElementArray<Face> adjacent = edges[j]->getFaces(mrk);
					assert(adjacent.size() <= 1);
					if( !adjacent.empty() && adjacent[0].GetPrivateMarker(mrk))
					{
						adjacent.RemPrivateMarker(mrk);
						stack.push_back(orient_face(edges[j],reverse ? n2 : n1,adjacent[0]));
					}
					//update edge nodes
					n1 = n2; //current end is new begin
					//find new end
					if( n2 == edges[(j+1)%edges.size()]->getBeg() )
						n2 = edges[(j+1)%edges.size()]->getEnd();
					else
						n2 = edges[(j+1)%edges.size()]->getBeg();
				}
				if( stack.empty() ) break;
				//get entry from stack
				orient_face r = stack.front();
				//remove face from stack
				stack.pop_front();
				//retrive edges for new face
				edges = r.face->getEdges();
				reverse = false;
				//figure out starting node order
				if( edges[0]->getBeg() == edges[1]->getBeg() ||
				   edges[0]->getBeg() == edges[1]->getEnd() )
				{
					n1 = edges[0]->getEnd();
					n2 = edges[0]->getBeg();
				}
				else
				{
					n1 = edges[0]->getBeg();
					n2 = edges[0]->getEnd();
				}
				//find out common edge orientation
				for(unsigned j = 0; j < edges.size(); j++)
				{
					if( edges[j] == r.bridge ) //found the edge
					{
						//reverse ordering on this face
						if( r.first == n1 )
						{
							r.face->SetPrivateMarker(rev);
							reverse = true;
						}
						break;
					}
					//update edge nodes
					n1 = n2; //current end is new begin
					//find new end
					if( n2 == edges[(j+1)%edges.size()]->getBeg() )
						n2 = edges[(j+1)%edges.size()]->getEnd();
					else
						n2 = edges[(j+1)%edges.size()]->getBeg();
				}
			} while(true);
			data.RemPrivateMarker(mrk);
			mesh->ReleasePrivateMarker(mrk);
			for(ElementArray<Face>::size_type f = 0; f < data.size(); f++)
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
223
224
			{
				d = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
225
226
227
228
229
230
231
232
233
234
235
236
237
				data[f]->Centroid(fcnt);
				ElementArray<Node> nodes = data[f]->getNodes();
				v1 = nodes[0].Coords();
				for (ElementArray<Node>::size_type i=0; i<nodes.size(); i++)
				{
					v2 = nodes[(i+1)%nodes.size()].Coords();
					d += c = det4v(point, fcnt, v1.data(), v2.data());
					v1.swap(v2);
				}
				//if(!data[f]->FaceOrientedOutside(self()))
				if( data[f]->GetPrivateMarker(rev) )
					c = -1.0;
				else
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
238
					c = 1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
239
240
241
242
243
				if(c*d > eps)
					vp++;
				else if(c*d < -eps)
					vm++;
				else
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
244
245
					vz++;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
246
247
			data.RemPrivateMarker(rev);
			mesh->ReleasePrivateMarker(rev);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
248
			if(vp*vm > 0) return false;
Kirill Terekhov's avatar
Kirill Terekhov committed
249
			else if( vz == 0 ) return true;
Kirill Terekhov's avatar
Kirill Terekhov committed
250
			else return true;
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
251
252
		}
		else
Kirill Terekhov's avatar
Kirill Terekhov committed
253
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
254
			int mdim = mesh->GetDimensions();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
255
256
257
258
259
260
261
262
			assert(mdim <= 3);
			Storage::real data[9][3];
			if( mdim < 3 )
			{
				memset(data,0,sizeof(Storage::real)*9*3);
			}
			Centroid(data[0]);
			ElementArray<Node> nodes = getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
263
			for(int i = 0; i < static_cast<int>(nodes.size()); i++)
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
264
265
266
267
268
269
270
271
272
273
			{
				int j = (i+1)%nodes.size();
				nodes[i].Centroid(data[1]);
				nodes[j].Centroid(data[2]);
				vec_diff(point,data[0],data[3],mdim);
				vec_diff(point,data[1],data[4],mdim);
				vec_diff(point,data[2],data[5],mdim);
				vec_cross_product(data[3],data[4],data[6]);
				vec_cross_product(data[4],data[5],data[7]);
				vec_cross_product(data[5],data[3],data[8]);
Kirill Terekhov's avatar
Kirill Terekhov committed
274
				
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
275
				if( vec_dot_product(data[6],data[7],mdim) >= 0 &&
Kirill Terekhov's avatar
Kirill Terekhov committed
276
277
				   vec_dot_product(data[7],data[8],mdim) >= 0 &&
				   vec_dot_product(data[6],data[8],mdim) >= 0 )
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
278
279
280
					return true; //inside one of the triangles
			}
			return false;
Kirill Terekhov's avatar
Kirill Terekhov committed
281
282
283
284
		}
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
285
	void Face::UnitNormal(real * nrm) const
Kirill Terekhov's avatar
Kirill Terekhov committed
286
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
287
288
289
		Mesh * m = GetMeshLink();
		m->GetGeometricData(GetHandle(),NORMAL,nrm); 
		integer dim = m->GetDimensions();
Kirill Terekhov's avatar
Kirill Terekhov committed
290
		real    l   = ::sqrt(vec_dot_product(nrm,nrm,dim)); 
Kirill Terekhov's avatar
Kirill Terekhov committed
291
		if(::fabs(l) > m->GetEpsilon()) 
Kirill Terekhov's avatar
Kirill Terekhov committed
292
293
		{
			for(integer i = 0; i < dim; i++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
294
				nrm[i] /= l; 
Kirill Terekhov's avatar
Kirill Terekhov committed
295
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
296
297
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
298
	void Face::OrientedNormal(Cell c, Storage::real * nrm) const
Kirill Terekhov's avatar
Kirill Terekhov committed
299
300
	{
		Normal(nrm); 
Kirill Terekhov's avatar
Kirill Terekhov committed
301
302
303
304
		if( !FaceOrientedOutside(c) )
		{
			integer dim = GetMeshLink()->GetDimensions();
			for(integer i = 0; i < dim; i++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
305
				nrm[i] = -nrm[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
306
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
307
308
309
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
310
	void Face::OrientedUnitNormal(Cell c, Storage::real * nrm) const
Kirill Terekhov's avatar
Kirill Terekhov committed
311
312
313
	{
		UnitNormal(nrm); 
		if( !FaceOrientedOutside(c) ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
314
315
316
		{
			integer dim = GetMeshLink()->GetDimensions();
			for(integer i = 0; i < dim; i++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
317
				nrm[i] = -nrm[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
318
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
319
320
321
322
	}
	
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
323
	bool Mesh::TestClosure(const HandleType * elements, integer num) const
Kirill Terekhov's avatar
Kirill Terekhov committed
324
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
325
326
327
		integer i;
		tiny_map<HandleType,int,64> e_visit;
		tiny_map<HandleType,int,64>::iterator it;
Kirill Terekhov's avatar
Kirill Terekhov committed
328
329
330
331
		if( !HideMarker() )
		{
			for(i = 0; i < num; i++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
332
333
334
				Element::adj_type const & lc = LowConn(elements[i]);
				for(Element::adj_type::size_type jt = 0; jt < lc.size(); jt++)
					e_visit[lc[jt]]++;
Kirill Terekhov's avatar
Kirill Terekhov committed
335
336
337
338
			}
		}
		else
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
339
			for(i = 0; i < num; i++) if( !GetMarker(elements[i],HideMarker()) )
Kirill Terekhov's avatar
Kirill Terekhov committed
340
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
341
342
343
344
345
346
				Element::adj_type const & lc = LowConn(elements[i]);
				for(Element::adj_type::size_type jt = 0; jt < lc.size(); jt++) 
				{
					if( !GetMarker(lc[jt],HideMarker()) ) 
						e_visit[lc[jt]]++;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
347
348
349
350
351
352
353
			}
		}
		for(it = e_visit.begin(); it != e_visit.end(); it++)
			if( it->second != 2 ) return false;
		return true;
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
354
	Element::GeometricType Mesh::ComputeGeometricType(ElementType etype, const HandleType * lc, INMOST_DATA_ENUM_TYPE s) const
Kirill Terekhov's avatar
Kirill Terekhov committed
355
356
357
358
359
360
361
362
363
364
365
366
367
	{
		Element::GeometricType ret = Element::Unset;
		if( s == 0 && etype != NODE) return ret;
		switch(etype)
		{
			case NODE: ret = Element::Vertex; break;
			case EDGE:
				if( s == 1 )
					ret = Element::Vertex;
				else if( s == 2 )
					ret = Element::Line;
				break;
			case FACE:
Kirill Terekhov's avatar
Kirill Terekhov committed
368
369

				if( Element::GetGeometricDimension(GetGeometricType(lc[0])) == 0 )
Kirill Terekhov's avatar
Kirill Terekhov committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
				{ 
					ret = Element::Line;
				}
				else
				{
					if( !GetTopologyCheck(NEED_TEST_CLOSURE) || TestClosure(lc,s) )
					{
						if( s == 3 )
							ret = Element::Tri;
						else if( s == 4 )
							ret = Element::Quad;
						else
							ret = Element::Polygon;
					}
					else ret = Element::MultiLine;
				}
				break;
			case CELL:
Kirill Terekhov's avatar
Kirill Terekhov committed
388
				if(  Element::GetGeometricDimension(GetGeometricType(lc[0])) == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
389
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
390
					if( !GetTopologyCheck(NEED_TEST_CLOSURE) || TestClosure(lc,s) )
Kirill Terekhov's avatar
Kirill Terekhov committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
					{
						if( s == 3 )
							ret = Element::Tri;
						else if( s == 4 )
							ret = Element::Quad;
						else
							ret = Element::Polygon;
					}
					else ret = Element::MultiLine;
				}
				else 
				{
					if( !GetTopologyCheck(NEED_TEST_CLOSURE) ||  TestClosure(lc,s) )
					{
						//test c_faces closure, if no closure, set as MultiPolygon
						INMOST_DATA_ENUM_TYPE quads = 0,tris = 0,i;
						for(i = 0; i < s; i++)
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
409
							if( GetGeometricType(lc[i]) == Element::Tri )
Kirill Terekhov's avatar
Kirill Terekhov committed
410
								tris++;
Kirill Terekhov's avatar
Kirill Terekhov committed
411
							else if( GetGeometricType(lc[i]) == Element::Quad )
Kirill Terekhov's avatar
Kirill Terekhov committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
								quads++;
						}
						if( tris == 4 && s == 4 )
							ret = Element::Tet;
						else if( quads == 6 && s == 6 )
							ret = Element::Hex;
						else if( tris == 4 && quads == 1 && s == tris+quads)
							ret = Element::Pyramid;
						else if( quads == 3 && tris == 2 && s == tris+quads)
							ret = Element::Prism;
						else
							ret = Element::Polyhedron;
					}
					else ret = Element::MultiPolygon;
				}
				break;
			case ESET: ret = Element::Set; break;
		}
		return ret;
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
433
	Storage::real Edge::Length() const 
Kirill Terekhov's avatar
Kirill Terekhov committed
434
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
		Storage::real ret; 
		GetMeshLink()->GetGeometricData(GetHandle(),MEASURE,&ret); 
		return ret;
	}

	Storage::real Face::Area() const 
	{
		real ret; 
		GetMeshLink()->GetGeometricData(GetHandle(),MEASURE,&ret); 
		return ret;
	}
	void Face::Normal(real * nrm) const 
	{
		GetMeshLink()->GetGeometricData(GetHandle(),NORMAL,nrm);
	}

	Storage::real Cell::Volume() const 
	{
		real ret; 
		GetMeshLink()->GetGeometricData(GetHandle(),MEASURE,&ret); 
		return ret;
	}

	void Element::ComputeGeometricType() const
	{
		GetMeshLink()->ComputeGeometricType(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
461
462
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
463
464
465
466
467
468
469
	void Mesh::ComputeGeometricType(HandleType h) 
	{
		SetGeometricType(h,Element::Unset);
		Element::adj_type const & lc = LowConn(h);
		if( !lc.empty() )
			SetGeometricType(h,ComputeGeometricType(GetHandleElementType(h),lc.data(),static_cast<integer>(lc.size())));
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
470
	
Kirill Terekhov's avatar
Kirill Terekhov committed
471
	void Mesh::RecomputeGeometricData(HandleType e)
Kirill Terekhov's avatar
Kirill Terekhov committed
472
473
474
475
476
	{
		//static std::map<Element *, int> numfixes;
		GeometricData d ;
		for(d = CENTROID; d <= NORMAL; d++) // first compute centroids and normals 
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
477
			if( HaveGeometricData(d,GetHandleElementType(e)) ) //compute centroid first
Kirill Terekhov's avatar
Kirill Terekhov committed
478
479
			{
				Tag t = GetGeometricTag(d);
Kirill Terekhov's avatar
Kirill Terekhov committed
480
481
				Storage::real * a = static_cast<Storage::real *>(MGetDenseLink(e,t));
				HideGeometricData(d,GetHandleElementType(e));
Kirill Terekhov's avatar
Kirill Terekhov committed
482
				GetGeometricData(e,d,a);
Kirill Terekhov's avatar
Kirill Terekhov committed
483
				ShowGeometricData(d,GetHandleElementType(e));
Kirill Terekhov's avatar
Kirill Terekhov committed
484
485
486
487
			}
		}


Kirill Terekhov's avatar
Kirill Terekhov committed
488
		if( GetHandleElementType(e) == CELL && HaveGeometricData(ORIENTATION,FACE)) //then correct the normal
Kirill Terekhov's avatar
Kirill Terekhov committed
489
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
490
491
492
			Element::adj_type & lc = LowConn(e);
			for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)
				if( !GetMarker(*it,HideMarker()) && HighConn(*it).size() == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
493
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
494
					Face(this,*it)->FixNormalOrientation();
Kirill Terekhov's avatar
Kirill Terekhov committed
495
496
497
498
				}
		}
		for(d = MEASURE; d <= BARYCENTER; d++) // compute the rest
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
499
			if( HaveGeometricData(d,GetHandleElementType(e)) )
Kirill Terekhov's avatar
Kirill Terekhov committed
500
501
			{
				Tag t = GetGeometricTag(d);
Kirill Terekhov's avatar
Kirill Terekhov committed
502
503
				Storage::real * a = static_cast<Storage::real *>(MGetDenseLink(e,t));
				HideGeometricData(d,GetHandleElementType(e));
Kirill Terekhov's avatar
Kirill Terekhov committed
504
				GetGeometricData(e,d,a);
Kirill Terekhov's avatar
Kirill Terekhov committed
505
				ShowGeometricData(d,GetHandleElementType(e));
Kirill Terekhov's avatar
Kirill Terekhov committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
			}
		}
	}
	
	
	void Mesh::RemoveGeometricData(GeomParam table)
	{
		for(GeomParam::iterator it = table.begin(); it != table.end(); ++it)
		{
			if( it->first == MEASURE    ) 
			{
				if(measure_tag.isValid())    
					measure_tag    = DeleteTag(measure_tag   ,it->second);
				for(ElementType etype = EDGE; etype <= CELL; etype = etype << 1) if( etype & it->second) HideGeometricData(MEASURE,etype);
			}
			if( it->first == CENTROID   ) 
			{
				if(centroid_tag.isValid())   
					centroid_tag   = DeleteTag(centroid_tag  ,it->second);
				for(ElementType etype = EDGE; etype <= CELL; etype = etype << 1) if( etype & it->second) HideGeometricData(CENTROID,etype);
			}
			if( it->first == BARYCENTER ) 
			{
				if(barycenter_tag.isValid()) 
					barycenter_tag = DeleteTag(barycenter_tag,it->second);
				for(ElementType etype = EDGE; etype <= CELL; etype = etype << 1) if( etype & it->second) HideGeometricData(BARYCENTER,etype);
			}
			if( it->first == NORMAL     ) 
			{
				if(normal_tag.isValid())
					normal_tag     = DeleteTag(normal_tag    ,it->second);
				for(ElementType etype = FACE; etype <= CELL; etype = etype << 1) if( etype & it->second) HideGeometricData(NORMAL,etype);
			}
			if( it->first == ORIENTATION) 
				if( FACE & it->second) HideGeometricData(ORIENTATION,FACE);
		}
	}
	
	void Mesh::RestoreGeometricTags()
	{
		for(GeometricData gtype = MEASURE; gtype <= NORMAL; gtype++)
		{
			bool restore = false;
549
			for(ElementType etype = EDGE; etype <= CELL && !restore; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
550
551
552
553
554
555
				if( HaveGeometricData(gtype,etype) )
					restore = true;
			if( restore )
			{
				switch(gtype)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
556
557
558
559
				case MEASURE:       measure_tag = GetTag(measure_name);    break;
				case CENTROID:     centroid_tag = GetTag(centroid_name);   break;
				case BARYCENTER: barycenter_tag = GetTag(barycenter_name); break;
				case NORMAL:         normal_tag = GetTag(normal_name);     break;
Kirill Terekhov's avatar
Kirill Terekhov committed
560
561
562
563
				}
			}
		}
	}
564

Kirill Terekhov's avatar
Kirill Terekhov committed
565
566
	void Mesh::RepairGeometricTags()
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
567
		if( HaveTag(measure_name) )
Kirill Terekhov's avatar
Kirill Terekhov committed
568
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
569
			measure_tag = GetTag(measure_name);
Kirill Terekhov's avatar
Kirill Terekhov committed
570
571
572
573
			for(ElementType etype = EDGE; etype <= CELL; etype = NextElementType(etype))
				if( measure_tag.isDefined(etype) && !HaveGeometricData(MEASURE,etype) )
					ShowGeometricData(MEASURE,etype);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
574
		if( HaveTag(centroid_name) )
Kirill Terekhov's avatar
Kirill Terekhov committed
575
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
576
			centroid_tag = GetTag(centroid_name);
Kirill Terekhov's avatar
Kirill Terekhov committed
577
578
579
580
			for(ElementType etype = EDGE; etype <= CELL; etype = NextElementType(etype))
				if( centroid_tag.isDefined(etype) && !HaveGeometricData(CENTROID,etype) )
					ShowGeometricData(CENTROID,etype);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
581
		if( HaveTag(barycenter_name) )
Kirill Terekhov's avatar
Kirill Terekhov committed
582
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
583
			barycenter_tag = GetTag(barycenter_name);
Kirill Terekhov's avatar
Kirill Terekhov committed
584
585
586
587
			for(ElementType etype = EDGE; etype <= CELL; etype = NextElementType(etype))
				if( barycenter_tag.isDefined(etype) && !HaveGeometricData(BARYCENTER,etype) )
					ShowGeometricData(BARYCENTER,etype);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
588
		if( HaveTag(normal_name) )
Kirill Terekhov's avatar
Kirill Terekhov committed
589
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
590
			normal_tag = GetTag(normal_name);
Kirill Terekhov's avatar
Kirill Terekhov committed
591
592
593
594
595
			for(ElementType etype = EDGE; etype <= CELL; etype = NextElementType(etype))
				if( normal_tag.isDefined(etype) && !HaveGeometricData(NORMAL,etype) )
					ShowGeometricData(NORMAL,etype);
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
596
597
598
	
	void Mesh::PrepareGeometricData(GeomParam table)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
599
		std::sort(&*table.begin(),&*table.end());
Kirill Terekhov's avatar
Kirill Terekhov committed
600
601
602
603
604
605
		for(GeomParam::iterator it = table.begin(); it != table.end(); ++it)
		{
			GeometricData types = it->first;
			ElementType mask = it->second;
			if( types == ORIENTATION )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
606
				//std::cout << "ORIENTATION" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
607
				if( mask & FACE )
Kirill Terekhov's avatar
Kirill Terekhov committed
608
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
609
610
611
#if defined(USE_OMP)
#pragma omp parallel for
#endif
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
612
613
614
615
616
					for(integer e = 0; e < FaceLastLocalID(); ++e) 
					{
						if( isValidElement(FACE,e) )
							Face(this,ComposeHandle(FACE,e))->FixNormalOrientation();
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
617
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
618
619
620
621
				ShowGeometricData(ORIENTATION,FACE);
			}
			if( types == MEASURE )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
622
				//std::cout << "MEASURE" << std::endl;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
623
				for(ElementType etype = EDGE; etype <= CELL; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
624
625
626
				{
					if( (mask & etype) && !HaveGeometricData(MEASURE,etype))
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
627
						measure_tag = CreateTag(measure_name,DATA_REAL,etype,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
628
629
630
#if defined(USE_OMP)
#pragma omp parallel for
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
631
632
633
634
635
						for(integer e = 0; e < LastLocalID(etype); ++e) if( isValidElement(etype,e) )
						{
							HandleType h = ComposeHandle(etype,e);
							GetGeometricData(h,MEASURE,static_cast<Storage::real *>(MGetDenseLink(h,measure_tag)));
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
636
637
638
639
640
641
						ShowGeometricData(MEASURE,etype);
					}
				}
			}
			if( types == CENTROID )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
642
				//std::cout << "CENTROID" << std::endl;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
643
				for(ElementType etype = EDGE; etype <= CELL; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
644
645
646
				{
					if( (mask & etype) && !HaveGeometricData(CENTROID,etype))
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
647
						centroid_tag = CreateTag(centroid_name,DATA_REAL,etype,NONE,GetDimensions());
Kirill Terekhov's avatar
Kirill Terekhov committed
648
649
650
#if defined(USE_OMP)
#pragma omp parallel for
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
651
						for(integer k = 0; k < LastLocalID(etype); ++k) if( isValidElement(etype,k) )
Kirill Terekhov's avatar
Kirill Terekhov committed
652
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
653
654
							HandleType h = ComposeHandle(etype,k);
							GetGeometricData(h,CENTROID,static_cast<Storage::real *>(MGetDenseLink(h,centroid_tag)));
Kirill Terekhov's avatar
Kirill Terekhov committed
655
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
656
657
658
659
660
661
						ShowGeometricData(CENTROID,etype);
					}
				}
			}
			if( types == BARYCENTER )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
662
				//std::cout << "BARYCENTER" << std::endl;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
663
				for(ElementType etype = EDGE; etype <= CELL; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
664
665
666
				{
					if( (mask & etype) && !HaveGeometricData(BARYCENTER,etype))
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
667
						barycenter_tag = CreateTag(barycenter_name,DATA_REAL,etype,NONE,GetDimensions());
Kirill Terekhov's avatar
Kirill Terekhov committed
668
669
670
#if defined(USE_OMP)
#pragma omp parallel for
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
671
672
673
674
675
						for(integer e = 0; e < LastLocalID(etype); ++e) if( isValidElement(etype,e) )
						{
							HandleType h = ComposeHandle(etype,e);
							GetGeometricData(h,BARYCENTER,static_cast<Storage::real *>(MGetDenseLink(h,barycenter_tag)));
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
676
677
678
679
680
681
						ShowGeometricData(BARYCENTER,etype);
					}
				}	
			}
			if( types == NORMAL )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
682
				//std::cout << "NORMAL" << std::endl;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
683
				for(ElementType etype = FACE; etype <= CELL; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
684
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
685
686
					if( (mask & etype) && !HaveGeometricData(NORMAL,etype))
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
687
						normal_tag = CreateTag(normal_name,DATA_REAL,etype,NONE,GetDimensions());
Kirill Terekhov's avatar
Kirill Terekhov committed
688
689
690
#if defined(USE_OMP)
#pragma omp parallel for
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
691
692
693
694
695
						for(integer e = 0; e < LastLocalID(etype); ++e) if( isValidElement(etype,e) )
						{
							HandleType h = ComposeHandle(etype,e);
							GetGeometricData(h,NORMAL,static_cast<Storage::real *>(MGetDenseLink(h,normal_tag)));
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
696
697
						ShowGeometricData(NORMAL,etype);
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
698
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
699
700
701
702
			}
		}
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
703
	void Mesh::GetGeometricData(HandleType e, GeometricData type, Storage::real * ret)
Kirill Terekhov's avatar
Kirill Terekhov committed
704
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
705
706
707
		assert(e != InvalidHandle());
		assert(ret != NULL);
		assert(type == MEASURE || type == CENTROID || type == BARYCENTER || type == NORMAL);
Kirill Terekhov's avatar
Kirill Terekhov committed
708
709
710
		ElementType etype = GetHandleElementType(e);
		integer edim = Element::GetGeometricDimension(GetGeometricType(e));
		integer mdim = GetDimensions();
Kirill Terekhov's avatar
Kirill Terekhov committed
711
712
713
714
715
		switch(type)
		{
			case MEASURE:
			if( HaveGeometricData(MEASURE,etype) )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
716
				*ret = static_cast<Storage::real *>(MGetDenseLink(e,measure_tag))[0];
Kirill Terekhov's avatar
Kirill Terekhov committed
717
718
719
720
721
722
723
724
725
				//~ if( isnan(*ret) || fabs(*ret) < 1e-15  ) throw -1;
			}
			else
			{
				switch(edim)
				{
					case 0: *ret = 0; break;
					case 1: //length of edge
					{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
726
						ElementArray<Node> nodes = Element(this,e)->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
727
728
729
730
731
732
733
						if( nodes.size() > 1 )
						{
							Storage::real c[3];
							vec_diff(nodes[0]->Coords().data(),nodes[1]->Coords().data(),c,mdim);
							*ret = vec_len(c,mdim);
						}
						else *ret = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
734
735
736
737
738
						//~ if( isnan(*ret) || fabs(*ret) < 1e-15  ) throw -1;
						break;
					}
					case 2: //area of face
					{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
739
						ElementArray<Node> nodes = Element(this,e)->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
740
						if( nodes.size() > 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
741
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
742
743
744
							real x[3] = {0,0,0};
							Storage::real_array x0 = nodes[0].Coords();
							for(ElementArray<Node>::size_type i = 1; i < nodes.size()-1; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
745
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
746
747
748
749
750
751
752
753
								Storage::real_array v1 = nodes[i].Coords();
								Storage::real_array v2 = nodes[i+1].Coords();
								if( mdim == 3 )
								{
									x[0] += (v1[1]-x0[1])*(v2[2]-x0[2]) - (v1[2]-x0[2])*(v2[1]-x0[1]);
									x[1] += (v1[2]-x0[2])*(v2[0]-x0[0]) - (v1[0]-x0[0])*(v2[2]-x0[2]);
								}
								x[2] += (v1[0]-x0[0])*(v2[1]-x0[1]) - (v1[1]-x0[1])*(v2[0]-x0[0]);
Kirill Terekhov's avatar
Kirill Terekhov committed
754
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
755
							*ret = ::sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2])*0.5;
Kirill Terekhov's avatar
Kirill Terekhov committed
756
						} else *ret = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
757
758
759
760
761
						//~ if( isnan(*ret) || fabs(*ret) < 1e-15  ) throw -1;
						break;
					}
					case 3: //volume of cell
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
762
						
Kirill Terekhov's avatar
Kirill Terekhov committed
763
764
						Cell me = Cell(this,e);
						ElementArray<Face> faces = me->getFaces();
Kirill Terekhov's avatar
Kirill Terekhov committed
765
						*ret = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
766
767
768
769
						//can copy orientation-independent algorithm from
						//incident_matrix.hpp: incident_matrix::compute_measure
						//assume mdim is of size 3 at most
						dynarray<Storage::real,3> fcnt(mdim), fnrm(mdim), ccnt(mdim);
770
						/*
Kirill Terekhov's avatar
Kirill Terekhov committed
771
772
						me->Centroid(ccnt.data());
						for(ElementArray<Face>::size_type i = 0; i < faces.size(); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
773
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
774
775
776
777
778
779
780
							faces[i]->Centroid(fcnt.data());
							for(int r = 0; r < mdim; ++r)
								fcnt[r] = fcnt[r]-ccnt[r];
							faces[i]->OrientedNormal(me,fnrm.data());
							*ret += vec_dot_product(fcnt.data(),fnrm.data(),mdim);
						}
						if( *ret < 0.0 ) //a robust algorithm that can handle unoriented cell
781
						 */
Kirill Terekhov's avatar
Kirill Terekhov committed
782
						if( !faces.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
783
						{
784
							//real was = *ret/3.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
							*ret = 0;
							Face cur = faces[0];
							Cell c1 = me;
							Mesh * mesh = c1.GetMeshLink();
							//firstly, have to figure out orientation of each face
							//mark all faces, so that we can perform adjacency retrival
							MarkerType mrk = mesh->CreatePrivateMarker();
							MarkerType rev = mesh->CreatePrivateMarker(); //reverse orientation
							faces.SetPrivateMarker(mrk); //0-th face orientation is default
							cur->RemPrivateMarker(mrk);
							Node n1,n2; //to retrive edge
							bool reverse = false; //reverse orientation in considered face
							std::deque< orient_face > stack; //edge and first node and face for visiting
							ElementArray<Edge> edges = cur->getEdges();
							do
							{
								//figure out starting node order
								if( edges[0]->getBeg() == edges[1]->getBeg() ||
								    edges[0]->getBeg() == edges[1]->getEnd() )
								{
									n1 = edges[0]->getEnd();
									n2 = edges[0]->getBeg();
								}
								else
								{
									n1 = edges[0]->getBeg();
									n2 = edges[0]->getEnd();
								}
								//schedule unvisited adjacent faces
								for(unsigned j = 0; j < edges.size(); j++)
								{
									//schedule face adjacent to considered edge
									ElementArray<Face> adjacent = edges[j]->getFaces(mrk);
									assert(adjacent.size() <= 1);
									if( !adjacent.empty() )
									{
										adjacent.RemPrivateMarker(mrk);
										stack.push_back(orient_face(edges[j],reverse ? n2 : n1,adjacent[0]));
									}
									//update edge nodes
									n1 = n2; //current end is new begin
									//find new end
									if( n2 == edges[(j+1)%edges.size()]->getBeg() )
										n2 = edges[(j+1)%edges.size()]->getEnd();
									else
										n2 = edges[(j+1)%edges.size()]->getBeg();
								}
								if( stack.empty() ) break;
								//get entry from stack
								orient_face r = stack.front();
								//remove face from stack
								stack.pop_front();
								//retrive edges for new face
								edges = r.face->getEdges();
								reverse = false;
								//figure out starting node order
								if( edges[0]->getBeg() == edges[1]->getBeg() ||
								   edges[0]->getBeg() == edges[1]->getEnd() )
								{
									n1 = edges[0]->getEnd();
									n2 = edges[0]->getBeg();
								}
								else
								{
									n1 = edges[0]->getBeg();
									n2 = edges[0]->getEnd();
								}
								//find out common edge orientation
								for(unsigned j = 0; j < edges.size(); j++)
								{
									if( edges[j] == r.bridge ) //found the edge
									{
										//reverse ordering on this face
										if( r.first == n1 )
										{
											r.face->SetPrivateMarker(rev);
											reverse = true;
										}
										break;
									}
									//update edge nodes
									n1 = n2; //current end is new begin
									//find new end
									if( n2 == edges[(j+1)%edges.size()]->getBeg() )
										n2 = edges[(j+1)%edges.size()]->getEnd();
									else
										n2 = edges[(j+1)%edges.size()]->getBeg();
								}
							} while(true);
							faces.RemPrivateMarker(mrk);
							mesh->ReleasePrivateMarker(mrk);
876
							c1->Centroid(ccnt.data());
Kirill Terekhov's avatar
Kirill Terekhov committed
877
							for(unsigned j = 0; j < faces.size(); j++)
Kirill Terekhov's avatar
Kirill Terekhov committed
878
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
879
								//compute normal to face
880
881
882
883
884
								faces[j].Centroid(fcnt.data());
								faces[j].Normal(fnrm.data());
								for(int r = 0; r < mdim; ++r)
									fcnt[r] = fcnt[r]-ccnt[r];
								*ret += (faces[j]->GetPrivateMarker(rev) ? -1.0 : 1.0)*vec_dot_product(fcnt.data(),fnrm.data(),3);
Kirill Terekhov's avatar
Kirill Terekhov committed
885
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
886
887
888
889
890
							*ret = fabs(*ret);
							faces.RemPrivateMarker(rev);
							mesh->ReleasePrivateMarker(rev);
							
							//std::cout << "volume is " << *ret/3.0 << " was " << was << " for " << me->LocalID() << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
891
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
892
893
						*ret /= 3.0;
						
Kirill Terekhov's avatar
Kirill Terekhov committed
894
895
896
897
898
899
900
901
						break;
					}
				}
			}
			//~ if( isnan(*ret) || fabs(*ret) < 1e-15  ) throw -1;
			break;
			case CENTROID:
			if(etype == NODE )
Kirill Terekhov's avatar
Kirill Terekhov committed
902
				memcpy(ret,MGetDenseLink(e,CoordsTag()),sizeof(real)*mdim);
Kirill Terekhov's avatar
Kirill Terekhov committed
903
904
			else if(HaveGeometricData(CENTROID,etype))
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
905
				memcpy(ret,MGetDenseLink(e,centroid_tag),sizeof(real)*mdim);
Kirill Terekhov's avatar
Kirill Terekhov committed
906
907
908
			}
			else
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
909
				ElementArray<Node> nodes = Element(this,e)->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
910
				memset(ret,0,sizeof(real)*mdim);
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
911
				if(nodes.size() != 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
912
				{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
913
914
915
916
917
918
919
					Storage::real div = 1.0/nodes.size();
					for(ElementArray<Node>::size_type i = 0; i < nodes.size(); i++)
					{
						Storage::real_array c =nodes[i].Coords();
						for(integer j = 0; j < mdim; j++) ret[j] += c[j];
					}
					for(integer j = 0; j < mdim; j++) ret[j] *= div;
Kirill Terekhov's avatar
Kirill Terekhov committed
920
921
922
923
924
				}
			}
			break;
			case BARYCENTER:
			if( etype == NODE )
Kirill Terekhov's avatar
Kirill Terekhov committed
925
				memcpy(ret,MGetDenseLink(e,CoordsTag()),sizeof(real)*mdim);
Kirill Terekhov's avatar
Kirill Terekhov committed
926
			else if(HaveGeometricData(BARYCENTER,etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
927
				memcpy(ret,MGetDenseLink(e,barycenter_tag),sizeof(real)*mdim);
Kirill Terekhov's avatar
Kirill Terekhov committed
928
929
930
931
932
			else
			{
				memset(ret,0,sizeof(real)*mdim);
				if( edim == 1 )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
933
					ElementArray<Node> n = Element(this,e)->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
934
935
936
937
938
939
940
941
942
943
944
945
					if( n.size() == 2 )
					{
						Storage::real_array a = n[0].Coords();
						Storage::real_array b = n[1].Coords();
						for(integer j = 0; j < dim; j++) 
							ret[j] = (a[j] + b[j])*0.5;
					}
					else if( n.size() == 1 )
					{
						Storage::real_array a = n[0].Coords();
						for(integer j = 0; j < dim; j++) ret[j] = a[j];
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
946
947
948
				}
				else if( edim == 2 )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
949
					ElementArray<Node> nodes = Element(this,e)->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
950
951
952
953
954
955
956
957
958
					real s,d, x1[3] = {0,0,0},x2[3] = {0,0,0},x[3] = {0,0,0};
					//here we compute area of polygon
					//~ if( HaveGeometricData(MEASURE,etype) && HaveGeometricData(NORMAL,etype) )
					//~ {
						//~ s = e->RealDF(measure_tag);
						//~ memcpy(x,&e->RealDF(normal_tag),sizeof(Storage::real)*mdim);
					//~ }
					//~ else
					//~ {
Kirill Terekhov's avatar
Kirill Terekhov committed
959
					if( nodes.size() > 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
960
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
961
962
						Storage::real_array x0 = nodes[0].Coords();
						for(ElementArray<Node>::size_type i = 1; i < nodes.size()-1; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
963
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
964
965
966
967
968
969
970
971
							Storage::real_array v1 = nodes[i].Coords();
							Storage::real_array v2 = nodes[i+1].Coords();
							if( mdim == 3 )
							{
								x[0] += (v1[1]-x0[1])*(v2[2]-x0[2]) - (v1[2]-x0[2])*(v2[1]-x0[1]);
								x[1] += (v1[2]-x0[2])*(v2[0]-x0[0]) - (v1[0]-x0[0])*(v2[2]-x0[2]);
							}
							x[2] += (v1[0]-x0[0])*(v2[1]-x0[1]) - (v1[1]-x0[1])*(v2[0]-x0[0]);
Kirill Terekhov's avatar
Kirill Terekhov committed
972
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
973
						s = ::sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
						x[0] /= s; x[1] /= s; x[2] /= s; //here we obtain the unit normal
						//~ }
						//here we compute the center
						Storage::real_array v0 = nodes[0].Coords();
						for(ElementArray<Node>::size_type j = 1; j < nodes.size()-1; j++)
						{
							Storage::real_array v1 = nodes[j].Coords();
							Storage::real_array v2 = nodes[j+1].Coords();
							for(integer k = 0; k < mdim; k++)
							{
								x1[k] = v0[k] - v1[k];
								x2[k] = v0[k] - v2[k];
							}
							d = det3v(x1,x2,x); //here we use unit normal
							for(integer k = 0; k < mdim; k++) 
								ret[k] += d*(v0[k]+v1[k]+v2[k]);
						}
						for(integer k = 0; k < mdim; k++) ret[k] /= 3.0 * s;
Kirill Terekhov's avatar
Kirill Terekhov committed
992
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
993
					else
Kirill Terekhov's avatar
Kirill Terekhov committed
994
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
995
						for(ElementArray<Node>::size_type i = 0; i < nodes.size(); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
996
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
997
998
							Storage::real_array c = nodes[i].Coords();
							for(integer k = 0; k < mdim; k++) ret[k] += c[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
999
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1000
						for(integer k = 0; k < mdim; k++) ret[k] /= static_cast<Storage::real>(nodes.size());
Kirill Terekhov's avatar
Kirill Terekhov committed
1001
1002
1003
1004
					}
				}
				else if( edim == 3 )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1005
					ElementArray<Face> faces = Element(this,e)->getFaces();
Kirill Terekhov's avatar
Kirill Terekhov committed
1006
					real d,c,vol = 0, y[3];
Kirill Terekhov's avatar
Kirill Terekhov committed
1007
					for(ElementArray<Face>::size_type i = 0; i < faces.size(); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1008
1009
					{
						d = y[0] = y[1] = y[2] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1010
						ElementArray<Node> nodes = faces[i].getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
1011
						Storage::real_array v0 = nodes[0].Coords();
Kirill Terekhov's avatar
Kirill Terekhov committed
1012
						for(ElementArray<Node>::size_type j = 1; j < nodes.size()-1; j++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1013
1014
1015
						{
							Storage::real_array v1 = nodes[j].Coords();
							Storage::real_array v2 = nodes[j+1].Coords();
Kirill Terekhov's avatar
Kirill Terekhov committed
1016
							c = det3v(v0.data(),v1.data(),v2.data());
Kirill Terekhov's avatar
Kirill Terekhov committed
1017
1018
1019
1020
1021
							d += c;
							y[0] += c * (v0[0] + v1[0] + v2[0]);
							y[1] += c * (v0[1] + v1[1] + v2[1]);
							y[2] += c * (v0[2] + v1[2] + v2[2]);
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1022
						c = faces[i].FaceOrientedOutside(Cell(this,e)) ? 1 : -1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
						ret[0] += c * y[0];
						ret[1] += c * y[1];
						ret[2] += c * y[2];
						vol += c*d;
					}
					ret[0] /= vol*4;
					ret[1] /= vol*4;
					ret[2] /= vol*4;
				}
			}
			break;
			case NORMAL:
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
1037
1038
//				real sret[3];
//				bool cmp = false;
				if( HaveGeometricData(NORMAL,etype) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1039
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1040
1041
					memcpy(ret,MGetDenseLink(e,normal_tag),sizeof(real)*mdim);
//					cmp = true;
Kirill Terekhov's avatar
Kirill Terekhov committed
1042
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1043
				else
Kirill Terekhov's avatar
Kirill Terekhov committed
1044
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
					memset(ret,0,sizeof(real)*mdim);
					if( edim == 2 )//&& mdim == 3)
					{
						ElementArray<Node> nodes = Element(this,e)->getNodes();
						
						Storage::real_array x0 = nodes[0].Coords(), a = x0, b;
						for(ElementArray<Node>::size_type i = 0; i < nodes.size(); i++)
						{
							b = nodes[(i+1)%nodes.size()].Coords();
							ret[0] += (a[1]-x0[1])*(b[2]-x0[2]) - (a[2]-x0[2])*(b[1]-x0[1]);
							ret[1] += (a[2]-x0[2])*(b[0]-x0[0]) - (a[0]-x0[0])*(b[2]-x0[2]);
							ret[2] += (a[0]-x0[0])*(b[1]-x0[1]) - (a[1]-x0[1])*(b[0]-x0[0]);
							a.swap(b);
						}
						/*
						 for(unsigned i = 0; i < nodes.size(); i++)
						 {
						 Storage::real_array a = nodes[i].Coords();
						 Storage::real_array b = nodes[(i+1)%nodes.size()].Coords();
						 ret[0] += a[1]*b[2] - a[2]*b[1];
						 ret[1] += a[2]*b[0] - a[0]*b[2];
						 ret[2] += a[0]*b[1] - a[1]*b[0];
						 }
						 */
						ret[0] *= 0.5;
						ret[1] *= 0.5;
						ret[2] *= 0.5;
					}
					else if( edim == 1 )//&& mdim == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1074
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1075
1076
						ElementArray<Node> nodes = Element(this,e)->getNodes();
						if( nodes.size() > 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1077
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
							Storage::real_array a = nodes[0].Coords();
							Storage::real_array b = nodes[1].Coords();
							ret[0] = b[1] - a[1];
							ret[1] = a[0] - b[0];
							Storage::real l = ::sqrt(ret[0]*ret[0]+ret[1]*ret[1]);
							if( l )
							{
								ret[0] /= l;
								ret[1] /= l;
							}
							l = ::sqrt((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1]));
							ret[0] *= l;
							ret[1] *= l;
Kirill Terekhov's avatar
Kirill Terekhov committed
1091
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1092
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
//					real err = 0;
//					for(int k = 0; k < 3; ++k)
//						err += (ret[0]-sret[0])*(ret[0]-sret[0]);
//					err = sqrt(err);
//					if( err > 1.0e-8 && cmp )
//					{
//						std::cout << "face " << GetHandleID(e) << " rank " << GetProcessorRank() <<  std::endl;
//						std::cout << "cn " << ret[0] << " " << ret[1] << " " << ret[2] << std::endl;
//						std::cout << "sn " << sret[0] << " " << sret[1] << " " << sret[2] << std::endl;
//					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
				}
			}
			break;
		}
		//~ if( type == MEASURE )
		//~ {
			//~ if( isnan(*ret) || fabs(*ret) < 1e-15  ) throw -1;
		//~ }
	}
	


Kirill Terekhov's avatar
Kirill Terekhov committed
1115
	bool Element::Planarity() const
Kirill Terekhov's avatar
Kirill Terekhov committed
1116
1117
	{
		Mesh * m = GetMeshLink();
Kirill Terekhov's avatar
Kirill Terekhov committed
1118
		integer dim = m->GetDimensions();
Kirill Terekhov's avatar
Kirill Terekhov committed
1119
		if( dim < 3 ) return true;
Kirill Terekhov's avatar
Kirill Terekhov committed
1120
		ElementArray<Node> p = getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
1121
		if( p.size() <= 3 ) return true;
Kirill Terekhov's avatar
Kirill Terekhov committed
1122
		ElementArray<Node>::size_type i, s = p.size();
Kirill Terekhov's avatar
Kirill Terekhov committed
1123
		Storage::real v[2][3] = {{0,0,0},{0,0,0}};
Kirill Terekhov's avatar
Kirill Terekhov committed
1124
1125
		vec_diff(p[1].Coords().data(),p[0].Coords().data(),v[0],3);
		vec_diff(p[2].Coords().data(),p[0].Coords().data(),v[1],3);
Kirill Terekhov's avatar
Kirill Terekhov committed
1126
1127
1128
		vec_cross_product(v[0],v[1],v[1]);
		for(i = 3; i < s; i++)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1129
			vec_diff(p[i].Coords().data(),p[0].Coords().data(),v[0],3);
Kirill Terekhov's avatar
Kirill Terekhov committed
1130
			if( ::fabs(vec_dot_product(v[0],v[1],3)) > m->GetEpsilon() ) return false;
Kirill Terekhov's avatar
Kirill Terekhov committed
1131
1132
1133
1134
1135
		}
		return true;
	}


Kirill Terekhov's avatar
Kirill Terekhov committed
1136
	bool Cell::Closure() const
Kirill Terekhov's avatar
Kirill Terekhov committed
1137
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1138
1139
		adj_type & lc = GetMeshLink()->LowConn(GetHandle());
		return lc.size() > 0 ? GetMeshLink()->TestClosure(lc.data(),static_cast<integer>(lc.size())) : false;
Kirill Terekhov's avatar
Kirill Terekhov committed
1140
1141
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
1142
	bool Face::Closure() const
Kirill Terekhov's avatar
Kirill Terekhov committed
1143
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1144
1145
		adj_type & lc = GetMeshLink()->LowConn(GetHandle());
		return lc.size() > 0 ? GetMeshLink()->TestClosure(lc.data(),static_cast<integer>(lc.size())) : false;
Kirill Terekhov's avatar
Kirill Terekhov committed
1146
1147
1148
1149
1150
1151
1152
1153
	}



	

	

Kirill Terekhov's avatar
Kirill Terekhov committed
1154
	bool Element::Boundary() const
Kirill Terekhov's avatar
Kirill Terekhov committed
1155
1156
1157
1158
	{
		switch(GetElementType())
		{
			case FACE:
Kirill Terekhov's avatar
Feature    
Kirill Terekhov committed
1159
1160
				if( !getAsFace()->FrontCell().isValid() )
					return true;
Kirill Terekhov's avatar
Kirill Terekhov committed
1161
				return false;
Kirill Terekhov's avatar
Kirill Terekhov committed
1162
			case CELL:
Kirill Terekhov's avatar
Kirill Terekhov committed
1163
1164
1165
			case EDGE:
			case NODE:
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1166
1167
				ElementArray<Element> faces = getAdjElements(FACE);
				for(ElementArray<Element>::iterator it = faces.begin(); it != faces.end(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1168
1169
1170
1171
1172
1173
1174
1175
1176
					if( it->Boundary() ) return true;
				return false;
			}
			default: return false;
		}
		return false;
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1177
1178
1179
1180
1181
1182
1183
1184
	
	std::ostream & spaces(std::ostream & out, int print)
	{
		while(print) {out << " "; print--;}
		return out;
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1185
	bool Face::CheckNormalOrientation() const
Kirill Terekhov's avatar
Kirill Terekhov committed
1186
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1187
		Mesh * mesh = GetMeshLink();
1188
        //integer dim = mesh->GetDimensions();
Kirill Terekhov's avatar
Kirill Terekhov committed
1189
1190
		Cell c1 = BackCell();
		if( c1.isValid() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1191
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
			Storage::real measure = 0;
			ElementArray<Face> data = c1.getFaces();
			Face cur = *this;
			//firstly, have to figure out orientation of each face
			//mark all faces, so that we can perform adjacency retrival
			MarkerType mrk = mesh->CreatePrivateMarker();
			MarkerType rev = mesh->CreatePrivateMarker(); //reverse orientation
			data.SetPrivateMarker(mrk); //0-th face orientation is default
			cur->RemPrivateMarker(mrk);
			Node n1,n2; //to retrive edge
			bool reverse = false; //reverse orientation in considered face
			std::deque< orient_face > stack; //edge and first node and face for visiting
			ElementArray<Edge> edges = cur->getEdges();
			do
Kirill Terekhov's avatar
Kirill Terekhov committed
1206
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
				//figure out starting node order
				if( edges[0]->getBeg() == edges[1]->getBeg() ||
				    edges[0]->getBeg() == edges[1]->getEnd() )
				{
					n1 = edges[0]->getEnd();
					n2 = edges[0]->getBeg();
				}
				else
				{
					n1 = edges[0]->getBeg();
					n2 = edges[0]->getEnd();
				}
				//schedule unvisited adjacent faces
				for(unsigned j = 0; j < edges.size(); j++)
				{
					//schedule face adjacent to considered edge
					ElementArray<Face> adjacent = edges[j]->getFaces(mrk);
					assert(adjacent.size() <= 1);
					if( !adjacent.empty() )
					{
						adjacent.RemPrivateMarker(mrk);
						stack.push_back(orient_face(edges[j],reverse ? n2 : n1,adjacent[0]));
					}
					//update edge nodes
					n1 = n2; //current end is new begin
					//find new end
					if( n2 == edges[(j+1)%edges.size()]->getBeg() )
						n2 = edges[(j+1)%edges.size()]->getEnd();
					else
						n2 = edges[(j+1)%edges.size()]->getBeg();
				}
				if( stack.empty() ) break;
				//get entry from stack
				orient_face r = stack.front();
				//remove face from stack
				stack.pop_front();
				//retrive edges for new face
				edges = r.face->getEdges();
				reverse = false;
				//figure out starting node order
				if( edges[0]->getBeg() == edges[1]->getBeg() ||
				   edges[0]->getBeg() == edges[1]->getEnd() )
				{
					n1 = edges[0]->getEnd();
					n2 = edges[0]->getBeg();
				}
				else
				{
					n1 = edges[0]->getBeg();
					n2 = edges[0]->getEnd();
				}
				//find out common edge orientation
				for(unsigned j = 0; j < edges.size(); j++)
				{
					if( edges[j] == r.bridge ) //found the edge
					{
						//reverse ordering on this face
						if( r.first == n1 )
						{
							r.face->SetPrivateMarker(rev);
							reverse = true;
						}
						break;
					}
					//update edge nodes
					n1 = n2; //current end is new begin
					//find new end
					if( n2 == edges[(j+1)%edges.size()]->getBeg() )
						n2 = edges[(j+1)%edges.size()]->getEnd();
					else
						n2 = edges[(j+1)%edges.size()]->getBeg();
				}
			} while(true);
			data.RemPrivateMarker(mrk);
			mesh->ReleasePrivateMarker(mrk);
			Storage::real nrm[3], cnt[3], ccnt[3];
			c1->Centroid(ccnt);
			for(unsigned j = 0; j < data.size(); j++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1286
				//std::cout << (data[j].GetPrivateMarker(rev) ? 0:1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1287
1288
1289
1290
1291
1292
				//compute normal to face
				data[j].Centroid(cnt);
				data[j].Normal(nrm);
				for(int r = 0; r < 3; ++r)
					cnt[r] = cnt[r]-ccnt[r];
				measure += (data[j]->GetPrivateMarker(rev) ? -1.0 : 1.0)*vec_dot_product(cnt,nrm,3);
Kirill Terekhov's avatar
Kirill Terekhov committed
1293
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1294
			//std::cout << "cur" << (cur->GetPrivateMarker(rev) ? 0:1) << " " << measure << " ";
1295
            //bool have_rev = cur->GetPrivateMarker(rev);
Kirill Terekhov's avatar
Kirill Terekhov committed
1296
1297
1298
			data.RemPrivateMarker(rev);
			mesh->ReleasePrivateMarker(rev);
			if( (measure < 0 ))// && !have_rev) || (measure > 0 && have_rev))
Kirill Terekhov's avatar
Kirill Terekhov committed
1299
1300
1301
1302
1303
				return false;
		}
		return true;
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1304
	bool Face::FixNormalOrientation() const
Kirill Terekhov's avatar
Kirill Terekhov committed
1305
1306
	{
		if( !CheckNormalOrientation() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1307
1308
1309
1310
		{
			if( FrontCell().isValid() )
				SwapCells();
			else
Kirill Terekhov's avatar
Kirill Terekhov committed
1311
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1312
1313
1314
1315
1316
1317
1318
				ReorderEdges(); //this is not thread-safe with respect to CheckNormalOrientation
				if( GetMeshLink()->HaveGeometricData(NORMAL,FACE) )
				{
					real_array nrm = GetMeshLink()->RealArrayDF(GetHandle(),GetMeshLink()->GetGeometricTag(NORMAL));
					for(real_array::size_type it = 0; it < nrm.size(); ++it)
						nrm[it] = -nrm[it];
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1319
1320
1321
1322
1323
1324
			}
			return true;
		}
		return false;
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
1325
	Storage::real meantri(Storage::real * v0, Storage::real * v1, Storage::real * v2, Storage::integer dim, Storage::real (*func)(Storage::real* x,Storage::real), Storage::real time)
Kirill Terekhov's avatar
Kirill Terekhov committed
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
	{
		Storage::real value = 0;
		static const Storage::real w[4] =   { -0.149570044467670, 0.175615257433204, 0.053347235608839 , 0.077113760890257};
		static const Storage::real a[4][3] =
		{
			{0.333333333333333,0.333333333333333,0.333333333333333},
			{0.479308067841923,0.260345966079038,0.260345966079038},
			{0.869739794195568,0.065130102902216,0.065130102902216},
			{0.638444188569809,0.312865496004875,0.048690315425316}
		};
		Storage::real XYG[13][3];
Kirill Terekhov's avatar
Kirill Terekhov committed
1337
		for (Storage::integer i = 0 ; i < dim; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1338
1339
1340
1341
			XYG[0][i] = 0.33333333333333333333*(v0[i]+v1[i]+v2[i]);
		 value += w[0] * func(XYG[0],time);
		for (int i = 0 ; i < 3 ; i++ )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1342
			for (Storage::integer j = 0 ; j < dim; j++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1343
1344
1345
1346
1347
				XYG[1+i][j] = v0[j] + (v1[j] - v0[j]) * a[1][i] + (v2[j] - v0[j])*a[1][(i+1)%3];
			value += w[1] * func