static int intersect_poylgon(RAY *Ray, POLYGON *Polyg, DBL *Depth) { DBL x, y, len; VECTOR p, d; /* Don't test degenerate polygons. */ if (Test_Flag(Polyg, DEGENERATE_FLAG)) { return(false); } Increase_Counter(stats[Ray_Polygon_Tests]); /* Transform the ray into the polygon space. */ MInvTransPoint(p, Ray->Initial, Polyg->Trans); MInvTransDirection(d, Ray->Direction, Polyg->Trans); VLength(len, d); VInverseScaleEq(d, len); /* Intersect ray with the plane in which the polygon lies. */ if (fabs(d[Z]) < ZERO_TOLERANCE) { return(false); } *Depth = -p[Z] / d[Z]; if ((*Depth < DEPTH_TOLERANCE) || (*Depth > Max_Distance)) { return(false); } /* Does the intersection point lie inside the polygon? */ x = p[X] + *Depth * d[X]; y = p[Y] + *Depth * d[Y]; if (in_polygon(Polyg->Data->Number, Polyg->Data->Points, x, y)) { Increase_Counter(stats[Ray_Polygon_Tests_Succeeded]); *Depth /= len; return (true); } else { return (false); } }
bool Polygon::Intersect(const BasicRay& ray, DBL *Depth, TraceThreadData *Thread) const { DBL x, y, len; Vector3d p, d; /* Don't test degenerate polygons. */ if (Test_Flag(this, DEGENERATE_FLAG)) return(false); Thread->Stats()[Ray_Polygon_Tests]++; /* Transform the ray into the polygon space. */ MInvTransPoint(p, ray.Origin, Trans); MInvTransDirection(d, ray.Direction, Trans); len = d.length(); d /= len; /* Intersect ray with the plane in which the polygon lies. */ if (fabs(d[Z]) < ZERO_TOLERANCE) return(false); *Depth = -p[Z] / d[Z]; if ((*Depth < DEPTH_TOLERANCE) || (*Depth > MAX_DISTANCE)) return(false); /* Does the intersection point lie inside the polygon? */ x = p[X] + *Depth * d[X]; y = p[Y] + *Depth * d[Y]; if (in_polygon(Data->Number, Data->Points, x, y)) { Thread->Stats()[Ray_Polygon_Tests_Succeeded]++; *Depth /= len; return (true); } else return (false); }
static int Intersect_Plane (RAY *Ray, PLANE *Plane, DBL *Depth) { DBL NormalDotOrigin, NormalDotDirection; VECTOR P, D; Increase_Counter(stats[Ray_Plane_Tests]); if (Plane->Trans == NULL) { VDot(NormalDotDirection, Plane->Normal_Vector, Ray->Direction); if (fabs(NormalDotDirection) < EPSILON) { return(false); } VDot(NormalDotOrigin, Plane->Normal_Vector, Ray->Initial); } else { MInvTransPoint(P, Ray->Initial, Plane->Trans); MInvTransDirection(D, Ray->Direction, Plane->Trans); VDot(NormalDotDirection, Plane->Normal_Vector, D); if (fabs(NormalDotDirection) < EPSILON) { return(false); } VDot(NormalDotOrigin, Plane->Normal_Vector, P); } *Depth = -(NormalDotOrigin + Plane->Distance) / NormalDotDirection; if ((*Depth >= DEPTH_TOLERANCE) && (*Depth <= Max_Distance)) { Increase_Counter(stats[Ray_Plane_Tests_Succeeded]); return (true); } else { return (false); } }
bool Plane::Intersect(const Ray& ray, DBL *Depth, TraceThreadData *Thread) const { DBL NormalDotOrigin, NormalDotDirection; VECTOR P, D; Thread->Stats()[Ray_Plane_Tests]++; if (Trans == NULL) { VDot(NormalDotDirection, Normal_Vector, ray.Direction); if (fabs(NormalDotDirection) < EPSILON) { return(false); } VDot(NormalDotOrigin, Normal_Vector, ray.Origin); } else { MInvTransPoint(P, ray.Origin, Trans); MInvTransDirection(D, ray.Direction, Trans); VDot(NormalDotDirection, Normal_Vector, D); if (fabs(NormalDotDirection) < EPSILON) { return(false); } VDot(NormalDotOrigin, Normal_Vector, P); } *Depth = -(NormalDotOrigin + Distance) / NormalDotDirection; if ((*Depth >= DEPTH_TOLERANCE) && (*Depth <= MAX_DISTANCE)) { Thread->Stats()[Ray_Plane_Tests_Succeeded]++; return (true); } else { return (false); } }
static int Intersect_Disc (RAY *Ray, DISC *disc, DBL *Depth) { DBL t, u, v, r2, len; VECTOR P, D; Increase_Counter(stats[Ray_Disc_Tests]); /* Transform the point into the discs space */ MInvTransPoint(P, Ray->Initial, disc->Trans); MInvTransDirection(D, Ray->Direction, disc->Trans); VLength(len, D); VInverseScaleEq(D, len); if (fabs(D[Z]) > EPSILON) { t = -P[Z] / D[Z]; if (t >= 0.0) { u = P[X] + t * D[X]; v = P[Y] + t * D[Y]; r2 = Sqr(u) + Sqr(v); if ((r2 >= disc->iradius2) && (r2 <= disc->oradius2)) { *Depth = t / len; if ((*Depth > Small_Tolerance) && (*Depth < Max_Distance)) { Increase_Counter(stats[Ray_Disc_Tests_Succeeded]); return (true); } } } } return (false); }
bool Lemon::All_Intersections(const Ray& ray, IStack& Depth_Stack, TraceThreadData *Thread) { bool Intersection_Found; int cnt, i; Vector3d Real_Normal; Vector3d Real_Pt,INormal; LEMON_INT I[4]; Vector3d P,D; DBL len; Thread->Stats()[Ray_Lemon_Tests]++; MInvTransPoint(P, ray.Origin, Trans); MInvTransDirection(D, ray.Direction, Trans); len = D.length(); D /= len; Intersection_Found = false; if ((cnt = Intersect(P, D, I, Thread)) != 0) { for (i = 0; i < cnt; i++) { Real_Pt = ray.Origin + I[i].d/len * ray.Direction; if (Clip.empty() || Point_In_Clip(Real_Pt, Clip, Thread)) { INormal = I[i].n; MTransNormal(Real_Normal, INormal, Trans); Real_Normal.normalize(); Depth_Stack->push(Intersection(I[i].d/len,Real_Pt,Real_Normal,this)); Intersection_Found = true; } } } if(Intersection_Found) { Thread->Stats()[Ray_Lemon_Tests_Succeeded]++; } return (Intersection_Found); }
bool Disc::Intersect(const BasicRay& ray, DBL *Depth) const { DBL t, u, v, r2, len; Vector3d P, D; /* Transform the point into the discs space */ MInvTransPoint(P, ray.Origin, Trans); MInvTransDirection(D, ray.Direction, Trans); len = D.length(); D /= len; if (fabs(D[Z]) > EPSILON) { t = -P[Z] / D[Z]; if (t >= 0.0) { u = P[X] + t * D[X]; v = P[Y] + t * D[Y]; r2 = Sqr(u) + Sqr(v); if ((r2 >= iradius2) && (r2 <= oradius2)) { *Depth = t / len; if ((*Depth > DEPTH_TOLERANCE) && (*Depth < MAX_DISTANCE)) return (true); } } } return (false); }
bool IsoSurface::All_Intersections(const Ray& ray, IStack& Depth_Stack, TraceThreadData *Thread) { int Side1 = 0, Side2 = 0, itrace = 0; DBL Depth1 = 0.0, Depth2 = 0.0; BasicRay New_Ray; Vector3d IPoint; Vector3d Plocal, Dlocal; DBL tmax = 0.0, tmin = 0.0, tmp = 0.0; DBL maxg = max_gradient; int i = 0 ; /* count of intervals in stack - 1 */ int IFound = false; int begin = 0, end = 0; bool in_shadow_test = false; Vector3d VTmp; Thread->Stats()[Ray_IsoSurface_Bound_Tests]++; if(container->Intersect(ray, Trans, Depth1, Depth2, Side1, Side2)) /* IsoSurface_Bound_Tests */ { Thread->Stats()[Ray_IsoSurface_Bound_Tests_Succeeded]++; in_shadow_test = ray.IsShadowTestRay(); if(Depth1 < 0.0) Depth1 = 0.0; if(Trans != NULL) { MInvTransPoint(Plocal, ray.Origin, Trans); MInvTransDirection(Dlocal, ray.Direction, Trans); } else { Plocal = ray.Origin; Dlocal = ray.Direction; } Thread->isosurfaceData->Inv3 = 1; if(closed != false) { VTmp = Plocal + Depth1 * Dlocal; tmp = Vector_Function(Thread->functionContext, VTmp); if(Depth1 > accuracy) { if(tmp < 0.0) /* The ray hits the bounding shape */ { IPoint = ray.Evaluate(Depth1); if(Clip.empty() || Point_In_Clip(IPoint, Clip, Thread)) { Depth_Stack->push(Intersection(Depth1, IPoint, this, 1, Side1)); IFound = true; itrace++; Thread->isosurfaceData->Inv3 *= -1; } } } else { if(tmp < (maxg * accuracy * 4.0)) { Depth1 = accuracy * 5.0; VTmp = Plocal + Depth1 * Dlocal; if(Vector_Function(Thread->functionContext, VTmp) < 0) Thread->isosurfaceData->Inv3 = -1; /* Change the sign of the function (IPoint is in the bounding shpae.)*/ } VTmp = Plocal + Depth2 * Dlocal; if(Vector_Function(Thread->functionContext, VTmp) < 0.0) { IPoint = ray.Evaluate(Depth2); if(Clip.empty() || Point_In_Clip(IPoint, Clip, Thread)) { Depth_Stack->push(Intersection(Depth2, IPoint, this, 1, Side2)); IFound = true; } } } } /* METHOD 2 by R. Suzuki */ tmax = Depth2 = min(Depth2, BOUND_HUGE); tmin = Depth1 = min(Depth2, Depth1); if((tmax - tmin) < accuracy) { if (IFound) Depth_Stack->pop(); // we added an intersection already, so we need to undo that return (false); } Thread->Stats()[Ray_IsoSurface_Tests]++; if((Depth1 < accuracy) && (Thread->isosurfaceData->Inv3 == 1)) { /* IPoint is on the isosurface */ VTmp = Plocal + tmin * Dlocal; if(fabs(Vector_Function(Thread->functionContext, VTmp)) < (maxg * accuracy * 4.0)) { tmin = accuracy * 5.0; VTmp = Plocal + tmin * Dlocal; if(Vector_Function(Thread->functionContext, VTmp) < 0) Thread->isosurfaceData->Inv3 = -1; /* change the sign and go into the isosurface */ } } Thread->isosurfaceData->ctx = Thread->functionContext; for (; itrace < max_trace; itrace++) { if(Function_Find_Root(*(Thread->isosurfaceData), Plocal, Dlocal, &tmin, &tmax, maxg, in_shadow_test, Thread) == false) break; else { IPoint = ray.Evaluate(tmin); if(Clip.empty() || Point_In_Clip(IPoint, Clip, Thread)) { Depth_Stack->push(Intersection(tmin, IPoint, this, 0, 0 /*Side1*/)); IFound = true; } } tmin += accuracy * 5.0; if((tmax - tmin) < accuracy) break; Thread->isosurfaceData->Inv3 *= -1; } if(IFound) Thread->Stats()[Ray_IsoSurface_Tests_Succeeded]++; } if(eval == true) { DBL temp_max_gradient = max_gradient; // TODO FIXME - works around nasty gcc (found using 4.0.1) bug failing to honor casting away of volatile on pass by value on template argument lookup [trf] max_gradient = max((DBL)temp_max_gradient, maxg); // TODO FIXME - This is not thread-safe but should be!!! [trf] } return (IFound); }
int Cone::Intersect(const BasicRay& ray, CONE_INT *Intersection, TraceThreadData *Thread) const { int i = 0; DBL a, b, c, z, t1, t2, len; DBL d; Vector3d P, D; Thread->Stats()[Ray_Cone_Tests]++; /* Transform the ray into the cones space */ MInvTransPoint(P, ray.Origin, Trans); MInvTransDirection(D, ray.Direction, Trans); len = D.length(); D /= len; if (Test_Flag(this, CYLINDER_FLAG)) { /* Solve intersections with a cylinder */ a = D[X] * D[X] + D[Y] * D[Y]; if (a > EPSILON) { b = P[X] * D[X] + P[Y] * D[Y]; c = P[X] * P[X] + P[Y] * P[Y] - 1.0; d = b * b - a * c; if (d >= 0.0) { d = sqrt(d); t1 = (-b + d) / a; t2 = (-b - d) / a; z = P[Z] + t1 * D[Z]; if ((t1 > Cone_Tolerance) && (t1 < MAX_DISTANCE) && (z >= 0.0) && (z <= 1.0)) { Intersection[i].d = t1 / len; Intersection[i++].t = SIDE_HIT; } z = P[Z] + t2 * D[Z]; if ((t2 > Cone_Tolerance) && (t2 < MAX_DISTANCE) && (z >= 0.0) && (z <= 1.0)) { Intersection[i].d = t2 / len; Intersection[i++].t = SIDE_HIT; } } } } else { /* Solve intersections with a cone */ a = D[X] * D[X] + D[Y] * D[Y] - D[Z] * D[Z]; b = D[X] * P[X] + D[Y] * P[Y] - D[Z] * P[Z]; c = P[X] * P[X] + P[Y] * P[Y] - P[Z] * P[Z]; if (fabs(a) < EPSILON) { if (fabs(b) > EPSILON) { /* One intersection */ t1 = -0.5 * c / b; z = P[Z] + t1 * D[Z]; if ((t1 > Cone_Tolerance) && (t1 < MAX_DISTANCE) && (z >= dist) && (z <= 1.0)) { Intersection[i].d = t1 / len; Intersection[i++].t = SIDE_HIT; } } } else { /* Check hits against the side of the cone */ d = b * b - a * c; if (d >= 0.0) { d = sqrt(d); t1 = (-b - d) / a; t2 = (-b + d) / a; z = P[Z] + t1 * D[Z]; if ((t1 > Cone_Tolerance) && (t1 < MAX_DISTANCE) && (z >= dist) && (z <= 1.0)) { Intersection[i].d = t1 / len; Intersection[i++].t = SIDE_HIT; } z = P[Z] + t2 * D[Z]; if ((t2 > Cone_Tolerance) && (t2 < MAX_DISTANCE) && (z >= dist) && (z <= 1.0)) { Intersection[i].d = t2 / len; Intersection[i++].t = SIDE_HIT; } } } } if (Test_Flag(this, CLOSED_FLAG) && (fabs(D[Z]) > EPSILON)) { d = (1.0 - P[Z]) / D[Z]; a = (P[X] + d * D[X]); b = (P[Y] + d * D[Y]); if (((Sqr(a) + Sqr(b)) <= 1.0) && (d > Cone_Tolerance) && (d < MAX_DISTANCE)) { Intersection[i].d = d / len; Intersection[i++].t = CAP_HIT; } d = (dist - P[Z]) / D[Z]; a = (P[X] + d * D[X]); b = (P[Y] + d * D[Y]); if ((Sqr(a) + Sqr(b)) <= (Test_Flag(this, CYLINDER_FLAG) ? 1.0 : Sqr(dist)) && (d > Cone_Tolerance) && (d < MAX_DISTANCE)) { Intersection[i].d = d / len; Intersection[i++].t = BASE_HIT; } } if (i) Thread->Stats()[Ray_Cone_Tests_Succeeded]++; return (i); }
int All_Parametric_Intersections(OBJECT* Object, RAY* Ray, ISTACK* Depth_Stack) { PARAMETRIC * Par = (PARAMETRIC *)Object; PRECOMP_PAR_DATA * PData = ((PARAMETRIC *)Object)->PData; VECTOR P, D, IPoint; UV_VECT low_vect, hi_vect; RAY New_Ray; DBL XRayMin, XRayMax, YRayMin, YRayMax, ZRayMin, ZRayMax, TPotRes, TLen; DBL Depth1, Depth2, temp, Len, UResult, VResult, TResult = HUGE_VAL; DBL low, hi, len; int MaxPrecompX, MaxPrecompY, MaxPrecompZ; int split, i = 0, Side1, Side2; int parX, parY; int i_flg; Increase_Counter(stats[Ray_Par_Bound_Tests]); if(Par->container_shape) { if(Par->Trans != NULL) { MInvTransPoint(New_Ray.Initial, Ray->Initial, Par->Trans); MInvTransDirection(New_Ray.Direction, Ray->Direction, Par->Trans); VLength(len, New_Ray.Direction); VInverseScaleEq(New_Ray.Direction, len); i_flg = Intersect_Sphere(&New_Ray, Par->container.sphere.center, (Par->container.sphere.radius) * (Par->container.sphere.radius), &Depth1, &Depth2); Depth1 = Depth1 / len; Depth2 = Depth2 / len; } else { i_flg = Intersect_Sphere(Ray, Par->container.sphere.center, (Par->container.sphere.radius) * (Par->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, Par->Trans, Par->container.box.corner1, Par->container.box.corner2, &Depth1, &Depth2, &Side1, &Side2); } if(!i_flg) return false; Increase_Counter(stats[Ray_Par_Bound_Tests_Succeeded]); Increase_Counter(stats[Ray_Parametric_Tests]); if (Par->Trans != NULL) { MInvTransPoint(P, Ray->Initial, Par->Trans); MInvTransDirection(D, Ray->Direction, Par->Trans); } else { P[X] = Ray->Initial[X]; P[Y] = Ray->Initial[Y]; P[Z] = Ray->Initial[Z]; D[X] = Ray->Direction[X]; D[Y] = Ray->Direction[Y]; D[Z] = Ray->Direction[Z]; } if (Depth1 == Depth2) Depth1 = 0; if ((Depth1 += 4 * Par->accuracy) > Depth2) return false; Intervals_Low[INDEX_U][0] = Par->umin; Intervals_Hi[INDEX_U][0] = Par->umax; Intervals_Low[INDEX_V][0] = Par->vmin; Intervals_Hi[INDEX_V][0] = Par->vmax; /* Fri 09-27-1996 0. */ SectorNum[0] = 1; MaxPrecompX = MaxPrecompY = MaxPrecompZ = 0; if (PData != NULL) { if (((PData->flags) & OK_X) != 0) MaxPrecompX = 1 << (PData->depth); if (((PData->flags) & OK_Y) != 0) MaxPrecompY = 1 << (PData->depth); if (((PData->flags) & OK_Z) != 0) MaxPrecompZ = 1 << (PData->depth); } /* 0 */ while (i >= 0) { low_vect[U] = Intervals_Low[INDEX_U][i]; hi_vect[U] = Intervals_Hi[INDEX_U][i]; Len = hi_vect[U] - low_vect[U]; split = INDEX_U; low_vect[V] = Intervals_Low[INDEX_V][i]; hi_vect[V] = Intervals_Hi[INDEX_V][i]; temp = hi_vect[V] - low_vect[V]; if (temp > Len) { Len = temp; split = INDEX_V; } parX = parY = 0; TLen = 0; /* X */ if (SectorNum[i] < MaxPrecompX) { low = PData->Low[0][SectorNum[i]]; hi = PData->Hi[0][SectorNum[i]]; } else Evaluate_Function_Interval_UV(*(Par->Function[0]), Par->accuracy, low_vect, hi_vect, Par->max_gradient, low, hi); /* fabs(D[X] *(T2-T1)) is not OK with new method */ if (close(D[0], 0)) { parX = 1; if ((hi < P[0]) || (low > P[0])) { i--; continue; } } else { XRayMin = (hi - P[0]) / D[0]; XRayMax = (low - P[0]) / D[0]; if (XRayMin > XRayMax) { temp = XRayMin; XRayMin = XRayMax; XRayMax = temp; } if ((XRayMin > Depth2) || (XRayMax < Depth1)) { i--; continue; } if ((TPotRes = XRayMin) > TResult) { i--; continue; } TLen = XRayMax - XRayMin; } /* Y */ if (SectorNum[i] < MaxPrecompY) { low = PData->Low[1][SectorNum[i]]; hi = PData->Hi[1][SectorNum[i]]; } else Evaluate_Function_Interval_UV(*(Par->Function[1]), Par->accuracy, low_vect, hi_vect, Par->max_gradient, low, hi); if (close(D[1], 0)) { parY = 1; if ((hi < P[1]) || (low > P[1])) { i--; continue; } } else { YRayMin = (hi - P[1]) / D[1]; YRayMax = (low - P[1]) / D[1]; if (YRayMin > YRayMax) { temp = YRayMin; YRayMin = YRayMax; YRayMax = temp; } if ((YRayMin > Depth2) || (YRayMax < Depth1)) { i--; continue; } if ((TPotRes = YRayMin) > TResult) { i--; continue; } if (parX == 0) { if ((YRayMin > XRayMax) || (YRayMax < XRayMin)) { i--; continue; } } if ((temp = YRayMax - YRayMin) > TLen) TLen = temp; } /* Z */ if ((SectorNum[i] < MaxPrecompZ) && (0 < SectorNum[i])) { low = PData->Low[2][SectorNum[i]]; hi = PData->Hi[2][SectorNum[i]]; } else Evaluate_Function_Interval_UV(*(Par->Function[2]), Par->accuracy, low_vect, hi_vect, Par->max_gradient, low, hi); if (close(D[2], 0)) { if ((hi < P[2]) || (low > P[2])) { i--; continue; } } else { ZRayMin = (hi - P[2]) / D[2]; ZRayMax = (low - P[2]) / D[2]; if (ZRayMin > ZRayMax) { temp = ZRayMin; ZRayMin = ZRayMax; ZRayMax = temp; } if ((ZRayMin > Depth2) || (ZRayMax < Depth1)) { i--; continue; } if ((TPotRes = ZRayMin) > TResult) { i--; continue; } if (parX == 0) { if ((ZRayMin > XRayMax) || (ZRayMax < XRayMin)) { i--; continue; } } if (parY == 0) { if ((ZRayMin > YRayMax) || (ZRayMax < YRayMin)) { i--; continue; } } if ((temp = ZRayMax - ZRayMin) > TLen) TLen = temp; } if (Len > TLen) Len = TLen; if (Len < Par->accuracy) { if ((TResult > TPotRes) && (TPotRes > Depth1)) { TResult = TPotRes; Par->last_u = UResult = low_vect[U]; Par->last_v = VResult = low_vect[V]; } i--; } else { /* 1 copy */ if ((SectorNum[i] *= 2) >= Max_intNumber) SectorNum[i] = Max_intNumber; SectorNum[i + 1] = SectorNum[i]; SectorNum[i]++; i++; Intervals_Low[INDEX_U][i] = low_vect[U]; Intervals_Hi[INDEX_U][i] = hi_vect[U]; Intervals_Low[INDEX_V][i] = low_vect[V]; Intervals_Hi[INDEX_V][i] = hi_vect[V]; /* 2 split */ temp = (Intervals_Hi[split][i] + Intervals_Low[split][i]) / 2.0; Intervals_Hi[split][i] = temp; Intervals_Low[split][i - 1] = temp; } } if (TResult < Depth2) { Increase_Counter(stats[Ray_Parametric_Tests_Succeeded]); VScale(IPoint, Ray->Direction, TResult); VAddEq(IPoint, Ray->Initial); if (Point_In_Clip(IPoint, Par->Clip)) { /* compute_param_normal( Par, UResult, VResult , &N); push_normal_entry( TResult ,IPoint, N, (OBJECT *) Object, Depth_Stack); */ // UV_VECT uv; // Make_UV_Vector(uv, UResult, VResult); // push_uv_entry(TResult, IPoint, uv, (OBJECT *)Object, Depth_Stack); push_entry(TResult, IPoint, (OBJECT *)Object, Depth_Stack); return true; } } return false; }
bool Ovus::All_Intersections(const Ray& ray, IStack& Depth_Stack, TraceThreadData *Thread) { bool Found = false; Vector3d Real_Normal, Real_Pt, INormal, IPoint; DBL Depth1, Depth2, Depth3, Depth4, Depth5, Depth6; DBL len, horizontal; Vector3d P,D; Thread->Stats()[Ray_Ovus_Tests]++; MInvTransPoint(P, ray.Origin, Trans); MInvTransDirection(D, ray.Direction, Trans); len = D.length(); D /= len; Intersect_Ovus_Spheres(P, D, &Depth1, &Depth2, &Depth3, &Depth4, &Depth5, &Depth6, Thread); if (Depth1 > EPSILON) { IPoint = P + Depth1 * D; if (IPoint[Y] < BottomVertical) { MTransPoint(Real_Pt, IPoint, Trans); if (Clip.empty()||(Point_In_Clip(Real_Pt, Clip, Thread))) { INormal = IPoint / BottomRadius; MTransNormal(Real_Normal, INormal, Trans); Real_Normal.normalize(); Depth_Stack->push(Intersection(Depth1/len, Real_Pt, Real_Normal, this)); Found = true; } } } if (Depth2 > EPSILON) { IPoint = P + Depth2 * D; if (IPoint[Y] < BottomVertical) { MTransPoint(Real_Pt, IPoint, Trans); if (Clip.empty()||(Point_In_Clip(Real_Pt, Clip, Thread))) { INormal = IPoint / BottomRadius; MTransNormal(Real_Normal, INormal, Trans); Real_Normal.normalize(); Depth_Stack->push(Intersection(Depth2/len, Real_Pt, Real_Normal, this)); Found = true; } } } if (Depth3 > EPSILON) { IPoint = P + Depth3 * D; if (IPoint[Y] > TopVertical) { MTransPoint(Real_Pt, IPoint, Trans); if (Clip.empty()||(Point_In_Clip(Real_Pt, Clip, Thread))) { INormal = IPoint; INormal[Y] -= BottomRadius; INormal /= TopRadius; MTransNormal(Real_Normal, INormal, Trans); Real_Normal.normalize(); Depth_Stack->push(Intersection(Depth3/len, Real_Pt, Real_Normal, this)); Found = true; } } } if (Depth4 > EPSILON) { IPoint = P + Depth4 * D; if (IPoint[Y] > TopVertical) { MTransPoint(Real_Pt, IPoint, Trans); if (Clip.empty()||(Point_In_Clip(Real_Pt, Clip, Thread))) { INormal = IPoint; INormal[Y] -= BottomRadius; INormal /= TopRadius; MTransNormal(Real_Normal, INormal, Trans); Real_Normal.normalize(); Depth_Stack->push(Intersection(Depth4/len, Real_Pt, Real_Normal, this)); Found = true; } } } if (Depth5 > EPSILON) { IPoint = P + Depth5 * D; MTransPoint(Real_Pt, IPoint, Trans); if (Clip.empty()||(Point_In_Clip(Real_Pt, Clip, Thread))) { INormal = IPoint; INormal[Y] -= VerticalPosition; horizontal = sqrt(Sqr(INormal[X]) + Sqr(INormal[Z])); INormal[X] += (INormal[X] * HorizontalPosition / horizontal); INormal[Z] += (INormal[Z] * HorizontalPosition / horizontal); INormal.normalize(); MTransNormal(Real_Normal, INormal, Trans); Real_Normal.normalize(); Depth_Stack->push(Intersection(Depth5/len, Real_Pt, Real_Normal, this)); Found = true; } } if (Depth6 > EPSILON) { IPoint = P + Depth6 * D; MTransPoint(Real_Pt, IPoint, Trans); if (Clip.empty()||(Point_In_Clip(Real_Pt, Clip, Thread))) { INormal = IPoint; INormal[Y] -= VerticalPosition; horizontal = sqrt(Sqr(INormal[X]) + Sqr(INormal[Z])); INormal[X] += (INormal[X] * HorizontalPosition / horizontal); INormal[Z] += (INormal[Z] * HorizontalPosition / horizontal); INormal.normalize(); MTransNormal(Real_Normal, INormal, Trans); Real_Normal.normalize(); Depth_Stack->push(Intersection(Depth6/len, Real_Pt, Real_Normal, this)); Found = true; } } if (Found) { Thread->Stats()[Ray_Ovus_Tests_Succeeded]++; } return (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); }
bool Lathe::Intersect(const BasicRay& ray, IStack& Depth_Stack, TraceThreadData *Thread) { int cnt; int found, j, n1, n2; DBL k, len, r, m, w, Dy2, r0, Dylen; DBL x1[7]; DBL x2[3]; DBL y1[6]; DBL y2[2]; DBL best; Vector3d P, D; LATHE_SPLINE_ENTRY *Entry; // Transform the ray into the lathe space. MInvTransPoint(P, ray.Origin, Trans); MInvTransDirection(D, ray.Direction, Trans); len = D.length(); D /= len; Dylen = D[Y] * len; // TODO FIXME - why don't we do this *before* normalizing, saving us the multiplication by len? #ifdef LATHE_EXTRA_STATS Thread->Stats()[Lathe_Bound_Tests]++; #endif // Test if ray misses lathe's cylindrical bound. if(((D[Y] >= 0.0) && (P[Y] > Height2)) || ((D[Y] <= 0.0) && (P[Y] < Height1)) || ((D[X] >= 0.0) && (P[X] > Radius2)) || ((D[X] <= 0.0) && (P[X] < -Radius2))) return false; // Get distance r0 of the ray from rotation axis (i.e. y axis). r0 = fabs(P[X] * D[Z] - P[Z] * D[X]); r = D[X] * D[X] + D[Z] * D[Z]; if(r > 0.0) r0 /= sqrt(r); // Test if ray misses lathe's cylindrical bound. if(r0 > Radius2) return false; // Intersect all cylindrical bounds. BCYL_INT *intervals = reinterpret_cast<BCYL_INT *>(Thread->BCyl_Intervals) ; BCYL_INT *rint = reinterpret_cast<BCYL_INT *>(Thread->BCyl_RInt) ; BCYL_INT *hint = reinterpret_cast<BCYL_INT *>(Thread->BCyl_HInt) ; if((cnt = Intersect_BCyl(Spline->BCyl, intervals, rint, hint, P, D)) == 0) return false; #ifdef LATHE_EXTRA_STATS Thread->Stats()[Lathe_Bound_Tests_Succeeded]++; #endif // Precalculate some constants that are ray-dependant only. m = D[X] * P[X] + D[Z] * P[Z]; Dy2 = D[Y] * D[Y]; // Step through the list of intersections. found = false; best = BOUND_HUGE; for(j = 0; j < cnt; j++) { // Get current segment. Entry = &Spline->Entry[intervals[j].n]; // If we already have the best intersection we may exit. if(!(Type & IS_CHILD_OBJECT) && (intervals[j].d[0] > best)) break; // Init number of roots found. n1 = 0; // Intersect segment. switch(Spline_Type) { // Linear spline case LINEAR_SPLINE: // Solve 2th order polynomial. x1[0] = Entry->C[Y] * Entry->C[Y] * r - Entry->C[X] * Entry->C[X] * Dy2; x1[1] = 2.0 * (Entry->C[Y] * ((Entry->D[Y] - P[Y]) * r + D[Y] * m) - Entry->C[X] * Entry->D[X] * Dy2); x1[2] = (Entry->D[Y] - P[Y]) * ((Entry->D[Y] - P[Y]) * r + 2.0 * D[Y] * m) + Dy2 * (P[X] * P[X] + P[Z] * P[Z] - Entry->D[X] * Entry->D[X]); n1 = Solve_Polynomial(2, x1, y1, false, 0.0, Thread); break; // Quadratic spline case QUADRATIC_SPLINE: // Solve 4th order polynomial. x1[0] = Entry->B[Y] * Entry->B[Y] * r - Entry->B[X] * Entry->B[X] * Dy2; x1[1] = 2.0 * (Entry->B[Y] * Entry->C[Y] * r - Entry->B[X] * Entry->C[X] * Dy2); x1[2] = r * (2.0 * Entry->B[Y] * (Entry->D[Y] - P[Y]) + Entry->C[Y] * Entry->C[Y]) + 2.0 * Entry->B[Y] * D[Y] * m - (2.0 * Entry->B[X] * Entry->D[X] + Entry->C[X] * Entry->C[X]) * Dy2; x1[3] = 2.0 * (Entry->C[Y] * ((Entry->D[Y] - P[Y]) * r + D[Y] * m) - Entry->C[X] * Entry->D[X] * Dy2); x1[4] = (Entry->D[Y] - P[Y]) * ((Entry->D[Y] - P[Y]) * r + 2.0 * D[Y] * m) + Dy2 * (P[X] * P[X] + P[Z] * P[Z] - Entry->D[X] * Entry->D[X]); n1 = Solve_Polynomial(4, x1, y1, Test_Flag(this, STURM_FLAG), 0.0, Thread); break; // Cubic spline case BEZIER_SPLINE: case CUBIC_SPLINE: // Solve 6th order polynomial. x1[0] = Entry->A[Y] * Entry->A[Y] * r - Entry->A[X] * Entry->A[X] * Dy2; x1[1] = 2.0 * (Entry->A[Y] * Entry->B[Y] * r - Entry->A[X] * Entry->B[X] * Dy2); x1[2] = (2.0 * Entry->A[Y] * Entry->C[Y] + Entry->B[Y] * Entry->B[Y]) * r - (2.0 * Entry->A[X] * Entry->C[X] + Entry->B[X] * Entry->B[X]) * Dy2; x1[3] = 2.0 * ((Entry->A[Y] * Entry->D[Y] + Entry->B[Y] * Entry->C[Y] - Entry->A[Y] * P[Y]) * r + Entry->A[Y] * D[Y] * m - (Entry->A[X] * Entry->D[X] + Entry->B[X] * Entry->C[X]) * Dy2); x1[4] = (2.0 * Entry->B[Y] * (Entry->D[Y] - P[Y]) + Entry->C[Y] * Entry->C[Y]) * r + 2.0 * Entry->B[Y] * D[Y] * m - (2.0 * Entry->B[X] * Entry->D[X] + Entry->C[X] * Entry->C[X]) * Dy2; x1[5] = 2.0 * (Entry->C[Y] * ((Entry->D[Y] - P[Y]) * r + D[Y] * m) - Entry->C[X] * Entry->D[X] * Dy2); x1[6] = (Entry->D[Y] - P[Y]) * ((Entry->D[Y] - P[Y]) * r + 2.0 * D[Y] * m) + Dy2 * (P[X] * P[X] + P[Z] * P[Z] - Entry->D[X] * Entry->D[X]); n1 = Solve_Polynomial(6, x1, y1, Test_Flag(this, STURM_FLAG), 0.0, Thread); break; } // Test roots for valid intersections. while(n1--) { w = y1[n1]; if((w >= 0.0) && (w <= 1.0)) { if(fabs(D[Y]) > EPSILON) { k = (w * (w * (w * Entry->A[Y] + Entry->B[Y]) + Entry->C[Y]) + Entry->D[Y] - P[Y]); if (test_hit(ray, Depth_Stack, k / Dylen, w, intervals[j].n, Thread)) { found = true; k /= D[Y]; if (k < best) best = k; } } else { k = w * (w * (w * Entry->A[X] + Entry->B[X]) + Entry->C[X]) + Entry->D[X]; x2[0] = r; x2[1] = 2.0 * m; x2[2] = P[X] * P[X] + P[Z] * P[Z] - k * k; n2 = Solve_Polynomial(2, x2, y2, false, 0.0, Thread); while(n2--) { k = y2[n2]; if(test_hit(ray, Depth_Stack, k / len, w, intervals[j].n, Thread)) { found = true; if(k < best) best = k; } } } } } } return found; }
bool Sor::Intersect(const BasicRay& ray, IStack& Depth_Stack, TraceThreadData *Thread) { int cnt; int found, j, n; DBL a, b, k, h, len, u, v, r0; DBL x[4]; DBL y[3]; DBL best; Vector3d P, D; SOR_SPLINE_ENTRY *Entry; /* Transform the ray into the surface of revolution space. */ MInvTransPoint(P, ray.Origin, Trans); MInvTransDirection(D, ray.Direction, Trans); len = D.length(); D /= len; /* Test if ray misses object's bounds. */ #ifdef SOR_EXTRA_STATS Thread->Stats()[Sor_Bound_Tests]++; #endif if (((D[Y] >= 0.0) && (P[Y] > Height2)) || ((D[Y] <= 0.0) && (P[Y] < Height1)) || ((D[X] >= 0.0) && (P[X] > Radius2)) || ((D[X] <= 0.0) && (P[X] < -Radius2))) { return(false); } /* Get distance of the ray from rotation axis (= y axis). */ r0 = P[X] * D[Z] - P[Z] * D[X]; if ((a = D[X] * D[X] + D[Z] * D[Z]) > 0.0) { r0 /= sqrt(a); } /* Test if ray misses object's bounds. */ if (r0 > Radius2) { return(false); } /* Test base/cap plane. */ found = false; best = BOUND_HUGE; if (Test_Flag(this, CLOSED_FLAG) && (fabs(D[Y]) > EPSILON)) { /* Test base plane. */ if (Base_Radius_Squared > DEPTH_TOLERANCE) { k = (Height1 - P[Y]) / D[Y]; u = P[X] + k * D[X]; v = P[Z] + k * D[Z]; b = u * u + v * v; if (b <= Base_Radius_Squared) { if (test_hit(ray, Depth_Stack, k / len, k, BASE_PLANE, 0, Thread)) { found = true; if (k < best) { best = k; } } } } /* Test cap plane. */ if (Cap_Radius_Squared > DEPTH_TOLERANCE) { k = (Height2 - P[Y]) / D[Y]; u = P[X] + k * D[X]; v = P[Z] + k * D[Z]; b = u * u + v * v; if (b <= Cap_Radius_Squared) { if (test_hit(ray, Depth_Stack, k / len, k, CAP_PLANE, 0, Thread)) { found = true; if (k < best) { best = k; } } } } } /* Intersect all cylindrical bounds. */ vector<BCYL_INT>& intervals = Thread->BCyl_Intervals; vector<BCYL_INT>& rint = Thread->BCyl_RInt; vector<BCYL_INT>& hint = Thread->BCyl_HInt; if ((cnt = Intersect_BCyl(Spline->BCyl, intervals, rint, hint, P, D)) == 0) { #ifdef SOR_EXTRA_STATS if (found) Thread->Stats()[Sor_Bound_Tests_Succeeded]++; #endif return(found); } #ifdef SOR_EXTRA_STATS Thread->Stats()[Sor_Bound_Tests_Succeeded]++; #endif /* Step through the list of intersections. */ for (j = 0; j < cnt; j++) { /* Get current segment. */ Entry = &Spline->Entry[intervals[j].n]; /* If we already have the best intersection we may exit. */ if (!(Type & IS_CHILD_OBJECT) && (intervals[j].d[0] > best)) { break; } /* Cubic curve. */ x[0] = Entry->A * D[Y] * D[Y] * D[Y]; /* x[1] = D[Y] * D[Y] * (3.0 * Entry->A * P[Y] + Entry->B) - D[X] * D[X] - D[Z] * D[Z]; */ x[1] = D[Y] * D[Y] * (3.0 * Entry->A * P[Y] + Entry->B) - a; x[2] = D[Y] * (P[Y] * (3.0 * Entry->A * P[Y] + 2.0 * Entry->B) + Entry->C) - 2.0 * (P[X] * D[X] + P[Z] * D[Z]); x[3] = P[Y] * (P[Y] * (Entry->A * P[Y] + Entry->B) + Entry->C) + Entry->D - P[X] * P[X] - P[Z] * P[Z]; n = Solve_Polynomial(3, x, y, Test_Flag(this, STURM_FLAG), 0.0, Thread); while (n--) { k = y[n]; h = P[Y] + k * D[Y]; if ((h >= Spline->BCyl->height[Spline->BCyl->entry[intervals[j].n].h1]) && (h <= Spline->BCyl->height[Spline->BCyl->entry[intervals[j].n].h2])) { if (test_hit(ray, Depth_Stack, k / len, k, CURVE, intervals[j].n, Thread)) { found = true; if (y[n] < best) { best = k; } } } } } return(found); }
static int intersect_cone(RAY *Ray, CONE *Cone, CONE_INT *Intersection) { int i = 0; DBL a, b, c, z, t1, t2, len; DBL d; VECTOR P, D; Increase_Counter(stats[Ray_Cone_Tests]); /* Transform the ray into the cones space */ MInvTransPoint(P, Ray->Initial, Cone->Trans); MInvTransDirection(D, Ray->Direction, Cone->Trans); VLength(len, D); VInverseScaleEq(D, len); if (Test_Flag(Cone, CYLINDER_FLAG)) { /* Solve intersections with a cylinder */ a = D[X] * D[X] + D[Y] * D[Y]; if (a > EPSILON) { b = P[X] * D[X] + P[Y] * D[Y]; c = P[X] * P[X] + P[Y] * P[Y] - 1.0; d = b * b - a * c; if (d >= 0.0) { d = sqrt(d); t1 = (-b + d) / a; t2 = (-b - d) / a; z = P[Z] + t1 * D[Z]; if ((t1 > Cone_Tolerance) && (t1 < Max_Distance) && (z >= 0.0) && (z <= 1.0)) { Intersection[i].d = t1 / len; Intersection[i++].t = SIDE_HIT; } z = P[Z] + t2 * D[Z]; if ((t2 > Cone_Tolerance) && (t1 < Max_Distance) && (z >= 0.0) && (z <= 1.0)) { Intersection[i].d = t2 / len; Intersection[i++].t = SIDE_HIT; } } } } else { /* Solve intersections with a cone */ a = D[X] * D[X] + D[Y] * D[Y] - D[Z] * D[Z]; b = D[X] * P[X] + D[Y] * P[Y] - D[Z] * P[Z]; c = P[X] * P[X] + P[Y] * P[Y] - P[Z] * P[Z]; if (fabs(a) < EPSILON) { if (fabs(b) > EPSILON) { /* One intersection */ t1 = -0.5 * c / b; z = P[Z] + t1 * D[Z]; if ((t1 > Cone_Tolerance) && (t1 < Max_Distance) && (z >= Cone->dist) && (z <= 1.0)) { Intersection[i].d = t1 / len; Intersection[i++].t = SIDE_HIT; } } } else { /* Check hits against the side of the cone */ d = b * b - a * c; if (d >= 0.0) { d = sqrt(d); t1 = (-b - d) / a; t2 = (-b + d) / a; z = P[Z] + t1 * D[Z]; if ((t1 > Cone_Tolerance) && (t1 < Max_Distance) && (z >= Cone->dist) && (z <= 1.0)) { Intersection[i].d = t1 / len; Intersection[i++].t = SIDE_HIT; } z = P[Z] + t2 * D[Z]; if ((t2 > Cone_Tolerance) && (t1 < Max_Distance) && (z >= Cone->dist) && (z <= 1.0)) { Intersection[i].d = t2 / len; Intersection[i++].t = SIDE_HIT; } } } } if (Test_Flag(Cone, CLOSED_FLAG) && (fabs(D[Z]) > EPSILON)) { d = (1.0 - P[Z]) / D[Z]; a = (P[X] + d * D[X]); b = (P[Y] + d * D[Y]); if (((Sqr(a) + Sqr(b)) <= 1.0) && (d > Cone_Tolerance) && (d < Max_Distance)) { Intersection[i].d = d / len; Intersection[i++].t = CAP_HIT; } d = (Cone->dist - P[Z]) / D[Z]; a = (P[X] + d * D[X]); b = (P[Y] + d * D[Y]); if ((Sqr(a) + Sqr(b)) <= (Test_Flag(Cone, CYLINDER_FLAG) ? 1.0 : Sqr(Cone->dist)) && (d > Cone_Tolerance) && (d < Max_Distance)) { Intersection[i].d = d / len; Intersection[i++].t = BASE_HIT; } } if (i) { Increase_Counter(stats[Ray_Cone_Tests_Succeeded]); } return (i); }
bool Parametric::All_Intersections(const Ray& ray, IStack& Depth_Stack, TraceThreadData *Thread) { VECTOR P, D, IPoint; UV_VECT low_vect, hi_vect, uv; Ray New_Ray; DBL XRayMin, XRayMax, YRayMin, YRayMax, ZRayMin, ZRayMax, TPotRes, TLen; DBL Depth1, Depth2, temp, Len, TResult = HUGE_VAL; DBL low, hi, len; int MaxPrecompX, MaxPrecompY, MaxPrecompZ; int split, i = 0, Side1, Side2; int parX, parY; int i_flg; DBL Intervals_Low[2][32]; DBL Intervals_Hi[2][32]; int SectorNum[32]; Thread->Stats()[Ray_Par_Bound_Tests]++; if(container_shape) { if(Trans != NULL) { MInvTransPoint(New_Ray.Origin, ray.Origin, Trans); MInvTransDirection(New_Ray.Direction, ray.Direction, Trans); VLength(len, New_Ray.Direction); VInverseScaleEq(New_Ray.Direction, len); i_flg = Sphere::Intersect(New_Ray, container.sphere.center, (container.sphere.radius) * (container.sphere.radius), &Depth1, &Depth2); Depth1 = Depth1 / len; Depth2 = Depth2 / len; } else { i_flg = Sphere::Intersect(ray, container.sphere.center, (container.sphere.radius) * (container.sphere.radius), &Depth1, &Depth2); } Thread->Stats()[Ray_Sphere_Tests]--; if(i_flg) Thread->Stats()[Ray_Sphere_Tests_Succeeded]--; } else { i_flg = Box::Intersect(ray, Trans, container.box.corner1, container.box.corner2, &Depth1, &Depth2, &Side1, &Side2); } if(!i_flg) return false; Thread->Stats()[Ray_Par_Bound_Tests_Succeeded]++; Thread->Stats()[Ray_Parametric_Tests]++; if (Trans != NULL) { MInvTransPoint(P, ray.Origin, Trans); MInvTransDirection(D, ray.Direction, Trans); } else { P[X] = ray.Origin[X]; P[Y] = ray.Origin[Y]; P[Z] = ray.Origin[Z]; D[X] = ray.Direction[X]; D[Y] = ray.Direction[Y]; D[Z] = ray.Direction[Z]; } if (Depth1 == Depth2) Depth1 = 0; if ((Depth1 += 4 * accuracy) > Depth2) return false; Intervals_Low[INDEX_U][0] = umin; Intervals_Hi[INDEX_U][0] = umax; Intervals_Low[INDEX_V][0] = vmin; Intervals_Hi[INDEX_V][0] = vmax; /* Fri 09-27-1996 0. */ SectorNum[0] = 1; MaxPrecompX = MaxPrecompY = MaxPrecompZ = 0; if (PData != NULL) { if (((PData->flags) & OK_X) != 0) MaxPrecompX = 1 << (PData->depth); if (((PData->flags) & OK_Y) != 0) MaxPrecompY = 1 << (PData->depth); if (((PData->flags) & OK_Z) != 0) MaxPrecompZ = 1 << (PData->depth); } /* 0 */ while (i >= 0) { low_vect[U] = Intervals_Low[INDEX_U][i]; hi_vect[U] = Intervals_Hi[INDEX_U][i]; Len = hi_vect[U] - low_vect[U]; split = INDEX_U; low_vect[V] = Intervals_Low[INDEX_V][i]; hi_vect[V] = Intervals_Hi[INDEX_V][i]; temp = hi_vect[V] - low_vect[V]; if (temp > Len) { Len = temp; split = INDEX_V; } parX = parY = 0; TLen = 0; /* X */ if (SectorNum[i] < MaxPrecompX) { low = PData->Low[0][SectorNum[i]]; hi = PData->Hi[0][SectorNum[i]]; } else Evaluate_Function_Interval_UV(Thread->functionContext, *(Function[0]), accuracy, low_vect, hi_vect, max_gradient, low, hi); /* fabs(D[X] *(T2-T1)) is not OK with new method */ if (close(D[0], 0)) { parX = 1; if ((hi < P[0]) || (low > P[0])) { i--; continue; } } else { XRayMin = (hi - P[0]) / D[0]; XRayMax = (low - P[0]) / D[0]; if (XRayMin > XRayMax) { temp = XRayMin; XRayMin = XRayMax; XRayMax = temp; } if ((XRayMin > Depth2) || (XRayMax < Depth1)) { i--; continue; } if ((TPotRes = XRayMin) > TResult) { i--; continue; } TLen = XRayMax - XRayMin; } /* Y */ if (SectorNum[i] < MaxPrecompY) { low = PData->Low[1][SectorNum[i]]; hi = PData->Hi[1][SectorNum[i]]; } else Evaluate_Function_Interval_UV(Thread->functionContext, *(Function[1]), accuracy, low_vect, hi_vect, max_gradient, low, hi); if (close(D[1], 0)) { parY = 1; if ((hi < P[1]) || (low > P[1])) { i--; continue; } } else { YRayMin = (hi - P[1]) / D[1]; YRayMax = (low - P[1]) / D[1]; if (YRayMin > YRayMax) { temp = YRayMin; YRayMin = YRayMax; YRayMax = temp; } if ((YRayMin > Depth2) || (YRayMax < Depth1)) { i--; continue; } if ((TPotRes = YRayMin) > TResult) { i--; continue; } if (parX == 0) { if ((YRayMin > XRayMax) || (YRayMax < XRayMin)) { i--; continue; } } if ((temp = YRayMax - YRayMin) > TLen) TLen = temp; } /* Z */ if ((SectorNum[i] < MaxPrecompZ) && (0 < SectorNum[i])) { low = PData->Low[2][SectorNum[i]]; hi = PData->Hi[2][SectorNum[i]]; } else Evaluate_Function_Interval_UV(Thread->functionContext, *(Function[2]), accuracy, low_vect, hi_vect, max_gradient, low, hi); if (close(D[2], 0)) { if ((hi < P[2]) || (low > P[2])) { i--; continue; } } else { ZRayMin = (hi - P[2]) / D[2]; ZRayMax = (low - P[2]) / D[2]; if (ZRayMin > ZRayMax) { temp = ZRayMin; ZRayMin = ZRayMax; ZRayMax = temp; } if ((ZRayMin > Depth2) || (ZRayMax < Depth1)) { i--; continue; } if ((TPotRes = ZRayMin) > TResult) { i--; continue; } if (parX == 0) { if ((ZRayMin > XRayMax) || (ZRayMax < XRayMin)) { i--; continue; } } if (parY == 0) { if ((ZRayMin > YRayMax) || (ZRayMax < YRayMin)) { i--; continue; } } if ((temp = ZRayMax - ZRayMin) > TLen) TLen = temp; } if (Len > TLen) Len = TLen; if (Len < accuracy) { if ((TResult > TPotRes) && (TPotRes > Depth1)) { TResult = TPotRes; Assign_UV_Vect(uv, low_vect); } i--; } else { /* 1 copy */ if ((SectorNum[i] *= 2) >= Max_intNumber) SectorNum[i] = Max_intNumber; SectorNum[i + 1] = SectorNum[i]; SectorNum[i]++; i++; Intervals_Low[INDEX_U][i] = low_vect[U]; Intervals_Hi[INDEX_U][i] = hi_vect[U]; Intervals_Low[INDEX_V][i] = low_vect[V]; Intervals_Hi[INDEX_V][i] = hi_vect[V]; /* 2 split */ temp = (Intervals_Hi[split][i] + Intervals_Low[split][i]) / 2.0; Intervals_Hi[split][i] = temp; Intervals_Low[split][i - 1] = temp; } } if (TResult < Depth2) { Thread->Stats()[Ray_Parametric_Tests_Succeeded]++; VScale(IPoint, ray.Direction, TResult); VAddEq(IPoint, ray.Origin); if (Clip.empty() || Point_In_Clip(IPoint, Clip, Thread)) { /* compute_param_normal( Par, UResult, VResult , &N); push_normal_entry( TResult ,IPoint, N, (ObjectPtr ) Object, Depth_Stack); */ Depth_Stack->push(Intersection(TResult, IPoint, uv, this)); return true; } } return false; }
int Intersect_Box(RAY *Ray, TRANSFORM *Trans, VECTOR Corner1, VECTOR Corner2, DBL *Depth1, DBL *Depth2, int *Side1, int *Side2) { int smin = 0, smax = 0; /* Side hit for min/max intersection. */ DBL t, tmin, tmax; VECTOR P, D; /* Transform the point into the boxes space */ if (Trans != NULL) { MInvTransPoint(P, Ray->Initial, Trans); MInvTransDirection(D, Ray->Direction, Trans); } else { Assign_Vector(P, Ray->Initial); Assign_Vector(D, Ray->Direction); } tmin = 0.0; tmax = BOUND_HUGE; /* * Sides first. */ if (D[X] < -EPSILON) { t = (Corner1[X] - P[X]) / D[X]; if (t < tmin) return(false); if (t <= tmax) { smax = SIDE_X_0; tmax = t; } t = (Corner2[X] - P[X]) / D[X]; if (t >= tmin) { if (t > tmax) return(false); smin = SIDE_X_1; tmin = t; } } else { if (D[X] > EPSILON) { t = (Corner2[X] - P[X]) / D[X]; if (t < tmin) return(false); if (t <= tmax) { smax = SIDE_X_1; tmax = t; } t = (Corner1[X] - P[X]) / D[X]; if (t >= tmin) { if (t > tmax) return(false); smin = SIDE_X_0; tmin = t; } } else { if ((P[X] < Corner1[X]) || (P[X] > Corner2[X])) { return(false); } } } /* * Check Top/Bottom. */ if (D[Y] < -EPSILON) { t = (Corner1[Y] - P[Y]) / D[Y]; if (t < tmin) return(false); if (t <= tmax - CLOSE_TOLERANCE) { smax = SIDE_Y_0; tmax = t; } else { /* * If intersection points are close to each other find out * which side to use, i.e. is most probably hit. [DB 9/94] */ if (t <= tmax + CLOSE_TOLERANCE) { if (-D[Y] > fabs(D[X])) smax = SIDE_Y_0; } } t = (Corner2[Y] - P[Y]) / D[Y]; if (t >= tmin + CLOSE_TOLERANCE) { if (t > tmax) return(false); smin = SIDE_Y_1; tmin = t; } else { /* * If intersection points are close to each other find out * which side to use, i.e. is most probably hit. [DB 9/94] */ if (t >= tmin - CLOSE_TOLERANCE) { if (-D[Y] > fabs(D[X])) smin = SIDE_Y_1; } } } else { if (D[Y] > EPSILON) { t = (Corner2[Y] - P[Y]) / D[Y]; if (t < tmin) return(false); if (t <= tmax - CLOSE_TOLERANCE) { smax = SIDE_Y_1; tmax = t; } else { /* * If intersection points are close to each other find out * which side to use, i.e. is most probably hit. [DB 9/94] */ if (t <= tmax + CLOSE_TOLERANCE) { if (D[Y] > fabs(D[X])) smax = SIDE_Y_1; } } t = (Corner1[Y] - P[Y]) / D[Y]; if (t >= tmin + CLOSE_TOLERANCE) { if (t > tmax) return(false); smin = SIDE_Y_0; tmin = t; } else { /* * If intersection points are close to each other find out * which side to use, i.e. is most probably hit. [DB 9/94] */ if (t >= tmin - CLOSE_TOLERANCE) { if (D[Y] > fabs(D[X])) smin = SIDE_Y_0; } } } else { if ((P[Y] < Corner1[Y]) || (P[Y] > Corner2[Y])) { return(false); } } } /* Now front/back */ if (D[Z] < -EPSILON) { t = (Corner1[Z] - P[Z]) / D[Z]; if (t < tmin) return(false); if (t <= tmax - CLOSE_TOLERANCE) { smax = SIDE_Z_0; tmax = t; } else { /* * If intersection points are close to each other find out * which side to use, i.e. is most probably hit. [DB 9/94] */ if (t <= tmax + CLOSE_TOLERANCE) { switch (smax) { case SIDE_X_0 : case SIDE_X_1 : if (-D[Z] > fabs(D[X])) smax = SIDE_Z_0; break; case SIDE_Y_0 : case SIDE_Y_1 : if (-D[Z] > fabs(D[Y])) smax = SIDE_Z_0; break; } } } t = (Corner2[Z] - P[Z]) / D[Z]; if (t >= tmin + CLOSE_TOLERANCE) { if (t > tmax) return(false); smin = SIDE_Z_1; tmin = t; } else { /* * If intersection points are close to each other find out * which side to use, i.e. is most probably hit. [DB 9/94] */ if (t >= tmin - CLOSE_TOLERANCE) { switch (smin) { case SIDE_X_0 : case SIDE_X_1 : if (-D[Z] > fabs(D[X])) smin = SIDE_Z_1; break; case SIDE_Y_0 : case SIDE_Y_1 : if (-D[Z] > fabs(D[Y])) smin = SIDE_Z_1; break; } } } } else { if (D[Z] > EPSILON) { t = (Corner2[Z] - P[Z]) / D[Z]; if (t < tmin) return(false); if (t <= tmax - CLOSE_TOLERANCE) { smax = SIDE_Z_1; tmax = t; } else { /* * If intersection points are close to each other find out * which side to use, i.e. is most probably hit. [DB 9/94] */ if (t <= tmax + CLOSE_TOLERANCE) { switch (smax) { case SIDE_X_0 : case SIDE_X_1 : if (D[Z] > fabs(D[X])) smax = SIDE_Z_1; break; case SIDE_Y_0 : case SIDE_Y_1 : if (D[Z] > fabs(D[Y])) smax = SIDE_Z_1; break; } } } t = (Corner1[Z] - P[Z]) / D[Z]; if (t >= tmin + CLOSE_TOLERANCE) { if (t > tmax) return(false); smin = SIDE_Z_0; tmin = t; } else { /* * If intersection points are close to each other find out * which side to use, i.e. is most probably hit. [DB 9/94] */ if (t >= tmin - CLOSE_TOLERANCE) { switch (smin) { case SIDE_X_0 : case SIDE_X_1 : if (D[Z] > fabs(D[X])) smin = SIDE_Z_0; break; case SIDE_Y_0 : case SIDE_Y_1 : if (D[Z] > fabs(D[Y])) smin = SIDE_Z_0; break; } } } } else { if ((P[Z] < Corner1[Z]) || (P[Z] > Corner2[Z])) { return(false); } } } if (tmax < DEPTH_TOLERANCE) { return (false); } *Depth1 = tmin; *Depth2 = tmax; *Side1 = smin; *Side2 = smax; return(true); }
int Torus::Intersect(const Ray& ray, DBL *Depth, SceneThreadData *Thread) const { int i, n; DBL len, R2, Py2, Dy2, PDy2, k1, k2; DBL y1, y2, r1, r2; DBL c[5]; DBL r[4]; VECTOR P, D; DBL DistanceP; // Distance from P to torus center (origo). DBL BoundingSphereRadius; // Sphere fully (amply) enclosing torus. DBL Closer; // P is moved Closer*D closer to torus. Thread->Stats()[Ray_Torus_Tests]++; /* Transform the ray into the torus space. */ MInvTransPoint(P, ray.Origin, Trans); MInvTransDirection(D, ray.Direction, Trans); VLength(len, D); VInverseScaleEq(D, len); i = 0; y1 = -MinorRadius; y2 = MinorRadius; r1 = Sqr(MajorRadius - MinorRadius); if ( MajorRadius < MinorRadius ) r1 = 0; r2 = Sqr(MajorRadius + MinorRadius); #ifdef TORUS_EXTRA_STATS Thread->Stats()[Torus_Bound_Tests]++; #endif if (Test_Thick_Cylinder(P, D, y1, y2, r1, r2)) { #ifdef TORUS_EXTRA_STATS Thread->Stats()[Torus_Bound_Tests_Succeeded]++; #endif // Move P close to bounding sphere to have more precise root calculation. // Bounding sphere radius is R + r, we add r once more to ensure // that P is safely outside sphere. BoundingSphereRadius = MajorRadius + MinorRadius + MinorRadius; DistanceP = VSumSqr(P); // Distance is currently squared. Closer = 0.0; if (DistanceP > Sqr(BoundingSphereRadius)) { DistanceP = sqrt(DistanceP); // Now real distance. Closer = DistanceP - BoundingSphereRadius; VAddScaledEq(P, Closer, D); } R2 = Sqr(MajorRadius); r2 = Sqr(MinorRadius); Py2 = P[Y] * P[Y]; Dy2 = D[Y] * D[Y]; PDy2 = P[Y] * D[Y]; k1 = P[X] * P[X] + P[Z] * P[Z] + Py2 - R2 - r2; k2 = P[X] * D[X] + P[Z] * D[Z] + PDy2; c[0] = 1.0; c[1] = 4.0 * k2; c[2] = 2.0 * (k1 + 2.0 * (k2 * k2 + R2 * Dy2)); c[3] = 4.0 * (k2 * k1 + 2.0 * R2 * PDy2); c[4] = k1 * k1 + 4.0 * R2 * (Py2 - r2); n = Solve_Polynomial(4, c, r, Test_Flag(this, STURM_FLAG), ROOT_TOLERANCE, Thread); while(n--) Depth[i++] = (r[n] + Closer) / len; } if (i) Thread->Stats()[Ray_Torus_Tests_Succeeded]++; return(i); }
bool Fractal::All_Intersections(const Ray& ray, IStack& Depth_Stack, TraceThreadData *Thread) { bool Intersection_Found; bool LastIsInside = false; bool CurrentIsInside, NextIsInside; DBL Depth, Depth_Max; DBL Dist, Dist_Next, LenSqr, LenInv; Vector3d IPoint, Mid_Point, Next_Point, Real_Pt; Vector3d Real_Normal, F_Normal; Vector3d Direction; BasicRay New_Ray; Thread->Stats()[Ray_Fractal_Tests]++; Intersection_Found = false; /* Get into Fractal's world. */ if (Trans != NULL) { MInvTransDirection(Direction, ray.Direction, Trans); LenSqr = Direction.lengthSqr(); if (LenSqr == 0.0) { return (false); } if (LenSqr != 1.0) { LenInv = 1.0 / sqrt(LenSqr); Direction *= LenInv; } else LenInv = 1.0; New_Ray.Direction = Direction; MInvTransPoint(New_Ray.Origin, ray.Origin, Trans); } else { Direction = ray.Direction; New_Ray = ray; LenInv = 1.0; } /* Bound fractal. */ if (!F_Bound(New_Ray, this, &Depth, &Depth_Max)) { return (false); } if (Depth_Max < Fractal_Tolerance) { return (false); } if (Depth < Fractal_Tolerance) { Depth = Fractal_Tolerance; } /* Jump to starting point */ Next_Point = New_Ray.Origin + Direction * Depth; CurrentIsInside = D_Iteration(Next_Point, this, Direction, &Dist, Thread->Fractal_IStack); /* Light ray starting inside ? */ if (CurrentIsInside) { Next_Point += (2.0 * Fractal_Tolerance) * Direction; Depth += 2.0 * Fractal_Tolerance; if (Depth > Depth_Max) { return (false); } CurrentIsInside = D_Iteration(Next_Point, this, Direction, &Dist, Thread->Fractal_IStack); } /* Ok. Trace it */ while (Depth < Depth_Max) { /* * Get close to the root: Advance with Next_Point, keeping track of last * position in IPoint... */ while (1) { if (Dist < Precision) Dist = Precision; Depth += Dist; if (Depth > Depth_Max) { if (Intersection_Found) Thread->Stats()[Ray_Fractal_Tests_Succeeded]++; return (Intersection_Found); } IPoint = Next_Point; Next_Point += Dist * Direction; NextIsInside = D_Iteration(Next_Point, this, Direction, &Dist_Next, Thread->Fractal_IStack); if (NextIsInside != CurrentIsInside) { /* Set surface was crossed... */ Depth -= Dist; break; } else { Dist = Dist_Next; /* not reached */ } } /* then, polish the root via bisection method... */ while (Dist > Fractal_Tolerance) { Dist *= 0.5; Mid_Point = IPoint + Dist * Direction; LastIsInside = Iteration(Mid_Point, this, Thread->Fractal_IStack); if (LastIsInside == CurrentIsInside) { IPoint = Mid_Point; Depth += Dist; if (Depth > Depth_Max) { if (Intersection_Found) Thread->Stats()[Ray_Fractal_Tests_Succeeded]++; return (Intersection_Found); } } } if (!CurrentIsInside) /* Mid_Point isn't inside the set */ { IPoint += Dist * Direction; Depth += Dist; Iteration(IPoint, this, Thread->Fractal_IStack); } else { if (LastIsInside != CurrentIsInside) { Iteration(IPoint, this, Thread->Fractal_IStack); } } if (Trans != NULL) { MTransPoint(Real_Pt, IPoint, Trans); Normal_Calc(this, F_Normal, Thread->Fractal_IStack); MTransNormal(Real_Normal, F_Normal, Trans); } else { Real_Pt = IPoint; Normal_Calc(this, Real_Normal, Thread->Fractal_IStack); } if (Clip.empty() || Point_In_Clip(Real_Pt, Clip, Thread)) { Real_Normal.normalize(); Depth_Stack->push(Intersection(Depth * LenInv, Real_Pt, Real_Normal, this)); Intersection_Found = true; /* If fractal isn't used with CSG we can exit now. */ if (!(Type & IS_CHILD_OBJECT)) { break; } } /* Start over where work was left */ IPoint = Next_Point; Dist = Dist_Next; CurrentIsInside = NextIsInside; } if (Intersection_Found) Thread->Stats()[Ray_Fractal_Tests_Succeeded]++; return (Intersection_Found); }
bool Superellipsoid::Intersect(const BasicRay& ray, IStack& Depth_Stack, TraceThreadData *Thread) { int i, cnt, Found = false; DBL dists[PLANECOUNT+2]; DBL t, t1, t2, v0, v1, len; Vector3d P, D, P0, P1, P2, P3; /* Transform the ray into the superellipsoid space. */ MInvTransPoint(P, ray.Origin, Trans); MInvTransDirection(D, ray.Direction, Trans); len = D.length(); D /= len; /* Intersect superellipsoid's bounding box. */ if (!intersect_box(P, D, &t1, &t2)) { return(false); } /* Test if superellipsoid lies 'behind' the ray origin. */ if (t2 < DEPTH_TOLERANCE) { return(false); } cnt = 0; if (t1 < DEPTH_TOLERANCE) { t1 = DEPTH_TOLERANCE; } dists[cnt++] = t1; dists[cnt++] = t2; /* Intersect ray with planes cutting superellipsoids in pieces. */ cnt = find_ray_plane_points(P, D, cnt, dists, t1, t2); if (cnt <= 1) { return(false); } P0 = P + dists[0] * D; v0 = evaluate_superellipsoid(P0); if (fabs(v0) < ZERO_TOLERANCE) { if (insert_hit(ray, dists[0] / len, Depth_Stack, Thread)) { if (Type & IS_CHILD_OBJECT) { Found = true; } else { return(true); } } } for (i = 1; i < cnt; i++) { P1 = P + dists[i] * D; v1 = evaluate_superellipsoid(P1); if (fabs(v1) < ZERO_TOLERANCE) { if (insert_hit(ray, dists[i] / len, Depth_Stack, Thread)) { if (Type & IS_CHILD_OBJECT) { Found = true; } else { return(true); } } } else { if (v0 * v1 < 0.0) { /* Opposite signs, there must be a root between */ solve_hit1(v0, P0, v1, P1, P2); P3 = P2 - P; t = P3.length(); if (insert_hit(ray, t / len, Depth_Stack, Thread)) { if (Type & IS_CHILD_OBJECT) { Found = true; } else { return(true); } } } else { /* * Although there was no sign change, we may actually be approaching * the surface. In this case, we are being fooled by the shape of the * surface into thinking there isn't a root between sample points. */ if (check_hit2(P, D, dists[i-1], P0, v0, dists[i], &t, P2)) { if (insert_hit(ray, t / len, Depth_Stack, Thread)) { if (Type & IS_CHILD_OBJECT) { Found = true; } else { return(true); } } else { break; } } } } v0 = v1; P0 = P1; } return(Found); }