2016-11-02 09:47:00 +00:00
|
|
|
#include <assert.h>
|
2016-10-27 15:03:57 +00:00
|
|
|
#include <stdio.h>
|
2016-11-02 10:13:13 +00:00
|
|
|
#include <memory>
|
2016-10-27 15:03:57 +00:00
|
|
|
|
|
|
|
#include "../ClipperUtils.hpp"
|
2016-11-02 09:47:00 +00:00
|
|
|
#include "../Geometry.hpp"
|
|
|
|
#include "../Layer.hpp"
|
|
|
|
#include "../Print.hpp"
|
2016-10-27 15:03:57 +00:00
|
|
|
#include "../PrintConfig.hpp"
|
2016-11-02 09:47:00 +00:00
|
|
|
#include "../Surface.hpp"
|
2016-10-27 15:03:57 +00:00
|
|
|
|
|
|
|
#include "FillBase.hpp"
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2016-11-08 08:59:25 +00:00
|
|
|
struct SurfaceGroupAttrib
|
|
|
|
{
|
2017-07-19 13:53:43 +00:00
|
|
|
SurfaceGroupAttrib() : is_solid(false), flow_width(0.f), pattern(-1) {}
|
2016-11-08 08:59:25 +00:00
|
|
|
bool operator==(const SurfaceGroupAttrib &other) const
|
2017-07-19 13:53:43 +00:00
|
|
|
{ return is_solid == other.is_solid && flow_width == other.flow_width && pattern == other.pattern; }
|
2016-11-08 08:59:25 +00:00
|
|
|
bool is_solid;
|
2017-07-19 13:53:43 +00:00
|
|
|
float flow_width;
|
2016-11-08 08:59:25 +00:00
|
|
|
// pattern is of type InfillPattern, -1 for an unset pattern.
|
|
|
|
int pattern;
|
|
|
|
};
|
|
|
|
|
2016-10-27 15:03:57 +00:00
|
|
|
// Generate infills for Slic3r::Layer::Region.
|
|
|
|
// The Slic3r::Layer::Region at this point of time may contain
|
|
|
|
// surfaces of various types (internal/bridge/top/bottom/solid).
|
|
|
|
// The infills are generated on the groups of surfaces with a compatible type.
|
|
|
|
// Returns an array of Slic3r::ExtrusionPath::Collection objects containing the infills generaed now
|
|
|
|
// and the thin fills generated by generate_perimeters().
|
|
|
|
void make_fill(LayerRegion &layerm, ExtrusionEntityCollection &out)
|
|
|
|
{
|
|
|
|
// Slic3r::debugf "Filling layer %d:\n", $layerm->layer->id;
|
|
|
|
|
2018-09-11 12:04:47 +00:00
|
|
|
double fill_density = layerm.region()->config().fill_density;
|
2016-10-27 15:03:57 +00:00
|
|
|
Flow infill_flow = layerm.flow(frInfill);
|
|
|
|
Flow solid_infill_flow = layerm.flow(frSolidInfill);
|
|
|
|
Flow top_solid_infill_flow = layerm.flow(frTopSolidInfill);
|
|
|
|
|
|
|
|
Surfaces surfaces;
|
|
|
|
|
|
|
|
// merge adjacent surfaces
|
|
|
|
// in case of bridge surfaces, the ones with defined angle will be attached to the ones
|
|
|
|
// without any angle (shouldn't this logic be moved to process_external_surfaces()?)
|
|
|
|
{
|
2016-11-08 08:59:25 +00:00
|
|
|
Polygons polygons_bridged;
|
|
|
|
polygons_bridged.reserve(layerm.fill_surfaces.surfaces.size());
|
2016-11-02 09:47:00 +00:00
|
|
|
for (Surfaces::iterator it = layerm.fill_surfaces.surfaces.begin(); it != layerm.fill_surfaces.surfaces.end(); ++ it)
|
2016-10-27 15:03:57 +00:00
|
|
|
if (it->bridge_angle >= 0)
|
2016-11-08 08:59:25 +00:00
|
|
|
polygons_append(polygons_bridged, *it);
|
2016-10-27 15:03:57 +00:00
|
|
|
|
|
|
|
// group surfaces by distinct properties (equal surface_type, thickness, thickness_layers, bridge_angle)
|
|
|
|
// group is of type Slic3r::SurfaceCollection
|
|
|
|
//FIXME: Use some smart heuristics to merge similar surfaces to eliminate tiny regions.
|
|
|
|
std::vector<SurfacesPtr> groups;
|
2016-11-02 09:47:00 +00:00
|
|
|
layerm.fill_surfaces.group(&groups);
|
2016-10-27 15:03:57 +00:00
|
|
|
|
|
|
|
// merge compatible groups (we can generate continuous infill for them)
|
|
|
|
{
|
|
|
|
// cache flow widths and patterns used for all solid groups
|
|
|
|
// (we'll use them for comparing compatible groups)
|
2016-11-08 08:59:25 +00:00
|
|
|
std::vector<SurfaceGroupAttrib> group_attrib(groups.size());
|
2016-11-02 09:47:00 +00:00
|
|
|
for (size_t i = 0; i < groups.size(); ++ i) {
|
2016-10-27 15:03:57 +00:00
|
|
|
// we can only merge solid non-bridge surfaces, so discard
|
|
|
|
// non-solid surfaces
|
2016-11-02 09:47:00 +00:00
|
|
|
const Surface &surface = *groups[i].front();
|
|
|
|
if (surface.is_solid() && (!surface.is_bridge() || layerm.layer()->id() == 0)) {
|
2016-11-08 08:59:25 +00:00
|
|
|
group_attrib[i].is_solid = true;
|
2017-07-19 13:53:43 +00:00
|
|
|
group_attrib[i].flow_width = (surface.surface_type == stTop) ? top_solid_infill_flow.width : solid_infill_flow.width;
|
2019-02-22 14:25:35 +00:00
|
|
|
group_attrib[i].pattern = surface.is_external() ?
|
|
|
|
(surface.is_top() ? layerm.region()->config().top_fill_pattern.value : layerm.region()->config().bottom_fill_pattern.value) :
|
|
|
|
ipRectilinear;
|
2016-10-27 15:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-08 08:59:25 +00:00
|
|
|
// Loop through solid groups, find compatible groups and append them to this one.
|
2016-11-02 09:47:00 +00:00
|
|
|
for (size_t i = 0; i < groups.size(); ++ i) {
|
2016-11-08 08:59:25 +00:00
|
|
|
if (! group_attrib[i].is_solid)
|
|
|
|
continue;
|
|
|
|
for (size_t j = i + 1; j < groups.size();) {
|
|
|
|
if (group_attrib[i] == group_attrib[j]) {
|
|
|
|
// groups are compatible, merge them
|
|
|
|
groups[i].insert(groups[i].end(), groups[j].begin(), groups[j].end());
|
|
|
|
groups.erase(groups.begin() + j);
|
|
|
|
group_attrib.erase(group_attrib.begin() + j);
|
|
|
|
} else
|
|
|
|
++ j;
|
2016-10-27 15:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 09:47:00 +00:00
|
|
|
// Give priority to bridges. Process the bridges in the first round, the rest of the surfaces in the 2nd round.
|
|
|
|
for (size_t round = 0; round < 2; ++ round) {
|
|
|
|
for (std::vector<SurfacesPtr>::iterator it_group = groups.begin(); it_group != groups.end(); ++ it_group) {
|
|
|
|
const SurfacesPtr &group = *it_group;
|
|
|
|
bool is_bridge = group.front()->bridge_angle >= 0;
|
|
|
|
if (is_bridge != (round == 0))
|
|
|
|
continue;
|
|
|
|
// Make a union of polygons defining the infiill regions of a group, use a safety offset.
|
|
|
|
Polygons union_p = union_(to_polygons(*it_group), true);
|
|
|
|
// Subtract surfaces having a defined bridge_angle from any other, use a safety offset.
|
2016-11-08 08:59:25 +00:00
|
|
|
if (! polygons_bridged.empty() && ! is_bridge)
|
|
|
|
union_p = diff(union_p, polygons_bridged, true);
|
2016-11-02 09:47:00 +00:00
|
|
|
// subtract any other surface already processed
|
|
|
|
//FIXME Vojtech: Because the bridge surfaces came first, they are subtracted twice!
|
2016-11-08 08:59:25 +00:00
|
|
|
// Using group.front() as a template.
|
|
|
|
surfaces_append(surfaces, diff_ex(union_p, to_polygons(surfaces), true), *group.front());
|
2016-10-27 15:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to detect any narrow surfaces that might collapse
|
|
|
|
// when adding spacing below
|
|
|
|
// such narrow surfaces are often generated in sloping walls
|
|
|
|
// by bridge_over_infill() and combine_infill() as a result of the
|
|
|
|
// subtraction of the combinable area from the layer infill area,
|
|
|
|
// which leaves small areas near the perimeters
|
|
|
|
// we are going to grow such regions by overlapping them with the void (if any)
|
|
|
|
// TODO: detect and investigate whether there could be narrow regions without
|
|
|
|
// any void neighbors
|
|
|
|
{
|
2016-11-02 09:47:00 +00:00
|
|
|
coord_t distance_between_surfaces = std::max(
|
|
|
|
std::max(infill_flow.scaled_spacing(), solid_infill_flow.scaled_spacing()),
|
|
|
|
top_solid_infill_flow.scaled_spacing());
|
|
|
|
Polygons surfaces_polygons = to_polygons(surfaces);
|
|
|
|
Polygons collapsed = diff(
|
|
|
|
surfaces_polygons,
|
2019-06-20 14:15:09 +00:00
|
|
|
offset2(surfaces_polygons, (float)-distance_between_surfaces/2, (float)+distance_between_surfaces/2),
|
2016-11-02 09:47:00 +00:00
|
|
|
true);
|
|
|
|
Polygons to_subtract;
|
|
|
|
to_subtract.reserve(collapsed.size() + number_polygons(surfaces));
|
|
|
|
for (Surfaces::const_iterator it_surface = surfaces.begin(); it_surface != surfaces.end(); ++ it_surface)
|
|
|
|
if (it_surface->surface_type == stInternalVoid)
|
|
|
|
polygons_append(to_subtract, *it_surface);
|
|
|
|
polygons_append(to_subtract, collapsed);
|
|
|
|
surfaces_append(
|
|
|
|
surfaces,
|
|
|
|
intersection_ex(
|
2019-06-20 14:15:09 +00:00
|
|
|
offset(collapsed, (float)distance_between_surfaces),
|
2016-11-02 09:47:00 +00:00
|
|
|
to_subtract,
|
|
|
|
true),
|
|
|
|
stInternalSolid);
|
2016-10-27 15:03:57 +00:00
|
|
|
}
|
2016-11-02 09:47:00 +00:00
|
|
|
|
2016-10-27 15:03:57 +00:00
|
|
|
if (0) {
|
2016-11-02 09:47:00 +00:00
|
|
|
// require "Slic3r/SVG.pm";
|
|
|
|
// Slic3r::SVG::output("fill_" . $layerm->print_z . ".svg",
|
|
|
|
// expolygons => [ map $_->expolygon, grep !$_->is_solid, @surfaces ],
|
|
|
|
// red_expolygons => [ map $_->expolygon, grep $_->is_solid, @surfaces ],
|
|
|
|
// );
|
2016-10-27 15:03:57 +00:00
|
|
|
}
|
2016-11-02 09:47:00 +00:00
|
|
|
|
2017-11-09 09:48:06 +00:00
|
|
|
for (const Surface &surface : surfaces) {
|
2016-11-02 09:47:00 +00:00
|
|
|
if (surface.surface_type == stInternalVoid)
|
|
|
|
continue;
|
2018-09-11 12:04:47 +00:00
|
|
|
InfillPattern fill_pattern = layerm.region()->config().fill_pattern.value;
|
2016-11-02 09:47:00 +00:00
|
|
|
double density = fill_density;
|
|
|
|
FlowRole role = (surface.surface_type == stTop) ? frTopSolidInfill :
|
|
|
|
(surface.is_solid() ? frSolidInfill : frInfill);
|
|
|
|
bool is_bridge = layerm.layer()->id() > 0 && surface.is_bridge();
|
2016-10-27 15:03:57 +00:00
|
|
|
|
2016-11-02 09:47:00 +00:00
|
|
|
if (surface.is_solid()) {
|
2016-11-08 08:59:25 +00:00
|
|
|
density = 100.;
|
2016-11-02 09:47:00 +00:00
|
|
|
fill_pattern = (surface.is_external() && ! is_bridge) ?
|
2019-02-22 14:25:35 +00:00
|
|
|
(surface.is_top() ? layerm.region()->config().top_fill_pattern.value : layerm.region()->config().bottom_fill_pattern.value) :
|
2016-11-02 09:47:00 +00:00
|
|
|
ipRectilinear;
|
|
|
|
} else if (density <= 0)
|
|
|
|
continue;
|
2016-10-27 15:03:57 +00:00
|
|
|
|
|
|
|
// get filler object
|
2016-11-03 06:18:27 +00:00
|
|
|
std::unique_ptr<Fill> f = std::unique_ptr<Fill>(Fill::new_from_type(fill_pattern));
|
2016-11-02 09:47:00 +00:00
|
|
|
f->set_bounding_box(layerm.layer()->object()->bounding_box());
|
2016-10-27 15:03:57 +00:00
|
|
|
|
|
|
|
// calculate the actual flow we'll be using for this infill
|
2016-11-02 09:47:00 +00:00
|
|
|
coordf_t h = (surface.thickness == -1) ? layerm.layer()->height : surface.thickness;
|
|
|
|
Flow flow = layerm.region()->flow(
|
|
|
|
role,
|
|
|
|
h,
|
|
|
|
is_bridge || f->use_bridge_flow(), // bridge flow?
|
|
|
|
layerm.layer()->id() == 0, // first layer?
|
|
|
|
-1, // auto width
|
|
|
|
*layerm.layer()->object()
|
2016-10-27 15:03:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// calculate flow spacing for infill pattern generation
|
2016-11-02 09:47:00 +00:00
|
|
|
bool using_internal_flow = false;
|
|
|
|
if (! surface.is_solid() && ! is_bridge) {
|
2016-10-27 15:03:57 +00:00
|
|
|
// it's internal infill, so we can calculate a generic flow spacing
|
|
|
|
// for all layers, for avoiding the ugly effect of
|
|
|
|
// misaligned infill on first layer because of different extrusion width and
|
|
|
|
// layer height
|
2016-11-02 09:47:00 +00:00
|
|
|
Flow internal_flow = layerm.region()->flow(
|
|
|
|
frInfill,
|
2018-09-11 12:04:47 +00:00
|
|
|
layerm.layer()->object()->config().layer_height.value, // TODO: handle infill_every_layers?
|
2016-11-02 09:47:00 +00:00
|
|
|
false, // no bridge
|
|
|
|
false, // no first layer
|
|
|
|
-1, // auto width
|
|
|
|
*layerm.layer()->object()
|
2016-10-27 15:03:57 +00:00
|
|
|
);
|
2016-11-02 09:47:00 +00:00
|
|
|
f->spacing = internal_flow.spacing();
|
2016-11-03 23:55:43 +00:00
|
|
|
using_internal_flow = true;
|
2016-10-27 15:03:57 +00:00
|
|
|
} else {
|
2016-11-02 09:47:00 +00:00
|
|
|
f->spacing = flow.spacing();
|
2016-10-27 15:03:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 09:47:00 +00:00
|
|
|
double link_max_length = 0.;
|
|
|
|
if (! is_bridge) {
|
2016-11-03 23:55:43 +00:00
|
|
|
#if 0
|
2018-09-11 12:04:47 +00:00
|
|
|
link_max_length = layerm.region()->config().get_abs_value(surface.is_external() ? "external_fill_link_max_length" : "fill_link_max_length", flow.spacing());
|
2016-11-02 09:47:00 +00:00
|
|
|
// printf("flow spacing: %f, is_external: %d, link_max_length: %lf\n", flow.spacing(), int(surface.is_external()), link_max_length);
|
2016-11-03 23:55:43 +00:00
|
|
|
#else
|
|
|
|
if (density > 80.) // 80%
|
|
|
|
link_max_length = 3. * f->spacing;
|
2016-11-02 15:55:56 +00:00
|
|
|
#endif
|
2016-11-03 23:55:43 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 09:47:00 +00:00
|
|
|
f->layer_id = layerm.layer()->id();
|
|
|
|
f->z = layerm.layer()->print_z;
|
2018-09-11 12:04:47 +00:00
|
|
|
f->angle = float(Geometry::deg2rad(layerm.region()->config().fill_angle.value));
|
2016-10-27 15:03:57 +00:00
|
|
|
// Maximum length of the perimeter segment linking two infill lines.
|
2019-06-20 14:15:09 +00:00
|
|
|
f->link_max_length = (coord_t)scale_(link_max_length);
|
2016-10-27 15:03:57 +00:00
|
|
|
// Used by the concentric infill pattern to clip the loops to create extrusion paths.
|
2019-06-20 14:15:09 +00:00
|
|
|
f->loop_clipping = coord_t(scale_(flow.nozzle_diameter) * LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER);
|
2016-11-02 09:47:00 +00:00
|
|
|
// f->layer_height = h;
|
|
|
|
|
2016-10-27 15:03:57 +00:00
|
|
|
// apply half spacing using this flow's own spacing and generate infill
|
2016-11-02 09:47:00 +00:00
|
|
|
FillParams params;
|
2019-06-20 14:15:09 +00:00
|
|
|
params.density = float(0.01 * density);
|
2016-11-08 08:59:25 +00:00
|
|
|
// params.dont_adjust = true;
|
|
|
|
params.dont_adjust = false;
|
2016-11-02 09:47:00 +00:00
|
|
|
Polylines polylines = f->fill_surface(&surface, params);
|
|
|
|
if (polylines.empty())
|
|
|
|
continue;
|
2016-10-27 15:03:57 +00:00
|
|
|
|
|
|
|
// calculate actual flow from spacing (which might have been adjusted by the infill
|
|
|
|
// pattern generator)
|
2016-11-02 09:47:00 +00:00
|
|
|
if (using_internal_flow) {
|
2016-10-27 15:03:57 +00:00
|
|
|
// if we used the internal flow we're not doing a solid infill
|
|
|
|
// so we can safely ignore the slight variation that might have
|
|
|
|
// been applied to $f->flow_spacing
|
|
|
|
} else {
|
2019-06-20 14:15:09 +00:00
|
|
|
flow = Flow::new_from_spacing(f->spacing, flow.nozzle_diameter, (float)h, is_bridge || f->use_bridge_flow());
|
2016-10-27 15:03:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 14:51:47 +00:00
|
|
|
// Save into layer.
|
|
|
|
auto *eec = new ExtrusionEntityCollection();
|
|
|
|
out.entities.push_back(eec);
|
|
|
|
// Only concentric fills are not sorted.
|
|
|
|
eec->no_sort = f->no_sort();
|
|
|
|
extrusion_entities_append_paths(
|
2018-11-02 19:45:23 +00:00
|
|
|
eec->entities, std::move(polylines),
|
2016-11-23 14:51:47 +00:00
|
|
|
is_bridge ?
|
|
|
|
erBridgeInfill :
|
|
|
|
(surface.is_solid() ?
|
|
|
|
((surface.surface_type == stTop) ? erTopSolidInfill : erSolidInfill) :
|
|
|
|
erInternalInfill),
|
|
|
|
flow.mm3_per_mm(), flow.width, flow.height);
|
2016-10-27 15:03:57 +00:00
|
|
|
}
|
2016-11-23 14:51:47 +00:00
|
|
|
|
2016-10-27 15:03:57 +00:00
|
|
|
// add thin fill regions
|
|
|
|
// thin_fills are of C++ Slic3r::ExtrusionEntityCollection, perl type Slic3r::ExtrusionPath::Collection
|
|
|
|
// Unpacks the collection, creates multiple collections per path.
|
|
|
|
// The path type could be ExtrusionPath, ExtrusionLoop or ExtrusionEntityCollection.
|
|
|
|
// Why the paths are unpacked?
|
2017-11-09 09:48:06 +00:00
|
|
|
for (const ExtrusionEntity *thin_fill : layerm.thin_fills.entities) {
|
2016-11-02 09:47:00 +00:00
|
|
|
ExtrusionEntityCollection &collection = *(new ExtrusionEntityCollection());
|
|
|
|
out.entities.push_back(&collection);
|
2017-11-09 09:48:06 +00:00
|
|
|
collection.entities.push_back(thin_fill->clone());
|
2016-10-27 15:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Slic3r
|