// File: /workspace/github_cpp_makefile_star2_O2/alan0201tw[P]RayTracer/src/triangle.cpp
// Function: triangle::hit(ray const&, float, float, hit_record&) const
bool triangle::hit(const ray& _ray, float t_min, float t_max, hit_record& rec) const
{
    // Reference : 
    // https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/moller-trumbore-ray-triangle-intersection
    // https://github.com/netolcc06/BabyRayTracer/blob/master/Triangle.hpp
    vec3 v0v1 = v1 - v0;
    vec3 v0v2 = v2 - v0;
    vec3 pvec = cross(_ray.direction(), v0v2);
    float det = dot(v0v1, pvec);

    // culling
    //if(det < kEpsilon) return false;
    // no culling
    if (fabs(det) < kEpsilon) return false; 

    float invDet = 1.0f / det;

    vec3 tvec = _ray.origin() - v0;
    float u = dot(tvec, pvec) * invDet;
    if(u < 0 || u > 1) return false;

    vec3 qvec = cross(tvec, v0v1);
    float v = dot(_ray.direction(), qvec) * invDet;
    if(v < 0 || u + v > 1) return false;

    float t = dot(v0v2, qvec) * invDet;
    if (t < 0) return false;

    // fill in collision info
    rec.hit_point = _ray.point_at_parameter(t);
    if(dot(normal, _ray.direction()) > 0.0f)
        rec.normal = -1.0f * normal;
    else
        rec.normal = normal;
    rec.t = t;
    rec.u = u;
    rec.v = v;
    rec.material_ref = triangle_mat;

    return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
addq $0x14, %rdi
leaq 0x8(%r14), %r12
movq %r12, %rsi
callq 0x9449
movaps %xmm0, (%rsp)
movaps %xmm1, 0x10(%rsp)
movlps %xmm0, 0x58(%rsp)
movss %xmm1, 0x60(%rsp)
leaq 0x20(%r14), %rdi
movq %r12, %rsi
callq 0x9449
leaq 0x30(%rsp), %rsi
movlps %xmm0, (%rsi)
movss %xmm1, 0x8(%rsi)
movsd 0xc(%r15), %xmm0
movss 0x14(%r15), %xmm1
leaq 0x20(%rsp), %rdi
movsd %xmm0, (%rdi)
movss %xmm1, 0x8(%rdi)
callq 0xbbdb
movaps 0x10(%rsp), %xmm4
movaps %xmm0, %xmm2
movaps (%rsp), %xmm3
mulps %xmm3, %xmm2
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
mulss %xmm0, %xmm3
addss %xmm2, %xmm3
mulss %xmm1, %xmm4
addss %xmm3, %xmm4
movaps 0x24b5e(%rip), %xmm3    # 0x30480
andps %xmm4, %xmm3
movss 0x24b63(%rip), %xmm2    # 0x30490
ucomiss %xmm3, %xmm2
jbe 0xb940
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movss 0x248bc(%rip), %xmm2    # 0x30204
divss %xmm4, %xmm2
movss %xmm2, 0x10(%rsp)
movsd (%r15), %xmm3
movss 0x8(%r15), %xmm2
leaq 0x68(%rsp), %rdi
movsd %xmm3, (%rdi)
movss %xmm2, 0x8(%rdi)
movq %r12, %rsi
movss %xmm1, (%rsp)
movaps %xmm0, 0x40(%rsp)
callq 0x9449
movss 0x10(%rsp), %xmm4
movss (%rsp), %xmm3
movlps %xmm0, 0x20(%rsp)
movss %xmm1, 0x28(%rsp)
movaps 0x40(%rsp), %xmm5
movaps %xmm5, %xmm2
mulps %xmm0, %xmm2
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
mulss %xmm0, %xmm5
addss %xmm2, %xmm5
mulss %xmm1, %xmm3
addss %xmm5, %xmm3
mulss %xmm4, %xmm3
xorl %eax, %eax
xorps %xmm0, %xmm0
ucomiss %xmm3, %xmm0
ja 0xb934
ucomiss 0x24839(%rip), %xmm3    # 0x30204
ja 0xb934
movss %xmm3, (%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xbbdb
movss 0x10(%rsp), %xmm4
movsd 0xc(%r15), %xmm2
movaps %xmm2, %xmm3
mulps %xmm0, %xmm3
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
mulss %xmm0, %xmm2
addss %xmm3, %xmm2
movss 0x14(%r15), %xmm5
mulss %xmm1, %xmm5
addss %xmm2, %xmm5
mulss %xmm4, %xmm5
xorl %eax, %eax
xorps %xmm2, %xmm2
ucomiss %xmm5, %xmm2
ja 0xb934
movss (%rsp), %xmm2
addss %xmm5, %xmm2
ucomiss 0x247d1(%rip), %xmm2    # 0x30204
ja 0xb934
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
mulss 0x34(%rsp), %xmm2
movss 0x30(%rsp), %xmm3
mulss %xmm0, %xmm3
addss %xmm2, %xmm3
mulss 0x38(%rsp), %xmm1
addss %xmm3, %xmm1
mulss %xmm1, %xmm4
xorps %xmm0, %xmm0
ucomiss %xmm4, %xmm0
ja 0xb932
movss %xmm5, 0x40(%rsp)
movq %r15, %rdi
movss %xmm4, 0x10(%rsp)
movaps %xmm4, %xmm0
callq 0x8924
movlps %xmm0, 0xc(%rbx)
movss %xmm1, 0x14(%rbx)
movsd 0xc(%r15), %xmm0
movss 0x2c(%r14), %xmm1
mulss %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulss 0x30(%r14), %xmm0
leaq 0x2c(%r14), %rdi
movss 0x34(%r14), %xmm2
mulss 0x14(%r15), %xmm2
addss %xmm0, %xmm1
addss %xmm1, %xmm2
ucomiss 0x249cd(%rip), %xmm2    # 0x30494
jbe 0xbae1
movss 0x2487f(%rip), %xmm0    # 0x30350
callq 0x8980
movlps %xmm0, 0x18(%rbx)
movss %xmm1, 0x20(%rbx)
jmp 0xbaee
movl 0x8(%rdi), %eax
movl %eax, 0x20(%rbx)
movq (%rdi), %rax
movq %rax, 0x18(%rbx)
movss (%rsp), %xmm0
movss 0x40(%rsp), %xmm2
movss 0x10(%rsp), %xmm1
movss %xmm1, (%rbx)
movss %xmm0, 0x4(%rbx)
movss %xmm2, 0x8(%rbx)
addq $0x38, %r14
addq $0x28, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x894e
movb $0x1, %al
jmp 0xb934
nop