vtkGeometryFilter problem

I don’t understand the behavior of vtkGeometryFilter in the case of a hexahedron - I would expect it to extract 6 faces and I get zero:

import vtk

reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName('a.vtu')
# a.vtu has:
# pts= [[1., 0., 0.], #0
#       [0., 0., 0.], #1
#       [0., 1., 0.], #2
#       [1., 1., 0.], #3
#       [0., 0., 1.], #4
#       [1., 0., 1.], #5
#       [0., 1., 1.], #6
#       [1., 1., 1.], #7
#      ]
# connectivity = [[1, 4, 6, 2, 0, 5, 7, 3]]
reader.Update()
ug = reader.GetOutput()
print('ug ', ug.GetNumberOfPoints(), ug.GetNumberOfCells())

gf = vtk.vtkGeometryFilter()
gf.SetInputData(ug)
gf.Update()
out = gf.GetOutput()
print('out', out.GetNumberOfPoints(), out.GetNumberOfCells())

yields:

ug  8 1
out 8 0

a.zip (449 Bytes)
the file visualizes correctly in paraview:
Screenshot from 2021-01-20 14-11-18

Probably the order of points in the cell is incorrect (e.g., the cell is turned inside out or invalid in some other way). This works well:

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

hex = vtk.vtkHexahedron()
for i in range(numberOfVertices):
    hex.GetPointIds().SetId(i, i)

ug = vtk.vtkUnstructuredGrid()
ug.SetPoints(points);
ug.InsertNextCell(hex.GetCellType(), hex.GetPointIds())

print('ug ', ug.GetNumberOfPoints(), ug.GetNumberOfCells())

gf = vtk.vtkGeometryFilter()
gf.SetInputData(ug)
gf.Update()
out = gf.GetOutput()
print('out', out.GetNumberOfPoints(), out.GetNumberOfCells())

output:

ug  8 1
out 8 6

Thanks Andras,
you’re right (as usual :)) in saying that the order was not correct… in my case order is:

while in yours script:

…and the vtkGeometryFIlter operates just fine.

It’s funny though that paraview can handle the incorrect one too (?), also other filters like vtkDataSetTriangleFilter seem not to care…

vtkDataSetTriangleFilter simply inserts faces into a hash table and counts uses of the faces. This is strictly not speaking consistent with the topology of the cell/dataset hence the different results.

Also, vtkDataSetTriangleFilter is now slower than vtkGeometryFilter (especially when building VTK threaded) and uses much more memory. At some point PV will switch to using vtkGeometryFilter - this switch is harder than it might seem because vtkDataSetTriangleFilter produces different/incorrect output in some situations. However, vtkDataSetSurfaceFilter handles certain types of higher order cells (e.g., Bezier) while vtkGeometryFilter currently does not.

1 Like

Hello,

Here’s a C++ comment if you wish to keep the order documented:

/**
 * The ids of the vertexes forming the geometry of a cell.
 *
 * Vertex id elements in the vId[8] array forming the geometry of a cell:
 *
 *                        J
 *                        |
 *                        |
 *
 *                        3-----------2   face 0 = 0 1 2 3
 *                       /|          /|   face 1 = 4 7 6 5
 *                     /  |        /  |   face 2 = 0 3 7 4
 *                   /    |      /    |   face 3 = 1 5 6 2
 *                  7--------- 6      |   face 4 = 0 4 5 1
 *                  |     |    |      |   face 5 = 3 2 6 7
 *                  |     0----|------1    --- I
 *                  |    /     |     /
 *                  |  /       |   /
 *                  |/         | /
 *                  4--------- 5
 *
 *               /
 *             /
 *           K
 *
 *
 *
 */

cheers,

Paulo

1 Like

Here is an example that uses vtkUnstructuredGrid for eight different cell types, once again the point order is critical: Cell3DDemonstration

2 Likes

Thanks! Maybe paraview picks first the connectivity and then uses it to insert the points (?) so that it still manages to define cell the orientation…

It is always better to specify the points in the correct order, as Will pointed out:

That example also provides the correct orientation for a variety of shapes which may also be useful.

Andrew, this is a good resource for VTK file formats:
https://lorensen.github.io/VTKExamples/site/VTKFileFormats/

Remind us again, is this now incorporated into the VTK Examples work that you just completed?

Paraview is a end-user software, so it likely does a preprocessing to ensure “clockwiseness” of faces, etc.

1 Like

Sure is, see: https://kitware.github.io/vtk-examples/site/VTKFileFormats/ everything from that site is in the new site.

I’m wondering if we should place some links from the documentation in the C++ header files pointing to the examples (including examples-related resources like VTK file formats). I’m a little leery about adding this kind of dependency, but with VTK examples now in great shape, maybe it’s time to think about doing so. Comments?

1 Like

In the VTK Examples I have a script which runs for each language producing a list of vtk classes and the examples, see: Cxx classes with examples and Python classes with examples for example.

This is the script: VTKClassesUsedInExamples.py, it currently produces markdown files and is run automatically when the VTK examples site is updated.

My initial thought would be to rewrite this (using pandas) to produce a JSON file that would live in https://github.com/Kitware/vtk-examples/tree/gh-pages/src that the VTK documentation process could read.

What do you think @ben.boeckel @sankhesh? Would it be easy to parse this JSON file to produce a list of relevant VTK Examples for each VTK class? It would be a largish file as it would contain the links to the vtk class and the vtk examples classes. Of course some cells will be empty.

1 Like

@amaclean The cross-reference between the doxygen documentation and vtk-examples would be a great addition. Currently, the perl script doc_class2example.pl does the cross-referencing between in-source examples and classes in doxygen documentation. We could modify it to include parsing of this new JSON file as well. I am not adept at perl and if required, we can make it a python script.

+1 python script

@will.schroeder, @sankhesh, @ben.boeckel if we can get rid of Perl great.

Ok here is a json file vtk_vtk-examples_xref.zip (118.0 KB) containing the VTK class and corresponding example by language. Just unzip it and you can play around with it using xref.py (1021 Bytes). If you think it is acceptable and easy to use from the VTK end, I’ll commit it.

Data layout:

  • Columns are the VTK classes.
  • Indexes are the languages: CSharp, Cxx, Java, Python; for these rows, each cell contains the comma separated list of relevant examples or None.
    The index VTKLink contains the relevant link to the VTK class description.

In each cell the links are in the usual markdown format:

Been thinking about this, a better json file is this vtk_vtk-examples_xref.zip (225.2 KB) the data layout is the same as above however the markdown has been replaced by (key, value) pairs and all classes used in the examples are included now so the file is a bit larger. The markdown format is easily created, namely [key](value).

This layout makes it easier to get statistics from the file like this:

Number of classes with examples 868
Number of classes with examples by language:
CSharp   188
Cxx      860
Java     264
Python   403
Largest number of examples by language and class
CSharp                        Cxx                           Java                          Python                       
vtkRenderWindow           102 vtkNew                    985 vtkRenderWindowInteractor 183 vtkNamedColors            316
vtkRenderer               101 vtkRenderWindow           854 vtkRenderer               177 vtkRenderer               303
vtkActor                   83 vtkRenderer               850 vtkRenderWindow           177 vtkRenderWindowInteractor 302
vtkPolyDataMapper          72 vtkNamedColors            843 vtkNamedColors            164 vtkRenderWindow           295
vtkPolyData                35 vtkRenderWindowInteractor 842 vtkActor                  157 vtkActor                  280
vtkSphereSource            31 vtkActor                  682 vtkPolyDataMapper         140 vtkPolyDataMapper         239
vtkPoints                  29 vtkProperty               643 vtkSphereSource            76 vtkSphereSource            83
vtkCellArray               18 vtkPolyDataMapper         580 vtkPoints                  35 vtkPoints                  76
vtkDataSetMapper           14 vtkPolyData               471 vtkPolyData                33 vtkPolyData                62
vtkImageViewer2            11 vtkCamera                 408 vtkDataSetMapper           18 vtkDataSetMapper           59

1 Like

@amaclean The json file looks good. I’ll take a first stab at doxygen integration next week.

@sankesh The JSON file is now on the web: vtk_vtk-examples_xref.json. This will be automatically updated whenever the vtk-examples web pages are updated.

This program xref.py (4.3 KB), when run with no parameters will automatically download the file from the web. It will produce the statistics as above. I hope you will find it useful both as a check of the file and it also how to download the url. It is a bit kludgy but it works!

When thinking about the examples and given that some classes (e.g. VTKNew) have over 900 examples, it is essential to limit the examples to say just five or less for each language. It might be nice to randomly select five examples when there are more than five for a given class. Thus the examples will change whenever the documentation is updated. What do you and others think?

@sankesh Here is an update to xref.py (5.9 KB) , I have added a function to randomly select five examples for a given VTK class and language see:

def ramdom_sample(d, vtk_class, lang, num=5):
    """
    For the given keys provide a random sample of examples.

    :param d: The dictionary.
    :param vtk_class: The VTK Class e.g. vtkActor.
    :param lang: The language, e.g. Cxx.
    :param num: The number of values.
    :return: A markdown formatted list of examples or None.
    """

Actually this JSON file is really useful (to me at least), so I will put a Python script into vtk_examples for users to query on VTK Class and language.