MyCurry/test/unit/grid_raytrace.cpp

108 lines
3.4 KiB
C++

/*
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
This file is part of MyCurry.
MyCurry is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MyCurry is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MyCurry. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catch.hpp"
#include "grid_raytrace.hpp"
#include "worldgrid.hpp"
#include "tileindextype.hpp"
#include "tileproperty.hpp"
#include <vector>
TEST_CASE ("Check that 2D raytracing works", "[raytracing][geometry]") {
using curry::for_each_voxel_under_segment;
using curry::vec2us;
using curry::vec2f;
using curry::WorldTileProperty;
curry::WorldGrid world(vec2us(64));
world.set_layers(vec2us(10, 10), std::vector<std::vector<curry::TileIndex>>( {{
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>({ curry::TileProperty() }));
REQUIRE(world.world_size() == vec2us(10));
REQUIRE(world.layer_count() == 1);
{
std::vector<WorldTileProperty> diagonal;
for_each_voxel_under_segment (
vec2f(0.0f, 0.0f), //from
vec2f(640.0f, 640.0f), //to
vec2us(1, 1), //objsize
world, //world
[diagonal](const WorldTileProperty& wtp) mutable {diagonal.push_back(wtp); return true;}
);
const auto* tile_prop = &world.tile_property(world.tile(vec2us(0)));
std::vector<WorldTileProperty> expected {
WorldTileProperty{vec2us(0, 0), tile_prop},
WorldTileProperty{vec2us(1, 1), tile_prop},
WorldTileProperty{vec2us(2, 2), tile_prop},
WorldTileProperty{vec2us(3, 3), tile_prop},
WorldTileProperty{vec2us(4, 4), tile_prop},
WorldTileProperty{vec2us(5, 5), tile_prop},
WorldTileProperty{vec2us(6, 6), tile_prop},
WorldTileProperty{vec2us(7, 7), tile_prop},
WorldTileProperty{vec2us(8, 8), tile_prop},
WorldTileProperty{vec2us(9, 9), tile_prop}
};
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);
}
}