// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movslq 0xc0(%rdi), %rcx
movl %ecx, %r12d
movslq 0xc4(%rdi), %rax
movl %eax, %ebp
cmpl %eax, %ecx
movq %rax, 0x20(%rsp)
movl %eax, %r13d
movq %rcx, (%rsp)
cmovgl %ecx, %r13d
movslq %r13d, %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, 0x30(%rsp)
movq 0xb8(%rbx), %r15
movq %rbx, 0x8(%rsp)
movl 0xcc(%rbx), %ecx
testl %ecx, %ecx
jle 0x5b236
incl %r13d
imull %r13d, %r13d
addl %r13d, %r13d
cvtsi2sd %r13d, %xmm0
xorl %eax, %eax
xorpd %xmm1, %xmm1
movsd 0x47db7(%rip), %xmm2    # 0xa2fb8
movsd (%r15,%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, (%r15,%rax,8)
incq %rax
cmpl %eax, %ecx
jne 0x5b201
testl %ebp, %ebp
movq (%rsp), %rbx
jle 0x5b33a
movq 0x8(%rsp), %rax
movslq 0xc8(%rax), %rcx
shlq $0x3, %rcx
leaq 0x1(%rbx), %rdx
xorl %esi, %esi
movq %r15, %rdi
testl %ebx, %ebx
jle 0x5b32b
xorl %r8d, %r8d
movl $0x1, %eax
xorl %r9d, %r9d
movsd (%rdi,%r8,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
movl $0x0, %r11d
jne 0x5b294
jp 0x5b294
movsd %xmm0, (%rdi,%r8,8)
xorl %r10d, %r10d
movl %r11d, %r9d
jmp 0x5b29e
movl $0x1, %r10d
testl %eax, %eax
je 0x5b2ab
incq %r8
movl %r10d, %eax
cmpq %r8, %r12
jne 0x5b26e
jmp 0x5b2c1
incl %r9d
movl %r9d, %eax
imull %r9d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movl %r9d, %r11d
jmp 0x5b286
testl %r12d, %r12d
jle 0x5b32b
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 0x5b2f9
jp 0x5b2f9
movsd %xmm1, -0x10(%rdi,%r8,8)
xorl %eax, %eax
movl %r11d, %r9d
jmp 0x5b31f
movl $0x1, %eax
testl %r10d, %r10d
jne 0x5b31f
incl %r9d
movl %r9d, %eax
imull %r9d, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
movl %r9d, %r11d
ja 0x5b2eb
decq %r8
movl %eax, %r10d
cmpq $0x1, %r8
jg 0x5b2d2
incq %rsi
addq %rcx, %rdi
cmpq %rbp, %rsi
jne 0x5b25b
movq %r15, 0x18(%rsp)
testl %r12d, %r12d
movq 0x30(%rsp), %rsi
jle 0x5b4cc
movq 0x8(%rsp), %rax
movslq 0xc8(%rax), %rcx
leal -0x1(%rbp), %eax
movl %eax, 0x14(%rsp)
shlq $0x3, %rcx
movq $0x0, (%rsp)
movsd 0x46cbc(%rip), %xmm0    # 0xa2030
movq 0x18(%rsp), %rax
movq %rax, 0x28(%rsp)
testl %ebp, %ebp
jle 0x5b4b2
movq 0x28(%rsp), %rax
xorl %edx, %edx
movsd (%rax), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
movl %edi, (%r14,%rdx,4)
incq %rdx
addq %rcx, %rax
cmpq %rdx, %rbp
jne 0x5b38d
cmpl $0x0, 0x20(%rsp)
jle 0x5b4b2
xorl %r8d, %r8d
movl $0x1, %r9d
xorl %edx, %edx
movl (%r14,%r8,4), %r10d
testl %r10d, %r10d
je 0x5b3d4
movslq %edx, %rax
cmpq %rax, %r8
jge 0x5b3dc
movl %edx, %r11d
jmp 0x5b409
xorl %r10d, %r10d
movl %r8d, %r11d
jmp 0x5b409
movl %r8d, %ebx
subl %eax, %ebx
leaq (%r14,%rax,4), %r13
movl %edx, %r11d
movl %ebx, %eax
imull %ebx, %eax
addl (%r13), %eax
cmpl %r10d, %eax
cmovll %eax, %r10d
cmovlel %edx, %r11d
decl %ebx
addq $0x4, %r13
incl %edx
cmpl %edx, %r9d
jne 0x5b3e8
movl %r10d, (%rsi,%r8,4)
incq %r8
incl %r9d
movl %r11d, %edx
cmpq %rbp, %r8
jne 0x5b3be
testl %ebp, %ebp
jle 0x5b4b2
movq 0x8(%rsp), %rax
movslq 0xc8(%rax), %r8
movq 0x18(%rsp), %rax
movq (%rsp), %rdx
leaq (%rax,%rdx,8), %r9
movl 0x14(%rsp), %ebx
movl %ebx, %r15d
movq 0x20(%rsp), %r11
leaq -0x1(%r11), %rdx
movl -0x4(%rsi,%r11,4), %r13d
testl %r13d, %r13d
je 0x5b4a4
movslq %ebx, %rax
cmpq %rax, %r11
jg 0x5b488
movl %r15d, %edi
subl %ebx, %edi
movl %eax, %esi
movl %edi, %r10d
imull %edi, %r10d
addl (%r14,%rsi,4), %r10d
cmpl %r13d, %r10d
cmovll %r10d, %r13d
cmovlel %eax, %ebx
leaq -0x1(%rax), %rsi
incl %edi
cmpq %rax, %r11
movq %rsi, %rax
jl 0x5b463
xorps %xmm1, %xmm1
cvtsi2sd %r13d, %xmm1
movq %rdx, %rax
imulq %r8, %rax
movsd %xmm1, (%r9,%rax,8)
movq 0x30(%rsp), %rsi
jmp 0x5b4a6
movl %edx, %ebx
decl %r15d
cmpq $0x1, %r11
movq %rdx, %r11
jg 0x5b448
movq (%rsp), %rdx
incq %rdx
addq $0x8, 0x28(%rsp)
movq %rdx, (%rsp)
cmpq %r12, %rdx
jne 0x5b37e
movq %r14, %rdi
movq %rsi, %rbx
callq 0xe5b0
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0
nop