fix crashing when extrusion is not assigned island

Add voxel filter grid for supports padding
This commit is contained in:
PavelMikus 2022-07-18 16:46:10 +02:00
parent f311ccbc4c
commit 1e4b56cc85

View File

@ -15,7 +15,7 @@
#include "libslic3r/ClipperUtils.hpp"
#include "Geometry/ConvexHull.hpp"
//#define DEBUG_FILES
#define DEBUG_FILES
#ifdef DEBUG_FILES
#include <boost/nowide/cstdio.hpp>
@ -193,6 +193,61 @@ private:
}
};
struct SupportGridFilter {
private:
Vec3f cell_size;
Vec3f origin;
Vec3f size;
Vec3i cell_count;
std::unordered_set<size_t> taken_cells { };
public:
SupportGridFilter(const PrintObject *po, float voxel_size) {
cell_size = Vec3f(voxel_size, voxel_size, voxel_size);
Vec2crd size_half = po->size().head<2>().cwiseQuotient(Vec2crd(2, 2)) + Vec2crd::Ones();
Vec3f min = unscale(Vec3crd(-size_half.x(), -size_half.y(), 0)).cast<float>() - cell_size;
Vec3f max = unscale(Vec3crd(size_half.x(), size_half.y(), po->height())).cast<float>() + cell_size;
origin = min;
size = max - min;
cell_count = size.cwiseQuotient(cell_size).cast<int>() + Vec3i::Ones();
}
Vec3i to_cell_coords(const Vec3f &position) const {
Vec3i cell_coords = (position - this->origin).cwiseQuotient(this->cell_size).cast<int>();
return cell_coords;
}
size_t to_cell_index(const Vec3i &cell_coords) const {
assert(cell_coords.x() >= 0);
assert(cell_coords.x() < cell_count.x());
assert(cell_coords.y() >= 0);
assert(cell_coords.y() < cell_count.y());
assert(cell_coords.z() >= 0);
assert(cell_coords.z() < cell_count.z());
return cell_coords.z() * cell_count.x() * cell_count.y()
+ cell_coords.y() * cell_count.x()
+ cell_coords.x();
}
Vec3f get_cell_center(const Vec3i &cell_coords) const {
return origin + cell_coords.cast<float>().cwiseProduct(this->cell_size)
+ this->cell_size.cwiseQuotient(Vec3f(2.0f, 2.0f, 2.0));
}
void take_position(const Vec3f &position) {
taken_cells.insert(to_cell_index(to_cell_coords(position)));
}
bool position_taken(const Vec3f &position) const {
return taken_cells.find(to_cell_index(to_cell_coords(position))) != taken_cells.end();
}
};
struct Island {
std::unordered_map<size_t, float> islands_under_with_connection_area;
std::vector<Vec3f> pivot_points;
@ -351,9 +406,11 @@ std::tuple<LayerIslands, PixelGrid, std::vector<size_t>> reckon_islands(
BOOST_LOG_TRIVIAL(debug)
<< "SSG: reckon islands on printz: " << layer->print_z;
//extract extrusions (connected paths from multiple lines) from the layer_lines. belonging to single polyline is determined by origin_entity ptr.
// result is a vector of [start, end) index pairs into the layer_lines vector
std::vector<std::pair<size_t, size_t>> extrusions; //start and end idx (one beyond last extrusion) [start,end)
const ExtrusionEntity *current_ex = nullptr;
for (size_t lidx = 1; lidx < layer_lines.size(); ++lidx) {
for (size_t lidx = 0; lidx < layer_lines.size(); ++lidx) {
const ExtrusionLine &line = layer_lines[lidx];
if (line.origin_entity == current_ex) {
extrusions.back().second = lidx + 1;
@ -374,8 +431,10 @@ std::tuple<LayerIslands, PixelGrid, std::vector<size_t>> reckon_islands(
<< "SSG: " << ext.second - ext.first;
}
std::vector<LinesDistancer> islands;
std::vector<std::vector<size_t>> island_extrusions;
std::vector<LinesDistancer> islands; // these search trees will be used to determine to which island does the extrusion begin
std::vector<std::vector<size_t>> island_extrusions; //final assigment of each extrusion to an island
// initliaze the search from external perimeters - at the beginning, there is island candidate for each external perimeter.
// some of them will disappear (e.g. holes)
for (size_t e = 0; e < extrusions.size(); ++e) {
if (layer_lines[extrusions[e].first].is_external_perimeter()) {
std::vector<ExtrusionLine> copy(extrusions[e].second - extrusions[e].first);
@ -386,22 +445,40 @@ std::tuple<LayerIslands, PixelGrid, std::vector<size_t>> reckon_islands(
island_extrusions.push_back( { e });
}
}
// backup code if islands not found - this can currently happen, as external perimeters may be also pure overhang perimeters, and there is no
// way to distinguish external extrusions with total certainty.
// If that happens, just make the first extrusion into island - it may be wrong, but it won't crash.
if (islands.empty() && !extrusions.empty()) {
std::vector<ExtrusionLine> copy(extrusions[0].second - extrusions[0].first);
for (size_t ex_line_idx = extrusions[0].first; ex_line_idx < extrusions[0].second; ++ex_line_idx) {
copy[ex_line_idx - extrusions[0].first] = layer_lines[ex_line_idx];
}
islands.emplace_back(copy);
island_extrusions.push_back( { 0 });
}
BOOST_LOG_TRIVIAL(debug)
<< "SSG: external perims: " << islands.size();
for (size_t i = 0; i < islands.size(); ++i) {
// assign non external extrusions to islands
for (size_t e = 0; e < extrusions.size(); ++e) {
if (!layer_lines[extrusions[e].first].is_external_perimeter()) {
bool island_assigned = false;
for (size_t i = 0; i < islands.size(); ++i) {
size_t _idx;
Vec2f _pt;
if (islands[i].signed_distance_from_lines(layer_lines[extrusions[e].first].a, _idx, _pt) < 0) {
island_extrusions[i].push_back(e);
island_assigned = true;
break;
}
}
if (!island_assigned) { // If extrusion is not assigned for some reason, push it into the first island. As with the previous backup code,
// it may be wrong, but it won't crash
island_extrusions[0].push_back(e);
}
}
}
}
// merge islands which are embedded within each other (mainly holes)
for (size_t i = 0; i < islands.size(); ++i) {
if (islands[i].get_lines().empty()) {
continue;
@ -424,7 +501,7 @@ std::tuple<LayerIslands, PixelGrid, std::vector<size_t>> reckon_islands(
<< "SSG: filter islands";
float flow_width = get_flow_width(layer->regions()[0], erExternalPerimeter);
// after filtering the layer lines into islands, build the result LayerIslands structure.
LayerIslands result { };
std::vector<size_t> line_to_island_mapping(layer_lines.size(), NULL_ISLAND);
for (const std::vector<size_t> &island_ex : island_extrusions) {
@ -437,9 +514,10 @@ std::tuple<LayerIslands, PixelGrid, std::vector<size_t>> reckon_islands(
for (size_t lidx = extrusions[extrusion_idx].first; lidx < extrusions[extrusion_idx].second; ++lidx) {
line_to_island_mapping[lidx] = result.islands.size();
const ExtrusionLine &line = layer_lines[lidx];
float volume = line.len * flow_width * layer->height * 0.7; // 1/sqrt(2) compensation for cylindrical shape
float volume = line.origin_entity->min_mm3_per_mm() * line.len;
island.volume += volume;
island.volume_centroid_accumulator += to_3d(Vec2f((line.a + line.b) / 2.0f), float(layer->print_z)) * volume;
island.volume_centroid_accumulator += to_3d(Vec2f((line.a + line.b) / 2.0f), float(layer->print_z))
* volume;
if (first_layer) {
float sticking_force = line.len * flow_width * params.base_adhesion;
@ -455,7 +533,8 @@ std::tuple<LayerIslands, PixelGrid, std::vector<size_t>> reckon_islands(
* float(PI);
float sticking_force = support_interface_area * params.support_adhesion;
island.sticking_force += sticking_force;
island.sticking_centroid_accumulator += sticking_force * to_3d(Vec2f(line.b), float(layer->print_z));
island.sticking_centroid_accumulator += sticking_force
* to_3d(Vec2f(line.b), float(layer->print_z));
island.pivot_points.push_back(to_3d(Vec2f(line.b), float(layer->print_z)));
}
}
@ -466,9 +545,10 @@ std::tuple<LayerIslands, PixelGrid, std::vector<size_t>> reckon_islands(
BOOST_LOG_TRIVIAL(debug)
<< "SSG: There are " << result.islands.size() << " islands on printz: " << layer->print_z;
//LayerIslands structure built. Now determine connections and their areas to the previous layer using raterization.
PixelGrid current_layer_grid = prev_layer_grid;
current_layer_grid.clear();
// build index image of current layer
tbb::parallel_for(tbb::blocked_range<size_t>(0, layer_lines.size()),
[&layer_lines, &current_layer_grid, &line_to_island_mapping](
tbb::blocked_range<size_t> r) {
@ -482,6 +562,7 @@ std::tuple<LayerIslands, PixelGrid, std::vector<size_t>> reckon_islands(
BOOST_LOG_TRIVIAL(debug)
<< "SSG: rasterized";
//compare the image of previous layer with the current layer. For each pair of overlapping valid pixels, add pixel area to the respecitve island connection
for (size_t x = 0; x < size_t(current_layer_grid.get_pixel_count().x()); ++x) {
for (size_t y = 0; y < size_t(current_layer_grid.get_pixel_count().y()); ++y) {
Vec2i coords = Vec2i(x, y);
@ -503,11 +584,17 @@ std::tuple<LayerIslands, PixelGrid, std::vector<size_t>> reckon_islands(
void check_global_stability(
float print_z,
std::vector<LayerIslands> &islands_graph,
SupportGridFilter &supports_presence_grid,
const std::vector<ExtrusionLine> &layer_lines,
const std::vector<size_t> &line_to_island_mapping,
Issues &issues,
const Params &params
) {
std::cout << "there are " << islands_graph.back().islands.size() << " islands, " << layer_lines.size() << " lines" << std::endl;
for (int i = 0; i < line_to_island_mapping.size(); ++i) {
std::cout << "line " << i << " belongs to island " << line_to_island_mapping[i] << std::endl;
}
// vector of islands, where each contains vector of line indices (to layer_lines vector)
// basically reverse of line_to_island_mapping
std::vector<std::vector<size_t>> islands_lines(islands_graph.back().islands.size());
@ -519,21 +606,24 @@ void check_global_stability(
using Accumulator = Island;
// islands_graph.back() refers to the top most (currently) layer
// islands_graph.back() refers to the top most (current) layer
for (size_t island_idx = 0; island_idx < islands_graph.back().islands.size(); ++island_idx) {
Island &island = islands_graph.back().islands[island_idx];
std::vector<ExtrusionLine> island_external_lines; //TODO currently not external but all
std::cout << "TOP LEVEL ITERATION FOR ISLAND: " << island_idx << std::endl;
std::vector<ExtrusionLine> island_external_lines;
for (size_t lidx : islands_lines[island_idx]) {
island_external_lines.push_back(layer_lines[lidx]);
}
LinesDistancer island_lines_dist(island_external_lines);
Accumulator acc = island; // in acc, we accumulate the mass and other properties of the object part as we traverse the islands down to bed
// There is one object part for each island at the top most layer, and each one is computed individually -
// Some of the calculations will be done mutliple times
// Some of the calculations will be done multiple times
int layer_idx = islands_graph.size() - 1;
// traverse the islands graph down, and for each connection area, calculate if it holds or breaks
while (acc.islands_under_with_connection_area.size() > 0) {
std::cout << "PARTIAL ITERATION FOR LAYER: " << layer_idx << std::endl;
//test for break between layer_idx and layer_idx -1;
LayerIslands below = islands_graph[layer_idx - 1]; // must exist, see while condition
layer_idx--;
@ -592,10 +682,10 @@ void check_global_stability(
float total_torque = bed_movement_torque + extruder_conflict_torque - weight_torque - sticking_torque;
if (total_torque > 0) {
Vec2f target_point;
size_t _idx;
Vec2f target_point { };
size_t _idx { };
island_lines_dist.signed_distance_from_lines(pivot_site_search_point, _idx, target_point);
// if (!supports_presence_grid.position_taken(to_3d(target_point, print_z))) {
if (!supports_presence_grid.position_taken(to_3d(target_point, print_z))) {
float area = params.support_points_interface_radius * params.support_points_interface_radius
* float(PI);
float sticking_force = area * params.support_adhesion;
@ -605,8 +695,8 @@ void check_global_stability(
island.sticking_centroid_accumulator += sticking_force * support_point;
issues.support_points.emplace_back(support_point,
extruder_conflict_torque - sticking_torque, extruder_pressure_direction);
// supports_presence_grid.take_position(to_3d(target_point, print_z));
// }
supports_presence_grid.take_position(to_3d(target_point, print_z));
}
}
#if 0
BOOST_LOG_TRIVIAL(debug)
@ -648,6 +738,7 @@ void check_global_stability(
}
}
std::cout << "FINAL ITERATION FOR THE BED LEVEL: " << acc.volume << std::endl;
// We have arrived to the bed level, now check for stability of the object part on the bed
std::vector<Vec2f> pivot_points;
for (const Vec3f &p_point : acc.pivot_points) {
@ -693,7 +784,7 @@ void check_global_stability(
Vec2f target_point;
size_t _idx;
island_lines_dist.signed_distance_from_lines(pivot_site_search_point, _idx, target_point);
// if (!supports_presence_grid.position_taken(to_3d(target_point, print_z))) {
if (!supports_presence_grid.position_taken(to_3d(target_point, print_z))) {
float area = params.support_points_interface_radius * params.support_points_interface_radius
* float(PI);
float sticking_force = area * params.support_adhesion;
@ -703,8 +794,8 @@ void check_global_stability(
island.sticking_centroid_accumulator += sticking_force * support_point;
issues.support_points.emplace_back(support_point,
extruder_conflict_torque - sticking_torque, extruder_pressure_direction);
// supports_presence_grid.take_position(to_3d(target_point, print_z));
// }
supports_presence_grid.take_position(to_3d(target_point, print_z));
}
}
#if 0
BOOST_LOG_TRIVIAL(debug)
@ -741,6 +832,8 @@ Issues check_object_stability(const PrintObject *po, const Params &params) {
std::vector<ExtrusionLine> layer_lines;
float flow_width = get_flow_width(po->layers()[po->layer_count() - 1]->regions()[0], erExternalPerimeter);
PixelGrid prev_layer_grid(po, flow_width);
SupportGridFilter supports_presence_grid { po, params.min_distance_between_support_points };
BOOST_LOG_TRIVIAL(debug)
<< "SSG: flow width: " << flow_width;
@ -841,7 +934,8 @@ Issues check_object_stability(const PrintObject *po, const Params &params) {
layer_lines, params);
islands_graph.push_back(std::move(layer_islands));
check_global_stability(layer->print_z, islands_graph, layer_lines, line_to_island_mapping, issues, params);
check_global_stability(layer->print_z, islands_graph, supports_presence_grid, layer_lines,
line_to_island_mapping, issues, params);
#ifdef DEBUG_FILES
for (size_t x = 0; x < size_t(layer_grid.get_pixel_count().x()); ++x) {