R/muscle.plot.stl.R

Defines functions muscle.plot.stl

Documented in muscle.plot.stl

muscle.plot.stl <-
function(fiber.list, res = 1, df = 2, radius = 1, cols=NULL, 
                          save.plot=FALSE, file.name="muscle.fibers", mirror.axis=FALSE,type="stl", ...){
  open3d()
  if(is.null(cols)){
    cols=rep("blue", length(fiber.list))
  }
  
  if(type=="ply"){  warning("PLY can be extremely slow, especially if saving to file!", immediate. = TRUE)
  }
  if(type=="ply" & res==1){res<-0.02}
  
  for(j in 1:length(fiber.list)){
    fiber.dat<-fiber.list[[j]]$fiber.points
    fiber.dat<-fiber.dat*res
    if(mirror.axis){
      fiber.dat[,2]<-fiber.dat[,2]*-1
    }
    fb.df <- data.frame(x = fiber.dat[, 1], y = fiber.dat[,2], z = fiber.dat[, 3])
    if(df==1){ fit <- tryCatch(lm(cbind(x,y)~z, data = fb.df))
    } else {
      fit <- tryCatch(lm(cbind(x,y)~nsp(z,df=df), data = fb.df))}
  
    if(type=="stl"){
      lines3d(cbind(predict(fit, newdata = list(z = fiber.dat[,3])), fiber.dat[,3]),col=cols[j], alpha=0.8)
    }
    if(type=="ply"){
      vertices<-cbind(predict(fit, newdata = list(z = fiber.dat[,3])), fiber.dat[,3])
      cly<- cylinder3d(vertices,color=cols[j],radius=radius*res/2,sides=8,closed = -2, ...)
      shade3d(cly) 
      wire3d(cly, col=cols[j])
      
    }
      }
    
  
  
  if(save.plot){
    if(type=="stl"){
    writeSTL(paste0(file.name,".stl"))
    }
    if(type=="ply"){
      writePLY(paste0(file.name,".ply"),withColors=TRUE)
    }
  }
  
}

Try the GoodFibes package in your browser

Any scripts or data that you put into this service are public.

GoodFibes documentation built on April 27, 2026, 9:09 a.m.