// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rsi, %r14
movq %rdi, %rbp
cmpl $0x3, 0x10(%rsi)
jg 0x64844
movq %rbp, %rdi
movq %r14, %rsi
callq 0x64230
movq %rbp, %rax
jmp 0x64af3
movq %rbp, %rdi
callq 0x641d8
leaq 0x20(%rbp), %rdi
leaq 0x483681(%rip), %r15    # 0x4e7ed8
leaq 0x8(%r15), %rsi
callq 0x68f66
leaq 0x30(%rbp), %rdi
addq $0x18, %r15
movq %r15, %rsi
callq 0x68f66
movl $0x0, 0x40(%rbp)
movslq 0x10(%r14), %r13
movq %r13, %rbx
shlq $0x4, %rbx
testq %r13, %r13
movq $-0x1, %r15
cmovnsq %rbx, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r12
testl %r13d, %r13d
je 0x648b5
xorl %r13d, %r13d
leaq (%r12,%r13), %rdi
callq 0x68e2c
addq $0x10, %r13
cmpq %r13, %rbx
jne 0x648a3
movq %r12, 0x8(%rbp)
movl $0x0, 0x10(%rbp)
movslq 0x10(%r14), %rax
xorl %ebx, %ebx
cmpq $0x2, %rax
jl 0x64900
movq 0x8(%r14), %rcx
leaq 0x10(%rcx), %rdx
xorl %edi, %edi
movl $0x1, %esi
movslq %edi, %r8
shlq $0x4, %r8
movsd (%rcx,%r8), %xmm0
ucomisd (%rdx), %xmm0
movl %esi, %ebx
ja 0x648f2
movl %edi, %ebx
incq %rsi
addq $0x10, %rdx
movl %ebx, %edi
cmpq %rsi, %rax
jne 0x648db
movq %rbp, 0x8(%rsp)
cmpl $0x0, 0x10(%r14)
jle 0x64aee
movq 0x8(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x20(%rsp)
movl %ebx, %eax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
leaq 0x88(%rsp), %rbp
movl %ebx, %r13d
movl %ebx, 0x1c(%rsp)
movslq %r13d, %r12
shlq $0x4, %r12
movq 0x8(%r14), %rsi
addq %r12, %rsi
movq 0x8(%rsp), %rcx
movslq 0x10(%rcx), %rdi
leal 0x1(%rdi), %eax
movl %eax, 0x10(%rcx)
shlq $0x4, %rdi
addq 0x8(%rcx), %rdi
callq 0x68f66
movq 0x8(%r14), %rsi
addq %r12, %rsi
movq 0x20(%rsp), %rdi
callq 0x66714
addq 0x8(%r14), %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0x68e38
leal 0x1(%r13), %eax
cltd
idivl 0x10(%r14)
movl %edx, %r12d
movslq %edx, %rdx
shlq $0x4, %rdx
addq 0x8(%r14), %rdx
leaq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x689ba
movq %r15, 0x28(%rsp)
cmpl $0x0, 0x10(%r14)
jle 0x64ac6
movl %r13d, %r15d
xorl %ebx, %ebx
xorl %r13d, %r13d
movl %r12d, %eax
movq %rax, 0x10(%rsp)
leaq 0x70(%rsp), %r12
cmpq %r13, %r15
je 0x64ab0
movq 0x8(%r14), %rsi
addq %rbx, %rsi
cmpq %r13, 0x30(%rsp)
jne 0x64a42
leaq 0x60(%rsp), %rdi
callq 0x6d501
leaq 0x811a6(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
mulsd 0x44e3a(%rip), %xmm1    # 0xa9838
addsd %xmm0, %xmm1
ucomisd 0x3d7de(%rip), %xmm1    # 0xa21e8
jbe 0x64ab0
movq 0x8(%r14), %rdx
addq %rbx, %rdx
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x689ba
leaq 0x60(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x6950a
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x6950a
jmp 0x64a99
movq %rbp, %rdi
callq 0x579d5
testl %eax, %eax
jne 0x64ab0
movq 0x8(%r14), %rsi
addq %rbx, %rsi
leaq 0x60(%rsp), %rdi
callq 0x6d501
ucomisd 0x3d781(%rip), %xmm0    # 0xa21e8
jbe 0x64ab0
movq 0x8(%r14), %rdx
addq %rbx, %rdx
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x689ba
leaq 0x60(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x6950a
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x6950a
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movl %r13d, %eax
movq %rax, 0x10(%rsp)
incq %r13
movslq 0x10(%r14), %rax
addq $0x10, %rbx
cmpq %rax, %r13
jl 0x649ca
jmp 0x64ace
movl %r12d, %eax
movq %rax, 0x10(%rsp)
movl 0x1c(%rsp), %ebx
movq 0x10(%rsp), %r13
cmpl %ebx, %r13d
movq 0x28(%rsp), %r15
je 0x64aee
incl %r15d
cmpl 0x10(%r14), %r15d
jl 0x64937
movq 0x8(%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x64b30
jmp 0x64b28
jmp 0x64b28
jmp 0x64b28
movq %rax, %r14
movq %r12, %rdi
movq %r15, %rsi
callq 0xe8c0
jmp 0x64b30
jmp 0x64b28
jmp 0x64b28
jmp 0x64b28
jmp 0x64b28
movq %rax, %r14
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x6451e
movq %r14, %rdi
callq 0xe7e0