slice_func.cpp 36.9 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1 2 3 4 5 6 7 8 9 10
#include "inmost.h"

using namespace INMOST;

double func(double x, double y, double z, int n)
{
	if( n == 0 )
		return sqrt(x*x+y*y)-0.25;
	else if( n == 1 )
		return -(sqrt((x-0.5)*(x-0.5)+(y-0.5)*(y-0.5)+(z-0.5)*(z-0.5))-0.5);
11 12 13 14 15 16 17 18 19 20 21 22 23
	else if( n == 2 )
	{
		if( x > 0.5 )
			return -(sqrt((x-0.7)*(x-0.7)+(y-0.2)*(y-0.2)+(z-0.4)*(z-0.4))-0.4);
		else
			return sqrt((x-0.3)*(x-0.3)+(y-0.4)*(y-0.4)+(z-0.5)*(z-0.5))-0.3;
	}
	else if( n == 3 )
		return y-(4*x*x*x*x-2*x*x+0.5)+z*z*z;
	else if( n == 4 )
		return y-(10*x*x*x*x-8*x*x*x-5*x*x+0.2+4*x)+z*z*z;
	else if( n == 5 )
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
24 25
		//double Lx = 0.2, Rx = 0.4, Ly = 0.1, Ry = 0.3;
		double Lx = 0., Rx = 2, Ly = 0.0, Ry = 4.5;
26 27 28 29 30 31 32 33 34 35 36 37
		if (x > Rx){
			if (y > Ry) return -sqrt( (x-Rx)*(x-Rx) + (y-Ry)*(y-Ry) );
			else return -(x-Rx);
		}
		if (x < Lx){
			if (y < Ly)  return -sqrt( (x-Lx)*(x-Lx) + (y-Ly)*(y-Ly) );
			else return (x - Lx);
		}
		if (y > Ry) return Ry - y;
		if (y < Ly) return y - Ly;
		return fmin( fmin(x-Lx,Rx-x), fmin(y-Ly, Ry-y) );
	}
38 39 40 41
	else if( n == 6 )
	{
		return std::min(std::min(sqrt((x-0.48)*(x-0.48) + (z-1)*(z-1))-0.25,sqrt((y-0.53)*(y-0.53) + (z-3)*(z-3))-0.24),sqrt((x-0.6)*(x-0.6) + (z-5)*(z-5))-0.3);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
42 43 44
	return 1;
}

45
double search(double r0, double r1, double c0[3], double c1[3], double p[3],int type, bool binary = false)
Kirill Terekhov's avatar
Kirill Terekhov committed
46
{
47 48
	double rp = 1.0e20, rp_min = 1.0e20, p_min[3];
	int iters = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
49 50
	do
	{
51 52 53 54 55 56
		double m1 = r0/(r0-r1), m0;
		if( m1 < 0.0 || m1 > 1.0 || binary ) m1 = 0.5;
		m0 = 1.0-m1;
		p[0] = m1*c1[0] + m0*c0[0];
		p[1] = m1*c1[1] + m0*c0[1];
		p[2] = m1*c1[2] + m0*c0[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
57
		rp = func(p[0],p[1],p[2],type);
58 59 60 61 62 63 64
		if( rp_min > rp )
		{
			p_min[0] = p[0];
			p_min[1] = p[1];
			p_min[2] = p[2];
			rp_min = rp;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
		if( rp*r0 < 0.0 )
		{
			c1[0] = p[0];
			c1[1] = p[1];
			c1[2] = p[2];
			r1 = rp;
		}
		else if( rp*r1 < 0.0 )
		{
			c0[0] = p[0];
			c0[1] = p[1];
			c0[2] = p[2];
			r0 = rp;
		}
79 80 81 82 83 84 85 86 87 88 89 90
		iters++;
	} while( fabs(rp) > 1.0e-9 && iters < 100 );
	
	if( rp > rp_min )
	{
		p[0] = p_min[0];
		p[1] = p_min[1];
		p[2] = p_min[2];
		rp = rp_min;
	}
	
	return rp;
Kirill Terekhov's avatar
Kirill Terekhov committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}

int main(int argc, char ** argv)
{
	if( argc < 2 )
	{
		std::cout << "Usage: " << argv[0] << " mesh  [mesh_out=grid.pmf] [type=perforated_strip]" << std::endl;
		return -1;
	}
	
	std::string grid_out = "grid.pmf";

	int type = 0;
	if( argc > 2 ) grid_out = std::string(argv[2]);
	if( argc > 3 ) type = atoi(argv[3]);
	
	

	Mesh m;
	m.Load(argv[1]);
Kirill Terekhov's avatar
Kirill Terekhov committed
111 112
	m.SetTopologyCheck(NEED_TEST_CLOSURE|PROHIBIT_MULTILINE|PROHIBIT_MULTIPOLYGON|GRID_CONFORMITY|DEGENERATE_EDGE|DEGENERATE_FACE|DEGENERATE_CELL | FACE_EDGES_ORDER | MARK_ON_ERROR);
	m.RemTopologyCheck(THROW_EXCEPTION);
113
	TagInteger material = m.CreateTag("MATERIAL",DATA_INTEGER,CELL|FACE|EDGE|NODE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
114 115 116 117
	Tag sliced = m.CreateTag("SLICED",DATA_BULK,FACE|EDGE|NODE,FACE|EDGE|NODE,1);

	std::cout << "Cells: " << m.NumberOfCells() << std::endl;
	std::cout << "Faces: " << m.NumberOfFaces() << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
118 119
	
	MarkerType original = m.CreateMarker();
120
	for(Mesh::iteratorNode it = m.BeginNode(); it != m.EndNode(); ++it) it->SetMarker(original);
Kirill Terekhov's avatar
Kirill Terekhov committed
121
	for(Mesh::iteratorEdge it = m.BeginEdge(); it != m.EndEdge(); ++it) it->SetMarker(original);
Kirill Terekhov's avatar
Kirill Terekhov committed
122 123 124 125 126 127 128 129 130 131 132

	MarkerType slice = m.CreateMarker();
	MarkerType mrk = m.CreateMarker();
	int nslice = 0, nmark = 0;
	for(Mesh::iteratorEdge it = m.BeginEdge(); it != m.EndEdge(); ++it) if( !it->GetMarker(mrk) )
	{
		double p[3],pc0[3],pc1[3];
		Storage::real_array c0 = it->getBeg()->Coords();
		Storage::real_array c1 = it->getEnd()->Coords();
		double r0 = func(c0[0],c0[1],c0[2],type);
		double r1 = func(c1[0],c1[1],c1[2],type);
133 134
		material[it->getBeg()] = (r0 <= 0)? 0 : 1;
		material[it->getEnd()] = (r1 <= 0)? 0 : 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
135 136 137 138 139
		//std::cout << "r0 " << r0 << " r1 " << r1 << std::endl;
		if( r0*r1 < -1.0e-12 )
		{
			pc0[0] = c0[0], pc0[1] = c0[1], pc0[2] = c0[2];
			pc1[0] = c1[0], pc1[1] = c1[1], pc1[2] = c1[2];
140 141 142 143 144 145 146 147
			double rp = search(r0,r1,pc0,pc1,p,type);
			if( rp > 1.0e-3 ) //cannot find intersection
			{
				rp = search(r0,r1,pc0,pc1,p,type,true);
				p[0] = c0[0]*0.5+c1[0]*0.5;
				p[1] = c0[1]*0.5+c1[1]*0.5;
				p[2] = c0[2]*0.5+c1[2]*0.5;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
148 149 150 151
			//p[0] = (r0*c1[0] - r1*c0[0])/(r0-r1);
			//p[1] = (r0*c1[1] - r1*c0[1])/(r0-r1);
			//p[2] = (r0*c1[2] - r1*c0[2])/(r0-r1);
			//std::cout << "p " << p[0] << " " << p[1] << " " << p[2] << std::endl;
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
			//distance to the corners
			
			double l0 = 0, l1 = 0, l;
			for(int r = 0; r < 3; ++r)
			{
				l0 += (p[r]-c0[r])*(p[r]-c0[r]);
				l1 += (p[r]-c1[r])*(p[r]-c1[r]);
			}
			l0 = sqrt(l0);
			l1 = sqrt(l1);
			l = l0+l1;
			if( l0 < 1.0e-5*l )
			{
				material[it->getBeg()] = 2;
				it->getBeg()->SetMarker(slice);
				nmark++;
			}
			else if( l1 < 1.0e-5*l )
			{
				material[it->getEnd()] = 2;
				it->getEnd()->SetMarker(slice);
				nmark++;
			}
			else
			{
				Node n = m.CreateNode(p);
				material[n] = 2;
				n.Bulk(sliced) = 1;
				n.SetMarker(slice);
				bool was_sliced = it->HaveData(sliced) ? true : false;
				ElementArray<Edge> ret = Edge::SplitEdge(it->self(),ElementArray<Node>(&m,1,n.GetHandle()),0);
				ret.SetMarker(mrk);
				if( was_sliced ) for(int q = 0; q < ret.size(); ++q) ret[q]->Bulk(sliced) = 1;
				nslice++;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
187 188 189 190 191
		}
		else
		{
			if( fabs(r0) < 1.0e-6 )
			{
192
				material[it->getBeg()] = 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
193 194 195 196 197
				it->getBeg()->SetMarker(slice);
				nmark++;
			}
			if( fabs(r1) < 1.0e-6 )
			{
198
				material[it->getEnd()] = 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
199 200 201 202 203 204
				it->getEnd()->SetMarker(slice);
				nmark++;
			}
		}
	}
	
205
	
Kirill Terekhov's avatar
Kirill Terekhov committed
206 207 208 209 210 211 212 213 214 215 216

	std::cout << "sliced edges: " << nslice << " marked nodes: " << nmark << std::endl;

	if( !Element::CheckConnectivity(&m) )
		std::cout << "Connectivity is broken" << std::endl;
	
	{
		int mat[3] = {0,0,0};
		int tot = 0;
		for(Mesh::iteratorNode it = m.BeginNode(); it != m.EndNode(); ++it)
		{
217
			mat[material[*it]]++;
Kirill Terekhov's avatar
Kirill Terekhov committed
218 219 220 221 222 223 224 225 226 227
			tot++;
		}
		std::cout << "node materials, 0: " << mat[0] << " 1: " << mat[1] << " 2: " << mat[2] << std::endl;
	}
	
	
	
	for(Mesh::iteratorEdge it = m.BeginEdge(); it != m.EndEdge(); ++it)
	{
		int mat[3] = {0,0,0};
228 229
		mat[material[it->getBeg()]]++;
		mat[material[it->getEnd()]]++;
Kirill Terekhov's avatar
Kirill Terekhov committed
230 231
		if( !(mat[0] == 0 || mat[1] == 0) )
		{
232
			material[*it] = 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
233 234
			std::cout << "oops, materials for edge nodes were not split, 0: " << mat[0] << " ,1: " << mat[1] << " ,2: " << mat[2] << std::endl;
		}
235 236 237
		else if( mat[0] != 0 ) material[*it] = 0;
		else if( mat[1] != 0 ) material[*it] = 1;
		else material[*it] = 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
238 239 240 241
	}
	
	
	nslice = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
242
	MarkerType unique = m.CreateMarker();
243
	for(Mesh::iteratorFace it = m.BeginFace(); it != m.EndFace(); ++it) if( !it->GetMarker(mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
244 245 246 247
	{
		ElementArray<Node> nodes = it->getNodes(slice); //those nodes should be ordered so that each pair forms an edge
		if( nodes.size() > 1 ) // if there is 1, then only one vertex touches the plane
		{
248 249
			//int nsliced = 0;
			//for(int q = 0; q < (int)nodes.size(); ++q) if( !nodes[q].GetMarker(original) ) nsliced++;
Kirill Terekhov's avatar
Kirill Terekhov committed
250
			//if there is more then two, then original face is non-convex
251 252 253
			//if( nsliced && nodes.size() > 2 ) std::cout << "Looks like face " << it->LocalID() << " is nonconvex, there is " << it->nbAdjElements(NODE) << " nodes, out of them " << nsliced << " new cuts on face" << " slice " << slice << " original " << original << std::endl;
			//else
			if( nodes.size() == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
254 255
			{
				Edge e = m.CreateEdge(nodes).first;
256
				material[e] = 2; //on slice
Kirill Terekhov's avatar
Kirill Terekhov committed
257 258 259 260
				e.Bulk(sliced) = 1;
				e.SetMarker(slice);
				bool was_sliced = it->HaveData(sliced) ? true : false;
				ElementArray<Face> ret = Face::SplitFace(it->self(),ElementArray<Edge>(&m,1,e.GetHandle()),0);
261
				ret.SetMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
262 263 264
				if( was_sliced ) for(int q = 0; q < ret.size(); ++q) ret[q]->Bulk(sliced) = 1;
				nslice++;
			}
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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
			else if( it->nbAdjElements(NODE,slice) != it->nbAdjElements(NODE) ) //not entire face is sliced
			{
				
				//std::cout << "Sliced nodes " << nodes.size() << " total nodes: ";
				nodes = it->getNodes();
				//std::cout << nodes.size() << std::endl;
				for(int q = 0; q < (int)nodes.size(); ++q)
				{
					Storage::real_array c0 = nodes[q].Coords();
					double r0 = func(c0[0],c0[1],c0[2],type);
					//std::cout << "NODE:" << nodes[q].LocalID() << " " << (nodes[q]->GetMarker(slice)?"":"not ") << "slice " << (nodes[q]->GetMarker(original)?"":"not ") << "original r="<<r0 << " material " << material[nodes[q]] << std::endl;
				}
				ElementArray<Edge> fedges = it->getEdges();
				for(int q = 0; q < (int)fedges.size(); ++q)
				{
					//std::cout << "EDGE:" << fedges[q].LocalID() << " " << fedges[q].getBeg().LocalID() << "<->" << fedges[q].getEnd().LocalID() << " " << (fedges[q]->GetMarker(slice)?"":"not ") << "slice material " << material[fedges[q]] << std::endl;
				}

				
				double c0[3],c1[3],pc0[3],pc1[3],p[3];
				it->Centroid(c0);
				double r0 = func(c0[0],c0[1],c0[2],type);
				int material0 = (r0 <= 0)? 0 : 1;
				bool s0 = false;
				if( fabs(r0) < 1.0e-6 ) s0 = true;
				
				//std::cout << "Centernode " << (s0?"sliced":"") << " r=" << r0 << std::endl;
				Node centernode = InvalidNode();
				ElementArray<Edge> split_edges(&m);
				ElementArray<Node> cutnodes(&m,nodes.size()), edge_nodes(&m,2);
				
				//calculate nodes that cut along the edges connecting centernode
				for(int q = 0; q < (int)nodes.size(); ++q) if( !nodes[q].GetMarker(slice) )
				{
					nodes[q].Centroid(c1);
					double r1 = func(c1[0],c1[1],c1[2],type);
					//std::cout << "NODE:" << nodes[q].LocalID() << " r0 " << r0 << " r1 " << r1 << " r0*r1 " << r0*r1 << std::endl;
					if( r0*r1 < -1.0e-12 )
					{
						pc0[0] = c0[0], pc0[1] = c0[1], pc0[2] = c0[2];
						pc1[0] = c1[0], pc1[1] = c1[1], pc1[2] = c1[2];
						double rp = search(r0,r1,pc0,pc1,p,type);
						if( rp > 1.0e-3 )
						{
							//std::cout << "inaccurate search " << rp;
							rp = search(r0,r1,pc0,pc1,p,type,true);
							//std::cout << " binary " << rp << std::endl;
							p[0] = c0[0]*0.5+c1[0]*0.5;
							p[1] = c0[1]*0.5+c1[1]*0.5;
							p[2] = c0[2]*0.5+c1[2]*0.5;
							
						}
						//distance to the center node
						double l0 = 0, l1 = 0, l;
						for(int r = 0; r < 3; ++r)
						{
							l0 += (p[r]-c0[r])*(p[r]-c0[r]);
							l1 += (p[r]-c1[r])*(p[r]-c1[r]);
						}
						l0 = sqrt(l0);
						l1 = sqrt(l1);
						l = l0+l1;
						if( l0 < 1.0e-3*l ) //edge goes through centernode
						{
							if( !centernode.isValid() )
								centernode = m.CreateNode(c0);
							cutnodes[q] = centernode;
							//std::cout << "selected centernode " << std::endl;
						}
						else if( l1 > 1.0e-3*l )
						{
							cutnodes[q] = m.CreateNode(p);
							//std::cout << "created new node " << std::endl;
						}
						else
						{
							material[nodes[q]] = 2;
							nodes[q].SetMarker(slice);
						}
						//else std::cout << "use old node " << std::endl;
					}
					else if( s0 )
					{
						if( !centernode.isValid() ) centernode = m.CreateNode(c0);
						cutnodes[q] = centernode;
					}
				}
				
				for(int q = 0; q < (int)cutnodes.size(); ++q) if( cutnodes[q].isValid() )
				{
					//std::cout << "New cut node " << cutnodes[q].LocalID() << " at " << q << " on line with " << nodes[q].LocalID() << " r=" << func(cutnodes[q].Coords()[0],cutnodes[q].Coords()[1],cutnodes[q].Coords()[2],type) << std::endl;
					Node n = cutnodes[q];
					material[n] = 2;
					n.Bulk(sliced) = 1;
					n.SetMarker(slice);
				}
				
				
				//go over triangles and figure out the path of the cut
				for(int q = 0; q < (int)nodes.size(); ++q)
				{
					int i1 = q;
					int i2 = (q+1)%nodes.size();
					Node n1 = nodes[i1];
					Node n2 = nodes[i2];
					bool s1 = n1->GetMarker(slice);
					bool s2 = n2->GetMarker(slice);
					if( s1 && s2 )
					{
						//cut passing through edge
						//skip this
					}
					else if( s2 )
					{
						//check cut on oposite edge
						if( cutnodes[i1].isValid() )
						{
							edge_nodes[0] = n2;
							edge_nodes[1] = cutnodes[i1];
Kirill Terekhov's avatar
Kirill Terekhov committed
384 385 386 387 388 389 390 391 392
							std::pair<Edge,bool> en = m.CreateEdge(edge_nodes);
							//if( en.second )
							{
								Edge e = en.first;
								material[e] = 2; //on slice
								e.Bulk(sliced) = 1;
								e.SetMarker(slice);
								split_edges.push_back(e);
							}
393 394 395 396 397 398 399 400 401
						}
					}
					else if( s1 )
					{
						//check cut on oposite edge
						if( cutnodes[i2].isValid() )
						{
							edge_nodes[0] = n1;
							edge_nodes[1] = cutnodes[i2];
Kirill Terekhov's avatar
Kirill Terekhov committed
402 403 404 405 406 407 408 409 410
							std::pair<Edge,bool> en = m.CreateEdge(edge_nodes);
							//if( en.second )
							{
								Edge e = en.first;
								material[e] = 2; //on slice
								e.Bulk(sliced) = 1;
								e.SetMarker(slice);
								split_edges.push_back(e);
							}
411 412 413 414 415 416
						}
					}
					else if( cutnodes[i1].isValid() && cutnodes[i2].isValid() )
					{
						edge_nodes[0] = cutnodes[i1];
						edge_nodes[1] = cutnodes[i2];
Kirill Terekhov's avatar
Kirill Terekhov committed
417 418 419 420 421 422 423 424 425
						std::pair<Edge,bool> en = m.CreateEdge(edge_nodes);
						//if( en.second )
						{
							Edge e = en.first;
							material[e] = 2; //on slice
							e.Bulk(sliced) = 1;
							e.SetMarker(slice);
							split_edges.push_back(e);
						}
426 427 428 429 430
					}
				}
				//split face with multiple edges
				if( !split_edges.empty() )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
431 432 433 434 435 436 437 438 439 440 441 442
					
					int k = 0;
					for(int q = 0; q < split_edges.size(); ++q)
					{
						if( !split_edges[q].GetMarker(unique) )
						{
							split_edges[q].SetMarker(unique);
							split_edges[k++] = split_edges[q];
						}
					}
					split_edges.RemMarker(unique);
					split_edges.resize(k);
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
					//std::cout << "Split edges " << split_edges.size() << std::endl;
					//for(int q = 0; q < (int)split_edges.size(); ++q)
					//	std::cout << split_edges[q].getBeg().LocalID() << "<->" << split_edges[q].getEnd().LocalID() << std::endl;
					bool was_sliced = it->HaveData(sliced) ? true : false;
					ElementArray<Face> ret = Face::SplitFace(it->self(),split_edges,0);
					ret.SetMarker(mrk);
					//std::cout << "New faces: " << ret.size() << ":";
					for(int q = 0; q < ret.size(); ++q)
					{
						int mat[3] = {0,0,0};
						ElementArray<Edge> fe = ret[q].getEdges();
						for( int l = 0; l < fe.size(); ++l) mat[material[fe[l]]]++;
						//std::cout << " " << ret[q].LocalID() << "[" << mat[0] << "," << mat[1] << "," << mat[2] << "]";
					}
					//std::cout << std::endl;
					if( was_sliced ) for(int q = 0; q < ret.size(); ++q) ret[q]->Bulk(sliced) = 1;
					nslice++;
				}
				//else std::cout << "No split edges " << std::endl;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
463
			//else std::cout << "No new edges on face " << it->LocalID() << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
464 465 466
		}
		//else std::cout << "Only one adjacent slice node, face " << it->LocalID() << std::endl;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
467
	m.ReleaseMarker(unique);
468
	
Kirill Terekhov's avatar
Kirill Terekhov committed
469 470 471 472 473

	nmark = 0;
	for(Mesh::iteratorEdge it = m.BeginEdge(); it != m.EndEdge(); ++it)
		if( !it->GetMarker(slice) && it->getBeg()->GetMarker(slice) && it->getEnd()->GetMarker(slice) )
		{
474
			if( material[*it] != 2 ) std::cout << "Edge supposed to get material 2, but have " << material[*it] << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488
			it->SetMarker(slice);
			nmark++;
		}

	std::cout << "sliced faces: " << nslice << " marked edges: " << nmark << std::endl;

	if( !Element::CheckConnectivity(&m) )
		std::cout << "Connectivity is broken" << std::endl;
	
	{
		int mat[3] = {0,0,0};
		int tot = 0;
		for(Mesh::iteratorEdge it = m.BeginEdge(); it != m.EndEdge(); ++it)
		{
489
			mat[material[*it]]++;
Kirill Terekhov's avatar
Kirill Terekhov committed
490 491 492 493 494 495 496 497 498 499
			tot++;
		}
		std::cout << "edge materials, 0: " << mat[0] << " 1: " << mat[1] << " 2: " << mat[2] << std::endl;
	}
	
	for(Mesh::iteratorFace it = m.BeginFace(); it != m.EndFace(); ++it)
	{
		int mat[3] = {0,0,0};
		ElementArray<Edge> edges = it->getEdges();
		for(int k = 0; k < (int)edges.size(); ++k)
500
			mat[material[edges[k]]]++;
Kirill Terekhov's avatar
Kirill Terekhov committed
501 502
		if( !(mat[0] == 0 || mat[1] == 0) )
		{
503 504
			material[*it] = 2;
			std::cout << "oops, materials for face " << it->LocalID() << " edges were not split, 0: " << mat[0] << " ,1: " << mat[1] << " ,2: " << mat[2] << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
505
		}
506 507 508
		else if( mat[0] != 0 ) material[*it] = 0;
		else if( mat[1] != 0 ) material[*it] = 1;
		else material[*it] = 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
509 510 511
	}
	
	nslice = 0;
512
	TagInteger indx = m.CreateTag("TEMP_INDX",DATA_INTEGER,NODE|EDGE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
513
	MarkerType visited = m.CreateMarker();
514 515 516
	MarkerType cmrk = m.CreateMarker();
	MarkerType isolate = m.CreateMarker();
	for(Mesh::iteratorCell it = m.BeginCell(); it != m.EndCell(); ++it) if( !it->GetMarker(mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
517 518
	{
		ElementArray<Edge> edges = it->getEdges(slice);
519
		if( !edges.empty() ) //these should form a triangle
Kirill Terekhov's avatar
Kirill Terekhov committed
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 554 555 556 557 558 559 560 561 562 563 564 565 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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 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 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 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 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
			//check edges form a simple loops, each node should be visited twice
			ElementArray<Face> split_faces(&m);
			std::map<Node,int> visit_count;
			for(ElementArray<Edge>::iterator jt = edges.begin(); jt != edges.end(); ++jt)
			{
				visit_count[jt->getBeg()]++;
				visit_count[jt->getEnd()]++;
			}
			bool simple = true;
			for(std::map<Node,int>::iterator jt = visit_count.begin(); jt != visit_count.end(); ++jt)
				simple &= (jt->second == 2);
			
			if( simple )
			{
				//gather loop by loop and create faces
				int loop_cnt = 0;
				//order edges
				ElementArray<Edge> order_edges(&m);
				
				Node last;
				int nvisited = 0;
				bool found = false;
				for(int k = 0; k < edges.size(); ++k) if( !edges[k]->GetMarker(original) )
				{
					nvisited++;
					last = InvalidNode();
					order_edges.push_back(edges[k]);
					order_edges.SetMarker(visited);
					found = true;
					break;
				}
				
				if( !found ) continue;
				
				
				while(nvisited != edges.size() )
				{
					found = false;
					for(int k = 0; k < edges.size(); ++k) if( !edges[k]->GetMarker(visited) )
					{
						bool match = false;
						if( last.isValid() )
						{
							if( edges[k]->getBeg() == last )
							{
								match = true;
								last = edges[k]->getEnd();
							}
							else if( edges[k]->getEnd() == last )
							{
								match = true;
								last = edges[k]->getBeg();
							}
						}
						else
						{
							if( edges[k]->getBeg() == order_edges.back()->getBeg() || edges[k]->getBeg() == order_edges.back()->getEnd() )
							{
								match = true;
								last = edges[k]->getEnd();
							}
							else if( edges[k]->getEnd() == order_edges.back()->getBeg() || edges[k]->getEnd() == order_edges.back()->getEnd() )
							{
								match = true;
								last = edges[k]->getBeg();
							}
						}
						if( match )
						{
							nvisited++;
							order_edges.push_back(edges[k]);
							order_edges.back().SetMarker(visited);
							found = true;
						}
					}
					if( !found || nvisited == edges.size() )
					{
						if( !order_edges.empty() )
						{
							//std::cout << "New loop " << ++loop_cnt << ": " << order_edges.size() << " total edges " << edges.size() << std::endl;
							//for(int k = 0; k < order_edges.size(); ++k)
							//	std::cout << order_edges[k].getBeg().LocalID() << "<->" << order_edges[k].getEnd().LocalID() << std::endl;
							
							//std::cout << "All:" << std::endl;
							//for(int k = 0; k < edges.size(); ++k)
							//	std::cout << edges[k].getBeg().LocalID() << "<->" << edges[k].getEnd().LocalID() << " " << (edges[k].GetMarker(visited) ? "visited":"") << " " << (edges[k].GetMarker(original) ? "original":"")<< std::endl;
							if( order_edges.size() > 2 )
							{
								std::pair<Face,bool> f = m.CreateFace(order_edges);
								material[f.first] = 2;
								f.first->Bulk(sliced) = 1;
								split_faces.push_back(f.first);
							}
							order_edges.clear();
							found = false;
							for(int k = 0; k < edges.size(); ++k) if( !edges[k]->GetMarker(visited) && !edges[k]->GetMarker(original))
							{
								nvisited++;
								last = InvalidNode();
								order_edges.push_back(edges[k]);
								order_edges.back().SetMarker(visited);
								found = true;
								break;
							}
							if( !found ) break;
						}
						else break;
					}
				}
				
				//if( !order_edges.empty() ) std::cout << "size: " << order_edges.size() << std::endl;
				
				edges.RemMarker(visited);
				
			}
			else
			{
				//edges do not breakup into simple loops, have to run complicated algorithm
				//split into pyramids joining faces
				ElementArray<Face> cfaces = it->getFaces();
				ElementArray<Edge> cedges = it->getEdges();
				ElementArray<Node> cnodes = it->getNodes();
				ElementArray<Node> cutnodes(&m,cnodes.size());
				ElementArray<Edge> cutedges(&m,cedges.size());
				ElementArray<Node> edge_nodes(&m,2);
				
				for(int k = 0; k < (int)cnodes.size(); ++k) indx[cnodes[k]] = k;
				for(int k = 0; k < (int)cedges.size(); ++k) indx[cedges[k]] = k;
				
				double c0[3],c1[3],pc0[3],pc1[3],p[3];
				it->Centroid(c0);
				double r0 = func(c0[0],c0[1],c0[2],type);
				int material0 = (r0 <= 0)? 0 : 1;
				bool s0 = false;
				if( fabs(r0) < 1.0e-6 ) s0 = true;
				
				//std::cout << "Number of cut edges: " << edges.size() << std::endl;
				//for(std::map<Node,int>::iterator jt = visit_count.begin(); jt != visit_count.end(); ++jt)
				//	std::cout << "NODE:" << jt->first.LocalID() << " visited " << jt->second << " times" << std::endl;
				//std::cout << "Centernode " << (s0?"sliced":"") << " r=" << r0 << std::endl;
				Node centernode = InvalidNode();
				
				
				//calculate nodes that cut along the edges connecting centernode
				for(int q = 0; q < (int)cnodes.size(); ++q)
				{
					if( !cnodes[q].GetMarker(slice) )
					{
						cnodes[q].Centroid(c1);
						double r1 = func(c1[0],c1[1],c1[2],type);
						//std::cout << "NODE:" << cnodes[q].LocalID() << " r0 " << r0 << " r1 " << r1 << " r0*r1 " << r0*r1 << " " << (cnodes[q].GetMarker(slice)?"":"not ") << "sliced" << std::endl;
						if( r0*r1 < -1.0e-12 )
						{
							pc0[0] = c0[0], pc0[1] = c0[1], pc0[2] = c0[2];
							pc1[0] = c1[0], pc1[1] = c1[1], pc1[2] = c1[2];
							double rp = search(r0,r1,pc0,pc1,p,type);
							if( rp > 1.0e-3 )
							{
								//std::cout << "inaccurate search " << rp;
								rp = search(r0,r1,pc0,pc1,p,type,true);
								//std::cout << " binary " << rp << std::endl;
								p[0] = c0[0]*0.5+c1[0]*0.5;
								p[1] = c0[1]*0.5+c1[1]*0.5;
								p[2] = c0[2]*0.5+c1[2]*0.5;
								
							}
							//distance to the center node
							double l0 = 0, l1 = 0, l;
							for(int r = 0; r < 3; ++r)
							{
								l0 += (p[r]-c0[r])*(p[r]-c0[r]);
								l1 += (p[r]-c1[r])*(p[r]-c1[r]);
							}
							l0 = sqrt(l0);
							l1 = sqrt(l1);
							l = l0+l1;
							if( l0 < 1.0e-3*l ) //edge goes through centernode
							{
								if( !centernode.isValid() )
									centernode = m.CreateNode(c0);
								cutnodes[q] = centernode;
								//std::cout << "selected centernode " << cutnodes[q].LocalID() << std::endl;
							}
							else if( l1 > 1.0e-3*l )
							{
								cutnodes[q] = m.CreateNode(p);
								//std::cout << "created new node " << cutnodes[q].LocalID() << std::endl;
							}
							else
							{
								material[cnodes[q]] = 2;
								cnodes[q].SetMarker(slice);
								//std::cout << "use old node " << std::endl;
							}
						}
						else if( s0 )
						{
							if( !centernode.isValid() ) centernode = m.CreateNode(c0);
							cutnodes[q] = centernode;
						}
					}
					//else cutnodes[q] = cnodes[q];
				}
				
				for(int q = 0; q < (int)cutnodes.size(); ++q) if( cutnodes[q].isValid() )
				{
					//std::cout << "New cut node " << cutnodes[q].LocalID() << " at " << q << " on line with " << cnodes[q].LocalID() << " r=" << func(cutnodes[q].Coords()[0],cutnodes[q].Coords()[1],cutnodes[q].Coords()[2],type) << std::endl;
					Node n = cutnodes[q];
					material[n] = 2;
					n.Bulk(sliced) = 1;
					n.SetMarker(slice);
				}
				
				for(int q = 0; q < (int)cnodes.size(); ++q) if( cnodes[q].GetMarker(slice) ) cutnodes[q] = cnodes[q];
				
				//now find cutedges
				
				for(int q = 0; q < (int)cedges.size(); ++q )
				{
					if( !cedges[q].GetMarker(slice ) )
					{
						Node n1 = cutnodes[indx[cedges[q].getBeg()]];
						Node n2 = cutnodes[indx[cedges[q].getEnd()]];
						if( n1.isValid() && n2.isValid() && n1 != n2 )
						{
							edge_nodes[0] = n1;
							edge_nodes[1] = n2;
							cutedges[q] = m.CreateEdge(edge_nodes).first;
						}
					}
				}
				
				for(int q = 0; q < (int)cutedges.size(); ++q) if( cutedges[q].isValid() )
				{
					//std::cout << "New cut edge " << cutedges[q].LocalID() << " at " << q << " on plane with " << cedges[q].LocalID() << " " << cutedges[q].getBeg().LocalID() << "<->" << cutedges[q].getEnd().LocalID() << " original " << cedges[q].getBeg().LocalID() << "<->" << cedges[q].getEnd().LocalID() << std::endl;
					Edge n = cutedges[q];
					material[n] = 2;
					n.Bulk(sliced) = 1;
					n.SetMarker(slice);
				}
				
				for(int q = 0; q < (int)cedges.size(); ++q) if( cedges[q].GetMarker(slice) ) cutedges[q] = cedges[q];
				
				ElementArray<Edge> split_edges(&m);
				//run over pyramids and collect faces, they should be already ordered into loops
				//although we still have to check they form closed loop
				std::map<Edge,int> vstcnt;
				for(int q = 0; q < (int)cfaces.size(); ++q)
				{
					split_edges.clear();
					ElementArray<Edge> fedges = cfaces[q].getEdges();
					for(int r = 0; r < (int)fedges.size(); ++r)
					{
						if( cutedges[indx[fedges[r]]].isValid() )
							split_edges.push_back(cutedges[indx[fedges[r]]]);
					}
					
					if( split_edges.size() > 2 ) //at least a triangle
					{
						visit_count.clear();
						for(int r = 0; r < (int)split_edges.size(); ++r)
						{
							visit_count[split_edges[r].getBeg()]++;
							visit_count[split_edges[r].getEnd()]++;
						}
						bool simple = true;
						for(std::map<Node,int>::iterator jt = visit_count.begin(); jt != visit_count.end(); ++jt)
							simple &= (jt->second == 2);
						
						if( simple )
						{
							for(int r = 0; r < split_edges.size(); ++r) vstcnt[split_edges[r]]++;
							Face f = m.CreateFace(split_edges).first;
							//std::cout << "Created face " << f.LocalID() << " with " << split_edges.size() << " edges: ";
							//for(int r = 0; r < split_edges.size(); ++r) std::cout << "EDGE:" << split_edges[r].LocalID()  << " ";
							//std::cout << " original FACE:" << cfaces[q].LocalID() << std::endl;
							material[f] = 2;
							f.Bulk(sliced) = 1;
							split_faces.push_back(f);
						}
						else
						{
							//std::cout << "Face not created, node visits:";
							//for(std::map<Node,int>::iterator jt = visit_count.begin(); jt != visit_count.end(); ++jt)
							//	std::cout << " NODE:" << jt->first.LocalID() << " " << jt->second;
							//std::cout << " edges: ";
							//for(int r = 0; r < split_edges.size(); ++r) std::cout << "EDGE:" << split_edges[r].LocalID()  << " ";
							//std::cout << " original FACE:" << cfaces[q].LocalID() << std::endl;
						}
					}
					else if( !split_edges.empty() )
					{
						//std::cout << "Face not created, edges:";
						//for(int r = 0; r < (int)split_edges.size(); ++r)
						//	std::cout << " EDGE:" << split_edges[r].LocalID();
						//std::cout << " original FACE:" << cfaces[q].LocalID() << std::endl;
					}
				}
				
				//resulting surface may touch the element with one or more edge
				//making it impossible to separate in conformal way
				//
				// in this case we have to isolate this edge with additional faces
				//
				// check if the edge is counted twice and appears on original element
				visit_count.clear();
				cedges.SetMarker(cmrk);
				
				bool isolate_alogirthm = false;
				for(std::map<Edge,int>::iterator jt = vstcnt.begin(); jt != vstcnt.end(); ++jt)
					if( jt->first.GetMarker(cmrk) && jt->second > 1 )
					{
						isolate_alogirthm = true;
						jt->first.SetMarker(isolate);
					}
				
				if( isolate_alogirthm )
				{
					//std::cout << "Isolation algorithm" << std::endl;
					//on pyramids, build faces that start from cutedges and end up with edge on original edge
					for(int q = 0; q < (int)cfaces.size(); ++q)
					{
						split_edges.clear();
						
						ElementArray<Edge> fedges = cfaces[q].getEdges();
						bool have_isolate = false;
						for(int r = 0; r < (int)fedges.size(); ++r) have_isolate |= fedges[r].GetMarker(isolate);
						if( have_isolate )
						{
							for(int r = 0; r < (int)fedges.size(); ++r) if( !fedges[r].GetMarker(isolate) && !fedges[r].GetMarker(slice) && cutedges[indx[fedges[r]]].isValid() )
							{
								split_edges.clear();
								int cnt = 0;
								Edge e;
								Face f;
								int mat = material[fedges[r]];
								e = fedges[r];
								Node e1n1 = e.getBeg();
								Node e1n2 = e.getEnd();
								Node e2n1 = cutnodes[indx[e.getBeg()]];
								Node e2n2 = cutnodes[indx[e.getEnd()]];
							
								//std::cout << "Cell edge: " << e.LocalID() << " " << e.getBeg().LocalID() << "<->" << e.getEnd().LocalID() << std::endl;
								//std::cout << "Cut edge: " << e.LocalID() << " " << cutnodes[indx[e.getBeg()]].LocalID() << "<->" << cutnodes[indx[e.getEnd()]].LocalID() << std::endl;
								split_edges.push_back(e);
								
								if( e2n1.isValid() && e2n1 != e1n1 )
								{
									edge_nodes[0] = e1n1;
									edge_nodes[1] = e2n1;
									e = m.CreateEdge(edge_nodes).first;
									material[e] = mat;
									e.Bulk(sliced) = 1;
									//std::cout << "New edge: " << e.LocalID() << " " << e.getBeg().LocalID() << "<->" << e.getEnd().LocalID() << std::endl;
									split_edges.push_back(e);
									cnt++;
								}
								
								e = cutedges[indx[fedges[r]]];
								//std::cout << "Cut edge: " << e.LocalID() << " " << e.getBeg().LocalID() << "<->" << e.getEnd().LocalID() << std::endl;
								split_edges.push_back(e);
								
								if( e2n2.isValid() && e2n2 != e1n2 )
								{
									edge_nodes[0] = e1n2;
									edge_nodes[1] = e2n2;
									e = m.CreateEdge(edge_nodes).first;
									material[e] = mat;
									e.Bulk(sliced) = 1;
									//std::cout << "New edge: " << e.LocalID() << " " << e.getBeg().LocalID() << "<->" << e.getEnd().LocalID() << std::endl;
									split_edges.push_back(e);
									cnt++;
								}
								
								if( cnt )
								{
									f = m.CreateFace(split_edges).first;
									//std::cout << "New face " << f.LocalID() << std::endl;
									material[f] = mat;
									f.Bulk(sliced) = 1;
									split_faces.push_back(f);
								}
							}
						}
					}
					cedges.RemMarker(isolate);
				}
				
				cedges.RemMarker(cmrk);
			}
			
			if( !split_faces.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
913
			{
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
				int lid = it->LocalID();
				ElementArray<Cell> ret = Cell::SplitCell(it->self(),split_faces,0);
				
				
				if(!simple )
				{
					//std::cout << (simple?"simple":"complex") << " algorithm, split cell " << lid << " with " << split_faces.size() << " faces ";
					//std::cout << " result in " << ret.size() << " cells:";
					//for(int q = 0; q < (int)ret.size(); ++q) std::cout << " " << ret[q].LocalID();
					//std::cout << std::endl;
				}
				
				ret.SetMarker(mrk);
				nslice++;
				
				if( false )//ret.size() == 1 && !simple )
Kirill Terekhov's avatar
Kirill Terekhov committed
930
				{
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
					ElementArray<Edge> cedges = ret[0].getEdges();
					
					
					cedges.SetMarker(cmrk);
					
					std::cout << "Original cut edges: " << std::endl;
					for(int k = 0; k < edges.size(); ++k) std::cout << edges[k].LocalID() << " ";
					std::cout << std::endl;
					
					std::cout << "Original cell edges: " << std::endl;
					for(int k = 0; k < cedges.size(); ++k) std::cout << cedges[k].LocalID() << " ";
					std::cout << std::endl;
					
					std::cout << "Cut faces edges: " << std::endl;
					std::map<Edge,int> vstcnt;
					for(int q = 0; q < split_faces.size(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
947
					{
948 949 950 951 952 953 954 955
						ElementArray<Edge> sedges = split_faces[q].getEdges();
						std::cout << "Face " << split_faces[q].LocalID() << ":";
						for(int r = 0; r < sedges.size(); ++r)
						{
							vstcnt[sedges[r]]++;
							std::cout << " " << sedges[r].LocalID() << "(" << (sedges[r].GetMarker(cmrk)?"s":"i") << ")";
						}
						std::cout << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
956
					}
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
					std::cout  << "Visit counts:";
					for(std::map<Edge,int>::iterator jt = vstcnt.begin(); jt != vstcnt.end(); ++jt)
						std::cout << " EDGE:" << jt->first.LocalID() << "(" << (jt->first.GetMarker(cmrk)?"s":"i") <<  ") " << jt->second;
					std::cout <<std::endl;
					
					std::cout << "Cell faces edges: " << std::endl;
					ElementArray<Face> cfaces = ret[0].getFaces();
					for(int k = 0; k < cfaces.size(); ++k)
					{
						ElementArray<Edge> sedges = cfaces[k].getEdges();
						std::cout << "Face " << cfaces[k].LocalID() << ":";
						for(int r = 0; r < sedges.size(); ++r)
						{
							vstcnt[sedges[r]]++;
							std::cout << " " << sedges[r].LocalID() << "(" << (sedges[r].GetMarker(cmrk)?"s":"i") << ")";
						}
						std::cout << std::endl;
					}
					std::cout  << "Visit counts including cell's faces:";
					for(std::map<Edge,int>::iterator jt = vstcnt.begin(); jt != vstcnt.end(); ++jt)
						std::cout << " EDGE:" << jt->first.LocalID() << "(" << (jt->first.GetMarker(cmrk)?"s":"i") <<  ") " << jt->second;
					std::cout <<std::endl;
					
					
					
					std::cout << "Cell:" << std::endl;
					for(int k = 0; k < cedges.size(); ++k)
					{
						Storage::real_array c1 = cedges[k].getBeg().Coords();
						Storage::real_array c2 = cedges[k].getEnd().Coords();
						std::cout << "(" << c1[0] << "," << c1[1] << "," << c1[2] << ")<->(" << c2[0] << "," << c2[1] << "," << c2[2] << ")" << std::endl;
					}
					std::cout << "Faces:" << std::endl;
					for(int q = 0; q < split_faces.size(); ++q)
					{
						ElementArray<Edge> sedges = split_faces[q].getEdges();
						for(int r = 0; r < sedges.size(); ++r)
						{
							Storage::real_array c1 = sedges[r].getBeg().Coords();
							Storage::real_array c2 = sedges[r].getEnd().Coords();
							std::cout << "(" << c1[0] << "," << c1[1] << "," << c1[2] << ")<->(" << c2[0] << "," << c2[1] << "," << c2[2] << ")" << std::endl;
						}
					}
					
					cedges.RemMarker(cmrk);
					
Kirill Terekhov's avatar
Kirill Terekhov committed
1003 1004 1005 1006
				}
			}
		}
	}
1007 1008
	m.ReleaseMarker(isolate);
	m.ReleaseMarker(cmrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1009
	m.ReleaseMarker(visited);
1010
	m.DeleteTag(indx);
Kirill Terekhov's avatar
Kirill Terekhov committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

	std::cout << "sliced cells: " << nslice << std::endl;
	
	if( !Element::CheckConnectivity(&m) )
		std::cout << "Connectivity is broken" << std::endl;
	
	{
		int mat[3] = {0,0,0};
		int tot = 0;
		for(Mesh::iteratorFace it = m.BeginFace(); it != m.EndFace(); ++it)
		{
1022
			mat[material[*it]]++;
Kirill Terekhov's avatar
Kirill Terekhov committed
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
			tot++;
		}
		std::cout << "face materials, 0: " << mat[0] << " 1: " << mat[1] << " 2: " << mat[2] << std::endl;
	}
	
	for(Mesh::iteratorCell it = m.BeginCell(); it != m.EndCell(); ++it)
	{
		int mat[3] = {0,0,0};
		ElementArray<Face> faces = it->getFaces();
		for(int k = 0; k < (int)faces.size(); ++k)
1033
			mat[material[faces[k]]]++;
Kirill Terekhov's avatar
Kirill Terekhov committed
1034 1035
		if( !(mat[0] == 0 || mat[1] == 0) )
		{
1036
			material[*it] = 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
1037 1038
			std::cout << "oops, materials for cell " << it->LocalID() << " faces were not split, 0: " << mat[0] << " ,1: " << mat[1] << " ,2: " << mat[2] << " slice edges " << it->getEdges(slice).size() << std::endl;
		}
1039 1040
		else if( mat[0] != 0 ) material[*it] = 0;
		else if( mat[1] != 0 ) material[*it] = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1041 1042 1043 1044 1045 1046
		else
		{
			//double cnt[3];
			//it->Centroid(cnt);
			//double v = func(cnt[0],cnt[1],cnt[2],type);
			//it->Integer(material) = (v <= 0.0 ? 0 : 1);
1047 1048
			material[*it] = 2;
			std::cout << "oops cannot determine material for cell, all faces have type 2, set to " << material[*it];
Kirill Terekhov's avatar
Kirill Terekhov committed
1049 1050 1051 1052 1053 1054 1055 1056 1057
		}
	}
	
	
	{
		int mat[3] = {0,0,0};
		int tot = 0;
		for(Mesh::iteratorCell it = m.BeginCell(); it != m.EndCell(); ++it)
		{
1058
			mat[material[*it]]++;
Kirill Terekhov's avatar
Kirill Terekhov committed
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 1087 1088 1089
			tot++;
		}
		std::cout << "cell materials, 0: " << mat[0] << " 1: " << mat[1] << " 2: " << mat[2] << std::endl;
	}
	
	Tag collapse = m.CreateTag("COLLAPSE",DATA_INTEGER,CELL,NONE,1);
	Tag el_mat = m.CreateTag("ELLIPSE_MATRIX",DATA_REAL,CELL,CELL,9);
	Tag el_cnt = m.CreateTag("ELLIPSE_CENTER",DATA_REAL,CELL,CELL,3);
	Tag level = m.CreateTag("LEVEL",DATA_REAL,CELL|FACE|EDGE|NODE,NONE,1);
	
	int ncollapsed = 0;
	for(Mesh::iteratorElement it = m.BeginElement(NODE|EDGE|FACE|CELL); it != m.EndElement(); ++it)
	{
		double cnt[3];
		it->Centroid(cnt);
		it->Real(level) = func(cnt[0],cnt[1],cnt[2],type);
	}
	
	for(Mesh::iteratorCell it = m.BeginCell(); it != m.EndCell(); ++it)
	{
		double vv, v, nvv, cnt[3];
		v = it->Volume();
		vv = v;
		nvv = 1;
		ElementArray<Cell> cells = it->NeighbouringCells();
		for(ElementArray<Cell>::iterator jt = cells.begin(); jt != cells.end(); ++jt)
		{
			vv += jt->Volume();
			nvv++;
		}
		vv /= nvv;
1090
		if( v < vv*0.01 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
		{
			if( false )
			{
				std::cout << "looks like cell " << it->LocalID() << " is too small, volume " << it->Volume() << std::endl;
				for(ElementArray<Cell>::iterator jt = cells.begin(); jt != cells.end(); ++jt)
				{
					std::cout << "adjacent cell " << jt->LocalID() << " volume " << jt->Volume() << std::endl;
				}
			}
			it->Integer(collapse) = 1;
			
			/*
			it->Centroid(cnt);
			vMatrix A(3,3), b(3,1);
			A(0,0) = unknown(1,0);
			A(1,1) = unknown(1,1);
			A(2,2) = unknown(1,2);
			A(1,2) = A(2,1) = unknown(0,3);
			A(1,3) = A(3,1) = unknown(0,4);
			A(2,3) = A(3,2) = unknown(0,5);
			b(0,0) = unknown(cnt[0],6);
			b(1,0) = unknown(cnt[1],7);
			b(2,0) = unknown(cnt[2],8);
			
			ElementArray<Node> nodes = it->getNodes();
			*/
			
			
			//find biggest face
			/*
			ElementArray<Face> faces = it->getFaces();
			Cell n = InvalidCell();
			double area = 0;
			for(int k = 0; k < (int)faces.size(); ++k)
			{
				Cell nn = it->Neighbour(faces[k]);
				if( nn.isValid() )
				{
					if( faces[k].Area() > area )
					{
						n = nn;
						area = faces[k].Area();
					}
				}
			}
			//merge cells
			if( n.isValid() )
			{
				ElementArray<Cell> unite(&m,2);
				unite[0] = it->self();
				unite[1] = n;
				Cell::UniteCells(unite,0);
			}
			 */
		}
		
	}
	
	for(Mesh::iteratorCell it = m.BeginCell(); it != m.EndCell(); ++it)
1150
		if( material[*it] == 0 || it->Integer(collapse) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1151 1152 1153 1154 1155 1156 1157 1158
			it->Delete();
	
	for(ElementType etype = FACE; etype >= NODE; etype = PrevElementType(etype) )
		for(Mesh::iteratorElement it = m.BeginElement(etype); it != m.EndElement(); ++it)
			if( it->nbAdjElements(CELL) == 0 ) it->Delete();
	

	m.ReleaseMarker(slice,NODE|EDGE);
1159 1160
	m.ReleaseMarker(original,NODE|EDGE);
	m.ReleaseMarker(mrk,EDGE|FACE|NODE);
Kirill Terekhov's avatar
Kirill Terekhov committed
1161 1162
	m.Save(grid_out);
	return 0;
1163
}