// File: 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:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x38(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd0(%rsp)
movq $0x0, 0xc8(%rsp)
movl $0x0, 0xc4(%rsp)
movq 0x38(%rsp), %rdi
movl 0xc4(%rsp), %eax
movl %eax, 0x34(%rsp)
callq 0x19c40
movl %eax, %ecx
movl 0x34(%rsp), %eax
cmpl %ecx, %eax
jge 0x222c9
movq 0x38(%rsp), %rdi
movl 0xc4(%rsp), %esi
callq 0x19c90
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0x1af30
cmpl $0x0, %eax
jne 0x21f9c
jmp 0x222b3
movq 0xb8(%rsp), %rdi
movl $0x1, %esi
callq 0x1af30
cmpl $0x0, %eax
jne 0x21fb8
jmp 0x222b3
leaq 0xa0(%rsp), %rdi
callq 0x8a990
movq 0xb8(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq (%rdi), %rax
leaq 0xa0(%rsp), %rdx
callq *0xa8(%rax)
cmpl $0x0, %eax
jne 0x21ff0
jmp 0x222b3
leaq 0xa0(%rsp), %rdi
callq 0x19ec0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm0
leaq 0x18fe93(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a3d0
cmpl $0x0, %eax
je 0x22040
jmp 0x222b3
leaq 0x88(%rsp), %rdi
callq 0xcfd70
movq 0xb8(%rsp), %rdi
movq (%rdi), %rax
leaq 0xa0(%rsp), %rsi
leaq 0x88(%rsp), %rdx
callq *0x98(%rax)
cmpl $0x0, %eax
jne 0x22078
jmp 0x222b3
leaq 0x88(%rsp), %rdi
callq 0x1af20
movsd 0x143f9b(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x84(%rsp)
cmpl $0x0, 0x84(%rsp)
jl 0x220ca
movl 0x84(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0x1af30
movl %eax, %ecx
movl 0x30(%rsp), %eax
cmpl %ecx, %eax
jl 0x220cf
jmp 0x222b3
leaq 0x88(%rsp), %rdi
callq 0x1af70
movsd 0x143f44(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x80(%rsp)
cmpl $0x0, 0x80(%rsp)
jl 0x22124
movl 0x80(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0xb8(%rsp), %rdi
movl $0x1, %esi
callq 0x1af30
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jl 0x22129
jmp 0x222b3
cvtsi2sdl 0x84(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0x1af30
movsd 0x8(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
movsd 0x144407(%rip), %xmm2   # 0x166560
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movaps 0x144418(%rip), %xmm1   # 0x166580
pand %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0x1af30
movsd 0x10(%rsp), %xmm0
cvtsi2sd %eax, %xmm2
movsd 0x143e95(%rip), %xmm1   # 0x166028
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
cvtsi2sdl 0x80(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0xb8(%rsp), %rdi
movl $0x1, %esi
callq 0x1af30
movsd 0x18(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
movsd 0x14438c(%rip), %xmm2   # 0x166560
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movaps 0x14439d(%rip), %xmm1   # 0x166580
pand %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0xb8(%rsp), %rdi
movl $0x1, %esi
callq 0x1af30
movsd 0x20(%rsp), %xmm0
cvtsi2sd %eax, %xmm2
movsd 0x143e17(%rip), %xmm1   # 0x166028
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x143e09(%rip), %xmm0   # 0x166030
subsd 0x78(%rsp), %xmm0
movsd 0x143dfb(%rip), %xmm1   # 0x166030
subsd 0x70(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0xb8(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x19db0
movq 0xd8(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x3b180
movsd %xmm0, 0x60(%rsp)
movsd 0x68(%rsp), %xmm0
divsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
ucomisd 0xd0(%rsp), %xmm0
jbe 0x222b1
movq 0xb8(%rsp), %rax
movq %rax, 0xc8(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
jmp 0x222b3
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x21f47
movq 0xc8(%rsp), %rax
addq $0xe8, %rsp
retq
nopl (%rax)