// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rdi, %r12
movslq 0x238(%rdi), %rcx
movl %ecx, %r13d
movslq 0x23c(%rdi), %rax
cmpl %eax, %ecx
movl %eax, %ebx
movq %rcx, 0x10(%rsp)
cmovgl %ecx, %ebx
movq %rax, 0x40(%rsp)
movl %eax, %ebp
movslq 0x240(%rdi), %rcx
movl %ecx, %eax
movq %rax, 0x28(%rsp)
cmpl %ecx, %ebx
movq %rcx, 0x8(%rsp)
cmovlel %ecx, %ebx
movslq %ebx, %rax
leaq (,%rax,8), %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 0x230(%r12), %rax
movq %r12, 0x18(%rsp)
movl 0x24c(%r12), %ecx
testl %ecx, %ecx
jle 0x304be
incl %ebx
cvtsi2sd %ebx, %xmm1
movsd 0x71d42(%rip), %xmm0    # 0xa21d8
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
xorl %edx, %edx
xorpd %xmm1, %xmm1
movsd (%rax,%rdx,8), %xmm2
cmpeqsd %xmm1, %xmm2
andpd %xmm0, %xmm2
movlpd %xmm2, (%rax,%rdx,8)
incq %rdx
cmpl %edx, %ecx
jne 0x304a4
movq %r13, 0x30(%rsp)
testl %r13d, %r13d
movq %rbp, 0x48(%rsp)
jle 0x3060d
movq 0x8(%rsp), %rcx
leaq 0x1(%rcx), %r12
decl %ecx
movl %ecx, 0x20(%rsp)
movq $0x0, (%rsp)
testl %ebp, %ebp
jle 0x305f9
movq 0x18(%rsp), %rcx
movl 0x244(%rcx), %edi
movl 0x248(%rcx), %r8d
movl 0x20(%rsp), %r9d
imull %r8d, %r9d
movq (%rsp), %rcx
addl %ecx, %r9d
xorl %r10d, %r10d
movl %ecx, %r11d
cmpl $0x0, 0x8(%rsp)
jle 0x305e2
movl $0x1, %ebp
movq 0x28(%rsp), %rcx
movl %r11d, %edx
xorl %ebx, %ebx
movslq %edx, %r13
movsd (%rax,%r13,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x30553
jp 0x30553
xorl %ebx, %ebx
movsd %xmm0, (%rax,%r13,8)
xorl %esi, %esi
jmp 0x3055c
movl $0x1, %esi
testl %ebp, %ebp
je 0x30568
addl %r8d, %edx
movl %esi, %ebp
decq %rcx
jne 0x30532
jmp 0x3057c
incq %rbx
movq %rbx, %rsi
imulq %rbx, %rsi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
jmp 0x30549
cmpl $0x0, 0x28(%rsp)
jle 0x305e2
movl $0x1, %ebp
movl %r9d, %ebx
movq %r12, %rcx
xorl %edx, %edx
movslq %ebx, %r13
movsd (%rax,%r13,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x305b1
jp 0x305b1
xorl %edx, %edx
movsd %xmm0, (%rax,%r13,8)
xorl %esi, %esi
jmp 0x305d4
movl $0x1, %esi
testl %ebp, %ebp
jne 0x305d4
incq %rdx
movq %rdx, %rsi
imulq %rdx, %rsi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
xorl %esi, %esi
ucomisd %xmm0, %xmm1
ja 0x305a7
decq %rcx
subl %r8d, %ebx
movl %esi, %ebp
cmpq $0x1, %rcx
jg 0x30590
incl %r10d
addl %edi, %r11d
addl %edi, %r9d
movq 0x48(%rsp), %rbp
cmpl %ebp, %r10d
jne 0x30518
movq (%rsp), %rcx
incl %ecx
movq %rcx, (%rsp)
cmpl 0x10(%rsp), %ecx
jne 0x304e8
cmpl $0x0, 0x28(%rsp)
movq 0x30(%rsp), %rcx
jle 0x3098d
decl %ecx
movl %ecx, 0x54(%rsp)
movq $0x0, 0x38(%rsp)
movsd 0x719fc(%rip), %xmm0    # 0xa2030
testl %ebp, %ebp
jle 0x307b7
movq 0x18(%rsp), %rcx
movl 0x244(%rcx), %edx
movq %rdx, 0x20(%rsp)
movl 0x248(%rcx), %edi
imull 0x38(%rsp), %edi
movl $0x0, (%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x307a0
xorl %ecx, %ecx
movq 0x30(%rsp), %rsi
leal (%rdi,%rcx), %edx
movslq %edx, %rdx
movsd (%rax,%rdx,8), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edx
movslq %edx, %rdx
movq %rdx, (%r14,%rcx,8)
incq %rcx
cmpq %rcx, %rsi
jne 0x30670
testl %esi, %esi
jle 0x307a0
xorl %r9d, %r9d
movl $0x1, %r10d
xorl %ecx, %ecx
movq 0x30(%rsp), %r8
movq (%r14,%r9,8), %rdx
testq %rdx, %rdx
je 0x306c3
movslq %ecx, %rsi
movq %r9, %rbx
subq %rsi, %rbx
jge 0x306ca
movl %ecx, %r11d
jmp 0x306f3
movl %r9d, %r11d
xorl %edx, %edx
jmp 0x306f3
leaq (%r14,%rsi,8), %r13
movl %ecx, %r11d
movl %ebx, %esi
imull %esi, %esi
addq (%r13), %rsi
cmpq %rdx, %rsi
cmovlel %ecx, %r11d
cmovlq %rsi, %rdx
decq %rbx
addq $0x8, %r13
incl %ecx
cmpl %ecx, %r10d
jne 0x306d1
movq %rdx, (%r15,%r9,8)
incq %r9
incl %r10d
movl %r11d, %ecx
cmpq %r8, %r9
jne 0x306aa
testl %r8d, %r8d
jle 0x307a0
movq 0x18(%rsp), %rcx
movl 0x248(%rcx), %r9d
imull 0x38(%rsp), %r9d
movl 0x244(%rcx), %r10d
imull (%rsp), %r10d
movl 0x54(%rsp), %ecx
movl %ecx, %ebp
movq 0x10(%rsp), %rbx
leaq -0x1(%rbx), %rdx
movq -0x8(%r15,%rbx,8), %r13
testq %r13, %r13
je 0x30793
movslq %ecx, %rsi
cmpq %rsi, %rdx
jg 0x3077a
movl %ebp, %r11d
subl %ecx, %r11d
movl %esi, %r8d
movl %r11d, %r12d
imull %r12d, %r12d
addq (%r14,%r8,8), %r12
cmpq %r13, %r12
cmovlel %esi, %ecx
cmovlq %r12, %r13
leaq -0x1(%rsi), %r8
incl %r11d
cmpq %rsi, %rbx
movq %r8, %rsi
jle 0x30753
xorps %xmm1, %xmm1
cvtsi2sd %r13, %xmm1
leal (%r9,%rdx), %esi
addl %r10d, %esi
movslq %esi, %rsi
movsd %xmm1, (%rax,%rsi,8)
jmp 0x30795
movl %edx, %ecx
decl %ebp
cmpq $0x1, %rbx
movq %rdx, %rbx
jg 0x30737
movl (%rsp), %ecx
incl %ecx
addq 0x20(%rsp), %rdi
movl %ecx, (%rsp)
cmpl 0x40(%rsp), %ecx
jne 0x3065e
movq 0x38(%rsp), %rcx
incl %ecx
movq %rcx, 0x38(%rsp)
cmpl 0x8(%rsp), %ecx
movq 0x48(%rsp), %rbp
jne 0x30634
cmpl $0x0, 0x28(%rsp)
movq 0x30(%rsp), %r8
jle 0x3098d
leal -0x1(%rbp), %ecx
movl %ecx, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
testl %r8d, %r8d
jle 0x30977
movq 0x18(%rsp), %rcx
movl 0x244(%rcx), %esi
movl 0x248(%rcx), %ecx
imull 0x8(%rsp), %ecx
movl %ecx, 0x10(%rsp)
movl $0x0, (%rsp)
cmpl $0x0, 0x40(%rsp)
jle 0x30962
movl 0x10(%rsp), %ecx
xorl %edx, %edx
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 %esi, %ecx
cmpq %rdx, %rbp
jne 0x3082d
testl %ebp, %ebp
jle 0x30962
xorl %r9d, %r9d
movl $0x1, %r10d
xorl %ecx, %ecx
movq (%r14,%r9,8), %r11
testq %r11, %r11
je 0x30879
movslq %ecx, %rdx
movq %r9, %rbx
subq %rdx, %rbx
jge 0x30881
movl %ecx, %edx
jmp 0x308a8
movl %r9d, %edx
xorl %r11d, %r11d
jmp 0x308a8
leaq (%r14,%rdx,8), %r13
movl %ecx, %edx
movl %ebx, %edi
imull %edi, %edi
addq (%r13), %rdi
cmpq %r11, %rdi
cmovlel %ecx, %edx
cmovlq %rdi, %r11
decq %rbx
addq $0x8, %r13
incl %ecx
cmpl %ecx, %r10d
jne 0x30887
movq %r11, (%r15,%r9,8)
incq %r9
incl %r10d
movl %edx, %ecx
cmpq %rbp, %r9
jne 0x30861
testl %ebp, %ebp
jle 0x30962
movq 0x18(%rsp), %rcx
movl 0x248(%rcx), %r9d
imull 0x8(%rsp), %r9d
movl 0x244(%rcx), %r10d
addl (%rsp), %r9d
movl 0x20(%rsp), %r13d
movl %r13d, %r11d
movq 0x40(%rsp), %rbx
leaq -0x1(%rbx), %rcx
movq -0x8(%r15,%rbx,8), %rdx
testq %rdx, %rdx
je 0x30953
movslq %r13d, %rdi
cmpq %rdi, %rbx
jg 0x3092e
movl %r11d, %ebp
subl %r13d, %ebp
movl %edi, %r12d
movl %ebp, %r8d
imull %r8d, %r8d
addq (%r14,%r12,8), %r8
cmpq %rdx, %r8
cmovlel %edi, %r13d
cmovlq %r8, %rdx
leaq -0x1(%rdi), %r8
incl %ebp
cmpq %rdi, %rbx
movq %r8, %rdi
jl 0x30907
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
movl %r10d, %edx
imull %ecx, %edx
addl %r9d, %edx
movslq %edx, %rdx
movsd %xmm1, (%rax,%rdx,8)
movq 0x30(%rsp), %r8
movq 0x48(%rsp), %rbp
jmp 0x30956
movl %ecx, %r13d
decl %r11d
cmpq $0x1, %rbx
movq %rcx, %rbx
jg 0x308eb
movl (%rsp), %ecx
incl %ecx
incl 0x10(%rsp)
movl %ecx, (%rsp)
cmpl %r8d, %ecx
jne 0x3081c
movq 0x8(%rsp), %rcx
incl %ecx
movq %rcx, 0x8(%rsp)
cmpl 0x28(%rsp), %ecx
jne 0x307f2
movq %r14, %rdi
callq 0xe5b0
movq %r15, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0
nop