Skip to content

Commit

Permalink
refactor Polygon & Board building
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaslepoix committed Nov 1, 2022
1 parent 78e7d11 commit 7d3ebcd
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 135 deletions.
29 changes: 28 additions & 1 deletion src/domain/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <utility>

#include "utils/signum.hpp"
#include "utils/vector_utils.hpp"
#include "conflicts/conflict_edge_in_polygon.hpp"

#include "board.hpp"
Expand Down Expand Up @@ -66,7 +67,33 @@ void sort_points_by_vector_orientation(vector<Point>& points, Point const& vecto
}

//******************************************************************************
Board::Board(vector<unique_ptr<Polygon>>& polygons)
unique_ptr<Board> Board::Builder::build() {
return make_unique<Board>(move(polygons));
}

//******************************************************************************
void Board::Builder::add_polygon(string const& name, initializer_list<Point> points) {
polygons.push_back(make_unique<Polygon>(name, from_init_list(points)));
}

//******************************************************************************
void Board::Builder::add_polygon(string const& name, vector<unique_ptr<Point const>>&& points) {
polygons.push_back(make_unique<Polygon>(name, move(points)));
}

//******************************************************************************
void Board::Builder::add_polygon_from_box(string const& name, Point const p1, Point const p3) {
vector<unique_ptr<Point const>> points(4);
points[0] = make_unique<Point const>(p1.x, p1.y);
points[1] = make_unique<Point const>(p1.x, p3.y);
points[2] = make_unique<Point const>(p3.x, p3.y);
points[3] = make_unique<Point const>(p3.x, p1.y);

polygons.push_back(make_unique<Polygon>(name, move(points)));
}

//******************************************************************************
Board::Board(vector<unique_ptr<Polygon>>&& polygons)
: conflict_manager(&line_policy_manager)
, line_policy_manager(params, &conflict_manager)
, polygons(move(polygons)) {
Expand Down
20 changes: 15 additions & 5 deletions src/domain/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,23 @@ class Board : public Entity, public Visitable<Board, EntityVisitor> {
std::vector<Edge*> edges;

public:
Params params;
//**************************************************************************
class Builder {
public:

void add_polygon(std::string const& name, std::initializer_list<Point> points);
void add_polygon(std::string const& name, std::vector<std::unique_ptr<Point const>>&& points);
void add_polygon_from_box(std::string const& name, Point const p1, Point const p3);

[[nodiscard]] std::unique_ptr<Board> build();

// Board(std::initializer_list<Polygon> _polygons);
explicit Board(std::vector<std::unique_ptr<Polygon>>& polygons);
Board(); // TODO
private:
std::vector<std::unique_ptr<Polygon>> polygons;
};

Params params;

void add_polygon(Polygon const polygon); // TODO
explicit Board(std::vector<std::unique_ptr<Polygon>>&& polygons);

/// Mesh resolution independant detection tasks
///*************************************************************************
Expand Down
81 changes: 24 additions & 57 deletions src/domain/geometrics/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,29 @@
using namespace std;

//******************************************************************************
Polygon::Polygon(initializer_list<Point> points)
: bounding({ begin(points)->x, begin(points)->x, begin(points)->y, begin(points)->y }) {
for(auto const& point : points)
this->points.push_back(make_unique<Point>(point)); // TODO do not use initializer_list because of copies
this->points.shrink_to_fit();

rotation = detect_rotation(this->points);

Point const* prev = this->points.back().get();
for(auto const& point : this->points) {
edges.push_back(make_unique<Edge>(prev, point.get()));
prev = point.get();
}
edges.shrink_to_fit();

detect_bounding();
Polygon::Polygon(string const& name, vector<unique_ptr<Point const>>&& points)
: rotation(detect_rotation(points))
, bounding(detect_bounding(points))
, name(name)
, points(move(points))
, edges(detect_edges(this->points))
{
detect_edge_normal();
}

//******************************************************************************
Polygon::Polygon(string name, initializer_list<Point> points)
: bounding({ begin(points)->x, begin(points)->x, begin(points)->y, begin(points)->y })
, name(move(name)) {
for(auto const& point : points)
this->points.push_back(make_unique<Point>(point)); // TODO do not use initializer_list because of copies
this->points.shrink_to_fit();

rotation = detect_rotation(this->points);

Point const* prev = this->points.back().get();
for(auto const& point : this->points) {
edges.push_back(make_unique<Edge>(prev, point.get()));
prev = point.get();
}
edges.shrink_to_fit();

detect_bounding();
detect_edge_normal();
}

//******************************************************************************
/*
Polygon::Polygon(vector<unique_ptr<Point const>> _points)
: points(_points) {
for(unique_ptr<Point const>& point : points) {
if(point->x < bounding[XMIN]) bounding[XMIN] = point->x;
if(point->x > bounding[XMAX]) bounding[XMAX] = point->x;
if(point->y < bounding[YMIN]) bounding[YMIN] = point->y;
if(point->y > bounding[YMAX]) bounding[YMAX] = point->y;
}
points.shrink_to_fit();
rotation = detect_rotation(points);
vector<unique_ptr<Edge>> detect_edges(vector<unique_ptr<Point const>> const& points) {
vector<unique_ptr<Edge>> edges;

Point const* prev = points.back().get();
for(unique_ptr<Point const>& point : points) {
for(auto const & point : points) {
edges.push_back(make_unique<Edge>(prev, point.get()));
prev = point.get();
}
edges.shrink_to_fit();

detect_edge_normal();
return edges;
}
*/

/// Cf. https://rosettacode.org/wiki/Shoelace_formula_for_polygonal_area#C.2B.2B
/// Cf. https://www.baeldung.com/cs/list-polygon-points-clockwise
Expand All @@ -89,7 +48,7 @@ Polygon::Polygon(vector<unique_ptr<Point const>> _points)
/// FDTD mesh is orthogonal.
///*****************************************************************************
template<class T>
Polygon::Rotation detect_rotation(T const& points) {
Polygon::Rotation detect_rotation(T const& points) noexcept {
double left_sum = 0.0;
double right_sum = 0.0;

Expand All @@ -112,17 +71,25 @@ Polygon::Rotation detect_rotation(T const& points) {
}

//******************************************************************************
template Polygon::Rotation detect_rotation(std::vector<std::unique_ptr<Point const>> const&);
template Polygon::Rotation detect_rotation(std::vector<Point const*> const&);
template Polygon::Rotation detect_rotation(std::vector<std::unique_ptr<Point const>> const&) noexcept;
template Polygon::Rotation detect_rotation(std::vector<Point const*> const&) noexcept;

//******************************************************************************
void Polygon::detect_bounding() {
Bounding2D detect_bounding(vector<unique_ptr<Point const>> const& points) noexcept {
Bounding2D bounding({
(*begin(points))->x,
(*begin(points))->x,
(*begin(points))->y,
(*begin(points))->y });

for(auto const& point : points) {
if(point->x < bounding[XMIN]) bounding[XMIN] = point->x;
if(point->x > bounding[XMAX]) bounding[XMAX] = point->x;
if(point->y < bounding[YMIN]) bounding[YMIN] = point->y;
if(point->y > bounding[YMAX]) bounding[YMAX] = point->y;
}

return bounding;
}

/// YMAX | YMAX
Expand All @@ -137,7 +104,7 @@ void Polygon::detect_bounding() {
/// YMIN | YMIN
/// CW | CCW
///*****************************************************************************
void Polygon::detect_edge_normal() {
void Polygon::detect_edge_normal() noexcept {
switch(rotation) {
case Rotation::CW:
for(auto const& edge : edges) {
Expand Down
29 changes: 16 additions & 13 deletions src/domain/geometrics/polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,32 @@ class Polygon
, public IConflictOrigin
/*, public IMeshLineOrigin*/ {
private:
void detect_bounding(); // TODO
void detect_edge_normal();
void detect_edge_normal() noexcept;

public:
enum class Rotation {
// UNKNOWN,
CW,
CCW,
COLINEAR
} rotation;
} const rotation;

enum class Type {
SHAPE,
PORT
} type; // TODO usefull?

Bounding2D bounding;
std::string name; // TODO
Bounding2D const bounding;
std::string const name;

// TODO rm unique_ptr ?
std::vector<std::unique_ptr<Point const>> points;
std::vector<std::unique_ptr<Point const>> const points;

/// edge[0] is between points[n] & points[0]
/// edge[x] is between points[x-1] & points[x]
///*************************************************************************
std::vector<std::unique_ptr<Edge>> edges;
std::vector<std::unique_ptr<Edge>> const edges;

explicit Polygon(std::initializer_list<Point> points);
Polygon(std::string name, std::initializer_list<Point> points);
Polygon(std::string const& name, std::vector<std::unique_ptr<Point const>>&& points);

// relation::PolygonEdge relation_to(Edge const* edge);
relation::PolygonPoint relation_to(Point const& point) const;
Expand All @@ -72,6 +69,12 @@ class Polygon

/// These are the two declaration authorized.
///*****************************************************************************
template<class T> Polygon::Rotation detect_rotation(T const& points);
extern template Polygon::Rotation detect_rotation(std::vector<std::unique_ptr<Point const>> const&);
extern template Polygon::Rotation detect_rotation(std::vector<Point const*> const&);
template<class T> Polygon::Rotation detect_rotation(T const& points) noexcept;
extern template Polygon::Rotation detect_rotation(std::vector<std::unique_ptr<Point const>> const&) noexcept;
extern template Polygon::Rotation detect_rotation(std::vector<Point const*> const&) noexcept;

//******************************************************************************
Bounding2D detect_bounding(std::vector<std::unique_ptr<Point const>> const& points) noexcept;

//******************************************************************************
std::vector<std::unique_ptr<Edge>> detect_edges(std::vector<std::unique_ptr<Point const>> const& points);
2 changes: 1 addition & 1 deletion src/domain/meshline_policy_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <algorithm>
#include <limits>

#include "utils/vector_view.hpp"
#include "utils/vector_utils.hpp"
#include "conflict_manager.hpp"

#include "meshline_policy_manager.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/infra/serializers/serializer_to_plantuml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "domain/mesh/interval.hpp"
#include "domain/board.hpp"
#include "infra/utils/to_string.hpp"
#include "utils/vector_view.hpp"
#include "utils/vector_utils.hpp"

#include "serializer_to_plantuml.hpp"

Expand Down
2 changes: 1 addition & 1 deletion src/infra/serializers/serializer_to_prettyprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "domain/mesh/interval.hpp"
#include "domain/board.hpp"
#include "infra/utils/to_string.hpp"
#include "utils/vector_view.hpp"
#include "utils/vector_utils.hpp"
#include "utils/color.hpp"

#include "serializer_to_prettyprint.hpp"
Expand Down
Loading

0 comments on commit 7d3ebcd

Please sign in to comment.