// File: /workspace/github_cpp_makefile_star2_O3/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
cmpl $0x0, 0x9c(%rdi)
jle 0x18eb0
movq %rsi, %rbx
movq %rdi, %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r13d, %r13d
leaq 0x30(%rsp), %r15
leaq 0x11aeb0(%rip), %r12    # 0x133c08
movq $0x0, (%rsp)
movq 0x90(%r14), %rax
movq (%rax,%r13,8), %rbp
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x3cbb4
movsd 0x40(%rsp), %xmm0
xorpd 0xc4619(%rip), %xmm0    # 0xdd3a0
movsd (%r12), %xmm1
ucomisd %xmm0, %xmm1
jae 0x18e9b
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x3cd80
movsd 0x10(%rsp), %xmm0
addsd 0xc4383(%rip), %xmm0    # 0xdd138
cvttsd2si %xmm0, %ecx
testl %ecx, %ecx
js 0x18e9b
movl 0x98(%rbp), %eax
cmpl %ecx, %eax
jle 0x18e9b
movsd 0x18(%rsp), %xmm0
addsd 0xc435b(%rip), %xmm0    # 0xdd138
cvttsd2si %xmm0, %esi
testl %esi, %esi
js 0x18e9b
movl 0x9c(%rbp), %edx
cmpl %esi, %edx
jle 0x18e9b
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 0xc459d(%rip), %xmm4    # 0xdd3b0
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 0xc4585(%rip), %xmm2    # 0xdd3c0
addpd %xmm2, %xmm1
andpd 0xc4589(%rip), %xmm1    # 0xdd3d0
divpd %xmm2, %xmm1
addpd 0xc458d(%rip), %xmm1    # 0xdd3e0
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 0x4feeb
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 0x9c(%r14), %rax
cmpq %rax, %r13
jl 0x18d60
jmp 0x18eb8
movq $0x0, (%rsp)
movq (%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop