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

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

  // Initalize values (0 if was set, max_value if not)
  RNScalar max_value = 2 * (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 if (*grid_valuesp == R2_GRID_UNKNOWN_VALUE) *grid_valuesp = max_value;
    else *grid_valuesp = 0.0;
    grid_valuesp++;
  }

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

  // Scan along y axis
  for (x = 0; x < XResolution(); x++) {
    // Copy grid values
    for (y = 0; y < YResolution(); y++) 
      oldBuffer[y] = (int) (GridValue(x, y) + 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, 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, %r12
movslq 0xc0(%rdi), %r13
movslq 0xc4(%rdi), %rax
cmpl %eax, %r13d
movq %rax, 0x10(%rsp)
cmovgl %r13d, %eax
movslq %eax, %rbx
leaq (,%rbx,4), %rax
testl %ebx, %ebx
pushq $-0x1
popq %r15
cmovnsq %rax, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r14
movq %r15, %rdi
callq 0xe030
movq %rax, %r15
incl %ebx
imull %ebx, %ebx
addl %ebx, %ebx
cvtsi2sd %ebx, %xmm0
movq 0xb8(%r12), %rax
movq %rax, 0x8(%rsp)
movq %r12, 0x18(%rsp)
movl 0xcc(%r12), %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
xorpd %xmm1, %xmm1
movsd 0x4087c(%rip), %xmm2    # 0x93f68
movq 0x8(%rsp), %rdx
cmpl %eax, %ecx
je 0x53726
movsd (%rdx,%rax,8), %xmm3
movapd %xmm3, %xmm4
cmpeqsd %xmm1, %xmm4
cmpeqsd %xmm2, %xmm3
andpd %xmm0, %xmm3
movapd %xmm4, %xmm5
andnpd %xmm3, %xmm5
andpd %xmm0, %xmm4
orpd %xmm5, %xmm4
movlpd %xmm4, (%rdx,%rax,8)
incq %rax
jmp 0x536f1
xorl %edx, %edx
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
movq 0x10(%rsp), %rax
testl %eax, %eax
movl $0x0, %esi
cmovgl %eax, %esi
movq 0x8(%rsp), %rax
leaq -0x8(%rax), %rdi
pushq $0x1
popq %r8
cmpl %esi, %edx
je 0x53844
movq 0x18(%rsp), %rax
movl 0xc8(%rax), %eax
imull %edx, %eax
cltq
movq 0x8(%rsp), %r9
leaq (%r9,%rax,8), %r9
xorl %r10d, %r10d
xorl %r11d, %r11d
movl %r8d, %ebx
cmpq %r10, %rcx
je 0x537c4
movsd (%r9,%r10,8), %xmm1
xorl %eax, %eax
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
movl $0x0, %ebp
jne 0x537a3
jp 0x537a3
movsd %xmm0, (%r9,%r10,8)
movl %ebp, %r11d
jmp 0x537aa
testl %ebx, %ebx
je 0x537b1
movl %r8d, %eax
incq %r10
movl %eax, %ebx
jmp 0x5377a
incl %r11d
movl %r11d, %ebx
imull %r11d, %ebx
cvtsi2sd %ebx, %xmm0
movl %r11d, %ebp
jmp 0x53798
movq 0x18(%rsp), %rax
movl 0xc8(%rax), %eax
imull %edx, %eax
cltq
leaq (%rdi,%rax,8), %r9
xorl %r10d, %r10d
movq %r13, %rax
xorl %ebx, %ebx
leaq (%r9,%rax,8), %r12
testq %rax, %rax
jle 0x5383d
movq %rax, %r11
movsd (%r12), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x537fe
jnp 0x5382a
addq $-0x8, %r12
leaq -0x1(%r11), %rax
testb $0x1, %bl
movl $0x0, %ebx
je 0x537e4
incl %r10d
movl %r10d, %eax
imull %r10d, %eax
cvtsi2sd %eax, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x53833
addq $0x8, %r12
jmp 0x5382d
xorl %r10d, %r10d
movsd %xmm0, (%r12)
decq %r11
movb $0x1, %bl
movq %r11, %rax
jmp 0x537e0
incl %edx
jmp 0x53750
movq 0x10(%rsp), %rax
decl %eax
movl %eax, 0x24(%rsp)
movl %esi, %esi
xorl %edi, %edi
movsd 0x3f7b5(%rip), %xmm0    # 0x93010
movq 0x8(%rsp), %rdx
cmpq %rcx, %rdi
je 0x53972
movq 0x18(%rsp), %rax
movslq 0xc8(%rax), %r9
leaq (,%r9,8), %rax
movq %rdx, 0x28(%rsp)
xorl %r10d, %r10d
cmpq %r10, %rsi
je 0x538a3
movsd (%rdx), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r11d
movl %r11d, (%r14,%r10,4)
incq %r10
addq %rax, %rdx
jmp 0x53885
xorl %r10d, %r10d
xorl %r11d, %r11d
cmpq %rsi, %r10
je 0x538ea
movl (%r14,%r10,4), %ebp
testl %ebp, %ebp
je 0x538dc
movslq %r11d, %rax
movl %r10d, %edx
subl %eax, %edx
cmpq %r10, %rax
jg 0x538e1
movl %edx, %ebx
imull %edx, %ebx
addl (%r14,%rax,4), %ebx
cmpl %ebp, %ebx
cmovll %ebx, %ebp
cmovlel %eax, %r11d
incq %rax
decl %edx
jmp 0x538be
xorl %ebp, %ebp
movl %r10d, %r11d
movl %ebp, (%r15,%r10,4)
incq %r10
jmp 0x538a9
movq 0x8(%rsp), %rax
movq %rdi, 0x30(%rsp)
leaq (%rax,%rdi,8), %r10
movl 0x24(%rsp), %ebp
movl %ebp, %edi
movq 0x10(%rsp), %r13
testq %r13, %r13
jle 0x5395c
leaq -0x1(%r13), %r12
movl (%r15,%r12,4), %ebx
testl %ebx, %ebx
je 0x53952
movslq %ebp, %rdx
movl %edi, %eax
subl %edx, %eax
cmpq %rdx, %r13
jg 0x5393f
movl %edx, %r8d
movl %eax, %r11d
imull %eax, %r11d
addl (%r14,%r8,4), %r11d
cmpl %ebx, %r11d
cmovll %r11d, %ebx
cmovlel %edx, %ebp
decq %rdx
incl %eax
jmp 0x5391b
cvtsi2sd %ebx, %xmm1
movq %r12, %rax
imulq %r9, %rax
movsd %xmm1, (%r10,%rax,8)
jmp 0x53955
movl %r12d, %ebp
decl %edi
movq %r12, %r13
jmp 0x53903
movq 0x30(%rsp), %rdi
incq %rdi
movq 0x28(%rsp), %rdx
addq $0x8, %rdx
jmp 0x53860
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