// File: /workspace/github_cpp_makefile_star2_O2/hzeller[P]gcode-multi-shell-extrude/polygon-offset.cc
// Function: PolygonOffset(std::vector<Vector2D, std::allocator<Vector2D>> const&, double, OffsetType)
Polygon PolygonOffset(const Polygon &polygon, double offset,
                      OffsetType type) {
  // Converting float to clipper integer values. Make sure
  // to stay within limits.
  const float kResolution = 1e4;
  const float kAccuracy = 0.01; // mm : cutting corners with this accuracy

  ClipperLib::Path path;
  Vector2D centroid;
  for (const Vector2D &p : polygon) {
    Vector2D clipper_point = p * kResolution;
    path.push_back(ClipperLib::IntPoint(clipper_point.x, clipper_point.y));
    centroid = centroid + clipper_point;
  }
  centroid = centroid / polygon.size();

  ClipperLib::Paths solutions;
  ClipperLib::ClipperOffset co(2.0, kAccuracy * kResolution);
  ClipperLib::JoinType join = ClipperLib::jtRound;
  switch (type) {
  case kOffsetRound:  join = ClipperLib::jtRound; break;
  case kOffsetSquare: join = ClipperLib::jtSquare; break;
  case kOffsetMiter:  join = ClipperLib::jtMiter; break;
  }
  co.AddPath(path, join, ClipperLib::etClosedPolygon);
  co.Execute(solutions, kResolution * offset);

  if (solutions.size() == 0)  // Nothing left.
    return Polygon();

  // A polygon might become pieces when offset. Use the one that is centered.
  ClipperLib::Path &centered_polygon = solutions[0];
  for (const ClipperLib::Path &solution : solutions) {
    if (is_centered(centroid, solution)) {
      centered_polygon = solution;
      break;
    }
  }

  Polygon tmp;
  for (const ClipperLib::IntPoint &p : centered_polygon) {
    tmp.push_back(Vector2D(p.X / kResolution, p.Y / kResolution));
  }

  // The way the clipper library works, the offset polygon might start at a
  // different point - after all, it is a different polygon.
  // Let's try to find the one that is closest to the start of the input polygon
  // by looking for the closest point to the line represented by the start-vector,
  // i.e. closest point in the same direction.
  const Vector2D &reference = polygon[0];
  double smallest = -1;
  std::size_t offset_index = 0;
  for (std::size_t i = 0; i < tmp.size(); ++i) {
    const Vector2D &p = tmp[i];
#if 0
    // TODO: find center and make line relative to that.
    // Then use http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_two_points
    const double dist = abs(reference.y * p.x - reference.x * p.y);
    const bool same_side = (reference.x * p.x >= 0) && (reference.y * p.y >= 0);
#else
    const double dist = distance(p.x - reference.x, p.y - reference.y, 0);
    const bool same_side = true;
#endif
    if (i == 0 || (dist < smallest && same_side)) {
      offset_index = i;
      smallest = dist;
    }
  }

  // .. then create the result by shifting that.
  Polygon result;
  for (std::size_t i = 0; i < tmp.size(); ++i) {
    result.push_back(tmp[(i + offset_index) % tmp.size()]);
  }
  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movl %edx, %ebp
movsd %xmm0, 0x58(%rsp)
movq %rdi, %rbx
xorpd %xmm0, %xmm0
leaq 0x70(%rsp), %r15
movapd %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movq (%rsi), %r13
movq %rsi, 0x8(%rsp)
movq 0x8(%rsi), %r14
leaq 0xa0(%rsp), %r12
cmpq %r14, %r13
movapd %xmm0, 0x60(%rsp)
je 0x631d
movupd (%r13), %xmm0
mulpd 0xcca4(%rip), %xmm0     # 0x12f80
cvttsd2si %xmm0, %rax
movapd %xmm0, 0x10(%rsp)
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
cvttsd2si %xmm0, %rcx
movq %rax, 0xa0(%rsp)
movq %rcx, 0xa8(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x6770
movapd 0x60(%rsp), %xmm0
addpd 0x10(%rsp), %xmm0
addq $0x10, %r13
jmp 0x62c3
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x4, %rax
js 0x6336
cvtsi2ss %rax, %xmm0
jmp 0x634b
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
leaq 0xa0(%rsp), %rdi
movsd 0xbd31(%rip), %xmm0     # 0x120a0
movsd 0xcc29(%rip), %xmm1     # 0x12fa0
callq 0xcdb4
xorl %edx, %edx
cmpl $0x1, %ebp
setne %dl
cmpl $0x2, %ebp
cmovel %ebp, %edx
leaq 0xa0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %ecx, %ecx
callq 0xceb8
movsd 0x58(%rsp), %xmm0
mulsd 0xcbfc(%rip), %xmm0     # 0x12fa8
leaq 0xa0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0xd208
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %rax
cmpq %r15, %rax
je 0x6481
cvtss2sd 0x10(%rsp), %xmm1
movapd 0x60(%rsp), %xmm2
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
divsd %xmm1, %xmm2
movq %r15, %rsi
cmpq %rax, %rsi
je 0x649b
movq (%rsi), %r8
movq 0x8(%rsi), %r9
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x80000000, %edi       # imm = 0x80000000
movl $0x80000000, %ecx       # imm = 0x80000000
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
cmpq %r9, %r8
je 0x644f
movslq %r10d, %r11
movq (%r8), %r14
movq 0x8(%r8), %r12
cmpq %r11, %r14
cmovll %r14d, %r10d
movslq %edi, %r11
cmpq %r11, %r14
cmovgl %r14d, %edi
movslq %edx, %r11
cmpq %r11, %r12
cmovll %r12d, %edx
movslq %ecx, %r11
cmpq %r11, %r12
cmovgl %r12d, %ecx
addq $0x10, %r8
jmp 0x6415
cvtsi2sd %r10d, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6478
cvtsi2sd %edi, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x6478
cvtsi2sd %edx, %xmm1
ucomisd %xmm1, %xmm2
jbe 0x6478
cvtsi2sd %ecx, %xmm1
ucomisd %xmm2, %xmm1
ja 0x6493
addq $0x18, %rsi
jmp 0x63f0
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
jmp 0x65c4
movq %r15, %rdi
callq 0x664e
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %r12
movapd %xmm0, (%r12)
andq $0x0, 0x10(%r12)
movq (%r15), %r14
movq 0x8(%r15), %r13
leaq 0x90(%rsp), %r15
cmpq %r13, %r14
je 0x64f6
cvtsi2ssq 0x8(%r14), %xmm0
cvtsi2ssq (%r14), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
divps 0xcab7(%rip), %xmm1     # 0x12f90
cvtps2pd %xmm1, %xmm0
movapd %xmm0, 0x90(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x5b92
addq $0x10, %r14
jmp 0x64bf
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rcx
movq %rcx, %rdx
subq %rsi, %rdx
sarq $0x4, %rdx
leaq 0x8(%rsi), %rdi
movsd 0xbb42(%rip), %xmm0     # 0x12060
xorl %r14d, %r14d
xorl %r8d, %r8d
cmpq %r8, %rdx
je 0x6577
movsd -0x8(%rdi), %xmm1
movsd (%rdi), %xmm2
subsd (%rax), %xmm1
subsd 0x8(%rax), %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
sqrtsd %xmm1, %xmm1
testq %r8, %r8
sete %r9b
ucomisd %xmm1, %xmm0
seta %r10b
orb %r9b, %r10b
testb %r10b, %r10b
jne 0x6566
movapd %xmm0, %xmm1
cmovneq %r8, %r14
incq %r8
addq $0x10, %rdi
movapd %xmm1, %xmm0
jmp 0x6524
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
xorl %r15d, %r15d
subq %rsi, %rcx
sarq $0x4, %rcx
cmpq %rcx, %r15
jae 0x65ba
leaq (%r14,%r15), %rax
xorl %edx, %edx
divq %rcx
shlq $0x4, %rdx
addq %rdx, %rsi
movq %rbx, %rdi
callq 0x5658
incq %r15
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rcx
jmp 0x6587
leaq 0x20(%rsp), %rdi
callq 0x5b22
leaq 0xa0(%rsp), %rdi
callq 0xce06
leaq 0x40(%rsp), %rdi
callq 0x671a
leaq 0x70(%rsp), %rdi
callq 0x673c
movq %rbx, %rax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6601
movq %rax, %r14
jmp 0x662d
movq %rax, %r14
jmp 0x6620
movq %rax, %r14
movq %rbx, %rdi
callq 0x5b22
jmp 0x6616
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0x5b22
leaq 0xa0(%rsp), %rdi
callq 0xce06
leaq 0x40(%rsp), %rdi
callq 0x671a
jmp 0x663c
movq %rax, %r14
leaq 0x70(%rsp), %rdi
callq 0x673c
movq %r14, %rdi
callq 0x33a0