Getting the coordinates of points from a Boost Geometry polygon
boost, boost-geometry, c++
Solution
I found a few things that needed to be fixed:
- One issue I see is in your templates. Be sure to put spaces!
- boost range works on containers or ranges that hold begin, end pairs
- Iterators represents something like a pointer to an object. Getting the size of the iterator will not do what you want. You need to either use boost::size of a whole container, or std::distance(begin_iterator,end_iterator).
Here is a version that compiles:
#include <vector>
#include <boost/range.hpp>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
using namespace boost::geometry;
typedef boost::geometry::model::point
<
double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree>
> spherical_point;
class PointAggregator {
private :
double *x, *y;
int count;
public :
PointAggregator(int size) {
x = (double*) malloc(sizeof(double) * size);
y = (double*) malloc(sizeof(double) * size);
count = 0;
}
~PointAggregator() {
free(x);
free(y);
}
inline void operator()(spherical_point& p) {
x[count] = get<0>(p);
y[count] = get<1>(p);
count++;
}
void GetResult(double *resultX, double *resultY) {
resultX = x;
resultY = y;
}
};
// added spaces to the close brackets >> becomes > >
void VectorToArray(std::vector<model::polygon<spherical_point> > resultVector, double x[], double y[], int *count) {
for (std::vector<model::polygon<spherical_point> >::iterator it = resultVector.begin(); it != resultVector.end(); ++it) {
if (boost::size(resultVector) >= 2) {
// getting the size of the whole container
*count = boost::size(resultVector);
PointAggregator* pa = new PointAggregator(*count);
boost::geometry::for_each_point(*it, *pa);
pa->GetResult(x, y);
delete(pa);
break;
}
}
}
Problem
I have a simple DLL doing some calculations with Boost Geometry polygons. (Mostly intersections and differences.) Since the DLL will be most likely called from C# code, and from Delphi and who knows from where else, I should convert the result into arrays that everything can handle. UPDATE: I had simplified and somewhat corrected my code. The new code looks completely different, uses a completely different approach (`for_each_point`), and somehow still doesn't compile. My new code: ``` #include <vector> #include <boost/range.hpp> #include <boost/geometry.hpp> #include <boost/geometry/geometries/polygon.hpp> using namespace boost::geometry; typedef boost::geometry::model::point < double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree> > spherical_point; class PointAggregator { private : double *x, *y; int count; public : PointAggregator(int size) { x = (double*) malloc(sizeof(double) * size); y = (double*) malloc(sizeof(double) * size); count = 0; } ~PointAggregator() { free(x); free(y); } inline void operator()(spherical_point& p) { x[count] = get<0>(p); y[count] = get<1>(p); count++; } void GetResult(double *resultX, double *resultY) { resultX = x; resultY = y; } }; void VectorToArray(std::vector<model::polygon<spherical_point>> resultVector, double x[], double y[], int *count) { int i = 0; for (std::vector<model::polygon<spherical_point>>::iterator it = resultVector.begin(); it != resultVector.end(); ++it) { if (boost::size(*it) >= 2) { *count = boost::size(*it); PointAggregator* pa = new PointAggregator(*count); boost::geometry::for_each_point(*it, *pa); pa->GetResult(x, y); delete(pa); break; } } } ``` The current compilation errors are: - error C2039: 'type' : is not a member of 'boost::mpl::eval_if_c' iterator.hpp 63 - error C3203: 'type' : unspecialized class template can't be used as a template argument for template parameter 'Iterator', expected a real type difference_type.hpp 25 - error C2955: 'boost::type' : use of class template requires template argument list difference_type.hpp 25 - error C2955: 'boost::iterator_difference' : use of class template requires template argument list difference_type.hpp 26 Which ones don't look like they have anything to do with this part of code (my filename is geometry.cpp), but everything else that uses Boost Geometry is commented out and I still get these errors, so... Here is my bad code that I had previously (edited by sehe) (I'm new to C++ and Boost so I might have missed some basic concept while putting code from the internet together.) I assume that I can just not iterate through a polygon that easily and I missed the non-trivial part, or that a polygon can not be used as a ring, or iterations are just not the way I thought they are, or I have no idea what else can be wrong. What did I do wrong?