From fbc296e1a093b3d2550b35e6b10dd49d185b772c Mon Sep 17 00:00:00 2001
From: Nicola Vigano <nicola.vigano@esrf.fr>
Date: Wed, 4 Feb 2015 18:04:54 +0100
Subject: [PATCH] Figures: added function for generating figures for papers

Signed-off-by: Nicola Vigano <nicola.vigano@esrf.fr>
---
 .../plotting/gt6DMakePhaseFigureForPaper.m    | 178 ++++++++++++++++++
 1 file changed, 178 insertions(+)
 create mode 100644 zUtil_Deformation/plotting/gt6DMakePhaseFigureForPaper.m

diff --git a/zUtil_Deformation/plotting/gt6DMakePhaseFigureForPaper.m b/zUtil_Deformation/plotting/gt6DMakePhaseFigureForPaper.m
new file mode 100644
index 00000000..4265f208
--- /dev/null
+++ b/zUtil_Deformation/plotting/gt6DMakePhaseFigureForPaper.m
@@ -0,0 +1,178 @@
+function gt6DMakePhaseFigureForPaper( phase_mat, phase_id, varargin )
+% gtCSMakeFigureForPaper( phase_mat, phase_id, varargin )
+%    conf = struct( ...
+%        'use_dmvol', true, ...
+%        'abs_vol', [], ...
+%        'abs_mask', [], ...
+%        'filename', '', ...
+%        'plane', 'xy', ...
+%        'flip_ud', false, ...
+%        'flip_lr', false, ...
+%        'slice', 1, ...
+%        'clims', [], ...
+%        'pixel_to_cm', 0.05, ...
+%        'borders', 'no', ...
+%        'colorbar', 'no');
+% 
+
+    conf = struct( ...
+        'use_dmvol', true, ...
+        'abs_vol', [], ...
+        'abs_mask', [], ...
+        'filename', '', ...
+        'plane', 'xy', ...
+        'flip_ud', false, ...
+        'flip_lr', false, ...
+        'slice', 1, ...
+        'clims', [], ...
+        'pixel_to_cm', 0.05, ...
+        'borders', 'no', ...
+        'colorbar', 'no' );
+    conf = parse_pv_pairs(conf, varargin);
+
+    slice_ids = get_slice(phase_mat.vol, conf.plane, conf.slice);
+    if (conf.use_dmvol && isfield(phase_mat, 'dmvol'))
+        slice_rod = get_slice(phase_mat.dmvol, conf.plane, conf.slice);
+        indx = find(slice_ids > 0);
+        r_vecs = reshape(slice_rod, [], 3);
+        r_vecs = r_vecs(indx, :);
+
+        cmap = get_cmap(r_vecs, phase_id);
+
+        slice_R = zeros(size(slice_ids));
+        slice_G = zeros(size(slice_ids));
+        slice_B = zeros(size(slice_ids));
+
+        slice_R(indx) = cmap(:, 1);
+        slice_G(indx) = cmap(:, 2);
+        slice_B(indx) = cmap(:, 3);
+    else
+        sample = GtSample.loadFromFile();
+
+        gids = sort(unique(slice_ids));
+        gids(gids < 1) = [];
+        r_vecs = sample.phases{phase_id}.R_vector(gids, :);
+
+        cmap = get_cmap(r_vecs, phase_id);
+
+        slice_R = zeros(size(slice_ids));
+        slice_G = zeros(size(slice_ids));
+        slice_B = zeros(size(slice_ids));
+        for ii = 1:numel(gids)
+            indx = find(slice_ids == gids(ii));
+            slice_R(indx) = cmap(ii, 1);
+            slice_G(indx) = cmap(ii, 2);
+            slice_B(indx) = cmap(ii, 3);
+        end
+    end
+
+    slice_abs = get_slice(conf.abs_vol, conf.plane, conf.slice);
+    slice_abs = (slice_abs - min(slice_abs(:))) ./ (max(slice_abs(:)) - min(slice_abs(:)));
+
+    slice_mask = get_slice(conf.abs_mask, conf.plane, conf.slice);
+    slice_R(~slice_mask) = slice_abs(~slice_mask);
+    slice_G(~slice_mask) = slice_abs(~slice_mask);
+    slice_B(~slice_mask) = slice_abs(~slice_mask);
+
+    % Note the inversion between lr/up because of matlab's transpose problem
+    if (conf.flip_ud)
+        slice_R = fliplr(slice_R);
+        slice_G = fliplr(slice_G);
+        slice_B = fliplr(slice_B);
+    end
+    if (conf.flip_lr)
+        slice_R = flipud(slice_R);
+        slice_G = flipud(slice_G);
+        slice_B = flipud(slice_B);
+    end
+
+    f = figure();
+    ax = axes('Parent', f);
+    im = imagesc(cat(3, slice_R', slice_G', slice_B'), 'Parent', ax);
+
+    if (~isempty(conf.clims))
+        set(ax, 'CLim', conf.clims)
+    end
+
+    set(f, 'Units', 'centimeters')
+    img_size = size(slice_ids) * conf.pixel_to_cm;
+    if (strcmpi(conf.borders, 'no'))
+        position = [0, 0, img_size];
+        set(f, 'Position', position)
+        set(f, 'Paperposition', position)
+
+        set(ax, 'Units', 'normalized')
+        set(ax, 'Position', [0, 0, 1, 1])
+    else
+        set(ax, 'Units', 'centimeters')
+        if (strcmpi(conf.colorbar, 'no'))
+            position = [0, 0, img_size] + [0, 0, 2, 1.5];
+            set(f, 'Position', position)
+            set(f, 'Paperposition', position)
+        else
+            position = [0, 0, img_size] + [0, 0, 3, 2];
+            set(f, 'Position', position)
+            set(f, 'Paperposition', position)
+
+            cb = colorbar('peer', ax, 'location', 'EastOutside');
+            set(cb, 'Units', 'centimeters')
+            position = [img_size(1)+1.35, 1, 0.65, img_size(2)];
+            set(cb, 'Position', position)
+        end
+        position = [1, 1, img_size];
+        set(ax, 'Position', position)
+    end
+
+    drawnow();
+
+    if (~isempty(conf.filename) && strcmpi(conf.filename, 'preview'))
+        [filename, filedir] = uigetfile({'*.png'; '*.eps'});
+        if (filename)
+            conf.filename = fullfile(filedir, filename);
+        else
+            conf.filename = [];
+        end
+    end
+
+    if (~isempty(conf.filename))
+        [~, ~, ext] = fileparts(conf.filename);
+        if (isempty(ext))
+            saveas(f, [conf.filename '.eps']);
+            saveas(f, [conf.filename '.png']);
+        else
+            saveas(f, conf.filename);
+        end
+    end
+end
+
+function slice = get_slice(vol, plane, slice_idx)
+    switch(plane)
+        case {'xy', 'XY'}
+            slice = vol(:, :, slice_idx, :);
+        case {'yx', 'YX'}
+            slice = permute(vol(:, :, slice_idx, :), [2 1 3]);
+        case {'yz', 'YZ'}
+            slice = squeeze(vol(slice_idx, :, :, :));
+        case {'zy', 'ZY'}
+            slice = permute(squeeze(vol(slice_idx, :, :, :)), [2 1 3]);
+        case {'xz', 'XZ'}
+            slice = squeeze(vol(:, slice_idx, :, :));
+        case {'zx', 'ZX'}
+            slice = permute(squeeze(vol(:, slice_idx, :, :)), [2 1 3]);
+    end
+end
+
+function cmap = get_cmap(r_vecs, phase_id)
+    p = gtLoadParameters();
+    cryst_system = p.cryst(phase_id).crystal_system;
+    cryst_spacegroup = p.cryst(phase_id).spacegroup;
+    symm = gtCrystGetSymmetryOperators(cryst_system, cryst_spacegroup);
+
+    [cmap, ~, ~] = gtIPFCmap(phase_id, [], ...
+        'r_vectors', r_vecs, ...
+        'crystal_system', cryst_system, ...
+        'background', false, ...
+        'symm', symm);
+end
+
+
-- 
GitLab