Skip to content

Instantly share code, notes, and snippets.

@hexagon5un
Created February 4, 2018 22:50
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hexagon5un/73e187dfa0404c69177795424f397ce6 to your computer and use it in GitHub Desktop.
Save hexagon5un/73e187dfa0404c69177795424f397ce6 to your computer and use it in GitHub Desktop.
OpenSCAD Hull Functions Library
module multiHull(){
for (i = [1 : $children-1])
hull(){
children(0);
children(i);
}
}
module sequentialHull(){
for (i = [0: $children-2])
hull(){
children(i);
children(i+1);
}
}
/* Extended fun hull functions */
module cylinders(points, diameter, thickness){
for (p=points){
translate(p) cylinder(d=diameter, h=thickness, center=true);
}
}
module plate(points, diameter, thickness, hole_diameter){
difference(){
hull() cylinders(points, diameter, thickness);
cylinders(points, hole_diameter, 1000);
}
}
module bar(length, width, thickness, hole_diameter){
plate([[0,0,0], [length,0,0]], width, thickness, hole_diameter);
}
module rounded_box(points, radius, height){
hull(){
for (p = points){
translate(p)
cylinder(r=radius, h=height);
}
}
}
module point_cloud(points, radius=1, facets=8){
for (p=points){
translate(p)
sphere(radius/cos(180/facets), $fn=facets);
// polygon sphere circumscribed on radius
}
}
module point_hull(points, radius=1, facets=8){
hull(){
point_cloud(points, radius, facets);
}
}
// Shrink the exterior of the points so that the hull fits inside
function select(vector, i) = [ for (p=vector) p[i] ];
function axis_center(v, i) = (max(select(v, i)) - min(select(v,i)))/2;
function center(v) = [ for (i = [0,1,2]) axis_center(v, i) ];
function shrink_point(point, center, radius) = [ for (i = [0,1,2]) point[i] > center[i] ? point[i] - radius : point[i] + radius ];
function shrink(points, radius) = [ for (p = points) shrink_point(p, center(points), radius) ];
module interior_point_hull(points, radius=1){
point_hull(shrink(points, radius), radius);
}
module interior_rounded_box(points, radius, height){
rounded_box(shrink(points, radius), radius, height);
}
module mirror_copy(v = [1, 0, 0]) {
children();
mirror(v) children();
}
@steve1515
Copy link

There's an error in the axis_center function. It should be function axis_center(v, i) = (max(select(v, i)) + min(select(v,i)))/2; rather than function axis_center(v, i) = (max(select(v, i)) - min(select(v,i)))/2;. Note the plus instead of the minus.

One more suggestion: The shrink_point() function move's points on the z-axis by a radius amount. This isn't correct when making interior_rounded_box's. I would suggest making the function like this: function shrink_point(point, center, radius) = [ for (i = [0,1]) point[i] > center[i] ? point[i] - radius : point[i] + radius ];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment