Skip to content
Snippets Groups Projects
Commit 81468192 authored by Yoann Guilhem's avatar Yoann Guilhem Committed by Nicola Vigano
Browse files

Add new function to do move a vector from Crystal to Lab coordinate system.

parent 8ace80a1
Branches continue_spectrum_refactoring
No related tags found
No related merge requests found
function Vs = gtVectorCryst2Lab(Vc, g, diag)
% GTVECTORLAB2CRYST Express crystal row vector(s) in laboratory CS.
%
% Vs = gtVectorCryst2Lab(Vc, g, diag)
% -----------------------------------
%
% INPUT:
% Vc = <double> Vector(s) expressed in the crystal CS.
% g = <cell> Cell containing crystal orientation matrix(ces).
% diag = <logical> If true, turn on diag mode which apply each
% orientation matrix g_i to corresponding vector Vc_i.
% Only valid if equal number of vectors and matrices.
%
% OUTPUT:
% Vs = <double> Input vector(s) Vc expressed in the laboratory CS.
% Shape of output depends of input size (see below).
%
% Note:
% This function can be used in 4 different modes:
% - Vc = 1x3 array (1 row vector) , g = 3x3xN array
% output: N vectors expressed in the laboratory CS
% output size: [N 3]
%
% - Vc = Mx3 array (M row vectors) , g = 3x3 array
% output: M vectors expressed in the laboratory CS
% output size: [M 3]
%
% - Vc = Mx3 array (M row vectors) , g = 3x3xN array
% output: MxN vectors expressed in the laboratory CS
% output size: [M 3 N]
%
% - diag mode (only if N=M, need diag argument, otherwise previous case)
% Vc = Mx3 array (M row vectors) , g = 3x3xN array (M=N)
% output: M vectors expressed in the laboratory CS
% output size: [M 3 M]
%
% Version 001 02-04-2013 by YGuilhem
if ~exist('diag', 'var') || isempty(diag)
diag = false;
else
diag = logical(diag);
end
% Check input orientation matrices g size
if (~isreal(g))
gtError('gtVectorCryst2Lab:wrong_input_type', ...
'Wrong input g type, wait a 3x3xN real array!');
elseif (size(g, 1) ~= 3 || size(g, 2) ~= 3)
gtError('gtVectorCryst2Lab:wrong_input_size', ...
'Input array g should be sized 3x3xN!');
end
% Check input vectors size
if size(Vc, 2) ~= 3
gtError('gtVectorCryst2Lab:wrong_input_size', ...
'Input array Vc should be sized Mx3!');
end
% Get number of vectors (M) and orientation matrices (N)
M = size(Vc, 1);
N = size(g, 3);
if M == 1 && N > 1
% Transpose and reshape Vc
Vc = reshape(Vc.', [3 1 M]);
% Multiply and sum along 1st axis to reproduce Vs = Vc . g
%Vs = squeeze(sum(bsxfun(@times, Vc, g), 1));
Vs = squeeze(sum(Vc(:, [1 1 1], ones(N, 1)).*g, 1)).';
elseif M >= 1 && N == 1
% Transpose g to get gT, reshape it and expand it along 1st axis
gT = reshape(g.', [1 3 3]);
% Reshape Vc
Vc = reshape(Vc, [M 1 3]);
% Multiply and sum along 3rd axis to reproduce Vs = gT . Vc
Vs = sum(bsxfun(@times, gT, Vc), 3);
elseif M == N && diag
% Transpose and reshape Vc
Vc = reshape(Vc.', [3 1 M]);
% Multiply and sum along 1st axis to reproduce Vs = Vc . g
%Vs = squeeze(sum(bsxfun(@times, Vc, g), 1)).';
Vs = squeeze(sum(Vc(:, [1 1 1], :).*g, 1)).';
elseif M > 1 && N > 1
% Transpose and reshape Vc
Vc = reshape(Vc.', [3 1 1 M]);
% Multiply and sum along 1st axis to reproduce Vs = Vc . g
Vs = permute(squeeze(sum(bsxfun(@times, Vc, g), 1)), [3 1 2]);
else
gtError('gtVectorCryst2Lab:wrong_input_size', ...
['Cannot handle the case where size(Vc)=' num2str(size(Vc)) ...
' and length(g)=' num2str(N)]);
end
end % end of function
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment