Skip to content

Commit 7b43a1a

Browse files
committed
Fix ShapeElement::length() for clockwise circular arcs
1 parent 9bbaab7 commit 7b43a1a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/irregular/shape.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ LengthDbl ShapeElement::length() const
147147
return distance(this->start, this->end);
148148
case ShapeElementType::CircularArc:
149149
LengthDbl r = distance(center, start);
150-
return angle_radian(start - center, end - center) * r;
150+
if (anticlockwise) {
151+
return angle_radian(start - center, end - center) * r;
152+
} else {
153+
return angle_radian(end - center, start - center) * r;
154+
}
151155
}
152156
return -1;
153157
}

test/irregular/shape_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,30 @@
55
using namespace packingsolver;
66
using namespace packingsolver::irregular;
77

8+
struct ShapeElementLengthTestParams
9+
{
10+
ShapeElement element;
11+
LengthDbl expected_length;
12+
};
13+
14+
class IrregularShapeElementLengthTest: public testing::TestWithParam<ShapeElementLengthTestParams> { };
15+
16+
TEST_P(IrregularShapeElementLengthTest, ShapeElementLength)
17+
{
18+
ShapeElementLengthTestParams test_params = GetParam();
19+
EXPECT_TRUE(equal(test_params.element.length(), test_params.expected_length));
20+
}
21+
22+
INSTANTIATE_TEST_SUITE_P(
23+
Irregular,
24+
IrregularShapeElementLengthTest,
25+
testing::ValuesIn(std::vector<ShapeElementLengthTestParams>{
26+
{build_shape({{0, 0}, {0, 1}}, true).elements.front(), 1 },
27+
{build_shape({{1, 0}, {0, 0, 1}, {0, 1}}, true).elements.front(), M_PI / 2 },
28+
{build_shape({{1, 0}, {0, 0, -1}, {0, -1}}, true).elements.front(), M_PI / 2 },
29+
}));
30+
31+
832
struct CleanShapeTestParams
933
{
1034
Shape shape;

0 commit comments

Comments
 (0)