diff --git a/include/packingsolver/irregular/shape.hpp b/include/packingsolver/irregular/shape.hpp index ece2c655..a4577960 100644 --- a/include/packingsolver/irregular/shape.hpp +++ b/include/packingsolver/irregular/shape.hpp @@ -43,6 +43,8 @@ struct Point Point rotate_radians(Angle angle) const; + Point rotate_radians(const Point& center, Angle angle) const; + Point axial_symmetry_identity_line() const; Point axial_symmetry_y_axis() const; @@ -67,10 +69,17 @@ Point operator-( LengthDbl norm( const Point& vector); +LengthDbl squared_norm( + const Point& vector); + LengthDbl distance( const Point& point_1, const Point& point_2); +LengthDbl squared_distance( + const Point& point_1, + const Point& point_2); + LengthDbl dot_product( const Point& vector_1, const Point& vector_2); @@ -102,6 +111,11 @@ inline AreaDbl compute_area( - (point_1.x - point_2.x) * (point_3.y - point_1.y)); } +int counter_clockwise( + const Point& point_1, + const Point& point_2, + const Point& point_3); + //////////////////////////////////////////////////////////////////////////////// ///////////////////////////////// ShapeElement ///////////////////////////////// //////////////////////////////////////////////////////////////////////////////// diff --git a/src/irregular/shape.cpp b/src/irregular/shape.cpp index bc56ffed..1e6c0633 100644 --- a/src/irregular/shape.cpp +++ b/src/irregular/shape.cpp @@ -34,6 +34,12 @@ LengthDbl irregular::norm( return std::sqrt(vector.x * vector.x + vector.y * vector.y); } +LengthDbl irregular::squared_norm( + const Point& vector) +{ + return vector.x * vector.x + vector.y * vector.y; +} + LengthDbl irregular::distance( const Point& point_1, const Point& point_2) @@ -41,6 +47,13 @@ LengthDbl irregular::distance( return norm(point_2 - point_1); } +LengthDbl irregular::squared_distance( + const Point& point_1, + const Point& point_2) +{ + return squared_norm(point_2 - point_1); +} + LengthDbl irregular::dot_product( const Point& vector_1, const Point& vector_2) @@ -102,6 +115,20 @@ Point Point::rotate_radians( return point_out; } +Point Point::rotate_radians( + const Point& center, + Angle angle) const +{ + Point point_out; + point_out.x = center.x + + std::cos(angle) * (this->x - center.x) + - std::sin(angle) * (this->y - center.y); + point_out.y = center.y + + std::sin(angle) * (this->x - center.x) + + std::cos(angle) * (this->y - center.y); + return point_out; +} + Point Point::axial_symmetry_identity_line() const { Point point_out; @@ -145,6 +172,21 @@ Angle irregular::angle_radian( return a; } +int irregular::counter_clockwise( + const Point& point_1, + const Point& point_2, + const Point& point_3) +{ + AreaDbl area = (point_2.x - point_1.x) * (point_3.y - point_1.y) + - (point_2.y - point_1.y) * (point_3.x - point_1.x); + if (strictly_greater(area, 0)) { + return -1; + } else if (strictly_lesser(area, 0)) { + return 1; + } + return 0; +} + //////////////////////////////////////////////////////////////////////////////// ///////////////////////////////// ShapeElement ///////////////////////////////// //////////////////////////////////////////////////////////////////////////////// @@ -336,13 +378,9 @@ std::vector irregular::approximate_circular_arc_by_line_segments( Angle angle_cur = (angle * (line_segment_id + 1)) / (number_of_line_segments - 1); if (!circular_arc.anticlockwise) angle_cur *= -1; - Point point_circle; - point_circle.x = circular_arc.center.x - + std::cos(angle_cur) * (circular_arc.start.x - circular_arc.center.x) - - std::sin(angle_cur) * (circular_arc.start.y - circular_arc.center.y); - point_circle.y = circular_arc.center.y - + std::sin(angle_cur) * (circular_arc.start.x - circular_arc.center.x) - + std::cos(angle_cur) * (circular_arc.start.y - circular_arc.center.y); + Point point_circle = circular_arc.start.rotate_radians( + circular_arc.center, + angle_cur); Point point_cur; if ((outer && !circular_arc.anticlockwise) || (!outer && circular_arc.anticlockwise)) { point_cur = point_circle; diff --git a/src/irregular/shape_convex_hull.cpp b/src/irregular/shape_convex_hull.cpp index 985d9f4b..2b639b0c 100644 --- a/src/irregular/shape_convex_hull.cpp +++ b/src/irregular/shape_convex_hull.cpp @@ -3,35 +3,6 @@ using namespace packingsolver; using namespace packingsolver::irregular; -namespace -{ - -int counter_clockwise( - const Point& point_1, - const Point& point_2, - const Point& point_3) -{ - AreaDbl area = (point_2.x - point_1.x) * (point_3.y - point_1.y) - - (point_2.y - point_1.y) * (point_3.x - point_1.x); - if (strictly_greater(area, 0)) { - return -1; - } else if (strictly_lesser(area, 0)) { - return 1; - } - return 0; -} - -LengthDbl square_distance( - const Point& point_1, - const Point& point_2) -{ - LengthDbl dx = point_1.x - point_2.x; - LengthDbl dy = point_1.y - point_2.y; - return dx * dx + dy * dy; -} - -} - Shape irregular::convex_hull( const Shape& shape) { @@ -71,8 +42,8 @@ Shape irregular::convex_hull( point_2); if (order != 0) return (order == -1); - return square_distance(point_least_y, point_1) - < square_distance(point_least_y, point_2); + return squared_distance(point_least_y, point_1) + < squared_distance(point_least_y, point_2); }); std::vector convex_hull;