// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rsi, %r14
movq %rdi, %r13
movq %rsi, %rbx
testq %rsi, %rsi
movq %rdi, 0x20(%rsp)
jne 0x55e6b
movl $0xd0, %edi
callq 0xe450
movq %rax, %rbx
movl 0xc0(%r13), %esi
movl 0xc4(%r13), %edx
movq %rax, %rdi
callq 0x51628
movq 0x20(%rsp), %r13
movq %r14, 0x30(%rsp)
leaq 0x58(%r13), %rsi
movq %rbx, %rdi
callq 0x51a6a
movslq 0xc0(%r13), %rcx
movslq 0xc4(%r13), %rax
cmpl %eax, %ecx
movq %rax, 0x8(%rsp)
movq %rcx, (%rsp)
cmovgl %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 %rax, %r12
movq %r14, %rdi
callq 0xe030
movq %rax, 0x10(%rsp)
movq %rbp, %r14
movq %rbp, %rdi
callq 0xe030
movq %rax, %rbp
movq %r14, %rdi
callq 0xe030
incl %r15d
imull %r15d, %r15d
leal (%r15,%r15,2), %ecx
cvtsi2sd %ecx, %xmm0
movq %rax, %r14
movl 0xcc(%r13), %eax
movq 0xb8(%r13), %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
xorpd %xmm1, %xmm1
cmpq %rdx, %rax
je 0x55f39
movq 0xb8(%rbx), %rsi
movsd (%rcx,%rdx,8), %xmm2
cmpeqsd %xmm1, %xmm2
andpd %xmm0, %xmm2
movlpd %xmm2, (%rsi,%rdx,8)
incq %rdx
jmp 0x55f15
xorl %ecx, %ecx
movq (%rsp), %rax
testl %eax, %eax
movl $0x0, %r15d
cmovgl %eax, %r15d
movq 0x8(%rsp), %rax
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
pushq $0x1
popq %rsi
xorpd %xmm0, %xmm0
cmpl %edx, %ecx
je 0x56084
movl 0xc8(%r13), %eax
imull %ecx, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
addq 0xb8(%r13), %rdi
xorl %r8d, %r8d
xorpd %xmm1, %xmm1
movl %esi, %eax
xorl %r9d, %r9d
cmpq %r8, %r15
je 0x55fef
movslq 0xc8(%rbx), %r11
movslq %ecx, %r10
imulq %r11, %r10
shlq $0x3, %r10
addq 0xb8(%rbx), %r10
movsd (%r10,%r8,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x55fc6
jp 0x55fc6
movsd (%rdi,%r8,8), %xmm1
xorl %eax, %eax
xorl %r9d, %r9d
jmp 0x55fea
testl %eax, %eax
je 0x55fce
movl %esi, %eax
jmp 0x55fea
incl %r9d
movl %r9d, %eax
imull %r9d, %eax
cvtsi2sd %eax, %xmm2
movsd %xmm2, (%r10,%r8,8)
movsd %xmm1, (%rdi,%r8,8)
xorl %eax, %eax
incq %r8
jmp 0x55f8d
movl 0xc8(%r13), %eax
imull %ecx, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
addq 0xb8(%r13), %rdi
xorl %r9d, %r9d
movq (%rsp), %r8
xorl %r10d, %r10d
testq %r8, %r8
jle 0x5607d
decq %r8
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %r11d
imull %ecx, %r11d
movslq %r11d, %r11
leaq (%rax,%r11,8), %rax
movsd (%rax,%r8,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x56040
jnp 0x56072
testb $0x1, %r9b
movl $0x0, %r9d
je 0x56011
incl %r10d
movl %r10d, %r9d
imull %r10d, %r9d
cvtsi2sd %r9d, %xmm3
movb $0x1, %r9b
ucomisd %xmm3, %xmm2
jbe 0x56011
movsd %xmm3, (%rax,%r8,8)
movsd %xmm1, (%rdi,%r8,8)
jmp 0x56011
movsd (%rdi,%r8,8), %xmm1
movb $0x1, %r9b
jmp 0x5600e
incl %ecx
jmp 0x55f61
movq 0x8(%rsp), %rax
decl %eax
movl %eax, 0x1c(%rsp)
movl %edx, %edx
xorl %eax, %eax
xorl %edi, %edi
movq %r14, 0x28(%rsp)
movq %r15, 0x38(%rsp)
cmpq %r15, %rdi
je 0x5623d
movq 0xb8(%r13), %r9
movslq 0xc8(%r13), %rcx
movq %rax, 0x40(%rsp)
addq %r9, %rax
movq %rcx, (%rsp)
leaq (,%rcx,8), %rcx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x5610a
movslq 0xc8(%rbx), %r8
movslq %esi, %r10
imulq %r8, %r10
leaq (,%rdi,8), %r8
addq 0xb8(%rbx), %r8
cvttsd2si (%r8,%r10,8), %r8d
movl %r8d, (%r12,%rsi,4)
movsd (%rax), %xmm0
movsd %xmm0, (%rbp,%rsi,8)
incq %rsi
addq %rcx, %rax
jmp 0x560cc
xorl %r10d, %r10d
xorl %ecx, %ecx
cmpq %rdx, %r10
je 0x5616d
movl (%r12,%r10,4), %r11d
movsd (%rbp,%r10,8), %xmm0
testl %r11d, %r11d
je 0x56153
movslq %ecx, %rax
movl %r10d, %esi
subl %eax, %esi
cmpq %r10, %rax
jg 0x56159
movl %esi, %r8d
imull %esi, %r8d
addl (%r12,%rax,4), %r8d
cmpl %r11d, %r8d
jg 0x5614c
movsd (%rbp,%rax,8), %xmm0
movl %eax, %ecx
movl %r8d, %r11d
incq %rax
decl %esi
jmp 0x5612c
xorl %r11d, %r11d
movl %r10d, %ecx
movq 0x10(%rsp), %rax
movl %r11d, (%rax,%r10,4)
movsd %xmm0, (%r14,%r10,8)
incq %r10
jmp 0x5610f
leaq (%r9,%rdi,8), %r9
movl 0x1c(%rsp), %r11d
movl %r11d, %r14d
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
jle 0x5621d
leaq -0x1(%rcx), %r15
movq 0x10(%rsp), %rax
movl (%rax,%r15,4), %esi
testl %esi, %esi
je 0x5620f
movq 0x28(%rsp), %rax
movsd (%rax,%r15,8), %xmm0
movslq %r11d, %rax
movl %r14d, %r8d
subl %eax, %r8d
cmpq %rax, %rcx
jg 0x561d9
movl %eax, %r13d
movl %r8d, %r10d
imull %r8d, %r10d
addl (%r12,%r13,4), %r10d
cmpl %esi, %r10d
jg 0x561d1
movsd (%rbp,%r13,8), %xmm0
movl %eax, %r11d
movl %r10d, %esi
decq %rax
incl %r8d
jmp 0x561ac
cvtsi2sd %esi, %xmm1
movslq 0xc8(%rbx), %rax
movslq %r15d, %rcx
imulq %rax, %rcx
leaq (,%rdi,8), %rax
addq 0xb8(%rbx), %rax
movsd %xmm1, (%rax,%rcx,8)
movq %r15, %rax
imulq (%rsp), %rax
movsd %xmm0, (%r9,%rax,8)
jmp 0x56212
movl %r15d, %r11d
decl %r14d
movq %r15, %rcx
jmp 0x5617e
incq %rdi
movq 0x40(%rsp), %rax
addq $0x8, %rax
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %r14
movq 0x38(%rsp), %r15
jmp 0x5609f
cmpq $0x0, 0x30(%rsp)
jne 0x5625a
movq %rbx, %rdi
callq 0x5216c
movl $0xd0, %esi
movq %rbx, %rdi
callq 0xe460
movq %r12, %rdi
callq 0xe5d0
movq 0x10(%rsp), %rdi
callq 0xe5d0
movq %rbp, %rdi
callq 0xe5d0
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5d0
movq %rax, %r14
movl $0xd0, %esi
movq %rbx, %rdi
callq 0xe460
movq %r14, %rdi
callq 0xe810