#Code for 3D function->stl files for molding and casting #stl creation functions similar to week 4 files #Laura Perovich Oct 2012 #Load package misc3d that includes surfaceTriangles function library(misc3d) #Define character constants used in the stl files tristart1<-"facet normal 0 0 0" tristart2<-" outer loop" triend1<-" endloop" triend2<-"endfacet" startline1<-"+" startline2<-" solid LAURA" endline<-" endsolid LAURA" #Make a function that will make each facet from data returned from surfaceTriangles applied to a function #(probably a more elegant way to do this?) makefacet<-function(data){ facetvector<-c() for (i in 1:nrow(data[[1]])){ v1<-paste(" vertex", as.character(data[[1]][i, 1]), as.character(data[[1]][i, 2]), as.character(data[[1]][i, 3]), sep=" ") v2<-paste(" vertex", as.character(data[[2]][i, 1]), as.character(data[[2]][i, 2]), as.character(data[[2]][i, 3]), sep=" ") v3<-paste(" vertex", as.character(data[[3]][i, 1]), as.character(data[[3]][i, 2]), as.character(data[[3]][i, 3]), sep=" ") facetvector<-c(facetvector, tristart1, tristart2, v1, v2, v3, triend1, triend2) } return(facetvector) } #Make a function that puts the facets together with the file headers and writes it out makestl<-function(location, facetvector){ fileConn<-file(location) writeLines(c(startline1, startline2, facetvector, endline), fileConn) close(fileConn) } ########################################################################## ######################## TRY A LEAF ###################################### ########################################################################## #What is a leaf? Simplify it to a function bounded by a oval-y sine-y curve. #Functions for leaf (also visuals) myleaf<-function(x, y) ifelse(x^2+y^2<.6+0.15*(sin(3*pi*x)+sin(3*pi*y)), (2.2+.9*x^3 + y*x+.9*y^3), ifelse(x^2+y^2>=.6+0.15*(sin(3*pi*x)+sin(3*pi*y)), 1, NA)) drawScene(surfaceTriangles(seq(-1,1,len=60), seq(-1,1,len=60),myleaf)) myleafhole<-function(x, y) ifelse(x^2+y^2<.6+0.15*(sin(3*pi*x)+sin(3*pi*y)), (2.2+.9*x^3 + y*x+.9*y^3), ifelse(x^2+y^2>=.6+0.15*(sin(3*pi*x)+sin(3*pi*y)) & -.1=.6+0.15*(sin(3*pi*x)+sin(3*pi*y)) & .27=.6+0.15*(sin(3*pi*x)+sin(3*pi*y)), -1.6, NA)) drawScene(surfaceTriangles(seq(-1,1,len=60), seq(-1,1,len=60),negmyleaf)) myleafoutcome<-function(x, y) ifelse(x^2+y^2<.6+0.15*(sin(3*pi*x)+sin(3*pi*y)), (2.2+.9*x^3 + y*x+.9*y^3), NA) drawScene(surfaceTriangles(seq(-1,1,len=60), seq(-1,1,len=60), myleafoutcome)) #MAKE ONE SIDE OF THE LEAF triA<-surfaceTriangles(seq(-1,1,len=150), seq(-1,1,len=150), myleafhole) fv<-makefacet(triA) makestl("/media/6AD8BBE2D8BBAAA9/Users/perovich/Documents/MIT/HTMAA/week6/function5T.stl", fv) #MAKE THE OTHER SIDE OF THE LEAF #The surface needs to be the negative of the original surface to survive the xy flip and stay parallel #The silicon negative should be deeper and the silicon positive shallower triA<-surfaceTriangles(seq(-1,1,len=150), seq(-1,1,len=150), negmyleaf) fv<-makefacet(triA) makestl("/media/6AD8BBE2D8BBAAA9/Users/perovich/Documents/MIT/HTMAA/week6/function5B.stl", fv) #SOMETHING EASY mediumf<-function(x,y) ifelse(x^2+y^2<1 & x^2+y^2>.4, -sqrt(x^2+y^2), ifelse(x^2+y^2<=.4, -.3, NA)) drawScene(surfaceTriangles(seq(-1,1,len=80), seq(-1,1,len=80), mediumf)) triA<-surfaceTriangles(seq(-1,1,len=150), seq(-1,1,len=150), mediumf) fv<-makefacet(triA) makestl("/media/6AD8BBE2D8BBAAA9/Users/perovich/Documents/MIT/HTMAA/week6/function6.stl", fv) #