From 80d0e1c3367628a3048f68e1514db399ded9ae03 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 10 Feb 2017 15:40:26 +0000 Subject: [PATCH] Add test for intersection and fix data in raytracer test. --- src/gamelib/grid_raytrace.cpp | 2 +- test/unit/grid_raytrace.cpp | 54 ++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/gamelib/grid_raytrace.cpp b/src/gamelib/grid_raytrace.cpp index 862e0db..3309bf7 100644 --- a/src/gamelib/grid_raytrace.cpp +++ b/src/gamelib/grid_raytrace.cpp @@ -71,7 +71,7 @@ namespace curry { assert(not (std::abs(r_cross_s) <= FLT_EPSILON)); const auto inv_r_cross_s = 1.0f / r_cross_s; const auto t = cross(q - p, s) * inv_r_cross_s; - const auto u = cross(q - p, r) * inv_r_cross_s; + //const auto u = cross(q - p, r) * inv_r_cross_s; return t; //if (0.0f <= t and t <= 1.0f and 0.0f <= u and u <= 1.0f) diff --git a/test/unit/grid_raytrace.cpp b/test/unit/grid_raytrace.cpp index 43970f4..5fbd6a9 100644 --- a/test/unit/grid_raytrace.cpp +++ b/test/unit/grid_raytrace.cpp @@ -24,7 +24,7 @@ #include "tileproperty.hpp" #include -TEST_CASE ("Check that 2D raytracing works", "[raytracing, geometry]") { +TEST_CASE ("Check that 2D raytracing works", "[raytracing][geometry]") { using curry::for_each_voxel_under_segment; using curry::vec2us; using curry::vec2f; @@ -32,16 +32,16 @@ TEST_CASE ("Check that 2D raytracing works", "[raytracing, geometry]") { curry::WorldGrid world(vec2us(64)); world.set_layers(vec2us(10, 10), std::vector>( {{ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }})); world.set_tile_properties(std::vector({ curry::TileProperty() })); @@ -74,3 +74,35 @@ TEST_CASE ("Check that 2D raytracing works", "[raytracing, geometry]") { CHECK(expected == diagonal); } } + +TEST_CASE ("Check that segment intersection code used by the raytracing code works", "[geometry][intersection]") { + using curry::vec2f; + using curry::segment_intersection; + + { + //Two segments crossing at <0, 0> + const vec2f a0(-1.0f, 0.0f); + const vec2f a1(2.0f, 0.0f); + const vec2f b0(0.0f, 1.0f); + const vec2f b1(0.0f, -2.0f); + + const float t1 = segment_intersection(a0, a1, b0, b1); + CHECK(0.5f == t1); + CHECK(vec2f(0.0f) == a0 + t1 * a1); + const float t2 = segment_intersection(b0, b1, a0, a1); + CHECK(0.5f == t2); + } + + { + //a diagonal segment starting off from a vertical segment + const vec2f a0(-1.0f, 0.0f); + const vec2f a1(3.0f, 2.0f); + const vec2f b0(-1.0f, 1.0f); + const vec2f b1(0.0f, -2.0f); + + const float t1 = segment_intersection(a0, a1, b0, b1); + CHECK(0.0f == t1); + const float t2 = segment_intersection(b0, b1, a0, a1); + CHECK(0.5f == t2); + } +}