Skip to content

Commit d15ae64

Browse files
authored
Merge pull request #9 from jspanchu/add-cpp-examples
Add cpp examples and update news
2 parents 6dc970c + c0466da commit d15ae64

File tree

8 files changed

+659
-13
lines changed

8 files changed

+659
-13
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules
22
docs/.vitepress/dist
3-
docs/.vitepress/cache
3+
docs/.vitepress/cache
4+
examples/cpp/vtk
5+
examples/cpp/*/build

docs/guide/cpp/app-1.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,101 @@
1-
Write the business logic of your application in C++ and wrap a simple interface to JavaScript with Embind.
1+
In this approach, you'll code an interactive WebAssembly application that responds to user actions, such as moving sliders on the web page using C++! You'll also enable mouse and keyboard interaction, set up an HTML page with a canvas element, and connect this canvas to a `vtkRenderWindow` and `vtkRenderWindowInteractor` for rendering and interaction.
2+
3+
# Write native code in C++
4+
In the following code, a `vtkConeSource` generates polygonal geometry, which is rendered by a `vtkPolyDataMapper`. We also wrap the C++ functions `void setConeResolution(int)` and `void setConeHeight(double)` to JavaScript and connect them to separate sliders that allow adjusting the cone's resolution and height, respectively.
5+
6+
::: code-group
7+
<<< ../../../examples/cpp/cone/main.cpp#emscriptenKeepalive{cpp:line-numbers=28} [Directly expose C functions]
8+
<<< ../../../examples/cpp/cone/main.cpp#embindFunctions{cpp:line-numbers=40} [Bind C++ functions with embind]
9+
<<< ../../../examples/cpp/cone/main.cpp#args{cpp:line-numbers=64} [Argument parsing]
10+
<<< ../../../examples/cpp/cone/main.cpp#vtk{cpp:line-numbers=91} [VTK setup]
11+
<<< ../../../examples/cpp/cone/main.cpp#bindCanvas{cpp:line-numbers=116} [Bind canvas]
12+
<<< ../../../examples/cpp/cone/main.cpp#coneSourceLifecycle{cpp:line-numbers=141} [Cleanup globals]
13+
<<< ../../../examples/cpp/cone/main.cpp#interactor{cpp:line-numbers=158} [Start interactor]
14+
<<< ../../../examples/cpp/cone/main.cpp{cpp:line-numbers=1} [Full code (main.cpp)]
15+
:::
16+
17+
Explaination:
18+
The referenced sections from `../../../examples/cpp/cone/main.cpp` demonstrate the following:
19+
20+
- **Directly expose C functions**
21+
22+
Native functions which take or return simple C data types can be wrapped conveniently
23+
with an `EMSCRIPTEN_KEEPALIVE` macro. This macro makes the function available in JavaScript with a prefix `_`. Here,
24+
a C symbol `setConeResolution` becomes `_setConeResolution`.
25+
26+
::: warning
27+
Symbols that are wrapped using `EMSCRIPTEN_KEEPALIVE` must have C-style linkage (`extern "C"`), otherwise the symbol name will be mangled like `__Z17setConeResolutioni` which is no fun!
28+
:::
29+
30+
- **Bind C++ functions with embind**
31+
32+
You can wrap any C++ function to JavaScript using the `emscripten::function` construct. Member functions of classes can be wrapped with `emscripten::class`. Emscripten does the to and fro conversion for methods that take or return complex types like `std::string`. You can learn more about this style of wrapping at [emscripten.org/docs](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html).
33+
34+
- **Argument parsing**
35+
36+
Command-line arguments allow configuration of the application at startup. This is similar to how you would parse arguments in desktop applications. In WASM, arguments are passed via the `arguments` key to the wasm module object.
37+
38+
- **VTK setup**
39+
40+
Initialize the VTK pipeline by creating a cone source, mapper, actor, renderer, and render window.
41+
42+
- **Bind canvas**
43+
44+
Connect the HTML canvas element to the VTK render window. This enables rendering output in the browser.
45+
46+
::: danger
47+
A number of things can go wrong in this step. You will see an error in the console if the binding is performed too late (after a VTK render call), or when the canvas selector has a spelling mistake.
48+
```js
49+
vtkWebAssemblyOpenGLRenderWindow (0x1d3c80): Error (0) initializing WebGL2.
50+
vtkWebAssemblyOpenGLRenderWindow (0x1d3c80): Failed to create Emscripten opengl context Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getParameter')
51+
at emscriptenWebGLGet (main.js:1:12409402)
52+
at _glGetIntegerv (main.js:1:12411165)
53+
at 023050d2:0xc1103
54+
at 023050d2:0x4d5c4b
55+
at 023050d2:0x4a73b9
56+
at 023050d2:0x17d647
57+
at 023050d2:0x4d3d4a
58+
at 023050d2:0x7108c8
59+
at callMain (main.js:1:12453949)
60+
at doRun (main.js:1:12454368)
61+
```
62+
:::
63+
64+
- **Cleanup globals**
65+
66+
We ensure that global objects are cleaned up when the interactor gets deleted by doing cleanup in a callback function that observes the `DeleteEvent` on the render window interactor. This application uses globals
67+
so that wrapped functions like `setConeResolution` and `render` can access the `vtkConeSource` object and `vtkRenderWindow` object respectively.
68+
69+
- **Start interactor**
70+
71+
Sets up and starts the VTK render window interactor, enabling mouse and keyboard interaction with the rendered scene.
72+
73+
The full code combines these steps to create a minimal interactive 3D visualization in the browser using VTK, C++, and Embind.
74+
75+
# Interface to the web with HTML
76+
The HTML provides a canvas with `id` "vtk-wasm-canvas". It also passes the resolution of cone and the canvas selector argument strings to the main wasm program using the `arguments` key in the `Module` dictionary.
77+
The `main.js` script is generated by emscripten. It checks whether a variable named `Module` is defined in the global scope and imports the configuration like program arguments, canvas, etc from the `Module` object.
78+
UI callbacks can access the wasm module using the `Module` variable.
79+
80+
::: code-group
81+
<<< ../../public/demo/cpp-app-1/index.html{html:line-numbers=1} [Full HTML (index.html)]
82+
:::
83+
84+
85+
::: tip
86+
You can also use the `-sMODULARIZE` + `-sEXPORT_NAME=createModule` emscripten link options which offer a different approach to instantiating
87+
the wasm runtime.
88+
That approach lets you call a function `createModule({...options})` that returns a Promise resolving with a handle to the wasm runtime.
89+
See [emscripten.org/docs](https://emscripten.org/docs/tools_reference/settings_reference.html#modularize) for more information.
90+
:::
91+
92+
# Build system
93+
Finally, the CMake code creates a WASM executable by compiling the C++ source code and linking with VTK libraries.
94+
95+
::: code-group
96+
<<< ../../../examples/cpp/cone/CMakeLists.txt{cmake:line-numbers=1} [Full CMake (CMakeLists.txt)]
97+
:::
98+
99+
## Result
100+
101+
<iframe src="/vtk-wasm/demo/cpp-app-1/index.html" style="width: 100%; height: 40vh; border: none;"></iframe>

docs/news.md

Lines changed: 224 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,239 @@
1-
## Latest updated
1+
## VTK 9.5 is out
2+
3+
__June 24, 2025__
4+
5+
The [9.5.0 release](https://www.kitware.com/vtk-9-5-0/) includes many improvments regarding the integration of WASM. In addition to observing VTK objects on the client side, you can also invoke methods on VTK objects. This enables us to implement client/server calls for handling picking with client side objects. The other hidden improvement is related to the increased number of vtk objects available in WASM which should help in reproducing more complex VTK scene more accurately.
6+
7+
You can install the equivalent python wheel with the command
8+
9+
```sh
10+
pip install "vtk==9.5.0" --extra-index-url https://wheels.vtk.org
11+
```
12+
13+
The WASM bundle is available here:
14+
15+
[vtk-9.5.0-wasm32-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5327/download)
16+
17+
## Fix serialization for RenderingAnnotation module
18+
19+
__June 23, 2025__
20+
21+
See [vtk/vtk!12192](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12192)
22+
- Fix deserialization of `vtkAlgorithm` inputs
23+
- Fix serialization of `vtkDataSetMapper` inputs
24+
- Enables (de)serialization of necessary indexed properties in _RenderingAnnotation_
25+
- Fix (de)serialization of `vtkLabelPlacementMapper` and `vtkLabelHierarchy`
26+
27+
## Add serialization for vtkGraph and ViewsInfovis module
28+
29+
__June 23, 2025__
30+
31+
See [vtk/vtk!12199](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12199)
32+
33+
- Added manual (de)serialization helper for `vtkGraph`
34+
- Enable auto serialization for subclasses of `vtkGraph`
35+
- Enable serialization in _ViewsInfovis_
36+
- Expose relevant properties, exclude redundant ones
37+
- Serialize child items in vtkContextTransform
38+
- Cannot do this in `vtkAbstractContextItem` since some subclasses (like `vtkDendrogramItem`) add items to themselves, leading to extra child items after deserialization and errors when rendering those child items.
39+
- A few unrelated serialization fixes (`vtkFramebufferPass`, `vtkMapperCollection`)
40+
41+
## 9.5.20250621 is now available!
42+
__June 21, 2025__
43+
44+
You can install the equivalent python wheel with the command
45+
46+
```sh
47+
pip install "vtk==9.5.20250621.dev0" --extra-index-url https://wheels.vtk.org
48+
```
49+
50+
The WASM bundle is available here:
51+
52+
1. [vtk-9.5.20250621-wasm32-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5329/download)
53+
2. [vtk-9.5.20250621-wasm64-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5328/download)
54+
55+
## 9.5.20250614 is now available!
56+
__June 14, 2025__
57+
58+
You can install the equivalent python wheel with the command
259

3-
Clipping planes on mapper is now supported. Which allow us to implement that demo in plain client side.
60+
```sh
61+
pip install "vtk==9.5.20250614.dev0" --extra-index-url https://wheels.vtk.org
62+
```
63+
64+
The WASM bundle is available here:
65+
66+
1. [vtk-9.5.20250614-wasm32-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5323/download)
67+
2. [vtk-9.5.20250614-wasm64-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5322/download)
68+
69+
## 9.5.20250607 is now available!
70+
__June 7, 2025__
71+
72+
You can install the equivalent python wheel with the command
73+
74+
```sh
75+
pip install "vtk==9.5.20250607.dev0" --extra-index-url https://wheels.vtk.org
76+
```
77+
78+
The WASM bundle is available here:
79+
80+
1. [vtk-9.5.20250607-wasm32-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5261/download)
81+
2. [vtk-9.5.20250607-wasm64-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5262/download)
82+
83+
## Clipping planes in WASM/WebGL
84+
85+
__June 6, 2025__
86+
87+
Clipping planes on mapper is now supported by [vtk/vtk!12176](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12176). This allow us to implement the TIE fighter clip plane demo in client side.
488

589
<iframe src="/vtk-wasm/demo/viewer-starfighter2.html" height="592" width="100%" frameborder="0" allowfullscreen="" title="Rendering clipping"></iframe>
690

7-
## Add support for 32 and 64 bits WASM bundle
91+
## Fix serialization for 3D widgets
92+
93+
__June 5, 2025__
94+
95+
- [vtk/vtk!12162](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12162) fixes serialization errors of classes
96+
in the _InteractionWidgets_ module.
97+
- Enable auto serialization for `vtkResliceCursorRepresentation`, `vtkDistanceRepresentation`, `vtkTextRepresentation`, and related classes.
98+
99+
## Fix serialization of various classes
100+
101+
__June 4, 2025__
102+
103+
- [vtk/vtk!12167] fixed serialization for various classes and enables serialization for more classes in the _ImagingCore_, _ImagingColor_, _RenderingGridAxes_, and the _RenderingImage_ modules.
104+
- The `vtkSignedCharArray` class now has serialization enabled.
105+
- The redundant width/height properties of vtkActor2D are now excluded from (de)serialization.
106+
107+
## 9.5.20250531 is now available!
108+
109+
__May 31, 2025__
110+
111+
You can install the equivalent python wheel with the command
112+
113+
```sh
114+
pip install "vtk==9.5.20250531.dev0" --extra-index-url https://wheels.vtk.org
115+
```
116+
117+
The WASM bundle is available here:
8118

9-
....
119+
1. [vtk-9.5.20250531-wasm32-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5198/download)
120+
2. [vtk-9.5.20250531-wasm64-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5197/download)
121+
122+
123+
## Add get/set functions in vtkRemoteSession
124+
125+
__May 27, 2025__
126+
127+
`vtkRemoteSession::getState` is deprecated in favor of `vtkRemoteSession::get`. The new `vtkRemoteSession::set` lets you
128+
apply properties from JSON in bulk on a VTK object. See [vtrk/vtk!12155](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12155)
10129

11130
## Add runtime support for WebGL2 and WebGPU
12131

132+
__May 24, 2025__
133+
13134
You can now choose the backend to use for rendering your VTK scene. The WebGPU is still work in progress but is now available for testing via runtime configuration option.
14135

15-
## Helper JavaScript library
136+
WebGPU classes in VTK are now serialized and available in the vtkWebAssemblyAsync.{mjs,wasm} files. Subsequent packages will distribute both vtkWebAssemblyAsync.{mjs,wasm} and vtkWebAssembly.{mjs,wasm} files. The async package will only work in browsers that enable JavaScript Promise Integration ([JSPI](https://github.com/WebAssembly/js-promise-integration)). See [vtk/vtk!12143](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12143)
16137

17-
A new JavaScript library is available under `@kitware/trame-vtklocal` which delivers a set of helper tools for VTK.wasm and JavaScript.
18-
This include a standalone viewer for a VTK scene dump, a wrapper for pure JavaScript usage and some core handler when creating a widget (i.e. React, Svelt, Angular...) for interating with a trame-vtklocal server implementation.
138+
## Fix serialization for RenderingVolume module
19139

20-
## API refactor with remote and standalone capabilities
140+
__May 27, 2025__
21141

22-
bla
142+
[vtk/vtk!12142] fixes serialization errors that arise when serializing classes in the _RenderingVolume_ module.
23143

24-
## VTK 9.5 is out
144+
## Add standalone and remote session API
145+
146+
__May 19, 2025__
147+
148+
The `vtkWebAssembly.mjs` library now provides two new classes `vtkRemoteSession` and `vtkStandaloneSession`.
149+
- Remote session API is concerned with use cases where a "server" creates VTK
150+
objects and sends the state to a WASM "client" that deserializes the state
151+
into objects to mimic the visualization pipeline on the "server".
152+
This API does not allow creating objects in the WASM world. It is possible,
153+
although very difficult and prone to bugs.
154+
155+
- Standalone API is important when one wants to directly create and manipulate objects
156+
in the local context in the absence of a server.
157+
158+
See [vtk/vtk!12110](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12110)
159+
160+
## Fix vtkFieldData deserialization when no. of arrays is the same but some or all individual arrays have changed
161+
162+
__May 19, 2025__
163+
164+
[vtk/vtk!12129](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12129) fixed a bug where the arrays had their values updated only when the total number of arrays in the vtkFieldData changed between deserializations. Now, it always update the array list because even though the number of arrays remain the same, the arrays themselves might be different.
165+
166+
## Add support for 32 and 64 bits WASM bundle
167+
168+
__May 14, 2025__
169+
170+
Build and distribute artifacts for wasm64. This package allows rendering large meshes bigger than 4GB. It requires a web browser that supports 64-bit wasm memories.
171+
172+
## 9.5.20250513 is now available!
173+
__May 13, 2025__
25174

26-
The 9.5 release includes many improvment regarding the integration of WASM. With this new release, on top of observing VTK objects on the client side, you can also perform methods call. This enables us to implement some client/server calls for handling picking with client side resolution. The other hidden improvement is related to the increased number of vtk objects available in WASM which should help in reproducing more complex VTK scene more accurately.
175+
You can install the equivalent python wheel with the command
176+
177+
```sh
178+
pip install "vtk==-9.5.20250513-wa" --extra-index-url https://wheels.vtk.org
179+
```
180+
181+
The WASM bundle is available here:
182+
183+
[vtk-9.5.20250513-wasm32-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5101/download)
184+
185+
## 9.5.20250510 is now available!
186+
__May 10, 2025__
187+
188+
You can install the equivalent python wheel with the command
189+
190+
```sh
191+
pip install "vtk==-9.5.20250510-wa" --extra-index-url https://wheels.vtk.org
192+
```
193+
194+
The WASM bundle is available here:
195+
196+
[vtk-9.5.20250510-wasm32-emscripten.tar.gz](https://gitlab.kitware.com/vtk/vtk/-/package_files/5067/download)
197+
198+
## Add serialization for pickers
199+
200+
__May 5, 2025__
201+
202+
[vtk/vtk!12095](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12095) enables auto serialization for pickers in the _RenderingCore_ module, as well as `vtkAssemblyNode`, `vtkAssemblyPath`, and `vtkProp3DCollection` for properties of certain pickers.
203+
204+
## Fix serialization of VTK classes
205+
206+
__April 25, 2025__
207+
208+
Many serialization issues were fixed in [vtk/vtk!12012](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12012). Serialization capability was also added to some classes. Here's a list of classes
209+
that were affected:
210+
211+
- vtkActor2D
212+
- vtkBitArray
213+
- vtkDataArray
214+
- vtkDiscretizableColorTransferFunction
215+
- vtkFieldData
216+
- vtkInformation
217+
- vtkLabeledContourMapper
218+
- vtkLogLookupTable
219+
- vtkMultiBlockDataSet
220+
- vtkOpenGLLabeledContourMapper
221+
- vtkPolyDataMapper2D
222+
- vtkProp3DFollower
223+
- vtkProperty
224+
- vtkRenderWindow
225+
- vtkScalarsToColors
226+
- vtkShaderProperty
227+
- vtkStructuredGrid
228+
- vtkTextPropertyCollection
229+
- vtkTexture
230+
- vtkVariant
231+
- vtkVariantArray
232+
- vtkWindow
233+
234+
## Helper JavaScript library
235+
236+
__October 7, 2024__
237+
238+
A new JavaScript library is available under `@kitware/trame-vtklocal` which delivers a set of helper tools for VTK.wasm and JavaScript.
239+
This include a standalone viewer for a VTK scene dump, a wrapper for pure JavaScript usage and some core handler when creating a widget (i.e. React, Svelt, Angular...) for interating with a trame-vtklocal server implementation.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
</head>
7+
8+
<body>
9+
<div>
10+
<input type="range" min="3" max="128" value="8" onchange="Module._setConeResolution(this.value); Module.render();"
11+
style="position: absolute; top: 1rem; right: 1rem; z-index: 10;" />
12+
<input type="range" min="1.0" max="2.0" value="1.0" step="0.1" onchange="Module.setConeHeight(this.value); Module.render();"
13+
style="position: absolute; top: 1rem; left: 1rem; z-index: 10;" />
14+
</div>
15+
<canvas id="vtk-wasm-canvas" tabindex="-1" onclick="focus()"></canvas>
16+
<script type="text/javascript">
17+
var Module = {
18+
arguments: [
19+
"--resolution", "8",
20+
"--canvas-selector", "#vtk-wasm-canvas"],
21+
canvas: () => {
22+
return document.querySelector("#vtk-wasm-canvas");
23+
},
24+
};
25+
</script>
26+
<script type="text/javascript" src="./main.js"></script>
27+
</body>
28+
29+
</html>

docs/public/demo/cpp-app-1/main.js

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)