我需要帮助开始使用 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()
最佳答案
我没用过,但你可以试试这个: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-Triangle 交集。当然你可以稍微优化一下 for 循环 :)
关于python - 在 Python 中,我如何体素化 3D 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11851342/