// File: /workspace/github_cpp_makefile_star2_O2/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 0x13e4e9(%rip), %edx    # 0x168a80
movl 0x13e4e7(%rip), %ecx    # 0x168a84
xorl %edi, %edi
xorl %esi, %esi
callq 0x23210
cvtsd2ss 0x13e2a2(%rip), %xmm0   # 0x168850
cvtsd2ss 0x13e2a2(%rip), %xmm1   # 0x168858
cvtsd2ss 0x13e2a2(%rip), %xmm2   # 0x168860
xorpd %xmm3, %xmm3
callq 0x23e50
movl $0x4100, %edi           # imm = 0x4100
callq 0x231f0
movl $0xb71, %edi            # imm = 0xB71
callq 0x23830
movsd 0xc2a45(%rip), %xmm0    # 0xed028
movapd %xmm0, %xmm1
movapd %xmm0, %xmm2
callq 0x23ea0
cmpl $0x0, 0x13e285(%rip)    # 0x16887c
jle 0x2a829
leaq 0x8(%rsp), %rdi
callq 0xa3b56
movslq 0x13e26e(%rip), %rbp    # 0x16887c
leaq (,%rbp,8), %rax
testq %rbp, %rbp
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x23040
movq %rax, %rbx
xorl %r14d, %r14d
leaq 0x28(%rsp), %r12
leaq 0x40(%rsp), %r13
movslq %ebp, %rax
cmpq %rax, %r14
jge 0x2a712
movq 0x13e228(%rip), %rax    # 0x168870
movq (%rax,%r14,8), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2a986
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2a9a0
movq %r12, %rdi
movq %r15, %rsi
callq 0x61fa5
movsd %xmm0, 0x20(%rsp)
movsd 0x38(%rsp), %xmm0
subsd 0x10(%r15), %xmm0
andpd 0xc2a09(%rip), %xmm0    # 0xed090
movsd 0xc2999(%rip), %xmm1    # 0xed028
ucomisd %xmm1, %xmm0
ja 0x2a704
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0x87dbe
ucomisd 0xc29ae(%rip), %xmm0    # 0xed058
jbe 0x2a704
movsd 0xc2974(%rip), %xmm3    # 0xed028
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 0xc2921(%rip), %xmm1    # 0xed008
ucomisd %xmm0, %xmm1
ja 0x2a704
movslq 0x14(%rsp), %rax
movsd %xmm0, (%rbx,%rax,8)
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2a97e
incq %r14
movl 0x13e16f(%rip), %ebp    # 0x16887c
jmp 0x2a635
movl 0x14(%rsp), %eax
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
pushq $0x1
popq %rsi
cmpq %rdx, %rcx
je 0x2a772
movq %rsi, %rdi
cmpl %edi, %eax
jle 0x2a76a
movsd (%rbx,%rdi,8), %xmm0
movsd (%rbx,%rcx,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2a765
movq 0x8(%rsp), %r8
movq (%r8,%rdi,8), %r9
movq (%r8,%rcx,8), %r10
movq %r10, (%r8,%rdi,8)
movq 0x8(%rsp), %r8
movq %r9, (%r8,%rcx,8)
movsd %xmm1, (%rbx,%rdi,8)
movsd %xmm0, (%rbx,%rcx,8)
incq %rdi
jmp 0x2a72d
incq %rcx
incq %rsi
jmp 0x2a725
movsd (%rbx), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x2a817
mulsd 0xc28ec(%rip), %xmm1    # 0xed078
xorl %r14d, %r14d
pushq $0x1
popq %rbp
leaq 0xc313b(%rip), %r15     # 0xed8d4
movsd %xmm1, 0x18(%rsp)
movslq %eax, %rcx
cmpq %rcx, %r14
jge 0x2a817
movsd (%rbx,%r14,8), %xmm0
ucomisd %xmm0, %xmm1
ja 0x2a812
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 %ebp, %esi
movl %ebp, %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
jmp 0x2a79f
movq %rbx, %rdi
callq 0x23c30
leaq 0x8(%rsp), %rdi
callq 0xa3bc4
xorl %r15d, %r15d
pushq $0x1
popq %rbx
movslq 0x13e056(%rip), %rax    # 0x16888c
cmpq %rax, %r15
jge 0x2a86e
movq 0x13e03e(%rip), %rax    # 0x168880
movq (%rax,%r15,8), %r14
movq (%r14), %rax
movq %r14, %rdi
callq *0x88(%rax)
movq (%r14), %rax
movq %r14, %rdi
movl %ebx, %esi
callq *0x78(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *0xa8(%rax)
incq %r15
jmp 0x2a82f
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2a885
jmp 0x2a885
jmp 0x2a885
jmp 0x2a885
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xa3bc4
movq %rbx, %rdi
callq 0x240f0