// File: 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:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x60(%rsp)
movq %rdi, %rax
movq %rax, 0x68(%rsp)
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movq 0x148(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpl $0x3, 0x10(%rax)
jg 0xba73b
movq 0x70(%rsp), %rsi
movq 0x60(%rsp), %rdi
callq 0xb9ab0
jmp 0xbac6b
movq 0x60(%rsp), %rdi
movb $0x0, 0x147(%rsp)
callq 0xb9a30
movq 0x60(%rsp), %rdi
addq $0x18, %rdi
leaq 0x4c028b(%rip), %rsi    # 0x57a9e8
callq 0xa5da0
jmp 0xba764
movq 0x70(%rsp), %rax
movq 0x60(%rsp), %rcx
movl $0x0, 0x40(%rcx)
movslq 0x10(%rax), %rax
movq %rax, 0x48(%rsp)
movl $0x10, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
movq %rdi, 0x50(%rsp)
callq 0xf030
movq %rax, 0x58(%rsp)
jmp 0xba7a5
movq 0x48(%rsp), %rax
cmpq $0x0, %rax
je 0xba7f7
movq 0x58(%rsp), %rax
movq 0x48(%rsp), %rdx
shlq $0x4, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xc3130
jmp 0xba7df
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rax
addq $0x10, %rax
cmpq %rcx, %rax
movq %rax, 0x40(%rsp)
jne 0xba7ce
movq 0x60(%rsp), %rax
movq 0x58(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x0, 0x10(%rax)
movl $0x0, 0x130(%rsp)
movl $0x1, 0x12c(%rsp)
movq 0x70(%rsp), %rcx
movl 0x12c(%rsp), %eax
cmpl 0x10(%rcx), %eax
jge 0xba910
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rdi
movslq 0x12c(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0xa5b60
movq %rax, 0x28(%rsp)
jmp 0xba85d
movq 0x70(%rsp), %rax
movq 0x28(%rsp), %rcx
movsd (%rcx), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x8(%rax), %rdi
movslq 0x130(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0xa5b60
movq %rax, 0x20(%rsp)
jmp 0xba892
movsd 0x18(%rsp), %xmm1
movq 0x20(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xba8f8
movl 0x12c(%rsp), %eax
movl %eax, 0x130(%rsp)
jmp 0xba8f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
jmp 0xbac5f
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
callq 0xf990
jmp 0xbac5f
jmp 0xba8fa
movl 0x12c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x12c(%rsp)
jmp 0xba822
movl 0x130(%rsp), %eax
movl %eax, 0x128(%rsp)
movl $0x0, 0x124(%rsp)
movq 0x70(%rsp), %rcx
movl 0x124(%rsp), %eax
cmpl 0x10(%rcx), %eax
jge 0xbac41
movq 0x60(%rsp), %rcx
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rsi
movslq 0x128(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq 0x8(%rcx), %rdi
movslq 0x10(%rcx), %rax
movl %eax, %edx
incl %edx
movl %edx, 0x10(%rcx)
shlq $0x4, %rax
addq %rax, %rdi
callq 0xc33e0
jmp 0xba978
movq 0x70(%rsp), %rax
movq 0x60(%rsp), %rdi
addq $0x18, %rdi
movq 0x8(%rax), %rsi
movslq 0x128(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0xbe470
jmp 0xba9a0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rsi
movslq 0x128(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0x110(%rsp), %rdi
callq 0xc3170
jmp 0xba9c7
movq 0x70(%rsp), %rcx
movl 0x128(%rsp), %eax
movl %eax, 0x10c(%rsp)
movl 0x10c(%rsp), %eax
incl %eax
movl 0x10(%rcx), %ecx
cltd
idivl %ecx
movq 0x70(%rsp), %rax
movl %edx, 0x128(%rsp)
movq 0x8(%rax), %rdx
movslq 0x128(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdx
leaq 0xe0(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0xc29c0
jmp 0xbaa1f
movl $0x0, 0xdc(%rsp)
movq 0x70(%rsp), %rcx
movl 0xdc(%rsp), %eax
cmpl 0x10(%rcx), %eax
jge 0xbac17
movl 0xdc(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jne 0xbaa54
jmp 0xbac01
movl 0xdc(%rsp), %eax
cmpl 0x130(%rsp), %eax
jne 0xbab2a
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rsi
movslq 0xdc(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0xe0(%rsp), %rdi
callq 0xcb900
movsd %xmm0, 0x10(%rsp)
jmp 0xbaa95
movsd 0x10(%rsp), %xmm1
leaq 0xbda3e(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm0
movsd 0x7b71a(%rip), %xmm2    # 0x1361c8
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0xd0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xbab25
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rdx
movslq 0xdc(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdx
leaq 0xa8(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0xc29c0
jmp 0xbab00
leaq 0xe0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xbd200
jmp 0xbab17
movl 0xdc(%rsp), %eax
movl %eax, 0x128(%rsp)
jmp 0xbabff
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rsi
movslq 0xdc(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0x110(%rsp), %rdi
callq 0xa17a0
movl %eax, 0xc(%rsp)
jmp 0xbab55
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
je 0xbab63
jmp 0xbac01
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rsi
movslq 0xdc(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0xe0(%rsp), %rdi
callq 0xcb900
movsd %xmm0, (%rsp)
jmp 0xbab8f
movsd (%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xbabfd
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rdx
movslq 0xdc(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdx
leaq 0x78(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0xc29c0
jmp 0xbabdb
leaq 0xe0(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xbd200
jmp 0xbabef
movl 0xdc(%rsp), %eax
movl %eax, 0x128(%rsp)
jmp 0xbabff
jmp 0xbac01
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0xbaa2a
movl 0x128(%rsp), %eax
cmpl 0x130(%rsp), %eax
jne 0xbac29
jmp 0xbac41
jmp 0xbac2b
movl 0x124(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0xba929
movb $0x1, 0x147(%rsp)
testb $0x1, 0x147(%rsp)
jne 0xbac5d
movq 0x60(%rsp), %rdi
callq 0xba090
jmp 0xbac6b
movq 0x60(%rsp), %rdi
callq 0xba090
jmp 0xbac78
movq 0x68(%rsp), %rax
addq $0x158, %rsp            # imm = 0x158
retq
movq 0x138(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)