Skip to content

Commit 662aee2

Browse files
committed
Fix irregular::Solution::write_svg
1 parent f5e1a84 commit 662aee2

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

include/packingsolver/irregular/solution.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ class Solution
176176
/** Write the solution to an SVG file. */
177177
void write_svg(
178178
const std::string& file_path,
179-
BinPos bin_pos) const;
179+
BinPos bin_pos,
180+
bool scaled = false) const;
180181

181182
/** Export solution characteristics to a JSON structure. */
182183
nlohmann::json to_json() const;

src/irregular/solution.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ void Solution::write(
420420

421421
void Solution::write_svg(
422422
const std::string& file_path,
423-
BinPos bin_pos) const
423+
BinPos bin_pos,
424+
bool scaled) const
424425
{
425426
if (file_path.empty())
426427
return;
@@ -433,22 +434,26 @@ void Solution::write_svg(
433434

434435
const SolutionBin& bin = this->bin(bin_pos);
435436
const BinType& bin_type = instance().bin_type(bin.bin_type_id);
436-
LengthDbl width = (bin_type.x_max - bin_type.x_min);
437-
LengthDbl height = (bin_type.y_max - bin_type.y_min);
437+
const Shape& bin_shape = (scaled)? bin_type.shape_scaled: bin_type.shape_orig;
438+
auto bin_mm = bin_shape.compute_min_max();
439+
LengthDbl width = (bin_mm.second.x - bin_mm.first.x);
440+
LengthDbl height = (bin_mm.second.y - bin_mm.first.y);
438441

439442
std::string s = "<svg viewBox=\""
440-
+ std::to_string(bin_type.x_min)
441-
+ " " + std::to_string(-bin_type.y_min - height)
443+
+ std::to_string(bin_mm.first.x)
444+
+ " " + std::to_string(-bin_mm.first.y - height)
442445
+ " " + std::to_string(width)
443446
+ " " + std::to_string(height)
444447
+ "\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n";
445448
file << s;
446449

447450
// Write bin.
448451
file << "<g>" << std::endl;
449-
file << bin_type.shape_scaled.to_svg();
450-
for (const Defect& defect: bin_type.defects)
451-
file << defect.shape_scaled.to_svg("red");
452+
file << bin_shape.to_svg();
453+
for (const Defect& defect: bin_type.defects) {
454+
const ShapeWithHoles& defect_shape = (scaled)? defect.shape_scaled: defect.shape_orig;
455+
file << defect_shape.to_svg("red");
456+
}
452457
file << "</g>" << std::endl;
453458

454459
// Write items.
@@ -460,17 +465,21 @@ void Solution::write_svg(
460465
LengthDbl y_min = std::numeric_limits<LengthDbl>::infinity();
461466
LengthDbl y_max = -std::numeric_limits<LengthDbl>::infinity();
462467

468+
Point bl_corner = item.bl_corner;
469+
if (scaled)
470+
bl_corner = this->instance().parameters().scale_value * bl_corner;
471+
463472
file << "<g>" << std::endl;
464473
for (const ItemShape& item_shape: item_type.shapes) {
465-
ShapeWithHoles shape = item_shape.shape_scaled;
474+
ShapeWithHoles shape = (scaled)? item_shape.shape_scaled: item_shape.shape_orig;
466475

467476
// Apply mirroring.
468477
if (item.mirror)
469478
shape.shape = shape.shape.axial_symmetry_y_axis();
470479
// Apply angles.
471480
shape.shape = shape.shape.rotate(item.angle);
472481
// Apply shift.
473-
shape.shape.shift(item.bl_corner.x, item.bl_corner.y);
482+
shape.shape.shift(bl_corner.x, bl_corner.y);
474483

475484
for (Counter hole_pos = 0;
476485
hole_pos < (Counter)item_shape.shape_scaled.holes.size();
@@ -481,7 +490,7 @@ void Solution::write_svg(
481490
// Apply angles.
482491
shape.holes[hole_pos] = shape.holes[hole_pos].rotate(item.angle);
483492
// Apply shift.
484-
shape.holes[hole_pos].shift(item.bl_corner.x, item.bl_corner.y);
493+
shape.holes[hole_pos].shift(bl_corner.x, bl_corner.y);
485494
}
486495

487496
file << shape.to_svg("blue");

0 commit comments

Comments
 (0)