Commit 6ca2ac89 authored by Lukas Werner's avatar Lukas Werner
Browse files

Implemented ability to raytrace rotated boxes

parent 343745b2
Pipeline #6742 failed with stage
in 152 minutes and 48 seconds
......@@ -91,8 +91,60 @@ namespace walberla {
}
inline bool intersects(BoxID box, Ray* ray, real_t* t) {
Mat3 invRotationMatrix = box->getRotation().getInverse();
Ray transformedRay(invRotationMatrix * (ray->origin - box->getPosition()), invRotationMatrix * ray->direction);
return intersects((GeomPrimitive*)box, ray, t);
Vec3 lengths = box->getLengths();
Vec3 lengthsHalved = lengths/real_t(2);
Vec3 bounds[2] = {
-lengthsHalved,
lengthsHalved
};
real_t txmin, txmax;
real_t tmin = txmin = (bounds[transformedRay.sign[0]][0] - transformedRay.origin[0]) * transformedRay.inv_direction[0];
real_t tmax = txmax = (bounds[1-transformedRay.sign[0]][0] - transformedRay.origin[0]) * transformedRay.inv_direction[0];
real_t tymin = (bounds[transformedRay.sign[1]][1] - transformedRay.origin[1]) * transformedRay.inv_direction[1];
real_t tymax = (bounds[1-transformedRay.sign[1]][1] - transformedRay.origin[1]) * transformedRay.inv_direction[1];
if (tmin > tymax || tymin > tmax) {
*t = INFINITY;
return false;
}
if (tymin > tmin) {
tmin = tymin;
}
if (tymax < tmax) {
tmax = tymax;
}
real_t tzmin = (bounds[transformedRay.sign[2]][2] - transformedRay.origin[2]) * transformedRay.inv_direction[2];
real_t tzmax = (bounds[1-transformedRay.sign[2]][2] - transformedRay.origin[2]) * transformedRay.inv_direction[2];
if (tmin > tzmax || tzmin > tmax) {
*t = INFINITY;
return false;
}
if (tzmin > tmin) {
tmin = tzmin;
}
if (tzmax < tmax) {
tmax = tzmax;
}
real_t t_;
if (tmin > 0) {
// ray hit box from outside
t_ = tmin;
} else if (tmax < 0) {
// tmin and tmax are smaller than 0 -> box is in rays negative direction
*t = INFINITY;
return false;
} else {
// ray origin within box
t_ = tmax;
}
*t = t_;
return true;
}
inline bool intersects(GeomPrimitive* body, Ray* ray, real_t* t) {
......
......@@ -77,14 +77,13 @@ void PlaneIntersectsTest() {
}
void BoxIntersectsTest() {
WALBERLA_LOG_INFO("RAY -> BOX");
MaterialID iron = Material::find("iron");
Box box1(127, 5, Vec3(0, -15, 0), Vec3(0, 0, 0), Quat(), Vec3(10, 10, 10), iron, false, true, false);
Ray ray1(Vec3(3,-5,3), Vec3(0,1,0));
real_t t;
WALBERLA_LOG_INFO("RAY -> BOX");
Box box1(127, 5, Vec3(0, -15, 0), Vec3(0, 0, 0), Quat(), Vec3(10, 10, 10), iron, false, true, false);
Ray ray1(Vec3(3,-5,3), Vec3(0,1,0));
WALBERLA_CHECK(!intersects(&box1, &ray1, &t));
Box box2(128, 5, Vec3(0, -2, 0), Vec3(0, 0, 0), Quat(), Vec3(10, 10, 10), iron, false, true, false);
......@@ -107,6 +106,14 @@ void BoxIntersectsTest() {
Ray ray4(Vec3(3,-5,3), Vec3(-2, 3, 0.5));
WALBERLA_CHECK(intersects(&box4, &ray4, &t));
WALBERLA_CHECK(realIsEqual(t, real_t(9.7068), 1e-4));
Box box5(128, 5, Vec3(4, 0, 0), Vec3(0, 0, 0), Quat(), Vec3(4, 4, 4), iron, false, true, false);
box5.rotate(0,0,math::M_PI/4);
Ray ray5(Vec3(0,1.5,0), Vec3(1,0,0));
WALBERLA_CHECK(intersects(&box5, &ray5, &t));
WALBERLA_CHECK(realIsEqual(t, real_t(2.67157), 1e-4));
WALBERLA_LOG_INFO("intersects: " << intersects(&box5, &ray5, &t) << ", t = " << t);
}
int main( int argc, char** argv )
......@@ -116,9 +123,9 @@ int main( int argc, char** argv )
SetBodyTypeIDs<BodyTuple>::execute();
SphereIntersectsTest();
PlaneIntersectsTest();
BoxIntersectsTest(); // does not work yet
//SphereIntersectsTest();
//PlaneIntersectsTest();
BoxIntersectsTest();
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