// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateNormals(gaps::R3SurfelPointGraph*, int)
R3SurfelPointSet *
CreatePointSet(R3SurfelScene *scene, R3Point& origin, 
  RNLength max_radius, RNLength min_height, RNLength max_height, RNLength max_spacing,
  RNVolume min_volume, RNVolume max_volume, int min_points)
{
  // Check origin Z coordinate
  R3SurfelPointSet *surfels2 = NULL;
  if (origin.Z() == RN_UNKNOWN) {
    // Extract points within XY radius
    R3SurfelCylinderConstraint cylinder_constraint(origin, max_radius);
    R3SurfelPointSet *surfels1 = CreatePointSet(scene, NULL, &cylinder_constraint);
    if (!surfels1) return NULL;
    
    // Check minimum number of points
    if ((min_points > 0) && (surfels1->NPoints() < min_points)) {
      delete surfels1;
      return NULL;
    }
    
    // Compute support plane
    RNScalar support_count = 0;
    R3Plane support_plane = EstimateSupportPlane(surfels1, 0.1, &support_count);

    // Move origin to support plane
    if ((support_count > 16) && (fabs(support_plane[2]) > 0.5)) {
      RNScalar support_z = -(origin[0]*support_plane[0] + origin[1]*support_plane[1] + support_plane[3]) / support_plane[2];
      origin[2] = support_z;
    }
    else {
      origin[2] = surfels1->BBox().ZMin();
    }

    // Remove points that are too low or too high
    R3SurfelCylinderConstraint z_constraint(origin, max_radius, origin[2] + min_height, origin[2] + max_height);
    surfels2 = CreatePointSet(surfels1, &z_constraint);
    delete surfels1;
    if (!surfels2) return NULL;
  }
  else {
    // Extract points with cylinder
    R3SurfelCylinderConstraint cylinder_constraint(origin, max_radius, origin[2] + min_height, origin[2] + max_height);
    surfels2 = CreatePointSet(scene, NULL, &cylinder_constraint);
    if (!surfels2) return NULL;
  }

  // Check minimum number of points
  if ((min_points > 0) && (surfels2->NPoints() < min_points)) {
    delete surfels2;
    return NULL;
  }

  // Remove points that are not connected
  R3SurfelPointSet *surfels3 = surfels2;
  if (max_spacing > 0) {
    surfels3 = CreateConnectedPointSet(surfels2, origin, max_radius,
      min_height, max_height, min_volume, max_volume, max_spacing, 256);
    delete surfels2;
    if (!surfels3) return NULL;
  }
  
  // Check minimum number of points
  if ((min_points > 0) && (surfels3->NPoints() < min_points)) {
    delete surfels3;
    return NULL;
  }

  // Return pointset
  return surfels3;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movslq 0x18(%rdi), %r12
testq %r12, %r12
je 0x251fd
movq %rdi, %r15
cmpl $0x2, 0x70(%rdi)
jge 0x25205
xorl %r14d, %r14d
jmp 0x254a8
movl %esi, 0x14(%rsp)
pushq $0x18
popq %r14
movq %r12, %rax
mulq %r14
movq %rax, %rbx
pushq $-0x1
popq %rbp
cmovoq %rbp, %rbx
movq %rbx, %rdi
callq 0x13030
movq %rax, 0x8(%rsp)
imulq $0x18, %r12, %r12
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
leaq (%rax,%r13), %rdi
callq 0x7897a
addq $0x18, %r13
cmpq %r13, %r12
jne 0x25231
movl 0x70(%r15), %r12d
leal 0x1(%r12), %eax
movslq %eax, %r13
movq %r13, %rax
mulq %r14
movq %rax, %rbx
cmovoq %rbp, %rbx
movq %rbx, %rdi
callq 0x13030
movq %rax, (%rsp)
incl %r12d
je 0x2528f
imulq $0x18, %r13, %r14
xorl %r12d, %r12d
movq (%rsp), %rax
leaq (%rax,%r12), %rdi
callq 0x781ba
addq $0x18, %r12
cmpq %r12, %r14
jne 0x25279
movq (%rsp), %rax
leaq 0x18(%rax), %rcx
movq %rcx, 0x18(%rsp)
addq $0x30, %rax
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
leaq 0x60(%rsp), %rbx
movq 0x8(%rsp), %r14
movslq 0x18(%r15), %rax
cmpq %rax, %rbp
jge 0x2549f
movq %rbp, %r12
shlq $0x4, %r12
movq 0x68(%r15), %rax
cmpl $0x1, 0xc(%rax,%r12)
jg 0x252e9
imulq $0x18, %rbp, %rdi
addq %r14, %rdi
leaq 0xaae79(%rip), %rsi     # 0xd0158
callq 0x78eec
jmp 0x25497
movq %rbp, 0x20(%rsp)
movq 0x10(%r15), %rsi
addq %r12, %rsi
cmpl $0x0, 0x14(%rsp)
je 0x253d5
movq %rbx, %rdi
callq 0x16310
movq (%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x784a6
callq 0x9c2ba
movq 0x68(%r15), %rsi
movl 0xc(%rsi,%r12), %ecx
cvtsi2sd %ecx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
movl %ecx, %eax
cltd
pushq $0x2
popq %r8
idivl %r8d
addl %edi, %eax
cltd
idivl %ecx
movq (%rsi,%r12), %rax
movslq %edi, %rcx
movq (%rax,%rcx,8), %rsi
movslq %edx, %rcx
movq (%rax,%rcx,8), %r13
movq %rbx, %rdi
callq 0x16310
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x784a6
movq %rbx, %rdi
movq %r13, %rsi
callq 0x16310
movq 0x28(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x784a6
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x78399
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x78399
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x7902e
movq %r13, %rdi
callq 0x78c7e
movq 0x20(%rsp), %rbp
imulq $0x18, %rbp, %rdi
addq %r14, %rdi
movq %r13, %rsi
jmp 0x2546c
movq %rbx, %rdi
callq 0x16310
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x784a6
movq 0x18(%rsp), %r13
xorl %r14d, %r14d
movq 0x68(%r15), %rax
movslq 0xc(%rax,%r12), %rbp
cmpq %rbp, %r14
jge 0x25423
movq (%rax,%r12), %rax
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
callq 0x16310
incq %r14
movq %r13, %rdi
movq %rbx, %rsi
callq 0x784a6
addq $0x18, %r13
jmp 0x253f1
incl %ebp
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movl %ebp, %esi
movq (%rsp), %r14
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x4d640
movq %rbx, %rdi
movq %r12, %rsi
movl %ebp, %edx
movq %r14, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x4d6c9
movq 0x20(%rsp), %rbp
imulq $0x18, %rbp, %rdi
movq 0x8(%rsp), %r14
addq %r14, %rdi
leaq 0x90(%rsp), %rsi
callq 0x78eec
imulq $0x18, %rbp, %r13
addq %r14, %r13
movq %r13, %rdi
callq 0x78b00
cltq
xorpd %xmm0, %xmm0
ucomisd (%r13,%rax,8), %xmm0
jbe 0x25497
movq %r13, %rdi
callq 0x78cd2
incq %rbp
jmp 0x252b1
movq (%rsp), %rdi
callq 0x136a0
movq %r14, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movq (%rsp), %rdi
jmp 0x254ce
movq %rax, %r15
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x139f0
movq %r15, %rdi
callq 0x138f0