%%               Chemnitz MTEX Workshop 2015
%                     
%%                 EBSD Analysis with MTEX
%                Ralf Hielscher, TU Chemnitz
%
%%
% The following script demostrates some of the facilities of MTEX to
% import, correct and analyze EBSD data.
%
% Run this script section by section and follow the output. You are
% encauraged to alter the script to get a better feeling about MTEX.
%
% important shortcuts:
% Strg + Return         - run the current section
% Shift + Strg + Return - run the current section and go to the next one
% F9                    - evaluate the selected command
%

%%

% adjust plotting convention
plotx2east

%% import data

fname = 'Forsterite.ctf';
ebsd = loadEBSD(fname,'convertEuler2SpatialReferenceFrame')

% ebsd_bad = loadEBSD('example1.ctf')
% ebsd2 = loadEBSD('example1.ctf','convertSpatial2EulerReferenceFrame');

%% plot phase information

plot(ebsd)

%% select data by phase

ebsd('Forsterite')

plot(ebsd('Forsterite'))

%% visualize properties

plot(ebsd('Fo'),ebsd('Fo').orientations.angle./degree)

% gcm % this is the current MTEX figure object

%CLim(gcm,[0,150])
%mtexColorMap black2white
%colorbar(gcm)

%% visualize orientations

% define a color coding
oM = ipdfHSVOrientationMapping(ebsd('Forsterite'));

% with respect to the 001 inverse pole figure
oM.inversePoleFigureDirection = zvector

% visualize the color map
plot(oM)

%%

% compute colors from orientations
colors = oM.orientation2color(ebsd('Fo').orientations);

% plot the colors
figure(2)
plot(ebsd('Fo'),colors)


%% understanding the colormap

figure(1)

hold all
plotIPDF(ebsd('Forsterite').orientations,'points',250,...
  'MarkerSize',5,'MarkerFaceColor','none','MarkerEdgeColor','k')
hold off


%% combine with other plots

figure(2)
hold on
plot(ebsd,ebsd.bc,'FaceAlpha',0.5)
%plot(ebsd,ebsd.bc)
mtexColorMap black2white
hold off

% TODO: talk with Florian about option translucent

%%  extract properties

close all
hist(ebsd.bc)

%% remove data according to properties

ebsd(ebsd.bc<50).phase = 0;

plot(ebsd)

%% define a region of interest

region = [5000 2000 12000 3000];

rectangle('position',region,'edgecolor','r','linewidth',2)

%% restrict to region of interest

ebsd_roi = ebsd(ebsd.inpolygon(region))

plot(ebsd_roi)

%% a more interactive way

poly = selectPolygon;

plot(ebsd(ebsd.inpolygon(poly)))


%%

rot = rotation('axis',zvector,'angle',2*degree);

plot(rotate(ebsd_roi,rot))

%% reconstruct grains

[grains,ebsd] = calcGrains(ebsd_roi)

plot(grains)

%% grain properties

hist(grains)

%% find the largest grain

% find id of largest grain
[m,id] = max(grains.area)     

% plot largest grain
plot(grains(id))            

%% define an orinetation mapping 

% 
oM = ipdfHSVOrientationMapping(grains(id));
plot(oM)

% plot grain orientations
hold on
plotIPDF(ebsd(grains(id)).orientations,'points',200)
hold off

%%

plot(ebsd(ebsd.grainId == id),...
  oM.orientation2color(ebsd(grains(id)).orientations))

%% colorize the grain orientations

% make it sharp
oM.whiteCenter = inv(grains(id).meanOrientation) * oM.inversePoleFigureDirection;
oM.colorStretching = 10;

% plot orientations
plot(ebsd(grains(id)),...
  oM.orientation2color(ebsd(grains(id)).orientations))

% plot boundary
hold on
plot(grains(id).boundary);
hold off

%%

plot(oM)

%%
plot(grains,grains.shapefactor)

%% colorize boundaries according to misorientation angle

plot(grains.boundary,'property','angle','linewidth',2)
colorbar

%% define a specific misorientation

cs = get(ebsd('Fo'),'CS');
h1 = Miller(0,1,1,cs);
mori1 = orientation('axis',h1,'angle',180*degree,cs,cs)
h2 = Miller(1,0,1,cs);
mori2 = orientation('axis',h2,'angle',180*degree,cs,cs)
h3 = Miller(1,1,0,cs);
mori3 = orientation('axis',h3,'angle',180*degree,cs,cs)

%% colorize boundaries according to misorientation

plotBoundary(grains);

hold on

plotBoundary(grains('Fo'),'property',mori1,'delta',5*degree,'linewidth',2,'linecolor','r')
plotBoundary(grains('Fo'),'property',mori2,'delta',5*degree,'linewidth',2,'linecolor','g')
plotBoundary(grains('Fo'),'property',mori3,'delta',5*degree,'linewidth',2,'linecolor','b')

hold off

%%
plotx2north
plot(ebsd)
legend off

saveFigure('../pic/ebsdPhaseH.png')

%%
plot(ebsd,ebsd.bc)
mtexColorMap black2white
saveFigure('../pic/ebsdbcH.png')

%%
[phi1,Phi,phi2] = ebsd.rotations.Euler;
plot(ebsd,phi1)
saveFigure('../pic/ebsdphi1H.png')

%%

plot(ebsd,ebsd.KAM)
mtexColorMap white2black
caxis([0,0.025])
saveFigure('../pic/ebsdKAMH.png')

%%

plot(ebsd('Fo'),ebsd('Fo').orientations)

saveFigure('../pic/ebsdOriH.png')

%%
oM.inversePoleFigureDirection = xvector
plot(oM)

set(gcf,'position',[200,200,200,200])
saveFigure('../pic/oMmmm100.png')
