libcruft-util/test/geom/segment.cpp

80 lines
2.2 KiB
C++
Raw Normal View History

#include "tap.hpp"
#include "geom/segment.hpp"
#include "geom/ops.hpp"
#include "region.hpp"
///////////////////////////////////////////////////////////////////////////////
void test_point_distance (cruft::TAP::logger &tap)
{
static struct {
cruft::point3f a, b;
cruft::point3f q;
float distance;
const char *message;
} const TESTS[] = {
{ { 0, 0, 0 }, { 1, 1, 1 }, { 0, 0, 0 }, 0, "origin line, origin point" },
{ { 0, 0, 0 }, { 1, 1, 1 }, { 1, 1, 1 }, 0, "origin line, point on line" },
{ { 0, 1, 0 }, { 1, 1, 0 }, { 0, 0, 0 }, 1, "+x line, unit distance" },
};
for (auto const& t: TESTS) {
cruft::geom::segment3f s {t.a, t.b};
auto d = distance (s, t.q);
tap.expect_eq (d, t.distance, "%!", t.message);
}
}
//-----------------------------------------------------------------------------
void test_region_intersection (cruft::TAP::logger &tap)
{
using p = cruft::point2i;
static struct {
cruft::geom::segment2i a;
cruft::region2i b;
bool expected;
char const *message;
} const TESTS[] = {
{
.a = { p { 0, 0 }, p { 4, 4 }, },
.b = { p { 1, 1 }, p { 2, 2 }, },
.expected = true,
.message = "rising into"
},
{
.a = { p { 0, 0 }, p { 1, 1 } },
.b = { p { -2, -2 }, p { 2, 2 } },
.expected = true,
.message = "completely internal",
},
{
.a = { p { 0, 0 }, p { 1, 0 } },
.b = { p { 0, 0 }, p { 1, 1 } },
.expected = true,
.message = "glancing horizontal",
},
{
.a = { p { 0, 0 }, p { -1, -1 } },
.b = { p { 0, 0 }, p { 1, 1 } },
.expected = true,
.message = "common origin",
},
};
for (auto const &t: TESTS)
tap.expect_eq (intersects (t.a, t.b), t.expected, "region intersection: %!", t.message);
}
///////////////////////////////////////////////////////////////////////////////
int
main (int, char**)
{
cruft::TAP::logger tap;
test_point_distance (tap);
test_region_intersection (tap);
return tap.status ();
}