// File: /workspace/github_cpp_makefile_star2_O2/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 0x5956
movl %eax, %r13d
xorl %ebx, %ebx
testl %eax, %eax
cmovlel %ebx, %r13d
xorl %r15d, %r15d
xorpd %xmm3, %xmm3
cmpl %ebx, %r13d
movapd %xmm3, 0x20(%rsp)
je 0x2f56
movq %r14, %rdi
movl %ebx, %esi
callq 0x595a
movq (%rax), %rcx
movq %rax, %rdi
movq %rbp, %rsi
callq *0x10(%rcx)
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm0
jne 0x2f1e
jnp 0x2f4c
movapd %xmm0, %xmm1
movapd 0x20(%rsp), %xmm3
cmpnltpd %xmm3, %xmm1
movapd %xmm3, %xmm2
cmpneqpd %xmm4, %xmm2
andpd %xmm1, %xmm2
movd %xmm2, %eax
testb $0x1, %al
cmovel %ebx, %r15d
jne 0x2f52
movapd %xmm0, %xmm3
jmp 0x2f52
movapd 0x20(%rsp), %xmm3
incl %ebx
jmp 0x2ef1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
jne 0x2f62
jnp 0x2fe1
leaq 0x18(%rbp), %r13
leaq 0xf8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movapd %xmm3, %xmm0
callq 0x5c12
leaq 0xe0(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x5ba6
movq %r14, %rdi
movl %r15d, %esi
callq 0x595a
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 0x5ce4
xorpd %xmm1, %xmm1
leaq 0x158(%rsp), %rdi
leaq 0x50(%rsp), %rsi
ucomisd %xmm0, %xmm1
jbe 0x2ff8
callq 0x5b94
jmp 0x3005
xorpd %xmm0, %xmm0
movq 0x18(%rsp), %rax
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
jmp 0x386b
movsd 0x3100(%rip), %xmm0     # 0x6100
callq 0x5c12
leaq 0x28(%r15), %rsi
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x5b94
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 0x304d
maxsd %xmm1, %xmm0
movapd %xmm0, %xmm4
cmpl $0x65, %r12d
jl 0x306c
addq $0x10, %r15
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x5b94
jmp 0x3868
cmpl $0x5, %r12d
jl 0x30bb
movapd %xmm4, 0x20(%rsp)
callq 0x3ffb
movapd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3053
movsd 0x305f(%rip), %xmm0     # 0x60f0
divsd %xmm1, %xmm0
leaq 0xf8(%rsp), %rbx
leaq 0xc8(%rsp), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5c12
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x5c7a
leal 0x1(%r12), %ebp
movl 0x8(%r15), %eax
cmpl $0x1, %eax
je 0x3106
testl %eax, %eax
jne 0x319d
callq 0x3ffb
mulsd 0x307a(%rip), %xmm0     # 0x6158
movsd %xmm0, 0x20(%rsp)
callq 0x3ffb
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
jb 0x32ee
sqrtsd %xmm0, %xmm0
jmp 0x32f3
addq $0x10, %r15
leaq 0x270(%rsp), %rbx
leaq 0xe0(%rsp), %rsi
movq %rbx, %rdi
callq 0x5b94
leaq 0x68(%rsp), %r12
movl %ebp, 0x20(%rsp)
leaq 0x50(%rsp), %rbp
movsd 0x3018(%rip), %xmm0     # 0x6150
movq %r12, %rdi
movq %rbp, %rsi
callq 0x5c12
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5ce4
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x5c12
leaq 0x258(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x5bca
leaq 0xf8(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x587c
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x32b4
movl %ebp, 0x20(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x5b94
leaq 0x38(%rsp), %rbx
leaq 0x50(%rsp), %rbp
movsd 0x2f88(%rip), %xmm0     # 0x6150
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5c12
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5ce4
leaq 0x128(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x5c12
leaq 0x240(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x5bca
leaq 0xf8(%rsp), %rdi
leaq 0x198(%rsp), %rsi
movq %rbx, %rdx
callq 0x587c
leaq 0x158(%rsp), %rbx
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x5ce4
xorpd %xmm1, %xmm1
xorl %eax, %eax
movsd %xmm0, 0x10(%rsp)
ucomisd %xmm1, %xmm0
seta %al
leaq 0x2f92(%rip), %rcx      # 0x61e0
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5ce4
movsd 0x8(%rsp), %xmm4
movapd %xmm0, %xmm3
mulsd %xmm0, %xmm3
movsd 0x2e76(%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 0x3529
addq $0x10, %r15
leaq 0x38(%rsp), %rbx
leaq 0xf8(%rsp), %rsi
movq %rbx, %rdi
movl 0x20(%rsp), %edx
movq %r14, %rcx
callq 0x2eb8
leaq 0x128(%rsp), %r14
leaq 0xc8(%rsp), %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x5bee
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
jmp 0x3863
callq 0x20e0
movsd %xmm0, 0x8(%rsp)
leaq 0x128(%rsp), %rbx
leaq 0x158(%rsp), %rsi
movq %rbx, %rdi
callq 0x5b94
movsd (%rbx), %xmm0
andpd 0x2eb3(%rip), %xmm0     # 0x61d0
xorl %eax, %eax
ucomisd 0x2e39(%rip), %xmm0     # 0x6160
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 0x2da6(%rip), %xmm3     # 0x60f0
xorpd %xmm2, %xmm2
unpcklpd %xmm3, %xmm2            # xmm2 = xmm2[0],xmm3[0]
andpd %xmm0, %xmm2
movq 0x2d92(%rip), %xmm0     # 0x60f0
pandn %xmm0, %xmm1
por %xmm2, %xmm1
leaq 0x90(%rsp), %rsi
movdqa %xmm1, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rdx
callq 0x5c34
movq %r12, %rdi
callq 0x5c8e
leaq 0x38(%rsp), %r13
movq %r13, %rdi
movq %rax, %rsi
callq 0x5b94
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x5c34
movsd 0x20(%rsp), %xmm0
callq 0x21e0
leaq 0x1c8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5c12
leaq 0x180(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5c12
movsd 0x20(%rsp), %xmm0
callq 0x2050
leaq 0x2d0(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5c12
leaq 0x1b0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5c12
leaq 0xb0(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x5ba6
movsd 0x2caf(%rip), %xmm0     # 0x60f0
subsd 0x10(%rsp), %xmm0
ucomisd 0x2d39(%rip), %xmm0     # 0x6188
jb 0x3457
sqrtsd %xmm0, %xmm0
jmp 0x345c
callq 0x20e0
leaq 0x2b8(%rsp), %rbx
leaq 0x128(%rsp), %rsi
movq %rbx, %rdi
callq 0x5c12
leaq 0xf8(%rsp), %r12
leaq 0xb0(%rsp), %rsi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x5ba6
movq %r12, %rdi
callq 0x5c8e
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5b94
addq $0x10, %r15
leaq 0x2a0(%rsp), %r12
leaq 0xe0(%rsp), %rsi
movq %r12, %rdi
callq 0x5b94
leaq 0x288(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5b94
leaq 0xf8(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x587c
leaq 0x180(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x2eb8
leaq 0xb0(%rsp), %rbx
leaq 0xc8(%rsp), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x5bee
movq 0x18(%rsp), %r12
jmp 0x385a
movapd 0x2cbf(%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 0x5c12
movsd 0xa8(%rsp), %xmm0
ucomisd 0x2c13(%rip), %xmm0     # 0x6188
movl 0x20(%rsp), %ebp
jb 0x3581
sqrtsd %xmm0, %xmm0
jmp 0x3586
callq 0x20e0
movapd 0x170(%rsp), %xmm2
mulsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movapd 0x2c4f(%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 0x5c12
leaq 0x128(%rsp), %r13
leaq 0x90(%rsp), %rsi
movq %r13, %rdi
movq %rbx, %rdx
callq 0x5bca
movq %r13, %rdi
callq 0x5c8e
leaq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0x5b94
movsd 0x10(%rsp), %xmm0
ucomisd 0x2b7c(%rip), %xmm0     # 0x6188
movapd 0x80(%rsp), %xmm2
ja 0x362a
leaq 0x38(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x5ce4
movapd %xmm0, %xmm2
movsd 0x2abe(%rip), %xmm1     # 0x60f0
movapd %xmm1, %xmm0
subsd %xmm2, %xmm0
movsd 0x2b26(%rip), %xmm2     # 0x6168
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x2b12(%rip), %xmm2     # 0x6170
subsd %xmm2, %xmm1
addq $0x10, %r15
cmpl $0x2, %r12d
movsd %xmm1, 0x8(%rsp)
movsd %xmm2, 0x10(%rsp)
jl 0x36ed
movsd 0x2af8(%rip), %xmm0     # 0x6178
mulsd %xmm2, %xmm0
addsd 0x2af4(%rip), %xmm0     # 0x6180
movsd %xmm0, 0x80(%rsp)
callq 0x3ffb
movsd 0x80(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x37ae
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 0x2eb8
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
movsd 0x10(%rsp), %xmm0
jmp 0x3833
leaq 0x180(%rsp), %r12
leaq 0xf8(%rsp), %rsi
movq %r12, %rdi
movl %ebp, %edx
movq %r14, %rcx
callq 0x2eb8
leaq 0xb0(%rsp), %rdi
movq %r12, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x5c12
leaq 0x1f8(%rsp), %r12
leaq 0xe0(%rsp), %rsi
movq %r12, %rdi
callq 0x5b94
leaq 0x1e0(%rsp), %r13
leaq 0x38(%rsp), %rsi
movq %r13, %rdi
callq 0x5b94
leaq 0x128(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x587c
leaq 0x1b0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x2eb8
leaq 0x1c8(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5c12
leaq 0x68(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movq %r14, %rdx
callq 0x5ba6
jmp 0x3838
movsd 0x293a(%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 0x5b94
leaq 0x210(%rsp), %r12
leaq 0x38(%rsp), %rsi
movq %r12, %rdi
callq 0x5b94
leaq 0x128(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x587c
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x2eb8
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5c12
movq 0x18(%rsp), %r12
leaq 0x90(%rsp), %rbx
leaq 0xc8(%rsp), %rsi
leaq 0x68(%rsp), %rdx
movq %rbx, %rdi
callq 0x5bee
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x5ba6
movq %r12, %rax
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq