File size: 1,723 Bytes
5c12344
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfe81cc
 
 
5c12344
 
 
 
 
 
bfe81cc
 
 
 
 
 
 
 
 
 
5c12344
bfe81cc
5c12344
 
 
bfe81cc
5c12344
 
 
 
 
 
 
 
 
 
 
 
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
# utils/pointcloud_tools.py
import numpy as np
import trimesh
from typing import List

class PointCloudProcessor:
    @staticmethod
    def normalize_points(points: np.ndarray) -> np.ndarray:
        """ํฌ์ธํŠธ ํด๋ผ์šฐ๋“œ ์ •๊ทœํ™”"""
        if len(points) == 0:
            return points
            
        # Z-score ์ •๊ทœํ™”
        mean = np.mean(points, axis=0)
        std = np.std(points, axis=0)
        # ํ‘œ์ค€ํŽธ์ฐจ๊ฐ€ 0์ธ ๊ฒฝ์šฐ ๋Œ€๋น„
        std = np.where(std < 1e-8, 1, std)
        return (points - mean) / std

    @staticmethod
    def filter_outliers(points: np.ndarray, z_thresh=3.0) -> np.ndarray:
        """์ด์ƒ์น˜ ์ œ๊ฑฐ"""
        if len(points) == 0:
            return points
        
        if len(points) <= 3:  # ํฌ์ธํŠธ๊ฐ€ ๋„ˆ๋ฌด ์ ์œผ๋ฉด ํ•„ํ„ฐ๋ง ์ƒ๋žต
            return points
            
        z_scores = np.abs(points - np.mean(points, axis=0)) / (np.std(points, axis=0) + 1e-8)
        mask = (z_scores < z_thresh).all(axis=1)
        
        # ๋ชจ๋“  ํฌ์ธํŠธ๊ฐ€ ํ•„ํ„ฐ๋ง๋˜์ง€ ์•Š๋„๋ก ๋ณดํ˜ธ
        if not np.any(mask):
            return points
            
        return points[mask]

    @staticmethod
    def create_mesh(points: np.ndarray) -> trimesh.Trimesh:
        """ํฌ์ธํŠธ ํด๋ผ์šฐ๋“œ์—์„œ ๋ฉ”์‰ฌ ์ƒ์„ฑ"""
        return trimesh.points.PointCloud(points)

    @staticmethod
    def merge_pointclouds(clouds: List[np.ndarray]) -> np.ndarray:
        """๋‹ค์ค‘ ํฌ์ธํŠธ ํด๋ผ์šฐ๋“œ ๋ณ‘ํ•ฉ"""
        return np.vstack(clouds) if clouds else np.array([])

    @staticmethod
    def save_as_obj(points: np.ndarray, filename: str):
        """OBJ ํŒŒ์ผ๋กœ ์ €์žฅ"""
        mesh = trimesh.points.PointCloud(points)
        mesh.export(filename)