// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::FillHoles(int)
void R2Grid::
FillHoles(int max_hole_size)
{
  // Interpolate vertically
  for (int ix = 0; ix < XResolution(); ix++) {
    int iy0 = -1;
    for (int iy1 = 0; iy1 < YResolution(); iy1++) {
      if (GridValue(ix, iy1) != R2_GRID_UNKNOWN_VALUE) { 
        if ((iy0 >= 0) && (iy0 < iy1-1) && (iy1-iy0 < max_hole_size)) {
          RNScalar value0 = GridValue(ix, iy0);
          if (value0 == R2_GRID_UNKNOWN_VALUE) continue;
          RNScalar value1 = GridValue(ix, iy1);
          if (value1 == R2_GRID_UNKNOWN_VALUE) continue;
          for (int iy = iy0+1; iy < iy1; iy++) {
            RNScalar t = (double) (iy - iy0) / (double) (iy1 - iy0);
            RNScalar value = (1-t)*value0 + t*value1;
            SetGridValue(ix, iy, value);
          }
        }
        iy0 = iy1;
      }
    }
  }

  // Interpolate horizontally
  for (int iy = 0; iy < YResolution(); iy++) {
    int ix0 = -1;
    for (int ix1 = 0; ix1 < XResolution(); ix1++) {
      if (GridValue(ix1, iy) != R2_GRID_UNKNOWN_VALUE) { 
        if ((ix0 >= 0) && (ix0 < ix1-1) && (ix1-ix0 < max_hole_size)) {
          RNScalar value0 = GridValue(ix0, iy);
          if (value0 == R2_GRID_UNKNOWN_VALUE) continue;
          RNScalar value1 = GridValue(ix1, iy);
          if (value1 == R2_GRID_UNKNOWN_VALUE) continue;
          for (int ix = ix0+1; ix < ix1; ix++) {
            RNScalar t = (double) (ix - ix0) / (double) (ix1 - ix0);
            RNScalar value = (1-t)*value0 + t*value1;
            SetGridValue(ix, iy, value);
          }
        }
        ix0 = ix1;
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl 0xc0(%rdi), %eax
movl 0xc4(%rdi), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
testl %eax, %eax
cmovlel %edx, %eax
movsd 0x40bd6(%rip), %xmm0    # 0x93f68
movsd 0x3fc6e(%rip), %xmm1    # 0x93008
xorl %r10d, %r10d
cmpq %rax, %r10
je 0x53492
movq 0xb8(%rdi), %r11
movslq 0xc8(%rdi), %rbx
leaq (%r11,%r10,8), %r14
addq %rdx, %r11
leaq (,%rbx,8), %r15
xorl %r12d, %r12d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq %rcx, %r12
je 0x53486
movq %r12, %r9
imulq %rbx, %r9
movsd (%r14,%r9,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x533ed
jnp 0x5347e
testl %ebp, %ebp
setns %r9b
leaq -0x1(%r12), %r8
movslq %ebp, %r13
cmpq %r13, %r8
setg %r8b
andb %r9b, %r8b
cmpb $0x1, %r8b
jne 0x5347b
movl %r12d, %r9d
subl %ebp, %r9d
cmpl %esi, %r9d
jge 0x5347b
movl %ebx, %r8d
imull %ebp, %r8d
movslq %r8d, %r8
movsd (%r14,%r8,8), %xmm3
ucomisd %xmm0, %xmm3
jne 0x5342e
jnp 0x5347e
cvtsi2sd %r9d, %xmm4
leaq 0x1(%r13), %rbp
imulq %r15, %rbp
addq %r11, %rbp
movabsq $0x1, %r9
leaq (%r9,%r13), %r8
cmpq %r12, %r8
jge 0x5347b
cvtsi2sd %r9d, %xmm5
divsd %xmm4, %xmm5
movapd %xmm1, %xmm6
subsd %xmm5, %xmm6
mulsd %xmm2, %xmm5
mulsd %xmm3, %xmm6
addsd %xmm5, %xmm6
movsd %xmm6, (%rbp)
incq %r9
addq %r15, %rbp
jmp 0x53448
movl %r12d, %ebp
incq %r12
jmp 0x533cb
incq %r10
addq $0x8, %rdx
jmp 0x5339d
xorl %edx, %edx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movabsq $0x1, %r9
cmpl %ecx, %edx
je 0x5356d
movq 0xb8(%rdi), %r10
movl 0xc8(%rdi), %r11d
imull %edx, %r11d
movslq %r11d, %rbx
leaq (%r10,%rbx,8), %rbx
xorl %r14d, %r14d
movl %r8d, %r15d
cmpq %rax, %r14
je 0x53566
movsd (%rbx,%r14,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x534e2
jnp 0x5355e
testl %r15d, %r15d
setns %bpl
leaq -0x1(%r14), %r13
movslq %r15d, %r12
cmpq %r12, %r13
setg %r13b
andb %bpl, %r13b
cmpb $0x1, %r13b
jne 0x5355b
movl %r14d, %ebp
subl %r15d, %ebp
cmpl %esi, %ebp
jge 0x5355b
leal (%r11,%r15), %r13d
movslq %r13d, %r13
movsd (%r10,%r13,8), %xmm3
ucomisd %xmm0, %xmm3
jne 0x5351f
jnp 0x5355e
cvtsi2sd %ebp, %xmm4
leaq (%rbx,%r12,8), %r15
movq %r9, %r13
leaq (%r12,%r13), %rbp
cmpq %r14, %rbp
jge 0x5355b
cvtsi2sd %r13d, %xmm5
divsd %xmm4, %xmm5
movapd %xmm1, %xmm6
subsd %xmm5, %xmm6
mulsd %xmm2, %xmm5
mulsd %xmm3, %xmm6
addsd %xmm5, %xmm6
movsd %xmm6, (%r15,%r13,8)
incq %r13
jmp 0x5352a
movl %r14d, %r15d
incq %r14
jmp 0x534cb
incl %edx
jmp 0x534a4
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq