From 4409743ea4ee152b07a54527c8ab2e71af0bd74e Mon Sep 17 00:00:00 2001
From: PavelMikus <pavel.mikus.mail@seznam.cz>
Date: Wed, 19 Apr 2023 13:32:24 +0200
Subject: [PATCH] Fixed wrong usage of thickPolyline, added description of what
 it should actually contain

---
 src/libslic3r/Fill/FillEnsuring.cpp | 18 +++++++++++++-----
 src/libslic3r/Polyline.hpp          |  3 +++
 src/libslic3r/PrintObject.cpp       |  6 ++++--
 3 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/src/libslic3r/Fill/FillEnsuring.cpp b/src/libslic3r/Fill/FillEnsuring.cpp
index a12fd5948..c58188d0d 100644
--- a/src/libslic3r/Fill/FillEnsuring.cpp
+++ b/src/libslic3r/Fill/FillEnsuring.cpp
@@ -168,7 +168,7 @@ ThickPolylines FillEnsuring::fill_surface_arachne(const Surface *surface, const
                                             polygon_sections[section_idx].end());
     }
 
-    double squared_distance_limit_reconnection = 4 * scaled_spacing * scaled_spacing;
+    double squared_distance_limit_reconnection = 4 * double(scaled_spacing) * double(scaled_spacing);
 
     Polygons reconstructed_area{};
     // reconstruct polygon from polygon sections
@@ -185,7 +185,9 @@ ThickPolylines FillEnsuring::fill_surface_arachne(const Surface *surface, const
             for (TracedPoly &traced_poly : current_traced_polys) {
                 auto maybe_first_overlap = std::upper_bound(polygon_slice.begin(), polygon_slice.end(), traced_poly.lows.back(),
                                                             [](const Point &low, const Line &seg) { return seg.b.y() < low.y(); });
-
+                if (maybe_first_overlap != polygon_slice.begin()) {
+                    maybe_first_overlap--;
+                }
                 if (maybe_first_overlap != polygon_slice.end() && // segment exists
                     segments_overlap(traced_poly.lows.back().y(), traced_poly.highs.back().y(), maybe_first_overlap->a.y(),
                                      maybe_first_overlap->b.y())) // segment is overlapping
@@ -252,22 +254,27 @@ ThickPolylines FillEnsuring::fill_surface_arachne(const Surface *surface, const
             for (ThickPolyline &traced_path : current_traced_paths) {
                 auto maybe_overlap = std::upper_bound(polygon_slice.begin(), polygon_slice.end(), traced_path.last_point(),
                                                       [](const Point &low, const Line &seg) { return seg.a.y() < low.y(); });
-                bool segment_added = false;
-                if (maybe_overlap != polygon_slice.begin())
+                if (maybe_overlap != polygon_slice.begin()) {
                     maybe_overlap--;
+                }
+                bool segment_added = false;
                 while (!segment_added && maybe_overlap != polygon_slice.end()) {
                     if ((traced_path.last_point() - maybe_overlap->a).cast<double>().squaredNorm() < 
                                 squared_distance_limit_reconnection) {
+                        traced_path.width.push_back(scaled_spacing);
                         traced_path.points.push_back(maybe_overlap->a);
                         traced_path.width.push_back(scaled_spacing);
+                        traced_path.width.push_back(scaled_spacing);
                         traced_path.points.push_back(maybe_overlap->b);
                         traced_path.width.push_back(scaled_spacing);
                         used_segments.insert(&(*maybe_overlap));
                         segment_added = true;
                     } else if ((traced_path.last_point() - maybe_overlap->b).cast<double>().squaredNorm() <
                                squared_distance_limit_reconnection) {
+                        traced_path.width.push_back(scaled_spacing);
                         traced_path.points.push_back(maybe_overlap->b);
                         traced_path.width.push_back(scaled_spacing);
+                        traced_path.width.push_back(scaled_spacing);
                         traced_path.points.push_back(maybe_overlap->a);
                         traced_path.width.push_back(scaled_spacing);
                         used_segments.insert(&(*maybe_overlap));
@@ -287,13 +294,14 @@ ThickPolylines FillEnsuring::fill_surface_arachne(const Surface *surface, const
                                                       [](const ThickPolyline &tp) { return tp.empty(); }),
                                        current_traced_paths.end());
 
-            for (const auto &segment : polygon_slice) {
+            for (const Line &segment : polygon_slice) {
                 if (used_segments.find(&segment) == used_segments.end()) {
                     ThickPolyline &new_path = current_traced_paths.emplace_back();
                     new_path.points.push_back(segment.a);
                     new_path.width.push_back(scaled_spacing);
                     new_path.points.push_back(segment.b);
                     new_path.width.push_back(scaled_spacing);
+                    new_path.endpoints = {true,true};
                 }
             }
         }
diff --git a/src/libslic3r/Polyline.hpp b/src/libslic3r/Polyline.hpp
index 8766c6d86..703e50cfa 100644
--- a/src/libslic3r/Polyline.hpp
+++ b/src/libslic3r/Polyline.hpp
@@ -206,6 +206,9 @@ struct ThickPolyline {
     void start_at_index(int index);
 
     Points                  points;
+    // vector of startpoint width and endpoint width of each line segment. The size should be always (points.size()-1) * 2
+    // e.g. let four be points a,b,c,d. that are three lines ab, bc, cd. for each line, there should be start width, so the width vector is:
+    // w(a), w(b), w(b), w(c), w(c), w(d)
     std::vector<coordf_t>   width;
     std::pair<bool,bool>    endpoints { false, false };
 };
diff --git a/src/libslic3r/PrintObject.cpp b/src/libslic3r/PrintObject.cpp
index 54fba176c..6a9bd0aae 100644
--- a/src/libslic3r/PrintObject.cpp
+++ b/src/libslic3r/PrintObject.cpp
@@ -2122,8 +2122,10 @@ void PrintObject::bridge_over_infill()
                 std::unordered_set<const Line *> used_segments;
                 for (TracedPoly &traced_poly : current_traced_polys) {
                     auto maybe_first_overlap = std::upper_bound(polygon_slice.begin(), polygon_slice.end(), traced_poly.lows.back(),
-                                                                [](const Point &low, const Line &seg) { return seg.b.y() > low.y(); });
-
+                                                                [](const Point &low, const Line &seg) { return seg.b.y() < low.y(); });
+                    if (maybe_first_overlap != polygon_slice.begin()) {
+                        maybe_first_overlap--;
+                    }
                     if (maybe_first_overlap != polygon_slice.end() && // segment exists
                         segments_overlap(traced_poly.lows.back().y(), traced_poly.highs.back().y(), maybe_first_overlap->a.y(),
                                          maybe_first_overlap->b.y())) // segment is overlapping