// File: /workspace/github_cpp_makefile_star2_O1/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
cmpl $0x0, 0x14(%rdi)
jle 0x1a8f2
movq %rsi, %rbx
movq %rdi, %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r13d, %r13d
leaq 0x50(%rsp), %r15
leaq 0x10(%rsp), %r12
movq $0x0, (%rsp)
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rbp
cmpl $0x0, 0x28(%rbp)
je 0x1a8e0
cmpl $0x0, 0x2c(%rbp)
je 0x1a8e0
movq %r15, %rdi
callq 0x4be44
movq (%rbp), %rax
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq *0xa8(%rax)
testl %eax, %eax
je 0x1a8e0
movsd 0x60(%rsp), %xmm0
xorpd 0xa289c(%rip), %xmm0    # 0xbd050
leaq 0xec52d(%rip), %rax     # 0x106ce8
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jae 0x1a8e0
movq %r12, %rdi
callq 0x6fee4
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x98(%rax)
testl %eax, %eax
je 0x1a8e0
movsd 0x10(%rsp), %xmm0
addsd 0xa2826(%rip), %xmm0    # 0xbd020
cvttsd2si %xmm0, %ecx
testl %ecx, %ecx
js 0x1a8e0
movl 0x28(%rbp), %eax
cmpl %ecx, %eax
jle 0x1a8e0
movsd 0x18(%rsp), %xmm0
addsd 0xa2801(%rip), %xmm0    # 0xbd020
cvttsd2si %xmm0, %esi
testl %esi, %esi
js 0x1a8e0
movl 0x2c(%rbp), %edx
cmpl %esi, %edx
jle 0x1a8e0
movd %esi, %xmm0
movd %ecx, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqa 0xa2d26(%rip), %xmm0    # 0xbd570
por %xmm0, %xmm1
subpd %xmm0, %xmm1
movd %edx, %xmm0
movd %eax, %xmm2
punpckldq %xmm0, %xmm2    # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
cvtdq2pd %xmm2, %xmm0
mulpd 0xa2d16(%rip), %xmm0    # 0xbd580
addpd %xmm0, %xmm1
andpd 0xa2d1a(%rip), %xmm1    # 0xbd590
divpd %xmm0, %xmm1
addpd 0xa2d1e(%rip), %xmm1    # 0xbd5a0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
mulsd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
leaq 0x38(%rbp), %rsi
leaq 0x38(%rsp), %rdi
leaq 0xed30c(%rip), %rdx     # 0x107bb0
callq 0x4a83e
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x272af
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
movslq 0x14(%r14), %rax
cmpq %rax, %r13
jl 0x1a767
jmp 0x1a8fa
movq $0x0, (%rsp)
movq (%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq