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

View File

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

View File

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

View File

@ -1,4 +1,5 @@
#include "methods.hh"
#include <algorithm>
#include <future>
#include <optional>
@ -18,15 +19,11 @@ static auto const thread_nbr = std::thread::hardware_concurrency();
* \param[in] other Element to copy
*/
ImageManipulator::ImageManipulator(const ImageManipulator &other)
: colors_{other.colors_},
reference_{other.reference_},
generated_image_{other.generated_image_},
shape_{other.shape_},
output_path_{other.output_path_},
diff_{other.diff_},
: colors_{other.colors_}, reference_{other.reference_},
generated_image_{other.generated_image_}, shape_{other.shape_},
output_path_{other.output_path_}, diff_{other.diff_},
total_iterations_{other.total_iterations_},
remaining_iter_{other.remaining_iter_},
width_{other.width_},
remaining_iter_{other.remaining_iter_}, width_{other.width_},
height_{other.height_}
{
}
@ -38,15 +35,13 @@ ImageManipulator::ImageManipulator(const ImageManipulator& other)
* \param[in] other Element to move
*/
ImageManipulator::ImageManipulator(ImageManipulator &&other) noexcept
: colors_{std::move(other.colors_)},
reference_{std::move(other.reference_)},
generated_image_{std::move(other.generated_image_)},
shape_{std::move(other.shape_)},
: colors_{std::move(other.colors_)}, reference_{std::move(
other.reference_)},
generated_image_{std::move(other.generated_image_)}, shape_{std::move(
other.shape_)},
output_path_{std::move(other.output_path_)},
diff_{std::move(other.diff_)},
total_iterations_{other.total_iterations_},
remaining_iter_{other.remaining_iter_},
width_{other.width_},
diff_{std::move(other.diff_)}, total_iterations_{other.total_iterations_},
remaining_iter_{other.remaining_iter_}, width_{other.width_},
height_{other.height_}
{
}
@ -63,10 +58,9 @@ ImageManipulator::ImageManipulator(std::string const t_input_path,
std::string const t_output_path,
int const t_iterations,
Shape::ShapeType const t_shape)
: reference_{cv::imread(t_input_path, cv::IMREAD_COLOR)},
shape_{Shape{t_shape}},
output_path_{t_output_path},
total_iterations_{t_iterations}
: reference_{cv::imread(t_input_path, cv::IMREAD_COLOR)}, shape_{Shape{
t_shape}},
output_path_{t_output_path}, total_iterations_{t_iterations}
{
if (!reference_.data) {
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,
int const t_iterations,
Shape::ShapeType const t_shape,
int const t_x,
int const t_y,
int const t_width,
int const t_height)
int const t_x, int const t_y,
int const t_width, int const t_height)
: reference_{t_origin_image(
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)})},
shape_{Shape{t_shape}},
total_iterations_{t_iterations}
shape_{Shape{t_shape}}, total_iterations_{t_iterations}
{
if (!reference_.data) {
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,
bool const t_controlled_size = false,
int const t_cols = 1,
int const t_rows = 0,
int const t_cols = 1, int const t_rows = 0,
int const t_submethod = 1)
{
switch (t_nb_method) {
case 1: {
method1();
break;
}
case 2: {
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;
}
case 1: method1(); break;
case 2: 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:
spdlog::error("Requested method {} is not implemented.", t_nb_method);
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
* \return double
*/
[[nodiscard]] auto ImageManipulator::euclidian_distance(
cv::Mat const& t_img) const noexcept -> double
[[nodiscard]] auto
ImageManipulator::euclidian_distance(cv::Mat const &t_img) const noexcept
-> double
{
double euclidian = 0.0;
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);
}
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
* 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
* \return Optional pair of cv::Mat and double
*/
[[nodiscard]] auto ImageManipulator::create_candidate(
bool const t_controlled_size = false)
[[nodiscard]] auto
ImageManipulator::create_candidate(bool const t_controlled_size = false)
{
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()];
if (t_controlled_size) {
create_shape();
}
else {
} else {
create_controlled_shape();
}
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_color Color of the square
*/
void ImageManipulator::draw_square(cv::Mat& t_img,
cv::Point const& t_top_left,
void ImageManipulator::draw_square(cv::Mat &t_img, cv::Point const &t_top_left,
int const t_size,
cv::Scalar const &t_color) const
{
@ -418,8 +396,8 @@ void ImageManipulator::method1()
spdlog::debug("Beginning method1, initial difference: {}", diff_);
while (remaining_iter_ > 0 && diff_ > 0.0) {
auto temp_image = generated_image_.clone();
auto const [rand_x, rand_y, size] = get_square_values();
draw_square(temp_image, cv::Point{rand_x, rand_y}, size, random_color());
create_shape();
draw_shape(temp_image, random_color());
if (auto const new_diff = euclidian_distance(temp_image);
new_diff < 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_submethod Method to be used on each tile
*/
void ImageManipulator::method5(bool const t_controlled_size,
int const t_cols,
int const t_rows,
int const t_submethod)
void ImageManipulator::method5(bool const t_controlled_size, int const t_cols,
int const t_rows, int const t_submethod)
{
spdlog::debug("Beginning method5, initial difference: {}", diff_);
spdlog::debug("Running on {} threads", thread_nbr);

View File

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

View File

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