我正在 Python 3.3 中编写一个简单的命令行程序,它读取 xyz 坐标的文本文件并在两者之间输出一个等效的三角形面。导出格式为 Wavefront obj 文件 ( https://en.wikipedia.org/wiki/Wavefront_.obj_file )。该算法仅用于处理来自地球的高分辨率卫星扫描的规则间隔点。实际上,我正在使用一组大约 340000 个点并在顶点四元组之间创建 2 个三角形。外部迭代在 x 方向上进行,而内部迭代在 y 方向上进行。因此,在 y 方向上为每个顶点创建成对的三角形面,直到它在 x 方向上移动并重复该过程。我将向您展示基本模式(线条是面部边缘):
v1--v5--v9
| \ | / |
v2--v6--v10
| / | \ |
v3--v7--v11
| \ | / |
v4--v8--v12
代码似乎可以正常工作,因为在 Blender 或 MeshLab 中导入文件会给出合理的结果,除了一件事:面部对的所有条纹似乎都没有与沿 x 轴的邻居相连。演示问题的渲染图片: 未连接的条纹。
通常,不同的面条纹之间不应该存在垂直偏移,因为它们沿内部边界(-线)共享相同的顶点。使用较少顶点和更常见的低坐标值的测试成功。该方法运行良好。也许问题不在于我的网格生成器,而在于 Blender、MeshLab 等的坐标限制。
这是在返回字符串中生成面部并将所有内容缝合在一起的函数:
def simpleTriangMesh(verts):
printAll("--creating simple triangulated mesh", "\n")
maxCoords = [max(verts[0]), max(verts[1]), max(verts[2])]
minCoords = [min(verts[0]), min(verts[1]), min(verts[2])]
printAll("max. coordinates (xyz): \n", maxCoords, "\n")
printAll("min. coordinates (xyz): \n", minCoords, "\n")
xVerts = 0 # amount of vertices in x-direction
yVerts = 0 # amount of vertices in y-direction
faceAmount = 0 # amount of required faces to skin grid
i = 0
temp = verts[0][0]
while(i < len(verts[0])):
if(temp < verts[0][i]):
yVerts = int(i)
break
temp = verts[0][i]
i += 1
xVerts = int(len(verts[0]) / float(yVerts))
faceAmount = ((xVerts - 1) * (yVerts - 1)) * 2
printAll("vertices in x direction: ", xVerts, "\n")
printAll("vertices in y direction: ", yVerts, "\n")
printAll("estimated amount of triangle faces: ",
faceAmount, "\n")
printAll("----generating vertex triangles representing the faces", "\n")
# list of vertex-index quadrupels representing the faces
faceList = [[0 for line in range(0, 3)] for face in range(0, int(faceAmount))]
f = 0
v = 0
# rather to draw hypotenuse of the triangles from topleft to bottomright
# or perpendicular to that (topright to bottomleft)
tl = True # the one that changes in y-direction
tl_rem = False # to remember the hypotenuse direction of the last topmost faces
while(f < len(faceList)):
# prevent creation of faces at the bottom line
# + guarantees that v = 1 when creating the first face
if(( v % yVerts ) == 0):
v += 1
tl = not tl_rem
tl_rem = tl
if(tl):
faceList[f][0] = v
faceList[f][1] = v + yVerts
faceList[f][2] = v + yVerts + 1
f += 1
faceList[f][0] = v
faceList[f][1] = v + yVerts + 1
faceList[f][2] = v + 1
else:
faceList[f][0] = v
faceList[f][1] = v + yVerts
faceList[f][2] = v + 1
f += 1
faceList[f][0] = v + 1
faceList[f][1] = v + yVerts
faceList[f][2] = v + yVerts + 1
f += 1
v += 1
tl = not tl
printAll("----preparing obj-file-content for export", "\n")
rectMesh_Obj = "" # string containing the mesh in obj-format (ascii)
tempVerts = ""
tempFaces = ""
row = 0
while(row < len(verts[0])):
# temp = ("v" + " " + str(verts[0][row]) + " " + str(verts[1][row])
# + " " + str(verts[2][row]) + "\n")
temp = ("v" + " " + str(verts[0][row]) + " " + str(verts[2][row])
+ " " + str(verts[1][row]) + "\n")
tempVerts += temp
row += 1
row = 0
while(row < len(faceList)):
temp = ("f"
+ " " + str(int(faceList[row][0]))
+ " " + str(int(faceList[row][1]))
+ " " + str(int(faceList[row][2]))
# + " " + str(int(faceList[row][3]))
+ "\n")
tempFaces += temp
row += 1
rectMesh_Obj += tempVerts + tempFaces
return(rectMesh_Obj)
输入到函数中的 verts-variable 具有二维列表的形式,类似于:
# x y z
vertsExample = [[3334, 3333, 3332], [2555, 2554, 2553], [10.2, 5.2, 6.7]]
我希望你们中的一些人能帮助我摆脱困境。如果有什么需要更多解释的,请告诉我,我会将其添加到第一篇文章中。