// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::Resample(int, int, int)
void R2Grid::
Resample(int xresolution, int yresolution, RNBoolean nearest_point_sampling)
{
  // Resample grid values at new resolution
  RNScalar *new_grid_values = NULL;
  int new_grid_size = xresolution * yresolution;
  if (new_grid_size > 0) {
    new_grid_values = new RNScalar [ new_grid_size ];
    assert(new_grid_values);
    if (grid_values && (grid_resolution[0] > 0) && (grid_resolution[1] > 0)) {
      RNScalar *new_grid_valuesp = new_grid_values;
      RNScalar xscale = (RNScalar) (grid_resolution[0]-1) / (RNScalar) (xresolution - 1);
      RNScalar yscale = (RNScalar) (grid_resolution[1]-1) / (RNScalar) (yresolution - 1);
      for (int j = 0; j < yresolution; j++) {
        RNScalar y = (j == yresolution-1) ? grid_resolution[1]-1 : j * yscale;
        for (int i = 0; i < xresolution; i++) {
          RNScalar x = (i == xresolution-1) ? grid_resolution[0]-1 : i * xscale;
          if (!nearest_point_sampling) {
            // Sample with bilinear interpolation
            *(new_grid_valuesp++) = GridValue(x, y);
          }
          else {
            // Sample at nearest point 
            int ix = (int) (x + 0.5);
            int iy = (int) (y + 0.5);
            if (ix > grid_resolution[0]-1) ix = grid_resolution[0]-1;
            if (iy > grid_resolution[1]-1) iy = grid_resolution[1]-1;
            *(new_grid_valuesp++) = GridValue(ix, iy);
          }
        }
      }
    }
    else {
      for (int i = 0; i < new_grid_size; i++) {
        new_grid_values[i] = 0;
      }
    }
  }

  // Reset grid variables
  grid_resolution[0] = xresolution;
  grid_resolution[1] = yresolution;
  grid_row_size = xresolution;
  grid_size = grid_row_size * yresolution;
  if (grid_values) delete [] grid_values;
  grid_values = new_grid_values;

  // Reset transformations
  SetWorldToGridTransformation(WorldBox());
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movl %esi, 0xcc(%rsp)
movl %edx, 0xc8(%rsp)
movl %ecx, 0xc4(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0xb8(%rsp)
movl 0xcc(%rsp), %eax
imull 0xc8(%rsp), %eax
movl %eax, 0xb4(%rsp)
cmpl $0x0, 0xb4(%rsp)
jle 0xb0474
movslq 0xb4(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0xb8(%rsp)
cmpq $0x0, 0xb8(%rax)
je 0xb0439
movq 0x28(%rsp), %rax
cmpl $0x0, 0xc0(%rax)
jle 0xb0439
movq 0x28(%rsp), %rax
cmpl $0x0, 0xc4(%rax)
jle 0xb0439
movq 0x28(%rsp), %rax
movq 0xb8(%rsp), %rcx
movq %rcx, 0xa8(%rsp)
movl 0xc0(%rax), %ecx
subl $0x1, %ecx
cvtsi2sd %ecx, %xmm0
movl 0xcc(%rsp), %ecx
subl $0x1, %ecx
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movl 0xc4(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movl 0xc8(%rsp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0xc8(%rsp), %eax
jge 0xb0437
movl 0x94(%rsp), %eax
movl 0xc8(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xb027f
movq 0x28(%rsp), %rax
movl 0xc4(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0xb0297
cvtsi2sdl 0x94(%rsp), %xmm0
mulsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0xcc(%rsp), %eax
jge 0xb041f
movl 0x84(%rsp), %eax
movl 0xcc(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xb02f4
movq 0x28(%rsp), %rax
movl 0xc0(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0xb030c
cvtsi2sdl 0x84(%rsp), %xmm0
mulsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
cmpl $0x0, 0xc4(%rsp)
jne 0xb035b
movq 0x28(%rsp), %rdi
movsd 0x78(%rsp), %xmm0
movsd 0x88(%rsp), %xmm1
callq 0xa83f0
movq 0xa8(%rsp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, 0xa8(%rsp)
movsd %xmm0, (%rax)
jmp 0xb0407
movq 0x28(%rsp), %rcx
movsd 0x82ca8(%rip), %xmm0    # 0x133010
addsd 0x78(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x74(%rsp)
movsd 0x82c92(%rip), %xmm0    # 0x133010
addsd 0x88(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x70(%rsp)
movl 0x74(%rsp), %eax
movl 0xc0(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0xb03b2
movq 0x28(%rsp), %rax
movl 0xc0(%rax), %eax
subl $0x1, %eax
movl %eax, 0x74(%rsp)
movq 0x28(%rsp), %rcx
movl 0x70(%rsp), %eax
movl 0xc4(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0xb03da
movq 0x28(%rsp), %rax
movl 0xc4(%rax), %eax
subl $0x1, %eax
movl %eax, 0x70(%rsp)
movq 0x28(%rsp), %rdi
movl 0x74(%rsp), %esi
movl 0x70(%rsp), %edx
callq 0xb92a0
movq 0xa8(%rsp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, 0xa8(%rsp)
movsd %xmm0, (%rax)
jmp 0xb0409
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0xb02b1
jmp 0xb0421
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0xb023c
jmp 0xb0472
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
cmpl 0xb4(%rsp), %eax
jge 0xb0470
movq 0xb8(%rsp), %rax
movslq 0x6c(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0xb0441
jmp 0xb0472
jmp 0xb0474
movq 0x28(%rsp), %rax
movl 0xcc(%rsp), %ecx
movl %ecx, 0xc0(%rax)
movl 0xc8(%rsp), %ecx
movl %ecx, 0xc4(%rax)
movl 0xcc(%rsp), %ecx
movl %ecx, 0xc8(%rax)
movl 0xc8(%rax), %ecx
imull 0xc8(%rsp), %ecx
movl %ecx, 0xcc(%rax)
cmpq $0x0, 0xb8(%rax)
je 0xb04e1
movq 0x28(%rsp), %rax
movq 0xb8(%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0xb04df
movq 0x10(%rsp), %rdi
callq 0xf670
jmp 0xb04e1
movq 0x28(%rsp), %rsi
movq 0xb8(%rsp), %rax
movq %rax, 0xb8(%rsi)
leaq 0x40(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0xb94f0
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0xa64f0
jmp 0xb0515
leaq 0x40(%rsp), %rdi
callq 0x6bc50
addq $0xd8, %rsp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x6bc50
movq 0x38(%rsp), %rdi
callq 0xf890
nopl (%rax)