• A magic method allowing a third variable used in comparison function of std::sort


    Problem background

    Let's say I have a bunch of points on a 3D line. Each of them is an Eigen::Vector3d variable with three values of x, y, and z of its coordinate. The algorithm is to sort them by the distance to the original point in order from the closest to farthest.

    Coding

    At first, the code snippet is:

    #include <vector>
    #include <math.h>
    #include <algorithm>
    
    // Eigen
    #include <Eigen/Core>
    
    // 'Cause we just want to sort them, it doesn't matter about whether should take the square root of them
    double calculateEuclideanDistance(const Eigen::Vector3d &ev3dPt1, const Eigen::Vector3d &ev3dPt2)
    {
        return ( (ev3dPt1 - ev3dPt2).dot(ev3dPt1 - ev3dPt2) );
    }
    

    Not upgraded version:

    bool sortEuclideanToOriginalPoint(const Eigen::Vector3d &ev3dPtsOnLine1, const Eigen::Vector3d &ev3dPtsOnLine2, const Eigen::Vector3d &ev3dOriginalPoint)
    {
        return( calculateEuclideanDistance( ev3dPtsOnLine1, ev3dOriginalPoint) <  calculateEuclideanDistance( ev3dPtsOnLine2, ev3dOriginalPoint) );
    }
    
        std::vevtor<Eigen::Vector3d> vEv3dPtsOnLine; // contains points on line
        // In this way getting errors
        std::sort (vEv3dPtsOnLine.begin(), vEv3dPtsOnLine.end(), sortEuclideanToOriginalPoint( , , ev3dOriginalPoint) );
    

    I look for the usage of std::sort [1], and the comparison function seems forbidden to take the third variable.


    Updated code: using Eigen::Vector4d instead of Eigen::Vector3d, the euclidean distance can be taken in the variable itself and no need for a third one.

    // Comparison function with third variable
    // the distance compared is stored in the fouth value, index [3]
    // it can be retrieved easily
    bool sortEuclideanToOriginalPoint(const Eigen::Vector4d &ev4dPtsOnLine1, const Eigen::Vector4d &ev4dPtsOnLine2)
    {
        return( ev4dPtsOnLine1[3] <  ev4dPtsOnLine2[3] );
    }
    
        std::vevtor<Eigen::Vector3d> vEv3dPtsOnLine; // contains points on line
        // Load the square Euclidean distances of vEv3dPtsOnLine and ev3dOriginalPoint to the fouth value of Eigen::Vector4d, vEv3dPtsOnLine[3]
        // So std::sort can be used directly
        std::vector<Eigen::Vector4d> vEv3dPtsOnLine;
        double dDisToOriginalPoint; // calculate the square root of distance between vEv3dPtsOnLine and ev3dOriginalPoint
        for (int nIndex = 0; nIndex < vEPtsOnLine.size(); nIndex++)
        {
            dDisToOriginalPoint = std::sqrt( calculateEuclideanDistance( vEv3dPtsOnLine.at(nIndex), dPtToOriginalPoint ) );
            vE4dPtsOnLine.push_back( Eigen::Vector4d(vEv3dPtsOnLine.at(nIndex)(0), vEv3dPtsOnLine.at(nIndex)(1), vEv3dPtsOnLine.at(nIndex)(2), dDisToOriginalPoint) );
        }
        std::sort (vEv4dPtsOnLine.begin(), vEv4dPtsOnLine.end(), sortEuclideanToOriginalPoint );
    

    A little trick is needed by the brain, not by the algorithm.

    References

    [1] std::sort - cppreference.com

  • 相关阅读:
    CodeBlocks 中fopen函数不支持命令 “r”
    【转载】分享一些Qt学习资源,欢迎下载
    【转载】知乎答案----孙志岗----Google 发布了程序员养成指南,国内互联网巨头是否也有类似的指南和课程推荐
    【转载】谷歌公司推荐的程序员必修课(英文教程)
    【转载】张逸--ThoughtWorks(中国)程序员读书雷达
    在windows环境下,为什么要用Notepad++编辑?
    【转载】池建强--趣谈个人建站
    JAVA入门第二季 第一章 类和对象
    CAP理论总结
    分布式通信方式之消息队列之RocketMQ
  • 原文地址:https://www.cnblogs.com/linweilin/p/11346333.html
Copyright © 2020-2023  润新知