Merge branch 'master' into lm_sla_supports_auto

This commit is contained in:
Lukas Matena 2018-12-14 09:19:18 +01:00
commit 054c1ca3ae
93 changed files with 7430 additions and 1048 deletions

104
src/libslic3r/Channel.hpp Normal file
View file

@ -0,0 +1,104 @@
#ifndef slic3r_Channel_hpp_
#define slic3r_Channel_hpp_
#include <deque>
#include <condition_variable>
#include <mutex>
#include <utility>
#include <boost/optional.hpp>
namespace Slic3r {
template<class T> class Channel
{
private:
using UniqueLock = std::unique_lock<std::mutex>;
using Queue = std::deque<T>;
public:
class Guard
{
public:
Guard(UniqueLock lock, const Queue &queue) : m_lock(std::move(lock)), m_queue(queue) {}
Guard(const Guard &other) = delete;
Guard(Guard &&other) = delete;
~Guard() {}
// Access trampolines
size_t size() const noexcept { return m_queue.size(); }
bool empty() const noexcept { return m_queue.empty(); }
typename Queue::const_iterator begin() const noexcept { return m_queue.begin(); }
typename Queue::const_iterator end() const noexcept { return m_queue.end(); }
typename Queue::const_reference operator[](size_t i) const { return m_queue[i]; }
Guard& operator=(const Guard &other) = delete;
Guard& operator=(Guard &&other) = delete;
private:
UniqueLock m_lock;
const Queue &m_queue;
};
Channel() {}
~Channel() {}
void push(const T& item, bool silent = false)
{
{
UniqueLock lock(m_mutex);
m_queue.push_back(item);
}
if (! silent) { m_condition.notify_one(); }
}
void push(T &&item, bool silent = false)
{
{
UniqueLock lock(m_mutex);
m_queue.push_back(std::forward(item));
}
if (! silent) { m_condition.notify_one(); }
}
T pop()
{
UniqueLock lock(m_mutex);
m_condition.wait(lock, [this]() { return !m_queue.empty(); });
auto item = std::move(m_queue.front());
m_queue.pop_front();
return item;
}
boost::optional<T> try_pop()
{
UniqueLock lock(m_mutex);
if (m_queue.empty()) {
return boost::none;
} else {
auto item = std::move(m_queue.front());
m_queue.pop();
return item;
}
}
// Unlocked observers
// Thread unsafe! Keep in mind you need to re-verify the result after acquiring lock!
size_t size() const noexcept { return m_queue.size(); }
bool empty() const noexcept { return m_queue.empty(); }
Guard read() const
{
return Guard(UniqueLock(m_mutex), m_queue);
}
private:
Queue m_queue;
std::mutex m_mutex;
std::condition_variable m_condition;
};
} // namespace Slic3r
#endif // slic3r_Channel_hpp_

View file

@ -336,7 +336,7 @@ double ConfigBase::get_abs_value(const t_config_option_key &opt_key, double rati
return static_cast<const ConfigOptionFloatOrPercent*>(raw_opt)->get_abs_value(ratio_over);
}
void ConfigBase::setenv_()
void ConfigBase::setenv_() const
{
t_config_option_keys opt_keys = this->keys();
for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it) {

View file

@ -1113,7 +1113,7 @@ public:
double get_abs_value(const t_config_option_key &opt_key) const;
double get_abs_value(const t_config_option_key &opt_key, double ratio_over) const;
void setenv_();
void setenv_() const;
void load(const std::string &file);
void load_from_ini(const std::string &file);
void load_from_gcode_file(const std::string &file);

View file

@ -9,7 +9,9 @@
#endif /* SLIC3R_GUI */
#include "libslic3r.h"
#include "ClipperUtils.hpp"
#include "EdgeGrid.hpp"
#include "SVG.hpp"
#if 0
// Enable debugging and assert in this file.
@ -756,8 +758,8 @@ void EdgeGrid::Grid::calculate_sdf()
float search_radius = float(m_resolution<<1);
m_signed_distance_field.assign(nrows * ncols, search_radius);
// For each cell:
for (size_t r = 0; r < m_rows; ++ r) {
for (size_t c = 0; c < m_cols; ++ c) {
for (int r = 0; r < (int)m_rows; ++ r) {
for (int c = 0; c < (int)m_cols; ++ c) {
const Cell &cell = m_cells[r * m_cols + c];
// For each segment in the cell:
for (size_t i = cell.begin; i != cell.end; ++ i) {
@ -842,6 +844,8 @@ void EdgeGrid::Grid::calculate_sdf()
#if 0
static int iRun = 0;
++ iRun;
if (wxImage::FindHandler(wxBITMAP_TYPE_PNG) == nullptr)
wxImage::AddHandler(new wxPNGHandler);
//#ifdef SLIC3R_GUI
{
wxImage img(ncols, nrows);
@ -1356,9 +1360,101 @@ Polygons EdgeGrid::Grid::contours_simplified(coord_t offset, bool fill_holes) co
return out;
}
inline int segments_could_intersect(
const Slic3r::Point &ip1, const Slic3r::Point &ip2,
const Slic3r::Point &jp1, const Slic3r::Point &jp2)
{
Vec2i64 iv = (ip2 - ip1).cast<int64_t>();
Vec2i64 vij1 = (jp1 - ip1).cast<int64_t>();
Vec2i64 vij2 = (jp2 - ip1).cast<int64_t>();
int64_t tij1 = cross2(iv, vij1);
int64_t tij2 = cross2(iv, vij2);
int sij1 = (tij1 > 0) ? 1 : ((tij1 < 0) ? -1 : 0); // signum
int sij2 = (tij2 > 0) ? 1 : ((tij2 < 0) ? -1 : 0);
return sij1 * sij2;
}
inline bool segments_intersect(
const Slic3r::Point &ip1, const Slic3r::Point &ip2,
const Slic3r::Point &jp1, const Slic3r::Point &jp2)
{
return segments_could_intersect(ip1, ip2, jp1, jp2) <= 0 &&
segments_could_intersect(jp1, jp2, ip1, ip2) <= 0;
}
std::vector<std::pair<EdgeGrid::Grid::ContourEdge, EdgeGrid::Grid::ContourEdge>> EdgeGrid::Grid::intersecting_edges() const
{
std::vector<std::pair<ContourEdge, ContourEdge>> out;
// For each cell:
for (int r = 0; r < (int)m_rows; ++ r) {
for (int c = 0; c < (int)m_cols; ++ c) {
const Cell &cell = m_cells[r * m_cols + c];
// For each pair of segments in the cell:
for (size_t i = cell.begin; i != cell.end; ++ i) {
const Slic3r::Points &ipts = *m_contours[m_cell_data[i].first];
size_t ipt = m_cell_data[i].second;
// End points of the line segment and their vector.
const Slic3r::Point &ip1 = ipts[ipt];
const Slic3r::Point &ip2 = ipts[(ipt + 1 == ipts.size()) ? 0 : ipt + 1];
for (size_t j = i + 1; j != cell.end; ++ j) {
const Slic3r::Points &jpts = *m_contours[m_cell_data[j].first];
size_t jpt = m_cell_data[j].second;
// End points of the line segment and their vector.
const Slic3r::Point &jp1 = jpts[jpt];
const Slic3r::Point &jp2 = jpts[(jpt + 1 == jpts.size()) ? 0 : jpt + 1];
if (&ipts == &jpts && (&ip1 == &jp2 || &jp1 == &ip2))
// Segments of the same contour share a common vertex.
continue;
if (segments_intersect(ip1, ip2, jp1, jp2)) {
// The two segments intersect. Add them to the output.
int jfirst = (&jpts < &ipts) || (&jpts == &ipts && jpt < ipt);
out.emplace_back(jfirst ?
std::make_pair(std::make_pair(&ipts, ipt), std::make_pair(&jpts, jpt)) :
std::make_pair(std::make_pair(&ipts, ipt), std::make_pair(&jpts, jpt)));
}
}
}
}
}
Slic3r::sort_remove_duplicates(out);
return out;
}
bool EdgeGrid::Grid::has_intersecting_edges() const
{
// For each cell:
for (int r = 0; r < (int)m_rows; ++ r) {
for (int c = 0; c < (int)m_cols; ++ c) {
const Cell &cell = m_cells[r * m_cols + c];
// For each pair of segments in the cell:
for (size_t i = cell.begin; i != cell.end; ++ i) {
const Slic3r::Points &ipts = *m_contours[m_cell_data[i].first];
size_t ipt = m_cell_data[i].second;
// End points of the line segment and their vector.
const Slic3r::Point &ip1 = ipts[ipt];
const Slic3r::Point &ip2 = ipts[(ipt + 1 == ipts.size()) ? 0 : ipt + 1];
for (size_t j = i + 1; j != cell.end; ++ j) {
const Slic3r::Points &jpts = *m_contours[m_cell_data[j].first];
size_t jpt = m_cell_data[j].second;
// End points of the line segment and their vector.
const Slic3r::Point &jp1 = jpts[jpt];
const Slic3r::Point &jp2 = jpts[(jpt + 1 == jpts.size()) ? 0 : jpt + 1];
if (! (&ipts == &jpts && (&ip1 == &jp2 || &jp1 == &ip2)) &&
segments_intersect(ip1, ip2, jp1, jp2))
return true;
}
}
}
}
return false;
}
#if 0
void EdgeGrid::save_png(const EdgeGrid::Grid &grid, const BoundingBox &bbox, coord_t resolution, const char *path)
{
if (wxImage::FindHandler(wxBITMAP_TYPE_PNG) == nullptr)
wxImage::AddHandler(new wxPNGHandler);
unsigned int w = (bbox.max(0) - bbox.min(0) + resolution - 1) / resolution;
unsigned int h = (bbox.max(1) - bbox.min(1) + resolution - 1) / resolution;
wxImage img(w, h);
@ -1450,4 +1546,59 @@ void EdgeGrid::save_png(const EdgeGrid::Grid &grid, const BoundingBox &bbox, coo
}
#endif /* SLIC3R_GUI */
// Find all pairs of intersectiong edges from the set of polygons.
std::vector<std::pair<EdgeGrid::Grid::ContourEdge, EdgeGrid::Grid::ContourEdge>> intersecting_edges(const Polygons &polygons)
{
double len = 0;
size_t cnt = 0;
BoundingBox bbox;
for (const Polygon &poly : polygons) {
if (poly.points.size() < 2)
continue;
for (size_t i = 0; i < poly.points.size(); ++ i) {
bbox.merge(poly.points[i]);
size_t j = (i == 0) ? (poly.points.size() - 1) : i - 1;
len += (poly.points[j] - poly.points[i]).cast<double>().norm();
++ cnt;
}
}
len /= double(cnt);
bbox.offset(20);
EdgeGrid::Grid grid;
grid.set_bbox(bbox);
grid.create(polygons, len);
return grid.intersecting_edges();
}
// Find all pairs of intersectiong edges from the set of polygons, highlight them in an SVG.
void export_intersections_to_svg(const std::string &filename, const Polygons &polygons)
{
std::vector<std::pair<EdgeGrid::Grid::ContourEdge, EdgeGrid::Grid::ContourEdge>> intersections = intersecting_edges(polygons);
BoundingBox bbox = get_extents(polygons);
SVG svg(filename.c_str(), bbox);
svg.draw(union_ex(polygons), "gray", 0.25f);
svg.draw_outline(polygons, "black");
std::set<const Points*> intersecting_contours;
for (const std::pair<EdgeGrid::Grid::ContourEdge, EdgeGrid::Grid::ContourEdge> &ie : intersections) {
intersecting_contours.insert(ie.first.first);
intersecting_contours.insert(ie.second.first);
}
// Highlight the contours with intersections.
coord_t line_width = coord_t(scale_(0.01));
for (const Points *ic : intersecting_contours) {
svg.draw_outline(Polygon(*ic), "green");
svg.draw_outline(Polygon(*ic), "black", line_width);
}
// Paint the intersections.
for (const std::pair<EdgeGrid::Grid::ContourEdge, EdgeGrid::Grid::ContourEdge> &intersecting_edges : intersections) {
auto edge = [](const EdgeGrid::Grid::ContourEdge &e) {
return Line(e.first->at(e.second),
e.first->at((e.second + 1 == e.first->size()) ? 0 : e.second + 1));
};
svg.draw(edge(intersecting_edges.first), "red", line_width);
svg.draw(edge(intersecting_edges.second), "red", line_width);
}
svg.Close();
}
} // namespace Slic3r

View file

@ -133,7 +133,7 @@ void FillGyroid::_fill_surface_single(
// no rotation is supported for this infill pattern (yet)
BoundingBox bb = expolygon.contour.bounding_box();
// Density adjusted to have a good %of weight.
double density_adjusted = std::max(0., params.density * 2.);
double density_adjusted = std::max(0., params.density * 2.44);
// Distance between the gyroid waves in scaled coordinates.
coord_t distance = coord_t(scale_(this->spacing) / density_adjusted);

View file

@ -1,5 +1,7 @@
#include "PostProcessor.hpp"
#include <boost/log/trivial.hpp>
#ifdef WIN32
namespace Slic3r {
@ -25,9 +27,10 @@ void run_post_process_scripts(const std::string &path, const PrintConfig &config
{
if (config.post_process.values.empty())
return;
//config.setenv_();
config.setenv_();
auto gcode_file = boost::filesystem::path(path);
if (!boost::filesystem::exists(gcode_file))
if (! boost::filesystem::exists(gcode_file))
throw std::runtime_error(std::string("Post-processor can't find exported gcode file"));
for (std::string script: config.post_process.values) {

View file

@ -545,7 +545,8 @@ WipeTower::ToolChangeResult WipeTowerPrusaMM::prime(
m_print_brim = true;
// Ask our writer about how much material was consumed:
m_used_filament_length[m_current_tool] += writer.get_and_reset_used_filament_length();
if (m_current_tool < m_used_filament_length.size())
m_used_filament_length[m_current_tool] += writer.get_and_reset_used_filament_length();
ToolChangeResult result;
result.priming = true;
@ -698,7 +699,8 @@ WipeTower::ToolChangeResult WipeTowerPrusaMM::toolchange_Brim(bool sideOnly, flo
m_print_brim = false; // Mark the brim as extruded
// Ask our writer about how much material was consumed:
m_used_filament_length[m_current_tool] += writer.get_and_reset_used_filament_length();
if (m_current_tool < m_used_filament_length.size())
m_used_filament_length[m_current_tool] += writer.get_and_reset_used_filament_length();
ToolChangeResult result;
result.priming = false;
@ -868,7 +870,8 @@ void WipeTowerPrusaMM::toolchange_Change(
material_type new_material)
{
// Ask the writer about how much of the old filament we consumed:
m_used_filament_length[m_current_tool] += writer.get_and_reset_used_filament_length();
if (m_current_tool < m_used_filament_length.size())
m_used_filament_length[m_current_tool] += writer.get_and_reset_used_filament_length();
// Speed override for the material. Go slow for flex and soluble materials.
int speed_override;

View file

@ -521,10 +521,14 @@ void Model::adjust_min_z()
unsigned int Model::get_auto_extruder_id(unsigned int max_extruders)
{
unsigned int id = s_auto_extruder_id;
if (++s_auto_extruder_id > max_extruders)
if (id > max_extruders) {
// The current counter is invalid, likely due to switching the printer profiles
// to a profile with a lower number of extruders.
reset_auto_extruder_id();
id = s_auto_extruder_id;
} else if (++ s_auto_extruder_id > max_extruders) {
reset_auto_extruder_id();
}
return id;
}

View file

@ -107,6 +107,23 @@ extern BoundingBox get_extents(const MultiPoint &mp);
extern BoundingBox get_extents_rotated(const std::vector<Point> &points, double angle);
extern BoundingBox get_extents_rotated(const MultiPoint &mp, double angle);
inline double length(const Points &pts) {
double total = 0;
if (! pts.empty()) {
auto it = pts.begin();
for (auto it_prev = it ++; it != pts.end(); ++ it, ++ it_prev)
total += (*it - *it_prev).cast<double>().norm();
}
return total;
}
inline double area(const Points &polygon) {
double area = 0.;
for (size_t i = 0, j = polygon.size() - 1; i < polygon.size(); j = i ++)
area += double(polygon[i](0) + polygon[j](0)) * double(polygon[i](1) - polygon[j](1));
return area;
}
} // namespace Slic3r
#endif

View file

@ -187,6 +187,24 @@ public:
m_map.emplace(std::make_pair(Vec2crd(pt->x()>>m_grid_log2, pt->y()>>m_grid_log2), std::move(value)));
}
// Erase a data point equal to value. (ValueType has to declare the operator==).
// Returns true if the data point equal to value was found and removed.
bool erase(const ValueType &value) {
const Point *pt = m_point_accessor(value);
if (pt != nullptr) {
// Range of fragment starts around grid_corner, close to pt.
auto range = m_map.equal_range(Point((*pt)(0)>>m_grid_log2, (*pt)(1)>>m_grid_log2));
// Remove the first item.
for (auto it = range.first; it != range.second; ++ it) {
if (it->second == value) {
m_map.erase(it);
return true;
}
}
}
return false;
}
// Return a pair of <ValueType*, distance_squared>
std::pair<const ValueType*, double> find(const Vec2crd &pt) {
// Iterate over 4 closest grid cells around pt,
@ -214,7 +232,7 @@ public:
}
}
}
return (value_min != nullptr && dist_min < coordf_t(m_search_radius * m_search_radius)) ?
return (value_min != nullptr && dist_min < coordf_t(m_search_radius) * coordf_t(m_search_radius)) ?
std::make_pair(value_min, dist_min) :
std::make_pair(nullptr, std::numeric_limits<double>::max());
}

View file

@ -81,8 +81,8 @@ extern BoundingBox get_extents(const Polylines &polylines);
inline double total_length(const Polylines &polylines) {
double total = 0;
for (Polylines::const_iterator it = polylines.begin(); it != polylines.end(); ++it)
total += it->length();
for (const Polyline &pl : polylines)
total += pl.length();
return total;
}

View file

@ -14,6 +14,8 @@
#include "PrintExport.hpp"
#include <boost/filesystem/path.hpp>
//! macro used to mark string used at localization,
//! return same string
#define L(s) Slic3r::I18N::translate(s)
@ -281,16 +283,17 @@ bool Print::is_step_done(PrintObjectStep step) const
std::vector<unsigned int> Print::object_extruders() const
{
std::vector<unsigned int> extruders;
extruders.reserve(m_regions.size() * 3);
for (PrintRegion* region : m_regions) {
for (const PrintRegion *region : m_regions) {
// these checks reflect the same logic used in the GUI for enabling/disabling
// extruder selection fields
if (region->config().perimeters.value > 0 || m_config.brim_width.value > 0)
extruders.push_back(region->config().perimeter_extruder - 1);
extruders.emplace_back(region->config().perimeter_extruder - 1);
if (region->config().fill_density.value > 0)
extruders.push_back(region->config().infill_extruder - 1);
extruders.emplace_back(region->config().infill_extruder - 1);
if (region->config().top_solid_layers.value > 0 || region->config().bottom_solid_layers.value > 0)
extruders.push_back(region->config().solid_infill_extruder - 1);
extruders.emplace_back(region->config().solid_infill_extruder - 1);
}
sort_remove_duplicates(extruders);
@ -480,14 +483,6 @@ bool Print::apply_config(DynamicPrintConfig config)
PrintObjectConfig new_config = this->default_object_config();
// we override the new config with object-specific options
normalize_and_apply_config(new_config, object->model_object()->config);
// Force a refresh of a variable layer height profile at the PrintObject if it is not valid.
if (! object->layer_height_profile_valid) {
// The layer_height_profile is not valid for some reason (updated by the user or invalidated due to some option change).
// Invalidate the slicing step, which in turn invalidates everything.
object->invalidate_step(posSlice);
// Trigger recalculation.
invalidated = true;
}
// check whether the new config is different from the current one
t_config_option_keys diff = object->config().diff(new_config);
object->config_apply_only(new_config, diff, true);
@ -567,8 +562,7 @@ exit_for_rearrange_regions:
// Always make sure that the layer_height_profiles are set, as they should not be modified from the worker threads.
for (PrintObject *object : m_objects)
if (! object->layer_height_profile_valid)
object->update_layer_height_profile();
object->update_layer_height_profile();
return invalidated;
}
@ -1141,6 +1135,8 @@ Print::ApplyStatus Print::apply(const Model &model, const DynamicPrintConfig &co
// Always make sure that the layer_height_profiles are set, as they should not be modified from the worker threads.
for (PrintObject *object : m_objects)
if (! object->layer_height_profile_valid)
// No need to call the next line as the step should already be invalidated above.
// update_apply_status(object->invalidate_step(posSlice));
object->update_layer_height_profile();
//FIXME there may be a race condition with the G-code export running at the background thread.
@ -1165,6 +1161,7 @@ bool Print::has_skirt() const
|| this->has_infinite_skirt();
}
// Precondition: Print::validate() requires the Print::apply() to be called its invocation.
std::string Print::validate() const
{
if (m_objects.empty())
@ -1231,8 +1228,8 @@ std::string Print::validate() const
}
if (this->has_wipe_tower() && ! m_objects.empty()) {
if (m_config.gcode_flavor != gcfRepRap && m_config.gcode_flavor != gcfMarlin)
return L("The Wipe Tower is currently only supported for the Marlin and RepRap/Sprinter G-code flavors.");
if (m_config.gcode_flavor != gcfRepRap && m_config.gcode_flavor != gcfRepetier && m_config.gcode_flavor != gcfMarlin)
return L("The Wipe Tower is currently only supported for the Marlin, RepRap/Sprinter and Repetier G-code flavors.");
if (! m_config.use_relative_e_distances)
return L("The Wipe Tower is currently only supported with the relative extruder addressing (use_relative_e_distances=1).");
SlicingParameters slicing_params0 = m_objects.front()->slicing_parameters();
@ -1253,12 +1250,10 @@ std::string Print::validate() const
return L("The Wipe Tower is only supported for multiple objects if they are printed with the same support_material_contact_distance");
if (! equal_layering(slicing_params, slicing_params0))
return L("The Wipe Tower is only supported for multiple objects if they are sliced equally.");
bool was_layer_height_profile_valid = object->layer_height_profile_valid;
object->update_layer_height_profile();
object->layer_height_profile_valid = was_layer_height_profile_valid;
if ( m_config.variable_layer_height ) { // comparing layer height profiles
bool failed = false;
// layer_height_profile should be set by Print::apply().
if (tallest_object->layer_height_profile.size() >= object->layer_height_profile.size() ) {
int i = 0;
while ( i < object->layer_height_profile.size() && i < tallest_object->layer_height_profile.size()) {
@ -1867,5 +1862,96 @@ int Print::get_extruder(const ExtrusionEntityCollection& fill, const PrintRegion
std::max<int>(region.config().perimeter_extruder.value - 1, 0);
}
} // namespace Slic3r
std::string Print::output_filename() const
{
// Set the placeholders for the data know first after the G-code export is finished.
// These values will be just propagated into the output file name.
DynamicConfig config = this->finished() ? this->print_statistics().config() : this->print_statistics().placeholders();
return this->PrintBase::output_filename(m_config.output_filename_format.value, "gcode", &config);
}
// Shorten the dhms time by removing the seconds, rounding the dhm to full minutes
// and removing spaces.
static std::string short_time(const std::string &time)
{
// Parse the dhms time format.
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
if (time.find('d') != std::string::npos)
::sscanf(time.c_str(), "%dd %dh %dm %ds", &days, &hours, &minutes, &seconds);
else if (time.find('h') != std::string::npos)
::sscanf(time.c_str(), "%dh %dm %ds", &hours, &minutes, &seconds);
else if (time.find('m') != std::string::npos)
::sscanf(time.c_str(), "%dm %ds", &minutes, &seconds);
else if (time.find('s') != std::string::npos)
::sscanf(time.c_str(), "%ds", &seconds);
// Round to full minutes.
if (days + hours + minutes > 0 && seconds >= 30) {
if (++ minutes == 60) {
minutes = 0;
if (++ hours == 24) {
hours = 0;
++ days;
}
}
}
// Format the dhm time.
char buffer[64];
if (days > 0)
::sprintf(buffer, "%dd%dh%dm", days, hours, minutes);
else if (hours > 0)
::sprintf(buffer, "%dh%dm", hours, minutes);
else if (minutes > 0)
::sprintf(buffer, "%dm", minutes);
else
::sprintf(buffer, "%ds", seconds);
return buffer;
}
DynamicConfig PrintStatistics::config() const
{
DynamicConfig config;
std::string normal_print_time = short_time(this->estimated_normal_print_time);
std::string silent_print_time = short_time(this->estimated_silent_print_time);
config.set_key_value("print_time", new ConfigOptionString(normal_print_time));
config.set_key_value("normal_print_time", new ConfigOptionString(normal_print_time));
config.set_key_value("silent_print_time", new ConfigOptionString(silent_print_time));
config.set_key_value("used_filament", new ConfigOptionFloat (this->total_used_filament));
config.set_key_value("extruded_volume", new ConfigOptionFloat (this->total_extruded_volume));
config.set_key_value("total_cost", new ConfigOptionFloat (this->total_cost));
config.set_key_value("total_weight", new ConfigOptionFloat (this->total_weight));
config.set_key_value("total_wipe_tower_cost", new ConfigOptionFloat (this->total_wipe_tower_cost));
config.set_key_value("total_wipe_tower_filament", new ConfigOptionFloat (this->total_wipe_tower_filament));
return config;
}
DynamicConfig PrintStatistics::placeholders()
{
DynamicConfig config;
for (const std::string &key : {
"print_time", "normal_print_time", "silent_print_time",
"used_filament", "extruded_volume", "total_cost", "total_weight",
"total_wipe_tower_cost", "total_wipe_tower_filament"})
config.set_key_value(key, new ConfigOptionString(std::string("{") + key + "}"));
return config;
}
std::string PrintStatistics::finalize_output_path(const std::string &path_in) const
{
std::string final_path;
try {
boost::filesystem::path path(path_in);
DynamicConfig cfg = this->config();
PlaceholderParser pp;
std::string new_stem = pp.process(path.stem().string(), 0, &cfg);
final_path = (path.parent_path() / (new_stem + path.extension().string())).string();
} catch (const std::exception &ex) {
BOOST_LOG_TRIVIAL(error) << "Failed to apply the print statistics to the export file name: " << ex.what();
final_path = path_in;
}
return final_path;
}
} // namespace Slic3r

View file

@ -78,7 +78,6 @@ private: // Prevents erroneous use by other classes.
public:
// vector of (vectors of volume ids), indexed by region_id
std::vector<std::vector<int>> region_volumes;
t_layer_height_ranges layer_height_ranges;
// Profile of increasing z to a layer height, to be linearly interpolated when calculating the layers.
// The pairs of <z, layer_height> are packed into a 1D array to simplify handling by the Perl XS.
@ -250,6 +249,13 @@ struct PrintStatistics
double total_wipe_tower_filament;
std::map<size_t, float> filament_stats;
// Config with the filled in print statistics.
DynamicConfig config() const;
// Config with the statistics keys populated with placeholder strings.
static DynamicConfig placeholders();
// Replace the print statistics placeholders in the path.
std::string finalize_output_path(const std::string &path_in) const;
void clear() {
estimated_normal_print_time.clear();
estimated_silent_print_time.clear();
@ -298,7 +304,10 @@ public:
// methods for handling state
bool is_step_done(PrintStep step) const { return Inherited::is_step_done(step); }
// Returns true if an object step is done on all objects and there's at least one object.
bool is_step_done(PrintObjectStep step) const;
// Returns true if the last step was finished with success.
bool finished() const override { return this->is_step_done(psGCodeExport); }
bool has_infinite_skirt() const;
bool has_skirt() const;
@ -343,8 +352,7 @@ public:
bool has_wipe_tower() const;
const WipeTowerData& wipe_tower_data() const { return m_wipe_tower_data; }
std::string output_filename() const override
{ return this->PrintBase::output_filename(m_config.output_filename_format.value, "gcode"); }
std::string output_filename() const override;
// Accessed by SupportMaterial
const PrintRegion* get_region(size_t idx) const { return m_regions[idx]; }

View file

@ -48,12 +48,14 @@ void PrintBase::update_object_placeholders()
}
}
std::string PrintBase::output_filename(const std::string &format, const std::string &default_ext) const
std::string PrintBase::output_filename(const std::string &format, const std::string &default_ext, const DynamicConfig *config_override) const
{
DynamicConfig cfg_timestamp;
PlaceholderParser::update_timestamp(cfg_timestamp);
DynamicConfig cfg;
if (config_override != nullptr)
cfg = *config_override;
PlaceholderParser::update_timestamp(cfg);
try {
boost::filesystem::path filename = this->placeholder_parser().process(format, 0, &cfg_timestamp);
boost::filesystem::path filename = this->placeholder_parser().process(format, 0, &cfg);
if (filename.extension().empty())
filename = boost::filesystem::change_extension(filename, default_ext);
return filename.string();

View file

@ -285,6 +285,8 @@ public:
void cancel_internal() { m_cancel_status = CANCELED_INTERNAL; }
// Cancel the running computation. Stop execution of all the background threads.
void restart() { m_cancel_status = NOT_CANCELED; }
// Returns true if the last step was finished with success.
virtual bool finished() const = 0;
const PlaceholderParser& placeholder_parser() const { return m_placeholder_parser; }
PlaceholderParser& placeholder_parser() { return m_placeholder_parser; }
@ -305,7 +307,7 @@ protected:
void throw_if_canceled() const { if (m_cancel_status) throw CanceledException(); }
// To be called by this->output_filename() with the format string pulled from the configuration layer.
std::string output_filename(const std::string &format, const std::string &default_ext) const;
std::string output_filename(const std::string &format, const std::string &default_ext, const DynamicConfig *config_override = nullptr) const;
// Update "scale", "input_filename", "input_filename_base" placeholders from the current printable ModelObjects.
void update_object_placeholders();

View file

@ -1297,10 +1297,11 @@ void PrintConfigDef::init_fff_params()
def->default_value = new ConfigOptionString("");
def = this->add("printhost_cafile", coString);
def->label = "HTTPS CA file";
def->label = "HTTPS CA File";
def->tooltip = "Custom CA certificate file can be specified for HTTPS OctoPrint connections, in crt/pem format. "
"If left blank, the default OS CA certificate repository is used.";
def->cli = "printhost-cafile=s";
def->mode = comAdvanced;
def->default_value = new ConfigOptionString("");
def = this->add("print_host", coString);
@ -2388,6 +2389,15 @@ void PrintConfigDef::init_sla_params()
def->min = 100;
def->default_value = new ConfigOptionInt(1440);
def = this->add("display_orientation", coEnum);
def->label = L("Display orientation");
def->tooltip = L("Display orientation");
def->cli = "display-orientation=s";
def->enum_keys_map = &ConfigOptionEnum<SLADisplayOrientation>::get_enum_values();
def->enum_values.push_back("Landscape");
def->enum_values.push_back("Portrait");
def->default_value = new ConfigOptionEnum<SLADisplayOrientation>(sladoPortrait);
def = this->add("printer_correction", coFloats);
def->full_label = L("Printer scaling correction");
def->tooltip = L("Printer scaling correction");

View file

@ -56,6 +56,11 @@ enum FilamentType {
ftPLA, ftABS, ftPET, ftHIPS, ftFLEX, ftSCAFF, ftEDGE, ftNGEN, ftPVA
};
enum SLADisplayOrientation {
sladoLandscape,
sladoPortrait
};
template<> inline const t_config_enum_values& ConfigOptionEnum<PrinterTechnology>::get_enum_values() {
static t_config_enum_values keys_map;
if (keys_map.empty()) {
@ -148,6 +153,15 @@ template<> inline const t_config_enum_values& ConfigOptionEnum<FilamentType>::ge
return keys_map;
}
template<> inline const t_config_enum_values& ConfigOptionEnum<SLADisplayOrientation>::get_enum_values() {
static const t_config_enum_values keys_map = {
{ "Landscape", sladoLandscape},
{ "Portrait", sladoPortrait}
};
return keys_map;
}
// Defines each and every confiuration option of Slic3r, including the properties of the GUI dialogs.
// Does not store the actual values, but defines default values.
class PrintConfigDef : public ConfigDef
@ -1035,6 +1049,7 @@ public:
ConfigOptionFloat display_height;
ConfigOptionInt display_pixels_x;
ConfigOptionInt display_pixels_y;
ConfigOptionEnum<SLADisplayOrientation> display_orientation;
ConfigOptionFloats printer_correction;
protected:
void initialize(StaticCacheBase &cache, const char *base_ptr)
@ -1046,6 +1061,7 @@ protected:
OPT_PTR(display_height);
OPT_PTR(display_pixels_x);
OPT_PTR(display_pixels_y);
OPT_PTR(display_orientation);
OPT_PTR(printer_correction);
}
};

View file

@ -31,8 +31,6 @@ template<FilePrinterFormat format>
class FilePrinter {
public:
void print_config(const Print&);
// Draw an ExPolygon which is a polygon inside a slice on the specified layer.
void draw_polygon(const ExPolygon& p, unsigned lyr);
@ -118,6 +116,7 @@ template<> class FilePrinter<FilePrinterFormat::SLA_PNGZIP>
Raster::PixelDim m_pxdim;
double m_exp_time_s = .0, m_exp_time_first_s = .0;
double m_layer_height = .0;
Raster::Origin m_o = Raster::Origin::TOP_LEFT;
std::string createIniContent(const std::string& projectname) {
double layer_height = m_layer_height;
@ -147,19 +146,41 @@ template<> class FilePrinter<FilePrinterFormat::SLA_PNGZIP>
+layerh_str+"+printer=DWARF3\n";
}
// Change this to TOP_LEFT if you want correct PNG orientation
static const Raster::Origin ORIGIN = Raster::Origin::BOTTOM_LEFT;
public:
enum RasterOrientation {
RO_LANDSCAPE,
RO_PORTRAIT
};
// We will play with the raster's coordinate origin parameter. When the
// printer should print in landscape mode it should have the Y axis flipped
// because the layers should be displayed upside down. PNG has its
// coordinate origin in the top-left corner so normally the Raster objects
// should be instantiated with the TOP_LEFT flag. However, in landscape mode
// we do want the pictures to be upside down so we will make BOTTOM_LEFT
// type rasters and the PNG format will do the flipping automatically.
// In case of portrait images, we have to rotate the image by a 90 degrees
// and flip the y axis. To get the correct upside-down orientation of the
// slice images, we can flip the x and y coordinates of the input polygons
// and do the Y flipping of the image. This will generate the correct
// orientation in portrait mode.
inline FilePrinter(double width_mm, double height_mm,
unsigned width_px, unsigned height_px,
double layer_height,
double exp_time, double exp_time_first):
double exp_time, double exp_time_first,
RasterOrientation ro = RO_PORTRAIT):
m_res(width_px, height_px),
m_pxdim(width_mm/width_px, height_mm/height_px),
m_exp_time_s(exp_time),
m_exp_time_first_s(exp_time_first),
m_layer_height(layer_height)
m_layer_height(layer_height),
// Here is the trick with the orientation.
m_o(ro == RO_LANDSCAPE? Raster::Origin::BOTTOM_LEFT :
Raster::Origin::TOP_LEFT )
{
}
@ -179,12 +200,12 @@ public:
inline void begin_layer(unsigned lyr) {
if(m_layers_rst.size() <= lyr) m_layers_rst.resize(lyr+1);
m_layers_rst[lyr].first.reset(m_res, m_pxdim, ORIGIN);
m_layers_rst[lyr].first.reset(m_res, m_pxdim, m_o);
}
inline void begin_layer() {
m_layers_rst.emplace_back();
m_layers_rst.front().first.reset(m_res, m_pxdim, ORIGIN);
m_layers_rst.front().first.reset(m_res, m_pxdim, m_o);
}
inline void finish_layer(unsigned lyr_id) {
@ -206,29 +227,35 @@ public:
inline void save(const std::string& path) {
try {
LayerWriter<LyrFmt> writer(path);
if(!writer.is_ok()) return;
std::string project = writer.get_name();
writer.next_entry("config.ini");
if(!writer.is_ok()) return;
writer << createIniContent(project);
for(unsigned i = 0; i < m_layers_rst.size(); i++) {
for(unsigned i = 0; i < m_layers_rst.size() && writer.is_ok(); i++)
{
if(m_layers_rst[i].second.rdbuf()->in_avail() > 0) {
char lyrnum[6];
std::sprintf(lyrnum, "%.5d", i);
auto zfilename = project + lyrnum + ".png";
writer.next_entry(zfilename);
if(!writer.is_ok()) break;
writer << m_layers_rst[i].second.str();
// writer << m_layers_rst[i].second.rdbuf();
// we can keep the date for later calls of this method
//m_layers_rst[i].second.str("");
}
}
writer.close();
} catch(std::exception& e) {
BOOST_LOG_TRIVIAL(error) << e.what();
return;
// Rethrow the exception
throw;
}
}

View file

@ -65,7 +65,6 @@ PrintObject::PrintObject(Print* print, ModelObject* model_object, bool add_insta
this->set_copies(copies);
}
this->layer_height_ranges = model_object->layer_height_ranges;
this->layer_height_profile = model_object->layer_height_profile;
}
@ -1109,7 +1108,7 @@ void PrintObject::discover_vertical_shells()
#if 1
// Intentionally inflate a bit more than how much the region has been shrunk,
// so there will be some overlap between this solid infill and the other infill regions (mainly the sparse infill).
shell = offset2(shell, - 0.5f * min_perimeter_infill_spacing, 0.8f * min_perimeter_infill_spacing, ClipperLib::jtSquare);
shell = offset(offset_ex(union_ex(shell), - 0.5f * min_perimeter_infill_spacing), 0.8f * min_perimeter_infill_spacing, ClipperLib::jtSquare);
if (shell.empty())
continue;
#else
@ -1330,7 +1329,7 @@ bool PrintObject::update_layer_height_profile(std::vector<coordf_t> &layer_heigh
bool updated = false;
// If the layer height profile is not set, try to use the one stored at the ModelObject.
if (layer_height_profile.empty() && layer_height_profile.data() != this->model_object()->layer_height_profile.data()) {
if (layer_height_profile.empty()) {
layer_height_profile = this->model_object()->layer_height_profile;
updated = true;
}
@ -1347,10 +1346,9 @@ bool PrintObject::update_layer_height_profile(std::vector<coordf_t> &layer_heigh
if (layer_height_profile.empty()) {
if (0)
// if (this->layer_height_profile.empty())
layer_height_profile = layer_height_profile_adaptive(slicing_params, this->layer_height_ranges,
this->model_object()->volumes);
layer_height_profile = layer_height_profile_adaptive(slicing_params, this->model_object()->layer_height_ranges, this->model_object()->volumes);
else
layer_height_profile = layer_height_profile_from_ranges(slicing_params, this->layer_height_ranges);
layer_height_profile = layer_height_profile_from_ranges(slicing_params, this->model_object()->layer_height_ranges);
updated = true;
}
return updated;

View file

@ -45,15 +45,20 @@ private:
TRawRenderer m_raw_renderer;
TRendererAA m_renderer;
Origin m_o;
std::function<void(agg::path_storage&)> m_flipy = [](agg::path_storage&) {};
inline void flipy(agg::path_storage& path) const {
path.flip_y(0, m_resolution.height_px);
}
public:
inline Impl(const Raster::Resolution& res, const Raster::PixelDim &pd,
Origin o):
m_resolution(res), m_pxdim(pd),
m_buf(res.pixels()),
m_rbuf(reinterpret_cast<TPixelRenderer::value_type*>(m_buf.data()),
res.width_px, res.height_px,
res.width_px*TPixelRenderer::num_components),
int(res.width_px*TPixelRenderer::num_components)),
m_pixfmt(m_rbuf),
m_raw_renderer(m_pixfmt),
m_renderer(m_raw_renderer),
@ -65,10 +70,6 @@ public:
// ras.gamma(agg::gamma_power(1.0));
clear();
if(m_o == Origin::TOP_LEFT) m_flipy = [this](agg::path_storage& path) {
path.flip_y(0, m_resolution.height_px);
};
}
void draw(const ExPolygon &poly) {
@ -76,12 +77,14 @@ public:
agg::scanline_p8 scanlines;
auto&& path = to_path(poly.contour);
m_flipy(path);
if(m_o == Origin::TOP_LEFT) flipy(path);
ras.add_path(path);
for(auto h : poly.holes) {
auto&& holepath = to_path(h);
m_flipy(holepath);
if(m_o == Origin::TOP_LEFT) flipy(holepath);
ras.add_path(holepath);
}
@ -205,8 +208,9 @@ void Raster::save(std::ostream& stream, Compression comp)
<< m_impl->resolution().height_px << " "
<< "255 ";
auto sz = m_impl->buffer().size()*sizeof(Impl::TBuffer::value_type);
stream.write(reinterpret_cast<const char*>(m_impl->buffer().data()),
m_impl->buffer().size()*sizeof(Impl::TBuffer::value_type));
std::streamsize(sz));
}
}
}

View file

@ -27,6 +27,13 @@ public:
PNG //!> PNG compression
};
/// The Rasterizer expects the input polygons to have their coordinate
/// system origin in the bottom left corner. If the raster is then
/// configured with the TOP_LEFT origin parameter (in the constructor) than
/// it will flip the Y axis in output to maintain the correct orientation.
/// This is the default case with PNG images. They have the origin in the
/// top left corner. Without the flipping, the image would be upside down
/// with the scaled (clipper) coordinate system of the input polygons.
enum class Origin {
TOP_LEFT,
BOTTOM_LEFT

View file

@ -0,0 +1,186 @@
#ifndef BICUBIC_HPP
#define BICUBIC_HPP
#include <algorithm>
#include <vector>
#include <cmath>
#include <Eigen/Dense>
namespace Slic3r {
namespace BicubicInternal {
// Linear kernel, to be able to test cubic methods with hat kernels.
template<typename T>
struct LinearKernel
{
typedef T FloatType;
static T a00() { return T(0.); }
static T a01() { return T(0.); }
static T a02() { return T(0.); }
static T a03() { return T(0.); }
static T a10() { return T(1.); }
static T a11() { return T(-1.); }
static T a12() { return T(0.); }
static T a13() { return T(0.); }
static T a20() { return T(0.); }
static T a21() { return T(1.); }
static T a22() { return T(0.); }
static T a23() { return T(0.); }
static T a30() { return T(0.); }
static T a31() { return T(0.); }
static T a32() { return T(0.); }
static T a33() { return T(0.); }
};
// Interpolation kernel aka Catmul-Rom aka Keyes kernel.
template<typename T>
struct CubicCatmulRomKernel
{
typedef T FloatType;
static T a00() { return 0; }
static T a01() { return (T)-0.5; }
static T a02() { return (T) 1.; }
static T a03() { return (T)-0.5; }
static T a10() { return (T) 1.; }
static T a11() { return 0; }
static T a12() { return (T)-5./2.; }
static T a13() { return (T) 3./2.; }
static T a20() { return 0; }
static T a21() { return (T) 0.5; }
static T a22() { return (T) 2.; }
static T a23() { return (T)-3./2.; }
static T a30() { return 0; }
static T a31() { return 0; }
static T a32() { return (T)-0.5; }
static T a33() { return (T) 0.5; }
};
// B-spline kernel
template<typename T>
struct CubicBSplineKernel
{
typedef T FloatType;
static T a00() { return (T) 1./6.; }
static T a01() { return (T) -3./6.; }
static T a02() { return (T) 3./6.; }
static T a03() { return (T) -1./6.; }
static T a10() { return (T) 4./6.; }
static T a11() { return 0; }
static T a12() { return (T) -6./6.; }
static T a13() { return (T) 3./6.; }
static T a20() { return (T) 1./6.; }
static T a21() { return (T) 3./6.; }
static T a22() { return (T) 3./6.; }
static T a23() { return (T)- 3./6.; }
static T a30() { return 0; }
static T a31() { return 0; }
static T a32() { return 0; }
static T a33() { return (T) 1./6.; }
};
template<class T>
inline T clamp(T a, T lower, T upper)
{
return (a < lower) ? lower :
(a > upper) ? upper : a;
}
}
template<typename KERNEL>
struct CubicKernel
{
typedef typename KERNEL KernelInternal;
typedef typename KERNEL::FloatType FloatType;
static FloatType kernel(FloatType x)
{
x = fabs(x);
if (x >= (FloatType)2.)
return 0.0f;
if (x <= (FloatType)1.) {
FloatType x2 = x * x;
FloatType x3 = x2 * x;
return KERNEL::a10() + KERNEL::a11() * x + KERNEL::a12() * x2 + KERNEL::a13() * x3;
}
assert(x > (FloatType)1. && x < (FloatType)2.);
x -= (FloatType)1.;
FloatType x2 = x * x;
FloatType x3 = x2 * x;
return KERNEL::a00() + KERNEL::a01() * x + KERNEL::a02() * x2 + KERNEL::a03() * x3;
}
static FloatType interpolate(FloatType f0, FloatType f1, FloatType f2, FloatType f3, FloatType x)
{
const FloatType x2 = x*x;
const FloatType x3 = x*x*x;
return f0*(KERNEL::a00() + KERNEL::a01() * x + KERNEL::a02() * x2 + KERNEL::a03() * x3) +
f1*(KERNEL::a10() + KERNEL::a11() * x + KERNEL::a12() * x2 + KERNEL::a13() * x3) +
f2*(KERNEL::a20() + KERNEL::a21() * x + KERNEL::a22() * x2 + KERNEL::a23() * x3) +
f3*(KERNEL::a30() + KERNEL::a31() * x + KERNEL::a32() * x2 + KERNEL::a33() * x3);
}
};
// Linear splines
typedef CubicKernel<BicubicInternal::LinearKernel<float>> LinearKernelf;
typedef CubicKernel<BicubicInternal::LinearKernel<double>> LinearKerneld;
// Catmul-Rom splines
typedef CubicKernel<BicubicInternal::CubicCatmulRomKernel<float>> CubicCatmulRomKernelf;
typedef CubicKernel<BicubicInternal::CubicCatmulRomKernel<double>> CubicCatmulRomKerneld;
typedef CubicKernel<BicubicInternal::CubicCatmulRomKernel<float>> CubicInterpolationKernelf;
typedef CubicKernel<BicubicInternal::CubicCatmulRomKernel<double>> CubicInterpolationKerneld;
// Cubic B-splines
typedef CubicKernel<BicubicInternal::CubicBSplineKernel<float>> CubicBSplineKernelf;
typedef CubicKernel<BicubicInternal::CubicBSplineKernel<double>> CubicBSplineKerneld;
template<typename KERNEL, typename Derived>
static float cubic_interpolate(const Eigen::ArrayBase<Derived> &F, const typename KERNEL::FloatType pt, const typename KERNEL::FloatType dx)
{
typedef typename KERNEL::FloatType T;
const int w = int(F.size());
const int ix = (int)floor(pt);
const T s = pt - (T)ix;
if (ix > 1 && ix + 2 < w) {
// Inside the fully interpolated region.
return KERNEL::interpolate(F[ix - 1], F[ix], F[ix + 1], F[ix + 2], s);
}
// Transition region. Extend with a constant function.
auto f = [&F, w](x) { return F[BicubicInternal::clamp(x, 0, w - 1)]; }
return KERNEL::interpolate(f(ix - 1), f(ix), f(ix + 1), f(ix + 2), s);
}
template<typename KERNEL, typename Derived>
static float bicubic_interpolate(const Eigen::MatrixBase<Derived> &F, const Eigen::Matrix<typename KERNEL::FloatType, 2, 1, Eigen::DontAlign> &pt, const typename KERNEL::FloatType dx)
{
typedef typename KERNEL::FloatType T;
const int w = F.cols();
const int h = F.rows();
const int ix = (int)floor(pt[0]);
const int iy = (int)floor(pt[1]);
const T s = pt[0] - (T)ix;
const T t = pt[1] - (T)iy;
if (ix > 1 && ix + 2 < w && iy > 1 && iy + 2 < h) {
// Inside the fully interpolated region.
return KERNEL::interpolate(
KERNEL::interpolate(F(ix-1,iy-1),F(ix ,iy-1),F(ix+1,iy-1),F(ix+2,iy-1),s),
KERNEL::interpolate(F(ix-1,iy ),F(ix ,iy ),F(ix+1,iy ),F(ix+2,iy ),s),
KERNEL::interpolate(F(ix-1,iy+1),F(ix ,iy+1),F(ix+1,iy+1),F(ix+2,iy+1),s),
KERNEL::interpolate(F(ix-1,iy+2),F(ix ,iy+2),F(ix+1,iy+2),F(ix+2,iy+2),s),t);
}
// Transition region. Extend with a constant function.
auto f = [&f, w, h](int x, int y) { return F(BicubicInternal::clamp(x,0,w-1),BicubicInternal::clamp(y,0,h-1)); }
return KERNEL::interpolate(
KERNEL::interpolate(f(ix-1,iy-1),f(ix ,iy-1),f(ix+1,iy-1),f(ix+2,iy-1),s),
KERNEL::interpolate(f(ix-1,iy ),f(ix ,iy ),f(ix+1,iy ),f(ix+2,iy ),s),
KERNEL::interpolate(f(ix-1,iy+1),f(ix ,iy+1),f(ix+1,iy+1),f(ix+2,iy+1),s),
KERNEL::interpolate(f(ix-1,iy+2),f(ix ,iy+2),f(ix+1,iy+2),f(ix+2,iy+2),s),t);
}
} // namespace Slic3r
#endif /* BICUBIC_HPP */

View file

@ -31,7 +31,9 @@ Contour3D convert(const Polygons& triangles, coord_t z, bool dir) {
}
Contour3D walls(const ExPolygon& floor_plate, const ExPolygon& ceiling,
double floor_z_mm, double ceiling_z_mm) {
double floor_z_mm, double ceiling_z_mm,
ThrowOnCancel thr)
{
using std::transform; using std::back_inserter;
ExPolygon poly;
@ -61,8 +63,10 @@ Contour3D walls(const ExPolygon& floor_plate, const ExPolygon& ceiling,
};
std::for_each(tri.begin(), tri.end(),
[&rp, &rpi, &poly, &idx, is_upper, fz, cz](const Polygon& pp)
[&rp, &rpi, thr, &idx, is_upper, fz, cz](const Polygon& pp)
{
thr(); // may throw if cancellation was requested
for(auto& p : pp.points)
if(is_upper(p))
rp.emplace_back(unscale(x(p), y(p), mm(cz)));
@ -213,11 +217,12 @@ inline Contour3D roofs(const ExPolygon& poly, coord_t z_distance) {
template<class ExP, class D>
Contour3D round_edges(const ExPolygon& base_plate,
double radius_mm,
double degrees,
double ceilheight_mm,
bool dir,
ExP&& last_offset = ExP(), D&& last_height = D())
double radius_mm,
double degrees,
double ceilheight_mm,
bool dir,
ThrowOnCancel throw_on_cancel,
ExP&& last_offset = ExP(), D&& last_height = D())
{
auto ob = base_plate;
auto ob_prev = ob;
@ -231,6 +236,8 @@ Contour3D round_edges(const ExPolygon& base_plate,
if(degrees >= 90) {
for(int i = 1; i <= steps; ++i) {
throw_on_cancel();
ob = base_plate;
double r2 = radius_mm * radius_mm;
@ -242,7 +249,7 @@ Contour3D round_edges(const ExPolygon& base_plate,
wh = ceilheight_mm - radius_mm + stepy;
Contour3D pwalls;
pwalls = walls(ob, ob_prev, wh, wh_prev);
pwalls = walls(ob, ob_prev, wh, wh_prev, throw_on_cancel);
curvedwalls.merge(pwalls);
ob_prev = ob;
@ -254,6 +261,7 @@ Contour3D round_edges(const ExPolygon& base_plate,
int tos = int(tox / stepx);
for(int i = 1; i <= tos; ++i) {
throw_on_cancel();
ob = base_plate;
double r2 = radius_mm * radius_mm;
@ -264,7 +272,7 @@ Contour3D round_edges(const ExPolygon& base_plate,
wh = ceilheight_mm - radius_mm - stepy;
Contour3D pwalls;
pwalls = walls(ob_prev, ob, wh_prev, wh);
pwalls = walls(ob_prev, ob, wh_prev, wh, throw_on_cancel);
curvedwalls.merge(pwalls);
ob_prev = ob;
@ -348,7 +356,8 @@ inline Point centroid(const ExPolygon& poly) {
/// with explicit bridges. Bridges are generated from each shape's centroid
/// to the center of the "scene" which is the centroid calculated from the shape
/// centroids (a star is created...)
ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50)
ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50,
ThrowOnCancel throw_on_cancel = [](){})
{
namespace bgi = boost::geometry::index;
using SpatElement = std::pair<BoundingBox, unsigned>;
@ -383,9 +392,10 @@ ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50)
idx = 0;
std::transform(centroids.begin(), centroids.end(),
std::back_inserter(punion),
[&punion, &boxindex, cc, max_dist_mm, &idx](const Point& c)
[&punion, &boxindex, cc, max_dist_mm, &idx, throw_on_cancel]
(const Point& c)
{
throw_on_cancel();
double dx = x(c) - x(cc), dy = y(c) - y(cc);
double l = std::sqrt(dx * dx + dy * dy);
double nx = dx / l, ny = dy / l;
@ -419,7 +429,7 @@ ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50)
}
void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h,
float layerh)
float layerh, ThrowOnCancel thrfn)
{
TriangleMesh m = mesh;
TriangleMeshSlicer slicer(&m);
@ -431,14 +441,17 @@ void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h,
heights.emplace_back(hi);
std::vector<ExPolygons> out; out.reserve(size_t(std::ceil(h/layerh)));
slicer.slice(heights, &out, [](){});
slicer.slice(heights, &out, thrfn);
size_t count = 0; for(auto& o : out) count += o.size();
ExPolygons tmp; tmp.reserve(count);
for(auto& o : out) for(auto& e : o) tmp.emplace_back(std::move(e));
output = unify(tmp);
for(auto& o : output) o = o.simplify(0.1/SCALING_FACTOR).front();
ExPolygons utmp = unify(tmp);
for(auto& o : utmp) {
auto&& smp = o.simplify(0.1/SCALING_FACTOR);
output.insert(output.end(), smp.begin(), smp.end());
}
}
void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out,
@ -447,7 +460,7 @@ void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out,
double mdist = 2*(1.8*cfg.min_wall_thickness_mm + 4*cfg.edge_radius_mm) +
cfg.max_merge_distance_mm;
auto concavehs = concave_hull(ground_layer, mdist);
auto concavehs = concave_hull(ground_layer, mdist, cfg.throw_on_cancel);
for(ExPolygon& concaveh : concavehs) {
if(concaveh.contour.points.empty()) return;
concaveh.holes.clear();
@ -505,6 +518,7 @@ void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out,
phi, // 170 degrees
0, // z position of the input plane
true,
cfg.throw_on_cancel,
ob, wh);
pool.merge(curvedwalls);
@ -512,7 +526,8 @@ void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out,
ExPolygon ob_contr = ob;
ob_contr.holes.clear();
auto pwalls = walls(ob_contr, inner_base, wh, -cfg.min_wall_height_mm);
auto pwalls = walls(ob_contr, inner_base, wh, -cfg.min_wall_height_mm,
cfg.throw_on_cancel);
pool.merge(pwalls);
Polygons top_triangles, bottom_triangles;
@ -528,6 +543,7 @@ void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out,
90, // 90 degrees
0, // z position of the input plane
false,
cfg.throw_on_cancel,
ob, wh);
pool.merge(curvedwalls);

View file

@ -2,6 +2,7 @@
#define SLABASEPOOL_HPP
#include <vector>
#include <functional>
namespace Slic3r {
@ -11,12 +12,14 @@ class TriangleMesh;
namespace sla {
using ExPolygons = std::vector<ExPolygon>;
using ThrowOnCancel = std::function<void(void)>;
/// Calculate the polygon representing the silhouette from the specified height
void base_plate(const TriangleMesh& mesh,
ExPolygons& output,
float zlevel = 0.1f,
float layerheight = 0.05f);
void base_plate(const TriangleMesh& mesh, // input mesh
ExPolygons& output, // Output will be merged with
float zlevel = 0.1f, // Plate creation level
float layerheight = 0.05f, // The sampling height
ThrowOnCancel thrfn = [](){}); // Will be called frequently
struct PoolConfig {
double min_wall_thickness_mm = 2;
@ -24,6 +27,8 @@ struct PoolConfig {
double max_merge_distance_mm = 50;
double edge_radius_mm = 1;
ThrowOnCancel throw_on_cancel = [](){};
inline PoolConfig() {}
inline PoolConfig(double wt, double wh, double md, double er):
min_wall_thickness_mm(wt),
@ -35,8 +40,7 @@ struct PoolConfig {
/// Calculate the pool for the mesh for SLA printing
void create_base_pool(const ExPolygons& base_plate,
TriangleMesh& output_mesh,
const PoolConfig& = PoolConfig()
);
const PoolConfig& = PoolConfig());
/// TODO: Currently the base plate of the pool will have half the height of the
/// whole pool. So the carved out space has also half the height. This is not

View file

@ -58,7 +58,7 @@ struct Contour3D {
points.insert(points.end(), ctr.points.begin(), ctr.points.end());
indices.insert(indices.end(), ctr.indices.begin(), ctr.indices.end());
for(auto n = s; n < indices.size(); n++) {
for(size_t n = s; n < indices.size(); n++) {
auto& idx = indices[n]; x(idx) += s3; y(idx) += s3; z(idx) += s3;
}
}

View file

@ -20,7 +20,6 @@ std::array<double, 3> find_best_rotation(const ModelObject& modelobj,
using libnest2d::opt::Optimizer;
using libnest2d::opt::TOptimizer;
using libnest2d::opt::StopCriteria;
using Quaternion = Eigen::Quaternion<double>;
static const unsigned MAX_TRIES = 100000;

View file

@ -169,7 +169,7 @@ Contour3D cylinder(double r, double h, size_t ssteps) {
auto steps = int(ssteps);
auto& points = ret.points;
auto& indices = ret.indices;
points.reserve(2*steps);
points.reserve(2*ssteps);
double a = 2*PI/steps;
Vec3d jp = {0, 0, 0};
@ -189,7 +189,7 @@ Contour3D cylinder(double r, double h, size_t ssteps) {
points.emplace_back(x, y, jp(Z));
}
indices.reserve(2*steps);
indices.reserve(2*ssteps);
auto offs = steps;
for(int i = 0; i < steps - 1; ++i) {
indices.emplace_back(i, i + offs, offs + i + 1);
@ -347,19 +347,22 @@ struct Pillar {
double radius = 1, size_t st = 45):
r(radius), steps(st), endpoint(endp), starts_from_head(false)
{
assert(steps > 0);
int steps_1 = int(steps - 1);
auto& points = mesh.points;
auto& indices = mesh.indices;
points.reserve(2*steps);
double a = 2*PI/steps;
for(int i = 0; i < steps; ++i) {
for(size_t i = 0; i < steps; ++i) {
double phi = i*a;
double x = jp(X) + r*std::cos(phi);
double y = jp(Y) + r*std::sin(phi);
points.emplace_back(x, y, jp(Z));
}
for(int i = 0; i < steps; ++i) {
for(size_t i = 0; i < steps; ++i) {
double phi = i*a;
double ex = endp(X) + r*std::cos(phi);
double ey = endp(Y) + r*std::sin(phi);
@ -368,14 +371,13 @@ struct Pillar {
indices.reserve(2*steps);
int offs = int(steps);
for(int i = 0; i < steps - 1; ++i) {
for(int i = 0; i < steps_1 ; ++i) {
indices.emplace_back(i, i + offs, offs + i + 1);
indices.emplace_back(i, offs + i + 1, i + 1);
}
int last = int(steps) - 1;
indices.emplace_back(0, last, offs);
indices.emplace_back(last, offs + last, offs);
indices.emplace_back(0, steps_1, offs);
indices.emplace_back(steps_1, offs + steps_1, offs);
}
Pillar(const Junction& junc, const Vec3d& endp):
@ -390,19 +392,22 @@ struct Pillar {
void add_base(double height = 3, double radius = 2) {
if(height <= 0) return;
assert(steps >= 0);
auto last = int(steps - 1);
if(radius < r ) radius = r;
double a = 2*PI/steps;
double z = endpoint(2) + height;
for(int i = 0; i < steps; ++i) {
for(size_t i = 0; i < steps; ++i) {
double phi = i*a;
double x = endpoint(0) + r*std::cos(phi);
double y = endpoint(1) + r*std::sin(phi);
base.points.emplace_back(x, y, z);
}
for(int i = 0; i < steps; ++i) {
for(size_t i = 0; i < steps; ++i) {
double phi = i*a;
double x = endpoint(0) + radius*std::cos(phi);
double y = endpoint(1) + radius*std::sin(phi);
@ -417,14 +422,13 @@ struct Pillar {
auto hcenter = int(base.points.size() - 1);
auto lcenter = int(base.points.size() - 2);
auto offs = int(steps);
for(int i = 0; i < steps - 1; ++i) {
for(int i = 0; i < last; ++i) {
indices.emplace_back(i, i + offs, offs + i + 1);
indices.emplace_back(i, offs + i + 1, i + 1);
indices.emplace_back(i, i + 1, hcenter);
indices.emplace_back(lcenter, offs + i + 1, offs + i);
}
auto last = int(steps - 1);
indices.emplace_back(0, last, offs);
indices.emplace_back(last, offs + last, offs);
indices.emplace_back(hcenter, last, 0);
@ -462,8 +466,6 @@ struct Bridge {
Bridge(const Junction& j1, const Junction& j2, double r_mm = 0.8):
Bridge(j1.pos, j2.pos, r_mm, j1.steps) {}
Bridge(const Junction& j, const Pillar& cl) {}
};
// A bridge that spans from model surface to model surface with small connecting
@ -515,8 +517,13 @@ struct Pad {
zlevel(ground_level + sla::get_pad_elevation(pcfg))
{
ExPolygons basep;
cfg.throw_on_cancel();
// The 0.1f is the layer height with which the mesh is sampled and then
// the layers are unified into one vector of polygons.
base_plate(object_support_mesh, basep,
float(cfg.min_wall_height_mm)/*,layer_height*/);
float(cfg.min_wall_height_mm), 0.1f, pcfg.throw_on_cancel);
for(auto& bp : baseplate) basep.emplace_back(bp);
create_base_pool(basep, tmesh, cfg);
@ -532,12 +539,12 @@ EigenMesh3D to_eigenmesh(const Contour3D& cntr) {
auto& V = emesh.V;
auto& F = emesh.F;
V.resize(cntr.points.size(), 3);
F.resize(cntr.indices.size(), 3);
V.resize(Eigen::Index(cntr.points.size()), 3);
F.resize(Eigen::Index(cntr.indices.size()), 3);
for (int i = 0; i < V.rows(); ++i) {
V.row(i) = cntr.points[i];
F.row(i) = cntr.indices[i];
V.row(i) = cntr.points[size_t(i)];
F.row(i) = cntr.indices[size_t(i)];
}
return emesh;
@ -564,18 +571,23 @@ EigenMesh3D to_eigenmesh(const TriangleMesh& tmesh) {
V.resize(3*stl.stats.number_of_facets, 3);
F.resize(stl.stats.number_of_facets, 3);
for (unsigned int i=0; i<stl.stats.number_of_facets; ++i) {
for (unsigned int i = 0; i < stl.stats.number_of_facets; ++i) {
const stl_facet* facet = stl.facet_start+i;
V(3*i+0, 0) = facet->vertex[0](0); V(3*i+0, 1) =
facet->vertex[0](1); V(3*i+0, 2) = facet->vertex[0](2);
V(3*i+1, 0) = facet->vertex[1](0); V(3*i+1, 1) =
facet->vertex[1](1); V(3*i+1, 2) = facet->vertex[1](2);
V(3*i+2, 0) = facet->vertex[2](0); V(3*i+2, 1) =
facet->vertex[2](1); V(3*i+2, 2) = facet->vertex[2](2);
V(3*i+0, 0) = double(facet->vertex[0](0));
V(3*i+0, 1) = double(facet->vertex[0](1));
V(3*i+0, 2) = double(facet->vertex[0](2));
F(i, 0) = 3*i+0;
F(i, 1) = 3*i+1;
F(i, 2) = 3*i+2;
V(3*i+1, 0) = double(facet->vertex[1](0));
V(3*i+1, 1) = double(facet->vertex[1](1));
V(3*i+1, 2) = double(facet->vertex[1](2));
V(3*i+2, 0) = double(facet->vertex[2](0));
V(3*i+2, 1) = double(facet->vertex[2](1));
V(3*i+2, 2) = double(facet->vertex[2](2));
F(i, 0) = int(3*i+0);
F(i, 1) = int(3*i+1);
F(i, 2) = int(3*i+2);
}
return outmesh;
@ -622,12 +634,19 @@ class SLASupportTree::Impl {
std::vector<Junction> m_junctions;
std::vector<Bridge> m_bridges;
std::vector<CompactBridge> m_compact_bridges;
Controller m_ctl;
Pad m_pad;
mutable TriangleMesh meshcache; mutable bool meshcache_valid;
mutable TriangleMesh meshcache; mutable bool meshcache_valid = false;
mutable double model_height = 0; // the full height of the model
public:
double ground_level = 0;
Impl() = default;
inline Impl(const Controller& ctl): m_ctl(ctl) {}
const Controller& ctl() const { return m_ctl; }
template<class...Args> Head& add_head(Args&&... args) {
m_heads.emplace_back(std::forward<Args>(args)...);
m_heads.back().id = long(m_heads.size() - 1);
@ -637,7 +656,7 @@ public:
template<class...Args> Pillar& add_pillar(long headid, Args&&... args) {
assert(headid >= 0 && headid < m_heads.size());
Head& head = m_heads[headid];
Head& head = m_heads[size_t(headid)];
m_pillars.emplace_back(head, std::forward<Args>(args)...);
Pillar& pillar = m_pillars.back();
pillar.id = long(m_pillars.size() - 1);
@ -650,17 +669,17 @@ public:
const Head& pillar_head(long pillar_id) const {
assert(pillar_id >= 0 && pillar_id < m_pillars.size());
const Pillar& p = m_pillars[pillar_id];
const Pillar& p = m_pillars[size_t(pillar_id)];
assert(p.starts_from_head && p.start_junction_id >= 0 &&
p.start_junction_id < m_heads.size() );
return m_heads[p.start_junction_id];
return m_heads[size_t(p.start_junction_id)];
}
const Pillar& head_pillar(long headid) const {
assert(headid >= 0 && headid < m_heads.size());
const Head& h = m_heads[headid];
const Head& h = m_heads[size_t(headid)];
assert(h.pillar_id >= 0 && h.pillar_id < m_pillars.size());
return m_pillars[h.pillar_id];
return m_pillars[size_t(h.pillar_id)];
}
template<class...Args> const Junction& add_junction(Args&&... args) {
@ -710,27 +729,38 @@ public:
meshcache = TriangleMesh();
for(auto& head : heads()) {
if(m_ctl.stopcondition()) break;
auto&& m = mesh(head.mesh);
meshcache.merge(m);
}
for(auto& stick : pillars()) {
if(m_ctl.stopcondition()) break;
meshcache.merge(mesh(stick.mesh));
meshcache.merge(mesh(stick.base));
}
for(auto& j : junctions()) {
if(m_ctl.stopcondition()) break;
meshcache.merge(mesh(j.mesh));
}
for(auto& cb : compact_bridges()) {
if(m_ctl.stopcondition()) break;
meshcache.merge(mesh(cb.mesh));
}
for(auto& bs : bridges()) {
if(m_ctl.stopcondition()) break;
meshcache.merge(mesh(bs.mesh));
}
if(m_ctl.stopcondition()) {
// In case of failure we have to return an empty mesh
meshcache = TriangleMesh();
return meshcache;
}
// TODO: Is this necessary?
meshcache.repair();
@ -743,8 +773,11 @@ public:
// WITH THE PAD
double full_height() const {
if(merged_mesh().empty() && !pad().empty())
return pad().cfg.min_wall_height_mm;
double h = mesh_height();
if(!pad().empty()) h += pad().cfg.min_wall_height_mm / 2;
if(!pad().empty()) h += sla::get_pad_elevation(pad().cfg);
return h;
}
@ -856,8 +889,8 @@ ClusterEl pts_convex_hull(const ClusterEl& inpts,
}
// Find the leftmost (bottom) point
int l = 0;
for (int i = 1; i < n; i++) {
size_t l = 0;
for (size_t i = 1; i < n; i++) {
if(std::abs(points[i](X) - points[l](X)) < ERR) {
if(points[i](Y) < points[l](Y)) l = i;
}
@ -868,7 +901,6 @@ ClusterEl pts_convex_hull(const ClusterEl& inpts,
// fill the output with the spatially ordered set of points.
// find the direction
Vec2d dir = (points[l] - points[(l+1)%n]).normalized();
hull = inpts;
auto& lp = points[l];
std::sort(hull.begin(), hull.end(),
@ -886,7 +918,7 @@ ClusterEl pts_convex_hull(const ClusterEl& inpts,
// Start from leftmost point, keep moving counterclockwise
// until reach the start point again. This loop runs O(h)
// times where h is number of points in result or output.
int p = l;
size_t p = l;
do
{
// Add current point to result
@ -897,8 +929,8 @@ ClusterEl pts_convex_hull(const ClusterEl& inpts,
// is to keep track of last visited most counterclock-
// wise point in q. If any point 'i' is more counterclock-
// wise than q, then update q.
int q = (p+1)%n;
for (int i = 0; i < n; i++)
size_t q = (p + 1) % n;
for (size_t i = 0; i < n; i++)
{
// If i is more counterclockwise than current q, then
// update q
@ -980,7 +1012,10 @@ bool SLASupportTree::generate(const PointSet &points,
// std::cout << "p " << pn << " " << points.row(pn) << std::endl;
// }
auto filterfn = [] (
auto& tifcl = ctl.cancelfn;
auto filterfn = [tifcl] (
const SupportConfig& cfg,
const PointSet& points,
const EigenMesh3D& mesh,
@ -990,26 +1025,29 @@ bool SLASupportTree::generate(const PointSet &points,
PointSet& headless_pos,
PointSet& headless_norm)
{
/* ******************************************************** */
/* Filtering step */
/* ******************************************************** */
// Get the points that are too close to each other and keep only the
// first one
auto aliases = cluster(points,
[cfg](const SpatElement& p,
const SpatElement& se){
auto aliases =
cluster(points,
[tifcl](const SpatElement& p, const SpatElement& se)
{
tifcl();
return distance(p.first, se.first) < D_SP;
}, 2);
filt_pts.resize(aliases.size(), 3);
filt_pts.resize(Eigen::Index(aliases.size()), 3);
int count = 0;
for(auto& a : aliases) {
// Here we keep only the front point of the cluster. TODO: centroid
// Here we keep only the front point of the cluster.
filt_pts.row(count++) = points.row(a.front());
}
tifcl();
// calculate the normals to the triangles belonging to filtered points
auto nmls = sla::normals(filt_pts, mesh);
@ -1025,6 +1063,7 @@ bool SLASupportTree::generate(const PointSet &points,
int pcount = 0, hlcount = 0;
for(int i = 0; i < count; i++) {
tifcl();
auto n = nmls.row(i);
// for all normals we generate the spherical coordinates and
@ -1084,7 +1123,7 @@ bool SLASupportTree::generate(const PointSet &points,
};
// Function to write the pinheads into the result
auto pinheadfn = [] (
auto pinheadfn = [tifcl] (
const SupportConfig& cfg,
PointSet& head_pos,
PointSet& nmls,
@ -1097,6 +1136,7 @@ bool SLASupportTree::generate(const PointSet &points,
/* ******************************************************** */
for (int i = 0; i < head_pos.rows(); ++i) {
tifcl();
result.add_head(
cfg.head_back_radius_mm,
cfg.head_front_radius_mm,
@ -1110,7 +1150,7 @@ bool SLASupportTree::generate(const PointSet &points,
// &filtered_points, &head_positions, &result, &mesh,
// &gndidx, &gndheight, &nogndidx, cfg
auto classifyfn = [] (
auto classifyfn = [tifcl] (
const SupportConfig& cfg,
const EigenMesh3D& mesh,
PointSet& head_pos,
@ -1126,11 +1166,12 @@ bool SLASupportTree::generate(const PointSet &points,
/* ******************************************************** */
// We should first get the heads that reach the ground directly
gndheight.reserve(head_pos.rows());
gndidx.reserve(head_pos.rows());
nogndidx.reserve(head_pos.rows());
gndheight.reserve(size_t(head_pos.rows()));
gndidx.reserve(size_t(head_pos.rows()));
nogndidx.reserve(size_t(head_pos.rows()));
for(unsigned i = 0; i < head_pos.rows(); i++) {
tifcl();
auto& head = result.heads()[i];
Vec3d dir(0, 0, -1);
@ -1147,18 +1188,22 @@ bool SLASupportTree::generate(const PointSet &points,
PointSet gnd(gndidx.size(), 3);
for(size_t i = 0; i < gndidx.size(); i++)
gnd.row(i) = head_pos.row(gndidx[i]);
gnd.row(long(i)) = head_pos.row(gndidx[i]);
// We want to search for clusters of points that are far enough from
// each other in the XY plane to not cross their pillar bases
// These clusters of support points will join in one pillar, possibly in
// their centroid support point.
auto d_base = 2*cfg.base_radius_mm;
ground_clusters = cluster(gnd,
[d_base, &cfg](const SpatElement& p, const SpatElement& s){
return distance(Vec2d(p.first(X), p.first(Y)),
Vec2d(s.first(X), s.first(Y))) < d_base;
}, 3); // max 3 heads to connect to one centroid
ground_clusters =
cluster(
gnd,
[d_base, tifcl](const SpatElement& p, const SpatElement& s)
{
tifcl();
return distance(Vec2d(p.first(X), p.first(Y)),
Vec2d(s.first(X), s.first(Y))) < d_base;
}, 3); // max 3 heads to connect to one centroid
};
// Helper function for interconnecting two pillars with zig-zag bridges
@ -1171,9 +1216,6 @@ bool SLASupportTree::generate(const PointSet &points,
const Head& phead = result.pillar_head(pillar.id);
const Head& nextphead = result.pillar_head(nextpillar.id);
// double d = 2*pillar.r;
// const Vec3d& pp = pillar.endpoint.cwiseProduct(Vec3d{1, 1, 0});
Vec3d sj = phead.junction_point();
sj(Z) = std::min(sj(Z), nextphead.junction_point()(Z));
Vec3d ej = nextpillar.endpoint;
@ -1218,7 +1260,7 @@ bool SLASupportTree::generate(const PointSet &points,
}
};
auto routing_ground_fn = [gnd_head_pt, interconnect](
auto routing_ground_fn = [gnd_head_pt, interconnect, tifcl](
const SupportConfig& cfg,
const ClusteredPoints& gnd_clusters,
const IndexSet& gndidx,
@ -1234,22 +1276,27 @@ bool SLASupportTree::generate(const PointSet &points,
cl_centroids.reserve(gnd_clusters.size());
SpatIndex pheadindex; // spatial index for the junctions
for(auto cl : gnd_clusters) {
for(auto& cl : gnd_clusters) { tifcl();
// place all the centroid head positions into the index. We will
// query for alternative pillar positions. If a sidehead cannot
// connect to the cluster centroid, we have to search for another
// head with a full pillar. Also when there are two elements in the
// cluster, the centroid is arbitrary and the sidehead is allowed to
// connect to a nearby pillar to increase structural stability.
if(cl.empty()) continue;
// get the current cluster centroid
unsigned cid = cluster_centroid(cl, gnd_head_pt,
[](const Vec3d& p1, const Vec3d& p2)
long lcid = cluster_centroid(cl, gnd_head_pt,
[tifcl](const Vec3d& p1, const Vec3d& p2)
{
tifcl();
return distance(Vec2d(p1(X), p1(Y)), Vec2d(p2(X), p2(Y)));
});
cl_centroids.push_back(cid);
assert(lcid >= 0);
auto cid = unsigned(lcid);
cl_centroids.push_back(unsigned(cid));
unsigned hid = gndidx[cl[cid]]; // Head index
Head& h = result.head(hid);
@ -1262,12 +1309,13 @@ bool SLASupportTree::generate(const PointSet &points,
// now we will go through the clusters ones again and connect the
// sidepoints with the cluster centroid (which is a ground pillar)
// or a nearby pillar if the centroid is unreachable.
long ci = 0;
for(auto cl : gnd_clusters) {
size_t ci = 0;
for(auto cl : gnd_clusters) { tifcl();
auto cidx = cl_centroids[ci];
cl_centroids[ci++] = cl[cidx];
long index_to_heads = gndidx[cl[cidx]];
size_t index_to_heads = gndidx[cl[cidx]];
auto& head = result.head(index_to_heads);
Vec3d startpoint = head.junction_point();
@ -1275,7 +1323,7 @@ bool SLASupportTree::generate(const PointSet &points,
// Create the central pillar of the cluster with its base on the
// ground
result.add_pillar(index_to_heads, endpoint, pradius)
result.add_pillar(long(index_to_heads), endpoint, pradius)
.add_base(cfg.base_height_mm, cfg.base_radius_mm);
// Process side point in current cluster
@ -1326,12 +1374,11 @@ bool SLASupportTree::generate(const PointSet &points,
return nearest_id;
};
for(auto c : cl) {
for(auto c : cl) { tifcl();
auto& sidehead = result.head(gndidx[c]);
sidehead.transform();
Vec3d jsh = sidehead.junction_point();
// Vec3d jp2d = {jsh(X), jsh(Y), gndlvl};
SpatIndex spindex = pheadindex;
long nearest_id = search_nearest(spindex, jsh);
@ -1344,11 +1391,11 @@ bool SLASupportTree::generate(const PointSet &points,
add_base(cfg.base_height_mm, cfg.base_radius_mm);
// connects to ground, eligible for bridging
cl_centroids.emplace_back(sidehead.id);
cl_centroids.emplace_back(c);
} else {
// Creating the bridge to the nearest pillar
const Head& nearhead = result.heads()[nearest_id];
const Head& nearhead = result.heads()[size_t(nearest_id)];
Vec3d jp = jsh;
Vec3d jh = nearhead.junction_point();
@ -1393,6 +1440,7 @@ bool SLASupportTree::generate(const PointSet &points,
ClusterEl ring;
while(!rem.empty()) { // loop until all the points belong to some ring
tifcl();
std::sort(rem.begin(), rem.end());
auto newring = pts_convex_hull(rem,
@ -1406,7 +1454,8 @@ bool SLASupportTree::generate(const PointSet &points,
SpatIndex innerring;
for(unsigned i : newring) {
const Pillar& pill = result.head_pillar(gndidx[i]);
innerring.insert(pill.endpoint, pill.id);
assert(pill.id >= 0);
innerring.insert(pill.endpoint, unsigned(pill.id));
}
// For all pillars in the outer ring find the closest in the
@ -1452,14 +1501,14 @@ bool SLASupportTree::generate(const PointSet &points,
}
};
auto routing_nongnd_fn = [](
auto routing_nongnd_fn = [tifcl](
const SupportConfig& cfg,
const std::vector<double>& gndheight,
const IndexSet& nogndidx,
Result& result)
{
// TODO: connect these to the ground pillars if possible
for(auto idx : nogndidx) {
for(auto idx : nogndidx) { tifcl();
auto& head = result.head(idx);
head.transform();
@ -1484,7 +1533,7 @@ bool SLASupportTree::generate(const PointSet &points,
}
};
auto process_headless = [](
auto process_headless = [tifcl](
const SupportConfig& cfg,
const PointSet& headless_pts,
const PointSet& headless_norm,
@ -1499,7 +1548,7 @@ bool SLASupportTree::generate(const PointSet &points,
// We will sink the pins into the model surface for a distance of 1/3 of
// HWIDTH_MM
for(int i = 0; i < headless_pts.rows(); i++) {
for(int i = 0; i < headless_pts.rows(); i++) { tifcl();
Vec3d sp = headless_pts.row(i);
Vec3d n = headless_norm.row(i);
@ -1608,6 +1657,7 @@ bool SLASupportTree::generate(const PointSet &points,
case HALT: pc = pc_prev; break;
case DONE:
case ABORT: break;
default: ;
}
ctl.statuscb(stepstate[pc], stepstr[pc]);
};
@ -1656,22 +1706,19 @@ SlicedSupports SLASupportTree::slice(float layerh, float init_layerh) const
fullmesh.merge(get_pad());
TriangleMeshSlicer slicer(&fullmesh);
SlicedSupports ret;
slicer.slice(heights, &ret, m_ctl.cancelfn);
slicer.slice(heights, &ret, get().ctl().cancelfn);
return ret;
}
const TriangleMesh &SLASupportTree::add_pad(const SliceLayer& baseplate,
double min_wall_thickness_mm,
double min_wall_height_mm,
double max_merge_distance_mm,
double edge_radius_mm) const
const PoolConfig& pcfg) const
{
PoolConfig pcfg;
pcfg.min_wall_thickness_mm = min_wall_thickness_mm;
pcfg.min_wall_height_mm = min_wall_height_mm;
pcfg.max_merge_distance_mm = max_merge_distance_mm;
pcfg.edge_radius_mm = edge_radius_mm;
// PoolConfig pcfg;
// pcfg.min_wall_thickness_mm = min_wall_thickness_mm;
// pcfg.min_wall_height_mm = min_wall_height_mm;
// pcfg.max_merge_distance_mm = max_merge_distance_mm;
// pcfg.edge_radius_mm = edge_radius_mm;
return m_impl->create_pad(merged_mesh(), baseplate, pcfg).tmesh;
}
@ -1684,14 +1731,14 @@ SLASupportTree::SLASupportTree(const PointSet &points,
const EigenMesh3D& emesh,
const SupportConfig &cfg,
const Controller &ctl):
m_impl(new Impl()), m_ctl(ctl)
m_impl(new Impl(ctl))
{
m_impl->ground_level = emesh.ground_level - cfg.object_elevation_mm;
generate(points, emesh, cfg, ctl);
}
SLASupportTree::SLASupportTree(const SLASupportTree &c):
m_impl(new Impl(*c.m_impl)), m_ctl(c.m_ctl) {}
m_impl(new Impl(*c.m_impl)) {}
SLASupportTree &SLASupportTree::operator=(const SLASupportTree &c)
{
@ -1701,5 +1748,8 @@ SLASupportTree &SLASupportTree::operator=(const SLASupportTree &c)
SLASupportTree::~SLASupportTree() {}
SLASupportsStoppedException::SLASupportsStoppedException():
std::runtime_error("") {}
}
}

View file

@ -69,6 +69,8 @@ struct SupportConfig {
double object_elevation_mm = 10;
};
struct PoolConfig;
/// A Control structure for the support calculation. Consists of the status
/// indicator callback and the stop condition predicate.
struct Controller {
@ -112,14 +114,13 @@ PointSet to_point_set(const std::vector<Vec3d>&);
class SLASupportsStoppedException: public std::runtime_error {
public:
using std::runtime_error::runtime_error;
SLASupportsStoppedException(): std::runtime_error("") {}
SLASupportsStoppedException();
};
/// The class containing mesh data for the generated supports.
class SLASupportTree {
class Impl;
std::unique_ptr<Impl> m_impl;
Controller m_ctl;
Impl& get() { return *m_impl; }
const Impl& get() const { return *m_impl; }
@ -158,10 +159,7 @@ public:
/// Adding the "pad" (base pool) under the supports
const TriangleMesh& add_pad(const SliceLayer& baseplate,
double min_wall_thickness_mm,
double min_wall_height_mm,
double max_merge_distance_mm,
double edge_radius_mm) const;
const PoolConfig& pcfg) const;
/// Get the pad geometry
const TriangleMesh& get_pad() const;

View file

@ -89,8 +89,6 @@ PointSet normals(const PointSet& points, const EigenMesh3D& mesh) {
#ifdef IGL_COMPATIBLE
Eigen::VectorXd dists;
Eigen::VectorXi I;
// Eigen::Matrix<double, Eigen::Dynamic, 1, Eigen::DontAlign> dists;
// Eigen::Matrix<int, Eigen::Dynamic, 1, Eigen::DontAlign> I;
PointSet C;
igl::point_mesh_squared_distance( points, mesh.V, mesh.F, dists, I, C);
@ -122,7 +120,7 @@ double ray_mesh_intersect(const Vec3d& s,
igl::Hit hit;
hit.t = std::numeric_limits<float>::infinity();
igl::ray_mesh_intersect(s, dir, m.V, m.F, hit);
return hit.t;
return double(hit.t);
}
// Clustering a set of points by the given criteria
@ -208,7 +206,7 @@ Segments model_boundary(const EigenMesh3D& emesh, double offs)
{
Segments ret;
Polygons pp;
pp.reserve(emesh.F.rows());
pp.reserve(size_t(emesh.F.rows()));
for (int i = 0; i < emesh.F.rows(); i++) {
auto trindex = emesh.F.row(i);

View file

@ -19,7 +19,6 @@
namespace Slic3r {
using SlicedModel = SlicedSupports;
using SupportTreePtr = std::unique_ptr<sla::SLASupportTree>;
class SLAPrintObject::SupportData {
@ -413,6 +412,12 @@ sla::SupportConfig make_support_cfg(const SLAPrintObjectConfig& c) {
return scfg;
}
void swapXY(ExPolygon& expoly) {
for(auto& p : expoly.contour.points) std::swap(p(X), p(Y));
for(auto& h : expoly.holes) for(auto& p : h.points) std::swap(p(X), p(Y));
}
}
void SLAPrint::process()
@ -445,7 +450,7 @@ void SLAPrint::process()
// Slicing the model object. This method is oversimplified and needs to
// be compared with the fff slicing algorithm for verification
auto slice_model = [this, ilh, ilhd](SLAPrintObject& po) {
auto slice_model = [this, ilh](SLAPrintObject& po) {
double lh = po.m_config.layer_height.getFloat();
TriangleMesh mesh = po.transformed_mesh();
@ -469,7 +474,7 @@ void SLAPrint::process()
for(float h = minZ + ilh; h < maxZ; h += flh)
if(h >= gnd) heights.emplace_back(h);
auto& layers = po.m_model_slices;
auto& layers = po.m_model_slices; layers.clear();
slicer.slice(heights, &layers, [this](){ throw_if_canceled(); });
};
@ -504,8 +509,6 @@ void SLAPrint::process()
return;
}
auto& emesh = po.m_supportdata->emesh;
auto& pts = po.m_supportdata->support_points; // nowhere filled yet
try {
sla::SupportConfig scfg = make_support_cfg(po.m_config);
sla::Controller ctl;
@ -528,8 +531,14 @@ void SLAPrint::process()
ctl.cancelfn = [this]() { throw_if_canceled(); };
po.m_supportdata->support_tree_ptr.reset(
new SLASupportTree(pts, emesh, scfg, ctl));
new SLASupportTree(po.m_supportdata->support_points,
po.m_supportdata->emesh, scfg, ctl));
// Create the unified mesh
auto rc = SlicingStatus::RELOAD_SCENE;
set_status(-1, L("Visualizing supports"));
po.m_supportdata->support_tree_ptr->merged_mesh();
set_status(-1, L("Visualizing supports"), rc);
} catch(sla::SLASupportsStoppedException&) {
// no need to rethrow
// throw_if_canceled();
@ -560,18 +569,17 @@ void SLAPrint::process()
auto&& trmesh = po.transformed_mesh();
// This call can get pretty time consuming
if(elevation < pad_h) sla::base_plate(trmesh, bp,
float(pad_h), float(lh));
auto thrfn = [this](){ throw_if_canceled(); };
po.m_supportdata->support_tree_ptr->add_pad(bp, wt, h, md, er);
if(elevation < pad_h)
sla::base_plate(trmesh, bp, float(pad_h), float(lh),
thrfn);
pcfg.throw_on_cancel = thrfn;
po.m_supportdata->support_tree_ptr->add_pad(bp, pcfg);
}
// if the base pool (which means also the support tree) is
// done, do a refresh when indicating progress. Now the
// geometries for the supports and the optional base pad are
// ready. We can grant access for the control thread to read
// the geometries, but first we have to update the caches:
po.support_mesh(); /*po->pad_mesh();*/
po.throw_if_canceled();
auto rc = SlicingStatus::RELOAD_SCENE;
set_status(-1, L("Visualizing supports"), rc);
};
@ -589,12 +597,11 @@ void SLAPrint::process()
// We have the layer polygon collection but we need to unite them into
// an index where the key is the height level in discrete levels (clipper)
auto index_slices = [this, ilh, ilhd](SLAPrintObject& po) {
auto index_slices = [ilhd](SLAPrintObject& po) {
po.m_slice_index.clear();
auto sih = LevelID(scale_(ilh));
auto sih = LevelID(scale_(ilhd));
// For all print objects, go through its initial layers and place them
// into the layers hash
// Establish the slice grid boundaries
auto bb = po.transformed_mesh().bounding_box();
double modelgnd = bb.min(Z);
double elevation = po.get_elevation();
@ -610,49 +617,46 @@ void SLAPrint::process()
// It is important that the next levels match the levels in
// model_slice method. Only difference is that here it works with
// scaled coordinates
auto& levelids = po.m_level_ids; levelids.clear();
if(sminZ >= smodelgnd) levelids.emplace_back(sminZ);
po.m_level_ids.clear();
if(sminZ >= smodelgnd) po.m_level_ids.emplace_back(sminZ);
for(LevelID h = sminZ + sih; h < smaxZ; h += slh)
if(h >= smodelgnd) levelids.emplace_back(h);
if(h >= smodelgnd) po.m_level_ids.emplace_back(h);
SlicedModel & oslices = po.m_model_slices;
std::vector<ExPolygons>& oslices = po.m_model_slices;
// If everything went well this code should not run at all, but
// let's be robust...
// assert(levelids.size() == oslices.size());
if(levelids.size() < oslices.size()) { // extend the levels until...
if(po.m_level_ids.size() < oslices.size()) { // extend the levels until...
BOOST_LOG_TRIVIAL(warning)
<< "Height level mismatch at rasterization!\n";
LevelID lastlvl = levelids.back();
while(levelids.size() < oslices.size()) {
LevelID lastlvl = po.m_level_ids.back();
while(po.m_level_ids.size() < oslices.size()) {
lastlvl += slh;
levelids.emplace_back(lastlvl);
po.m_level_ids.emplace_back(lastlvl);
}
}
// shortcut for empty index into the slice vectors
static const auto EMPTY_SLICE = SLAPrintObject::SliceRecord::NONE;
for(int i = 0; i < oslices.size(); ++i) {
LevelID h = levelids[i];
for(size_t i = 0; i < oslices.size(); ++i) {
LevelID h = po.m_level_ids[i];
float fh = float(double(h) * SCALING_FACTOR);
// now for the public slice index:
SLAPrintObject::SliceRecord& sr = po.m_slice_index[fh];
// There should be only one slice layer for each print object
assert(sr.model_slices_idx == EMPTY_SLICE);
assert(sr.model_slices_idx == SLAPrintObject::SliceRecord::NONE);
sr.model_slices_idx = i;
}
if(po.m_supportdata) { // deal with the support slices if present
auto& sslices = po.m_supportdata->support_slices;
std::vector<ExPolygons>& sslices = po.m_supportdata->support_slices;
po.m_supportdata->level_ids.clear();
po.m_supportdata->level_ids.reserve(sslices.size());
for(int i = 0; i < sslices.size(); ++i) {
for(int i = 0; i < int(sslices.size()); ++i) {
int a = i == 0 ? 0 : 1;
int b = i == 0 ? 0 : i - 1;
LevelID h = sminZ + a * sih + b * slh;
@ -661,16 +665,14 @@ void SLAPrint::process()
float fh = float(double(h) * SCALING_FACTOR);
SLAPrintObject::SliceRecord& sr = po.m_slice_index[fh];
assert(sr.support_slices_idx == EMPTY_SLICE);
sr.support_slices_idx = i;
assert(sr.support_slices_idx == SLAPrintObject::SliceRecord::NONE);
sr.support_slices_idx = SLAPrintObject::SliceRecord::Idx(i);
}
}
};
auto& levels = m_printer_input;
// Rasterizing the model objects, and their supports
auto rasterize = [this, ilh, ilhd, max_objstatus, &levels]() {
auto rasterize = [this, max_objstatus]() {
if(canceled()) return;
// clear the rasterizer input
@ -678,32 +680,39 @@ void SLAPrint::process()
for(SLAPrintObject * o : m_objects) {
auto& po = *o;
SlicedModel & oslices = po.m_model_slices;
std::vector<ExPolygons>& oslices = po.m_model_slices;
// We need to adjust the min Z level of the slices to be zero
LevelID smfirst = po.m_supportdata? po.m_supportdata->level_ids.front() : 0;
LevelID mfirst = po.m_level_ids.front();
LevelID smfirst =
po.m_supportdata && !po.m_supportdata->level_ids.empty() ?
po.m_supportdata->level_ids.front() : 0;
LevelID mfirst = po.m_level_ids.empty()? 0 : po.m_level_ids.front();
LevelID gndlvl = -(std::min(smfirst, mfirst));
// now merge this object's support and object slices with the rest
// of the print object slices
for(int i = 0; i < oslices.size(); ++i) {
auto& lyrs = levels[gndlvl + po.m_level_ids[i]];
for(size_t i = 0; i < oslices.size(); ++i) {
auto& lyrs = m_printer_input[gndlvl + po.m_level_ids[i]];
lyrs.emplace_back(oslices[i], po.m_instances);
}
if(!po.m_supportdata) continue;
auto& sslices = po.m_supportdata->support_slices;
for(int i = 0; i < sslices.size(); ++i) {
auto& lyrs = levels[gndlvl + po.m_supportdata->level_ids[i]];
std::vector<ExPolygons>& sslices = po.m_supportdata->support_slices;
for(size_t i = 0; i < sslices.size(); ++i) {
LayerRefs& lyrs =
m_printer_input[gndlvl + po.m_supportdata->level_ids[i]];
lyrs.emplace_back(sslices[i], po.m_instances);
}
}
// collect all the keys
std::vector<long long> keys; keys.reserve(levels.size());
for(auto& e : levels) keys.emplace_back(e.first);
std::vector<long long> keys; keys.reserve(m_printer_input.size());
for(auto& e : m_printer_input) keys.emplace_back(e.first);
// If the raster has vertical orientation, we will flip the coordinates
bool flpXY = m_printer_config.display_orientation.getInt() ==
SLADisplayOrientation::sladoPortrait;
{ // create a raster printer for the current print parameters
// I don't know any better
@ -713,18 +722,22 @@ void SLAPrint::process()
double w = printcfg.display_width.getFloat();
double h = printcfg.display_height.getFloat();
unsigned pw = printcfg.display_pixels_x.getInt();
unsigned ph = printcfg.display_pixels_y.getInt();
auto pw = unsigned(printcfg.display_pixels_x.getInt());
auto ph = unsigned(printcfg.display_pixels_y.getInt());
double lh = ocfg.layer_height.getFloat();
double exp_t = matcfg.exposure_time.getFloat();
double iexp_t = matcfg.initial_exposure_time.getFloat();
m_printer.reset(new SLAPrinter(w, h, pw, ph, lh, exp_t, iexp_t));
if(flpXY) { std::swap(w, h); std::swap(pw, ph); }
m_printer.reset(new SLAPrinter(w, h, pw, ph, lh, exp_t, iexp_t,
flpXY? SLAPrinter::RO_PORTRAIT :
SLAPrinter::RO_LANDSCAPE));
}
// Allocate space for all the layers
SLAPrinter& printer = *m_printer;
auto lvlcnt = unsigned(levels.size());
auto lvlcnt = unsigned(m_printer_input.size());
printer.layers(lvlcnt);
unsigned slot = PRINT_STEP_LEVELS[slapsRasterize];
@ -734,12 +747,12 @@ void SLAPrint::process()
// procedure to process one height level. This will run in parallel
auto lvlfn =
[this, &slck, &keys, &levels, &printer, slot, sd, ist, &pst]
[this, &slck, &keys, &printer, slot, sd, ist, &pst, flpXY]
(unsigned level_id)
{
if(canceled()) return;
LayerRefs& lrange = levels[keys[level_id]];
LayerRefs& lrange = m_printer_input[keys[level_id]];
// Switch to the appropriate layer in the printer
printer.begin_layer(level_id);
@ -754,8 +767,9 @@ void SLAPrint::process()
for(ExPolygon slice : sl) {
// The order is important here:
// apply rotation before translation...
slice.rotate(cp.rotation);
slice.rotate(double(cp.rotation));
slice.translate(cp.shift(X), cp.shift(Y));
if(flpXY) swapXY(slice);
printer.draw_polygon(slice, level_id);
}
}
@ -765,7 +779,7 @@ void SLAPrint::process()
printer.finish_layer(level_id);
// Status indication
auto st = ist + unsigned(sd*level_id*slot/levels.size());
auto st = ist + unsigned(sd*level_id*slot/m_printer_input.size());
{ std::lock_guard<SpinMutex> lck(slck);
if( st > pst) {
set_status(int(st), PRINT_STEP_LABELS[slapsRasterize]);
@ -886,6 +900,7 @@ bool SLAPrint::invalidate_state_by_config_options(const std::vector<t_config_opt
"display_height",
"display_pixels_x",
"display_pixels_y",
"display_orientation",
"printer_correction"
};
@ -926,6 +941,18 @@ bool SLAPrint::invalidate_state_by_config_options(const std::vector<t_config_opt
return invalidated;
}
// Returns true if an object step is done on all objects and there's at least one object.
bool SLAPrint::is_step_done(SLAPrintObjectStep step) const
{
if (m_objects.empty())
return false;
tbb::mutex::scoped_lock lock(this->state_mutex());
for (const SLAPrintObject *object : m_objects)
if (! object->m_state.is_done_unguarded(step))
return false;
return true;
}
SLAPrintObject::SLAPrintObject(SLAPrint *print, ModelObject *model_object):
Inherited(print, model_object),
m_stepmask(slaposCount, true),
@ -1098,7 +1125,9 @@ TriangleMesh SLAPrintObject::get_mesh(SLAPrintObjectStep step) const
const TriangleMesh& SLAPrintObject::support_mesh() const
{
if(m_config.supports_enable.getBool() && m_supportdata &&
m_supportdata->support_tree_ptr) return m_supportdata->support_tree_ptr->merged_mesh();
m_supportdata->support_tree_ptr) {
return m_supportdata->support_tree_ptr->merged_mesh();
}
return EMPTY_MESH;
}

View file

@ -177,14 +177,18 @@ private: // Prevents erroneous use by other classes.
public:
SLAPrint(): m_stepmask(slapsCount, true) {}
virtual ~SLAPrint() { this->clear(); }
virtual ~SLAPrint() override { this->clear(); }
PrinterTechnology technology() const noexcept { return ptSLA; }
PrinterTechnology technology() const noexcept override { return ptSLA; }
void clear() override;
bool empty() const override { return m_objects.empty(); }
ApplyStatus apply(const Model &model, const DynamicPrintConfig &config) override;
void process() override;
// Returns true if an object step is done on all objects and there's at least one object.
bool is_step_done(SLAPrintObjectStep step) const;
// Returns true if the last step was finished with success.
bool finished() const override { return this->is_step_done(slaposIndexSlices); }
template<class Fmt> void export_raster(const std::string& fname) {
if(m_printer) m_printer->save<Fmt>(fname);

View file

@ -458,6 +458,8 @@ Polygons collect_slices_outer(const Layer &layer)
class SupportGridPattern
{
public:
// Achtung! The support_polygons need to be trimmed by trimming_polygons, otherwise
// the selection by island_samples (see the island_samples() method) will not work!
SupportGridPattern(
// Support islands, to be stretched into a grid. Already trimmed with min(lower_layer_offset, m_gap_xy)
const Polygons &support_polygons,
@ -485,6 +487,18 @@ public:
bbox.align_to_grid(grid_resolution);
m_grid.set_bbox(bbox);
m_grid.create(*m_support_polygons, grid_resolution);
#if 0
if (m_grid.has_intersecting_edges()) {
// EdgeGrid fails to produce valid signed distance function for self-intersecting polygons.
m_support_polygons_rotated = simplify_polygons(*m_support_polygons);
m_support_polygons = &m_support_polygons_rotated;
m_grid.set_bbox(bbox);
m_grid.create(*m_support_polygons, grid_resolution);
// assert(! m_grid.has_intersecting_edges());
printf("SupportGridPattern: fixing polygons with intersection %s\n",
m_grid.has_intersecting_edges() ? "FAILED" : "SUCCEEDED");
}
#endif
m_grid.calculate_sdf();
// Sample a single point per input support polygon, keep it as a reference to maintain corresponding
// polygons if ever these polygons get split into parts by the trimming polygons.
@ -499,9 +513,12 @@ public:
{
// Generate islands, so each island may be tested for overlap with m_island_samples.
assert(std::abs(2 * offset_in_grid) < m_grid.resolution());
ExPolygons islands = diff_ex(
m_grid.contours_simplified(offset_in_grid, fill_holes),
*m_trimming_polygons, false);
#ifdef SLIC3R_DEBUG
Polygons support_polygons_simplified = m_grid.contours_simplified(offset_in_grid, fill_holes);
ExPolygons islands = diff_ex(support_polygons_simplified, *m_trimming_polygons, false);
#else
ExPolygons islands = diff_ex(m_grid.contours_simplified(offset_in_grid, fill_holes), *m_trimming_polygons, false);
#endif
// Extract polygons, which contain some of the m_island_samples.
Polygons out;
@ -551,7 +568,10 @@ public:
bbox.merge(get_extents(islands));
if (!out.empty())
bbox.merge(get_extents(out));
if (!support_polygons_simplified.empty())
bbox.merge(get_extents(support_polygons_simplified));
SVG svg(debug_out_path("extract_support_from_grid_trimmed-%d.svg", iRun).c_str(), bbox);
svg.draw(union_ex(support_polygons_simplified), "gray", 0.25f);
svg.draw(islands, "red", 0.5f);
svg.draw(union_ex(out), "green", 0.5f);
svg.draw(union_ex(*m_support_polygons), "blue", 0.5f);
@ -568,7 +588,121 @@ public:
return out;
}
#ifdef SLIC3R_DEBUG
void serialize(const std::string &path)
{
FILE *file = ::fopen(path.c_str(), "wb");
::fwrite(&m_support_spacing, 8, 1, file);
::fwrite(&m_support_angle, 8, 1, file);
uint32_t n_polygons = m_support_polygons->size();
::fwrite(&n_polygons, 4, 1, file);
for (uint32_t i = 0; i < n_polygons; ++ i) {
const Polygon &poly = (*m_support_polygons)[i];
uint32_t n_points = poly.size();
::fwrite(&n_points, 4, 1, file);
for (uint32_t j = 0; j < n_points; ++ j) {
const Point &pt = poly.points[j];
::fwrite(&pt.x, sizeof(coord_t), 1, file);
::fwrite(&pt.y, sizeof(coord_t), 1, file);
}
}
n_polygons = m_trimming_polygons->size();
::fwrite(&n_polygons, 4, 1, file);
for (uint32_t i = 0; i < n_polygons; ++ i) {
const Polygon &poly = (*m_trimming_polygons)[i];
uint32_t n_points = poly.size();
::fwrite(&n_points, 4, 1, file);
for (uint32_t j = 0; j < n_points; ++ j) {
const Point &pt = poly.points[j];
::fwrite(&pt.x, sizeof(coord_t), 1, file);
::fwrite(&pt.y, sizeof(coord_t), 1, file);
}
}
::fclose(file);
}
static SupportGridPattern deserialize(const std::string &path, int which = -1)
{
SupportGridPattern out;
out.deserialize_(path, which);
return out;
}
// Deserialization constructor
bool deserialize_(const std::string &path, int which = -1)
{
FILE *file = ::fopen(path.c_str(), "rb");
if (file == nullptr)
return false;
m_support_polygons = &m_support_polygons_deserialized;
m_trimming_polygons = &m_trimming_polygons_deserialized;
::fread(&m_support_spacing, 8, 1, file);
::fread(&m_support_angle, 8, 1, file);
//FIXME
//m_support_spacing *= 0.01 / 2;
uint32_t n_polygons;
::fread(&n_polygons, 4, 1, file);
m_support_polygons_deserialized.reserve(n_polygons);
int32_t scale = 1;
for (uint32_t i = 0; i < n_polygons; ++ i) {
Polygon poly;
uint32_t n_points;
::fread(&n_points, 4, 1, file);
poly.points.reserve(n_points);
for (uint32_t j = 0; j < n_points; ++ j) {
coord_t x, y;
::fread(&x, sizeof(coord_t), 1, file);
::fread(&y, sizeof(coord_t), 1, file);
poly.points.emplace_back(Point(x * scale, y * scale));
}
if (which == -1 || which == i)
m_support_polygons_deserialized.emplace_back(std::move(poly));
printf("Polygon %d, area: %lf\n", i, area(poly.points));
}
::fread(&n_polygons, 4, 1, file);
m_trimming_polygons_deserialized.reserve(n_polygons);
for (uint32_t i = 0; i < n_polygons; ++ i) {
Polygon poly;
uint32_t n_points;
::fread(&n_points, 4, 1, file);
poly.points.reserve(n_points);
for (uint32_t j = 0; j < n_points; ++ j) {
coord_t x, y;
::fread(&x, sizeof(coord_t), 1, file);
::fread(&y, sizeof(coord_t), 1, file);
poly.points.emplace_back(Point(x * scale, y * scale));
}
m_trimming_polygons_deserialized.emplace_back(std::move(poly));
}
::fclose(file);
m_support_polygons_deserialized = simplify_polygons(m_support_polygons_deserialized, false);
//m_support_polygons_deserialized = to_polygons(union_ex(m_support_polygons_deserialized, false));
// Create an EdgeGrid, initialize it with projection, initialize signed distance field.
coord_t grid_resolution = coord_t(scale_(m_support_spacing));
BoundingBox bbox = get_extents(*m_support_polygons);
bbox.offset(20);
bbox.align_to_grid(grid_resolution);
m_grid.set_bbox(bbox);
m_grid.create(*m_support_polygons, grid_resolution);
m_grid.calculate_sdf();
// Sample a single point per input support polygon, keep it as a reference to maintain corresponding
// polygons if ever these polygons get split into parts by the trimming polygons.
m_island_samples = island_samples(*m_support_polygons);
return true;
}
const Polygons& support_polygons() const { return *m_support_polygons; }
const Polygons& trimming_polygons() const { return *m_trimming_polygons; }
const EdgeGrid::Grid& grid() const { return m_grid; }
#endif /* SLIC3R_DEBUG */
private:
SupportGridPattern() {}
SupportGridPattern& operator=(const SupportGridPattern &rhs);
#if 0
@ -639,6 +773,12 @@ private:
// Internal sample points of supporting expolygons. These internal points are used to pick regions corresponding
// to the initial supporting regions, after these regions werre grown and possibly split to many by the trimming polygons.
Points m_island_samples;
#ifdef SLIC3R_DEBUG
// support for deserialization of m_support_polygons, m_trimming_polygons
Polygons m_support_polygons_deserialized;
Polygons m_trimming_polygons_deserialized;
#endif /* SLIC3R_DEBUG */
};
namespace SupportMaterialInternal {
@ -783,17 +923,40 @@ namespace SupportMaterialInternal {
if (surface.surface_type == stBottomBridge && surface.bridge_angle != -1)
polygons_append(bridges, surface.expolygon);
//FIXME add the gap filled areas. Extrude the gaps with a bridge flow?
contact_polygons = diff(contact_polygons, bridges, true);
// Add the bridge anchors into the region.
// Remove the unsupported ends of the bridges from the bridged areas.
//FIXME add supports at regular intervals to support long bridges!
polygons_append(contact_polygons,
intersection(
bridges = diff(bridges,
// Offset unsupported edges into polygons.
offset(layerm->unsupported_bridge_edges.polylines, scale_(SUPPORT_MATERIAL_MARGIN), SUPPORT_SURFACES_OFFSET_PARAMETERS),
bridges));
offset(layerm->unsupported_bridge_edges.polylines, scale_(SUPPORT_MATERIAL_MARGIN), SUPPORT_SURFACES_OFFSET_PARAMETERS));
// Remove bridged areas from the supported areas.
contact_polygons = diff(contact_polygons, bridges, true);
}
}
#if 0
static int Test()
{
// for (int i = 0; i < 30; ++ i)
{
int i = -1;
// SupportGridPattern grid("d:\\temp\\support-top-contacts-final-run1-layer460-z70.300000-prev.bin", i);
// SupportGridPattern grid("d:\\temp\\support-top-contacts-final-run1-layer460-z70.300000.bin", i);
auto grid = SupportGridPattern::deserialize("d:\\temp\\support-top-contacts-final-run1-layer27-z5.650000.bin", i);
std::vector<std::pair<EdgeGrid::Grid::ContourEdge, EdgeGrid::Grid::ContourEdge>> intersections = grid.grid().intersecting_edges();
if (! intersections.empty())
printf("Intersections between contours!\n");
Slic3r::export_intersections_to_svg("d:\\temp\\support_polygon_intersections.svg", grid.support_polygons());
Slic3r::SVG::export_expolygons("d:\\temp\\support_polygons.svg", union_ex(grid.support_polygons(), false));
Slic3r::SVG::export_expolygons("d:\\temp\\trimming_polygons.svg", union_ex(grid.trimming_polygons(), false));
Polygons extracted = grid.extract_support(scale_(0.21 / 2), true);
Slic3r::SVG::export_expolygons("d:\\temp\\extracted.svg", union_ex(extracted, false));
printf("hu!");
}
return 0;
}
static int run_support_test = Test();
#endif /* SLIC3R_DEBUG */
// Generate top contact layers supporting overhangs.
// For a soluble interface material synchronize the layer heights with the object, otherwise leave the layer height undefined.
// If supports over bed surface only are requested, don't generate contact layers over an object.
@ -1096,6 +1259,8 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::top_contact_
}
}
// Achtung! The contact_polygons need to be trimmed by slices_margin_cached, otherwise
// the selection by island_samples (see the SupportGridPattern::island_samples() method) will not work!
SupportGridPattern support_grid_pattern(
// Support islands, to be stretched into a grid.
contact_polygons,
@ -1114,9 +1279,14 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::top_contact_
// Reduce the amount of dense interfaces: Do not generate dense interfaces below overhangs with 60% overhang of the extrusions.
Polygons dense_interface_polygons = diff(overhang_polygons,
offset2(lower_layer_polygons, - no_interface_offset * 0.5f, no_interface_offset * (0.6f + 0.5f), SUPPORT_SURFACES_OFFSET_PARAMETERS));
// offset(lower_layer_polygons, no_interface_offset * 0.6f, SUPPORT_SURFACES_OFFSET_PARAMETERS));
if (! dense_interface_polygons.empty()) {
//FIXME do it for the bridges only?
dense_interface_polygons =
// Achtung! The dense_interface_polygons need to be trimmed by slices_margin_cached, otherwise
// the selection by island_samples (see the SupportGridPattern::island_samples() method) will not work!
diff(
// Regularize the contour.
offset(dense_interface_polygons, no_interface_offset * 0.1f),
slices_margin_cached);
SupportGridPattern support_grid_pattern(
// Support islands, to be stretched into a grid.
dense_interface_polygons,
@ -1126,8 +1296,34 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::top_contact_
m_object_config->support_material_spacing.value + m_support_material_flow.spacing(),
Geometry::deg2rad(m_object_config->support_material_angle.value));
new_layer.polygons = support_grid_pattern.extract_support(m_support_material_flow.scaled_spacing()/2 + 5, false);
#ifdef SLIC3R_DEBUG
{
support_grid_pattern.serialize(debug_out_path("support-top-contacts-final-run%d-layer%d-z%f.bin", iRun, layer_id, layer.print_z));
BoundingBox bbox = get_extents(contact_polygons);
bbox.merge(get_extents(new_layer.polygons));
::Slic3r::SVG svg(debug_out_path("support-top-contacts-final0-run%d-layer%d-z%f.svg", iRun, layer_id, layer.print_z));
svg.draw(union_ex(*new_layer.contact_polygons, false), "gray", 0.5f);
svg.draw(union_ex(contact_polygons, false), "blue", 0.5f);
svg.draw(union_ex(dense_interface_polygons, false), "green", 0.5f);
svg.draw(union_ex(new_layer.polygons, true), "red", 0.5f);
svg.draw_outline(union_ex(new_layer.polygons, true), "black", "black", scale_(0.1f));
}
#endif /* SLIC3R_DEBUG */
}
}
#ifdef SLIC3R_DEBUG
{
BoundingBox bbox = get_extents(contact_polygons);
bbox.merge(get_extents(new_layer.polygons));
::Slic3r::SVG svg(debug_out_path("support-top-contacts-final-run%d-layer%d-z%f.svg", iRun, layer_id, layer.print_z));
svg.draw(union_ex(*new_layer.contact_polygons, false), "gray", 0.5f);
svg.draw(union_ex(contact_polygons, false), "blue", 0.5f);
svg.draw(union_ex(overhang_polygons, false), "green", 0.5f);
svg.draw(union_ex(new_layer.polygons, true), "red", 0.5f);
svg.draw_outline(union_ex(new_layer.polygons, true), "black", "black", scale_(0.1f));
}
#endif /* SLIC3R_DEBUG */
// Even after the contact layer was expanded into a grid, some of the contact islands may be too tiny to be extruded.
// Remove those tiny islands from new_layer.polygons and new_layer.contact_polygons.

View file

@ -7,6 +7,8 @@
// Shows camera target in the 3D scene
#define ENABLE_SHOW_CAMERA_TARGET 0
// Log debug messages to console when changing selection
#define ENABLE_SELECTION_DEBUG_OUTPUT 0
//=============
// 1.42.0 techs

View file

@ -1212,6 +1212,345 @@ static inline void remove_tangent_edges(std::vector<IntersectionLine> &lines)
}
}
struct OpenPolyline {
OpenPolyline() {};
OpenPolyline(const IntersectionReference &start, const IntersectionReference &end, Points &&points) :
start(start), end(end), points(std::move(points)), consumed(false) { this->length = Slic3r::length(this->points); }
void reverse() {
std::swap(start, end);
std::reverse(points.begin(), points.end());
}
IntersectionReference start;
IntersectionReference end;
Points points;
double length;
bool consumed;
};
// called by TriangleMeshSlicer::make_loops() to connect sliced triangles into closed loops and open polylines by the triangle connectivity.
// Only connects segments crossing triangles of the same orientation.
static void chain_lines_by_triangle_connectivity(std::vector<IntersectionLine> &lines, Polygons &loops, std::vector<OpenPolyline> &open_polylines)
{
// Build a map of lines by edge_a_id and a_id.
std::vector<IntersectionLine*> by_edge_a_id;
std::vector<IntersectionLine*> by_a_id;
by_edge_a_id.reserve(lines.size());
by_a_id.reserve(lines.size());
for (IntersectionLine &line : lines) {
if (! line.skip()) {
if (line.edge_a_id != -1)
by_edge_a_id.emplace_back(&line);
if (line.a_id != -1)
by_a_id.emplace_back(&line);
}
}
auto by_edge_lower = [](const IntersectionLine* il1, const IntersectionLine *il2) { return il1->edge_a_id < il2->edge_a_id; };
auto by_vertex_lower = [](const IntersectionLine* il1, const IntersectionLine *il2) { return il1->a_id < il2->a_id; };
std::sort(by_edge_a_id.begin(), by_edge_a_id.end(), by_edge_lower);
std::sort(by_a_id.begin(), by_a_id.end(), by_vertex_lower);
// Chain the segments with a greedy algorithm, collect the loops and unclosed polylines.
IntersectionLines::iterator it_line_seed = lines.begin();
for (;;) {
// take first spare line and start a new loop
IntersectionLine *first_line = nullptr;
for (; it_line_seed != lines.end(); ++ it_line_seed)
if (it_line_seed->is_seed_candidate()) {
//if (! it_line_seed->skip()) {
first_line = &(*it_line_seed ++);
break;
}
if (first_line == nullptr)
break;
first_line->set_skip();
Points loop_pts;
loop_pts.emplace_back(first_line->a);
IntersectionLine *last_line = first_line;
/*
printf("first_line edge_a_id = %d, edge_b_id = %d, a_id = %d, b_id = %d, a = %d,%d, b = %d,%d\n",
first_line->edge_a_id, first_line->edge_b_id, first_line->a_id, first_line->b_id,
first_line->a.x, first_line->a.y, first_line->b.x, first_line->b.y);
*/
IntersectionLine key;
for (;;) {
// find a line starting where last one finishes
IntersectionLine* next_line = nullptr;
if (last_line->edge_b_id != -1) {
key.edge_a_id = last_line->edge_b_id;
auto it_begin = std::lower_bound(by_edge_a_id.begin(), by_edge_a_id.end(), &key, by_edge_lower);
if (it_begin != by_edge_a_id.end()) {
auto it_end = std::upper_bound(it_begin, by_edge_a_id.end(), &key, by_edge_lower);
for (auto it_line = it_begin; it_line != it_end; ++ it_line)
if (! (*it_line)->skip()) {
next_line = *it_line;
break;
}
}
}
if (next_line == nullptr && last_line->b_id != -1) {
key.a_id = last_line->b_id;
auto it_begin = std::lower_bound(by_a_id.begin(), by_a_id.end(), &key, by_vertex_lower);
if (it_begin != by_a_id.end()) {
auto it_end = std::upper_bound(it_begin, by_a_id.end(), &key, by_vertex_lower);
for (auto it_line = it_begin; it_line != it_end; ++ it_line)
if (! (*it_line)->skip()) {
next_line = *it_line;
break;
}
}
}
if (next_line == nullptr) {
// Check whether we closed this loop.
if ((first_line->edge_a_id != -1 && first_line->edge_a_id == last_line->edge_b_id) ||
(first_line->a_id != -1 && first_line->a_id == last_line->b_id)) {
// The current loop is complete. Add it to the output.
loops.emplace_back(std::move(loop_pts));
#ifdef SLIC3R_TRIANGLEMESH_DEBUG
printf(" Discovered %s polygon of %d points\n", (p.is_counter_clockwise() ? "ccw" : "cw"), (int)p.points.size());
#endif
} else {
// This is an open polyline. Add it to the list of open polylines. These open polylines will processed later.
loop_pts.emplace_back(last_line->b);
open_polylines.emplace_back(OpenPolyline(
IntersectionReference(first_line->a_id, first_line->edge_a_id),
IntersectionReference(last_line->b_id, last_line->edge_b_id), std::move(loop_pts)));
}
break;
}
/*
printf("next_line edge_a_id = %d, edge_b_id = %d, a_id = %d, b_id = %d, a = %d,%d, b = %d,%d\n",
next_line->edge_a_id, next_line->edge_b_id, next_line->a_id, next_line->b_id,
next_line->a.x, next_line->a.y, next_line->b.x, next_line->b.y);
*/
loop_pts.emplace_back(next_line->a);
last_line = next_line;
next_line->set_skip();
}
}
}
std::vector<OpenPolyline*> open_polylines_sorted(std::vector<OpenPolyline> &open_polylines, bool update_lengths)
{
std::vector<OpenPolyline*> out;
out.reserve(open_polylines.size());
for (OpenPolyline &opl : open_polylines)
if (! opl.consumed) {
if (update_lengths)
opl.length = Slic3r::length(opl.points);
out.emplace_back(&opl);
}
std::sort(out.begin(), out.end(), [](const OpenPolyline *lhs, const OpenPolyline *rhs){ return lhs->length > rhs->length; });
return out;
}
// called by TriangleMeshSlicer::make_loops() to connect remaining open polylines across shared triangle edges and vertices.
// Depending on "try_connect_reversed", it may or may not connect segments crossing triangles of opposite orientation.
static void chain_open_polylines_exact(std::vector<OpenPolyline> &open_polylines, Polygons &loops, bool try_connect_reversed)
{
// Store the end points of open_polylines into vectors sorted
struct OpenPolylineEnd {
OpenPolylineEnd(OpenPolyline *polyline, bool start) : polyline(polyline), start(start) {}
OpenPolyline *polyline;
// Is it the start or end point?
bool start;
const IntersectionReference& ipref() const { return start ? polyline->start : polyline->end; }
// Return a unique ID for the intersection point.
// Return a positive id for a point, or a negative id for an edge.
int id() const { const IntersectionReference &r = ipref(); return (r.point_id >= 0) ? r.point_id : - r.edge_id; }
bool operator==(const OpenPolylineEnd &rhs) const { return this->polyline == rhs.polyline && this->start == rhs.start; }
};
auto by_id_lower = [](const OpenPolylineEnd &ope1, const OpenPolylineEnd &ope2) { return ope1.id() < ope2.id(); };
std::vector<OpenPolylineEnd> by_id;
by_id.reserve(2 * open_polylines.size());
for (OpenPolyline &opl : open_polylines) {
if (opl.start.point_id != -1 || opl.start.edge_id != -1)
by_id.emplace_back(OpenPolylineEnd(&opl, true));
if (try_connect_reversed && (opl.end.point_id != -1 || opl.end.edge_id != -1))
by_id.emplace_back(OpenPolylineEnd(&opl, false));
}
std::sort(by_id.begin(), by_id.end(), by_id_lower);
// Find an iterator to by_id_lower for the particular end of OpenPolyline (by comparing the OpenPolyline pointer and the start attribute).
auto find_polyline_end = [&by_id, by_id_lower](const OpenPolylineEnd &end) -> std::vector<OpenPolylineEnd>::iterator {
for (auto it = std::lower_bound(by_id.begin(), by_id.end(), end, by_id_lower);
it != by_id.end() && it->id() == end.id(); ++ it)
if (*it == end)
return it;
return by_id.end();
};
// Try to connect the loops.
std::vector<OpenPolyline*> sorted_by_length = open_polylines_sorted(open_polylines, false);
for (OpenPolyline *opl : sorted_by_length) {
if (opl->consumed)
continue;
opl->consumed = true;
OpenPolylineEnd end(opl, false);
for (;;) {
// find a line starting where last one finishes
auto it_next_start = std::lower_bound(by_id.begin(), by_id.end(), end, by_id_lower);
for (; it_next_start != by_id.end() && it_next_start->id() == end.id(); ++ it_next_start)
if (! it_next_start->polyline->consumed)
goto found;
// The current loop could not be closed. Unmark the segment.
opl->consumed = false;
break;
found:
// Attach this polyline to the end of the initial polyline.
if (it_next_start->start) {
auto it = it_next_start->polyline->points.begin();
std::copy(++ it, it_next_start->polyline->points.end(), back_inserter(opl->points));
} else {
auto it = it_next_start->polyline->points.rbegin();
std::copy(++ it, it_next_start->polyline->points.rend(), back_inserter(opl->points));
}
opl->length += it_next_start->polyline->length;
// Mark the next polyline as consumed.
it_next_start->polyline->points.clear();
it_next_start->polyline->length = 0.;
it_next_start->polyline->consumed = true;
if (try_connect_reversed) {
// Running in a mode, where the polylines may be connected by mixing their orientations.
// Update the end point lookup structure after the end point of the current polyline was extended.
auto it_end = find_polyline_end(end);
auto it_next_end = find_polyline_end(OpenPolylineEnd(it_next_start->polyline, !it_next_start->start));
// Swap the end points of the current and next polyline, but keep the polyline ptr and the start flag.
std::swap(opl->end, it_next_end->start ? it_next_end->polyline->start : it_next_end->polyline->end);
// Swap the positions of OpenPolylineEnd structures in the sorted array to match their respective end point positions.
std::swap(*it_end, *it_next_end);
}
// Check whether we closed this loop.
if ((opl->start.edge_id != -1 && opl->start.edge_id == opl->end.edge_id) ||
(opl->start.point_id != -1 && opl->start.point_id == opl->end.point_id)) {
// The current loop is complete. Add it to the output.
//assert(opl->points.front().point_id == opl->points.back().point_id);
//assert(opl->points.front().edge_id == opl->points.back().edge_id);
// Remove the duplicate last point.
opl->points.pop_back();
if (opl->points.size() >= 3) {
if (try_connect_reversed && area(opl->points) < 0)
// The closed polygon is patched from pieces with messed up orientation, therefore
// the orientation of the patched up polygon is not known.
// Orient the patched up polygons CCW. This heuristic may close some holes and cavities.
std::reverse(opl->points.begin(), opl->points.end());
loops.emplace_back(std::move(opl->points));
}
opl->points.clear();
break;
}
// Continue with the current loop.
}
}
}
// called by TriangleMeshSlicer::make_loops() to connect remaining open polylines across shared triangle edges and vertices,
// possibly closing small gaps.
// Depending on "try_connect_reversed", it may or may not connect segments crossing triangles of opposite orientation.
static void chain_open_polylines_close_gaps(std::vector<OpenPolyline> &open_polylines, Polygons &loops, double max_gap, bool try_connect_reversed)
{
const coord_t max_gap_scaled = (coord_t)scale_(max_gap);
// Sort the open polylines by their length, so the new loops will be seeded from longer chains.
// Update the polyline lengths, return only not yet consumed polylines.
std::vector<OpenPolyline*> sorted_by_length = open_polylines_sorted(open_polylines, true);
// Store the end points of open_polylines into ClosestPointInRadiusLookup<OpenPolylineEnd>.
struct OpenPolylineEnd {
OpenPolylineEnd(OpenPolyline *polyline, bool start) : polyline(polyline), start(start) {}
OpenPolyline *polyline;
// Is it the start or end point?
bool start;
const Point& point() const { return start ? polyline->points.front() : polyline->points.back(); }
bool operator==(const OpenPolylineEnd &rhs) const { return this->polyline == rhs.polyline && this->start == rhs.start; }
};
struct OpenPolylineEndAccessor {
const Point* operator()(const OpenPolylineEnd &pt) const { return pt.polyline->consumed ? nullptr : &pt.point(); }
};
typedef ClosestPointInRadiusLookup<OpenPolylineEnd, OpenPolylineEndAccessor> ClosestPointLookupType;
ClosestPointLookupType closest_end_point_lookup(max_gap_scaled);
for (OpenPolyline *opl : sorted_by_length) {
closest_end_point_lookup.insert(OpenPolylineEnd(opl, true));
if (try_connect_reversed)
closest_end_point_lookup.insert(OpenPolylineEnd(opl, false));
}
// Try to connect the loops.
for (OpenPolyline *opl : sorted_by_length) {
if (opl->consumed)
continue;
OpenPolylineEnd end(opl, false);
if (try_connect_reversed)
// The end point of this polyline will be modified, thus the following entry will become invalid. Remove it.
closest_end_point_lookup.erase(end);
opl->consumed = true;
size_t n_segments_joined = 1;
for (;;) {
// Find a line starting where last one finishes, only return non-consumed open polylines (OpenPolylineEndAccessor returns null for consumed).
std::pair<const OpenPolylineEnd*, double> next_start_and_dist = closest_end_point_lookup.find(end.point());
const OpenPolylineEnd *next_start = next_start_and_dist.first;
// Check whether we closed this loop.
double current_loop_closing_distance2 = (opl->points.back() - opl->points.front()).cast<double>().squaredNorm();
bool loop_closed = current_loop_closing_distance2 < coordf_t(max_gap_scaled) * coordf_t(max_gap_scaled);
if (next_start != nullptr && loop_closed && current_loop_closing_distance2 < next_start_and_dist.second) {
// Heuristics to decide, whether to close the loop, or connect another polyline.
// One should avoid closing loops shorter than max_gap_scaled.
loop_closed = sqrt(current_loop_closing_distance2) < 0.3 * length(opl->points);
}
if (loop_closed) {
// Remove the start point of the current polyline from the lookup.
// Mark the current segment as not consumed, otherwise the closest_end_point_lookup.erase() would fail.
opl->consumed = false;
closest_end_point_lookup.erase(OpenPolylineEnd(opl, true));
if (current_loop_closing_distance2 == 0.) {
// Remove the duplicate last point.
opl->points.pop_back();
} else {
// The end points are different, keep both of them.
}
if (opl->points.size() >= 3) {
if (try_connect_reversed && n_segments_joined > 1 && area(opl->points) < 0)
// The closed polygon is patched from pieces with messed up orientation, therefore
// the orientation of the patched up polygon is not known.
// Orient the patched up polygons CCW. This heuristic may close some holes and cavities.
std::reverse(opl->points.begin(), opl->points.end());
loops.emplace_back(std::move(opl->points));
}
opl->points.clear();
opl->consumed = true;
break;
}
if (next_start == nullptr) {
// The current loop could not be closed. Unmark the segment.
opl->consumed = false;
if (try_connect_reversed)
// Re-insert the end point.
closest_end_point_lookup.insert(OpenPolylineEnd(opl, false));
break;
}
// Attach this polyline to the end of the initial polyline.
if (next_start->start) {
auto it = next_start->polyline->points.begin();
if (*it == opl->points.back())
++ it;
std::copy(it, next_start->polyline->points.end(), back_inserter(opl->points));
} else {
auto it = next_start->polyline->points.rbegin();
if (*it == opl->points.back())
++ it;
std::copy(it, next_start->polyline->points.rend(), back_inserter(opl->points));
}
++ n_segments_joined;
// Remove the end points of the consumed polyline segment from the lookup.
OpenPolyline *opl2 = next_start->polyline;
closest_end_point_lookup.erase(OpenPolylineEnd(opl2, true));
if (try_connect_reversed)
closest_end_point_lookup.erase(OpenPolylineEnd(opl2, false));
opl2->points.clear();
opl2->consumed = true;
// Continue with the current loop.
}
}
}
void TriangleMeshSlicer::make_loops(std::vector<IntersectionLine> &lines, Polygons* loops) const
{
#if 0
@ -1221,231 +1560,83 @@ void TriangleMeshSlicer::make_loops(std::vector<IntersectionLine> &lines, Polygo
assert(l.a != l.b);
#endif /* _DEBUG */
remove_tangent_edges(lines);
// There should be no tangent edges, as the horizontal triangles are ignored and if two triangles touch at a cutting plane,
// only the bottom triangle is considered to be cutting the plane.
// remove_tangent_edges(lines);
struct OpenPolyline {
OpenPolyline() {};
OpenPolyline(const IntersectionReference &start, const IntersectionReference &end, Points &&points) :
start(start), end(end), points(std::move(points)), consumed(false) {}
void reverse() {
std::swap(start, end);
std::reverse(points.begin(), points.end());
#ifdef SLIC3R_DEBUG_SLICE_PROCESSING
BoundingBox bbox_svg;
{
static int iRun = 0;
for (const Line &line : lines) {
bbox_svg.merge(line.a);
bbox_svg.merge(line.b);
}
SVG svg(debug_out_path("TriangleMeshSlicer_make_loops-raw_lines-%d.svg", iRun ++).c_str(), bbox_svg);
for (const Line &line : lines)
svg.draw(line);
svg.Close();
}
IntersectionReference start;
IntersectionReference end;
Points points;
bool consumed;
};
#endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
std::vector<OpenPolyline> open_polylines;
{
// Build a map of lines by edge_a_id and a_id.
std::vector<IntersectionLine*> by_edge_a_id;
std::vector<IntersectionLine*> by_a_id;
by_edge_a_id.reserve(lines.size());
by_a_id.reserve(lines.size());
for (IntersectionLine &line : lines) {
if (! line.skip()) {
if (line.edge_a_id != -1)
by_edge_a_id.emplace_back(&line);
if (line.a_id != -1)
by_a_id.emplace_back(&line);
}
chain_lines_by_triangle_connectivity(lines, *loops, open_polylines);
#ifdef SLIC3R_DEBUG_SLICE_PROCESSING
{
static int iRun = 0;
SVG svg(debug_out_path("TriangleMeshSlicer_make_loops-polylines-%d.svg", iRun ++).c_str(), bbox_svg);
svg.draw(union_ex(*loops));
for (const OpenPolyline &pl : open_polylines)
svg.draw(Polyline(pl.points), "red");
svg.Close();
}
auto by_edge_lower = [](const IntersectionLine* il1, const IntersectionLine *il2) { return il1->edge_a_id < il2->edge_a_id; };
auto by_vertex_lower = [](const IntersectionLine* il1, const IntersectionLine *il2) { return il1->a_id < il2->a_id; };
std::sort(by_edge_a_id.begin(), by_edge_a_id.end(), by_edge_lower);
std::sort(by_a_id.begin(), by_a_id.end(), by_vertex_lower);
// Chain the segments with a greedy algorithm, collect the loops and unclosed polylines.
IntersectionLines::iterator it_line_seed = lines.begin();
for (;;) {
// take first spare line and start a new loop
IntersectionLine *first_line = nullptr;
for (; it_line_seed != lines.end(); ++ it_line_seed)
if (it_line_seed->is_seed_candidate()) {
//if (! it_line_seed->skip()) {
first_line = &(*it_line_seed ++);
break;
}
if (first_line == nullptr)
break;
first_line->set_skip();
Points loop_pts;
loop_pts.emplace_back(first_line->a);
IntersectionLine *last_line = first_line;
/*
printf("first_line edge_a_id = %d, edge_b_id = %d, a_id = %d, b_id = %d, a = %d,%d, b = %d,%d\n",
first_line->edge_a_id, first_line->edge_b_id, first_line->a_id, first_line->b_id,
first_line->a.x, first_line->a.y, first_line->b.x, first_line->b.y);
*/
IntersectionLine key;
for (;;) {
// find a line starting where last one finishes
IntersectionLine* next_line = nullptr;
if (last_line->edge_b_id != -1) {
key.edge_a_id = last_line->edge_b_id;
auto it_begin = std::lower_bound(by_edge_a_id.begin(), by_edge_a_id.end(), &key, by_edge_lower);
if (it_begin != by_edge_a_id.end()) {
auto it_end = std::upper_bound(it_begin, by_edge_a_id.end(), &key, by_edge_lower);
for (auto it_line = it_begin; it_line != it_end; ++ it_line)
if (! (*it_line)->skip()) {
next_line = *it_line;
break;
}
}
}
if (next_line == nullptr && last_line->b_id != -1) {
key.a_id = last_line->b_id;
auto it_begin = std::lower_bound(by_a_id.begin(), by_a_id.end(), &key, by_vertex_lower);
if (it_begin != by_a_id.end()) {
auto it_end = std::upper_bound(it_begin, by_a_id.end(), &key, by_vertex_lower);
for (auto it_line = it_begin; it_line != it_end; ++ it_line)
if (! (*it_line)->skip()) {
next_line = *it_line;
break;
}
}
}
if (next_line == nullptr) {
// Check whether we closed this loop.
if ((first_line->edge_a_id != -1 && first_line->edge_a_id == last_line->edge_b_id) ||
(first_line->a_id != -1 && first_line->a_id == last_line->b_id)) {
// The current loop is complete. Add it to the output.
loops->emplace_back(std::move(loop_pts));
#ifdef SLIC3R_TRIANGLEMESH_DEBUG
printf(" Discovered %s polygon of %d points\n", (p.is_counter_clockwise() ? "ccw" : "cw"), (int)p.points.size());
#endif
} else {
// This is an open polyline. Add it to the list of open polylines. These open polylines will processed later.
loop_pts.emplace_back(last_line->b);
open_polylines.emplace_back(OpenPolyline(
IntersectionReference(first_line->a_id, first_line->edge_a_id),
IntersectionReference(last_line->b_id, last_line->edge_b_id), std::move(loop_pts)));
}
break;
}
/*
printf("next_line edge_a_id = %d, edge_b_id = %d, a_id = %d, b_id = %d, a = %d,%d, b = %d,%d\n",
next_line->edge_a_id, next_line->edge_b_id, next_line->a_id, next_line->b_id,
next_line->a.x, next_line->a.y, next_line->b.x, next_line->b.y);
*/
loop_pts.emplace_back(next_line->a);
last_line = next_line;
next_line->set_skip();
}
}
}
#endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
// Now process the open polylines.
if (! open_polylines.empty()) {
// Store the end points of open_polylines into vectors sorted
struct OpenPolylineEnd {
OpenPolylineEnd(OpenPolyline *polyline, bool start) : polyline(polyline), start(start) {}
OpenPolyline *polyline;
// Is it the start or end point?
bool start;
const IntersectionReference& ipref() const { return start ? polyline->start : polyline->end; }
int point_id() const { return ipref().point_id; }
int edge_id () const { return ipref().edge_id; }
};
auto by_edge_lower = [](const OpenPolylineEnd &ope1, const OpenPolylineEnd &ope2) { return ope1.edge_id() < ope2.edge_id(); };
auto by_point_lower = [](const OpenPolylineEnd &ope1, const OpenPolylineEnd &ope2) { return ope1.point_id() < ope2.point_id(); };
std::vector<OpenPolylineEnd> by_edge_id;
std::vector<OpenPolylineEnd> by_point_id;
by_edge_id.reserve(2 * open_polylines.size());
by_point_id.reserve(2 * open_polylines.size());
for (OpenPolyline &opl : open_polylines) {
if (opl.start.edge_id != -1)
by_edge_id .emplace_back(OpenPolylineEnd(&opl, true));
if (opl.end.edge_id != -1)
by_edge_id .emplace_back(OpenPolylineEnd(&opl, false));
if (opl.start.point_id != -1)
by_point_id.emplace_back(OpenPolylineEnd(&opl, true));
if (opl.end.point_id != -1)
by_point_id.emplace_back(OpenPolylineEnd(&opl, false));
}
std::sort(by_edge_id .begin(), by_edge_id .end(), by_edge_lower);
std::sort(by_point_id.begin(), by_point_id.end(), by_point_lower);
// Do it in two rounds, first try to connect in the same direction only,
// then try to connect the open polylines in reversed order as well.
chain_open_polylines_exact(open_polylines, *loops, false);
chain_open_polylines_exact(open_polylines, *loops, true);
// Try to connect the loops.
for (OpenPolyline &opl : open_polylines) {
if (opl.consumed)
#ifdef SLIC3R_DEBUG_SLICE_PROCESSING
{
static int iRun = 0;
SVG svg(debug_out_path("TriangleMeshSlicer_make_loops-polylines2-%d.svg", iRun++).c_str(), bbox_svg);
svg.draw(union_ex(*loops));
for (const OpenPolyline &pl : open_polylines) {
if (pl.points.empty())
continue;
opl.consumed = true;
OpenPolylineEnd end(&opl, false);
for (;;) {
// find a line starting where last one finishes
OpenPolylineEnd* next_start = nullptr;
if (end.edge_id() != -1) {
auto it_begin = std::lower_bound(by_edge_id.begin(), by_edge_id.end(), end, by_edge_lower);
if (it_begin != by_edge_id.end()) {
auto it_end = std::upper_bound(it_begin, by_edge_id.end(), end, by_edge_lower);
for (auto it_edge = it_begin; it_edge != it_end; ++ it_edge)
if (! it_edge->polyline->consumed) {
next_start = &(*it_edge);
break;
}
}
}
if (next_start == nullptr && end.point_id() != -1) {
auto it_begin = std::lower_bound(by_point_id.begin(), by_point_id.end(), end, by_point_lower);
if (it_begin != by_point_id.end()) {
auto it_end = std::upper_bound(it_begin, by_point_id.end(), end, by_point_lower);
for (auto it_point = it_begin; it_point != it_end; ++ it_point)
if (! it_point->polyline->consumed) {
next_start = &(*it_point);
break;
}
}
}
if (next_start == nullptr) {
// The current loop could not be closed. Unmark the segment.
opl.consumed = false;
break;
}
// Attach this polyline to the end of the initial polyline.
if (next_start->start) {
auto it = next_start->polyline->points.begin();
std::copy(++ it, next_start->polyline->points.end(), back_inserter(opl.points));
//opl.points.insert(opl.points.back(), ++ it, next_start->polyline->points.end());
} else {
auto it = next_start->polyline->points.rbegin();
std::copy(++ it, next_start->polyline->points.rend(), back_inserter(opl.points));
//opl.points.insert(opl.points.back(), ++ it, next_start->polyline->points.rend());
}
end = *next_start;
end.start = !end.start;
next_start->polyline->points.clear();
next_start->polyline->consumed = true;
// Check whether we closed this loop.
const IntersectionReference &ip1 = opl.start;
const IntersectionReference &ip2 = end.ipref();
if ((ip1.edge_id != -1 && ip1.edge_id == ip2.edge_id) ||
(ip1.point_id != -1 && ip1.point_id == ip2.point_id)) {
// The current loop is complete. Add it to the output.
//assert(opl.points.front().point_id == opl.points.back().point_id);
//assert(opl.points.front().edge_id == opl.points.back().edge_id);
// Remove the duplicate last point.
opl.points.pop_back();
if (opl.points.size() >= 3) {
// The closed polygon is patched from pieces with messed up orientation, therefore
// the orientation of the patched up polygon is not known.
// Orient the patched up polygons CCW. This heuristic may close some holes and cavities.
double area = 0.;
for (size_t i = 0, j = opl.points.size() - 1; i < opl.points.size(); j = i ++)
area += double(opl.points[j](0) + opl.points[i](0)) * double(opl.points[i](1) - opl.points[j](1));
if (area < 0)
std::reverse(opl.points.begin(), opl.points.end());
loops->emplace_back(std::move(opl.points));
}
opl.points.clear();
break;
}
// Continue with the current loop.
}
svg.draw(Polyline(pl.points), "red");
svg.draw(pl.points.front(), "blue");
svg.draw(pl.points.back(), "blue");
}
svg.Close();
}
#endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
// Try to close gaps.
// Do it in two rounds, first try to connect in the same direction only,
// then try to connect the open polylines in reversed order as well.
const double max_gap = 2.; //mm
chain_open_polylines_close_gaps(open_polylines, *loops, max_gap, false);
chain_open_polylines_close_gaps(open_polylines, *loops, max_gap, true);
#ifdef SLIC3R_DEBUG_SLICE_PROCESSING
{
static int iRun = 0;
SVG svg(debug_out_path("TriangleMeshSlicer_make_loops-polylines-final-%d.svg", iRun++).c_str(), bbox_svg);
svg.draw(union_ex(*loops));
for (const OpenPolyline &pl : open_polylines) {
if (pl.points.empty())
continue;
svg.draw(Polyline(pl.points), "red");
svg.draw(pl.points.front(), "blue");
svg.draw(pl.points.back(), "blue");
}
svg.Close();
}
#endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
}
// Only used to cut the mesh into two halves.
@ -1580,10 +1771,11 @@ void TriangleMeshSlicer::make_expolygons(const Polygons &loops, ExPolygons* slic
// p_slices = diff(p_slices, *loop);
//}
// perform a safety offset to merge very close facets (TODO: find test case for this)
double safety_offset = scale_(0.0499);
//FIXME see https://github.com/prusa3d/Slic3r/issues/520
// double safety_offset = scale_(0.0001);
// Perform a safety offset to merge very close facets (TODO: find test case for this)
// 0.0499 comes from https://github.com/slic3r/Slic3r/issues/959
// double safety_offset = scale_(0.0499);
// 0.0001 is set to satisfy GH #520, #1029, #1364
double safety_offset = scale_(0.0001);
/* The following line is commented out because it can generate wrong polygons,
see for example issue #661 */