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

时间:2013-06-28 10:34:21

标签: python blender mesh wavefront

我正在Python 3.3中编写一个简单的命令行程序,它读取xyz坐标的文本文件,并在其间输出等效的三角形面。导出格式为Wavefront obj-files(https://en.wikipedia.org/wiki/Wavefront_.obj_file)。 algorthm仅用于处理来自地球高分辨率卫星扫描的常规间隔点。实际上,我正在使用一组大约340000个点并在顶点四元组之间创建2个三角形。外迭代在x方向上进行,而内迭代在y方向上。因此,为y方向上的每个顶点创建成对的三角形面,直到它在x方向上移动并重复该过程。我将向您展示主要模式(线条是面边缘):

v1--v5--v9
| \ | / |
v2--v6--v10
| / | \ |
v3--v7--v11
| \ | / |
v4--v8--v12

代码似乎有效,因为在Blender或MeshLab中导入文件会产生合理的结果,除了一件事:所有面对的条纹似乎都没有沿着x轴与它们的邻居连接。演示问题的渲染图片: unconnected stripes

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

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

1 个答案:

答案 0 :(得分:1)

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

我的猜测: Wavefront obj格式的数字字节大小太小。或者更正确:常见的3D程序不要指望数字像现实世界那么大。这样他们就会以混乱的方式解释他们得到的东西。

我希望这个解决方案能帮助将来的某个人!