Polygons ExtrusionLoop::grow() const { if (this->paths.empty()) return Polygons(); // collect all the path widths std::vector<float> widths; for (ExtrusionPaths::const_iterator path = this->paths.begin(); path != this->paths.end(); ++path) widths.push_back(path->width); // grow this polygon with the minimum common width // (this ensures vertices are grown correctly, which doesn't happen if we just // union the paths grown individually) const float min_width = *std::min_element(widths.begin(), widths.end()); const Polygon p = this->polygon(); Polygons pp = diff( offset(p, +scale_(min_width/2)), offset(p, -scale_(min_width/2)) ); // if we have thicker segments, grow them if (min_width != *std::max_element(widths.begin(), widths.end())) { for (ExtrusionPaths::const_iterator path = this->paths.begin(); path != this->paths.end(); ++path) append_to(pp, path->grow()); } return union_(pp); }
std::string GCode::change_layer(const Layer &layer) { this->layer = &layer; this->layer_index++; this->first_layer = (layer.id() == 0); // avoid computing islands and overhangs if they're not needed if (this->config.avoid_crossing_perimeters) { ExPolygons islands; union_(layer.slices, &islands, true); this->avoid_crossing_perimeters.init_layer_mp(islands); } std::string gcode; if (this->layer_count > 0) { gcode += this->writer.update_progress(this->layer_index, this->layer_count); } coordf_t z = layer.print_z + this->config.z_offset.value; // in unscaled coordinates if (EXTRUDER_CONFIG(retract_layer_change) && this->writer.will_move_z(z)) { gcode += this->retract(); } { std::ostringstream comment; comment << "move to next layer (" << this->layer_index << ")"; gcode += this->writer.travel_to_z(z, comment.str()); } // forget last wiping path as wiping after raising Z is pointless this->wipe.reset_path(); return gcode; }
Slic3r::Polygons union_(const Slic3r::Polygons &subject, bool safety_offset) { Polygons pp; union_(subject, &pp, safety_offset); return pp; }
Slic3r::ExPolygons union_ex(const Slic3r::Polygons &subject, bool safety_offset) { ExPolygons expp; union_(subject, &expp, safety_offset); return expp; }
void simplify_polygons(const Slic3r::Polygons &subject, Slic3r::ExPolygons* retval, bool preserve_collinear) { PROFILE_FUNC(); if (!preserve_collinear) { Polygons polygons; simplify_polygons(subject, &polygons, preserve_collinear); union_(polygons, retval); return; } // convert into Clipper polygons ClipperLib::Paths input_subject; Slic3rMultiPoints_to_ClipperPaths(subject, &input_subject); ClipperLib::PolyTree polytree; ClipperLib::Clipper c; c.PreserveCollinear(true); c.StrictlySimple(true); c.AddPaths(input_subject, ClipperLib::ptSubject, true); c.Execute(ClipperLib::ctUnion, polytree, ClipperLib::pftNonZero, ClipperLib::pftNonZero); // convert into ExPolygons PolyTreeToExPolygons(polytree, retval); }
typename __combined<set<T>, U, Types...>::type set<T>::union_(U &&other, Types &&... others) const { typename __combined<set<T>, U, Types...>::type tmp = union_(std::forward<Types...>(others)...); tmp.data->insert(other.begin(), other.end()); return tmp; }
Slic3r::Polygons union_(const Slic3r::ExPolygons &subject1, const Slic3r::ExPolygons &subject2, bool safety_offset) { Polygons pp; for (Slic3r::ExPolygons::const_iterator it = subject1.begin(); it != subject1.end(); ++it) { Polygons spp = *it; pp.insert(pp.end(), spp.begin(), spp.end()); } for (Slic3r::ExPolygons::const_iterator it = subject2.begin(); it != subject2.end(); ++it) { Polygons spp = *it; pp.insert(pp.end(), spp.begin(), spp.end()); } Polygons retval; union_(pp, &retval, safety_offset); return retval; }
void union_persons(int n, char (*persons)[MAX_PERSON_NAME_LENGTH], int *ancestors) { int i, a_idx, b_idx; char a_name[MAX_PERSON_NAME_LENGTH], b_name[MAX_PERSON_NAME_LENGTH]; for (i = 0; i < n; i++) { scanf("%s %s", a_name, b_name); /* TODO Error handling here. */ a_idx = find_person(a_name, persons); b_idx = find_person(b_name, persons); union_(a_idx, b_idx, ancestors); } }
set<typename __combined<T, U>::type> set<T>:: operator|(set<U> const &other) const { return union_(other); }
none_type set<T>::update(Types &&... others) { *this = union_(std::forward<Types>(others)...); return {}; }
void union_(const Slic3r::Polygons &subject1, const Slic3r::Polygons &subject2, Slic3r::Polygons* retval, bool safety_offset) { Polygons pp = subject1; pp.insert(pp.end(), subject2.begin(), subject2.end()); union_(pp, retval, safety_offset); }
void BridgeDetector::coverage(double angle, Polygons* coverage) const { // Clone our expolygon and rotate it so that we work with vertical lines. ExPolygon expolygon = this->expolygon; expolygon.rotate(PI/2.0 - angle, Point(0,0)); /* Outset the bridge expolygon by half the amount we used for detecting anchors; we'll use this one to generate our trapezoids and be sure that their vertices are inside the anchors and not on their contours leading to false negatives. */ ExPolygons grown; offset(expolygon, &grown, this->extrusion_width/2.0); // Compute trapezoids according to a vertical orientation Polygons trapezoids; for (ExPolygons::const_iterator it = grown.begin(); it != grown.end(); ++it) it->get_trapezoids2(&trapezoids, PI/2.0); // get anchors, convert them to Polygons and rotate them too Polygons anchors; for (ExPolygons::const_iterator anchor = this->_anchors.begin(); anchor != this->_anchors.end(); ++anchor) { Polygons pp = *anchor; for (Polygons::iterator p = pp.begin(); p != pp.end(); ++p) p->rotate(PI/2.0 - angle, Point(0,0)); anchors.insert(anchors.end(), pp.begin(), pp.end()); } Polygons covered; for (Polygons::const_iterator trapezoid = trapezoids.begin(); trapezoid != trapezoids.end(); ++trapezoid) { Lines lines = trapezoid->lines(); Lines supported; intersection(lines, anchors, &supported); // not nice, we need a more robust non-numeric check for (size_t i = 0; i < supported.size(); ++i) { if (supported[i].length() < this->extrusion_width) { supported.erase(supported.begin() + i); i--; } } if (supported.size() >= 2) covered.push_back(*trapezoid); } // merge trapezoids and rotate them back Polygons _coverage; union_(covered, &_coverage); for (Polygons::iterator p = _coverage.begin(); p != _coverage.end(); ++p) p->rotate(-(PI/2.0 - angle), Point(0,0)); // intersect trapezoids with actual bridge area to remove extra margins // and append it to result intersection(_coverage, this->expolygon, coverage); /* if (0) { my @lines = map @{$_->lines}, @$trapezoids; $_->rotate(-(PI/2 - $angle), [0,0]) for @lines; require "Slic3r/SVG.pm"; Slic3r::SVG::output( "coverage_" . rad2deg($angle) . ".svg", expolygons => [$self->expolygon], green_expolygons => $self->_anchors, red_expolygons => $coverage, lines => \@lines, ); } */ }
void SVGExport::writeSVG(const std::string &outputfile) { // align to origin taking raft into account BoundingBoxf3 bb = this->mesh.bounding_box(); if (this->config.raft_layers > 0) { bb.min.x -= this->config.raft_offset.value; bb.min.y -= this->config.raft_offset.value; bb.max.x += this->config.raft_offset.value; bb.max.y += this->config.raft_offset.value; } this->mesh.translate(-bb.min.x, -bb.min.y, -bb.min.z); // align to origin bb.translate(-bb.min.x, -bb.min.y, -bb.min.z); // align to origin const Sizef3 size = bb.size(); // if we are generating a raft, first_layer_height will not affect mesh slicing const float lh = this->config.layer_height.value; const float first_lh = this->config.first_layer_height.value; // generate the list of Z coordinates for mesh slicing // (we slice each layer at half of its thickness) std::vector<float> slice_z, layer_z; { const float first_slice_lh = (this->config.raft_layers > 0) ? lh : first_lh; slice_z.push_back(first_slice_lh/2); layer_z.push_back(first_slice_lh); } while (layer_z.back() + lh/2 <= this->mesh.stl.stats.max.z) { slice_z.push_back(layer_z.back() + lh/2); layer_z.push_back(layer_z.back() + lh); } // perform the slicing std::vector<ExPolygons> layers; TriangleMeshSlicer(&this->mesh).slice(slice_z, &layers); // generate a solid raft if requested if (this->config.raft_layers > 0) { ExPolygons raft = offset_ex(layers.front(), scale_(this->config.raft_offset)); for (int i = this->config.raft_layers; i >= 1; --i) { layer_z.insert(layer_z.begin(), first_lh + lh * (i-1)); layers.insert(layers.begin(), raft); } // prepend total raft height to all sliced layers for (int i = this->config.raft_layers; i < layer_z.size(); ++i) layer_z[i] += first_lh + lh * (this->config.raft_layers-1); } // generate support material std::vector<Points> support_material(layers.size()); if (this->config.support_material) { // generate a grid of points according to the configured spacing, // covering the entire object bounding box Points support_material_points; for (coordf_t x = bb.min.x; x <= bb.max.x; x += this->config.support_material_spacing) { for (coordf_t y = bb.min.y; y <= bb.max.y; y += this->config.support_material_spacing) { support_material_points.push_back(Point(scale_(x), scale_(y))); } } // check overhangs, starting from the upper layer, and detect which points apply // to each layer ExPolygons overhangs; for (int i = layer_z.size()-1; i >= 0; --i) { overhangs = diff_ex(union_(overhangs, layers[i+1]), layers[i]); for (Points::const_iterator it = support_material_points.begin(); it != support_material_points.end(); ++it) { for (ExPolygons::const_iterator e = overhangs.begin(); e != overhangs.end(); ++e) { if (e->contains(*it)) { support_material[i].push_back(*it); break; } } } } } double support_material_radius = this->config.support_material_extrusion_width.get_abs_value(this->config.layer_height)/2; FILE* f = fopen(outputfile.c_str(), "w"); fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n" "<svg width=\"%f\" height=\"%f\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:slic3r=\"http://slic3r.org/namespaces/slic3r\" viewport-fill=\"black\">\n" "<!-- Generated using Slic3r %s http://slic3r.org/ -->\n" , size.x, size.y, SLIC3R_VERSION); for (size_t i = 0; i < layer_z.size(); ++i) { fprintf(f, "\t<g id=\"layer%zu\" slic3r:z=\"%0.4f\">\n", i, layer_z[i]); for (ExPolygons::const_iterator it = layers[i].begin(); it != layers[i].end(); ++it) { std::string pd; Polygons pp = *it; for (Polygons::const_iterator mp = pp.begin(); mp != pp.end(); ++mp) { std::ostringstream d; d << "M "; for (Points::const_iterator p = mp->points.begin(); p != mp->points.end(); ++p) { d << unscale(p->x) << " "; d << unscale(p->y) << " "; } d << "z"; pd += d.str() + " "; } fprintf(f,"\t\t<path d=\"%s\" style=\"fill: %s; stroke: %s; stroke-width: %s; fill-type: evenodd\" slic3r:area=\"%0.4f\" />\n", pd.c_str(), "white", "black", "0", unscale(unscale(it->area())) ); } for (Points::const_iterator it = support_material[i].begin(); it != support_material[i].end(); ++it) { fprintf(f,"\t\t<circle cx=\"%f\" cy=\"%f\" r=\"%f\" stroke-width=\"0\" fill=\"white\" slic3r:type=\"support\" />\n", unscale(it->x), unscale(it->y), support_material_radius ); } fprintf(f,"\t</g>\n"); } fprintf(f,"</svg>\n"); }
/// The LayerRegion 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. /// Fills an array of ExtrusionPathCollection objects containing the infills generated now /// and the thin fills generated by generate_perimeters(). void LayerRegion::make_fill() { this->fills.clear(); const double fill_density = this->region()->config.fill_density; const Flow infill_flow = this->flow(frInfill); const Flow solid_infill_flow = this->flow(frSolidInfill); const Flow top_solid_infill_flow = this->flow(frTopSolidInfill); const coord_t perimeter_spacing = this->flow(frPerimeter).scaled_spacing(); SurfaceCollection 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()?) { Polygons polygons_bridged; polygons_bridged.reserve(this->fill_surfaces.surfaces.size()); for (Surfaces::const_iterator it = this->fill_surfaces.surfaces.begin(); it != this->fill_surfaces.surfaces.end(); ++it) if (it->is_bridge() && it->bridge_angle >= 0) append_to(polygons_bridged, (Polygons)*it); // group surfaces by distinct properties (equal surface_type, thickness, thickness_layers, bridge_angle) // group is of type SurfaceCollection // FIXME: Use some smart heuristics to merge similar surfaces to eliminate tiny regions. std::vector<SurfacesConstPtr> groups; this->fill_surfaces.group(&groups); // merge compatible solid 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) std::vector<SurfaceGroupAttrib> group_attrib(groups.size()); for (size_t i = 0; i < groups.size(); ++i) { const Surface &surface = *groups[i].front(); // we can only merge solid non-bridge surfaces, so discard // non-solid or bridge surfaces if (!surface.is_solid() || surface.is_bridge()) continue; group_attrib[i].is_solid = true; group_attrib[i].fw = (surface.is_top()) ? top_solid_infill_flow.width : solid_infill_flow.width; group_attrib[i].pattern = surface.is_top() ? this->region()->config.top_infill_pattern.value : surface.is_bottom() ? this->region()->config.bottom_infill_pattern.value : ipRectilinear; } // Loop through solid groups, find compatible groups and append them to this one. for (size_t i = 0; i < groups.size(); ++i) { 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 append_to(groups[i], groups[j]); groups.erase(groups.begin() + j); group_attrib.erase(group_attrib.begin() + j); } else { ++j; } } } } // Give priority to oriented 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<SurfacesConstPtr>::const_iterator it_group = groups.begin(); it_group != groups.end(); ++ it_group) { const SurfacesConstPtr &group = *it_group; const bool is_oriented_bridge = group.front()->is_bridge() && group.front()->bridge_angle >= 0; if (is_oriented_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(group), true); // Subtract surfaces having a defined bridge_angle from any other, use a safety offset. if (!is_oriented_bridge && !polygons_bridged.empty()) union_p = diff(union_p, polygons_bridged, true); // subtract any other surface already processed //FIXME Vojtech: Because the bridge surfaces came first, they are subtracted twice! surfaces.append( diff_ex(union_p, to_polygons(surfaces), true), *group.front() // template ); } } } // 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 { 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 = (Polygons)surfaces; Polygons collapsed = diff( surfaces_polygons, offset2(surfaces_polygons, -distance_between_surfaces/2, +distance_between_surfaces/2), true ); Polygons to_subtract; surfaces.filter_by_type((stInternal | stVoid), &to_subtract); append_to(to_subtract, collapsed); surfaces.append( intersection_ex( offset(collapsed, distance_between_surfaces), to_subtract, true ), (stInternal | stSolid) ); } if (false) { // 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 ], // ); } for (Surfaces::const_iterator surface_it = surfaces.surfaces.begin(); surface_it != surfaces.surfaces.end(); ++surface_it) { const Surface &surface = *surface_it; if (surface.surface_type == (stInternal | stVoid)) continue; InfillPattern fill_pattern = this->region()->config.fill_pattern.value; double density = fill_density; FlowRole role = (surface.is_top()) ? frTopSolidInfill : surface.is_solid() ? frSolidInfill : frInfill; const bool is_bridge = this->layer()->id() > 0 && surface.is_bridge(); if (surface.is_solid()) { density = 100.; fill_pattern = (surface.is_top()) ? this->region()->config.top_infill_pattern.value : (surface.is_bottom() && !is_bridge) ? this->region()->config.bottom_infill_pattern.value : ipRectilinear; } else if (density <= 0) continue; // get filler object #if SLIC3R_CPPVER >= 11 std::unique_ptr<Fill> f = std::unique_ptr<Fill>(Fill::new_from_type(fill_pattern)); #else std::auto_ptr<Fill> f = std::auto_ptr<Fill>(Fill::new_from_type(fill_pattern)); #endif // switch to rectilinear if this pattern doesn't support solid infill if (density > 99 && !f->can_solid()) #if SLIC3R_CPPVER >= 11 f = std::unique_ptr<Fill>(Fill::new_from_type(ipRectilinear)); #else f = std::auto_ptr<Fill>(Fill::new_from_type(ipRectilinear)); #endif f->bounding_box = this->layer()->object()->bounding_box(); // calculate the actual flow we'll be using for this infill coordf_t h = (surface.thickness == -1) ? this->layer()->height : surface.thickness; Flow flow = this->region()->flow( role, h, is_bridge || f->use_bridge_flow(), // bridge flow? this->layer()->id() == 0, // first layer? -1, // auto width *this->layer()->object() ); // calculate flow spacing for infill pattern generation bool using_internal_flow = false; if (!surface.is_solid() && !is_bridge) { // 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 Flow internal_flow = this->region()->flow( frInfill, h, // use the calculated surface thickness here for internal infill instead of the layer height to account for infill_every_layers false, // no bridge false, // no first layer -1, // auto width *this->layer()->object() ); f->min_spacing = internal_flow.spacing(); using_internal_flow = true; } else { f->min_spacing = flow.spacing(); } f->endpoints_overlap = scale_(this->region()->config.get_abs_value("infill_overlap", (unscale(perimeter_spacing) + (f->min_spacing))/2)); f->layer_id = this->layer()->id(); f->z = this->layer()->print_z; f->angle = Geometry::deg2rad(this->region()->config.fill_angle.value); // Maximum length of the perimeter segment linking two infill lines. f->link_max_length = (!is_bridge && density > 80) ? scale_(3 * f->min_spacing) : 0; // Used by the concentric infill pattern to clip the loops to create extrusion paths. f->loop_clipping = scale_(flow.nozzle_diameter) * LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER; // apply half spacing using this flow's own spacing and generate infill f->density = density/100; f->dont_adjust = false; /* std::cout << surface.expolygon.dump_perl() << std::endl << " layer_id: " << f->layer_id << " z: " << f->z << " angle: " << f->angle << " min-spacing: " << f->min_spacing << " endpoints_overlap: " << f->endpoints_overlap << std::endl << std::endl; */ Polylines polylines = f->fill_surface(surface); if (polylines.empty()) continue; // calculate actual flow from spacing (which might have been adjusted by the infill // pattern generator) if (using_internal_flow) { // 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->spacing() } else { flow = Flow::new_from_spacing(f->spacing(), flow.nozzle_diameter, h, is_bridge || f->use_bridge_flow()); } // Save into layer. ExtrusionEntityCollection* coll = new ExtrusionEntityCollection(); coll->no_sort = f->no_sort(); this->fills.entities.push_back(coll); { ExtrusionRole role; if (is_bridge) { role = erBridgeInfill; } else if (surface.is_solid()) { role = (surface.is_top()) ? erTopSolidInfill : erSolidInfill; } else { role = erInternalInfill; } ExtrusionPath templ(role); templ.mm3_per_mm = flow.mm3_per_mm(); templ.width = flow.width; templ.height = flow.height; coll->append(STDMOVE(polylines), templ); } } // add thin fill regions // thin_fills are of C++ Slic3r::ExtrusionEntityCollection, perl type Slic3r::ExtrusionPath::Collection // Unpacks the collection, creates multiple collections per path so that they will // be individually included in the nearest neighbor search. // The path type could be ExtrusionPath, ExtrusionLoop or ExtrusionEntityCollection. for (ExtrusionEntitiesPtr::const_iterator thin_fill = this->thin_fills.entities.begin(); thin_fill != this->thin_fills.entities.end(); ++ thin_fill) { ExtrusionEntityCollection* coll = new ExtrusionEntityCollection(); this->fills.entities.push_back(coll); coll->append(**thin_fill); } }