Skip to content

Commit 5d0e70e

Browse files
committed
Refactor shape code
1 parent efc2bd7 commit 5d0e70e

File tree

3 files changed

+61
-38
lines changed

3 files changed

+61
-38
lines changed

Diff for: include/packingsolver/irregular/shape.hpp

+14
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ struct Point
4343

4444
Point rotate_radians(Angle angle) const;
4545

46+
Point rotate_radians(const Point& center, Angle angle) const;
47+
4648
Point axial_symmetry_identity_line() const;
4749

4850
Point axial_symmetry_y_axis() const;
@@ -67,10 +69,17 @@ Point operator-(
6769
LengthDbl norm(
6870
const Point& vector);
6971

72+
LengthDbl squared_norm(
73+
const Point& vector);
74+
7075
LengthDbl distance(
7176
const Point& point_1,
7277
const Point& point_2);
7378

79+
LengthDbl squared_distance(
80+
const Point& point_1,
81+
const Point& point_2);
82+
7483
LengthDbl dot_product(
7584
const Point& vector_1,
7685
const Point& vector_2);
@@ -102,6 +111,11 @@ inline AreaDbl compute_area(
102111
- (point_1.x - point_2.x) * (point_3.y - point_1.y));
103112
}
104113

114+
int counter_clockwise(
115+
const Point& point_1,
116+
const Point& point_2,
117+
const Point& point_3);
118+
105119
////////////////////////////////////////////////////////////////////////////////
106120
///////////////////////////////// ShapeElement /////////////////////////////////
107121
////////////////////////////////////////////////////////////////////////////////

Diff for: src/irregular/shape.cpp

+45-7
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,26 @@ LengthDbl irregular::norm(
3434
return std::sqrt(vector.x * vector.x + vector.y * vector.y);
3535
}
3636

37+
LengthDbl irregular::squared_norm(
38+
const Point& vector)
39+
{
40+
return vector.x * vector.x + vector.y * vector.y;
41+
}
42+
3743
LengthDbl irregular::distance(
3844
const Point& point_1,
3945
const Point& point_2)
4046
{
4147
return norm(point_2 - point_1);
4248
}
4349

50+
LengthDbl irregular::squared_distance(
51+
const Point& point_1,
52+
const Point& point_2)
53+
{
54+
return squared_norm(point_2 - point_1);
55+
}
56+
4457
LengthDbl irregular::dot_product(
4558
const Point& vector_1,
4659
const Point& vector_2)
@@ -102,6 +115,20 @@ Point Point::rotate_radians(
102115
return point_out;
103116
}
104117

118+
Point Point::rotate_radians(
119+
const Point& center,
120+
Angle angle) const
121+
{
122+
Point point_out;
123+
point_out.x = center.x
124+
+ std::cos(angle) * (this->x - center.x)
125+
- std::sin(angle) * (this->y - center.y);
126+
point_out.y = center.y
127+
+ std::sin(angle) * (this->x - center.x)
128+
+ std::cos(angle) * (this->y - center.y);
129+
return point_out;
130+
}
131+
105132
Point Point::axial_symmetry_identity_line() const
106133
{
107134
Point point_out;
@@ -145,6 +172,21 @@ Angle irregular::angle_radian(
145172
return a;
146173
}
147174

175+
int irregular::counter_clockwise(
176+
const Point& point_1,
177+
const Point& point_2,
178+
const Point& point_3)
179+
{
180+
AreaDbl area = (point_2.x - point_1.x) * (point_3.y - point_1.y)
181+
- (point_2.y - point_1.y) * (point_3.x - point_1.x);
182+
if (strictly_greater(area, 0)) {
183+
return -1;
184+
} else if (strictly_lesser(area, 0)) {
185+
return 1;
186+
}
187+
return 0;
188+
}
189+
148190
////////////////////////////////////////////////////////////////////////////////
149191
///////////////////////////////// ShapeElement /////////////////////////////////
150192
////////////////////////////////////////////////////////////////////////////////
@@ -398,13 +440,9 @@ std::vector<ShapeElement> irregular::approximate_circular_arc_by_line_segments(
398440
Angle angle_cur = (angle * (line_segment_id + 1)) / (number_of_line_segments - 1);
399441
if (!circular_arc.anticlockwise)
400442
angle_cur *= -1;
401-
Point point_circle;
402-
point_circle.x = circular_arc.center.x
403-
+ std::cos(angle_cur) * (circular_arc.start.x - circular_arc.center.x)
404-
- std::sin(angle_cur) * (circular_arc.start.y - circular_arc.center.y);
405-
point_circle.y = circular_arc.center.y
406-
+ std::sin(angle_cur) * (circular_arc.start.x - circular_arc.center.x)
407-
+ std::cos(angle_cur) * (circular_arc.start.y - circular_arc.center.y);
443+
Point point_circle = circular_arc.start.rotate_radians(
444+
circular_arc.center,
445+
angle_cur);
408446
Point point_cur;
409447
if ((outer && !circular_arc.anticlockwise) || (!outer && circular_arc.anticlockwise)) {
410448
point_cur = point_circle;

Diff for: src/irregular/shape_convex_hull.cpp

+2-31
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,6 @@
33
using namespace packingsolver;
44
using namespace packingsolver::irregular;
55

6-
namespace
7-
{
8-
9-
int counter_clockwise(
10-
const Point& point_1,
11-
const Point& point_2,
12-
const Point& point_3)
13-
{
14-
AreaDbl area = (point_2.x - point_1.x) * (point_3.y - point_1.y)
15-
- (point_2.y - point_1.y) * (point_3.x - point_1.x);
16-
if (strictly_greater(area, 0)) {
17-
return -1;
18-
} else if (strictly_lesser(area, 0)) {
19-
return 1;
20-
}
21-
return 0;
22-
}
23-
24-
LengthDbl square_distance(
25-
const Point& point_1,
26-
const Point& point_2)
27-
{
28-
LengthDbl dx = point_1.x - point_2.x;
29-
LengthDbl dy = point_1.y - point_2.y;
30-
return dx * dx + dy * dy;
31-
}
32-
33-
}
34-
356
Shape irregular::convex_hull(
367
const Shape& shape)
378
{
@@ -71,8 +42,8 @@ Shape irregular::convex_hull(
7142
point_2);
7243
if (order != 0)
7344
return (order == -1);
74-
return square_distance(point_least_y, point_1)
75-
< square_distance(point_least_y, point_2);
45+
return squared_distance(point_least_y, point_1)
46+
< squared_distance(point_least_y, point_2);
7647
});
7748

7849
std::vector<Point> convex_hull;

0 commit comments

Comments
 (0)