%% Grain Reconstruction

close all; plotx2east
mtexdata forsterite
ebsd = ebsd(inpolygon(ebsd,[5 2 10 5]*10^3));

% make a phase plot
plot(ebsd)

%% Basic grain reconstruction


grains = calcGrains(ebsd,'angle',10*degree)

%%

hold on
plot(grains.boundary,'linewidth',2.5)
hold off


%% Filling notindexed holes

[grains,ebsd.grainId] = calcGrains(ebsd('indexed'))

plot(ebsd)
hold on
plot(grains.boundary,'linewidth',2.5)
hold off

%% remove one pixel grains

small_grains = grains(grains.grainSize<4)

ebsd(small_grains) = []



%% Smooth grain boundary

grains = smooth(grains,5)

plot(ebsd)
hold on
plot(grains.boundary,'linewidth',2.5)
hold off


%%

[grains,ebsd.grainId,ebsd.mis2mean] = calcGrains(ebsd);
notIndexed = grains('notIndexed')

%%

plot(notIndexed,log(notIndexed.grainSize ./ notIndexed.boundarySize))
mtexColorbar

%%

% the "not indexed grains" we want to remove
toRemove = notIndexed(notIndexed.grainSize ./ notIndexed.boundarySize<0.8)

% now we remove the corresponding EBSD measurements
ebsd(toRemove) = []

% and perform grain reconstruction with the reduces EBSD data set
[grains,ebsd.grainId,ebsd.mis2mean] = calcGrains(ebsd);

plot(grains,'lineWidth',2)

%%

plot(ebsd)

hold on
plot(grains.boundary,'linewidth',1.5)
hold off

%% Non convex data sets
% 

% cut of a non convex region from our previous data set
poly = 1.0e+04 *[...
  0.6853    0.2848
  0.7102    0.6245
  0.8847    0.3908
  1.1963    0.6650
  1.1371    0.2880
  0.6853    0.2833
  0.6853    0.2848];

ebsdP = ebsd(ebsd.inpolygon(poly))

plot(ebsdP,'micronBar','off')
legend off

%%

grains = calcGrains(ebsdP('indexed'))

hold on
plot(grains.boundary,'linewidth',1.5)
hold off  

%%

plot(ebsdP,'micronBar','off')
legend off

% compute the grains
[grains,ebsdP.grainId,ebsd.mis2mean] = calcGrains(ebsdP('indexed'),'boundary','tight')

% plot the grain boundary
hold on
plot(grains.boundary,'linewidth',1.5)
hold off  

%% Labeling the grains

% only the very big grains
big_grains = grains(grains.grainSize>50);

hold on
text(big_grains,int2str(big_grains.id))
hold off


%% crystal shapes

cS = crystalShape.olivine(ebsd('fo').CS)

plot(ebsdP,'micronBar','off')

hold on
plot(grains.boundary,'linewidth',1.5)
plot(big_grains('fo'),0.5*cS,'colored')

hold off


%% Single grains

plot(grains,grains.GOS)

hold on
text(big_grains,int2str(big_grains.id))
hold off

%%

myGrain = grains('id',13);


plot(ebsdP(myGrain),ebsdP(myGrain).orientations)
hold on
plot(myGrain.boundary,'lineWidth',2)
hold off

%%

colorKey = axisAngleColorKey(myGrain)
colorKey.oriRef = myGrain.meanOrientation

color = colorKey.orientation2color(ebsdP(myGrain).orientations);
plot(ebsdP(myGrain), color)


%%

mis2mean = ebsdP(myGrain).mis2mean;

mis2mean.axis

colorKey = HSVDirectionKey(mis2mean.axis.CS)

%%

plot(colorKey)

%%

color = colorKey.direction2color(mis2mean.axis);
plot(ebsdP(myGrain),color)

%%

plot(ebsdP(myGrain), mis2mean.angle ./ degree)

%%

gB = grains.boundary('Forsterite','Forsterite');

gB.misorientation

hold on
plot(gB, gB.misorientation.angle ./degree,'linewidth',3)
hold off

%%
plot(grains)

%%

gB.direction

%%

gB.






