Table of Contents

From C++ to C#

NetVtk is generated from VTK's headers: a C++ signature decides its C# signature. With the rules below, VTK's reference and C++ code translate line by line.

Objects

// C++: vtkNew<vtkSphereSource> sphere; the constructor calls vtkSphereSource::New()
using var sphere = new vtkSphereSource();
using var same = vtkSphereSource.New(); // the same, as VTK spells it

Every class deriving from vtkObjectBase is a C# class whose proxy holds one reference: see Object lifetimes. A class gets a constructor when VTK gives it a New() and it isn't abstract. The object factory picks the class New() makes: new vtkRenderWindow() is the platform's OpenGL window.

A C# cast doesn't reach the C++ object: casts go through SafeDownCast.

// vtkPolyData::SafeDownCast(object): the derived proxy, or null
using var data = sphere.GetOutputDataObject(0); // a vtkDataObject
using var polyData = vtkPolyData.SafeDownCast(data);
using var grid = vtkUnstructuredGrid.SafeDownCast(data); // null

vtkSmartPointer<T> in a signature is T:

// static vtkSmartPointer<vtkFloatArray> GetCellNormals(vtkPolyData*): the proxy holds the
// reference the smart pointer held
using vtkFloatArray normals = vtkPolyDataNormals.GetCellNormals(polyData);

Names

C++ C#
vtkActor Vtk.vtkActor
nested vtkCommand::EventIds vtkCommand_EventIds: scopes joined by _
file-scope enum VTKCellType VTKCellType
vtkAOSDataArrayTemplate<double> vtkAOSDataArrayTemplate_double: a template instance named by its arguments
Finalize() (vtkRenderWindow and nine others) left out: it clashes with C#'s finalizer

Size hints

VTK marks pointers it returns with their length (VTK_SIZEHINT): such a return is a C# array of that length, copied right after the call. Writing to the copy doesn't change the object: use the setter.

// double* GetBounds() VTK_SIZEHINT(6): a C# array of 6, copied right after the call
double[] bounds = polyData.GetBounds(); // xmin, xmax, ymin, ymax, zmin, zmax
double[] center = polyData.GetCenter(); // 3
double[] color = property.GetColor();   // 3
int[] size = window.GetSize();          // 2

// pointers in parameters are arrays too
property.SetColor(new[] { 0.2, 0.4, 0.8 });
var first = new double[3];
polyData.GetPoint(0, first); // fills the array given

Strings

// const char* both ways, UTF-8; null for nullptr
array.SetName("Temperatur in °C");
string name = array.GetName();
string className = array.GetClassName(); // "vtkDoubleArray"

Numbers

C++ C#
vtkIdType long: 64 bits on every platform
vtkMTimeType ulong
unsigned long (event ids, observer tags) ulong, range-checked on Windows, where long is 32 bits
int, float, double, bool the same
unsigned char byte
T* of numbers, as a parameter T[]
long points = polyData.GetNumberOfPoints(); // vtkIdType: 64 bits on every platform
ulong time = polyData.GetMTime();           // vtkMTimeType

// C unsigned long: event ids, observer tags
ulong tag = polyData.AddObserver(vtkCommand_EventIds.ModifiedEvent, (_, _) => { });

Enums

// vtkCommand::ModifiedEvent: the enum vtkCommand::EventIds, C# vtkCommand_EventIds
var modified = vtkCommand_EventIds.ModifiedEvent;

// VTK's setters take an int where C++ converts: cast
contour.SetInputArrayToProcess(
  0, 0, 0, (int)vtkDataObject_FieldAssociations.FIELD_ASSOCIATION_POINTS, "stress");

// enums outside a class keep their names
var hexahedron = VTKCellType.VTK_HEXAHEDRON;

Templates

// vtkDoubleArray derives from vtkAOSDataArrayTemplate<double>: in C#, a class named by its
// arguments, with the template's typed members
vtkAOSDataArrayTemplate_double typed = array;
double second = typed.GetValue(1);

The typed arrays' bulk copies ToArray, CopyFrom and AsSpan are members of these instances, FromArray a static member of each typed array: Data and arrays.

Left out

What can't be mapped is left out, each member logged with its reason when NetVtk is generated: Finalize(), std::vector parameters and returns, PrintSelf (ToString() gives VTK's Print text instead), and what VTK marks VTK_WRAPEXCLUDE.