// File: /workspace/github_cpp_makefile_star2_O1/Aries-orz[P]Ari-RayTracing/src/Engine.cpp
// Function: rt::engine::radianceRT(rt::Ray const&, int, rt::Scene*)
Vec3d radianceRT(const Ray &r, int depth, Scene* scene) {
    double tmin = 0;
    int id = 0;
    int objSize = scene->getSize();
    for (int i=0; i<objSize; ++i) {
        double t = scene->getObj(i)->hit(r);
        if (!t) continue;
        if (t < tmin || !tmin) {
            tmin = t;
            id = i;
        }
    }
    if (!tmin) return Vec3d();
        
    Vec3d x = r.o + r.d * tmin;
    RTObject *obj = scene->getObj(id);
    Vec3d n = obj->getNormal(r, tmin);
    Vec3d nl = n.dot(r.d)<0 ? n : n*-1;
    Vec3d f = obj->color;
    double p = f.x>f.y && f.x>f.z ? f.x : f.y>f.z ? f.y : f.z;
        
    if (depth > 100) return obj->emission;
    if (++depth > 5) {
        if (randRT() < p) f = f * (1/p);
        else return obj->emission;
    }
    //Diffuse reflection
    if (obj->mat == RTObject::DIFFUSE) {
            double r1 = 2 * M_PI * randRT();
            double r2 = randRT(), r2s = sqrt(r2);
            Vec3d w = nl, u = ((fabs(w.x)>.1 ? Vec3d(0,1,0) : Vec3d(1,0,0))^w).norm(), v = w^u;
            Vec3d d = (u*cos(r1)*r2s + v*sin(r1)*r2s + w*sqrt(1-r2)).norm();
            return obj->emission + f * (radianceRT(Ray(x,d), depth, scene));
        }
    //Specular reflection
    else if (obj->mat == RTObject::SPECULAR)
        return obj->emission + f * (radianceRT(Ray(x, r.d-n*2*n.dot(r.d)), depth, scene));
    //Refraction
    Ray reflRay(x, r.d-n*2*n.dot(r.d));
    bool into = n.dot(nl)>0;
    double nc=1, nt=1.5, nnt=into?nc/nt:nt/nc, ddn=r.d.dot(nl), cos2t;
    //When total reflection
    if ((cos2t=1-nnt*nnt*(1-ddn*ddn))<0)
        return obj->emission + f * (radianceRT(reflRay,depth,scene));
    //otherwise, choose reflection or refraction
    Vec3d tdir = (r.d*nnt - n*((into?1:-1)*(ddn*nnt+sqrt(cos2t)))).norm();
    double a=nt-nc, b=nt+nc, R0=a*a/(b*b), c = 1-(into?-ddn:tdir.dot(n));
    double Re=R0+(1-R0)*c*c*c*c*c,Tr=1-Re,P=.25+.5*Re,RP=Re/P,TP=Tr/(1-P);
    //Russian roulette method to reduce the variance
    return obj->emission + f * (depth>2 ? (randRT()<P ? radianceRT(reflRay,depth,scene)*RP:radianceRT(Ray(x,tdir),depth, scene)*TP) : radianceRT(reflRay,depth,scene)*Re+radianceRT(Ray(x,tdir),depth,scene)*Tr);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2e8, %rsp            # imm = 0x2E8
movq %rcx, %r14
movl %edx, %r12d
movq %rsi, %rbp
movq %rdi, 0x18(%rsp)
movq %rcx, %rdi
callq 0x5c90
testl %eax, %eax
jle 0x30b6
movl %eax, %r13d
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %ebx, %ebx
movq %r14, %rdi
movl %ebx, %esi
callq 0x5c94
movq (%rax), %rcx
movq %rax, %rdi
movq %rbp, %rsi
callq *0x10(%rcx)
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jne 0x3083
jnp 0x30ad
movapd %xmm0, %xmm1
movapd 0x20(%rsp), %xmm2
cmpnltpd %xmm2, %xmm1
cmpneqpd %xmm3, %xmm2
andpd %xmm1, %xmm2
movd %xmm2, %eax
testb $0x1, %al
cmovel %ebx, %r15d
jne 0x30ad
movapd %xmm0, 0x20(%rsp)
incl %ebx
cmpl %ebx, %r13d
jne 0x3061
jmp 0x30c3
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movapd 0x20(%rsp), %xmm0
ucomisd 0x3fcf(%rip), %xmm0     # 0x70a0
jne 0x30d9
jnp 0x315a
leaq 0x18(%rbp), %r13
leaq 0x128(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movapd %xmm0, 0x20(%rsp)
callq 0x5eec
leaq 0xe0(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x5e62
movq %r14, %rdi
movl %r15d, %esi
callq 0x5c94
movq %rax, %r15
movq (%rax), %rax
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movapd 0x20(%rsp), %xmm0
callq *0x18(%rax)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5ffa
xorpd %xmm1, %xmm1
leaq 0x158(%rsp), %rdi
leaq 0x60(%rsp), %rsi
ucomisd %xmm0, %xmm1
jbe 0x3174
callq 0x5e44
jmp 0x3181
xorpd %xmm0, %xmm0
movq 0x18(%rsp), %rax
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
jmp 0x3a19
movsd 0x3ed4(%rip), %xmm0     # 0x7050
callq 0x5eec
leaq 0x28(%r15), %rsi
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x5e44
movsd (%rbx), %xmm4
movsd 0x8(%rbx), %xmm0
movsd 0x10(%rbx), %xmm1
movapd %xmm1, %xmm2
cmpltpd %xmm4, %xmm2
movapd %xmm0, %xmm3
cmpltpd %xmm4, %xmm3
andpd %xmm2, %xmm3
movd %xmm3, %eax
testb $0x1, %al
jne 0x31c9
maxsd %xmm1, %xmm0
movapd %xmm0, %xmm4
cmpl $0x65, %r12d
jl 0x31e8
addq $0x10, %r15
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x5e44
jmp 0x3a16
cmpl $0x5, %r12d
jl 0x3246
movapd %xmm4, 0x20(%rsp)
callq 0x2070
movapd 0x20(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x3e4a(%rip), %xmm0     # 0x7058
ucomisd %xmm0, %xmm1
jbe 0x31cf
movsd 0x3e44(%rip), %xmm0     # 0x7060
divsd %xmm1, %xmm0
leaq 0x128(%rsp), %rbx
leaq 0xc8(%rsp), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5eec
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x5f6a
leal 0x1(%r12), %ebp
movl 0x8(%r15), %eax
cmpl $0x1, %eax
je 0x32a7
testl %eax, %eax
jne 0x333b
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x3de8(%rip), %xmm0     # 0x7058
movsd %xmm0, 0x20(%rsp)
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x3dce(%rip), %xmm0     # 0x7058
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
jb 0x348c
sqrtsd %xmm0, %xmm0
jmp 0x3491
addq $0x10, %r15
leaq 0x270(%rsp), %rbx
leaq 0xe0(%rsp), %rsi
movq %rbx, %rdi
callq 0x5e44
leaq 0x78(%rsp), %r12
movl %ebp, 0x20(%rsp)
leaq 0x60(%rsp), %rbp
movsd 0x3d8f(%rip), %xmm0     # 0x7068
movq %r12, %rdi
movq %rbp, %rsi
callq 0x5eec
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5ffa
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x5eec
leaq 0x258(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x5e90
leaq 0x128(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x5b70
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x3452
movl %ebp, 0x20(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x5e44
leaq 0x48(%rsp), %rbx
leaq 0x60(%rsp), %rbp
movsd 0x3d02(%rip), %xmm0     # 0x7068
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5eec
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5ffa
leaq 0xf8(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x5eec
leaq 0x240(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x5e90
leaq 0x128(%rsp), %rdi
leaq 0x198(%rsp), %rsi
movq %rbx, %rdx
callq 0x5b70
leaq 0x158(%rsp), %rbx
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x5ffa
xorpd %xmm1, %xmm1
xorl %eax, %eax
movsd %xmm0, 0x10(%rsp)
ucomisd %xmm1, %xmm0
seta %al
leaq 0x3d04(%rip), %rcx      # 0x70f0
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5ffa
movsd 0x8(%rsp), %xmm4
movapd %xmm0, %xmm3
mulsd %xmm0, %xmm3
movsd 0x3c48(%rip), %xmm2     # 0x7060
movapd %xmm2, %xmm1
subsd %xmm3, %xmm1
movapd %xmm4, %xmm3
mulsd %xmm4, %xmm3
mulsd %xmm1, %xmm3
subsd %xmm3, %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x36cd
addq $0x10, %r15
leaq 0x48(%rsp), %rbx
leaq 0x128(%rsp), %rsi
movq %rbx, %rdi
movl 0x20(%rsp), %edx
movq %r14, %rcx
callq 0x3024
leaq 0xf8(%rsp), %r14
leaq 0xc8(%rsp), %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x5ebe
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
jmp 0x3a11
callq 0x20e0
movsd %xmm0, 0x8(%rsp)
movsd 0x20(%rsp), %xmm0
mulsd 0x3bcb(%rip), %xmm0     # 0x7070
movsd %xmm0, 0x20(%rsp)
leaq 0xf8(%rsp), %rbx
leaq 0x158(%rsp), %rsi
movq %rbx, %rdi
callq 0x5e44
movsd (%rbx), %xmm0
andpd 0x3c11(%rip), %xmm0     # 0x70e0
ucomisd 0x3ba1(%rip), %xmm0     # 0x7078
jbe 0x34f6
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x38(%rsp)
movq %rax, 0x40(%rsp)
jmp 0x3511
leaq 0x38(%rsp), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, -0x8(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
leaq 0x128(%rsp), %rbx
leaq 0x30(%rsp), %rsi
leaq 0xf8(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rdx
callq 0x5f14
movq %rbx, %rdi
callq 0x5f8a
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movq %rax, %rsi
callq 0x5e44
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x5f14
movsd 0x20(%rsp), %xmm0
callq 0x21e0
leaq 0x1c8(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x5eec
leaq 0x180(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5eec
movsd 0x20(%rsp), %xmm0
callq 0x2050
leaq 0x2d0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5eec
leaq 0x1b0(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5eec
leaq 0xb0(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x5e62
movsd 0x3a7b(%rip), %xmm0     # 0x7060
subsd 0x10(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x35fb
sqrtsd %xmm0, %xmm0
jmp 0x3600
callq 0x20e0
leaq 0x2b8(%rsp), %rbx
leaq 0xf8(%rsp), %rsi
movq %rbx, %rdi
callq 0x5eec
leaq 0x128(%rsp), %r12
leaq 0xb0(%rsp), %rsi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x5e62
movq %r12, %rdi
callq 0x5f8a
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5e44
addq $0x10, %r15
leaq 0x2a0(%rsp), %r12
leaq 0xe0(%rsp), %rsi
movq %r12, %rdi
callq 0x5e44
leaq 0x288(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5e44
leaq 0x128(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x5b70
leaq 0x180(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x3024
leaq 0xb0(%rsp), %rbx
leaq 0xc8(%rsp), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x5ebe
movq 0x18(%rsp), %r12
jmp 0x3a08
movapd 0x3a2b(%rip), %xmm1     # 0x7100
movapd %xmm0, 0x170(%rsp)
xorpd %xmm0, %xmm1
movapd %xmm1, 0x90(%rsp)
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
movapd %xmm4, %xmm0
movsd %xmm2, 0xa8(%rsp)
callq 0x5eec
movsd 0xa8(%rsp), %xmm0
ucomisd 0x398a(%rip), %xmm0     # 0x70a0
movl 0x20(%rsp), %ebp
jb 0x3722
sqrtsd %xmm0, %xmm0
jmp 0x3727
callq 0x20e0
movapd 0x170(%rsp), %xmm2
mulsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movapd 0x39be(%rip), %xmm1     # 0x7100
xorpd %xmm2, %xmm1
xorpd %xmm0, %xmm0
cmpltsd 0x10(%rsp), %xmm0
andpd %xmm0, %xmm2
andnpd %xmm1, %xmm0
orpd %xmm2, %xmm0
leaq 0x78(%rsp), %rbx
leaq 0x60(%rsp), %rsi
movq %rbx, %rdi
callq 0x5eec
leaq 0xf8(%rsp), %r13
leaq 0x30(%rsp), %rsi
movq %r13, %rdi
movq %rbx, %rdx
callq 0x5e90
movq %r13, %rdi
callq 0x5f8a
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x5e44
movsd 0x10(%rsp), %xmm0
ucomisd 0x38f6(%rip), %xmm0     # 0x70a0
movapd 0x90(%rsp), %xmm2
ja 0x37c8
leaq 0x48(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x5ffa
movapd %xmm0, %xmm2
movsd 0x3890(%rip), %xmm1     # 0x7060
movapd %xmm1, %xmm0
subsd %xmm2, %xmm0
movsd 0x38a0(%rip), %xmm2     # 0x7080
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x388c(%rip), %xmm2     # 0x7088
subsd %xmm2, %xmm1
addq $0x10, %r15
cmpl $0x2, %r12d
movsd %xmm1, 0x8(%rsp)
movsd %xmm2, 0x10(%rsp)
jl 0x389e
movsd 0x386e(%rip), %xmm0     # 0x7090
mulsd %xmm2, %xmm0
addsd 0x386a(%rip), %xmm0     # 0x7098
movsd %xmm0, 0x90(%rsp)
callq 0x2070
movsd 0x90(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x3804(%rip), %xmm0     # 0x7058
ucomisd %xmm0, %xmm1
jbe 0x395f
movsd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0xf8(%rsp), %rbx
leaq 0x128(%rsp), %rsi
movq %rbx, %rdi
movl %ebp, %edx
movq %r14, %rcx
callq 0x3024
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
movsd 0x10(%rsp), %xmm0
jmp 0x39e4
leaq 0x180(%rsp), %r12
leaq 0x128(%rsp), %rsi
movq %r12, %rdi
movl %ebp, %edx
movq %r14, %rcx
callq 0x3024
leaq 0xb0(%rsp), %rdi
movq %r12, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x5eec
leaq 0x1f8(%rsp), %r12
leaq 0xe0(%rsp), %rsi
movq %r12, %rdi
callq 0x5e44
leaq 0x1e0(%rsp), %r13
leaq 0x48(%rsp), %rsi
movq %r13, %rdi
callq 0x5e44
leaq 0xf8(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x5b70
leaq 0x1b0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x3024
leaq 0x1c8(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5eec
leaq 0x78(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movq %r14, %rdx
callq 0x5e62
jmp 0x39e9
movsd 0x36f9(%rip), %xmm0     # 0x7060
subsd %xmm1, %xmm0
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
leaq 0x228(%rsp), %rbx
leaq 0xe0(%rsp), %rsi
movq %rbx, %rdi
callq 0x5e44
leaq 0x210(%rsp), %r12
leaq 0x48(%rsp), %rsi
movq %r12, %rdi
callq 0x5e44
leaq 0xf8(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x5b70
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x3024
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5eec
movq 0x18(%rsp), %r12
leaq 0x30(%rsp), %rbx
leaq 0xc8(%rsp), %rsi
leaq 0x78(%rsp), %rdx
movq %rbx, %rdi
callq 0x5ebe
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x5e62
movq %r12, %rax
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq