问题
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.
The example should be like this: http://people.sc.fsu.edu/~jburkardt/data/off/box.png
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.
回答1:
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
来源:https://stackoverflow.com/questions/31129968/off-files-on-python