Last active
August 29, 2015 14:02
-
-
Save kintel/4645e2c91eb02e577688 to your computer and use it in GitHub Desktop.
Adaptation of misc code to use list comprehensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use <loft.scad> | |
use <helpers.scad> | |
function drop(t) = 100 * 0.5 * (1 - cos(180 * t)) * sin(180 * t) + 1; | |
function path(t) = [0, 0, 80 + 80 * cos(180 * t)]; | |
function rotate(t) = 180 * pow((1 - t), 3); | |
$fn=12; | |
function circle(r) = [for (a=[0:360/$fn:360-360/$fn]) [r * sin(a), r * cos(a), 0]]; | |
function shape() = circle(1); | |
step = 0.01; | |
path_transforms = [for (t=[0:step:1-step]) m_translate(path(t)) * m_rotate([0,0,rotate(t)]) * m_scale([drop(t), drop(t), 1])]; | |
loft(shape(), path_transforms); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use <loft.scad> | |
use <helpers.scad> | |
function f(t) = [ | |
(t / 1.5 + 0.5) * 100 * cos(6 * 360 * t), | |
(t / 1.5 + 0.5) * 100 * sin(6 * 360 * t), | |
200 * (1 - t) | |
]; | |
function shape() = [ | |
[-10, -1, 0], | |
[-10, 6, 0], | |
[ -7, 6, 0], | |
[ -7, 1, 0], | |
[ 7, 1, 0], | |
[ 7, 6, 0], | |
[ 10, 6, 0], | |
[ 10, -1, 0]]; | |
step = 0.005; | |
path = [for (t=[0:step:1-step]) f(t)]; | |
path_transforms = construct_transform_path(path); | |
loft(shape(), path_transforms); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use <loft.scad> | |
use <helpers.scad> | |
function func0(x)= 1; | |
function func1(x) = 30 * sin(180 * x); | |
function func2(x) = -30 * sin(180 * x); | |
function func3(x) = (sin(270 * (1 - x) - 90) * sqrt(6 * (1 - x)) + 2); | |
function func4(x) = 180 * x / 2; | |
function func5(x) = 2 * 180 * x * x * x; | |
function func6(x) = 3 - 2.5 * x; | |
function square(size) = [[-size/2,-size/2,0], [-size/2,size/2,0], [size/2,size/2,0], [size/2,-size/2,0]]; | |
pathstep = 1; | |
height = 100; | |
shape_points = square(10); | |
path_transforms1 = [for (i=[0:pathstep:height]) let(t=i/height) m_translate([func1(t),func1(t),i]) * m_rotate([0,0,func4(t)])]; | |
path_transforms2 = [for (i=[0:pathstep:height]) let(t=i/height) m_translate([func2(t),func2(t),i]) * m_rotate([0,0,func4(t)])]; | |
path_transforms3 = [for (i=[0:pathstep:height]) let(t=i/height) m_translate([func1(t),func2(t),i]) * m_rotate([0,0,func4(t)])]; | |
path_transforms4 = [for (i=[0:pathstep:height]) let(t=i/height) m_translate([func2(t),func1(t),i]) * m_rotate([0,0,func4(t)])]; | |
loft(shape_points, path_transforms1); | |
loft(shape_points, path_transforms2); | |
loft(shape_points, path_transforms3); | |
loft(shape_points, path_transforms4); | |
path_transforms5 = [for (i=[0:pathstep:height]) let(t=i/height) m_translate([0,0,i]) * m_scale([func3(t),func3(t),i]) * m_rotate([0,0,func4(t)])]; | |
translate([100, 0, 0]) | |
loft(shape_points, path_transforms5); | |
path_transforms6 = [for (i=[0:pathstep:height]) let(t=i/height) m_translate([0,0,i]) * m_scale([func6(t),func6(t),i]) * m_rotate([0,0,func5(t)])]; | |
translate([-100, 0, 0]) | |
loft(shape_points, path_transforms6); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Linalg helpers | |
function norm(x) = sqrt(x*x); | |
function unit(x) = x/norm(x); | |
function cross(x,y) = [x[1]*y[2]-x[2]*y[1], x[2]*y[0]-x[0]*y[2], x[0]*y[1]-x[1]*y[0]]; | |
function rot_trace(m) = m[0][0] + m[1][1] + m[2][2]; | |
function rot_angle(m) = acos((rot_trace(m)-1)/2); | |
function rot_axis(m) = 1 / (2*sin(rot_angle(m))) * [m[2][1] - m[1][2], m[0][2] - m[2][0], m[1][0] - m[0][1]]; | |
function take_3(v) = [v[0],v[1],v[2]]; | |
function rotation_part(m) = [take_3(m[0]),take_3(m[1]),take_3(m[2])]; | |
function translation_part(m) = [m[0][3],m[1][3],m[2][3]]; | |
function transpose_3(m) = [[m[0][0],m[1][0],m[2][0]],[m[0][1],m[1][1],m[2][1]],[m[0][2],m[1][2],m[2][2]]]; | |
function transpose_4(m) = [[m[0][0],m[1][0],m[2][0],m[3][0]], | |
[m[0][1],m[1][1],m[2][1],m[3][1]], | |
[m[0][2],m[1][2],m[2][2],m[3][2]], | |
[m[0][3],m[1][3],m[2][3],m[3][3]]]; | |
function construct_rt(r,t) = [concat(r[0],t[0]),concat(r[1],t[1]),concat(r[2],t[2]),[0,0,0,1]]; | |
function invert_rt(m) = construct_rt(transpose_3(rotation_part(m)), -(transpose_3(rotation_part(m)) * translation_part(m))); | |
function rotation_from_axis(x,y,z) = [[x[0],y[0],z[0]],[x[1],y[1],z[1]],[x[2],y[2],z[2]]]; | |
function identity3()=[[1,0,0],[0,1,0],[0,0,1]]; | |
function identity4()=[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]; | |
function column_3(m,j) = [m[0][j],m[1][j],m[2][j]]; | |
function m_translate(v) = [ [1, 0, 0, v.x], | |
[0, 1, 0, v.y], | |
[0, 0, 1, v.z], | |
[0, 0, 0, 1 ] ]; | |
function m_rotate(v) = [[ cos(v.z), -sin(v.z), 0, 0], | |
[ sin(v.z), cos(v.z), 0, 0], | |
[ 0, 0, 1, 0], | |
[ 0, 0, 0, 1] ] * | |
[[ cos(v.y), 0, sin(v.y), 0], | |
[ 0, 1, 0, 0], | |
[-sin(v.y), 0, cos(v.y), 0], | |
[ 0, 0, 0, 1] ] * | |
[[1, 0, 0, 0], | |
[0, cos(v.x), -sin(v.x), 0], | |
[0, sin(v.x), cos(v.x), 0], | |
[0, 0, 0, 1] ]; | |
function m_scale(v) = [ [v.x, 0, 0, 0], | |
[0, v.y, 0, 0], | |
[0, 0, v.z, 0], | |
[0, 0, 0, 1 ] ]; | |
function vec3(v) = [v[0], v[1], v[2]]; | |
function vec4(v) = [v[0], v[1], v[2], 1]; | |
// List helpers | |
function flatten(list) = [ for (i = list, v = i) v ]; | |
function range(r) = [ for(x=r) x ]; | |
function reverse(list) = [for (i = [len(list)-1:-1:0]) list[i]]; | |
function transform(m, list) = [for (p=list) vec3(m * vec4(p))]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use <helpers.scad> | |
function trajectory(translation, rotation, steps=2) = [for (i=[0:steps-1]) let (t=i/steps) m_translate(t*translation) * m_rotate(t*rotation)]; | |
function rotate_from_to(a,b,_axis=[]) = | |
len(_axis) == 0 | |
? rotate_from_to(a,b,unit(cross(a,b))) | |
: _axis*_axis >= 0.99 ? rotation_from_axis(unit(b),_axis,cross(_axis,unit(b))) * | |
transpose_3(rotation_from_axis(unit(a),_axis,cross(_axis,unit(a)))) : identity3(); | |
function make_orthogonal(u,v) = unit(u - unit(v) * (unit(v) * u)); | |
// Prevent creeping nonorthogonality | |
function coerce(m) = [unit(m[0]), make_orthogonal(m[1],m[0]), make_orthogonal(make_orthogonal(m[2],m[0]),m[1])]; | |
function tangent_path(path, i) = | |
i == 0 ? | |
unit(path[1] - path[0]) : | |
(i == len(path)-1 ? | |
unit(path[i] - path[i-1]) : | |
unit(path[i+1]-path[i-1])); | |
function construct_transform_path(path) = | |
[let (l=len(path)) | |
for (i=[0:l-1]) | |
construct_rt(rotate_from_to([0,0,1], tangent_path(path, i)), path[i])]; | |
module loft(shape, path_transforms, closed=false) { | |
pathlen = len(path_transforms); | |
segments = pathlen + (closed ? 0 : -1); | |
function loft_points() = | |
flatten([for (i=[0:pathlen]) transform(path_transforms[i], shape)]); | |
function loop_faces() = [let (facets=len(shape)) | |
for(s=[0:segments-1], i=[0:facets-1]) | |
[(s%pathlen) * facets + i, | |
(s%pathlen) * facets + (i + 1) % facets, | |
((s + 1) % pathlen) * facets + (i + 1) % facets, | |
((s + 1) % pathlen) * facets + i]]; | |
bottom_cap = closed ? [] : [[for (i=[len(shape)-1:-1:0]) i]]; | |
top_cap = closed ? [] : [[for (i=[0:len(shape)-1]) i+len(shape)*(pathlen-1)]]; | |
polyhedron(points = loft_points(), faces = concat(loop_faces(), bottom_cap, top_cap)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************** | |
* Superformula | |
* (c) 2014 Torsten Paul <Torsten.Paul@gmx.de> | |
* License: CC-BY-SA 3.0 | |
* | |
* See http://en.wikipedia.org/wiki/Superformula | |
*/ | |
// Display configuration | |
gridx = 50; | |
gridy = 50; | |
columns = 3; | |
height = 4; | |
angle_step = 2; | |
// Parameters of the superformula (with an additional scale | |
// factor to make the resulting objects roughly the same size). | |
a = [ 1, 1, 1, 1, 1, 1, 1000, 1, 3, 1, 1, 4]; | |
b = [ 1, 1, 1, 1, 1, 1, 200, 0.6, 2, 1, 3, 3]; | |
m = [ 3, 1, 5, 8, 16, 6, 52, 30, 6, 6, 6, 30]; | |
n1 = [4.5, 0.5, 2, 0.5, 0.5, 1, 8, 75, 1.5, 0.4, 3.8, 6]; | |
n2 = [ 10, 0.5, 7, 0.5, 0.5, 7, 3, 1.5, 0.5, 0, 16, 7]; | |
n3 = [ 10, 0.5, 7, 10, 16, 8, 2, 35, 2, 6, 10, 3]; | |
f = [ 10, 22, 8, 12, 10, 2, 3, 10, 8, 15, 0.8, 4]; // scale factor | |
// helper function | |
function r1(phi, idx) = pow(abs(cos(m[idx] * phi / 4) / a[idx]), n2[idx]); | |
function r2(phi, idx) = pow(abs(sin(m[idx] * phi / 4) / b[idx]), n3[idx]); | |
// main superformula returning the radius for a given angle phi | |
function r(phi, idx) = f[idx] * pow(abs(r1(phi, idx) + r2(phi, idx)), -1 / n1[idx]); | |
// convert polar coordinates to cartesian coordinates | |
function point(phi, idx) = [ r(phi, idx) * cos(phi), r(phi, idx) * sin(phi)]; | |
// function to collect all points in 360 degrees | |
function points(angle, idx) = [for (i=[0:angle_step:360-angle_step]) point(i, idx)]; | |
for (idx = [0 : len(m) - 1]) | |
translate([gridx * (idx % columns), gridy * floor(idx / columns), 0]) | |
linear_extrude(height = height, scale = 0) | |
polygon(points(0, idx)); |
View raw
(Sorry about that, but we can’t show files that are this big right now.)
View raw
(Sorry about that, but we can’t show files that are this big right now.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment