0

I'm new to stackoverflow, sorry if I format the text wrong.

I have a polygon that I want to rotate by a given angle.

void Polygon::rotateBy( const float cosx, const float sinx )
{
   for ( std::deque<PolygonPoint>::iterator i = point_list.begin(); i != point_list.end(); ++i )
    {
        i->x = (i->x * cosx) - (i->y * sinx);
        i->y = (i->x * sinx) + (i->y * cosx);
    }
}

This has a very unexpected result:

polygon.rotateBy(ccosx, csinx);

std::priority_queue<Edge, std::vector<Edge>, comparePolySegmentsByY> segments;
for(std::deque<PolygonPoint>::const_iterator i = (polygon.point_list.cbegin() + 1); i != polygon.point_list.cend(); ++i)
{
    segments.push(Edge(*(i-1), *i, i-1));
}
segments.push(Edge(polygon.point_list[ polygon.point_list.size() - 1 ], polygon.point_list[0], polygon.point_list.end() - 1));

The comparePolySegmentsByY:

   class comparePolySegmentsByY
   {
    public:
    bool operator()(const Edge& pol1, const Edge& pol2) const
    {
        float t1 = pol1.getMinY() - pol2.getMinY();
        if ( abs(t1) > Eps ) 
            return t1 > 0;
        else
        {
            if ( abs( pol1.getMinX() - pol2.getMinX() ) < Eps )
            {
                if ( abs ( pol1.getMaxY() - pol2.getMaxY() ) < Eps )
                    return pol1.getMaxX() > pol2.getMaxX();
                else
                    return pol1.getMaxY() > pol2.getMaxY();
            }
            else
                return pol1.getMaxX() > pol2.getMinX();
        }
    }
 };

I use the comparePolySegmentsByY to order those in the priority queue. This code gives back the error:

  Debug assertion failed: Invalid operator< 

when I push the Edge in the segments priority list. If I comment out (delete) the rotate statement (but not the rotateBy function) all works fine! I really have no idea what is happening here.

Regic
  • 35
  • 5
  • possible duplicate of [Invalid < operator assertion in sort](http://stackoverflow.com/questions/12549289/invalid-operator-assertion-in-sort); you need to make sure your sort function is stable. – Adam Maras Sep 03 '13 at 18:43
  • Wait I was wrong, Edge has no operator<, the comparison is implemented in the comparePolySegmentsByY. I edit the post to be correct. I never change the Edge after I added it to the queue, of course. But...the Edge has an iterator member that points to the original point in the polygon, but the polygon is unchanged too after that one rotation. I know its not really good but I've been careful with it and it didnt cause any error. So far at least. – Regic Sep 03 '13 at 18:54
  • Please post the code for `comparePolySegmentsByY`. – Adam Maras Sep 03 '13 at 18:58
  • New results: this problem doesn't occur with gcc on mac. I think it must be because of some optimalization that is done in visual c++ and that is incorrect. I think the iterator is the key somehow... the compiler handles it differently then simple pointers (f.e. debug assertions). I guess the makers of visual c++ compiler assume that you don't store iterator for an outside object (altho it is not forbidden as far as I know, iterator is just a kind of smart pointer). The moral is to avoid this I guess. – Regic Sep 04 '13 at 21:35

0 Answers0