genetic-images/src/shapes.cc

64 lines
2.0 KiB
C++

#include "shapes.hh"
#include <cmath>
#include <utility>
using point_arr = std::array<cv::Point, 4>;
Shape::Shape(Shape::ShapeType const t_type) : type_{t_type}
{
switch (t_type) {
case ShapeType::Triangle: nb_points_ = 3; break;
case ShapeType::Square: nb_points_ = 4; break;
default: nb_points_ = 4; break;
}
}
Shape::Shape(Shape &&other) noexcept
: type_{std::move(other.type_)}, points_{std::move(other.points_)},
nb_points_{std::move(other.nb_points_)}
{
}
/**
* Generates all the needed points for the corresponding shape described in
* \ref type_.
*
* \param t_max_pos Bottom-rightmost point of the image the shape is generated
* for
* \return Array of points describing the shape
*/
void Shape::update(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 + 1;
cv::Point const top_left
= {rand() % (t_max_pos.x - size + 1), rand() % (t_max_pos.y - size + 1)};
if (type_ == ShapeType::Triangle) {
create_triangle_points(top_left, size);
} 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
{
bool top_left = rand() % 2 == 0;
points_ = {
cv::Point{top_left ? t_top_left.x : t_top_left.x + t_size, t_top_left.y},
cv::Point{top_left ? t_top_left.x + t_size : t_top_left.x,
t_top_left.y + rand() % t_size},
cv::Point{t_top_left.x + rand() % t_size, t_top_left.y + t_size},
cv::Point{0, 0}};
}
void Shape::create_square_points(cv::Point const &t_top_left,
int const t_size) noexcept
{
points_ = {cv::Point{t_top_left.x, t_top_left.y},
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},
cv::Point{t_top_left.x + t_size, t_top_left.y}};
}