SHORT DESCRIPTION OF MY PROBLEM
I need to implement GCODE autorefactoring from G1 instructions to G2 and G3 (http://www.cnccookbook.com/CCCNCGCodeArcsG02G03.htm) for 3D Printing.
G1 is a movement in straight line with printing (path is describe by vector).
I'm searching for algorytm to aproxymate circle/arc (specialy it's midpoint) based on given vectors path. Note that G2 and G3 can't print curves that are not part of a circle - so not every vectors path can be approximate this way.
LONG DESCRIPTION OF MY PROBLEM
I'm searching way to approximate part (or all) of vectors path (can be regular polygon, part of it or inregular polygon part) by circle (arc). But let's, at first, focuse on regural polygons.
In the picture i drew different cases of this problem. NOTE: Every polygons are build by vectors (like in point 5).
- Approximation on full x-gon.
- Approximation of part x-gon where one side is different
- Approximate of x-gon where two sides are different but equal to each other
- Approximate of x-gon where two sides are different (not even to each other)not equal
- Approximation of part x-gom where all sides are equal
It's not the eod of story...there are couple criteria:
- Circle's start/end point must be in the beginning/end of vectors path.
- I need to know circles mid point.
Solutions that i found (good and bad):
1) and 5) - my simple solution
This is the easiest case. I can count radius between each side that shares one opint. If they have same length and angles between each are equal i can calculate circles mid point (as point that belongs to perpendiculat middle-lines, one middle-line per side) and i have all i need: Start point, end point, mid-point.
But this solution works only for cases 1 and 5.
I realy have no ide what to do when i have cases 2,3,4 or inregural polygon's part
You can obtain curvature radius center for any 2 line segments
In 3D use plane of the object (3 lines not 2). The radius is just distance between center and the lines joint point (blue line). If radius is too big then handle both lines as single line (no intersection or too far intersection)
compute all segments like in #1
join arcs with the same radius and center to single arc if joined
handle the changing curvature
if the arc is changing the center or radius do it like in this picture
first segment does not have previous line so use the next instead that cause the irregularity at start arc ...
That should cover all cases hope my hand-paint-drawings are making sense ...
If your circular arcs must start and end on given endpoints, then the center point will be somewhere on the mediatrix and there remains a single degree of freedom.
So you can use the least squares method to find a best fit: assume you know the radius, which gives you the center, and compute the sum of squared distances of the remaining vertices to the circumference (distance to the center minus radius). The best fit is the one that minimizes that sum. Let's hope that there is an analytical solution.