Skip to content

Commit 3e6807a

Browse files
committed
Implement compute_area function for triangles and use it in shape simpl.
1 parent bc19be9 commit 3e6807a

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

include/packingsolver/irregular/shape.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ using ItemShapePos = int64_t;
1414
using ShapePos = int64_t;
1515
using Counter = int64_t;
1616

17+
////////////////////////////////////////////////////////////////////////////////
18+
//////////////////////////////////// Point /////////////////////////////////////
19+
////////////////////////////////////////////////////////////////////////////////
20+
1721
/**
1822
* Structure for a point.
1923
*/
@@ -89,6 +93,21 @@ Angle angle_radian(
8993
const Point& vector_1,
9094
const Point& vector_2);
9195

96+
inline AreaDbl compute_area(
97+
const Point& point_1,
98+
const Point& point_2,
99+
const Point& point_3)
100+
{
101+
// https://en.wikipedia.org/wiki/Area_of_a_triangle#Using_coordinates
102+
return 0.5 * (
103+
(point_1.x - point_3.x) * (point_2.y - point_1.y)
104+
- (point_1.x - point_2.x) * (point_3.y - point_1.y));
105+
}
106+
107+
////////////////////////////////////////////////////////////////////////////////
108+
///////////////////////////////// ShapeElement /////////////////////////////////
109+
////////////////////////////////////////////////////////////////////////////////
110+
92111
enum class ShapeElementType
93112
{
94113
LineSegment,

src/irregular/shape_simplification.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,10 @@ AreaDbl compute_approximation_cost(
204204
return std::numeric_limits<Angle>::infinity();
205205
LengthDbl xp = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denom;
206206
LengthDbl yp = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom;
207-
AreaDbl cost = shape.copies * build_shape({
208-
{element.element.start.x}, {element.element.start.y},
207+
AreaDbl cost = shape.copies * compute_area(
208+
element.element.start,
209209
{xp, yp},
210-
{element.element.end.x, element.element.end.y}
211-
}).compute_area();
210+
element.element.end);
212211
if (cost < 0)
213212
return std::numeric_limits<Angle>::infinity();
214213
return cost;
@@ -218,11 +217,10 @@ AreaDbl compute_approximation_cost(
218217
}
219218
} else {
220219
// angle_next < M_PI
221-
Angle cost = shape.copies * build_shape({
222-
{element.element.start.x, element.element.start.y},
223-
{element_next.element.end.x, element_next.element.end.y},
224-
{element.element.end.x, element.element.end.y}
225-
}).compute_area();
220+
Angle cost = shape.copies * compute_area(
221+
element.element.start,
222+
element_next.element.end,
223+
element.element.end);
226224
if (cost < 0) {
227225
throw std::runtime_error(
228226
"irregular::compute_approximation_cost: outer; "
@@ -260,10 +258,10 @@ AreaDbl compute_approximation_cost(
260258
return std::numeric_limits<Angle>::infinity();
261259
LengthDbl xp = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denom;
262260
LengthDbl yp = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom;
263-
AreaDbl cost = shape.copies * build_shape({
264-
{element.element.start.x, element.element.start.y},
265-
{element.element.end.x, element.element.end.y},
266-
{xp, yp}}).compute_area();
261+
AreaDbl cost = shape.copies * compute_area(
262+
element.element.start,
263+
element.element.end,
264+
{xp, yp});
267265
if (cost < 0)
268266
return std::numeric_limits<Angle>::infinity();
269267
return cost;
@@ -273,11 +271,10 @@ AreaDbl compute_approximation_cost(
273271
}
274272
} else {
275273
// angle_next < M_PI
276-
Angle cost = shape.copies * build_shape({
277-
{element.element.start.x, element.element.start.y},
278-
{element.element.end.x, element.element.end.y},
279-
{element_next.element.end.x, element_next.element.end.y}
280-
}).compute_area();
274+
Angle cost = shape.copies * compute_area(
275+
element.element.start,
276+
element.element.end,
277+
element_next.element.end);
281278
if (cost < 0) {
282279
throw std::runtime_error(
283280
"irregular::compute_approximation_cost: inner; "
@@ -384,10 +381,10 @@ void apply_approximation(
384381
}
385382
LengthDbl xp = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denom;
386383
LengthDbl yp = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom;
387-
AreaDbl cost = build_shape({
388-
{element.element.start.x, element.element.start.y},
389-
{element.element.end.x, element.element.end.y},
390-
{xp, yp}}).compute_area();
384+
AreaDbl cost = compute_area(
385+
element.element.start,
386+
element.element.end,
387+
{xp, yp});
391388
element_prev.element.end = {xp, yp};
392389
element_next.element.start = {xp, yp};
393390
} else {

0 commit comments

Comments
 (0)