2018-04-20 15:07:16 +10:00
|
|
|
#include "tap.hpp"
|
|
|
|
|
|
|
|
#include "geom/segment.hpp"
|
2019-03-21 16:48:40 +11:00
|
|
|
#include "geom/ops.hpp"
|
|
|
|
#include "region.hpp"
|
2018-04-20 15:07:16 +10:00
|
|
|
|
|
|
|
|
2019-03-21 16:26:00 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void test_point_distance (cruft::TAP::logger &tap)
|
|
|
|
{
|
2018-04-20 15:07:16 +10:00
|
|
|
static struct {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point3f a, b;
|
|
|
|
cruft::point3f q;
|
2018-04-20 15:07:16 +10:00
|
|
|
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) {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::geom::segment3f s {t.a, t.b};
|
2018-04-20 15:07:16 +10:00
|
|
|
auto d = distance (s, t.q);
|
|
|
|
tap.expect_eq (d, t.distance, "%!", t.message);
|
|
|
|
}
|
2019-03-21 16:26:00 +11:00
|
|
|
}
|
2018-04-20 15:07:16 +10:00
|
|
|
|
2019-03-21 16:26:00 +11:00
|
|
|
|
2019-03-21 16:48:40 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-22 15:11:01 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void test_bresenham (cruft::TAP::logger &tap)
|
|
|
|
{
|
|
|
|
static struct {
|
|
|
|
cruft::point2i a;
|
|
|
|
cruft::point2i b;
|
|
|
|
std::vector<cruft::point2i> expected;
|
|
|
|
char const *message;
|
|
|
|
} const TESTS[] = {
|
|
|
|
{
|
|
|
|
.a = { 0 },
|
|
|
|
.b = { 3 },
|
|
|
|
.expected = { { 0 }, { 1 }, { 2 }, { 3 } },
|
|
|
|
.message = "positive unit direction"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.a = { 1, 2 },
|
|
|
|
.b = { 1, 4 },
|
|
|
|
.expected = { { 1, 2 }, { 1, 3 }, { 1, 4 } },
|
|
|
|
.message = "vertical only"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.a = { -2, 1 },
|
|
|
|
.b = { 1, -1 },
|
|
|
|
.expected = { { -2, 1 }, { -1, 0 }, { 0, 0 }, { 1, -1 } },
|
|
|
|
.message = "cross origin"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.a = { 1, 1 },
|
|
|
|
.b = { 1, 1 },
|
|
|
|
.expected = { { 1, 1 } },
|
|
|
|
.message = "zero length segment"
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto const &t: TESTS) {
|
|
|
|
std::vector<cruft::point2i> computed;
|
2020-02-17 17:40:03 +11:00
|
|
|
for (auto const p: cruft::geom::bresenham ({ t.a, t.b }))
|
2019-03-22 15:11:01 +11:00
|
|
|
computed.push_back (p);
|
|
|
|
tap.expect_eq (computed, t.expected, "bresenham: %!", t.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 16:26:00 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
|
|
|
main (int, char**)
|
|
|
|
{
|
|
|
|
cruft::TAP::logger tap;
|
|
|
|
test_point_distance (tap);
|
2019-03-21 16:48:40 +11:00
|
|
|
test_region_intersection (tap);
|
2019-03-22 15:11:01 +11:00
|
|
|
test_bresenham (tap);
|
2018-04-20 15:07:16 +10:00
|
|
|
return tap.status ();
|
|
|
|
}
|