// File: ingowald[P]closestSurfacePointQueries/src/distanceQueries.cpp
// Function: rtdqComputeClosestPointsdi
void rtdqComputeClosestPointsdi(distance_query_scene scene,
                                  double   *out_closest_point_pos_x,
                                  double   *out_closest_point_pos_y,
                                  double   *out_closest_point_pos_z,
                                  size_t   out_closest_point_pos_stride,
                                  double   *out_closest_point_dist,
                                  size_t   out_closest_point_dist_stride,
                                  int32_t *out_closest_point_primID,
                                  size_t   out_closest_point_primID_stride,
                                  const double *in_query_point_x,
                                  const double *in_query_point_y,
                                  const double *in_query_point_z,
                                  const size_t in_query_point_stride,
                                  const size_t numQueryPoints)
  {
    QueryObject *qo = (QueryObject *)scene;
    if (!qo)
      return; 

#if CACHE_LAST_RESULT
    vec3f lastPoint;
#endif
    for (size_t qpi=0;qpi<numQueryPoints;qpi++) {
      const vec3fa queryPoint(in_query_point_x[qpi*in_query_point_stride],
                              in_query_point_y[qpi*in_query_point_stride],
                              in_query_point_z[qpi*in_query_point_stride]);
      
      QueryResult qr;
      qr.distance = std::numeric_limits<float>::infinity();
      qr.primID   = -1;
  
#if CACHE_LAST_RESULT
      if (qpi > 0) {
        qr.distance = length(queryPoint-lastPoint)*(((1<<22)+1)/float(1<<22));
      }
#endif
      oneQuery(qr,qo,queryPoint);
#if CACHE_LAST_RESULT
      lastPoint = qr.point;
#endif
      
      if (out_closest_point_pos_x)
        out_closest_point_pos_x[qpi*out_closest_point_pos_stride] = qr.point.x;
      if (out_closest_point_pos_y)
        out_closest_point_pos_y[qpi*out_closest_point_pos_stride] = qr.point.y;
      if (out_closest_point_pos_z)
        out_closest_point_pos_z[qpi*out_closest_point_pos_stride] = qr.point.z;
      if (out_closest_point_primID)
        out_closest_point_primID[qpi*out_closest_point_primID_stride] = qr.primID;
      if (out_closest_point_dist)
        out_closest_point_dist[qpi*out_closest_point_dist_stride] = qr.distance;
    }
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x8b94
jmp 0x8cd5
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq 0x48(%rbp), %rax
jae 0x8cd5
movq 0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq 0x40(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq 0x40(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
cvtsd2ss %xmm1, %xmm1
movq 0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq 0x40(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
cvtsd2ss %xmm2, %xmm2
leaq -0x50(%rbp), %rdi
callq 0x9300
leaq -0x68(%rbp), %rdi
callq 0x9340
callq 0x90e0
movss %xmm0, -0x58(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rsi
leaq -0x68(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x8430
cmpq $0x0, -0x10(%rbp)
je 0x8c42
movss -0x68(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq -0x28(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpq $0x0, -0x18(%rbp)
je 0x8c64
movss -0x64(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq -0x28(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpq $0x0, -0x20(%rbp)
je 0x8c86
movss -0x60(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq -0x28(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpq $0x0, 0x18(%rbp)
je 0x8ca0
movl -0x54(%rbp), %edx
movq 0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq 0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
cmpq $0x0, -0x30(%rbp)
je 0x8cc2
movss -0x58(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq 0x10(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x8cc4
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x8b9c
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)