다각형 Polygon 생성 방법 - SmartX-Team/Omniverse GitHub Wiki

해당 예제에서는 Omniverse 상에서 복잡한 모양의 다각형 Polygon을 생성하는 예제이다.

두개 이상의 Object가 겹치는 영역을 새로운 intersection Mesh 로 만드는 예제

import omni
from pxr import Usd, UsdGeom, Gf, Sdf
import math

# Omniverse 상에 shapely 설치 및 임포트
try:
    from shapely.geometry import Polygon
except ImportError:
    import subprocess
    import sys
    subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'shapely'])
    from shapely.geometry import Polygon

# 새로운 스테이지 생성
stage = omni.usd.get_context().get_stage()

# 원의 중심과 반지름 설정
center1 = Gf.Vec3f(-5, 0, 0)
center2 = Gf.Vec3f(5, 0, 0)
radius = 10
num_points = 100  # 원을 구성할 점의 수

def create_circle(center, radius, num_points, stage, name):
    points = []
    for i in range(num_points):
        angle = 2 * math.pi * i / num_points
        x = center[0] + radius * math.cos(angle)
        z = center[2] + radius * math.sin(angle)
        points.append((x, center[1], z))
    
    # UsdGeom.Mesh 생성
    polygon = UsdGeom.Mesh.Define(stage, f"/World/{name}")
    points_gf = [Gf.Vec3f(x, y, z) for x, y, z in points]
    polygon.CreatePointsAttr(points_gf)
    polygon.CreateFaceVertexCountsAttr([num_points])
    polygon.CreateFaceVertexIndicesAttr(list(range(num_points)))
    
    return points

# 원 생성
points1 = create_circle(center1, radius, num_points, stage, "Circle1")
points2 = create_circle(center2, radius, num_points, stage, "Circle2")

# shapely 폴리곤으로 변환
polygon1 = Polygon([(x, z) for x, y, z in points1])
polygon2 = Polygon([(x, z) for x, y, z in points2])

# 교차 영역 계산
intersection = polygon1.intersection(polygon2)
intersection_coords = list(intersection.exterior.coords)

# 교차 영역 Mesh 생성
def create_polygon_mesh(stage, name, vertices):
    path = Sdf.Path(f"/World/{name}")
    mesh = UsdGeom.Mesh.Define(stage, path)
    
    # Vertices 생성
    points_gf = [Gf.Vec3f(x, 0, y) for x, y in vertices]
    mesh.CreatePointsAttr(points_gf)
    
    # Face vertex indices 및 counts 생성
    num_vertices = len(vertices)
    face_vertex_counts = [num_vertices]
    face_vertex_indices = list(range(num_vertices))
    
    mesh.CreateFaceVertexCountsAttr(face_vertex_counts)
    mesh.CreateFaceVertexIndicesAttr(face_vertex_indices)

create_polygon_mesh(stage, "IntersectionPolygon", intersection_coords)

실행결과

두개의 원 Mesh 를 생성하고 겹치는 영역은 intersection Mesh 로 생성됨을 확인할 수 있다.

image