// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::SquaredDistanceTransform()
void R3Grid::
SquaredDistanceTransform(void)
{
  int x,y,z,s,t;
  long long dist,square,new_dist;
  long long* oldBuffer;
  long long* newBuffer;
  int first;
  int i;

  // Allocate temporary buffers
  int res = XResolution();
  if (res < YResolution()) res = YResolution();
  if (res < ZResolution()) res = ZResolution();
  oldBuffer = new long long [res];
  assert(oldBuffer);
  newBuffer = new long long[res];
  assert(newBuffer);

  // Initalize values (0 if was set, max_value if not)
  RNScalar max_value = 3.0 * (res+1) * (res+1);
  RNScalar *grid_valuesp = grid_values;
  for (i = 0; i < grid_size; i++) {
    if (*grid_valuesp == 0.0) *grid_valuesp = max_value;
    else *grid_valuesp = 0.0;
    grid_valuesp++;
  }

  // Scan along z axis
  for (x = 0; x < XResolution(); x++) {
    for (y = 0; y < YResolution(); y++) {
      first = 1;
      dist = 0;
      for (z = 0; z < ZResolution(); z++) {
        if (GridValue(x,y,z) == 0.0) {
          dist=0;
          first=0;
          SetGridValue(x, y, z, 0);
        }
        else if (first == 0) {
          dist++;
          square = dist*dist;
          SetGridValue(x, y, z, square);
        }
      }
			
      // backward scan
      dist = 0;
      first = 1;
      for (z = ZResolution()-1; z >= 0; z--) {
        if (GridValue(x,y,z) == 0.0){
          dist = 0;
          first = 0;
          SetGridValue(x, y, z, 0);
        }
        else if (first == 0) {
          dist++;
          square = dist*dist;
          if (square < GridValue(x, y, z)) {
            SetGridValue(x, y, z, square);
          }
        }
      }
    }
  }

  // Scan along x axis
  for (z = 0; z < ZResolution(); z++) {
    for (y = 0; y < YResolution(); y++) {
      // Copy grid values
      for(x = 0; x < XResolution(); x++) 
        oldBuffer[x] = (int) (GridValue(x, y, z) + 0.5);
		
      // forward scan
      s = 0;
      for (x = 0; x < XResolution(); x++) {
        dist = oldBuffer[x];
        if (dist) {
          for(t = s; t <= x; t++) {
            new_dist = oldBuffer[t] + (x - t) * (x - t);
            if (new_dist <= dist) {
              dist = new_dist;
              s = t;
            }
          }
        }
        else {
          s = x;
        }
        newBuffer[x] = dist;
      }
			
      // backwards scan
      s = XResolution() - 1;
      for (x = XResolution()-1; x >= 0 ; x--) {
        dist = newBuffer[x];
        if (dist) {
          for (t = s; t >= x; t--) {
            new_dist = oldBuffer[t] + (x - t) * (x - t);
            if (new_dist <= dist) {
              dist = new_dist;
              s = t;
            }
          }
          SetGridValue(x, y, z, dist);
        }
        else {
          s=x;
        }
      }
    }
  }
		
  // along y axis
  for (z = 0; z < ZResolution(); z++) {
    for (x = 0; x < XResolution(); x++) {
      // Copy grid values
      for (y = 0; y < YResolution(); y++)
        oldBuffer[y] = (int) (GridValue(x, y, z) + 0.5);
			
      // forward scan
      s = 0;
      for (y = 0; y < YResolution(); y++) {
        dist = oldBuffer[y];
        if (dist) {
          for (t = s; t <= y ; t++) {
            new_dist = oldBuffer[t] + (y - t) * (y - t);
            if (new_dist <= dist){
              dist = new_dist;
              s = t;
            }
          }
        }
        else { 
          s = y;
        }
        newBuffer[y] = dist;
      }

      // backward scan
      s = YResolution() - 1;
      for(y = YResolution()-1; y >=0 ; y--) {
        dist = newBuffer[y];
        if (dist) {
          for (t = s; t > y ; t--) {
            new_dist = oldBuffer[t] + (y - t) * (y - t);
            if (new_dist <= dist){
              dist = new_dist;
              s = t;
            }
          }
          SetGridValue(x, y, z, dist);
        }
        else { 
          s = y; 
        }
      }
    }
  }
	
  // Delete temporary buffers
  delete[] oldBuffer;
  delete[] newBuffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movslq 0x238(%rdi), %rcx
movslq 0x23c(%rdi), %rax
cmpl %eax, %ecx
movq %rax, 0x18(%rsp)
movq %rcx, 0x8(%rsp)
cmovgl %ecx, %eax
movslq 0x240(%rdi), %rcx
cmpl %ecx, %eax
movq %rcx, %r13
cmovlel %ecx, %eax
movslq %eax, %r12
leaq (,%r12,8), %rax
testl %r12d, %r12d
pushq $-0x1
popq %r15
cmovnsq %rax, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r14
movq %r15, %rdi
callq 0xe030
movq %rax, %r15
incl %r12d
cvtsi2sd %r12d, %xmm1
movsd 0x663bf(%rip), %xmm0    # 0x931d8
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movq 0x230(%rbx), %rax
movq %rbx, 0x10(%rsp)
movl 0x24c(%rbx), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
xorpd %xmm1, %xmm1
cmpl %edx, %ecx
je 0x2ce5a
movsd (%rax,%rdx,8), %xmm2
cmpeqsd %xmm1, %xmm2
andpd %xmm0, %xmm2
movlpd %xmm2, (%rax,%rdx,8)
incq %rdx
jmp 0x2ce3e
xorl %edi, %edi
movq %r13, %rbx
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
movq %rcx, 0x28(%rsp)
movq 0x18(%rsp), %rcx
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
movl %edx, (%rsp)
movq 0x8(%rsp), %rdx
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
movl %ecx, 0x4(%rsp)
pushq $0x1
popq %r8
xorpd %xmm0, %xmm0
cmpl 0x4(%rsp), %edi
je 0x2cfac
xorl %r9d, %r9d
cmpl (%rsp), %r9d
je 0x2cfa5
movq 0x10(%rsp), %rcx
movl 0x248(%rcx), %edx
movl 0x244(%rcx), %r10d
imull %r9d, %r10d
addl %edi, %r10d
movq 0x28(%rsp), %r11
xorl %r12d, %r12d
movl %r8d, %ecx
subq $0x1, %r11
jb 0x2cf19
movslq %r10d, %rsi
movsd (%rax,%rsi,8), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm2
jne 0x2cefc
jp 0x2cefc
xorl %r12d, %r12d
movsd %xmm1, (%rax,%rsi,8)
xorl %ecx, %ecx
jmp 0x2cf03
testl %ecx, %ecx
je 0x2cf08
movl %r8d, %ecx
addl %edx, %r10d
jmp 0x2ced6
incq %r12
movq %r12, %rcx
imulq %r12, %rcx
cvtsi2sd %rcx, %xmm1
jmp 0x2cef3
movq 0x10(%rsp), %rcx
movl 0x248(%rcx), %r10d
movl 0x244(%rcx), %r11d
imull %r9d, %r11d
addl %edi, %r11d
xorl %edx, %edx
movq %rbx, %r13
xorl %ecx, %ecx
leaq 0x1(%r13), %r12
decl %r13d
imull %r10d, %r13d
addl %r11d, %r13d
movq %r12, %rbp
decq %r12
testq %r12, %r12
jle 0x2cf9d
movslq %r13d, %rsi
movsd (%rax,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x2cf63
jnp 0x2cf87
subl %r10d, %r13d
testb $0x1, %cl
movl $0x0, %ecx
je 0x2cf48
incq %rdx
movq %rdx, %rcx
imulq %rdx, %rcx
cvtsi2sd %rcx, %xmm2
ucomisd %xmm2, %xmm1
ja 0x2cf8d
jmp 0x2cf92
xorpd %xmm2, %xmm2
xorl %edx, %edx
movsd %xmm2, (%rax,%rsi,8)
addq $-0x2, %rbp
movb $0x1, %cl
movq %rbp, %r13
jmp 0x2cf3a
incl %r9d
jmp 0x2cea8
incl %edi
jmp 0x2ce9b
movq 0x8(%rsp), %rcx
decl %ecx
movl %ecx, 0x34(%rsp)
movl 0x4(%rsp), %r8d
xorl %edx, %edx
movsd 0x6604a(%rip), %xmm0    # 0x93010
cmpl 0x28(%rsp), %edx
je 0x2d0ed
xorl %r10d, %r10d
movq %rdx, 0x20(%rsp)
cmpl (%rsp), %r10d
je 0x2d0e6
movq 0x10(%rsp), %rcx
movl 0x248(%rcx), %r11d
imull %edx, %r11d
movl 0x244(%rcx), %ebp
imull %r10d, %ebp
leal (%r11,%rbp), %ecx
xorl %edx, %edx
cmpq %rdx, %r8
je 0x2d026
leal (%rcx,%rdx), %edi
movslq %edi, %rdi
movsd (%rax,%rdi,8), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
movslq %edi, %rdi
movq %rdi, (%r14,%rdx,8)
incq %rdx
jmp 0x2d002
xorl %edi, %edi
xorl %r12d, %r12d
cmpq %r8, %rdi
je 0x2d074
movq (%r14,%rdi,8), %r13
testq %r13, %r13
je 0x2d065
movslq %r12d, %rcx
movq %rdi, %rdx
subq %rcx, %rdx
cmpq %rdi, %rcx
jg 0x2d06b
movl %edx, %r9d
imull %r9d, %r9d
addq (%r14,%rcx,8), %r9
cmpq %r13, %r9
cmovlel %ecx, %r12d
cmovlq %r9, %r13
incq %rcx
decq %rdx
jmp 0x2d042
movl %edi, %r12d
xorl %r13d, %r13d
movq %r13, (%r15,%rdi,8)
incq %rdi
jmp 0x2d02b
movl 0x34(%rsp), %r12d
movl %r12d, %edx
movq 0x8(%rsp), %r13
testq %r13, %r13
jle 0x2d0d9
decq %r13
movq (%r15,%r13,8), %rcx
testq %rcx, %rcx
je 0x2d0d2
movslq %r12d, %r9
movl %edx, %edi
subl %r9d, %edi
cmpq %r9, %r13
jg 0x2d0bd
movl %r9d, %esi
movl %edi, %ebx
imull %ebx, %ebx
addq (%r14,%rsi,8), %rbx
cmpq %rcx, %rbx
cmovlel %r9d, %r12d
cmovlq %rbx, %rcx
decq %r9
incl %edi
jmp 0x2d09a
cvtsi2sd %rcx, %xmm1
leal (%r11,%r13), %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movsd %xmm1, (%rax,%rcx,8)
jmp 0x2d0d5
movl %r13d, %r12d
decl %edx
jmp 0x2d081
incl %r10d
movq 0x20(%rsp), %rdx
jmp 0x2cfd8
incl %edx
jmp 0x2cfc6
movq 0x18(%rsp), %rcx
decl %ecx
movl %ecx, 0x8(%rsp)
movl (%rsp), %esi
xorl %edx, %edx
cmpl 0x28(%rsp), %edx
je 0x2d231
xorl %r9d, %r9d
movq %rdx, 0x20(%rsp)
cmpl 0x4(%rsp), %r9d
je 0x2d22a
movq 0x10(%rsp), %rcx
movl 0x248(%rcx), %r10d
imull %edx, %r10d
movl 0x244(%rcx), %r11d
addl %r9d, %r10d
movl %r10d, %ecx
xorl %edx, %edx
cmpq %rdx, %rsi
je 0x2d15d
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
movslq %edi, %rdi
movq %rdi, (%r14,%rdx,8)
incq %rdx
addl %r11d, %ecx
jmp 0x2d139
xorl %edx, %edx
xorl %ebp, %ebp
cmpq %rsi, %rdx
je 0x2d1a8
movq (%r14,%rdx,8), %r12
testq %r12, %r12
je 0x2d19a
movslq %ebp, %rcx
movq %rdx, %rdi
subq %rcx, %rdi
cmpq %rdx, %rcx
jg 0x2d19f
movl %edi, %r8d
imull %r8d, %r8d
addq (%r14,%rcx,8), %r8
cmpq %r12, %r8
cmovlel %ecx, %ebp
cmovlq %r8, %r12
incq %rcx
decq %rdi
jmp 0x2d178
movl %edx, %ebp
xorl %r12d, %r12d
movq %r12, (%r15,%rdx,8)
incq %rdx
jmp 0x2d161
movl %r9d, (%rsp)
movl 0x8(%rsp), %edx
movl %edx, %edi
movq 0x18(%rsp), %r13
testq %r13, %r13
jle 0x2d219
leaq -0x1(%r13), %r12
movq (%r15,%r12,8), %rcx
testq %rcx, %rcx
je 0x2d20f
movslq %edx, %r8
movl %edi, %ebp
subl %r8d, %ebp
cmpq %r8, %r13
jg 0x2d1f6
movl %r8d, %ebx
movl %ebp, %r9d
imull %r9d, %r9d
addq (%r14,%rbx,8), %r9
cmpq %rcx, %r9
cmovlel %r8d, %edx
cmovlq %r9, %rcx
decq %r8
incl %ebp
jmp 0x2d1d1
cvtsi2sd %rcx, %xmm1
movl %r11d, %ecx
imull %r12d, %ecx
addl %r10d, %ecx
movslq %ecx, %rcx
movsd %xmm1, (%rax,%rcx,8)
jmp 0x2d212
movl %r12d, %edx
decl %edi
movq %r12, %r13
jmp 0x2d1b7
movl (%rsp), %r9d
incl %r9d
movq 0x20(%rsp), %rdx
jmp 0x2d10f
incl %edx
jmp 0x2d0fd
movq %r14, %rdi
callq 0xe5d0
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5d0
nop