// File: tomfunkhouser[P]gaps/apps/conf2img/RGBDSegmentation.cpp
// Function: gaps::RGBDCreateSegmentation(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::R3Point const&, gaps::R3Vector const&, gaps::R3Vector const&)
static R3Segmentation *
RGBDCreateSegmentation(const R2Grid& px_image, const R2Grid& py_image, const R2Grid& pz_image,
  const R2Grid& nx_image, const R2Grid& ny_image, const R2Grid& nz_image,
  const R2Grid& depth_image, const R2Grid& radius_image, 
  const R2Grid& boundary_image, const R2Image& color_image,
  const R3Point& viewpoint, const R3Vector& towards, const R3Vector& up)
{
  // Allocate segmentation
  R3Segmentation *segmentation = new R3Segmentation();
  if (!segmentation) {
    RNFail("Unable to allocate segmentation.\n");
    return NULL;
  }

  // Adjust segmentation parameters ???
  segmentation->min_cluster_points = 10 * depth_image.NEntries() / (640 * 480);
  
  // Create points
  if (!RGBDCreateSegmentationPoints(segmentation,
    px_image, py_image, pz_image, nx_image, ny_image, nz_image,
    depth_image, radius_image, boundary_image, color_image)) {
    RNFail("Unable to create points for segmentation.\n");
    delete segmentation;
    return 0;
  }

  // Check points
  if (segmentation->points.NEntries() == 0) {
    RNFail("Zero points for segmentation.\n");
    delete segmentation;
    return 0;
  }

  // Create clusters
  if (!segmentation->CreateClusters(R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE)) {
    RNFail("Unable to create clusters for segmentation.\n");
    delete segmentation;
    return 0;
  }

  // Return segmentation
  return segmentation;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x98, %rsp
movq 0xe0(%rsp), %rax
movq 0xd8(%rsp), %rax
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rax
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %rax
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq %rdx, 0x78(%rsp)
movq %rcx, 0x70(%rsp)
movq %r8, 0x68(%rsp)
movq %r9, 0x60(%rsp)
movl $0xe0, %edi
callq 0x249f0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
callq 0x8d470
jmp 0x315d2
movq 0x40(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0x31625
leaq 0x1ae320(%rip), %rdi    # 0x1df90b
movb $0x0, %al
callq 0x144e00
movq $0x0, 0x90(%rsp)
jmp 0x317b4
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
movl $0xe0, %esi
callq 0x24a10
jmp 0x317c7
movq 0xb0(%rsp), %rdi
callq 0x30fd0
imull $0xa, %eax, %eax
movl $0x4b000, %ecx          # imm = 0x4B000
cltd
idivl %ecx
movl %eax, %ecx
movq 0x58(%rsp), %rax
movl %ecx, 0x38(%rax)
movq 0x58(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0x80(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq 0x70(%rsp), %r8
movq 0x68(%rsp), %r9
movq 0x60(%rsp), %r14
movq 0xb0(%rsp), %rbx
movq 0xb8(%rsp), %r11
movq 0xc0(%rsp), %r10
movq 0xc8(%rsp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x317e0
cmpl $0x0, %eax
jne 0x316fa
leaq 0x1ae274(%rip), %rdi    # 0x1df92d
movb $0x0, %al
callq 0x144e00
movq 0x58(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x316e9
movq 0x38(%rsp), %rdi
callq 0x8d880
movq 0x38(%rsp), %rdi
movl $0xe0, %esi
callq 0x24a10
movq $0x0, 0x90(%rsp)
jmp 0x317b4
movq 0x58(%rsp), %rdi
callq 0x30560
cmpl $0x0, %eax
jne 0x3174e
leaq 0x1ae248(%rip), %rdi    # 0x1df958
movb $0x0, %al
callq 0x144e00
movq 0x58(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x31740
movq 0x30(%rsp), %rdi
callq 0x8d880
movq 0x30(%rsp), %rdi
movl $0xe0, %esi
callq 0x24a10
movq $0x0, 0x90(%rsp)
jmp 0x317b4
movq 0x58(%rsp), %rdi
movl $0x3, %esi
callq 0x910e0
cmpl $0x0, %eax
jne 0x317a7
leaq 0x1ae20e(%rip), %rdi    # 0x1df977
movb $0x0, %al
callq 0x144e00
movq 0x58(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x31799
movq 0x28(%rsp), %rdi
callq 0x8d880
movq 0x28(%rsp), %rdi
movl $0xe0, %esi
callq 0x24a10
movq $0x0, 0x90(%rsp)
jmp 0x317b4
movq 0x58(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r14
retq
movq 0x50(%rsp), %rdi
callq 0x25290
nopw %cs:(%rax,%rax)