<p>I' trying to write a monte carlo ray tracer in julia, but my main function returns stack overflow error. Below is my main function:</p>

<pre><code>function trace(ray::Ray, surfaces::Array{Any, 1}, depth::Int64, maxDepth::Int64)
material, t = findIntersection(ray, surfaces, Inf)
if typeof(material) == Empty
    Vec3(0,0,0)
end
if depth &gt; maxDepth
    Vec3(0,0,0)
end
if material.isLight == true
    material.emittance
end
normal = material.normal
ρ = material.reflectance
BRDF = ρ/3.1415
R = randHemi(normal)
cosθ = dot(R,normal)
newRay = Ray(ray.s + t*ray.d, R)
In = trace(newRay, surfaces, depth+1, maxDepth)
2.0*3.1415*BRDF*In
end
</code></pre>

<p>But when I run it, I receive this error:</p>

<pre><code>StackOverflowError:

Stacktrace:
 [1] findIntersection(::Ray, ::Array{Any,1}, ::Float64) at .\In[31]:10
 [2] trace(::Ray, ::Array{Any,1}, ::Int64, ::Int64) at .\In[33]:2
 [3] trace(::Ray, ::Array{Any,1}, ::Int64, ::Int64) at .\In[33]:18 (repeats 
14493 times)
 [4] top-level scope at In[35]:1
</code></pre>

<p>And here is my intersection function:</p>

<pre><code>function findIntersection(ray::Ray, surfaces::Array{Any, 1}, tmin::Float64)
hitSurface = Empty(Vec3(0,0,0), Vec3(0,0,0), Vec3(0,0,0), false)
for surface in surfaces
    t = intersect(surface, ray)
    if t &lt; tmin
        hitSurface = surface
        tmin = t
    end
end
return hitSurface, tmin       
end
</code></pre>

CC BY-SA 4.0
2019-09-25T14:37:53.780Z
did you forget a return in the ifs of the trace function?
ratchet freak
CC BY-SA 4.0
2019-09-26T02:51:59.687Z
`trace` is calling itself 14,000+ times. What is `maxDepth` set to? This is probably set by whomever is calling `trace`. You haven't posted that, so we can't tell you much more.
user1118321
CC BY-SA 4.0
2019-09-26T05:46:51.173Z
You could change the function to iterate ray steps sequentially instead of calling itself recursively to avoid this, although a 14,000 step ray sounds far too much for a path tracer, usually I cap these to around 8 or so max steps.
PaulHK
<raytracing>
Why does my julia ray tracer return stack overflow error?
user11237
# Why does my julia ray tracer return stack overflow error?

(Asked by: username_0 on 2019-09-25)

I' trying to write a monte carlo ray tracer in julia, but my main function returns stack overflow error. Below is my main function:

```
function trace(ray::Ray, surfaces::Array{Any, 1}, depth::Int64, maxDepth::Int64)
material, t = findIntersection(ray, surfaces, Inf)
if typeof(material) == Empty
    Vec3(0,0,0)
end
if depth > maxDepth
    Vec3(0,0,0)
end
if material.isLight == true
    material.emittance
end
normal = material.normal
ρ = material.reflectance
BRDF = ρ/3.1415
R = randHemi(normal)
cosθ = dot(R,normal)
newRay = Ray(ray.s + t*ray.d, R)
In = trace(newRay, surfaces, depth+1, maxDepth)
2.0*3.1415*BRDF*In
end

```

But when I run it, I receive this error:

```
StackOverflowError:

Stacktrace:
 [1] findIntersection(::Ray, ::Array{Any,1}, ::Float64) at .\In[31]:10
 [2] trace(::Ray, ::Array{Any,1}, ::Int64, ::Int64) at .\In[33]:2
 [3] trace(::Ray, ::Array{Any,1}, ::Int64, ::Int64) at .\In[33]:18 (repeats 
14493 times)
 [4] top-level scope at In[35]:1

```

And here is my intersection function:

```
function findIntersection(ray::Ray, surfaces::Array{Any, 1}, tmin::Float64)
hitSurface = Empty(Vec3(0,0,0), Vec3(0,0,0), Vec3(0,0,0), false)
for surface in surfaces
    t = intersect(surface, ray)
    if t < tmin
        hitSurface = surface
        tmin = t
    end
end
return hitSurface, tmin       
end

```

**Tags:** raytracing

> **Comments:**
> **username_1**: did you forget a return in the ifs of the trace function?  
> **username_2**: `trace` is calling itself 14,000+ times. What is `maxDepth` set to? This is probably set by whomever is calling `trace`. You haven't posted that, so we can't tell you much more.  
> **username_3**: You could change the function to iterate ray steps sequentially instead of calling itself recursively to avoid this, although a 14,000 step ray sounds far too much for a path tracer, usually I cap these to around 8 or so max steps.  

---

## 0 answers

