gpt4 book ai didi

c++ - 带有两个返回语句的 std::partition

转载 作者:行者123 更新时间:2023-11-28 04:40:09 25 4
gpt4 key购买 nike

是否可以使用两个 return 语句将 std::partition 与条件一起使用?
在我的示例中,我想根据彼此之间的距离将线段分成几组。非相交线段之间的最小距离可以计算为线 1(左线)和线 2(右线)的端点之一之间的距离。
但也有可能第 1 行的另一个端点更接近第 2 行。这是一些代码:

#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

float getDistance(float x1, float y1, float x2, float y2) {
float diff_x = x2 - x1;
float diff_y = y2 - y1;

float distance = sqrtf(diff_x*diff_x + diff_y * diff_y);

return distance;
}

float getMinimumDistance(Point2f point, Point2f startpoint, Point2f endpoint) {
// Return minimum distance between line segment vw and point p
const float l2 = (endpoint.x - startpoint.x)*(endpoint.x - startpoint.x) + (endpoint.y - startpoint.y)*(endpoint.y - startpoint.y); // i.e. |w-v|^2 - avoid a sqrt
if (l2 == 0.0) {
return getDistance(point.x, point.y, startpoint.x, startpoint.y); // v == w case
}

const float t = std::max(float(0), std::min((float)1, ((point.x - startpoint.x)*(endpoint.x - startpoint.x) + (point.y - startpoint.y)*(endpoint.y - startpoint.y) / l2)));

const Point2f projection = Point2f(startpoint.x + t * (endpoint.x - startpoint.x), startpoint.y + t * (endpoint.y - startpoint.y)); // Projection falls on the segment
return getDistance(point.x, point.y, projection.x, projection.y);
}

int main(int argc, char** argv)
{
// Create some values - both vectors have the same size
vector<Vec4f> line_segments_1;

line_segments_1.push_back(Vec4f(1.0, 1.0, 5.0, 5.0));
line_segments_1.push_back(Vec4f(2.0, 0.5, 5.0, 2.5));
line_segments_1.push_back(Vec4f(0.0, 0.5, 1.0, 2.5));
line_segments_1.push_back(Vec4f(5.0, 1.0, 5.0, 4.0));
line_segments_1.push_back(Vec4f(3.0, 1.0, 4.0, 3.0));
line_segments_1.push_back(Vec4f(5.0, 5.0, 1.0, 5.0));
line_segments_1.push_back(Vec4f(4.0, 8.0, 8.0, 4.0));
line_segments_1.push_back(Vec4f(8.0, 4.0, 8.0, 1.0));
line_segments_1.push_back(Vec4f(7.0, 2.0, 9.0, 3.0));

int pixel_tolerance = 2;
vector<int> labels;

// Using std::partition
if (line_segments_1.size() > 1) {
int n_labels = partition(line_segments_1, labels, [pixel_tolerance](const Vec4f &left_line, const Vec4f &right_line) { // Clustering using partition, the pixel tolerance and two conditions
return getMinimumDistance(Point2f(left_line[0], left_line[1]), Point2f(right_line[0], right_line[1]), Point2f(right_line[2], right_line[3])) < pixel_tolerance; // If one statement is true, the lines belong to a cluster
return getMinimumDistance(Point2f(left_line[2], left_line[3]), Point2f(right_line[0], right_line[1]), Point2f(right_line[2], right_line[3])) < pixel_tolerance;
});
}
}

最佳答案

第一个return总是结束函数,第二个是不可达的。

您可能需要的是逻辑或,拼写为 ||

return (getMinimumDistance(Point2f(left_line[0], left_line[1]), Point2f(right_line[0], right_line[1]), Point2f(right_line[2], right_line[3])) < pixel_tolerance)
|| (getMinimumDistance(Point2f(left_line[2], left_line[3]), Point2f(right_line[0], right_line[1]), Point2f(right_line[2], right_line[3])) < pixel_tolerance);

关于c++ - 带有两个返回语句的 std::partition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50366973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com