Skip to content

Instantly share code, notes, and snippets.

@ismailsunni
Created February 15, 2023 10:45
Show Gist options
  • Save ismailsunni/f1b1640e1e93d5e52384c287778f70a0 to your computer and use it in GitHub Desktop.
Save ismailsunni/f1b1640e1e93d5e52384c287778f70a0 to your computer and use it in GitHub Desktop.
Bug boost::geometry::within
#include <iostream>
#include <list>
#include <string>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::polygon<point_type> polygon_type;
typedef boost::geometry::model::linestring<point_type> linestring_t;
void printPoint(point_type point)
{
std::cout << "(" << point.x() << ", " << point.y() << ")";
}
void printLine(linestring_t line)
{
printPoint(line[0]);
std::cout << " - ";
printPoint(line[1]);
}
std::string booleanToString(bool b)
{
return (b ? "yes" : "no");
}
int main()
{
std::cout << "Using Boost "
<< BOOST_VERSION / 100000 << "." // major version
<< BOOST_VERSION / 100 % 1000 << "." // minor version
<< BOOST_VERSION % 100 // patch level
<< std::endl;
polygon_type poly;
boost::geometry::read_wkt(
"POLYGON((0.000754582 8.98312e-05, 0.000754582 -0.000538987, -0.000125764 -0.000538987, -0.000125764 8.98312e-05, 0.000754582, 8.98312e-05))",
poly);
point_type p1(0, 0);
point_type p2(0.000629, 0);
point_type p3(0.001258, 0);
point_type p4(0, -0.000449);
point_type p5(0.000629, -0.000449);
point_type p6(0.001258, -0.000449);
linestring_t FE{p6, p5};
linestring_t EF{p5, p6};
std::cout << "Checking reversed line" << std::endl;
std::cout << " is within: " << (boost::geometry::within(EF, poly) ? "yes" : "no") << std::endl;
std::cout << " is intersect: " << (boost::geometry::intersects(EF, poly) ? "yes" : "no") << std::endl;
std::cout << " is within: " << (boost::geometry::within(FE, poly) ? "yes" : "no") << std::endl;
std::cout << " is intersect: " << (boost::geometry::intersects(FE, poly) ? "yes" : "no") << std::endl
<< std::endl;
linestring_t l1{p1, p4};
linestring_t l2{p4, p5};
linestring_t l3{p2, p5};
linestring_t l4{p6, p5};
linestring_t l5{p3, p6};
linestring_t l6{p1, p2};
linestring_t l7{p2, p3};
linestring_t l8{p5, p2}; // Reverse of l3
// Within
std::vector<bool> within{
true,
true,
true,
false,
false,
true,
false,
true};
// Assume within -> intersects
std::vector<bool>
intersects{
true,
true,
true,
true,
false,
true,
true,
true};
std::vector<linestring_t> lines{l1, l2, l3, l4, l5, l6, l7, l8};
std::cout << "Comparing within and intersect result" << std::endl;
int i = 0;
for (auto line : lines)
{
std::cout << "line " << (i + 1) << ": ";
bool is_within = boost::geometry::within(line, poly);
bool is_intersect = boost::geometry::intersects(line, poly);
printLine(line);
std::cout << std::endl;
std::cout << "Boost:" << std::endl;
std::cout << " is within: " << booleanToString(is_within) << std::endl;
std::cout << " is intersect: " << booleanToString(is_intersect) << std::endl;
std::cout << "Expected:" << std::endl;
std::cout << " is within: " << booleanToString(within.at(i)) << std::endl;
std::cout << " is intersect: " << booleanToString(intersects.at(i)) << std::endl;
if (is_within != within.at(i))
{
std::cout << "Within is wrong, should be " << within.at(i) << " got " << is_within << std::endl;
}
if (is_intersect != intersects.at(i))
{
std::cout << "Intersect is wrong , should be " << intersects.at(i) << " got " << is_intersect << std::endl;
}
std::cout << std::endl;
i++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment