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

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

  // Allocate temporary buffers
  res = XResolution();
  if (res < YResolution()) res = YResolution();
  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);
  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 x axis
  for (y = 0; y < YResolution(); y++) {
    first = 1;
    value = 0;
    dist = 0;
    for (x = 0; x < XResolution(); x++) {
      if (dgrid->GridValue(x,y) == 0.0) {
        first=0;
        dist=0;
        value = GridValue(x,y);
        assert(value != 0);
      }
      else if (first == 0) {
        dist++;
        square = dist*dist;
        dgrid->SetGridValue(x, y, square);
        SetGridValue(x, y, value);
        assert(value != 0);
      }
    }
			
    // backward scan
    dist = 0;
    first = 1;
    for (x = XResolution()-1; x >= 0; x--) {
      if (dgrid->GridValue(x,y) == 0.0){
        dist = 0;
        first = 0;
        value = GridValue(x,y);
        assert(value != 0);
      }
      else if (first == 0) {
        dist++;
        square = dist*dist;
        if (square < dgrid->GridValue(x, y)) {
          dgrid->SetGridValue(x, y, square);
          SetGridValue(x, y, value);
          assert(value != 0);
        }
      }
    }
  }

  // Scan along y axis
  for (x = 0; x < XResolution(); x++) {
    // Copy grid values
    for (y = 0; y < YResolution(); y++) {
      old_dist[y] = (int) (dgrid->GridValue(x, y));
      old_value[y] = GridValue(x, y);
    }
			
    // 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, dist);
        SetGridValue(x, y, 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, %rbx
movq %rdi, %r12
movq %rsi, %r13
testq %rsi, %rsi
jne 0x5ec29
movl $0xd0, %edi
callq 0xe440
movq %rax, %r13
movl 0xc0(%r12), %esi
movl 0xc4(%r12), %edx
movq %rax, %rdi
callq 0x59bf4
movq %rbx, 0x48(%rsp)
leaq 0x58(%r12), %rsi
movq %r13, %rdi
callq 0x5a060
movslq 0xc0(%r12), %rcx
movl %ecx, %eax
movq %rax, 0x10(%rsp)
movslq 0xc4(%r12), %rbx
movl %ebx, %eax
movq %rax, 0x40(%rsp)
cmpl %ebx, %ecx
movq %rbx, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
cmovgl %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, 0x8(%rsp)
movq %rax, %r12
movq %r14, %rdi
callq 0xe030
movq %rax, %r14
movq %r15, %rdi
callq 0xe030
movq %rax, %rbp
movq %r15, %rdi
callq 0xe030
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rax
movslq 0xcc(%rax), %rax
testq %rax, %rax
jle 0x5ed10
incl %ebx
imull %ebx, %ebx
leal (%rbx,%rbx,2), %ecx
cvtsi2sd %ecx, %xmm0
movq 0x8(%rsp), %rcx
movq 0xb8(%rcx), %rcx
movq 0xb8(%r13), %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 0x5ecf5
movq 0x40(%rsp), %rbx
testl %ebx, %ebx
movq 0x28(%rsp), %r15
jle 0x5ee7e
leaq 0x1(%r15), %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movslq %ecx, %rdx
xorpd %xmm1, %xmm1
cmpl $0x0, 0x10(%rsp)
jle 0x5edd2
movslq 0xc8(%r13), %rsi
movq 0x8(%rsp), %r8
movl 0xc8(%r8), %edi
imull %edx, %edi
movslq %edi, %rdi
shlq $0x3, %rdi
addq 0xb8(%r8), %rdi
imulq %rdx, %rsi
shlq $0x3, %rsi
addq 0xb8(%r13), %rsi
movl $0x1, %r11d
xorl %r8d, %r8d
xorl %r9d, %r9d
movsd (%rsi,%r8,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5ed99
jp 0x5ed99
movsd (%rdi,%r8,8), %xmm1
xorl %r10d, %r10d
xorl %r9d, %r9d
jmp 0x5edc5
movl $0x1, %r10d
testl %r11d, %r11d
jne 0x5edc5
incl %r9d
movl %r9d, %r10d
imull %r9d, %r10d
xorps %xmm2, %xmm2
cvtsi2sd %r10d, %xmm2
movsd %xmm2, (%rsi,%r8,8)
movsd %xmm1, (%rdi,%r8,8)
xorl %r10d, %r10d
incq %r8
movl %r10d, %r11d
cmpq %r8, 0x10(%rsp)
jne 0x5ed7d
testl %r15d, %r15d
jle 0x5ee74
movslq 0xc8(%r13), %rsi
movq 0x8(%rsp), %r8
movl 0xc8(%r8), %edi
imull %edx, %edi
movslq %edi, %rdi
shlq $0x3, %rdi
addq 0xb8(%r8), %rdi
imulq %rdx, %rsi
shlq $0x3, %rsi
addq 0xb8(%r13), %rsi
movl $0x1, %r10d
xorl %r8d, %r8d
movq %rax, %rdx
movsd -0x10(%rsi,%rdx,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5ee36
jp 0x5ee36
movsd -0x10(%rdi,%rdx,8), %xmm1
xorl %r9d, %r9d
xorl %r8d, %r8d
jmp 0x5ee68
movl $0x1, %r9d
testl %r10d, %r10d
jne 0x5ee68
incl %r8d
movl %r8d, %r9d
imull %r8d, %r9d
xorps %xmm3, %xmm3
cvtsi2sd %r9d, %xmm3
xorl %r9d, %r9d
ucomisd %xmm3, %xmm2
jbe 0x5ee68
movsd %xmm3, -0x10(%rsi,%rdx,8)
movsd %xmm1, -0x10(%rdi,%rdx,8)
decq %rdx
movl %r9d, %r10d
cmpq $0x1, %rdx
jg 0x5ee1a
incl %ecx
cmpl %ebx, %ecx
jne 0x5ed2c
cmpl $0x0, 0x10(%rsp)
jle 0x5f08f
movq 0x8(%rsp), %rax
movq 0xb8(%rax), %r8
movslq 0xc8(%rax), %r9
movq 0x18(%rsp), %rax
decl %eax
movl %eax, 0x3c(%rsp)
shlq $0x3, %r9
xorl %r10d, %r10d
movq $0x0, 0x20(%rsp)
movq %r9, 0x50(%rsp)
testl %ebx, %ebx
jle 0x5eefa
movslq 0xc8(%r13), %rax
movq 0xb8(%r13), %rdx
addq %r10, %rdx
shlq $0x3, %rax
movq %r8, %rsi
xorl %edi, %edi
cvttsd2si (%rdx), %ecx
movl %ecx, (%r12,%rdi,4)
movsd (%rsi), %xmm0
movsd %xmm0, (%rbp,%rdi,8)
incq %rdi
addq %rax, %rdx
addq %r9, %rsi
cmpq %rdi, %rbx
jne 0x5eeda
movq %r10, 0x58(%rsp)
movq %r8, 0x60(%rsp)
testl %ebx, %ebx
movq 0x30(%rsp), %rcx
jle 0x5ef72
xorl %eax, %eax
movl $0x1, %edx
xorl %esi, %esi
movl (%r12,%rax,4), %r8d
movsd (%rbp,%rax,8), %xmm0
testl %r8d, %r8d
je 0x5ef5a
movslq %esi, %rdi
cmpq %rdi, %rax
jl 0x5ef5f
movl %eax, %r9d
subl %esi, %r9d
movl %r9d, %r10d
imull %r9d, %r10d
addl (%r12,%rdi,4), %r10d
cmpl %r8d, %r10d
jg 0x5ef4e
movsd (%rbp,%rdi,8), %xmm0
movl %edi, %esi
movl %r10d, %r8d
incq %rdi
decl %r9d
cmpl %edi, %edx
jne 0x5ef33
jmp 0x5ef5f
movl %eax, %esi
xorl %r8d, %r8d
movl %r8d, (%r14,%rax,4)
movsd %xmm0, (%rcx,%rax,8)
incq %rax
incl %edx
cmpq %rbx, %rax
jne 0x5ef16
cmpl $0x0, 0x18(%rsp)
jle 0x5f060
movq 0x8(%rsp), %rax
movslq 0xc8(%rax), %rcx
movq %rcx, 0x28(%rsp)
movq 0x20(%rsp), %rcx
leaq (,%rcx,8), %r9
movq 0xb8(%rax), %r10
addq %r9, %r10
movl 0x3c(%rsp), %eax
movl %eax, %ecx
movq 0x18(%rsp), %rdx
leaq -0x1(%rdx), %rsi
movl -0x4(%r14,%rdx,4), %edi
testl %edi, %edi
je 0x5f04f
movq %r14, %r8
movq %r13, %r15
movq 0x30(%rsp), %r11
movsd (%r11,%rsi,8), %xmm0
movslq %eax, %rbx
cmpq %rbx, %rdx
jg 0x5f00e
movl %ecx, %r14d
subl %eax, %r14d
movl %ebx, %r13d
movl %r14d, %r11d
imull %r14d, %r11d
addl (%r12,%r13,4), %r11d
cmpl %edi, %r11d
jg 0x5efff
movsd (%rbp,%r13,8), %xmm0
movl %ebx, %eax
movl %r11d, %edi
leaq -0x1(%rbx), %r11
incl %r14d
cmpq %rbx, %rdx
movq %r11, %rbx
jl 0x5efe0
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
movq %r15, %r13
movslq 0xc8(%r15), %rdi
movslq %esi, %r11
imulq %rdi, %r11
movq 0xb8(%r15), %rdi
addq %r9, %rdi
movsd %xmm1, (%rdi,%r11,8)
movq %rsi, %rdi
imulq 0x28(%rsp), %rdi
movsd %xmm0, (%r10,%rdi,8)
movq 0x40(%rsp), %rbx
movq %r8, %r14
jmp 0x5f051
movl %esi, %eax
decl %ecx
cmpq $0x1, %rdx
movq %rsi, %rdx
jg 0x5efb0
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x58(%rsp), %r10
addq $0x8, %r10
movq 0x60(%rsp), %r8
addq $0x8, %r8
movq %rcx, 0x20(%rsp)
cmpq 0x10(%rsp), %rcx
movq 0x50(%rsp), %r9
jne 0x5eebc
cmpq $0x0, 0x48(%rsp)
jne 0x5f0ac
movq %r13, %rdi
callq 0x5a814
movl $0xd0, %esi
movq %r13, %rdi
callq 0xe450
movq %r12, %rdi
callq 0xe5b0
movq %r14, %rdi
callq 0xe5b0
movq %rbp, %rdi
callq 0xe5b0
movq 0x30(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0
movq %rax, %r14
movl $0xd0, %esi
movq %r13, %rdi
callq 0xe450
movq %r14, %rdi
callq 0xe7e0