Friday, August 24, 2018

Cad Exercises - Pipe

Revisiting Cad Exercises

source: https://goo.gl/xLKvB8.




$fn=60;
assembly();
module assembly()
{
    rotate([0,-45,0])translate([-225,0,0])flange();
    rotate([0,-45,0])translate([-225,0,0])flangerim();
    translate([-225,0,0])mirror([0,0,1])flange();
    rotate([90,0,0]) pipe();
}

module flange()
{
rotate_extrude()
    translate([0,-37]){
            hull(){
                polygon([[44,0],[112.5,0],[44,37.1]]);
                translate([102.5,27])circle(10,$fn=15);
            }
        fillet([68.5+15,37-0.5],[68.5-1,37],[68.5-1,37+15]);
// fillet is a tiny bit smaller to smooth with the pipe
        translate([44,36])square([23.5,15]);
    }
}

module flangerim()
{
rotate_extrude()
    translate([0,-37]){
    translate([44,-10])square([65-44,10]);
}
}

module pipe()
{
rotate_extrude(angle =45,$fn=60, convexity = 20)
    translate([-225,0,0])
    difference(){
        circle(68.5);
        circle(44);
    }
}

module fillet(a,b,c)
{
difference(){
    polygon([a,b,c]);
    translate(a+(c-b))circle(norm(a-b),$fn=24);
}}



 



Friday, June 22, 2018

Flower Shape Path / Spirograph

Generates a path in the shape of a Spirograph flower.
Compounding rotations. 
Self intersecting path for most values.
Usage:



function flower(r1=100,r2,r3,v1,v2 ,steps=200)=
[ for(t=[0:1/steps:1])

let(
x =r1* sin(t*360) +r2* sin(t*v1*360)+r3* sin(t*v2*360),  
y =r1*cos(t*360) +r2*cos(t*v1*360)  +r3*cos(t*v2*360)   )
[x,y] ];
// function close adds a last point equal yo the first

function close(p)= concat(p,[p[0]]);
 
function rnd(a = 1, b = 0, s = []) = s == [] ? 
(rands(min(a, b), max(
  a, b), 1)[0]) : (rands(min(a, b), max(a, b), 1, s)[0]);
function un(v) = v / max(1e-15, norm(v));
 
color(un([round(rnd(3)) ,round(rnd(3))  *0.75 ,round(rnd(3))  ])) {
paramas=[
70,rnd(30),rnd(15),
round(rnd(3,17))*(round(rnd(-1,1))==1?1:-1),
round(rnd(3,20)*(round(rnd(-1,1))==1?1:-1))  ,300];
echo(paramas);
p=close(flower(paramas[0],paramas[1],paramas[2],
paramas[3],paramas[4],paramas[5],paramas[6]));
  
polygon(p);
}
 

Wednesday, June 13, 2018

Flower Shape Path / Fourier series

Generates a path 
in the shape of a Fourier series flower.
Compounding random amplifications for each octave.
random number of octaves and repeating symmetries 
Non Self intersecting path for any values. 


Usage:


function FlowerF(r=100,isteps=5,w,a,h=2,h2)=
let(steps=min(isteps*w,300))
[  for(t=[0:1/steps:1])
let( r2= r*1.05+vsum([
for (k = [h2:h:max(9,22-w)])     a[k]*(sin(k*t*w*360)/k)*r] ),
  x =r2* sin(t*360), 
 y =r2* cos(t*360)  )
[x,y] ];




module polyline(p) {for(i=[0:max(0,len(p)-2)])line(p[i],p[i+1]);}
module line(p1, p2 ,width=0.5) 
{
    hull() {
        translate(p1) sphere(width);
        translate(p2) sphere(width);
    }
}

a=concat([rnd(0.5),rnd(0.75)],[for (k = [0:1:30])rnd(1)]);
p= (FlowerF(100,200,
    round(rnd(0,3))*2+1,a,
     min(2,round(rnd(1,3))),min(2,round(rnd(1,3)))));
 
polygon(p); 
//polyline(p); 
// function close adds a last point equal yo the first function close(p)= concat(p,[p[0]]); function rnd(a = 1, b = 0, s = []) = s == [] ? (rands(min(a, b), max( a, b), 1)[0]) : (rands(min(a, b), max(a, b), 1, s)[0]); function vsum(l) = len(l) > 0 ? [ for(li=l) 1 ] * l : undef;



 



Saturday, March 10, 2018

Idea for laparoscopic manipulation inside space helmet

  

Laparoscopic instruments can possibly be used by an astronaut to manipulate items inside its helmet, consume food or clean up. Leaks is an obvious challenge during use but can likely be avoided in in the stowed position. 

 

 

Sunday, February 11, 2018

Morphing between two 2D shapes with the same number of points.

Morph from one set (of points) to another.
Softcurve()makes transition ease in and out.


for(i=[-0:0.1:1])
{
morphed=lerp(rabbit(),elephant(),softcurve(i)) ;
translate([i*200,0,0])
polygon( morphed);
}

function lerp(start,end,bias)=(end*bias+start*(1-bias));
function softcurve(a)=let(b=clamp(a))(b*b*(3-2*b));
function clamp(a,b=0,c=10)=min(max(a,b),c);
  

function rabbit()=[[-4.76,-3.76],[-5.76,-5.76],[-4.76,-9.76],[0.24,-9.76],[4.24,-9.76],[6.24,-5.76],[6.24,-3.76],[4.24,-2.76],[2.24,-1.76],[4.24,-0.76],[5.24,1.24],[3.24,3.24],[2.24,3.24],[3.24,5.24],[3.24,7.24],[1.24,6.24],[0.24,3.24],[-0.76,3.24],[-1.76,3.24],[-2.76,6.24],[-7.76,6.24],[-4.76,4.24],[-3.76,2.24],[-5.76,0.24],[-3.76,-1.76]]
+[for(i=[0: (24) ])rands(-1,1,2)]*0.5;
function elephant()=[[-11,-5.84],[-8,-5.84],[-8,-0.84],[-4,-1.84],[0,-1.84],[1,-0.84],[1,-5.84],[4,-5.84],[4,-0.84],[7,-4.84],[10,-4.84],[10,-2.84],[8,-2.84],[6,0.16],[8,4.16],[6,7.16],[3,6.16],[1,9.16],[-2,8.16],[-2,5.16],[1,4.16],[-3,4.16],[-10,3.16],[-11,-0.84],[-11,-5.84]]
+[for(i=[0: (24) ])rands(-1,1,2)]*0.5;


Monday, December 18, 2017

Unique With Order Preserved

Remove duplicates from a list with order preserved.


function unique(m)=
     [for(i=[0:len(m)-1])if(search([m[i]],m,1)==[i])m[i]];
 

Friday, December 15, 2017

Clamp For List

Clamp every number in a list structure between two boundary numbers.

echo(clamplist([10,-.20,03,40,21,1,0,0.6,-2]));

function clamplist(v,b=0,c=1) = 
len(v) == undef ? min(max(v,min(b,c)),max(b,c)): 
len(v) == 0 ? [] : 
[for (i = v) clamplist(i,b,c)];

 

Tuesday, December 12, 2017

Select random item from a list

Select random item from a curated collection.

My_selection=sel_rnd( [1, 3, 0.5, 100, sin(2)]  );

function sel_rnd(v)=v[floor(rnd(len(v)))];

 

Friday, December 1, 2017

Fit a 4 point bezier curve for desired length

module to draw a simple 4 point Bez curve. 
function for piece-wise length  estimation. 

And a recursive function to fit control points for the length of your pleasure. 

function AdjustBezier(v,l, precision = 0.00001)=
l<norm(v[0]-v[3])?
let(e=echo("Cant be that short, sorry"))[v[0],v[0] ,v[3] ,v[3]]:
let(
current_lenght=len3bz(v),
error=l/current_lenght,
e=echo(l,current_lenght,error),
new_v=[v[0],v[0]+(v[1]-v[0])*error,v[3]+(v[2]-v[3])*error,v[3]]
)
abs(1-error)>precision?AdjustBezier(new_v,l):v;



function len3bz(v, precision = 0.001, t = 0, acc = 0) =
 t > 1 ? acc : 
len3bz(v, precision, t + precision, acc 
+ norm(bez2(t, v) - bez2(t + precision, v)));

function bez2(t, v) = (len(v) > 2) ? bez2(t, [
  for (i = [0: len(v) - 2]) v[i]* (t)  + v[i + 1] * (1 - t)
]): v[0]* (t)  + v[1]* (1 - t) ;

module ShowControl(v) { // translate(t(v[0])) sphere(v[0][3]);
 
    for (i = [1: len(v) - 1]) {
      // vg  translate(t(v[i])) sphere(v[i][3]);
      hull() {
        translate(t(v[i])) sphere(0.5);
        translate(t(v[i - 1])) sphere(0.5);
      }
    }
}

module ShowBezier(v,steps=50) 
{ // translate(t(v[0])) sphere(v[0][3]);
 step=1/steps;
    for (i = [-step:step:  1+step]) {
      // vg  translate(t(v[i])) sphere(v[i][3]);
      hull() {
        translate(t(bez2(clamp(i), v) ))sphere(1);
        translate(t(bez2(clamp(i+step), v))) sphere(1);
      }
    }
}


function t(v) = [v.x, v.y, v.z];
function clamp(a, b = 0, c = 1) = min(max(a, b), c);

MyBezier=[[0,0,0],[0,0,10],[50,0,50],[50,10,50]];
Newlengh=175;

ShowControl(MyBezier) ; 
ShowBezier(MyBezier) ;
echo(len3bz(MyBezier));

MyNewBezier=AdjustBezier(MyBezier,Newlengh) ;

color("red"){
ShowControl(MyNewBezier) ; 
ShowBezier(MyNewBezier) ;
echo(len3bz(MyNewBezier));}

Wednesday, October 11, 2017

0 - 1 Remapping Functions: Compressor / Expander / Biramp

/* remapping functions */

v1=rnd();v2=rnd(); 

for(i=[-0:1/160:1])translate([i,0])square([1/160,  ((  comp(i,v1,v2)))]);
    function comp(i,c=0.5,s=0.75)=i<c? ramp(i,0,c,0,s) :ramp(i,c,1,s,1) ;
    function ramp(v,bi1=1/3,bi2=2/3,start=1/3, end=2/3) = 
        let(b1=min(bi1,bi2),b2=max(bi1,bi2))
        v<=b1?start:v<=b2?lerp(start,end,(v-b1)/(b2-b1))    :end;
     function lerp(start, end, bias) = (end * bias + start * (1 - bias));

function rnd(a = 1, b = 0, s = []) = 
  s == [] ? 
   (rands(min(a, b), max(   a, b), 1)[0]) 
  : 
   (rands(min(a, b), max(a, b), 1, s)[0])
  ; 



Monday, October 9, 2017

0 - 1 Remapping Functions: Map / BiSignMap / InfMap

/* remapping functions */


for(i=[-1:1/160:1])translate([bisignmap(i),0])square([1/160,  map(infmap(i*12) )   ]);

// Map takes a number in some range 
// and remaps it into the desired range
function  map(  value,istart=-1, istop=1, ostart=0, ostop=1) =
ostart + (ostop - ostart) * ((value - istart) / (istop - istart));

// BiSignMap takes a number in the range -1 - 1
// and remaps it into the range 0 - 1.
function bisignmap(i)=(i+1)/2;

// InfMap takes a number in the range -inf - inf
// and remaps it into the range -1 - 1 . 
function infmap(i)=max(0,1-(1/(abs(i)+1)))*sign(i);

Saturday, October 7, 2017

0 - 1 Remapping Functions: Smooth Step 2

/* remapping functions */


for(i=[-0:1/160:1])translate([i,0])square([1/160,   ( (   smooth2(i) )) ]);
     function smooth2(x,k=5,n=4) =1- (exp( -k*pow(x,n) ));
 

Compared to smooth step

Thursday, October 5, 2017

0 - 1 Remapping Functions: Power Smooth / Inverse Power Smooth

/* remapping functions */



for(i=[-0:1/160:1])translate([i,0])square([1/160,   ( (   powersmooth(i) )) ]);
     function powersmooth(x) =       ((x+ x-ipowersmooth(x))-0.5)*0.9+0.5 ;

for(i=[-0:1/160:1])translate([i,0])square([1/160,   ( (   ipowersmooth(i) )) ]);
    function ipowersmooth(x) =  clamp(pow(x-0.5,3)*4+0.5);
    function clamp(a, b = 0, c = 1) = min(max(a, b), c);
 


Tuesday, October 3, 2017

0 - 1 Remapping Functions: Powers 10 to -10

/* remapping functions */



for(i=[-0:1/160:1])translate([i,0])square([1/160,   ( (   power(i,10) )) ]);
     function power(x,n) = n<0?pow(x,1/abs(min(-1,n-1) )):pow(x,max(1,n+1))   ;


for(i=[-0:1/160:1])translate([i,0])square([1/160,   ( (   power(i,-10) )) ]);
     function power(x,n) = n<0?pow(x,1/abs(min(-1,n-1) )):pow(x,max(1,n+1))   ;
 


Sunday, October 1, 2017

0 - 1 Remapping Functions: Multiple Smooth Steps

/* remapping functions */


for(i=[-0:1/160:1])translate([i,0])square([1/160,   (smooths(  smooth(i),3 )) ]);
    function smooths(v,steps) =smooth( smooth(mods(v,steps) ))/steps+mstep(v,steps);
    function smooth (a) =let (b = clamp(a))(b * b * (3 - 2 * b));
    function clamp(a, b = 0, c = 1) = min(max(a, b), c);
    function mstep(v,steps=3) = (floor(v*steps)/steps );
    function mods(v,steps=3) = ( (v*steps)%1);
 


Friday, September 29, 2017

0 - 1 Remapping Functions: Inverse Cosine-Wave

/* remapping functions */


for(i=[-0:1/160:1])translate([i,0])square([1/160,    (icosw(  (i))) ]);
     function icosw(i)=-cos(i*360)/2+0.5+i;
 

 

Monday, September 25, 2017

0 - 1 Remapping Functions: Cosine-Wave

/* remapping functions */


for(i=[-0:1/160:1])translate([i ,  min(0,cosw(i))])
                 square([1/160,   abs(cosw(  (i))) ]);
    function cosw(i)=cos(i*360)/2 -0.5+i;
 

 

Saturday, September 23, 2017

0 - 1 Remapping Functions: Inverse Sine-Wave

/* remapping functions */


for(i=[-0:1/160:1])translate([i,  min(0,isinw(i))])
                square([1/160,   abs(isinw(  (i))) ]);
    function isinw(i)=gauss(i)-sin(i*360)/2;
 
    function gauss(x) =       x + (x - smooth(x));
    function smooth (a) =let (b = clamp(a))(b * b * (3 - 2 * b));
    function clamp(a, b = 0, c = 1) = min(max(a, b), c);

 

Thursday, September 21, 2017

0 - 1 Remapping Functions: Sine-Wave

/* remapping functions */


for(i=[-0:1/160:1])translate([i,0])square([1/160,    (sinw(  (i))) ]);
    function sinw(i)=sin(i*360)/2+smooth(i);
    function gauss(x) =       x + (x - smooth(x));
    function smooth (a) =let (b = clamp(a))(b * b * (3 - 2 * b));
    function clamp(a, b = 0, c = 1) = min(max(a, b), c);

 

Tuesday, September 19, 2017

0 - 1 Remapping Functions: Multiple Arcs 2

/* remapping functions */

for(i=[-0:1/160:1])translate([i,0])square([1/160,   (arcs2(  (i),3)) ]);
    function arc1 (x,n=0) = let(a= n>0? arc1(x,(n-1)):x) clamp(sqrt(1-(1-a)*(1-a)));
    function arc2 (x,n=0) =let(a= n>0? arc2(x,(n-1)):x) clamp( 1-sqrt(1-(a)*(a)));
    function arcs1(v,steps=3) = arc1(mods(v,steps),1)/steps+mstep(v,steps);
    function arcs2(v,steps=3) = arc2(mods(v,steps),1)/steps+mstep(v,steps);
    function clamp(a, b = 0, c = 1) = min(max(a, b), c);
    function mstep(v,steps=3) = (floor(v*steps)/steps );
    function mods(v,steps=3) = ( (v*steps)%1);