// File: /workspace/github_cpp_makefile_star2_O1/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), %rbp
movl %ebp, %eax
movq %rax, 0x20(%rsp)
movl 0xc4(%rdi), %r13d
cmpl %r13d, %ebp
movl %r13d, %ebx
cmovgl %ebp, %ebx
movslq %ebx, %rax
leaq (,%rax,4), %rcx
testl %eax, %eax
movq $-0x1, %r15
cmovnsq %rcx, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r14
movq %r15, %rdi
callq 0xe030
movq %rax, %r15
movq 0xb8(%r12), %rdx
movq %r12, 0x8(%rsp)
movl 0xcc(%r12), %ecx
testl %ecx, %ecx
jle 0x5bf80
incl %ebx
imull %ebx, %ebx
addl %ebx, %ebx
cvtsi2sd %ebx, %xmm0
xorl %eax, %eax
xorpd %xmm1, %xmm1
movsd 0x4706b(%rip), %xmm2    # 0xa2fb8
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
cmpl %eax, %ecx
jne 0x5bf4d
movq %rdx, (%rsp)
testl %r13d, %r13d
movq 0x20(%rsp), %rbx
jle 0x5c086
movq 0x8(%rsp), %rax
movslq 0xc8(%rax), %rcx
shlq $0x3, %rcx
leaq 0x1(%rbp), %rdx
xorl %esi, %esi
movq (%rsp), %rdi
testl %ebx, %ebx
jle 0x5c00e
xorl %r8d, %r8d
movl $0x1, %eax
xorl %r9d, %r9d
movsd (%rdi,%r8,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
movl $0x0, %r11d
jne 0x5bfe1
jp 0x5bfe1
movsd %xmm0, (%rdi,%r8,8)
xorl %r10d, %r10d
movl %r11d, %r9d
jmp 0x5bfeb
movl $0x1, %r10d
testl %eax, %eax
je 0x5bff8
incq %r8
movl %r10d, %eax
cmpq %r8, %rbx
jne 0x5bfbb
jmp 0x5c00e
incl %r9d
movl %r9d, %eax
imull %r9d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movl %r9d, %r11d
jmp 0x5bfd3
testl %ebp, %ebp
jle 0x5c077
xorl %r9d, %r9d
movl $0x1, %r10d
movq %rdx, %r8
movsd -0x10(%rdi,%r8,8), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movl $0x0, %r11d
jne 0x5c045
jp 0x5c045
movsd %xmm1, -0x10(%rdi,%r8,8)
xorl %eax, %eax
movl %r11d, %r9d
jmp 0x5c06b
movl $0x1, %eax
testl %r10d, %r10d
jne 0x5c06b
incl %r9d
movl %r9d, %eax
imull %r9d, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
movl %r9d, %r11d
ja 0x5c037
decq %r8
movl %eax, %r10d
cmpq $0x1, %r8
jg 0x5c01e
incq %rsi
addq %rcx, %rdi
cmpq %r13, %rsi
jne 0x5bfac
testl %ebx, %ebx
jle 0x5c209
movq 0x8(%rsp), %rax
movslq 0xc8(%rax), %rcx
movslq %r13d, %rax
movq %rax, 0x28(%rsp)
leal -0x1(%r13), %eax
movl %eax, 0x1c(%rsp)
shlq $0x3, %rcx
movq $0x0, 0x10(%rsp)
movsd 0x45f71(%rip), %xmm0    # 0xa2030
movq (%rsp), %rdi
testl %r13d, %r13d
jle 0x5c0e8
movq %rdi, %rax
xorl %edx, %edx
movsd (%rax), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %esi
movl %esi, (%r14,%rdx,4)
incq %rdx
addq %rcx, %rax
cmpq %rdx, %r13
jne 0x5c0cd
movq %rdi, 0x30(%rsp)
testl %r13d, %r13d
jle 0x5c155
xorl %r9d, %r9d
movl $0x1, %r10d
xorl %edx, %edx
movl (%r14,%r9,4), %esi
testl %esi, %esi
je 0x5c112
movslq %edx, %rax
cmpq %rax, %r9
jge 0x5c119
movl %edx, %r11d
jmp 0x5c143
xorl %esi, %esi
movl %r9d, %r11d
jmp 0x5c143
movl %r9d, %ebx
subl %eax, %ebx
leaq (%r14,%rax,4), %rbp
movl %edx, %r11d
movl %ebx, %eax
imull %ebx, %eax
addl (%rbp), %eax
cmpl %esi, %eax
cmovll %eax, %esi
cmovlel %edx, %r11d
decl %ebx
addq $0x4, %rbp
incl %edx
cmpl %edx, %r10d
jne 0x5c125
movl %esi, (%r15,%r9,4)
incq %r9
incl %r10d
movl %r11d, %edx
cmpq %r13, %r9
jne 0x5c0fd
testl %r13d, %r13d
jle 0x5c1e8
movq 0x8(%rsp), %rax
movslq 0xc8(%rax), %r9
movq (%rsp), %rax
movq 0x10(%rsp), %rdx
leaq (%rax,%rdx,8), %r10
movl 0x1c(%rsp), %esi
movl %esi, %r12d
movq 0x28(%rsp), %rbp
leaq -0x1(%rbp), %rdx
movl -0x4(%r15,%rbp,4), %ebx
testl %ebx, %ebx
je 0x5c1da
movslq %esi, %rax
cmpq %rax, %rbp
jg 0x5c1c4
movl %r12d, %r8d
subl %esi, %r8d
movl %eax, %edi
movl %r8d, %r11d
imull %r8d, %r11d
addl (%r14,%rdi,4), %r11d
cmpl %ebx, %r11d
cmovll %r11d, %ebx
cmovlel %eax, %esi
leaq -0x1(%rax), %rdi
incl %r8d
cmpq %rax, %rbp
movq %rdi, %rax
jl 0x5c19e
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
movq %rdx, %rax
imulq %r9, %rax
movsd %xmm1, (%r10,%rax,8)
jmp 0x5c1dc
movl %edx, %esi
decl %r12d
cmpq $0x1, %rbp
movq %rdx, %rbp
jg 0x5c183
movq 0x10(%rsp), %rdx
incq %rdx
movq 0x30(%rsp), %rdi
addq $0x8, %rdi
movq %rdx, 0x10(%rsp)
cmpq 0x20(%rsp), %rdx
jne 0x5c0c3
movq %r14, %rdi
callq 0xe5b0
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0
nop