// File: tomfunkhouser[P]gaps/apps/conf2img/RGBDSegmentation.cpp
// Function: gaps::RGBDCreateSegmentationChannel(gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Image const&, gaps::R2Grid&, gaps::R3Point const&, gaps::R3Vector const&, gaps::R3Vector const&, char const*)
int
RGBDCreateSegmentationChannel(const R2Grid& depth_image, 
  const R2Grid& px_image, const R2Grid& py_image, const R2Grid& pz_image, const R2Grid& boundary_image, 
  const R2Grid& nx_image, const R2Grid& ny_image, const R2Grid& nz_image, const R2Grid& radius_image, 
  const R2Image& color_image, R2Grid& output_segmentation_image,
  const R3Point& viewpoint, const R3Vector& towards, const R3Vector& up,
  const char *output_segmentation_filename = NULL)
{
  // Initialize segmentation image
  output_segmentation_image = depth_image;
  output_segmentation_image.Clear(R2_GRID_UNKNOWN_VALUE);
  
  // Create segmentation
  R3Segmentation *segmentation = RGBDCreateSegmentation(
    px_image, py_image, pz_image, nx_image, ny_image, nz_image, 
    depth_image, radius_image, boundary_image, color_image, 
    viewpoint, towards, up);

  // Check segmentation
  if (!segmentation) return 0;

  // Fill segmentation image
  for (int i = 0; i < segmentation->clusters.NEntries(); i++) {
    R3SegmentationCluster *cluster = segmentation->clusters.Kth(i);
    for (int j = 0; j < cluster->points.NEntries(); j++) {
      R3SegmentationPoint *point = cluster->points.Kth(j);
      if (RNIsNegativeOrZero(point->depth)) continue;
      if (point->data_index < 0) continue;
      if (point->data_index >= output_segmentation_image.NEntries()) continue;
      output_segmentation_image.SetGridValue(point->data_index, i+1);
    }
  }

  // Write segmentation file (this is a hack)
  if (output_segmentation_filename) {
    // if (!RGBDWriteSegmentation(segmentation, output_segmentation_filename)) return 0;
    if (!segmentation->WriteFile(output_segmentation_filename)) return 0;
  }
  
  // Delete segmentation
  delete segmentation;

  // Return success
  return 1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq 0x120(%rsp), %rax
movq 0x118(%rsp), %rax
movq 0x110(%rsp), %rax
movq 0x108(%rsp), %rax
movq 0x100(%rsp), %rax
movq 0xf8(%rsp), %rax
movq 0xf0(%rsp), %rax
movq 0xe8(%rsp), %rax
movq 0xe0(%rsp), %rax
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq %r9, 0x80(%rsp)
movq 0xa8(%rsp), %rsi
movq 0x100(%rsp), %rdi
callq 0x1183d0
movq 0x100(%rsp), %rdi
movsd 0x1add55(%rip), %xmm0   # 0x1df050
callq 0x11aed0
movq 0xa0(%rsp), %rdi
movq 0x98(%rsp), %rsi
movq 0x90(%rsp), %rdx
movq 0x80(%rsp), %rcx
movq 0xe0(%rsp), %r8
movq 0xe8(%rsp), %r9
movq 0xa8(%rsp), %r12
movq 0xf0(%rsp), %r15
movq 0x88(%rsp), %r14
movq 0xf8(%rsp), %rbx
movq 0x108(%rsp), %r11
movq 0x110(%rsp), %r10
movq 0x118(%rsp), %rax
movq %r12, (%rsp)
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x31550
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x313ac
movl $0x0, 0xb4(%rsp)
jmp 0x31534
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0x78(%rsp), %rdi
addq $0x18, %rdi
callq 0x30560
movl %eax, %ecx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x314cf
movq 0x78(%rsp), %rdi
addq $0x18, %rdi
movl 0x74(%rsp), %esi
callq 0x32460
movq %rax, 0x68(%rsp)
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x68(%rsp), %rdi
addq $0x8, %rdi
callq 0x30560
movl %eax, %ecx
movl 0x50(%rsp), %eax
cmpl %ecx, %eax
jge 0x314bd
movq 0x68(%rsp), %rdi
addq $0x8, %rdi
movl 0x64(%rsp), %esi
callq 0x32480
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rax
movsd (%rax), %xmm0
leaq 0x21ee36(%rip), %rax    # 0x250278
movsd (%rax), %xmm1
callq 0x31020
cmpl $0x0, %eax
je 0x31452
jmp 0x314ad
movq 0x58(%rsp), %rax
cmpl $0x0, 0xd4(%rax)
jge 0x31462
jmp 0x314ad
movq 0x58(%rsp), %rax
movl 0xd4(%rax), %eax
movl %eax, 0x4c(%rsp)
movq 0x100(%rsp), %rdi
callq 0x30fd0
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jl 0x3148a
jmp 0x314ad
movq 0x100(%rsp), %rdi
movq 0x58(%rsp), %rax
movl 0xd4(%rax), %esi
movl 0x74(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
callq 0x309f0
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x313f7
jmp 0x314bf
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x313b4
cmpq $0x0, 0x120(%rsp)
je 0x31500
movq 0x78(%rsp), %rdi
movq 0x120(%rsp), %rsi
callq 0x91990
cmpl $0x0, %eax
jne 0x314fe
movl $0x0, 0xb4(%rsp)
jmp 0x31534
jmp 0x31500
movq 0x78(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
je 0x31529
movq 0x40(%rsp), %rdi
callq 0x8d880
movq 0x40(%rsp), %rdi
movl $0xe0, %esi
callq 0x24a10
movl $0x1, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
nopw (%rax,%rax)