clang format update

This commit is contained in:
Phuntsok Drak-pa 2019-04-27 16:27:22 +02:00
parent bc9dcf4142
commit b962d50996
7 changed files with 520 additions and 559 deletions

View File

@ -1,12 +1,13 @@
#pragma once #pragma once
#include "shapes.hh" #include "shapes.hh"
#include <array>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <string> #include <string>
#include <vector> #include <vector>
class ImageManipulator { class ImageManipulator
{
public: public:
ImageManipulator() = delete; ImageManipulator() = delete;
@ -18,29 +19,21 @@ class ImageManipulator {
/// \brief Load image from input, and prepare for output /// \brief Load image from input, and prepare for output
ImageManipulator(std::string const t_input_path, ImageManipulator(std::string const t_input_path,
std::string const t_output_path, std::string const t_output_path, int const iterations,
int const iterations,
Shape::ShapeType const t_shape); Shape::ShapeType const t_shape);
/// \brief Basically makes views from image /// \brief Basically makes views from image
ImageManipulator(cv::Mat const& t_origin_image, ImageManipulator(cv::Mat const &t_origin_image, int const t_iterations,
int const t_iterations, Shape::ShapeType const t_shape, int const t_x, int const t_y,
Shape::ShapeType const t_shape, int const t_width, int const t_height);
int const t_x,
int const t_y,
int const t_width,
int const t_height);
[[nodiscard]] auto operator=(ImageManipulator &other) = delete; [[nodiscard]] auto operator=(ImageManipulator &other) = delete;
[[nodiscard]] auto operator=(ImageManipulator &&other) noexcept = delete; [[nodiscard]] auto operator=(ImageManipulator &&other) noexcept = delete;
/// \brief Execute the nth method on the current object /// \brief Execute the nth method on the current object
void exec_method(int const t_nb_method, void exec_method(int const t_nb_method, bool const t_controlled_size,
bool const t_controlled_size, int const t_cols, int const t_rows, int const t_submethod);
int const t_cols,
int const t_rows,
int const t_submethod);
/** /**
* \brief Write the generated image to the output path * \brief Write the generated image to the output path
@ -93,12 +86,13 @@ class ImageManipulator {
void threaded_get_color(int const t_h); void threaded_get_color(int const t_h);
/// \brief Draw a square on an image /// \brief Draw a square on an image
[[deprecated]] void draw_square(cv::Mat& t_img, [[deprecated]] void draw_square(cv::Mat &t_img, cv::Point const &t_top_left,
cv::Point const& t_top_left,
int const t_size, int const t_size,
cv::Scalar const &t_color) const; cv::Scalar const &t_color) const;
void draw_shape(cv::Mat &t_img, cv::Scalar &&t_color); void draw_shape(cv::Mat &t_img, cv::Scalar &&t_color);
void create_shape() noexcept;
void create_controlled_shape() noexcept;
/// \brief Update this objects generated image /// \brief Update this objects generated image
void update_gen_image(cv::Mat const &t_img, double const t_diff); void update_gen_image(cv::Mat const &t_img, double const t_diff);
@ -124,9 +118,7 @@ class ImageManipulator {
/// \brief Fifth method as described in the /// \brief Fifth method as described in the
/// [report](https://labs.phundrak.fr/phundrak/genetic-images/blob/master/report/report.pdf) /// [report](https://labs.phundrak.fr/phundrak/genetic-images/blob/master/report/report.pdf)
void method5(bool const t_controlled_size, void method5(bool const t_controlled_size, int const cols, int const rows,
int const cols,
int const rows,
int const submethod); int const submethod);
// members ////////////////////////////////////////////////////////////////// // members //////////////////////////////////////////////////////////////////

View File

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <filesystem>
#include "shapes.hh" #include "shapes.hh"
#include <filesystem>
struct ParsedArgs { struct ParsedArgs {
std::filesystem::path input_path; std::filesystem::path input_path;
std::filesystem::path output_path; std::filesystem::path output_path;

View File

@ -4,7 +4,8 @@
#include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp> #include <opencv2/imgproc.hpp>
class Shape { class Shape
{
public: public:
static constexpr int MAX_POINTS{4}; static constexpr int MAX_POINTS{4};
enum class ShapeType { Square, Triangle }; enum class ShapeType { Square, Triangle };
@ -30,8 +31,7 @@ class Shape {
Shape &operator=(Shape &&other) noexcept = delete; Shape &operator=(Shape &&other) noexcept = delete;
/// \brief Generates a shape's points /// \brief Generates a shape's points
void operator()(cv::Point&& t_max_pos, void operator()(cv::Point &&t_max_pos, int const t_max_size,
int const t_max_size,
int const t_min_size = 0) noexcept; int const t_min_size = 0) noexcept;
[[nodiscard]] auto get_points() const noexcept [[nodiscard]] auto get_points() const noexcept
@ -46,7 +46,10 @@ class Shape {
return type_; return type_;
} }
[[nodiscard]] auto get_nb_points() const noexcept { return nb_points_; } [[nodiscard]] auto get_nb_points() const noexcept
{
return nb_points_;
}
protected: protected:
private: private:
@ -55,7 +58,6 @@ class Shape {
void create_triangle_points(cv::Point const &t_top_left, void create_triangle_points(cv::Point const &t_top_left,
int const t_size) noexcept; int const t_size) noexcept;
ShapeType const type_{ShapeType::Square}; ShapeType const type_{ShapeType::Square};
std::array<cv::Point, Shape::MAX_POINTS> points_{ std::array<cv::Point, Shape::MAX_POINTS> points_{
cv::Point{0, 0}, cv::Point{0, 0}, cv::Point{0, 0}, cv::Point{0, 0}}; cv::Point{0, 0}, cv::Point{0, 0}, cv::Point{0, 0}, cv::Point{0, 0}};

View File

@ -1,5 +1,6 @@
#include "methods.hh" #include "methods.hh"
#include "parseargs.hh" #include "parseargs.hh"
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <iostream> #include <iostream>

View File

@ -1,4 +1,5 @@
#include "methods.hh" #include "methods.hh"
#include <algorithm> #include <algorithm>
#include <future> #include <future>
#include <optional> #include <optional>
@ -18,15 +19,11 @@ static auto const thread_nbr = std::thread::hardware_concurrency();
* \param[in] other Element to copy * \param[in] other Element to copy
*/ */
ImageManipulator::ImageManipulator(const ImageManipulator &other) ImageManipulator::ImageManipulator(const ImageManipulator &other)
: colors_{other.colors_}, : colors_{other.colors_}, reference_{other.reference_},
reference_{other.reference_}, generated_image_{other.generated_image_}, shape_{other.shape_},
generated_image_{other.generated_image_}, output_path_{other.output_path_}, diff_{other.diff_},
shape_{other.shape_},
output_path_{other.output_path_},
diff_{other.diff_},
total_iterations_{other.total_iterations_}, total_iterations_{other.total_iterations_},
remaining_iter_{other.remaining_iter_}, remaining_iter_{other.remaining_iter_}, width_{other.width_},
width_{other.width_},
height_{other.height_} height_{other.height_}
{ {
} }
@ -38,15 +35,13 @@ ImageManipulator::ImageManipulator(const ImageManipulator& other)
* \param[in] other Element to move * \param[in] other Element to move
*/ */
ImageManipulator::ImageManipulator(ImageManipulator &&other) noexcept ImageManipulator::ImageManipulator(ImageManipulator &&other) noexcept
: colors_{std::move(other.colors_)}, : colors_{std::move(other.colors_)}, reference_{std::move(
reference_{std::move(other.reference_)}, other.reference_)},
generated_image_{std::move(other.generated_image_)}, generated_image_{std::move(other.generated_image_)}, shape_{std::move(
shape_{std::move(other.shape_)}, other.shape_)},
output_path_{std::move(other.output_path_)}, output_path_{std::move(other.output_path_)},
diff_{std::move(other.diff_)}, diff_{std::move(other.diff_)}, total_iterations_{other.total_iterations_},
total_iterations_{other.total_iterations_}, remaining_iter_{other.remaining_iter_}, width_{other.width_},
remaining_iter_{other.remaining_iter_},
width_{other.width_},
height_{other.height_} height_{other.height_}
{ {
} }
@ -63,10 +58,9 @@ ImageManipulator::ImageManipulator(std::string const t_input_path,
std::string const t_output_path, std::string const t_output_path,
int const t_iterations, int const t_iterations,
Shape::ShapeType const t_shape) Shape::ShapeType const t_shape)
: reference_{cv::imread(t_input_path, cv::IMREAD_COLOR)}, : reference_{cv::imread(t_input_path, cv::IMREAD_COLOR)}, shape_{Shape{
shape_{Shape{t_shape}}, t_shape}},
output_path_{t_output_path}, output_path_{t_output_path}, total_iterations_{t_iterations}
total_iterations_{t_iterations}
{ {
if (!reference_.data) { if (!reference_.data) {
spdlog::critical("Could not open or find image!\n"); spdlog::critical("Could not open or find image!\n");
@ -88,15 +82,12 @@ ImageManipulator::ImageManipulator(std::string const t_input_path,
ImageManipulator::ImageManipulator(cv::Mat const &t_origin_image, ImageManipulator::ImageManipulator(cv::Mat const &t_origin_image,
int const t_iterations, int const t_iterations,
Shape::ShapeType const t_shape, Shape::ShapeType const t_shape,
int const t_x, int const t_x, int const t_y,
int const t_y, int const t_width, int const t_height)
int const t_width,
int const t_height)
: reference_{t_origin_image( : reference_{t_origin_image(
cv::Range{t_y, std::min(t_y + t_height, t_origin_image.rows)}, cv::Range{t_y, std::min(t_y + t_height, t_origin_image.rows)},
cv::Range{t_x, std::min(t_x + t_width, t_origin_image.cols)})}, cv::Range{t_x, std::min(t_x + t_width, t_origin_image.cols)})},
shape_{Shape{t_shape}}, shape_{Shape{t_shape}}, total_iterations_{t_iterations}
total_iterations_{t_iterations}
{ {
if (!reference_.data) { if (!reference_.data) {
spdlog::critical("Could not open or find image!\n"); spdlog::critical("Could not open or find image!\n");
@ -121,31 +112,15 @@ ImageManipulator::ImageManipulator(cv::Mat const& t_origin_image,
*/ */
void ImageManipulator::exec_method(int const t_nb_method, void ImageManipulator::exec_method(int const t_nb_method,
bool const t_controlled_size = false, bool const t_controlled_size = false,
int const t_cols = 1, int const t_cols = 1, int const t_rows = 0,
int const t_rows = 0,
int const t_submethod = 1) int const t_submethod = 1)
{ {
switch (t_nb_method) { switch (t_nb_method) {
case 1: { case 1: method1(); break;
method1(); case 2: method2(); break;
break; case 3: method3(); break;
} case 4: method4(t_controlled_size); break;
case 2: { case 5: method5(t_controlled_size, t_cols, t_rows, t_submethod); break;
method2();
break;
}
case 3: {
method3();
break;
}
case 4: {
method4(t_controlled_size);
break;
}
case 5: {
method5(t_controlled_size, t_cols, t_rows, t_submethod);
break;
}
default: default:
spdlog::error("Requested method {} is not implemented.", t_nb_method); spdlog::error("Requested method {} is not implemented.", t_nb_method);
std::exit(-1); std::exit(-1);
@ -165,8 +140,9 @@ void ImageManipulator::exec_method(int const t_nb_method,
* \param t_img Image with which the distance is computed * \param t_img Image with which the distance is computed
* \return double * \return double
*/ */
[[nodiscard]] auto ImageManipulator::euclidian_distance( [[nodiscard]] auto
cv::Mat const& t_img) const noexcept -> double ImageManipulator::euclidian_distance(cv::Mat const &t_img) const noexcept
-> double
{ {
double euclidian = 0.0; double euclidian = 0.0;
for (auto itr1 = reference_.begin<uchar>(), itr2 = t_img.begin<uchar>(); for (auto itr1 = reference_.begin<uchar>(), itr2 = t_img.begin<uchar>();
@ -224,6 +200,26 @@ void ImageManipulator::exec_method(int const t_nb_method,
return std::tuple<int, int, int>(rand_x, rand_y, size); return std::tuple<int, int, int>(rand_x, rand_y, size);
} }
void ImageManipulator::create_shape() noexcept
{
shape_(cv::Point{reference_.size().width, reference_.size().height},
std::min(reference_.size().width, reference_.size().height));
}
void ImageManipulator::create_controlled_shape() noexcept
{
float const coef = static_cast<float>(remaining_iter_)
/ static_cast<float>(total_iterations_);
int const min_size
= static_cast<int>((static_cast<float>(std::min(reference_.size().width,
reference_.size().height))
/ 2.0f)
* coef);
int const max_size = min_size * 2 + 1;
shape_(cv::Point{reference_.size().width, reference_.size().height}, max_size,
min_size);
}
/** /**
* Creates a temporary image on which a random square is drawn. If its * Creates a temporary image on which a random square is drawn. If its
* euclidian distance with the reference image proves to be an improvement from * euclidian distance with the reference image proves to be an improvement from
@ -233,31 +229,14 @@ void ImageManipulator::exec_method(int const t_nb_method,
* \param[in] t_controlled_size Enables controlled square size * \param[in] t_controlled_size Enables controlled square size
* \return Optional pair of cv::Mat and double * \return Optional pair of cv::Mat and double
*/ */
[[nodiscard]] auto ImageManipulator::create_candidate( [[nodiscard]] auto
bool const t_controlled_size = false) ImageManipulator::create_candidate(bool const t_controlled_size = false)
{ {
auto temp_img = generated_image_.clone(); auto temp_img = generated_image_.clone();
auto create_shape = [&]() {
return shape_(cv::Point{reference_.size().width, reference_.size().height},
std::min(reference_.size().width, reference_.size().height));
};
auto create_controlled_shape = [&]() {
float const coef = static_cast<float>(remaining_iter_)
/ static_cast<float>(total_iterations_);
int const min_size = static_cast<int>(
(static_cast<float>(
std::min(reference_.size().width, reference_.size().height))
/ 2.0f)
* coef);
int const max_size = min_size * 2 + 1;
return shape_(cv::Point{reference_.size().width, reference_.size().height},
max_size, min_size);
};
auto const &color = colors_[rand() % colors_.size()]; auto const &color = colors_[rand() % colors_.size()];
if (t_controlled_size) { if (t_controlled_size) {
create_shape(); create_shape();
} } else {
else {
create_controlled_shape(); create_controlled_shape();
} }
draw_shape(temp_img, cv::Scalar{static_cast<double>(color[0]), draw_shape(temp_img, cv::Scalar{static_cast<double>(color[0]),
@ -353,8 +332,7 @@ void ImageManipulator::threaded_get_color(int const t_h)
* \param[in] t_size Size of the square * \param[in] t_size Size of the square
* \param[in] t_color Color of the square * \param[in] t_color Color of the square
*/ */
void ImageManipulator::draw_square(cv::Mat& t_img, void ImageManipulator::draw_square(cv::Mat &t_img, cv::Point const &t_top_left,
cv::Point const& t_top_left,
int const t_size, int const t_size,
cv::Scalar const &t_color) const cv::Scalar const &t_color) const
{ {
@ -418,8 +396,8 @@ void ImageManipulator::method1()
spdlog::debug("Beginning method1, initial difference: {}", diff_); spdlog::debug("Beginning method1, initial difference: {}", diff_);
while (remaining_iter_ > 0 && diff_ > 0.0) { while (remaining_iter_ > 0 && diff_ > 0.0) {
auto temp_image = generated_image_.clone(); auto temp_image = generated_image_.clone();
auto const [rand_x, rand_y, size] = get_square_values(); create_shape();
draw_square(temp_image, cv::Point{rand_x, rand_y}, size, random_color()); draw_shape(temp_image, random_color());
if (auto const new_diff = euclidian_distance(temp_image); if (auto const new_diff = euclidian_distance(temp_image);
new_diff < diff_) { new_diff < diff_) {
update_gen_image(temp_image, new_diff); update_gen_image(temp_image, new_diff);
@ -497,10 +475,8 @@ void ImageManipulator::method4(bool const t_controlled_size)
* \param[in] t_rows Number of rows the reference should be divided into * \param[in] t_rows Number of rows the reference should be divided into
* \param[in] t_submethod Method to be used on each tile * \param[in] t_submethod Method to be used on each tile
*/ */
void ImageManipulator::method5(bool const t_controlled_size, void ImageManipulator::method5(bool const t_controlled_size, int const t_cols,
int const t_cols, int const t_rows, int const t_submethod)
int const t_rows,
int const t_submethod)
{ {
spdlog::debug("Beginning method5, initial difference: {}", diff_); spdlog::debug("Beginning method5, initial difference: {}", diff_);
spdlog::debug("Running on {} threads", thread_nbr); spdlog::debug("Running on {} threads", thread_nbr);

View File

@ -1,4 +1,5 @@
#include "parseargs.hh" #include "parseargs.hh"
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <iostream> #include <iostream>
@ -19,15 +20,13 @@ namespace po = boost::program_options;
* \param[out] t_input Input path * \param[out] t_input Input path
* \param[out] t_output Output path * \param[out] t_output Output path
*/ */
void processFilenames(po::variables_map const& t_vm, void processFilenames(po::variables_map const &t_vm, path const &t_input,
path const& t_input,
path &t_output) path &t_output)
{ {
if (!t_vm.count("output")) { if (!t_vm.count("output")) {
t_output.replace_filename("output_" t_output.replace_filename("output_"
+ std::string{t_input.filename().string()}); + std::string{t_input.filename().string()});
} } else if (!t_output.has_extension()) {
else if (!t_output.has_extension()) {
t_output.replace_extension(".png"); t_output.replace_extension(".png");
} }
} }
@ -46,25 +45,26 @@ void processFilenames(po::variables_map const& t_vm,
{ {
ParsedArgs ret{}; ParsedArgs ret{};
po::options_description desc("Allowed options"); po::options_description desc("Allowed options");
desc.add_options()("help,h", "Display this help message")( desc.add_options()
"input,i", po::value<path>(), "Input image")( ("help,h", "Display this help message")
"output,o", po::value<path>(), ("input,i", po::value<path>(), "Input image")
"Image output path (default: \"output_\" + input path)")( ("output,o", po::value<path>(),
"iterations,n", po::value<int>(), "Number of iterations (default: 2000)")( "Image output path (default: \"output_\" + input path)")
"method,m", po::value<int>(), "Method number to be used (default: 1)")( ("iterations,n", po::value<int>(), "Number of iterations (default: 2000)")
"form,f", po::value<int>(), "Select shape (1:square, 2:triangle)")( ("method,m", po::value<int>(), "Method number to be used (default: 1)")
"cols,c", po::value<int>(), ("form,f", po::value<int>(), "Select shape (1:square, 2:triangle)")
("cols,c", po::value<int>(),
"For method 5 only, number of columns the reference image should be " "For method 5 only, number of columns the reference image should be "
"divided into. If the value is equal to 0, then it will be assumed " "divided into. If the value is equal to 0, then it will be assumed "
"there will be as many rows as there are collumns. (default: 0)")( "there will be as many rows as there are collumns. (default: 0)")
"rows,r", po::value<int>(), ("rows,r", po::value<int>(),
"For method 5 only, number of rows the reference image should be " "For method 5 only, number of rows the reference image should be "
"divided into. (default: 1)")( "divided into. (default: 1)")
"submethod,S", po::value<int>(), ("submethod,S", po::value<int>(),
"Sub-method that will be used to generate the individual tiles from " "Sub-method that will be used to generate the individual tiles from "
"method 5. (default: 1)")("size,s", "method 5. (default: 1)")
"Enables controlled size of the random shapes")( ("size,s", "Enables controlled size of the random shapes")
"verbose,v", "Enables verbosity"); ("verbose,v", "Enables verbosity");
po::variables_map vm; po::variables_map vm;
po::store(po::parse_command_line(t_ac, t_av, desc), vm); po::store(po::parse_command_line(t_ac, t_av, desc), vm);
po::notify(vm); po::notify(vm);
@ -84,14 +84,9 @@ void processFilenames(po::variables_map const& t_vm,
: DEFAULT_ITERATIONS; : DEFAULT_ITERATIONS;
ret.method = vm.count("method") ? vm["method"].as<int>() : 1; ret.method = vm.count("method") ? vm["method"].as<int>() : 1;
switch (vm.count("form") ? vm["form"].as<int>() : 1) { switch (vm.count("form") ? vm["form"].as<int>() : 1) {
case 2: case 2: ret.shape = Shape::ShapeType::Triangle; break;
ret.shape= Shape::ShapeType::Triangle; case 1: [[fallthrough]];
break; default: ret.shape = Shape::ShapeType::Square; break;
case 1:
[[fallthrough]];
default:
ret.shape = Shape::ShapeType::Square;
break;
} }
ret.cols = vm.count("cols") ? vm["cols"].as<int>() : 0; ret.cols = vm.count("cols") ? vm["cols"].as<int>() : 0;

View File

@ -1,4 +1,5 @@
#include "shapes.hh" #include "shapes.hh"
#include <cmath> #include <cmath>
#include <utility> #include <utility>
@ -11,17 +12,13 @@ Shape::Shape(Shape::ShapeType const t_type) : type_{t_type}
nb_points_ = 3; nb_points_ = 3;
break; break;
} }
case ShapeType::Square: case ShapeType::Square: [[fallthrough]];
[[fallthrough]]; default: nb_points_ = 4; break;
default:
nb_points_ = 4;
break;
} }
} }
Shape::Shape(Shape &&other) noexcept Shape::Shape(Shape &&other) noexcept
: type_{std::move(other.type_)}, : type_{std::move(other.type_)}, points_{std::move(other.points_)},
points_{std::move(other.points_)},
nb_points_{std::move(other.nb_points_)} nb_points_{std::move(other.nb_points_)}
{ {
} }
@ -34,8 +31,7 @@ Shape::Shape(Shape&& other) noexcept
* for * for
* \return Array of points describing the shape * \return Array of points describing the shape
*/ */
void Shape::operator()(cv::Point&& t_max_pos, void Shape::operator()(cv::Point &&t_max_pos, int const t_max_size,
int const t_max_size,
int const t_min_size) noexcept int const t_min_size) noexcept
{ {
int const size = (rand() % (t_max_size - t_min_size)) + t_min_size; int const size = (rand() % (t_max_size - t_min_size)) + t_min_size;
@ -43,13 +39,11 @@ void Shape::operator()(cv::Point&& t_max_pos,
= {rand() % (t_max_pos.x - size), rand() % (t_max_pos.y - size)}; = {rand() % (t_max_pos.x - size), rand() % (t_max_pos.y - size)};
if (type_ == ShapeType::Triangle) { if (type_ == ShapeType::Triangle) {
create_triangle_points(top_left, size); create_triangle_points(top_left, size);
} } else { // ShapeType::Square
else { // ShapeType::Square
create_square_points(top_left, size); create_square_points(top_left, size);
} }
} }
void Shape::create_triangle_points(cv::Point const &t_top_left, void Shape::create_triangle_points(cv::Point const &t_top_left,
int const t_size) noexcept int const t_size) noexcept
{ {