Generation of continuous hybrid zig-zag and contour paths for 3D printing

The generation of the printing paths is a decisive step in additive manufacturing (AM). There is a variety of patterns that offer different characteristics, but those that are strictly continuous become especially relevant in certain types of AM by extrusion, with materials like bioinks, carbon or clays, since they do not allow the retraction of the material and travelling movements result in the generation of artifacts. In this work, we present (1) a method that generates continuous paths to fill 2D polygons with a hybrid zig-zag and contour pattern with any direction and line separation, which extends an algorithm that decomposes the 2D area to be filled into convex areas, overcoming its limitations to generate less subpolygons in certain cases, (2) a method to join the subpolygon trajectories such that a continuous path that fills the whole polygon is obtained, and (3) a publicly available dataset containing (a) a set of 2D polygons that are relevant to test the performance of the algorithms and (b) the results of filling those polygons with our methodology. Results show that the developed methods produce satisfactory results for the polygons contained in the evaluation dataset, including a couple of demonstrations of real 3D prints with the generated trajectories. Further work is needed to extend the methodology to produce suitable solutions for polygons with curved holes.


Introduction
Additive manufacturing (AM) is a manufacturing method that describes a group of processes to produce objects by  [1]. Given its ability to obtain near-net shape products with very little material waste, this method is revolutionizing the manufacturing industry [2]. Some of the advantages of AM are its high resolution and the possibility to decide the pattern to fill the areas that form the 2D layers of the model. There is a great variety of patterns, which can be grouped according to their shape and/or continuity. The fact of having a strictly continuous trajectory becomes very relevant in certain cases. In wire and arc AM one of the requirements is to minimize the number of tool path-passes, and following continuous paths a single pass per polygon is obtained [3]. In solid freeform fabrication based on welding, one of the most important requirements is that the path is strictly continuous [4]. In fused deposition modelling, recent studies show that the use of continuous carbon fibre-reinforced thermoplastics improves the mechanical properties of the model compared to different approaches like the use of additive of short fibres [5,6]. Yao et al. [7] employed a continuous toolpath planning strategy for fused filament fabrication that not only reduced the postprocessing time but also supported material recycling and reuse. When it comes to scaffold fabrication, it is very important to minimize the number of start and stop points, as they cause material agglomeration, which does not satisfy the Tissue Engineering goals [8]. Finally, there are materials, such as clays [9] or bioinks, which do not have the ability to retract and the dripping produced by travelling movements could produce undesired material overlap. This fact becomes especially relevant in bioprinting, where respecting the internal geometry with a high fidelity is crucial to guarantee cell survival and proliferation [10]. However, it cannot be guaranteed that non-convex polygons, i.e. those with at least one interior angle greater than 180 • , can be continuously filled with any separation between parallel lines and orientation. Therefore, if the area to be filled is concave, it will be necessary to break it down into convex areas. In this manuscript, we present a methodology for the generation of continuous infill paths that follow a hybrid zig-zag and contour pattern with configurable separation between parallel lines and direction. In this way, the print head avoids travelling movements within a closed polygon with or without holes. To do this, an algorithm that decomposes concave polygons into convex subpolygons has been extended. Then we present an algorithm that fills each of the convex areas with the desired pattern and joins each of them into a single continuous path. Finally, we provide a publicly available dataset of 2D polygons that present challenges to the convex decomposition and path joining algorithms, such that other researchers can evaluate their own methods and compare with our results. The outline of this article is organized as follows: Section 2 shows a summary of the most relevant literature. Section 3 explains the method followed for path planning. Section 4 shows and discusses the results obtained by this method. Finally, Section 5 summarizes the conclusions drawn about the proposed method.

Generation of continuous path
Hergel et al. [9] propose a method to generate continuous paths without transfer movements to avoid artifacts when printing ceramics, but that prioritizes to be self-supporting rather than having a concrete internal structure. Zhao and Guo [11] reviewed the most typical patterns used in conventional AM, namely raster, zig-zag, contour, grid, spiral, Hilbert filling curve and honeycomb. Among the mentioned patterns only the zig-zag, the spiral, the Hilbert filling curve and the honeycomb can be obtained with continuous paths. Zhao et al. [12] propose a variation of the conventional spiral, resulting in a single connected path of Fermat spirals, but which doesn't allow the direction of the infill to be changed. Bertoldi et al. [13] agree that when the head deposits long straight lines, the quality of union can be very poor, so they proposed using the Hilbert curve. However, the printing time is between 2 and 4 times longer than other patterns. Hexagonal honeycombs offer excellent mechanical strength at low density, but like the Hilbert curve, the printing time increases considerably compared to other patterns [14]. Ding et al. [3] present a variation of contour and zig-zag pattern, obtaining a continuous path that acquires the precision of the contour pattern and the simplicity of the zig-zag. Besides, zig-zag pattern works well for filling with high density, the computational cost to generate it is relatively short and its internal structure is very regular, which makes it suitable for being applied in fields that require fidelity in internal structures. The main disadvantage of this pattern, in contrast, is that continuity cannot be guaranteed for every direction and polygon combination. In fact, for many concave polygons it will be impossible to obtain a continuous path if they are not processed previously. Section 2.2 discusses different methods to break down concave polygons into convex areas.

Polygon decomposition
Whenever we want to fill in a non-convex polygon with a continuous zig-zag pattern we will proceed to break it down into convex parts. There are different methods to decompose concave polygons into convex subpolygons. De Berg et al. [15] collect several methods of triangular decomposition which would result in multiple convex triangular areas, but some triangle angles could be too sharp, potentially leading to overlapping material. Decomposing a polygon into trapezoids is a valid option that was catalogued by Asano et al. [16] as an NP-complete problem, 1 for which a method with a O(n log n) (n being the number of vertices of the polygon) running time 2 was proposed for polygons containing holes. However, other methods are able to produce less convex subpolygons, such as the one in Ding et al. [3], which decomposes a concave polygon into convex subpolygons following a divide-and-conquer strategy. 3 This method defines as notches all the vertices that join edges that form internal angles greater than 180 • , and tries to eliminate them by 3 different routines depending on the number of notches and vertices that lie between the extensions of the two edges that form each notch and their distance to the bisector line. Given its relative simplicity and its compatibility with the zig-zag pattern, we extend their formulation.

Limitations of the literature
The method proposed by Ding et al. [3] does not include the following aspects in relation to the generation of continuous paths with hybrid zig-zag and contour pattern: -The method to obtain convex polygons does not mention the possibility of two extension lines intersecting in different polygons, and therefore does not state how these cases should be managed. -The method to obtain convex polygons may achieve a suboptimal solution when a hole is contained in the area covered by the extension lines and the polygon in which the node to be removed is located. -The method to obtain convex polygons may achieve a suboptimal solution because only contemplates joining the reference notch with the notch/vertex that is closest to the bisector. -The method to obtain a continuous trajectory does not cover how to join the paths of different subpolygons. In addition, the literature does not include a 2D polygon dataset with its coordinates that would allow to check and compare the performance of convex polygon decomposition and the joining of the paths of the decomposed subpolygons.

Methodology
The infill generation produces the paths that will be printed in the internal area of the polygons of each layer that are obtained from the slicing of the model. It means that only geometrical aspects are considered but not the process parameters (deposition, speed, acceleration, etc.). In this work, we implemented an algorithm that aims to fill in the polygons that form a 2D layer following a continuous hybrid zig-zag and contour pattern with any direction and separation between parallel lines. Figure 1 shows a polygon of a 2D layer and a possible infill. Given a 2D polygon P ⊂ R 2 that may contain holes and that is closed and bounded, the goal is to obtain a sequence of points P 1 , P 2 , . . . , P k ∈ R 2 , that defines a closed and continuous polyline, without self-intersections, and moreover such polyline forms a hybrid zig-zag pattern which fills the area described by the input polygon P .
The scope of the methods proposed in this work is shown resumed in Fig. 2.

2D layer layout
For each 2D layer there is a hierarchy of polygons in the form of a tree, like the one shown on the left of Fig. 3. Even-level polygons are polygons that are going to be filled, and their vertices are numbered counter-clockwise, whereas odd-level polygons are holes that do not have to be filled and their vertices are numbered clockwise. It is assumed that these structures are already defined and will be input parameters for our algorithm. To simplify the problem, the algorithm restructures the 2D layer, so that all the evendepth polygons greater than 1 are set as level 0 polygons and

Polygon decomposition
The first step is to check if the polygon is convex or not, for which we check if there are internal angles greater than 180 • , as Ding et al. [3] do. If it is convex, the decomposition is unnecessary and we can proceed to generate the infill (Section 3.3). Otherwise, we will need to carry out the decomposition, for which we present an extension of the method of Ding et al. [3] that handles cases that were not considered in their formulation. On the one hand, the method of Ding et al. [3] does not consider the possibility that the extensions of the edges that form a notch intersect two different polygons, which is illustrated in Fig. 4(a). It turns out that in these cases it is not possible to determine how many vertices and notches remain between both since they are two different contours. In our formulation, we have catalogued those notches as unmanageable and we will try to eliminate them once some partition line has been generated and the vertices have been reorganized.
To generate the partition lines the method of Ding et al. [3] follows 3 different routines according to the number of notches and vertices along the edges from point A to B. However, this way of counting notches and vertices presents two limitations when a hole is completely within the area covered by the extension lines and the contour of the polygon they intersect. The first limitation, which is   Fig. 5(a), can occur when there are notches along the geometry edges between points A and B. In those cases, according to the method of Ding et al. [3] the partition line should be the one that joins the reference notch with the closest notch to the bisector line of the reference notch, thus eliminating two notches with a single partition line. However, if the hole is along the path of the partition line, the second notch that was expected to be removed cannot be removed. The second limitation is derived from the first one, and is illustrated in Fig. 5(b). As we said, only the union with the notches and vertices that are along the geometry edges between points A and B is considered. However, if the notches of the hole were also taken into account, we would avoid the previous limitation and would give the possibility of eliminating the reference notch and another one of the hole with a single partition line.
In case there is more than one notches/vertices between points A and B, according to the method of Ding et al. [3], the partition line should be formed with the notch/vertex that is closer to the bisector line of the reference notch. However, we have found cases in which the decomposition results in more subpolygons than the necessary to have only convex areas. With the aim of obtaining better results in such cases, we also consider the option of creating the partition line with the closest notch/vertex to the reference notch. The number of subpolygons obtained with this method is always equal or smaller than following the method proposed by Ding et al. [3], but shapes can vary. In order to obtain the subpolygons with less sharp turns we look at the angles of the ends of each of the possible partition lines: 1. Get the interior angles in both ends of the possible partition line (see the red and blue angles in Fig. 6) and add them.  Once the decomposition is finished, it is necessary to identify those edges shared by two subpolygons, and in case they have different lengths, add the necessary vertices to the one with the greatest length, so that the edge they finally share has the same vertices (see Fig. 8). Although this may mean having collinear segments in the same subpolygon, it will simplify the union of their zig-zags.

Path generation for convex polygons
The steps to generate the path with the desired orientation and distances between lines for convex polygons are described below: 1. Transform the polygon to a coordinate system in which the main lines of the zig-zag are parallel to the horizontal axis, which simplifies the code.  2. If the polygon to be filled is the result of the decomposition of a non-convex polygon, apply an offset of a distance D equal to one of the zig-zag separations. This will prevent it from overlapping with the polygon next to it, as well as giving enough space for the union between them. The resultant polygon, which will be called unionPolygon, is shown in solid green in Fig. 9(a). If unionPolygon cannot be generated, no infill pattern can be generated. In case the polygon was originally convex, unionPolygon is the polygon itself. 3. Identify the left chain of unionPolygon, which is defined as the polyline that goes from the lowest to the highest vertex of the polygon by the left side of it. The left chain of unionPolygon is plotted in dash-dotted dark blue in Fig. 9(b). 4. Offset the left chain the same distance as in step 2 and form a new polygon using the remaining edges of unionPolygon. The resultant polygon, called contourPolygon, is used together with unionPolygon to generate the contour of the path, and it is graphed in dotted violet in Fig. 9(c). 5. Offset contourPolygon the same distance as in step 2.
The resultant polygon, plotted in solid cyan in Fig. 9(d), is called zigzagPolygon and it is used to create the  zig-zag of the path. If zigzagPolygon cannot be generated (result of too wide line separations), the resultant infill pattern will be the contour of unionPolygon. 6. To generate the zig-zag, build horizontal lines from the lowest point of zigzagPolygon, varying the separation between them according to the side of the turn of the zig-zag, until the highest point is reached and look for the intersections with zigzagPolygon. Join the intersections by the right and left side alternatively following the contour of zigzagPolygon. Figure 10(a) shows the evolution of the zig-zag and Fig. 10(b) shows the final zig-zag pattern. At this point, we have implemented an automatic adjustment of the chosen line separations (which can be enabled or disabled) to ensure that the polygon is completely filled at the top. 7. Extend the zig-zag, following if possible the slope of the corresponding edge of zigzagPolygon, to  contourPolygon. If the extension of the zig-zag intersects the left chain of contourPolygon (see Fig. 11(a)), a line parallel, at a distance D, to the first segment of the left chain is defined and its point of intersection with contourPolygon will be the point of union between the zig-zag and the contour (see Fig. 11(b)). This step turns to be especially relevant when the variation of the slope of adjacent edges is very slight (curved polygons, for example) and when the chosen line separations is very big, where the number of vertices of contourPolygon and zigzagPolygon is not the same. Once the union is made, go through contourPolygon in clockwise direction until the lowest part of its left chain is reached. 8. Traverse unionPolygon in a clockwise direction until the highest part of the left chain of contourPolygon is reached, as shown in Fig. 10(c). 9. Go through contourPolygon in counter-clockwise direction, as shown in Fig. 10(d), until the point where the extension of the first line of the zig-zag and contour-Polygon would intersect, and join it with the beginning of the zig-zag. Figure 10(d) shows the final path of a convex polygon with a horizontal continuous hybrid zig-zag and contour pattern. 10. Transform the original polygon and the whole pattern to the original coordinate system. Figure 12 shows the infill of the same polygon with the same separation between lines and different orientations.

Joining of subpolygon paths into a single path
To carry out the union of the paths of the different subpolygons, which will be made around the edges shared by two of them, we make a graph of adjacency, where alternative extension to get a non-self-intersecting path each subpolygon is a node and its degree is the number of shared edges it has. From the adjacency graph we construct a spanning tree as follows: 1. We identify the root node as the node with lowest degree and, in case of a tie, we choose the first-generated node among them. 2. We mark that root node as visited. 3. Starting from the root node, we proceed with a recursive traversal of the graph: we mark the edges that connect the current node of the traversal with a still-unmarked node, mark those still-unmarked nodes, and then recurse the traversal to them in order.
The spanning graph, which is shown in Fig. 13(b), is formed by all the marked edges and has the identified root. This way it is assured that the paths of all the subpolygons will be joined at least with another path, and globally all of them will be joined by the paths of other subpolygons.
The steps to make the union of the paths of two subpolygons are stated below: 1. Identify the two points of the same path that delimit the line parallel and closer to the edge that is shared by the subpolygons to be joined. These points are plotted in grey in Fig. 14. 2. Decide around which two points the union is going to be made, and identify them as p 1 and p 2 . To try to avoid very sharp turns, the internal angle that would form each subpolygon in the environment of each point is calculated and if any of these angles is lower than 25 • , it is proposed to make the union around the other end. If this is not the case, the angles of the two subpolygons around the same end are summed up and the union is made at the end at which there is an angle closer to 0, 90, 180 or 270 • , prioritizing the angles closer to 0 and 180 over 90 and 270 and the convex ones over the concave ones. 3. Extend 1 and 2 and look for their point of intersection, which is called p 1,2 (see Fig. 15). 4. Generate two lines parallel to 1 and 2 at a distance D (see 1 and 2 in Fig. 15). The intersection points of these lines with their respective zig-zags are labelled as There are 2 cases that can vary the way to obtain these points.
(a) Variation 1: p 1,2 is inside one of the zig-zags. Find the intersection of the line of the zig-zag in which p 1,2 is not contained with the zig-zag in which p 1,2 is contained. This point is shown as a dot with the name p 1,2 aux in Fig. 16. (b) Variation 2: 1 or 2 is left out of the zig-zag because the polygon is too small. In that case, the procedure used to obtain p 1,2 is repeated at the other end, such that p 1 and p 2 become p 1 and p 2 and their intersection p 1,2 . Figure 17 shows this type of union, where the green pattern, due to its small size, is the cause of this variation. Modify the original paths to join them by means of the found points. p 1 and p 2 are joined with p 1,2 , and the segments that joined p 1 with p 1 and p 2 with p 2 are removed. p 1 and p 2 will be joined between them, either directly or by crossing p 1,2 . To decide whether to pass through p 1,2 , calculate the internal angles that would result from going through it or not. From each of the options, the minimum angle is found and the greater of the two is chosen in order to avoid very abrupt turns, as it can be seen in Fig. 18. In case of a tie, going through p 1,2 is preferred over the direct connection, as it respects more the fidelity of the contour of the polygon Fig. 15 Auxiliary points used to make the union of two paths Once all the zig-zags have been joined and a unique continuous path has been achieved all the collinear segments that may have been generated are removed.

Theoretical evaluation
In order to validate the results obtained in both the nonconvex polygon decomposition and the path generation, and given the lack of a 2D polygon dataset with which to check them, it has been decided to generate one. The repository 4 includes the coordinates of the polygons, together with a justification of why they are relevant, and the results obtained with our algorithm. In terms of the polygon decomposition the dataset allows the following situations to be evaluated: -The extensions of the edges that form the reference notch intersect two different boundaries. -There is a complete hole inside the area covered by the extensions of the edges that form the reference notch. -Intersection of lines with the same slope (overlap of lines).
When it comes to path generation, the dataset covers the following cases of relevance: -The edge of union of two subpolygons are not exactly the same, but one is longer than the other. -The edge of one of the subpolygons is used as a double union area with two different subpolygons. -Need to generate a graph that indicates which subpolygons are to be joined together. This dataset contains 35 JSON encoded files with the coordinates of 15 convex polygons to check that the generation of the hybrid zig-zag and contour infill pattern is correctly performed, and 20 non-convex polygons to validate the algorithms of decomposition and generation of continuous paths. The case studies include the generation of the infill paths with 48 combinations of separation between lines and zig-zag orientations, except for polygon NCPolygon20.json, which only has a single configuration for the separation between lines (a total of 1633 cases). The purpose of having the polygon NCPolygon20.json in the dataset is to illustrate the performance of the method when polygons that have holes with coarse circular shapes are processed with our methodology. Finally, the repository also contains Python3 code that can be used to load and visualize the polygons and their infills.

Experimental evaluation
The infill trajectories generated for the polygons NCPoly-gon11.json and NCPolygon16.json of the evaluation dataset were used to perform an experimental printing test using a Creality Ender-5 Plus 3D printer. The infill for NCPoly-gon11.json has a separation of 2 mm between lines, while the infill for NCPolygon16.json has a separation of 0.5 mm. Polylactic acid (PLA) was extruded with no retraction during printing. The feed rate was set at 1.2 m/min and the extruder speed was 45 mm/s, obtaining a trace of 0.51 mm of diameter.

Results
Since our method expands the work by Ding et al. [3], we first present a comparison with their results in Table 1. These results have been generated with a variant of our method that allows to define the orientation of the infill pattern for each of the convex subpolygons independently, allowing a closer replication of the case studies in [3]. As we do not have the exact coordinates of the polygons or the parameters of the fill patterns used by Ding et al. [3], we can only make a visual comparison of the results, with no conclusions about the number of path elements.
The additional results obtained for the other cases included in our evaluation dataset confirm the capability of the proposed methodology to deal with cases beyond those studied in [3], including nested polygons with holes (see Fig. 19), and other ones targeting the limitations discussed in Section 2.3. Furthermore, we provide an extensive report of our results in the repository introduced in Section 3.5.1, where we have stored the generated paths for each of the case studies of the evaluation dataset (there is a text file, .txt, file containing the coordinates of the trajectories generated for each polygon with the various combinations of separations between lines and orientations of the infill pattern). We believe that this information will ease quantitative comparisons with future works from other researchers. In addition to the mentioned results, we show in Figs. 20 and 21 the outcomes of the experimental tests (Section. 3.5.2), as a demonstration of the suitability of the generated trajectories to be printed with no interruptions in the thread deposition.
Although the reported results are satisfactory (in all of the cases a strictly continuous path that follows the required pattern was generated), the method presents limitations when dealing with polygons that have holes with curved shapes. In those cases, the convex decomposition algorithm produces subpolygons with small areas and very acute angles (this will produce undesired material accumulation in the corners of the trajectories). Polygons with those characteristics produce problems in the computation of the required insets/offsets to create the infill path for the subpolygon and also for the computation of the subpolygons' unions (even the strategies presented in Section 3.4 may not suffice to solve all the possible cases). The result obtained for polygon NCPolygon20.json, which has holes with coarse curved shapes, is a demonstration of this limitation (Fig. 22). Furthermore, including more vertices to describe the curved holes would make the problem worse, since there would be too many notches to eliminate and the resulting convex areas would be too narrow to be filled with the zig-zag pattern without overlapping the material. In addition to this, when the size of the subpolygon and the configured separation between lines does not allow to generate any infill (not even the simplified version using unionPolygon), it will not be possible to generate a continuous trajectory for the entire non-convex polygon if that subpolygon is the only connecting path with other subpolygons.
Finally, we present the results of printing the infill paths for NCPolygon11.json and NCPolygon16.json (see Figs. 20 and 21, respectively) of the evaluation dataset, as a demonstration that the produced trajectories can be

Conclusions and future work
This work has presented the following contributions: -Improvement of the current state of art of polygon decomposition into convex areas and although we do not guarantee that we have a global optimal solution, we can get less subpolygons than in other methods of decomposition [3,15,16]. -A method to create a closed hybrid zig-zag and contour infill pattern for convex polygons.
-A method to join the hybrid zig-zag and contour patterns of two adjacent subpolygons, obtaining a single continuous path for the whole polygon, which makes it possible to be used in a wide variety of modes of additive manufacturing. -A publicly available dataset of 2D polygons to check and compare the results.
This method, however, has certain limitations. On the one hand, although a valid result can be achieved, our method is not the most suitable for filling polygons with curved holes, as it would generate many subpolygons with very sharp areas where material could overlap. On the other hand, this work does not cover the way in which the different polygons that make up a layer should be joined, nor the union between two consecutive layers.
Author contribution All authors contributed to the study conception and design. Gorka Gomez was the main responsible for the implementation of the code and produced the first draft of the article. Camilo Cortés contributed to the design of the implemented methods and contributed to the writing of the article. Carles Creus monitored the implementation of the algorithms and contributed to the writing of the article. Maialen Zelaia contributed to the creation and organization of the repository that contains the case studies and results of evaluation of the algorithm, and also contributed to the writing of the article. Aitor Moreno contributed to the acquisition of the funding for this project and revised the manuscript.
Funding Partial funding obtained from the Basque Government through the Elkartek KK-2019/00006 grant for project Bioprint-ing4Healing.
Availability of data and material Data is available in the following repository: https://github.com/Vicomtech/Dataset-of-2D-polygons-for -Additive-Manufacturing Code availability Code is not available.

Declarations
Ethics approval No ethical approval was required for this research.

Conflict of interest The authors declare no competing interests.
Open Access This article is licensed under a Creative Commons Attribution 4.0 International License, which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons licence, and indicate if changes were made. The images or other third party material in this article are included in the article's Creative Commons licence, unless indicated otherwise in a credit line to the material. If material is not included in the article's Creative Commons licence and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder. To view a copy of this licence, visit http://creativecommons. org/licenses/by/4.0/.