# raytracing in vtk

**URL:** https://discourse.vtk.org/t/raytracing-in-vtk/2364
**Category:** Support
**Created:** [January 1, 2020, 9:16pm UTC](https://discourse.vtk.org/t/raytracing-in-vtk/2364 "2020-01-01T21:16:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![svabhishek29](https://discourse.vtk.org/user_avatar/discourse.vtk.org/svabhishek29/32/479_2.png) [@svabhishek29](https://discourse.vtk.org/u/svabhishek29)
#### Post date: [January 1, 2020, 9:16pm UTC](https://discourse.vtk.org/t/raytracing-in-vtk/2364/1 "2020-01-01T21:16:49Z")

</div>

hi,

i was using Paraviewer and was able to use the OSPRayCaster to render the volume with raycasting to get better lighting and shadows and colors,

how can this be achived in vtk in python?

thanks

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 1, 2020, 11:27pm UTC](https://discourse.vtk.org/t/raytracing-in-vtk/2364/2 "2020-01-01T23:27:06Z")

</div>

Hi, Abhi,

Below is a manual implementation explained in high details in Python that was done in 2014 (quite old):

> **[From Ray Casting to Ray Tracing with Python and VTK](https://pyscience.wordpress.com/2014/10/05/from-ray-casting-to-ray-tracing-with-python-and-vtk/)**
>
> In this post I will show how to use VTK to trace rays emanating from the cell-centers of a source mesh, intersecting with another target mesh, and then show you how to cast subsequent rays bouncing…

  
Note that this was prior to ray tracing officially supported by VTK in 2016.

Since then, googling led to more recent isolated efforts published in public source repositories:

> **[Gjacquenot/PyRayTracer](https://github.com/Gjacquenot/PyRayTracer)**
>
> Preliminary code for ray tracing with Python and VTK - Gjacquenot/PyRayTracer

  

> <https://github.com/Kitware/VTK/blob/master/Examples/Medical/Python/Medical4.py>

all the best,

Paulo

---

<div class="post-metadata">

### Author: ![Dave\_DeMarle](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dave_demarle/32/6_2.png) [@Dave\_DeMarle](https://discourse.vtk.org/u/Dave_DeMarle)
#### Post date: [January 3, 2020, 6:26pm UTC](https://discourse.vtk.org/t/raytracing-in-vtk/2364/3 "2020-01-03T18:26:15Z")

</div>

The OSPRay/OptiX ray tracing engines can be accessed from python like so:

rw = vtk.vtkRenderWindow()  
r = vtk.vtkRenderer()  
rw.AddRenderer®  
rtp = vtk.vtkOSPRayPass()  
r.SetPass(rtp)

From then on (or until you call r.SetPass(None)) rw.Render() will go through the ray tracer.  
See the [tests](https://gitlab.kitware.com/vtk/vtk/tree/master/Rendering/RayTracing/Testing/Cxx) (all in c++ at the VTK level unfortunately) for ideas of what to do with it.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 3, 2020, 7:58pm UTC](https://discourse.vtk.org/t/raytracing-in-vtk/2364/4 "2020-01-03T19:58:12Z")

</div>

OptiX rendering works quite well, too (and it’s real-time if your GPU is good enough).

The only painful limitation is that volume rendering is not supported (at least not when I last tried). Is there a chance that volume rendering will be supported by the OptiX backend? Can OptiX and IndeX be used in VTK for mixed polygonal geometry and volumetric rendering?

---

<div class="post-metadata">

### Author: ![Dave\_DeMarle](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dave_demarle/32/6_2.png) [@Dave\_DeMarle](https://discourse.vtk.org/u/Dave_DeMarle)
#### Post date: [January 3, 2020, 9:23pm UTC](https://discourse.vtk.org/t/raytracing-in-vtk/2364/5 "2020-01-03T21:23:53Z")

</div>

I am not aware of any current work to enable VolumeRendering in [VisRTX](https://github.com/NVIDIA/VisRTX) and thus OptiX within VTK. There are some leanings toward promoting IndeX from ParaView up to VTK, but I don’t know of any plans for tight OptiX/IndeX interoperability.

OSPRay’s path tracer in VTK also lacks volume rendering. That option will get volume rendering later this year when we bump to OSPRay 2.x and add [OpenVKL](https://www.openvkl.org/). OSPRay’s ray caster already has VolumeRendering, but could use minor tweaks to improve sampling in some situations.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 3, 2020, 10:22pm UTC](https://discourse.vtk.org/t/raytracing-in-vtk/2364/6 "2020-01-03T22:22:36Z")

</div>

Thanks a lot this is very useful information. Photorealistic volume rendering (mixed with polygonal geometry) is getting more and more common in medical image visualization and it would be great if we had a solution for this in VTK (preferably with GPU acceleration).

---

<div class="post-metadata">

### Author: ![IanL](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/bc8723/32.png) [@IanL](https://discourse.vtk.org/u/IanL)
#### Post date: [January 7, 2020, 8:47am UTC](https://discourse.vtk.org/t/raytracing-in-vtk/2364/7 "2020-01-07T08:47:47Z")

</div>

That sounds very promising, I look forward to trying it out when the work is done. I must confess that I was surprised that the OSPRay volume ray tracing looked more like ray casting with shadows when I tried it a year or two ago. This explains why that was. I’ll second the call for GPU acceleration (potentially NVidia RTX?), this would be great.
