0

我有一个形状为 (Number_voxel,4) 的体素坐标张量。第二个暗淡仅指示该体素的 batch_id 和坐标 (batch_id,x,y,z)。

如何使用此张量创建可以在 meshlab 上看到的体素?

目前,我使用这些代码,但结果并不好。

 import numpy as np
 import os
 from plyfile import PlyData, PlyElement



def write_ply(points, face_data, filename, text=True):

  points = [(points[i,0], points[i,1], points[i,2]) for i in range(points.shape[0])]

  vertex = np.array(points, dtype=[('x', 'f4'), ('y', 'f4'),('z', 'f4')])

  face = np.empty(len(face_data),dtype=[('vertex_indices', 'i4', (4,))])
  face['vertex_indices'] = face_data

  ply_faces = PlyElement.describe(face, 'face')
  ply_vertexs = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
  PlyData([ply_vertexs, ply_faces], text=text).write(filename)

def occ2points(coordinates):
  points  = []
  len = coordinates.shape[0]
  for i in range(len):
    points.append(np.array([int(coordinates[i,1]),int(coordinates[i,2]),int(coordinates[i,3])]))
 
  return np.array(points)

def generate_faces(points):
  corners = np.zeros((8*len(points),3))
  faces = np.zeros((6*len(points),4))
  for index in range(len(points)):
    corners[index*8]= np.array([points[index,0]-0.5, points[index,1]-0.5, points[index,2]-0.5])
    corners[index*8+1]= np.array([points[index,0]+0.5, points[index,1]-0.5, points[index,2]-0.5])
    corners[index*8+2]= np.array([points[index,0]-0.5, points[index,1]+0.5, points[index,2]-0.5])
    corners[index*8+3]= np.array([points[index,0]+0.5, points[index,1]+0.5, points[index,2]-0.5])
    corners[index*8+4]= np.array([points[index,0]-0.5, points[index,1]-0.5, points[index,2]+0.5])
    corners[index*8+5]= np.array([points[index,0]+0.5, points[index,1]-0.5, points[index,2]+0.5])
    corners[index*8+6]= np.array([points[index,0]-0.5, points[index,1]+0.5, points[index,2]+0.5])
    corners[index*8+7]= np.array([points[index,0]+0.5, points[index,1]+0.5, points[index,2]+0.5])
    faces[index*6]= np.array([len(points)+8*index, len(points)+8*index+1,len(points)+8*index+2,len(points)+8*index+3])
    faces[index*6+1]= np.array([len(points)+8*index+4, len(points)+8*index+5,len(points)+8*index+6,len(points)+8*index+7])
    faces[index*6+2]= np.array([len(points)+8*index+2, len(points)+8*index+3,len(points)+8*index+6,len(points)+8*index+7])
    faces[index*6+3]= np.array([len(points)+8*index+0, len(points)+8*index+1,len(points)+8*index+4,len(points)+8*index+5])
    faces[index*6+4]= np.array([len(points)+8*index+0, len(points)+8*index+2,len(points)+8*index+4,len(points)+8*index+6])
    faces[index*6+5]= np.array([len(points)+8*index+1, len(points)+8*index+3,len(points)+8*index+5,len(points)+8*index+7])
return corners, faces

def writeocc(coordinates,save_path,filename):
  points = occ2points(coordinates)
  #print(points.shape)
  corners, faces = generate_faces(points)
  if points.shape[0] == 0:
      print('the predicted mesh has zero point!')
  else:
      points = np.concatenate((points,corners),axis=0)
      write_ply(points, faces, os.path.join(save_path,filename))

''' 就像这张图片:体素占用的可视化 谢谢大家!

4

1 回答 1

0

看起来您的代码只需要稍作更改即可确保存储的面保持方向的一致性。

将函数中的面定义替换为generate_faces(points)

    base=len(points)+8*index
    faces[index*6]= np.array([base+2, base+3,base+1,base+0])
    faces[index*6+1]= np.array([base+4, base+5, base+7,base+6])
    faces[index*6+2]= np.array([base+3, base+2, base+6,base+7])
    faces[index*6+3]= np.array([base+0, base+1, base+5,base+4])
    faces[index*6+4]= np.array([base+2, base+0,base+4,base+6])
    faces[index*6+5]= np.array([base+1, base+3,base+7,base+5])
    
  return corners, faces

这是一对体素的结果......

在此处输入图像描述

于 2022-01-12T13:34:36.740 回答