
%% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
%
%         /$$$$$$  /$$$$$$$   /$$$$$$  /$$$$$$ /$$   /$$  /$$$$$$           
%        /$$__  $$| $$__  $$ /$$__  $$|_  $$_/| $$$ | $$ /$$__  $$          
%       | $$  \__/| $$  \ $$| $$  \ $$  | $$  | $$$$| $$| $$  \__/          
%       | $$ /$$$$| $$$$$$$/| $$$$$$$$  | $$  | $$ $$ $$|  $$$$$$           
%       | $$|_  $$| $$__  $$| $$__  $$  | $$  | $$  $$$$ \____  $$          
%       | $$  \ $$| $$  \ $$| $$  | $$  | $$  | $$\  $$$ /$$  \ $$          
%       |  $$$$$$/| $$  | $$| $$  | $$ /$$$$$$| $$ \  $$|  $$$$$$/          
%        \______/ |__/  |__/|__/  |__/|______/|__/  \__/ \______/       
%
%                                                    /$$                    
%                                                   |__/                    
%   /$$$$$$  /$$   /$$  /$$$$$$   /$$$$$$   /$$$$$$$ /$$  /$$$$$$$  /$$$$$$ 
%  /$$__  $$|  $$ /$$/ /$$__  $$ /$$__  $$ /$$_____/| $$ /$$_____/ /$$__  $$
% | $$$$$$$$ \  $$$$/ | $$$$$$$$| $$  \__/| $$      | $$|  $$$$$$ | $$$$$$$$
% | $$_____/  >$$  $$ | $$_____/| $$      | $$      | $$ \____  $$| $$_____/
% |  $$$$$$$ /$$/\  $$|  $$$$$$$| $$      |  $$$$$$$| $$ /$$$$$$$/|  $$$$$$$
%  \_______/|__/  \__/ \_______/|__/       \_______/|__/|_______/  \_______/
%                                                                           
%                                                                           
%                                                                                                                                                     
%                                                                           
%                     /$$$$$$   /$$$$$$   /$$$$$$   /$$$$$$                 
%                    /$$__  $$ /$$$_  $$ /$$__  $$ /$$$_  $$                
%                   |__/  \ $$| $$$$\ $$|__/  \ $$| $$$$\ $$                
%                     /$$$$$$/| $$ $$ $$  /$$$$$$/| $$ $$ $$                
%                    /$$____/ | $$\ $$$$ /$$____/ | $$\ $$$$                
%                   | $$      | $$ \ $$$| $$      | $$ \ $$$                
%                   | $$$$$$$$|  $$$$$$/| $$$$$$$$|  $$$$$$/                
%                   |________/ \______/ |________/ \______/                 
%                                                                           
%                                                                   
%                                                                                                                                     
% The aim of this exercise is to become more familiar with ebsd data and
% grains, access and plot properties of grains

%% ########################################################################
%
% PART 1
%
% #########################################################################

%% 0) load ebsd data ebsd1.ctf  ###########################################
% load the ebsd data from Wednesday




%% 1) grains ##############################################################
% a) compute grains from the ebsd dataset
% usefull commands: calcgrains
[grains,ebsd.grainId,ebsd.mis2mean] = calcGrains(ebsd,'angle',10*degree);

% b) plot those grains



% c) remember yesterday, we plotted ebsd points with an ipf colorcoding;
% repeat and compare with a plot of the grain mean orientation




%% 2) explore the properties of grains ####################################
% a) plot a frequency distribution and a map for area equivalent
% diameter (deq) and grain orientation spread (GOS)




% Bonus: Plot also an area weighted histogram of deq
% hint: (class population) * (class value)^2 and then normalize 




%% 3) take care of twins ##################################################
% Before we proceed, we should make sure that we are looking at grains and not twins:
% A very common twin in quartz is the Dauphiné twin, which is defined by
% a 60 degree totation around 0001

% it becomes pretty obvious when plotting e.g. the misorientation angles.
% axis-angle pairs etc. more on that later this week. For now we will just
% look at the angle histogram of grain boundary misorientations
% more detailed exploration of grains.boundary will come later

plotAngleDistribution(grains('q').boundary('q','q').misorientation, 'resolution',1*degree)

% Question:
% Visualise the grain boundary in the map and colorcode the misorientation
% angle and misorientation axis.



%% taking care of twins
% There are many ways to get rid of twins but for this particular one,
% we can use a trick by temporarily increasing the symmetry to hexagonal 
% - the twin dioes not exist - then segment grains and set back the symmetry. 
% Now also the meanOrientation represents something like the modal 
% orientation of the grains


% option 1) merge the grains
% get the grain boundaries
gb_qtz = grains.boundary('q','q');
 %define a misorientation assosciated with dauphine twinning
rot = orientation('axis',Miller(0,0,0,1,ebsd('q').CS),'angle',60*degree,cs,cs);
% calculates the angle between two trotations - the misorientation and the twinning rotation 
ang_r = angle(gb_qtz('q','q').misorientation,rot);
% condition/index all angles below a certain (magic) threshold
ind = ang_r < 6.*degree; % what angle should we choose here?
%select the boundaries based on this condition                                                                        
twinBoundary = gb_qtz(ind);
% merge grains connected with that twin boundary
[grains_merged,parentID_grains]= merge(grains,twinBoundary);
% make a copy of the ebsd dataset
ebsd_merged = ebsd;
% update the grainIds to the parentIds, so each ebsd_merged pixel knows
% again to which grain it belongs to
ebsd_merged.grainId  = parentID_grains(ebsd.grainId);
% plot it
figure
plot(ebsd('q'),ebsd('q').orientations,'faceAlpha',0.15)
hold on
plot(grains_merged.boundary('q','q'),'linewidth',2)
hold on
plot(twinBoundary,'linecolor','r','linewidth',2)

% note that now, grains do not carry any meanOrientation - since it is
% probably nonsense!

% just for this particular twinning relation on can also temporarily
% increase the crystal symmetry to hexagonal - the twin dioes not exist - 
% segment grains and set back the symmetry. Now the meanOrientation esists
% and represents something like the modal orientation of the grains

% option 2) change the symmetry
csq= ebsd('q').CS
cshq = crystalSymmetry('622','mineral', 'Quartz')
ebsd('q').CS=cshq
[grainsD,ebsd.grainId,ebsd.mis2mean] = calcGrains(ebsd,'angle',10*degree);
ebsd('q').CS=csq
grainsD('q').CS=csq
hold on
plot(grainsD.boundary('q','q'),'linecolor','b','linewidth',2)
hold off
% from here, go on with grains_merged / ebsd_merged or grainsD/ebsd

plotAngleDistribution(grainsD('q').boundary('q','q').misorientation, 'resolution',1*degree)

%% ------------------------------------------------------------------------
% a) Question: Reproduce the figures from 2a) and compare. Which measure is
% now more useful, which certainly is not?




%%
close all


%% 4) Explore more grain parameters:#######################################

% Question: 
% a) plot a map of grains which have an aspect ratio larger 2.5
% for simplicity create a variable just containing quartz grains
% and use interpolated shapes and discard grain which are too small e.g.
% 5px
% note: for serious work, you probably wouldn't want to trust
% shapes of grains smaller 20,50 ... pixels, depending on the property




%% ------------------------------------------------------------------------
% Question:
% b) plot only grains with an aspect ratio larger 2 AND with a long axis
% direction +/- 10 degree from ESE-WNW, plot ipdf standard colorcoding
% useful command:principalComponents




% c) Plot all long axis direction with a reasonable nice colorcoding, 
% e.g. mtexColorMap(colorcet('PHASE4'))





%% %#######################################################################
% selecting grains
%% -------------------------------------------------------------------------
% i)  manually select data - grains
%select a grain by clicking at it, when done, hit enter
plot(grainsD('q'),grainsD('q').meanOrientation)
grain_xy= ginput;% close on those grains you want to select, hit ENTER when done
selected_grains = grainsD(findByLocation(grainsD,grain_xy))
plot(ebsd(selected_grains),ebsd(selected_grains).orientations)

%%
close all

%% Bonus question:
% Rüdiger wants buy a new steel bicycle frame. Which type of steel should
% he get so he has a nice, light, durable and sturdy frame?

%% ########################################################################
%
% PART 2
%
% #########################################################################
%%
% now let's explore another dataset:
% in matlab you can store any varibale in a .mat file and load it again
% load the file gbm_ex.mat
 
csq= ebsd('q').CS
cshq = crystalSymmetry('622','mineral', 'Quartz')
ebsd('q').CS=cshq
[grains,ebsd.grainId,ebsd.mis2mean] = calcGrains(ebsd,'angle',10*degree);
ebsd('q').CS=csq
grains('q').CS=csq

 %% 5) alternative segmentation ###########################################
% it is possible to use any other criterion to segment grains
% e.g. only the c-axis directions

% a) segment grains by the angle between adjacent c-axes
% hint on syntax: calcGrains(ebsd(),'custom',propery,'delta',value)


% b) now overlay both grain maps


 
% b) What is the total quartz grain boundary length of both grain segmentations?
% Hint: inspect grains.boundary('q','q')
% useful commands: grain2d/smooth


%% 6) explore grain shapes ################################################

% a) Smooth the grains nicely.

% b) Plot grain colorcoded by the paris factor and limit the color LUt to
% values < 50.


% c) compute radiusDelta
% useful commands grains.hull


% d) Compare the textures of grains with high / low paris factors and low
% and high radiusDelta

