2019-04-27 13:45:39 +00:00
|
|
|
#include "shapes.hh"
|
2019-04-27 14:27:22 +00:00
|
|
|
|
2019-04-27 13:45:39 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
using point_arr = std::array<cv::Point, 4>;
|
|
|
|
|
|
|
|
Shape::Shape(Shape::ShapeType const t_type) : type_{t_type}
|
|
|
|
{
|
2019-04-27 14:27:22 +00:00
|
|
|
switch (t_type) {
|
2019-04-27 16:04:32 +00:00
|
|
|
case ShapeType::Triangle: nb_points_ = 3; break;
|
|
|
|
case ShapeType::Square: nb_points_ = 4; break;
|
2019-04-27 14:27:22 +00:00
|
|
|
default: nb_points_ = 4; break;
|
|
|
|
}
|
2019-04-27 13:45:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 14:27:22 +00:00
|
|
|
Shape::Shape(Shape &&other) noexcept
|
|
|
|
: type_{std::move(other.type_)}, points_{std::move(other.points_)},
|
2019-04-27 13:45:39 +00:00
|
|
|
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
|
|
|
|
*/
|
2019-04-27 16:04:32 +00:00
|
|
|
void Shape::update(cv::Point &&t_max_pos, int const t_max_size,
|
|
|
|
int const t_min_size) noexcept
|
2019-04-27 13:45:39 +00:00
|
|
|
{
|
2019-04-28 17:30:23 +00:00
|
|
|
int const size = (rand() % (t_max_size - t_min_size)) + t_min_size + 1;
|
2019-04-27 14:27:22 +00:00
|
|
|
cv::Point const top_left
|
|
|
|
= {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
|
|
|
|
create_square_points(top_left, size);
|
|
|
|
}
|
2019-04-27 13:45:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 14:27:22 +00:00
|
|
|
void Shape::create_triangle_points(cv::Point const &t_top_left,
|
2019-04-27 13:45:39 +00:00
|
|
|
int const t_size) noexcept
|
|
|
|
{
|
2019-04-27 16:04:32 +00:00
|
|
|
bool top_left = rand() % 2 == 0;
|
2019-04-28 17:30:23 +00:00
|
|
|
points_ = {
|
2019-04-27 14:27:22 +00:00
|
|
|
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,
|
2019-04-27 16:04:32 +00:00
|
|
|
t_top_left.y + rand() % t_size},
|
2019-04-27 14:27:22 +00:00
|
|
|
cv::Point{t_top_left.x + rand() % t_size, t_top_left.y + t_size},
|
|
|
|
cv::Point{0, 0}};
|
2019-04-27 13:45:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 14:27:22 +00:00
|
|
|
void Shape::create_square_points(cv::Point const &t_top_left,
|
2019-04-27 13:45:39 +00:00
|
|
|
int const t_size) noexcept
|
|
|
|
{
|
2019-04-27 14:27:22 +00:00
|
|
|
points_ = {cv::Point{t_top_left.x, t_top_left.y},
|
|
|
|
cv::Point{t_top_left.x, t_top_left.y + t_size},
|
2019-04-27 16:04:32 +00:00
|
|
|
cv::Point{t_top_left.x + t_size, t_top_left.y + t_size},
|
2019-04-27 14:27:22 +00:00
|
|
|
cv::Point{t_top_left.x + t_size, t_top_left.y}};
|
2019-04-27 13:45:39 +00:00
|
|
|
}
|