#Code to turn a 3d function in R into a stl file that can be 3D printed #Right now requires an additional step in MeshLab to make the surface into a solid. #Do Filter->Remeshing->Uniform Mesh Resampling with settings: precision at 1.0%, #offset at 53.0%, and absolute distance selected #Laura Perovich Oct 2012 #COMPOSITES WEEK #Make some mini pots for plants--try a jello one. #I just need/want a one sided mold for this one #Dimensions will be set in Partsworks3D #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) } ########################################################################## ############## SOMETHING SUPER EASY ###################################### ########################################################################## #Plant pots. drawScene(surfaceTriangles(seq(-1,1,len=60), seq(-1,1,len=60), function(x, y) ifelse(x^2+y^2<.8+0.1*sin(3*pi*(x+y)), (x^3 + y*x), ifelse(x^2+y^2>=.8+0.1*sin(3*pi*(x+y)), 1, NA)), color2 = "green")) drawScene(surfaceTriangles(seq(-8,8,len=200), seq(-8,8,len=200), plantsII, color2="green")) #Make it a rectangle with sloped edges up #plants<-function(x, y) ifelse(x>=.8 & y<=.8, x, ifelse(y>=.8 & x<.8, y, ifelse(x<=(-.8) & y>=-.8, abs(x), ifelse(y<=(-.8), abs(y), ifelse(x<=.8 & y<=.8 & x>=-.8 & y>=-.8, .8, abs(x)))))) a<-.6 b<-.95 center<-c(0,0) circleone<-function(x,y, c1, c2) (x-c1)^2+(y-c2)^2<=a^2 circletwo<-function(x,y, c1, c2) (x-c1)^2+(y-c2)^2<=b^2 coneone<-function(x,y, c1, c2) sqrt((x-c1)^2+(y-c2)^2) #One pot version #plants<-function(x, y) ifelse(x^2+y^2<=.7^2, .7, ifelse(x^2+y^2<=.95^2, sqrt(x^2+y^2), NA)) #plants<-function(x, y) ifelse(circleone(x,y, 0, 0), a, ifelse(circletwo(x,y,0,0), coneone(x, y, 0,0), NA)) #Draw a few of them so I can mess some up plantsII<-function(x,y) ifelse(circleone(x,y, 0, 0), a, ifelse(circletwo(x,y,0,0), coneone(x, y, 0,0), ifelse(circleone(x,y, 2, 2), a, ifelse(circletwo(x,y,2,2), coneone(x,y, 2, 2), ifelse(circleone(x,y, 4, 4), a, ifelse(circletwo(x,y,4,4), coneone(x,y, 4, 4), NA)))))) triA<-surfaceTriangles(seq(-8,8,len=200), seq(-8,8,len=200), plantsII) fv<-makefacet(triA) makestl("/media/6AD8BBE2D8BBAAA9/Users/perovich/Documents/MIT/HTMAA/week10/plantThree.stl", fv)