// File: tomfunkhouser[P]gaps/apps/conf2conf/conf2conf.cpp
// Function: DeleteUnwantedImages(gaps::RGBDConfiguration*)
static int
DeleteUnwantedImages(RGBDConfiguration *configuration)
{
  // Make list of unwanted images
  RNArray<RGBDImage *> unwanted_images;
  for (int i = 0; i < configuration->NImages(); i++) {
    RGBDImage *image = configuration->Image(i);

    // Check if should load range of image indices
    if ((i < load_images_starting_at_index) || (i > load_images_ending_at_index)) {
      unwanted_images.Insert(image);
      continue;
    }

    // Check if should load every kth image  
    if (load_every_kth_image > 1) {
      if ((i % load_every_kth_image) != 0) {
        unwanted_images.Insert(image);
        continue;
      }
    }

    // Check if should load images within viewpoint bbox
    if (!viewpoint_bbox.IsEmpty()) {
      if (!R3Contains(viewpoint_bbox, image->WorldViewpoint())) {
        unwanted_images.Insert(image);
        continue;
      }
    }

    // Check if should load images within observation bbox
    if (!observation_bbox.IsEmpty()) {
      RNScalar min_overlap = 0.1;
      if (!R3Contains(observation_bbox, image->WorldViewpoint())) {
        // Compute conservative bounding box
        R3Box image_bbox = image->WorldBBox();
        
        // Check conservative bounding box dimensions
        RNScalar image_volume = image_bbox.Volume();
        if (image_volume == 0) {
          unwanted_images.Insert(image);
          continue;
        }
          
        // Conservative bounding box intersection
        R3Box overlap_box = observation_bbox;
        overlap_box.Intersect(image_bbox);
        if (overlap_box.IsEmpty()) {
          unwanted_images.Insert(image);
          continue;
        }
        
        if (TRUE) {
          // Compute exact bounding box
          image->InvalidateWorldBBox();
          image->ReadDepthChannel();
          R3Box image_bbox = image->WorldBBox();
          image->ReleaseDepthChannel();

          // Check exact bounding box dimensions
          RNScalar image_volume = image_bbox.Volume();
          if (image_volume == 0) {
            unwanted_images.Insert(image);
            continue;
          }
          
          // Exact bounding box intersection
          R3Box overlap_box = observation_bbox;
          overlap_box.Intersect(image_bbox);
          if (overlap_box.IsEmpty()) {
            unwanted_images.Insert(image);
            continue;
          }
        
          // Check exact bounding box overlap 
          RNScalar overlap = overlap_box.Volume() / image_volume;
          if (overlap < min_overlap) {
            unwanted_images.Insert(image);
            continue;
          }
        }
      }
    }
  }

  // Delete unwanted images
  for (int i = 0; i < unwanted_images.NEntries(); i++) {
    RGBDImage *image = unwanted_images.Kth(i);
    delete image;
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x1e0(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x19c80
movl $0x0, 0x1cc(%rsp)
movl 0x1cc(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0x1e0(%rsp), %rdi
callq 0x19c40
movl %eax, 0x64(%rsp)
jmp 0x18295
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
cmpl %ecx, %eax
jge 0x187dd
movq 0x1e0(%rsp), %rdi
movl 0x1cc(%rsp), %esi
callq 0x19c90
movq %rax, 0x58(%rsp)
jmp 0x182c0
movq 0x58(%rsp), %rax
movq %rax, 0x1b0(%rsp)
movl 0x1cc(%rsp), %eax
cmpl 0x199d46(%rip), %eax    # 0x1b2020
jl 0x182eb
movl 0x1cc(%rsp), %eax
cmpl 0x199417(%rip), %eax    # 0x1b1700
jle 0x18320
movq 0x1b0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x19cc0
jmp 0x18302
jmp 0x187c7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x1887d
cmpl $0x1, 0x1993dd(%rip)    # 0x1b1704
jle 0x1835a
movl 0x1cc(%rsp), %eax
cltd
idivl 0x1993cd(%rip)          # 0x1b1704
cmpl $0x0, %edx
je 0x18358
movq 0x1b0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x19cc0
jmp 0x18353
jmp 0x187c7
jmp 0x1835a
leaq 0x199c1f(%rip), %rdi    # 0x1b1f80
callq 0x19cf0
movl %eax, 0x54(%rsp)
jmp 0x1836c
movl 0x54(%rsp), %eax
cmpl $0x0, %eax
jne 0x183d6
movq 0x1b0(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0x19db0
jmp 0x1838c
leaq 0x199bed(%rip), %rdi    # 0x1b1f80
leaq 0x198(%rsp), %rsi
callq 0x37410
movl %eax, 0x50(%rsp)
jmp 0x183a6
movl 0x50(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x183b8
jmp 0x183d4
movq 0x1b0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x19cc0
jmp 0x183cf
jmp 0x187c7
jmp 0x183d6
leaq 0x199b6b(%rip), %rdi    # 0x1b1f48
callq 0x19cf0
movl %eax, 0x4c(%rsp)
jmp 0x183e8
movl 0x4c(%rsp), %eax
cmpl $0x0, %eax
jne 0x187c5
movabsq $0x3fb999999999999a, %rax # imm = 0x3FB999999999999A
movq %rax, 0x190(%rsp)
movq 0x1b0(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x19db0
jmp 0x1841e
leaq 0x199b23(%rip), %rdi    # 0x1b1f48
leaq 0x178(%rsp), %rsi
callq 0x37410
movl %eax, 0x48(%rsp)
jmp 0x18438
movl 0x48(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1844d
jmp 0x187c3
movq 0x1b0(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0x28d70
jmp 0x18464
leaq 0x140(%rsp), %rdi
callq 0x76cb0
movsd %xmm0, 0x40(%rsp)
jmp 0x18479
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x138(%rsp)
movsd 0x138(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x184dc
jp 0x184dc
movq 0x1b0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x19cc0
jmp 0x184b3
movl $0x4, 0x134(%rsp)
jmp 0x18779
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x187b1
leaq 0x199a65(%rip), %rsi    # 0x1b1f48
leaq 0xf8(%rsp), %rdi
callq 0x75370
jmp 0x184f2
leaq 0xf8(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x774a0
jmp 0x18509
leaq 0xf8(%rsp), %rdi
callq 0x19cf0
movl %eax, 0x3c(%rsp)
jmp 0x1851c
movl 0x3c(%rsp), %eax
cmpl $0x0, %eax
je 0x18565
movq 0x1b0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x19cc0
jmp 0x1853c
movl $0x4, 0x134(%rsp)
jmp 0x1876c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x187a4
movq 0x1b0(%rsp), %rdi
movq (%rdi), %rax
movq 0x1c0(%rax), %rax
callq *%rax
jmp 0x1857b
movq 0x1b0(%rsp), %rdi
movq (%rdi), %rax
movq 0x108(%rax), %rax
callq *%rax
jmp 0x18591
movq 0x1b0(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x28d70
jmp 0x185a8
movq 0x1b0(%rsp), %rdi
movq (%rdi), %rax
movq 0x158(%rax), %rax
callq *%rax
jmp 0x185be
leaq 0xc0(%rsp), %rdi
callq 0x76cb0
movsd %xmm0, 0x30(%rsp)
jmp 0x185d3
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0xb8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x18636
jp 0x18636
movq 0x1b0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x19cc0
jmp 0x1860d
movl $0x4, 0x134(%rsp)
jmp 0x18747
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x18797
leaq 0x19990b(%rip), %rsi    # 0x1b1f48
leaq 0x80(%rsp), %rdi
callq 0x75370
jmp 0x1864c
leaq 0x80(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x774a0
jmp 0x18663
leaq 0x80(%rsp), %rdi
callq 0x19cf0
movl %eax, 0x2c(%rsp)
jmp 0x18676
movl 0x2c(%rsp), %eax
cmpl $0x0, %eax
je 0x186cc
movq 0x1b0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x19cc0
jmp 0x18696
movl $0x4, 0x134(%rsp)
jmp 0x1873a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x19b90
jmp 0x18797
leaq 0x80(%rsp), %rdi
callq 0x76cb0
movsd %xmm0, 0x20(%rsp)
jmp 0x186e1
movsd 0x20(%rsp), %xmm0
divsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm1
movsd 0x190(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1872f
movq 0x1b0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x19cc0
jmp 0x18722
movl $0x4, 0x134(%rsp)
jmp 0x1873a
movl $0x0, 0x134(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x19b90
leaq 0xc0(%rsp), %rdi
callq 0x19b90
movl 0x134(%rsp), %eax
testl %eax, %eax
jne 0x1876c
jmp 0x18761
movl $0x0, 0x134(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x19b90
leaq 0x140(%rsp), %rdi
callq 0x19b90
movl 0x134(%rsp), %eax
testl %eax, %eax
je 0x18795
jmp 0x18793
jmp 0x187c7
jmp 0x187c3
leaq 0xc0(%rsp), %rdi
callq 0x19b90
leaq 0xf8(%rsp), %rdi
callq 0x19b90
leaq 0x140(%rsp), %rdi
callq 0x19b90
jmp 0x1887d
jmp 0x187c5
jmp 0x187c7
movl 0x1cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1cc(%rsp)
jmp 0x18277
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x19e00
movl %eax, 0x1c(%rsp)
jmp 0x18800
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x18858
movl 0x74(%rsp), %esi
leaq 0x1d0(%rsp), %rdi
callq 0x19e10
movq %rax, 0x10(%rsp)
jmp 0x18824
movq 0x10(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x18849
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x1884b
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x187e5
movl $0x1, 0x134(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x19e30
movl $0x1, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
retq
leaq 0x1d0(%rsp), %rdi
callq 0x19e30
movq 0x1c0(%rsp), %rdi
callq 0x12c20
nopw (%rax,%rax)