Separate draw function

This commit is contained in:
Filip Sykala 2021-03-24 09:39:17 +01:00 committed by Lukas Matena
parent 60a0ac46c2
commit 942443429e
9 changed files with 123 additions and 16 deletions

View File

@ -222,4 +222,43 @@ std::tuple<double, double, double> LineUtils::get_param(const Linef &line)
double b = normal.y();
double c = -a * line.a.x() - b * line.a.y();
return {a, b, c};
}
void LineUtils::draw(SVG & svg,
const Line &line,
const char *color,
coordf_t stroke_width,
const char *name,
bool side_points,
const char *color_a,
const char *color_b)
{
svg.draw(line, color, stroke_width);
bool use_name = name != nullptr;
if (use_name) {
Point middle = line.a/2 + line.b/2;
svg.draw_text(middle, name, color);
}
if (side_points) {
std::string name_a = (use_name) ? "A" : (std::string("A_") + name);
std::string name_b = (use_name) ? "B" : (std::string("B_") + name);
svg.draw_text(line.a, name_a.c_str(), color_a);
svg.draw_text(line.b, name_b.c_str(), color_b);
}
}
void LineUtils::draw(SVG & svg,
const Lines &lines,
const char * color,
coordf_t stroke_width,
bool ord,
bool side_points,
const char * color_a,
const char * color_b)
{
for (const auto &line : lines) {
draw(svg, line, color, stroke_width,
(ord) ? std::to_string(&line - &lines.front()).c_str() : nullptr,
side_points, color_a, color_b);
}
}

View File

@ -3,7 +3,9 @@
#include <optional>
#include <tuple>
#include <string>
#include <libslic3r/Line.hpp>
#include <libslic3r/SVG.hpp>
namespace Slic3r::sla {
@ -81,6 +83,23 @@ public:
/// <returns>a, b, c</returns>
static std::tuple<double, double, double> get_param(const Line &line);
static std::tuple<double, double, double> get_param(const Linef &line);
static void draw(SVG & svg,
const Line &line,
const char *color = "gray",
coordf_t stroke_width = 0,
const char *name = nullptr,
bool side_points = false,
const char *color_a = "lightgreen",
const char *color_b = "lightblue");
static void draw(SVG & svg,
const Lines &lines,
const char *color = "gray",
coordf_t stroke_width = 0,
bool ord = false, // write order as text
bool side_points = false,
const char *color_a = "lightgreen",
const char *color_b = "lightblue");
};
} // namespace Slic3r::sla

View File

@ -63,4 +63,24 @@ bool PolygonUtils::is_ccw(const Polygon &polygon, const Point &center) {
prev = &point;
}
return true;
}
}
bool PolygonUtils::is_not_self_intersect(const Polygon &polygon,
const Point & center)
{
auto get_angle = [&center](const Point &point) {
Point diff_point = point - center;
return atan2(diff_point.y(), diff_point.x());
};
bool found_circle_end = false; // only one can be on polygon
double prev_angle = get_angle(polygon.points.back());
for (const Point &point : polygon.points) {
double angle = get_angle(point);
if (angle < prev_angle) {
if (found_circle_end) return false;
found_circle_end = true;
}
prev_angle = angle;
}
return true;
}

View File

@ -71,6 +71,14 @@ public:
/// <param name="center">center point inside polygon</param>
/// <returns>True when all points in polygon are CCW with center</returns>
static bool is_ccw(const Polygon &polygon, const Point &center);
/// <summary>
/// ! Only for polygon around point, like Voronoi diagram cell
/// </summary>
/// <param name="polygon">Polygon to check</param>
/// <param name="center">Center inside polygon, points create circle around center</param>
/// <returns>True when valid without self intersection otherwise FALSE</returns>
static bool is_not_self_intersect(const Polygon &polygon, const Point &center);
};
} // namespace Slic3r::sla
#endif // slic3r_SLA_SuppotstIslands_PolygonUtils_hpp_

View File

@ -16,6 +16,7 @@ public:
// factory method to iniciate config
static SampleConfig create(const SupportPointGenerator::Config &config)
{
// TODO: find valid params !!!!
SampleConfig result;
result.max_distance = 100. * config.head_diameter;
result.head_radius = config.head_diameter / 2;

View File

@ -13,7 +13,7 @@
#include <libslic3r/ClipperUtils.hpp> // allign
// comment to enable assert()
// comment definition of NDEBUG to enable assert()
// #define NDEBUG
#include <cassert>
@ -191,6 +191,13 @@ Slic3r::Points SampleIslandUtils::to_points(const SupportIslandPoints &support_p
return VectorUtils::transform(support_points, transform_func);
}
std::vector<Slic3r::Vec2f> SampleIslandUtils::to_points_f(const SupportIslandPoints &support_points)
{
std::function<Vec2f(const SupportIslandPoint &p)> transform_func =
[](const SupportIslandPoint &p) { return p.point.cast<float>(); };
return VectorUtils::transform(support_points, transform_func);
}
void SampleIslandUtils::align_samples(SupportIslandPoints &samples,
const ExPolygon & island,
const SampleConfig & config)
@ -250,8 +257,8 @@ coord_t SampleIslandUtils::align_once(SupportIslandPoints &samples,
break;
}
}
Point center = island_cell->centroid();
assert(island_cell != nullptr);
Point center = island_cell->centroid();
assert(is_points_in_distance(center, island_cell->points, config.max_distance));
#ifdef VISUALIZE_SAMPLE_ISLAND_UTILS_ALIGN_ONCE
svg.draw(polygon, "lightgray");

View File

@ -129,7 +129,11 @@ public:
const SampleConfig & config,
VoronoiGraph::ExPath &longest_path);
/// <summary>
/// Transform support point to slicer points
/// </summary>
static Slic3r::Points to_points(const SupportIslandPoints &support_points);
static std::vector<Vec2f> to_points_f(const SupportIslandPoints &support_points);
/// <summary>
/// keep same distances between support points

View File

@ -12,6 +12,10 @@
#include <libslic3r/VoronoiVisualUtils.hpp>
// comment definition of NDEBUG to enable assert()
// #define NDEBUG
#include <cassert>
//#define SLA_CELL_2_POLYGON_DEBUG
using namespace Slic3r::sla;
@ -144,6 +148,7 @@ std::optional<Slic3r::Line> VoronoiGraphUtils::to_line(
return Line(segment->a.cast<coord_t>(), segment->b.cast<coord_t>());
}
Slic3r::Polygon VoronoiGraphUtils::to_polygon(const Lines &lines,
const Point &center,
double maximal_distance,
@ -196,21 +201,10 @@ Slic3r::Polygon VoronoiGraphUtils::to_polygon(const Lines &lines,
points.push_back(p2);
}
Polygon polygon(points);
//if (!polygon.contains(center)) draw(polygon, lines, center);
assert(polygon.is_valid());
if (!polygon.contains(center)) {
SVG svg("bad_polygon.svg", {polygon.points});
svg.draw(polygon, "orange");
svg.draw(lines, "red");
int counter = 0;
for (auto &line : lines) {
++counter;
svg.draw_text(line.a, ("A"+std::to_string(counter)).c_str(), "lightgreen");
svg.draw_text(line.b, ("B" + std::to_string(counter)).c_str(), "lightblue");
}
svg.draw(center);
}
assert(polygon.contains(center));
assert(PolygonUtils::is_ccw(polygon, center));
assert(PolygonUtils::is_not_self_intersect(polygon, center));
return polygon;
}
@ -1063,3 +1057,13 @@ void VoronoiGraphUtils::draw(SVG & svg,
draw(svg, path.nodes, width, mainPathColor);
}
void VoronoiGraphUtils::draw(const Polygon &polygon,
const Lines & lines,
const Point & center)
{
SVG svg("Bad_polygon.svg", {polygon.points});
svg.draw(polygon, "orange");
LineUtils::draw(svg, lines, "red", 0., true, true);
svg.draw(center);
}

View File

@ -349,6 +349,11 @@ public: // draw function for debug
static void draw(SVG & svg,
const VoronoiGraph::ExPath &path,
coord_t width);
// draw polygon when convert from cell
static void draw(const Slic3r::Polygon &polygon,
const Slic3r::Lines & lines,
const Slic3r::Point & center);
};
} // namespace Slic3r::sla