本文介绍了我的boolinq .orderBy有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试试下面的可编译代码:

  #include< math.h> 
#include< iostream>
#include< algorithm>
#include< vector>
#includeboolinq.h

float dtSqrt(float x)
{
return sqrtf(x);
}

///返回两点之间的距离。
/// @param [in] v1一点。 [(x,y,z)]
/// @param [in] v2一个点。 [(x,y,z)]
/// @return两点之间的距离。
inline float dtVdist(const float * v1,const float * v2)
{
const float dx = v2 [0] - v1 [0]
const float dy = v2 [1] - v1 [1];
const float dz = v2 [2] - v1 [2];
return dtSqrt(dx * dx + dy * dy + dz * dz);
}

int main(){
float target [] = {1,2,3};

float floats1 [] = {1.321f,2.123f,3.333f};
float floats2 [] = {1.011f,2.234f,3.555f};
float float3 [] = {1.9f,2.9f,3.9f};
float floats4 [] = {1,2,3};
float float5 [] = {1,2,3.123f};

std :: vector< const float *> float;
floatsVector.push_back(floats1);
floatsVector.push_back(floats2);
floatsVector.push_back(floats3);
floatsVector.push_back(floats4);
floatsVector.push_back(floats5);

// stl way
// std :: sort(floatatsVector.begin(),floatsVector.end(),[&](const float * pointA,const float * pointB) - > bool {
// auto distA = dtVdist(pointA,target);
// auto distB = dtVdist(pointB,target);
// return distA< distB;
//});
// auto stl_point = floatsVector.front();

try {
auto point = boolinq :: from(floatsVector)
.orderBy([&](const float * point) - > float {
auto dist = dtVdist(point,target);
return dist;
})
.reverse()
.toVector()
.front

std :: cout<<点[0]<< <点[1]< <点[2]< ; << std :: endl;

} catch(std :: exception& e){
std :: cout< e.what()< std :: endl;
}
std :: cin.get();
return 0;
}

有提供的示例...?

解决方案

显然boolinq不支持.orderBy()。reverse() - 这有点担心,授予。如果你按负距离排序(没有反向),它会工作。



对我来说,这看起来像boolinq不准备严重使用。


So I try next compilable code:

#include <math.h> 
#include <iostream> 
#include <algorithm>
#include <vector>   
#include "boolinq.h"

float dtSqrt(float x)
{
    return sqrtf(x);
}

/// Returns the distance between two points.
///  @param[in]     v1  A point. [(x, y, z)]
///  @param[in]     v2  A point. [(x, y, z)]
/// @return The distance between the two points.
inline float dtVdist(const float* v1, const float* v2)
{
    const float dx = v2[0] - v1[0];
    const float dy = v2[1] - v1[1];
    const float dz = v2[2] - v1[2];
    return dtSqrt(dx*dx + dy*dy + dz*dz);
}

int main () {
    float target[] = {1,2,3};

    float floats1[] = {1.321f,2.123f,3.333f};
    float floats2[] = {1.011f,2.234f,3.555f};
    float floats3[] = {1.9f,2.9f,3.9f};
    float floats4[] = {1,2,3};
    float floats5[] = {1,2,3.123f};

    std::vector<const float *> floatsVector;
    floatsVector.push_back(floats1);
    floatsVector.push_back(floats2);
    floatsVector.push_back(floats3);
    floatsVector.push_back(floats4);
    floatsVector.push_back(floats5);

    //stl way
    //std::sort(floatsVector.begin(), floatsVector.end(),  [&](const float* pointA, const float* pointB) -> bool{
    //  auto distA =  dtVdist(pointA, target);
    //  auto distB =  dtVdist(pointB, target);
    //  return distA < distB;
    //});
    // auto stl_point = floatsVector.front();

    try {
        auto point = boolinq::from( floatsVector )
            .orderBy([&](const float* point) -> float{
                auto dist =  dtVdist(point, target);
                return dist;
        })
            .reverse()
            .toVector()
            .front();

        std::cout << point[0] << " " << point[1] << " " << point[2] << ";" << std::endl;

    } catch (std::exception &e) {
        std::cout << e.what() << std::endl;
    }
    std::cin.get();
    return 0;
}

having boolinq header this programm compiles quite fast. Yet it fails uncachably! at runtime with some inner vector assertion error:

problem is:

 boolinq::from( floatsVector )
                .orderBy([&](const float* point) -> float{
                    auto dist =  dtVdist(point, target);
                    return dist;
            }).toVector();

is empty vector.

uncommenting stl code makes programm work as expected!

futher more simple point reordering makes it work as expected:

float floats1[] = {1,2,3};
float floats2[] = {1,2,3.123f};
float floats3[] = {1.321f,2.123f,3.333f};
float floats4[] = {1.011f,2.234f,3.555f};
float floats5[] = {1.9f,2.9f,3.9f};

A really strange bug...

btw next code:

#include <math.h> 
#include <iostream> 
#include <algorithm>
#include <vector>   
#include "boolinq/boolinq.h"

struct Point {
    float X;
    float Y;
    float Z;
    Point() : X(0), Y(0), Z(0) {}
    Point(float X, float Y, float Z) : X(X), Y(Y), Z(Z) {}
};

float dtSqrt(float x)
{
    return sqrtf(x);
}

inline float dtVdist(const float* v1, const float* v2)
{
    const float dx = v2[0] - v1[0];
    const float dy = v2[1] - v1[1];
    const float dz = v2[2] - v1[2];
    return dtSqrt(dx*dx + dy*dy + dz*dz);
}

inline float dtVSqrDist(const Point & v1, const Point & v2)
{
    const float dx = v2.X - v1.X;
    const float dy = v2.Y - v1.Y;
    const float dz = v2.Z - v1.Z;
    return dx*dx + dy*dy + dz*dz;
}

int main () {
    auto target = Point(1,2,3);
    auto pointA = Point(1,-2,3);
    auto pointB = Point(1,2,3);

    std::vector<Point> pointsVector;
    pointsVector.push_back(pointA);
    pointsVector.push_back(pointB);

    //stl way
    std::sort(pointsVector.begin(), pointsVector.end(),
        [&](const Point & pointA, const Point & pointB) -> bool{
            auto distA =  dtVSqrDist(pointA, target);
            auto distB =  dtVSqrDist(pointB, target);
            return distA < distB;
    });
    std::reverse(pointsVector.begin(), pointsVector.end());
    auto stl_point = pointsVector.front();

    std::cout << "stl point: " << stl_point.X << " " << stl_point.Y << " " << stl_point.Z << ";" << std::endl;

    //try {

    auto points = boolinq::from( pointsVector )
        .orderBy([&](const Point & point) -> float{
            auto dist =  dtVSqrDist(point, target);
            return dist;
    })
        .reverse()
        .toVector();

    auto point = points.empty() ? Point() : points.front();

    std::cout << "boolinq point: " << point.X << " " << point.Y << " " << point.Z << ";" << std::endl;

    //} catch (std::exception &e) {
    //  std::cout << e.what() << std::endl;
    //}
    std::cin.get();
    return 0;
}

Produces:

stl point: 1 -2 3;
boolinq point: 0 0 0;

What is wrong with my code? It looks alike orderBy examples thay provide on main boolinq page...?

解决方案

Apparently boolinq does not support .orderBy().reverse() - which is somewhat worrying, granted. If you sort by the negative distance (without reverse), it works.

To me this looks like boolinq isn't ready for serious use.

这篇关于我的boolinq .orderBy有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:27