2013-06-28 55 views
2

我正在Python 3.3中編寫一個簡單的命令行程序,該程序讀取xyz座標的文本文件,並輸出等值三角面片。導出格式爲Wavefront目標文件(https://en.wikipedia.org/wiki/Wavefront_.obj_file)。該算法僅用於與來自地球高分辨率衛星掃描的規則間隔點。實際上,我使用了一組約340000個點,並在頂點四字形之間創建了2個三角形。外迭代沿x方向進行,而內迭代沿y方向進行。因此,爲y方向上的每個頂點創建一對三角形面,直到它在x方向上移動並重復該過程。我會告訴你的原則,模式(線臉部棱角):Python 3.3 - 創建三維網格作爲Wavefront obj-file從規律間隔的頂點

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

代碼似乎在上班途中在攪拌機或MeshLab導入的文件給出了合理的結果,除了一兩件事:所有的條紋臉對似乎沒有沿着x軸與它們的鄰居連接。顯示問題的渲染圖片: unconnected stripes

通常,不應該有不同的表面條紋之間的垂直偏移,因爲它們沿着它們的內部邊界(-line)共享相同的頂點。具有較少頂點和較常見的低座標值的測試成功。該方法工作得很好。也許問題不在於我的網格生成器,而是在Blender,MeshLab等的座標限制內。

下面是其在返回字符串生成面和針跡的everythin一起功能:

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) 

其被輸入到該函數的頂點 - 變量具有2維列表的形式,類似到:

#      x     y     z 
vertsExample = [[3334, 3333, 3332], [2555, 2554, 2553], [10.2, 5.2, 6.7]] 

我希望你們中的一些人能夠幫助我擺脫苦難。如果需要更多解釋,請告訴我,我會將其添加到第一篇文章中。

回答

1

我終於解決了這個問題。問題不在我的網格生成器程序中。當頂點座標太大時,Blender和MeshLab(以及很可能的其他3D程序)會做一些奇怪的事情。如果我將現實世界的地理投影座標減小到較小的相對座標,則一切正常(https://dl.dropboxusercontent.com/u/13547611/meshGenWorking001.png)。

我的猜測: Wavefront obj格式的數字字節大小有限。或者更正確一些:常見的3D程序並不指望數字像真實世界一樣大。這樣他們就會以令人困惑的方式解釋他們所得到的。

我希望這個解決方案可以幫助未來的人!