Polygon Fill Algorithm

I am working on a utility for cutting mesh for 3D printing. In general, it should cut a 3d mesh model into 2d shapes (several polygons, possibly with holes) and fill them with tracks of a certain thickness using a specific template. These paths will be used to generate gcode commands for flashing a 3D printer.

There are various open source tools with the same goals written in python and perl. But my goal is to understand the slicer workflow and write my own tool in C or C ++.

So far I can get the outline of the slice and now I'm going to fill them with paths. The problem is that I did not find an effective algorithm for this. Schematic example of filling:

Can anyone advise how to create these fill paths? Thanks.


I am currently using the following algorithm:

  • Find the bounding box of the shape
  • Split bb vertically with lines (number of lines = bb.width / path.thickness)
  • Find the intersection points for the form and each line (there should be two points per line)
  • Build segments from these points offset from the border
  • Add segments to join the source segments together to form a line strip.
  • We are ready to generate gcode or draw a path

Simple infill algorithm

This is a simple and fast algorithm, but it does not work for concave polygons and polygons with holes. It also uses only one specified pattern.

+7
source share
3 answers

After some research time, I ended up with the following algorithm: enter image description here However, there is the possibility of optimization.

+2
source

In the approach below, a filling pattern will be created consisting of a single path (i.e. the filling nozzle will never be turned off, moved and turned on again) whenever possible.

After your step 4 (โ€œBuild segments from these points off-boundโ€), rotate each segment of the vertical line 2 or more points: the top and bottom end points plus (imagine your chart is drawn on a transparent place a sheet of paper with horizontal lines on the bottom and mark where the segments of the vertical line in the diagram intersect the horizontal lines on the paper).

Now we form a graph-weighted graph containing a vertex for each point, with an edge connecting two vertices when their corresponding points are less than or equal to one mesh unit. Also add edges between adjacent extreme points of the line segments and between adjacent lower points. Use the Euclidean distance between points for the weight of the edge. Finally, the magic part: find the minimum Hamiltonian weight path on this graph. This is a path that visits each vertex exactly once and has a minimum length. Limiting the minimum length ensures that the path never crosses itself, because if any two lines intersect, say, a line from a to b and a line from c to d, then it will always be possible to create a shorter common path by deleting these two lines and creating two new lines using another combination of endpoints (either --- c, and b --- d, or --- and b --- c). This is the path you fill.

Finding the Hamilton path (not to mention the Hamiltonian path of minimum weight) is an NP-hard problem, which is closely related to the more well-known problem of the salesman commander. Since many good accurate TSP solvers already exist (e.g. Concorde ), it would be wise to use one of them instead to find travel for travelers, and then simply remove one of the edges to create a short Hamiltonian path. (Even if you remove the heaviest edge, this will not necessarily lead to a Hamiltonian path of minimum length, since it may be that there are shorter paths that do not start and end at neighboring peaks, but we donโ€™t really care about the total length here, we you just need a path that visits all the peaks and does not cross itself.)

Unfortunately, the graph does not guarantee the presence of either a Hamiltonian trajectory or a tour of traveling sellers. (Obviously, they cannot exist if the graph is disabled, for example, but even connected graphs may not have one or both: for example, a graph with any vertex of degree 1 cannot have a TSP tour.) In this case, there is a TSP Solver, which you use, you can find tours that do not visit all the peaks, you can simply repeat this until all the peaks are covered. If this does not happen, I will return to the original algorithm.

+7
source

You can see this webpage about hatching algorithms for regions.

+2
source

All Articles