CSE 477 / G. Farin
3Axes and 5Axes Cutter Motions

We simulate the motions of 3-axes and 5-axis cutters.

(* a control mesh: *)
bbb = {Table[{i,j,-i^2/5+i-Sin[2i*j]},{i,4},{j,4}],
	{{{0,0,0},{1,0,.3},{2,0,.3},{4,0,0}},
	{{0,1,0},{1,1,.3},{2,1,.3},{4,1,0}},
	{{3,3,0},{3.5,3,.3},{3.7,3,.3},{4,3,0}},	{{4,6,.2},{4,6,.2}+0.00001{RandomReal[],RandomReal[],RandomReal[]},{4,6,.2},{4,6,.2}}} (* to simulate the VW machining accident *)
};

bb=bbb[[2]];


center=Sum[Sum[bb[[i,j]],{i,4}],{j,4}]/16;
Do[bb[[i,j]]=bb[[i,j]]-center,{i,4},{j,4}];
dists=Table[Norm[bb[[i,j]]-center],{i,4},{j,4}];
radius=Max[dists];

surf[u_,v_]:= Sum[Sum[bb[[i,j]]*BernsteinBasis[3,i-1,u]*BernsteinBasis[3,j-1,v],{i,4}],{j,4}];
surfu[u_,v_]:= Sum[Sum[(bb[[i+1,j]]-bb[[i,j]])*BernsteinBasis[2,i-1,u]*BernsteinBasis[3,j-1,v],{i,3}],{j,4}];
surfv[u_,v_]:= Sum[Sum[(bb[[i,j+1]]-bb[[i,j]])*BernsteinBasis[3,i-1,u]*BernsteinBasis[2,j-1,v],{i,4}],{j,3}];
normal[u_,v_]:=-Cross[surfu[u,v],surfv[u,v]]/Norm[Cross[surfu[u,v],surfv[u,v]]];

netplot=Graphics3D[{PointSize[Large],Red,Map[Point,bb],Gray,Line[bb],Line[Transpose[bb]]}];
surfplot=Graphics3D[{Opacity[0.8],Orange,BSplineSurface[bb]}];
normalplot[u_,v_]:=Graphics3D[{Thick,Red,{Arrowheads[Large],Arrow[{surf[u,v],surf[u,v]+normal[u,v]}]}}];

cyl3axes[u_,v_]:=Graphics3D[{Brown,Cylinder[{surf[u,v],surf[u,v]+{0,0,2}},0.1]}]
cyl5axes[u_,v_]:=Graphics3D[{Red,Cylinder[{surf[u,v],surf[u,v]+normal[u,v]},0.2]}]

Manipulate[(

Show[{surfplot,netplot,If[toggle == 0,cyl3axes[u,v],cyl5axes[u,v]]},Boxed->False,ViewPoint->{-6,-4,7},PlotRange-> 0.5radius]),{u,0,1},{v,0,1},{toggle,0,1,1}]