// File: Lut99[P]RayTracer/src/lib/ObjectTree.cpp
// Function: RayTracer::ObjectTree::hit(RayTracer::Ray const&, double, double, RayTracer::HitRecord&) const
bool ObjectTree::hit(const Ray& ray, double t_min, double t_max, HitRecord& record) const {
    // Do different things depending if we're optimised or not
    if (this->is_optimised) {
        // Quit if there is nothing
        if (this->root == nullptr) { return false; }

        // Simply use the tree structure
        return this->root->quick_hit(ray, t_min, t_max) && this->root->hit(ray, t_min, t_max, record);
    }

    cerr << "WARNING: ObjectTree @ " << this << " is not optimised." << endl;

    // Otherwise, do it loop-wise
    double t_best = t_max;
    bool hit = false;
    for (size_t i = 0; i < this->objects.size(); i++) {
        // Fetch the correct item from the list
        RenderObject* obj = this->objects.at(i);
        if (obj->quick_hit(ray, t_min, t_best) && obj->hit(ray, t_min, t_best, record)) {
            t_best = record.t;
            hit = true;
        }
    }

    // Return if we hit or not
    return hit;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movsd %xmm0, 0x48(%rsp)
movsd %xmm1, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsp)
testb $0x1, 0x20(%rax)
je 0x16cdec
movq 0x10(%rsp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x16cd85
movb $0x0, 0x67(%rsp)
jmp 0x16cefa
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x50(%rsp), %rsi
movsd 0x48(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
movq (%rdi), %rax
callq *0x10(%rax)
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0xf(%rsp)
jne 0x16cdb4
jmp 0x16cddd
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x50(%rsp), %rsi
movsd 0x48(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
movq 0x38(%rsp), %rdx
movq (%rdi), %rax
callq *0x18(%rax)
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
andb $0x1, %al
movb %al, 0x67(%rsp)
jmp 0x16cefa
movq 0x3f1ed(%rip), %rdi     # 0x1abfe0
leaq 0xb6b4(%rip), %rsi      # 0x1784ae
callq 0xd05b0
movq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0xd0490
movq %rax, %rdi
leaq 0xb6af(%rip), %rsi      # 0x1784c5
callq 0xd05b0
movq %rax, %rdi
movq 0x3f113(%rip), %rsi     # 0x1abf38
callq 0xd0610
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movb $0x0, 0x2f(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rax
movq %rax, (%rsp)
callq 0x15d790
movq %rax, %rcx
movq (%rsp), %rax
cmpq %rcx, %rax
jae 0x16cef0
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x15da90
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq 0x50(%rsp), %rsi
movsd 0x48(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movq (%rdi), %rax
callq *0x20(%rax)
testb $0x1, %al
jne 0x16cea0
jmp 0x16cedb
movq 0x18(%rsp), %rdi
movq 0x50(%rsp), %rsi
movsd 0x48(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movq 0x38(%rsp), %rdx
movq (%rdi), %rax
callq *0x30(%rax)
testb $0x1, %al
jne 0x16cec7
jmp 0x16cedb
movq 0x38(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movb $0x1, 0x2f(%rsp)
jmp 0x16cedd
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0x16ce44
movb 0x2f(%rsp), %al
andb $0x1, %al
movb %al, 0x67(%rsp)
movb 0x67(%rsp), %al
andb $0x1, %al
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)