<p>I understand how slicer programs create sets of closed-loop polygons to print on a layer-by-layer basis.  For a given closed loop polygon which needs to be printed, the tool path generator will know the coordinates and how those coordinates are connected to each other, such that traversing a set of segments in that order will bring the extruder head back to the first coordinate to complete the closed loop.</p>

<p>My question is: By what mechanism does the tool path generator decide which direction to traverse the closed loop?  As it is a loop, that loop could be printed "clockwise" or "counter-clockwise", as it were.  Any details, and links to further explanations of how some of the big-name slicer programs determine this is much appreciated.</p>

[{'answer_id': 7972, 'author': 'Trish', 'author_id': 8884, 'author_profile': 'https://3dprinting.stackexchange.com/users/8884', 'pm_score': 2, 'selected': False, 'text': '<h2>Math</h2>\n<p>In math, there is a way how a path is to be followed, and that is usually counterclockwise:</p>\n<p>Assuming a perimeter path of a circle with <span class="math-container">$r=1$</span> around <span class="math-container">$(2,2)$</span>, then the  path can be defined as <span class="math-container">$f(p)={{\\cos(p)+2}\\choose{\\sin(p)+2}}$</span> - where <span class="math-container">$p$</span> is the path parameter, in this case an angle of 0 to 360°, and just increasing the angle rotates right hand around. If we had the same path but a different starting point, a shift by <span class="math-container">$\\theta$</span>, then the path would read <span class="math-container">$f(p)={{\\cos(p+\\theta)+2}\\choose{\\sin(p+\\theta)+2}}$</span>. So math is usually <em>counterclockwise</em>.</p>\n<h2>Slicers</h2>\n<p>Every slicer is applying math. As far as I can tell, any Slicer generates a perimeter path, which is always performed in the same way if sliced with the same settings. For one case look at this:\n<a href="https://i.stack.imgur.com/nSlg7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nSlg7.png" alt="5 seconds of print sliced in cura" /></a>\nCounterclockwise starting from a 7 o clock position in this case. However, other slicers or other objects on the printbed might use other engines, thus doing it not that way. They might go clockwise since solving a path with <span class="math-container">$p=0°\\to360°$</span> and solving it <span class="math-container">$p=360°\\to0°$</span> results in producing the exact same print, just opposed print direction of the perimeter.</p>\n<p>As long as the perimeter of an object is solved as being done as one closed loop, the perimeter will need to have just one, prescribed direction. This direction will be clockwise or counterclockwise depending on how the slicer exactly solves its calculations. Since both directions are equally valid, it is a programmer\'s decision. A programmer might even prescribe clockwise or counterclockwise solution based on any factor they wanted. They might use layer number (for alternating directions) or a user setting or even an RNG, if they wanted to.</p>\n<p>On the other hand, how the memory is operated and written can also result in the path and the math looking differently. Two examples:</p>\n<ul>\n<li>Solving the path correctly counterclockwise and putting the slicing into a FILO memory, resulting in a clockwise operation starting from the last solved point.</li>\n<li>Solving counterclockwise and saving into FIFO, running counterclockwise.</li>\n</ul>\n<h2>Conclusion</h2>\n<p>Slicers for 3D printing have a hard-coded way to choose the direction that is followed when producing G-code. Any and all perimeters will be printed, starting from some arbitrary point, into that direction. In the end, it is a choice of the <em>programmer</em> of the slicing engine that determines if the path is to be run down &quot;forward&quot; or &quot;backward&quot; in mathematical sense.</p>\n<h2>Addendum</h2>\n<p>Slicers are derived from CAM programming. CAM - computer assisted machining - takes into account one more thing when solving the tool path that is not relevant to a 3D printer: The direction of the fluting of the tool. In fact, this one will determine into which direction the path will give a better cut and changing the fluting should swap the path direction to ensure best results.</p>\n'}, {'answer_id': 7983, 'author': 'Thetravellingfool', 'author_id': 1414, 'author_profile': 'https://3dprinting.stackexchange.com/users/1414', 'pm_score': 3, 'selected': False, 'text': '<p>While <a href="https://3dprinting.stackexchange.com/a/7972/8884">this</a> answer makes a valid attempt at answering the question, it is based on personal experience.</p>\n\n<p>I went to the literature and directly to the source code in Cura to find the answer.  In the academic article "<a href="https://link.springer.com/article/10.1007/s00170-012-4706-y" rel="noreferrer">Identifying the Directions of a Set of 2D Contours for Additive Manufacturing Process Planning</a>", Volpato et al. describe several methods for identifying the arbitrary directions of each contour in each layer, and additionally identifying which contours were "internal" and which were "external".  I quote from the paper:</p>\n\n<blockquote>\n  <p>The information regarding contour\n  direction, which is either clockwise (CW — internal) or\n  counterclockwise (CCW — external), is needed for path\n  planning for material processing.</p>\n</blockquote>\n\n<p>They go on to explain the importance of identifying which contours are external, and which are internal, such that the path planning algorithm can later determine where infill should be placed.  Infill is placed internal to any external contours, and external to any internal contours.  </p>\n\n<blockquote>\n  <p>When the normal vectors in STL models\n  are assumed to be correct, a simple way to identify whether\n  a 2D contour is CW or CCW is to analyze the vector (cross)\n  product between a normal vector and a vector obtained from\n  two vertices of the facet.</p>\n</blockquote>\n\n<p>This assumes the slicer has already determined intersection points between slicing planes and the STL file, and has sorted those intersection points into closed-contours.  This initial intersection point gathering and contour construction leads to an arbitrary directionality:</p>\n\n<blockquote>\n  <p>As any line segment of a contour can be the first in the\n  sequence when the segments are connected, its orientation\n  will dictate the direction of the contour. Hence, the 2D contours\n  formed are classified randomly, and an external contour, for\n  example, might be assigned a CW or CCW direction. Therefore, this step is unable to correctly identify the directions of the contours generated.</p>\n  \n  <p>The ray-tracing method, which is actually based on the\n  point-in-polygon test, determines which contours are\n  contained by others, and the orientation of each contour is\n  then alternated between CCW and CW, the outermost contours being oriented CCW.</p>\n</blockquote>\n\n<p>So, the default directionality of a closed contour generated by a slicing program for FDM additive manufacturing turns out to be CCW based on cross products described above (and based on additional methods outlined in the paper).  Of course the standard directionality of a PRINTED contour does not HAVE to be this way, it appears to be a standard adopted by the AM community.  However, when a model produces contours inside of contours, the arbitrary directionality of those contours is determined, and then alternated from outside to inside, starting with CCW.  </p>\n\n<p>As confirmation, according to a simple comment in the CURA source code:</p>\n\n<pre><code>/*!\n * Outer polygons should be counter-clockwise,\n * inner hole polygons should be clockwise.\n * (When negative X is to the left and negative Y is downward.)\n */\n</code></pre>\n'}]
['https://3dprinting.stackexchange.com/questions/7971', 'https://3dprinting.stackexchange.com', 'https://3dprinting.stackexchange.com/users/1414/']