2012-08-07 63 views
9

我需要Python入門(我幾乎一無所知)的幫助,以便對從Rhino生成的3D網格進行體素化。數據輸入將是.OBJ文件,輸出也是如此。這種用法的最終目的是找出建築物內兩點之間的最短距離。但那是爲了以後。至於現在,我需要先體素化3D網格。體素化原語可能只是一個簡單的立方體。在Python中,如何體元化3D網格

到目前爲止,我可以從OBJ文件解析器中讀取,並從解析的obj中讀取V,VT,VN,F前綴,並使用這些座標來查找3D對象的邊界框。什麼應該是正確的方式來體現網格?

import objParser 
import math 

inputFile = 'test.obj' 
vList = []; vtList = []; vnList = []; fList = [] 

def parseOBJ(inputFile): 
list = [] 
vList, vtList, vnList, fList = objParser.getObj(inputFile) 
print 'in parseOBJ' 
#print vList, vtList, vnList, fList 
return vList, vtList, vnList, fList 

def findBBox(vList): 
    i = 0; j=0; x_min = float('inf'); x_max = float('-inf'); y_min = float('inf'); 
    y_max = float('-inf'); z_min = float('inf'); z_max = float('-inf'); 
    xWidth = 0; yWidth = 0; zWidth =0 

print 'in findBBox' 
while i < len(vList): 
     #find min and max x value 
     if vList[i][j] < x_min: 
      x_min = float(vList[i][j]) 
     elif vList[i][j] > x_max: 
      x_max = float(vList[i][j]) 

     #find min and max y value 
     if vList[i][j + 1] < y_min: 
      y_min = float(vList[i][j + 1]) 
     elif vList[i][j + 1] > y_max: 
      y_max = float(vList[i][j + 1]) 

     #find min and max x value 
     if vList[i][j + 2] < z_min: 
      z_min = vList[i][j + 2] 
     elif vList[i][j + 2] > z_max: 
      z_max = vList[i][j + 2] 

     #incriment the counter int by 3 to go to the next set of (x, y, z) 
     i += 3; j=0 

xWidth = x_max - x_min 
yWidth = y_max - y_min 
zWidth = z_max - z_min 
length = xWidth, yWidth, zWidth 
volume = xWidth* yWidth* zWidth 
print 'x_min, y_min, z_min : ', x_min, y_min, z_min 
print 'x_max, y_max, z_max : ', x_max, y_max, z_max 
print 'xWidth, yWidth, zWidth : ', xWidth, yWidth, zWidth 
return length, volume 

def init(): 
    list = parseOBJ(inputFile) 
    findBBox(list[0]) 

print init() 

回答

6

我還沒有使用它,但你可以試試這個:http://packages.python.org/glitter/api/examples.voxelization-module.html

或者這個工具:http://www.patrickmin.com/binvox/

如果你想自己做,你有兩個主要途徑:

  • 當考慮到網格內部時,「真實」體素化 - 相當複雜,而且您需要大量的CSG操作。我無法幫到你。
  • 一個'假' - 一個體素每個三角形的網格。這非常簡單,您只需檢查三角形和軸對齊的立方體的交點即可。然後,你只是做:

    for every triagle: 
        for every cube: 
         if triangle intersects cube: 
          set cube = full 
         else: 
          set cube = empty 
    

所有你需要做的是實施的BoundingBox - 三角路口。當然,你可以優化那些循環有點:)

+2

Binvox已經轉移到[此位置](http://www.patrickmin.com/binvox/) – 2016-09-04 19:27:00

+0

感謝您的信息@A_A,我已更新鏈接。 – kolenda 2018-02-02 10:04:18