More modern C++ pipeline setup

As a parallel to @berk.geveci’s post on more pythonic wrappers (and particularly @jaswantp’s example, I’d like input on how we might use modern C++ and the nlohmann::json to achieve something similar in native C++.

Here’s @jaswantp’s example ported to an imaginary C++ example:

auto pipeline =
  vtk::Pipeline<vtkElevationFilter>()
    .connect<vtkStrinkFilter>({{"ShrinkFactor", 0.3}})
    .connect<vtkGeometryFilter>()
    .connect<vtkPolyDataConnectivityFilter>({
      {"ColorRegions", true},
      {"ExtractionMode", VTK_EXTRACT_ALL_REGIONS}})
    .connect<vtkPolyDataNormals>()
    ;

auto cone =
  vtk::Pipeline<vtkConeSource>({
    {"Radius", 5}, {"Resolution", 8}, {"Height", 2}
  }).execute();
vtk::Show(pipeline.execute(cone));

auto cylinder =
  vtk::Pipeline<vtkCylinderSource>({
    {"Radius", 6}, {"Resolution", 9}, {"Height", 3}
  }).execute();
vtk::Show(pipeline.execute(cylinder));

Unlike python, the vtk::Pipeline<Filter>(filterParams) template could return a new object with additional templated methods such as “connect<Filter>(filterParams)”, “execute(vtkDataObject* data)”, “tee(pipeline1, pipeline2)”, etc.

The filterParams argument would be an initializer-list used to construct nlohmann::json and some wrapper-like code could turn the JSON data into calls on the constructed filter.

1 Like

Looks amazing! +1 for taking that python example and showing what is possible from C++.

This is new in VTK C++. Although, we’ve seen this in pyvista and other pythonic flavors of VTK, where it’s convenient to call one function from the interpreter instead of creating all the boilerplate instances. I’m interested in what others think about it for C++ API.

This is vaguely similar with the approach being explored for trame+vtk-wasm instead of vtk.js for client-side rendering.

While this is a considerable amount of effort, let it not be a showstopper since @Sebastien_Jourdain and I are already developing a way to (de)serialize a VTK C++ object into json (yep, using vtknlohman::json). It can be reused to construct a VTK C++ object after you have the constructor properties as a json object!

1 Like