Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PolygonTrimmer.cpp « libslic3r « src - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c4e06fc5847f6f67e55272207406ca578b35c62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "PolygonTrimmer.hpp"
#include "EdgeGrid.hpp"
#include "Geometry.hpp"

namespace Slic3r {

TrimmedLoop trim_loop(const Polygon &loop, const EdgeGrid::Grid &grid)
{
	assert(! loop.empty());
	assert(loop.size() >= 2);

	TrimmedLoop out;

	if (loop.size() >= 2) {

		struct Visitor {
			Visitor(const EdgeGrid::Grid &grid, const Slic3r::Point *pt_prev, const Slic3r::Point *pt_this) : grid(grid), pt_prev(pt_prev), pt_this(pt_this) {}

			bool operator()(coord_t iy, coord_t ix) {
				// Called with a row and colum of the grid cell, which is intersected by a line.
				auto cell_data_range = grid.cell_data_range(iy, ix);
				for (auto it_contour_and_segment = cell_data_range.first; it_contour_and_segment != cell_data_range.second; ++ it_contour_and_segment) {
					// End points of the line segment and their vector.
					auto segment = grid.segment(*it_contour_and_segment);
					if (Geometry::segments_intersect(segment.first, segment.second, *pt_prev, *pt_this)) {
						// The two segments intersect. Add them to the output.
					}
				}
				// Continue traversing the grid along the edge.
				return true;
			}

			const EdgeGrid::Grid &grid;
			const Slic3r::Point  *pt_this;
			const Slic3r::Point  *pt_prev;
		} visitor(grid, &loop.points.back(), nullptr);

		for (const Point &pt_this : loop.points) {
			visitor.pt_this = &pt_this;
			grid.visit_cells_intersecting_line(*visitor.pt_prev, pt_this, visitor);
			visitor.pt_prev = &pt_this;
		}
	}

	return out;
}

std::vector<TrimmedLoop> trim_loops(const Polygons &loops, const EdgeGrid::Grid &grid)
{
	std::vector<TrimmedLoop> out;
	out.reserve(loops.size());
	for (const Polygon &loop : loops)
		out.emplace_back(trim_loop(loop, grid));
	return out;
}

}