// File: /workspace/github_cpp_makefile_star2_O1/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, %eax
movq %rax, 0x40(%rsp)
movslq 0x23c(%rdi), %rax
cmpl %eax, %ecx
movl %eax, %ebx
movq %rcx, 0x8(%rsp)
cmovgl %ecx, %ebx
movq %rax, 0x48(%rsp)
movl %eax, %eax
movq %rax, 0x28(%rsp)
movslq 0x240(%rdi), %rax
movl %eax, %ecx
movq %rcx, 0x10(%rsp)
cmpl %eax, %ebx
movq %rax, 0x38(%rsp)
cmovlel %eax, %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, 0x20(%rsp)
movl 0x24c(%r12), %ecx
testl %ecx, %ecx
jle 0x306c9
incl %ebx
cvtsi2sd %ebx, %xmm1
movsd 0x71b37(%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 0x306af
cmpl $0x0, 0x40(%rsp)
jle 0x3080f
movq 0x38(%rsp), %rcx
leaq 0x1(%rcx), %r12
movq 0x10(%rsp), %rcx
decl %ecx
movl %ecx, 0x18(%rsp)
movq $0x0, (%rsp)
cmpl $0x0, 0x28(%rsp)
jle 0x307fb
movq 0x20(%rsp), %rcx
movl 0x244(%rcx), %ebp
movl 0x248(%rcx), %r8d
movl 0x18(%rsp), %r9d
imull %r8d, %r9d
movq (%rsp), %rcx
addl %ecx, %r9d
xorl %r10d, %r10d
movl %ecx, %r11d
cmpl $0x0, 0x10(%rsp)
jle 0x30783
movl $0x1, %esi
movq 0x10(%rsp), %rdx
movl %r11d, %ebx
xorl %r13d, %r13d
movslq %ebx, %rcx
movsd (%rax,%rcx,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x3075a
jp 0x3075a
xorl %r13d, %r13d
movsd %xmm0, (%rax,%rcx,8)
xorl %edi, %edi
jmp 0x30763
movl $0x1, %edi
testl %esi, %esi
je 0x3076f
addl %r8d, %ebx
movl %edi, %esi
decq %rdx
jne 0x3073a
jmp 0x30783
incq %r13
movq %r13, %rsi
imulq %r13, %rsi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
jmp 0x30751
cmpl $0x0, 0x38(%rsp)
jle 0x307e7
movl $0x1, %ecx
movl %r9d, %ebx
movq %r12, %r13
xorl %edx, %edx
movslq %ebx, %rsi
movsd (%rax,%rsi,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x307b6
jp 0x307b6
xorl %edx, %edx
movsd %xmm0, (%rax,%rsi,8)
xorl %edi, %edi
jmp 0x307d9
movl $0x1, %edi
testl %ecx, %ecx
jne 0x307d9
incq %rdx
movq %rdx, %rcx
imulq %rdx, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
xorl %edi, %edi
ucomisd %xmm0, %xmm1
ja 0x307ad
decq %r13
subl %r8d, %ebx
movl %edi, %ecx
cmpq $0x1, %r13
jg 0x30797
incl %r10d
addl %ebp, %r11d
addl %ebp, %r9d
cmpl 0x28(%rsp), %r10d
jne 0x30723
movq (%rsp), %rcx
incl %ecx
movq %rcx, (%rsp)
cmpl 0x8(%rsp), %ecx
jne 0x306f0
cmpl $0x0, 0x10(%rsp)
movq 0x40(%rsp), %rbx
movq 0x28(%rsp), %r8
jle 0x309cc
leal -0x1(%rbx), %ecx
movl %ecx, 0x54(%rsp)
movq $0x0, 0x30(%rsp)
movsd 0x717f4(%rip), %xmm0    # 0xa2030
testl %r8d, %r8d
jle 0x309b6
movq 0x20(%rsp), %rcx
movl 0x244(%rcx), %edx
movq %rdx, 0x18(%rsp)
movl 0x248(%rcx), %edi
imull 0x30(%rsp), %edi
movl $0x0, (%rsp)
testl %ebx, %ebx
jle 0x3088f
xorl %ecx, %ecx
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, %rbx
jne 0x3086d
testl %ebx, %ebx
jle 0x308f7
xorl %r9d, %r9d
movl $0x1, %r10d
xorl %ecx, %ecx
movq (%r14,%r9,8), %rdx
testq %rdx, %rdx
je 0x308b6
movslq %ecx, %rsi
movq %r9, %r11
subq %rsi, %r11
jge 0x308bd
movl %ecx, %esi
jmp 0x308e6
movl %r9d, %esi
xorl %edx, %edx
jmp 0x308e6
leaq (%r14,%rsi,8), %r13
movl %ecx, %esi
movl %r11d, %r8d
imull %r8d, %r8d
addq (%r13), %r8
cmpq %rdx, %r8
cmovlel %ecx, %esi
cmovlq %r8, %rdx
decq %r11
addq $0x8, %r13
incl %ecx
cmpl %ecx, %r10d
jne 0x308c3
movq %rdx, (%r15,%r9,8)
incq %r9
incl %r10d
movl %esi, %ecx
cmpq %rbx, %r9
jne 0x3089e
cmpl $0x0, 0x8(%rsp)
jle 0x3099a
movq 0x20(%rsp), %rcx
movl 0x248(%rcx), %r9d
imull 0x30(%rsp), %r9d
movl 0x244(%rcx), %r10d
imull (%rsp), %r10d
movl 0x54(%rsp), %ecx
movl %ecx, %r8d
movq 0x8(%rsp), %r13
leaq -0x1(%r13), %rdx
movq -0x8(%r15,%r13,8), %rsi
testq %rsi, %rsi
je 0x3098c
movslq %ecx, %rbx
cmpq %rbx, %rdx
jg 0x3096e
movl %r8d, %r11d
subl %ecx, %r11d
movl %ebx, %r12d
movl %r11d, %ebp
imull %ebp, %ebp
addq (%r14,%r12,8), %rbp
cmpq %rsi, %rbp
cmovlel %ebx, %ecx
cmovlq %rbp, %rsi
leaq -0x1(%rbx), %r12
incl %r11d
cmpq %rbx, %r13
movq %r12, %rbx
jle 0x30948
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
leal (%r9,%rdx), %esi
addl %r10d, %esi
movslq %esi, %rsi
movsd %xmm1, (%rax,%rsi,8)
movq 0x40(%rsp), %rbx
jmp 0x3098e
movl %edx, %ecx
decl %r8d
cmpq $0x1, %r13
movq %rdx, %r13
jg 0x3092c
movl (%rsp), %ecx
incl %ecx
addq 0x18(%rsp), %rdi
movl %ecx, (%rsp)
cmpl 0x48(%rsp), %ecx
movq 0x28(%rsp), %r8
jne 0x30867
movq 0x30(%rsp), %rcx
incl %ecx
movq %rcx, 0x30(%rsp)
cmpl 0x10(%rsp), %ecx
jne 0x3083c
cmpl $0x0, 0x38(%rsp)
jle 0x30b86
leal -0x1(%r8), %ecx
movl %ecx, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movsd 0x71640(%rip), %xmm0    # 0xa2030
testl %ebx, %ebx
jle 0x30b70
movq 0x20(%rsp), %rcx
movl 0x244(%rcx), %esi
movl 0x248(%rcx), %r9d
imull 0x8(%rsp), %r9d
movl $0x0, (%rsp)
testl %r8d, %r8d
jle 0x30a42
movl %r9d, %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, %r8
jne 0x30a21
movl %r9d, 0x10(%rsp)
testl %r8d, %r8d
jle 0x30aaf
xorl %r9d, %r9d
movl $0x1, %r10d
xorl %edx, %edx
movq (%r14,%r9,8), %r11
testq %r11, %r11
je 0x30a6f
movslq %edx, %rcx
movq %r9, %rbx
subq %rcx, %rbx
jge 0x30a77
movl %edx, %ecx
jmp 0x30a9e
movl %r9d, %ecx
xorl %r11d, %r11d
jmp 0x30a9e
leaq (%r14,%rcx,8), %r13
movl %edx, %ecx
movl %ebx, %edi
imull %edi, %edi
addq (%r13), %rdi
cmpq %r11, %rdi
cmovlel %edx, %ecx
cmovlq %rdi, %r11
decq %rbx
addq $0x8, %r13
incl %edx
cmpl %edx, %r10d
jne 0x30a7d
movq %r11, (%r15,%r9,8)
incq %r9
incl %r10d
movl %ecx, %edx
cmpq %r8, %r9
jne 0x30a57
cmpl $0x0, 0x48(%rsp)
jle 0x30b4e
movq 0x20(%rsp), %rcx
movl 0x248(%rcx), %r9d
imull 0x8(%rsp), %r9d
movl 0x244(%rcx), %r10d
addl (%rsp), %r9d
movl 0x18(%rsp), %ebx
movl %ebx, %r8d
movq 0x48(%rsp), %r13
leaq -0x1(%r13), %rdx
movq -0x8(%r15,%r13,8), %rcx
testq %rcx, %rcx
je 0x30b40
movslq %ebx, %rdi
cmpq %rdi, %r13
jg 0x30b25
movl %r8d, %r11d
subl %ebx, %r11d
movl %edi, %r12d
movl %r11d, %ebp
imull %ebp, %ebp
addq (%r14,%r12,8), %rbp
cmpq %rcx, %rbp
cmovlel %edi, %ebx
cmovlq %rbp, %rcx
leaq -0x1(%rdi), %r12
incl %r11d
cmpq %rdi, %r13
movq %r12, %rdi
jl 0x30aff
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
movl %r10d, %ecx
imull %edx, %ecx
addl %r9d, %ecx
movslq %ecx, %rcx
movsd %xmm1, (%rax,%rcx,8)
jmp 0x30b42
movl %edx, %ebx
decl %r8d
cmpq $0x1, %r13
movq %rdx, %r13
jg 0x30ae3
movl (%rsp), %ecx
incl %ecx
movl 0x10(%rsp), %r9d
incl %r9d
movq 0x40(%rsp), %rbx
movl %ecx, (%rsp)
cmpl %ebx, %ecx
movq 0x28(%rsp), %r8
jne 0x30a17
movq 0x8(%rsp), %rcx
incl %ecx
movq %rcx, 0x8(%rsp)
cmpl 0x38(%rsp), %ecx
jne 0x309f0
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