Hi all,
I'm trying to find out the cause of a bug in an open source project (LibreCAD). I am at my wit's end.
The problem is (from the perspective of a user) that some arcs are drawn in the wrong direction.
Here is an example:

The left half of the image is the way how it should look like. On the right side you can see the bug.
It's an image of a 0,0/200,100 rectangle which should be filled with a hatch (the hatch is called "arcs")
What some other guys and I found out so far:
#this bug seems to be present in release version only
#may!!! only effect 32bit os
My recent debugging orgy led to these results:
The order of the start point and end point of the arc to be drawn are transposed in the QList "is2":
QList<std::shared_ptr<RS_Vector> > is2;
QList<std::shared_ptr<RS_Vector> > is2;
To copy to clipboard, switch view to plain text mode
As "is2" is created/filled in the sort section I think the bug have to be somewhere there.
Here is the code snippet of the sorting:
...
// sort the intersection points into is2:
RS_Vector sp = startPoint;
double sa = center.angleTo(sp);
if(ellipse != NULL) sa=ellipse->getEllipseAngle(sp);
QList<std::shared_ptr<RS_Vector> > is2;
bool done;
double minDist;
double dist = 0.0;
std::shared_ptr<RS_Vector> av;
std::shared_ptr<RS_Vector> v;
RS_Vector last = RS_Vector(false);
do {
done = true;
minDist = RS_MAXDOUBLE;
av.reset();
for (int i = 0; i < is.size(); ++i) {
v = is.at(i);
double a;
switch(e->rtti()){
case RS2::EntityLine:
dist = sp.distanceTo(*v);
break;
case RS2::EntityArc:
case RS2::EntityCircle:
a = center.angleTo(*v);
dist = reversed?
fmod(sa - a + 2.*M_PI,2.*M_PI):
fmod(a - sa + 2.*M_PI,2.*M_PI);
break;
case RS2::EntityEllipse:
a = ellipse->getEllipseAngle(*v);
dist = reversed?
fmod(sa - a + 2.*M_PI,2.*M_PI):
fmod(a - sa + 2.*M_PI,2.*M_PI);
break;
default:
break;
}
if (dist<minDist) {
minDist = dist;
done = false;
av = v;
}
}
// copy to sorted list, removing double points
if (!done && av.get()!=NULL) {
if (last.valid==false || last.distanceTo(*av)>RS_TOLERANCE) {
is2.append(std::shared_ptr<RS_Vector>(new RS_Vector(*av)));
last = *av;
}
#if QT_VERSION < 0x040400
emu_qt44_removeOne(is, av);
#else
is.removeOne(av);
#endif
av.reset();
}
} while(!done);
...
...
// sort the intersection points into is2:
RS_Vector sp = startPoint;
double sa = center.angleTo(sp);
if(ellipse != NULL) sa=ellipse->getEllipseAngle(sp);
QList<std::shared_ptr<RS_Vector> > is2;
bool done;
double minDist;
double dist = 0.0;
std::shared_ptr<RS_Vector> av;
std::shared_ptr<RS_Vector> v;
RS_Vector last = RS_Vector(false);
do {
done = true;
minDist = RS_MAXDOUBLE;
av.reset();
for (int i = 0; i < is.size(); ++i) {
v = is.at(i);
double a;
switch(e->rtti()){
case RS2::EntityLine:
dist = sp.distanceTo(*v);
break;
case RS2::EntityArc:
case RS2::EntityCircle:
a = center.angleTo(*v);
dist = reversed?
fmod(sa - a + 2.*M_PI,2.*M_PI):
fmod(a - sa + 2.*M_PI,2.*M_PI);
break;
case RS2::EntityEllipse:
a = ellipse->getEllipseAngle(*v);
dist = reversed?
fmod(sa - a + 2.*M_PI,2.*M_PI):
fmod(a - sa + 2.*M_PI,2.*M_PI);
break;
default:
break;
}
if (dist<minDist) {
minDist = dist;
done = false;
av = v;
}
}
// copy to sorted list, removing double points
if (!done && av.get()!=NULL) {
if (last.valid==false || last.distanceTo(*av)>RS_TOLERANCE) {
is2.append(std::shared_ptr<RS_Vector>(new RS_Vector(*av)));
last = *av;
}
#if QT_VERSION < 0x040400
emu_qt44_removeOne(is, av);
#else
is.removeOne(av);
#endif
av.reset();
}
} while(!done);
...
To copy to clipboard, switch view to plain text mode
The sort process should sort the points/elements in the list "is" and add them in the correct order* to "is2".
*the order should be: start point, the point next to the start point, ..., the end point
The funny thing (at least for me) is, when I add qDebug("angle centerToNextIntersection: %f",a);
after these lines :
case RS2::EntityArc:
case RS2::EntityCircle:
a = center.angleTo(*v);
dist = reversed?
fmod(sa - a + 2.*M_PI,2.*M_PI):
fmod(a - sa + 2.*M_PI,2.*M_PI);
->here->here->
case RS2::EntityArc:
case RS2::EntityCircle:
a = center.angleTo(*v);
dist = reversed?
fmod(sa - a + 2.*M_PI,2.*M_PI):
fmod(a - sa + 2.*M_PI,2.*M_PI);
->here->here->
To copy to clipboard, switch view to plain text mode
the weird behaviour seems to be gone!
If you would like to have a look at the discussion of this bug in the LibreCAD forum (there are more pictures of the bug):
http://forum.librecad.org/bugs-probl...tp5708894.html
If you would like to have a look at the whole file where the bug is present:
https://github.com/LibreCAD/LibreCAD...e/rs_hatch.cpp
(the sort process begins at about the line 335)
Are the elements of QList added/removed in the wrong way?
What could also lead to a behaviour where the order of QList may become upside down/incorrect?
ANY help is appreciated!
(also hints about things I could try)
using:
ubuntu 12.04 32bit
Compiler: GNU GCC 4.6.3
Qt Version: 4.8.1
Bookmarks