// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Polygon.cpp
// Function: gaps::R2Polygon::Curvature(int, double) const
RNScalar R2Polygon::
Curvature(int k, RNLength radius) const
{
  // Just checking
  if (npoints < 3) return 0;

  // Find first point
  R2Point *pointA = &points[(k - 1 + npoints) % npoints];
  if (radius > 0) {
    R2Point *prev = pointA;
    RNLength distance = R2Distance(*pointA, points[k]);
    if (distance < radius) {
      for (int i = 2;  i < npoints/3; i++) {
        pointA = &points[(k - i + npoints) % npoints];
        distance += R2Distance(*pointA, *prev);
        if (distance >= radius) break; 
        prev = pointA;
      }
    }
  }

  // Find second point
  R2Point *pointB = &points[(k + 1) % npoints];
  if (radius > 0) {
    R2Point *prev = pointB;
    RNLength distance = R2Distance(*pointB, points[k]);
    if (distance < radius) {
      for (int i = 2;  i < npoints/3; i++) {
        pointB = &points[(k + i) % npoints];
        distance += R2Distance(*pointB, *prev);
        if (distance >= radius) break;
        prev = pointB;
      }
    }
  }

  // Compute curvature
  R2Vector va = *pointA - points[k];
  R2Vector vb = *pointB - points[k];
  RNLength lena = va.Length();
  RNLength lenb = vb.Length();
  if (RNIsZero(lena) || RNIsZero(lenb)) return RN_UNKNOWN;
  RNScalar angle = R2InteriorAngle(va, vb);
  RNScalar cross = (va[0]*vb[1] - va[1]*vb[0]);
  RNScalar sign = (cross > 0) ? -1 : 1;
  RNScalar curvature = sign * (RN_PI - angle) / (lena + lenb);

  // Flip if polygon is clockwise
  if (clockwise) curvature = -curvature;

  // Return curvature
  return curvature;
}

// Assembly:
movapd %xmm0, %xmm1
movl 0x10(%rdi), %ecx
xorpd %xmm0, %xmm0
cmpl $0x3, %ecx
jl 0x64754
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %r15d
movq %rdi, %rbx
movq 0x8(%rdi), %r13
leal (%r15,%rcx), %eax
decl %eax
cltd
idivl %ecx
movslq %edx, %r14
shlq $0x4, %r14
addq %r13, %r14
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x64588
movslq %r15d, %r12
movq %r12, %rbp
shlq $0x4, %rbp
addq %rbp, %r13
movq %r14, %rdi
movq %r13, %rsi
movsd %xmm1, (%rsp)
callq 0x6c52b
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x645a1
movq %rbp, 0x10(%rsp)
movl 0x10(%rbx), %ecx
cmpl $0x9, %ecx
jl 0x6457a
leal -0x2(%r15), %r13d
movl $0x2, %ebp
movsd %xmm0, 0x8(%rsp)
movq %r14, %rsi
leal (%rcx,%r13), %eax
cltd
idivl %ecx
movslq %edx, %r14
shlq $0x4, %r14
addq 0x8(%rbx), %r14
movq %r14, %rdi
callq 0x6c52b
addsd 0x8(%rsp), %xmm0
ucomisd (%rsp), %xmm0
movl 0x10(%rbx), %ecx
jae 0x6457a
incl %ebp
movslq %ecx, %rax
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rax
addl %edx, %eax
decl %r13d
cmpl %eax, %ebp
jl 0x64527
leal 0x1(%r15), %eax
cltd
idivl %ecx
movq 0x10(%rsp), %rbp
jmp 0x645a9
leal 0x1(%r15), %eax
cltd
idivl %ecx
movslq %edx, %rax
shlq $0x4, %rax
addq %rax, %r13
movslq %r15d, %r12
jmp 0x64634
leal 0x1(%r15), %eax
cltd
idivl 0x10(%rbx)
movq 0x8(%rbx), %rax
movslq %edx, %r13
shlq $0x4, %r13
addq %rax, %r13
addq %rax, %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x6c52b
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %al
movl 0x10(%rbx), %ecx
cmpl $0x9, %ecx
setge %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x64634
movl $0x2, %ebp
movq %r13, %rsi
movsd %xmm0, 0x8(%rsp)
leal (%r15,%rbp), %eax
cltd
idivl %ecx
movslq %edx, %r13
shlq $0x4, %r13
addq 0x8(%rbx), %r13
movq %r13, %rdi
callq 0x6c52b
addsd 0x8(%rsp), %xmm0
ucomisd (%rsp), %xmm0
jae 0x64634
incl %ebp
movslq 0x10(%rbx), %rcx
imulq $0x55555556, %rcx, %rax # imm = 0x55555556
movq %rax, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rax
addl %edx, %eax
cmpl %eax, %ebp
jl 0x645e6
shlq $0x4, %r12
movq 0x8(%rbx), %rdx
addq %r12, %rdx
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x682e3
addq 0x8(%rbx), %r12
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x682e3
movq %r15, %rdi
callq 0x68476
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0x68476
movsd 0x8(%rsp), %xmm3
movapd %xmm0, %xmm4
leaq 0x8150b(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
movapd 0x3d987(%rip), %xmm2    # 0xa2020
xorpd %xmm1, %xmm2
ucomisd %xmm2, %xmm3
movsd 0x3d99f(%rip), %xmm0    # 0xa2048
jb 0x646b5
ucomisd %xmm3, %xmm1
jae 0x64746
ucomisd %xmm2, %xmm4
jb 0x646c5
ucomisd %xmm4, %xmm1
jae 0x64746
leaq 0x28(%rsp), %r14
leaq 0x18(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movsd %xmm4, (%rsp)
callq 0x6d0b8
movsd (%r14), %xmm2
movsd (%r15), %xmm1
mulsd 0x8(%r14), %xmm1
mulsd 0x8(%r15), %xmm2
subsd %xmm1, %xmm2
leaq 0x458c8(%rip), %rax     # 0xa9fc8
movsd (%rax), %xmm3
subsd %xmm0, %xmm3
movapd 0x3d910(%rip), %xmm1    # 0xa2020
movapd %xmm3, %xmm4
xorpd %xmm1, %xmm4
xorpd %xmm0, %xmm0
cmpltsd %xmm2, %xmm0
andpd %xmm0, %xmm4
andnpd %xmm3, %xmm0
orpd %xmm4, %xmm0
movsd 0x8(%rsp), %xmm2
addsd (%rsp), %xmm2
divsd %xmm2, %xmm0
cmpl $0x0, 0x40(%rbx)
je 0x64746
xorpd %xmm1, %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop