// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movl %esi, %r13d
movq %rdi, %rbx
movl %edx, %eax
imull %esi, %eax
testl %eax, %eax
jle 0x5e9cd
movl %ecx, %ebp
movq %rdx, 0x10(%rsp)
movl %eax, 0x4(%rsp)
movl %eax, %r14d
leaq (,%r14,8), %rdi
callq 0xe030
movq %rax, 0x8(%rsp)
movq 0xb8(%rbx), %rdi
testq %rdi, %rdi
movq %rdi, 0x40(%rsp)
je 0x5e9a7
movl 0xc0(%rbx), %r12d
testl %r12d, %r12d
jle 0x5e9a7
movl 0xc4(%rbx), %eax
movq %rax, 0x38(%rsp)
testl %eax, %eax
jle 0x5e9a7
movq 0x10(%rsp), %rax
testl %eax, %eax
jle 0x5ead4
movq %rbx, 0x18(%rsp)
movq %rax, %rcx
leal -0x1(%r12), %r14d
cvtsi2sd %r14d, %xmm2
leal -0x1(%r13), %eax
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rax
decl %eax
movl %eax, 0x2c(%rsp)
cvtsi2sd %eax, %xmm3
movapd %xmm2, %xmm4
leal -0x1(%rcx), %eax
movl %eax, 0x30(%rsp)
cvtsi2sd %eax, %xmm1
divsd %xmm0, %xmm4
movsd %xmm4, 0x70(%rsp)
movsd %xmm3, 0x50(%rsp)
divsd %xmm1, %xmm3
movsd %xmm3, 0x48(%rsp)
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd 0x437a7(%rip), %xmm1    # 0xa2038
movq 0x8(%rsp), %rbx
movq %r13, 0x58(%rsp)
movsd %xmm2, 0x68(%rsp)
movsd 0x50(%rsp), %xmm3
movsd %xmm3, 0x20(%rsp)
cmpl 0x30(%rsp), %eax
je 0x5e8c3
movsd 0x48(%rsp), %xmm3
mulsd %xmm0, %xmm3
movsd %xmm3, 0x20(%rsp)
movsd %xmm0, 0x60(%rsp)
movl %eax, 0x34(%rsp)
testl %r13d, %r13d
jle 0x5e97c
movsd 0x20(%rsp), %xmm0
addsd 0x4374c(%rip), %xmm0    # 0xa2030
cvttsd2si %xmm0, %r15d
cmpl %r15d, 0x38(%rsp)
cmovlel 0x2c(%rsp), %r15d
movq 0x18(%rsp), %rax
imull 0xc8(%rax), %r15d
xorpd %xmm3, %xmm3
movapd %xmm2, %xmm0
cmpl $0x1, %r13d
je 0x5e919
movsd 0x70(%rsp), %xmm0
mulsd %xmm3, %xmm0
testl %ebp, %ebp
je 0x5e93c
addsd 0x4370b(%rip), %xmm0    # 0xa2030
cvttsd2si %xmm0, %eax
cmpl %eax, %r12d
cmovlel %r14d, %eax
addl %r15d, %eax
cltq
movsd (%rdi,%rax,8), %xmm0
jmp 0x5e96b
movq 0x18(%rsp), %rdi
movsd 0x20(%rsp), %xmm1
movsd %xmm3, 0x78(%rsp)
callq 0x5a27c
movsd 0x78(%rsp), %xmm3
movsd 0x436d8(%rip), %xmm1    # 0xa2038
movsd 0x68(%rsp), %xmm2
movq 0x40(%rsp), %rdi
movsd %xmm0, (%rbx)
addq $0x8, %rbx
addsd %xmm1, %xmm3
decl %r13d
jne 0x5e905
movsd 0x60(%rsp), %xmm0
addsd %xmm1, %xmm0
movl 0x34(%rsp), %eax
incl %eax
cmpl 0x10(%rsp), %eax
movq 0x58(%rsp), %r13
jne 0x5e8a1
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %r15
jmp 0x5e9c2
shlq $0x3, %r14
movq 0x8(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0xe260
movq 0x40(%rsp), %rdi
movl 0x4(%rsp), %eax
movq 0x10(%rsp), %rdx
jmp 0x5e9d7
movq 0xb8(%rbx), %rdi
xorl %r15d, %r15d
movl %r13d, 0xc0(%rbx)
movl %edx, 0xc4(%rbx)
movl %r13d, 0xc8(%rbx)
movl %eax, 0xcc(%rbx)
testq %rdi, %rdi
je 0x5e9fb
callq 0xe5b0
movq %r15, 0xb8(%rbx)
leaq 0xd8(%rsp), %r14
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movq %r14, %rdi
callq 0x680a6
movl 0xc0(%rbx), %eax
decl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movl 0xc4(%rbx), %eax
decl %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
leaq 0xc8(%rsp), %r15
movq %r15, %rdi
callq 0x680a6
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
callq 0x680a6
movq %r14, %rdi
movq %rbx, %rsi
callq 0x681ba
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
callq 0x680a6
movq %r15, %rdi
movq %rbx, %rsi
callq 0x681ba
leaq 0x80(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x652ca
movq %rbx, %rdi
movq %r12, %rsi
callq 0x59092
leaq 0x80(%rsp), %rdi
callq 0x65c14
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r13d, 0xc0(%rbx)
movl %eax, 0xc4(%rbx)
movl %r13d, 0xc8(%rbx)
movl 0x4(%rsp), %eax
movl %eax, 0xcc(%rbx)
movq 0x8(%rsp), %r15
jmp 0x5e9f6
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x65c14
movq %rbx, %rdi
callq 0xe7e0