// File: /workspace/github_cpp_makefile_star2_O2/IsaacLaw12[P]RayTracer/./src/Model.cpp
// Function: Model::intersect_ray(Ray&, Eigen::Matrix<double, 3, 1, 0, 3, 1>&)
double Model::intersect_ray(Ray& ray, Eigen::Vector3d &hit_normal){
    double smallest_t = MISSED_T_VALUE;
    const Eigen::Vector3d& ray_dir = ray.get_dir();
    Eigen::Vector3d a_vertex, b_vertex, c_vertex,  a_b, a_c, a_l, solution;
    Eigen::Matrix3d M = Eigen::Matrix3d();
    Eigen::Matrix3d MMs1, MMs2, MMs3;
    double beta, gamma, t_value;
    std::set<int> intersected_faces = bounding_box->intersected_faces(ray);

    //std::cout << "Intersected_faces: "<<intersected_faces.size() << "\n";
    //std::cout << "returned intersected faces: \n ";
    //    for (auto i = intersected_faces.begin(); i != intersected_faces.end(); ++i)
    //        std::cout << *i << ' ';
    //std::cout << "\n";
    for (auto face_num: intersected_faces){
    //for (int face_num=0; face_num<Faces.cols(); face_num++){
        a_vertex = get_vertex( Faces(0, face_num) - 1 );
        b_vertex = get_vertex( Faces(1, face_num) - 1 );
        c_vertex = get_vertex( Faces(2, face_num) - 1 );

        a_b = a_vertex - b_vertex;
        a_c = a_vertex - c_vertex;
        a_l = a_vertex - ray.get_point();

        M << a_b(0), a_c(0), ray_dir(0),
             a_b(1), a_c(1), ray_dir(1),
             a_b(2), a_c(2), ray_dir(2);
        MMs1 = Eigen::Matrix3d(M);
        MMs2 = Eigen::Matrix3d(M);
        MMs3 = Eigen::Matrix3d(M);
        for (int i=0; i<3; i++){
            MMs1(i,0) = a_l(i);
            MMs2(i,1) = a_l(i);
            MMs3(i,2) = a_l(i);
        }
        beta = MMs1.determinant() / M.determinant();
        if (beta < 0 || beta > 1){
            continue;
        }
        gamma = MMs2.determinant() / M.determinant();
        if (gamma < 0 || beta+gamma > 1){
            continue;
        }

        t_value = MMs3.determinant() / M.determinant();
        if ((t_value < smallest_t) && (t_value > 0.00001)){
            smallest_t = t_value;
            if (smoothing){
                hit_normal = get_smooth_face_normal(face_num, beta, gamma);
            } else{
                hit_normal = get_face_normal(face_num);
            }

            if (hit_normal.dot(ray_dir) > 0)
                hit_normal = -1 * hit_normal;
        }
    }

    return smallest_t;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rdx, 0x28(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movsd 0x188(%rdi), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rsi, %rdi
callq 0x17daa
movq %rax, %r12
movq 0x190(%r15), %rsi
leaq 0x148(%rsp), %rbx
movq %rbx, %rdi
movq %r14, 0x60(%rsp)
movq %r14, %rdx
callq 0x17150
movq 0x18(%rbx), %rcx
movq %r15, 0x20(%rsp)
leaq 0x1b0(%r15), %rax
movq %rax, 0x38(%rsp)
leaq 0x8(%r12), %rax
movq %rax, 0x58(%rsp)
movq %r12, 0x48(%rsp)
addq $0x10, %r12
movq %r12, 0x50(%rsp)
pushq $0x1
popq %rbx
pushq $0x2
popq %r14
leaq 0x1d8(%rsp), %r13
leaq 0x150(%rsp), %rax
cmpq %rax, %rcx
je 0x9d35
movq %rcx, 0x68(%rsp)
movslq 0x20(%rcx), %rdx
movq 0x38(%rsp), %rdi
xorl %esi, %esi
movq %rdx, 0x18(%rsp)
callq 0xa554
leaq 0x70(%rsp), %rbp
movl (%rax), %edx
decl %edx
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
callq 0x93ee
leaq 0xe8(%rsp), %rdi
movq %rbp, %rsi
callq 0x52e8
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
callq 0xa554
movl (%rax), %edx
decl %edx
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
callq 0x93ee
leaq 0xb8(%rsp), %r15
leaq 0x178(%rsp), %rdi
movq %rbp, %rsi
callq 0x52e8
movq 0x38(%rsp), %rdi
movq %r14, %rbx
movq %r14, %rsi
movq 0x18(%rsp), %rdx
callq 0xa554
movl (%rax), %edx
decl %edx
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
callq 0x93ee
leaq 0x1f0(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x52e8
leaq 0xe8(%rsp), %r14
movq %r14, 0x70(%rsp)
leaq 0x178(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0xd0(%rsp), %rdi
movq %rbp, %rsi
callq 0xc5d7
movq %r14, 0x70(%rsp)
movq %r12, 0x78(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0xc5d7
movq 0x60(%rsp), %rdi
callq 0x17da4
leaq 0x70(%rsp), %rbp
movq %rbx, %r14
pushq $0x1
popq %rbx
leaq 0xe8(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movq %rax, 0x78(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0xc5d7
leaq 0x100(%rsp), %rax
movq %rax, 0x70(%rsp)
andq $0x0, 0x78(%rsp)
movq %rbx, 0x80(%rsp)
movq %rbx, 0x88(%rsp)
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
movq %rbp, %rdi
leaq 0xb8(%rsp), %rsi
callq 0xa914
movq %rax, %rdi
movq 0x48(%rsp), %rsi
callq 0xa914
movq %rax, %rdi
leaq 0xd8(%rsp), %rsi
callq 0xa914
movq %rax, %rdi
leaq 0xc0(%rsp), %rsi
callq 0xa914
movq %rax, %rdi
movq 0x58(%rsp), %rsi
callq 0xa914
movq %rax, %rdi
leaq 0xe0(%rsp), %rsi
callq 0xa914
movq %rax, %rdi
leaq 0xc8(%rsp), %rsi
callq 0xa914
movq %rax, %rdi
movq 0x50(%rsp), %rsi
callq 0xa914
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
callq 0xa9bc
pushq $0x9
popq %r15
movq %r15, %rcx
movq %rbp, %rdi
leaq 0x100(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x250(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0xa9cc
movq %r15, %rcx
movq %rbp, %rdi
leaq 0x100(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x208(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0xa9cc
pushq $0x9
popq %rcx
movq %rbp, %rdi
leaq 0x100(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x190(%rsp), %rdi
movq %rbp, %rsi
callq 0xa9cc
xorl %ebp, %ebp
cmpq $0x3, %rbp
je 0x9ba5
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa8e8
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %r12, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0xa9e8
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa8e8
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0xa9e8
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa8e8
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
leaq 0x190(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0xa9e8
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
incq %rbp
jmp 0x9b0e
movq %r12, %rdi
callq 0xcf0a
movsd %xmm0, (%rsp)
leaq 0x100(%rsp), %rdi
callq 0xcf0a
movsd (%rsp), %xmm1
divsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
ja 0x9d23
movsd %xmm1, (%rsp)
ucomisd 0xe44d(%rip), %xmm1     # 0x18030
ja 0x9d23
movq %r15, %rdi
callq 0xcf0a
movsd %xmm0, 0x30(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xcf0a
movsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
ja 0x9d23
movsd (%rsp), %xmm0
movsd %xmm1, 0x30(%rsp)
addsd %xmm1, %xmm0
ucomisd 0xe3fd(%rip), %xmm0     # 0x18030
ja 0x9d23
leaq 0x190(%rsp), %rdi
callq 0xcf0a
movsd %xmm0, 0x10(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xcf0a
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x9d23
movsd %xmm1, 0x10(%rsp)
ucomisd 0xf7c7(%rip), %xmm1     # 0x19448
jbe 0x9d23
movq 0x20(%rsp), %rsi
cmpb $0x1, 0x251(%rsi)
jne 0x9cb4
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %rdx
movsd (%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
callq 0x9594
jmp 0x9cc6
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %rdx
callq 0x945e
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x52e8
movq %rbp, %rdi
movq 0x48(%rsp), %rsi
callq 0xc836
ucomisd 0xf765(%rip), %xmm0     # 0x19450
jbe 0x9d17
orl $-0x1, 0x44(%rsp)
leaq 0x70(%rsp), %r15
movq %r15, %rdi
leaq 0x44(%rsp), %rsi
movq 0x28(%rsp), %rbp
movq %rbp, %rdx
callq 0x62ea
movq %rbp, %rdi
movq %r15, %rsi
callq 0x6403
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x68(%rsp), %rdi
callq 0x32e0
movq %rax, %rcx
jmp 0x98b0
leaq 0x148(%rsp), %rdi
callq 0xadca
movsd 0x8(%rsp), %xmm0
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9d7d
jmp 0x9d7d
jmp 0x9d7d
jmp 0x9d7d
jmp 0x9d7d
jmp 0x9d7d
jmp 0x9d7d
jmp 0x9d7d
jmp 0x9d7d
jmp 0x9d7d
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0xa9bc
jmp 0x9d80
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0xadca
movq %rbx, %rdi
callq 0x3490
nop