// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Polygon.cpp
// Function: gaps::R2Polygon::ConvexHull() const
const R2Polygon R2Polygon::
ConvexHull(void) const
{
  // Check number of points
  if (npoints <= 3) return *this;

  // Create convex hull polygon
  R2Polygon convex_hull;
  convex_hull.bbox = R2null_box;
  convex_hull.clockwise = FALSE;
  convex_hull.points = new R2Point [ npoints ];
  convex_hull.npoints = 0;

  // Find index of leftmost point
  int leftmost_index = 0;
  for (int i = 1; i < npoints; i++) {
    if (points[i][0] < points[leftmost_index][0]) {
      leftmost_index = i;
    }
  }

  // Iteratively find next vertex for convex hull via gift-wrapping
  int index = leftmost_index;
  for (int i = 0; i < npoints; i++) {
    // Insert convex hull point
    convex_hull.points[convex_hull.npoints++] = points[index];
    convex_hull.bbox.Union(points[index]);
    R2Point last_point = points[index];
    int last_index = index;

    // Find next giftwrapping point index
    index = (last_index+1)%npoints;
    R2Line line(last_point, points[index]);
    for (int j = 0; j < npoints; j++) {
      if (j == last_index) continue;
      if (j == leftmost_index) {
        RNScalar d = R2SignedDistance(line, points[j]) + 1000 * RN_EPSILON;
        if (d > 0) {
          line = R2Line(last_point, points[j]);
          index = j;
        }
      }
      else {
        if (R2Contains(last_point, points[j])) continue;
        RNScalar d = R2SignedDistance(line, points[j]);
        if (d > 0) {
          line = R2Line(last_point, points[j]);
          index = j;
        }
      }
    }

    // Check if completed loop
    if (index == leftmost_index) break;
  }

  // Return the convex polygon
  return convex_hull;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r14
movq %rdi, %rbp
cmpl $0x3, 0x10(%rsi)
jg 0x5b415
movq %rbp, %rdi
movq %r14, %rsi
callq 0x5ae24
jmp 0x5b667
movq %rbp, %rdi
callq 0x5add6
leaq 0x18(%rbp), %rdi
leaq 0x481ac0(%rip), %rsi    # 0x4dcee8
movq %rdi, 0x18(%rsp)
callq 0x51550
andl $0x0, 0x40(%rbp)
movslq 0x10(%r14), %r15
movq %r15, %rbx
shlq $0x4, %rbx
testq %r15, %r15
pushq $-0x1
popq %r12
cmovnsq %rbx, %r12
movq %r12, %rdi
callq 0xe030
movq %rax, %r13
movl $0x0, %eax
testl %r15d, %r15d
je 0x5b47a
xorl %r15d, %r15d
leaq (%r15,%r13), %rdi
callq 0x5f58a
addq $0x10, %r15
cmpq %r15, %rbx
jne 0x5b464
movl 0x10(%r14), %eax
movq %r13, 0x8(%rbp)
andl $0x0, 0x10(%rbp)
movq 0x8(%r14), %rcx
movslq %eax, %rdx
leaq 0x10(%rcx), %rsi
pushq $0x1
popq %rdi
xorl %r12d, %r12d
cmpq %rdx, %rdi
jge 0x5b4bd
movslq %r12d, %r8
shlq $0x4, %r8
movsd (%rcx,%r8), %xmm0
ucomisd (%rsi), %xmm0
movl %edi, %r8d
ja 0x5b4b1
movl %r12d, %r8d
incq %rdi
addq $0x10, %rsi
movl %r8d, %r12d
jmp 0x5b493
movl %r12d, %ebx
shlq $0x4, %rbx
xorl %r13d, %r13d
movl %r12d, %ecx
movq %rbp, (%rsp)
movl %r12d, 0x14(%rsp)
cmpl %eax, %r13d
jge 0x5b667
movq %rcx, 0x8(%rsp)
movslq %ecx, %r15
shlq $0x4, %r15
movq 0x8(%r14), %rsi
addq %r15, %rsi
movslq 0x10(%rbp), %rdi
leal 0x1(%rdi), %eax
movl %eax, 0x10(%rbp)
shlq $0x4, %rdi
addq 0x8(%rbp), %rdi
callq 0x5f6c4
movq 0x8(%r14), %rsi
addq %r15, %rsi
movq 0x18(%rsp), %rdi
callq 0x5d088
addq 0x8(%r14), %r15
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x5f596
movq %r13, 0x20(%rsp)
movq 0x8(%rsp), %r12
leal 0x1(%r12), %eax
cltd
idivl 0x10(%r14)
movl %edx, %r15d
movslq %edx, %rdx
shlq $0x4, %rdx
addq 0x8(%r14), %rdx
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x5f1a4
movl %r12d, %r12d
shlq $0x4, %r12
xorl %r13d, %r13d
xorl %ebp, %ebp
movl %r15d, %eax
movq %rax, 0x8(%rsp)
movslq 0x10(%r14), %rax
cmpq %rax, %rbp
jge 0x5b648
cmpq %r13, %r12
je 0x5b63c
movq 0x8(%r14), %rsi
cmpq %r13, %rbx
jne 0x5b5e3
addq %rbx, %rsi
leaq 0x38(%rsp), %rdi
callq 0x63638
leaq 0x7f607(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm1
mulsd 0x3f21b(%rip), %xmm1    # 0x9a7c8
addsd %xmm0, %xmm1
ucomisd 0x37c2f(%rip), %xmm1    # 0x931e8
leaq 0x60(%rsp), %r15
jbe 0x5b63c
movq 0x8(%r14), %rdx
addq %rbx, %rdx
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
callq 0x5f1a4
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x5c9aa
jmp 0x5b635
addq %r13, %rsi
leaq 0x28(%rsp), %rdi
callq 0x4f809
testl %eax, %eax
jne 0x5b63c
movq 0x8(%r14), %rsi
addq %r13, %rsi
leaq 0x38(%rsp), %rdi
callq 0x63638
ucomisd 0x37bdb(%rip), %xmm0    # 0x931e8
leaq 0x60(%rsp), %r15
jbe 0x5b63c
movq 0x8(%r14), %rdx
addq %r13, %rdx
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
callq 0x5f1a4
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x5c9aa
movl %ebp, %eax
movq %rax, 0x8(%rsp)
incq %rbp
addq $0x10, %r13
jmp 0x5b56e
movq 0x20(%rsp), %r13
incl %r13d
movl 0x14(%rsp), %r12d
movq 0x8(%rsp), %rcx
cmpl %r12d, %ecx
movq (%rsp), %rbp
jne 0x5b4d3
movq %rbp, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x5b6a6
jmp 0x5b69f
jmp 0x5b69f
movq %rax, %r14
movq %r13, %rdi
movq %r12, %rsi
callq 0xe8f0
jmp 0x5b6a6
jmp 0x5b69f
jmp 0x5b69f
jmp 0x5b69f
jmp 0x5b69f
jmp 0x5b69f
movq %rax, %r14
movq (%rsp), %rbp
movq %rbp, %rdi
callq 0x5b0fa
movq %r14, %rdi
callq 0xe810