// File: /workspace/github_cpp_makefile_star2_O2/alan0201tw[P]RayTracer/src/bvh.cpp
// Function: bvh_node::bvh_node(std::vector<std::shared_ptr<hitable>, std::allocator<std::shared_ptr<hitable>>> const&, float, float)
bvh_node::bvh_node(const std::vector<hitable_reference>& _list, float _t0, float _t1)
{
    list = _list;

    int list_size = (int)list.size();

    //std::cout << "list_size = " << list_size << "\n";

    int random_axis = int(3 * drand48());
    if(random_axis == 0)
    {
        // Reference : 
        // according to https://stackoverflow.com/questions/12308243/trying-to-use-qsort-with-vector
        // using std::sort with functor is the fastest, comparing to qsort and
        // std::sort with custom function as compare operator
        std::sort(list.begin(), list.end(), box_x_compare());
    }
    else if(random_axis == 1)
    {
        std::sort(list.begin(), list.end(), box_y_compare());
    }
    else
    {
        std::sort(list.begin(), list.end(), box_z_compare());
    }

    // edge cases when the bounding volume contains only one or two elements
    if(list_size == 1)
    {
        left = right = _list[0];
    }
    else if(list_size == 2)
    {
        left = _list[0];
        right = _list[1];
    }
    else
    {
        int half_size = list_size / 2;
        std::vector<hitable_reference> left_list(list.begin(), list.begin() + half_size);
        std::vector<hitable_reference> right_list(list.begin() + half_size, list.end());

        left = std::make_shared<bvh_node>(left_list, _t0, _t1);
        right = std::make_shared<bvh_node>(right_list, _t0, _t1);
    }

    aabb box_left, box_right;
    if(left->bounding_box(_t0, _t1, box_left) == false || right->bounding_box(_t0, _t1, box_right) == false)
    {
        std::cerr << "No bounding box in bvh_node constructor!\n";
    }
    box = surrounding_box(box_left, box_right);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r13
movq %rdi, %rbx
movss %xmm0, 0x38(%rsp)
movss %xmm0, 0x34(%rsp)
movss %xmm1, 0x3c(%rsp)
movss %xmm1, 0x30(%rsp)
leaq 0x3d82e(%rip), %rax     # 0x47310
movq %rax, (%rdi)
leaq 0x40(%rdi), %r14
andq $0x0, 0x50(%rdi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x40(%rdi)
movupd %xmm0, 0x8(%rdi)
movupd %xmm0, 0x18(%rdi)
movq %r14, %rdi
callq 0x74ac
movq 0x48(%rbx), %rbp
subq 0x40(%rbx), %rbp
callq 0x63e0
mulsd 0x2689a(%rip), %xmm0    # 0x303b8
cvttsd2si %xmm0, %eax
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rsi
cmpl $0x1, %eax
je 0x9b3a
testl %eax, %eax
jne 0x9b41
callq 0x9eb8
jmp 0x9b46
callq 0xa67d
jmp 0x9b46
callq 0xadd1
leaq 0x8(%rbx), %r12
leaq 0x18(%rbx), %r15
shrq $0x4, %rbp
cmpl $0x2, %ebp
je 0x9b70
cmpl $0x1, %ebp
jne 0x9b9d
movq (%r13), %rsi
movq %r15, %rdi
callq 0x772a
movq %r12, %rdi
movq %r15, %rsi
jmp 0x9b87
movq (%r13), %rsi
movq %r12, %rdi
callq 0x772a
movq (%r13), %rsi
addq $0x10, %rsi
movq %r15, %rdi
callq 0x772a
movss 0x3c(%rsp), %xmm1
movss 0x38(%rsp), %xmm0
jmp 0x9c5c
pushq $0x2
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
movq (%r14), %rsi
movslq %eax, %r13
shlq $0x4, %r13
leaq (%rsi,%r13), %rdx
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rcx
callq 0x9e62
addq 0x40(%rbx), %r13
movq 0x48(%rbx), %rdx
leaq 0x58(%rsp), %rdi
leaq 0x40(%rsp), %rcx
movq %r13, %rsi
callq 0x9e62
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x34(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x9e94
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0xb7a2
leaq 0x48(%rsp), %rdi
callq 0x7426
leaq 0x40(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x34(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x9e94
leaq 0x40(%rsp), %rsi
movq %r15, %rdi
callq 0xb7a2
leaq 0x48(%rsp), %rdi
callq 0x7426
leaq 0x58(%rsp), %rdi
callq 0x7404
leaq 0x70(%rsp), %rdi
callq 0x7404
movss 0x34(%rsp), %xmm0
movss 0x30(%rsp), %xmm1
movq (%r12), %rdi
movq (%rdi), %rax
leaq 0x70(%rsp), %rsi
callq *0x8(%rax)
testb %al, %al
je 0x9c8d
movq (%r15), %rdi
movss 0x34(%rsp), %xmm0
movss 0x30(%rsp), %xmm1
movq (%rdi), %rax
leaq 0x58(%rsp), %rsi
callq *0x8(%rax)
testb %al, %al
jne 0x9ca0
movq 0x3e34c(%rip), %rdi     # 0x47fe0
leaq 0x26735(%rip), %rsi     # 0x303d0
callq 0x6420
movq 0x68(%rsp), %rax
movq %rax, 0x28(%rsp)
movups 0x58(%rsp), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x70(%rsp), %xmm0
movupd %xmm0, (%rsp)
leaq 0x40(%rsp), %rdi
callq 0xc5d9
movq 0x50(%rsp), %rax
movq %rax, 0x38(%rbx)
movups 0x40(%rsp), %xmm0
movups %xmm0, 0x28(%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9cfc
movq %rax, %r15
leaq 0x58(%rsp), %rdi
callq 0x7404
jmp 0x9d0e
movq %rax, %r15
leaq 0x70(%rsp), %rdi
callq 0x7404
jmp 0x9d25
jmp 0x9d22
jmp 0x9d22
jmp 0x9d22
jmp 0x9d22
movq %rax, %r15
movq %r14, %rdi
callq 0x7404
leaq 0x20(%rbx), %rdi
callq 0x7426
addq $0x10, %rbx
movq %rbx, %rdi
callq 0x7426
movq %r15, %rdi
callq 0x6710