1

I've tried for some time to make the following code run properly. The problem is in the comparison function of sort(comp). It compares 2 doubles but in some cases it causes the program to crash with the message "Debug Assertion failed! Program: retrace.exe File:c:program files (x86)\microsoft visual studio 10.0\vc\include\algorithm Expression:Invalid operator<". I'm 100% it's from the stl's sort because when the program crashes the last line that has been successfully executed is the one before sort. At first I thought that it is some precision problem with double but now I doubt it. Any help or info will be appreciated.

Vector startg;
bool comp(const std::pair<Vector, int>& p1, const std::pair<Vector, int>& p2)
{
    return (p1.first-startg).lengthSqr() < (p2.first-startg).lengthSqr();
}

bool Blobs::intersect(const Ray& ray, IntersectionInfo& info) const
{
    startg.set(ray.start.x, ray.start.y, ray.start.z);
    std::vector<std::pair<Vector, int> > spheres_inters;
    //not important stuff
    for(int i=0;i<n;i++)
    {
        Vector H = ray.start - centres[i];
        double A = ray.dir.lengthSqr();
        double B = 2 * dot(H, ray.dir);
        double C = H.lengthSqr() - bounding_radius*bounding_radius;
        double D = B*B - 4*A*C;
        if(D<=0)
            continue;
        double x1 = (-B + sqrt(D)) / (2*A);
        double x2 = (-B - sqrt(D)) / (2*A);
        Vector v1 = ray.start + x1*ray.dir;
        Vector v2 = ray.start + x2*ray.dir;
        spheres_inters.push_back(std::make_pair(v1, i));
        spheres_inters.push_back(std::make_pair(v2, i));
    }
    if(spheres_inters.size()==0)
        return false;
    ///////////////////////////////////////////////////////////////

    std::sort(spheres_inters.begin(), spheres_inters.end(), comp);//THE PROBLEM IS HERE!

    /* the rest of the method.....*/
ks1322
  • 33,961
  • 14
  • 109
  • 164
  • 7
    `double` is not strictly ordered. You must restrict yourself to non-NaN values if you want to stand a chance of using it. Here is a [previous question of mine](http://stackoverflow.com/questions/8096817/is-nan-a-valid-key-value-for-associative-containers) on this subject. – Kerrek SB Feb 11 '12 at 21:37
  • 3
    Can you provide a complete test-case that shows the problem? (see http://sscce.org) – Oliver Charlesworth Feb 11 '12 at 21:39
  • @Kerrek SB, how can I convert double to non-Nan? The first thing that comes to my mind is to convert them to int. Or should I check if the doubles I am comparing are NaN? – user1204323 Feb 11 '12 at 22:36
  • @OliCharlesworth, {x=0.10517935643670717 y=0.42613577783692147 z=-0.69864384638842925 }, {x=-0.65493769178130767 y=0.27844455022391795 z=0.23691748038907412 }, {x=-0.61780614150244828 y=0.28565923414228972 z=0.19121551576976237 }, {x=0.0071540578372295549 y=0.40708939975445579 z=-0.57799311420121424 } and the lengths are 6.1142385211670041, 2.3370925967856433, 7.4708443791305621, 2.2502460725540434 – user1204323 Feb 11 '12 at 22:48
  • 2
    @user1204323 better to actually read that link (http://sscce.org/)? E.g. Where is `Vector` defined? – sehe Feb 11 '12 at 22:52
  • This is part of a project that has more than 20 files. I can't post them all. I tried to reproduce the problem by simply trying to std::sort() a std::vector but it didn't work(in other words, the simpler program worked fine). So I decided to post this, hoping that someone would come up with a solution without the whole code. Sorry if it is trouble. – user1204323 Feb 12 '12 at 00:49
  • _"It compares 2 doubles"_ No. It compares 2 `pair`s, first by `Vector` and only after that by `double`. And nowhere is there any indication of what that `Vector` is. Therefore there is no way to answer this question and no point in speculating about it. – underscore_d Nov 15 '18 at 23:00

0 Answers0