// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreatePointSet(gaps::R3SurfelScene*, gaps::R3Point&, double, double, double, double, double, double, 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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %edx, %ebx
movsd %xmm5, 0x20(%rsp)
movsd %xmm4, 0x18(%rsp)
movsd %xmm3, 0x28(%rsp)
movapd %xmm2, %xmm3
movq %rsi, %r14
movq %rdi, %r15
movsd 0x10(%rsi), %xmm2
ucomisd 0xbb0a0(%rip), %xmm2    # 0xdd160
movsd %xmm3, 0x10(%rsp)
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%rsp)
jne 0x2212e
jp 0x2212e
leaq 0x58(%rsp), %r12
movsd 0xbb08e(%rip), %xmm1    # 0xdd170
movsd 0xbb07e(%rip), %xmm2    # 0xdd168
movq %r12, %rdi
movq %r14, %rsi
callq 0x303e6
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x21f7f
movq %rax, %r12
testq %rax, %rax
je 0x2211f
testl %ebx, %ebx
jle 0x22163
cmpl %ebx, 0x10(%r12)
jge 0x22163
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
leaq 0x58(%rsp), %rdi
callq 0x31dba
jmp 0x222c0
movapd %xmm1, %xmm4
movapd %xmm2, %xmm1
addsd %xmm4, %xmm1
addsd %xmm3, %xmm2
leaq 0x58(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x303e6
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x21f7f
movq %rax, %r15
jmp 0x22243
leaq 0x30(%rsp), %rdx
movq $0x0, (%rdx)
leaq 0x38(%rsp), %rdi
movsd 0xbb1fc(%rip), %xmm0    # 0xdd378
movq %r12, %rsi
callq 0x22302
movsd 0x30(%rsp), %xmm0
ucomisd 0xbaf9e(%rip), %xmm0    # 0xdd130
movsd 0x10(%rsp), %xmm3
movsd 0x8(%rsp), %xmm4
jbe 0x221eb
movsd 0x48(%rsp), %xmm0
movapd 0xbb222(%rip), %xmm1    # 0xdd3d0
andpd %xmm0, %xmm1
ucomisd 0xbaf7e(%rip), %xmm1    # 0xdd138
jbe 0x221eb
movsd (%r14), %xmm2
movsd 0x8(%r14), %xmm1
mulsd 0x40(%rsp), %xmm1
mulsd 0x38(%rsp), %xmm2
addsd %xmm1, %xmm2
addsd 0x50(%rsp), %xmm2
xorpd 0xbb1bb(%rip), %xmm2    # 0xdd3a0
divsd %xmm0, %xmm2
jmp 0x221f2
movsd 0x30(%r12), %xmm2
movsd %xmm2, 0x10(%r14)
movapd %xmm2, %xmm1
addsd %xmm4, %xmm1
addsd %xmm3, %xmm2
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
movsd (%rsp), %xmm0
callq 0x303e6
leaq 0x90(%rsp), %rsi
movq %r12, %rdi
callq 0x21f14
movq %rax, %r15
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
leaq 0x90(%rsp), %rdi
callq 0x31dba
leaq 0x58(%rsp), %rdi
callq 0x31dba
testq %r15, %r15
je 0x222c0
testl %ebx, %ebx
movsd 0x28(%rsp), %xmm5
jle 0x22262
cmpl %ebx, 0x10(%r15)
jl 0x222b7
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm5
jbe 0x222ad
movq %r15, %rdi
movq %r14, %rsi
movsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movsd 0x18(%rsp), %xmm3
movsd 0x20(%rsp), %xmm4
movl $0x100, %edx            # imm = 0x100
callq 0x226ac
movq %rax, %r14
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
movq %r14, %r15
testq %r14, %r14
je 0x222c0
testl %ebx, %ebx
jle 0x222c3
cmpl %ebx, 0x10(%r15)
jge 0x222c3
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
xorl %r15d, %r15d
movq %r15, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x31dba
jmp 0x222f0
jmp 0x222ed
jmp 0x222ed
jmp 0x222ed
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x31dba
movq %rbx, %rdi
callq 0x138b0