ENH: merge tree support layers to support layers

Will greately reduce repeated codes.

Change-Id: I506a97a907b5b393fe41e13ae53e2f7c9247c4c5
This commit is contained in:
Arthur 2022-12-09 14:08:30 +08:00 committed by Lane.Wei
parent b6ef31f7b9
commit 646b259972
11 changed files with 123 additions and 624 deletions

View file

@ -1283,7 +1283,6 @@ void PrintObject::clear_shared_object()
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": this=%1%, clear previous shared object data %2%")%this %m_shared_object;
m_layers.clear();
m_support_layers.clear();
m_tree_support_layers.clear();
m_shared_object = nullptr;
@ -1296,7 +1295,6 @@ void PrintObject::copy_layers_from_shared_object()
if (m_shared_object) {
m_layers.clear();
m_support_layers.clear();
m_tree_support_layers.clear();
firstLayerObjSliceByVolume.clear();
firstLayerObjSliceByGroups.clear();
@ -1304,7 +1302,6 @@ void PrintObject::copy_layers_from_shared_object()
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": this=%1%, copied layers from object %2%")%this%m_shared_object;
m_layers = m_shared_object->layers();
m_support_layers = m_shared_object->support_layers();
m_tree_support_layers = m_shared_object->tree_support_layers();
firstLayerObjSliceByVolume = m_shared_object->firstLayerObjSlice();
firstLayerObjSliceByGroups = m_shared_object->firstLayerObjGroups();
@ -1706,13 +1703,6 @@ void Print::_make_skirt()
break;
layer->support_fills.collect_points(object_points);
}
// BBS
for (const TreeSupportLayer* layer : object->m_tree_support_layers) {
if (layer->print_z > skirt_height_z)
break;
layer->support_fills.collect_points(object_points);
}
object_convex_hulls.insert({ object, Slic3r::Geometry::convex_hull(object_points) });
@ -1859,12 +1849,12 @@ Polygons Print::first_layer_islands() const
Polygons object_islands;
for (ExPolygon &expoly : object->m_layers.front()->lslices)
object_islands.push_back(expoly.contour);
if (! object->support_layers().empty())
object->support_layers().front()->support_fills.polygons_covered_by_spacing(object_islands, float(SCALED_EPSILON));
if (! object->tree_support_layers().empty()) {
ExPolygons& expolys_first_layer = object->m_tree_support_layers.front()->lslices;
for (ExPolygon &expoly : expolys_first_layer) {
object_islands.push_back(expoly.contour);
if (!object->support_layers().empty()) {
if (object->support_layers().front()->support_type==stInnerNormal)
object->support_layers().front()->support_fills.polygons_covered_by_spacing(object_islands, float(SCALED_EPSILON));
else if(object->support_layers().front()->support_type==stInnerTree) {
ExPolygons &expolys_first_layer = object->m_support_layers.front()->lslices;
for (ExPolygon &expoly : expolys_first_layer) { object_islands.push_back(expoly.contour); }
}
}
islands.reserve(islands.size() + object_islands.size() * object->instances().size());
@ -2215,7 +2205,7 @@ std::string PrintStatistics::finalize_output_path(const std::string &path_in) co
#define JSON_SUPPORT_LAYER_ISLANDS "support_islands"
#define JSON_SUPPORT_LAYER_FILLS "support_fills"
#define JSON_SUPPORT_LAYER_INTERFACE_ID "interface_id"
#define JSON_SUPPORT_LAYER_TYPE "support_type"
#define JSON_LAYER_REGION_CONFIG_HASH "config_hash"
#define JSON_LAYER_REGION_SLICES "slices"
@ -2846,6 +2836,7 @@ void extract_layer(const json& layer_json, Layer& layer) {
void extract_support_layer(const json& support_layer_json, SupportLayer& support_layer) {
extract_layer(support_layer_json, support_layer);
support_layer.support_type = support_layer_json[JSON_SUPPORT_LAYER_TYPE];
//support_islands
int islands_count = support_layer_json[JSON_SUPPORT_LAYER_ISLANDS].size();
for (int islands_index = 0; islands_index < islands_count; islands_index++)
@ -2874,27 +2865,6 @@ void extract_support_layer(const json& support_layer_json, SupportLayer& support
return;
}
void extract_tree_support_layer(const json& tree_support_layer_json, TreeSupportLayer& tree_support_layer) {
extract_layer(tree_support_layer_json, tree_support_layer);
//support_fills
tree_support_layer.support_fills.no_sort = tree_support_layer_json[JSON_SUPPORT_LAYER_FILLS][JSON_EXTRUSION_NO_SORT];
int treesupport_fills_entities_count = tree_support_layer_json[JSON_SUPPORT_LAYER_FILLS][JSON_EXTRUSION_ENTITIES].size();
for (int treesupport_fills_entities_index = 0; treesupport_fills_entities_index < treesupport_fills_entities_count; treesupport_fills_entities_index++)
{
const json& extrusion_entity_json = tree_support_layer_json[JSON_SUPPORT_LAYER_FILLS][JSON_EXTRUSION_ENTITIES][treesupport_fills_entities_index];
bool ret = convert_extrusion_from_json(extrusion_entity_json, tree_support_layer.support_fills);
if (!ret) {
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << boost::format(": error parsing fills found at tree_support_layer %1%, print_z %2%")%tree_support_layer.id() %tree_support_layer.print_z;
char error_buf[1024];
::sprintf(error_buf, "Error while parsing fills at tree_support_layer %d, print_z %f", tree_support_layer.id(), tree_support_layer.print_z);
throw Slic3r::FileIOError(error_buf);
}
}
return;
}
int Print::export_cached_data(const std::string& directory, bool with_space)
{
int ret = 0;
@ -2960,7 +2930,7 @@ int Print::export_cached_data(const std::string& directory, bool with_space)
std::string file_name = directory +"/obj_"+std::to_string(arrange_order)+".json";
try {
json root_json, layers_json = json::array(), support_layers_json = json::array(), tree_support_layers_json = json::array();
json root_json, layers_json = json::array(), support_layers_json = json::array();
root_json[JSON_OBJECT_NAME] = model_obj->name;
root_json[JSON_ARRANGE_ORDER] = arrange_order;
@ -3005,6 +2975,7 @@ int Print::export_cached_data(const std::string& directory, bool with_space)
convert_layer_to_json(support_layer_json, support_layer);
support_layer_json[JSON_SUPPORT_LAYER_INTERFACE_ID] = support_layer->interface_id();
support_layer_json[JSON_SUPPORT_LAYER_TYPE] = support_layer->support_type;
//support_islands
for (const ExPolygon& support_island : support_layer->support_islands.expolygons) {
@ -3068,136 +3039,6 @@ int Print::export_cached_data(const std::string& directory, bool with_space)
} // for each layer*/
root_json[JSON_SUPPORT_LAYERS] = std::move(support_layers_json);
//export the tree support layers
std::vector<json> tree_support_layers_json_vector(obj->tree_support_layer_count());
tbb::parallel_for(
tbb::blocked_range<size_t>(0, obj->tree_support_layer_count()),
[&tree_support_layers_json_vector, obj, convert_layer_to_json](const tbb::blocked_range<size_t>& tree_support_layer_range) {
for (size_t ts_layer_index = tree_support_layer_range.begin(); ts_layer_index < tree_support_layer_range.end(); ++ ts_layer_index) {
const TreeSupportLayer *tree_support_layer = obj->get_tree_support_layer(ts_layer_index);
json treesupport_layer_json, treesupport_fills_json, treesupportfills_entities_json = json::array();
//json overhang_areas_json = json::array(), roof_areas_json = json::array(), roof_1st_layer_json = json::array(), floor_areas_json = json::array(), base_areas_json = json::array();
convert_layer_to_json(treesupport_layer_json, tree_support_layer);
//tree_support_fills
treesupport_fills_json[JSON_EXTRUSION_NO_SORT] = tree_support_layer->support_fills.no_sort;
treesupport_fills_json[JSON_EXTRUSION_ENTITY_TYPE] = JSON_EXTRUSION_TYPE_COLLECTION;
for (const ExtrusionEntity* extrusion_entity : tree_support_layer->support_fills.entities) {
json treesupportfill_entity_json, treesupportfill_entity_paths_json = json::array();
bool ret = convert_extrusion_to_json(treesupportfill_entity_json, treesupportfill_entity_paths_json, extrusion_entity);
if (!ret)
continue;
treesupportfills_entities_json.push_back(std::move(treesupportfill_entity_json));
}
treesupport_fills_json[JSON_EXTRUSION_ENTITIES] = std::move(treesupportfills_entities_json);
treesupport_layer_json[JSON_SUPPORT_LAYER_FILLS] = std::move(treesupport_fills_json);
//following data are not needed in the later stage
//overhang_areas
/*for (const ExPolygon& overhang_area : tree_support_layer->overhang_areas) {
json overhang_area_json = overhang_area;
overhang_areas_json.push_back(std::move(overhang_area_json));
}
treesupport_layer_json["overhang_areas"] = std::move(overhang_areas_json);
//roof_areas
for (const ExPolygon& roof_area : tree_support_layer->roof_areas) {
json roof_area_json = roof_area;
roof_areas_json.push_back(std::move(roof_area_json));
}
treesupport_layer_json["roof_areas"] = std::move(roof_areas_json);
//roof_1st_layer
for (const ExPolygon& layer_poly : tree_support_layer->roof_1st_layer) {
json layer_poly_json = layer_poly;
roof_1st_layer_json.push_back(std::move(layer_poly_json));
}
treesupport_layer_json["roof_1st_layer"] = std::move(roof_1st_layer_json);
//floor_areas
for (const ExPolygon& floor_area : tree_support_layer->floor_areas) {
json floor_area_json = floor_area;
floor_areas_json.push_back(std::move(floor_area_json));
}
treesupport_layer_json["floor_areas"] = std::move(floor_areas_json);
//base_areas
for (const ExPolygon& base_area : tree_support_layer->base_areas) {
json base_area_json = base_area;
base_areas_json.push_back(std::move(base_area_json));
}
treesupport_layer_json["base_areas"] = std::move(base_areas_json);*/
tree_support_layers_json_vector[ts_layer_index] = std::move(treesupport_layer_json);
}
}
);
for (int ts_index = 0; ts_index < tree_support_layers_json_vector.size(); ts_index++) {
tree_support_layers_json.push_back(std::move(tree_support_layers_json_vector[ts_index]));
}
tree_support_layers_json_vector.clear();
#if 0
for (const TreeSupportLayer *tree_support_layer : obj->tree_support_layers()) {
json treesupport_layer_json, treesupport_fills_json, treesupportfills_entities_json = json::array();
json overhang_areas_json = json::array(), roof_areas_json = json::array(), roof_1st_layer_json = json::array(), floor_areas_json = json::array(), base_areas_json = json::array();
convert_layer_to_json(treesupport_layer_json, tree_support_layer);
//tree_support_fills
treesupport_fills_json[JSON_EXTRUSION_NO_SORT] = tree_support_layer->support_fills.no_sort;
treesupport_fills_json[JSON_EXTRUSION_ENTITY_TYPE] = JSON_EXTRUSION_TYPE_COLLECTION;
for (const ExtrusionEntity* extrusion_entity : tree_support_layer->support_fills.entities) {
json treesupportfill_entity_json, treesupportfill_entity_paths_json = json::array();
bool ret = convert_extrusion_to_json(treesupportfill_entity_json, treesupportfill_entity_paths_json, extrusion_entity);
if (!ret)
continue;
treesupportfills_entities_json.push_back(std::move(treesupportfill_entity_json));
}
treesupport_fills_json[JSON_EXTRUSION_ENTITIES] = std::move(treesupportfills_entities_json);
treesupport_layer_json[JSON_SUPPORT_LAYER_FILLS] = std::move(treesupport_fills_json);
//overhang_areas
/*for (const ExPolygon& overhang_area : tree_support_layer->overhang_areas) {
json overhang_area_json = overhang_area;
overhang_areas_json.push_back(std::move(overhang_area_json));
}
treesupport_layer_json["overhang_areas"] = std::move(overhang_areas_json);
//roof_areas
for (const ExPolygon& roof_area : tree_support_layer->roof_areas) {
json roof_area_json = roof_area;
roof_areas_json.push_back(std::move(roof_area_json));
}
treesupport_layer_json["roof_areas"] = std::move(roof_areas_json);
//roof_1st_layer
for (const ExPolygon& layer_poly : tree_support_layer->roof_1st_layer) {
json layer_poly_json = layer_poly;
roof_1st_layer_json.push_back(std::move(layer_poly_json));
}
treesupport_layer_json["roof_1st_layer"] = std::move(roof_1st_layer_json);
//floor_areas
for (const ExPolygon& floor_area : tree_support_layer->floor_areas) {
json floor_area_json = floor_area;
floor_areas_json.push_back(std::move(floor_area_json));
}
treesupport_layer_json["floor_areas"] = std::move(floor_areas_json);
//base_areas
for (const ExPolygon& base_area : tree_support_layer->base_areas) {
json base_area_json = base_area;
base_areas_json.push_back(std::move(base_area_json));
}
treesupport_layer_json["base_areas"] = std::move(base_areas_json);*/
tree_support_layers_json.push_back(std::move(treesupport_layer_json));
} // for each layer
#endif
root_json[JSON_TREE_SUPPORT_LAYERS] = std::move(tree_support_layers_json);
filename_vector.push_back(file_name);
json_vector.push_back(std::move(root_json));
@ -3276,7 +3117,6 @@ int Print::load_cached_data(const std::string& directory)
obj->clear_layers();
obj->clear_support_layers();
obj->clear_tree_support_layers();
int arrange_order = model_instance->arrange_order;
if (arrange_order <= 0) {
@ -3328,13 +3168,12 @@ int Print::load_cached_data(const std::string& directory)
std::string name = root_json.at(JSON_OBJECT_NAME);
int order = root_json.at(JSON_ARRANGE_ORDER);
int layer_count = 0, support_layer_count = 0, treesupport_layer_count = 0;
int layer_count = 0, support_layer_count = 0;
layer_count = root_json[JSON_LAYERS].size();
support_layer_count = root_json[JSON_SUPPORT_LAYERS].size();
treesupport_layer_count = root_json[JSON_TREE_SUPPORT_LAYERS].size();
BOOST_LOG_TRIVIAL(info) << __FUNCTION__<<boost::format(":will load %1%, arrange_order %2%, layer_count %3%, support_layer_count %4%, treesupport_layer_count %5%")%name %order %layer_count %support_layer_count %treesupport_layer_count;
BOOST_LOG_TRIVIAL(info) << __FUNCTION__<<boost::format(":will load %1%, arrange_order %2%, layer_count %3%, support_layer_count %4%")%name %order %layer_count %support_layer_count;
Layer* previous_layer = NULL;
//create layer and layer regions
@ -3415,35 +3254,6 @@ int Print::load_cached_data(const std::string& directory)
}
);
//tree support layers
Layer* previous_tree_support_layer = NULL;
//create tree_support_layers
for (int index = 0; index < treesupport_layer_count; index++)
{
json& layer_json = root_json[JSON_TREE_SUPPORT_LAYERS][index];
TreeSupportLayer* new_tree_support_layer = obj->add_tree_support_layer(layer_json[JSON_LAYER_ID], layer_json[JSON_LAYER_HEIGHT], layer_json[JSON_LAYER_PRINT_Z], layer_json[JSON_LAYER_SLICE_Z]);
if (!new_tree_support_layer) {
BOOST_LOG_TRIVIAL(error) <<__FUNCTION__<< boost::format(":add_support_layer failed, out of memory");
return CLI_OUT_OF_MEMORY;
}
if (previous_tree_support_layer) {
previous_tree_support_layer->upper_layer = new_tree_support_layer;
new_tree_support_layer->lower_layer = previous_tree_support_layer;
}
previous_tree_support_layer = new_tree_support_layer;
}
BOOST_LOG_TRIVIAL(info) << __FUNCTION__<< boost::format(": finished load support_layers, start to load treesupport_layers.");
tbb::parallel_for(
tbb::blocked_range<size_t>(0, obj->tree_support_layer_count()),
[&root_json, &obj](const tbb::blocked_range<size_t>& tree_support_layer_range) {
for (size_t layer_index = tree_support_layer_range.begin(); layer_index < tree_support_layer_range.end(); ++ layer_index) {
const json& layer_json = root_json[JSON_TREE_SUPPORT_LAYERS][layer_index];
TreeSupportLayer* tree_support_layer = obj->get_tree_support_layer(layer_index);
extract_tree_support_layer(layer_json, *tree_support_layer);
}
}
);
count ++;
BOOST_LOG_TRIVIAL(info) << __FUNCTION__<< boost::format(": load object %1% from %2% successfully.")%count%object_filenames[obj_index].first;
}