Error refining FEM mesh using iso2mesh-2021

While tracking down an unrelated issue computing a mixed head model using an FEM approach, I got an error when using iso2mesh-2021 to decrease the max element volume size <=0.001. On line 408 of bst_process_fem_mesh, there is a snippet of code to sort elements by tissue type by sorting inner to outer and comparing the max distance from the center of the head model:

% Sort weired elements, works in all case even when there are more output tissues than inputs
distOut_tmp = distance_out;
for ind = 1 : length(distance_in)
tmp = find(round(distOut_tmp,3)<=round(distance_in(ind),3));
distOut_tmp(tmp) = ind;
end

This will work 99 times out of 100, but I got an error when the scalp layer distance_out was slightly greater than the distance_in (0.1754 vs. 0.1756). The error can be replicated by creating an FEM mesh for the default ICBM152 using all the default settings, extracting surfaces from the FEM, selecting the extracted surfaces, and generating a new FEM using iso2mesh-2021 with a max volume of 0.001. I solved the error by replacing that code with this snippet, which I think is a bit more robust:

num_out = length(distance_out);
distOut_tmp = zeros(1,num_out);
for ind = 1:num_out
[~,distOut_tmp(ind)] = min(abs(distance_out(ind)-distance_in));
end

Best,
Kyle

1 Like

Thanks Kyle for this deep investigation, I will try to replicate the issue and check your solution.