split_nonplanar.cpp 14.8 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 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
#include <stdio.h>
#include "inmost.h"


using namespace INMOST;
typedef Storage::real real;
typedef Storage::real_array real_array;

real dot(real a[3], real b[3])
{
	return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}

void cross(real a[3], real b[3], real out[3])
{
	out[0] = a[1]*b[2] - a[2]*b[1];
	out[1] = a[2]*b[0] - a[0]*b[2];
	out[2] = a[0]*b[1] - a[1]*b[0];
}

static void normalize(double v[3])
{
	double d = sqrt(dot(v, v));
	if (d) for (int k = 0; k < 3; ++k) v[k] /= d;
}


struct node
{
	real coo[2];
	node(const real _coo[2])
	{
		coo[0] = _coo[0];
		coo[1] = _coo[1];
	}
	node(const node & b)
	{
		coo[0] = b.coo[0];
		coo[1] = b.coo[1];
	}
	node & operator = (node const & b)
	{
		coo[0] = b.coo[0];
		coo[1] = b.coo[1];
		return *this;
	}
};


struct segment
{
	real beg[2];
	real end[2];
	segment(const real _beg[2], const real _end[2])
	{
		for(int k = 0; k < 2; ++k)
		{
			beg[k] = _beg[k];
			end[k] = _end[k];
		}
	}
	segment(const segment & b)
	{
		for(int k = 0; k < 2; ++k)
		{
			beg[k] = b.beg[k];
			end[k] = b.end[k];
		}
	}
	segment & operator = (segment const & b)
	{
		for(int k = 0; k < 2; ++k)
		{
			beg[k] = b.beg[k];
			end[k] = b.end[k];
		}
		return *this;
	}
};


//check whether two segments intersect
bool intersect_segments(const segment & a, const segment & b, bool print = false)
{
	const real eps = 1.0e-9;
	real div1 = (a.beg[0] - a.end[0])*(b.beg[1] - b.end[1]);
	real div2 = (a.beg[1] - a.end[1])*(b.beg[0] - b.end[0]);
	real div = div1 - div2;
	real t, find[2];
	if( print )
	{
		std::cout << "(" << a.beg[0] << "," << a.beg[1] << ",0) <-> (" << a.end[0] << "," << a.end[1] << ",0)" << std::endl;
		std::cout << "(" << b.beg[0] << "," << b.beg[1] << ",0) <-> (" << b.end[0] << "," << b.end[1] << ",0)" << std::endl;
	}
	if( print ) std::cout << "div = " << div << " div1 " << div1 << " div2 " << div2 << " against " << eps*(std::abs(div1) + std::abs(div2)) << std::endl;
	if (std::abs(div) <= eps*(std::abs(div1) + std::abs(div2))) //segments are parallel
	{
		//return true;
		if( print ) std::cout << "parallel segments, div = " << div << std::endl;
		if( std::abs(a.beg[0]-a.end[0]) < eps*(std::abs(a.beg[0])+std::abs(a.end[0])) ) //parallel to x
		{
			real a0 = a.beg[1], a1 = a.end[1];
			real b0 = b.beg[1], b1 = b.end[1];
			if( print ) std::cout << "parallel to x, projection on Oy: a [" << a0 << "," << a1 << "], b [" << b0 << "," << b1 << "]" << std::endl;
			if( a0 > a1 ) std::swap(a0,a1);
			if( b0 > b1 ) std::swap(b0,b1);
			if( a1 > b1 ) //change a and b
			{
				std::swap(a0,b0);
				std::swap(a1,b1);
			}
			if( a1 > b0+eps )
			{
				if( print ) std::cout << "intersection" << std::endl;
				return true;
			}
			if( print ) std::cout << "no intersection" << std::endl;
			return false;
		}
		//reconstruct k in y = kx+b
		real ak = (a.end[1]-a.beg[1])/(a.end[0]-a.beg[0]);
		real bk = (b.end[1]-b.beg[1])/(b.end[0]-b.beg[0]);
		//reconstruct bs in y = kx+b
		real ab = (a.end[0]*a.beg[1] - a.beg[0]*a.end[1])/(a.end[0]-a.beg[0]);
		real bb = (b.end[0]*b.beg[1] - b.beg[0]*b.end[1])/(b.end[0]-b.beg[0]);
		if( print )
		{
			std::cout << "parallel lines:" << std::endl;
			std::cout << "a: y = " << ak << "*x + " << ab << std::endl;
			std::cout << "b: y = " << bk << "*x + " << bb << std::endl;
			std::cout << "kdiff: " << ak - bk << std::endl;
			std::cout << "bdiff: " << ab - bb << " against " << eps*(std::abs(ab)+std::abs(bb)) <<  std::endl;
		}
		if( std::abs(ab-bb) <= eps*(std::abs(ab)+std::abs(bb)) )
		{
			//test segments
			int ind = ak > 1.0 ? 1 : 0; //choose projection of segments
			real a0 = a.beg[ind], a1 = a.end[ind];
			real b0 = b.beg[ind], b1 = b.end[ind];
			if( print ) std::cout << "projection on O" << (ind?"y":"x") << ": a [" << a0 << "," << a1 << "], b [" << b0 << "," << b1 << "]" << std::endl;
			if( a0 > a1 ) std::swap(a0,a1);
			if( b0 > b1 ) std::swap(b0,b1);
			if( a1 > b1 ) //change a and b
			{
				std::swap(a0,b0);
				std::swap(a1,b1);
			}
			if( a1 > b0+eps )
			{
				if( print ) std::cout << "intersection" << std::endl;
				return true;
			}
			if( print ) std::cout << "no intersection" << std::endl;
			return false;
		}
		//printf("divisor is zero\n");
		return false;
	}
	find[0] = ((a.beg[0]*a.end[1] - a.beg[1]*a.end[0])*(b.beg[0] - b.end[0]) - (a.beg[0] - a.end[0])*(b.beg[0]*b.end[1] - b.beg[1]*b.end[0])) / div;
	find[1] = ((a.beg[0]*a.end[1] - a.beg[1]*a.end[0])*(b.beg[1] - b.end[1]) - (a.beg[1] - a.end[1])*(b.beg[0]*b.end[1] - b.beg[1]*b.end[0])) / div;
	if( print ) std::cout << "intersection: (" << find[0] << "," << find[1] << ")" << std::endl;
	//probably some of these tests are redundant
	bool inter = true;
	if (std::abs(a.end[0] - a.beg[0]) > eps)
	{
		t = (find[0] - a.beg[0]) / (a.end[0] - a.beg[0]);
		if( print ) std::cout << "coef a on Ox " << t << std::endl;
		if (t < eps || t > 1.0 - eps)  inter = false;
	}
	if (std::abs(a.end[1] - a.beg[1]) > eps)
	{
		t = (find[1] - a.beg[1]) / (a.end[1] - a.beg[1]);
		if( print ) std::cout << "coef a on Oy " << t << std::endl;
		if (t < eps || t > 1.0 - eps)  inter = false;
	}
	if (std::abs(b.end[0] - b.beg[0]) > eps)
	{
		t = (find[0] - b.beg[0]) / (b.end[0] - b.beg[0]);
		if( print ) std::cout << "coef b on Ox " << t << std::endl;
		if (t < eps || t > 1.0 - eps)  inter = false;
	}
	if (std::abs(b.end[1] - b.beg[1]) > eps)
	{
		t = (find[1] - b.beg[1]) / (b.end[1] - b.beg[1]);
		if( print ) std::cout << "coef b on Oy " << t << std::endl;
		if (t < eps || t > 1.0 - eps)  inter = false;
	}
	if( inter )
	{
		if( print ) std::cout << "intersection" << std::endl;
		return true;
	}
	if( print ) std::cout << "no intersection" << std::endl;
	return false;
}

//check whether any of the segments from setb intersect any segments from seta
bool check_intersect(const std::vector<segment> & segmentsa, const std::vector<segment> & segmentsb, bool print = false)
{
	if( print )
	{
		std::cout << "segments [" << segmentsa.size() << "]:" << std::endl;
		for(int k = 0; k < segmentsa.size(); ++k)
			std::cout << "(" << segmentsa[k].beg[0] << "," << segmentsa[k].beg[1] << ",0) <-> (" << segmentsa[k].end[0] << "," << segmentsa[k].end[1] << ",0)" << std::endl;
		std::cout << "joints [" << segmentsb.size() << "]:" << std::endl;
		for(int k = 0; k < segmentsb.size(); ++k)
			std::cout << "(" << segmentsb[k].beg[0] << "," << segmentsb[k].beg[1] << ",0) <-> (" << segmentsb[k].end[0] << "," << segmentsb[k].end[1] << ",0)" << std::endl;
	}
	for(int i = 0; i < (int)segmentsb.size(); ++i)
	{
		for(int j = 0; j < (int)segmentsa.size(); ++j)
		{
			if( print ) std::cout << "check segment " << j << " and joint " << i << std::endl;
			if( intersect_segments(segmentsa[j],segmentsb[i],print) )
			{
				if( print ) std::cout << "there is intersection of segment " << j << " and joint " << i << std::endl;
				return true;
			}
		}
	}
	if( print ) std::cout << "there is no intersection" << std::endl;
	return false;
}


//check whether any of the centers of setb drop outside of loop represented by seta
bool check_dropout(const std::vector<node> & loop, const std::vector<segment> & segmentsb)
{
	for(int i = 0; i < (int)segmentsb.size(); ++i)
	{
		real cnt[2];
		cnt[0] = (segmentsb[i].beg[0]+segmentsb[i].end[0])*0.5;
		cnt[1] = (segmentsb[i].beg[1]+segmentsb[i].end[1])*0.5;
		
		int    cn = 0;    // the  crossing number counter
		// loop through all edges of the polygon
		for (int j = 0; j < loop.size()-1; j++)  // edge from V[i]  to V[i+1]
		{
			real px = cnt[0];
			real py = cnt[1];
			real v0x = loop[j].coo[0];
			real v0y = loop[j].coo[1];
			real v1x = loop[j+1].coo[0];
			real v1y = loop[j+1].coo[1];
			if (   ((v0y <= py) && (v1y >  py)) // an upward crossing
				|| ((v0y  > py) && (v1y <= py)))// a downward crossing
			{
				// compute  the actual edge-ray intersect x-coordinate
				real vt = (py  - v0y) / (v1y - v0y);
				if (px <  v0x + vt * (v1x - v0x)) // P.x < intersect
					++cn;   // a valid crossing of y=P.y right of P.x
			}
		}
		if( !(cn&1) ) //out of polygon
			return true;
	}
	return false;
}


int main(int argc, char ** argv)
{
	if( argc < 2 )
	{
		printf("Usage: %s input_mesh [output_mesh]\n",argv[0]);
		return -1;
	}
	
	Mesh A("");
	A.Load(argv[1]);
	
	Mesh::GeomParam table;
	table[ORIENTATION] = FACE;
	table[BARYCENTER] = FACE | CELL | EDGE;
	A.PrepareGeometricData(table);
	
	Tag proj_coords = A.CreateTag("PROJECTED_COORDS",DATA_REAL,NODE,NONE,2);
Kirill Terekhov's avatar
Kirill Terekhov committed
278
	MarkerType myedges = A.CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
279
	real nrm[3], cnt[3], ncnt[3], scnt[3], pcnt[3], fcnt[3], ray[3], orthx[3], orthy[3], d, nd, c[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
280 281
	std::cout << "Start splitting faces" << std::endl;
	int nsplit = 0, had_faces = A.NumberOfFaces();
Kirill Terekhov's avatar
Kirill Terekhov committed
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
	for(Mesh::iteratorFace it = A.BeginFace(); it != A.EndFace(); ++it)
	{
		if( !it->Planarity() )
		{
			std::vector<segment> segments;
			it->UnitNormal(nrm);
			it->Centroid(cnt);
			d = dot(cnt,nrm);
			ElementArray<Node> face_nodes = it->getNodes();
			//project coordinates
			for(ElementArray<Node>::iterator mt = face_nodes.begin(); mt != face_nodes.end(); ++mt)
			{
				mt->Centroid(ncnt);
				nd = dot(ncnt,nrm)-d;
				//project onto plane
				pcnt[0] = ncnt[0] - cnt[0];
				pcnt[1] = ncnt[1] - cnt[1];
				pcnt[2] = ncnt[2] - cnt[2];
				//choose the basis
				if( mt == face_nodes.begin() )
				{
					orthx[0] = pcnt[0];
					orthx[1] = pcnt[1];
					orthx[2] = pcnt[2];
					normalize(orthx);
					cross(orthx,nrm,orthy);
				}
				//compute and record the coords
				real_array c = mt->RealArray(proj_coords);
				c[0] = dot(orthx,pcnt);
				c[1] = dot(orthy,pcnt);
			}
			
			//gather all the segments in projected coordinates
			ElementArray<Edge> face_edges = it->getEdges();
Kirill Terekhov's avatar
Kirill Terekhov committed
317
			face_edges.SetMarker(myedges);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
			for(ElementArray<Edge>::iterator mt = face_edges.begin(); mt != face_edges.end(); ++mt)
				segments.push_back(segment(mt->getBeg().RealArray(proj_coords).data(),mt->getEnd().RealArray(proj_coords).data()));
			//form a loop out of face nodes
			std::vector<node> loop;
			for(ElementArray<Node>::iterator mt = face_nodes.begin(); mt != face_nodes.end(); ++mt)
				loop.push_back(node(mt->RealArray(proj_coords).data()));
			loop.push_back(loop.front());
			//try each adjacent node
			bool success = false;
			for(ElementArray<Node>::iterator jt = face_nodes.begin(); jt != face_nodes.end() && !success; ++jt)
			{
				std::vector<segment> joints;
				//check that the node sees centers of all segments
				for(ElementArray<Edge>::iterator mt = face_edges.begin(); mt != face_edges.end(); ++mt)
				{
					if( mt->getBeg() != jt->self() && mt->getEnd() != jt->self() ) 	//skip adjacent to the node
					{
						scnt[0] = (mt->getBeg()->RealArray(proj_coords)[0]+mt->getEnd()->RealArray(proj_coords)[0])*0.5;
						scnt[1] = (mt->getBeg()->RealArray(proj_coords)[1]+mt->getEnd()->RealArray(proj_coords)[1])*0.5;
						joints.push_back(segment(jt->RealArray(proj_coords).data(),scnt));
					}
				}
				
				if( !check_intersect(segments,joints) && !check_dropout(loop,joints) )
				{
					//found a suitable node
					ElementArray<Edge> new_edges(&A);
					ElementArray<Node> edge_nodes(&A,2);
					edge_nodes[0] = jt->self();
					for(ElementArray<Node>::iterator kt = face_nodes.begin(); kt != face_nodes.end(); ++kt)
					{
						if( kt->self() != jt->self() )
						{
							edge_nodes[1] = kt->self();
							std::pair<Edge,bool> edge = A.CreateEdge(edge_nodes);
Kirill Terekhov's avatar
Kirill Terekhov committed
353 354
							//if( edge.second ) //this is actually a new edge
							if( !edge.first.GetMarker(myedges) )
Kirill Terekhov's avatar
Kirill Terekhov committed
355 356 357
								new_edges.push_back(edge.first);
						}
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
358 359
					int had_id = it->LocalID();
					int had_nodes = it->nbAdjElements(NODE);
Kirill Terekhov's avatar
Kirill Terekhov committed
360
					ElementArray<Face> split_faces = Face::SplitFace(it->self(),new_edges,0);
Kirill Terekhov's avatar
Kirill Terekhov committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
					for(int q = 0; q < split_faces.size(); ++q)
						if( !split_faces[q].Planarity() )
						{
							std::cout << __FILE__ << ":" << __LINE__ << "Face " << split_faces[q].LocalID() << " non-planar after split, nodes " << split_faces[q].nbAdjElements(NODE) << " original face " << had_id << " with " << had_nodes << " nodes, split by " << new_edges.size() << " edges, resulted in " << split_faces.size() << " new faces" << std::endl;
							std::cout << "original nodes:";
							for(ElementArray<Node>::iterator kt = face_nodes.begin(); kt != face_nodes.end(); ++kt)
								std::cout << " " << kt->LocalID();
							std::cout << std::endl;
							std::cout << "split node " << jt->LocalID() << std::endl;
							std::cout << "split edges:";
							for(ElementArray<Edge>::iterator kt = new_edges.begin(); kt != new_edges.end(); ++kt)
								std::cout << " " << kt->LocalID() << " (" << kt->getBeg().LocalID() << "," << kt->getEnd().LocalID() << ")";
							std::cout << std::endl;
						}
					nsplit++;
Kirill Terekhov's avatar
Kirill Terekhov committed
376 377 378
					success = true;
				}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
			face_edges.RemMarker(myedges);
			if( !success )
			{
				std::cout << "Face " << it->LocalID() << ", nodes " << it->nbAdjElements(NODE) << ", testing center" << std::endl;
				std::vector<segment> joints;
				//check that the node sees centers of all segments
				for(int k = 0; k < (int)segments.size(); ++k)
				{
					const real cnt0[2] = {0.0,0.0};
					scnt[0] = (segments[k].beg[0]+segments[k].end[0])*0.5;
					scnt[1] = (segments[k].beg[1]+segments[k].end[1])*0.5;
					joints.push_back(segment(cnt0,scnt));
				}
				if( check_intersect(segments,joints) )
				{
					std::cout << "Center of face " << it->LocalID() << " is not suitable to divide it" << std::endl;
				}
				else
				{
					//found a suitable node
					ElementArray<Edge> new_edges(&A);
					ElementArray<Node> edge_nodes(&A,2);
					edge_nodes[0] = A.CreateNode(cnt);
					for(ElementArray<Node>::iterator kt = face_nodes.begin(); kt != face_nodes.end(); ++kt)
					{
						edge_nodes[1] = kt->self();
						std::pair<Edge,bool> edge = A.CreateEdge(edge_nodes);
						new_edges.push_back(edge.first);
					}
					ElementArray<Face> split_faces = Face::SplitFace(it->self(),new_edges,0);
					int had_id = it->LocalID();
					int had_nodes = it->nbAdjElements(NODE);
					for(int q = 0; q < split_faces.size(); ++q)
						if( !split_faces[q].Planarity() )
							std::cout << __FILE__ << ":" << __LINE__ << "Face " << split_faces[q].LocalID() << " non-planar after split, nodes " << split_faces[q].nbAdjElements(NODE) << " original face " << had_id << " with " << had_nodes << " nodes, split by " << new_edges.size() << " edges, resulted in " << split_faces.size() << " new faces" << std::endl;
					nsplit++;
					success = true;
				}
			}
			if( !success ) std::cout << "Cannot split face " << it->LocalID() << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
419 420 421
		}
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
422 423 424 425 426 427 428
	std::cout <<"total split: " << nsplit << " / " << had_faces << std::endl;
	A.ReleaseMarker(myedges);
	for(Mesh::iteratorFace it = A.BeginFace(); it != A.EndFace(); ++it)
		if( !it->Planarity() )
			std::cout << it->LocalID() << " is still non-planar, nodes " << it->nbAdjElements(NODE) << std::endl;
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	if( A.HaveTag("GRIDNAME") )
	{
		Storage::bulk_array nameA = A.self().BulkArray(A.GetTag("GRIDNAME"));
		std::string ins = "_split_nonplanar";
		nameA.insert(nameA.end(),ins.c_str(),ins.c_str()+ins.size());
	}
	
	if( argc > 2 )
	{
		std::cout << "Save to " << argv[2] << std::endl;
		A.Save(argv[2]);
	}
	else
	{
		std::cout << "Save to out.vtk" << std::endl;
		A.Save("out.vtk");
	}

	return 0;
}