// File: /workspace/github_cpp_makefile_star2_O2/IsaacLaw12[P]RayTracer/./src/RenderImage.cpp
// Function: RenderImage::calculate_color(Ray&, double, SceneObject*, Eigen::Matrix<double, 3, 1, 0, 3, 1>&, Eigen::Matrix<double, 3, 1, 0, 3, 1>&, Eigen::Matrix<double, 3, 1, 0, 3, 1>&, int)
void RenderImage::calculate_color(Ray& ray, double t_value, SceneObject* hit_obj, Eigen::Vector3d &hit_normal, Eigen::Vector3d &accum, Eigen::Vector3d &ampl, int level){
    Eigen::Vector3d vector_to_light, intersect_pos, dif_refl, color;
    Eigen::Vector3d obj_intsct_vector, light_reflect_point;
    if (t_value == MISSED_T_VALUE){
        accum << 0,0,0;
        return;
    }
    color = hit_obj->get_ambient_color() * scene->get_ambient();
    double light_concentration, proximity_reflection;
    intersect_pos = ray.get_ray_loc(t_value);

    for(Light light:scene->get_lights()){
        if (light.get_lighting_group() != hit_obj->get_lighting_group()){
            continue;
        }
        if (!lightReachesObject(light, intersect_pos)){
            continue;
        }
        // Diffuse reflection
        vector_to_light = (light.get_pos() - intersect_pos).normalized();
        light_concentration = hit_normal.dot(vector_to_light);
        if (light_concentration < 0){
                continue;
        }

        dif_refl =  hit_obj->get_diffuse_color() * light.get_color() * light_concentration;
        color += dif_refl;
        // Specular reflection

        // The vector from the intersect point to the camera
        obj_intsct_vector = (ray.get_point() - intersect_pos).normalized();
        // The point opposite the light flipped on the normal
        light_reflect_point = (2 * hit_normal.dot(vector_to_light) *  hit_normal) - vector_to_light;
        proximity_reflection = obj_intsct_vector.dot(light_reflect_point);
        if (proximity_reflection > 0.0){
            color += hit_obj->get_specular_color() * light.get_color() * std::pow(proximity_reflection, hit_obj->get_phong());
        }

    }
    accum = accum + ampl.cwiseProduct(color);

    Eigen::Vector3d kr_ampl = hit_obj->get_kr() * ampl;
    Eigen::Matrix3d ko = hit_obj->get_ko();
    double opacity = (ko * Eigen::Vector3d(1,1,1)).dot(Eigen::Vector3d(1,1,1));
    double dummy_t = 0;
    // Reflections
    if (level > 0){
        Eigen::Vector3d reflect_accum = Eigen::Vector3d::Zero();
        Eigen::Vector3d w = -1 * ray.get_dir();
        Eigen::Vector3d ray_dir = (2 * hit_normal.dot(w) * hit_normal - w).normalized();
        Ray reflect_ray = Ray(intersect_pos, ray_dir);
        ray_trace(reflect_ray, reflect_accum, kr_ampl, --level, dummy_t);
        Eigen::Vector3d reflect_ampl = ko * ampl;
        accum = accum + reflect_ampl.cwiseProduct(reflect_accum);
    }
    // Calculate refraction
    if (level > 0 && opacity < 3.0){
        Eigen::Vector3d refract_accum = Eigen::Vector3d::Zero();
        Ray refract_ray = hit_obj->get_refracted_ray(ray, intersect_pos, hit_normal);
        ray_trace(refract_ray, refract_accum, kr_ampl, --level, dummy_t);
        Eigen::Vector3d refract_ampl = (Eigen::Matrix3d::Identity() - ko) * ampl;
        accum = accum + refract_ampl.cwiseProduct(refract_accum);
    }
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movsd 0x8(%rdi), %xmm1
ucomisd %xmm0, %xmm1
jne 0xd788
jp 0xd788
leaq 0x170(%rsp), %rdi
movq %r8, (%rdi)
andq $0x0, 0x8(%rdi)
pushq $0x1
popq %rax
movq %rax, 0x10(%rdi)
movq %rax, 0x18(%rdi)
andq $0x0, (%r8)
leaq 0xc0(%rsp), %rsi
andq $0x0, (%rsi)
callq 0x6232
leaq 0x30(%rsp), %rsi
andq $0x0, (%rsi)
movq %rax, %rdi
callq 0x6232
leaq 0x170(%rsp), %rdi
callq 0x62da
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %r15
movq %rcx, 0x10(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x58(%rsp)
leaq 0x170(%rsp), %r14
movq %r14, %rdi
movq %rdx, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x11936
movq (%r15), %rdi
callq 0x1040c
leaq 0xc0(%rsp), %r12
movq %r14, (%r12)
movq %rax, 0x8(%r12)
leaq 0x128(%rsp), %rdi
movq %r12, %rsi
callq 0xe5ee
leaq 0x170(%rsp), %rbx
movq %rbx, %rdi
movq %r13, 0x28(%rsp)
movq %r13, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x17db0
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
callq 0x52e8
movq %r15, 0x8(%rsp)
movq (%r15), %rdi
callq 0x1042a
movq (%rax), %r13
movq 0x8(%rax), %rbx
movq %rbp, 0xa8(%rsp)
cmpq %rbx, %r13
je 0xdae1
movups (%r13), %xmm0
movaps %xmm0, 0x170(%rsp)
movq 0x20(%r13), %rax
movq %rax, 0x20(%r14)
movups 0x10(%r13), %xmm0
movups %xmm0, 0x10(%r14)
movq 0x38(%r13), %rax
movq %rax, 0x38(%r14)
movupd 0x28(%r13), %xmm0
movupd %xmm0, 0x28(%r14)
movb 0x40(%r13), %al
movb %al, 0x1b0(%rsp)
movq %r14, %rdi
callq 0x61f2
movl %eax, %r15d
movq %rbp, %rdi
callq 0x1198c
cmpl %eax, %r15d
jne 0xdad8
movq 0xa0(%rsp), %rax
movq %rax, 0x150(%rsp)
movupd 0x90(%rsp), %xmm0
movapd %xmm0, 0x140(%rsp)
movq 0x8(%rsp), %rdi
movq %r14, %rsi
leaq 0x140(%rsp), %rdx
callq 0xde00
testb %al, %al
je 0xdad8
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x61ca
movq %r15, 0x30(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %r12, %rdi
leaq 0x30(%rsp), %rsi
callq 0x537a
leaq 0x158(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x5806
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0xc836
movsd %xmm0, 0xb8(%rsp)
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0xdad8
movq %r12, %rdi
movq %rbp, %rsi
callq 0x11928
movq %r12, %rbp
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x61de
movq %rbp, 0x60(%rsp)
movq %r12, 0x68(%rsp)
leaq 0x30(%rsp), %r12
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0xdf50
leaq 0x250(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0xeb38
leaq 0x128(%rsp), %rdi
movq %r15, %rsi
callq 0xdfa0
movq 0x28(%rsp), %rdi
callq 0x17da4
movq %rax, 0x30(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x537a
leaq 0x1e8(%rsp), %rdi
movq %rbp, %rsi
callq 0x5806
movq 0x10(%rsp), %r15
movq %r15, %rdi
leaq 0x158(%rsp), %rsi
callq 0xc836
addsd %xmm0, %xmm0
movsd %xmm0, 0x78(%rsp)
movq %r12, %rdi
leaq 0x78(%rsp), %rsi
movq %rbp, %r12
movq 0xa8(%rsp), %rbp
movq %r15, %rdx
callq 0x5423
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0xe0(%rsp)
leaq 0x158(%rsp), %rax
movq %rax, 0xf0(%rsp)
leaq 0x238(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0xecbf
leaq 0x1e8(%rsp), %rdi
movq %r15, %rsi
callq 0xc836
ucomisd 0xb9ef(%rip), %xmm0     # 0x19450
jbe 0xdad8
movq %r12, %rdi
movq %rbp, %rsi
movsd %xmm0, 0x20(%rsp)
callq 0x11944
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x61de
movq %r12, 0x60(%rsp)
movq %r15, 0x68(%rsp)
movq %rbp, %rdi
callq 0x11918
movapd %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
callq 0x3480
movsd %xmm0, 0x110(%rsp)
leaq 0x30(%rsp), %r15
movq %r15, %rdi
leaq 0x60(%rsp), %rsi
leaq 0x110(%rsp), %rdx
callq 0xdf50
leaq 0x128(%rsp), %rdi
movq %r15, %rsi
callq 0xdfbc
addq $0x48, %r13
jmp 0xd82c
leaq 0x170(%rsp), %rbx
movq 0x58(%rsp), %rdi
movq %rdi, (%rbx)
movq 0x18(%rsp), %r14
movq %r14, 0x8(%rbx)
leaq 0x128(%rsp), %rax
movq %rax, 0x10(%rbx)
movq %rbx, %rsi
callq 0xee57
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x11968
leaq 0xc0(%rsp), %r15
movq %rbx, (%r15)
movq %r14, 0x8(%r15)
leaq 0x78(%rsp), %rdi
movq %r15, %rsi
callq 0xef34
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1197a
movapd 0xd028(%rip), %xmm0     # 0x1ab70
movapd %xmm0, (%r15)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x10(%r15)
leaq 0x60(%rsp), %r14
movq %rbx, (%r14)
movq %r15, 0x8(%r14)
leaq 0x30(%rsp), %rsi
movapd %xmm0, (%rsi)
movq %rax, 0x10(%rsi)
movq %r14, %rdi
callq 0xef62
movl 0x2a0(%rsp), %eax
testl %eax, %eax
jle 0xd776
leaq 0xc0(%rsp), %r15
movq %r15, %rdi
movsd %xmm0, 0x20(%rsp)
callq 0x52c8
movq %r14, %rdi
movq %r15, %rsi
callq 0x548c
leaq 0x30(%rsp), %r15
orl $-0x1, (%r15)
movq 0x28(%rsp), %rdi
callq 0x17daa
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x62ea
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0xcf88
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0xc836
addsd %xmm0, %xmm0
leaq 0x1b8(%rsp), %rsi
movsd %xmm0, (%rsi)
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movq %r14, %rdx
leaq 0x60(%rsp), %r14
callq 0x5423
movsd 0x10(%r12), %xmm0
movsd %xmm0, 0x18(%r13)
movq 0x18(%r12), %rax
movq %rax, 0x20(%r13)
movq %r15, 0x30(%r13)
leaq 0x1d0(%rsp), %r13
leaq 0xc0(%rsp), %rsi
movq %r13, %rdi
callq 0xdfd8
movups 0x90(%rsp), %xmm0
leaq 0x220(%rsp), %rsi
movaps %xmm0, (%rsi)
movq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsi)
movupd (%r13), %xmm0
leaq 0x200(%rsp), %rdx
movapd %xmm0, (%rdx)
movq 0x10(%r13), %rax
movq %rax, 0x10(%rdx)
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
callq 0x17d1e
movl 0x2a0(%rsp), %eax
leal -0x1(%rax), %r8d
leaq 0x78(%rsp), %rcx
leaq 0xb0(%rsp), %r9
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq 0x58(%rsp), %r13
movq %r14, %rdx
callq 0xd5da
movq %rbx, (%r12)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%r12)
leaq 0x30(%rsp), %r12
leaq 0x1b8(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0xef34
movq %r13, (%r12)
movq %r15, 0x8(%r12)
movq %r14, 0x10(%r12)
leaq 0x30(%rsp), %rsi
movq %r13, %rdi
callq 0xee57
movsd 0x20(%rsp), %xmm1
movl 0x2a0(%rsp), %r12d
movl %r12d, %eax
decl %eax
je 0xd776
movsd 0xce39(%rip), %xmm0     # 0x1ab60
ucomisd %xmm1, %xmm0
jbe 0xd776
leaq 0xc0(%rsp), %r15
movq %r15, %rdi
callq 0x52c8
movq %r14, %rdi
movq %r15, %rsi
callq 0x548c
movq (%rbp), %rax
leaq 0xc0(%rsp), %r15
leaq 0x90(%rsp), %rcx
movq %r15, %rdi
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
movq 0x10(%rsp), %r8
callq *0x18(%rax)
addl $-0x2, %r12d
leaq 0x78(%rsp), %rcx
leaq 0xb0(%rsp), %r9
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %r12d, %r8d
callq 0xd5da
leaq 0x1d0(%rsp), %rdi
callq 0xab1c
leaq 0x30(%rsp), %r15
movq %rbx, 0x8(%r15)
movq 0x18(%rsp), %rax
movq %rax, 0x18(%r15)
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf044
movq %r13, (%r15)
movq %rbx, 0x8(%r15)
movq %r14, 0x10(%r15)
leaq 0x30(%rsp), %rsi
movq %r13, %rdi
callq 0xee57
jmp 0xd776
jmp 0xdde8
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x62da
movq %rbx, %rdi
callq 0x3490