5

I am new to the generic geometry library that is proposed for inclusion with boost:

http://geometrylibrary.geodan.nl/

I have two vectors vector<int> Xb, Yb that I am trying to create a polygon from. I am trying to get something along the lines of the following code snippet:

 polygon_2d P;

 vector<double>::const_iterator xi;
 vector<double>::const_iterator yi;

 for (xi=Xb.begin(), yi=Yb.begin(); xi!=Xb.end(); ++xi, ++yi)
  P.push_back (make<point_2d>(*xi, *yi));

The above code does not work, complaining that P does not have a push_back member function. How do I initialize the polygon from points that have coordinates vector<int> Xb,vector<int> Yb?

genpfault
  • 51,148
  • 11
  • 85
  • 139
D R
  • 21,936
  • 38
  • 112
  • 149
  • 7
    A quick note, the likely hood that specific library you mention will survive a review process is highly unlikely. Your best option today is to write a light-weight C++ wrapper for the Generic Polygon Clipper library, as none of the proposed submissions for 2D polygon operations come anywhere near the performance GPC provides. –  Sep 17 '09 at 20:12
  • Thanks for the suggestion,Beh. GPC seems like a nice library, however it seems to be missing a feature that is important for me - namely the ability to compute the area of a polygon. – D R Sep 22 '09 at 23:59
  • 1
    Dzhelil, fortunately the area algorithm is trivial to implement yourself. http://alienryderflex.com/polygon_area/ – Stefan Monov Dec 29 '09 at 13:23
  • 5
    @Beh Tou Cheh - For your and others information, it survived and has become a part of Boost C++ Libraries. – mloskot Jan 25 '10 at 23:30
  • @mloskot What version of boost is it expected to be in? – D R Jan 26 '10 at 02:56
  • @Dzhelil Rufat - the aim is to release as soon as feasible, what should mean in practice next Boost release (current is 1.41.0) or next after that. – mloskot Jan 26 '10 at 14:01
  • 1
    Now it has been included in Boost as Boost.geometry – Ivan Xiao Jul 20 '11 at 22:24

3 Answers3

13

Here is example to the extension of your original question you asked as a comment below Kirill's answer: Are intersections between polygons possible?

Yes, polygon-polygon intersections are supported by Boost.Geometry (aka GGL)

#include <iostream>
#include <vector>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/cartesian2d.hpp>
#include <boost/geometry/geometries/adapted/c_array_cartesian.hpp>

using namespace boost::geometry;

int main(void)
{
    // Define a polygons and fill the outer rings.
    polygon_2d a;
    {
        const double c[][2] = {
            {160, 330}, {60, 260}, {20, 150}, {60, 40}, {190, 20}, {270, 130}, {260, 250}, {160, 330}
        };
        assign(a, c);
    }
    correct(a);
    std::cout << "A: " << dsv(a) << std::endl;

    polygon_2d b;
    {
        const double c[][3] = {
            {300, 330}, {190, 270}, {150, 170}, {150, 110}, {250, 30}, {380, 50}, {380, 250}, {300, 330}
        };
        assign(b, c);
    }
    correct(b);
    std::cout << "B: " << dsv(b) << std::endl;

    // Calculate interesection
    typedef std::vector<polygon_2d > polygon_list;
    polygon_list v;

    intersection_inserter<polygon_2d>(a, b, std::back_inserter(v));
    std::cout << "Intersection of polygons A and B" << std::endl;
    for (polygon_list::const_iterator it = v.begin(); it != v.end(); ++it)
    {
        std::cout << dsv(*it) << std::endl;
    }

    return 0;
}

Here is the result (the polygon being intersection is moved to south for better visibility):

alt text

I hope it will work for you.

Community
  • 1
  • 1
mloskot
  • 37,086
  • 11
  • 109
  • 136
6
append(P, make<point_2d>(*xi, *yi));
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • Thanks, this solves the above problem. However, now I am running into another one. Trying to intersect a polygon_2d with another polygon_2d returns an error. The examples only show how to do intersections between box_2d and polygon_2d. Are intersections between polygons possible? – D R Sep 17 '09 at 20:12
0

You can also use a tuple to initialize the polygon

#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

and

boost::geometry::assign_points(
    polygon, boost::assign::tuple_list_of
        (300, 330) (190, 270) (150, 170) (150, 110) (250, 30) (380, 50)
        (380, 250) (300, 330)
);
Marios V
  • 1,174
  • 9
  • 15