// File: /workspace/github_cpp_makefile_star2_O1/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 0x15cba
movq %rax, %r12
movq 0x190(%r15), %rsi
leaq 0x128(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
callq 0x15480
movq 0x18(%rbx), %r13
leaq 0x130(%rsp), %rax
cmpq %rax, %r13
je 0xa336
leaq 0x8(%r12), %rax
movq %rax, 0x60(%rsp)
movq %r12, %rax
addq $0x10, %rax
movq %rax, 0x58(%rsp)
movq %rsp, %rbp
movslq 0x20(%r13), %rbx
testq %rbx, %rbx
js 0xa35b
movq 0x1b8(%r15), %rax
testq %rax, %rax
jle 0xa35b
cmpq %rbx, 0x1c0(%r15)
jle 0xa35b
movq 0x1b0(%r15), %rcx
imulq %rbx, %rax
movl (%rcx,%rax,4), %edx
decl %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x993e
movq 0x1b8(%r15), %rax
cmpq $0x2, %rax
jl 0xa35b
cmpq %rbx, 0x1c0(%r15)
jle 0xa35b
movaps (%rsp), %xmm0
movaps %xmm0, 0x100(%rsp)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x1b0(%r15), %rcx
imulq %rbx, %rax
movl 0x4(%rcx,%rax,4), %edx
decl %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x993e
movq 0x1b8(%r15), %rax
cmpq $0x3, %rax
jl 0xa35b
cmpq %rbx, 0x1c0(%r15)
jle 0xa35b
movaps (%rsp), %xmm0
movaps %xmm0, 0x110(%rsp)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x1b0(%r15), %rcx
imulq %rbx, %rax
movl 0x8(%rcx,%rax,4), %edx
decl %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x993e
movapd (%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movapd 0x100(%rsp), %xmm4
movapd %xmm4, %xmm2
subpd 0x110(%rsp), %xmm2
movapd %xmm2, 0xe0(%rsp)
movsd 0x30(%rsp), %xmm3
movapd %xmm3, %xmm2
subsd 0x68(%rsp), %xmm2
movsd %xmm2, 0xf0(%rsp)
movapd %xmm4, %xmm2
subpd %xmm0, %xmm2
movapd %xmm2, 0xc0(%rsp)
movapd %xmm3, %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
movq %r14, %rdi
callq 0x15cb4
movupd (%rax), %xmm0
movapd 0x100(%rsp), %xmm1
subpd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movsd 0x30(%rsp), %xmm0
subsd 0x10(%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movl $0x1, %eax
movq %rax, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movsd 0xe0(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movq %rbp, %rdi
leaq 0xc0(%rsp), %rsi
callq 0xaf6c
movq %rax, %rdi
movq %r12, %rsi
callq 0xaf6c
movq %rax, %rdi
leaq 0xe8(%rsp), %rsi
callq 0xaf6c
movq %rax, %rdi
leaq 0xc8(%rsp), %rsi
callq 0xaf6c
movq %rax, %rdi
movq 0x60(%rsp), %rsi
callq 0xaf6c
movq %rax, %rdi
leaq 0xf0(%rsp), %rsi
callq 0xaf6c
movq %rax, %rdi
leaq 0xd0(%rsp), %rsi
callq 0xaf6c
movq %rax, %rdi
movq 0x58(%rsp), %rsi
callq 0xaf6c
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
cmpq $0x3, %rax
jne 0xa37a
cmpq $0x3, 0x10(%rsp)
jne 0xa37a
movapd 0x80(%rsp), %xmm5
movapd 0x90(%rsp), %xmm4
movapd 0xa0(%rsp), %xmm6
movsd 0xb0(%rsp), %xmm1
movapd 0x40(%rsp), %xmm3
movsd 0x50(%rsp), %xmm2
movsd 0xa8(%rsp), %xmm7
movapd %xmm6, %xmm0
shufpd $0x1, %xmm7, %xmm0      # xmm0 = xmm0[1],xmm7[0]
movsd 0x98(%rsp), %xmm8
movapd %xmm4, %xmm7
shufpd $0x1, %xmm8, %xmm7      # xmm7 = xmm7[1],xmm8[0]
movsd 0x78(%rsp), %xmm8
movapd %xmm3, %xmm9
shufpd $0x1, %xmm8, %xmm9      # xmm9 = xmm9[1],xmm8[0]
mulpd %xmm7, %xmm9
mulpd %xmm0, %xmm7
movapd %xmm4, %xmm10
unpcklpd %xmm1, %xmm10           # xmm10 = xmm10[0],xmm1[0]
movsd 0x90(%rsp), %xmm11
movapd %xmm1, %xmm12
unpcklpd %xmm11, %xmm12          # xmm12 = xmm12[0],xmm11[0]
mulpd %xmm10, %xmm12
subpd %xmm7, %xmm12
movapd %xmm3, %xmm7
movhpd 0x70(%rsp), %xmm7       # xmm7 = xmm7[0],mem[0]
mulpd %xmm12, %xmm7
movapd %xmm2, %xmm10
movhpd 0x80(%rsp), %xmm10      # xmm10 = xmm10[0],mem[0]
mulpd %xmm10, %xmm0
movapd %xmm3, %xmm12
shufpd $0x1, %xmm1, %xmm12     # xmm12 = xmm12[1],xmm1[0]
movapd %xmm1, %xmm13
unpcklpd %xmm8, %xmm13           # xmm13 = xmm13[0],xmm8[0]
mulpd %xmm12, %xmm13
subpd %xmm0, %xmm13
movsd 0x88(%rsp), %xmm0
movapd %xmm5, %xmm8
shufpd $0x1, %xmm0, %xmm8      # xmm8 = xmm8[1],xmm0[0]
mulpd %xmm13, %xmm8
subpd %xmm8, %xmm7
movapd %xmm4, %xmm8
unpcklpd %xmm11, %xmm8           # xmm8 = xmm8[0],xmm11[0]
mulpd %xmm10, %xmm8
movapd %xmm6, %xmm0
movhpd 0xa0(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
subpd %xmm8, %xmm9
mulpd %xmm9, %xmm0
addpd %xmm7, %xmm0
movapd %xmm0, %xmm7
unpckhpd %xmm0, %xmm7            # xmm7 = xmm7[1],xmm0[1]
divsd %xmm7, %xmm0
xorpd %xmm8, %xmm8
ucomisd %xmm0, %xmm8
ja 0xa31a
ucomisd 0xbf0b(%rip), %xmm0     # 0x16030
ja 0xa31a
movapd 0x70(%rsp), %xmm13
movupd 0x48(%rsp), %xmm9
movapd %xmm13, %xmm8
unpckhpd %xmm13, %xmm8           # xmm8 = xmm8[1],xmm13[1]
movapd %xmm13, %xmm10
unpcklpd %xmm6, %xmm10           # xmm10 = xmm10[0],xmm6[0]
shufpd $0x1, %xmm9, %xmm6      # xmm6 = xmm6[1],xmm9[0]
movapd %xmm6, %xmm11
xorpd 0xcdaf(%rip), %xmm11    # 0x16f10
mulsd %xmm5, %xmm11
movapd %xmm8, %xmm12
mulsd %xmm1, %xmm12
addsd %xmm11, %xmm12
mulsd 0x40(%rsp), %xmm12
shufpd $0x2, %xmm13, %xmm1     # xmm1 = xmm1[0],xmm13[1]
mulpd %xmm9, %xmm1
shufpd $0x1, %xmm5, %xmm9      # xmm9 = xmm9[1],xmm5[0]
mulpd %xmm6, %xmm9
subpd %xmm9, %xmm1
mulpd %xmm10, %xmm1
movapd %xmm1, %xmm6
subsd %xmm12, %xmm6
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm6, %xmm1
divsd %xmm7, %xmm1
xorpd %xmm6, %xmm6
ucomisd %xmm1, %xmm6
ja 0xa31a
movapd %xmm0, %xmm6
addsd %xmm1, %xmm6
ucomisd 0xbe61(%rip), %xmm6     # 0x16030
ja 0xa31a
movapd %xmm5, %xmm6
unpckhpd %xmm5, %xmm6            # xmm6 = xmm6[1],xmm5[1]
movapd %xmm3, %xmm9
shufpd $0x1, %xmm4, %xmm9      # xmm9 = xmm9[1],xmm4[0]
movapd %xmm4, %xmm10
shufpd $0x1, %xmm5, %xmm10     # xmm10 = xmm10[1],xmm5[0]
mulpd %xmm9, %xmm10
xorpd 0xcd0f(%rip), %xmm9     # 0x16f10
mulsd %xmm5, %xmm9
mulsd %xmm2, %xmm8
addsd %xmm9, %xmm8
mulsd %xmm6, %xmm8
shufpd $0x2, %xmm13, %xmm2     # xmm2 = xmm2[0],xmm13[1]
mulpd %xmm4, %xmm2
subpd %xmm10, %xmm2
unpcklpd %xmm3, %xmm13           # xmm13 = xmm13[0],xmm3[0]
mulpd %xmm2, %xmm13
movapd %xmm13, %xmm2
subsd %xmm8, %xmm2
unpckhpd %xmm13, %xmm13          # xmm13 = xmm13[1,1]
addsd %xmm2, %xmm13
divsd %xmm7, %xmm13
movsd 0x20(%rsp), %xmm2
ucomisd %xmm13, %xmm2
jbe 0xa31a
ucomisd 0xcccf(%rip), %xmm13    # 0x16f30
jbe 0xa31a
movapd %xmm13, 0x30(%rsp)
cmpb $0x1, 0x251(%r15)
jne 0xa287
movq %rbp, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x9b1c
jmp 0xa294
movq %rbp, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x99a8
movq 0x28(%rsp), %rax
movups (%rax), %xmm0
movaps (%rsp), %xmm1
movups %xmm1, (%rax)
movaps %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
movsd 0x10(%rax), %xmm1
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x10(%rax)
movupd (%rax), %xmm0
movupd (%r12), %xmm1
mulpd %xmm0, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
movsd 0x10(%rax), %xmm1
movsd 0x10(%r12), %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
ucomisd 0xcc2d(%rip), %xmm3     # 0x16f20
movapd 0x30(%rsp), %xmm3
jbe 0xa314
movapd 0xcc0d(%rip), %xmm2     # 0x16f10
xorpd %xmm2, %xmm0
movupd %xmm0, (%rax)
xorpd %xmm2, %xmm1
movlpd %xmm1, 0x10(%rax)
movsd %xmm3, 0x20(%rsp)
movq %r13, %rdi
callq 0x3300
movq %rax, %r13
leaq 0x130(%rsp), %rax
cmpq %rax, %r13
jne 0x9d9b
leaq 0x128(%rsp), %rdi
callq 0xb1e8
movsd 0x20(%rsp), %xmm0
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc131(%rip), %rdi      # 0x16493
leaq 0xc15f(%rip), %rsi      # 0x164c8
leaq 0xcc80(%rip), %rcx      # 0x16ff0
movl $0x16d, %edx            # imm = 0x16D
callq 0x31a0
leaq 0xc5c7(%rip), %rdi      # 0x16948
leaq 0xc65e(%rip), %rsi      # 0x169e6
leaq 0xd852(%rip), %rcx      # 0x17be1
movl $0x78, %edx
callq 0x31a0
movq %rax, %rbx
jmp 0xa3b9
movq %rax, %rbx
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
cmpq $0x3, %rax
jne 0xa3ce
cmpq $0x3, 0x10(%rsp)
jne 0xa3ce
leaq 0x128(%rsp), %rdi
callq 0xb1e8
movq %rbx, %rdi
callq 0x34b0
callq 0x35cc
nop