(C++) BoostGeometryExample15
February 24, 2017 · View on GitHub
(C++) BoostGeometryExample15



Boost.Geometry example 15: Obtain the intersection of a polygon and linestring is a Boost.Geometry example.
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
Qt project file: ./CppBoostGeometryExample15/CppBoostGeometryExample15.pro
exists(../../ConsoleApplication.pri) { include(../../ConsoleApplication.pri) } !exists(../../ConsoleApplication.pri) { QT += core QT += gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += console CONFIG -= app_bundle TEMPLATE = app CONFIG(release, debug|release) { DEFINES += NDEBUG NTRACE_BILDERBIKKEL } QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ unix { QMAKE_CXXFLAGS += -Werror } } exists(../../Libraries/Boost.pri) { include(../../Libraries/Boost.pri) } !exists(../../Libraries/Boost.pri) { win32 { INCLUDEPATH += \ ../../Libraries/boost_1_55_0 } } SOURCES += main.cpp
./CppBoostGeometryExample15/main.cpp
#include <cassert> #include <iostream> #include <vector> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #pragma GCC diagnostic ignored "-Wunused-variable" #include <boost/geometry.hpp> //#include <boost/geometry/algorithms/correct.hpp> //#include <boost/geometry/algorithms/equals.hpp> //#include <boost/geometry/algorithms/intersection.hpp> //#include <boost/geometry/geometries/linestring.hpp> #include <boost/geometry/geometries/point_xy.hpp> //#include <boost/geometry/geometries/polygon.hpp> //#include <boost/geometry/io/wkt/read.hpp> //#include <boost/geometry/io/wkt/write.hpp> #include <boost/geometry/io/wkt/wkt.hpp> #pragma GCC diagnostic pop /* + *---* + + | | | | | + | | + *---* + +-* | | A | | | B | | | + | | + + *---* = + +-* | | | | | + +---+ + + | | | +-+-+-+-+-+ +-+-+-+-+-+ +-+-+-+-+-+ */ int main() { typedef boost::geometry::model::d2::point_xy<double> Coordinat2D; typedef boost::geometry::model::linestring<Coordinat2D> Linestring; typedef boost::geometry::model::polygon<Coordinat2D> Polygon; typedef std::vector<Linestring> Linestrings; //Create polygon A Polygon a; boost::geometry::read_wkt( "POLYGON((4.0 4.0,4.0 1.0,2.0 1.0,2.0 4.0))", a ); boost::geometry::correct(a); //Create linestring B Linestring b; boost::geometry::read_wkt( "LINESTRING(1.0 3.0,3.0 3.0,3.0 2.0,1.0 2.0)", b ); boost::geometry::correct(b); //Create intersection Linestrings intersections; boost::geometry::intersection(a,b,intersections); assert(intersections.size() == 1); //Extract the only intersection const auto found = intersections[0]; Linestring expected; boost::geometry::read_wkt( "LINESTRING(2.0 3.0,3.0 3.0,3.0 2.0,2.0 2.0)", expected ); std::cout << "Found:" << '\n' << "As WKT: " << boost::geometry::wkt<decltype(found)>(found) << '\n' << "#points: " << found.size() << '\n' ; for (const auto point: found) { std::cout << '(' << boost::geometry::get<0>(point) << ',' << boost::geometry::get<1>(point) << ')' << '\n' ; } std::cout << "Expected:" << '\n' << "As WKT: " << boost::geometry::wkt<decltype(expected)>(expected) << '\n' << "#points: " << expected.size() << '\n'; for (const auto point: expected) { std::cout << '(' << boost::geometry::get<0>(point) << ',' << boost::geometry::get<1>(point) << ')' << '\n' ; } assert(found.size() == expected.size()); assert(boost::geometry::equals(found,expected)); } /* Screen output: Found: As WKT: LINESTRING(2 3,3 3,3 2,2 2) #points: 4 (2,3) (3,3) (3,2) (2,2) Expected: As WKT: LINESTRING(2 3,3 3,3 2,2 2) #points: 4 (2,3) (3,3) (3,2) (2,2) Press <RETURN> to close this window... */