Skip to content

Instantly share code, notes, and snippets.

@hovissimo
Last active February 28, 2016 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hovissimo/2fdec1736880e77d49da to your computer and use it in GitHub Desktop.
Save hovissimo/2fdec1736880e77d49da to your computer and use it in GitHub Desktop.
This function calculates the minimum fn value to turn a curve for a defined feature size. You can use this to optimize render times. This script is designed to be used with the tool at openjscad.org. It is probably most useful for someone who does a lot of 3d printing.
/**** LICENSE
* Copyright (c) 2016 Andrew "Hovis" Biddle
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
****/
var fnByMaxStepDistance = function(max, radius) {
var tau = 6.28318;
// We want to find the correct fn value so that the segments of a given "curve"
// are not longer than max. This means that we don't have to use overlarge fn values
// to get a curve smooth enough for our printer and make renders take forever.
// fn is the number of steps (out of 360 degrees or tau (2*pi) radians) to approximate
// the curve over, so the angular distance between steps (theta) is given by tau/fn.
// >>> theta = tau/fn
// Rearranging for fn
// >>> fn = tau/theta
// tau is constant, so we need to find the maximum angle theta to get our max step
// length. Law of Cosines to the rescue!
// >>> c^2 = a^2 + b^2 - 2ab*cos(gamma)
// In this case c is our step length (l), a and b are both the curve's radius, and
// gamma is our angular distance theta.
// Simplilfying:
// >>> l^2 = 2*r^2 * (1 - cos(theta))
// Solve for theta:
// >>> theta = acos( 1 - (l^2 / 2*r^2) )
// The rest is implementation
var fn = Math.ceil(tau/Math.acos(1 - max*max/(2*radius*radius)));
return fn;
// testing code below
var length = Math.sqrt(2*radius*radius*(1 - Math.cos(tau/fn)));
echo("fn " + fn + " at radius " + radius + " gives step length:" + length);
};
function main() {
var ri = 2;
var ro = 12;
var max_step = 0.4;
return [
torus({
ri: ri,
fni: fnByMaxStepDistance(max_step, ri),
ro: ro,
fno: fnByMaxStepDistance(max_step, ro),
})
];
}
@hovissimo
Copy link
Author

The contents of this gist are freely available for anyone to use for any purpose. Attribution is appreciated, but not required.

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