Python 3.3-从规则间隔的顶点创建三维网格作为波前obj文件

Python 3.3-从规则间隔的顶点创建三维网格作为波前obj文件,python,blender,mesh,wavefront,Python,Blender,Mesh,Wavefront,我正在用Python3.3编写一个简单的命令行程序,该程序读取xyz坐标的文本文件,并输出一个对应的三角形面。导出格式为波前obj文件()。该算法仅用于处理高分辨率卫星对地球扫描的规则间隔点。实际上,我正在使用一组大约340000个点,并在一个四顶点之间创建两个三角形。外部迭代在x方向进行,而内部迭代在y方向上进行。因此,将为y方向上的每个顶点创建一对三角形面,直到它在x方向上移动并重复该过程。我将向您展示基本图案(线条为面边): 该代码的工作方式似乎与在Blender或MeshLab中导入文件

我正在用Python3.3编写一个简单的命令行程序,该程序读取xyz坐标的文本文件,并输出一个对应的三角形面。导出格式为波前obj文件()。该算法仅用于处理高分辨率卫星对地球扫描的规则间隔点。实际上,我正在使用一组大约340000个点,并在一个四顶点之间创建两个三角形。外部迭代在x方向进行,而内部迭代在y方向上进行。因此,将为y方向上的每个顶点创建一对三角形面,直到它在x方向上移动并重复该过程。我将向您展示基本图案(线条为面边):

该代码的工作方式似乎与在Blender或MeshLab中导入文件的方式相同,但有一点除外:面对的所有条纹似乎都没有沿x轴与其相邻条纹相连。演示问题的渲染图片:

通常,不同的面条纹之间不应该有垂直偏移,因为它们沿内部边界(-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)

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

我终于解决了这个问题。问题不在我的网格生成器程序中。Blender和MeshLab(很可能还有其他3D程序)在顶点坐标太大时会做一些奇怪的事情。如果我将真实世界的地理投影坐标减少到较小的相对坐标,那么一切都很好()

我猜: 波阵面obj格式的字节大小对于其数字来说太有限。或者更准确地说:普通3D程序并不期望数字像现实世界中的数字那么大。通过这种方式,他们以一种令人困惑的方式解释他们得到的东西

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

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)
#                       x                   y                   z
vertsExample = [[3334, 3333, 3332], [2555, 2554, 2553], [10.2, 5.2, 6.7]]