// File: /workspace/github_cpp_makefile_star2_O1/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 0x18fe2
movq %rsi, %rbx
movq %rdi, %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r13d, %r13d
leaq 0x30(%rsp), %r15
leaq 0x11bd6e(%rip), %r12    # 0x134c08
movq $0x0, (%rsp)
movq 0x90(%r14), %rax
movq (%rax,%r13,8), %rbp
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x3cf44
movsd 0x40(%rsp), %xmm0
xorpd 0xc54d7(%rip), %xmm0    # 0xde3a0
movsd (%r12), %xmm1
ucomisd %xmm0, %xmm1
jae 0x18fcd
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x3d128
movsd 0x10(%rsp), %xmm0
addsd 0xc5249(%rip), %xmm0    # 0xde140
cvttsd2si %xmm0, %ecx
testl %ecx, %ecx
js 0x18fcd
movl 0x98(%rbp), %eax
cmpl %ecx, %eax
jle 0x18fcd
movsd 0x18(%rsp), %xmm0
addsd 0xc5221(%rip), %xmm0    # 0xde140
cvttsd2si %xmm0, %esi
testl %esi, %esi
js 0x18fcd
movl 0x9c(%rbp), %edx
cmpl %esi, %edx
jle 0x18fcd
movd %esi, %xmm0
movd %ecx, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqa 0xc5463(%rip), %xmm0    # 0xde3b0
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 0xc5453(%rip), %xmm0    # 0xde3c0
addpd %xmm0, %xmm1
andpd 0xc5457(%rip), %xmm1    # 0xde3d0
divpd %xmm0, %xmm1
addpd 0xc545b(%rip), %xmm1    # 0xde3e0
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 0x50893
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 0x18ea2
jmp 0x18fea
movq $0x0, (%rsp)
movq (%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop