mtex-toolbox / mtex

MTEX is a free Matlab toolbox for quantitative texture analysis. Homepage:
http://mtex-toolbox.github.io/
GNU General Public License v2.0
281 stars 185 forks source link

A small bug in intersect function #179

Closed latmarat closed 8 years ago

latmarat commented 8 years ago

Hello:

intersect is a really nice function but there is a small bug that may not always show up.

If I remove boundary grains or have a hole between "good" grains for whatever reason, intersect counts segments outside good grains and adds them to the output array segLength, while it shouldn't. See figure below.

intersect-bug

We can see that here the right number of segments is 3 but intersect will give 4 segments (see code below)

Perhaps, there should be additional inside condition for the adjacency matrix with good grains as input but so far I couldn't figure out a fast way to do that. Could you please fix this or suggest a temporary work-around?

Thanks, Marat

Code

mtexdata alu

grains = calcGrains(ebsd,'angle',15*degree);

bnd = any(grains.boundary.grainId==0,2);
bndGrainsId = grains.boundary(bnd).grainId;
bndGrainsId(bndGrainsId==0) = [];
grains(bndGrainsId) = [];

[xi,yi,segments] = grains.boundary.intersect([0,5],[56,5]);

numel(segments)
ralfHielscher commented 8 years ago

Hi,

thank you for reporting this issue. However, I do not think I can fix this within the function as intersect takes as input only boundary segments and does not know about grains. Maybe you can check afterwards, whether a segment is within a grain or not. E.g. by

% restrict to the intersection points
xi = xi(~isnan(xi));
yi = yi(~isnan(yi));

% compute the midpoints - this requires that the points are ordered - which is here the case but in general might not
mpx = 0.5*(xi(1:end-1)+xi(2:end));
mpy = 0.5*(yi(1:end-1)+yi(2:end));

% the checks whether the midpoints are inside the given grains
any(grains.checkInside([mpx(:),mpy(:)]),2)

I hope this helps,

Ralf.