Advertisement
Guest User

Odd normals pt2

a guest
Mar 5th, 2015
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. import vtk
  2. raw_points = [
  3.     (0.0, 0.0, 0.0),  # 0
  4.     (0.0, 0.0, 2.0),  # 1
  5.     (0.0, 2.0, 0.0),  # 2
  6.     (0.0, 2.0, 2.0),  # 3
  7.  
  8.     (2.0, 0.0, 0.0),  # 4
  9.     (2.0, 0.0, 2.0),  # 5
  10.     (2.0, 2.0, 0.0),  # 6
  11.     (2.0, 2.0, 2.0),  # 7
  12. ]
  13.  
  14. raw_polyhedron = [
  15.     [0, 1, 3, 2],
  16.     [4, 5, 7, 6],
  17.     [0, 1, 5, 4],
  18.     [2, 3, 7, 6],
  19.     [0, 2, 6, 4],
  20.     [1, 3, 7, 5],
  21. ]
  22.  
  23. # Add points
  24. points = vtk.vtkPoints()
  25. points.SetNumberOfPoints(len(raw_points))
  26. for index, point in enumerate(raw_points):
  27.     points.SetPoint(index, point)
  28.  
  29.  
  30. # Add Polygon
  31. polygon_faces = vtk.vtkCellArray()
  32. for face in raw_polyhedron:
  33.     q = vtk.vtkPolygon()
  34.     q.GetPointIds().SetNumberOfIds(len(face))
  35.     for index, edge in enumerate(face):
  36.         q.GetPointIds().SetId(index, edge)
  37.     polygon_faces.InsertNextCell(q)
  38.  
  39. face_stream = vtk.vtkIdList()
  40. face_stream.InsertNextId(polygon_faces.GetNumberOfCells())
  41. vertex_list = vtk.vtkIdList()
  42.  
  43. polygon_faces.InitTraversal()
  44. while polygon_faces.GetNextCell(vertex_list) == 1:
  45.     face_stream.InsertNextId(vertex_list.GetNumberOfIds())
  46.     for j in range(vertex_list.GetNumberOfIds()):
  47.         face_stream.InsertNextId(vertex_list.GetId(j))
  48.  
  49. # Prepare unstructured grid
  50. unstructured_grid = vtk.vtkUnstructuredGrid()
  51. unstructured_grid.SetPoints(points)
  52. unstructured_grid.InsertNextCell(vtk.VTK_POLYHEDRON, face_stream)
  53.  
  54. # Get cube normals
  55. cell = unstructured_grid.GetCell(0)
  56. normals = vtk.vtkPolyDataNormals()
  57. normals.SetInputData(cell.GetPolyData())
  58. normals.ComputePointNormalsOff()
  59. normals.ComputeCellNormalsOn()
  60. normals.Update()
  61. print
  62. for i in xrange(cell.GetNumberOfFaces()):
  63.     print i, "=>", normals.GetOutput().GetCellData().GetNormals().GetTuple(i)
  64. # 0 => (-1.0, 0.0, 0.0)
  65. # 1 => (1.0, 0.0, 0.0)
  66. # 2 => (0.0, -1.0, 0.0)
  67. # 3 => (0.0, 1.0, 0.0)
  68. # 4 => (0.0, 0.0, -1.0)
  69. # 5 => (0.5773502588272095, -0.5773502588272095, 0.5773502588272095) -> Odd Result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement