Skip to content

Refactor shape code #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/packingsolver/irregular/shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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 /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Expand Down
52 changes: 45 additions & 7 deletions src/irregular/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,26 @@ 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)
{
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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -336,13 +378,9 @@ std::vector<ShapeElement> 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;
Expand Down
33 changes: 2 additions & 31 deletions src/irregular/shape_convex_hull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<Point> convex_hull;
Expand Down
Loading