// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rsi, %r14
movq %rdi, %r12
movq %rsi, %rbx
testq %rsi, %rsi
jne 0x2ee5f
movl $0x250, %edi            # imm = 0x250
callq 0xe450
movq %rax, %rbx
movl 0x238(%r12), %esi
movl 0x23c(%r12), %edx
movl 0x240(%r12), %ecx
movq %rax, %rdi
callq 0x2b2ec
movq %r14, 0x60(%rsp)
leaq 0x110(%r12), %rsi
movq %rbx, %rdi
callq 0x2f570
movslq 0x238(%r12), %rcx
movslq 0x23c(%r12), %rax
cmpl %eax, %ecx
movq %rax, 0x48(%rsp)
movq %rcx, 0x20(%rsp)
cmovgl %ecx, %eax
movslq 0x240(%r12), %rcx
cmpl %ecx, %eax
movq %rcx, 0x18(%rsp)
cmovlel %ecx, %eax
movslq %eax, %r15
leaq (,%r15,4), %r14
testl %r15d, %r15d
pushq $-0x1
popq %rax
cmovsq %rax, %r14
leaq (,%r15,8), %rcx
cmovsq %rax, %rcx
movq %rcx, %rbp
movq %r14, %rdi
callq 0xe030
movq %r12, %r13
movq %rax, %r12
movq %r14, %rdi
callq 0xe030
movq %rax, 0x38(%rsp)
movq %rbp, %r14
movq %rbp, %rdi
callq 0xe030
movq %rax, %rbp
movq %r14, %rdi
callq 0xe030
incl %r15d
movl %r15d, %ecx
imull %ecx, %ecx
imull %r15d, %ecx
leal (%rcx,%rcx,2), %ecx
cvtsi2sd %ecx, %xmm0
movq %rax, 0x30(%rsp)
movl 0x24c(%r13), %eax
movq %r13, 0x40(%rsp)
movq 0x230(%r13), %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
xorpd %xmm1, %xmm1
cmpq %rdx, %rax
je 0x2ef55
movq 0x230(%rbx), %rsi
movsd (%rcx,%rdx,8), %xmm2
cmpeqsd %xmm1, %xmm2
andpd %xmm0, %xmm2
movlpd %xmm2, (%rsi,%rdx,8)
incq %rdx
jmp 0x2ef31
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
testl %edx, %edx
movl $0x0, %eax
cmovgl %edx, %eax
movq %rax, 0x50(%rsp)
movq 0x48(%rsp), %rax
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
movl %edx, 0x28(%rsp)
movq 0x20(%rsp), %rdx
testl %edx, %edx
movl $0x0, %eax
cmovgl %edx, %eax
movl %eax, 0x14(%rsp)
pushq $0x1
popq %r13
xorpd %xmm0, %xmm0
cmpl 0x14(%rsp), %ecx
je 0x2f13d
xorl %edi, %edi
cmpl 0x28(%rsp), %edi
je 0x2f136
movq 0x40(%rsp), %rax
movq 0x230(%rax), %rdx
movl 0x248(%rax), %r8d
movl 0x244(%rax), %r9d
imull %edi, %r9d
addl %ecx, %r9d
xorl %r10d, %r10d
xorpd %xmm1, %xmm1
movl %r13d, %esi
xorl %eax, %eax
cmpq %r10, 0x50(%rsp)
je 0x2f050
movq 0x230(%rbx), %r11
movl 0x248(%rbx), %r14d
imull %r10d, %r14d
movl 0x244(%rbx), %r15d
imull %edi, %r15d
addl %ecx, %r14d
addl %r15d, %r14d
movslq %r14d, %r14
movsd (%r11,%r14,8), %xmm2
ucomisd %xmm0, %xmm2
movslq %r9d, %r15
jne 0x2f026
jp 0x2f026
movsd (%rdx,%r15,8), %xmm1
xorl %esi, %esi
xorl %eax, %eax
jmp 0x2f048
testl %esi, %esi
je 0x2f02f
movl %r13d, %esi
jmp 0x2f048
incl %eax
movl %eax, %esi
imull %eax, %esi
cvtsi2sd %esi, %xmm2
movsd %xmm2, (%r11,%r14,8)
movsd %xmm1, (%rdx,%r15,8)
xorl %esi, %esi
incq %r10
addl %r8d, %r9d
jmp 0x2efdc
movq 0x40(%rsp), %rax
movq 0x230(%rax), %rdx
movq %rdx, 0x8(%rsp)
movl 0x248(%rax), %esi
movl 0x244(%rax), %r10d
imull %edi, %r10d
addl %ecx, %r10d
xorl %edx, %edx
movq 0x18(%rsp), %r8
xorl %r15d, %r15d
movl %esi, %r9d
imull %r8d, %r9d
addl %r10d, %r9d
leal -0x1(%r8), %eax
imull %esi, %eax
addl %r10d, %eax
movq %r8, %r11
testq %r11, %r11
jle 0x2f12b
leaq -0x1(%r11), %r8
movq 0x230(%rbx), %r14
decl %r11d
imull 0x248(%rbx), %r11d
movl 0x244(%rbx), %r13d
imull %edi, %r13d
addl %ecx, %r11d
addl %r13d, %r11d
movslq %r11d, %r13
movsd (%r14,%r13,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x2f0d7
jnp 0x2f118
subl %esi, %r9d
subl %esi, %eax
movq %r8, %r11
testb $0x1, %dl
movl $0x0, %edx
je 0x2f096
incl %r15d
movl %r15d, %eax
imull %r15d, %eax
cvtsi2sd %eax, %xmm3
movb $0x1, %dl
ucomisd %xmm3, %xmm2
jbe 0x2f07f
movsd %xmm3, (%r14,%r13,8)
movslq %r9d, %rax
movq 0x8(%rsp), %r9
movsd %xmm1, (%r9,%rax,8)
jmp 0x2f07f
cltq
movq 0x8(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm1
movb $0x1, %dl
jmp 0x2f07c
incl %edi
pushq $0x1
popq %r13
jmp 0x2efa5
incl %ecx
jmp 0x2ef99
movq 0x20(%rsp), %rax
decl %eax
movl %eax, 0x5c(%rsp)
movl 0x14(%rsp), %edi
xorl %r8d, %r8d
movsd 0x63eb9(%rip), %xmm0    # 0x93010
cmpl 0x50(%rsp), %r8d
je 0x2f319
xorl %r9d, %r9d
cmpl 0x28(%rsp), %r9d
je 0x2f311
movq 0x40(%rsp), %rax
movq 0x230(%rax), %r10
movl 0x248(%rax), %ecx
imull %r8d, %ecx
movl 0x244(%rax), %eax
imull %r9d, %eax
movq %rcx, 0x8(%rsp)
movq %rax, 0x18(%rsp)
addl %ecx, %eax
xorl %edx, %edx
cmpq %rdx, %rdi
je 0x2f1f0
movq 0x230(%rbx), %rcx
movl 0x248(%rbx), %esi
imull %r8d, %esi
movl 0x244(%rbx), %r11d
imull %r9d, %r11d
addl %esi, %r11d
addl %edx, %r11d
movslq %r11d, %rsi
movsd (%rcx,%rsi,8), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
movl %ecx, (%r12,%rdx,4)
leal (%rax,%rdx), %ecx
movslq %ecx, %rcx
movsd (%r10,%rcx,8), %xmm1
movsd %xmm1, (%rbp,%rdx,8)
incq %rdx
jmp 0x2f19e
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdi, %rdx
je 0x2f255
movl (%r12,%rdx,4), %eax
movsd (%rbp,%rdx,8), %xmm1
testl %eax, %eax
je 0x2f23a
movslq %esi, %r11
movl %edx, %r14d
subl %r11d, %r14d
cmpq %rdx, %r11
jg 0x2f23e
movl %r14d, %r15d
imull %r14d, %r15d
addl (%r12,%r11,4), %r15d
cmpl %eax, %r15d
jg 0x2f232
movsd (%rbp,%r11,8), %xmm1
movl %r11d, %esi
movl %r15d, %eax
incq %r11
decl %r14d
jmp 0x2f210
xorl %eax, %eax
movl %edx, %esi
movq 0x38(%rsp), %rcx
movl %eax, (%rcx,%rdx,4)
movq 0x30(%rsp), %rax
movsd %xmm1, (%rax,%rdx,8)
incq %rdx
jmp 0x2f1f4
movl 0x5c(%rsp), %r15d
movl %r15d, %esi
movq 0x20(%rsp), %rdx
testq %rdx, %rdx
jle 0x2f309
decq %rdx
movq 0x38(%rsp), %rax
movl (%rax,%rdx,4), %eax
testl %eax, %eax
je 0x2f2ff
movq 0x30(%rsp), %rcx
movsd (%rcx,%rdx,8), %xmm1
movslq %r15d, %r11
movl %esi, %r14d
subl %r11d, %r14d
cmpq %r11, %rdx
jg 0x2f2bd
movl %r11d, %ecx
movl %r14d, %r13d
imull %r14d, %r13d
addl (%r12,%rcx,4), %r13d
cmpl %eax, %r13d
jg 0x2f2b5
movsd (%rbp,%rcx,8), %xmm1
movl %r11d, %r15d
movl %r13d, %eax
decq %r11
incl %r14d
jmp 0x2f291
cvtsi2sd %eax, %xmm2
movq 0x230(%rbx), %rax
movl 0x248(%rbx), %ecx
imull %r8d, %ecx
movl 0x244(%rbx), %r11d
imull %r9d, %r11d
addl %edx, %ecx
addl %r11d, %ecx
movslq %ecx, %rcx
movsd %xmm2, (%rax,%rcx,8)
movq 0x8(%rsp), %rax
addl %edx, %eax
addl 0x18(%rsp), %eax
cltq
movsd %xmm1, (%r10,%rax,8)
jmp 0x2f302
movl %edx, %r15d
decl %esi
jmp 0x2f262
incl %r9d
jmp 0x2f165
incl %r8d
jmp 0x2f157
movq 0x48(%rsp), %rax
decl %eax
movl %eax, 0x20(%rsp)
movl 0x28(%rsp), %edx
xorl %r13d, %r13d
movq %rdx, 0x28(%rsp)
cmpl 0x50(%rsp), %r13d
je 0x2f508
xorl %r8d, %r8d
cmpl 0x14(%rsp), %r8d
je 0x2f500
movq 0x40(%rsp), %rax
movq 0x230(%rax), %r10
movl 0x248(%rax), %ecx
imull %r13d, %ecx
movl 0x244(%rax), %eax
movl %eax, 0x8(%rsp)
addl %r8d, %ecx
movl %ecx, 0x18(%rsp)
movl %ecx, %eax
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x2f3be
movq 0x230(%rbx), %rcx
movl 0x248(%rbx), %edi
imull %r13d, %edi
movl 0x244(%rbx), %r9d
imull %esi, %r9d
addl %r8d, %edi
addl %r9d, %edi
movslq %edi, %rdi
cvttsd2si (%rcx,%rdi,8), %ecx
movl %ecx, (%r12,%rsi,4)
cltq
movsd (%r10,%rax,8), %xmm0
movsd %xmm0, (%rbp,%rsi,8)
incq %rsi
addl 0x8(%rsp), %eax
jmp 0x2f374
xorl %esi, %esi
xorl %r11d, %r11d
cmpq %rdx, %rsi
je 0x2f425
movl (%r12,%rsi,4), %eax
movsd (%rbp,%rsi,8), %xmm0
testl %eax, %eax
je 0x2f409
movslq %r11d, %r9
movl %esi, %r14d
subl %r9d, %r14d
cmpq %rsi, %r9
jg 0x2f40e
movl %r14d, %r15d
imull %r14d, %r15d
addl (%r12,%r9,4), %r15d
cmpl %eax, %r15d
jg 0x2f401
movsd (%rbp,%r9,8), %xmm0
movl %r9d, %r11d
movl %r15d, %eax
incq %r9
decl %r14d
jmp 0x2f3df
xorl %eax, %eax
movl %esi, %r11d
movq 0x38(%rsp), %rcx
movl %eax, (%rcx,%rsi,4)
movq 0x30(%rsp), %rax
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
jmp 0x2f3c3
movl 0x20(%rsp), %r15d
movl %r15d, %edi
movq 0x48(%rsp), %rsi
testq %rsi, %rsi
jle 0x2f4f8
leaq -0x1(%rsi), %rax
movq 0x38(%rsp), %rcx
movl (%rcx,%rax,4), %r9d
testl %r9d, %r9d
je 0x2f4eb
movq %r10, %rdx
movl %r8d, %r10d
movq %r13, %r8
movq 0x30(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
movslq %r15d, %r14
movl %edi, %r13d
subl %r14d, %r13d
cmpq %r14, %rsi
jg 0x2f499
movl %r14d, %ecx
movl %r13d, %r11d
imull %r13d, %r11d
addl (%r12,%rcx,4), %r11d
cmpl %r9d, %r11d
jg 0x2f491
movsd (%rbp,%rcx,8), %xmm0
movl %r14d, %r15d
movl %r11d, %r9d
decq %r14
incl %r13d
jmp 0x2f46d
cvtsi2sd %r9d, %xmm1
movq 0x230(%rbx), %rcx
movl 0x248(%rbx), %esi
movq %r8, %r13
imull %r13d, %esi
movl 0x244(%rbx), %r9d
imull %eax, %r9d
movl %r10d, %r8d
addl %r10d, %esi
addl %r9d, %esi
movslq %esi, %rsi
movsd %xmm1, (%rcx,%rsi,8)
movl 0x8(%rsp), %ecx
imull %eax, %ecx
addl 0x18(%rsp), %ecx
movslq %ecx, %rcx
movq %rdx, %r10
movsd %xmm0, (%rdx,%rcx,8)
movq 0x28(%rsp), %rdx
jmp 0x2f4ee
movl %eax, %r15d
decl %edi
movq %rax, %rsi
jmp 0x2f432
incl %r8d
jmp 0x2f33e
incl %r13d
jmp 0x2f330
cmpq $0x0, 0x60(%rsp)
jne 0x2f525
movq %rbx, %rdi
callq 0x2bc02
movl $0x250, %esi            # imm = 0x250
movq %rbx, %rdi
callq 0xe460
movq %r12, %rdi
callq 0xe5d0
movq 0x38(%rsp), %rdi
callq 0xe5d0
movq %rbp, %rdi
callq 0xe5d0
movq 0x30(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5d0
movq %rax, %r14
movl $0x250, %esi            # imm = 0x250
movq %rbx, %rdi
callq 0xe460
movq %r14, %rdi
callq 0xe810
nop