options(repr.plot.width=3.5, repr.plot.height=3.5) options(warn=-1) # turns off warnings, to turn on: "options(warn=0)" library(Matrix) library(rgl) library(geometry) library(plotly) library(akima) library(plot3D) library(network) # Importing the libraries for (f in list.files(path="nt_toolbox/toolbox_general/", pattern="*.R")) { source(paste("nt_toolbox/toolbox_general/", f, sep="")) } for (f in list.files(path="nt_toolbox/toolbox_signal/", pattern="*.R")) { source(paste("nt_toolbox/toolbox_signal/", f, sep="")) } X = read_mesh("nt_toolbox/data/nefertiti.off")$X0 F = read_mesh("nt_toolbox/data/nefertiti.off")$F0 n = dim(X)[2] B = compute_boundary(F) p = length(B) options(repr.plot.width=3.5, repr.plot.height=5) # plot mesh plot_mesh(X,F) #plot boundary lines(X[1,B+1], X[2,B+1], col="red", lwd=3) W = Matrix(0, nrow=n, ncol=n, sparse=TRUE) for (i in (0:2)) { i2 = (i+1)%%3 i3 = (i+2)%%3 u = X[,(F+1)[i2+1,]] - X[,(F+1)[i+1,]] v = X[,(F+1)[i3+1,]] - X[,(F+1)[i+1,]] u = u/rbind(sqrt(colSums(u**2)), sqrt(colSums(u**2)),sqrt(colSums(u**2))); v = v/rbind(sqrt(colSums(v**2)), sqrt(colSums(v**2)),sqrt(colSums(v**2))); alpha = 1/(tan(acos(colSums(u*v)))) alpha = pmax(alpha, 10^(-2)*matrix(1,nrow=1, ncol=length(alpha))) W = W+sparseMatrix(x=alpha, i=(F+1)[i2+1,], j=(F+1)[i3+1,], dims=c(n,n)) W = W+sparseMatrix(x=alpha, i=(F+1)[i3+1,], j=(F+1)[i2+1,], dims=c(n,n)) } d = as.vector(colSums(W)) D = Diagonal(length(d),d) L = D - W p = length(B) t = seq(from=0,to=2*pi,by=(2*pi/p)) t = t[-(p+1)] Z = rbind(cos(t),sin(t)) L1 = Matrix(L, sparse=TRUE) L1[B+1,] = 0 for (i in (1:length(B))) { L1[(B + 1)[i], (B + 1)[i]] = 1 } R = Matrix(0, nrow=2, ncol=n) R[,B+1] = Z Y = Matrix(0, nrow=2, ncol=n) Y[1,] = solve(L1,R[1,]) Y[2,] = solve(L1,R[2,]) options(repr.plot.width=4, repr.plot.height=5, colorscale=gray) plot_mesh(as.matrix(rbind(Y,matrix(0,nrow=1,ncol=n))),F) source("nt_solutions/meshdeform_1_parameterization/exo1.R") ## Insert your code here. source("nt_solutions/meshdeform_1_parameterization/exo2.R") ## Insert your code here. source("nt_solutions/meshdeform_1_parameterization/exo3.R") ## Insert your code here. library(Matrix) C = replicate(3, runif(1000, 0, 1)) C = C/cbind(Matrix(rowSums(C)),Matrix(rowSums(C)),Matrix(rowSums(C))) C = C[order(C[,1], C[,2], C[,3]),] lambd = t(C) library(imager) library(magick) n1 = 256 T = load_image("nt_toolbox/data/lena.png", n1) options(repr.plot.width=6, repr.plot.height=5) x = seq(from = 0, to = 1, by = 1/(dim(T)[1]-1)) for (i in (1:100)){ px = Y[1,(F+1)[,i]] py = Y[2, (F+1)[,i]] points_x = t(lambd) %*% px points_y = t(lambd) %*% py points = cbind(points_x,points_y) col = rescale(bicubic(x, x, T, points_x, points_y )$z) col1 = cbind(col, col, col) Px = X[1,(F+1)[,i]] Py = X[2,(F+1)[,i]] Pz = X[3,(F+1)[,i]] Points_x = t(lambd) %*% Px Points_y = t(lambd) %*% Py Points_z = t(lambd) %*% Pz points3D(matrix(Points_x), matrix(Points_y), matrix(Points_z), col=as.color(col), pch=20, box=FALSE,colkey=FALSE,cex=0.05, theta=10) par(new=TRUE) } u1 = c(267,266)*n1/512 u2 = c(267,328)*n1/512 u3 = c(355,301)*n1/512 imageplot(T) lines(c(u1[2],u2[2],u3[2], u1[2]),c(u1[1],u2[1],u3[1],u1[1]), col="blue") v1 = c(310,125)*n1/512. v2 = c(315,350)*n1/512. v3 = c(105,232)*n1/512. options(repr.plot.width=5, repr.plot.height=5) Y_big = Y*(n1-1) + 1 beg = sort(W@i) end = W@i plot (NA, xlim=c(0,250), ylim=c(0,250), box=FALSE, clab="", ylab="", axes=FALSE) for (i in (1:length(beg))){ lines(c(Y_big[1, (beg+1)[i]],Y_big[1,(end+1)[i]]), c(Y_big[2,(beg+1)[i]], Y_big[2,(end+1)[i]]), col="Blue", xlab="", ylab="", axes=FALSE) } lines (c(v1[2],v2[2],v3[2], v1[2]),c(v1[1],v2[1],v3[1],v1[1]), col="red") T1 = t(perform_image_similitude(T, "affine", (u1-1)/n1,(v1-1)/n1,(u2-1)/n1,(v2-1)/n1,(u3-1)/n1,(v3-1)/n1)) imageplot(T1) for (i in (1:length(beg))){ lines(c(Y_big[1, (beg+1)[i]],Y_big[1,(end+1)[i]]), c(Y_big[2,(beg+1)[i]], Y_big[2,(end+1)[i]]), col="Blue", xlab="", ylab="", axes=FALSE) } lines (c(v1[2],v2[2],v3[2], v1[2]),c(v1[1],v2[1],v3[1],v1[1]), col="red") options(repr.plot.width=6, repr.plot.height=5) x = seq(from = 0, to = 1, by = 1/(dim(T)[1]-1)) for (i in (1:100)){ px = Y[1,(F+1)[,i]] py = Y[2, (F+1)[,i]] points_x = t(lambd) %*% px points_y = t(lambd) %*% py points = cbind(points_x,points_y) col = rescale(bicubic(x, x, T1, points_x, points_y )$z) col1 = cbind(col, col, col) Px = X[1,(F+1)[,i]] Py = X[2,(F+1)[,i]] Pz = X[3,(F+1)[,i]] Points_x = t(lambd) %*% Px Points_y = t(lambd) %*% Py Points_z = t(lambd) %*% Pz points3D(matrix(Points_x), matrix(Points_y), matrix(Points_z), col=as.color(col), pch=20, box=FALSE,colkey=FALSE,cex=0.05, theta=10) par(new=TRUE) }