setwd()
library(Morpho)
library(rgl)
library(geomorph)

###################################################################
####################Complete Femur##################################
###################################################################

template.lm <- read.pts("./Femur_ANG10_90_Lsym.pts")
template.mesh <- ply2mesh(file="./Femur_ANG10_90_Lsym.ply")

# Single landmarks
fixed <- as.integer(c(1:25))
# Sliding along surfaces
patch <- as.matrix(template.lm[125:619,])
# Sliding along curves
template.lm2 <- template.lm[1:124,]
# All sliding semi landmarks
slidings <- as.integer(c(26:619))
# Sliding along surfaces
surface <- as.integer(c(125:619))
# Femoral head
SC1d <- as.integer(c(26:30))
SC2d <- as.integer(c(31:34))
# greater trochanter
SC3d <- as.integer(c(35:38))
SC4d <- as.integer(c(39:42))
# Lesser trochanter
SC5d <- as.integer(c(43:47))
SC6d <- as.integer(c(48:51))
# proxi-medial depression of the fourth trochanter
SC7d <- as.integer(c(52:59))
SC8d <- as.integer(c(60:67))
# fourth trochanter
SC9d <- as.integer(c(68:75))
SC10d <- as.integer(c(76:79))
#Mediodistal crest
SC11d <- as.integer(c(80:88))
SC12d <- as.integer(c(89:92))
SC13d <- as.integer(c(93:99))
SC14d <- as.integer(c(100:107))
# Anterior outline of the lateral condyle
SC15d <- as.integer(c(108:116))
SC16d <- as.integer(c(117:124))                      


# All curves

curves <- list(SC1d,SC2d,SC3d,SC4d,SC5d,SC6d,SC7d,SC8d,SC9d,SC10d,SC11d,SC12d,SC13d,SC14d,SC15d,SC16d)

# atlas list(curves)

atlas <- createAtlas(template.mesh, as.matrix(template.lm2), patch = patch, corrCurves = curves, patchCurves = NULL, keep.fix = fixed)
plotAtlas(atlas, pt.size = 2, render = "s", point="s", legend=F)


### SLIDING 

datadef <- list.files("./Landmark/Femur/Complet",full.names=T, pattern=".pts")
def <- lapply(datadef,read.pts)

array.lm <- list2array(def)
lab<-as.factor(datadef)
lab<-substr(lab, start=26, stop=60)
lab<-gsub(".pts", "", lab)
dimnames(array.lm)[[3]]<-lab

# projection

patched <- placePatch(atlas, array.lm, path="./Dossier_3D/Femur/Complet", inflate=20)

checkLM(patched, path="./Dossier_3D/Femur/Complet/", atlas = atlas, render = "s", pt.size=2)

# spline relaxation between all specimens
template.tot <- rbind(as.matrix(template.lm2),patch)

relaxed.template <- array(dim=dim(patched))

for (i in 1:dim(patched)[3]){relaxed.template[,,i]<-relaxLM(patched[,,i], template.tot, 
                                 slidings, outlines = curves,
                                 surp = surface, sur.name= paste("./Dossier_3D/Femur/Complet/", dimnames(patched)[[3]][i],".ply",sep=""), 
                                 mesh = NULL, tol = 1e-09,
                                 deselect = FALSE, inc.check = TRUE, iterations = 5, fixRepro = TRUE,
                                 missing = NULL, bending = TRUE, stepsize = 1,
                                 use.lm = NULL)}
dimnames(relaxed.template)[[3]]<-dimnames(patched)[[3]]

checkLM(relaxed.template, path="./Dossier_3D/Femur/Complet/", atlas=atlas,render="s",alpha=1, pt.size=2)

#Spline relaxation between all sepcimens and the template

slided<-slider3d(relaxed.template, slidings, outlines = curves, surp = surface,
                 sur.path = "./Dossier_3D/Femur/Complet", sur.name = NULL, meshlist = NULL, ignore = NULL,
                 sur.type = "ply", tol = 1e-07, deselect = FALSE, inc.check = T,
                 recursive = TRUE, iterations = 2, initproc = TRUE,
                 pairedLM = 0,mc.cores = 2,
                 fixRepro = TRUE) 

checkLM(slided$dataslide, path="./Dossier_3D/Femur/Complet/", atlas=atlas, render="s", pt.size=2)

save(slided, file= "./Landmark/Femur/Complet/Slide/slided_fc_red")