// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::FillHoles(int)
void R3Grid::
FillHoles(int max_hole_size)
{
  // Interpolate in z
  for (int ix = 0; ix < XResolution(); ix++) {
    for (int iy = 0; iy < YResolution(); iy++) {
      int iz0 = -1;
      for (int iz1 = 0; iz1 < ZResolution(); iz1++) {
        if (GridValue(ix, iy, iz1) != 0.0) { 
          if ((iz0 >= 0) && (iz0 < iz1-1) && (iz1-iz0 < max_hole_size)) {
            RNScalar value0 = GridValue(ix, iy, iz0);
            if (value0 == 0.0) continue;
            RNScalar value1 = GridValue(ix, iy, iz1);
            if (value1 == 0.0) continue;
            for (int iz = iz0+1; iz < iz1; iz++) {
              RNScalar t = (double) (iz - iz0) / (double) (iz1 - iz0);
              RNScalar value = (1-t)*value0 + t*value1;
              SetGridValue(ix, iy, iz, value);
            }
          }
          iz0 = iz1;
        }
      }
    }
  }
  
  // Interpolate in y
  for (int iz = 0; iz < ZResolution(); iz++) {
    for (int ix = 0; ix < XResolution(); ix++) {
      int iy0 = -1;
      for (int iy1 = 0; iy1 < YResolution(); iy1++) {
        if (GridValue(ix, iy1, iz) != 0.0) { 
          if ((iy0 >= 0) && (iy0 < iy1-1) && (iy1-iy0 < max_hole_size)) {
            RNScalar value0 = GridValue(ix, iy0, iz);
            if (value0 == 0.0) continue;
            RNScalar value1 = GridValue(ix, iy1, iz);
            if (value1 == 0.0) 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, iz, value);
            }
          }
          iy0 = iy1;
        }
      }
    }
  }
  
  // Interpolate in x
  for (int iy = 0; iy < YResolution(); iy++) {
    for (int iz = 0; iz < ZResolution(); iz++) {
      int ix0 = -1;
      for (int ix1 = 0; ix1 < XResolution(); ix1++) {
        if (GridValue(ix1, iy, iz) != 0.0) { 
          if ((ix0 >= 0) && (ix0 < ix1-1) && (ix1-ix0 < max_hole_size)) {
            RNScalar value0 = GridValue(ix0, iy, iz);
            if (value0 == 0.0) continue;
            RNScalar value1 = GridValue(ix1, iy, iz);
            if (value1 == 0.0) 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, iz, value);
            }
          }
          ix0 = ix1;
        }
      }
    }
  }
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movl %esi, 0xdc(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x28(%rsp)
movl $0x0, 0xd8(%rsp)
movq 0x28(%rsp), %rdi
movl 0xd8(%rsp), %eax
movl %eax, 0x24(%rsp)
callq 0x138c0
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x5c1c4
movl $0x0, 0xd4(%rsp)
movq 0x28(%rsp), %rdi
movl 0xd4(%rsp), %eax
movl %eax, 0x20(%rsp)
callq 0x138e0
movl %eax, %ecx
movl 0x20(%rsp), %eax
cmpl %ecx, %eax
jge 0x5c1ac
movl $0xffffffff, 0xd0(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0xcc(%rsp)
movq 0x28(%rsp), %rdi
movl 0xcc(%rsp), %eax
movl %eax, 0x1c(%rsp)
callq 0x13900
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x5c194
movq 0x28(%rsp), %rdi
movl 0xd8(%rsp), %esi
movl 0xd4(%rsp), %edx
movl 0xcc(%rsp), %ecx
callq 0x6bad0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5bfd7
jp 0x5bfd7
jmp 0x5c17c
cmpl $0x0, 0xd0(%rsp)
jl 0x5c16e
movl 0xd0(%rsp), %eax
movl 0xcc(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x5c16e
movl 0xcc(%rsp), %eax
subl 0xd0(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jge 0x5c16e
movq 0x28(%rsp), %rdi
movl 0xd8(%rsp), %esi
movl 0xd4(%rsp), %edx
movl 0xd0(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5c05a
jp 0x5c05a
jmp 0x5c17e
movq 0x28(%rsp), %rdi
movl 0xd8(%rsp), %esi
movl 0xd4(%rsp), %edx
movl 0xcc(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0xb8(%rsp)
movsd 0xb8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5c09b
jp 0x5c09b
jmp 0x5c17e
movl 0xd0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
cmpl 0xcc(%rsp), %eax
jge 0x5c16c
movq 0x28(%rsp), %rdi
movl 0xb4(%rsp), %eax
subl 0xd0(%rsp), %eax
cvtsi2sd %eax, %xmm0
movl 0xcc(%rsp), %eax
subl 0xd0(%rsp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xd6f0a(%rip), %xmm0    # 0x133008
subsd 0xa8(%rsp), %xmm0
movsd 0xc0(%rsp), %xmm2
movsd 0xa8(%rsp), %xmm1
mulsd 0xb8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movl 0xd8(%rsp), %esi
movl 0xd4(%rsp), %edx
movl 0xb4(%rsp), %ecx
movsd 0xa0(%rsp), %xmm0
callq 0x6bc60
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x5c0ac
jmp 0x5c16e
movl 0xcc(%rsp), %eax
movl %eax, 0xd0(%rsp)
jmp 0x5c17e
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x5bf85
jmp 0x5c196
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x5bf4c
jmp 0x5c1ae
movl 0xd8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd8(%rsp)
jmp 0x5bf1e
movl $0x0, 0x9c(%rsp)
movq 0x28(%rsp), %rdi
movl 0x9c(%rsp), %eax
movl %eax, 0x18(%rsp)
callq 0x13900
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0x5c454
movl $0x0, 0x98(%rsp)
movq 0x28(%rsp), %rdi
movl 0x98(%rsp), %eax
movl %eax, 0x14(%rsp)
callq 0x138c0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x5c43c
movl $0xffffffff, 0x94(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x90(%rsp)
movq 0x28(%rsp), %rdi
movl 0x90(%rsp), %eax
movl %eax, 0x10(%rsp)
callq 0x138e0
movl %eax, %ecx
movl 0x10(%rsp), %eax
cmpl %ecx, %eax
jge 0x5c424
movq 0x28(%rsp), %rdi
movl 0x98(%rsp), %esi
movl 0x90(%rsp), %edx
movl 0x9c(%rsp), %ecx
callq 0x6bad0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5c288
jp 0x5c288
jmp 0x5c40c
cmpl $0x0, 0x94(%rsp)
jl 0x5c3fe
movl 0x94(%rsp), %eax
movl 0x90(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x5c3fe
movl 0x90(%rsp), %eax
subl 0x94(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jge 0x5c3fe
movq 0x28(%rsp), %rdi
movl 0x98(%rsp), %esi
movl 0x94(%rsp), %edx
movl 0x9c(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0x88(%rsp)
movsd 0x88(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5c30b
jp 0x5c30b
jmp 0x5c40e
movq 0x28(%rsp), %rdi
movl 0x98(%rsp), %esi
movl 0x90(%rsp), %edx
movl 0x9c(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5c34c
jp 0x5c34c
jmp 0x5c40e
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x90(%rsp), %eax
jge 0x5c3fc
movq 0x28(%rsp), %rdi
movl 0x7c(%rsp), %eax
subl 0x94(%rsp), %eax
cvtsi2sd %eax, %xmm0
movl 0x90(%rsp), %eax
subl 0x94(%rsp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0xd6c65(%rip), %xmm0    # 0x133008
subsd 0x70(%rsp), %xmm0
movsd 0x88(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
mulsd 0x80(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x98(%rsp), %esi
movl 0x7c(%rsp), %edx
movl 0x9c(%rsp), %ecx
movsd 0x68(%rsp), %xmm0
callq 0x6bc60
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x5c35a
jmp 0x5c3fe
movl 0x90(%rsp), %eax
movl %eax, 0x94(%rsp)
jmp 0x5c40e
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x5c236
jmp 0x5c426
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x5c1fd
jmp 0x5c43e
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x5c1cf
movl $0x0, 0x64(%rsp)
movq 0x28(%rsp), %rdi
movl 0x64(%rsp), %eax
movl %eax, 0xc(%rsp)
callq 0x138e0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x5c662
movl $0x0, 0x60(%rsp)
movq 0x28(%rsp), %rdi
movl 0x60(%rsp), %eax
movl %eax, 0x8(%rsp)
callq 0x13900
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jge 0x5c650
movl $0xffffffff, 0x5c(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x58(%rsp)
movq 0x28(%rsp), %rdi
movl 0x58(%rsp), %eax
movl %eax, 0x4(%rsp)
callq 0x138c0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x5c63e
movq 0x28(%rsp), %rdi
movl 0x58(%rsp), %esi
movl 0x64(%rsp), %edx
movl 0x60(%rsp), %ecx
callq 0x6bad0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5c4fa
jp 0x5c4fa
jmp 0x5c62c
cmpl $0x0, 0x5c(%rsp)
jl 0x5c624
movl 0x5c(%rsp), %eax
movl 0x58(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x5c624
movl 0x58(%rsp), %eax
subl 0x5c(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jge 0x5c624
movq 0x28(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0x64(%rsp), %edx
movl 0x60(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5c55f
jp 0x5c55f
jmp 0x5c62e
movq 0x28(%rsp), %rdi
movl 0x58(%rsp), %esi
movl 0x64(%rsp), %edx
movl 0x60(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0x48(%rsp)
movsd 0x48(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5c591
jp 0x5c591
jmp 0x5c62e
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x58(%rsp), %eax
jge 0x5c622
movq 0x28(%rsp), %rdi
movl 0x44(%rsp), %eax
subl 0x5c(%rsp), %eax
cvtsi2sd %eax, %xmm0
movl 0x58(%rsp), %eax
subl 0x5c(%rsp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0xd6a33(%rip), %xmm0    # 0x133008
subsd 0x38(%rsp), %xmm0
movsd 0x50(%rsp), %xmm2
movsd 0x38(%rsp), %xmm1
mulsd 0x48(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x44(%rsp), %esi
movl 0x64(%rsp), %edx
movl 0x60(%rsp), %ecx
movsd 0x30(%rsp), %xmm0
callq 0x6bc60
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x5c59c
jmp 0x5c624
movl 0x58(%rsp), %eax
movl %eax, 0x5c(%rsp)
jmp 0x5c62e
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x5c4b4
jmp 0x5c640
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x5c484
jmp 0x5c652
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x5c45c
addq $0xe8, %rsp
retq
nopw (%rax,%rax)