// File: ingowald[P]closestSurfacePointQueries/src/distanceQueries.cpp
// Function: bvhlib::oneQuery(bvhlib::QueryResult&, bvhlib::QueryObject*, ospcommon::vec_t<float, 3, 1> const&)
void oneQuery(QueryResult &result,
                QueryObject *qo,
                const vec3fa &point)
  {
    std::priority_queue<std::pair<float,const BVH::Node *>,
                        std::vector<std::pair<float,const BVH::Node *>>,
                        std::greater<std::pair<float,const BVH::Node *>>
                        > traversalQueue;
    
    // push sentinel, so we never have to check if the queue runs dry.
    traversalQueue.push(std::pair<float,const BVH::Node *>
                        (std::numeric_limits<float>::infinity(),NULL));
    
    const std::vector<BVH::Node> &nodeList = qo->bvh.nodeList;
    const BVH::Node *node = &nodeList[0];
    while (node) {
      if (!node->isLeaf) {
        // this is a inner node ...
        const BVH::Node *const child0 = &nodeList[node->child+0];
        const BVH::Node *const child1 = &nodeList[node->child+1];
        const float dist0 = computeDistance(child0,point);
        const float dist1 = computeDistance(child1,point);

        /* fast path: check if closer of the two children is already
           as good as anything the queue has to offer - because if so,
           we can save the pushing and popping of this node, and set
           to it right away */
        if (dist0 < dist1) {
          if (dist0 <= std::min(traversalQueue.top().first,result.distance)) {
            node = child0;
            if (dist1 < result.distance)
              traversalQueue.push(std::pair<float,const BVH::Node*>(dist1,child1));
            continue;
          }
        } else {
          if (dist1 <= std::min(traversalQueue.top().first,result.distance)) {
            node = child1;
            if (dist0 < result.distance)
              traversalQueue.push(std::pair<float,const BVH::Node*>(dist0,child0));
            continue;
          }
        }

        /* fast-path optimization didn't apply - push both children
           (if they can't be culled, of course), and let the queue
           provide the next node */
        if (dist0 < result.distance)
          traversalQueue.push(std::pair<float,const BVH::Node*>(dist0,child0));
        if (dist1 < result.distance)
          traversalQueue.push(std::pair<float,const BVH::Node*>(dist1,child1));
      } else {
        qo->testPrimAndUpdateIfCloser(result,point,node->child);
      }


      /* need to get a next node to traverse, from the trv queue: */
      
      /* note - _not_ checking for empty, since we know to have a sentinel, anyway */
      // if (traversalQueue.empty()) break;
      
      // closest candidate is already too far?
      if (traversalQueue.top().first >= result.distance) break;

      // closest candidate might be closer: pop it and use it
      node = traversalQueue.top().second;


      if (node == NULL) {
        PING;
        PRINT(node);
        PRINT(result.distance);
        PRINT(traversalQueue.top().first);
      }

      traversalQueue.pop();
    }
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x9060
callq 0x90e0
movss %xmm0, -0x4c(%rbp)
movq $0x0, -0x58(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x90f0
jmp 0x8475
leaq -0x38(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x9090
jmp 0x8484
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9120
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0x899a
movq -0x78(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x8755
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, %esi
callq 0x9120
movq %rax, -0x80(%rbp)
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rax
movl 0x1c(%rax), %eax
incl %eax
movl %eax, %esi
callq 0x9120
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x9140
movss %xmm0, -0xd4(%rbp)
jmp 0x8502
movss -0xd4(%rbp), %xmm0
movss %xmm0, -0x8c(%rbp)
movq -0x88(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x9140
movss %xmm0, -0xd8(%rbp)
jmp 0x852c
movss -0xd8(%rbp), %xmm0
movss %xmm0, -0x90(%rbp)
movss -0x8c(%rbp), %xmm1
movss -0x90(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x861e
movss -0x8c(%rbp), %xmm0
movss %xmm0, -0xe4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x9240
movq %rax, -0xe0(%rbp)
jmp 0x8577
movq -0xe0(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x10, %rsi
callq 0x9200
movq %rax, -0xf0(%rbp)
jmp 0x8594
movss -0xe4(%rbp), %xmm1
movq -0xf0(%rbp), %rax
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
jb 0x8619
movq -0x80(%rbp), %rax
movq %rax, -0x78(%rbp)
movss -0x90(%rbp), %xmm1
movq -0x8(%rbp), %rax
movss 0x10(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x8614
leaq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0x88(%rbp), %rdx
callq 0x9260
jmp 0x85e6
leaq -0x38(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x9090
jmp 0x85f8
jmp 0x8614
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x92e0
jmp 0x89ac
jmp 0x84a1
jmp 0x86ca
movss -0x90(%rbp), %xmm0
movss %xmm0, -0xfc(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x9240
movq %rax, -0xf8(%rbp)
jmp 0x8640
movq -0xf8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x10, %rsi
callq 0x9200
movq %rax, -0x108(%rbp)
jmp 0x865d
movss -0xfc(%rbp), %xmm1
movq -0x108(%rbp), %rax
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
jb 0x86c8
movq -0x88(%rbp), %rax
movq %rax, -0x78(%rbp)
movss -0x8c(%rbp), %xmm1
movq -0x8(%rbp), %rax
movss 0x10(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x86c3
leaq -0xb0(%rbp), %rdi
leaq -0x8c(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x9260
jmp 0x86af
leaq -0x38(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x9090
jmp 0x86c1
jmp 0x86c3
jmp 0x84a1
jmp 0x86ca
movss -0x8c(%rbp), %xmm1
movq -0x8(%rbp), %rax
movss 0x10(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x870d
leaq -0xc0(%rbp), %rdi
leaq -0x8c(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x9260
jmp 0x86f9
leaq -0x38(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x9090
jmp 0x870b
jmp 0x870d
movss -0x90(%rbp), %xmm1
movq -0x8(%rbp), %rax
movss 0x10(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x8753
leaq -0xd0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0x88(%rbp), %rdx
callq 0x9260
jmp 0x873f
leaq -0x38(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x9090
jmp 0x8751
jmp 0x8753
jmp 0x8777
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x78(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, %ecx
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
jmp 0x8775
jmp 0x8777
leaq -0x38(%rbp), %rdi
callq 0x9240
movq %rax, -0x110(%rbp)
jmp 0x8789
movq -0x110(%rbp), %rax
movss (%rax), %xmm0
movq -0x8(%rbp), %rax
ucomiss 0x10(%rax), %xmm0
jb 0x87a3
jmp 0x899a
leaq -0x38(%rbp), %rdi
callq 0x9240
movq %rax, -0x118(%rbp)
jmp 0x87b5
movq -0x118(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x898a
movq 0xf7fa(%rip), %rdi      # 0x17fd0
leaq 0x8e63(%rip), %rsi      # 0x11640
callq 0x3260
movq %rax, -0x120(%rbp)
jmp 0x87eb
movq -0x120(%rbp), %rdi
leaq 0x8b27(%rip), %rsi      # 0x11320
callq 0x3260
movq %rax, -0x128(%rbp)
jmp 0x8807
movq -0x128(%rbp), %rdi
leaq 0x8e49(%rip), %rsi      # 0x1165e
callq 0x3260
movq %rax, -0x130(%rbp)
jmp 0x8823
movq -0x130(%rbp), %rdi
movq 0xf787(%rip), %rsi      # 0x17fb8
callq 0x32a0
jmp 0x8838
movq 0xf791(%rip), %rdi      # 0x17fd0
leaq 0x8e5c(%rip), %rsi      # 0x116a2
callq 0x3260
movq %rax, -0x138(%rbp)
jmp 0x8854
movq -0x138(%rbp), %rdi
leaq 0x8e45(%rip), %rsi      # 0x116a7
callq 0x3260
movq %rax, -0x140(%rbp)
jmp 0x8870
movq -0x140(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x3210
movq %rax, -0x148(%rbp)
jmp 0x8889
movq -0x148(%rbp), %rdi
movq 0xf721(%rip), %rsi      # 0x17fb8
callq 0x32a0
jmp 0x889e
movq 0xf72b(%rip), %rdi      # 0x17fd0
leaq 0x8dff(%rip), %rsi      # 0x116ab
callq 0x3260
movq %rax, -0x150(%rbp)
jmp 0x88ba
movq -0x150(%rbp), %rdi
leaq 0x8ddf(%rip), %rsi      # 0x116a7
callq 0x3260
movq %rax, -0x158(%rbp)
jmp 0x88d6
movq -0x158(%rbp), %rdi
movq -0x8(%rbp), %rax
movss 0x10(%rax), %xmm0
callq 0x31e0
movq %rax, -0x160(%rbp)
jmp 0x88f4
movq -0x160(%rbp), %rdi
movq 0xf6b6(%rip), %rsi      # 0x17fb8
callq 0x32a0
jmp 0x8909
movq 0xf6c0(%rip), %rdi      # 0x17fd0
leaq 0x8da4(%rip), %rsi      # 0x116bb
callq 0x3260
movq %rax, -0x168(%rbp)
jmp 0x8925
movq -0x168(%rbp), %rdi
leaq 0x8d74(%rip), %rsi      # 0x116a7
callq 0x3260
movq %rax, -0x170(%rbp)
jmp 0x8941
leaq -0x38(%rbp), %rdi
callq 0x9240
movq %rax, -0x178(%rbp)
jmp 0x8953
movq -0x170(%rbp), %rdi
movq -0x178(%rbp), %rax
movss (%rax), %xmm0
callq 0x31e0
movq %rax, -0x180(%rbp)
jmp 0x8973
movq -0x180(%rbp), %rdi
movq 0xf637(%rip), %rsi      # 0x17fb8
callq 0x32a0
jmp 0x8988
jmp 0x898a
leaq -0x38(%rbp), %rdi
callq 0x9290
jmp 0x8995
jmp 0x84a1
leaq -0x38(%rbp), %rdi
callq 0x92e0
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x33e0
nopw %cs:(%rax,%rax)