intersection.hpp 13.2 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
 * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
 * version 0.1                                                                  *
4
 * Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *                                                                              *
 * This library is free software; you can redistribute it and/or modify it      *
 * under the terms of the GNU Lesser General Public License as published by the *
 * Free Software Foundation; either version 2.1 of the License, or (at your     *
 * option) any later version.                                                   *
 *                                                                              *
 * This library is distributed in the hope that it will be useful, but WITHOUT  *
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
 * for more details.                                                            *
 *                                                                              *
 * You should have received a copy of the GNU Lesser General Public License     *
 * along with this library; if not, write to the Free Software Foundation,      *
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
 *                                                                              *
20
 * Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

namespace CGoGN
{

namespace Geom
{

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

template <typename VEC3>
Intersection intersectionLinePlane(const VEC3& P, const VEC3& Dir, const VEC3& PlaneP,  const VEC3& NormP, VEC3& Inter)
{
	float b = NormP * Dir;

	#define PRECISION 1e-20
	if(fabs(b) < PRECISION)		//ray parallel to triangle
	{
		VEC3 v= PlaneP - P;
		float c = NormP * v;
		if(fabs(c) < PRECISION)
			return EDGE_INTERSECTION;
		return NO_INTERSECTION ;
	}
    #undef PRECISION

	float a = NormP * (PlaneP - P);

	Inter = P + (a/b)*Dir;
	return FACE_INTERSECTION;
}


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
//template <typename VEC3>
//Intersection intersectionRayTriangleFaceOnly(const VEC3& P, const VEC3& Dir, const VEC3& Ta,  const VEC3& Tb, const VEC3& Tc)
//{
//	typedef typename VEC3::DATA_TYPE T ;
//
//	VEC3 u = Ta - P ;
//	VEC3 v = Tb - P ;
//	VEC3 w = Tc - P ;
//	T x = tripleProduct(Dir, u, v) ;
//	T y = tripleProduct(Dir, v, w) ;
//	T z = tripleProduct(Dir, w, u) ;
//	if((x < T(0) && y < T(0) && z < T(0)) || (x > T(0) && y > T(0) && z > T(0)))
//		return FACE_INTERSECTION ;
//	return NO_INTERSECTION ;
//}


template <typename VEC3>
Intersection intersectionRayTriangleOpt(const VEC3& P, const VEC3& Dir, const VEC3& Ta,  const VEC3& Tb, const VEC3& Tc, VEC3& Inter)
{
	typedef typename VEC3::DATA_TYPE T ;

	VEC3 u = Ta - P ;
	VEC3 v = Tb - P ;
	VEC3 w = Tc - P ;

	T x = tripleProduct(Dir, u, v) ;
	T y = tripleProduct(Dir, v, w) ;
	T z = tripleProduct(Dir, w, u) ;

	unsigned int np=0;
	unsigned int nn=0;
	unsigned int nz=0;

	if (x>T(0))
		++np;
	else
		if (x<T(0))
			++nn;
		else ++nz;

	if (y>T(0))
		++np;
	else
		if (y<T(0))
			++nn;
		else ++nz;

	if (z>T(0))
		++np;
	else
		if (z<T(0))
			++nn;
		else ++nz;

	if ((np !=0) && (nn!=0))
		return NO_INTERSECTION;

	T sum = x + y + z ;
	T alpha = y / sum ;
	T beta = z / sum ;
	T gamma = T(1) - alpha - beta ;
	Inter = Ta * alpha + Tb * beta + Tc * gamma ;


	return Intersection(FACE_INTERSECTION-nz);

//	switch(nz)
//	{
//	case 0:
//		{
//			T sum = x + y + z ;
//			T alpha = y / sum ;
//			T beta = z / sum ;
//			T gamma = T(1) - alpha - beta ;
//			Inter = Ta * alpha + Tb * beta + Tc * gamma ;
//			return FACE_INTERSECTION ;
//		}
//		break;
//	case 1:
//		return EDGE_INTERSECTION;
//		break;
//	case 2:
//		return VERTEX_INTERSECTION;
//		break;
//	default:
//		return NO_INTERSECTION;
//		break;
//	}
//	// just for no warning compilation
//	return NO_INTERSECTION;
}



template <typename VEC3>
Intersection intersectionRayTriangleOpt(const VEC3& P, const VEC3& Dir, const VEC3& Ta,  const VEC3& Tb, const VEC3& Tc)
{
	typedef typename VEC3::DATA_TYPE T ;

	VEC3 u = Ta - P ;
	VEC3 v = Tb - P ;
	VEC3 w = Tc - P ;

	T x = tripleProduct(Dir, u, v) ;
	T y = tripleProduct(Dir, v, w) ;
	T z = tripleProduct(Dir, w, u) ;

	unsigned int np=0;
	unsigned int nn=0;
	unsigned int nz=0;

	if (x>T(0))
		++np;
	else
		if (x<T(0))
			++nn;
		else ++nz;

	if (y>T(0))
		++np;
	else
		if (y<T(0))
			++nn;
		else ++nz;

	if (z>T(0))
		++np;
	else
		if (z<T(0))
			++nn;
		else ++nz;

	if ((np !=0) && (nn!=0))
		return NO_INTERSECTION;

	return Intersection(FACE_INTERSECTION-nz);

}

195

Pierre Kraemer's avatar
Pierre Kraemer committed
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
template <typename VEC3>
Intersection intersectionRayTriangle(const VEC3& P, const VEC3& Dir, const VEC3& Ta,  const VEC3& Tb, const VEC3& Tc, VEC3& Inter)
{
	typedef typename VEC3::DATA_TYPE T ;

	VEC3 u = Ta - P ;
	VEC3 v = Tb - P ;
	VEC3 w = Tc - P ;
	T x = tripleProduct(Dir, u, v) ;
	T y = tripleProduct(Dir, v, w) ;
	T z = tripleProduct(Dir, w, u) ;
	if((x < T(0) && y < T(0) && z < T(0)) || (x > T(0) && y > T(0) && z > T(0)))
	{
		T sum = x + y + z ;
		T alpha = y / sum ;
		T beta = z / sum ;
		T gamma = T(1) - alpha - beta ;
		Inter = Ta * alpha + Tb * beta + Tc * gamma ;
		return FACE_INTERSECTION ;
	}
	if(
		( x == T(0) && y > T(0) && z > T(0) ) || ( x == T(0) && y < T(0) && z < T(0) ) || // intersection on [Ta,Tb]
		( x > T(0) && y == T(0) && z > T(0) ) || ( x < T(0) && y == T(0) && z < T(0) ) || // intersection on [Tb,Tc]
		( x > T(0) && y > T(0) && z == T(0) ) || ( x < T(0) && y < T(0) && z == T(0) )	  // intersection on [Tc,Ta]
	)
		return EDGE_INTERSECTION ;
	if(
		( x == T(0) && y == T(0) && z != T(0) ) || // intersection on Tb
		( x == T(0) && y != T(0) && z == T(0) ) || // intersection on Ta
		( x != T(0) && y == T(0) && z == T(0) )	   // intersection on Tc
	)
		return VERTEX_INTERSECTION ;
	return NO_INTERSECTION ;
}

template <typename VEC3>
Intersection intersectionLineTriangle(const VEC3& P, const VEC3& Dir, const VEC3& Ta,  const VEC3& Tb, const VEC3& Tc, VEC3& Inter)
{
	typedef typename VEC3::DATA_TYPE T ;

	VEC3 u = Tb - Ta ;
	VEC3 v = Tc - Ta ;
	VEC3 n = u ^ v ;

	VEC3 w0 = P - Ta ;
    T a = -(n * w0) ;
    T b = (n * Dir) ;

#define PRECISION 1e-20
    if(fabs(b) < PRECISION)			//ray parallel to triangle
untereiner's avatar
untereiner committed
246
			return NO_INTERSECTION ;
Pierre Kraemer's avatar
Pierre Kraemer committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
#undef PRECISION

	T r = a / b ;
	Inter = P + r * Dir ;			// intersect point of ray and plane

    // is I inside T?
	T uu = u.norm2() ;
	T uv = u * v ;
	T vv = v.norm2() ;
	VEC3 w = Inter - Ta ;
	T wu = w * u ;
	T wv = w * v ;
	T D = (uv * uv) - (uu * vv) ;

    // get and test parametric coords
	T s = ((uv * wv) - (vv * wu)) / D ;
	if(s < T(0) || s > T(1))
		return NO_INTERSECTION ;
	T t = ((uv * wu) - (uu * wv)) / D ;
	if(t < T(0) || (s + t) > T(1))
        return NO_INTERSECTION ;

	if((s == T(0) || s == T(1)))
		if(t == T(0) || t == T(1))
			return VERTEX_INTERSECTION ;
		else
			return EDGE_INTERSECTION ;
	else if(t == T(0) || t == T(1))
			return EDGE_INTERSECTION ;

    return FACE_INTERSECTION ;
}

template <typename VEC3>
Intersection intersectionLineTriangle2D(const VEC3& P, const VEC3& Dir, const VEC3& Ta,  const VEC3& Tb, const VEC3& Tc, VEC3& Inter)
{
	Inclusion inc = isPointInTriangle(P,Ta,Tb,Tc) ;
	VEC3 N = Ta ^ Tb ;

	switch(inc)
	{
		case FACE_INCLUSION :
			Inter = P ;
			return FACE_INTERSECTION ;
		case EDGE_INCLUSION :
			Inter = P ;
			return EDGE_INTERSECTION ;
		case VERTEX_INCLUSION :
			Inter = P ;
			return VERTEX_INTERSECTION ;
		default : //NO_INCLUSION : test if ray enters the triangle
			VEC3 P2 = P + Dir ;
			Orientation2D oA = testOrientation2D(Ta, P, P2, N) ;
			Orientation2D oB = testOrientation2D(Tb, P, P2, N) ;
			Orientation2D oC = testOrientation2D(Tc, P, P2, N) ;

			if(oA == oB && oB == oC)
				return NO_INTERSECTION ;

			Orientation2D oPBC = testOrientation2D(P,Tb,Tc,N) ;
			if(oPBC == LEFT)  // same side of A, test edge AC and AB
			{
				if(oA == LEFT)
				{
					if(oB == ALIGNED)
					{
						Inter = Tb ;
						return VERTEX_INTERSECTION ;
					}
					//inter with AB
317
//					CGoGNout << __FILE__ << " TODO compute edge coplanar intersection AB" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
318 319 320 321 322 323 324 325 326 327 328 329 330
					return EDGE_INTERSECTION ;
				}
				if(oA == ALIGNED)
				{
					Inter = Ta ;
					return VERTEX_INTERSECTION ;
				}
				if(oC == ALIGNED)
				{
					Inter = Tc ;
					return VERTEX_INTERSECTION ;
				}
				//inter with AC
331
//				CGoGNout << __FILE__ << " TODO compute edge coplanar intersection AC" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
				return EDGE_INTERSECTION ;
			}
			if(oPBC == RIGHT) // same side of BC, test this edge
			{
				if(oB == ALIGNED)
				{
					Inter = Tb ;
					return VERTEX_INTERSECTION ;
				}
				if(oC == ALIGNED)
				{
					Inter = Tc ;
					return VERTEX_INTERSECTION ;
				}
				//inter with BC
347
//				CGoGNout << __FILE__ << " TODO compute edge coplanar intersection BC" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361
				return EDGE_INTERSECTION ;
			}

			//aligned with BC
			//possibly colliding with edge AB or AC
			Orientation2D oPAB = testOrientation2D(P,Ta,Tb,N) ;
			if(oPAB == RIGHT) //possibly colliding with edge AB
			{
				if(oA == ALIGNED)
				{
					Inter = Ta ;
					return VERTEX_INTERSECTION ;
				}
				//inter with AB
362
//				CGoGNout << __FILE__ << " TODO compute edge coplanar intersection AB" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
				return EDGE_INTERSECTION ;
			}
			if(oPAB == ALIGNED)
			{
				Inter = Tb ;
				return VERTEX_INTERSECTION ;
			}
			//possibly colliding with edge AC
			else if(oC == ALIGNED)
			{
				Inter = Tc ;
				return VERTEX_INTERSECTION ;
			}
			else if(oA == ALIGNED)
			{
				Inter = Ta ;
				return VERTEX_INTERSECTION ;
			}
			//inter with AC
382
//			CGoGNout << __FILE__ << " TODO compute edge coplanar intersection AC" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
			return EDGE_INTERSECTION ;
	}
}

template <typename VEC3>
Intersection intersectionSegmentTriangle(const VEC3& PA, const VEC3& PB, const VEC3& Ta, const VEC3& Tb, const VEC3& Tc, VEC3& Inter)
{
	typedef typename VEC3::DATA_TYPE T ;
	const T precision = std::numeric_limits<T>::min();

	VEC3 u = Tb - Ta ;
	VEC3 v = Tc - Ta ;
	VEC3 Dir = PB - PA ;

	VEC3 n = u ^ v ;

	VEC3 w0 = PA - Ta ;
    float a = -(n * w0) ;
    float b = (n * Dir) ;

    if(fabs(b) < precision)			//ray parallel to triangle
404
		return NO_INTERSECTION ;
Pierre Kraemer's avatar
Pierre Kraemer committed
405 406 407 408 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

	//compute intersection
	T r = a / b ;

	if((r < -precision) || (r > (T(1) + precision)))
		return NO_INTERSECTION;

	Inter = PA + r * Dir;			// intersect point of ray and plane

    // is I inside T?
	T uu = u.norm2() ;
	T uv = u * v ;
	T vv = v.norm2() ;
	VEC3 w = Inter - Ta ;
	T wu = w * u ;
	T wv = w * v ;
	T D = (uv * uv) - (uu * vv) ;

    // get and test parametric coords
	T s = ((uv * wv) - (vv * wu)) / D ;
	if(s < T(0) || s > T(1))
		return NO_INTERSECTION ;
	T t = ((uv * wu) - (uu * wv)) / D ;
	if(t < T(0) || (s + t) > T(1))
        return NO_INTERSECTION ;

	if((s == T(0) || s == T(1)))
		if(t == T(0) || t == T(1))
			return VERTEX_INTERSECTION ;
		else
			return EDGE_INTERSECTION ;
	else if(t == T(0) || t == T(1))
			return EDGE_INTERSECTION ;

    return FACE_INTERSECTION ;
}

// template <typename VEC3>
// Intersection intersectionSegmentSegment2D(const VEC3& PA, const VEC3& PB, const VEC3& PC,  const VEC3& PD, VEC3& Inter) 
// {
445
// 	CGoGNout << __FILE__ << " " << __LINE__ << " to write intersectionSegmentSegment2D" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
446 447 448 449
// 	return NO_INTERSECTION;
// }

template <typename VEC3, typename PLANE3D>
untereiner's avatar
untereiner committed
450
Intersection intersectionPlaneRay(const PLANE3D& pl,const VEC3& p1,const VEC3& dir, VEC3& Inter)
Pierre Kraemer's avatar
Pierre Kraemer committed
451
{
untereiner's avatar
untereiner committed
452
	typename VEC3::DATA_TYPE denom = pl.normal()*dir;
453

untereiner's avatar
untereiner committed
454
	if(denom==0)
455
	{
untereiner's avatar
untereiner committed
456
		if(pl.distance(p1)==0)
457
		{
untereiner's avatar
untereiner committed
458 459
				Inter = p1;
				return FACE_INTERSECTION;
460 461 462 463 464
		}
		else
			return NO_INTERSECTION;
	}

untereiner's avatar
untereiner committed
465
	typename VEC3::DATA_TYPE isect = ( pl.normal() * (pl.normal()*-1.0f*pl.d()-p1) ) / denom;
466 467

	Inter = p1 + dir * isect;
Pierre Kraemer's avatar
Pierre Kraemer committed
468 469

	if(0.0f <= isect)
untereiner's avatar
untereiner committed
470
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
471
		return FACE_INTERSECTION;
untereiner's avatar
untereiner committed
472
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
473 474 475 476 477

	return NO_INTERSECTION;
}

template <typename VEC3>
untereiner's avatar
untereiner committed
478
Intersection intersection2DSegmentSegment(const VEC3& PA, const VEC3& PB, const VEC3& QA,  const VEC3& QB, VEC3& Inter)
Pierre Kraemer's avatar
Pierre Kraemer committed
479 480 481
{
	typedef typename VEC3::DATA_TYPE T ;

untereiner's avatar
untereiner committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	VEC3 vp1p2 = PB - PA;
	VEC3 vq1q2 = QB - QA;
	VEC3 vp1q1 = QA - PA;
	T delta = vp1p2[0]*vq1q2[1]- vp1p2[1]*vq1q2[0];
	T coeff = vp1q1[0]*vq1q2[1]- vp1q1[1]*vq1q2[0];

	if(delta==0) //parallel
	{
		//test if collinear
		if(coeff==0)
		{
			//collinear
			//TODO : check if there is a common point between the two edges
			Inter = QA;
			return EDGE_INTERSECTION;
		}
		else
			return NO_INTERSECTION;
	}
	else
		Inter = VEC3((PA[0]*delta+vp1p2[0]*coeff)/delta,(PA[1]*delta+vp1p2[1]*coeff)/delta,(PA[2]*delta+vp1p2[2]*coeff)/delta);

Pierre Kraemer's avatar
Pierre Kraemer committed
504

505
	//test if inter point is outside the edges
untereiner's avatar
untereiner committed
506 507 508 509
	if( (Inter[0]<PA[0] && Inter[0]<PB[0]) || (Inter[0]>PA[0] && Inter[0]>PB[0])
			|| (Inter[0]<QA[0] && Inter[0]<QB[0]) || (Inter[0]>QA[0] && Inter[0]>QB[0])
			|| (Inter[1]<PA[1] && Inter[1]<PB[1]) || (Inter[1]>PA[1] && Inter[1]>PB[1])
			|| (Inter[1]<QA[1] && Inter[1]<QB[1]) || (Inter[1]>QA[1] && Inter[1]>QB[1])
510 511 512
	)
		return NO_INTERSECTION;

untereiner's avatar
untereiner committed
513 514 515
	if(Geom::arePointsEquals(PA,Inter) || Geom::arePointsEquals(PB,Inter) || Geom::arePointsEquals(QA,Inter) || Geom::arePointsEquals(QB,Inter))
		return VERTEX_INTERSECTION;

Pierre Kraemer's avatar
Pierre Kraemer committed
516 517 518
	return EDGE_INTERSECTION;
}

untereiner's avatar
untereiner committed
519
}
Pierre Kraemer's avatar
Pierre Kraemer committed
520

untereiner's avatar
untereiner committed
521
}