// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::R2Grid(gaps::R2Grid const&, int, int, int, int)
R2Grid::
R2Grid(const R2Grid& grid, int x1, int y1, int x2, int y2)
  : grid_values(NULL)
{
  // Determine grid resolution
  grid_resolution[0] = x2 - x1 + 1;
  grid_resolution[1] = y2 - y1 + 1;
  if (grid_resolution[0] < 0) grid_resolution[0] = 0;
  if (grid_resolution[1] < 0) grid_resolution[1] = 0;
  grid_row_size = grid_resolution[0];
  grid_size = grid_row_size * grid_resolution[1];

  // Allocate grid values
  if (grid_size <= 0) grid_values = NULL;
  else grid_values = new RNScalar [ grid_size ];
  assert(!grid_size || grid_values);

  // Copy grid values
  for (int j = 0; j < grid_resolution[1]; j++) {
    int iy = y1 + j;
    for (int i = 0; i < grid_resolution[0]; i++) {
      int ix = x1 + i;
      RNScalar value = 0;
      if ((ix >= 0) && (ix < grid.XResolution())) {
        if ((iy >= 0) && (iy < grid.YResolution())) {
          value = grid.GridValue(ix, iy);
        }
      }
      SetGridValue(i, j, value);
    }
  }

  // Determine transforms
  world_to_grid_transform = grid.world_to_grid_transform;
  world_to_grid_transform.Translate(R2Vector(-x1, -y1));
  world_to_grid_scale_factor = grid.world_to_grid_scale_factor;
  grid_to_world_transform = world_to_grid_transform.Inverse();
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movl %edx, 0xe4(%rsp)
movl %ecx, 0xe0(%rsp)
movl %r8d, 0xdc(%rsp)
movl %r9d, 0xd8(%rsp)
movq 0xf0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
movq %rdi, %rax
movq %rax, 0x30(%rsp)
callq 0xbed00
movq 0x38(%rsp), %rdi
addq $0x58, %rdi
movq %rdi, 0x40(%rsp)
callq 0xbed00
jmp 0xa6ab4
movq 0x30(%rsp), %rax
movq $0x0, 0xb8(%rax)
movl 0xdc(%rsp), %ecx
subl 0xe4(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, 0xc0(%rax)
movl 0xd8(%rsp), %ecx
subl 0xe0(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, 0xc4(%rax)
cmpl $0x0, 0xc0(%rax)
jge 0xa6b25
movq 0x30(%rsp), %rax
movl $0x0, 0xc0(%rax)
jmp 0xa6b25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
jmp 0xa6e29
movq 0x30(%rsp), %rax
cmpl $0x0, 0xc4(%rax)
jge 0xa6b42
movq 0x30(%rsp), %rax
movl $0x0, 0xc4(%rax)
movq 0x30(%rsp), %rax
movl 0xc0(%rax), %ecx
movl %ecx, 0xc8(%rax)
movl 0xc8(%rax), %ecx
imull 0xc4(%rax), %ecx
movl %ecx, 0xcc(%rax)
cmpl $0x0, 0xcc(%rax)
jg 0xa6b81
movq 0x30(%rsp), %rax
movq $0x0, 0xb8(%rax)
jmp 0xa6bdb
movq 0x30(%rsp), %rax
movslq 0xcc(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xf030
movq %rax, 0x28(%rsp)
jmp 0xa6baf
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0xb8(%rax)
jmp 0xa6bdb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
jmp 0xa6e1f
movl $0x0, 0xc8(%rsp)
movq 0x30(%rsp), %rcx
movl 0xc8(%rsp), %eax
cmpl 0xc4(%rcx), %eax
jge 0xa6d4a
movl 0xe0(%rsp), %eax
addl 0xc8(%rsp), %eax
movl %eax, 0xc4(%rsp)
movl $0x0, 0xc0(%rsp)
movq 0x30(%rsp), %rcx
movl 0xc0(%rsp), %eax
cmpl 0xc0(%rcx), %eax
jge 0xa6d32
movl 0xe4(%rsp), %eax
addl 0xc0(%rsp), %eax
movl %eax, 0xbc(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xb0(%rsp)
cmpl $0x0, 0xbc(%rsp)
jl 0xa6cf7
movl 0xbc(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0xe8(%rsp), %rdi
callq 0xb9260
movl %eax, 0x24(%rsp)
jmp 0xa6c83
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
jge 0xa6cf7
cmpl $0x0, 0xc4(%rsp)
jl 0xa6cf5
movl 0xc4(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0xe8(%rsp), %rdi
callq 0xb9280
movl %eax, 0x1c(%rsp)
jmp 0xa6cb7
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %ecx
cmpl %ecx, %eax
jge 0xa6cf5
movq 0xe8(%rsp), %rdi
movl 0xbc(%rsp), %esi
movl 0xc4(%rsp), %edx
callq 0xb92a0
movsd %xmm0, 0x10(%rsp)
jmp 0xa6ce6
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
jmp 0xa6cf7
movq 0x30(%rsp), %rdi
movl 0xc0(%rsp), %esi
movl 0xc8(%rsp), %edx
movsd 0xb0(%rsp), %xmm0
callq 0x6bc00
jmp 0xa6d1a
jmp 0xa6d1c
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0xa6c1e
jmp 0xa6d34
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0xa6be6
movq 0x30(%rsp), %rdi
movq 0xe8(%rsp), %rsi
addq $0x58, %rsi
addq $0x58, %rdi
callq 0xb9200
jmp 0xa6d66
movq 0x30(%rsp), %rax
addq $0x58, %rax
movq %rax, 0x8(%rsp)
movl 0xe4(%rsp), %eax
negl %eax
cvtsi2sd %eax, %xmm0
movl 0xe0(%rsp), %eax
negl %eax
cvtsi2sd %eax, %xmm1
leaq 0xa0(%rsp), %rdi
callq 0xc3c30
jmp 0xa6d9d
movq 0x8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0xb92e0
jmp 0xa6db1
movq 0x30(%rsp), %rsi
movq 0xe8(%rsp), %rax
movsd 0xb0(%rax), %xmm0
movsd %xmm0, 0xb0(%rsi)
addq $0x58, %rsi
leaq 0x48(%rsp), %rdi
callq 0xb9310
jmp 0xa6dde
movq 0x30(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xb9200
jmp 0xa6def
leaq 0x48(%rsp), %rdi
callq 0xb9250
addq $0xf8, %rsp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
leaq 0x48(%rsp), %rdi
callq 0xb9250
movq 0x40(%rsp), %rdi
callq 0xb9250
movq 0x38(%rsp), %rdi
callq 0xb9250
movq 0xd0(%rsp), %rdi
callq 0xf890