diff --git a/unwrap.scad b/unwrap.scad new file mode 100644 index 0000000..8dbb788 --- /dev/null +++ b/unwrap.scad @@ -0,0 +1,53 @@ +use <./vector.scad> + +function _unwrap_x_half(original,unwrapped=[],offset=0)= + let(length=len(original)) + let(half=(length-2)/2+1) + let(i=len(unwrapped)) + let(prev=original[i-1]) + let(cur=original[i]) + let(dx=cur[0]-prev[0]) + let(dz=cur[2]-prev[2]) + let(new=[offset+sqrt(pow(dx,2)+pow(dz,2)),cur[1],0]) + + i < half ? _unwrap_x_half(original,concat(unwrapped,[new]),new[0]) : concat(unwrapped,[new]) ; + +; + +function _unwrap_x(p)= + let(length=len(p)) + let(half=(length-2)/2+1) + let(first=_unwrap_x_half(p,[[p[0][0],p[0][1],0]],p[0][0])) + let(second=[ + for( i = [len(first):length-1]) + [p[length-i][0],p[i][1],0] + ]) + flatten_vec([first,second]) +; + + +function _unwrap_y_half(original,unwrapped,offset,s)= + let(length=len(original)) + let(half=(length-2)/2+1) + let(i=len(unwrapped)) + let(prev=original[i-1]) + let(cur=original[i]) + let(dx=cur[0]-prev[0]) + let(dy=cur[1]-prev[1]) + let(dz=cur[2]-prev[2]) + let(new=[cur[0],offset+s*sqrt(pow(dy,2)+pow(dz,2)),0]) + + i < half ? _unwrap_y_half(original,concat(unwrapped,[new]),new[1],s) : concat(unwrapped,[new]) ; + +; + +function _unwrap_y(p,s=1)= + let(length=len(p)) + let(half=(length-2)/2+1) + let(first=_unwrap_y_half(p,[[p[0][0],p[0][1],0]],p[0][1],s)) + let(second=[ + for( i = [len(first):length-1]) + [p[i][0],first[length-i][1],0] + ]) + flatten_vec([first,second]) +; diff --git a/wireframe.scad b/wireframe.scad new file mode 100644 index 0000000..c41312e --- /dev/null +++ b/wireframe.scad @@ -0,0 +1,48 @@ +module _rod(a, b, r) { + translate(a) sphere(r=r); + translate(b) sphere(r=r); + + dir = b-a; + h = norm(dir); + if(dir[0] == 0 && dir[1] == 0) { + // no transformation necessary + cylinder(r=r, h=h); + } + else { + w = dir / h; + u0 = cross(w, [0,0,1]); + u = u0 / norm(u0); + v0 = cross(w, u); + v = v0 / norm(v0); + multmatrix(m=[[u[0], v[0], w[0], a[0]], + [u[1], v[1], w[1], a[1]], + [u[2], v[2], w[2], a[2]], + [0, 0, 0, 1]]) + cylinder(r=r, h=h); + } + } + +module outline(points3,r=0.5) { + p=len(points3); + for(start = [0:1:p-1]) { + stop=(start+1)%p; + a = points3[start]; + b = points3[stop]; + // FIXME: Do not repeat points + echo(a,b); + _rod(a,b,r); + } +} + +module wireframe(points3,polygons,r=0.5) { + for(p=polygons) { + for(set = [0:1:len(p)-1]) { + a = points3[p[set]]; + b = points3[p[(set+1)%len(p)]]; + // FIXME: Do not repeat points + echo(p[set],p[(set+1)%len(p)],a,b); + _rod(a,b,r); + } + + } +}