python 格式文件 读取off,.OFF文件在Python上

I've been practicing with Python and PyOpenGL, but I can't seem to open .OFF files (Object File Format) with Python.

If you're wondering, .OFF files are files that contain positions of a 3D object.

OFF

8 6 0

-0.500000 -0.500000 0.500000

0.500000 -0.500000 0.500000

-0.500000 0.500000 0.500000

0.500000 0.500000 0.500000

-0.500000 0.500000 -0.500000

0.500000 0.500000 -0.500000

-0.500000 -0.500000 -0.500000

0.500000 -0.500000 -0.500000

4 0 1 3 2

4 2 3 5 4

4 4 5 7 6

4 6 7 1 0

4 1 7 5 3

4 6 0 2 4

I want to read this file and make it appear on python.

So far what I've had to do is write every coordinate from the .OFF file manually. But the program needs to be able to read every .OFF file you give it to.

I could only make the example I gave because I made a tuple of tuples for both the verticies and the surfaces:

verticiesCube = (

(-0.5,-0.5,0.5),

(0.5,-0.5,0.5),

(-0.5,0.5,0.5),

...

)

And

surfacesCube = (

(0,1,2,3),

(2,3,5,4),

(4,5,7,6),

...

)

To then do:

def Read(verticies,surfaces):

glBegin(QL_QUADS)

for surface in surfaces:

for vertex in surface:

glVertex3fv(verticies[vertex])

glEnd()

Then I run Read(verticiesCube, surfacesCube) and a Cube appears.

My point is that you need to be able to do this with every .OFF file, and sometimes not all of them are equal (they have colors specified as well, or sometimes there are things written before the numbers so I don't know how to skip them. Example:)

OFF

#

# cone.off

#

22 40 120

0.000000 1.000000 0.000000

0.000000 0.000000 0.000000

0.500000 0.000000 0.000000

(The #'s and the cone.off are the things I mentioned)

So how do I save the coordinates and surfaces of the .OFF files into similar tuples so I can apply the Read() algorithm.

解决方案

No idea on what the last field in the second line means. The rest can be deduced easily:

def read_off(file):

if 'OFF' != file.readline().strip():

raise('Not a valid OFF header')

n_verts, n_faces, n_dontknow = tuple([int(s) for s in file.readline().strip().split(' ')])

verts = [[float(s) for s in file.readline().strip().split(' ')] for i_vert in range(n_verts)]

faces = [[int(s) for s in file.readline().strip().split(' ')][1:] for i_face in range(n_faces)]

return verts, faces

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值