# Howto wrap a class that does not subclass vtkObject

**URL:** https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889
**Category:** Support
**Created:** [July 6, 2022, 2:00pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889 "2022-07-06T14:00:18Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![jensgw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jensgw](https://discourse.vtk.org/u/jensgw)
#### Post date: [July 6, 2022, 2:00pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/1 "2022-07-06T14:00:18Z")

</div>

Hi there,

as far as I can tell, subclassing from vtkObject automatically includes the class in the wrapping process.  
How do you tell the wrapper to also include additional classes that do not subclass vtkObject?

Best regards  
Jens

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [July 6, 2022, 3:05pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/2 "2022-07-06T15:05:17Z")

</div>

Nothing special should be required to wrap these classes, except that they need to have a public copy constructor. Some very brief documentation is given [here](https://vtk.org/doc/nightly/html/md__builds_gitlab_kitware_sciviz_ci_Documentation_Doxygen_PythonWrappers.html#special-types).

In order for the wrappers to see the header for the class, it must be [placed in a module like this example](https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples/Modules/Wrapping).

---

<div class="post-metadata">

### Author: ![jensgw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jensgw](https://discourse.vtk.org/u/jensgw)
#### Post date: [July 6, 2022, 3:41pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/3 "2022-07-06T15:41:37Z")

</div>

Thanks for the answer and links, I’ll have a look.  
Is there more information specific to java wrapping?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [July 6, 2022, 3:46pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/4 "2022-07-06T15:46:59Z")

</div>

Ah, sorry. I thought you were asking about Python wrapping. For Java wrapping, only vtkObjectBase classes can be wrapped, and wrapping documentation is virtually non-existent. As far as I understand, java wrapping of external modules isn’t even supported in VTK 9. Only the classes within VTK itself are wrapped for java.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [July 6, 2022, 3:49pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/5 "2022-07-06T15:49:36Z")

</div>

> [@dgobbi](#):
>
> As far as I understand, java wrapping of external modules isn’t even supported in VTK 9.

The macros should work to generate the code, but the inter-project dependencies are not there. So you can make a `.jar`, but loading it is a manual process.

---

<div class="post-metadata">

### Author: ![jensgw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jensgw](https://discourse.vtk.org/u/jensgw)
#### Post date: [July 6, 2022, 4:04pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/6 "2022-07-06T16:04:08Z")

</div>

What I am trying to do is not exactly external, vtkOBBTree has an internal data structure, vtkOBBNode, which we need java access for.

So I moved the nested class vtkOBBNode from vtkOBBTree to its own header and src files.  
Now I am trying to get it to wrap for java without the need to subclass from vtkObject.  
But having read your post, this approach does not work.  
Now I try to subclass from vtkObject and hope that somehow works.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [July 6, 2022, 4:22pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/7 "2022-07-06T16:22:47Z")

</div>

Note that you can derive from vtkObjectBase, which is more lightweight than vtkObject.

To convert vtkOBBNode into a vtkObjectBase, it will need

1. “`vtkTypeMacro()`” and and “`static vtkOBBNode* New()`” in its definition
2. “`vtkStandardNewMacro(vtkOBBNode)`” in its .cxx file

Its allocation/deletion in vtkOBBNode.cxx will probably also have to change. All “`new vtkOBBNode`” will have to become “`vtkOBBNode::New()`”. All deletions such as “`delete this->Tree;`” become “`this->Tree->Delete();`”

---

<div class="post-metadata">

### Author: ![jensgw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jensgw](https://discourse.vtk.org/u/jensgw)
#### Post date: [July 6, 2022, 4:43pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/8 "2022-07-06T16:43:07Z")

</div>

Thank you for the hints!  
You mean the usage of `new vtkOBBNode` and equivalents of `delete this->Tree;` have to change over the entire vtk codebase, e.g. in vtkOBBTree and in vtkOBBDicer and so on to  
::New() and -\>Delete()?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [July 6, 2022, 5:03pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/9 "2022-07-06T17:03:12Z")

</div>

Note that I said “probably”. A proper vtkObject has a protected constructor/destructor and therefore must be constructed/destructed with `New()` and `Delete()`. But if you’re doing something purely for your own use, you might be able to leave the constructor/destructor public (but then vtkOBBNode wouldn’t participate in VTK’s garbage collection, and I’m not sure what the side-effects would be).

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [July 6, 2022, 8:47pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/10 "2022-07-06T20:47:55Z")

</div>

AFAIK, stack allocation of `vtkObjectBase` is not possible because what does this code do?

```cpp
vtkNew<vtkObject> obj_heap;
{
    vtkObject obj_stack;
    obj.Register(obj_heap);
    // `obj_stack` is *definitely* going away, but its dtor will do "nothing" because it has reference counts, but access will be UB.
}

```

I’m afraid that `vtkObjectBase` is just incompatible with stack usage due to design decisions made 20+ years ago.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [July 6, 2022, 10:07pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/11 "2022-07-06T22:07:52Z")

</div>

Fortunately vtkOBBNode is only ever allocated on the heap.

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [July 7, 2022, 12:05am UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/12 "2022-07-07T00:05:14Z")

</div>

Since `vtkOBBNode` is a protected field in `vtkOBBTree` how do you intend to access it in your wrapper code?

Is the class needed only for the callback in `vtkOBBTree::IntersectWithOBBTree`? If so I suggest you redefine the callback passing **double Corner[3]** and **double Axes[3][3]** parameters for each node instead. Then _overload_ the public methods of `vtkOBBTree` which require `vtkOBBNode` with the same parameters.

---

<div class="post-metadata">

### Author: ![jensgw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jensgw](https://discourse.vtk.org/u/jensgw)
#### Post date: [July 7, 2022, 5:11pm UTC](https://discourse.vtk.org/t/howto-wrap-a-class-that-does-not-subclass-vtkobject/8889/13 "2022-07-07T17:11:15Z")

</div>

I created an new Method `vtkOBBNode* GetRootOfTree();` to get the root node.

I don’t know exactly what my colleague needs yet so I exposed the entire OBBNode and created getters for its properties.

Yes, it worked with swapping new for ::New() and delete for -\>Delete().  
Thanks to all the commenters, your input was invaluable to solve this!
