STDの比較関数で使用される第三の変数::ソートが可能に魔法の方法

問題の背景

のは、私は、3Dライン上の点の束を持っているとしましょう。それらの各々は、Eigen::Vector3dその座標のx、y及びzの3つの値を持つ変数。このアルゴリズムは、最も近いから最も遠いするために、元の点までの距離によってそれらを並べ替えることです。

コーディング

まず、コードスニペットは、次のとおりです。

// 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) );
}

Unupgradedバージョン:

bool sortEuclideanToOriginalPoint(const Eigen::Vector3d &ev3dPtsOnLine1, const Eigen::Vector3d &ev3dPtsOnLine2, const Eigen::Vector3d &ev3dOriginalPoint)
{
    return( calculateEuclideanDistance( ev3dPtsOnLine1, ev3dOriginalPoint) <  calculateEuclideanDistance( ev3dPtsOnLine2, ev3dOriginalPoint) );
}

    std::vector<Eigen::Vector3d> vEv3dPtsOnLine;
    double dPtToOriginalPoint; // calculate the square root of distance between ePtsOnLine and ev3dOriginalPoint
    for (int nIndex = 0; nIndex < vEv3dPtsOnLine.size(); nIndex++)
    {
        dPtToEnd = std::sqrt( calculateEuclideanDistance( vEv3dPtsOnLine.at(nIndex), ev3dOriginalPoint ) );
        vE4dPtsOnLine.push_back( Eigen::Vector4d(vEPtsOnLine.at(nIndex)(0), vEPtsOnLine.at(nIndex)(1), vEPtsOnLine.at(nIndex)(2), ev3dOriginalPoint) );
    }
    // In this way to get errors
    std::sort (vEv4dPtsOnLine.begin(), vEv4dPtsOnLine.end(), sortEuclideanToOriginalPoint( , , ) );

私はの使用法を探しstd::sort 、[1] および比較関数は、3番目の変数を取ることを禁じられそうです。

だから私は使用して、私のコードを更新するEigen::Vector4d代わりにEigen::Vector3d、だから、ユークリッド距離は、変数自体と三番目が不要に撮影することができます。

    // Load the square Euclidean distances of ePtsOnLine and eLineEnd to Eigen::Vector4d
    // So std::sort can be used directly
    std::vector<Eigen::Vector4d> vEv4dPtsOnLine;
    double dPtToOriginalPoint; // calculate the square root of distance between ePtsOnLine and ev3dOriginalPoint
    for (int nIndex = 0; nIndex < vEPtsOnLine.size(); nIndex++)
    {
        // calculate the distance with closet poleEnd. Here is poleEnd1
        dPtToEnd = std::sqrt( calculateEuclideanDistance( vEv4dPtsOnLine.at(nIndex), ev3dOriginalPoint ) );
        vE4dPtsOnLine.push_back( Eigen::Vector4d(vEPtsOnLine.at(nIndex)(0), vEPtsOnLine.at(nIndex)(1), vEPtsOnLine.at(nIndex)(2), ev3dOriginalPoint) );
    }
    std::sort (vEv4dPtsOnLine.begin(), vEv4dPtsOnLine.end(), sortEuclideanToOriginalPoint );

リファレンス

[1] のstd ::ソート- cppreference.com

おすすめ

転載: www.cnblogs.com/williamc17/p/11346333.html