// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::Voronoi(gaps::R3Grid*)
void R3Grid::
Voronoi(R3Grid *squared_distance_grid)
{
  int dist;
  int* old_dist;
  int* new_dist;
  RNScalar value;
  RNScalar *old_value;
  RNScalar *new_value;
  R3Grid *dgrid;
  int res,square,tmp_dist,first;
  int x,y,z,s,t,i;

  // Allocate distance grid
  if (squared_distance_grid) dgrid = squared_distance_grid;
  else dgrid = new R3Grid(XResolution(), YResolution(), ZResolution());
  assert(dgrid);
  dgrid->SetWorldToGridTransformation(WorldToGridTransformation());

  // Allocate temporary buffers
  res = XResolution();
  if (res < YResolution()) res = YResolution();
  if (res < ZResolution()) res = ZResolution();
  old_dist = new int[res];
  new_dist = new int[res];
  old_value = new RNScalar[res];
  new_value = new RNScalar[res];
  assert(old_dist && new_dist && old_value && new_value);

  // Initalize distance grid values (0 if was set, max_value if not)
  RNScalar max_value = 3 * (res+1) * (res+1) * (res+1);
  for (i = 0; i < grid_size; i++) {
    if (grid_values[i] == 0.0) dgrid->grid_values[i] = max_value;
    else dgrid->grid_values[i] = 0.0;
  }

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

  // 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++) {
        old_dist[x] = (int) (dgrid->GridValue(x, y, z) + 0.5);
        old_value[x] = GridValue(x, y, z);
      }
		
      // forward scan
      s = 0;
      for (x = 0; x < XResolution(); x++) {
        dist = old_dist[x];
        value = old_value[x];
        if (dist) {
          for(t = s; t <= x; t++) {
            tmp_dist = old_dist[t] + (x - t) * (x - t);
            if (tmp_dist <= dist) {
              dist = tmp_dist;
              value = old_value[t];
              s = t;
            }
          }
        }
        else {
          s = x;
        }
        new_dist[x] = dist;
        new_value[x] = value;
      }
			
      // backwards scan
      s = XResolution() - 1;
      for (x = XResolution()-1; x >= 0 ; x--) {
        dist = new_dist[x];
        value = new_value[x];
        if (dist) {
          for (t = s; t >= x; t--) {
            tmp_dist = old_dist[t] + (x - t) * (x - t);
            if (tmp_dist <= dist) {
              dist = tmp_dist;
              value = old_value[t];
              s = t;
            }
          }
          dgrid->SetGridValue(x, y, z, dist);
          SetGridValue(x, y, z, value);
        }
        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++) {
        old_dist[y] = (int) (dgrid->GridValue(x, y, z));
        old_value[y] = GridValue(x, y, z);
      }
			
      // forward scan
      s = 0;
      for (y = 0; y < YResolution(); y++) {
        dist = old_dist[y];
        value = old_value[y];
        if (dist) {
          for (t = s; t <= y ; t++) {
            tmp_dist = old_dist[t] + (y - t) * (y - t);
            if (tmp_dist <= dist){
              dist = tmp_dist;
              value = old_value[t];
              s = t;
            }
          }
        }
        else { 
          s = y;
        }
        new_dist[y] = dist;
        new_value[y] = value;
      }

      // backward scan
      s = YResolution() - 1;
      for(y = YResolution()-1; y >=0 ; y--) {
        dist = new_dist[y];
        value = new_value[y];
        if (dist) {
          for (t = s; t > y ; t--) {
            tmp_dist = old_dist[t] + (y - t) * (y - t);
            if (tmp_dist <= dist){
              dist = tmp_dist;
              value = old_value[t];
              s = t;
            }
          }
          dgrid->SetGridValue(x, y, z, dist);
          SetGridValue(x, y, z, value);
        }
        else { 
          s = y; 
        }
      }
    }
  }
	
  // Delete temporary buffers
  if (!squared_distance_grid) delete dgrid;
  delete[] old_dist;
  delete[] new_dist;
  delete[] old_value;
  delete[] new_value;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r14
movq %rdi, %r12
movq %rsi, %rbx
testq %rsi, %rsi
jne 0x32b40
movl $0x250, %edi            # imm = 0x250
callq 0xe440
movq %rax, %rbx
movl 0x238(%r12), %esi
movl 0x23c(%r12), %edx
movl 0x240(%r12), %ecx
movq %rax, %rdi
callq 0x2e74c
movq %r14, 0x78(%rsp)
movq %rbx, 0x40(%rsp)
leaq 0x110(%r12), %rsi
movq %rbx, %rdi
callq 0x3336c
movslq 0x238(%r12), %rcx
movl %ecx, %eax
movq %rax, 0x50(%rsp)
movslq 0x23c(%r12), %rax
cmpl %eax, %ecx
movl %eax, %r15d
movq %rcx, 0x30(%rsp)
cmovgl %ecx, %r15d
movq %rax, 0x68(%rsp)
movl %eax, %eax
movq %rax, 0x58(%rsp)
movslq 0x240(%r12), %rax
movl %eax, %ecx
movq %rcx, 0x28(%rsp)
cmpl %eax, %r15d
movq %rax, 0x48(%rsp)
cmovlel %eax, %r15d
movslq %r15d, %rax
leaq (,%rax,4), %r14
testl %eax, %eax
movq $-0x1, %rcx
cmovsq %rcx, %r14
leaq (,%rax,8), %rbx
cmovsq %rcx, %rbx
movq %r14, %rdi
callq 0xe030
movq %r12, 0x18(%rsp)
movq %rax, %r12
movq %r14, %rdi
movq 0x40(%rsp), %r14
callq 0xe030
movq %rax, %r13
movq %rbx, %rdi
callq 0xe030
movq %rax, %rbp
movq %rbx, %rdi
callq 0xe030
movq %rax, 0x60(%rsp)
movq 0x18(%rsp), %rax
movslq 0x24c(%rax), %rax
testq %rax, %rax
jle 0x32c5b
incl %r15d
movl %r15d, %ecx
imull %ecx, %ecx
imull %r15d, %ecx
leal (%rcx,%rcx,2), %ecx
cvtsi2sd %ecx, %xmm0
movq 0x18(%rsp), %rcx
movq 0x230(%rcx), %rcx
movq 0x230(%r14), %rdx
xorl %esi, %esi
xorpd %xmm1, %xmm1
movsd (%rcx,%rsi,8), %xmm2
cmpeqsd %xmm1, %xmm2
andpd %xmm0, %xmm2
movlpd %xmm2, (%rdx,%rsi,8)
incq %rsi
cmpq %rsi, %rax
jne 0x32c40
cmpl $0x0, 0x50(%rsp)
movq 0x58(%rsp), %r11
movq 0x60(%rsp), %rbx
movq 0x28(%rsp), %rdx
jle 0x32e5b
movq 0x48(%rsp), %rax
incq %rax
movq %rax, 0x38(%rsp)
leal -0x1(%rdx), %eax
movl %eax, 0x20(%rsp)
movl $0x0, 0x10(%rsp)
xorpd %xmm0, %xmm0
testl %r11d, %r11d
jle 0x32e47
xorl %ecx, %ecx
testl %edx, %edx
movl %ecx, 0xc(%rsp)
jle 0x32d53
movq 0x230(%r14), %rdi
movl 0x248(%r14), %r8d
movq 0x18(%rsp), %rax
movq 0x230(%rax), %r9
movl 0x248(%rax), %r10d
movl 0x244(%rax), %r11d
imull %ecx, %r11d
movl 0x10(%rsp), %eax
addl %eax, %r11d
movl 0x244(%r14), %r15d
imull %ecx, %r15d
addl %eax, %r15d
movl $0x1, %r14d
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
movq %rdx, %rax
movslq %r15d, %rsi
movsd (%rdi,%rsi,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x32d1b
jp 0x32d1b
movslq %r11d, %rcx
movsd (%r9,%rcx,8), %xmm1
xorl %edx, %edx
xorl %ecx, %ecx
jmp 0x32d43
movl $0x1, %edx
testl %r14d, %r14d
jne 0x32d43
incl %ecx
movl %ecx, %edx
imull %ecx, %edx
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
movsd %xmm2, (%rdi,%rsi,8)
movslq %r11d, %rdx
movsd %xmm1, (%r9,%rdx,8)
xorl %edx, %edx
addl %r10d, %r11d
addl %r8d, %r15d
movl %edx, %r14d
decq %rax
jne 0x32cfc
jmp 0x32d57
xorpd %xmm1, %xmm1
cmpl $0x0, 0x48(%rsp)
jle 0x32e29
movq 0x40(%rsp), %rcx
movq 0x230(%rcx), %rdi
movl 0x248(%rcx), %r8d
movq 0x18(%rsp), %rax
movq 0x230(%rax), %r9
movl 0x248(%rax), %r10d
movl 0x244(%rax), %eax
movl 0xc(%rsp), %esi
imull %esi, %eax
movl 0x10(%rsp), %edx
addl %edx, %eax
movl 0x20(%rsp), %r15d
movl %r15d, %r11d
imull %r10d, %r11d
addl %eax, %r11d
movl 0x244(%rcx), %eax
imull %esi, %eax
addl %edx, %eax
imull %r8d, %r15d
addl %eax, %r15d
movl $0x1, %edx
xorl %ecx, %ecx
movq 0x38(%rsp), %rax
movslq %r15d, %rsi
movsd (%rdi,%rsi,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x32de8
jp 0x32de8
movslq %r11d, %rcx
movsd (%r9,%rcx,8), %xmm1
xorl %r14d, %r14d
xorl %ecx, %ecx
jmp 0x32e17
movl $0x1, %r14d
testl %edx, %edx
jne 0x32e17
incl %ecx
movl %ecx, %edx
imull %ecx, %edx
xorps %xmm3, %xmm3
cvtsi2sd %edx, %xmm3
xorl %r14d, %r14d
ucomisd %xmm3, %xmm2
jbe 0x32e17
movsd %xmm3, (%rdi,%rsi,8)
movslq %r11d, %rdx
movsd %xmm1, (%r9,%rdx,8)
decq %rax
subl %r10d, %r11d
subl %r8d, %r15d
movl %r14d, %edx
cmpq $0x1, %rax
jg 0x32dc8
movl 0xc(%rsp), %ecx
incl %ecx
movq 0x58(%rsp), %r11
cmpl %r11d, %ecx
movq 0x40(%rsp), %r14
movq 0x28(%rsp), %rdx
jne 0x32ca0
movl 0x10(%rsp), %eax
incl %eax
movl %eax, 0x10(%rsp)
cmpl 0x30(%rsp), %eax
jne 0x32c95
testl %edx, %edx
movq 0x50(%rsp), %rdi
jle 0x330bd
leal -0x1(%rdi), %eax
movl %eax, 0x74(%rsp)
movq $0x0, 0x10(%rsp)
movsd 0x6f1b0(%rip), %xmm0    # 0xa2030
testl %r11d, %r11d
jle 0x330a7
movq 0x18(%rsp), %rax
movq 0x230(%rax), %r8
movl 0x244(%rax), %ecx
movq %rcx, 0x80(%rsp)
movl 0x248(%rax), %r9d
imull 0x10(%rsp), %r9d
movl $0x0, 0xc(%rsp)
movq %r8, 0x20(%rsp)
testl %edi, %edi
jle 0x32f16
movq 0x230(%r14), %rax
movl 0x248(%r14), %edx
imull 0x10(%rsp), %edx
movl 0x244(%r14), %ecx
imull 0xc(%rsp), %ecx
addl %edx, %ecx
xorl %edx, %edx
leal (%rcx,%rdx), %esi
movslq %esi, %rsi
movsd (%rax,%rsi,8), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %esi
movl %esi, (%r12,%rdx,4)
leal (%r9,%rdx), %esi
movslq %esi, %rsi
movsd (%r8,%rsi,8), %xmm1
movsd %xmm1, (%rbp,%rdx,8)
incq %rdx
cmpq %rdx, %rdi
jne 0x32ee4
movq %r9, 0x38(%rsp)
testl %edi, %edi
jle 0x32f85
xorl %eax, %eax
movl $0x1, %ecx
xorl %esi, %esi
movl (%r12,%rax,4), %r9d
movsd (%rbp,%rax,8), %xmm1
testl %r9d, %r9d
je 0x32f6c
movslq %esi, %rdx
cmpq %rdx, %rax
jl 0x32f71
movl %eax, %r8d
subl %esi, %r8d
movl %r8d, %r10d
imull %r8d, %r10d
addl (%r12,%rdx,4), %r10d
cmpl %r9d, %r10d
jg 0x32f60
movsd (%rbp,%rdx,8), %xmm1
movl %edx, %esi
movl %r10d, %r9d
incq %rdx
decl %r8d
cmpl %edx, %ecx
jne 0x32f45
jmp 0x32f71
movl %eax, %esi
xorl %r9d, %r9d
movl %r9d, (%r13,%rax,4)
movsd %xmm1, (%rbx,%rax,8)
incq %rax
incl %ecx
cmpq %rdi, %rax
jne 0x32f28
cmpl $0x0, 0x30(%rsp)
jle 0x33072
movq 0x18(%rsp), %rax
movq 0x230(%rax), %r9
movl 0x248(%rax), %r10d
imull 0x10(%rsp), %r10d
movl 0x244(%rax), %r11d
imull 0xc(%rsp), %r11d
movl 0x74(%rsp), %ecx
movl %ecx, %ebx
movq 0x30(%rsp), %rsi
leaq -0x1(%rsi), %r15
movl -0x4(%r13,%rsi,4), %edx
testl %edx, %edx
je 0x33060
movq 0x60(%rsp), %rax
movsd (%rax,%r15,8), %xmm1
movslq %ecx, %r8
cmpq %r8, %r15
jg 0x33017
movl %ebx, %r14d
subl %ecx, %r14d
movl %r8d, %edi
movl %r14d, %eax
imull %r14d, %eax
addl (%r12,%rdi,4), %eax
cmpl %edx, %eax
jg 0x33008
movsd (%rbp,%rdi,8), %xmm1
movl %r8d, %ecx
movl %eax, %edx
leaq -0x1(%r8), %rax
incl %r14d
cmpq %r8, %rsi
movq %rax, %r8
jle 0x32feb
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
movq 0x40(%rsp), %r14
movq 0x230(%r14), %rax
movl 0x248(%r14), %edx
imull 0x10(%rsp), %edx
movl 0x244(%r14), %edi
imull 0xc(%rsp), %edi
addl %r15d, %edx
addl %edi, %edx
movslq %edx, %rdx
movsd %xmm2, (%rax,%rdx,8)
leal (%r10,%r15), %eax
addl %r11d, %eax
cltq
movsd %xmm1, (%r9,%rax,8)
jmp 0x33063
movl %r15d, %ecx
decl %ebx
cmpq $0x1, %rsi
movq %r15, %rsi
jg 0x32fc1
movl 0xc(%rsp), %eax
incl %eax
movq 0x38(%rsp), %r9
addq 0x80(%rsp), %r9
movl %eax, 0xc(%rsp)
cmpl 0x68(%rsp), %eax
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %r11
movq 0x60(%rsp), %rbx
movq 0x20(%rsp), %r8
jne 0x32ebd
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
cmpl 0x28(%rsp), %eax
jne 0x32e80
cmpl $0x0, 0x48(%rsp)
jle 0x33305
leal -0x1(%r11), %eax
movl %eax, 0x20(%rsp)
movq $0x0, 0x10(%rsp)
testl %edi, %edi
jle 0x332ef
movq 0x18(%rsp), %rax
movq 0x230(%rax), %r9
movl 0x244(%rax), %r10d
movl 0x248(%rax), %r15d
imull 0x10(%rsp), %r15d
movl $0x0, 0xc(%rsp)
movq %r9, 0x30(%rsp)
movl %r10d, 0x28(%rsp)
testl %r11d, %r11d
jle 0x33164
movq 0x230(%r14), %rax
movl 0x244(%r14), %ecx
movl 0x248(%r14), %edx
imull 0x10(%rsp), %edx
addl 0xc(%rsp), %edx
movl %r15d, %esi
xorl %edi, %edi
movslq %edx, %rdx
cvttsd2si (%rax,%rdx,8), %r8d
movl %r8d, (%r12,%rdi,4)
movslq %esi, %rsi
movsd (%r9,%rsi,8), %xmm0
movsd %xmm0, (%rbp,%rdi,8)
incq %rdi
addl %r10d, %esi
addl %ecx, %edx
cmpq %rdi, %r11
jne 0x3313b
movl %r15d, 0x38(%rsp)
testl %r11d, %r11d
jle 0x331d1
xorl %eax, %eax
movl $0x1, %ecx
xorl %edi, %edi
movl (%r12,%rax,4), %r9d
movsd (%rbp,%rax,8), %xmm0
testl %r9d, %r9d
je 0x331b8
movslq %edi, %rdx
cmpq %rdx, %rax
jl 0x331bd
movl %eax, %esi
subl %edi, %esi
movl %esi, %r10d
imull %esi, %r10d
addl (%r12,%rdx,4), %r10d
cmpl %r9d, %r10d
jg 0x331ad
movsd (%rbp,%rdx,8), %xmm0
movl %edx, %edi
movl %r10d, %r9d
incq %rdx
decl %esi
cmpl %edx, %ecx
jne 0x33192
jmp 0x331bd
movl %eax, %edi
xorl %r9d, %r9d
movl %r9d, (%r13,%rax,4)
movsd %xmm0, (%rbx,%rax,8)
incq %rax
incl %ecx
cmpq %r11, %rax
jne 0x33177
cmpl $0x0, 0x68(%rsp)
jle 0x332c1
movq 0x18(%rsp), %rax
movq 0x230(%rax), %r9
movl 0x248(%rax), %r10d
imull 0x10(%rsp), %r10d
movl 0x244(%rax), %r11d
addl 0xc(%rsp), %r10d
movl 0x20(%rsp), %ecx
movl %ecx, %eax
movq 0x68(%rsp), %rdi
leaq -0x1(%rdi), %r15
movl -0x4(%r13,%rdi,4), %edx
testl %edx, %edx
je 0x332af
movsd (%rbx,%r15,8), %xmm0
movslq %ecx, %rsi
cmpq %rsi, %rdi
jg 0x3325d
movl %eax, %r14d
subl %ecx, %r14d
movl %esi, %r8d
movl %r14d, %ebx
imull %r14d, %ebx
addl (%r12,%r8,4), %ebx
cmpl %edx, %ebx
jg 0x3324e
movsd (%rbp,%r8,8), %xmm0
movl %esi, %ecx
movl %ebx, %edx
leaq -0x1(%rsi), %r8
incl %r14d
cmpq %rsi, %rdi
movq %r8, %rsi
jl 0x33231
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movq 0x40(%rsp), %r14
movq 0x230(%r14), %rdx
movl 0x248(%r14), %esi
imull 0x10(%rsp), %esi
movl %r11d, %r8d
imull %r15d, %r8d
movl 0x244(%r14), %ebx
imull %r15d, %ebx
addl 0xc(%rsp), %esi
addl %ebx, %esi
movslq %esi, %rsi
movsd %xmm1, (%rdx,%rsi,8)
addl %r10d, %r8d
movslq %r8d, %rdx
movsd %xmm0, (%r9,%rdx,8)
movq 0x60(%rsp), %rbx
jmp 0x332b2
movl %r15d, %ecx
decl %eax
cmpq $0x1, %rdi
movq %r15, %rdi
jg 0x3320c
movl 0xc(%rsp), %eax
incl %eax
movl 0x38(%rsp), %r15d
incl %r15d
movq 0x50(%rsp), %rdi
movl %eax, 0xc(%rsp)
cmpl %edi, %eax
movq 0x58(%rsp), %r11
movq 0x30(%rsp), %r9
movl 0x28(%rsp), %r10d
jne 0x33113
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
cmpl 0x48(%rsp), %eax
jne 0x330d9
cmpq $0x0, 0x78(%rsp)
jne 0x33322
movq %r14, %rdi
callq 0x2f17c
movl $0x250, %esi            # imm = 0x250
movq %r14, %rdi
callq 0xe450
movq %r12, %rdi
callq 0xe5b0
movq %r13, %rdi
callq 0xe5b0
movq %rbp, %rdi
callq 0xe5b0
movq %rbx, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0
movq %rax, %r14
movl $0x250, %esi            # imm = 0x250
movq %rbx, %rdi
callq 0xe450
movq %r14, %rdi
callq 0xe7e0
nop