File size: 980 Bytes
4468629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import trimesh
from scipy.spatial import Delaunay

class ModelGenerator:
    def __init__(self):
        pass

    def depth_to_points(self, depth_map, scale=1.0):
        """Convert depth map to 3D points"""
        height, width = depth_map.shape
        x, y = np.meshgrid(np.arange(width), np.arange(height))
        
        # Create points array
        points = np.stack([x.flatten(), y.flatten(), depth_map.flatten() * scale], axis=1)
        
        return points

    def generate_mesh(self, points):
        """Generate a 3D mesh from points"""
        # Project points to 2D for triangulation
        points_2d = points[:, :2]
        
        # Create triangulation
        tri = Delaunay(points_2d)
        
        # Create mesh
        mesh = trimesh.Trimesh(vertices=points, faces=tri.simplices)
        
        return mesh

    def save_mesh(self, mesh, filename):
        """Save the mesh to a file"""
        mesh.export(filename)