c++ - 带有两个返回语句的 std::partition
问题描述
是否可以使用两个返回语句将 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);
推荐阅读
- php - 在 Laravel 中使用忘记后,cookie 仍然存在于 $_COOKIE 数组中
- python - col 函数如何知道我们指的是哪个 DataFrame?
- flutter - 如何在颤振中添加自定义日历?
- python - 如何在保持领先空白的同时将 CSV 或 excel 转换为 PDF?
- r - write.csv 中的 fileEncoding="UTF8" 不起作用
- javascript - 如果出现错误,我对 fetch api 有一些疑问
- python - Python酸洗对象和这些对象中的对象容器
- angular - 如何使用 Angular 中的打字稿更改 pupop 警报的颜色
- java - Java字符串替换所有字符?
- android - 膨胀类 huawei.android.widget.ImageView android.app.ActivityThread$H.handleMessage 时出错