static int All_Box_Intersections(OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack) { int Intersection_Found; int Side1, Side2; DBL Depth1, Depth2; VECTOR IPoint; Increase_Counter(stats[Ray_Box_Tests]); Intersection_Found = false; if (Intersect_Box(Ray, ((BOX *)Object)->Trans, ((BOX *)Object)->bounds[0], ((BOX *)Object)->bounds[1], &Depth1, &Depth2, &Side1, &Side2)) { if (Depth1 > DEPTH_TOLERANCE) { VEvaluateRay(IPoint, Ray->Initial, Depth1, Ray->Direction); if (Point_In_Clip(IPoint, Object->Clip)) { push_entry_i1(Depth1,IPoint,Object,Side1,Depth_Stack); Intersection_Found = true; } } VEvaluateRay(IPoint, Ray->Initial, Depth2, Ray->Direction); if (Point_In_Clip(IPoint, Object->Clip)) { push_entry_i1(Depth2,IPoint,Object,Side2,Depth_Stack); Intersection_Found = true; } } if (Intersection_Found) { Increase_Counter(stats[Ray_Box_Tests_Succeeded]); } return (Intersection_Found); }
static int All_IsoSurface_Intersections(OBJECT* Object, RAY* Ray, ISTACK* Depth_Stack) { ISOSURFACE * Isosrf = (ISOSURFACE *)Object; int Side1 = 0, Side2 = 0, itrace = 0, i_flg = 0; DBL Depth1 = 0.0, Depth2 = 0.0, len = 0.0; RAY New_Ray; VECTOR IPoint; VECTOR P, D; DBL tmax = 0.0, tmin = 0.0, tmp = 0.0; int i = 0 ; /* count of intervals in stack - 1 */ int IFound = false; int begin = 0, end = 0; bool in_shadow_test = false; VECTOR VTmp; Increase_Counter(stats[Ray_IsoSurface_Bound_Tests]); in_shadow_test = ((Ray->Optimisiation_Flags & OPTIMISE_SHADOW_TEST) == OPTIMISE_SHADOW_TEST); if(Isosrf->container_shape) { if(Isosrf->Trans != NULL) { MInvTransPoint(New_Ray.Initial, Ray->Initial, Isosrf->Trans); MInvTransDirection(New_Ray.Direction, Ray->Direction, Isosrf->Trans); VLength(len, New_Ray.Direction); VInverseScaleEq(New_Ray.Direction, len); i_flg = Intersect_Sphere(&New_Ray, Isosrf->container.sphere.center, (Isosrf->container.sphere.radius) * (Isosrf->container.sphere.radius), &Depth1, &Depth2); Depth1 = Depth1 / len; Depth2 = Depth2 / len; } else { i_flg = Intersect_Sphere(Ray, Isosrf->container.sphere.center, (Isosrf->container.sphere.radius) * (Isosrf->container.sphere.radius), &Depth1, &Depth2); } Decrease_Counter(stats[Ray_Sphere_Tests]); if(i_flg) Decrease_Counter(stats[Ray_Sphere_Tests_Succeeded]); } else { i_flg = Intersect_Box(Ray, Isosrf->Trans, Isosrf->container.box.corner1, Isosrf->container.box.corner2, &Depth1, &Depth2, &Side1, &Side2); } if(Depth1 < 0.0) Depth1 = 0.0; if(i_flg) /* IsoSurface_Bound_Tests */ { Increase_Counter(stats[Ray_IsoSurface_Bound_Tests_Succeeded]); if(Isosrf->Trans != NULL) { MInvTransPoint(P, Ray->Initial, Isosrf->Trans); MInvTransDirection(D, Ray->Direction, Isosrf->Trans); } else { Assign_Vector(P, Ray->Initial); Assign_Vector(D, Ray->Direction); } Isosrf->Inv3 = 1; if(Isosrf->closed != false) { VEvaluateRay(VTmp, P, Depth1, D); tmp = Vector_IsoSurface_Function(Isosrf, VTmp); if(Depth1 > Isosrf->accuracy) { if(tmp < 0.0) /* The ray hits the bounding shape */ { VEvaluateRay(IPoint, Ray->Initial, Depth1, Ray->Direction); if(Point_In_Clip(IPoint, Object->Clip)) { push_entry_i1(Depth1, IPoint, Object, Side1, Depth_Stack); IFound = true; itrace++; Isosrf->Inv3 *= -1; } } } else { if(tmp < (Isosrf->max_gradient * Isosrf->accuracy * 4.0)) { Depth1 = Isosrf->accuracy * 5.0; VEvaluateRay(VTmp, P, Depth1, D); if(Vector_IsoSurface_Function(Isosrf, VTmp) < 0) Isosrf->Inv3 = -1; /* Change the sign of the function (IPoint is in the bounding shpae.)*/ } VEvaluateRay(VTmp, P, Depth2, D); if(Vector_IsoSurface_Function(Isosrf, VTmp) < 0.0) { VEvaluateRay(IPoint, Ray->Initial, Depth2, Ray->Direction); if(Point_In_Clip(IPoint, Object->Clip)) { push_entry_i1(Depth2, IPoint, Object, Side2, Depth_Stack); IFound = true; } } } } /* METHOD 2 by R. Suzuki */ tmax = Depth2 = min(Depth2, BOUND_HUGE); tmin = Depth1 = min(Depth2, Depth1); if((tmax - tmin) < Isosrf->accuracy) return (false); Increase_Counter(stats[Ray_IsoSurface_Tests]); if((Depth1 < Isosrf->accuracy) && (Isosrf->Inv3 == 1)) { /* IPoint is on the isosurface */ VEvaluateRay(VTmp, P, tmin, D); if(fabs(Vector_IsoSurface_Function(Isosrf, VTmp)) < (Isosrf->max_gradient * Isosrf->accuracy * 4.0)) { tmin = Isosrf->accuracy * 5.0; VEvaluateRay(VTmp, P, tmin, D); if(Vector_IsoSurface_Function(Isosrf, VTmp) < 0) Isosrf->Inv3 = -1; /* change the sign and go into the isosurface */ } } for (; itrace < Isosrf->max_trace; itrace++) { if(IsoSurface_Function_Find_Root(Isosrf, P, D, &tmin, &tmax, in_shadow_test) == false) break; else { VEvaluateRay(IPoint, Ray->Initial, tmin, Ray->Direction); if(Point_In_Clip(IPoint, Object->Clip)) { push_entry_i1(tmin, IPoint, Object, 0 /*Side1*/, Depth_Stack); IFound = true; } } tmin += Isosrf->accuracy * 5.0; if((tmax - tmin) < Isosrf->accuracy) break; Isosrf->Inv3 *= -1; } if(IFound) Increase_Counter(stats[Ray_IsoSurface_Tests_Succeeded]); } return (IFound); }