less raw for-loops

This commit is contained in:
Phuntsok Drak-pa 2019-04-25 02:53:53 +02:00
parent 0aa81fdec0
commit cf1eb02507

View File

@ -294,9 +294,8 @@ void ImageManipulator::get_color_set()
thread_list.push_back( thread_list.push_back(
std::thread(&ImageManipulator::threaded_get_color, this, h + i)); std::thread(&ImageManipulator::threaded_get_color, this, h + i));
} }
for (auto& th : thread_list) { std::for_each(thread_list.begin(), thread_list.end(),
th.join(); [](auto& th) { th.join(); });
}
} }
colors_.shrink_to_fit(); colors_.shrink_to_fit();
} }
@ -339,12 +338,11 @@ void ImageManipulator::draw_square(cv::Mat& t_img,
int const t_size, int const t_size,
cv::Scalar const& t_color) const cv::Scalar const& t_color) const
{ {
auto points = std::make_unique<cv::Point[]>(4); std::array<cv::Point, 4> points
points[0] = t_top_left; = {t_top_left, cv::Point{t_top_left.x, t_top_left.y + t_size},
points[1] = cv::Point{t_top_left.x, t_top_left.y + t_size}; cv::Point{t_top_left.x + t_size, t_top_left.y + t_size},
points[2] = cv::Point{t_top_left.x + t_size, t_top_left.y + t_size}; cv::Point{t_top_left.x + t_size, t_top_left.y}};
points[3] = cv::Point{t_top_left.x + t_size, t_top_left.y}; fillConvexPoly(t_img, points.data(), 4, t_color);
fillConvexPoly(t_img, points.get(), 4, t_color);
} }
/** /**
@ -406,8 +404,6 @@ void ImageManipulator::method1()
void ImageManipulator::method2() void ImageManipulator::method2()
{ {
spdlog::debug("Beginning method2, initial difference: {}", diff_); spdlog::debug("Beginning method2, initial difference: {}", diff_);
spdlog::debug("nb_total_iter: {}, nb_remaining_iter: {}", total_iterations_,
remaining_iter_);
spdlog::debug("Running on {} threads", thread_nbr); spdlog::debug("Running on {} threads", thread_nbr);
get_color_set(); get_color_set();
spdlog::debug("{} colors detected", colors_.size()); spdlog::debug("{} colors detected", colors_.size());
@ -421,8 +417,6 @@ void ImageManipulator::method2()
void ImageManipulator::method3() void ImageManipulator::method3()
{ {
spdlog::debug("Beginning method3, initial difference: {}", diff_); spdlog::debug("Beginning method3, initial difference: {}", diff_);
spdlog::debug("nb_total_iter: {}, nb_remaining_iter: {}", total_iterations_,
remaining_iter_);
spdlog::debug("Running on {} threads", thread_nbr); spdlog::debug("Running on {} threads", thread_nbr);
get_color_set(); get_color_set();
spdlog::debug("{} colors detected", colors_.size()); spdlog::debug("{} colors detected", colors_.size());
@ -440,8 +434,6 @@ void ImageManipulator::method3()
void ImageManipulator::method4(bool const t_controlled_size) void ImageManipulator::method4(bool const t_controlled_size)
{ {
spdlog::debug("Beginning method4, initial difference: {}", diff_); spdlog::debug("Beginning method4, initial difference: {}", diff_);
spdlog::debug("nb_total_iter: {}, nb_remaining_iter: {}", total_iterations_,
remaining_iter_);
spdlog::debug("Running on {} threads", thread_nbr); spdlog::debug("Running on {} threads", thread_nbr);
get_color_set(); get_color_set();
spdlog::debug("{} colors detected", colors_.size()); spdlog::debug("{} colors detected", colors_.size());
@ -449,16 +441,19 @@ void ImageManipulator::method4(bool const t_controlled_size)
std::vector<std::future<std::optional<std::pair<cv::Mat, double>>>> std::vector<std::future<std::optional<std::pair<cv::Mat, double>>>>
results{}; results{};
std::vector<std::pair<cv::Mat, double>> values{}; std::vector<std::pair<cv::Mat, double>> values{};
// launch asynchronously candidate image generation
for (size_t i = 0; i < thread_nbr; ++i) { for (size_t i = 0; i < thread_nbr; ++i) {
results.push_back(std::async(std::launch::async, results.push_back(std::async(std::launch::async,
&ImageManipulator::create_candidate, this, &ImageManipulator::create_candidate, this,
t_controlled_size)); t_controlled_size));
} }
for (auto& elem : results) { // if candidate is a success, store it
if (auto res = elem.get(); res.has_value() && res->second < diff_) { std::for_each(results.begin(), results.end(), [&values, this](auto& elem) {
if (auto res = elem.get(); res.has_value() && res->second < this->diff_) {
values.push_back(*res); values.push_back(*res);
} }
} });
// apply best candidate
if (values.size() > 0) { if (values.size() > 0) {
auto const pos auto const pos
= std::min_element(std::begin(values), std::end(values), = std::min_element(std::begin(values), std::end(values),
@ -488,14 +483,13 @@ void ImageManipulator::method5(bool const t_controlled_size,
spdlog::debug("{} tiles", tiles.size()); spdlog::debug("{} tiles", tiles.size());
std::vector<std::thread> thread_list{}; std::vector<std::thread> thread_list{};
for (auto& row : tiles) { std::for_each(tiles.begin(), tiles.end(), [&](auto& row) {
for (auto& tile : row) { std::for_each(row.begin(), row.end(), [&](auto& tile) {
thread_list.emplace_back( thread_list.emplace_back(
[&]() { tile.exec_method(t_submethod, t_controlled_size); }); [&]() { tile.exec_method(t_submethod, t_controlled_size); });
} });
} });
for (auto& th : thread_list) { std::for_each(thread_list.begin(), thread_list.end(),
th.join(); [](auto& th) { th.join(); });
}
merge_tiles(tiles); merge_tiles(tiles);
} }