# RayCasting 3D Image with "bit objects"

**URL:** https://discourse.vtk.org/t/raycasting-3d-image-with-bit-objects/7731
**Category:** Support
**Created:** [January 27, 2022, 7:24pm UTC](https://discourse.vtk.org/t/raycasting-3d-image-with-bit-objects/7731 "2022-01-27T19:24:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![osemmler](https://discourse.vtk.org/user_avatar/discourse.vtk.org/osemmler/32/4599_2.png) [@osemmler](https://discourse.vtk.org/u/osemmler)
#### Post date: [January 27, 2022, 7:24pm UTC](https://discourse.vtk.org/t/raycasting-3d-image-with-bit-objects/7731/1 "2022-01-27T19:24:03Z")

</div>

Hello,  
I’d like to ask your advice.

**INPUT**

- 3D image
  - Individual bits in a pixel represent individual objects:
    - 0 [b0000] - no object
    - 1 [b0001] - object A
    - 2 [b0010] - object B
    - 4 [b0100] - object C
    - 8 [b1000] - object D
    - …

  - the picture also contains intersections of objects:
    - 3 [b0011] - objects A and B
    - 12 [b1100] - objects C and D
    - 13 [b1101] - objects A and C and D
    - …

**OUTPUT**

A 2D image created by ray casting a 3D image by setting all the bits of the objects the ray “visited” during the pass. Basically using vtkGPUVolumeRayCastMapper with SetBlendModeToAdditive() but instead of summing perform a binary OR function.

I need the outline of each object defined in the 3D image from the current camera position.

Currently I am ray casting each object from the 3D image separately but I need to optimize the application.

Can you please advise me what combination of filters I should choose?  
Thank you in advance for any advice…

---

<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: [January 28, 2022, 3:46pm UTC](https://discourse.vtk.org/t/raycasting-3d-image-with-bit-objects/7731/2 "2022-01-28T15:46:10Z")

</div>

I don’t think bit operations are possible in the composer (maybe someone else knows otherwise?)

But if you only have 4 objects, you can put each object in its own 8-bit channel. Then you can try a four-channel maximum intensity projection.

---

<div class="post-metadata">

### Author: ![osemmler](https://discourse.vtk.org/user_avatar/discourse.vtk.org/osemmler/32/4599_2.png) [@osemmler](https://discourse.vtk.org/u/osemmler)
#### Post date: [January 29, 2022, 6:20am UTC](https://discourse.vtk.org/t/raycasting-3d-image-with-bit-objects/7731/3 "2022-01-29T06:20:00Z")

</div>

Unfortunately the image contains more objects (approximately 20).

I tried to modify the filters mentioned above, but they have limitations that prevent me from using them despite the modification.

I am currently considering implementing a custom RayCasting algorithm optimized for my case.

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [February 2, 2022, 1:50pm UTC](https://discourse.vtk.org/t/raycasting-3d-image-with-bit-objects/7731/4 "2022-02-02T13:50:27Z")

</div>

The GPU volume ray cast mapper supports label mapping with up to 256 labels. You’d have to create a 3D volume that serves as a mask for the mapper identifying individual labels (objects, in your case). Take a look at the [TestGPURayCastLabelMap](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Rendering/Volume/Testing/Cxx/TestGPURayCastLabelMap.cxx) for an example.

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [February 2, 2022, 2:00pm UTC](https://discourse.vtk.org/t/raycasting-3d-image-with-bit-objects/7731/5 "2022-02-02T14:00:58Z")

</div>

> [@osemmler](#):
>
> Basically using vtkGPUVolumeRayCastMapper with SetBlendModeToAdditive() but instead of summing perform a binary OR function.

The volume mapper supports user specified custom shader code pieces. Take a look at [TestGPURayCastUserShader](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Rendering/VolumeOpenGL2/Testing/Cxx/TestGPURayCastUserShader.cxx) for an example of how to override shader code in the volume mapper. The blending is done using the `VTK::Shading::Impl` tag.
