// File: /workspace/github_cpp_makefile_star2_O1/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, 0x70(%rsp)
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq (%rsi), %r13
movq %rsi, 0x48(%rsp)
movq 0x8(%rsi), %r14
xorpd %xmm0, %xmm0
cmpq %r14, %r13
je 0x701b
movsd %xmm0, 0x8(%rsp)
leaq 0x30(%rsp), %r15
leaq 0x90(%rsp), %r12
movsd %xmm0, (%rsp)
movsd (%r13), %xmm1
movsd 0xdfa6(%rip), %xmm0     # 0x14f30
mulsd %xmm0, %xmm1
movsd 0x8(%r13), %xmm2
movsd %xmm1, 0x28(%rsp)
cvttsd2si %xmm1, %rax
mulsd %xmm0, %xmm2
movsd %xmm2, 0x78(%rsp)
cvttsd2si %xmm2, %rcx
movq %rax, 0x90(%rsp)
movq %rcx, 0x98(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x6fdf
movupd 0x90(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, 0x38(%rsp)
jmp 0x6fea
movq %r15, %rdi
movq %r12, %rdx
callq 0x75d4
movsd 0x8(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd (%rsp), %xmm0
addsd 0x78(%rsp), %xmm0
movsd %xmm0, (%rsp)
addq $0x10, %r13
cmpq %r14, %r13
jne 0x6f7c
jmp 0x7026
movsd %xmm0, (%rsp)
movsd %xmm0, 0x8(%rsp)
movq 0x48(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x4, %rax
js 0x7042
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x705a
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
leaq 0x90(%rsp), %rdi
movsd 0xcfbf(%rip), %xmm0     # 0x14040
movsd 0xdeaf(%rip), %xmm1     # 0x14f38
callq 0xec40
xorl %edx, %edx
cmpl $0x1, %ebp
setne %dl
cmpl $0x2, %ebp
cmovel %ebp, %edx
leaq 0x90(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %ecx, %ecx
callq 0xedcc
movsd 0x70(%rsp), %xmm0
mulsd 0xde72(%rip), %xmm0     # 0x14f30
leaq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xf308
movq 0x50(%rsp), %r15
movq 0x58(%rsp), %r13
cmpq %r15, %r13
je 0x7342
xorps %xmm0, %xmm0
cvtss2sd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd (%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq %r15, %r12
movq (%r12), %rax
movq 0x8(%r12), %rcx
movsd 0xde2a(%rip), %xmm0     # 0x14f48
movapd %xmm0, %xmm2
movsd 0xde16(%rip), %xmm3     # 0x14f40
movapd %xmm3, %xmm1
cmpq %rcx, %rax
je 0x719d
movl $0x80000000, %edx       # imm = 0x80000000
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x80000000, %r8d       # imm = 0x80000000
movslq %esi, %rsi
movq (%rax), %r9
movq 0x8(%rax), %r10
cmpq %rsi, %r9
cmovll %r9d, %esi
movslq %r8d, %r8
cmpq %r8, %r9
cmovgl %r9d, %r8d
movslq %edi, %rdi
cmpq %rdi, %r10
cmovll %r10d, %edi
movslq %edx, %rdx
cmpq %rdx, %r10
cmovgl %r10d, %edx
addq $0x10, %rax
cmpq %rcx, %rax
jne 0x7148
xorps %xmm3, %xmm3
cvtsi2sd %esi, %xmm3
xorps %xmm2, %xmm2
cvtsi2sd %r8d, %xmm2
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
xorl %ebp, %ebp
movsd 0x8(%rsp), %xmm4
ucomisd %xmm3, %xmm4
jbe 0x71c5
ucomisd %xmm4, %xmm2
jbe 0x71c5
movsd (%rsp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x71c5
ucomisd (%rsp), %xmm0
seta %bpl
testb %bpl, %bpl
je 0x71ea
movq %r15, %rdi
movq %r12, %rsi
callq 0x749a
addq $0x18, %r12
cmpq %r13, %r12
sete %al
orb %al, %bpl
je 0x710d
jmp 0x71f7
addq $0x18, %r12
cmpq %r13, %r12
jne 0x710d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq (%r15), %r14
movq 0x8(%r15), %r13
cmpq %r13, %r14
je 0x7291
leaq 0x10(%rsp), %r15
leaq 0x80(%rsp), %r12
xorps %xmm0, %xmm0
cvtsi2ssq (%r14), %xmm0
movss 0xdd1d(%rip), %xmm2     # 0x14f50
divss %xmm2, %xmm0
xorps %xmm1, %xmm1
cvtsi2ssq 0x8(%r14), %xmm1
cvtss2sd %xmm0, %xmm0
divss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
movsd %xmm0, 0x80(%rsp)
movsd %xmm1, 0x88(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x727d
movups 0x80(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x18(%rsp)
jmp 0x7288
movq %r15, %rdi
movq %r12, %rdx
callq 0x6796
addq $0x10, %r14
cmpq %r13, %r14
jne 0x7223
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x7357
movq 0x48(%rsp), %rcx
movq (%rcx), %r15
movsd 0xcd5f(%rip), %xmm3     # 0x14010
movl $0x8, %r12d
xorl %r13d, %r13d
xorpd %xmm2, %xmm2
xorl %r14d, %r14d
movsd -0x8(%rax,%r12), %xmm0
subsd (%r15), %xmm0
movsd (%rax,%r12), %xmm1
subsd 0x8(%r15), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jb 0x72f1
sqrtsd %xmm0, %xmm0
jmp 0x7304
movsd %xmm3, (%rsp)
callq 0x31a0
movsd (%rsp), %xmm3
xorpd %xmm2, %xmm2
testq %r13, %r13
sete %al
ucomisd %xmm0, %xmm3
seta %cl
orb %al, %cl
testb %cl, %cl
jne 0x731b
movapd %xmm3, %xmm0
cmovneq %r13, %r14
incq %r13
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
addq $0x10, %r12
movapd %xmm0, %xmm3
cmpq %rcx, %r13
jb 0x72c1
jmp 0x735a
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
jmp 0x73dc
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
subq %rdi, %rcx
je 0x73ca
sarq $0x4, %rcx
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 0x73a9
movupd (%rdx), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, 0x8(%rbx)
jmp 0x73b1
movq %rbx, %rdi
callq 0x68ba
incq %r15
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
subq %rdi, %rcx
sarq $0x4, %rcx
cmpq %rcx, %r15
jb 0x7380
testq %rdi, %rdi
je 0x73dc
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
leaq 0x90(%rsp), %rdi
callq 0xec94
leaq 0x50(%rsp), %rdi
callq 0x7586
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x740a
movq 0x40(%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
movq %rax, %r14
jmp 0x7470
jmp 0x7460
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x7442
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x3220
jmp 0x7442
movq %rax, %r14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7463
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
jmp 0x7463
movq %rax, %r14
jmp 0x747a
movq %rax, %r14
leaq 0x90(%rsp), %rdi
callq 0xec94
leaq 0x50(%rsp), %rdi
callq 0x7586
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7491
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
movq %r14, %rdi
callq 0x3380
nop