geometry.cpp 69 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
	__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];
	}
	
30
31
32
33
34
35
	__INLINE static void vec_diff(const Storage::real_array & 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];
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
36
37
38
39
40
41
42
	__INLINE static void vec_diff(const Storage::real_array & vecin1,const Storage::real_array & vecin2, Storage::real * vecout, unsigned int size)
	{
		for(unsigned int i = 0; i < size; i++)
			vecout[i] = vecin1[i] - vecin2[i];
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
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
	__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
69
		return ::sqrt(vec_len2(vecin,size));
Kirill Terekhov's avatar
Kirill Terekhov committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
	}
	
	__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
95
		len = ::sqrt(len);
Kirill Terekhov's avatar
Kirill Terekhov committed
96
97
98
99
		for(unsigned int i = 0; i < size; i++)
			vecin[i] /= len;
		return len;
	}
100
101
102
103
104
105
106
107
108
109
110
111
112

	__INLINE static Storage::real triarea3d(const Storage::real p1[3], const Storage::real p2[3], const Storage::real p3[3])
	{
		Storage::real v12[3], v13[3], v23[3], l12, l13, l23, halfperim;
		vec_diff(p2,p1,v12,3);
		vec_diff(p3,p1,v13,3);
		vec_diff(p3,p2,v23,3);
		l12 = vec_len(v12,3);
		l13 = vec_len(v13,3);
		l23 = vec_len(v23,3);
		halfperim = 0.5*(l12+l13+l23);
		return sqrt(halfperim*(halfperim-l12)*(halfperim-l13)*(halfperim-l23));
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
113
114
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
115
	ElementArray<Cell> Cell::NeighbouringCells() const
Kirill Terekhov's avatar
Kirill Terekhov committed
116
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
117
118
119
		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
120
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
121
122
			Cell c = Neighbour(f->self());
			if( c.isValid() ) ret.push_back(c);
Kirill Terekhov's avatar
Kirill Terekhov committed
123
124
125
126
127
		}
		return ret;
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
128
	Cell Cell::Neighbour(Face f) const
Kirill Terekhov's avatar
Kirill Terekhov committed
129
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
130
131
		Cell b = f->BackCell();
		if( b == self() )
Kirill Terekhov's avatar
Kirill Terekhov committed
132
133
134
			return f->FrontCell();
		return b;
	}
135
136
137
138

	bool Face::Inside(const Storage::real * point) const
	{
		Mesh * mesh = GetMeshLink();
139
		real eps = mesh->GetEpsilon();
140
		integer dim = mesh->GetDimensions();
141
142
143
		integer mdim = GetElementDimension();
		
		if(mdim < 2)
144
		{
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
			// check whether point lies on edge
			real v1[3], v2[3], v12[3], r1[3], r2[3], nrm[3], area, h, len;
			ElementArray<Node> nodes = getNodes();
			assert(nodes.size() == 2);// 
			nodes[0].Centroid(v1);
			nodes[1].Centroid(v2);
			vec_diff(point,v1,r1,dim);
			vec_diff(point,v2,r2,dim);
			vec_diff(v2,v1,v12,dim);
			len = vec_len(v12,dim);
			assert(len > 1e-20);
			vec_cross_product(r1,v12,nrm);
			area = vec_len(nrm,dim);
			h = area / len;
			if( h > eps )	return false; // point does not lie on line
			return vec_dot_product(r1,r2,dim) <= 0.0; // point is between edge nodes
161
162
		}

163
		real nrm[3], cnt[3], v[3];
164
165
		UnitNormal(nrm);
		Centroid(cnt);
166
167
168
		vec_diff(cnt,point,v,dim);
		real d = vec_dot_product(nrm,v,dim);
		if(fabs(d) > eps)	return false;	// point is too far from the face plane
169

170
171
172
173
174
		// 2d algorithm from Cell::Inside 
		real data[9][3];
		for(int k = 0; k < 9; k++) for(int j = 0; j < 3; j++)	data[k][j] = 0;
		ElementArray<Node> nodes = getNodes();
		for(int i = 0; i < static_cast<int>(nodes.size()); i++)
175
		{
176
177
178
179
180
181
182
183
184
185
186
187
188
189
			int j = (i+1)%nodes.size();
			nodes[i].Centroid(data[1]);
			nodes[j].Centroid(data[2]);
			vec_diff(point,data[0],data[3],dim);
			vec_diff(point,data[1],data[4],dim);
			vec_diff(point,data[2],data[5],dim);
			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]);
			
			if( vec_dot_product(data[6],data[7],dim) >= 0 &&
				vec_dot_product(data[7],data[8],dim) >= 0 &&
				vec_dot_product(data[8],data[6],dim) >= 0 )
				return true; //inside one of the triangles
190
191
192
		}
		return false;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
193
	
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
194
	bool Cell::Inside(const Storage::real * point) const//check for 2d case
Kirill Terekhov's avatar
Kirill Terekhov committed
195
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
196
		Mesh * mesh = GetMeshLink();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
197
198
199
		integer dim = GetElementDimension();
		if( dim == 3 )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
200
			/*
201
			std::map<HandleType,real,16> hits;
Kirill Terekhov's avatar
Kirill Terekhov committed
202
203
204
205
206
207
208
209
210
			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
211
212
213
			integer vp = 0;
			integer vm = 0;
			integer vz = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
			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
307
308
			{
				d = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
309
310
311
312
313
314
315
316
317
318
319
320
321
				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
322
					c = 1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
323
324
325
326
327
				if(c*d > eps)
					vp++;
				else if(c*d < -eps)
					vm++;
				else
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
328
329
					vz++;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
330
331
			data.RemPrivateMarker(rev);
			mesh->ReleasePrivateMarker(rev);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
332
			if(vp*vm > 0) return false;
Kirill Terekhov's avatar
Kirill Terekhov committed
333
			else if( vz == 0 ) return true;
Kirill Terekhov's avatar
Kirill Terekhov committed
334
			else return true;
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
335
336
		}
		else
Kirill Terekhov's avatar
Kirill Terekhov committed
337
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
338
			int mdim = mesh->GetDimensions();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
339
340
341
342
343
344
345
346
			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
347
			for(int i = 0; i < static_cast<int>(nodes.size()); i++)
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
348
349
350
351
352
353
354
355
356
357
			{
				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
358
				
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
359
				if( vec_dot_product(data[6],data[7],mdim) >= 0 &&
Kirill Terekhov's avatar
Kirill Terekhov committed
360
361
				   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
362
363
364
					return true; //inside one of the triangles
			}
			return false;
Kirill Terekhov's avatar
Kirill Terekhov committed
365
366
367
368
		}
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
369
	void Face::UnitNormal(real * nrm) const
Kirill Terekhov's avatar
Kirill Terekhov committed
370
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
371
372
373
		Mesh * m = GetMeshLink();
		m->GetGeometricData(GetHandle(),NORMAL,nrm); 
		integer dim = m->GetDimensions();
Kirill Terekhov's avatar
Kirill Terekhov committed
374
		real    l   = ::sqrt(vec_dot_product(nrm,nrm,dim)); 
Kirill Terekhov's avatar
Kirill Terekhov committed
375
		if(::fabs(l) > m->GetEpsilon()) 
Kirill Terekhov's avatar
Kirill Terekhov committed
376
377
		{
			for(integer i = 0; i < dim; i++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
378
				nrm[i] /= l; 
Kirill Terekhov's avatar
Kirill Terekhov committed
379
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
380
381
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
382
	void Face::OrientedNormal(Cell c, Storage::real * nrm) const
Kirill Terekhov's avatar
Kirill Terekhov committed
383
384
	{
		Normal(nrm); 
Kirill Terekhov's avatar
Kirill Terekhov committed
385
386
387
388
		if( !FaceOrientedOutside(c) )
		{
			integer dim = GetMeshLink()->GetDimensions();
			for(integer i = 0; i < dim; i++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
389
				nrm[i] = -nrm[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
390
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
391
392
393
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
394
	void Face::OrientedUnitNormal(Cell c, Storage::real * nrm) const
Kirill Terekhov's avatar
Kirill Terekhov committed
395
396
397
	{
		UnitNormal(nrm); 
		if( !FaceOrientedOutside(c) ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
398
399
400
		{
			integer dim = GetMeshLink()->GetDimensions();
			for(integer i = 0; i < dim; i++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
401
				nrm[i] = -nrm[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
402
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
403
404
405
406
	}
	
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
407
	bool Mesh::TestClosure(const HandleType * elements, integer num) const
Kirill Terekhov's avatar
Kirill Terekhov committed
408
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
409
		integer i;
410
411
		std::map<HandleType,int> e_visit;
		std::map<HandleType,int>::iterator it;
Kirill Terekhov's avatar
Kirill Terekhov committed
412
413
414
415
		if( !HideMarker() )
		{
			for(i = 0; i < num; i++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
416
417
418
				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
419
420
421
422
			}
		}
		else
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
423
			for(i = 0; i < num; i++) if( !GetMarker(elements[i],HideMarker()) )
Kirill Terekhov's avatar
Kirill Terekhov committed
424
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
425
426
427
428
429
430
				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
431
432
433
434
435
436
437
			}
		}
		for(it = e_visit.begin(); it != e_visit.end(); it++)
			if( it->second != 2 ) return false;
		return true;
	}
	
438
	Element::GeometricType Mesh::ComputeGeometricType(ElementType etype, const HandleType * lc, INMOST_DATA_ENUM_TYPE size)
Kirill Terekhov's avatar
Kirill Terekhov committed
439
440
	{
		Element::GeometricType ret = Element::Unset;
441
442
443
444
		INMOST_DATA_ENUM_TYPE s = 0;
		if( isMeshModified() )
			s = Mesh::Count(lc,size,HideMarker());
		else s = size;
Kirill Terekhov's avatar
Kirill Terekhov committed
445
		int dmax = -1, dmin = 4;
Kirill Terekhov's avatar
Kirill Terekhov committed
446
447
448
449
450
451
452
453
454
455
456
		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:
457
				for (INMOST_DATA_ENUM_TYPE k = 0; k < size; ++k) if( !Hidden(lc[k]) )
Kirill Terekhov's avatar
Kirill Terekhov committed
458
459
460
461
462
463
464
				{
					int d = Element::GetGeometricDimension(GetGeometricType(lc[k]));
					if (dmax < d) dmax = d;
					if (dmin > d) dmin = d;
				}
				if (dmax != dmin)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
465
					ret = Element::MultiLine;
Kirill Terekhov's avatar
Kirill Terekhov committed
466
467
				}
				else if( dmax == 0 )
Kirill Terekhov's avatar
Kirill Terekhov committed
468
469
470
471
472
				{ 
					ret = Element::Line;
				}
				else
				{
473
					if( !GetTopologyCheck(NEED_TEST_CLOSURE) || TestClosure(lc,size) )
Kirill Terekhov's avatar
Kirill Terekhov committed
474
475
476
477
478
479
480
481
482
483
484
485
					{
						if( s == 3 )
							ret = Element::Tri;
						else if( s == 4 )
							ret = Element::Quad;
						else
							ret = Element::Polygon;
					}
					else ret = Element::MultiLine;
				}
				break;
			case CELL:
486
				for (INMOST_DATA_ENUM_TYPE k = 0; k < size; ++k) if( !Hidden(lc[k]) )
Kirill Terekhov's avatar
Kirill Terekhov committed
487
488
489
490
491
492
493
494
495
496
				{
					int d = Element::GetGeometricDimension(GetGeometricType(lc[k]));
					if (dmax < d) dmax = d;
					if (dmin > d) dmin = d;
				}
				if (dmax != dmin)
				{
					ret = Element::MultiPolygon;
				}
				else if(  dmax == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
497
				{
498
					if( !GetTopologyCheck(NEED_TEST_CLOSURE) || TestClosure(lc,size) )
Kirill Terekhov's avatar
Kirill Terekhov committed
499
500
501
502
503
504
505
506
507
508
509
510
					{
						if( s == 3 )
							ret = Element::Tri;
						else if( s == 4 )
							ret = Element::Quad;
						else
							ret = Element::Polygon;
					}
					else ret = Element::MultiLine;
				}
				else 
				{
511
					if( !GetTopologyCheck(NEED_TEST_CLOSURE) ||  TestClosure(lc,size) )
Kirill Terekhov's avatar
Kirill Terekhov committed
512
					{
513
514
515
516
517
						// for simple cells there is no more then one common edge
						// otherwise the cell should be treated as polyhedron
						bool check = true;
						MarkerType common = CreatePrivateMarker();
						for(INMOST_DATA_ENUM_TYPE i = 0; i < s; ++i)
Kirill Terekhov's avatar
Kirill Terekhov committed
518
						{
519
520
521
522
523
524
525
526
527
528
529
							const Element::adj_type & ilc = LowConn(lc[i]); // access edges of the i-th face
							for(INMOST_DATA_ENUM_TYPE r = 0; r < ilc.size(); ++r ) SetPrivateMarker(ilc[r],common);
							for(INMOST_DATA_ENUM_TYPE j = i+1; j < s; ++j )
							{
								const Element::adj_type & jlc = LowConn(lc[j]); // access edges of the j-th face
								int cnt = 0;
								for(INMOST_DATA_ENUM_TYPE r = 0; r < jlc.size(); ++r )
									if( GetPrivateMarker(jlc[r],common) ) cnt++;
								if( cnt > 1 ) check = false;
							}
							for(INMOST_DATA_ENUM_TYPE r = 0; r < ilc.size(); ++r ) RemPrivateMarker(ilc[r],common);
Kirill Terekhov's avatar
Kirill Terekhov committed
530
						}
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
						ReleasePrivateMarker(common);
						if( check )
						{
							//test c_faces closure, if no closure, set as MultiPolygon
							INMOST_DATA_ENUM_TYPE quads = 0,tris = 0,i;
							for(i = 0; i < size; i++) if( !Hidden(lc[i]) )
							{
								if( GetGeometricType(lc[i]) == Element::Tri )
									tris++;
								else if( GetGeometricType(lc[i]) == Element::Quad )
									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::Polyhedron;
Kirill Terekhov's avatar
Kirill Terekhov committed
555
556
557
558
559
560
561
562
563
					}
					else ret = Element::MultiPolygon;
				}
				break;
			case ESET: ret = Element::Set; break;
		}
		return ret;
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
564
	Storage::real Edge::Length() const 
Kirill Terekhov's avatar
Kirill Terekhov committed
565
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
		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
592
593
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
594
595
596
597
598
599
600
	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
601
	
Kirill Terekhov's avatar
Kirill Terekhov committed
602
	void Mesh::RecomputeGeometricData(HandleType e)
Kirill Terekhov's avatar
Kirill Terekhov committed
603
604
605
606
607
	{
		//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
608
			if( HaveGeometricData(d,GetHandleElementType(e)) ) //compute centroid first
Kirill Terekhov's avatar
Kirill Terekhov committed
609
610
			{
				Tag t = GetGeometricTag(d);
Kirill Terekhov's avatar
Kirill Terekhov committed
611
612
				Storage::real * a = static_cast<Storage::real *>(MGetDenseLink(e,t));
				HideGeometricData(d,GetHandleElementType(e));
Kirill Terekhov's avatar
Kirill Terekhov committed
613
				GetGeometricData(e,d,a);
Kirill Terekhov's avatar
Kirill Terekhov committed
614
				ShowGeometricData(d,GetHandleElementType(e));
Kirill Terekhov's avatar
Kirill Terekhov committed
615
616
617
618
			}
		}


619
		if( HaveGeometricData(ORIENTATION,FACE) )
Kirill Terekhov's avatar
Kirill Terekhov committed
620
		{
621
622
623
624
625
626
627
628
629
630
631
632
633
			if( GetHandleElementType(e) == CELL ) //then correct the normal
			{
				Element::adj_type & lc = LowConn(e);
				for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)
					if( !GetMarker(*it,HideMarker()) )
					{
						//Element::adj_type & hc = HighConn(e);
						//if( Mesh::Count(&hc[0],hc.size(),HideMarker()) == 1 )
							Face(this,*it)->FixNormalOrientation();
					}
			}
			else if( GetHandleElementType(e) == FACE )
				Face(this,e)->FixNormalOrientation();
Kirill Terekhov's avatar
Kirill Terekhov committed
634
		}
635

Kirill Terekhov's avatar
Kirill Terekhov committed
636
637
		for(d = MEASURE; d <= BARYCENTER; d++) // compute the rest
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
638
			if( HaveGeometricData(d,GetHandleElementType(e)) )
Kirill Terekhov's avatar
Kirill Terekhov committed
639
640
			{
				Tag t = GetGeometricTag(d);
Kirill Terekhov's avatar
Kirill Terekhov committed
641
642
				Storage::real * a = static_cast<Storage::real *>(MGetDenseLink(e,t));
				HideGeometricData(d,GetHandleElementType(e));
Kirill Terekhov's avatar
Kirill Terekhov committed
643
				GetGeometricData(e,d,a);
Kirill Terekhov's avatar
Kirill Terekhov committed
644
				ShowGeometricData(d,GetHandleElementType(e));
Kirill Terekhov's avatar
Kirill Terekhov committed
645
646
			}
		}
647
648
649
650
651
		
		if( GetHandleElementType(e) == CELL ) //then correct the normal
		{
			
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
	}
	
	
	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;
693
			for(ElementType etype = EDGE; etype <= CELL && !restore; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
694
695
696
697
698
699
				if( HaveGeometricData(gtype,etype) )
					restore = true;
			if( restore )
			{
				switch(gtype)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
700
701
702
703
				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
704
705
706
707
				}
			}
		}
	}
708

Kirill Terekhov's avatar
Kirill Terekhov committed
709
710
	void Mesh::RepairGeometricTags()
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
711
		if( HaveTag(measure_name) )
Kirill Terekhov's avatar
Kirill Terekhov committed
712
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
713
			measure_tag = GetTag(measure_name);
Kirill Terekhov's avatar
Kirill Terekhov committed
714
715
716
717
			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
718
		if( HaveTag(centroid_name) )
Kirill Terekhov's avatar
Kirill Terekhov committed
719
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
720
			centroid_tag = GetTag(centroid_name);
Kirill Terekhov's avatar
Kirill Terekhov committed
721
722
723
724
			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
725
		if( HaveTag(barycenter_name) )
Kirill Terekhov's avatar
Kirill Terekhov committed
726
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
727
			barycenter_tag = GetTag(barycenter_name);
Kirill Terekhov's avatar
Kirill Terekhov committed
728
729
730
731
			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
732
		if( HaveTag(normal_name) )
Kirill Terekhov's avatar
Kirill Terekhov committed
733
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
734
			normal_tag = GetTag(normal_name);
Kirill Terekhov's avatar
Kirill Terekhov committed
735
736
737
738
739
			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
740
741
742
743
744
745
746
747
748
	
	void Mesh::PrepareGeometricData(GeomParam table)
	{
		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
749
				//std::cout << "ORIENTATION" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
750
				if( mask & FACE )
Kirill Terekhov's avatar
Kirill Terekhov committed
751
				{
752
753
754
					if( HideMarker() )
					{
						MarkerType hm = HideMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
755
756
757
#if defined(USE_OMP)
#pragma omp parallel for
#endif
758
759
760
761
762
763
764
765
766
767
768
						for(integer e = 0; e < FaceLastLocalID(); ++e) 
						{
							if( isValidElement(FACE,e) )
							{
								HandleType h = ComposeHandle(FACE,e);
								if( !GetMarker(h,hm) )
									Face(this,h)->FixNormalOrientation();
							}
						}
					}
					else
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
769
					{
770
						//std::cout << "Fix orientation" << std::endl;
771
772
773
#if defined(USE_OMP)
#pragma omp parallel for
#endif
774
775
						for(integer e = 0; e < FaceLastLocalID(); ++e) if( isValidElement(FACE,e) )
							Face(this,ComposeHandle(FACE,e))->FixNormalOrientation();
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
776
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
777
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
778
779
780
781
				ShowGeometricData(ORIENTATION,FACE);
			}
			if( types == MEASURE )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
782
				//std::cout << "MEASURE" << std::endl;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
783
				for(ElementType etype = EDGE; etype <= CELL; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
784
785
786
				{
					if( (mask & etype) && !HaveGeometricData(MEASURE,etype))
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
787
						measure_tag = CreateTag(measure_name,DATA_REAL,etype,NONE,1);
788
789
790
						if( HideMarker() )
						{
							MarkerType hm = HideMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
791
792
793
#if defined(USE_OMP)
#pragma omp parallel for
#endif
794
795
796
797
798
799
800
							for(integer e = 0; e < LastLocalID(etype); ++e) if( isValidElement(etype,e) )
							{
								HandleType h = ComposeHandle(etype,e);
								if( !GetMarker(h,hm) ) GetGeometricData(h,MEASURE,static_cast<Storage::real *>(MGetDenseLink(h,measure_tag)));
							}
						}
						else
Kirill Terekhov's avatar
Kirill Terekhov committed
801
						{
802
803
804
805
806
807
808
809
#if defined(USE_OMP)
#pragma omp parallel for
#endif
							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
810
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
811
812
813
814
815
816
						ShowGeometricData(MEASURE,etype);
					}
				}
			}
			if( types == CENTROID )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
817
				//std::cout << "CENTROID" << std::endl;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
818
				for(ElementType etype = EDGE; etype <= CELL; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
819
820
821
				{
					if( (mask & etype) && !HaveGeometricData(CENTROID,etype))
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
822
						centroid_tag = CreateTag(centroid_name,DATA_REAL,etype,NONE,GetDimensions());
823
824
825
						if( HideMarker() )
						{
							MarkerType hm = HideMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
826
827
828
#if defined(USE_OMP)
#pragma omp parallel for
#endif
829
830
831
832
833
834
835
							for(integer k = 0; k < LastLocalID(etype); ++k) if( isValidElement(etype,k) )
							{
								HandleType h = ComposeHandle(etype,k);
								if( !GetMarker(h,hm) ) GetGeometricData(h,CENTROID,static_cast<Storage::real *>(MGetDenseLink(h,centroid_tag)));
							}
						}
						else
Kirill Terekhov's avatar
Kirill Terekhov committed
836
						{
837
838
839
840
841
842
843
844
#if defined(USE_OMP)
#pragma omp parallel for
#endif
							for(integer k = 0; k < LastLocalID(etype); ++k) if( isValidElement(etype,k) )
							{
								HandleType h = ComposeHandle(etype,k);
								GetGeometricData(h,CENTROID,static_cast<Storage::real *>(MGetDenseLink(h,centroid_tag)));
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
845
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
846
847
848
849
850
851
						ShowGeometricData(CENTROID,etype);
					}
				}
			}
			if( types == BARYCENTER )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
852
				//std::cout << "BARYCENTER" << std::endl;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
853
				for(ElementType etype = EDGE; etype <= CELL; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
854
855
856
				{
					if( (mask & etype) && !HaveGeometricData(BARYCENTER,etype))
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
857
						barycenter_tag = CreateTag(barycenter_name,DATA_REAL,etype,NONE,GetDimensions());
858
859
860
						if( HideMarker() )
						{
							MarkerType hm = HideMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
861
862
863
#if defined(USE_OMP)
#pragma omp parallel for
#endif
864
865
866
867
868
869
870
							for(integer e = 0; e < LastLocalID(etype); ++e) if( isValidElement(etype,e) )
							{
								HandleType h = ComposeHandle(etype,e);
								if( !GetMarker(h,hm) ) GetGeometricData(h,BARYCENTER,static_cast<Storage::real *>(MGetDenseLink(h,barycenter_tag)));
							}
						}
						else
Kirill Terekhov's avatar
Kirill Terekhov committed
871
						{
872
873
874
875
876
877
878
879
#if defined(USE_OMP)
#pragma omp parallel for
#endif
							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
880
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
881
882
883
884
885
886
						ShowGeometricData(BARYCENTER,etype);
					}
				}	
			}
			if( types == NORMAL )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
887
				//std::cout << "NORMAL" << std::endl;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
888
				for(ElementType etype = FACE; etype <= CELL; etype = NextElementType(etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
889
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
890
891
					if( (mask & etype) && !HaveGeometricData(NORMAL,etype))
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
892
						normal_tag = CreateTag(normal_name,DATA_REAL,etype,NONE,GetDimensions());
893
894
895
						if( HideMarker() )
						{
							MarkerType hm = HideMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
896
897
898
#if defined(USE_OMP)
#pragma omp parallel for
#endif
899
900
901
902
903
904
905
							for(integer e = 0; e < LastLocalID(etype); ++e) if( isValidElement(etype,e) )
							{
								HandleType h = ComposeHandle(etype,e);
								if( !GetMarker(h,hm) ) GetGeometricData(h,NORMAL,static_cast<Storage::real *>(MGetDenseLink(h,normal_tag)));
							}
						}
						else
Kirill Terekhov's avatar
Kirill Terekhov committed
906
						{
907
908
909
910
911
912
913
914
#if defined(USE_OMP)
#pragma omp parallel for
#endif
							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
915
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
916
917
						ShowGeometricData(NORMAL,etype);
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
918
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
919
920
921
			}
		}
	}
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959

	bool Cell::CheckConvexity() const {return GetMeshLink()->CheckConvexity(getFaces());}

	bool Mesh::CheckConvexity(const ElementArray<Face> & faces) const
	{
		if( !faces.empty() )
		{
			std::vector<Storage::real> x(faces.size()*6,0.0);
			Storage::real * n = &x[faces.size()*3];
			Storage::real eps = GetEpsilon();
			//precompute data for all faces
#if defined(USE_OMP)
#pragma omp critical (reorder_edges)
#endif
			{ //collect data safely so that nobody changes it
				for(ElementArray<Face>::size_type j = 0; j != faces.size(); ++j)
				{
					faces[j].Centroid(&x[j*3]);
					faces[j].UnitNormal(&n[j*3]);
				}
			}
			//run comparison
			for(ElementArray<Face>::size_type j = 0; j != faces.size(); ++j)
			{
				Storage::real dota = 0, dots = 0, dotv, v[3] = {0.,0.,0.};
				for(ElementArray<Face>::size_type m = 0; m != faces.size(); ++m)
				{
					vec_diff(&x[m*3],&x[j*3],v,3);
					dotv = vec_dot_product(&n[j*3],v,3);
					dots += dotv;
					dota += fabs(dotv);
				}
				if( fabs(fabs(dots) - dota) > eps )
					return false;
			}
		}
		return true;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
960
	
Kirill Terekhov's avatar
Kirill Terekhov committed
961
962
963
964
965
966
967
	void Mesh::FacesOrientation(ElementArray<Face> & faces, MarkerType rev)
	{
		//can copy orientation-independent algorithm from
		//incident_matrix.hpp: incident_matrix::compute_measure
		//assume mdim is of size 3 at most
		if( !faces.empty() )
		{
968
			if( CheckConvexity(faces) ) //simpler algorithm
Kirill Terekhov's avatar
Kirill Terekhov committed
969
			{
970
971
972
973
				std::vector<Storage::real> x(faces.size()*6);
				Storage::real * n = &x[faces.size()*3];
				Storage::real xc[3] = {0.,0.,0.}, v[3] = {0.,0.,0.};
				for(ElementArray<Face>::size_type j = 0; j != faces.size(); ++j)
Kirill Terekhov's avatar
Kirill Terekhov committed
974
				{
975
976
977
978
979
					faces[j].Centroid(&x[j*3]);
					faces[j].UnitNormal(&n[j*3]);
					xc[0] += x[j*3+0];
					xc[1] += x[j*3+1];
					xc[2] += x[j*3+2];
Kirill Terekhov's avatar
Kirill Terekhov committed
980
				}
981
982
983
984
				xc[0] /= (Storage::real)faces.size();
				xc[1] /= (Storage::real)faces.size();
				xc[2] /= (Storage::real)faces.size();
				for(ElementArray<Face>::size_type j = 0; j != faces.size(); ++j)
Kirill Terekhov's avatar
Kirill Terekhov committed
985
				{
986
987
					vec_diff(&x[j*3],xc,v,3);
					if( vec_dot_product(&n[j*3],v,3) < 0 )
Kirill Terekhov's avatar
Kirill Terekhov committed
988
					{
989
990
						if( isPrivate(rev) )
							faces[j].SetPrivateMarker(rev);
Kirill Terekhov's avatar
Kirill Terekhov committed
991
						else
992
993
							faces[j].SetMarker(rev);
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
994
				}
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
			}
			else
			{
				//real was = *ret/3.0;
				Face cur = faces[0];
				Mesh * mesh = faces.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
Kirill Terekhov's avatar
Kirill Terekhov committed
1012
				{
1013
1014
1015
					//figure out starting node order
					if( edges[0]->getBeg() == edges[1]->getBeg() ||
						edges[0]->getBeg() == edges[1]->getEnd() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1016
					{
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
						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() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1032
						{
1033
1034
							adjacent.RemPrivateMarker(mrk);
							stack.push_back(orient_face(edges[j],reverse ? n2 : n1,adjacent[0]));
Kirill Terekhov's avatar
Kirill Terekhov committed
1035
						}
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
						//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();
Kirill Terekhov's avatar
Kirill Terekhov committed
1058
1059
					}
					else
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
					{
						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 )
							{
								if( isPrivate(rev) )
									r.face->SetPrivateMarker(rev);
								else
									r.face->SetMarker(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);
				//faces.RemPrivateMarker(rev);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1093
1094
1095
		}
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1096
	void Mesh::GetGeometricData(HandleType e, GeometricData type, Storage::real * ret)
Kirill Terekhov's avatar
Kirill Terekhov committed
1097
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1098
1099
		assert(e != InvalidHandle());
		assert(ret != NULL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1100
1101
1102
1103
		assert(type == MEASURE ||
			   type == CENTROID ||
			   type == BARYCENTER ||
			   type == NORMAL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1104
1105
1106
		ElementType etype = GetHandleElementType(e);
		integer edim = Element::GetGeometricDimension(GetGeometricType(e));
		integer mdim = GetDimensions();
Kirill Terekhov's avatar
Kirill Terekhov committed
1107
1108
1109
1110
1111
		switch(type)
		{
			case MEASURE:
			if( HaveGeometricData(MEASURE,etype) )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1112
				*ret = static_cast<Storage::real *>(MGetDenseLink(e,measure_tag))[0];
Kirill Terekhov's avatar
Kirill Terekhov committed
1113
1114
1115
1116
1117
1118
1119
1120
1121
				//~ 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
1122
						ElementArray<Node> nodes = Element(this,e)->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
1123
1124
						if( nodes.size() > 1 )
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1125
1126
1127
1128
							real c[3] = {0,0,0};
							real_array v0 = nodes[0].Coords();
							real_array v1 = nodes[1].Coords();
							vec_diff(v0.data(),v1.data(),c,mdim);
Kirill Terekhov's avatar
Kirill Terekhov committed
1129
1130
1131
							*ret = vec_len(c,mdim);
						}
						else *ret = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1132
1133
1134
1135
1136
						//~ if( isnan(*ret) || fabs(*ret) < 1e-15  ) throw -1;
						break;
					}
					case 2: //area of face
					{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1137
						ElementArray<Node> nodes = Element(this,e)->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
1138
						if( nodes.size() > 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1139
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1140
							*ret = 0;
1141
							real nt[3] = { 0,0,0 }, l1[3] = { 0,0,0 }, l2[3] = { 0,0,0 }, n0[3] = { 0,0,0 };// , ss, at;
1142
1143
1144
							real_array v0, v1, v2;
							v0 = nodes[0].Coords();
							for(int i = 1; i < (int)nodes.size()-1; i++)
1145
1146
							{
								v1 = nodes[i].Coords();
1147
1148
1149
								v2 = nodes[i+1].Coords();
								vec_diff(v1,v0,l1,mdim);
								vec_diff(v2,v0,l2,mdim);
1150
1151
1152
1153
								vec_cross_product(l1,l2,nt);
								for(int q = 0; q < 3; ++q)
									n0[q] += nt[q]*0.5;
							}
1154
							*ret = vec_len(n0,3);
Kirill Terekhov's avatar
Kirill Terekhov committed
1155
							//code below is not really needed
1156
1157
							/*
							for(int i = 1; i < (int)nodes.size()-1; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1158
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
1159
								v1 = nodes[i].Coords();
1160
1161
1162
								v2 = nodes[i+1].Coords();
								vec_diff(v1,v0,l1,mdim);
								vec_diff(v2,v0,l2,mdim);
Kirill Terekhov's avatar
Kirill Terekhov committed
1163
								vec_cross_product(l1,l2,nt);
1164
1165
								for(int q = 0; q < 3; ++q) 
									nt[q] *= 0.5;
Kirill Terekhov's avatar
Kirill Terekhov committed
1166
								ss = vec_dot_product(n0,nt,3);
1167
								if( ss ) ss /= fabs(ss);
1168
1169
								at = sqrt(vec_dot_product(nt,nt,3))*ss;
								*ret += at;
Kirill Terekhov's avatar
Kirill Terekhov committed
1170
							}
1171
							*/
1172
							if( *ret != *ret ) std::cout << "area is nan" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1173
							*ret = fabs(*ret);
Kirill Terekhov's avatar
Kirill Terekhov committed
1174
						} else *ret = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1175
1176
1177
1178
1179
						//~ if( isnan(*ret) || fabs(*ret) < 1e-15  ) throw -1;
						break;
					}
					case 3: //volume of cell
					{
1180
1181
						//bool print = false;
//redo:
Kirill Terekhov's avatar
Kirill Terekhov committed
1182
1183
						Cell me = Cell(this,e);
						ElementArray<Face> faces = me->getFaces();
1184
1185
1186
						//bool ornt = true;//!HaveGeometricData(ORIENTATION,FACE);
						bool ornt = !HaveGeometricData(ORIENTATION,FACE);
						//bool ornt = !CheckConvexity(faces);
Kirill Terekhov's avatar
Kirill Terekhov committed
1187
1188
						MarkerType rev = 0;
						if( ornt )
Kirill Terekhov's avatar
Kirill Terekhov committed
1189
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1190
1191
							rev = CreatePrivateMarker();
							FacesOrientation(faces,rev);
Kirill Terekhov's avatar
Kirill Terekhov committed
1192
						}
1193
						real vol = 0, a, at, volp;
Kirill Terekhov's avatar
Kirill Terekhov committed
1194
						real x[3] = {0,0,0}, n0[3] = {0,0,0}, s, ss;
Kirill Terekhov's avatar
Kirill Terekhov committed
1195
						real l1[3] = {0,0,0}, l2[3] = {0,0,0};
1196
						real nt[3] = {0,0,0};
1197
						real vc[3] = {0,0,0};
1198
						//me.Centroid(cx);
1199
						//if( print ) std::cout << "cx: " << cx[0] << " " << cx[1] << " " << cx[2] << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1200
						for(unsigned j = 0; j < faces.size(); j++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1201
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1202
1203
1204
1205
1206
1207
1208
							//compute normal to face
							ElementArray<Node> nodes = faces[j].getNodes();
							if( ornt )
								s = faces[j].GetPrivateMarker(rev) ? -1.0 : 1.0;
							else
								s = faces[j].FaceOrientedOutside(me) ? 1.0 : -1.0;
							x[0] = x[1] = x[2] = 0;
1209
							n0[0] = n0[1] = n0[2] = 0;
1210
							vc[0] = vc[1] = vc[2] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1211
							a = 0;
1212
1213
							real_array v0 = nodes[0].Coords(), v1, v2;
							for(int i = 1; i < (int)nodes.size()-1; i++)
1214
1215
							{
								v1 = nodes[i].Coords();
1216
1217
1218
								v2 = nodes[i+1].Coords();
								vec_diff(v1,v0,l1,mdim);
								vec_diff(v2,v0,l2,mdim);
1219
1220
1221
1222
								vec_cross_product(l1,l2,nt);
								for(int q = 0; q < 3; ++q)
									n0[q] += nt[q]*0.5;
							}
1223
							for(int i = 1; i < (int)nodes.size()-1; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1224
							{
1225
1226
1227
1228
								v1 = nodes[i].Coords();
								v2 = nodes[i+1].Coords();
								vec_diff(v1,v0,l1,mdim);
								vec_diff(v2,v0,l2,mdim);
Kirill Terekhov's avatar
Kirill Terekhov committed
1229
								vec_cross_product(l1,l2,nt);
1230
1231
								for(int q = 0; q < 3; ++q) 
									nt[q] *= 0.5;
Kirill Terekhov's avatar
Kirill Terekhov committed
1232
								ss = vec_dot_product(n0,nt,3);
1233
1234
								if( ss ) 
									ss /= fabs(ss);
Kirill Terekhov's avatar
Kirill Terekhov committed
1235
								at = sqrt(vec_dot_product(nt,nt,3))*ss;
1236
								//same as faces[j].Barycenter(x)
Kirill Terekhov's avatar
Kirill Terekhov committed
1237
								for(int q = 0; q < 3; ++q)
1238
									x[q] += at*(v0[q]+v1[q]+v2[q])/3.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1239
								a += at;
Kirill Terekhov's avatar
Kirill Terekhov committed
1240
							}
1241
1242
1243
							if( a )
							{
								for(int q = 0; q < 3; ++q) 
1244
									x[q] = x[q]/a;
1245
							}
1246
							else Element(this,e).Centroid(x);
Kirill Terekhov's avatar
Kirill Terekhov committed
1247
							volp = vec_dot_product(x,n0,3) / 3.0;
1248
							vol += s*volp;
Kirill Terekhov's avatar
Kirill Terekhov committed
1249
1250
1251
1252
						}
						if( ornt )
						{
							if( vol < 0.0 ) vol = -vol;
Kirill Terekhov's avatar
Kirill Terekhov committed
1253
							faces.RemPrivateMarker(rev);
Kirill Terekhov's avatar
Kirill Terekhov committed
1254
							ReleasePrivateMarker(rev);
Kirill Terekhov's avatar
Kirill Terekhov committed
1255
						}
1256
						*ret = vol;
Kirill Terekhov's avatar
Kirill Terekhov committed
1257
1258
1259
1260
1261
1262
1263
						break;
					}
				}
			}
			//~ if( isnan(*ret) || fabs(*ret) < 1e-15  ) throw -1;
			break;
			case CENTROID:
Kirill Terekhov's avatar
Kirill Terekhov committed
1264
1265
1266
				if(etype == NODE )
					memcpy(ret,MGetDenseLink(e,CoordsTag()),sizeof(real)*mdim);
				else if(HaveGeometricData(CENTROID,etype))
Kirill Terekhov's avatar
Kirill Terekhov committed
1267
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1268
1269