// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movupd (%rdi), %xmm0
movupd 0x18(%rdi), %xmm1
movsd 0x28(%rdi), %xmm2
subsd 0x10(%rdi), %xmm2
subpd %xmm0, %xmm1
leaq 0x20(%rsp), %rax
movapd %xmm1, (%rax)
movsd %xmm2, 0x10(%rax)
movupd (%rsi), %xmm0
movupd 0x88(%rdi), %xmm1
subpd %xmm1, %xmm0
movupd %xmm0, (%rsi)
movsd 0x10(%rsi), %xmm0
subsd 0x98(%rdi), %xmm0
movsd %xmm0, 0x10(%rsi)
movupd (%rdx), %xmm0
movupd 0x88(%rdi), %xmm1
subpd %xmm1, %xmm0
movupd %xmm0, (%rdx)
movsd 0x10(%rdx), %xmm0
subsd 0x98(%rdi), %xmm0
movsd %xmm0, 0x10(%rdx)
movupd (%rcx), %xmm2
movupd 0x88(%rdi), %xmm0
subpd %xmm0, %xmm2
movupd %xmm2, (%rcx)
movsd 0x10(%rcx), %xmm3
subsd 0x98(%rdi), %xmm3
movsd %xmm3, 0x10(%rcx)
movupd (%rdx), %xmm5
movupd (%rsi), %xmm0
movsd 0x10(%rsi), %xmm1
movapd %xmm2, %xmm6
subpd %xmm5, %xmm6
subpd %xmm0, %xmm5
movsd 0x10(%rdx), %xmm4
movapd %xmm3, %xmm7
unpcklpd %xmm4, %xmm7            # xmm7 = xmm7[0],xmm4[0]
unpcklpd %xmm1, %xmm4            # xmm4 = xmm4[0],xmm1[0]
subpd %xmm4, %xmm7
subpd %xmm2, %xmm0
subsd %xmm3, %xmm1
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
leaq 0x190(%rsp), %rdi
movq %rax, (%rdi)
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
movhpd 0x107c(%rip), %xmm3     # xmm3 = xmm3[0],mem[0]
movupd %xmm2, 0x8(%rdi)
leaq 0x170(%rsp), %rbp
movapd %xmm3, (%rbp)
xorl %edx, %edx
movq %rdx, 0x10(%rbp)
leaq 0x150(%rsp), %r10
movapd %xmm2, (%r10)
movq %rax, 0x10(%r10)
movapd %xmm5, 0x70(%rsp)
movapd %xmm5, 0x100(%rsp)
movhpd %xmm7, 0x110(%rsp)
movapd %xmm6, 0x60(%rsp)
movupd %xmm6, 0x118(%rsp)
movapd %xmm7, 0x50(%rsp)
movlpd %xmm7, 0x128(%rsp)
movapd %xmm0, 0x130(%rsp)
movsd %xmm1, 0x140(%rsp)
movq 0x10(%rdi), %rcx
movq %rcx, 0xc0(%rsp)
movups (%rdi), %xmm0
movaps %xmm0, 0xb0(%rsp)
movapd (%rbp), %xmm0
movupd %xmm0, 0xc8(%rsp)
movq %rdx, 0xd8(%rsp)
movapd %xmm2, 0xe0(%rsp)
movq %rax, 0xf0(%rsp)
movq %rsi, 0x48(%rsp)
movsd 0x100(%rsp,%rdx), %xmm0
movq %rdx, 0x40(%rsp)
movups 0x108(%rsp,%rdx), %xmm1
movaps %xmm1, 0x90(%rsp)
movaps %xmm0, 0xa0(%rsp)
shufps $0x4e, %xmm0, %xmm1     # xmm1 = xmm1[2,3],xmm0[0,1]
movaps %xmm1, 0x80(%rsp)
xorl %r12d, %r12d
movups 0xb0(%rsp,%r12), %xmm0
movaps %xmm0, (%rsp)
movq 0xc0(%rsp,%r12), %rax
movq %rax, 0x10(%rsp)
movsd (%rsp), %xmm0
movupd 0x8(%rsp), %xmm1
movapd %xmm1, %xmm2
shufpd $0x1, %xmm0, %xmm2      # xmm2 = xmm2[1],xmm0[0]
movapd 0x90(%rsp), %xmm4
mulpd %xmm4, %xmm2
movapd 0xa0(%rsp), %xmm3
mulsd %xmm1, %xmm3
mulpd 0x80(%rsp), %xmm1
subpd %xmm2, %xmm1
mulsd %xmm4, %xmm0
subsd %xmm3, %xmm0
movapd %xmm1, (%rsp)
movsd %xmm0, 0x10(%rsp)
subq $0x8, %rsp
leaq 0x8(%rsp), %rsi
leaq 0x28(%rsp), %rdx
movq %r15, %rcx
movq %r14, %r15
movq %r14, %r8
movq %rbx, %r14
movq %rbx, %r9
movq %r10, %r13
pushq %r10
movq %rbp, %rbx
pushq %rbp
movq %rdi, %rbp
pushq %rdi
callq 0x152a2
addq $0x20, %rsp
testb %al, %al
je 0x15272
addq $0x18, %r12
cmpq $0x48, %r12
movq %rbp, %rdi
movq %rbx, %rbp
movq %r14, %rbx
movq %r15, %r14
movq 0x48(%rsp), %r15
movq %r13, %r10
jne 0x1509d
movq 0x40(%rsp), %rdx
addq $0x18, %rdx
cmpq $0x48, %rdx
jne 0x15068
subq $0x8, %rsp
leaq 0x28(%rsp), %rdx
movq %rdi, %rsi
movq %r15, %rcx
movq %r14, %r8
movq %rbx, %r9
pushq %r10
pushq %rbp
pushq %rdi
movq %rdi, %r12
movq %r10, %r13
callq 0x152a2
addq $0x20, %rsp
testb %al, %al
je 0x15272
subq $0x8, %rsp
leaq 0x28(%rsp), %rdx
movq %rbp, %rsi
movq %r15, %rcx
movq %r14, %r8
movq %rbx, %r9
pushq %r13
pushq %rbp
pushq %r12
callq 0x152a2
addq $0x20, %rsp
testb %al, %al
je 0x15272
subq $0x8, %rsp
leaq 0x28(%rsp), %rdx
movq %r13, %rsi
movq %r15, %rcx
movq %r14, %r8
movq %rbx, %r9
pushq %r13
pushq %rbp
pushq %r12
callq 0x152a2
addq $0x20, %rsp
testb %al, %al
je 0x15272
movapd 0x70(%rsp), %xmm2
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
movapd 0x60(%rsp), %xmm3
movapd %xmm3, %xmm1
unpckhpd %xmm3, %xmm1            # xmm1 = xmm1[1],xmm3[1]
mulsd %xmm3, %xmm0
mulsd %xmm2, %xmm1
subsd %xmm0, %xmm1
movapd 0x50(%rsp), %xmm4
movapd %xmm4, %xmm0
shufpd $0x1, %xmm2, %xmm0      # xmm0 = xmm0[1],xmm2[0]
shufpd $0x1, %xmm3, %xmm2      # xmm2 = xmm2[1],xmm3[0]
shufpd $0x1, %xmm4, %xmm3      # xmm3 = xmm3[1],xmm4[0]
mulpd %xmm0, %xmm3
mulpd %xmm4, %xmm2
subpd %xmm3, %xmm2
movq %rsp, %rsi
movapd %xmm2, (%rsi)
movsd %xmm1, 0x10(%rsi)
subq $0x8, %rsp
leaq 0x28(%rsp), %rdx
movq %r15, %rcx
movq %r14, %r8
movq %rbx, %r9
pushq %r13
pushq %rbp
pushq %r12
callq 0x152a2
addq $0x20, %rsp
jmp 0x15274
xorl %eax, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq