/* $Id: shoot_regularisers.c 7155 2017-08-17 10:55:05Z john $ */ /* (c) John Ashburner (2011) */ #include #include extern double log(double x); #include "shoot_boundary.h" /* % MATLAB code (requiring Symbolic Toolbox) for computing the % regulariser for linear elasticity. syms mu lam syms v0 v1 v2 N = 3; O = sym(zeros(N)); OO = kron(kron(O,O),O); I = sym(eye(N)); G1 = sym(spdiags(repmat([-1 1],N,1),[ 0 1],N,N)); G1(N,1) = 1; G2 = sym(spdiags(repmat([-1 1],N,1),[-1 0],N,N)); G2(1,N) = -1; G = {G1 G2}; LL = sym(zeros(3*N^3,3*N^3)); for i=1:2, Di = kron(I,kron(I,G{i}))/v0; for j=1:2, Dj = kron(I,kron(G{j},I))/v1; for k=1:2, Dk = kron(G{k},kron(I,I))/v2; L1 = [ Di OO OO OO Dj OO OO OO Dk 0.5*Dj 0.5*Di OO 0.5*Dk OO 0.5*Di OO 0.5*Dk 0.5*Dj 0.5*Dj 0.5*Di OO 0.5*Dk OO 0.5*Di OO 0.5*Dk 0.5*Dj]; % Elements of symmetric part of Jacobian L2 = [ Di Dj Dk]; % Divergence LL = LL + mu*(L1.'*L1) + lam*(L2.'*L2); end end end LL = LL/8; o = sym(ones(1,27)); LL = LL.*kron([v0*o v1*o v2*o],[o o o].').*kron([v0*o v1*o v2*o].',[o o o]); L1 = simplify(reshape(LL(:,2+3+9 ),[3 3 3 3])) L2 = simplify(reshape(LL(:,2+3+9+27),[3 3 3 3])) L3 = simplify(reshape(LL(:,2+3+9+54),[3 3 3 3])) */ /* % MATLAB code (requiring Symbolic Toolbox) for computing the % regulariser for bending energy. Note that an additional multiplication % by the square of the voxel size is needed. syms s1 s2 s3 syms l1 l2 l3 zz = sym(zeros(3,3)); K1 = cat(3,zz,[0 -1/(v0*v0) 0; 0 2/(v0*v0) 0; 0 -1/(v0*v0) 0],zz); K2 = cat(3,zz,[0 0 0; -1/(v1*v1) 2/(v1*v1) -1/(v1*v1); 0 0 0],zz); K3 = sym(zeros(3,3,3)); K3(2,2,1) = -1/(v2*v2); K3(2,2,2) = 2/(v2*v2); K3(2,2,3) = -1/(v2*v2); K = K1+K2+K3; K1 = K*l1; K1(2,2,2) = K1(2,2,2)+l2; K2 = K*l1; K2(2,2,2) = K2(2,2,2)+l3; % L = convn(K,K) L = sym(zeros(5,5,5)); for i=1:3, for j=1:3, for k=1:3, L(i-1+1:i+1+1,j-1+1:j+1+1,k-1+1:k+1+1) = L(i-1+1:i+1+1,j-1+1:j+1+1,k-1+1:k+1+1) + K1(i,j,k)*K2; end; end; end; disp(simplify(L(3:end,3:end,3:end))) */ double trapprox(mwSize dm[], float a[], double s[]) { double v0 = s[0]*s[0], v1 = s[1]*s[1], v2 = s[2]*s[2]; double lam0 = s[3], lam1 = s[4], lam2 = s[5], mu = s[6], lam = s[7]; double w000, wx000, wy000, wz000; double tr = 0.0; mwSignedIndex i, j, k; w000 = lam2*(6*(v0*v0+v1*v1+v2*v2) +8*(v0*v1+v0*v2+v1*v2)) +lam1*2*(v0+v1+v2) + lam0; wx000 = 2*mu*(2*v0+v1+v2)/v0+2*lam + w000/v0; wy000 = 2*mu*(v0+2*v1+v2)/v1+2*lam + w000/v1; wz000 = 2*mu*(v0+v1+2*v2)/v2+2*lam + w000/v2; for(k=0; k>1)&1; j>2)&1; i