Commit 267990ba authored by Lukas Werner's avatar Lukas Werner
Browse files

Consider special cases in ray - sphere intersection

parent fb0f395e
Pipeline #6350 failed with stage
in 76 minutes and 43 seconds
......@@ -48,7 +48,24 @@ namespace walberla {
real_t root = sqrt(discriminant);
real_t t0 = (-b - root) / (real_t(2.) * a); // point where the ray enters the sphere
real_t t1 = (-b + root) / (real_t(2.) * a); // point where the ray leaves the sphere
*t = (t0 < t1) ? t0 : t1; //assign the closest hit point to t
if (t0 < 0 && t1 < 0) {
*t = real_t(INFINITY);
return false;
}
real_t t_;
t_ = (t0 < t1) ? t0 : t1; // assign the closest hit point to t
if (t_ < 0) {
// at least one of the calculated hit point is behind the rays origin
if (t1 < 0) {
// both of the points are behind the origin (ray does not hit sphere)
*t = real_t(INFINITY);
return false;
} else {
// one point is hit by the ray (ray is within sphere)
t_ = t1;
}
}
*t = t_;
return true;
}
......
......@@ -28,21 +28,28 @@ void SphereIntersectsTest()
{
MaterialID iron = Material::find("iron");
Sphere sp1(123, 1, Vec3(3,3,3), Vec3(0,0,0), Quat(), 2, iron, false, true, false);
real_t t;
// ray through the center
Ray ray1(Vec3(-5,3,3), Vec3(1,0,0));
WALBERLA_LOG_INFO("RAY -> SPHERE");
real_t t;
WALBERLA_LOG_INFO("RAY -> SPHERE: through center (hitting)");
WALBERLA_CHECK(intersects(&sp1, &ray1, &t));
WALBERLA_CHECK(realIsEqual(t, real_t(6)))
// ray tangential
Ray ray2(Vec3(-5,3,3), Vec3(7.5,0,sqrt(real_t(15))/real_t(2)));
WALBERLA_LOG_INFO("RAY -> SPHERE: tangential (not hitting)");
WALBERLA_CHECK(!intersects(&sp1, &ray2, &t));
// sphere behind ray origin
Sphere sp2(123, 1, Vec3(-8,3,3), Vec3(0,0,0), Quat(), 2, iron, false, true, false);
WALBERLA_CHECK(!intersects(&sp2, &ray1, &t));
// sphere around ray origin
Sphere sp3(123, 1, Vec3(-5,3,3), Vec3(0,0,0), Quat(), 2, iron, false, true, false);
WALBERLA_CHECK(intersects(&sp3, &ray1, &t));
WALBERLA_LOG_INFO(t);
WALBERLA_CHECK(realIsEqual(t, real_t(2)));
}
void PlaneIntersectsTest() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment