From 8f304318b3b99dd22fa32bf94652193882fdfc3c Mon Sep 17 00:00:00 2001
From: Nicola Vigano <nicola.vigano@esrf.fr>
Date: Wed, 9 Mar 2016 12:30:10 +0100
Subject: [PATCH] GtEBSDView: improved to accept and use the EBSD data
 structure

Signed-off-by: Nicola Vigano <nicola.vigano@esrf.fr>
---
 zUtil_TomoUtils/GtEBSDView.m | 102 ++++++++++++++++++++++++++---------
 1 file changed, 76 insertions(+), 26 deletions(-)

diff --git a/zUtil_TomoUtils/GtEBSDView.m b/zUtil_TomoUtils/GtEBSDView.m
index 5c03d380..1af6555f 100644
--- a/zUtil_TomoUtils/GtEBSDView.m
+++ b/zUtil_TomoUtils/GtEBSDView.m
@@ -1,9 +1,10 @@
 classdef GtEBSDView < GtBaseGuiElem
     properties
+        ebsd = [];
     end
 
     methods
-        function self = GtEBSDView(ebsd_map, varargin)
+        function self = GtEBSDView(ebsd_str, varargin)
         % GTVOLVIEW/GTVOLVIEW Constructor
             self = self@GtBaseGuiElem();
 
@@ -11,24 +12,16 @@ classdef GtEBSDView < GtBaseGuiElem
 
             self.conf.cmap = 'gray';
 
-            if (isstruct(ebsd_map))
-                ebsd = ebsd_map;
-                ebsd_map = ebsd.map_r;
-
-                ebsd_mask = ebsd.mask;
-                phase_id = ebsd.phase_id;
-            else
-                ebsd_mask = [];
-                phase_id = 1;
+            if (~isstruct(ebsd_str))
+                error('GtEBSDView:wrong_argument', ...
+                    'Only an EBSD data structure is accepted')
             end
-            self.conf.ebsd_map = ebsd_map;
-            self.conf.mask = ebsd_mask;
+            self.ebsd = ebsd_str;
             self.conf.ipf_axis = [];
-            self.conf.phase_id = phase_id;
 
             self.conf.horDim = 1;
             self.conf.verDim = 2;
-            [dim(1), dim(2), dim(3)] = size(self.conf.ebsd_map);
+            [dim(1), dim(2), dim(3)] = size(self.ebsd.map_r);
             self.conf.dim = dim(1:2);
             self.conf.hordisplay = [1 self.conf.dim(1)];
             self.conf.verdisplay = [1 self.conf.dim(2)];
@@ -59,7 +52,7 @@ classdef GtEBSDView < GtBaseGuiElem
         % GTVOLVIEW/GETVOLVALUE Returns the value for the given point, from the
         % volume stored in the object
             roundedPoint = round(point);
-            r_vec = obj.conf.ebsd_map(roundedPoint(1), roundedPoint(2), :);
+            r_vec = obj.ebsd.map_r(roundedPoint(1), roundedPoint(2), :);
             r_vec = reshape(r_vec, 1, 3);
         end
 
@@ -84,11 +77,11 @@ classdef GtEBSDView < GtBaseGuiElem
         function initParams(self, arguments)
             initParams@GtBaseGuiElem(self, arguments)
 
-            bad_voxels_mask = any(abs(self.conf.ebsd_map) > eps('single'), 3);
-            if (isempty(self.conf.mask))
-                self.conf.mask = bad_voxels_mask;
+            bad_voxels_mask = any(abs(self.ebsd.map_r) > eps('single'), 3);
+            if (isempty(self.ebsd.mask))
+                self.ebsd.mask = bad_voxels_mask;
             else
-                self.conf.mask = self.conf.mask & bad_voxels_mask;
+                self.ebsd.mask = self.ebsd.mask & bad_voxels_mask;
             end
         end
 
@@ -186,8 +179,8 @@ classdef GtEBSDView < GtBaseGuiElem
             set(obj.conf.h_im, 'UIContextMenu', obj.conf.h_context_menu);
 
             % Copy pixel info in context menu
-            cbk = @(src, evt)copy3DPixInfoToClipboard(obj);
             obj.conf.h_menu_items = {};
+            cbk = @(src, evt)copy3DPixInfoToClipboard(obj);
             obj.conf.h_menu_items{end+1} = uimenu(obj.conf.h_context_menu, ...
                                            'Label', 'Copy pixel 3D info', ...
                                            'Callback', cbk);
@@ -195,6 +188,14 @@ classdef GtEBSDView < GtBaseGuiElem
             obj.conf.h_menu_items{end+1} = uimenu(obj.conf.h_context_menu, ...
                                            'Label', 'Copy grain Average R-vector info', ...
                                            'Callback', cbk);
+            cbk = @(src, evt)segmentGrain(obj);
+            obj.conf.h_menu_items{end+1} = uimenu(obj.conf.h_context_menu, ...
+                                           'Label', 'Segment grain to file...', ...
+                                           'Callback', cbk);
+            cbk = @(src, evt)extractBandContrastOfGrain(obj);
+            obj.conf.h_menu_items{end+1} = uimenu(obj.conf.h_context_menu, ...
+                                           'Label', 'Extract grain Band Contrast to file...', ...
+                                           'Callback', cbk);
         end
 
         function resetUiComponents(obj)
@@ -230,7 +231,7 @@ classdef GtEBSDView < GtBaseGuiElem
             else
                 point = round(self.getPixel());
             end
-            [grain_r_vecs, grain_mask] = gtDefEBSDSegmentGrainFromSeed(self.conf.ebsd_map, self.conf.mask, point);
+            [grain_r_vecs, grain_mask] = gtDefEBSDSegmentGrainFromSeed(self.ebsd.map_r, self.ebsd.mask, point);
             grain_r_vecs = gtDefDmvol2Gvdm(permute(grain_r_vecs, [1 2 4 3]));
             avg_r_vec = sum(grain_r_vecs(:, grain_mask(:)), 2) ./ sum(grain_mask(:));
             clipString = sprintf('(%d, %d) [%g, %g, %g]', point, avg_r_vec');
@@ -240,6 +241,55 @@ classdef GtEBSDView < GtBaseGuiElem
             clipboard('copy', clipString);
         end
 
+        function segmentGrain(self)
+            if (isfield(self.conf, 'clicked_point') && ~isempty(self.conf.clicked_point))
+                point = round(self.conf.clicked_point);
+            else
+                point = round(self.getPixel());
+            end
+            [grain_r_vecs, grain_mask, grain_bb] = gtDefEBSDSegmentGrainFromSeed(self.ebsd.map_r, self.ebsd.mask, point);
+
+            filter = {'*.mat', 'MATLAB Files (*.mat)'};
+            [filename, pathname] = uiputfile( ...
+                filter, 'Select file...', ...
+                fullfile('EBSD', 'grain_seg.mat'));
+
+            grain_struct = struct( ...
+                'R_vectors', grain_r_vecs, ...
+                'mask', grain_mask, ...
+                'bb', grain_bb); %#ok<NASGU>
+
+            if (filename)
+                save(fullfile(pathname, filename), '-struct', 'grain_struct', '-v7.3');
+            end
+        end
+
+        function extractBandContrastOfGrain(self)
+            if (isfield(self.conf, 'clicked_point') && ~isempty(self.conf.clicked_point))
+                point = round(self.conf.clicked_point);
+            else
+                point = round(self.getPixel());
+            end
+            [~, grain_mask, grain_bb] = gtDefEBSDSegmentGrainFromSeed(self.ebsd.map_r, self.ebsd.mask, point);
+
+            grain_lims = grain_bb([1 2 1 2]) + [1 1 grain_bb(3:4)];
+            grain_bc = self.ebsd.bc(grain_lims(1):grain_lims(3), grain_lims(2):grain_lims(4));
+
+            grain_struct = struct( ...
+                'band_contrast', grain_bc, ...
+                'mask', grain_mask, ...
+                'bb', grain_bb); %#ok<NASGU>
+
+            filter = {'*.mat', 'MATLAB Files (*.mat)'};
+            [filename, pathname] = uiputfile( ...
+                filter, 'Select file...', ...
+                fullfile('EBSD', 'grain_bc.mat'));
+
+            if (filename)
+                save(fullfile(pathname, filename), '-struct', 'grain_struct', '-v7.3');
+            end
+        end
+
         function doUpdateDisplay(obj)
         % GTVOLVIEW/DOUPDATEDISPLAY Function that actually updates all the
         % visualized GUI components of the GUI element
@@ -288,17 +338,17 @@ classdef GtEBSDView < GtBaseGuiElem
         % transformations on them, to adapt to the requested visualization needs
 
             p = gtLoadParameters();
-            cryst_system = p.cryst(self.conf.phase_id).crystal_system;
-            cryst_spacegroup = p.cryst(self.conf.phase_id).spacegroup;
+            cryst_system = p.cryst(self.ebsd.phase_id).crystal_system;
+            cryst_spacegroup = p.cryst(self.ebsd.phase_id).spacegroup;
             symm = gtCrystGetSymmetryOperators(cryst_system, cryst_spacegroup);
 
-            indx = find(self.conf.mask);
+            indx = find(self.ebsd.mask);
 
-            r_vecs = reshape(self.conf.ebsd_map, [], 3);
+            r_vecs = reshape(self.ebsd.map_r, [], 3);
             r_vecs = r_vecs(indx, :);
 
             [cmap, ~, ~] = gtIPFCmap( ...
-                self.conf.phase_id, self.conf.ipf_axis, ...
+                self.ebsd.phase_id, self.conf.ipf_axis, ...
                 'r_vectors', r_vecs, ...
                 'crystal_system', cryst_system, ...
                 'background', false, ...
-- 
GitLab