Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.29
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
raycast.cpp
1#include <Core/Geometry/RayCast.hpp>
2#include <Core/Math/Math.hpp>
3#include <catch2/catch_test_macros.hpp>
4
5TEST_CASE( "Core/Geometry/RayCast", "[unittests][Core][Core/Geometry][RayCast]" ) {
6 using namespace Ra::Core;
7 Aabb ones( -Vector3::Ones(), Vector3::Ones() );
8 // For all directions x, y, z
9 for ( int i = 0; i < 3; ++i ) {
10 // From negative x and positive x
11 for ( int sig = -1; sig < 2; sig += 2 ) {
12 Vector3 s = 3 * sig * Vector3::Unit( i );
13 Vector3 d = -sig * Vector3::Unit( i );
14
15 for ( int p = -10; p < 11; p++ ) {
16 for ( int q = -10; q < 11; q++ ) {
17 Vector3 dir = d + Scalar( p ) / 10_ra * Vector3::Unit( ( i + 1 ) % 3 ) +
18 Scalar( q ) / 10_ra * Vector3::Unit( ( i + 2 ) % 3 );
19
20 // Fire a ray towards the box (some hit, some miss).
21 {
22 Eigen::ParametrizedLine<Scalar, 3> r( s, dir.normalized() );
23
24 Scalar t = 0_ra;
25 Vector3 n = Vector3::Zero();
26 const bool result = Geometry::RayCastAabb( r, ones, t, n );
27
28 if ( std::abs( p ) <= 5 && std::abs( q ) <= 5 ) {
29 // The ray should have hit
30 REQUIRE( result );
31 // Wrong normal
32 REQUIRE(
33 Math::areApproxEqual( n.dot( sig * Vector3::Unit( i ) ), 1_ra ) );
34 // Wrong hit point
35 REQUIRE( Math::areApproxEqual( r.pointAt( t )[i], Scalar( sig ) ) );
36 }
37 else { REQUIRE( !result ); } // The ray should have missed
38 }
39
40 // Fire a ray on the other direction (which should miss)
41 {
42 Eigen::ParametrizedLine<Scalar, 3> r( s, -dir.normalized() );
43
44 Scalar t;
45 Vector3 n;
46 const bool result = Geometry::RayCastAabb( r, ones, t, n );
47
48 // The ray should have missed (t<0)
49 REQUIRE( !result );
50 }
51
52 // Fire a ray from within the box.
53 {
54 Eigen::ParametrizedLine<Scalar, 3> r( Vector3::Zero(), dir.normalized() );
55
56 Scalar t;
57 Vector3 n { 0, 0, 0 };
58 const bool result = Geometry::RayCastAabb( r, ones, t, n );
59
60 // The ray should have hit (inside hit)
61 REQUIRE( result );
62 // Hit should be at origin
63 REQUIRE( Math::areApproxEqual( t, 0_ra ) );
64 // Wrong normal (inside hit)
65 REQUIRE( Math::areApproxEqual( n.dot( dir.normalized() ), -1_ra ) );
66 }
67 }
68 }
69 }
70 }
71}
This namespace contains everything "low level", related to data, datastuctures, and computation.
Definition Cage.cpp:5