在Python中,如何对三维网格进行体素化

2024-04-20 16:11:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要帮助开始使用Python(我几乎一无所知)来对Rhino生成的3D网格进行体素化。数据输入是一个.OBJ文件,输出也是。这种用法的最终目的是找出建筑物内两点之间的最短距离。但那是以后的事了。现在,我需要首先对三维网格进行体素化。体素化原语可能只是一个简单的立方体。

到目前为止,我可以从一个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()

Tags: obj网格floatminmaxinfprintinputfile
1条回答
网友
1楼 · 发布于 2024-04-20 16:11:28

我没用过,但你可以试试这个: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
    

您只需实现一个边界框三角形相交。当然,您可以对循环进行一些优化:)

相关问题 更多 >