[vtkusers] Import Mesh Mystery (UnstructuredGrid) -Help!

Helvin Lui helvinlui at gmail.com
Sun Aug 23 10:18:58 EDT 2009


Hi everyone! :-)
I have written a function that imports a mesh from a .txt file.

def displayMesh(file_path):
    import vtk
    from vtk import vtkTriangle
    VTK_TRIANGLE = vtkTriangle().GetCellType()

    with open(file_path, 'r') as f:

        aMeshGrid = vtk.vtkUnstructuredGrid()
        aMeshGrid.Allocate(2, 180)

        # Get number of mesh points
        no_points = int(f.readline())

        # Set number of points
        meshPoints = vtk.vtkPoints()
        meshPoints.SetNumberOfPoints(no_points)

        # Iterate through point data
        for i in range(no_points):
            # Get coord info for each point
            point_info = f.readline().split() # I need to split, before I
assign to point_coord
                                              # else the whole thing is
split into single numbers
            #print point_info # Check reading
            point_ID = (int(point_info[0])-1) # -1 because the IDs need to
start with 0.
            point_x = float(point_info[1])
            point_y = float(point_info[2])
            point_z = float(point_info[3])
            # Set coord info in mesh
            meshPoints.InsertPoint(point_ID, point_x, point_y, point_z)

        # Get number of elements
        no_elements = int(f.readline())

        # Set number of elements
        for i in range(no_elements):
            element_info = f.readline().split()
            element_ID = (int(element_info[0])-1)
            element_no_nodes = int(element_info[1])

            element_ID_list = vtk.vtkIdList()
            for j in range(element_no_nodes):
                node_no = int(element_info[j+2])
                element_ID_list.InsertNextId(node_no)
                print j, node_no
            if element_no_nodes == 3: # a triangle
                cell_type = VTK_TRIANGLE

            aMeshGrid.InsertNextCell(cell_type, element_ID_list)

    aMeshGrid.SetPoints(meshPoints)

    aMeshMapper = vtk.vtkDataSetMapper()
    aMeshMapper.SetInput(aMeshGrid)

    aMeshActor = vtk.vtkActor()
    aMeshActor.SetMapper(aMeshMapper)
    aMeshActor.GetProperty().SetDiffuseColor(1, 0, 0)

    # Create the usual rendering stuff.
    ren = vtk.vtkRenderer()
    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren)
    renWin.SetSize(300, 300)
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    ren.AddActor(aMeshActor)

    # Zoom, to see actor. (in case it was too small to see)
    ren.ResetCamera() #
http://www.vtk.org/doc/nightly/html/classvtkRenderer.html#b14d1aeb74a4990f2da819e09d028d65
    cam1 = ren.GetActiveCamera()

    ## Render the scene and start interaction.
    iren.Initialize()
    renWin.Render()
    iren.Start()


My goal is to display this particular file:

    44
     1      0.000000000      0.000000000      0.000000000
     2      0.100000001      0.000000000      0.000000000
     3      0.200000003      0.000000000      0.000000000
     4      0.300000012      0.000000000      0.000000000
     5      0.400000006      0.000000000      0.000000000
     6      0.500000000      0.000000000      0.000000000
     7      0.600000024      0.000000000      0.000000000
     8      0.699999988      0.000000000      0.000000000
     9      0.800000012      0.000000000      0.000000000
    10      0.900000036      0.000000000      0.000000000
    11      1.000000000      0.000000000      0.000000000
    12      0.000000000      0.066666670      0.000000000
    13      0.100000001      0.066666670      0.000000000
    14      0.200000003      0.066666670      0.000000000
    15      0.300000012      0.066666670      0.000000000
    16      0.400000006      0.066666670      0.000000000
    17      0.500000000      0.066666670      0.000000000
    18      0.600000024      0.066666670      0.000000000
    19      0.699999988      0.066666670      0.000000000
    20      0.800000012      0.066666670      0.000000000
    21      0.900000036      0.066666670      0.000000000
    22      1.000000000      0.066666670      0.000000000
    23      0.000000000      0.133333340      0.000000000
    24      0.100000001      0.133333340      0.000000000
    25      0.200000003      0.133333340      0.000000000
    26      0.300000012      0.133333340      0.000000000
    27      0.400000006      0.133333340      0.000000000
    28      0.500000000      0.133333340      0.000000000
    29      0.600000024      0.133333340      0.000000000
    30      0.699999988      0.133333340      0.000000000
    31      0.800000012      0.133333340      0.000000000
    32      0.900000036      0.133333340      0.000000000
    33      1.000000000      0.133333340      0.000000000
    34      0.000000000      0.200000018      0.000000000
    35      0.100000001      0.200000018      0.000000000
    36      0.200000003      0.200000018      0.000000000
    37      0.300000012      0.200000018      0.000000000
    38      0.400000006      0.200000018      0.000000000
    39      0.500000000      0.200000018      0.000000000
    40      0.600000024      0.200000018      0.000000000
    41      0.699999988      0.200000018      0.000000000
    42      0.800000012      0.200000018      0.000000000
    43      0.900000036      0.200000018      0.000000000
    44      1.000000000      0.200000018      0.000000000
    60
     1      3      1      2     13      0      1         0.00500
     2      3      2      3     14      0      1         0.00500
     3      3      3      4     15      0      1         0.00500
     4      3      4      5     16      0      1         0.00500
     5      3      5      6     17      0      1         0.00500
     6      3      6      7     18      0      1         0.00500
     7      3      7      8     19      0      1         0.00500
     8      3      8      9     20      0      1         0.00500
     9      3      9     10     21      0      1         0.00500
    10      3     10     11     22      0      1         0.00500
    11      3      1     13     12      0      1         0.00500
    12      3      2     14     13      0      1         0.00500
    13      3      3     15     14      0      1         0.00500
    14      3      4     16     15      0      1         0.00500
    15      3      5     17     16      0      1         0.00500
    16      3      6     18     17      0      1         0.00500
    17      3      7     19     18      0      1         0.00500
    18      3      8     20     19      0      1         0.00500
    19      3      9     21     20      0      1         0.00500
    20      3     10     22     21      0      1         0.00500
    21      3     12     13     24      0      1         0.00500
    22      3     13     14     25      0      1         0.00500
    23      3     14     15     26      0      1         0.00500
    24      3     15     16     27      0      1         0.00500
    25      3     16     17     28      0      1         0.00500
    26      3     17     18     29      0      1         0.00500
    27      3     18     19     30      0      1         0.00500
    28      3     19     20     31      0      1         0.00500
    29      3     20     21     32      0      1         0.00500
    30      3     21     22     33      0      1         0.00500
    31      3     12     24     23      0      1         0.00500
    32      3     13     25     24      0      1         0.00500
    33      3     14     26     25      0      1         0.00500
    34      3     15     27     26      0      1         0.00500
    35      3     16     28     27      0      1         0.00500
    36      3     17     29     28      0      1         0.00500
    37      3     18     30     29      0      1         0.00500
    38      3     19     31     30      0      1         0.00500
    39      3     20     32     31      0      1         0.00500
    40      3     21     33     32      0      1         0.00500
    41      3     23     24     35      0      1         0.00500
    42      3     24     25     36      0      1         0.00500
    43      3     25     26     37      0      1         0.00500
    44      3     26     27     38      0      1         0.00500
    45      3     27     28     39      0      1         0.00500
    46      3     28     29     40      0      1         0.00500
    47      3     29     30     41      0      1         0.00500
    48      3     30     31     42      0      1         0.00500
    49      3     31     32     43      0      1         0.00500
    50      3     32     33     44      0      1         0.00500
    51      3     23     35     34      0      1         0.00500
    52      3     24     36     35      0      1         0.00500
    53      3     25     37     36      0      1         0.00500
    54      3     26     38     37      0      1         0.00500
    55      3     27     39     38      0      1         0.00500
    56      3     28     40     39      0      1         0.00500
    57      3     29     41     40      0      1         0.00500
    58      3     30     42     41      0      1         0.00500
    59      3     31     43     42      0      1         0.00500
    60      3     32     44     43      0      1         0.00500

This however, runs into no errors in command line, and the render window
appears, but the actor is not displayed. A window pops up saying
"vtkpython.exe has stopped working, windows is checking for a solution to
the problem."

To better understand why this is happening, I wrote a simpler mesh file:

    7
    1       1.000000000       0.000000000       0.000000000
    2       3.000000000       0.000000000       0.000000000
    3       0.000000000       2.000000000       0.000000000
    4       2.000000000       2.000000000       0.000000000
    5       4.000000000       2.000000000       0.000000000
    6       1.000000000       4.000000000       0.000000000
    7       3.000000000       4.000000000       0.000000000
    6
    1       3       1       4       3      0      1         0.00500
    2       3       1       2       4      0      1         0.00500
    3       3       2       5       4      0      1         0.00500
    4       3       3       4       6      0      1         0.00500
    5       3       4       7       6      0      1         0.00500
    6       3       4       5       7      0      1         0.00500

When I run the code to display this mesh, it worked.
Mystery. I wonder why? Do you know?

Help much appreciated!  : )

-- 
Helvin

"Though the world may promise me more, I'm just made to be filled with the
Lord."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090824/7b345bdb/attachment.htm>


More information about the vtkusers mailing list