본문 바로가기
기타/3D 메쉬 데이터

[Mesh]vtk 라이브러리로 polydata 만들기

by 혜 림 2022. 1. 12.

 

그러니까 mesh 만들기!

 

일단 vtk 에서는 적절한 polydata를 만들어주면 그거를 rendering 해준다

렌더링에 대한 코딩은 접어두고 적절한 polydata를 어떻게 만들 것인지에 먼저 집중해보자 

 

#1. mesh의 기본이 되는 삼각형(polygon)

혹은 사각형

 

2차원에서 간단한 삼각형을 만들어보자

이 삼각형이 쌓여서 우리는 mesh data를 만들게 된다. 

 

쉽게 생각하자면.

vtkPoints()를 통해서 포인트의 위치 정보를 저장

vtkTriangle()을 통해서 포인트의 순서 정보를 저장(하나의 cell)

vtkCellArray()을 통해서 cell을 뭉텅이화

 	
    points = vtk.vtkPoints()
    points.InsertNextPoint(0.0, 0.0, 0.0)
    points.InsertNextPoint(0.0, 1.0, 0.0)
    points.InsertNextPoint(1.0, 0.0 ,0.0)

    triangle = vtk.vtkTriangle()
    triangle.GetPointIds().SetId(0,0)
    triangle.GetPointIds().SetId(1,1)
    triangle.GetPointIds().SetId(2,2)

    triangles = vtk.vtkCellArray()
    triangles.InsertNextCell(triangle)


    #vtkPolyData -> 3차원 오브젝트 정보를 가지고있는 객체
    polydata = vtk.vtkPolyData()
    polydata.SetPoints(points)
    polydata.SetPolys(triangles)

 

 

#2. 삼각형 polygon으로 만든 cube mesh

 

크게 두가지 방법이 있다.

1번 방법 2번 방법
vtkPoint
vtkCellArray
vtkPoint
vtkTriangle(혹은 vtkPolygon)
vtkCellArray

 

1번 방법이 빠르다. 그리고 메모리도 적게 차지할 것 같다.

속도는 직접 확인해봤고, 메모리는 직접 확인해보지는 않았다. 

 

어떤 차이가 있느냐? 

약간 리스트 [[1,2],[3,4]] 를 만든다고 칠 때

리스트 안에 [1,2]를 바로바로 append 할 것인가

혹은 [1,2] 와 [3,4]의 리시트를 따로 만든 후에 합칠 것인가 의 차이 

 

삼각형을 여러개 만들어서 그걸 뭉텅이로 만들어라 이게 2번 방법

 

1번 방법이 빠르니까 1번 코드만 보자 

 

    points = vtk.vtkPoints()
    points.InsertNextPoint(0.0, 0.0, 0.0)
    points.InsertNextPoint(0.0, 1.0, 0.0)
    points.InsertNextPoint(1.0, 1.0 ,0.0)
    points.InsertNextPoint(1.0, 0.0 ,0.0)
    points.InsertNextPoint(0.0, 0.0, 1.0)
    points.InsertNextPoint(0.0, 1.0, 1.0)
    points.InsertNextPoint(1.0, 1.0 ,1.0)
    points.InsertNextPoint(1.0, 0.0 ,1.0)
    points.InsertNextPoint(1.0, 0.0 ,1.0)

    triangles = vtk.vtkCellArray()
    triangles.InsertNextCell(3, (0,1,2))
    triangles.InsertNextCell(3, (0,2,3))
    triangles.InsertNextCell(3, (0,3,4))
    triangles.InsertNextCell(3, (3,4,7))
    triangles.InsertNextCell(3, (2,3,7))
    triangles.InsertNextCell(3, (2,6,7))
    triangles.InsertNextCell(3, (1,2,5))
    triangles.InsertNextCell(3, (2,5,6))
    triangles.InsertNextCell(3, (0,1,4))
    triangles.InsertNextCell(3, (1,4,5))
    triangles.InsertNextCell(3, (4,5,7))
    triangles.InsertNextCell(3, (5,6,7))


    #vtkPolyData -> 3차원 오브젝트 정보를 가지고있는 객체
    polydata = vtk.vtkPolyData()
    polydata.SetPoints(points)
    polydata.SetPolys(triangles)

 

만약 내가 만든 삼각형 하나하나 보고 싶다면 polydata.SetLines(triangles) 하면 된다. 한 면에는 삼각형이 빠져보이는데, 이는 line이라서 그렇다. 네 개의 점만 있으면 바로 면은 만들 수 있지만, 모서리를 모두 만들고 싶다면 네 개의 점과 마지막 점과 첫 번째 점을 이어줘서 닫힌 형태의 line을 만들기 위해 하나의 점이 더 필요하기 때문. 이건 뇌피셜 

 

#3. scratch부터 읽는 STL 파일 

 

이건 STL 파일에 대해서 먼저 쓴 다음에 포스팅하겠다...

댓글