Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import vtk
- raw_points = [
- (0.0, 0.0, 0.0), # 0
- (0.0, 0.0, 2.0), # 1
- (0.0, 2.0, 0.0), # 2
- (0.0, 2.0, 2.0), # 3
- (2.0, 0.0, 0.0), # 4
- (2.0, 0.0, 2.0), # 5
- (2.0, 2.0, 0.0), # 6
- (2.0, 2.0, 2.0), # 7
- ]
- raw_polyhedron = [
- [0, 1, 3, 2],
- [4, 5, 7, 6],
- [0, 1, 5, 4],
- [2, 3, 7, 6],
- [0, 2, 6, 4],
- [1, 3, 7, 5],
- ]
- # Add points
- points = vtk.vtkPoints()
- points.SetNumberOfPoints(len(raw_points))
- for index, point in enumerate(raw_points):
- points.SetPoint(index, point)
- # Add Polygon
- polygon_faces = vtk.vtkCellArray()
- for face in raw_polyhedron:
- q = vtk.vtkPolygon()
- q.GetPointIds().SetNumberOfIds(len(face))
- for index, edge in enumerate(face):
- q.GetPointIds().SetId(index, edge)
- polygon_faces.InsertNextCell(q)
- face_stream = vtk.vtkIdList()
- face_stream.InsertNextId(polygon_faces.GetNumberOfCells())
- vertex_list = vtk.vtkIdList()
- polygon_faces.InitTraversal()
- while polygon_faces.GetNextCell(vertex_list) == 1:
- face_stream.InsertNextId(vertex_list.GetNumberOfIds())
- for j in range(vertex_list.GetNumberOfIds()):
- face_stream.InsertNextId(vertex_list.GetId(j))
- unstructured_grid = vtk.vtkUnstructuredGrid()
- unstructured_grid.SetPoints(points)
- unstructured_grid.InsertNextCell(vtk.VTK_POLYHEDRON, face_stream)
- # Trying to view data internals
- # =================================================================================================
- cell = unstructured_grid.GetCell(0)
- for face_id in xrange(cell.GetNumberOfFaces()):
- face = cell.GetFace(face_id)
- print face.GetNumberOfPoints()
- print
- pd = cell.GetPolyData()
- print pd.GetNumberOfPolys()
- data = pd.GetPolys().GetData()
- print [data.GetTuple(i)[0] for i in xrange(data.GetSize())]
- # Odd results:
- # [
- # 4.0,
- # 0.0, 1.0, 3.0, 2.0,
- # 4.0,
- # 4.0, 5.0, 7.0, 6.0,
- # 4.0,
- # 0.0, 1.0, 5.0, 4.0,
- # 4.0,
- # 2.0, 3.0, 7.0, 6.0,
- # 4.0,
- # 0.0, 2.0, 6.0, 4.0,
- # 4.0,
- # 1.0, 3.0, 7.0 <-- Shouldn't have 4 values here?
- # ]
- # =================================================================================================
- # Get cube normals
- normals = vtk.vtkPolyDataNormals()
- normals.SetInputData(cell.GetPolyData())
- normals.ComputePointNormalsOff()
- normals.ComputeCellNormalsOn()
- normals.Update()
- print
- for i in xrange(cell.GetNumberOfFaces()):
- print i, "=>", normals.GetOutput().GetCellData().GetNormals().GetTuple(i)
- # 0 => (-1.0, 0.0, 0.0)
- # 1 => (1.0, 0.0, 0.0)
- # 2 => (0.0, -1.0, 0.0)
- # 3 => (0.0, 1.0, 0.0)
- # 4 => (0.0, 0.0, -1.0)
- # 5 => (0.5773502588272095, -0.5773502588272095, 0.5773502588272095) -> Odd Result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement