Skip to main content
 首页 » 编程设计

Python 3.3之从规则间隔的顶点创建 3D 网格作为 Wavefront obj 文件

2024年11月01日8linjiqin

我正在用 Python 3.3 编写一个简单的命令行程序,它读取一个包含 xyz 坐标的文本文件,并输出它们之间的等效三角形面。导出格式为 Wavefront 对象文件 ( 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 轴的邻居相连。演示问题的渲染图片: unconnected stripes .

通常,不同的面部条纹之间不应有垂直偏移,因为它们沿内部边界(线)共享相同的顶点。具有较少顶点和更常见的低坐标值的测试成功。该方法工作得很好。也许问题不在于我的网格生成器,而在于 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]] 

我希望你们中的一些人能帮助我摆脱痛苦。如果需要更多解释,请告诉我,我会将其添加到第一篇文章中。

请您参考如下方法:

我终于解决了这个问题。问题不在我的网格生成器程序中。当顶点坐标太大时,Blender 和 MeshLab(很可能还有其他 3D 程序)会做一些奇怪的事情。如果我将现实世界的地理投影坐标减少到更小的相对坐标,一切正常(https://dl.dropboxusercontent.com/u/13547611/meshGenWorking001.png)。

我的猜测: Wavefront obj-format 的字节大小对于它的数字来说太有限了。或者更正确地说:普通 3D 程序不希望数字像现实世界那样大。他们以一种令人困惑的方式解释他们得到的东西。

我希望这个解决方案对 future 的人有所帮助!