2015-04-13 16:45:56 +10:00
|
|
|
#include "region.hpp"
|
|
|
|
|
|
|
|
#include "point.hpp"
|
|
|
|
#include "tap.hpp"
|
2012-05-11 12:21:47 +10:00
|
|
|
|
2017-08-21 18:48:52 +10:00
|
|
|
#include <vector>
|
2012-05-11 12:21:47 +10:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-11 12:21:47 +10:00
|
|
|
int
|
2016-01-19 18:31:49 +11:00
|
|
|
main (int, char **)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2016-01-19 18:31:49 +11:00
|
|
|
|
2017-07-19 17:19:20 +10:00
|
|
|
// check that two overlapping regions successfully test for intersection
|
2012-05-11 12:21:47 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::point2d ap { 32.7, -6.09703 };
|
|
|
|
const cruft::extent2d ae { 0.8, 2. };
|
|
|
|
const cruft::region2d a (ap, ae);
|
2015-03-06 01:09:37 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::point2d bp {33.5, -4.5};
|
|
|
|
const cruft::extent2d be { 0.5, 0.5 };
|
|
|
|
const cruft::region2d b (bp, be);
|
2012-05-11 12:21:47 +10:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect (!a.intersects (b), "simple 2d intersection");
|
2012-05-11 12:21:47 +10:00
|
|
|
}
|
|
|
|
|
2017-07-19 17:19:20 +10:00
|
|
|
// check various floating point maximums successfully test for
|
|
|
|
// intersection. the concern is that infinities are incorrectly handled
|
|
|
|
// in some comparisons.
|
|
|
|
tap.expect (
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region2d::max ().intersects (
|
|
|
|
cruft::region2d::unit ()
|
2017-07-19 17:19:20 +10:00
|
|
|
),
|
|
|
|
"maximal region2d intersection"
|
|
|
|
);
|
|
|
|
|
|
|
|
tap.expect (
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region2f::max ().intersects (
|
|
|
|
cruft::region2f::unit ()
|
2017-07-19 17:19:20 +10:00
|
|
|
),
|
|
|
|
"maximal region2f intersection"
|
|
|
|
);
|
2012-06-15 16:38:57 +10:00
|
|
|
|
2017-07-19 17:19:20 +10:00
|
|
|
// ensure unit regions are... unit sized...
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::region2d::unit ().area (), 1.0, "unit region2d area");
|
|
|
|
tap.expect_eq (cruft::region2f::unit ().area (), 1.0f, "unit region2f area");
|
2012-06-15 16:38:57 +10:00
|
|
|
|
2017-07-19 17:19:20 +10:00
|
|
|
// test boundary cases of includes and contains
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::point2f p0 { 0 };
|
|
|
|
const cruft::extent2f e0 { 2 };
|
|
|
|
const cruft::region2f r {p0, e0};
|
2015-03-06 01:09:37 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (!r.inclusive (cruft::point2f {-1, 1}), "region/point inclusive, invalid x");
|
|
|
|
tap.expect (!r.inclusive (cruft::point2f { 1, 3}), "region/point inclusive, invalid y ");
|
2012-06-08 16:48:33 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (r.inclusive (cruft::point2f {1, 1}), "region/point inclusive, centre");
|
|
|
|
tap.expect (r.inclusive (cruft::point2f {0, 0}), "region/point inclusive, base");
|
|
|
|
tap.expect (r.inclusive (cruft::point2f {2, 2}), "region/point inclusive, corner");
|
2017-07-19 17:19:20 +10:00
|
|
|
}
|
2012-06-08 16:48:33 +10:00
|
|
|
|
2018-06-14 15:07:25 +10:00
|
|
|
// ensure union operator behaves as expected
|
2017-07-31 15:42:05 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::point2f p { -1 };
|
|
|
|
const cruft::extent2f e { 2 };
|
|
|
|
const cruft::region2f r { p, e };
|
2017-07-31 15:42:05 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (r | cruft::point2f { 0, 0 }, r, "identity union");
|
2017-07-31 15:42:05 +10:00
|
|
|
tap.expect_eq (
|
2018-08-05 14:42:02 +10:00
|
|
|
r | cruft::point2f { 2, 3 },
|
|
|
|
cruft::region2f { p, cruft::extent2f { 3, 4 } },
|
2017-07-31 15:42:05 +10:00
|
|
|
"positive expanding union"
|
|
|
|
);
|
|
|
|
tap.expect_eq (
|
2018-08-05 14:42:02 +10:00
|
|
|
r | cruft::point2f { -3, -2 },
|
|
|
|
cruft::region2f {
|
|
|
|
cruft::point2f { -3, -2 },
|
|
|
|
cruft::extent2f { 4, 3 }
|
2017-07-31 15:42:05 +10:00
|
|
|
},
|
|
|
|
"negative expanding union"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-08-21 18:48:52 +10:00
|
|
|
// ensure make_region covers the expected values
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::region2i REGION {
|
2019-03-08 09:38:13 +11:00
|
|
|
cruft::point2i { 0, 0 },
|
|
|
|
cruft::point2i { 3, 2 }
|
2017-08-21 18:48:52 +10:00
|
|
|
};
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::point2i EXPECTED[] = {
|
2019-03-08 09:38:13 +11:00
|
|
|
{ 0, 0 }, { 1, 0 }, { 2, 0 },
|
|
|
|
{ 0, 1 }, { 1, 1 }, { 2, 1 },
|
2017-08-21 18:48:52 +10:00
|
|
|
};
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
std::vector<cruft::point2i> values;
|
2018-03-23 16:38:54 +11:00
|
|
|
std::copy (std::cbegin (REGION.step ()), std::cend (REGION.step ()), std::back_inserter (values));
|
2017-08-21 18:48:52 +10:00
|
|
|
|
|
|
|
bool success = values.size () == std::size (EXPECTED)
|
|
|
|
&& std::equal (std::cbegin (values), std::cend (values),
|
|
|
|
std::cbegin (EXPECTED), std::cend (EXPECTED));
|
|
|
|
tap.expect (success, "make_range(region2i)");
|
|
|
|
};
|
|
|
|
|
2019-08-30 13:45:08 +10:00
|
|
|
// test distance2 for points
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
cruft::region2f r;
|
|
|
|
cruft::point2f p;
|
|
|
|
float distance2;
|
|
|
|
char const *message;
|
|
|
|
} const TESTS[] = {
|
|
|
|
{
|
|
|
|
{ cruft::point2f { 1, 1 }, cruft::point2f { 3, 5 } },
|
|
|
|
{ 0, 0 },
|
|
|
|
2.f,
|
|
|
|
"origin to offset rect"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{ cruft::point2f { 1, 1 }, cruft::point2f { 3, 5 } },
|
|
|
|
{ 2, 2 },
|
|
|
|
0.f,
|
|
|
|
"point inside region"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{ cruft::point2f { 1, 1 }, cruft::point2f { 3, 5 } },
|
|
|
|
{ 4, 7 },
|
|
|
|
1 + 4,
|
|
|
|
"offset point to offset rect"
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto const &t: TESTS) {
|
|
|
|
auto const d2 = distance2 (t.r, t.p);
|
|
|
|
tap.expect_eq (d2, t.distance2, "region-point distance2: %!", t.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
return tap.status ();
|
2012-05-11 12:21:47 +10:00
|
|
|
}
|