# More modern C++ pipeline setup

**URL:** <https://discourse.vtk.org/t/more-modern-c-pipeline-setup/13244>\
**Category:** Development\
**Created:** [February 2, 2024, 10:36pm UTC](https://discourse.vtk.org/t/more-modern-c-pipeline-setup/13244 "2024-02-02T22:36:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcthomp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dcthomp/32/60_2.png) [@dcthomp](https://discourse.vtk.org/u/dcthomp)\
**Post date:** [February 2, 2024, 10:36pm UTC](https://discourse.vtk.org/t/more-modern-c-pipeline-setup/13244/1 "2024-02-02T22:36:20Z")

</div>

As a parallel to @berk.geveci’s post on [more pythonic wrappers](https://discourse.vtk.org/t/more-pythonic-vtk-wrapping/13092/29) (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:

```cpp
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.

---

<div class="post-metadata">

**Author:** ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)\
**Post date:** [February 3, 2024, 1:33am UTC](https://discourse.vtk.org/t/more-modern-c-pipeline-setup/13244/2 "2024-02-03T01:33:48Z")

</div>

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

> [@dcthomp](#):
>
> ```auto
> vtk::Show(pipeline.execute(cone));
> 
> ```

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.

> [@dcthomp](#):
>
> 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.

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

> [@dcthomp](#):
>
> and some wrapper-like code could turn the JSON data into calls on the constructed filter.

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!
