src/scene.cc
branchpyrit
changeset 36 b490093b0ac3
parent 34 28f6e8b9d5d1
child 38 5d043eeb09d9
equal deleted inserted replaced
35:fb170fccb19f 36:b490093b0ac3
   327 	dist = t;
   327 	dist = t;
   328 	return true;
   328 	return true;
   329 #endif
   329 #endif
   330 }
   330 }
   331 
   331 
       
   332 bool Triangle::intersect_bbox(const BBox &bbox) const
       
   333 {
       
   334 	const Vector3 boxcenter = (bbox.L+bbox.H)*0.5;
       
   335 	const Vector3 boxhalfsize = (bbox.H-bbox.L)*0.5;
       
   336 	const Vector3 v0 = A->P - boxcenter;
       
   337 	const Vector3 v1 = B->P - boxcenter;
       
   338 	const Vector3 v2 = C->P - boxcenter;
       
   339 	const Vector3 e0 = v1-v0;
       
   340 	const Vector3 e1 = v2-v1;
       
   341 	const Vector3 e2 = v0-v2;
       
   342 
       
   343 	Float fex = fabsf(e0.x);
       
   344 	Float fey = fabsf(e0.y);
       
   345 	Float fez = fabsf(e0.z);
       
   346 
       
   347 	Float p0,p1,p2,min,max,rad;
       
   348 
       
   349 	p0 = e0.z*v0.y - e0.y*v0.z;
       
   350 	p2 = e0.z*v2.y - e0.y*v2.z;
       
   351 	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
       
   352 	rad = fez * boxhalfsize.y + fey * boxhalfsize.z;
       
   353 	if(min>rad || max<-rad) return false;
       
   354 
       
   355 	p0 = -e0.z*v0.x + e0.x*v0.z;
       
   356 	p2 = -e0.z*v2.x + e0.x*v2.z;
       
   357 	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
       
   358 	rad = fez * boxhalfsize.x + fex * boxhalfsize.z;
       
   359 	if(min>rad || max<-rad) return false;
       
   360 
       
   361 	p1 = e0.y*v1.x - e0.x*v1.y;
       
   362 	p2 = e0.y*v2.x - e0.x*v2.y;
       
   363 	if(p2<p1) {min=p2; max=p1;} else {min=p1; max=p2;}
       
   364 	rad = fey * boxhalfsize.x + fex * boxhalfsize.y;
       
   365 	if(min>rad || max<-rad) return false;
       
   366 
       
   367 	fex = fabsf(e1.x);
       
   368 	fey = fabsf(e1.y);
       
   369 	fez = fabsf(e1.z);
       
   370 
       
   371 	p0 = e1.z*v0.y - e1.y*v0.z;
       
   372 	p2 = e1.z*v2.y - e1.y*v2.z;
       
   373 	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
       
   374 	rad = fez * boxhalfsize.y + fey * boxhalfsize.z;
       
   375 	if(min>rad || max<-rad) return false;
       
   376 
       
   377 	p0 = -e1.z*v0.x + e1.x*v0.z;
       
   378 	p2 = -e1.z*v2.x + e1.x*v2.z;
       
   379 	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
       
   380 	rad = fez * boxhalfsize.x + fex * boxhalfsize.z;
       
   381 	if(min>rad || max<-rad) return false;
       
   382 
       
   383 	p0 = e1.y*v0.x - e1.x*v0.y;
       
   384 	p1 = e1.y*v1.x - e1.x*v1.y;
       
   385 	if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
       
   386 	rad = fey * boxhalfsize.x + fex * boxhalfsize.y;
       
   387 	if(min>rad || max<-rad) return false;
       
   388 
       
   389 	fex = fabsf(e2.x);
       
   390 	fey = fabsf(e2.y);
       
   391 	fez = fabsf(e2.z);
       
   392 
       
   393 	p0 = e2.z*v0.y - e2.y*v0.z;
       
   394 	p1 = e2.z*v1.y - e2.y*v1.z;
       
   395 	if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
       
   396 	rad = fez * boxhalfsize.y + fey * boxhalfsize.z;
       
   397 	if(min>rad || max<-rad) return false;
       
   398 
       
   399 	p0 = -e2.z*v0.x + e2.x*v0.z;
       
   400 	p1 = -e2.z*v1.x + e2.x*v1.z;
       
   401 	if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
       
   402 	rad = fez * boxhalfsize.x + fex * boxhalfsize.z;
       
   403 	if(min>rad || max<-rad) return false;
       
   404 
       
   405 	p1 = e2.y*v1.x - e2.x*v1.y;
       
   406 	p2 = e2.y*v2.x - e2.x*v2.y;
       
   407 	if(p2<p1) {min=p2; max=p1;} else {min=p1; max=p2;}
       
   408 	rad = fey * boxhalfsize.x + fex * boxhalfsize.y;
       
   409 	if(min>rad || max<-rad) return false;
       
   410 
       
   411 	/* test overlap in the {x,y,z}-directions */
       
   412 	/* test in X-direction */
       
   413 	min = v0.x;
       
   414 	if (v1.x < min) min = v1.x;
       
   415 	if (v2.x < min) min = v2.x;
       
   416 	max = v0.x;
       
   417 	if (v1.x > max) max = v1.x;
       
   418 	if (v2.x > max) max = v2.x;
       
   419 	if(min>boxhalfsize.x || max<-boxhalfsize.x) return false;
       
   420 
       
   421 	/* test in Y-direction */
       
   422 	min = v0.y;
       
   423 	if (v1.y < min) min = v1.y;
       
   424 	if (v2.y < min) min = v2.y;
       
   425 	max = v0.y;
       
   426 	if (v1.y > max) max = v1.y;
       
   427 	if (v2.y > max) max = v2.y;
       
   428 	if(min>boxhalfsize.y || max<-boxhalfsize.y) return false;
       
   429 
       
   430 	/* test in Z-direction */
       
   431 	min = v0.z;
       
   432 	if (v1.z < min) min = v1.z;
       
   433 	if (v2.z < min) min = v2.z;
       
   434 	max = v0.z;
       
   435 	if (v1.z > max) max = v1.z;
       
   436 	if (v2.z > max) max = v2.z;
       
   437 	if(min>boxhalfsize.z || max<-boxhalfsize.z) return false;
       
   438 
       
   439 	/*  test if the box intersects the plane of the triangle */
       
   440 	Vector3 vmin,vmax;
       
   441 	Float v;
       
   442 	for(int q=0;q<3;q++)
       
   443 	{
       
   444 		v=v0[q];
       
   445 		if(N[q]>0.0f)
       
   446 		{
       
   447 			vmin.cell[q]=-boxhalfsize[q] - v;
       
   448 			vmax.cell[q]= boxhalfsize[q] - v;
       
   449 		}
       
   450 		else
       
   451 		{
       
   452 			vmin.cell[q]= boxhalfsize[q] - v;
       
   453 			vmax.cell[q]=-boxhalfsize[q] - v;
       
   454 		}
       
   455 	}
       
   456 	if(dot(N,vmin)>0.0f) return false;
       
   457 	if(dot(N,vmax)>=0.0f) return true;
       
   458 
       
   459 	return false;
       
   460 }
       
   461 
   332 BBox Triangle::get_bbox() const
   462 BBox Triangle::get_bbox() const
   333 {
   463 {
   334 	BBox bbox = BBox();
   464 	BBox bbox = BBox();
   335 	bbox.L = A->P;
   465 	bbox.L = A->P;
   336 	if (B->P.x < bbox.L.x)  bbox.L.x = B->P.x;
   466 	if (B->P.x < bbox.L.x)  bbox.L.x = B->P.x;