Boost logo

Geometry :

Subject: [ggl] Keeping information for points when running algorithms
From: V D (zedxz2)
Date: 2011-11-02 13:33:57


Hi,

Let's say I have a custom point type (it's a point, but it has flags, see code below).

My problem is that I have some flags on some of those points, and when I'm running bg::intersection I am losing those flags, even if the point is part of the output, ie: it is not chopped.
I would have thought that non-chopped points in the intersection would have been copy constructed, therefore preserving my flags.

I'm assuming bg creates new points and just assign the coordinates via the setters ?

Consider the following case, where we are intersecting the green polygon with a box

If the top right vertex (1024, 1024) of the green rectangle had a flag = 0x01, I would have wanted that vertex, which is as-is part of the output, to also have the flag 0x01.

Poly before:
512 0 Flag: 0
1024 0 Flag: 0
1024 1024 Flag: 1
512 1024 Flag: 0
512 0 Flag: 0

After running bg::intersection on those 2:
1024 0 Flag: 0
1024 1024 Flag: 0
512 1024 Flag: 0
512 0 Flag: 0
512 0 Flag: 0
512 0 Flag: 0
1024 0 Flag: 0
1024 0 Flag: 0

Notice 1024 1024 doesn't have the 0x01 flag anymore :(
I can work around by keeping a (x,y) -> flags mapping, that works well for integers (this case), but that might be tricky for floating point polygons :/

Thoughts ?

Thank you!
-V

*Code for this example:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/foreach.hpp>
#include <vector>

template <typename T>
class SpecialPoint
{
public:
  SpecialPoint()
  : flags(0)
  {}
  
  T x() const { return x_; }
  T y() const { return y_; }

  void set_x(T x) { x_ = x; }
  void set_y(T y) { y_ = y; }
  
  uint64_t flags;
  
private:
  T x_;
  T y_;
};

typedef SpecialPoint<int> MyPoint;

BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(MyPoint, int, boost::geometry::cs::cartesian,
  MyPoint::x,
  MyPoint::y,
  MyPoint::set_x,
  MyPoint::set_y)
  
int main()
{
 typedef boost::geometry::model::polygon<MyPoint, false > polygon_type;
 typedef boost::geometry::model::box<MyPoint > box_type;
 
 box_type box;
 polygon_type poly;

 boost::geometry::read_wkt("POLYGON((0 0, 2047 0, 2047 2047, 0 2047, 0 0))", box);
 boost::geometry::read_wkt("POLYGON((512 0, 1024 0, 1024 1024, 512 1024, 512 0))", poly);

 // Put a flag on the (1024, 1024) point
 poly.outer()[2].flags = 1;
 
 std::cout << "Poly before: \n";
 BOOST_FOREACH(const MyPoint& pt, poly.outer())
 {
   std::cout << pt.x() << " " << pt.y() << " Flag: " << pt.flags << "\n";
 }

 
 std::vector<polygon_type> output;
 boost::geometry::intersection(poly, box, output);
 
 std::cout << "\nOutput geometry: \n";
 
 BOOST_FOREACH(const MyPoint& pt, output[0].outer())
 {
   std::cout << pt.x() << " " << pt.y() << " Flag: " << pt.flags << "\n";
 }
  
 return 0;
}

-------------- next part --------------
Skipped content of type multipart/related


Geometry list run by mateusz at loskot.net