### Include Boost.Geometry headers and use namespace Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/quickstart Basic setup for using Boost.Geometry library. Include the main header and geometry-specific headers for point and polygon types, then use the boost::geometry namespace. Boost.Geometry is header-only, requiring no library linking. ```cpp #include #include #include using namespace boost::geometry; ``` -------------------------------- ### Boost.Geometry R-tree Setup and Includes (C++) Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/spatial_indexes/rtree_quickstart This snippet includes the necessary Boost.Geometry headers for R-tree operations, defining common namespaces and data structures. It sets up the foundation for spatial indexing by including point, box, and R-tree specific headers, along with utilities for output and storing query results. ```cpp #include #include #include #include // to store queries results #include // just for output #include #include namespace bg = boost::geometry; namespace bgi = boost::geometry::index; ``` -------------------------------- ### Register Boost.Tuple as point type Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/quickstart Register Boost.Tuple coordinate system with Boost.Geometry to use tuples as point objects. Enables seamless integration of Boost.Tuple with geometry algorithms. ```cpp #include BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian) ``` -------------------------------- ### Boost.Geometry R-tree Data Insertion (C++) Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/spatial_indexes/rtree_quickstart Populates the R-tree by creating and inserting several box-value pairs. This example illustrates the use of the `insert()` method and the creation of box objects, assuming `geometry::envelope()` might be used in more complex scenarios. It demonstrates a basic loop for generating and adding data. ```cpp // create some values for ( unsigned i = 0 ; i < 10 ; ++i ) { // create a box box b(point(i + 0.0f, i + 0.0f), point(i + 0.5f, i + 0.5f)); // insert new value rtree.insert(std::make_pair(b, i)); } ``` -------------------------------- ### Boost Geometry transform example with translation, scaling, and rotation Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/algorithms/transform/transform_3_with_strategy An example demonstrating the use of Boost.Geometry's transform algorithm with various strategies including translation, scaling, and rotation. It shows how to transform points and print the results. ```cpp #include #include int main() { namespace trans = boost::geometry::strategy::transform; using boost::geometry::dsv; using point_type = boost::geometry::model::point; point_type p1(1.0, 1.0); // Translate over (1.5, 1.5) point_type p2; trans::translate_transformer translate(1.5, 1.5); boost::geometry::transform(p1, p2, translate); // Scale with factor 3.0 point_type p3; trans::scale_transformer scale(3.0); boost::geometry::transform(p1, p3, scale); // Rotate with respect to the origin (0,0) over 90 degrees (clockwise) point_type p4; trans::rotate_transformer rotate(90.0); boost::geometry::transform(p1, p4, rotate); std::cout << "p1: " << dsv(p1) << std::endl << "p2: " << dsv(p2) << std::endl << "p3: " << dsv(p3) << std::endl << "p4: " << dsv(p4) << std::endl; return 0; } ``` -------------------------------- ### Boost.Geometry box_view Example Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/views/box_view Demonstrates the usage of boost::geometry::box_view to iterate over the corners of a box and calculate its area. This example requires the Boost.Geometry library and shows how to adapt a box to the Boost.Range concept. ```cpp #include #include int main() { using box_type = boost::geometry::model::box < boost::geometry::model::point >; // Define the Boost.Range compatible type: using box_view = boost::geometry::box_view; box_type box; boost::geometry::assign_values(box, 0, 0, 4, 4); box_view view(box); // Iterating in clockwise direction over the points of this box for (boost::range_iterator::type it = boost::begin(view); it != boost::end(view); ++it) { std::cout << " " << boost::geometry::dsv(*it); } std::cout << std::endl; // Note that a box_view is tagged as a ring, so supports area etc. std::cout << "Area: " << boost::geometry::area(view) << std::endl; return 0; } ``` -------------------------------- ### Boost.Geometry model::linestring Example Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/models/model_linestring An example demonstrating the declaration and use of Boost.Geometry's model::linestring, including creating linestrings, appending points, and calculating the length. It utilizes C++11 initializer list syntax and the bg::append and bg::length functions. ```cpp #include #include #include namespace bg = boost::geometry; int main() { using point_t = bg::model::point; using linestring_t = bg::model::linestring; linestring_t ls1; linestring_t ls2{{0.0, 0.0}, {1.0, 0.0}, {1.0, 2.0}}; bg::append(ls1, point_t(0.0, 0.0)); bg::append(ls1, point_t(1.0, 0.0)); bg::append(ls1, point_t(1.0, 2.0)); double l = bg::length(ls1); std::cout << l << std::endl; return 0; } ``` -------------------------------- ### Boost.Geometry R-tree K-Nearest Neighbor Query (C++) Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/spatial_indexes/rtree_quickstart Executes a k-nearest neighbor (k-nn) query to find a specified number of values closest to a given point. This example uses `bgi::nearest()` to find the 5 nearest values to the point (0, 0) and stores them in a vector. The order of results is not guaranteed. ```cpp // find 5 nearest values to a point std::vector result_n; rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n)); ``` -------------------------------- ### Boost.Geometry model::ring Example in C++ Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/models/model_ring This C++ example demonstrates the declaration and usage of `boost::geometry::model::ring`. It shows how to create rings using both default and initializer list constructors, append points using `bg::append`, calculate the area using `bg::area`, and print the result. It requires including the necessary Boost.Geometry headers. ```cpp #include #include #include namespace bg = boost::geometry; int main() { using point_t = bg::model::point; using ring_t = bg::model::ring; ring_t ring1; ring_t ring2{{0.0, 0.0}, {0.0, 5.0}, {5.0, 5.0}, {5.0, 0.0}, {0.0, 0.0}}; bg::append(ring1, point_t(0.0, 0.0)); bg::append(ring1, point_t(0.0, 5.0)); bg::append(ring1, point_t(5.0, 5.0)); bg::append(ring1, point_t(5.0, 0.0)); bg::append(ring1, point_t(0.0, 0.0)); double a = bg::area(ring1); std::cout << a << std::endl; return 0; } ``` -------------------------------- ### Set Point Coordinates Example - Boost.Geometry C++ Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/access/set/set_2 Practical example demonstrating how to create a 2D point and set its x and y coordinates using the set function. The example creates a point_xy object, sets dimension 0 (x-coordinate) to 1 and dimension 1 (y-coordinate) to 2, then outputs the point using the dsv function. Requires boost/geometry headers and the boost::geometry namespace. ```cpp #include #include #include namespace bg = boost::geometry; int main() { bg::model::d2::point_xy point; bg::set<0>(point, 1); bg::set<1>(point, 2); std::cout << "Location: " << bg::dsv(point) << std::endl; return 0; } ``` -------------------------------- ### Boost.Geometry Tag Dispatching Example Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/core/tag Demonstrates the tag dispatching mechanism in Boost.Geometry. This C++ example shows how to use the 'tag' metafunction to apply different logic based on the geometry type (point, polygon, multipolygon). It utilizes custom 'dispatch' structs and free functions like 'get' and 'dsv'. ```cpp #include #include #include #include #include BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian) template struct dispatch {}; // Specialization for points template <> struct dispatch { template static inline void apply(Point const& p) { // Use the Boost.Geometry free function "get" // working on all supported point types std::cout << "Hello POINT, you are located at: " << boost::geometry::get<0>(p) << ", " << boost::geometry::get<1>(p) << std::endl; } }; // Specialization for polygons template <> struct dispatch { template static inline void apply(Polygon const& p) { // Use the Boost.Geometry manipulator "dsv" // working on all supported geometries std::cout << "Hello POLYGON, you look like: " << boost::geometry::dsv(p) << std::endl; } }; // Specialization for multipolygons template <> struct dispatch { template static inline void apply(MultiPolygon const& m) { // Use the Boost.Range free function "size" because all // multigeometries comply to Boost.Range std::cout << "Hello MULTIPOLYGON, you contain: " << boost::size(m) << " polygon(s)" << std::endl; } }; template inline void hello(Geometry const& geometry) { // Call the metafunction "tag" to dispatch, and call method (here "apply") dispatch < typename boost::geometry::tag::type >::apply(geometry); } int main() { // Define polygon type (here: based on a Boost.Tuple) using polygon_type = boost::geometry::model::polygon>; // Declare and fill a polygon and a multipolygon polygon_type poly; boost::geometry::exterior_ring(poly) = {{0, 0}, {0, 10}, {10, 5}, {0, 0}}; boost::geometry::model::multi_polygon multi; multi.push_back(poly); // Call "hello" for point, polygon, multipolygon hello(boost::make_tuple(2, 3)); hello(poly); hello(multi); return 0; } ``` -------------------------------- ### Boost.Geometry model::multi_point Usage Example Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/models/model_multi_point A complete C++ example showcasing the declaration and use of `boost::geometry::model::multi_point`. It demonstrates creating multi-points using default and initializer list constructors, appending individual points, and retrieving the total number of points. This code requires the Boost.Geometry library. ```cpp #include #include #include namespace bg = boost::geometry; int main() { using point_t = bg::model::point; using mpoint_t = bg::model::multi_point; mpoint_t mpt1; mpoint_t mpt2{{{0.0, 0.0}, {1.0, 1.0}, {2.0, 2.0}}}; bg::append(mpt1, point_t(0.0, 0.0)); bg::append(mpt1, point_t(1.0, 1.0)); bg::append(mpt1, point_t(2.0, 2.0)); std::size_t count = bg::num_points(mpt1); std::cout << count << std::endl; return 0; } ``` -------------------------------- ### Boost.Geometry R-tree Quick Start Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/spatial_indexes/rtree_examples/quick_start Demonstrates the creation and usage of a Boost.Geometry R-tree for spatial indexing. It includes inserting boxes, performing spatial intersection queries, and nearest neighbor queries. This snippet requires Boost.Geometry and standard C++ libraries. ```cpp #include #include #include #include #include #include #include namespace bg = boost::geometry; namespace bgi = boost::geometry::index; int main() { typedef bg::model::point point; typedef bg::model::box box; typedef std::pair value; bgi::rtree< value, bgi::quadratic<16> > rtree; for ( unsigned i = 0 ; i < 10 ; ++i ) { box b(point(i + 0.0f, i + 0.0f), point(i + 0.5f, i + 0.5f)); rtree.insert(std::make_pair(b, i)); } box query_box(point(0, 0), point(5, 5)); std::vector result_s; rtree.query(bgi::intersects(query_box), std::back_inserter(result_s)); std::vector result_n; rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n)); std::cout << "spatial query box:" << std::endl; std::cout << bg::wkt(query_box) << std::endl; std::cout << "spatial query result:" << std::endl; BOOST_FOREACH(value const& v, result_s) std::cout << bg::wkt(v.first) << " - " << v.second << std::endl; std::cout << "knn query point:" << std::endl; std::cout << bg::wkt(point(0, 0)) << std::endl; std::cout << "knn query result:" << std::endl; BOOST_FOREACH(value const& v, result_n) std::cout << bg::wkt(v.first) << " - " << v.second << std::endl; return 0; } ``` -------------------------------- ### Boost.Range Sliced Adaptor Example with Linestring (C++) Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/adapted/boost_range/sliced Demonstrates how to use the Boost.Range sliced adaptor to create a sub-range (slice) of a Boost.Geometry linestring. This example includes necessary headers, defines a linestring, applies the sliced adaptor with specified start and end indices, and prints the resulting slice. ```cpp #include #include #include #include #include int main() { using xy = boost::geometry::model::d2::point_xy; boost::geometry::model::linestring line = {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}}; std::cout << boost::geometry::dsv(line | boost::adaptors::sliced(1, 3)) << std::endl; return 0; } ``` -------------------------------- ### Boost.Geometry model::point Declaration and Usage Example Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/models/model_point This C++ example demonstrates the declaration and basic usage of the Boost.Geometry model::point class. It shows how to create points with different coordinate types and dimensions, set individual coordinates using both generic and class-specific methods, and retrieve coordinates. ```cpp #include #include namespace bg = boost::geometry; int main() { bg::model::point point1; bg::model::point point2(1.0, 2.0, 3.0); bg::set<0>(point1, 1.0); point1.set<1>(2.0); double x = bg::get<0>(point1); double y = point1.get<1>(); std::cout << x << ", " << y << std::endl; return 0; } ``` -------------------------------- ### Boost.Geometry Index Query Examples Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/spatial_indexes/group__rtree__functions/query_rtree______const____predicates_const____outiter_ Examples demonstrating various ways to use the `query` function with different predicate combinations in Boost.Geometry. These include spatial queries, custom satisfies predicates, and nearest neighbor searches. ```cpp // return elements intersecting box bgi::query(tree, bgi::intersects(box), std::back_inserter(result)); // return elements intersecting poly but not within box bgi::query(tree, bgi::intersects(poly) && !bgi::within(box), std::back_inserter(result)); // return elements overlapping box and meeting my_fun value predicate bgi::query(tree, bgi::overlaps(box) && bgi::satisfies(my_fun), std::back_inserter(result)); // return 5 elements nearest to pt and elements are intersecting box bgi::query(tree, bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result)); // For each found value do_something (it is a type of function object) tree.query(bgi::intersects(box), boost::make_function_output_iterator(do_something())); ``` -------------------------------- ### Boost.Geometry Index Query Examples Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/spatial_indexes/boost__geometry__index__rtree/query_predicates_const____outiter_ Code examples demonstrating the usage of the Boost.Geometry index `query` function with various predicates, including spatial, satisfies, and nearest neighbor searches. It also shows how to connect multiple predicates and use different output iterator types like `std::back_inserter` and custom function output iterators. ```cpp // return elements intersecting box tree.query(bgi::intersects(box), std::back_inserter(result)); // return elements intersecting poly but not within box tree.query(bgi::intersects(poly) && !bgi::within(box), std::back_inserter(result)); // return elements overlapping box and meeting my_fun unary predicate tree.query(bgi::overlaps(box) && bgi::satisfies(my_fun), std::back_inserter(result)); // return 5 elements nearest to pt and elements are intersecting box tree.query(bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result)); // For each found value do_something (it is a type of function object) tree.query(bgi::intersects(box), boost::make_function_output_iterator(do_something())); // For each value stored in the rtree do_something // always_true is a type of function object always returning true tree.query(bgi::satisfies(always_true()), boost::make_function_output_iterator(do_something())); // C++11 (lambda expression) tree.query(bgi::intersects(box), boost::make_function_output_iterator([](value_type const& val){ // do something })); // C++14 (generic lambda expression) tree.query(bgi::intersects(box), boost::make_function_output_iterator([](auto const& val){ // do something })); ``` -------------------------------- ### Boost.Geometry point_square Buffer Strategy Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/strategies/strategy_buffer_point_square This example demonstrates how to use the `strategy::buffer::point_square` as a `PointStrategy` for the `buffer` algorithm in Boost.Geometry. It shows the creation of square buffers around a multipoint geometry using various buffer strategies (distance, side, join, end) along with the point_square strategy. The code requires Boost.Geometry headers for geometries and strategies. ```cpp #include #include #include int main() { using point = boost::geometry::model::d2::point_xy; using polygon = boost::geometry::model::polygon; // Declare the point_square strategy boost::geometry::strategy::buffer::point_square point_strategy; // Declare other strategies boost::geometry::strategy::buffer::distance_symmetric distance_strategy(0.5); boost::geometry::strategy::buffer::join_round join_strategy; boost::geometry::strategy::buffer::end_round end_strategy; boost::geometry::strategy::buffer::side_straight side_strategy; // Declare/fill of a multi point boost::geometry::model::multi_point mp; boost::geometry::read_wkt("MULTIPOINT((3 3),(3 4),(4 4),(7 3))", mp); // Create the buffer of a multi point boost::geometry::model::multi_polygon result; boost::geometry::buffer(mp, result, distance_strategy, side_strategy, join_strategy, end_strategy, point_strategy); return 0; } ``` -------------------------------- ### Register and use C arrays as points Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/quickstart Register C array coordinate system with Boost.Geometry to use plain C arrays as point types. After registration, C arrays can be used directly with Boost.Geometry algorithms like distance calculation. ```cpp #include BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian) ``` ```cpp int a[2] = {1,1}; int b[2] = {2,3}; double d = distance(a, b); std::cout << "Distance a-b is: " << d << std::endl; ``` -------------------------------- ### Boost.Geometry R-tree Initialization (C++) Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/spatial_indexes/rtree_quickstart Initializes an R-tree using the quadratic insertion algorithm with a maximum of 16 elements per node. This demonstrates how to configure the R-tree with specific parameters for performance tuning, such as choosing an appropriate algorithm and node capacity. ```cpp // create the rtree using default constructor bgi::rtree< value, bgi::quadratic<16> > rtree; ``` -------------------------------- ### Box Construction and Manipulation Example Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/models/model_box Demonstrates the declaration and usage of the Boost.Geometry model::box. It covers default construction, construction with corner points (including C++11 initialization), setting individual coordinates using both generic and class-specific methods, and retrieving coordinates. ```cpp #include #include namespace bg = boost::geometry; int main() { using point_t = bg::model::point; using box_t = bg::model::box; box_t box1; box_t box2(point_t(0.0, 0.0), point_t(5.0, 5.0)); box_t box3{{0.0, 0.0}, {5.0, 5.0}}; bg::set(box1, 1.0); bg::set(box1, 2.0); box1.max_corner().set<0>(3.0); box1.max_corner().set<1>(4.0); double min_x = bg::get(box1); double min_y = bg::get(box1); double max_x = box1.max_corner().get<0>(); double max_y = box1.max_corner().get<1>(); std::cout << min_x << ", " << min_y << ", " << max_x << ", " << max_y << std::endl; return 0; } ``` -------------------------------- ### Boost.Geometry point_order Example: Polygon Winding Order Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/core/point_order An example demonstrating how to use the Boost.Geometry point_order metafunction to examine the winding order of a polygon. This code includes necessary headers for geometry types and prints the order value along with constants for clockwise and counterclockwise. ```cpp #include #include #include #include int main() { using point_type = boost::geometry::model::d2::point_xy; using polygon_type = boost::geometry::model::polygon; boost::geometry::order_selector order = boost::geometry::point_order::value; std::cout << "order: " << order << std::endl << "(clockwise = " << boost::geometry::clockwise << ", counterclockwise = " << boost::geometry::counterclockwise << ") " << std::endl; return 0; } ``` -------------------------------- ### Boost Geometry equals Example with Polygon and Box Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/algorithms/equals/equals_3_with_strategy Complete example demonstrating the equals function comparing two polygons and a polygon with a box. Creates polygon and box geometries using boost tuples, registers the cartesian coordinate system, and outputs whether geometries are spatially equal. Shows practical usage with multiple geometry type combinations. ```cpp #include #include #include #include BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian) int main() { using point = boost::tuple; boost::geometry::model::polygon poly1, poly2; boost::geometry::exterior_ring(poly1) = {{0, 0}, {0, 5}, {5, 5}, {5, 0}, {0, 0}}; boost::geometry::exterior_ring(poly2) = {{5, 0}, {0, 0}, {0, 5}, {5, 5}, {5, 0}}; std::cout << "polygons are spatially " << (boost::geometry::equals(poly1, poly2) ? "equal" : "not equal") << std::endl; boost::geometry::model::box box; boost::geometry::assign_values(box, 0, 0, 5, 5); std::cout << "polygon and box are spatially " << (boost::geometry::equals(box, poly2) ? "equal" : "not equal") << std::endl; return 0; } ``` -------------------------------- ### Boost Geometry strategy::distance::cross_track Constructor Examples Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/strategies/strategy_distance_cross_track Examples of constructors for the strategy::distance::cross_track class. The default constructor is shown, along with constructors that accept a radius type and a specific strategy instance. ```cpp cross_track() cross_track(typename Strategy::radius_type const & r) cross_track(Strategy const & s) ``` -------------------------------- ### Example: Register Legacy Point and Calculate Distance Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/adapted/register/boost_geometry_register_point_2d Complete example demonstrating how to register a custom legacy_point struct with Boost.Geometry and use library functions like assign_values and distance. The example shows defining a point structure, registering it with the macro, assigning values, and calculating the distance between two points. ```C++ #include #include #include struct legacy_point { double x, y; }; BOOST_GEOMETRY_REGISTER_POINT_2D(legacy_point, double, cs::cartesian, x, y) int main() { legacy_point p1, p2; namespace bg = boost::geometry; bg::assign_values(p1, 1, 1); bg::assign_values(p2, 2, 2); double d = bg::distance(p1, p2); std::cout << "Distance: " << d << std::endl; return 0; } ``` -------------------------------- ### Perform k-NN Queries with Boost.Geometry.Index (C++) Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/spatial_indexes/queries Illustrates how to perform k-Nearest Neighbors (k-NN) queries using the `bgi::nearest` predicate. Examples show querying for the `k` closest values to a Point, Box, and Segment. For non-point indexables, `bg::comparable_distance` is used. ```cpp std::vector returned_values; Point pt(/*...*/); rt.query(bgi::nearest(pt, k), std::back_inserter(returned_values)); Box box(/*...*/); rt.query(bgi::nearest(box, k), std::back_inserter(returned_values)); Segment seg(/*...*/); rt.query(bgi::nearest(seg, k), std::back_inserter(returned_values)); ``` -------------------------------- ### Boost.Geometry segment_view Example Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/views/segment_view Demonstrates the usage of segment_view to iterate over the points of a segment and calculate its length. It adapts a segment to the Boost.Range concept, enabling linestring operations. ```cpp #include #include int main() { using segment_type = boost::geometry::model::segment < boost::geometry::model::point >; using segment_view = boost::geometry::segment_view; segment_type segment; boost::geometry::assign_values(segment, 0, 0, 1, 1); segment_view view(segment); // Iterating over the points of this segment for (boost::range_iterator::type it = boost::begin(view); it != boost::end(view); ++it) { std::cout << " " << boost::geometry::dsv(*it); } std::cout << std::endl; // Note that a segment_view is tagged as a linestring, so supports length etc. std::cout << "Length: " << boost::geometry::length(view) << std::endl; return 0; } ``` -------------------------------- ### Create R-tree with Range Adaptors in Boost.Geometry C++ Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/spatial_indexes/rtree_examples/range_adaptors Complete example demonstrating how to construct a Boost.Geometry R-tree spatial index using Boost.Range adaptors to transform a vector of boxes. The indexed adaptor assigns indices to elements, while the transformed adaptor applies a function object to convert elements into key-value pairs. The example creates 10 boxes, builds an R-tree, and queries it using the first box as a search criterion. ```cpp #include #include #include #include #include #include #include #include #include namespace bg = boost::geometry; namespace bgi = boost::geometry::index; template struct pair_maker { typedef std::pair result_type; template inline result_type operator()(T const& v) const { return result_type(v.value(), v.index()); } }; int main() { typedef bg::model::point point; typedef bg::model::box box; typedef std::vector container; typedef container::size_type size_type; typedef std::pair value; container boxes; for ( size_type i = 0 ; i < 10 ; ++i ) { box b(point(i + 0.0f, i + 0.0f), point(i + 0.5f, i + 0.5f)); boxes.push_back(b); } bgi::rtree< value, bgi::quadratic<16> > rtree(boxes | boost::adaptors::indexed() | boost::adaptors::transformed(pair_maker())); std::cout << rtree.count(boxes[0]) << std::endl; return 0; } ``` -------------------------------- ### Boost.Geometry join_round Strategy Example Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/strategies/strategy_buffer_join_round Demonstrates how to use the join_round strategy with the buffer algorithm to create buffered geometries featuring rounded corners. This involves setting up various buffer strategies, defining geometry objects, and then applying the buffer operation. ```cpp #include #include #include int main() { using point = boost::geometry::model::d2::point_xy; using polygon = boost::geometry::model::polygon; // Declare the join_round strategy with 72 points for a full circle boost::geometry::strategy::buffer::join_round join_strategy(72); // Declare other strategies boost::geometry::strategy::buffer::distance_symmetric distance_strategy(1.0); boost::geometry::strategy::buffer::end_flat end_strategy; boost::geometry::strategy::buffer::side_straight side_strategy; boost::geometry::strategy::buffer::point_circle point_strategy; // Declare/fill a multi polygon boost::geometry::model::multi_polygon mp; boost::geometry::read_wkt("MULTIPOLYGON(((5 5,7 8,9 5,5 5)),((8 7,8 10,11 10,11 7,8 7)))", mp); // Create the buffered geometry with rounded corners boost::geometry::model::multi_polygon result; boost::geometry::buffer(mp, result, distance_strategy, side_strategy, join_strategy, end_strategy, point_strategy); return 0; } ``` -------------------------------- ### Calculate polygon area Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/quickstart Compute the area of a polygon using Boost.Geometry's area function. Works with any registered polygon type and coordinate system. ```cpp std::cout << "Area: " << area(poly) << std::endl; ``` -------------------------------- ### Calculate distance between mixed point types Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/quickstart Demonstrate Boost.Geometry's flexibility by calculating distance between points of different types (C array and Boost.Tuple). The template library automatically handles type conversions. ```cpp double d2 = distance(a, p); std::cout << "Distance a-p is: " << d2 << std::endl; ``` -------------------------------- ### Define and use spherical coordinate points Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/quickstart Create points in spherical coordinate system (with equatorial and degree units) and calculate great-circle distance between them. Demonstrates non-Cartesian geometry support where the library automatically applies spherical distance calculations. ```cpp using spherical_point = boost::geometry::model::point < double, 2, boost::geometry::cs::spherical_equatorial >; spherical_point amsterdam(4.90, 52.37); spherical_point paris(2.35, 48.86); double const earth_radius = 3959; // miles std::cout << "Distance in miles: " << distance(amsterdam, paris) * earth_radius << std::endl; ``` -------------------------------- ### Check point-in-polygon using within algorithm Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/quickstart Determine whether a point (Boost.Tuple) is located within a polygon. Demonstrates the `within` function which performs point-in-polygon testing by comparing the point against polygon boundaries defined by C array coordinates. ```cpp double points[][2] = {{2.0, 1.3}, {4.1, 3.0}, {5.3, 2.6}, {2.9, 0.7}, {2.0, 1.3}}; model::polygon> poly; append(poly, points); boost::tuple p = boost::make_tuple(3.7, 2.0); std::cout << "Point p is in polygon? " << std::boolalpha << within(p, poly) << std::endl; ``` -------------------------------- ### Calculate distance between two Cartesian points Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/quickstart Compute the Euclidean distance between two 2D points using Boost.Geometry's point_xy type. The distance function automatically applies the appropriate distance formula based on the coordinate system. ```cpp model::d2::point_xy p1(1, 1), p2(2, 2); std::cout << "Distance p1-p2 is: " << distance(p1, p2) << std::endl; ``` -------------------------------- ### Initialize and Use Boost.Geometry Polyhedral Surface (C++) Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/models/model_polyhedral_surface This C++ code snippet demonstrates how to declare and use the Boost.Geometry model::polyhedral_surface. It covers initialization via default constructor, list initialization, manual appending of points, and reading from Well-Known Text (WKT) format. The example also shows how to output the polyhedral surface in WKT format. ```cpp #include #include #include namespace bg = boost::geometry; int main() { using point_t = bg::model::point; using polygon_t = bg::model::polygon; using polyhedral_t = bg::model::polyhedral_surface; polyhedral_t polyhedral0; polyhedral_t polyhedral1 = {{{{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, {0, 0, 0}}}, {{{0, 0, 0}, {0, 1, 0}, {0, 1, 1}, {0, 0, 1}, {0, 0, 0}}}, {{{0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1}, {0, 0, 0}}}, {{{1, 1, 1}, {1, 0, 1}, {0, 0, 1}, {0, 1, 1}, {1, 1, 1}}}, {{{1, 1, 1}, {1, 0, 1}, {1, 0, 0}, {1, 1, 0}, {1, 1, 1}}}, {{{1, 1, 1}, {1, 1, 0}, {0, 1, 0}, {0, 1, 1}, {1, 1, 1}}}}; polyhedral0.resize(4); bg::append(polyhedral0[0].outer(), point_t(0.0, 0.0, 0.0)); bg::append(polyhedral0[0].outer(), point_t(5.0, 0.0, 0.0)); bg::append(polyhedral0[0].outer(), point_t(0.0, 5.0, 0.0)); bg::append(polyhedral0[1].outer(), point_t(0.0, 0.0, 0.0)); bg::append(polyhedral0[1].outer(), point_t(0.0, 5.0, 0.0)); bg::append(polyhedral0[1].outer(), point_t(0.0, 0.0, 5.0)); bg::append(polyhedral0[2].outer(), point_t(0.0, 0.0, 0.0)); bg::append(polyhedral0[2].outer(), point_t(5.0, 0.0, 0.0)); bg::append(polyhedral0[2].outer(), point_t(0.0, 0.0, 5.0)); bg::append(polyhedral0[3].outer(), point_t(5.0, 0.0, 0.0)); bg::append(polyhedral0[3].outer(), point_t(0.0, 5.0, 0.0)); bg::append(polyhedral0[3].outer(), point_t(0.0, 0.0, 5.0)); polyhedral_t polyhedral2; bg::read_wkt("POLYHEDRALSURFACE(((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)),\ ((0 0 0, 0 1 0, 0 1 1, 0 0 1, 0 0 0)),\ ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)),\ ((1 1 1, 1 0 1, 0 0 1, 0 1 1, 1 1 1)),\ ((1 1 1, 1 0 1, 1 0 0, 1 1 0, 1 1 1)),\ ((1 1 1, 1 1 0, 0 1 0, 0 1 1, 1 1 1))", polyhedral2); std::cout << bg::wkt(polyhedral0) << std::endl; std::cout << bg::wkt(polyhedral1) << std::endl; std::cout << bg::wkt(polyhedral2) << std::endl; return 0; } ``` -------------------------------- ### Get Coordinate Type of Polygon (C++) Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/core/coordinate_type This example demonstrates how to use the boost::geometry::coordinate_type metafunction to retrieve the coordinate type of a polygon. It requires including the necessary Boost.Geometry headers for points and polygons. The output shows the name of the coordinate type. ```cpp #include #include #include #include #include int main() { using point_type = boost::geometry::model::d2::point_xy; using polygon_type = boost::geometry::model::polygon; using ctype = boost::geometry::coordinate_type::type; std::cout << "type: " << typeid(ctype).name() << std::endl; return 0; } ``` -------------------------------- ### Check rectangle overlap and reassign Qt objects Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/quickstart Detect if two Qt rectangles overlap using Boost.Geometry's overlaps function, then reassign the second rectangle's values. Demonstrates how Qt classes can be adapted and used with Boost.Geometry after proper registration. ```cpp QRect r1(100, 200, 15, 15); QRect r2(110, 210, 20, 20); if (overlaps(r1, r2)) { assign_values(r2, 200, 300, 220, 320); } ``` -------------------------------- ### qbegin() Usage Example - Iterative rtree Query with Nearest Predicate Source: https://www.boost.org/doc/libs/latest/libs/geometry/doc/html/geometry/reference/spatial_indexes/group__rtree__functions/qbegin_rtree______const____predicates_const___ Demonstrates iterative querying on an rtree using qbegin() with a nearest() predicate to find the 3 nearest points to a given point, then applying do_something() to each result using std::for_each. ```cpp std::for_each(bgi::qbegin(tree, bgi::nearest(pt, 3)), bgi::qend(tree), do_something()); ```