// File: /workspace/github_cpp_makefile_star2_O3/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 $0x158, %rsp            # imm = 0x158
movq %rdx, 0x28(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movsd 0x188(%rdi), %xmm0
movsd %xmm0, 0x20(%rsp)
movq %rsi, %rdi
callq 0x15c86
movq %rax, %r12
movq 0x190(%r15), %rsi
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdi
movq %r14, 0x60(%rsp)
movq %r14, %rdx
callq 0x1544c
movq 0x18(%rbx), %r13
leaq 0xe8(%rsp), %rax
cmpq %rax, %r13
je 0xa265
leaq 0x8(%r12), %rax
movq %rax, 0x58(%rsp)
movq %r12, %rbx
addq $0x10, %rbx
movq %rsp, %rbp
movslq 0x20(%r13), %r14
testq %r14, %r14
js 0xa28a
movq 0x1b8(%r15), %rax
testq %rax, %rax
jle 0xa28a
cmpq %r14, 0x1c0(%r15)
jle 0xa28a
movq 0x1b0(%r15), %rcx
imulq %r14, %rax
movl (%rcx,%rax,4), %edx
decl %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x98b8
movq 0x1b8(%r15), %rax
cmpq $0x2, %rax
jl 0xa28a
cmpq %r14, 0x1c0(%r15)
jle 0xa28a
movaps (%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movsd 0x10(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
movq 0x1b0(%r15), %rcx
imulq %r14, %rax
movl 0x4(%rcx,%rax,4), %edx
decl %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x98b8
movq 0x1b8(%r15), %rax
cmpq $0x3, %rax
jl 0xa28a
cmpq %r14, 0x1c0(%r15)
jle 0xa28a
movaps (%rsp), %xmm0
movaps %xmm0, 0xb0(%rsp)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x1b0(%r15), %rcx
imulq %r14, %rax
movl 0x8(%rcx,%rax,4), %edx
decl %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x98b8
movapd 0x30(%rsp), %xmm2
movapd %xmm2, %xmm0
subpd (%rsp), %xmm0
movapd 0x40(%rsp), %xmm3
movapd %xmm3, %xmm1
subsd 0x10(%rsp), %xmm1
subpd 0xb0(%rsp), %xmm2
movapd %xmm2, 0x90(%rsp)
movapd %xmm3, %xmm2
subsd 0x68(%rsp), %xmm2
movsd %xmm2, 0xa0(%rsp)
movapd %xmm0, 0x70(%rsp)
movsd %xmm1, 0x80(%rsp)
movq 0x60(%rsp), %rdi
callq 0x15c80
movupd (%rax), %xmm0
movapd 0x30(%rsp), %xmm1
subpd %xmm0, %xmm1
movapd %xmm1, 0x30(%rsp)
movapd %xmm1, 0xc0(%rsp)
movapd 0x40(%rsp), %xmm0
subsd 0x10(%rax), %xmm0
movapd %xmm0, 0x40(%rsp)
movsd %xmm0, 0xd0(%rsp)
leaq 0x110(%rsp), %rax
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movl $0x1, %eax
movq %rax, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x110(%rsp)
movq %rbp, %rdi
leaq 0x70(%rsp), %rsi
callq 0xaeca
movq %rax, %rdi
movq %r12, %rsi
callq 0xaeca
movq %rax, %rdi
leaq 0x98(%rsp), %rsi
callq 0xaeca
movq %rax, %rdi
leaq 0x78(%rsp), %rsi
callq 0xaeca
movq %rax, %rdi
movq 0x58(%rsp), %rsi
callq 0xaeca
movq %rax, %rdi
leaq 0xa0(%rsp), %rsi
callq 0xaeca
movq %rax, %rdi
leaq 0x80(%rsp), %rsi
callq 0xaeca
movq %rax, %rdi
movq %rbx, %rsi
callq 0xaeca
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
cmpq $0x3, %rax
jne 0xa2a9
cmpq $0x3, 0x10(%rsp)
jne 0xa2a9
movapd 0x110(%rsp), %xmm15
movapd 0x120(%rsp), %xmm3
movapd 0x130(%rsp), %xmm2
movapd 0x140(%rsp), %xmm5
movsd 0x150(%rsp), %xmm4
movapd %xmm5, %xmm1
unpckhpd %xmm5, %xmm1            # xmm1 = xmm1[1],xmm5[1]
movapd %xmm2, %xmm6
unpckhpd %xmm2, %xmm6            # xmm6 = xmm6[1],xmm2[1]
movapd %xmm4, %xmm0
unpcklpd %xmm4, %xmm0            # xmm0 = xmm0[0],xmm4[0]
movapd 0x30(%rsp), %xmm14
movapd %xmm14, %xmm7
unpckhpd %xmm15, %xmm7           # xmm7 = xmm7[1],xmm15[1]
mulpd %xmm7, %xmm0
mulpd %xmm6, %xmm7
mulpd %xmm1, %xmm6
movapd %xmm2, %xmm8
unpcklpd %xmm2, %xmm8            # xmm8 = xmm8[0],xmm2[0]
movapd %xmm2, %xmm9
mulsd %xmm4, %xmm9
unpcklpd %xmm9, %xmm9            # xmm9 = xmm9[0,0]
subpd %xmm6, %xmm9
movapd %xmm14, %xmm10
unpcklpd %xmm15, %xmm10          # xmm10 = xmm10[0],xmm15[0]
mulpd %xmm9, %xmm10
movapd 0x40(%rsp), %xmm9
unpcklpd %xmm3, %xmm9            # xmm9 = xmm9[0],xmm3[0]
movapd %xmm9, %xmm6
mulpd %xmm1, %xmm6
subpd %xmm6, %xmm0
movapd %xmm3, %xmm6
unpckhpd %xmm3, %xmm6            # xmm6 = xmm6[1],xmm3[1]
mulpd %xmm6, %xmm0
subpd %xmm0, %xmm10
mulpd %xmm8, %xmm9
subpd %xmm9, %xmm7
movapd %xmm5, %xmm0
unpcklpd %xmm5, %xmm0            # xmm0 = xmm0[0],xmm5[0]
mulpd %xmm7, %xmm0
addpd %xmm10, %xmm0
movapd %xmm0, %xmm7
unpckhpd %xmm0, %xmm7            # xmm7 = xmm7[1],xmm0[1]
divsd %xmm7, %xmm0
xorpd %xmm9, %xmm9
ucomisd %xmm0, %xmm9
ja 0xa249
ucomisd 0xbfe5(%rip), %xmm0     # 0x16030
ja 0xa249
movupd 0xc8(%rsp), %xmm9
movapd 0xceac(%rip), %xmm13    # 0x16f10
xorpd %xmm13, %xmm1
movapd %xmm15, %xmm10
unpckhpd %xmm15, %xmm10          # xmm10 = xmm10[1],xmm15[1]
movapd %xmm3, %xmm11
mulsd %xmm1, %xmm11
movapd %xmm10, %xmm12
mulsd %xmm4, %xmm12
addsd %xmm11, %xmm12
mulsd %xmm14, %xmm12
movapd %xmm9, %xmm11
xorpd %xmm13, %xmm11
movapd %xmm9, %xmm13
shufpd $0x1, %xmm3, %xmm13     # xmm13 = xmm13[1],xmm3[0]
unpcklpd %xmm11, %xmm1           # xmm1 = xmm1[0],xmm11[0]
mulpd %xmm13, %xmm1
shufpd $0x2, %xmm15, %xmm4     # xmm4 = xmm4[0],xmm15[1]
mulpd %xmm9, %xmm4
addpd %xmm1, %xmm4
movapd %xmm15, %xmm1
unpcklpd %xmm5, %xmm1            # xmm1 = xmm1[0],xmm5[0]
mulpd %xmm4, %xmm1
movapd %xmm1, %xmm4
subsd %xmm12, %xmm4
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm4, %xmm1
divsd %xmm7, %xmm1
xorpd %xmm4, %xmm4
ucomisd %xmm1, %xmm4
ja 0xa249
movapd %xmm0, %xmm4
addsd %xmm1, %xmm4
ucomisd 0xbf31(%rip), %xmm4     # 0x16030
ja 0xa249
movapd %xmm9, %xmm4
unpckhpd %xmm9, %xmm4            # xmm4 = xmm4[1],xmm9[1]
movapd %xmm14, %xmm5
unpckhpd %xmm14, %xmm5           # xmm5 = xmm5[1],xmm14[1]
movapd 0xcdee(%rip), %xmm12    # 0x16f10
xorpd %xmm12, %xmm8
movapd %xmm5, %xmm11
xorpd %xmm12, %xmm11
unpcklpd %xmm8, %xmm11           # xmm11 = xmm11[0],xmm8[0]
mulsd %xmm3, %xmm5
mulsd %xmm4, %xmm10
subsd %xmm5, %xmm10
mulsd %xmm10, %xmm6
movapd %xmm2, %xmm4
shufpd $0x1, %xmm3, %xmm4      # xmm4 = xmm4[1],xmm3[0]
mulpd %xmm11, %xmm4
unpckhpd %xmm15, %xmm9           # xmm9 = xmm9[1],xmm15[1]
mulpd %xmm2, %xmm9
addpd %xmm4, %xmm9
unpcklpd %xmm14, %xmm15          # xmm15 = xmm15[0],xmm14[0]
mulpd %xmm9, %xmm15
movapd %xmm15, %xmm2
subsd %xmm6, %xmm2
unpckhpd %xmm15, %xmm15          # xmm15 = xmm15[1,1]
addsd %xmm2, %xmm15
divsd %xmm7, %xmm15
movsd 0x20(%rsp), %xmm2
ucomisd %xmm15, %xmm2
jbe 0xa249
ucomisd 0xcd8e(%rip), %xmm15    # 0x16f30
jbe 0xa249
movapd %xmm15, 0x30(%rsp)
cmpb $0x1, 0x251(%r15)
jne 0xa1c9
movq %rbp, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x9a96
jmp 0xa1d7
movq %rbp, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x9922
movapd (%rsp), %xmm0
movq 0x28(%rsp), %rax
movupd %xmm0, (%rax)
movsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x10(%rax)
movupd (%r12), %xmm2
mulpd %xmm0, %xmm2
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
movsd 0x10(%r12), %xmm2
mulsd %xmm1, %xmm2
addsd %xmm3, %xmm2
ucomisd 0xcd03(%rip), %xmm2     # 0x16f20
movapd 0x30(%rsp), %xmm3
jbe 0xa243
movapd 0xcce3(%rip), %xmm2     # 0x16f10
xorpd %xmm2, %xmm0
movq 0x28(%rsp), %rax
movupd %xmm0, (%rax)
xorpd %xmm2, %xmm1
movlpd %xmm1, 0x10(%rax)
movsd %xmm3, 0x20(%rsp)
movq %r13, %rdi
callq 0x3300
movq %rax, %r13
leaq 0xe8(%rsp), %rax
cmpq %rax, %r13
jne 0x9d15
leaq 0xe0(%rsp), %rdi
callq 0xb148
movsd 0x20(%rsp), %xmm0
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc202(%rip), %rdi      # 0x16493
leaq 0xc230(%rip), %rsi      # 0x164c8
leaq 0xcd51(%rip), %rcx      # 0x16ff0
movl $0x16d, %edx            # imm = 0x16D
callq 0x31a0
leaq 0xc698(%rip), %rdi      # 0x16948
leaq 0xc72f(%rip), %rsi      # 0x169e6
leaq 0xd923(%rip), %rcx      # 0x17be1
movl $0x78, %edx
callq 0x31a0
movq %rax, %rbx
jmp 0xa2e8
movq %rax, %rbx
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
cmpq $0x3, %rax
jne 0xa2fd
cmpq $0x3, 0x10(%rsp)
jne 0xa2fd
leaq 0xe0(%rsp), %rdi
callq 0xb148
movq %rbx, %rdi
callq 0x34b0
callq 0x35ca