// File: tomfunkhouser[P]gaps/apps/conf2sfl/conf2sfl.cpp
// Function: CreateSegmentation(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::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 *
CreateSegmentation(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& tx_image, const R2Grid& ty_image, const R2Grid& tz_image,
  const R2Grid& depth_image, const R2Grid& r1_image, const R2Grid& r2_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);
  segmentation->scale_tolerances_with_depth = TRUE;
  // segmentation->print_progress = TRUE;
  // segmentation->PrintParameters();
  
  // Create points
  if (!CreatePoints(segmentation, px_image, py_image, pz_image,
    nx_image, ny_image, nz_image, tx_image, ty_image, tz_image,
    depth_image, r1_image, r2_image, boundary_image, color_image)) {
    RNFail("Unable to create points for segmentation.\n");
    delete segmentation;
    return 0;
  }

  // Check points
  if (segmentation->points.NEntries() == 0) {
    delete segmentation;
    return 0;
  }

  // Create clusters
  if (!segmentation->CreateClusters(R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE)) {
    delete segmentation;
    return 0;
  }

  // Return segmentation
  return segmentation;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq 0x140(%rsp), %rax
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rax
movq 0x128(%rsp), %rax
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 %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq %r9, 0x80(%rsp)
movl $0xe0, %edi
callq 0x16740
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x60(%rsp)
callq 0x42240
jmp 0x1bb85
movq 0x60(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x1bbd8
leaq 0x1e186c(%rip), %rdi    # 0x1fd40a
movb $0x0, %al
callq 0x1779b0
movq $0x0, 0xb0(%rsp)
jmp 0x1bd9a
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0xe0, %esi
callq 0x16750
jmp 0x1bdb4
movq 0x108(%rsp), %rdi
callq 0x1f290
imull $0xa, %eax, %eax
movl $0x4b000, %ecx          # imm = 0x4B000
cltd
idivl %ecx
movl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0x38(%rax)
movq 0x78(%rsp), %rax
movl $0x1, 0xcc(%rax)
movq 0x78(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movq 0x98(%rsp), %rcx
movq 0x90(%rsp), %r8
movq 0x88(%rsp), %r9
movq 0x80(%rsp), %rbp
movq 0xf0(%rsp), %r13
movq 0xf8(%rsp), %r12
movq 0x100(%rsp), %r15
movq 0x108(%rsp), %r14
movq 0x110(%rsp), %rbx
movq 0x118(%rsp), %r11
movq 0x120(%rsp), %r10
movq 0x128(%rsp), %rax
movq %rbp, (%rsp)
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
callq 0x1d3c0
cmpl $0x0, %eax
jne 0x1bcfc
leaq 0x1e1771(%rip), %rdi    # 0x1fd42c
movb $0x0, %al
callq 0x1779b0
movq 0x78(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
je 0x1bceb
movq 0x58(%rsp), %rdi
callq 0x42650
movq 0x58(%rsp), %rdi
movl $0xe0, %esi
callq 0x16750
movq $0x0, 0xb0(%rsp)
jmp 0x1bd9a
movq 0x78(%rsp), %rdi
callq 0x1ecf0
cmpl $0x0, %eax
jne 0x1bd42
movq 0x78(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, %rax
je 0x1bd34
movq 0x50(%rsp), %rdi
callq 0x42650
movq 0x50(%rsp), %rdi
movl $0xe0, %esi
callq 0x16750
movq $0x0, 0xb0(%rsp)
jmp 0x1bd9a
movq 0x78(%rsp), %rdi
movl $0x3, %esi
callq 0x45eb0
cmpl $0x0, %eax
jne 0x1bd8d
movq 0x78(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, %rax
je 0x1bd7f
movq 0x48(%rsp), %rdi
callq 0x42650
movq 0x48(%rsp), %rdi
movl $0xe0, %esi
callq 0x16750
movq $0x0, 0xb0(%rsp)
jmp 0x1bd9a
movq 0x78(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x70(%rsp), %rdi
callq 0x16ca0
nop