// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/RGBD/RGBDConfiguration.cpp
// Function: gaps::RGBDConfiguration::FindImage(gaps::R3Point const&) const
RGBDImage *RGBDConfiguration::
FindImage(const R3Point& lookat_position) const
{
  // Search for image with best view of a point
  RNScalar best_score = 0;
  RGBDImage *best_image = NULL;
  for (int i = 0; i < NImages(); i++) {
    RGBDImage *image = Image(i);
    
    // Check image
    if (image->NPixels(RN_X) == 0) continue;
    if (image->NPixels(RN_Y) == 0) continue;

    // Get/check point depth
    R3Point camera_position;
    if (!image->TransformWorldToCamera(lookat_position, camera_position)) continue;
    RNScalar point_depth = -camera_position.Z();
    if (RNIsNegativeOrZero(point_depth)) continue;

    // Get/check projection into image
    R2Point image_position;
    if (!image->TransformCameraToImage(camera_position, image_position)) continue;
    int ix = (int) (image_position.X() + 0.5);
    if ((ix < 0) || (ix >= image->NPixels(RN_X))) continue;
    int iy = (int) (image_position.Y() + 0.5);
    if ((iy < 0) || (iy >= image->NPixels(RN_Y))) continue;
  
    // Get image centrality
    double dx = fabs(ix - 0.5*image->NPixels(RN_X)) / (0.5*image->NPixels(RN_X));
    double dy = fabs(iy - 0.5*image->NPixels(RN_Y)) / (0.5*image->NPixels(RN_Y));
    double image_centrality = (1.0 - dx) * (1.0 - dy);

    // Get distance
    RNScalar dd = R3SquaredDistance(image->WorldViewpoint(), lookat_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 $0x68, %rsp
movq %rsi, %rbx
movq %rdi, %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebp, %ebp
leaq 0x50(%rsp), %r15
leaq 0x10(%rsp), %r12
movq $0x0, (%rsp)
movslq 0x14(%r14), %rax
cmpq %rax, %rbp
jge 0x195da
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %r13
cmpl $0x0, 0x28(%r13)
je 0x195d2
cmpl $0x0, 0x2c(%r13)
je 0x195d2
movq %r15, %rdi
callq 0x45d4a
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq *0xa8(%rax)
testl %eax, %eax
je 0x195d2
movsd 0x60(%rsp), %xmm0
xorpd 0x8ebb4(%rip), %xmm0    # 0xa8050
leaq 0xde855(%rip), %rax     # 0xf7cf8
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jae 0x195d2
movq %r12, %rdi
callq 0x6663e
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x98(%rax)
testl %eax, %eax
je 0x195d2
movsd 0x10(%rsp), %xmm0
addsd 0x8eb36(%rip), %xmm0    # 0xa8018
cvttsd2si %xmm0, %ecx
testl %ecx, %ecx
js 0x195d2
movl 0x28(%r13), %eax
cmpl %ecx, %eax
jle 0x195d2
movsd 0x18(%rsp), %xmm0
addsd 0x8eb10(%rip), %xmm0    # 0xa8018
cvttsd2si %xmm0, %esi
testl %esi, %esi
js 0x195d2
movl 0x2c(%r13), %edx
cmpl %esi, %edx
jle 0x195d2
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 0x8efb4(%rip), %xmm4    # 0xa84f0
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 0x8ef9c(%rip), %xmm2    # 0xa8500
addpd %xmm2, %xmm1
andpd 0x8efa0(%rip), %xmm1    # 0xa8510
divpd %xmm2, %xmm1
addpd 0x8efa4(%rip), %xmm1    # 0xa8520
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
mulsd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x155fc
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x2492d
movapd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm0, %xmm1
movq (%rsp), %rax
cmovaq %r13, %rax
movq %rax, (%rsp)
maxsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
incq %rbp
jmp 0x19440
movq (%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq