.OFF文件在Python上 [英] .OFF files on Python

查看:442
本文介绍了.OFF文件在Python上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Python和PyOpenGL进行练习,但是似乎无法打开.OFF文件(

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

如果您想知道,.OFF文件是包含3D对象位置的文件.

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

我想读取此文件并使其显示在python上.

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

示例应如下所示: http://people.sc.fsu.edu/~jburkardt/data/off/box.png

到目前为止,我要做的是手动写入.OFF文件中的每个坐标.但是该程序必须能够读取您提供给它的每个.OFF文件.

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),
   ...
   )

还有

 surfacesCube = (
   (0,1,2,3),
   (2,3,5,4),
   (4,5,7,6),
   ...
   )

要这样做:

 def Read(verticies,surfaces):
   glBegin(QL_QUADS)
   for surface in surfaces:
      for vertex in surface:
        glVertex3fv(verticies[vertex])
   glEnd()

然后我运行Read(verticiesCube,surfacesCube),然后出现一个Cube. 我的观点是,您需要能够对每个.OFF文件执行此操作,有时并非所有文件都相等(它们也指定了颜色,或者有时在数字之前写了一些东西,所以我不知道如何跳过它们.例如:)

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.off)

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

因此,如何将.OFF文件的坐标和曲面保存到类似的元组中,以便可以应用Read()算法.

推荐答案

不知道第二行最后一个字段的含义.其余的可以轻松推论:

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

这篇关于.OFF文件在Python上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆