Skip to content

Instantly share code, notes, and snippets.

@ochafik
Last active January 30, 2021 15:21
Show Gist options
  • Save ochafik/53b7fd8bde6eaa232f61ff22b5852f0d to your computer and use it in GitHub Desktop.
Save ochafik/53b7fd8bde6eaa232f61ff22b5852f0d to your computer and use it in GitHub Desktop.
OpenSCAD Chainmail benchmark for fast-union evaluation
// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0
//
// Author: Olivier Chafik (http://ochafik.com)
//
// This is extracted from my little chain/scalemail project shared in
// https://gist.github.com/ochafik/6d19e3b131153305b44ea275d5d930e3
//
// It is simpler to render, this example is crafted to exercice the fast-union
// feature I'm proposing in https://github.com/openscad/openscad/pull/3636
//
// Rendering is *much* faster with the feature push-transforms-down-unions
// I'm proposing in https://github.com/openscad/openscad/pull/3637 (or would be faster without
// the top-level transform, letting lazy-union shine), but that's not he point
// here: just trying to test fast-union.
//
// alias openscad=$PWD/OpenSCAD.app/Contents/MacOS/OpenSCAD
// time openscad --enable=fast-union --enable=lazy-union chainmail.scad -o chainmail_fast_lazy.stl
// time openscad --enable=fast-union chainmail.scad -o chainmail_fast.stl
// time openscad --enable=lazy-union chainmail.scad -o chainmail_lazy.stl
//
$fn=20;
step = 0.01; //$preview ? 0.01 : 0.001;
thickness = 0.5;
height = 1;
r1 = 2;
r2 = 3;
N = 5;
stride = r1 + r2;
function f(t, r1, r2, h, m=4, n=1) =
let (avgRadius = (r1 + r2) / 2)
[
cos(n * t) * (avgRadius + (r2 - r1) / 2 * cos(m * t)),
sin(n * t) * (avgRadius + (r2 - r1) / 2 * cos(m * t)),
h * sin(m * t)
];
module draw_curve(p, thickness) {
for (i=[0:len(p)-2]) {
hull() {
translate(p[i]) sphere(d=thickness);
translate(p[i+1]) sphere(d=thickness);
}
}
}
module torus_knot() {
points = [for (t=[0:step:1]) f(t * 360, r1=r1, r2=r2, h=height)];
draw_curve(points, thickness);
}
translate([-N * stride / 2 + stride / 2, -N * stride / 2 + stride / 2, height + thickness / 2]) {
for (i=[0:N-1]) translate([i * stride,0,0])
for(j=[0:N-1]) translate([0,j * stride,0])
torus_knot();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment