在python中从3d数组创建一个.obj文件

Die*_*ana 7 python unity-game-engine marching-cubes .obj nifti

我的目标是使用python从一个漂亮的(.nii)格式获取.obj文件,目的是在Unity上打开它.我知道"scikit-image"软件包有一个名为"measure"的模块,它实现了Marching cube算法.我将行进立方体算法应用于我的数据,并获得我期望的结果:

verts, faces, normals, values = measure.marching_cubes_lewiner(nifty_data, 0)
Run Code Online (Sandbox Code Playgroud)

然后我可以绘制数据:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(verts[:, 0], verts[:,1], faces, verts[:, 2],
                linewidth=0.2, antialiased=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我已经找到了将数据(顶点,面法线,值)保存为.obj的函数,但我还没找到.因此,我决定自己建造它.

thefile = open('test.obj', 'w')
for item in verts:
  thefile.write("v {0} {1} {2}\n".format(item[0],item[1],item[2]))

for item in normals:
  thefile.write("vn {0} {1} {2}\n".format(item[0],item[1],item[2]))

for item in faces:
  thefile.write("f {0}//{0} {1}//{1} {2}//{2}\n".format(item[0],item[1],item[2]))  

thefile.close()
Run Code Online (Sandbox Code Playgroud)

但是当我将数据导入到unity时,我得到了以下结果:

在此输入图像描述

在此输入图像描述

所以我的问题如下:

  • 我在.obj制作过程中做错了什么?
  • 是否有更好的方式执行此操作的模块或功能?
  • 有可能做我想做的事吗?

谢谢.

更多例子:

蟒蛇:

在此输入图像描述

统一:

在此输入图像描述

Die*_*ana 9

解决方案:经过数小时的调试,解决方案非常简单!只需将+1添加到应用行进多维数据集所提供的面部数据即可。问题在于Python认为顶点从0开始,而unity则认为它们从1开始。这就是为什么它们不匹配的原因!没关系。

顶点,面孔,法线,值= measure.marching_cubes_lewiner(nifty_data,0)

面孔=面孔+1

成功!

在此处输入图片说明