fangq / brain2mesh

Brain2Mesh - a one-liner for high-quality brain mesh generation
http://mcx.space/brain2mesh
Other
34 stars 14 forks source link

Error when calling brain2mesh #13

Open tmedani opened 3 years ago

tmedani commented 3 years ago

Hi @fangq,

I'm trying to run the bran2mesh function on a segmented MRI into 5 tissues, but I'm not able to make it work as expected, here is my example :

the link below contains the "tissues" data which is the segmentation of the MRI into 5 main tissues, ordered from inner to outer (label 1 to 5) wm(1), gm(2), csf(3), skull(4) and scalp(5)

here is the code

load('tissues') % download from this link https://www.dropbox.com/t/XBpvzMCBg9acTGTB
seg2 = [];
names={'wm','gm','csf','skull','scalp'};
for i = 5:-1:1
    seg2.(names{i}) = uint8(tissues.Cube==i);
end

cfg.dotruncate = 1;
[node,elem,face] = brain2mesh(seg2,cfg);

here is the output

>> cfg.dotruncate = 1;
[node,elem,face] = brain2mesh(seg2,cfg);
95  segname=fieldnames(density);
extracting surfaces from a volume ...
region 1 centroid : 130.000000 91.666667 1.333333

processing threshold level 1...
Surface Mesh Extraction Utility (Based on CGAL 3.6) 
(modified for iso2mesh by Qianqian Fang) 
http://iso2mesh.sf.net 

RNG seed 1648335518 
set initial mesh size to 50 
Refining... maximum node number is set to 100000 
reach maximum node limit, element angular bound may not satisfied 

done. 
Final number of points: 100000 
surface mesh generation is complete
extracting surfaces from a volume ...
region 1 centroid : 119.500000 21.666667 1.333333

processing threshold level 1...
Surface Mesh Extraction Utility (Based on CGAL 3.6) 
(modified for iso2mesh by Qianqian Fang) 
http://iso2mesh.sf.net 

RNG seed 1648335518 
set initial mesh size to 50 
Refining... maximum node number is set to 100000 

done. 
Final number of points: 40491 
surface mesh generation is complete
extracting surfaces from a volume ...
region 1 centroid : 117.500000 18.666667 1.333333

processing threshold level 1...
Surface Mesh Extraction Utility (Based on CGAL 3.6) 
(modified for iso2mesh by Qianqian Fang) 
http://iso2mesh.sf.net 

RNG seed 1648335518 
set initial mesh size to 50 
Refining... maximum node number is set to 100000 
reach maximum node limit, element angular bound may not satisfied 

done. 
Final number of points: 100000 
surface mesh generation is complete
extracting surfaces from a volume ...
region 1 centroid : 116.000000 16.666667 1.333333

processing threshold level 1...
Surface Mesh Extraction Utility (Based on CGAL 3.6) 
(modified for iso2mesh by Qianqian Fang) 
http://iso2mesh.sf.net 

RNG seed 1648335518 
set initial mesh size to 50 
Refining... maximum node number is set to 100000 

done. 
Final number of points: 19658 
surface mesh generation is complete
generating tetrahedral mesh from closed surfaces ...
creating volumetric mesh from a surface mesh ...
volume mesh generation is complete
extracting surfaces from a volume ...
region 1 centroid : 114.000000 14.666667 1.333333

processing threshold level 1...
Surface Mesh Extraction Utility (Based on CGAL 3.6) 
(modified for iso2mesh by Qianqian Fang) 
http://iso2mesh.sf.net 

RNG seed 1648335518 
set initial mesh size to 50 
Refining... maximum node number is set to 100000 

done. 
Final number of points: 1835 
surface mesh generation is complete
generating tetrahedral mesh from closed surfaces ...
creating volumetric mesh from a surface mesh ...
volume mesh generation is complete
Error using surfboolean (line 188)
surface boolean command failed:
cd "C:\Users\33649\AppData\Local\Temp\iso2mesh-medani\" && "G:\My Drive\GitFolders\GitHub\iso2mesh\bin\cork.exe" -first
"C:\Users\33649\AppData\Local\Temp\iso2mesh-medani\pre_surfbool1.off" "C:\Users\33649\AppData\Local\Temp\iso2mesh-medani\pre_surfbool2.off"
"C:\Users\33649\AppData\Local\Temp\iso2mesh-medani\post_surfbool.off" -1648335518
ERROR: error at c:\users\fangq\cork\src\mesh\mesh.isct.tpp, line #1063: Ran out of tries to perturb the mesh

Error in brain2mesh (line 235)
        [final_surf_n,final_surf_f] = surfboolean(nbox,fbox(:,[1 3 2]),'first',surf_n,surf_f);

As usual, I will appreciate it If you can orient me to solve this issue.

Thank you in advance for your help.

best,

fangq commented 2 years ago

this is embarrassing - I did not notice this question posted over a year ago. looks like the dropbox link has expired - let me know if this still an issue.

ftadel commented 2 years ago

@tmedani Can you please check that this is not an issue anymore? Otherwise, can you post the data again? Thanks

tmedani commented 2 years ago

Hi @fangq,

this is embarrassing - I did not notice this question posted over a year ago. looks like the dropbox link has expired - let me know if this still an issue.

no problem, we moved to another task in meantime and this one was not the top priority.

@tmedani Can you please check that this is not an issue anymore?

so the issue is still, I generated an equivalent data, here is the link to it: https://www.dropbox.com/s/57nyv6nz6xi6qwf/tissues.mat?dl=0

load('tissues') % download from this link https://www.dropbox.com/s/57nyv6nz6xi6qwf/tissues.mat?dl=0
seg2 = [];
names={'wm','gm','csf','skull','scalp'};
for i = 5:-1:1
    seg2.(names{i}) = uint8(tissues.Cube==i);
end

cfg.dotruncate = 1;
[node,elem,face] = brain2mesh(seg2,cfg);

Thanks again for your help :)

fangq commented 2 years ago

I am able to reproduce this issue on my Linux desktop.

I believe the issue is caused by csf and gm surfaces are touching each-other. see this plot, where the red/black lines are gm surface poking outside of csf surface, and most of these are either co-planar or intersecting.

csf_surf

because cork, the surface boolean code only supports limited precision, it is insufficient to resolve co-planar surfaces/features.

there is a decouple option with surfboolean that can be used to push apart two surfaces that enclosing each other, but it is not used in the initial pass of the brain2mesh workflow. I will see what I can do to avoid this.

DerfelLee commented 2 years ago

Hi I was running the example script SPM_example_brain.m and SPM_example_wholehad.m, but it reported that 'valface cannot be recognized'. Ive already run the checkdependency file, the error still occurred. I wonder where i can find the valface.m file? Thanks and best regards

fangq commented 2 years ago

@DerfelLee, volface is a function in iso2mesh - I suppose you did not add path to iso2mesh, can you verify?

https://github.com/fangq/iso2mesh/blob/master/volface.m

DerfelLee commented 2 years ago

Thank you for your reply! However, I’m pretty sure that I’ve added the iso2mesh to the path. There is nothing wrong with the volface function, the error message is about ‘valface’. The error code is shown below. The first line of the error is that function ‘valface’ cannot be recognized. Thanks again and best regards. @.***

从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送

发件人: Qianqian @.> 发送时间: 2022年5月2日 20:22 收件人: @.> 抄送: @.>; @.> 主题: Re: [fangq/brain2mesh] Error when calling brain2mesh (#13)

@DerfelLeehttps://github.com/DerfelLee, volface is a function in iso2mesh - I suppose you did not add path to iso2mesh, can you verify?

https://github.com/fangq/iso2mesh/blob/master/volface.m

― Reply to this email directly, view it on GitHubhttps://github.com/fangq/brain2mesh/issues/13#issuecomment-1114785591, or unsubscribehttps://github.com/notifications/unsubscribe-auth/APMMVSPIGIBEIFZPLDPNQWLVH7CH3ANCNFSM42JYM4EQ. You are receiving this because you were mentioned.Message ID: @.***>

fangq commented 2 years ago

@DerfelLee, you were right, there is a typo in a recent update to meshabox.m in iso2mesh. now it should be fixed. please redownload iso2mesh from github.

DerfelLee commented 2 years ago

Thank you so much for replying me fixing the problem! Now I can successfully run the brain2mesh functions and get the ‘node’, ‘elem’ and ‘face’ matrixes. I am wondering whether these matrix can generate mesh files in ‘.mesh’ format and to be imported into AtlasViewer, is that possible? Btw, I noticed that there are still something wrong with the ‘brain1020’ function, the error code is shown below: @.*** The first line of the code says the index at position 1 is out of boundary.

Thanks again for paying attention on these issues and best regards!

从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送

发件人: Qianqian @.> 发送时间: 2022年5月3日 21:39 收件人: @.> 抄送: @.>; @.> 主题: Re: [fangq/brain2mesh] Error when calling brain2mesh (#13)

@DerfelLeehttps://github.com/DerfelLee, you were right, there is a typo in a recent update to meshabox.m in iso2mesh. now it should be fixed. please redownload iso2mesh from github.

― Reply to this email directly, view it on GitHubhttps://github.com/fangq/brain2mesh/issues/13#issuecomment-1116109653, or unsubscribehttps://github.com/notifications/unsubscribe-auth/APMMVSLKGE3WMRYBNT5KXNLVIET7PANCNFSM42JYM4EQ. You are receiving this because you were mentioned.Message ID: @.***>

fangq commented 2 years ago

@DerfelLee, did you see the index at position 1 is out of boundary using the built in examples or one of your datasets? if the latter, I would like to get a small script to reproduce, can you share?

tmedani commented 2 years ago

@fangq thanks for checking, This is confusing me now, the tissues that I shared are the outputs of the headreco process from simnibs. and these tissues should be free from any intersections. I will check that again and I will let you know.

btw, could we use these tissue with vol2mesh?

Thanks again for your support,

Best

tmedani commented 2 years ago

I am able to reproduce this issue on my Linux desktop.

I believe the issue is caused by csf and gm surfaces are touching each-other. see this plot, where the red/black lines are gm surface poking outside of csf surface, and most of these are either co-planar or intersecting.

csf_surf

is there a way to refine the surface extraction process from the volume, I think that the tissue volumes are free from the intersection, but the extracted surfaces are intersecting, if we refine the size of the triangles we can avoid the intersections.

DerfelLee commented 1 year ago

The problem occurred when using the built in examples

发自我的iPhone

在 2022年5月6日,22:22,Qianqian Fang @.***> 写道:



@DerfelLeehttps://github.com/DerfelLee, did you see the index at position 1 is out of boundary using the built in examples or one of your datasets? if the latter, I would like to get a small script to reproduce, can you share?

— Reply to this email directly, view it on GitHubhttps://github.com/fangq/brain2mesh/issues/13#issuecomment-1119677740, or unsubscribehttps://github.com/notifications/unsubscribe-auth/APMMVSNJ5ELNB5LALQTOGHDVIUTI5ANCNFSM42JYM4EQ. You are receiving this because you were mentioned.Message ID: @.***>

fangq commented 6 months ago

@DerfelLee and @tmedani, I am sorry that I haven't had chance working on brain2mesh for a while.

I am no longer able to download the dropbox sample data that previously failed. I am wondering if you can send me an updated link, I will have some time to take a deeper dive.

Update: never mind, I found a previously downloaded tissues.mat, and was able to reproduce the problem. let me find a workaround.

iamrblc commented 2 months ago

Hey,

I am a complete newbie MATLAB user, sorry if some of my description is trivial. I ran into a few issues (on Mac M1, all necessary folders are added to the path).

(0th issue:

1st issue:

seg = cat(4, scalp, skull, csf, gm, wm, air); [node, elem, face] = brain2mesh(seg);

Error: Index exceeds the number of array elements. Index must not exceed 5.

Fine, I removed 'air', although the documentation says it shouldn't be an issue, but for now let's try without it. Then it ran smoothly until...

2nd issue

Error using surfboolean
surface boolean command failed:
cd "<path>/iso2mesh-rblc/" &&
"<path>iso2mesh/bin/cork.mexmaca64" -resolve
"<path>/T/iso2mesh-rblc/pre_surfbool1.off"
"<path>T/iso2mesh-rblc/pre_surfbool2.off"
"<path>/T/iso2mesh-rblc/post_surfbool.off"
-1648335518
ERROR:

Error in brain2mesh (line 309)
        [surf_n,surf_f] = surfboolean(wm_n(:,1:3),wm_f(:,1:3),'resolve',pial_n,pial_f);

Error in mesh_creator (line 11)
[node, elem, face] = brain2mesh(seg);

I saw that a similar issue was reported before with this comment _"because cork, the surface boolean code only supports limited precision, it is insufficient to resolve co-planar surfaces/features.

there is a decouple option with surfboolean that can be used to push apart two surfaces that enclosing each other, but it is not used in the initial pass of the brain2mesh workflow. I will see what I can do to avoid this."_

Looking at the brain2mesh source code it seems like that the second loop with the decouple option is not reached despite of this fail, but I have no clue if this is really the issue, and if so, how I can overcome this.

Thanks.