Discussion:
[OpenSCAD] Rounded Polygon
irevdev
2017-07-22 12:35:41 UTC
Permalink
Hi all,

Just sharing a tool that I think is pretty useful for rounding the points on
a polygon. The idea is that along with the list of coordinates that you
would normally pass to polygon(), a list of radii is also given and they are
used to round the corners at each of the coordinates.

<Loading Image...>

code here
<https://github.com/Irev-Dev/Round-Anything/blob/master/polyround.scad>

I normally design SCAD things mostly with primitives, but I always get
frustrated when it comes to adding fillets later (internal radii in
particular), I'm going to try and design things from now on by extruding
these rounded polygons (where practical).



--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
TLC123
2017-07-22 14:20:10 UTC
Permalink
Oh Yes this will be used extensively , i assure you.



--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897p21903.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
irevdev
2017-07-22 21:36:42 UTC
Permalink
Good to hear :)

I forgot to add, one current issue is that if three consecutive points are
col-linear it breaks :/
It's probably easy to fix, I've just hit a wall in motivation.



--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897p21904.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
TLC123
2017-07-23 08:21:03 UTC
Permalink
Yeah i get some runaway error too. Not sure if its the same issue though.
Co-linearity is easily fixed in function round3points.
if more tha 98% collinear just return input back as is.
swap the end of round3points to:

swapout:for:
I think my issue occur when radius for p2 dont fit between p1 -p3.
Maybe that should be left to be expected
or limited so the arc never can go outside the three points?
Also i probably will use [x,y,r], format points in my trials.
Easy enough to separate them before calling polyRound.
Any reason you selected against that?
Thanks again for a great module that will com to much use. Not least with
sweeps.
<Loading Image...>




--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897p21908.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
irevdev
2017-07-23 12:00:54 UTC
Permalink
Post by TLC123
Co-linearity is easily fixed in function round3points.
if more tha 98% collinear just return input back as is.
Thanks for having a look at the co-linearity problem. I've only had a quick
look at your code now before bed so I mightn't understand it properly but I
would think that if you test for co-linearity and the result is true, then
you should return nothing and move onto the next set. My thought experiment
is that if there are 5 points where points 2 , 3 and 4 are co-linear,
processing points 1,2&3 together is fine likewise so is 3,4&5. If we just
skip processing points 2,3&4 it should be fine. Returning the original three
points might cause problems, I'll have a look at it though later.
Post by TLC123
I think my issue occur when radius for p2 dont fit between p1 -p3.
Maybe that should be left to be expected
or limited so the arc never can go outside the three points?
Your suggestion to limit the arc to stay within the three points, I'm
thinking out loud now, at first I thought I wouldn't work, but now I think
it could potentially work if instead of just processing the list of points
three at a time like it is currently p[i],p[i+1],p[i+2] instead it grabs the
last point from the previous arc so arcpoints[last point of last
arc],p[i+1],p[i+2]. it would have to do something different on the first and
last iteration of the for loop, but that's seem like a nice simple solution.
Post by TLC123
Also i probably will use [x,y,r], format points in my trials.
Easy enough to separate them before calling polyRound.
Any reason you selected against that?
Thanks again for a great module that will com to much use. Not least with
sweeps.
In terms of the format [[x,y,r],[x,y,r],etc], I think you're right, I think
it's better. I posted this code as soon at it was mostly working so I
haven't actually used it for anything other than the examples, but trying to
implement it today on an actual project of mine I found it confusing as to
which radius lined up with which point on the polygon. So I think I'll make
this change, but with any of those three things, feel free to make a pull
request.

I'll have a closer look at your code soon, though probably not until
Tuesday, I'm busy tomorrow.




--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897p21909.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
droftarts
2017-07-23 14:51:56 UTC
Permalink
Post by TLC123
Also i probably will use [x,y,r], format points in my trials.
<br>
Easy enough to separate them before calling polyRound.
<br>
Any reason you selected against that?
<br>
Probably so he could draw the original polygon easily, independently of the
script, for testing.

The main reason I started the other script was to enable CSG export to
FreeCAD and then into Fusion 360, as this couldn't be done using hull (read
the thread here
<http://forum.openscad.org/Script-to-replicate-hull-and-minkoswki-for-CSG-export-import-into-FreeCAD-td16537.html>
). This meant that circles and arcs keep their definition, rather than being
broken up into a mesh. Your script does seem to create a mesh, ie arcs are
broken up into straight segments, so it doesn't maintain the geometry of
circles when you import it into FreeCAD.

Unlike the other script, the points don't have to go around in a clockwise
order with yours, which is nice.

Ian





--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897p21910.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
droftarts
2017-07-23 15:21:39 UTC
Permalink
Thinking about it, an approach that is somewhere between the two scripts
might be the best of both. Take the initial polygon, calculate the tangent
points between each three points and the radius, redraw the polygon without
the middle point but with the tangent points, then union or difference the
circle from it. This would give you the same shape as your version, but
constructed from a polygon with unioned or differenced circles, like my
version. This would mean on CSG export that it would maintain it's geometry,
rather than being converted to a mesh, so can be opened in other CAD
software as a solid body, rather than a mesh.

Ian



--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897p21911.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
KeithSloan52
2017-07-23 16:35:38 UTC
Permalink
There seem's to be some miss understandings about FreeCAD's import of SCAD
and CSG files.

1) FreeCAD does try and support hull and minkowski requests. For it to work
FreeCAD must be able to find the OpenSCAD executable. The path to the
OpenSCAD executable is an option of the OpenSCAD work bench. ( It can be
very slow.)

2) For hull & minkowski to work with 2D objects the option to allow FreeCAD
to download and update DXF libraries must be enabled see general
import/export preference, The downloadable library is in general no longer
used by FreeCAD but IS used by importCSG for hull & Minkowski requests
involving 2D objects. There was a bug with LWPOLYINE in the DXF library that
has not long been fixed.

3) polyhedron should also be supported, but again there was a bug until
recently.

So if you have problems then please try the latest 0.17 version and if
still, problems please report in the FreeCAD forum.



--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897p21912.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
irevdev
2017-07-23 19:26:20 UTC
Permalink
Post by droftarts
Thinking about it, an approach that is somewhere between the two scripts
might be the best of both. Take the initial polygon, calculate the tangent
points between each three points and the radius, redraw the polygon
without the middle point but with the tangent points, then union or
difference the circle from it. This would give you the same shape as your
version, but constructed from a polygon with unioned or differenced
circles, like my version. This would mean on CSG export that it would
maintain it's geometry, rather than being converted to a mesh, so can be
opened in other CAD software as a solid body, rather than a mesh.
Ian
Hi Ian,

short answer is if you use fn of 1 with my code it will draw the polygon
along all the tangent points only.

This was my initial approach, but I dumped it when I realised if you wanted
to have a relatively large radius compare to the overall size of the shape
than unioning it with a circle would mean the circle would take over. ie the
radius of 10 in the example, a circle that big would be bigger than most of
the shape. I thought it could be fixed by cutting the circle down before it
got unioned but at that point it seemed drawing points of the circle would
be easier.

If you wanted go back to this like I said you can use fn of 1 or you could
modify the round3points() function so that instead of passing the tangent
points and circle center to CentreN2PointsArc() it just returns these values
instead, that way you would also have the coordinates of the circle center
for the circle unioning.



--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897p21913.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
droftarts
2017-07-23 21:37:16 UTC
Permalink
Ah yes, I see the problem, and now I try it, the other script has this
problem too. Though it probably shows how often it's a problem, as neither I
nor anyone else has noticed it before! Thanks for the extra info, I'll take
a look.

Keith: I'd like to see some examples of what FreeCad can import correctly
from OpenSCAD, but I think that belongs in another thread, rather than
hijack this one!

Ian



--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897p21914.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Ronaldo Persiano
2017-07-23 23:02:12 UTC
Permalink
Post by TLC123
I think my issue occur when radius for p2 dont fit between p1 -p3.
Maybe that should be left to be expected
or limited so the arc never can go outside the three points?
The issue is not restricted to that extreme cases. It is enough to have
tangent points in an edge in reversed order.

module test2(size,r) {
p = [ [0,0], [size,0],[size,size],[0,size] ];
polygon( polyRound(p,r,10));
translate([0,0,-1])%polygon(p);
}

test2(100,[80,20,30,50]);



If polyRound is recoded to identify those cases, either ​it should do
nothing or reduce some radiuses. Unhapilly, the radius reduction problem
has usually many solutions.
Parkinbot
2017-07-24 08:52:23 UTC
Permalink
As Ronaldo mentioned it is not straight forward to deal with conflicting
radii. But, even a robust implemention will be quite difficult, this
function is a nice-to-have if handled with care.

Here is my implementation of it. It uses $fs to control arc graining and
$fs=.2; // graining
b=[[-5,0],[5,3],[0,7],[8,7],[20,20],[10,0]]; //points
br=[1, 1.5, 1, 10, 1.5, 5]; //radiuses
polygon(polyRound(b,br));
%translate([0,0,0.2])polygon(b);
p=[[0,0],[0,20],[15,15],[3,10],[15,0],[6,2]];//points
pr=[2, 4, 3, 3, 1, 8];//radiuses
translate([25,0,0])polygon(polyRound(p,pr,$fs=.4));
%translate([25,0,0.2])polygon(p);
function polyRound(b, r) = [for(i = [0:len(b)-1]) each tri(b, i,
r[(i+1)%len(b)])];
function tri(L,i, r) = let(N=len(L)) // uses $fs
let(A = L[i]) let(B=L[(i+1)%N], C=L[(i+2)%N]) //
points
let(a = (A-B)/norm((A-B)), b = (C-B)/norm((C-B))) //
vectors
let(a_= [-a[1], a[0]], b_=[b[1],-b[0]]) //
normals
let(m = r*(b_-a_),k=(a[0]*m[1]-a[1]*m[0])/(b[0]*a[1]-b[1]*a[0])) //
equation stuff
let(s = sign(k), w = 2*atn([r,s*k]), w0 = atn(-s*a_)) //
angles
let(n = round(abs(r*w/180*PI)/$fs), M = B+s*k*a+s*r*a_) //
middle point
[for(z = [w0:-s*w/(n+1):w0-s*w-s*.0001]) M+r*[cos(z), sin(z)]]; // arc
// full circle atan
function atn(X) = let(w = X[0]==0?X[1]<0?270:90:atan(X[1]/X[0]))
X[0]<0?180+w:(w+360)%360;
--
View this message in context: http://forum.openscad.org/Rounded-Polygon-tp21897p21928.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Loading...