// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/conf2img/conf2img.cpp
// Function: RenderConfiguration(gaps::RGBDConfiguration const&, gaps::R3Point const&, gaps::R3Vector const&, gaps::R3Vector const&, double, double)
static void
RenderConfiguration(const RGBDConfiguration& configuration,
  const R3Point& target_viewpoint, const R3Vector& target_towards, const R3Vector& target_up,
  RNScalar min_affinity = 0.01, RNScalar min_affinity_ratio = 0.25)
{
  // Clear window
  glViewport(0, 0, width, height);
  glClearColor(background.R(), background.G(), background.B(), 0.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glEnable(GL_DEPTH_TEST);
  RNLoadRgb(1.0, 1.0, 1.0);

  // Draw images
  if (configuration.NImages() > 0) {
    // Create array of affinities
    RNArray<RGBDImage *> images;
    RNScalar *affinities = new RNScalar [ configuration.NImages() ];
    for (int i = 0; i < configuration.NImages(); i++) {
      RGBDImage *image = configuration.Image(i);
      RNScalar affinity = Affinity(image->WorldViewpoint(), image->WorldTowards(), target_viewpoint, target_towards);
      if (affinity < min_affinity) continue;
      affinities[images.NEntries()] = affinity;
      images.Insert(image);
    }

    // Sort images based on affinity
    for (int i = 0; i < images.NEntries(); i++) {
      for (int j = i+1; j < images.NEntries(); j++) {
        if (affinities[j] > affinities[i]) {
          RGBDImage *swap_image = images[j];
          images[j] = images[i];
          images[i] = swap_image;
          RNScalar swap_affinity = affinities[j];
          affinities[j] = affinities[i];
          affinities[i] = swap_affinity;
        }
      }
    }

    // Draw images with best affinity
    RNScalar best_affinity = affinities[0];
    if (best_affinity > 0) {
      for (int i = 0; i < images.NEntries(); i++) {
        RGBDImage *image = images.Kth(i);
        RNScalar affinity = affinities[i];
        if ((min_affinity_ratio > 0) && (affinity < min_affinity_ratio * best_affinity)) continue;
        image->ReadChannels();
        image->DrawSurfels(RGBD_PHOTO_COLOR_SCHEME);
        image->ReleaseChannels();
        printf("%d %s %g\n", i, image->Name(), affinity);
      }
    }

    // Delete array of affinities
    delete [] affinities;
  }
  
  // Draw surfaces
  for (int i = 0; i < configuration.NSurfaces(); i++) {
    RGBDSurface *surface = configuration.Surface(i);
    surface->ReadChannels();
    surface->DrawTexture(RGBD_PHOTO_COLOR_SCHEME);
    surface->ReleaseChannels();
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %r15
movl 0x14e5eb(%rip), %edx    # 0x179a60
movl 0x14e5e9(%rip), %ecx    # 0x179a64
xorl %edi, %edi
xorl %esi, %esi
callq 0x231f0
cvtsd2ss 0x14e3a4(%rip), %xmm0   # 0x179830
cvtsd2ss 0x14e3a4(%rip), %xmm1   # 0x179838
cvtsd2ss 0x14e3a4(%rip), %xmm2   # 0x179840
xorpd %xmm3, %xmm3
callq 0x23e30
movl $0x4100, %edi           # imm = 0x4100
callq 0x231d0
movl $0xb71, %edi            # imm = 0xB71
callq 0x23820
movsd 0xd9b67(%rip), %xmm0    # 0x105028
movapd %xmm0, %xmm1
movapd %xmm0, %xmm2
callq 0x23e80
cmpl $0x0, 0x14e387(%rip)    # 0x17985c
jle 0x2b73c
leaq 0x8(%rsp), %rdi
callq 0xb238c
movslq 0x14e370(%rip), %r14    # 0x17985c
leaq (,%r14,8), %rax
testq %r14, %r14
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x23040
movq %rax, %rbx
testl %r14d, %r14d
jle 0x2b608
xorl %r14d, %r14d
leaq 0x40(%rsp), %r13
movq 0x14e32e(%rip), %rax    # 0x179850
movq (%rax,%r14,8), %r12
leaq 0x38(%r12), %rbp
movq %r13, %rdi
movq %rbp, %rsi
leaq 0x1519d8(%rip), %rdx    # 0x17cf10
callq 0x91aae
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
leaq 0xdde6c(%rip), %rdx     # 0x1093b8
callq 0x91a36
movq %r13, %rdi
movq %r15, %rsi
callq 0x69f47
movsd %xmm0, 0x20(%rsp)
movsd 0x50(%rsp), %xmm0
subsd 0x10(%r15), %xmm0
andpd 0xd9b1a(%rip), %xmm0    # 0x105090
movsd 0xd9aaa(%rip), %xmm1    # 0x105028
ucomisd %xmm1, %xmm0
ja 0x2b5f5
leaq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x93dd6
ucomisd 0xd9abd(%rip), %xmm0    # 0x105058
jbe 0x2b5f5
movsd 0xd9a83(%rip), %xmm3    # 0x105028
movapd %xmm3, %xmm1
movsd 0x20(%rsp), %xmm4
divsd %xmm4, %xmm1
movapd %xmm3, %xmm2
cmpltsd %xmm4, %xmm2
andpd %xmm2, %xmm1
andnpd %xmm3, %xmm2
orpd %xmm1, %xmm2
mulsd %xmm0, %xmm0
mulsd %xmm2, %xmm0
movsd 0xd9a30(%rip), %xmm1    # 0x105008
ucomisd %xmm0, %xmm1
ja 0x2b5f5
movslq 0x14(%rsp), %rdx
movsd %xmm0, (%rbx,%rdx,8)
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0xb2422
incq %r14
movslq 0x14e25d(%rip), %rax    # 0x17985c
cmpq %rax, %r14
jl 0x2b51b
movslq 0x14(%rsp), %rax
testq %rax, %rax
jle 0x2b72a
movl $0x1, %ecx
xorl %edx, %edx
movq %rdx, %rsi
incq %rdx
cmpq %rax, %rdx
jae 0x2b66b
movsd (%rbx,%rsi,8), %xmm0
movq %rcx, %rdi
movsd (%rbx,%rdi,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2b663
movq 0x8(%rsp), %r8
movq (%r8,%rdi,8), %r9
movq (%r8,%rsi,8), %r10
movq %r10, (%r8,%rdi,8)
movq 0x8(%rsp), %r8
movq %r9, (%r8,%rsi,8)
movsd %xmm0, (%rbx,%rdi,8)
movsd %xmm1, (%rbx,%rsi,8)
movapd %xmm1, %xmm0
incq %rdi
cmpq %rdi, %rax
jne 0x2b630
incq %rcx
cmpq %rax, %rdx
jne 0x2b61d
movl 0x14(%rsp), %eax
movsd (%rbx), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
seta %cl
testl %eax, %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x2b72a
mulsd 0xd99da(%rip), %xmm1    # 0x105078
xorl %r14d, %r14d
leaq 0xda22c(%rip), %r15     # 0x1058d4
movsd %xmm1, 0x18(%rsp)
movsd (%rbx,%r14,8), %xmm0
ucomisd %xmm0, %xmm1
ja 0x2b71f
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rsp), %rax
movq (%rax,%r14,8), %r12
movq (%r12), %rax
movq %r12, %rdi
callq *0xf8(%rax)
movq (%r12), %rax
movq %r12, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq *0xe0(%rax)
movq (%r12), %rax
movq %r12, %rdi
callq *0x148(%rax)
movq 0x1d0(%r12), %rdx
movq %r15, %rdi
movl %r14d, %esi
movsd 0x20(%rsp), %xmm0
movb $0x1, %al
callq 0x23080
movl 0x14(%rsp), %eax
movsd 0x18(%rsp), %xmm1
incq %r14
movslq %eax, %rcx
cmpq %rcx, %r14
jl 0x2b6ae
movq %rbx, %rdi
callq 0x23c30
leaq 0x8(%rsp), %rdi
callq 0xb23f6
cmpl $0x0, 0x14e129(%rip)    # 0x17986c
jle 0x2b788
xorl %r14d, %r14d
movq 0x14e111(%rip), %rax    # 0x179860
movq (%rax,%r14,8), %rbx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x88(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
movl $0x1, %esi
callq *0x78(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0xa8(%rax)
incq %r14
movslq 0x14e0e9(%rip), %rax    # 0x17986c
cmpq %rax, %r14
jl 0x2b748
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2b79f
jmp 0x2b79f
jmp 0x2b79f
jmp 0x2b79f
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xb23f6
movq %rbx, %rdi
callq 0x240d0