Skip to content

Instantly share code, notes, and snippets.

@thehans
Created November 14, 2022 03:57
Embed
What would you like to do?
OpenSCAD 3D Supershape
// OpenSCAD 3D Supershapes by Hans Loeblich 2022
// Formulas for "spherical product" from "Supershape in 3D" by Paul Bourke
// http://paulbourke.net/geometry/supershape/ (scroll down for 3D)
// Many nice sample values to try from that page too!
/* [Function R1] */
m1 = 3;
n1_1 = 1;
n2_1 = 1;
n3_1 = 1;
a1 = 1;
b1 = 1;
/* [Function R2] */
m2 = 6;
n1_2 = 2;
n2_2 = 1;
n3_2 = 1;
a2 = 1;
b2 = 1;
/* [Resolution] */
$fn=200;
// returns a function which evaluates the given superformula at a single angle
function superformula_factory(m=4,n1=4,n2=4,n3=1,a=1,b=1) =
function(th) (abs(cos(m/4*th)/a)^n2 + abs(sin(m/4*th)/b)^n3)^(-1/n1);
ni = $fn;
nj = round($fn/2);
R1 = superformula_factory(m1, n1_1, n2_1, n3_1, a1, b1);
R2 = superformula_factory(m2, n1_2, n2_2, n3_2, a2, b2);
pp = [for(j=[0:1:nj]) for(i=[0:1:ni-1])
let(th=-180+i*360/ni, phi=-90+j*180/nj, r1=R1(th), r2=R2(phi), cosphi=cos(phi))
[r1*cos(th)*r2*cosphi, r1*sin(th)*r2*cosphi, r2*sin(phi)]
];
polyhedron(points=pp, faces=[for(j=[0:1:nj-1]) let(ij=j*ni,ij2=(j+1)*ni)
for(i=[0:1:ni-1])
let(i1=ij + i, i2=ij + ((i+1)%ni), i3=ij2 + i, i4=ij2 + (i+1)%ni)
each [[i2,i1,i3],[i3,i4,i2]]
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment