// File: /workspace/github_cpp_makefile_star2_O1/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 0x15155e(%rip), %edx    # 0x17ca60
movl 0x15155c(%rip), %ecx    # 0x17ca64
xorl %edi, %edi
xorl %esi, %esi
callq 0x231f0
cvtsd2ss 0x151317(%rip), %xmm0   # 0x17c830
cvtsd2ss 0x151317(%rip), %xmm1   # 0x17c838
cvtsd2ss 0x151317(%rip), %xmm2   # 0x17c840
xorpd %xmm3, %xmm3
callq 0x23e40
movl $0x4100, %edi           # imm = 0x4100
callq 0x231d0
movl $0xb71, %edi            # imm = 0xB71
callq 0x23820
movsd 0xdcada(%rip), %xmm0    # 0x108028
movapd %xmm0, %xmm1
movapd %xmm0, %xmm2
callq 0x23e90
cmpl $0x0, 0x1512fa(%rip)    # 0x17c85c
jle 0x2b7c4
leaq 0x8(%rsp), %rdi
callq 0xb398c
movslq 0x1512e3(%rip), %rax    # 0x17c85c
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x23040
movq %rax, %rbx
cmpl $0x0, 0x1512be(%rip)    # 0x17c85c
jle 0x2b6a0
xorl %r14d, %r14d
leaq 0x40(%rsp), %r13
movq 0x15129d(%rip), %rax    # 0x17c850
movq (%rax,%r14,8), %r12
leaq 0x38(%r12), %rbp
movq %r13, %rdi
movq %rbp, %rsi
leaq 0x154947(%rip), %rdx    # 0x17ff10
callq 0x92eb2
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
leaq 0xe0dfb(%rip), %rdx     # 0x10c3d8
callq 0x92e3a
movq %r13, %rdi
movq %r15, %rsi
callq 0x6b003
movsd %xmm0, 0x20(%rsp)
movsd 0x50(%rsp), %xmm1
subsd 0x10(%r15), %xmm1
andpd 0xdca89(%rip), %xmm1    # 0x108090
xorpd %xmm0, %xmm0
movsd 0xdca15(%rip), %xmm2    # 0x108028
ucomisd %xmm2, %xmm1
ja 0x2b668
leaq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x951ea
movsd 0xdc9f8(%rip), %xmm3    # 0x108028
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
xorpd %xmm1, %xmm1
cmpltsd %xmm0, %xmm1
mulsd %xmm0, %xmm0
mulsd %xmm2, %xmm0
andpd %xmm1, %xmm0
movsd 0xdc998(%rip), %xmm1    # 0x108008
ucomisd %xmm0, %xmm1
ja 0x2b68d
movslq 0x14(%rsp), %rdx
movsd %xmm0, (%rbx,%rdx,8)
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0xb3a22
incq %r14
movslq 0x1511c5(%rip), %rax    # 0x17c85c
cmpq %rax, %r14
jl 0x2b5ac
movslq 0x14(%rsp), %rax
testq %rax, %rax
jle 0x2b703
movl $0x1, %ecx
xorl %edx, %edx
movq %rdx, %rsi
incq %rdx
cmpq %rax, %rdx
jae 0x2b6fb
movq %rcx, %rdi
movsd (%rbx,%rdi,8), %xmm0
movsd (%rbx,%rsi,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2b6f3
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 %xmm1, (%rbx,%rdi,8)
movsd %xmm0, (%rbx,%rsi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x2b6bf
incq %rcx
cmpq %rax, %rdx
jne 0x2b6b1
movsd (%rbx), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x2b7b2
cmpl $0x0, 0x14(%rsp)
jle 0x2b7b2
mulsd 0xdc950(%rip), %xmm1    # 0x108078
xorl %r14d, %r14d
leaq 0xdd1a5(%rip), %r15     # 0x1088d7
movsd %xmm1, 0x18(%rsp)
movsd (%rbx,%r14,8), %xmm0
ucomisd %xmm0, %xmm1
ja 0x2b7a5
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
movsd 0x18(%rsp), %xmm1
incq %r14
movslq 0x14(%rsp), %rax
cmpq %rax, %r14
jl 0x2b738
movq %rbx, %rdi
callq 0x23c40
leaq 0x8(%rsp), %rdi
callq 0xb39f6
cmpl $0x0, 0x1510a1(%rip)    # 0x17c86c
jle 0x2b810
xorl %r14d, %r14d
movq 0x151089(%rip), %rax    # 0x17c860
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 0x151061(%rip), %rax    # 0x17c86c
cmpq %rax, %r14
jl 0x2b7d0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2b827
jmp 0x2b827
jmp 0x2b827
jmp 0x2b827
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xb39f6
movq %rbx, %rdi
callq 0x240e0