octgrid.cpp 143 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
4

#include "octgrid.h"
#include <math.h>
#include <new>
Kirill Terekhov's avatar
Kirill Terekhov committed
5
#include <deque>
Kirill Terekhov's avatar
Kirill Terekhov committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
extern bool allow_coarse;
extern bool allow_refine;

extern bool global_problem;
const bool unite_faces = false;
const INMOST_DATA_ENUM_TYPE multiparent = ENUMUNDEF; //set ENUMUNDEF to enable union of small cells
bool remove_orphan_elements = true;
extern int global_test_number;
#define MINVOLFRAC 0.001

void default_transformation(double xyz[3]) { (void) xyz; }
int default_cell_should_unite(struct grid * g, int cell) { (void) g; (void) cell; return 0; }
int default_cell_should_split(struct grid * g, int cell) { (void) g; (void) cell; return 0; }
void default_cell_unite_data(struct grid * g, int cell) { (void) g; (void) cell; }
void default_cell_split_data(struct grid * g, int cell) { (void) g; (void) cell; }
void default_vert_interpolate_data(struct grid * g,int big_cell, int nvert, int * verts, int * isnew) { (void) g; (void) big_cell; (void ) nvert; (void ) verts; (void ) isnew;  }
void default_vert_init_data(struct grid * g, int vert) {(void) g; (void) vert;};
void default_cell_init_data(struct grid * g, int cell) {(void) g; (void) cell;};
void default_vert_destroy_data(struct grid * g, int vert) {(void) g; (void) vert;};
void default_cell_destroy_data(struct grid * g, int cell) {(void) g; (void) cell;};
Kirill Terekhov's avatar
Kirill Terekhov committed
26
27
void default_vert_to_INMOST(struct grid * g, int vert, Node v) {(void) g; (void) vert; (void) v;};
void default_cell_to_INMOST(struct grid * g, int cell, Cell r) {(void) g; (void) cell; (void) r;};
Kirill Terekhov's avatar
Kirill Terekhov committed
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
void default_init_mesh(struct grid * g) {(void ) g;} ;


void make_vec(double p1[3], double p2[3], double out[3])
{
	out[0] = p1[0] - p2[0];
	out[1] = p1[1] - p2[1];
	out[2] = p1[2] - p2[2];
}

void cross_prod(double v1[3], double v2[3], double out[3])
{
	out[0] = v1[1]*v2[2] - v1[2]*v2[1];
	out[1] = v1[2]*v2[0] - v1[0]*v2[2];
	out[2] = v1[0]*v2[1] - v1[1]*v2[0];
}

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;
}
	
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]);
}

double dot_prod(double v1[3],double v2[3])
{
	return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2];
}

int invert_side(int side)
{
	return side/2*2 + (side%2+1)%2;
}

int add_element(int * elements, int * nelements, int newe)
{
	int i;
	for(i = 0; i < (*nelements); i++)
		if( newe == elements[i] )
		{
			//printf("already have %d at %d\n",new,i);
			return i;
		}
	//printf("elements[%d] = %d\n",nelements,new);
	elements[(*nelements)++] = newe;
	return (*nelements)-1;
}



int cellSearch(struct grid * g, int m, double v[3])
{
	int c,i;
	if( !g->cells[m].leaf )
	{
		c = 0;
		for(i = 0; i < 3; i++)
			if( v[i] > g->cells[m].center[i] )
				c += 1 << i;
		return cellSearch(g,g->cells[m].children[c],v);
	}
	return m;
}

int gridSearch(struct grid * g, double v[3])
{
	int i,j,k,l, flag;
	for(i = 0; i < g->n[0]; i++)
	for(j = 0; j < g->n[1]; j++)
	for(k = 0; k < g->n[2]; k++)
	{
		flag = 1;
		for(l = 0; l < 3 && flag; l++)
			if(!(g->cells[g->mgrid[i][j][k]].center[l] - g->cells[g->mgrid[i][j][k]].side[l]*0.5 <= v[l] &&
				 g->cells[g->mgrid[i][j][k]].center[l] + g->cells[g->mgrid[i][j][k]].side[l]*0.5 >= v[l]))
				 flag = 0;
		if( flag ) return cellSearch(g,g->mgrid[i][j][k],v);
	}
	return -1;
}


void vertGetCoord(struct grid * g, int v, double coord[3])
{
	int i;
	if( v == -1 ) return;
	if( !g->verts[v].busy ) return;
	for(i = 0; i < 1<<DIM; i++)
	{
		if( g->verts[v].env[((1<<DIM)-1)-i] != -1 && g->cells[g->verts[v].env[((1<<DIM)-1)-i]].vertexes[i] == v )
		{
			coord[0] = g->cells[g->verts[v].env[((1<<DIM)-1)-i]].center[0] + ((i & 1) * 2 - 1) * g->cells[g->verts[v].env[((1<<DIM)-1)-i]].side[0]*0.5;
			coord[1] = g->cells[g->verts[v].env[((1<<DIM)-1)-i]].center[1] + ((i & 2)     - 1) * g->cells[g->verts[v].env[((1<<DIM)-1)-i]].side[1]*0.5;
			coord[2] = g->cells[g->verts[v].env[((1<<DIM)-1)-i]].center[2] + ((i & 4) / 2 - 1) * g->cells[g->verts[v].env[((1<<DIM)-1)-i]].side[2]*0.5;
			return;
		}
	}
}

int cellAround(struct grid * g, int m, int side, int neighbours[1<<(DIM-1)])
{
	int k,v,c,q,ret = 0;
	const int vert[3][4] = {{0,2,6,4},{0,1,5,4},{0,1,3,2}};
	const int chck[3][4] = {{1,3,7,5},{2,3,7,6},{4,5,7,6}};
	int add = (side%2)*(1<<side/2);
	for(k = 0; k < 1<<(DIM-1); k++)
	{
		v = g->cells[m].vertexes[vert[side/2][k]+add];
		if( v == -1 ) TSNH;
		c = g->verts[v].env[vert[side/2][(k+DIM-1)%(1<<(DIM-1))]+add];
		if( c != -1 )
		{
			q = g->cells[c].vertexes[chck[side/2][k]-add];
			if( q != v ) c = -1;
		}
		if( c != -1 ) add_element(neighbours,&ret,c);
	}
	return ret;
}




void vertDestroyINMOST(struct grid *g, int m)
{
	//printf("%s\n",__FUNCTION__);
Kirill Terekhov's avatar
Kirill Terekhov committed
159
	if( g->verts[m].mv != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
160
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
161
		g->mesh->Delete(g->verts[m].mv);
Kirill Terekhov's avatar
Kirill Terekhov committed
162
163
164
165
166
167
	}
}

void vertCreateINMOST(struct grid * g, int m)
{
	Storage::real xyz[3];
Kirill Terekhov's avatar
Kirill Terekhov committed
168
	if( g->verts[m].mv != InvalidHandle() ) return;
Kirill Terekhov's avatar
Kirill Terekhov committed
169
170
	vertGetCoord(g,m,xyz);
	g->transformation(xyz);
Kirill Terekhov's avatar
Kirill Terekhov committed
171
172
	g->verts[m].mv = g->mesh->CreateNode(xyz)->GetHandle();
	g->mesh->SetMarker(g->verts[m].mv,g->octree_node);
Kirill Terekhov's avatar
Kirill Terekhov committed
173
	//g->verts[m].mv->Integer(g->new_marker) = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
174
	g->vert_to_INMOST(g,m,Node(g->mesh,g->verts[m].mv));
Kirill Terekhov's avatar
Kirill Terekhov committed
175
176
177
178
179
180
181
182
183
184
}

void cellDestroyINMOST(struct grid * g, int m)
{

	if( !g->cells[m].mr->empty() )
	{
		std::vector<Storage::integer> other_del;
		for(int k = 0; k < g->cells[m].mr->size(); k++)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
185
186
			Cell ck = Cell(g->mesh,(*g->cells[m].mr)[k]);
			Storage::integer_array p = ck->IntegerArray(g->parent);
Kirill Terekhov's avatar
Kirill Terekhov committed
187
188
189
190
191
192
193
194
195
196
			for(int j = 0; j < p.size(); j++) if( p[j] != m )
			{
				other_del.push_back(p[j]); //this cell is united, should delete it's other parents
				for(int q = 0; q < g->cells[p[j]].mr->size(); q++)
					if( (*g->cells[m].mr)[k] == (*g->cells[p[j]].mr)[q] ) //remove the cell from other parent, so we don't delete it twice
					{
						g->cells[p[j]].mr->erase(g->cells[p[j]].mr->begin()+q);
						break;
					}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
197
198
199
200
201
			ElementArray<Face> faces = ck->getFaces();
			ElementArray<Edge> edges = ck->getEdges();
			ElementArray<Node> nodes = ck->getNodes();
			ck->Delete();
			for(ElementArray<Face>::iterator f = faces.begin(); f != faces.end(); f++)
Kirill Terekhov's avatar
Kirill Terekhov committed
202
203
			{
				if( f->nbAdjElements(CELL) == 0 ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
204
					f->Delete();
Kirill Terekhov's avatar
Kirill Terekhov committed
205
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
206
			for(ElementArray<Edge>::iterator e = edges.begin(); e != edges.end(); e++)
Kirill Terekhov's avatar
Kirill Terekhov committed
207
208
			{
				if( e->nbAdjElements(FACE) == 0 ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
209
					e->Delete();
Kirill Terekhov's avatar
Kirill Terekhov committed
210
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
211
			for(ElementArray<Node>::iterator e = nodes.begin(); e != nodes.end(); e++)
Kirill Terekhov's avatar
Kirill Terekhov committed
212
213
			{
				if( !e->GetMarker(g->octree_node) && e->nbAdjElements(EDGE) == 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
214
					e->Delete();
Kirill Terekhov's avatar
Kirill Terekhov committed
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
			}
		}
		g->cells[m].mr->clear();
		std::sort(other_del.begin(),other_del.end());
		other_del.resize(std::unique(other_del.begin(),other_del.end())-other_del.begin());
		for(int k = 0; k < other_del.size(); k++)
			cellDestroyINMOST(g,other_del[k]);
	}
}

void reverse_face(int * face, bool * mid)
{
	int i;
	for(i = 0; i < face[0]/2; i++)
	{
		int temp = face[i+1];
		face[i+1] = face[face[0]-i];
		face[face[0]-i] = temp;
		
		bool btemp = mid[i+1];
		mid[i+1] = mid[face[0]-i];
		mid[face[0]-i] = btemp;
	}
}

void reverse_face2(int * face, bool * mid)
{
	int i;
	for(i = 0; i < (face[0]-1)/2; i++)
	{
		int temp = face[i+2];
		face[i+2] = face[face[0]-i];
		face[face[0]-i] = temp;
		
		bool btemp = mid[i+2];
		mid[i+2] = mid[face[0]-i];
		mid[face[0]-i] = btemp;
	}
}

int vertGetMiddle(struct grid * g, int m, int side, int edge)
{
	int i,j,current_vert,opposit_vert,middle_vert,adj_cell;
	const int nvf[6][4] = {{0,4,6,2},{1,3,7,5},{0,1,5,4},{2,6,7,3},{0,2,3,1},{4,5,7,6}};
	const int nve[6][4][2][2] = 
	{
		{{{5,6},{1,2}},{{2,7},{0,5}},{{0,3},{4,7}},{{1,4},{3,6}}},
		{{{2,7},{0,5}},{{5,6},{1,2}},{{1,4},{3,6}},{{0,3},{4,7}}},
		{{{3,5},{2,4}},{{4,7},{0,3}},{{0,6},{1,7}},{{1,2},{5,6}}},
		{{{4,7},{0,3}},{{3,5},{2,4}},{{1,2},{5,6}},{{0,6},{1,7}}},
		{{{3,6},{1,4}},{{1,7},{0,6}},{{0,5},{2,7}},{{2,4},{3,5}}},
		{{{1,7},{0,6}},{{3,6},{1,4}},{{2,4},{3,5}},{{0,5},{2,7}}}
	};
	for(i = 0; i < 2; i++)
	{
		current_vert = g->cells[m].vertexes[nvf[side][(edge+i)%4]];
		opposit_vert = g->cells[m].vertexes[nvf[side][(edge+(i+1)%2)%4]];
		for(j = 0; j < 2; j++)
		{
			adj_cell = g->verts[current_vert].env[nve[side][edge][i][j]];
			if( adj_cell != -1 && g->cells[m].level < g->cells[adj_cell].level )
			{
				middle_vert = g->cells[adj_cell].vertexes[nve[side][edge][i][1-j]];
				if( middle_vert == opposit_vert ) continue;
				return middle_vert;
			}
		}
		adj_cell = g->verts[current_vert].env[nvf[side][(edge+(i+1)%2)%4]];
		if( adj_cell != -1 && g->cells[m].level < g->cells[adj_cell].level )
		{
			middle_vert = g->cells[adj_cell].vertexes[7-nvf[side][(edge+i)%4]];
			if( middle_vert == opposit_vert ) continue;
			return middle_vert;
		}
	}
	return -1;
}

Kirill Terekhov's avatar
Kirill Terekhov committed
293
void cellGetFaceVerts(struct grid * g, int m, int side, int * nverts, Node verts[54], bool * mid,int * faces,  int reverse)
Kirill Terekhov's avatar
Kirill Terekhov committed
294
295
296
297
298
299
300
301
{
	int i,middle;
	const int nvf[6][4] = {{0,4,6,2},{1,3,7,5},{0,1,5,4},{2,6,7,3},{0,2,3,1},{4,5,7,6}};
	faces[0] = 0;
	mid[0] = false;
	for(i = 0; i < 4; i++)
	{
		mid[1+faces[0]] = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
302
		verts[(faces[1+faces[0]] = (*nverts))] = Node(g->mesh,g->verts[g->cells[m].vertexes[nvf[side][i]]].mv);
Kirill Terekhov's avatar
Kirill Terekhov committed
303
304
305
306
307
		faces[0]++;
		(*nverts)++;
		if( (middle = vertGetMiddle(g,m,side,i)) != -1)
		{
			mid[1+faces[0]] = true;
Kirill Terekhov's avatar
Kirill Terekhov committed
308
			verts[(faces[1+faces[0]] = (*nverts))] = Node(g->mesh,g->verts[middle].mv);
Kirill Terekhov's avatar
Kirill Terekhov committed
309
310
311
312
313
314
315
316
317
318
319
320
321
			faces[0]++;
			(*nverts)++;
			
		}
	}
	if( reverse ) 
	{
		reverse_face2(faces,mid);
	}
}



Kirill Terekhov's avatar
Kirill Terekhov committed
322
std::vector<Edge> traverse_edges_sub(Edge start, Edge current, MarkerType edgeset, MarkerType visited_bridge, MarkerType visited_edge)
Kirill Terekhov's avatar
Kirill Terekhov committed
323
324
{
	//~ if( current == start ) return std::vector<Edge *> (1,start);
Kirill Terekhov's avatar
Kirill Terekhov committed
325
326
	std::vector< std::vector<Edge> > paths;
	ElementArray<Node> n = current->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
327
328
329
330
331
	for(int j = 0; j < n.size(); j++)
	{
		if( !n[j].GetMarker(visited_bridge) )
		{
			n[j].SetMarker(visited_bridge);
Kirill Terekhov's avatar
Kirill Terekhov committed
332
			ElementArray<Edge> e = n[j].getEdges();
Kirill Terekhov's avatar
Kirill Terekhov committed
333
334
			for(int i = 0; i < e.size(); i++) 
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
335
				if( e[i] == start ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
336
337
				{
					n[j].RemMarker(visited_bridge);
Kirill Terekhov's avatar
Kirill Terekhov committed
338
					return std::vector<Edge> (1,start);
Kirill Terekhov's avatar
Kirill Terekhov committed
339
340
341
342
				}
				if( e[i].GetMarker(edgeset) && !e[i].GetMarker(visited_edge) )
				{
					e[i].SetMarker(visited_edge);
Kirill Terekhov's avatar
Kirill Terekhov committed
343
					std::vector<Edge> ret = traverse_edges_sub(start,e[i],edgeset,visited_bridge,visited_edge);
Kirill Terekhov's avatar
Kirill Terekhov committed
344
345
346
					e[i].RemMarker(visited_edge);
					if( !ret.empty() )  
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
347
						ret.push_back(e[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
						paths.push_back(ret);
					}
				}
			}
			n[j].RemMarker(visited_bridge);
		}
	}
	if( !paths.empty() )
	{
		int min = 0;
		for(int j = 1; j < paths.size(); j++)
		{
			if( paths[j].size() < paths[min].size() )
				min = j;
		}
		return paths[min];
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
365
	return std::vector<Edge>();
Kirill Terekhov's avatar
Kirill Terekhov committed
366
367
368
}
//This function may be slow, because we collect all the arrays
//should detect shortest path here, then collect one array with shortest path
Kirill Terekhov's avatar
Kirill Terekhov committed
369
std::vector<Edge> traverse_edges(Edge start, MarkerType edgeset, MarkerType visited_bridge, MarkerType visited_edge)
Kirill Terekhov's avatar
Kirill Terekhov committed
370
{
Kirill Terekhov's avatar
Kirill Terekhov committed
371
372
	std::vector< std::vector<Edge> > paths;
	ElementArray<Node> n = start->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
373
374
375
376
377
378
379
380
	start->SetMarker(visited_edge);
	//~ std::cout << "start edge " << start << std::endl;
	for(int j = 0; j < n.size(); j++)
	{
		if( !n[j].GetMarker(visited_bridge) )
		{
			//~ std::cout  << "enter to the bridge " << &n[j] << " " << n[j].Coords()[0] << "," << n[j].Coords()[1] << "," << n[j].Coords()[2] << std::endl;
			n[j].SetMarker(visited_bridge);
Kirill Terekhov's avatar
Kirill Terekhov committed
381
			ElementArray<Edge> e = n[j].getEdges();
Kirill Terekhov's avatar
Kirill Terekhov committed
382
383
384
385
			for(int i = 0; i < e.size(); i++) 
				if( e[i].GetMarker(edgeset) && !e[i].GetMarker(visited_edge) )
				{
					e[i].SetMarker(visited_edge);
Kirill Terekhov's avatar
Kirill Terekhov committed
386
					std::vector<Edge> ret = traverse_edges_sub(start,e[i],edgeset,visited_bridge,visited_edge);
Kirill Terekhov's avatar
Kirill Terekhov committed
387
388
389
					e[i].RemMarker(visited_edge);
					if( !ret.empty() )  
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
390
						ret.push_back(e[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
						paths.push_back(ret);
					}
				}
			n[j].RemMarker(visited_bridge);
		}
	}
	start->RemMarker(visited_edge);
	if( !paths.empty() )
	{
		int min = 0;
		for(int j = 1; j < paths.size(); j++)
		{
			if( paths[j].size() < paths[min].size() )
				min = j;
		}
		return paths[min];
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
408
	return std::vector<Edge>();
Kirill Terekhov's avatar
Kirill Terekhov committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
}

class matcenter
{
	Storage::real xyz[3];
	mat_ret_type mat;
	int visit_count;
public:
	matcenter() :mat()
	{
		xyz[0] = xyz[1] = xyz[2] = 0;
		visit_count = 0;
	}
	matcenter(Storage::integer_array _mat, Storage::real * _xyz) : mat(_mat.begin(),_mat.end())
	{
		xyz[0] = _xyz[0];
		xyz[1] = _xyz[1];
		xyz[2] = _xyz[2];
		visit_count = 0;
	}
	matcenter(mat_ret_type const & _mat, Storage::real * _xyz, int count) : mat(_mat)
	{
		xyz[0] = _xyz[0];
		xyz[1] = _xyz[1];
		xyz[2] = _xyz[2];
		visit_count = count;
	}
	matcenter(const matcenter & other) : mat(other.mat)
	{
		xyz[0] = other.xyz[0];
		xyz[1] = other.xyz[1];
		xyz[2] = other.xyz[2];
		visit_count = other.visit_count;
	}
	matcenter & operator =(matcenter const & other)
	{
		xyz[0] = other.xyz[0];
		xyz[1] = other.xyz[1];
		xyz[2] = other.xyz[2];
		mat = other.mat;
		visit_count = other.visit_count;
		return *this;
	}
	int get_count() {return visit_count;}
	Storage::real * get_center() {return xyz;}
	mat_ret_type & get_mat() {return mat;}
	bool contain_mat(Storage::integer m) const {return std::binary_search(mat.begin(),mat.end(),m);}
	mat_ret_type intersect(mat_ret_type & mats) const
	{
		mat_ret_type intersection(std::min(mat.size(),mats.size()));
		intersection.resize(std::set_intersection(mat.begin(),mat.end(),mats.begin(),mats.end(),intersection.begin())-intersection.begin());
		return intersection;
	}
	mat_ret_type unite(mat_ret_type & mats) const
	{
		mat_ret_type intersection(mat.size()+mats.size());
		intersection.resize(std::set_union(mat.begin(),mat.end(),mats.begin(),mats.end(),intersection.begin())-intersection.begin());
		return intersection;
	}
};

Kirill Terekhov's avatar
Kirill Terekhov committed
470
471
472
473
474
475
476
477
478
479
480
481
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
482
483
484
485

template<class T>
class incident_matrix
{
486
487
488
489
490
491
	std::vector< unsigned char > matrix;
	std::vector< char > visits;
	std::vector< T > head_column;
	std::vector<Element> head_row;
	std::vector<unsigned char > head_row_count;
	std::vector<unsigned> insert_order;
Kirill Terekhov's avatar
Kirill Terekhov committed
492
	bool exit_recurse;
493
494
495
496
497
	std::vector<T> min_loop, temp_loop; //used as return
	std::vector< char > hide_column;
	std::vector< char > hide_row;
	std::vector< char > stub_row;
	std::vector< double > centroids, normals;
Kirill Terekhov's avatar
Kirill Terekhov committed
498
	double min_loop_measure;
Kirill Terekhov's avatar
Kirill Terekhov committed
499
	Mesh * mesh;
Kirill Terekhov's avatar
Kirill Terekhov committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
	
	bool do_hide_row(unsigned k)
	{
		if( hide_column[k] == 0 )
		{
			hide_column[k] = 1;
			for(unsigned i = 0; i < head_row_count.size(); i++)
			if( matrix[k*head_row_count.size()+i] == 1 )
			{
				head_row_count[i] -= 1;
				if( head_row_count[i] == 0 ) 
				{
					hide_row[i] = 1;
					stub_row[i] = 0;
				}
			}
			insert_order.pop_back();
		} 
		return true;
	}
	
	bool do_show_row(unsigned k)
	{
		if( hide_column[k] == 1 )
		{
			hide_column[k] = 0;
			
			bool success = true;
			for(unsigned i = 0; i < head_row_count.size(); i++)
			if( matrix[k*head_row_count.size()+i] == 1 )
			{
				head_row_count[i] += 1;
				if( head_row_count[i] > 0 ) hide_row[i] = 0;
				if( head_row_count[i] > 2 ) success = false;
			}
			insert_order.push_back(k);
			if( !success ) do_hide_row(k);
			return success;
			
		} else return true;
	}
	bool test_success()
	{
		bool success = true;
		for(unsigned j = 0; j < head_row_count.size(); j++)
		{
			if( head_row_count[j] == 1 )
			{
				success = false;
				break;
			}
		}
		return success;
	}
554
	Storage::real compute_measure(std::vector<T> & data)
Kirill Terekhov's avatar
Kirill Terekhov committed
555
556
557
558
559
560
	{
		Storage::real measure = 0;
		if( data[0]->GetElementDimension() == 1 ) //this is edge //use geometric dimension here for 2d compatibility
		{
			//calculate area
			int mdim = data[0]->GetMeshLink()->GetDimensions();
Kirill Terekhov's avatar
Kirill Terekhov committed
561
			ElementArray<Node> nodes,n1,n2;
Kirill Terekhov's avatar
Kirill Terekhov committed
562
563
			n1 = data[0]->getNodes();
			n2 = data[1]->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
564
			if( n1[0] == n2[0] || n1[0] == n2[1])
Kirill Terekhov's avatar
Kirill Terekhov committed
565
566
567
568
569
			{
				nodes.push_back(n1[1]);
				nodes.push_back(n1[0]);
			}
			else
Kirill Terekhov's avatar
Kirill Terekhov committed
570
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
571
572
573
				nodes.push_back(n1[0]);
				nodes.push_back(n1[1]);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
574
			for(typename ElementArray<T>::size_type j = 1; j < data.size(); j++)
Kirill Terekhov's avatar
Kirill Terekhov committed
575
576
			{
				n1 = data[j]->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
577
				if( nodes.back() == n1[0] )
Kirill Terekhov's avatar
Kirill Terekhov committed
578
579
580
581
					nodes.push_back(n1[1]);
				else
					nodes.push_back(n1[0]);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
582
			
Kirill Terekhov's avatar
Kirill Terekhov committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
			Storage::real x[3] = {0,0,0};
			Storage::real_array x0 = nodes[0].Coords();
			for(unsigned i = 1; i < nodes.size()-1; i++)
			{
				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]);
			}
			measure = sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2])*0.5;
			
		}
		else //this is 3d face
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
601
602
603
604
605
606
			//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
			for(int k = 1; k < data.size(); ++k)
				data[k]->SetPrivateMarker(mrk); //0-th face orientation is default
Kirill Terekhov's avatar
Kirill Terekhov committed
607
			Node n1,n2, v1,v2; //to retrive edge
Kirill Terekhov's avatar
Kirill Terekhov committed
608
609
610
611
612
613
			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 = data[0]->getEdges();
			do
Kirill Terekhov's avatar
Kirill Terekhov committed
614
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
615
616
				//figure out starting node order
				if( edges[0]->getBeg() == edges[1]->getBeg() ||
Kirill Terekhov's avatar
Kirill Terekhov committed
617
					edges[0]->getBeg() == edges[1]->getEnd() )
Kirill Terekhov's avatar
Kirill Terekhov committed
618
619
620
621
				{
					n1 = edges[0]->getEnd();
					n2 = edges[0]->getBeg();
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
622
				else
Kirill Terekhov's avatar
Kirill Terekhov committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
				{
					n1 = edges[0]->getBeg();
					n2 = edges[0]->getEnd();
				}
				//schedule unvisited adjacent faces
				for(typename ElementArray<Edge>::size_type 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[0].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
Kirill Terekhov's avatar
Kirill Terekhov committed
641
642
643
644
					v1 = edges[(j+1)%edges.size()]->getBeg();
					v2 = edges[(j+1)%edges.size()]->getEnd();
					if( n2 == v1 ) n2 = v2;
					else n2 = v1;
Kirill Terekhov's avatar
Kirill Terekhov committed
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
				}
				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(typename ElementArray<Node>::size_type 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
Kirill Terekhov's avatar
Kirill Terekhov committed
682
683
684
685
					v1 = edges[(j+1)%edges.size()]->getBeg();
					v2 = edges[(j+1)%edges.size()]->getEnd();
					if( n2 == v1 ) n2 = v2;
					else n2 = v1;
Kirill Terekhov's avatar
Kirill Terekhov committed
686
687
688
689
690
				}
			} while(true);
			for(int k = 0; k < data.size(); ++k)
				data[k].RemPrivateMarker(mrk);
			mesh->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
691
			Storage::real cnt[3], nrm[3];
Kirill Terekhov's avatar
Kirill Terekhov committed
692
693
			for(typename ElementArray<T>::size_type j = 0; j < data.size(); j++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
694
695
696
697
				data[j]->Centroid(cnt);
				data[j]->getAsFace()->Normal(nrm);
				measure += (data[j]->GetPrivateMarker(rev) ? -1.0 : 1.0)*dot_prod(cnt,nrm);
				//measure += d;
Kirill Terekhov's avatar
Kirill Terekhov committed
698
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
699
700
701
			for(int k = 0; k < data.size(); ++k)
				data[k].RemPrivateMarker(rev);
			mesh->ReleasePrivateMarker(rev);
Kirill Terekhov's avatar
Kirill Terekhov committed
702
			measure /= 3.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
703
			measure = fabs(measure);
Kirill Terekhov's avatar
Kirill Terekhov committed
704
705
706
707
708
		}
		return measure;
	}
	void recursive_find(unsigned node, unsigned length)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
709
		//if( !min_loop.empty() && length > min_loop.size() ) return;
Kirill Terekhov's avatar
Kirill Terekhov committed
710
711
712
713
714
715
716
		bool success = false;
		if( do_show_row(node) )
		{
			success = test_success();
			
			if( success )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
717
				//if( min_loop.empty() || min_loop.size() >= length )
Kirill Terekhov's avatar
Kirill Terekhov committed
718
719
720
721
722
723
				{
					
					
					temp_loop.resize(length);
					for(unsigned j = 0; j < insert_order.size(); j++)
						temp_loop[j] = head_column[insert_order[j]];
Kirill Terekhov's avatar
Kirill Terekhov committed
724
					Storage::real measure = compute_measure(temp_loop);
Kirill Terekhov's avatar
Kirill Terekhov committed
725
					
Kirill Terekhov's avatar
Kirill Terekhov committed
726
					
Kirill Terekhov's avatar
Kirill Terekhov committed
727
					if( min_loop.empty() || min_loop_measure >= measure )
Kirill Terekhov's avatar
Kirill Terekhov committed
728
729
					{
						min_loop.swap(temp_loop);
Kirill Terekhov's avatar
Kirill Terekhov committed
730
						min_loop_measure = measure;
Kirill Terekhov's avatar
Kirill Terekhov committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
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
						//~ if( min_loop.size() == head_column.size() ) // all elements were visited
						//~ {
							//~ unsigned num = 0; 
							//~ for(unsigned j = 0; j < head_row.size(); j++) //check that all bridge elements were visited - we don't have any other loop then
								//~ num += hide_row[j];
							//~ if( num == head_row.size() ) exit_recurse = true; //exit recursive loop
						//~ }
					}
				}
			}
			else
			{
				bool stub = false;
				for(unsigned j = 0; j < head_row_count.size() && !exit_recurse; j++) //first try follow the order
				{
					if( stub_row[j] == 0 && matrix[node*head_row_count.size()+j] == 1 && head_row_count[j] == 1 )
					{
						for(unsigned q = 0; q < head_column.size() && !exit_recurse; q++)
						{
							if( visits[q] > 0 && matrix[q*head_row_count.size()+j] == 1 && hide_column[q] == 1 ) 
							{
								recursive_find(q,length+1);
							}
						}
						if( head_row_count[j] == 1 )
						{
							stub_row[j] = 1;
							stub = true;
							break; //this is a stub path
						} 
					}
				}
			
				if( !stub ) for(unsigned j = 0; j < head_row_count.size() && !exit_recurse; j++)
				{
					if( stub_row[j] == 0 && matrix[node*head_row_count.size()+j] == 0 && head_row_count[j] == 1 )
					{
						for(unsigned q = 0; q < head_column.size() && !exit_recurse; q++)
						{
							if( visits[q] > 0 && matrix[q*head_row_count.size()+j] == 1 && hide_column[q] == 1 ) 
							{
								recursive_find(q,length+1);
							}
						}
						if( head_row_count[j] == 1 ) 
						{
							stub_row[j] = 1;
							stub = true;
							break; //this is a stub path
						}
					}
				}
				
			}
			do_hide_row(node);
		}
		if( length == 1 )
		{
			for(unsigned j = 0; j < head_row.size(); j++)
				stub_row[j] = 0;
		}
	}
public:
	bool all_visited()
	{
		for(unsigned k = 0; k < visits.size(); k++)
			if( visits[k] != 0 ) return false;
		return true;
	}
	void print_matrix()
	{
		Storage::real cnt[3];
		for(unsigned k = 0; k < head_column.size(); k++)
		{
			for(unsigned j = 0; j < head_row.size(); j++)
				std::cout << static_cast<int>(matrix[k*head_row.size()+ j]);
			std::cout << " " << (int)visits[k];
			head_column[k]->Centroid(cnt);
			std::cout << " " << cnt[0] << " " << cnt[1] << " " << cnt[2];
			std::cout << std::endl;
		}
		std::cout << std::endl;
	}
	template<typename InputIterator>
	incident_matrix(InputIterator beg, InputIterator end, unsigned num_inner)
	: head_column(beg,end), min_loop()
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
818
		min_loop_measure = 1.0e20;
Kirill Terekhov's avatar
Kirill Terekhov committed
819
		//isInputForwardIterators<T,InputIterator>();
Kirill Terekhov's avatar
Kirill Terekhov committed
820
821
822
		if( !head_column.empty() )
		{
			Mesh * m = head_column[0]->GetMeshLink();
Kirill Terekhov's avatar
Kirill Terekhov committed
823
			mesh = m;
Kirill Terekhov's avatar
Kirill Terekhov committed
824
			MarkerType hide_marker = m->CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
825
826

			visits.resize(head_column.size());
827
			for(typename std::vector<T>::iterator it = head_column.begin(); it != head_column.end(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
828
829
830
			{
				unsigned k = it-head_column.begin();
				visits[k] = k < num_inner ? 2 : 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
831
832
				ElementArray<Element> sub = (*it)->getAdjElements((*it)->GetElementType() >> 1);
				for(ElementArray<Element>::iterator jt = sub.begin(); jt != sub.end(); ++jt)
Kirill Terekhov's avatar
Kirill Terekhov committed
833
834
					if( !jt->GetMarker(hide_marker) )
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
835
						head_row.push_back(jt->self());
Kirill Terekhov's avatar
Kirill Terekhov committed
836
837
838
839
840
841
						jt->SetMarker(hide_marker);
					}
			}
			
			
			
842
			std::map<Element,int> mat_num;
Kirill Terekhov's avatar
Kirill Terekhov committed
843
			
844
			for(std::vector<Element>::iterator it = head_row.begin(); it != head_row.end(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
845
846
847
848
849
850
851
852
853
854
855
			{
				(*it)->RemMarker(hide_marker);
				mat_num[*it] = it-head_row.begin();
			}	
			
			m->ReleaseMarker(hide_marker);
				
			matrix.resize(head_row.size()*head_column.size(),0);
			
			
			
856
			for(typename std::vector<T>::iterator it = head_column.begin(); it != head_column.end(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
857
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
858
859
				ElementArray<Element> sub = (*it)->getAdjElements((*it)->GetElementType() >> 1);
				for(ElementArray<Element>::iterator jt = sub.begin(); jt != sub.end(); ++jt)
Kirill Terekhov's avatar
Kirill Terekhov committed
860
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
861
					matrix[(it-head_column.begin())*head_row.size()+mat_num[jt->self()]] = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
				}
			}
			
			head_row_count.resize(head_row.size(),0);
			
			stub_row.resize(head_row.size(),0);
			
			hide_row.resize(head_row.size(),1);
			
			hide_column.resize(head_column.size(),1);
		}
	}
	incident_matrix(const incident_matrix & other) 
	: matrix(other.matrix), head_column(other.head_column), head_row(other.head_row), 
	  head_row_count(other.head_row_count), min_loop(other.min_loop), 
	  hide_row(other.hide_row), hide_column(other.hide_column), 
	  stub_row(other.stub_row) 
	{
	}
	incident_matrix & operator =(const incident_matrix & other) 
	{
		matrix = other.matrix; 
		head_column = other.head_column; 
		head_row = other.head_row; 
		head_row_count = other.head_row_count; 
		min_loop = other.min_loop; 
		hide_row = other.hide_row; 
		hide_column = other.hide_column;
		stub_row = other.stub_row;
		return *this;
	}
	~incident_matrix()
	{
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
896
	bool find_shortest_loop(ElementArray<T> & ret)
Kirill Terekhov's avatar
Kirill Terekhov committed
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
	{
		ret.clear();
		exit_recurse = false;
		unsigned first = UINT_MAX;
		do
		{
			first = UINT_MAX;
			for(unsigned q = 0; q < head_column.size(); q++)
				if( visits[q] == 1 )
				{
					first = q;
					break;
				}
			if( first != UINT_MAX )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
912
				min_loop_measure = 1.0e20;
Kirill Terekhov's avatar
Kirill Terekhov committed
913
914
915
916
917
918
				recursive_find(first,1);
				if( min_loop.empty() )
					visits[first]--; //don't start again from this element
			}
		} while( min_loop.empty() && first != UINT_MAX );
		
919
		for(typename std::vector<T>::iterator it = min_loop.begin(); it != min_loop.end(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
920
921
			ret.push_back(it->self());
		//ret.insert(ret.end(),min_loop.begin(),min_loop.end());
Kirill Terekhov's avatar
Kirill Terekhov committed
922
923
924
925
926
		min_loop.clear();
		
		if( !ret.empty() )
		{
			Mesh * m = ret[0]->GetMeshLink();
Kirill Terekhov's avatar
Kirill Terekhov committed
927
			MarkerType hide_marker = m->CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
			for(unsigned k = 0; k < ret.size(); k++) ret[k]->SetMarker(hide_marker);
			for(unsigned k = 0; k < head_column.size(); k++)
				if( head_column[k]->GetMarker(hide_marker) ) visits[k]--;
			for(unsigned k = 0; k < ret.size(); k++) ret[k]->RemMarker(hide_marker);
			m->ReleaseMarker(hide_marker);
			return true;
		}
		return false;
	}
	
	Element * get_element(unsigned k) {return head_column[k];}
	void set_visit(unsigned k, char vis ) { visits[k] = vis; }
};


class edge_Comparator
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
945
	private: MarkerType medge;
Kirill Terekhov's avatar
Kirill Terekhov committed
946
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
947
		edge_Comparator(MarkerType medge):medge(medge){}
Kirill Terekhov's avatar
Kirill Terekhov committed
948
		bool operator()(Edge a, Edge b){return a->GetMarker(medge) < b->GetMarker(medge);}
Kirill Terekhov's avatar
Kirill Terekhov committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
	};



void cellCreateINMOST(struct grid * g, int m, bool print = false)
{
	
	//if( m == 337 ) print = true;
	//if( m == 12743 ) print = true;
	//print = true;
	//if( m == 7852 || m == 7853 ) print = true;
	//if( m == 9919 ) print = true;
	//~ if( m == 3950 ) print = true;
	//~ if( (m == 63 || m == 64) && global_test_number == 7 ) print = true;
	//~ if( m == 26 && global_test_number == 24 ) print = true;

	//if( m == 113 ) print = true;
	//~ if( m == 13 ) print = true;
	//~ if( m == 190 ) print = true;
	
	int trigger_problem = 0;
	//~ if( m == 37 ) 
	//~ {
		//~ print = true;
		//~ trigger_problem = 100;
	//~ }
Kirill Terekhov's avatar
Kirill Terekhov committed
975
	ElementArray<Node> edge_nodes(g->mesh,2);
Kirill Terekhov's avatar
Kirill Terekhov committed
976
977
978
979
980
981
982
	const bool check = false;
	const int nvf[6][4] = {{0,4,6,2},{1,3,7,5},{0,1,5,4},{2,6,7,3},{0,2,3,1},{4,5,7,6}};
	int i,j,k,l,nn, neighbours[1<<(DIM-1)];
	int faces[24][9];
	bool mid[24][9];
	int sides[24];
	int dirs[24];
Kirill Terekhov's avatar
Kirill Terekhov committed
983
	Node verts[216];
Kirill Terekhov's avatar
Kirill Terekhov committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
	if( !g->cells[m].mr->empty() ) return;
	k = l = 0;
	for(j = 0; j < 6; j++)
	{
		if( print ) std::cout << "side " << j << std::endl;
		nn = cellAround(g,m,j,neighbours);
		if( nn == 4 )
		{
			for(i = 0; i < nn; i++)
			{
				sides[l] = j;
				dirs[l] = (j+1)%2;
				cellGetFaceVerts(g,neighbours[i],invert_side(j),&k,verts,mid[l],faces[l],dirs[l]);
				if( print ) 
				{
					std::cout << "face " << l << std::endl;
					for(unsigned jj = 0; jj < faces[l][0]; jj++) std::cout << jj << " " <<faces[l][jj+1] << " mid " << mid[l][jj+1] << " vert " << verts[faces[l][jj+1]]->LocalID() << std::endl;
				}
				
				
				l++;
			}
		}
		else 
		{
			sides[l] = j;
			dirs[l] = j%2;
			cellGetFaceVerts(g,m,j,&k,verts,mid[l],faces[l],dirs[l]);
			if( print ) 
			{
				std::cout << "face " << l << std::endl;
				for(unsigned jj = 0; jj < faces[l][0]; jj++) std::cout << jj << " " << faces[l][jj+1] << " mid " << mid[l][jj+1] << " vert " << verts[faces[l][jj+1]]->LocalID() << std::endl;
			}
			l++;
		}
	}
	
	
	//detect that cutcell is needed
	bool cutcell = false;
	Storage::integer_array mat = verts[faces[0][1]]->IntegerArray(g->materials);
	if( mat.size() != 1 ) cutcell = true;
	for(i = 0; i < l && !cutcell; i++)
	{
		for(j = 0; j < faces[i][0]; j++)
		{
			Storage::integer_array mats2 = verts[faces[i][1+j]]->IntegerArray( g->materials );
			if( mats2.size() > 1 || mats2[0] != mat[0] )
			{
				cutcell = true;
				break;
			}
		}
	}
	
	g->cells[m].vol = 0;

	for(i = 0; i < l; i++)
	{
		Storage::real_array x0 = verts[faces[i][1]]->Coords();
		Storage::real x[3];
		Storage::real y[3];
		x[0] = x[1] = x[2] = 0;
		y[0] = x0[0];
		y[1] = x0[1];
		y[2] = x0[2];
		//std::cout << "0 " << x0[0] << " " << x0[1] << " " << x0[2] << std::endl;
		for(j = 1; j < faces[i][0]-1; j++)
		{
			Storage::real_array v1 = verts[faces[i][1+j]]->Coords();
			Storage::real_array v2 = verts[faces[i][1+(j+1)%faces[i][0]]]->Coords();
			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]);
			y[0] += v1[0];
			y[1] += v1[1];
			y[2] += v1[2];
			//std::cout << j << " " << v1[0] << " " << v1[1] << " " << v1[2] << std::endl;
		}
		x[0] *= 0.5;
		x[1] *= 0.5;
		x[2] *= 0.5;
		x0 = verts[faces[i][faces[i][0]]]->Coords();
		//std::cout << faces[i][0]-1 << " " << x0[0] << " " << x0[1] << " " << x0[2] << std::endl;
		y[0] = (y[0] + x0[0])/faces[i][0];
		y[1] = (y[1] + x0[1])/faces[i][0];
		y[2] = (y[2] + x0[2])/faces[i][0];
		
		//if( !dirs[i] )
		if( sides[i]%2 )
		{
			x[0] = -x[0];
			x[1] = -x[1];
			x[2] = -x[2];
		}
		//std::cout << "x " << x[0] << " " << x[1] << " " << x[2] << " y " << y[0] << " " << y[1] << " " << y[2] << " dirs " << dirs[i] << std::endl;
		g->cells[m].vol += x[0]*y[0] + x[1]*y[1] + x[2]*y[2];
	}

	g->cells[m].vol /= 3.0;
	
	if( !cutcell )
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1087
1088
		ElementArray<Face> c_faces;
		ElementArray<Edge> f_edges(g->mesh);
Kirill Terekhov's avatar
Kirill Terekhov committed
1089
1090
		for(i = 0; i < l; i++)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1091
			f_edges.resize(faces[i][0]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1092
1093
1094
1095
1096
			for(j = 0; j < faces[i][0]; j++)
			{
				
				edge_nodes[0] = verts[faces[i][1+j]];
				edge_nodes[1] = verts[faces[i][1+(j+1)%faces[i][0]]];
Kirill Terekhov's avatar
Kirill Terekhov committed
1097
				f_edges[j] = g->mesh->CreateEdge(edge_nodes).first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1098
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1099
			Face new_face = g->mesh->CreateFace(f_edges).first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1100
1101
			c_faces.push_back(new_face);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1102
		Cell c = g->mesh->CreateCell(c_faces).first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1103
1104
		if( print ) 
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1105
1106
			std::cout << __FILE__ << ":" << __LINE__ << " new cell " << c->GetHandle() << " id " << c->LocalID() << " type " << Element::GeometricTypeName(c->GetGeometricType()) << " nodes " << c->nbAdjElements(NODE) << std::endl;
			ElementArray<Element> nodes = c->getAdjElements(NODE);
Kirill Terekhov's avatar
Kirill Terekhov committed
1107
1108
1109
1110
			for(j = 0; j < nodes.size(); j++)
				std::cout << "node[" << j << "]: " << nodes[j].LocalID() << std::endl;
		}
		c->Integer(g->parent) = m;
Kirill Terekhov's avatar
Kirill Terekhov committed
1111
1112
		g->cells[m].mr->push_back(c->GetHandle());
		c->Integer(g->cell_material) = mat[0];
Kirill Terekhov's avatar
Kirill Terekhov committed
1113
1114
1115
1116
	}
	
	else
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1117
1118
1119
1120
		MarkerType face_on_face = g->mesh->CreateMarker();
		MarkerType edge_on_face = g->mesh->CreateMarker();
		MarkerType edge_on_edge = g->mesh->CreateMarker();
		MarkerType multi_edge   = g->mesh->CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
1121
		
1122
1123
1124
1125
1126
1127
1128
1129
1130
		std::vector<Element> en1;
		std::map<int,int> edges_mat;
		std::vector<Edge> face_edges;
		std::vector<Edge> potential_edges;
		std::vector<Edge> skipped_edges;
		std::vector<std::vector<Node> > edge_cut_nodes(l);
		std::vector<std::vector<Node> > edge_cut_nodes2(l);
		std::vector<Element> face_elements; // collect nodes and edges here in good order (normal outside)
		std::vector<Storage::integer> mat_intersection, mat_union, matse0,matse1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1131
		mat_ret_type mat0d,mat2d;
1132
1133
1134
1135
1136
1137
		std::vector<Node> node_in_face(l,InvalidNode());
		std::vector<HandleType> faces_on_face;
		std::vector<Face> inner_faces;
		std::vector<int> can_skip, cannot_skip, skip_mats;
		std::map<int,int> mat_cuts_on_edge[4];
		std::map<int, std::vector<Edge> > edges_by_material; // edges that lay inside inital octree face
Kirill Terekhov's avatar
Kirill Terekhov committed
1138
		ElementArray<Node> split_node(g->mesh,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
		if( print ) std::cout << "calculate cutcell" << std::endl;

	
		
		for(i = 0; i < l; i++)
		{
			if( print ) std::cout << "face " << i << "/" << l << std::endl;
			
			
			
			bool is_create_center_node = false;
			face_elements.clear();
			edges_mat.clear();
			mat_cuts_on_edge[0].clear();
			mat_cuts_on_edge[1].clear();
			mat_cuts_on_edge[2].clear();
			mat_cuts_on_edge[3].clear();
			int edge_side = -1;
			int num_good_cuts = 0;


			for(j = 0; j < faces[i][0]; j++)
			{
				bool cut_edge = false;
				if( !mid[i][1+j] ) edge_side++;
				if( edge_side > 3 ) throw -1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1165
1166
1167
				Node n0 = verts[faces[i][1+j]];
				Node n1 = InvalidNode();
				Node n2 = verts[faces[i][1+(j+1)%faces[i][0]]];
Kirill Terekhov's avatar
Kirill Terekhov committed
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
				Storage::integer_array mat0 = n0->IntegerArray( g->materials );
				Storage::integer_array mat2 = n2->IntegerArray( g->materials );
				
				if( mat0.size() > 1 ) edge_cut_nodes2[i].push_back(n0);
				if( mat2.size() > 1 ) edge_cut_nodes2[i].push_back(n2);
				
				mat_intersection.resize(std::min(mat0.size(),mat2.size()));
				mat_intersection.resize(std::set_intersection(mat0.begin(),mat0.end(),mat2.begin(),mat2.end(),mat_intersection.begin())-mat_intersection.begin());
				
				if( mat_intersection.empty() )
				{
					cut_edge = true;
					mat0d.clear();
					mat0d.insert(mat0d.end(),mat0.begin(),mat0.end());
					mat2d.clear();
					mat2d.insert(mat2d.end(),mat2.begin(),mat2.end());
				}
				else
				{
					mat0d.resize(mat0.size());
					mat0d.resize(std::set_difference(mat0.begin(),mat0.end(),mat_intersection.begin(),mat_intersection.end(),mat0d.begin())-mat0d.begin());
					mat2d.resize(mat2.size());
					mat2d.resize(std::set_difference(mat2.begin(),mat2.end(),mat_intersection.begin(),mat_intersection.end(),mat2d.begin())-mat2d.begin());
					if( !mat0d.empty() && !mat2d.empty() )
					{
						cut_edge = true;
						//~ trigger_problem = 100;
					}
				}
				
				//~ if( mat0.size() == mat2.size() )
				//~ {
					//~ cut_edge = false;
					//~ for(k = 0; k < mat0.size(); k++)
						//~ if( mat0[k] != mat2[k] )
							//~ cut_edge = true;
				//~ }
				//~ else cut_edge = true;
				
				if( j == 0 ) face_elements.push_back(n0);
				
				if( cut_edge ) //cut edge!
				{
					if( print ) std::cout << "edge " << j << " is cut!" << std::endl;
					en1.clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
1213
1214
					ElementArray<Element> nodesn0 = n0->BridgeAdjacencies(EDGE,NODE);
					ElementArray<Element> nodesn2 = n2->BridgeAdjacencies(EDGE,NODE);
Kirill Terekhov's avatar
Kirill Terekhov committed
1215
					MarkerType inter = g->mesh->CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
1216
1217
1218
1219
					
					if( inter == 0 ) throw -1;
					
					for(k = 0; k < nodesn0.size(); k++) nodesn0[k].SetMarker(inter);
Kirill Terekhov's avatar
Kirill Terekhov committed
1220
					for(k = 0; k < nodesn2.size(); k++) if( nodesn2[k].GetMarker(inter) )	en1.push_back(nodesn2[k]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1221
1222
1223
1224
1225
1226
1227
					for(k = 0; k < nodesn0.size(); k++)	nodesn0[k].RemMarker(inter);
					g->mesh->ReleaseMarker(inter);
					
					
					if( print ) std::cout << "candidates: " << en1.size() << std::endl;

					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1228
						n1 = InvalidNode();
Kirill Terekhov's avatar
Kirill Terekhov committed
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
						double v1[3], v2[3];
						make_vec(&n0->Coords()[0],&n2->Coords()[0],v1);
						double l1 = sqrt(dot_prod(v1,v1)), l2;
						v1[0] /= l1;
						v1[1] /= l1;
						v1[2] /= l1;
						if( print ) std::cout << "l1: " << l1 << std::endl;
						for(k = 0; k < en1.size(); k++)
						{
							if( print ) std::cout << "cut edge candidate " << k << "/" << en1.size() << " " << en1[k]->LocalID() << std::endl; 
							make_vec(&en1[k]->getAsNode()->Coords()[0],&n2->Coords()[0],v2);
							l2 = sqrt(dot_prod(v2,v2));
							if( print ) std::cout << "l2: " << l2 << std::endl;
							if( l2 > 0 )
							{
								v2[0] /= l2;
								v2[1] /= l2;
								v2[2] /= l2;
								if( print )
								{
									double cp[3];
									cross_prod(v1,v2,cp);
									std::cout << "cross_product squared norm: " << dot_prod(cp,cp) << " dot product " << dot_prod(v1,v2) << " lengths " << l1 << " " << l2 << std::endl;
								}
								//if( dot_prod(cp,cp) < 1e-9 && l2 < l1)
								if( fabs(dot_prod(v1,v2)-1.0) < 1e-9 && l2 < l1 )
								{
									if( print ) std::cout << "choosen this node! " << std::endl;
									n1 = en1[k]->getAsNode();
									break;
								}
							}
						}	
					}
					
Kirill Terekhov's avatar
Kirill Terekhov committed
1264
					if( n1 == InvalidNode() ) // node not found, create
Kirill Terekhov's avatar
Kirill Terekhov committed
1265
1266
1267
1268
1269
1270
1271
					{
						if( print ) std::cout << "node not found - create one" << std::endl;
						Storage::real coord[3], coord2[3];
						matcenter centers[2];
						centers[0] = matcenter(mat0d,&n0->Coords()[0],0);
						centers[1] = matcenter(mat2d,&n2->Coords()[0],0);
						//make iterations to find correct position of the node
Kirill Terekhov's avatar
Kirill Terekhov committed
1272
						int max = 16;
Kirill Terekhov's avatar
Kirill Terekhov committed
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
						int found = 0;
						for(int q = 0; q < max; q++)
						{
							//calculate mean center
							coord[0] = 0;
							coord[1] = 0;
							coord[2] = 0;
							for(int r = 0; r < 2; r++)
							{
								coord[0] += centers[r].get_center()[0];
								coord[1] += centers[r].get_center()[1];
								coord[2] += centers[r].get_center()[2];
							}
							coord[0] *= 0.5;
							coord[1] *= 0.5;
							coord[2] *= 0.5;
							
							mat_ret_type mats = g->get_material_types(coord);
							
							bool visited = false;
							for(int r = 0; r < 2; r++)
							{
								mat_ret_type inter = centers[r].intersect(mats);
								if( !inter.empty() )
								{
									coord2[0] = (coord[0] + centers[r].get_center()[0])*0.5;
									coord2[1] = (coord[1] + centers[r].get_center()[1])*0.5;
									coord2[2] = (coord[2] + centers[r].get_center()[2])*0.5;
									centers[r] = matcenter(inter,coord2, centers[r].get_count()+1);
									visited = true;
									found++;
								}
							}
							if( !visited ) 
								break;
						}
						
						if( found == 0 && !mat_intersection.empty()) 
						{
							cut_edge = false;
						}
						
						
						
						if( cut_edge )
						{
							
							mat_union.swap(mat_intersection);
							coord[0] = 0;
							coord[1] = 0;
							coord[2] = 0;
							for(int r = 0; r < 2; r++) 
							{
								coord[0] += centers[r].get_center()[0];
								coord[1] += centers[r].get_center()[1];
								coord[2] += centers[r].get_center()[2];
								
								{
									mat_intersection.resize(mat_union.size()+centers[r].get_mat().size());
									mat_intersection.resize(std::set_union(mat_union.begin(),mat_union.end(),centers[r].get_mat().begin(),centers[r].get_mat().end(),mat_intersection.begin())-mat_intersection.begin());
									mat_union.swap(mat_intersection);
								}
							}
							coord[0] *= 0.5;
							coord[1] *= 0.5;
							coord[2] *= 0.5;
							
							
							
							n1 = g->mesh->CreateNode(coord);
							//~ n1->SetMarker(g->cut_cell_node);
							Storage::integer_array mat1 = n1->IntegerArray(g->materials);
							
							
							mat1.resize(mat_union.size());
							for(int r = 0; r < mat_union.size(); ++r)
								mat1[r] = mat_union[r];

							if( print ) std::cout << "new node " << n1->LocalID() << std::endl;
						}
					}
					
					if( cut_edge )
					{
					
						Storage::integer_array mat1 = n1->IntegerArray(g->materials);
Kirill Terekhov's avatar
Kirill Terekhov committed
1359
						Edge e0, e2;
Kirill Terekhov's avatar
Kirill Terekhov committed
1360
1361
1362
1363
1364
1365
						
						edge_cut_nodes[i].push_back(n1);
						num_good_cuts++;
						
						edge_nodes[0] = n0;
						edge_nodes[1] = n1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1366
						e0 = g->mesh->CreateEdge(edge_nodes).first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
						Storage::integer_array emat0 = e0->IntegerArray(g->materials);

						if( print ) std::cout << "new edge " << e0->LocalID() << std::endl;
						
						if( emat0.empty() )
						{
							mat_intersection.resize(std::min(mat0.size(),mat1.size()));
							mat_intersection.resize(std::set_intersection(mat0.begin(),mat0.end(),mat1.begin(),mat1.end(),mat_intersection.begin())-mat_intersection.begin());
							emat0.resize(mat_intersection.size());
							for(int r = 0; r < emat0.size(); r++) emat0[r] = mat_intersection[r];
						}

						e0->SetMarker(edge_on_edge);
						
						for(int r = 0; r < emat0.size(); r++)
						{
							mat_cuts_on_edge[edge_side][emat0[r]]++;
							edges_mat[emat0[r]] |= 1 << edge_side;
						}
						
						potential_edges.push_back(e0);
						
						e0->Integer(g->edge_face_number) = edge_side;
						
						
						
						face_elements.push_back(e0);
						
						face_elements.push_back(n1);
						
						
						edge_nodes[0] = n1;
						edge_nodes[1] = n2;
Kirill Terekhov's avatar
Kirill Terekhov committed
1400
						e2 = g->mesh->CreateEdge(edge_nodes).first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
						Storage::integer_array emat2 = e2->IntegerArray(g->materials);

						if( print ) std::cout << "new edge " << e2->LocalID() << std::endl;
						
						if( emat2.empty() )
						{
							mat_intersection.resize(std::min(mat1.size(),mat2.size()));
							mat_intersection.resize(std::set_intersection(mat1.begin(),mat1.end(),mat2.begin(),mat2.end(),mat_intersection.begin())-mat_intersection.begin());
							emat2.resize(mat_intersection.size());
							for(int r = 0; r < emat2.size(); r++) emat2[r] = mat_intersection[r];
						}

						e2->SetMarker(edge_on_edge);
						
						
						for(int r = 0; r < emat2.size(); r++)
						{
							mat_cuts_on_edge[edge_side][emat2[r]]++;
							edges_mat[emat2[r]] |= 1 << edge_side;
						}
						potential_edges.push_back(e2);
							
						e2->Integer(g->edge_face_number) = edge_side;
							
						face_elements.push_back(e2);
						
						
							
						face_elements.push_back(n2);

					}
				}
				
				if( !cut_edge ) // regular edge
				{
					if( print ) std::cout << "edge " << j << " have no cut" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1437
					Edge e0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1438
1439
					edge_nodes[0] = n0;
					edge_nodes[1] = n2;
Kirill Terekhov's avatar
Kirill Terekhov committed
1440
					e0 = g->mesh->CreateEdge(edge_nodes).first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
					Storage::integer_array emat0 = e0->IntegerArray(g->materials);
					
					if( emat0.empty() )
					{
						mat_intersection.resize(std::min(mat0.size(),mat2.size()));
						mat_intersection.resize(std::set_intersection(mat0.begin(),mat0.end(),mat2.begin(),mat2.end(),mat_intersection.begin())-mat_intersection.begin());
						emat0.insert(emat0.end(),mat_intersection.begin(),mat_intersection.end());
					}

					for(int r = 0; r < emat0.size(); r++)
						edges_mat[emat0[r]] |= 1 << edge_side;
					potential_edges.push_back(e0);
					e0->SetMarker(edge_on_edge);
					
					
					e0->Integer(g->edge_face_number) = edge_side;
					
					face_elements.push_back(e0);
					
					face_elements.push_back(n2);
				}
				
			}
			face_elements.pop_back(); //remove last node, that is equal to first
			
			skipped_edges.clear();
			face_edges.clear();
			
			//~ print = 1;
			//~ if( m == 636 ) print = 1;
			
			if( print ) for(k = 0; k < face_elements.size(); k++)
			{
				std::cout << k << " " << ElementTypeName(face_elements[k]->GetElementType()) << " " << face_elements[k]->LocalID() << " materials: ";
				Storage::integer_array mats = face_elements[k]->IntegerArray(g->materials);
				for(j = 0; j < mats.size(); j++) std::cout << mats[j] << " "; std::cout << std::endl;
			}
			
			{		
				cannot_skip.clear();
				can_skip.clear();
				
1483
				for(std::vector<Element>::iterator it = face_elements.begin(); it != face_elements.end(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
1484
1485
1486
1487
1488
1489
					if( (*it)->GetElementType() == EDGE )
					{
						Storage::integer_array a = (*it)->IntegerArray(g->materials);
						if( a.size() == 1 )
							cannot_skip.push_back(a[0]);
					}
1490
				for(std::map<int,int>::iterator it = edges_mat.begin(); it != edges_mat.end() && !is_create_center_node; ++it) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
				{
					if( (it->second & 1) + ((it->second & 2) >> 1) + ((it->second & 4) >> 2) + ((it->second & 8) >> 3) <= 1 )
						can_skip.push_back(it->first);
					
				}
				std::sort(can_skip.begin(),can_skip.end());
				can_skip.resize(std::unique(can_skip.begin(),can_skip.end())-can_skip.begin());
				std::sort(cannot_skip.begin(),cannot_skip.end());
				cannot_skip.resize(std::unique(cannot_skip.begin(),cannot_skip.end())-cannot_skip.begin());
				skip_mats.resize(can_skip.size());
				skip_mats.resize(std::set_difference(can_skip.begin(),can_skip.end(),cannot_skip.begin(),cannot_skip.end(),skip_mats.begin())-skip_mats.begin());
				
				if( print )
				{
					std::cout << "cannot skip materials (" << cannot_skip.size() << "): ";
					for(j = 0; j < cannot_skip.size(); j++)
						std::cout << cannot_skip[j] << " ";
					std::cout << std::endl;
					std::cout << "can skip materials (" << can_skip.size() << "): ";
					for(j = 0; j < can_skip.size(); j++)
						std::cout << can_skip[j] << " ";
					std::cout << std::endl;
					std::cout << "skip materials (" << skip_mats.size() << "): ";
					for(j = 0; j < skip_mats.size(); j++)
						std::cout << skip_mats[j] << " ";
					std::cout << std::endl;
				}
			}
			{
				unsigned found = 0;
				Storage::integer_array mats0, mats1, matse0a, matse1a;
1522
				std::map<int,int> edge_sides;
Kirill Terekhov's avatar
Kirill Terekhov committed
1523
1524
1525
				bool stuck = false;
				while( !stuck )
				{
1526
1527
					std::map<int,int nummats;
					std::map<int,int>::iterator minmat, qt;
Kirill Terekhov's avatar
Kirill Terekhov committed
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
					int local_found = 0, last_q = -1, last_e = -1, last_j = -1, cutnodes = 0;
					Storage::integer curmat;
					for(j = 1; j < face_elements.size(); j+=2) //traverse all edges
					{
						mats0 = face_elements[j]->IntegerArray(g->materials);
						for(Storage::integer_array::iterator kt = mats0.begin(); kt != mats0.end(); ++kt)
							if( !std::binary_search(skip_mats.begin(),skip_mats.end(),*kt) )
								nummats[*kt]++;
					}
					
					if( print )
					{
						std::cout << "nummats (" << nummats.size() << "): ";
						for(qt = nummats.begin(); qt != nummats.end(); qt++)
							std::cout << "(" << qt->first << "," << qt->second << ") ";
						std::cout << std::endl;
					}
					
					if( nummats.empty() ) break;
					
					bool success = false;
					while( !success )
					{
						minmat = nummats.begin();
						if( print ) std::cout << "select min: (" << minmat->first << "," << minmat->second << ")" << std::endl;
						for(qt = nummats.begin()+1; qt != nummats.end(); qt++)
							if( qt->second < minmat->second )
							{
								minmat = qt;
								if( print ) std::cout << "select min: (" << minmat->first << "," << minmat->second << ")" << std::endl;
							}
						curmat = minmat->first;
						
						if( print ) std::cout << "current material " << curmat << std::endl;
						
1563
						std::vector< std::pair< std::pair<int,int> , int > > maxpath;
Kirill Terekhov's avatar
Kirill Terekhov committed
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687