// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movq %rdi, %r12
movq %rsi, %r15
testq %rsi, %rsi
jne 0x3298c
movl $0x250, %edi            # imm = 0x250
callq 0xe440
movq %rax, %r15
movl 0x238(%r12), %esi
movl 0x23c(%r12), %edx
movl 0x240(%r12), %ecx
movq %rax, %rdi
callq 0x2e450
movq %r15, 0x28(%rsp)
movq %rbx, 0x78(%rsp)
leaq 0x110(%r12), %rsi
movq %r15, %rdi
callq 0x331b6
movslq 0x238(%r12), %rcx
movl %ecx, %eax
movq %rax, 0x20(%rsp)
movslq 0x23c(%r12), %rax
cmpl %eax, %ecx
movl %eax, %ebx
movq %rcx, 0x18(%rsp)
cmovgl %ecx, %ebx
movq %rax, 0x60(%rsp)
movl %eax, %eax
movq %rax, 0x58(%rsp)
movslq 0x240(%r12), %rcx
movl %ecx, %eax
movq %rax, 0x38(%rsp)
cmpl %ecx, %ebx
movq %rcx, 0x30(%rsp)
cmovlel %ecx, %ebx
movslq %ebx, %rax
leaq (,%rax,4), %r14
testl %eax, %eax
movq $-0x1, %rcx
cmovsq %rcx, %r14
leaq (,%rax,8), %r15
cmovsq %rcx, %r15
movq %r14, %rdi
callq 0xe030
movq %r12, %r13
movq %rax, %r12
movq %r14, %rdi
callq 0xe030
movq %rax, 0x50(%rsp)
movq %r15, %rdi
callq 0xe030
movq %rax, %rbp
movq %r15, %rdi
movq 0x28(%rsp), %r14
callq 0xe030
movq %rax, 0x48(%rsp)
movq %r13, 0x10(%rsp)
movslq 0x24c(%r13), %rax
testq %rax, %rax
jle 0x32aa0
incl %ebx
movl %ebx, %ecx
imull %ecx, %ecx
imull %ebx, %ecx
leal (%rcx,%rcx,2), %ecx
cvtsi2sd %ecx, %xmm0
movq 0x10(%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 0x32a85
cmpl $0x0, 0x20(%rsp)
movq 0x58(%rsp), %r13
jle 0x32c78
movq 0x30(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rsp)
decl %eax
movl %eax, 0x40(%rsp)
xorl %edx, %edx
xorpd %xmm0, %xmm0
testl %r13d, %r13d
jle 0x32c6c
xorl %esi, %esi
cmpl $0x0, 0x30(%rsp)
jle 0x32c5c
movq 0x28(%rsp), %rax
movq 0x230(%rax), %rdi
movl 0x248(%rax), %r8d
movq 0x10(%rsp), %rcx
movq 0x230(%rcx), %r9
movl 0x248(%rcx), %r10d
movl 0x244(%rcx), %r11d
imull %esi, %r11d
addl %edx, %r11d
movl 0x244(%rax), %ebx
imull %esi, %ebx
addl %edx, %ebx
movl $0x1, %r15d
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
movq 0x38(%rsp), %rax
movslq %ebx, %r13
movsd (%rdi,%r13,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x32b51
jp 0x32b51
movslq %r11d, %rcx
movsd (%r9,%rcx,8), %xmm1
xorl %r14d, %r14d
xorl %ecx, %ecx
jmp 0x32b7f
movl $0x1, %r14d
testl %r15d, %r15d
jne 0x32b7f
incl %ecx
movl %ecx, %r14d
imull %ecx, %r14d
xorps %xmm2, %xmm2
cvtsi2sd %r14d, %xmm2
movsd %xmm2, (%rdi,%r13,8)
movslq %r11d, %r14
movsd %xmm1, (%r9,%r14,8)
xorl %r14d, %r14d
addl %r10d, %r11d
addl %r8d, %ebx
movl %r14d, %r15d
decq %rax
jne 0x32b30
cmpl $0x0, 0x38(%rsp)
jle 0x32c5c
movq 0x28(%rsp), %rcx
movq 0x230(%rcx), %rdi
movl 0x248(%rcx), %r8d
movq 0x10(%rsp), %rax
movq 0x230(%rax), %r9
movl 0x248(%rax), %r10d
movl 0x244(%rax), %eax
imull %esi, %eax
addl %edx, %eax
movl 0x40(%rsp), %ebx
movl %ebx, %r11d
imull %r10d, %r11d
addl %eax, %r11d
movl 0x244(%rcx), %eax
imull %esi, %eax
addl %edx, %eax
imull %r8d, %ebx
addl %eax, %ebx
movl $0x1, %r15d
xorl %ecx, %ecx
movq 0x8(%rsp), %rax
movslq %ebx, %r13
movsd (%rdi,%r13,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x32c16
jp 0x32c16
movslq %r11d, %rcx
movsd (%r9,%rcx,8), %xmm1
xorl %r14d, %r14d
xorl %ecx, %ecx
jmp 0x32c4a
movl $0x1, %r14d
testl %r15d, %r15d
jne 0x32c4a
incl %ecx
movl %ecx, %r14d
imull %ecx, %r14d
xorps %xmm3, %xmm3
cvtsi2sd %r14d, %xmm3
xorl %r14d, %r14d
ucomisd %xmm3, %xmm2
jbe 0x32c4a
movsd %xmm3, (%rdi,%r13,8)
movslq %r11d, %r15
movsd %xmm1, (%r9,%r15,8)
decq %rax
subl %r10d, %r11d
subl %r8d, %ebx
movl %r14d, %r15d
cmpq $0x1, %rax
jg 0x32bf5
incl %esi
movq 0x58(%rsp), %r13
cmpl %r13d, %esi
jne 0x32ad5
incl %edx
cmpl 0x18(%rsp), %edx
jne 0x32aca
cmpl $0x0, 0x38(%rsp)
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %rax
jle 0x3314b
decl %eax
movl %eax, 0x74(%rsp)
movq $0x0, 0x8(%rsp)
movsd 0x6f38c(%rip), %xmm0    # 0xa2030
testl %r13d, %r13d
jle 0x32edd
movq 0x10(%rsp), %rax
movq 0x230(%rax), %rdx
movl 0x244(%rax), %ecx
movq %rcx, 0x80(%rsp)
movl 0x248(%rax), %edi
imull 0x8(%rsp), %edi
xorl %r8d, %r8d
cmpl $0x0, 0x18(%rsp)
jle 0x32ec7
movq 0x230(%r15), %rax
movl 0x248(%r15), %esi
imull 0x8(%rsp), %esi
movl 0x244(%r15), %ecx
imull %r8d, %ecx
addl %esi, %ecx
xorl %esi, %esi
movq 0x20(%rsp), %r10
leal (%rcx,%rsi), %r9d
movslq %r9d, %r9
movsd (%rax,%r9,8), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
movl %r9d, (%r12,%rsi,4)
leal (%rdi,%rsi), %r9d
movslq %r9d, %r9
movsd (%rdx,%r9,8), %xmm1
movsd %xmm1, (%rbp,%rsi,8)
incq %rsi
cmpq %rsi, %r10
jne 0x32d07
testl %r10d, %r10d
jle 0x32ec7
xorl %eax, %eax
movl $0x1, %ecx
xorl %esi, %esi
movq 0x20(%rsp), %r14
movl (%r12,%rax,4), %r9d
movsd (%rbp,%rax,8), %xmm1
testl %r9d, %r9d
je 0x32d9a
movslq %esi, %r10
cmpq %r10, %rax
jl 0x32d9f
movl %eax, %r11d
subl %esi, %r11d
movl %r11d, %ebx
imull %r11d, %ebx
addl (%r12,%r10,4), %ebx
cmpl %r9d, %ebx
jg 0x32d8d
movsd (%rbp,%r10,8), %xmm1
movl %r10d, %esi
movl %ebx, %r9d
incq %r10
decl %r11d
cmpl %r10d, %ecx
jne 0x32d70
jmp 0x32d9f
movl %eax, %esi
xorl %r9d, %r9d
movq 0x50(%rsp), %r10
movl %r9d, (%r10,%rax,4)
movq 0x48(%rsp), %r9
movsd %xmm1, (%r9,%rax,8)
incq %rax
incl %ecx
cmpq %r14, %rax
jne 0x32d53
testl %r14d, %r14d
jle 0x32ec7
movq 0x10(%rsp), %rax
movq 0x230(%rax), %rcx
movq %rcx, 0x40(%rsp)
movl 0x248(%rax), %ecx
imull 0x8(%rsp), %ecx
movq %rcx, 0x68(%rsp)
movl 0x244(%rax), %r11d
imull %r8d, %r11d
movl 0x74(%rsp), %ecx
movl %ecx, %r9d
movq 0x18(%rsp), %rsi
leaq -0x1(%rsi), %rbx
movq 0x50(%rsp), %rax
movl -0x4(%rax,%rsi,4), %r14d
testl %r14d, %r14d
je 0x32eb5
movq 0x48(%rsp), %rax
movsd (%rax,%rbx,8), %xmm1
movslq %ecx, %r13
cmpq %r13, %rbx
jg 0x32e5c
movl %r9d, %r15d
subl %ecx, %r15d
movl %r13d, %r10d
movl %r15d, %eax
imull %r15d, %eax
addl (%r12,%r10,4), %eax
cmpl %r14d, %eax
jg 0x32e4d
movsd (%rbp,%r10,8), %xmm1
movl %r13d, %ecx
movl %eax, %r14d
leaq -0x1(%r13), %rax
incl %r15d
cmpq %r13, %rsi
movq %rax, %r13
jle 0x32e2d
xorps %xmm2, %xmm2
cvtsi2sd %r14d, %xmm2
movq 0x28(%rsp), %r15
movq 0x230(%r15), %rax
movl 0x248(%r15), %r10d
imull 0x8(%rsp), %r10d
movl 0x244(%r15), %r14d
imull %r8d, %r14d
addl %ebx, %r10d
addl %r14d, %r10d
movslq %r10d, %r10
movsd %xmm2, (%rax,%r10,8)
movq 0x68(%rsp), %rax
addl %ebx, %eax
addl %r11d, %eax
cltq
movq 0x40(%rsp), %r10
movsd %xmm1, (%r10,%rax,8)
movq 0x58(%rsp), %r13
jmp 0x32eb7
movl %ebx, %ecx
decl %r9d
cmpq $0x1, %rsi
movq %rbx, %rsi
jg 0x32dfe
incl %r8d
addq 0x80(%rsp), %rdi
cmpl 0x60(%rsp), %r8d
jne 0x32cd5
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
cmpl 0x30(%rsp), %eax
jne 0x32ca4
cmpl $0x0, 0x38(%rsp)
movq 0x20(%rsp), %r14
jle 0x3314b
leal -0x1(%r13), %eax
movl %eax, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
testl %r14d, %r14d
jle 0x33135
movq 0x10(%rsp), %rax
movq 0x230(%rax), %rdx
movl 0x244(%rax), %esi
movl 0x248(%rax), %eax
imull 0x8(%rsp), %eax
movl %eax, 0x18(%rsp)
xorl %r8d, %r8d
cmpl $0x0, 0x60(%rsp)
jle 0x33125
movq 0x230(%r15), %rax
movl 0x244(%r15), %ecx
movl 0x248(%r15), %edi
imull 0x8(%rsp), %edi
addl %r8d, %edi
movl 0x18(%rsp), %r9d
xorl %r10d, %r10d
movslq %edi, %rdi
cvttsd2si (%rax,%rdi,8), %r11d
movl %r11d, (%r12,%r10,4)
movslq %r9d, %r9
movsd (%rdx,%r9,8), %xmm0
movsd %xmm0, (%rbp,%r10,8)
incq %r10
addl %esi, %r9d
addl %ecx, %edi
cmpq %r10, %r13
jne 0x32f71
testl %r13d, %r13d
jle 0x33125
xorl %eax, %eax
movl $0x1, %ecx
xorl %edi, %edi
movl (%r12,%rax,4), %r9d
movsd (%rbp,%rax,8), %xmm0
testl %r9d, %r9d
je 0x32ff4
movslq %edi, %r10
cmpq %r10, %rax
jl 0x32ff9
movl %eax, %r11d
subl %edi, %r11d
movl %r11d, %ebx
imull %r11d, %ebx
addl (%r12,%r10,4), %ebx
cmpl %r9d, %ebx
jg 0x32fe7
movsd (%rbp,%r10,8), %xmm0
movl %r10d, %edi
movl %ebx, %r9d
incq %r10
decl %r11d
cmpl %r10d, %ecx
jne 0x32fca
jmp 0x32ff9
movl %eax, %edi
xorl %r9d, %r9d
movq 0x50(%rsp), %r10
movl %r9d, (%r10,%rax,4)
movq 0x48(%rsp), %r9
movsd %xmm0, (%r9,%rax,8)
incq %rax
incl %ecx
cmpq %r13, %rax
jne 0x32fad
testl %r13d, %r13d
jle 0x33125
movq 0x10(%rsp), %rax
movq 0x230(%rax), %rcx
movq %rcx, 0x40(%rsp)
movl 0x248(%rax), %r10d
imull 0x8(%rsp), %r10d
movl 0x244(%rax), %eax
movl %eax, 0x68(%rsp)
addl %r8d, %r10d
movl 0x30(%rsp), %edi
movl %edi, %eax
movq 0x60(%rsp), %rcx
leaq -0x1(%rcx), %r9
movq 0x50(%rsp), %r11
movl -0x4(%r11,%rcx,4), %ebx
testl %ebx, %ebx
je 0x33113
movq 0x48(%rsp), %r11
movsd (%r11,%r9,8), %xmm0
movslq %edi, %r14
cmpq %r14, %rcx
jg 0x330b4
movl %eax, %r15d
subl %edi, %r15d
movl %r14d, %r11d
movl %r15d, %r13d
imull %r15d, %r13d
addl (%r12,%r11,4), %r13d
cmpl %ebx, %r13d
jg 0x330a5
movsd (%rbp,%r11,8), %xmm0
movl %r14d, %edi
movl %r13d, %ebx
leaq -0x1(%r14), %r11
incl %r15d
cmpq %r14, %rcx
movq %r11, %r14
jl 0x33085
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
movq 0x28(%rsp), %r15
movq 0x230(%r15), %r11
movl 0x248(%r15), %ebx
imull 0x8(%rsp), %ebx
movl 0x244(%r15), %r14d
imull %r9d, %r14d
addl %r8d, %ebx
addl %r14d, %ebx
movslq %ebx, %rbx
movsd %xmm1, (%r11,%rbx,8)
movl 0x68(%rsp), %r11d
imull %r9d, %r11d
addl %r10d, %r11d
movslq %r11d, %r11
movq 0x40(%rsp), %rbx
movsd %xmm0, (%rbx,%r11,8)
movq 0x20(%rsp), %r14
movq 0x58(%rsp), %r13
jmp 0x33116
movl %r9d, %edi
decl %eax
cmpq $0x1, %rcx
movq %r9, %rcx
jg 0x33056
incl %r8d
incl 0x18(%rsp)
cmpl %r14d, %r8d
jne 0x32f41
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
cmpl 0x38(%rsp), %eax
jne 0x32f14
cmpq $0x0, 0x78(%rsp)
jne 0x33168
movq %r15, %rdi
callq 0x2ee9e
movl $0x250, %esi            # imm = 0x250
movq %r15, %rdi
callq 0xe450
movq %r12, %rdi
callq 0xe5b0
movq 0x50(%rsp), %rdi
callq 0xe5b0
movq %rbp, %rdi
callq 0xe5b0
movq 0x48(%rsp), %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 %r15, %rdi
callq 0xe450
movq %r14, %rdi
callq 0xe7e0
nop