forked from sohampahari/SemGeoAttnNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_geometry.py
More file actions
56 lines (47 loc) · 1.96 KB
/
Copy pathpreprocess_geometry.py
File metadata and controls
56 lines (47 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import torch
import trimesh
import numpy as np
from tqdm import tqdm
def preprocess_geometry(
mesh_dir="meshes/",
output_dir="geometry_data/"
):
"""
Reads .obj files and saves (Positions + Normals) as .pt tensors.
Output Shape: (N, 6) -> [x, y, z, nx, ny, nz]
"""
os.makedirs(output_dir, exist_ok=True)
mesh_files = [f for f in os.listdir(mesh_dir) if f.endswith('.obj')]
print(f"Pre-processing geometry for {len(mesh_files)} meshes...")
for mesh_file in tqdm(mesh_files):
name = mesh_file.replace('.obj', '')
# Load mesh (force distinct vertices to match other data)
try:
# process=False is CRITICAL to keep vertex order identical
# to your 'semantics' and 'smooth_gaze' data
mesh = trimesh.load(os.path.join(mesh_dir, mesh_file), process=False)
except Exception as e:
print(f"Failed to load {mesh_file}: {e}")
continue
# 1. Get Vertices (N, 3)
verts = torch.tensor(mesh.vertices, dtype=torch.float32)
# 2. Get/Compute Normals (N, 3)
# Some .obj files have vertex normals, some don't.
try:
# Try to read existing vertex normals
if hasattr(mesh, 'vertex_normals') and mesh.vertex_normals.shape == verts.shape:
normals = torch.tensor(mesh.vertex_normals, dtype=torch.float32)
else:
# Fallback: Compute them (might be slightly different from original)
mesh.fix_normals()
normals = torch.tensor(mesh.vertex_normals, dtype=torch.float32)
except:
# Final fallback: Zeros (rare)
normals = torch.zeros_like(verts)
# 3. Concatenate (N, 6)
geo_tensor = torch.cat([verts, normals], dim=1)
# 4. Save
torch.save(geo_tensor, os.path.join(output_dir, f"{name}.pt"))
if __name__ == "__main__":
preprocess_geometry()