// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rdi, %r15
movq %rsi, %rbx
testq %rsi, %rsi
jne 0x5de55
movl $0xd0, %edi
callq 0xe440
movq %rax, %rbx
movl 0xc0(%r15), %esi
movl 0xc4(%r15), %edx
movq %rax, %rdi
callq 0x58ed8
movq %r14, 0x60(%rsp)
movq %rbx, 0x58(%rsp)
leaq 0x58(%r15), %rsi
movq %rbx, %rdi
callq 0x59362
movslq 0xc0(%r15), %rcx
movl %ecx, %eax
movq %rax, 0x50(%rsp)
movslq 0xc4(%r15), %r13
movl %r13d, %ebx
cmpl %r13d, %ecx
movq %r13, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
cmovgl %ecx, %r13d
movslq %r13d, %rax
leaq (,%rax,4), %r14
testl %eax, %eax
movq $-0x1, %rcx
cmovsq %rcx, %r14
leaq (,%rax,8), %rbp
cmovsq %rcx, %rbp
movq %rbp, 0x8(%rsp)
movq %r14, %rdi
callq 0xe030
movq %rax, %r12
movq %r14, %rdi
movq 0x58(%rsp), %r14
callq 0xe030
movq %rax, 0x18(%rsp)
movq %rbp, %rdi
callq 0xe030
movq %rax, %rbp
movq 0x8(%rsp), %rdi
callq 0xe030
movq %rax, 0x8(%rsp)
movslq 0xcc(%r15), %rax
testq %rax, %rax
jle 0x5df3f
incl %r13d
imull %r13d, %r13d
leal (%r13,%r13,2), %ecx
cvtsi2sd %ecx, %xmm0
movq 0xb8(%r15), %rcx
movq 0xb8(%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 0x5df24
testl %ebx, %ebx
movq 0x50(%rsp), %r13
movq 0x18(%rsp), %r11
jle 0x5e0aa
movq 0x10(%rsp), %rax
incq %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
cmpl $0x0, 0x10(%rsp)
jle 0x5e0a0
movslq 0xc8(%r14), %rsi
movl 0xc8(%r15), %edx
imull %ecx, %edx
movslq %edx, %rdi
shlq $0x3, %rdi
addq 0xb8(%r15), %rdi
movslq %ecx, %rdx
imulq %rdx, %rsi
shlq $0x3, %rsi
addq 0xb8(%r14), %rsi
movl $0x1, %r11d
xorpd %xmm1, %xmm1
xorl %r8d, %r8d
xorl %r9d, %r9d
movsd (%rsi,%r8,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5dfc7
jp 0x5dfc7
movsd (%rdi,%r8,8), %xmm1
xorl %r10d, %r10d
xorl %r9d, %r9d
jmp 0x5dff3
movl $0x1, %r10d
testl %r11d, %r11d
jne 0x5dff3
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, %r13
jne 0x5dfab
testl %r13d, %r13d
movq 0x18(%rsp), %r11
jle 0x5e0a0
movslq 0xc8(%r14), %rsi
movl 0xc8(%r15), %edi
imull %edx, %edi
movslq %edi, %rdi
shlq $0x3, %rdi
addq 0xb8(%r15), %rdi
imulq %rdx, %rsi
shlq $0x3, %rsi
addq 0xb8(%r14), %rsi
movl $0x1, %r10d
xorl %r8d, %r8d
movq %rax, %rdx
movsd -0x10(%rsi,%rdx,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5e062
jp 0x5e062
movsd -0x10(%rdi,%rdx,8), %xmm1
xorl %r9d, %r9d
xorl %r8d, %r8d
jmp 0x5e094
movl $0x1, %r9d
testl %r10d, %r10d
jne 0x5e094
incl %r8d
movl %r8d, %r9d
imull %r8d, %r9d
xorps %xmm3, %xmm3
cvtsi2sd %r9d, %xmm3
xorl %r9d, %r9d
ucomisd %xmm3, %xmm2
jbe 0x5e094
movsd %xmm3, -0x10(%rsi,%rdx,8)
movsd %xmm1, -0x10(%rdi,%rdx,8)
decq %rdx
movl %r9d, %r10d
cmpq $0x1, %rdx
jg 0x5e046
incl %ecx
cmpl %ebx, %ecx
jne 0x5df5f
movq %r15, 0x38(%rsp)
testl %r13d, %r13d
jle 0x5e2bd
movq 0x38(%rsp), %rax
movq 0xb8(%rax), %rcx
movq %rcx, 0x48(%rsp)
movslq 0xc8(%rax), %rcx
movq 0x20(%rsp), %rax
decl %eax
movl %eax, 0x34(%rsp)
shlq $0x3, %rcx
movq $0x0, 0x40(%rsp)
movq $0x0, 0x28(%rsp)
cmpl $0x0, 0x20(%rsp)
jle 0x5e29b
movslq 0xc8(%r14), %rax
movq 0xb8(%r14), %rdx
addq 0x40(%rsp), %rdx
shlq $0x3, %rax
movq 0x48(%rsp), %rsi
xorl %edi, %edi
cvttsd2si (%rdx), %r8d
movl %r8d, (%r12,%rdi,4)
movsd (%rsi), %xmm0
movsd %xmm0, (%rbp,%rdi,8)
incq %rdi
addq %rax, %rdx
addq %rcx, %rsi
cmpq %rdi, %rbx
jne 0x5e11a
testl %ebx, %ebx
jle 0x5e29b
xorl %eax, %eax
movl $0x1, %edx
xorl %esi, %esi
movq 0x8(%rsp), %r15
movl (%r12,%rax,4), %edi
movsd (%rbp,%rax,8), %xmm0
testl %edi, %edi
je 0x5e197
movslq %esi, %r8
cmpq %r8, %rax
jl 0x5e19b
movl %eax, %r9d
subl %esi, %r9d
movl %r9d, %r10d
imull %r9d, %r10d
addl (%r12,%r8,4), %r10d
cmpl %edi, %r10d
jg 0x5e18a
movsd (%rbp,%r8,8), %xmm0
movl %r8d, %esi
movl %r10d, %edi
incq %r8
decl %r9d
cmpl %r8d, %edx
jne 0x5e16d
jmp 0x5e19b
movl %eax, %esi
xorl %edi, %edi
movl %edi, (%r11,%rax,4)
movsd %xmm0, (%r15,%rax,8)
incq %rax
incl %edx
cmpq %rbx, %rax
jne 0x5e151
testl %ebx, %ebx
jle 0x5e29b
movq 0x38(%rsp), %rax
movslq 0xc8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq 0x28(%rsp), %rdx
leaq (,%rdx,8), %r9
movq 0xb8(%rax), %r10
addq %r9, %r10
movl 0x34(%rsp), %eax
movl %eax, %r15d
movq 0x20(%rsp), %rdx
leaq -0x1(%rdx), %rsi
movl -0x4(%r11,%rdx,4), %edi
testl %edi, %edi
je 0x5e289
movq 0x8(%rsp), %r8
movsd (%r8,%rsi,8), %xmm0
movslq %eax, %r14
cmpq %r14, %rdx
jg 0x5e244
movl %r15d, %r13d
subl %eax, %r13d
movl %r14d, %r8d
movl %r13d, %r11d
imull %r13d, %r11d
addl (%r12,%r8,4), %r11d
cmpl %edi, %r11d
jg 0x5e235
movsd (%rbp,%r8,8), %xmm0
movl %r14d, %eax
movl %r11d, %edi
leaq -0x1(%r14), %r8
incl %r13d
cmpq %r14, %rdx
movq %r8, %r14
jl 0x5e215
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
movq 0x58(%rsp), %r14
movslq 0xc8(%r14), %rdi
movslq %esi, %r8
imulq %rdi, %r8
movq 0xb8(%r14), %rdi
addq %r9, %rdi
movsd %xmm1, (%rdi,%r8,8)
movq %rsi, %rdi
imulq 0x10(%rsp), %rdi
movsd %xmm0, (%r10,%rdi,8)
movq 0x50(%rsp), %r13
movq 0x18(%rsp), %r11
jmp 0x5e28b
movl %esi, %eax
decl %r15d
cmpq $0x1, %rdx
movq %rsi, %rdx
jg 0x5e1eb
movq 0x28(%rsp), %rdx
incq %rdx
addq $0x8, 0x40(%rsp)
addq $0x8, 0x48(%rsp)
movq %rdx, 0x28(%rsp)
cmpq %r13, %rdx
jne 0x5e0f1
cmpq $0x0, 0x60(%rsp)
jne 0x5e2da
movq %r14, %rdi
callq 0x59b0c
movl $0xd0, %esi
movq %r14, %rdi
callq 0xe450
movq %r12, %rdi
callq 0xe5b0
movq 0x18(%rsp), %rdi
callq 0xe5b0
movq %rbp, %rdi
callq 0xe5b0
movq 0x8(%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 %rbx, %rdi
callq 0xe450
movq %r14, %rdi
callq 0xe7e0