diff --git a/xs/src/libslic3r/MotionPlanner.cpp b/xs/src/libslic3r/MotionPlanner.cpp index 82108afd9..b562a2832 100644 --- a/xs/src/libslic3r/MotionPlanner.cpp +++ b/xs/src/libslic3r/MotionPlanner.cpp @@ -226,6 +226,9 @@ MotionPlannerGraph::find_node(const Point &point) const void MotionPlannerGraph::shortest_path(size_t from, size_t to, Polyline* polyline) { + // this prevents a crash in case for some reason we got here with an empty adjacency list + if (this->adjacency_list.empty()) return; + const weight_t max_weight = std::numeric_limits::infinity(); std::vector min_distance; diff --git a/xs/src/libslic3r/Point.cpp b/xs/src/libslic3r/Point.cpp index 2515d41a6..c04ff71b3 100644 --- a/xs/src/libslic3r/Point.cpp +++ b/xs/src/libslic3r/Point.cpp @@ -107,10 +107,13 @@ Point::nearest_point_index(const PointPtrs &points) const return this->nearest_point_index(p); } -void +bool Point::nearest_point(const Points &points, Point* point) const { - *point = points.at(this->nearest_point_index(points)); + int idx = this->nearest_point_index(points); + if (idx == -1) return false; + *point = points.at(idx); + return true; } double diff --git a/xs/src/libslic3r/Point.hpp b/xs/src/libslic3r/Point.hpp index 28d216df2..f23f14a90 100644 --- a/xs/src/libslic3r/Point.hpp +++ b/xs/src/libslic3r/Point.hpp @@ -37,7 +37,7 @@ class Point int nearest_point_index(const Points &points) const; int nearest_point_index(const PointConstPtrs &points) const; int nearest_point_index(const PointPtrs &points) const; - void nearest_point(const Points &points, Point* point) const; + bool nearest_point(const Points &points, Point* point) const; double distance_to(const Point &point) const; double distance_to(const Line &line) const; double ccw(const Point &p1, const Point &p2) const;