from __future__ import division import nt_toolbox as nt from nt_solutions import meshdeform_1_parameterization as solutions %matplotlib inline %load_ext autoreload %autoreload 2 name = 'nefertiti' [X, F] = read_mesh(name) n = size(X, 2) clear options; options.name = name options.verb = 0 B = compute_boundary(F, options) p = length(B) clf; hold on plot_mesh(X, F, options) shading('faceted') hh = plot3(X(1, B), X(2, B), X(3, B), 'r') set_linewidth(hh, 4) W = make_sparse(n, n) for i in 1: 3: i2 = mod(i , 3) + 1 i3 = mod(i + 1, 3) + 1 u = X(: , F(i2, : )) - X(: , F(i, : )) v = X(: , F(i3, : )) - X(: , F(i, : )) % normalize the vectors u = u ./ repmat(sqrt(sum(u.^2, 1)), [3 1]) v = v ./ repmat(sqrt(sum(v.^2, 1)), [3 1]) % compute angles alpha = 1 ./ tan(acos(sum(u.*v, 1))) alpha = max(alpha, 1e-2); % avoid degeneracy W = W + make_sparse(F(i2, : ), F(i3, : ), alpha, n, n) W = W + make_sparse(F(i3, : ), F(i2, : ), alpha, n, n) d = full(sum(W, 1)) D = spdiags(d(: ), 0, n, n) L = D - W p = length(B) t = linspace(0, 2*pi(), p + 1); t(p + 1) = [] Z = [cos(t); sin(t)] L1 = L L1(B, : ) = 0 for i in 1: length(B): L1(B(i), B(i)) = 1 R = zeros(2, n) R(: , B) = Z if using_matlab() Y = (L1 \ R')' else options.maxit = 300 Y(1, : ) = perform_cg(L1, R(1, : )', options)' Y(2, : ) = perform_cg(L1, R(2, : )', options)' options.lighting = 0 plot_mesh([Y; zeros(1, n)], F) shading('faceted') solutions.exo1() ## Insert your code here. solutions.exo2() ## Insert your code here. solutions.exo3() ## Insert your code here. n1 = 256 T = load_image('lena', n1) T = T(n1: -1: 1, : ) options.texture = T options.texture_coords = Y plot_mesh(X, F, options) zoom(1.5) u1 = [247; 266]*n1/ 512 u2 = [247; 328]*n1/ 512 u3 = [161; 301]*n1/ 512 clf; hold('on') if using_matlab() imageplot(T) else imageplot(T(n1: -1: 1, : )) h = plot([u1(2) u2(2) u3(2) u1(2)], [u1(1) u2(1) u3(1) u1(1)], '.-b') if using_matlab() set(h, 'LineWidth', 2) set(h, 'MarkerSize', 20) v1 = [310; 125]*n1/ 512 v2 = [315; 350]*n1/ 512 v3 = [105; 232]*n1/ 512 clf; hold('on') plot_graph(W, Y'*(n1-1) + 1, 'b') h = plot([v1(2) v2(2) v3(2) v1(2)], [v1(1) v2(1) v3(1) v1(1)], '.-r') axis('off'); axis('equal') if using_matlab() set(h, 'LineWidth', 2) set(h, 'MarkerSize', 20) T1 = perform_image_similitude(T', 'affine', (u1-1)/ n1, (v1-1)/ n1, (u2-1)/ n1, (v2-1)/ n1, (u3-1)/ n1, (v3-1)/ n1)' clf; hold on if using_matlab() imageplot(T1) else imageplot(T1(n1: -1: 1, : )) plot_graph(W, Y'*(n1-1) + 1, 'b') h = plot([v1(2) v2(2) v3(2) v1(2)], [v1(1) v2(1) v3(1) v1(1)], '.-r') axis('off'); axis('equal') if using_matlab() set(h, 'LineWidth', 2) set(h, 'MarkerSize', 20) options.texture = T1 plot_mesh(X, F, options) zoom(1.5) q = 64 M = zeros(q, q, 3) for i in 1: 3: M(: , : , i) = compute_triang_interp(F, Y, X(i, : ), q) imageplot(M) C = ones(q, q)*0.8; % color colormap(gray(256)) surf(M(: , : , 1), M(: , : , 2), M(: , : , 3), C) view(-40, 70) zoom(1.5) axis tight; axis square; axis off camlight local; shading faceted [V, U] = meshgrid(1: 32, 1: 32) T = mod(V + U, 2) colormap(gray(256)) plot_surf_texture(M, T) view(-40, 70); zoom(1.5) axis tight; axis square; axis off camlight A = make_sparse(2*n, 2*n) A(1: n, 1: n) = L A(n + 1: 2*n, n + 1: 2*n) = L for s in 1: p: s1 = mod(s-2, p) + 1 s2 = mod(s, p) + 1 i = B(s) i1 = B(s1) i2 = B(s2) A(i, [i1 i2] + n) = [1, -1] A(i + n, [i1 i2]) = [-1, + 1] i1 = B(1) i2 = B(round(p/ 2)) A(i1, : ) = 0; A(i1 + n, : ) = 0 A(i1, i1) = 1; A(i1 + n, i1 + n) = 1 A(i2, : ) = 0; A(i2 + n, : ) = 0 A(i2, i2) = 1; A(i2 + n, i2 + n) = 1 y = zeros(2*n, 1) y([i1 i2]) = [0 1] y([i1 i2] + n) = [0 0] if using_matlab() Y = reshape(A\y, [n 2])' else options.maxit = 300 Y = reshape(perform_cg(A, y, options), [n 2])' Y = Y - repmat(min(Y, [], 2), [1 n]) Y = Y/ max(Y(: )) plot_mesh([Y; zeros(1, n)], F) shading faceted; axis tight solutions.exo4() ## Insert your code here. name = 'david-head' [X, F] = read_mesh(name) n = size(X, 2) clear options; options.name = name options.lighting = 1 plot_mesh(X, F, options) shading faceted solutions.exo5() ## Insert your code here. hold on plot_mesh(X, F, options) hh = plot3(X(1, B), X(2, B), X(3, B), 'r') set_linewidth(hh, 3) view(-150, -30) solutions.exo6() ## Insert your code here. solutions.exo7() ## Insert your code here.