ckc99u commited on
Commit
23ce2fb
·
verified ·
1 Parent(s): f2b8f4d

Update RigNet/quick_start.py

Browse files
Files changed (1) hide show
  1. RigNet/quick_start.py +31 -33
RigNet/quick_start.py CHANGED
@@ -90,64 +90,61 @@ def create_single_data(mesh_filename):
90
  # batch
91
  batch = torch.zeros(len(v), dtype=torch.long)
92
 
93
- # ========== VOXEL: Use trimesh instead of binvox (NO X11 REQUIRED) ==========
94
  binvox_file = mesh_filename.replace('_remesh.obj', '_normalized.binvox')
95
 
96
  if not os.path.exists(binvox_file):
97
- print(f" voxelizing mesh with trimesh (no binvox)...")
98
 
99
  try:
100
  # Load mesh with trimesh
101
  mesh_tri = trimesh.load(normalized_obj)
102
 
103
- # Voxelize to 88x88x88 grid
104
- # Use exact=True for accurate inside/outside determination
105
- voxel_grid = mesh_tri.voxelized(pitch=1.0/88, method='subdivide')
 
 
 
106
 
107
- # Get voxel matrix and ensure it's 88x88x88
108
- vox_matrix = voxel_grid.matrix.astype(bool)
109
- current_shape = vox_matrix.shape
110
-
111
- # Create a centered 88x88x88 array
112
- final_vox = np.zeros((88, 88, 88), dtype=bool)
113
-
114
- # Calculate centering offsets
115
- offset_x = max(0, (88 - current_shape[0]) // 2)
116
- offset_y = max(0, (88 - current_shape[1]) // 2)
117
- offset_z = max(0, (88 - current_shape[2]) // 2)
118
 
119
- # Copy data to centered position
120
- end_x = min(offset_x + current_shape[0], 88)
121
- end_y = min(offset_y + current_shape[1], 88)
122
- end_z = min(offset_z + current_shape[2], 88)
123
-
124
- src_end_x = end_x - offset_x
125
- src_end_y = end_y - offset_y
126
- src_end_z = end_z - offset_z
127
 
128
- final_vox[offset_x:end_x, offset_y:end_y, offset_z:end_z] = \
129
- vox_matrix[:src_end_x, :src_end_y, :src_end_z]
 
 
 
 
 
 
130
 
131
- # Create binvox-compatible object
132
- class VoxelData:
133
- def __init__(self, data, dims, translate, scale):
134
  self.data = data
135
  self.dims = dims
136
  self.translate = translate
137
  self.scale = scale
 
138
 
139
- vox_obj = VoxelData(
140
- data=final_vox,
141
  dims=[88, 88, 88],
142
  translate=[0.0, 0.0, 0.0],
143
- scale=1.0
 
144
  )
145
 
146
  # Save as binvox format for caching
147
  with open(binvox_file, 'wb') as f:
148
  binvox_rw.write(vox_obj, f)
149
 
150
- print(f" ✓ Voxelization complete: {binvox_file} (trimesh method)")
151
 
152
  except Exception as e:
153
  print(f" ERROR: Trimesh voxelization failed: {e}")
@@ -159,6 +156,7 @@ def create_single_data(mesh_filename):
159
 
160
  data = Data(x=v[:, 3:6], pos=v[:, 0:3], tpl_edge_index=tpl_e, geo_edge_index=geo_e, batch=batch)
161
  return data, vox, surface_geodesic, translation_normalize, scale_normalize
 
162
  # def create_single_data(mesh_filename):
163
  # """
164
  # create input data for the network. The data is wrapped by Data structure in pytorch-geometric library
 
90
  # batch
91
  batch = torch.zeros(len(v), dtype=torch.long)
92
 
93
+ # voxel - Use trimesh instead of binvox (no X11 required)
94
  binvox_file = mesh_filename.replace('_remesh.obj', '_normalized.binvox')
95
 
96
  if not os.path.exists(binvox_file):
97
+ print(f" voxelizing mesh (trimesh-based, no binvox)...")
98
 
99
  try:
100
  # Load mesh with trimesh
101
  mesh_tri = trimesh.load(normalized_obj)
102
 
103
+ # Voxelize: create a 88x88x88 grid
104
+ # Calculate pitch to fit mesh in 88^3 grid
105
+ bounds = mesh_tri.bounds
106
+ dims = bounds[1] - bounds[0]
107
+ max_dim = max(dims)
108
+ pitch = max_dim / 88.0
109
 
110
+ # Create voxel grid
111
+ voxel_grid = mesh_tri.voxelized(pitch=pitch)
 
 
 
 
 
 
 
 
 
112
 
113
+ # Ensure it's exactly 88x88x88 by padding/cropping
114
+ vox_matrix = voxel_grid.matrix
115
+ current_shape = vox_matrix.shape
 
 
 
 
 
116
 
117
+ # Pad to 88x88x88 if smaller
118
+ if any(s < 88 for s in current_shape):
119
+ padded = np.zeros((88, 88, 88), dtype=bool)
120
+ padded[:current_shape[0], :current_shape[1], :current_shape[2]] = vox_matrix
121
+ vox_matrix = padded
122
+ # Crop to 88x88x88 if larger
123
+ elif any(s > 88 for s in current_shape):
124
+ vox_matrix = vox_matrix[:88, :88, :88]
125
 
126
+ # Create binvox-compatible object with ALL required attributes
127
+ class Voxels:
128
+ def __init__(self, data, dims, translate, scale, axis_order):
129
  self.data = data
130
  self.dims = dims
131
  self.translate = translate
132
  self.scale = scale
133
+ self.axis_order = axis_order # Required by binvox_rw
134
 
135
+ vox_obj = Voxels(
136
+ data=vox_matrix,
137
  dims=[88, 88, 88],
138
  translate=[0.0, 0.0, 0.0],
139
+ scale=1.0,
140
+ axis_order='xyz' # Standard axis order
141
  )
142
 
143
  # Save as binvox format for caching
144
  with open(binvox_file, 'wb') as f:
145
  binvox_rw.write(vox_obj, f)
146
 
147
+ print(f" ✓ Voxelization complete: {binvox_file}")
148
 
149
  except Exception as e:
150
  print(f" ERROR: Trimesh voxelization failed: {e}")
 
156
 
157
  data = Data(x=v[:, 3:6], pos=v[:, 0:3], tpl_edge_index=tpl_e, geo_edge_index=geo_e, batch=batch)
158
  return data, vox, surface_geodesic, translation_normalize, scale_normalize
159
+
160
  # def create_single_data(mesh_filename):
161
  # """
162
  # create input data for the network. The data is wrapped by Data structure in pytorch-geometric library