在Python中,我如何体素化3D网格

use*_*600 10 python 3d mesh voxel .obj

我需要帮助开始使用Python(我几乎不知道)来体验从Rhino生成的3D网格.数据输入将是.OBJ文件,输出也是如此.这种用法的最终目的是找到建筑物内两点之间的最短距离.但这是为了以后.至于现在,我需要首先体素化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()
Run Code Online (Sandbox Code Playgroud)

kol*_*nda 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
    
    Run Code Online (Sandbox Code Playgroud)

您需要做的就是实现BoundingBox-Triangle交集.当然你可以优化那些循环:)

  • Binvox已搬到[这个位置](http://www.patrickmin.com/binvox/) (2认同)