Polygon, accessing Points_Data
Greetings, This is my first post here. Please help me as I am stuck on how to access the points defining the new polygon generated with two other polygons and a boolean operation. I am having a hard time with using the iterators properly. The code runs well up to that point. Thanks in advance, ~Amanda namespace gtl = boost::polygon; using namespace boost::polygon::operators; using namespace gtl; //lets construct the two polygons typedef gtl::polygon_data Polygon; typedef gtl::polygon_traits::point_type Point; //Polygon1 Point pts1[numcorners1]; for (int index=0; index<numcorners1; index++) { pts1[index]=gtl::construct<Point>(rotated_corners1[index].x*1000, rotated_corners1[index].y*1000); } Polygon poly1; gtl::set_points(poly1, pts1, pts1+numcorners1); //Polygon 2 Point pts2[numcorners2]; for (int index=0; index<numcorners2; index++) { pts2[index]=gtl::construct<Point>(rotated_corners2[index].x*1000, rotated_corners2[index].y*1000); //mult by 1000 because it rounds to integers. } Polygon poly2; gtl::set_points(poly2, pts2, pts2+numcorners2); using namespace boost::polygon::operators; Polygon poly3; poly3 = poly1 & poly2; Point pts3[poly3.size()]; //how does one use iterators ?! std::vector<point_data<int> >::iterator itr ; itr= poly3.begin(); int index=0 for(itr; itr != ploy3.end(); ++itr) { pts3(index)=*itr; index++; } -- View this message in context: http://boost.2283326.n4.nabble.com/Polygon-accessing-Points-Data-tp3446015p3... Sent from the Boost - Users mailing list archive at Nabble.com.
Any hints on accessing the content (meaning the vertexes) of a Polygon once it has been created ? -- View this message in context: http://boost.2283326.n4.nabble.com/Polygon-accessing-Points-Data-tp3446015p3... Sent from the Boost - Users mailing list archive at Nabble.com.
This is the last time I bump this in hope of help... Thanks in advance. -- View this message in context: http://boost.2283326.n4.nabble.com/Polygon-accessing-Points-Data-tp3446015p3... Sent from the Boost - Users mailing list archive at Nabble.com.
AmandaLind wrote:
This is the last time I bump this in hope of help...
You should be a little more patient, I read the list every day and search for Polygon in the subject line.
namespace gtl = boost::polygon; using namespace boost::polygon::operators; using namespace gtl;
//lets construct the two polygons typedef gtl::polygon_data Polygon; typedef gtl::polygon_traits::point_type Point;
Both polygon_data and polygon_traits are templates, so the above code doesn't appear legal. You need to specify the coordinate type for polygon data and the polygon type for polygon traits. ...
Polygon poly1; gtl::set_points(poly1, pts1, pts1+numcorners1);
...
Polygon poly2; gtl::set_points(poly2, pts2, pts2+numcorners2);
using namespace boost::polygon::operators;
Polygon poly3; poly3 = poly1 & poly2;
The above code should not compile because the result of a boolean AND between two polygons can be zero, one or many polygons. It must be stored to a model of polygon set concept. The polygon_data assignment operator should not accept the operator template produced by &.
Point pts3[poly3.size()];
//how does one use iterators ?! std::vector<point_data<int> >::iterator itr ; itr= poly3.begin(); int index=0 for(itr; itr != ploy3.end(); ++itr) { pts3(index)=*itr; index++; }
They are just like stl iterators (in fact they are std::vector iterators in this case.) Just do: std::vector<point_type> pts; Pts.insert(pts.end(), poly3.begin(), poly3.end()); However, you need extra code before you get to that point. Try typedef std::vector<boost::polygon::polygon_data<int> > PolySet; typedef boost::polygon::polygon_data<int> Poly; typedef boost::polygon::polygon_traits<Poly>::point_type Point; Poly poly1, poly2; gtl::set_points(poly1, pts1, pts1+numcorners1); gtl::set_points(poly2, pts2, pts2+numcorners2); PolySet polys; using namespace boost::polygon::operators; boost::polygon::assign(polys, poly1 & poly2); for(std::size_t i = 0; i < polys.size(); ++i) { std::vector<Point> pts; pts.insert(pts.end(), polys[i].begin(), polys[i].end()); //do whatever you wanted with the points } I wrote the above code off the top of my head, so don't be too alarmed if it doesn't compile for some reason. Use the example code and look at the unit test code as well as the tutorial code to see usage of the library. Regards, Luke
Luke, Thank you for your response ! I don't understand your comment on the first block of code, mostly because I don't see Coordinate type or Polygon type in the example you provided at the end. ~Amanda -- View this message in context: http://boost.2283326.n4.nabble.com/Polygon-accessing-Points-Data-tp3446015p3... Sent from the Boost - Users mailing list archive at Nabble.com.
AmandaLind wrote:
Luke, Thank you for your response !
I don't understand your comment on the first block of code, mostly because I don't see Coordinate type or Polygon type in the example you provided at the end.
~Amanda
int is coordinate type and Poly is polygon type. Coordinate type and polygon type are the names of the template parameters for polygon_data and polygon_traits templates. I substitute int and Poly in my example at the end when I instantiate those templates. Regards, Luke
Will the following two lines work if pts are of a vector of Points instead of an array of points ? Point pts2[numcorners2]; //chaged to std::vector pts2; gtl::set_points(poly2, pts2, pts2+numcorners2); And lastly, does anything about the following loop stand out to you as bad accessing of Polygon points ? for(std::size_t i = 0; i < num_polys; ++i) { pts.insert(pts.end(), polys[i].begin(), polys[i].end()); num_points_in_poly=pts.size(); numpointsperpoly.push_back(num_points_in_poly); for(int index =0; i < num_points_in_poly; index++) { std::cout<<" Here are the stubborn points x= "<<gtl::x(pts[index])<<", y= "<<gtl::y(pts[index])<<std::endl; tempcorner.assign(gtl::x(pts[index]),gtl::y(pts[index]),0); unioncorners.push_back(tempcorner); } } Thanks again, you're a lifesaver. -- View this message in context: http://boost.2283326.n4.nabble.com/Polygon-accessing-Points-Data-tp3446015p3... Sent from the Boost - Users mailing list archive at Nabble.com.
I ask the first part because Point pts2[numcorners2]; for (int index=0; index<numcorners2; index++) { pts2[index]=gtl::construct<Point>(rotated_corners2[index].x*1000, rotated_corners2[index].y*1000); //mult by 1000 because it rounds to integers. } is throwing an error since Point is not "Plain Old Data" and numcorners2, though a const int, is a variable. -- View this message in context: http://boost.2283326.n4.nabble.com/Polygon-accessing-Points-Data-tp3446015p3... Sent from the Boost - Users mailing list archive at Nabble.com.
Hi, Luke and Amanda I am kind of having the problem with the polygon. When I do the & (AND) operation of two polygons, the points in resulted polygon are not correct. Do you any ideas?? I have a post in the forum called Logic operation in boost polygon. Thank you so much! -- View this message in context: http://boost.2283326.n4.nabble.com/Polygon-accessing-Points-Data-tp3446015p3... Sent from the Boost - Users mailing list archive at Nabble.com.
HxH wrote:
Hi, Luke and Amanda I am kind of having the problem with the polygon. When I do the & (AND) operation of two polygons, the points in resulted polygon are not correct. Do you any ideas?? I have a post in the forum called Logic operation in boost polygon.
Thank you so much!
I have replied to your post. You are most welcome, Luke
AmandaLind wrote:
Will the following two lines work if pts are of a vector of Points instead of an array of points ?
Point pts2[numcorners2]; //chaged to std::vector pts2; gtl::set_points(poly2, pts2, pts2+numcorners2);
Yes, vector::iterator supports operator+, however, you can also use pts2.end().
And lastly, does anything about the following loop stand out to you as bad accessing of Polygon points ?
for(std::size_t i = 0; i < num_polys; ++i) { pts.insert(pts.end(), polys[i].begin(), polys[i].end()); num_points_in_poly=pts.size(); numpointsperpoly.push_back(num_points_in_poly);
for(int index =0; i < num_points_in_poly; index++) { std::cout<<" Here are the stubborn points x= "<<gtl::x(pts[index])<<", y= "<<gtl::y(pts[index])<<std::endl;
tempcorner.assign(gtl::x(pts[index]),gtl::y(pts[index]),0); unioncorners.push_back(tempcorner); } }
You are forgetting to clear pts before yout insert. You don't want to append the points from each polygon to the same vector each iteration of the outer loop. Other than that I don't see any obvious problems. Regards, Luke
participants (3)
-
AmandaLind
-
HxH
-
Simonson, Lucanus J