// File: 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:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x38(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xb8(%rsp)
movq $0x0, 0xb0(%rsp)
movl $0x0, 0xac(%rsp)
movq 0x38(%rsp), %rdi
movl 0xac(%rsp), %eax
movl %eax, 0x34(%rsp)
callq 0x16cc0
movl %eax, %ecx
movl 0x34(%rsp), %eax
cmpl %ecx, %eax
jge 0x1dbe9
movq 0x38(%rsp), %rdi
movl 0xac(%rsp), %esi
callq 0x30800
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rsi
movq 0xc8(%rsp), %rdx
leaq 0x88(%rsp), %rdi
callq 0x69290
leaq 0x88(%rsp), %rdi
callq 0x187a0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%rsp), %xmm0
leaq 0x1deba3(%rip), %rax    # 0x1fc548
movsd (%rax), %xmm1
callq 0x308b0
cmpl $0x0, %eax
je 0x1d9b8
jmp 0x1dbd3
movq 0xa0(%rsp), %rsi
leaq 0x70(%rsp), %rdi
leaq 0x88(%rsp), %rdx
callq 0x695f0
leaq 0x70(%rsp), %rdi
callq 0x308e0
movsd 0x186774(%rip), %xmm1   # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x6c(%rsp)
cmpl $0x0, 0x6c(%rsp)
jl 0x1da16
movl 0x6c(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x308f0
movl %eax, %ecx
movl 0x30(%rsp), %eax
cmpl %ecx, %eax
jl 0x1da1b
jmp 0x1dbd3
leaq 0x70(%rsp), %rdi
callq 0x30910
movsd 0x18672b(%rip), %xmm1   # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x68(%rsp)
cmpl $0x0, 0x68(%rsp)
jl 0x1da5f
movl 0x68(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x30920
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jl 0x1da64
jmp 0x1dbd3
cvtsi2sdl 0x6c(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x308f0
movsd 0x8(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
movsd 0x186901(%rip), %xmm2   # 0x1a4390
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movaps 0x186922(%rip), %xmm1   # 0x1a43c0
pand %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x308f0
movsd 0x10(%rsp), %xmm0
cvtsi2sd %eax, %xmm2
movsd 0x186691(%rip), %xmm1   # 0x1a4158
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
cvtsi2sdl 0x68(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x30920
movsd 0x18(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
movsd 0x186890(%rip), %xmm2   # 0x1a4390
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movaps 0x1868b1(%rip), %xmm1   # 0x1a43c0
pand %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x30920
movsd 0x20(%rsp), %xmm0
cvtsi2sd %eax, %xmm2
movsd 0x186620(%rip), %xmm1   # 0x1a4158
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x18661a(%rip), %xmm0   # 0x1a4168
subsd 0x60(%rsp), %xmm0
movsd 0x18660c(%rip), %xmm1   # 0x1a4168
subsd 0x58(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x30940
movq %rax, %rdi
movq 0xc8(%rsp), %rsi
callq 0x8e7b0
movsd %xmm0, 0x48(%rsp)
movsd 0x50(%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
ucomisd 0xb8(%rsp), %xmm0
jbe 0x1dbd1
movq 0xa0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
jmp 0x1dbd3
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x1d90f
movq 0xb0(%rsp), %rax
addq $0xd8, %rsp
retq
nopl (%rax)