<p>I am writing a raytracer in F# using montecarlo sampling
I would like to make my recursive calls tail recursive but I am not sure of this is possible with MC raytracing as one has to evaluate sampled hemisphere before returning.</p>

<p>The accululator variables are there to facilitate the tail recursion and works for naiive raytracers which just trace along a single ray until it terminates.
At the moment the problem is the for loop.</p>

<p>Any help / pointers / references I can read would be appreciated</p>

<p>Currently my code looks as follows:</p>

<pre><code>        if surfaceGeometry.IntersectionAcceptable realSolution t 1.0f (PointForRay ray t)
        then
            let emittedShading = surface.Emitted
            let e = accEmitted + accScatter*emittedShading 
            let mcSamples = surface.SampleCount

            //TODO rewrite this to make it tail recursive
            let mutable totalShading = e/surface.MCNormalization
            for _ in 0..mcSamples-1 do
                let (doesRayContribute,outRay,cosOfIncidence) = surface.Scatter ray t ((int)newTraceDepth)
                let shading = surface.BRDF*cosOfIncidence / (surface.PDF*surface.MCNormalization)
                let s = accScatter*shading
                totalShading &lt;- totalShading + (rayTrace newTraceDepth (outRay , e , s))
            totalShading
        else 
           accEmitted + backgroundColor*accScatter
</code></pre>

CC BY-SA 4.0
2018-06-24T03:48:37.443Z
Are you familiar with path tracing? It lends itself well to a tail-recursive design, as you can accumulate the total radiance and "throughput" (product of reflectances) along the path. You would not have a for-loop here, since the path does not branch into multiple child rays—only a top-level for-loop to fire many paths per pixel.
Nathan Reed
CC BY-SA 4.0
2018-06-24T10:06:23.910Z
Yes, I have a path tracer which uses tail recusion. But Its not the same as montecarlo sampling imho. Any indication that it is (mathematically) equivalent is  most welcome
Marc HPunkt
<raytracing><rendering><monte-carlo>
Tail Recursive Monte Carlo Raytracing
Marc HPunkt
# Tail Recursive Monte Carlo Raytracing

(Asked by: username_0 on 2018-06-23)

I am writing a raytracer in F# using montecarlo sampling
I would like to make my recursive calls tail recursive but I am not sure of this is possible with MC raytracing as one has to evaluate sampled hemisphere before returning.

The accululator variables are there to facilitate the tail recursion and works for naiive raytracers which just trace along a single ray until it terminates.
At the moment the problem is the for loop.

Any help / pointers / references I can read would be appreciated

Currently my code looks as follows:

```
        if surfaceGeometry.IntersectionAcceptable realSolution t 1.0f (PointForRay ray t)
        then
            let emittedShading = surface.Emitted
            let e = accEmitted + accScatter*emittedShading 
            let mcSamples = surface.SampleCount

            //TODO rewrite this to make it tail recursive
            let mutable totalShading = e/surface.MCNormalization
            for _ in 0..mcSamples-1 do
                let (doesRayContribute,outRay,cosOfIncidence) = surface.Scatter ray t ((int)newTraceDepth)
                let shading = surface.BRDF*cosOfIncidence / (surface.PDF*surface.MCNormalization)
                let s = accScatter*shading
                totalShading <- totalShading + (rayTrace newTraceDepth (outRay , e , s))
            totalShading
        else 
           accEmitted + backgroundColor*accScatter

```

**Tags:** raytracing, rendering, monte-carlo

> **Comments:**
> **username_1**: Are you familiar with path tracing? It lends itself well to a tail-recursive design, as you can accumulate the total radiance and "throughput" (product of reflectances) along the path. You would not have a for-loop here, since the path does not branch into multiple child rays—only a top-level for-loop to fire many paths per pixel.  
> **username_0**: Yes, I have a path tracer which uses tail recusion. But Its not the same as montecarlo sampling imho. Any indication that it is (mathematically) equivalent is  most welcome  

---

## 0 answers

