// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelScene.cpp
// Function: gaps::R3SurfelScene::FindImageByBestView(gaps::R3Point const&, gaps::R3Vector const&)
R3SurfelImage *R3SurfelScene::
FindImageByBestView(const R3Point& query_position, const R3Vector& query_normal)
{
  // Search for image with best view of a point
  RNScalar best_score = 0;
  R3SurfelImage *best_image = NULL;
  for (int i = 0; i < NImages(); i++) {
    R3SurfelImage *image = Image(i);

    // Get/check point depth
    R3Point camera_position = image->TransformFromWorldToCamera(query_position);
    RNScalar point_depth = -camera_position.Z();
    if (RNIsNegativeOrZero(point_depth)) continue;

    // Get/check projection into image
    R2Point image_position = image->TransformFromCameraToImage(camera_position);
    int ix = (int) (image_position.X() + 0.5);
    if ((ix < 0) || (ix >= image->ImageWidth())) continue;
    int iy = (int) (image_position.Y() + 0.5);
    if ((iy < 0) || (iy >= image->ImageHeight())) continue;

#if 0
    // Get/check image depth
    const R2Grid *depth_channel = image->DepthChannel();
    if (depth_channel) {
      RNScalar image_depth = depth_channel->GridValue(ix, iy);
      if (image_depth != R2_GRID_UNKNOWN_VALUE) {
        RNScalar delta_depth = fabs(image_depth - point_depth);
        if (delta_depth > 0.1 * point_depth) continue;
      }
    }
#endif
   
#if 0
    // Get/check NdotV
    RNScalar NdotV = 1.0;
    if (!query_normal.IsZero()) {
      R3Vector V = image->Viewpoint() - query_position;
      V.Normalize();
      RNScalar NdotV = query_normal.Dot(V);
      if (RNIsNegativeOrZero(NdotV)) NdotV = continue;
    }
#endif
   
    // Get image centrality
    double dx = fabs(ix - 0.5*image->ImageWidth()) / (0.5*image->ImageWidth());
    double dy = fabs(iy - 0.5*image->ImageHeight()) / (0.5*image->ImageHeight());
    double image_centrality = (1.0 - dx) * (1.0 - dy);

    // Get distance
    RNScalar dd = R3SquaredDistance(image->Viewpoint(), query_position);

    // Get/check score
    RNScalar score = image_centrality / dd;
    if (score > best_score) {
      best_image = image;
      best_score = score;
    }
  }

  // Return best image
  return best_image;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r13d, %r13d
leaq 0x30(%rsp), %r15
leaq 0x10ca41(%rip), %r12    # 0x124c28
movq $0x0, (%rsp)
movslq 0x9c(%r14), %rax
cmpq %rax, %r13
jge 0x18342
movq 0x90(%r14), %rax
movq (%rax,%r13,8), %rbp
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x38272
movsd 0x40(%rsp), %xmm0
xorpd 0xb017a(%rip), %xmm0    # 0xc83a0
movsd (%r12), %xmm1
ucomisd %xmm0, %xmm1
jae 0x1833a
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x3843e
movsd 0x10(%rsp), %xmm0
addsd 0xafee4(%rip), %xmm0    # 0xc8138
cvttsd2si %xmm0, %ecx
testl %ecx, %ecx
js 0x1833a
movl 0x98(%rbp), %eax
cmpl %ecx, %eax
jle 0x1833a
movsd 0x18(%rsp), %xmm0
addsd 0xafebc(%rip), %xmm0    # 0xc8138
cvttsd2si %xmm0, %esi
testl %esi, %esi
js 0x1833a
movl 0x9c(%rbp), %edx
cmpl %esi, %edx
jle 0x1833a
movd %esi, %xmm0
movd %ecx, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
pxor %xmm3, %xmm3
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movdqa 0xb00fe(%rip), %xmm4    # 0xc83b0
por %xmm4, %xmm1
subpd %xmm4, %xmm1
movd %edx, %xmm0
movd %eax, %xmm2
punpckldq %xmm0, %xmm2    # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
punpckldq %xmm3, %xmm2    # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1]
por %xmm4, %xmm2
subpd %xmm4, %xmm2
mulpd 0xb00e6(%rip), %xmm2    # 0xc83c0
addpd %xmm2, %xmm1
andpd 0xb00ea(%rip), %xmm1    # 0xc83d0
divpd %xmm2, %xmm1
addpd 0xb00ee(%rip), %xmm1    # 0xc83e0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
mulsd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
leaq 0x30(%rbp), %rdi
movq %rbx, %rsi
callq 0x495a1
movapd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm0, %xmm1
movq (%rsp), %rax
cmovaq %rbp, %rax
movq %rax, (%rsp)
maxsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
incq %r13
jmp 0x181ef
movq (%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop