// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateConnectedPointSet(gaps::R3SurfelPointSet*, gaps::R3SurfelPointSet*, double, int)
R3SurfelPointSet *
CreateConnectedPointSet(R3SurfelPointSet *pointset, R3SurfelPointSet *seedset,
  RNLength grid_spacing, int max_grid_resolution)
{
  // Just checking
  if (pointset->NPoints() == 0) return NULL;
  if (seedset->NPoints() == 0) return NULL;

  // Create pointset grid
  R3Grid *pointset_grid = CreateGrid(pointset, grid_spacing, max_grid_resolution);
  if (!pointset_grid) return NULL;
  if (pointset_grid->NEntries() == 0) { delete pointset_grid; return NULL; }

  // Create seedset grid
  R3Grid seedset_grid(*pointset_grid); seedset_grid.Clear(0);
  int step = seedset->NPoints() / seedset_grid.NEntries() + 1;
  for (int j = 0; j < seedset->NPoints(); j += step) {
    const R3SurfelPoint *point = seedset->Point(j);
    R3Point position = point->Position();
    seedset_grid.RasterizeWorldPoint(position, 1);
  }

  // Mask to part of grid connected to seeds
  MaskToSeededConnectedComponent(*pointset_grid, seedset_grid, 0.5);

  // Create connnected point set
  R3SurfelGridConstraint constraint(pointset_grid);
  R3SurfelPointSet *connected_pointset = CreatePointSet(pointset, &constraint);

  // Delete grids
  delete pointset_grid;

  // Return connected point set
  return connected_pointset;
}

// Assembly:
subq $0x338, %rsp            # imm = 0x338
movq %rdi, 0x328(%rsp)
movq %rsi, 0x320(%rsp)
movsd %xmm0, 0x318(%rsp)
movl %edx, 0x314(%rsp)
movq 0x328(%rsp), %rdi
callq 0x18700
cmpl $0x0, %eax
jne 0x3ab2a
movq $0x0, 0x330(%rsp)
jmp 0x3ae02
movq 0x320(%rsp), %rdi
callq 0x18700
cmpl $0x0, %eax
jne 0x3ab4d
movq $0x0, 0x330(%rsp)
jmp 0x3ae02
movq 0x328(%rsp), %rdi
movsd 0x318(%rsp), %xmm0
movl 0x314(%rsp), %esi
callq 0x39b40
movq %rax, 0x308(%rsp)
cmpq $0x0, 0x308(%rsp)
jne 0x3ab8e
movq $0x0, 0x330(%rsp)
jmp 0x3ae02
movq 0x308(%rsp), %rdi
callq 0x43fd0
cmpl $0x0, %eax
jne 0x3abdd
movq 0x308(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x3abcc
movq 0x38(%rsp), %rdi
callq 0xe0050
movq 0x38(%rsp), %rdi
movl $0x250, %esi            # imm = 0x250
callq 0x13580
movq $0x0, 0x330(%rsp)
jmp 0x3ae02
movq 0x308(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xdffb0
movq 0x30(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0xe5d60
jmp 0x3ac06
movq 0x320(%rsp), %rdi
callq 0x18700
movl %eax, 0x2c(%rsp)
jmp 0x3ac19
leaq 0xb8(%rsp), %rdi
callq 0x43fd0
movl %eax, 0x28(%rsp)
jmp 0x3ac2c
movl 0x28(%rsp), %ecx
movl 0x2c(%rsp), %eax
cltd
idivl %ecx
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x320(%rsp), %rdi
callq 0x18700
movl %eax, 0x24(%rsp)
jmp 0x3ac6a
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3ad0f
movq 0x320(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x18710
movq %rax, 0x18(%rsp)
jmp 0x3ac95
movq 0x18(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rsi
leaq 0x80(%rsp), %rdi
callq 0x18730
jmp 0x3acb9
leaq 0xb8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movsd 0x169497(%rip), %xmm0   # 0x1a4168
xorl %edx, %edx
callq 0x43ff0
jmp 0x3acda
jmp 0x3acdc
movl 0xa8(%rsp), %eax
addl 0xa4(%rsp), %eax
movl %eax, 0xa4(%rsp)
jmp 0x3ac4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x3adf3
movq 0x308(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movsd 0x169431(%rip), %xmm0   # 0x1a4158
callq 0x3ae20
jmp 0x3ad2e
movq 0x308(%rsp), %rsi
leaq 0x1c180b(%rip), %rax    # 0x1fc548
movsd (%rax), %xmm0
leaq 0x48(%rsp), %rdi
movl $0x4, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
xorps %xmm2, %xmm2
movaps %xmm2, %xmm1
callq 0x518c0
jmp 0x3ad60
movq 0x328(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x32140
movq %rax, 0x10(%rsp)
jmp 0x3ad79
movq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x308(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x3adaf
movq 0x8(%rsp), %rdi
callq 0xe0050
movq 0x8(%rsp), %rdi
movl $0x250, %esi            # imm = 0x250
callq 0x13580
movq 0x40(%rsp), %rax
movq %rax, 0x330(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x43fc0
leaq 0xb8(%rsp), %rdi
callq 0xe0050
jmp 0x3ae02
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x43fc0
leaq 0xb8(%rsp), %rdi
callq 0xe0050
jmp 0x3ae12
movq 0x330(%rsp), %rax
addq $0x338, %rsp            # imm = 0x338
retq
movq 0xb0(%rsp), %rdi
callq 0x13970
nop