// File: /workspace/github_cpp_makefile_star2_O1/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 0xbbe9f(%rip), %xmm2    # 0xde168
movsd %xmm3, 0x10(%rsp)
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%rsp)
jne 0x22333
jp 0x22333
leaq 0x58(%rsp), %r12
movsd 0xbbe8d(%rip), %xmm1    # 0xde178
movsd 0xbbe7d(%rip), %xmm2    # 0xde170
movq %r12, %rdi
movq %r14, %rsi
callq 0x307da
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x22183
movq %rax, %r12
testq %rax, %rax
je 0x22328
testl %ebx, %ebx
jle 0x2237b
cmpl %ebx, 0x10(%r12)
jge 0x2237b
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x22462
movapd %xmm1, %xmm4
movapd %xmm2, %xmm1
addsd %xmm4, %xmm1
addsd %xmm3, %xmm2
leaq 0x58(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x307da
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x22183
movq %rax, %r15
leaq 0x58(%rsp), %rdi
callq 0x321ae
testq %r15, %r15
jne 0x22471
jmp 0x224e4
leaq 0x30(%rsp), %rdx
movq $0x0, (%rdx)
leaq 0x38(%rsp), %rdi
movsd 0xbbfe4(%rip), %xmm0    # 0xde378
movq %r12, %rsi
callq 0x22526
movsd 0x30(%rsp), %xmm0
ucomisd 0xbbd8e(%rip), %xmm0    # 0xde138
movsd 0x10(%rsp), %xmm3
movsd 0x8(%rsp), %xmm4
jbe 0x22403
movsd 0x48(%rsp), %xmm0
movapd 0xbc00a(%rip), %xmm1    # 0xde3d0
andpd %xmm0, %xmm1
ucomisd 0xbbd6e(%rip), %xmm1    # 0xde140
jbe 0x22403
movsd (%r14), %xmm2
movsd 0x8(%r14), %xmm1
mulsd 0x40(%rsp), %xmm1
mulsd 0x38(%rsp), %xmm2
addsd %xmm1, %xmm2
addsd 0x50(%rsp), %xmm2
xorpd 0xbbfa3(%rip), %xmm2    # 0xde3a0
divsd %xmm0, %xmm2
jmp 0x2240a
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 0x307da
leaq 0x90(%rsp), %rsi
movq %r12, %rdi
callq 0x22118
movq %rax, %r15
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
testq %r15, %r15
setne %r12b
leaq 0x90(%rsp), %rdi
callq 0x321ae
leaq 0x58(%rsp), %rdi
callq 0x321ae
testb %r12b, %r12b
je 0x224e4
testl %ebx, %ebx
jle 0x2247b
cmpl %ebx, 0x10(%r15)
jl 0x224db
xorpd %xmm0, %xmm0
movsd 0x28(%rsp), %xmm5
ucomisd %xmm0, %xmm5
jbe 0x224d1
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 0x228d7
movq %rax, %r14
testq %r15, %r15
je 0x224c9
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
movq %r14, %r15
testq %r14, %r14
je 0x224e4
testl %ebx, %ebx
jle 0x224e7
cmpl %ebx, 0x10(%r15)
jge 0x224e7
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 0x321ae
jmp 0x22514
jmp 0x22511
jmp 0x22511
jmp 0x22511
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x321ae
movq %rbx, %rdi
callq 0x138b0