// File: /workspace/github_cpp_makefile_star2_O3/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 $0x188, %rsp            # imm = 0x188
movl %edx, %ebp
movsd %xmm0, 0x78(%rsp)
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq (%rsi), %r13
movq %rsi, 0x38(%rsp)
movq 0x8(%rsi), %r14
cmpq %r14, %r13
je 0x6d66
movapd %xmm0, 0x50(%rsp)
leaq 0x20(%rsp), %r15
leaq 0x90(%rsp), %r12
movupd (%r13), %xmm0
mulpd 0xe283(%rip), %xmm0     # 0x14f70
cvttsd2si %xmm0, %rax
movapd %xmm0, 0x40(%rsp)
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
cvttsd2si %xmm0, %rcx
movq %rax, 0x90(%rsp)
movq %rcx, 0x98(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x6d32
movupd 0x90(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, 0x28(%rsp)
jmp 0x6d3d
movq %r15, %rdi
movq %r12, %rdx
callq 0x72ce
movapd 0x50(%rsp), %xmm0
addpd 0x40(%rsp), %xmm0
movapd %xmm0, 0x50(%rsp)
addq $0x10, %r13
cmpq %r14, %r13
jne 0x6cdf
movq 0x38(%rsp), %rax
movq (%rax), %r13
movq 0x8(%rax), %rax
jmp 0x6d6f
movapd %xmm0, 0x50(%rsp)
movq %r13, %rax
subq %r13, %rax
sarq $0x4, %rax
js 0x6d82
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x6d9a
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
leaq 0x90(%rsp), %rdi
movsd 0xd2f7(%rip), %xmm0     # 0x140b8
movsd 0xe1c7(%rip), %xmm1     # 0x14f90
callq 0xe872
xorl %edx, %edx
cmpl $0x1, %ebp
setne %dl
cmpl $0x2, %ebp
cmovel %ebp, %edx
leaq 0x90(%rsp), %rdi
leaq 0x20(%rsp), %rsi
xorl %ecx, %ecx
callq 0xe9fc
movsd 0x78(%rsp), %xmm0
mulsd 0xe19a(%rip), %xmm0     # 0x14f98
leaq 0x90(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xef3e
movq 0x60(%rsp), %r15
movq 0x68(%rsp), %rax
cmpq %r15, %rax
je 0x6f0a
xorps %xmm1, %xmm1
cvtss2sd 0x40(%rsp), %xmm1
movapd 0x50(%rsp), %xmm2
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
divsd %xmm1, %xmm2
movapd %xmm2, %xmm7
movsd 0xe152(%rip), %xmm1     # 0x14fa0
movsd 0xe152(%rip), %xmm2     # 0x14fa8
movq %r15, %rsi
movq (%rsi), %rcx
movq 0x8(%rsi), %rdx
movapd %xmm2, %xmm5
movapd %xmm2, %xmm3
movapd %xmm1, %xmm4
movapd %xmm1, %xmm6
cmpq %rdx, %rcx
je 0x6ee3
movl $0x80000000, %edi       # imm = 0x80000000
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl $0x80000000, %r10d      # imm = 0x80000000
movslq %r8d, %r8
movq (%rcx), %r11
movq 0x8(%rcx), %r14
cmpq %r8, %r11
cmovll %r11d, %r8d
movslq %r10d, %r10
cmpq %r10, %r11
cmovgl %r11d, %r10d
movslq %r9d, %r9
cmpq %r9, %r14
cmovll %r14d, %r9d
movslq %edi, %rdi
cmpq %rdi, %r14
cmovgl %r14d, %edi
addq $0x10, %rcx
cmpq %rdx, %rcx
jne 0x6e8c
xorps %xmm6, %xmm6
cvtsi2sd %r8d, %xmm6
xorps %xmm5, %xmm5
cvtsi2sd %r10d, %xmm5
xorps %xmm4, %xmm4
cvtsi2sd %r9d, %xmm4
xorps %xmm3, %xmm3
cvtsi2sd %edi, %xmm3
ucomisd %xmm6, %xmm0
jbe 0x6efb
ucomisd %xmm0, %xmm5
jbe 0x6efb
ucomisd %xmm4, %xmm7
jbe 0x6efb
ucomisd %xmm7, %xmm3
ja 0x6f1f
addq $0x18, %rsi
cmpq %rax, %rsi
jne 0x6e59
jmp 0x6f27
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
jmp 0x70ce
movq %r15, %rdi
callq 0x718a
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq (%r15), %r14
movq 0x8(%r15), %r13
cmpq %r13, %r14
je 0x70aa
movq %rsp, %r15
leaq 0x80(%rsp), %r12
xorps %xmm0, %xmm0
cvtsi2ssq 0x8(%r14), %xmm0
xorps %xmm1, %xmm1
cvtsi2ssq (%r14), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
divps 0xe011(%rip), %xmm1     # 0x14f80
cvtps2pd %xmm1, %xmm0
movapd %xmm0, 0x80(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x6f9c
movapd 0x80(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, 0x8(%rsp)
jmp 0x6fa7
movq %r15, %rdi
movq %r12, %rdx
callq 0x650a
addq $0x10, %r14
cmpq %r13, %r14
jne 0x6f54
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, %rcx
subq %rdi, %rcx
je 0x70ac
sarq $0x4, %rcx
movq 0x38(%rsp), %rdx
movq (%rdx), %rdx
movsd (%rdx), %xmm0
movsd 0x8(%rdx), %xmm1
cmpq $0x1, %rcx
movq %rcx, %rdx
adcq $0x0, %rdx
leaq 0x8(%rdi), %rsi
movsd 0xd09f(%rip), %xmm2     # 0x14090
xorl %r8d, %r8d
xorl %r14d, %r14d
movsd -0x8(%rsi), %xmm3
subsd %xmm0, %xmm3
movsd (%rsi), %xmm4
subsd %xmm1, %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm3, %xmm3
addsd %xmm4, %xmm3
sqrtsd %xmm3, %xmm3
testq %r8, %r8
sete %r9b
ucomisd %xmm3, %xmm2
seta %r10b
orb %r9b, %r10b
testb %r10b, %r10b
jne 0x7033
movapd %xmm2, %xmm3
cmovneq %r8, %r14
incq %r8
addq $0x10, %rsi
movapd %xmm3, %xmm2
cmpq %r8, %rdx
jne 0x6ff7
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
cmpq %rdi, %rax
je 0x70c1
xorl %r15d, %r15d
leaq (%r14,%r15), %rax
xorl %edx, %edx
divq %rcx
shlq $0x4, %rdx
addq %rdi, %rdx
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x7088
movupd (%rdx), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, 0x8(%rbx)
jmp 0x7090
movq %rbx, %rdi
callq 0x662e
incq %r15
movq (%rsp), %rdi
movq 0x8(%rsp), %rcx
subq %rdi, %rcx
sarq $0x4, %rcx
cmpq %rcx, %r15
jb 0x705f
jmp 0x70bc
xorl %edi, %edi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
testq %rdi, %rdi
je 0x70ce
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
leaq 0x90(%rsp), %rdi
callq 0xe8ca
leaq 0x60(%rsp), %rdi
callq 0x727a
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x70fc
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
movq %rbx, %rax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7131
movq %rax, %r14
jmp 0x715c
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x7139
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x3220
jmp 0x7139
movq %rax, %r14
jmp 0x714f
movq %rax, %r14
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x714f
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
leaq 0x90(%rsp), %rdi
callq 0xe8ca
leaq 0x60(%rsp), %rdi
callq 0x727a
jmp 0x716b
movq %rax, %r14
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x7182
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
movq %r14, %rdi
callq 0x3380