explanation
explanation
“I am not 100% understand what is the syntax of the dat files.”
I would just have to infer it from the example you pointed to.
I would just have to infer it from the example you pointed to.
“Does anyone have any experience with such files?”
Well, it would be application-dependent, so prior experience doesn’t necessarily translate to this situation.
Well, it would be application-dependent, so prior experience doesn’t necessarily translate to this situation.
“How can Mathematica generate such dat files?”
I’ve broken it down into small steps that are hopefully sufficiently self-explanatory. To get the raw mesh data, you can either directly access the mesh data structure, or you can use the helper functions MeshCoordinates and MeshCells. Mathematica indexes lists starting at 1, but the example you pointed to indicates that indices start at 0, so be careful to do that conversion (I’ve done it in AsFacetDAT). One issue might be with the patches file. I assumed that the patch delimiter is simply a hash character, “#”, even though in the example you pointed to there are longer comments like “# end of facet 0”. The reason I assumed that the “#” had any semantic meaning at all is because it said that facets/patches can be three or four vertices, and I didn’t see any other indicator of patch length. Also, I may be misunderstanding something, because the patches file seems redundant (since everything can be determined from the other two files without the need of the patches file). So my approach may need to be fixed in that regard.
I’ve broken it down into small steps that are hopefully sufficiently self-explanatory. To get the raw mesh data, you can either directly access the mesh data structure, or you can use the helper functions MeshCoordinates and MeshCells. Mathematica indexes lists starting at 1, but the example you pointed to indicates that indices start at 0, so be careful to do that conversion (I’ve done it in AsFacetDAT). One issue might be with the patches file. I assumed that the patch delimiter is simply a hash character, “#”, even though in the example you pointed to there are longer comments like “# end of facet 0”. The reason I assumed that the “#” had any semantic meaning at all is because it said that facets/patches can be three or four vertices, and I didn’t see any other indicator of patch length. Also, I may be misunderstanding something, because the patches file seems redundant (since everything can be determined from the other two files without the need of the patches file). So my approach may need to be fixed in that regard.
seashell mesh
seashell mesh
In[]:=
seashell=Import["ExampleData/seashell.stl"]
Out[]=
In[]:=
MeshCoordinates[seashell]//Short
Out[]//Short=
{{-0.4408,-0.010667,0.018667},913,{-0.3096,-20,0.018667}}
In[]:=
MeshCells[seashell,2]//Short
Out[]//Short=
{Polygon[{1,2,3}],Polygon[{1,3,4}],Polygon[{2,5,6}],1674,Polygon[{898,914,913}],Polygon[{899,900,915}],Polygon[{899,915,914}]}
the vertices file
the vertices file
In[]:=
AsVertextDAT[pts:{{_,_,_}...}]:=StringRiffle[pts,"\n","\t"]
In[]:=
ExportString[AsVertextDAT[MeshCoordinates[seashell]],"List"]//Short
Out[]//Short=
-0.4408 -0.010667 0.018667-0.2808 0.021333 0.018667-0.2830….018667-0.2904 -0.012267 0.018667-0.3096 -0.018667 0.018667
In[]:=
Export["test.vertices.dat",AsVertextDAT[MeshCoordinates[seashell]],"List"]
Out[]=
test.vertices.dat
the facets idx file
the facets idx file
In[]:=
AsFacetDAT[polys:{Polygon[{__Integer}]...}]:=StringRiffle[polys[[All,1]]-1,"\n","\t"]
In[]:=
ExportString[AsFacetDAT[MeshCells[seashell,2]],"List"]//Short
Out[]//Short=
0 1 20 2 31 4 51 5 24 6 74 7 56 8 96 9 78 10 118 11…2896 912 911897 898 913897 913 912898 899 914898 914 913
In[]:=
Export["test.facetIdx.dat",AsFacetDAT[MeshCells[seashell,2]],"List"]
Out[]=
test.facetIdx.dat
the patches file
the patches file
In[]:=
NormalizeMeshData[pts:{{_,_,_}...},polys:{Polygon[{__Integer}]...}]:=Map[Extract[pts,#]&,polys[[All,1]],{-1}]
In[]:=
AsPatchDAT[facets:{{{_,_,_}...}...}]:=StringRiffle[facets,"\n#\n","\n","\t"]
In[]:=
Short[ExportString[AsPatchDAT[NormalizeMeshData[MeshCoordinates[seashell],MeshCells[seashell,2]]],"List"],3]
Out[]//Short=
-0.4408 -0.010667 0.018667-0.2808 0.021333 0.018667-0.28304 0.018868 0.006471#-0.4408 -0.010667 0.018667-0.28304 0.018868 0.006471-0.4…6-0.30032 -0.018667 0.018667-0.3096 -0.018667 0.018667#-0.27984 -0.012174 0.020776-0.3096 -0.018667 0.018667-0.2904 -0.012267 0.018667
In[]:=
Export["test.patches.dat",AsPatchDAT[NormalizeMeshData[MeshCoordinates[seashell],MeshCells[seashell,2]]],"List"]
Out[]=
test.patches.dat