我的julia ray tracer返回StackOverflower错误

我的julia ray tracer返回StackOverflower错误,julia,raytracing,Julia,Raytracing,我试图在julia中编写光线跟踪器,但我的主函数返回StackOverflowerError。以下是我的主要功能: 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)

我试图在julia中编写光线跟踪器,但我的主函数返回StackOverflowerError。以下是我的主要功能:

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.14159
    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.14159*BRDF*In
end
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
这是我的交集函数:

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.14159
    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.14159*BRDF*In
end
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
错误的原因是什么?

通常在递归调用过多时发生。简而言之,在大多数编程语言中,递归调用的数量是有限制的

在您的示例中,
trace
函数递归调用自身,可能导致堆栈溢出。您有一个参数
maxDepth
,可以限制这一点。将其设置为较低的值可能会解决此特定问题。

通常在递归调用过多时发生。简而言之,在大多数编程语言中,递归调用的数量是有限制的


在您的示例中,
trace
函数递归调用自身,可能导致堆栈溢出。您有一个参数
maxDepth
,可以限制这一点。将其设置为较低的值可能会解决此特定问题。

您的问题是错过了
返回值。
你想要的是

if depth > maxDepth
    return Vec3(0,0,0)
end

没有返回,该行只分配一个
Vec3
,代码继续循环。

您的问题是错过了
返回。
你想要的是

if depth > maxDepth
    return Vec3(0,0,0)
end

如果没有返回,该行只分配了一个
Vec3
,代码继续循环。

这很奇怪,因为maxdepth=3和depth=0,但我收到了这个错误。对不起,我的英语很奇怪,因为maxdepth=3和depth=0,但我收到了这个错误。对不起我的英语。