// File: IsaacLaw12[P]RayTracer/./src/OctTree.cpp
// Function: OctTree::triangle_intersects(Eigen::Matrix<double, 3, 1, 0, 3, 1>&, Eigen::Matrix<double, 3, 1, 0, 3, 1>&, Eigen::Matrix<double, 3, 1, 0, 3, 1>&)
bool OctTree::triangle_intersects(Eigen::Vector3d& v0, Eigen::Vector3d& v1, Eigen::Vector3d& v2) {
    // Test if this AABB intersects with the specified triangle
    Eigen::Vector3d dims = max_corner - min_corner;
    Eigen::Vector3d boxHalfSize(dims[0], dims[1], dims[2]);
    v0 = v0 - center;
    v1 = v1 - center;
    v2 = v2 - center;
    Eigen::Vector3d e0 = v1 - v0;
    Eigen::Vector3d e1 = v2 - v1;
    Eigen::Vector3d e2 = v0 - v2;

    Eigen::Vector3d u0 = Eigen::Vector3d(1.0, 0.0, 0.0);
    Eigen::Vector3d u1 = Eigen::Vector3d(0.0, 1.0, 0.0);
    Eigen::Vector3d u2 = Eigen::Vector3d(0.0, 0.0, 1.0);

    Eigen::Vector3d edges[] = {e0, e1, e2};
    Eigen::Vector3d axes[] = {u0, u1, u2};

    Eigen::Vector3d axis;
    for (auto edge:edges){
        for (auto axis:axes){
            axis = axis.cross(edge);
            if (!axis_test(axis, boxHalfSize, v0, v1, v2, u0, u1, u2)) return false;
        }
    }

    if (!axis_test(u0, boxHalfSize, v0, v1, v2, u0, u1, u2)) return false;
    if (!axis_test(u1, boxHalfSize, v0, v1, v2, u0, u1, u2)) return false;
    if (!axis_test(u2, boxHalfSize, v0, v1, v2, u0, u1, u2)) return false;

    Eigen::Vector3d normal = e0.cross(e1);
    if (!axis_test(normal, boxHalfSize, v0, v1, v2, u0, u1, u2)) return false;

    return true;
}

// Assembly:
subq $0x358, %rsp            # imm = 0x358
movq %rdi, 0x348(%rsp)
movq %rsi, 0x340(%rsp)
movq %rdx, 0x338(%rsp)
movq %rcx, 0x330(%rsp)
movq 0x348(%rsp), %rdx
movq %rdx, 0x30(%rsp)
movq %rdx, %rsi
addq $0x18, %rsi
leaq 0x300(%rsp), %rdi
callq 0x9e80
leaq 0x318(%rsp), %rdi
leaq 0x300(%rsp), %rsi
callq 0xdcc0
leaq 0x318(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x459c0
movq %rax, 0x20(%rsp)
leaq 0x318(%rsp), %rdi
movl $0x1, %esi
callq 0x459c0
movq %rax, 0x28(%rsp)
leaq 0x318(%rsp), %rdi
movl $0x2, %esi
callq 0x459c0
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %rax, %rcx
leaq 0x2e8(%rsp), %rdi
callq 0x9c70
movq 0x30(%rsp), %rdx
movq 0x340(%rsp), %rsi
addq $0x88, %rdx
leaq 0x2d0(%rsp), %rdi
callq 0x9e80
movq 0x340(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
callq 0x1e190
movq 0x30(%rsp), %rdx
movq 0x338(%rsp), %rsi
addq $0x88, %rdx
leaq 0x2b8(%rsp), %rdi
callq 0x9e80
movq 0x338(%rsp), %rdi
leaq 0x2b8(%rsp), %rsi
callq 0x1e190
movq 0x30(%rsp), %rdx
movq 0x330(%rsp), %rsi
addq $0x88, %rdx
leaq 0x2a0(%rsp), %rdi
callq 0x9e80
movq 0x330(%rsp), %rdi
leaq 0x2a0(%rsp), %rsi
callq 0x1e190
movq 0x338(%rsp), %rsi
movq 0x340(%rsp), %rdx
leaq 0x270(%rsp), %rdi
callq 0x9e80
leaq 0x288(%rsp), %rdi
leaq 0x270(%rsp), %rsi
callq 0xdcc0
movq 0x330(%rsp), %rsi
movq 0x338(%rsp), %rdx
leaq 0x240(%rsp), %rdi
callq 0x9e80
leaq 0x258(%rsp), %rdi
leaq 0x240(%rsp), %rsi
callq 0xdcc0
movq 0x340(%rsp), %rsi
movq 0x330(%rsp), %rdx
leaq 0x210(%rsp), %rdi
callq 0x9e80
leaq 0x228(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0xdcc0
movsd 0x52b2(%rip), %xmm0     # 0x5d008
movsd %xmm0, 0x1f0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x1e8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x1e0(%rsp)
leaq 0x1f8(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
leaq 0x1e8(%rsp), %rdx
leaq 0x1e0(%rsp), %rcx
callq 0x9c70
xorps %xmm0, %xmm0
movsd %xmm0, 0x1c0(%rsp)
movsd 0x5258(%rip), %xmm0     # 0x5d008
movsd %xmm0, 0x1b8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x1b0(%rsp)
leaq 0x1c8(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
leaq 0x1b8(%rsp), %rdx
leaq 0x1b0(%rsp), %rcx
callq 0x9c70
xorps %xmm0, %xmm0
movsd %xmm0, 0x190(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x188(%rsp)
movsd 0x51fe(%rip), %xmm0     # 0x5d008
movsd %xmm0, 0x180(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0x190(%rsp), %rsi
leaq 0x188(%rsp), %rdx
leaq 0x180(%rsp), %rcx
callq 0x9c70
leaq 0x130(%rsp), %rdi
leaq 0x288(%rsp), %rsi
callq 0xa4f0
leaq 0x130(%rsp), %rdi
addq $0x18, %rdi
leaq 0x258(%rsp), %rsi
callq 0xa4f0
leaq 0x130(%rsp), %rdi
addq $0x30, %rdi
leaq 0x228(%rsp), %rsi
callq 0xa4f0
leaq 0xe0(%rsp), %rdi
leaq 0x1f8(%rsp), %rsi
callq 0xa4f0
leaq 0xe0(%rsp), %rdi
addq $0x18, %rdi
leaq 0x1c8(%rsp), %rsi
callq 0xa4f0
leaq 0xe0(%rsp), %rdi
addq $0x30, %rdi
leaq 0x198(%rsp), %rsi
callq 0xa4f0
leaq 0xc8(%rsp), %rdi
callq 0x11e20
leaq 0x130(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xc0(%rsp), %rax
addq $0x48, %rax
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rax
cmpq 0xb0(%rsp), %rax
je 0x58050
movq 0xb8(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0xa4f0
leaq 0xe0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x90(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rax
cmpq 0x80(%rsp), %rax
je 0x58035
movq 0x88(%rsp), %rsi
leaq 0x68(%rsp), %rdi
callq 0xa4f0
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x98(%rsp), %rdx
callq 0xa000
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x9d10
movq 0x30(%rsp), %rdi
movq 0x340(%rsp), %rcx
movq 0x338(%rsp), %r8
movq 0x330(%rsp), %r9
leaq 0x68(%rsp), %rsi
leaq 0x2e8(%rsp), %rdx
leaq 0x1f8(%rsp), %r11
leaq 0x1c8(%rsp), %r10
leaq 0x198(%rsp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x582a0
testb $0x1, %al
jne 0x5801a
movb $0x0, 0x357(%rsp)
jmp 0x58201
jmp 0x5801c
movq 0x88(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x88(%rsp)
jmp 0x57f66
jmp 0x58037
movq 0xb8(%rsp), %rax
addq $0x18, %rax
movq %rax, 0xb8(%rsp)
jmp 0x57f07
movq 0x30(%rsp), %rdi
movq 0x340(%rsp), %rcx
movq 0x338(%rsp), %r8
movq 0x330(%rsp), %r9
leaq 0x1f8(%rsp), %r11
leaq 0x2e8(%rsp), %rdx
leaq 0x1c8(%rsp), %r10
leaq 0x198(%rsp), %rax
movq %r11, %rsi
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x582a0
testb $0x1, %al
jne 0x580b4
movb $0x0, 0x357(%rsp)
jmp 0x58201
movq 0x30(%rsp), %rdi
movq 0x340(%rsp), %rcx
movq 0x338(%rsp), %r8
movq 0x330(%rsp), %r9
leaq 0x1c8(%rsp), %r10
leaq 0x2e8(%rsp), %rdx
leaq 0x1f8(%rsp), %r11
leaq 0x198(%rsp), %rax
movq %r10, %rsi
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x582a0
testb $0x1, %al
jne 0x58118
movb $0x0, 0x357(%rsp)
jmp 0x58201
movq 0x30(%rsp), %rdi
movq 0x340(%rsp), %rcx
movq 0x338(%rsp), %r8
movq 0x330(%rsp), %r9
leaq 0x198(%rsp), %rax
leaq 0x2e8(%rsp), %rdx
leaq 0x1f8(%rsp), %r11
leaq 0x1c8(%rsp), %r10
movq %rax, %rsi
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x582a0
testb $0x1, %al
jne 0x5817c
movb $0x0, 0x357(%rsp)
jmp 0x58201
leaq 0x38(%rsp), %rdi
leaq 0x288(%rsp), %rsi
leaq 0x258(%rsp), %rdx
callq 0xa000
movq 0x30(%rsp), %rdi
movq 0x340(%rsp), %rcx
movq 0x338(%rsp), %r8
movq 0x330(%rsp), %r9
leaq 0x38(%rsp), %rsi
leaq 0x2e8(%rsp), %rdx
leaq 0x1f8(%rsp), %r11
leaq 0x1c8(%rsp), %r10
leaq 0x198(%rsp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x582a0
testb $0x1, %al
jne 0x581f9
movb $0x0, 0x357(%rsp)
jmp 0x58201
movb $0x1, 0x357(%rsp)
movb 0x357(%rsp), %al
andb $0x1, %al
addq $0x358, %rsp            # imm = 0x358
retq
nopw %cs:(%rax,%rax)