// File: /workspace/github_cpp_makefile_star2_O3/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 0x5adc
testl %eax, %eax
jle 0x3063
movl %eax, %r13d
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %ebx, %ebx
movq %r14, %rdi
movl %ebx, %esi
callq 0x5ae0
movq (%rax), %rcx
movq %rax, %rdi
movq %rbp, %rsi
callq *0x10(%rcx)
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jne 0x2f9b
jnp 0x2fc5
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 0x2fc5
movapd %xmm0, 0x20(%rsp)
incl %ebx
cmpl %ebx, %r13d
jne 0x2f79
xorpd %xmm1, %xmm1
movapd 0x20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x2fe2
jnp 0x3063
leaq 0x18(%rbp), %r13
leaq 0xf8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movapd %xmm0, 0x20(%rsp)
callq 0x5d12
leaq 0xe0(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x5ca6
movq %r14, %rdi
movl %r15d, %esi
callq 0x5ae0
movq %rax, %r15
movq (%rax), %rax
leaq 0x50(%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 0x5de4
xorpd %xmm1, %xmm1
leaq 0x158(%rsp), %rdi
leaq 0x50(%rsp), %rsi
ucomisd %xmm0, %xmm1
jbe 0x307d
callq 0x5c94
jmp 0x308a
xorpd %xmm0, %xmm0
movq 0x18(%rsp), %rax
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
jmp 0x3933
movsd 0x307b(%rip), %xmm0     # 0x6100
callq 0x5d12
leaq 0x28(%r15), %rsi
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x5c94
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 0x30d2
maxsd %xmm1, %xmm0
movapd %xmm0, %xmm4
cmpl $0x65, %r12d
jl 0x30f1
addq $0x10, %r15
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x5c94
jmp 0x3930
cmpl $0x5, %r12d
jl 0x314f
movapd %xmm4, 0x20(%rsp)
callq 0x2070
movapd 0x20(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x3039(%rip), %xmm0     # 0x6150
ucomisd %xmm0, %xmm1
jbe 0x30d8
movsd 0x2fcb(%rip), %xmm0     # 0x60f0
divsd %xmm1, %xmm0
leaq 0xf8(%rsp), %rbx
leaq 0xc8(%rsp), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5d12
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x5d7a
leal 0x1(%r12), %ebp
movl 0x8(%r15), %eax
cmpl $0x1, %eax
je 0x31b8
testl %eax, %eax
jne 0x324f
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x2fd7(%rip), %xmm0     # 0x6150
mulsd 0x2fdf(%rip), %xmm0     # 0x6160
movsd %xmm0, 0x20(%rsp)
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x2fb5(%rip), %xmm0     # 0x6150
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
jb 0x33a0
sqrtsd %xmm0, %xmm0
jmp 0x33a5
addq $0x10, %r15
leaq 0x270(%rsp), %rbx
leaq 0xe0(%rsp), %rsi
movq %rbx, %rdi
callq 0x5c94
leaq 0x68(%rsp), %r12
movl %ebp, 0x20(%rsp)
leaq 0x50(%rsp), %rbp
movsd 0x2f6e(%rip), %xmm0     # 0x6158
movq %r12, %rdi
movq %rbp, %rsi
callq 0x5d12
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5de4
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x5d12
leaq 0x258(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x5cca
leaq 0xf8(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x59b4
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x3366
movl %ebp, 0x20(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x5c94
leaq 0x38(%rsp), %rbx
leaq 0x50(%rsp), %rbp
movsd 0x2ede(%rip), %xmm0     # 0x6158
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5d12
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5de4
leaq 0x128(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x5d12
leaq 0x240(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x5cca
leaq 0xf8(%rsp), %rdi
leaq 0x198(%rsp), %rsi
movq %rbx, %rdx
callq 0x59b4
leaq 0x158(%rsp), %rbx
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x5de4
xorpd %xmm1, %xmm1
xorl %eax, %eax
movsd %xmm0, 0x10(%rsp)
ucomisd %xmm1, %xmm0
seta %al
leaq 0x2ee0(%rip), %rcx      # 0x61e0
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5de4
movsd 0x8(%rsp), %xmm4
movapd %xmm0, %xmm3
mulsd %xmm0, %xmm3
movsd 0x2dc4(%rip), %xmm2     # 0x60f0
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 0x35de
addq $0x10, %r15
leaq 0x38(%rsp), %rbx
leaq 0xf8(%rsp), %rsi
movq %rbx, %rdi
movl 0x20(%rsp), %edx
movq %r14, %rcx
callq 0x2f38
leaq 0x128(%rsp), %r14
leaq 0xc8(%rsp), %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x5cee
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
jmp 0x392b
callq 0x20e0
movsd %xmm0, 0x8(%rsp)
leaq 0x128(%rsp), %rbx
leaq 0x158(%rsp), %rsi
movq %rbx, %rdi
callq 0x5c94
movsd (%rbx), %xmm0
andpd 0x2e01(%rip), %xmm0     # 0x61d0
xorl %eax, %eax
ucomisd 0x2d8f(%rip), %xmm0     # 0x6168
seta %al
movd %eax, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
psllq $0x3f, %xmm0
psrad $0x1f, %xmm0
pshufd $0xf5, %xmm0, %xmm1     # xmm1 = xmm0[1,1,3,3]
movsd 0x2cf4(%rip), %xmm3     # 0x60f0
xorpd %xmm2, %xmm2
unpcklpd %xmm3, %xmm2            # xmm2 = xmm2[0],xmm3[0]
andpd %xmm0, %xmm2
movq 0x2ce0(%rip), %xmm0     # 0x60f0
pandn %xmm0, %xmm1
por %xmm2, %xmm1
leaq 0x90(%rsp), %rsi
movdqa %xmm1, (%rsi)
movq $0x0, 0x10(%rsi)
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rdx
callq 0x5d34
movq %r12, %rdi
callq 0x5d8e
leaq 0x38(%rsp), %r13
movq %r13, %rdi
movq %rax, %rsi
callq 0x5c94
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x5d34
movsd 0x20(%rsp), %xmm0
callq 0x21e0
leaq 0x1c8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5d12
leaq 0x180(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5d12
movsd 0x20(%rsp), %xmm0
callq 0x2050
leaq 0x2d0(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5d12
leaq 0x1b0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5d12
leaq 0xb0(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x5ca6
movsd 0x2bfa(%rip), %xmm0     # 0x60f0
subsd 0x10(%rsp), %xmm0
ucomisd 0x2c8c(%rip), %xmm0     # 0x6190
jb 0x350c
sqrtsd %xmm0, %xmm0
jmp 0x3511
callq 0x20e0
leaq 0x2b8(%rsp), %rbx
leaq 0x128(%rsp), %rsi
movq %rbx, %rdi
callq 0x5d12
leaq 0xf8(%rsp), %r12
leaq 0xb0(%rsp), %rsi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x5ca6
movq %r12, %rdi
callq 0x5d8e
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5c94
addq $0x10, %r15
leaq 0x2a0(%rsp), %r12
leaq 0xe0(%rsp), %rsi
movq %r12, %rdi
callq 0x5c94
leaq 0x288(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5c94
leaq 0xf8(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x59b4
leaq 0x180(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x2f38
leaq 0xb0(%rsp), %rbx
leaq 0xc8(%rsp), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x5cee
movq 0x18(%rsp), %r12
jmp 0x3922
movapd 0x2c0a(%rip), %xmm1     # 0x61f0
movapd %xmm0, 0x170(%rsp)
xorpd %xmm0, %xmm1
movapd %xmm1, 0x80(%rsp)
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
movapd %xmm4, %xmm0
movsd %xmm2, 0xa8(%rsp)
callq 0x5d12
movsd 0xa8(%rsp), %xmm0
ucomisd 0x2b66(%rip), %xmm0     # 0x6190
movl 0x20(%rsp), %ebp
jb 0x3636
sqrtsd %xmm0, %xmm0
jmp 0x363b
callq 0x20e0
movapd 0x170(%rsp), %xmm2
mulsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movapd 0x2b9a(%rip), %xmm1     # 0x61f0
xorpd %xmm2, %xmm1
xorpd %xmm0, %xmm0
cmpltsd 0x10(%rsp), %xmm0
andpd %xmm0, %xmm2
andnpd %xmm1, %xmm0
orpd %xmm2, %xmm0
leaq 0x68(%rsp), %rbx
leaq 0x50(%rsp), %rsi
movq %rbx, %rdi
callq 0x5d12
leaq 0x128(%rsp), %r13
leaq 0x90(%rsp), %rsi
movq %r13, %rdi
movq %rbx, %rdx
callq 0x5cca
movq %r13, %rdi
callq 0x5d8e
leaq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0x5c94
movsd 0x10(%rsp), %xmm0
ucomisd 0x2acf(%rip), %xmm0     # 0x6190
movapd 0x80(%rsp), %xmm2
ja 0x36df
leaq 0x38(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x5de4
movapd %xmm0, %xmm2
movsd 0x2a09(%rip), %xmm1     # 0x60f0
movapd %xmm1, %xmm0
subsd %xmm2, %xmm0
movsd 0x2a79(%rip), %xmm2     # 0x6170
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x2a65(%rip), %xmm2     # 0x6178
subsd %xmm2, %xmm1
addq $0x10, %r15
cmpl $0x2, %r12d
movsd %xmm1, 0x8(%rsp)
movsd %xmm2, 0x10(%rsp)
jl 0x37b5
movsd 0x2a47(%rip), %xmm0     # 0x6180
mulsd %xmm2, %xmm0
addsd 0x2a43(%rip), %xmm0     # 0x6188
movsd %xmm0, 0x80(%rsp)
callq 0x2070
movsd 0x80(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x29e5(%rip), %xmm0     # 0x6150
ucomisd %xmm0, %xmm1
jbe 0x3876
movsd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x128(%rsp), %rbx
leaq 0xf8(%rsp), %rsi
movq %rbx, %rdi
movl %ebp, %edx
movq %r14, %rcx
callq 0x2f38
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
movsd 0x10(%rsp), %xmm0
jmp 0x38fb
leaq 0x180(%rsp), %r12
leaq 0xf8(%rsp), %rsi
movq %r12, %rdi
movl %ebp, %edx
movq %r14, %rcx
callq 0x2f38
leaq 0xb0(%rsp), %rdi
movq %r12, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x5d12
leaq 0x1f8(%rsp), %r12
leaq 0xe0(%rsp), %rsi
movq %r12, %rdi
callq 0x5c94
leaq 0x1e0(%rsp), %r13
leaq 0x38(%rsp), %rsi
movq %r13, %rdi
callq 0x5c94
leaq 0x128(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x59b4
leaq 0x1b0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x2f38
leaq 0x1c8(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5d12
leaq 0x68(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movq %r14, %rdx
callq 0x5ca6
jmp 0x3900
movsd 0x2872(%rip), %xmm0     # 0x60f0
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 0x5c94
leaq 0x210(%rsp), %r12
leaq 0x38(%rsp), %rsi
movq %r12, %rdi
callq 0x5c94
leaq 0x128(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x59b4
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x2f38
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5d12
movq 0x18(%rsp), %r12
leaq 0x90(%rsp), %rbx
leaq 0xc8(%rsp), %rsi
leaq 0x68(%rsp), %rdx
movq %rbx, %rdi
callq 0x5cee
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x5ca6
movq %r12, %rax
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq