我试图解析vrml文件中的一些文本块。具体地说,我对indexedfaceset中包含的信息感兴趣,它表示由其坐标点(field points)和点之间的连通性(field coordIndex)定义的一系列三角形。在
我已经成功地创建了一个函数来解析文本的单个indexedFaceSet(第一个),但是我无法理解如何修改代码,以便可以为每个indexedFaceSet存储以下列表[translate,verts,facets,normals]
。在
我的职能是:
def extractDataFromVRML(root):
#Lists to be populated
translate=[]
verts=[]
facets=[]
normals=[]
#Parsing the vrml file
f=open(root+'.wrl')
while 1:# skip through initial stuff
linney=str.split(f.readline())
# print linney
if len(linney) == 4:
if linney[0] == 'translation': #Not shown in th vrml example
transx = float(linney[1])
transy = float(linney[2])
transz = float(linney[3])
t=[transx,transy,transz]
translate.append(t)
if linney == ['point', '[']:
break
#print 'Reading vertex coordinates.'
while 1:
xyz = f.readline()
i = str.find(xyz,',')
if i < 0: # end of vertex coordinates
break
verts.append(xyz[:i]) # building a list of xyz strings
print ('We have',len(verts)-1,'vertices.')
#for v in verts: print v
print ('Reading triangles.')
while 1:
linney=f.readline()
if linney == '}\n':
break # end of file
abc = str.split(str.replace(linney,',',' ')) # l of vertex ids
if len(abc) < 3:
continue # separation between groups of triangles
# look up a vertex in the list to generate a triangle tuple
xyz = str.split(verts[int(abc[0])])
p1=[float(xyz[0]),float(xyz[1]),float(xyz[2])]
xyz = str.split(verts[int(abc[1])])
p2=[float(xyz[0]),float(xyz[1]),float(xyz[2])]
xyz = str.split(verts[int(abc[2])])
p3=[float(xyz[0]),float(xyz[1]),float(xyz[2])]
tri=[p1,p2,p3]
facets.append(tri)
#n=getNormals(p1[0],p1[1],p1[2],p2[0],p2[1],p2[2],p3[0],p3[1],p3[2])
#normals.append(n)
print ('END')
return [translate,verts,facets,normals]
vrml中的立方体示例如下:
^{pr2}$
这是一个非常重要的解析问题。如果您能找到一些Python包进行解析,那就最好了。我的方法是:逐行扫描文件。如果我看到我理解的东西,那么将剩余的行传递给该块的解析器。例如,我有解析器来解析以“geometry IndexedFaceSet”行开头的块。在
输出
^{pr2}$注释
logging
库创建的itertools.takewhile()
和itertools.dropwhile()
函数来简化一些任务相关问题 更多 >
编程相关推荐