// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r13
leaq 0x18(%rdi), %rax
leaq 0xc0(%rsp), %rbp
movq %rax, (%rbp)
movq %rdi, 0x8(%rbp)
leaq 0x1b0(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x5a62
movaps (%r15), %xmm0
leaq 0x20(%rsp), %rax
movaps %xmm0, (%rax)
movsd 0x10(%r15), %xmm0
movsd %xmm0, 0x10(%rax)
addq $0x88, %r13
movq %r12, (%rbp)
movq %r13, 0x8(%rbp)
leaq 0xc0(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0xc5d7
movq %r14, (%rbp)
movq %r13, 0x8(%rbp)
leaq 0xc0(%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0xc5d7
movq %rbx, (%rbp)
movq %r13, 0x8(%rbp)
leaq 0xc0(%rsp), %r13
movq %rbx, %rdi
movq %rbx, %rbp
movq %r13, %rsi
callq 0xc5d7
movq %r14, (%r13)
movq %r12, 0x8(%r13)
leaq 0x58(%rsp), %rdi
leaq 0xc0(%rsp), %r13
movq %r13, %rsi
callq 0x5a62
movq %rbx, (%r13)
movq %r14, %r15
movq %r14, 0x8(%r13)
leaq 0x40(%rsp), %rdi
leaq 0xc0(%rsp), %r13
movq %r13, %rsi
callq 0x5a62
movq %r12, %rbx
movq %r12, (%r13)
movq %rbp, %r14
movq %rbp, 0x8(%r13)
leaq 0x1d0(%rsp), %r13
leaq 0xc0(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x5a62
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
leaq 0x148(%rsp), %rcx
movq %rax, (%rcx)
xorps %xmm0, %xmm0
movhps 0x13b1(%rip), %xmm0     # xmm0 = xmm0[0,1],mem[0,1]
leaq 0x130(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rcx)
leaq 0x110(%rsp), %rsi
movaps %xmm0, (%rsi)
movq %rax, 0x10(%rsi)
leaq 0x58(%rsp), %rdi
movq 0x10(%rdi), %rax
movq %rax, 0x10(%rbp)
movups (%rdi), %xmm0
movaps %xmm0, (%rbp)
leaq 0x40(%rsp), %rax
movups (%rax), %xmm0
movups %xmm0, 0x18(%rbp)
movq 0x10(%rax), %rax
movq %rax, 0x28(%rbp)
movups (%r13), %xmm0
movaps %xmm0, 0x30(%rbp)
movq 0x10(%r13), %rax
movq %rax, 0x40(%rbp)
movq 0x10(%rcx), %rax
movq %rax, 0x170(%rsp)
movups (%rcx), %xmm0
movaps %xmm0, 0x160(%rsp)
movaps (%rdx), %xmm0
movups %xmm0, 0x178(%rsp)
movq 0x10(%rdx), %rax
movq %rax, 0x188(%rsp)
movapd (%rsi), %xmm0
movapd %xmm0, 0x190(%rsp)
movq 0x10(%rsi), %rax
movq %rax, 0x1a0(%rsp)
xorl %eax, %eax
movq %rsp, %r13
leaq 0xa0(%rsp), %rbp
cmpq $0x48, %rax
je 0x16e4c
movsd 0xc0(%rsp,%rax), %xmm0
movq %rax, 0x38(%rsp)
movupd 0xc8(%rsp,%rax), %xmm1
movapd %xmm1, 0x80(%rsp)
movapd %xmm0, 0x90(%rsp)
shufpd $0x1, %xmm0, %xmm1      # xmm1 = xmm1[1],xmm0[0]
movapd %xmm1, 0x70(%rsp)
xorl %r12d, %r12d
cmpq $0x48, %r12
je 0x16e3e
movq 0x170(%rsp,%r12), %rax
movq %rax, 0x10(%rsp)
movups 0x160(%rsp,%r12), %xmm0
movaps %xmm0, (%rsp)
movsd (%rsp), %xmm0
movupd 0x8(%rsp), %xmm1
movapd %xmm1, %xmm2
shufpd $0x1, %xmm0, %xmm2      # xmm2 = xmm2[1],xmm0[0]
movapd 0x80(%rsp), %xmm4
mulpd %xmm4, %xmm2
movapd 0x90(%rsp), %xmm3
mulsd %xmm1, %xmm3
mulpd 0x70(%rsp), %xmm1
subpd %xmm2, %xmm1
mulsd %xmm4, %xmm0
subsd %xmm3, %xmm0
movapd %xmm1, 0xa0(%rsp)
movsd %xmm0, 0xb0(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x52e8
subq $0x8, %rsp
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
movq %rbx, %rcx
movq %r15, %r8
movq %r14, %r9
leaq 0x118(%rsp), %rax
pushq %rax
leaq 0x140(%rsp), %rax
pushq %rax
leaq 0x160(%rsp), %rax
pushq %rax
callq 0x16f7e
addq $0x20, %rsp
addq $0x18, %r12
testb %al, %al
jne 0x16d73
jmp 0x16f4d
movq 0x38(%rsp), %rax
addq $0x18, %rax
jmp 0x16d32
subq $0x8, %rsp
leaq 0x28(%rsp), %rdx
leaq 0x150(%rsp), %r12
movq %r12, %rsi
movq %rbx, %rcx
movq %r15, %r8
movq %r14, %r9
leaq 0x118(%rsp), %rbp
pushq %rbp
leaq 0x140(%rsp), %r13
pushq %r13
pushq %r12
callq 0x16f7e
addq $0x20, %rsp
testb %al, %al
je 0x16f4d
subq $0x8, %rsp
leaq 0x28(%rsp), %rdx
movq %r13, %rsi
movq %rbx, %rcx
movq %r15, %r8
movq %r14, %r9
pushq %rbp
pushq %r13
pushq %r12
callq 0x16f7e
addq $0x20, %rsp
testb %al, %al
je 0x16f4d
subq $0x8, %rsp
leaq 0x28(%rsp), %rdx
movq %rbp, %rsi
movq %rbx, %rcx
movq %r15, %r8
movq %r14, %r9
pushq %rbp
pushq %r13
pushq %r12
callq 0x16f7e
addq $0x20, %rsp
testb %al, %al
je 0x16f4d
movsd 0x40(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movupd 0x60(%rsp), %xmm2
movupd 0x48(%rsp), %xmm3
movapd %xmm2, %xmm4
shufpd $0x1, %xmm1, %xmm4      # xmm4 = xmm4[1],xmm1[0]
mulpd %xmm3, %xmm4
mulsd %xmm3, %xmm1
shufpd $0x1, %xmm0, %xmm3      # xmm3 = xmm3[1],xmm0[0]
mulpd %xmm2, %xmm3
subpd %xmm4, %xmm3
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm1
movq %rsp, %rsi
movapd %xmm3, (%rsi)
movsd %xmm1, 0x10(%rsi)
subq $0x8, %rsp
leaq 0x28(%rsp), %rdx
movq %rbx, %rcx
movq %r15, %r8
movq %r14, %r9
pushq %rbp
pushq %r13
pushq %r12
callq 0x16f7e
addq $0x20, %rsp
jmp 0x16f4f
xorl %eax, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop