Factory_Layout_Simulator / utils /pointcloud_tools.py
shin0624's picture
Update utils/pointcloud_tools.py
bfe81cc verified
Raw History Blame Contribute Delete
1.72 kB
# 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)