// File: 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 %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movss %xmm0, -0x1c(%rbp)
movss %xmm1, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0x190(%rbp)
movq %rsi, %rdi
addq $0x14, %rdi
addq $0x8, %rsi
movq %rsi, -0x188(%rbp)
callq 0x11890
movq -0x190(%rbp), %rdi
movq -0x188(%rbp), %rsi
movss %xmm1, -0x40(%rbp)
movlpd %xmm0, -0x48(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
addq $0x20, %rdi
callq 0x11890
movss %xmm1, -0x60(%rbp)
movlpd %xmm0, -0x68(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0x88(%rbp)
movlpd %xmm0, -0x90(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x18ba0
movss %xmm1, -0x98(%rbp)
movlpd %xmm0, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x74(%rbp)
movl -0x98(%rbp), %eax
movl %eax, -0x6c(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x74(%rbp), %rsi
callq 0x11900
movss %xmm0, -0xa4(%rbp)
movss -0xa4(%rbp), %xmm0
callq 0x18c40
movaps %xmm0, %xmm1
movss 0x65f9b(%rip), %xmm0    # 0x7e450
ucomiss %xmm1, %xmm0
jbe 0x184c3
movb $0x0, -0x1(%rbp)
jmp 0x1880a
movss -0xa4(%rbp), %xmm1
movss 0x65d29(%rip), %xmm0    # 0x7e1fc
divss %xmm1, %xmm0
movss %xmm0, -0xa8(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf870
movq -0x190(%rbp), %rsi
movss %xmm1, -0xc8(%rbp)
movlpd %xmm0, -0xd0(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0xb8(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0xc0(%rbp)
addq $0x8, %rsi
leaq -0xc0(%rbp), %rdi
callq 0x11890
movss %xmm1, -0xd8(%rbp)
movlpd %xmm0, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0xb4(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0xac(%rbp)
leaq -0xb4(%rbp), %rdi
leaq -0x74(%rbp), %rsi
callq 0x11900
mulss -0xa8(%rbp), %xmm0
movss %xmm0, -0xe4(%rbp)
xorps %xmm0, %xmm0
ucomiss -0xe4(%rbp), %xmm0
ja 0x18594
movss -0xe4(%rbp), %xmm0
movss 0x65c6d(%rip), %xmm1    # 0x7e1fc
ucomiss %xmm1, %xmm0
jbe 0x1859d
movb $0x0, -0x1(%rbp)
jmp 0x1880a
leaq -0xb4(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x18ba0
movss %xmm1, -0xf8(%rbp)
movlpd %xmm0, -0x100(%rbp)
movl -0xf8(%rbp), %eax
movl %eax, -0xe8(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0x118(%rbp)
movlpd %xmm0, -0x120(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x110(%rbp)
movl -0x118(%rbp), %eax
movl %eax, -0x108(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x11900
mulss -0xa8(%rbp), %xmm0
movss %xmm0, -0x104(%rbp)
xorps %xmm0, %xmm0
ucomiss -0x104(%rbp), %xmm0
ja 0x18656
movss -0xe4(%rbp), %xmm0
addss -0x104(%rbp), %xmm0
movss 0x65bab(%rip), %xmm1    # 0x7e1fc
ucomiss %xmm1, %xmm0
jbe 0x1865f
movb $0x0, -0x1(%rbp)
jmp 0x1880a
leaq -0x58(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x11900
mulss -0xa8(%rbp), %xmm0
movss %xmm0, -0x124(%rbp)
xorps %xmm0, %xmm0
ucomiss -0x124(%rbp), %xmm0
jbe 0x18694
movb $0x0, -0x1(%rbp)
jmp 0x1880a
movq -0x18(%rbp), %rdi
movss -0x124(%rbp), %xmm0
callq 0xf920
movq -0x190(%rbp), %rax
movss %xmm1, -0x138(%rbp)
movlpd %xmm0, -0x140(%rbp)
movl -0x138(%rbp), %ecx
movl %ecx, -0x128(%rbp)
movq -0x140(%rbp), %rcx
movq %rcx, -0x130(%rbp)
movq -0x28(%rbp), %rcx
movl -0x128(%rbp), %edx
movl %edx, 0x14(%rcx)
movq -0x130(%rbp), %rdx
movq %rdx, 0xc(%rcx)
addq $0x2c, %rax
movq %rax, -0x198(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf8b0
movq -0x198(%rbp), %rdi
movss %xmm1, -0x158(%rbp)
movlpd %xmm0, -0x160(%rbp)
movq -0x160(%rbp), %rax
movq %rax, -0x14c(%rbp)
movl -0x158(%rbp), %eax
movl %eax, -0x144(%rbp)
leaq -0x14c(%rbp), %rsi
callq 0x11900
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x187a3
movq -0x190(%rbp), %rdi
addq $0x2c, %rdi
movss 0x65cfa(%rip), %xmm0    # 0x7e454
callq 0xfaa0
movss %xmm1, -0x178(%rbp)
movlpd %xmm0, -0x180(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x16c(%rbp)
movl -0x178(%rbp), %eax
movl %eax, -0x164(%rbp)
movq -0x28(%rbp), %rax
movq -0x16c(%rbp), %rcx
movq %rcx, 0x18(%rax)
movl -0x164(%rbp), %ecx
movl %ecx, 0x20(%rax)
jmp 0x187bc
movq -0x190(%rbp), %rcx
movq -0x28(%rbp), %rax
movq 0x2c(%rcx), %rdx
movq %rdx, 0x18(%rax)
movl 0x34(%rcx), %ecx
movl %ecx, 0x20(%rax)
movq -0x190(%rbp), %rsi
movss -0x124(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, (%rax)
movss -0xe4(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, 0x4(%rax)
movss -0x104(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, 0x8(%rax)
addq $0x38, %rsi
movq -0x28(%rbp), %rdi
addq $0x28, %rdi
callq 0xf8f0
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
nopl (%rax,%rax)