Commit fb0f395e authored by Lukas Werner's avatar Lukas Werner
Browse files

Added ray plane intersection with tests

parent 01982e87
Pipeline #6348 failed with stage
in 60 minutes and 27 seconds
......@@ -13,6 +13,8 @@
#include <boost/tuple/tuple.hpp>
#define EPSILON real_t(1e-4)
namespace walberla {
namespace pe {
namespace raytracing {
......@@ -36,7 +38,7 @@ namespace walberla {
real_t b = real_t(2.) * (displacement * ray->direction);
real_t c = (displacement * displacement) - (sphere->getRadius() * sphere->getRadius());
real_t discriminant = b*b - real_t(4.)*a*c;
if (discriminant < real_t(1e-4)) {
if (discriminant < EPSILON) {
// with discriminant smaller than 0, sphere is not hit by ray
// (no solution for quadratic equation)
// with discriminant being 0, sphere only tangentially hits the ray (not enough)
......@@ -51,7 +53,21 @@ namespace walberla {
}
inline bool intersects(PlaneID plane, Ray* ray, real_t* t) {
// ToDo
real_t denominator = plane->getNormal() * ray->direction;
if (std::abs(denominator) > EPSILON) {
real_t t_;
t_ = ((plane->getPosition() - ray->origin) * plane->getNormal()) / denominator;
if (t_ > EPSILON) {
Vec3 intersectionPoint = (t_*ray->direction + ray->origin);
Vec3 originToIntersection = ray->origin - intersectionPoint;
*t = originToIntersection.length();
return true;
} else {
*t = INFINITY;
}
}
*t = INFINITY;
return false;
}
}
......
......@@ -22,7 +22,7 @@ using namespace walberla;
using namespace walberla::pe;
using namespace walberla::pe::raytracing;
typedef boost::tuple<Box, Capsule, Plane, Sphere> BodyTuple;
typedef boost::tuple<Box, Capsule, Plane, Sphere> BodyTuple ;
void SphereIntersectsTest()
{
......@@ -45,6 +45,30 @@ void SphereIntersectsTest()
WALBERLA_CHECK(!intersects(&sp1, &ray2, &t));
}
void PlaneIntersectsTest() {
MaterialID iron = Material::find("iron");
// plane with center 3,3,3 and parallel to y-z plane
Plane pl1(1, 1, Vec3(3, 3, 3), Vec3(1, 0, 0), real_t(1.0), iron);
Ray ray1(Vec3(-5,3,3), Vec3(1,0,0));
real_t t;
WALBERLA_LOG_INFO("RAY -> PLANE");
WALBERLA_CHECK(intersects(&pl1, &ray1, &t), "ray through center did not hit");
WALBERLA_CHECK(realIsEqual(t, real_t(8)), "distance between ray and plane is incorrect");
Ray ray2(Vec3(-5,3,3), Vec3(1,0,-1));
WALBERLA_CHECK(intersects(&pl1, &ray2, &t), "ray towards random point on plane didn't hit");
WALBERLA_CHECK(realIsEqual(t, real_t(sqrt(real_t(128)))), "distance between ray and plane is incorrect");
// plane with center 3,3,3 and parallel to x-z plane
Plane pl2(1, 1, Vec3(3, 3, 3), Vec3(0, 1, 0), real_t(1.0), iron);
WALBERLA_CHECK(!intersects(&pl2, &ray1, &t), "ray parallel to plane shouldnt hit");
// plane with center -10,3,3 and parallel to y-z plane
Plane pl4(1, 1, Vec3(-10, 3, 3), Vec3(1, 0, 0), real_t(1.0), iron);
WALBERLA_CHECK(!intersects(&pl4, &ray1, &t), "ray hit plane behind origin");
}
int main( int argc, char** argv )
{
......@@ -54,6 +78,7 @@ int main( int argc, char** argv )
SetBodyTypeIDs<BodyTuple>::execute();
SphereIntersectsTest();
PlaneIntersectsTest();
return EXIT_SUCCESS;
}
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