Skip to content

Commit

Permalink
Update JSON search tree output
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanf committed Dec 19, 2024
1 parent 0290d8b commit 2d9d522
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 148 deletions.
4 changes: 4 additions & 0 deletions include/packingsolver/irregular/instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ struct ShapeElement
ShapeElement axial_symmetry_y_axis() const;

std::string to_string() const;

nlohmann::json to_json() const;
};

enum class ShapeType
Expand Down Expand Up @@ -205,6 +207,8 @@ struct Shape

std::string to_string(Counter indentation) const;

nlohmann::json to_json() const;

std::string to_svg(double factor) const;

void write_svg(
Expand Down
145 changes: 80 additions & 65 deletions src/irregular/branching_scheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2392,41 +2392,66 @@ Solution BranchingScheme::to_solution(
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

nlohmann::json BranchingScheme::json_export_init()
nlohmann::json BranchingScheme::json_export_init() const
{
const auto& trapezoid_sets = trapezoid_sets_[(int)Direction::LeftToRightThenBottomToTop];
nlohmann::json json_init;
Counter i = 0;

// Bins.
// TODO
json_bins_init_ids_ = std::vector<Counter>(instance().number_of_bin_types(), -1);
for (BinTypeId bin_type_id = 0;
bin_type_id < instance().number_of_bin_types();
++bin_type_id) {
const BinType& bin_type = instance().bin_type(bin_type_id);

json_bins_init_ids_[bin_type_id] = i;
json_init[i][0] = {
{"Shape", bin_type.shape.to_json()},
{"FillColor", ""},
};
for (DefectId defect_id = 0;
defect_id < (DefectId)bin_type.defects.size();
++defect_id) {
const Defect& defect = bin_type.defects[defect_id];
json_init[i][defect_id + 1] = {
{"Shape", bin_type.shape.to_json()},
{"FillColor", "red"},
};
}
i++;
}

// Items.
json_items_init_ids_ = std::vector<std::vector<std::vector<Counter>>>(trapezoid_sets_.size());
for (TrapezoidSetId trapezoid_set_id = 0;
trapezoid_set_id < (TrapezoidSetId)trapezoid_sets.size();
++trapezoid_set_id) {
const TrapezoidSet& trapezoid_set = trapezoid_sets[trapezoid_set_id];
const ItemType& item_type = instance().item_type(trapezoid_set.item_type_id);
json_items_init_ids_ = std::vector<std::vector<std::unordered_map<double, Counter>>>(instance().number_of_item_types());
for (ItemTypeId item_type_id = 0;
item_type_id < instance().number_of_item_types();
++item_type_id) {
const ItemType& item_type = instance().item_type(item_type_id);

json_items_init_ids_[trapezoid_set_id] = std::vector<std::vector<Counter>>(trapezoid_sets_.size());
for (ItemShapePos item_shape_pos = 0;
item_shape_pos < (ItemShapePos)trapezoid_set.shapes.size();
++item_shape_pos) {
const auto& item_shape_trapezoids = trapezoid_set.shapes[item_shape_pos];

json_items_init_ids_[trapezoid_set_id][item_shape_pos] = std::vector<Counter>(trapezoid_sets_.size(), -1);
for (TrapezoidPos item_shape_trapezoid_pos = 0;
item_shape_trapezoid_pos < (TrapezoidPos)item_shape_trapezoids.size();
++item_shape_trapezoid_pos) {
json_items_init_ids_[trapezoid_set_id][item_shape_pos][item_shape_trapezoid_pos] = i;
// TODO
json_init[i] = {

};
i++;
json_items_init_ids_[item_type_id] = std::vector<std::unordered_map<double, Counter>>(2);
for (bool mirror: {false, true}) {

for (const auto& angle_range: item_type.allowed_rotations) {

json_items_init_ids_[item_type_id][mirror][angle_range.first] = i;

for (ItemShapePos item_shape_pos = 0;
item_shape_pos < (ItemShapePos)item_type.shapes.size();
++item_shape_pos) {
const ItemShape& item_shape = item_type.shapes[item_shape_pos];
Shape shape = (!mirror)?
item_shape.shape:
item_shape.shape.axial_symmetry_y_axis();
shape = shape.rotate(angle_range.first);
json_init[i][item_shape_pos] = {
{"Shape", shape.to_json()},
{"FillColor", "blue"},
};
}
}
}
i++;
}

return json_init;
Expand All @@ -2435,46 +2460,9 @@ nlohmann::json BranchingScheme::json_export_init()
nlohmann::json BranchingScheme::json_export(
const std::shared_ptr<Node>& node) const
{
nlohmann::json plot;
Counter i = 0;
// Plot bin.
plot[i];
i++;
// Plot extra trapezoids.
for (ItemPos extra_trapezoid_pos = 0;
extra_trapezoid_pos < (ItemPos)node->extra_trapezoids.size();
++extra_trapezoid_pos) {
const GeneralizedTrapezoid& trapezoid = node->extra_trapezoids[extra_trapezoid_pos].trapezoid;

file << "<g>" << std::endl;
file << trapezoid.to_svg("red", factor);
LengthDbl x = (trapezoid.x_max() + trapezoid.x_min()) / 2;
LengthDbl y = (trapezoid.y_top() + trapezoid.y_bottom()) / 2;
file << "<text x=\"" << std::to_string(x * factor) << "\" y=\"" << std::to_string(-y * factor) << "\" dominant-baseline=\"middle\" text-anchor=\"middle\">" << std::to_string(extra_trapezoid_pos) << "</text>" << std::endl;
file << "</g>" << std::endl;
plot[i];
i++;
}
// Plot uncovered trapezoids.
for (TrapezoidPos uncovered_trapezoid_pos = 0;
uncovered_trapezoid_pos < (ItemPos)node->uncovered_trapezoids.size();
++uncovered_trapezoid_pos) {
GeneralizedTrapezoid trapezoid = node->uncovered_trapezoids[uncovered_trapezoid_pos].trapezoid;
trapezoid.extend_left(bb_bin_type.x_min);

file << "<g>" << std::endl;
file << trapezoid.to_svg("blue", factor);
LengthDbl x = (trapezoid.x_max() + trapezoid.x_min()) / 2;
LengthDbl y = (trapezoid.y_top() + trapezoid.y_bottom()) / 2;
file << "<text x=\"" << std::to_string(x * factor) << "\" y=\"" << std::to_string(-y * factor) << "\" dominant-baseline=\"middle\" text-anchor=\"middle\">" << std::to_string(uncovered_trapezoid_pos) << "</text>" << std::endl;
file << "</g>" << std::endl;
plot[i];
i++;
}

return {
{"ID", node->id},
{"ParentID", (node->parent == nullptr)? -1: node->parent->id},
nlohmann::json json = {
{"Id", node->id},
{"ParentId", (node->parent == nullptr)? -1: node->parent->id},
{"TrapezoidSetId", node->trapezoid_set_id},
{"X", node->x},
{"Y", node->y},
Expand All @@ -2484,7 +2472,34 @@ nlohmann::json BranchingScheme::json_export(
{"ItemArea", node->item_area},
{"ItemConvexHullArea", node->item_convex_hull_area},
{"GuideArea", node->guide_area},
{"Plot", plot}};
};
if (node->number_of_items == 0)
return json;

nlohmann::json plot;
Counter i = 0;
// Plot bin.
BinTypeId bin_type_id = instance().bin_type_id(node->number_of_bins - 1);
plot[i] = {
{"Id",json_bins_init_ids_[bin_type_id]},
{"X", 0},
{"Y", 0},
};
i++;
// Plot items.
for (std::shared_ptr<const Node> node_tmp = node;
node_tmp->number_of_bins == node->number_of_bins;
node_tmp = node_tmp->parent) {
const TrapezoidSet& trapezoid_set = trapezoid_sets_[(int)node_tmp->last_bin_direction][node_tmp->trapezoid_set_id];
plot[i] = {
{"Id", json_items_init_ids_[trapezoid_set.item_type_id][trapezoid_set.mirror].at(trapezoid_set.angle)},
{"X", node_tmp->x},
{"Y", node_tmp->y},
};
i++;
}
json["Plot"] = plot;
return json;
}

void BranchingScheme::write_svg(
Expand Down
6 changes: 3 additions & 3 deletions src/irregular/branching_scheme.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ class BranchingScheme
const std::shared_ptr<Node>& node,
const std::string& file_path) const;

nlohmann::json json_export_init();
nlohmann::json json_export_init() const;

nlohmann::json json_export(
const std::shared_ptr<Node>& node) const;
Expand All @@ -442,9 +442,9 @@ class BranchingScheme

std::vector<AreaDbl> item_types_convex_hull_area_;

std::vector<std::vector<std::vector<Counter>>> json_items_init_ids_;
mutable std::vector<std::vector<std::unordered_map<Angle, Counter>>> json_items_init_ids_;

std::vector<std::vector<std::vector<Counter>>> json_bins_init_ids_;
mutable std::vector<Counter> json_bins_init_ids_;

mutable Counter node_id_ = 0;

Expand Down
117 changes: 37 additions & 80 deletions src/irregular/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ std::string ShapeElement::to_string() const
return "";
}

nlohmann::json ShapeElement::to_json() const
{
nlohmann::json json;
json["type"] = element2str(type);
json["start"]["x"] = start.x;
json["start"]["y"] = start.y;
json["end"]["x"] = end.x;
json["end"]["y"] = end.y;
if (type == ShapeElementType::CircularArc) {
json["center"]["x"] = center.x;
json["center"]["y"] = center.y;
json["anticlockwise"] = anticlockwise;
}
return json;
}

ShapeElement ShapeElement::rotate(
Angle angle) const
{
Expand Down Expand Up @@ -526,6 +542,17 @@ std::string Shape::to_string(
return s;
}

nlohmann::json Shape::to_json() const
{
nlohmann::json json;
for (ElementPos element_pos = 0;
element_pos < (ElementPos)elements.size();
++element_pos) {
json[element_pos] = elements[element_pos].to_json();
}
return json;
}

std::string Shape::to_svg(double factor) const
{
std::string s = "M";
Expand Down Expand Up @@ -1270,62 +1297,20 @@ void Instance::write(
json["bin_types"][bin_type_id]["copies"] = bin_type.copies;
json["bin_types"][bin_type_id]["copies_min"] = bin_type.copies_min;
json["bin_types"][bin_type_id]["type"] = "general";
for (ElementPos element_pos = 0;
element_pos < (ElementPos)bin_type.shape.elements.size();
++element_pos) {
const ShapeElement& element = bin_type.shape.elements[element_pos];
json["bin_types"][bin_type_id]["elements"][element_pos]["type"] = element2str(element.type);
json["bin_types"][bin_type_id]["elements"][element_pos]["start"]["x"] = element.start.x;
json["bin_types"][bin_type_id]["elements"][element_pos]["start"]["y"] = element.start.y;
json["bin_types"][bin_type_id]["elements"][element_pos]["end"]["x"] = element.end.x;
json["bin_types"][bin_type_id]["elements"][element_pos]["end"]["y"] = element.end.y;
if (element.type == ShapeElementType::CircularArc) {
json["bin_types"][bin_type_id]["elements"][element_pos]["center"]["x"] = element.center.x;
json["bin_types"][bin_type_id]["elements"][element_pos]["center"]["y"] = element.center.y;
json["bin_types"][bin_type_id]["elements"][element_pos]["anticlockwise"] = element.anticlockwise;
}
}
json["bin_types"][bin_type_id]["elements"] = bin_type.shape.to_json();
// Bin defects.
for (DefectId defect_id = 0;
defect_id < (DefectId)bin_type.defects.size();
++defect_id) {
const Defect& defect = bin_type.defects[defect_id];
json["bin_types"][bin_type_id]["defects"][defect_id]["type"] = "general";
for (Counter element_pos = 0;
element_pos < (Counter)defect.shape.elements.size();
++element_pos) {
const ShapeElement& element = defect.shape.elements[element_pos];
json["bin_types"][bin_type_id]["defects"][defect_id]["elements"][element_pos]["type"] = element2str(element.type);
json["bin_types"][bin_type_id]["defects"][defect_id]["elements"][element_pos]["start"]["x"] = element.start.x;
json["bin_types"][bin_type_id]["defects"][defect_id]["elements"][element_pos]["start"]["y"] = element.start.y;
json["bin_types"][bin_type_id]["defects"][defect_id]["elements"][element_pos]["end"]["x"] = element.end.x;
json["bin_types"][bin_type_id]["defects"][defect_id]["elements"][element_pos]["end"]["y"] = element.end.y;
if (element.type == ShapeElementType::CircularArc) {
json["bin_types"][bin_type_id]["defects"][defect_id]["elements"][element_pos]["center"]["x"] = element.center.x;
json["bin_types"][bin_type_id]["defects"][defect_id]["elements"][element_pos]["center"]["y"] = element.center.y;
json["bin_types"][bin_type_id]["defects"][defect_id]["elements"][element_pos]["anticlockwise"] = element.anticlockwise;
}
for (Counter hole_pos = 0;
hole_pos < (Counter)defect.holes.size();
++hole_pos) {
const Shape& hole = defect.holes[hole_pos];
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["type"] = "general";
for (Counter element_pos = 0;
element_pos < (Counter)hole.elements.size();
++element_pos) {
const ShapeElement& element = hole.elements[element_pos];
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["elements"][element_pos]["type"] = element2str(element.type);
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["elements"][element_pos]["start"]["x"] = element.start.x;
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["elements"][element_pos]["start"]["y"] = element.start.y;
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["elements"][element_pos]["end"]["x"] = element.end.x;
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["elements"][element_pos]["end"]["y"] = element.end.y;
if (element.type == ShapeElementType::CircularArc) {
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["elements"][element_pos]["center"]["x"] = element.center.x;
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["elements"][element_pos]["center"]["y"] = element.center.y;
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["elements"][element_pos]["anticlockwise"] = element.anticlockwise;
}
}
}
json["bin_types"][bin_type_id]["defects"][defect_id]["elements"] = defect.shape.to_json();
for (Counter hole_pos = 0;
hole_pos < (Counter)defect.holes.size();
++hole_pos) {
const Shape& hole = defect.holes[hole_pos];
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["type"] = "general";
json["bin_types"][bin_type_id]["defects"][defect_id]["holes"][hole_pos]["elements"] = hole.to_json();
}
}
}
Expand All @@ -1349,41 +1334,13 @@ void Instance::write(
++item_shape_pos) {
const ItemShape& item_shape = item_type.shapes[item_shape_pos];
json["item_types"][item_type_id]["shapes"][item_shape_pos]["type"] = "general";
for (Counter element_pos = 0;
element_pos < (Counter)item_shape.shape.elements.size();
++element_pos) {
const ShapeElement& element = item_shape.shape.elements[element_pos];
json["item_types"][item_type_id]["shapes"][item_shape_pos]["elements"][element_pos]["type"] = element2str(element.type);
json["item_types"][item_type_id]["shapes"][item_shape_pos]["elements"][element_pos]["start"]["x"] = element.start.x;
json["item_types"][item_type_id]["shapes"][item_shape_pos]["elements"][element_pos]["start"]["y"] = element.start.y;
json["item_types"][item_type_id]["shapes"][item_shape_pos]["elements"][element_pos]["end"]["x"] = element.end.x;
json["item_types"][item_type_id]["shapes"][item_shape_pos]["elements"][element_pos]["end"]["y"] = element.end.y;
if (element.type == ShapeElementType::CircularArc) {
json["item_types"][item_type_id]["shapes"][item_shape_pos]["elements"][element_pos]["center"]["x"] = element.center.x;
json["item_types"][item_type_id]["shapes"][item_shape_pos]["elements"][element_pos]["center"]["y"] = element.center.y;
json["item_types"][item_type_id]["shapes"][item_shape_pos]["elements"][element_pos]["anticlockwise"] = element.anticlockwise;
}
}
json["item_types"][item_type_id]["shapes"][item_shape_pos]["elements"] = item_shape.shape.to_json();
for (Counter hole_pos = 0;
hole_pos < (Counter)item_shape.holes.size();
++hole_pos) {
const Shape& hole = item_shape.holes[hole_pos];
json["item_types"][item_type_id]["shapes"][item_shape_pos]["holes"][hole_pos]["type"] = "general";
for (Counter element_pos = 0;
element_pos < (Counter)hole.elements.size();
++element_pos) {
const ShapeElement& element = hole.elements[element_pos];
json["item_types"][item_type_id]["shapes"][item_shape_pos]["holes"][hole_pos]["elements"][element_pos]["type"] = element2str(element.type);
json["item_types"][item_type_id]["shapes"][item_shape_pos]["holes"][hole_pos]["elements"][element_pos]["start"]["x"] = element.start.x;
json["item_types"][item_type_id]["shapes"][item_shape_pos]["holes"][hole_pos]["elements"][element_pos]["start"]["y"] = element.start.y;
json["item_types"][item_type_id]["shapes"][item_shape_pos]["holes"][hole_pos]["elements"][element_pos]["end"]["x"] = element.end.x;
json["item_types"][item_type_id]["shapes"][item_shape_pos]["holes"][hole_pos]["elements"][element_pos]["end"]["y"] = element.end.y;
if (element.type == ShapeElementType::CircularArc) {
json["item_types"][item_type_id]["shapes"][item_shape_pos]["holes"][hole_pos]["elements"][element_pos]["center"]["x"] = element.center.x;
json["item_types"][item_type_id]["shapes"][item_shape_pos]["holes"][hole_pos]["elements"][element_pos]["center"]["y"] = element.center.y;
json["item_types"][item_type_id]["shapes"][item_shape_pos]["holes"][hole_pos]["elements"][element_pos]["anticlockwise"] = element.anticlockwise;
}
}
json["item_types"][item_type_id]["shapes"][item_shape_pos]["holes"][hole_pos]["elements"] = hole.to_json();
}
}
}
Expand Down

0 comments on commit 2d9d522

Please sign in to comment.