// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::Slice(int, int) const
R2Grid *R3Grid::
Slice(int dim, int grid_coordinate) const
{
  // Extract 2D grid along slice at given coordinate in given dimension

  // Compute parameters
  int dim1 = (dim+1)%3;
  int dim2 = (dim+2)%3;
  R3Box world_box = WorldBox();
  R2Box slice_box(world_box[0][dim1], world_box[0][dim2], world_box[1][dim1], world_box[1][dim2]);

  // Allocate 2D grid for slice
  R2Grid *slice = new R2Grid(grid_resolution[dim1], grid_resolution[dim2], slice_box);
  if (!slice) {
    RNFail("Unable to allocate slice\n");
    return NULL;
  }

  // Fill values
  for (int i = 0; i < grid_resolution[dim1]; i++) {
    for (int j = 0; j < grid_resolution[dim2]; j++) {
      RNScalar value = 0.0;
      if (dim == RN_X) value = GridValue(grid_coordinate, i, j);
      else if (dim == RN_Y) value = GridValue(j, grid_coordinate, i);
      else value = GridValue(i, j, grid_coordinate);
      slice->SetGridValue(i, j, value);
    }
  }

  // Return slice
  return slice;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x128(%rsp)
movl %esi, 0x124(%rsp)
movl %edx, 0x120(%rsp)
movq 0x128(%rsp), %rsi
movq %rsi, 0x70(%rsp)
movl 0x124(%rsp), %edx
movl %edx, %ecx
incl %ecx
movl %edx, %eax
movslq %ecx, %rcx
imulq $0x55555556, %rcx, %rdx # imm = 0x55555556
movq %rdx, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rdx
addl %ecx, %edx
movl %edx, %ecx
leal (%rcx,%rcx,2), %edx
negl %edx
movl %edx, %ecx
leal 0x1(%rax,%rcx), %eax
movl %eax, 0x11c(%rsp)
movl 0x124(%rsp), %edx
movl %edx, %ecx
addl $0x2, %ecx
movl %edx, %eax
movslq %ecx, %rcx
imulq $0x55555556, %rcx, %rdx # imm = 0x55555556
movq %rdx, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rdx
addl %ecx, %edx
movl %edx, %ecx
leal (%rcx,%rcx,2), %edx
negl %edx
movl %edx, %ecx
leal 0x2(%rax,%rcx), %eax
movl %eax, 0x118(%rsp)
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x6bb40
movq 0x78(%rsp), %rdi
xorl %esi, %esi
callq 0x44f40
movq %rax, 0x80(%rsp)
jmp 0x584a3
movq 0x80(%rsp), %rdi
movl 0x11c(%rsp), %esi
callq 0x36be0
movq %rax, 0x68(%rsp)
jmp 0x584be
movq 0x68(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
leaq 0xe0(%rsp), %rdi
xorl %esi, %esi
callq 0x44f40
movq %rax, 0x60(%rsp)
jmp 0x584e3
movq 0x60(%rsp), %rdi
movl 0x118(%rsp), %esi
callq 0x36be0
movq %rax, 0x50(%rsp)
jmp 0x584fb
movq 0x50(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0xe0(%rsp), %rdi
movl $0x1, %esi
callq 0x44f40
movq %rax, 0x48(%rsp)
jmp 0x58523
movq 0x48(%rsp), %rdi
movl 0x11c(%rsp), %esi
callq 0x36be0
movq %rax, 0x38(%rsp)
jmp 0x5853b
movq 0x38(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0xe0(%rsp), %rdi
movl $0x1, %esi
callq 0x44f40
movq %rax, 0x30(%rsp)
jmp 0x58563
movq 0x30(%rsp), %rdi
movl 0x118(%rsp), %esi
callq 0x36be0
movq %rax, 0x20(%rsp)
jmp 0x5857b
movsd 0x28(%rsp), %xmm2
movsd 0x40(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
movq 0x20(%rsp), %rax
movsd (%rax), %xmm3
leaq 0xb8(%rsp), %rdi
callq 0xbd610
jmp 0x585a5
movl $0xd0, %edi
callq 0xf4c0
movq %rax, 0x18(%rsp)
jmp 0x585b6
movq 0x18(%rsp), %rdi
movq 0x70(%rsp), %rax
movslq 0x11c(%rsp), %rcx
movl 0x238(%rax,%rcx,4), %esi
movslq 0x118(%rsp), %rcx
movl 0x238(%rax,%rcx,4), %edx
leaq 0xb8(%rsp), %rcx
callq 0xa6380
jmp 0x585ed
movq 0x18(%rsp), %rax
movq %rax, 0xa0(%rsp)
cmpq $0x0, 0xa0(%rsp)
jne 0x5868f
leaq 0xdbc6c(%rip), %rdi     # 0x13427c
xorl %eax, %eax
callq 0xd4f30
jmp 0x58619
movq $0x0, 0x130(%rsp)
movl $0x1, 0x9c(%rsp)
jmp 0x5881e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x58855
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x58848
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
movl $0xd0, %esi
callq 0xf4d0
jmp 0x58848
movl $0x0, 0x98(%rsp)
movq 0x70(%rsp), %rcx
movl 0x98(%rsp), %eax
movslq 0x11c(%rsp), %rdx
cmpl 0x238(%rcx,%rdx,4), %eax
jge 0x58803
movl $0x0, 0x94(%rsp)
movq 0x70(%rsp), %rcx
movl 0x94(%rsp), %eax
movslq 0x118(%rsp), %rdx
cmpl 0x238(%rcx,%rdx,4), %eax
jge 0x587eb
xorps %xmm0, %xmm0
movsd %xmm0, 0x88(%rsp)
cmpl $0x0, 0x124(%rsp)
jne 0x58735
movq 0x70(%rsp), %rdi
movl 0x120(%rsp), %esi
movl 0x98(%rsp), %edx
movl 0x94(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0x10(%rsp)
jmp 0x58724
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
jmp 0x587ad
cmpl $0x1, 0x124(%rsp)
jne 0x58777
movq 0x70(%rsp), %rdi
movl 0x94(%rsp), %esi
movl 0x120(%rsp), %edx
movl 0x98(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0x8(%rsp)
jmp 0x58766
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
jmp 0x587ab
movq 0x70(%rsp), %rdi
movl 0x98(%rsp), %esi
movl 0x94(%rsp), %edx
movl 0x120(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, (%rsp)
jmp 0x5879d
movsd (%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
jmp 0x587ad
movq 0xa0(%rsp), %rdi
movl 0x98(%rsp), %esi
movl 0x94(%rsp), %edx
movsd 0x88(%rsp), %xmm0
callq 0x6bc00
jmp 0x587d3
jmp 0x587d5
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x586c6
jmp 0x587ed
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x5869a
movq 0xa0(%rsp), %rax
movq %rax, 0x130(%rsp)
movl $0x1, 0x9c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x6bc50
leaq 0xe0(%rsp), %rdi
callq 0x36a80
movq 0x130(%rsp), %rax
addq $0x138, %rsp            # imm = 0x138
retq
leaq 0xb8(%rsp), %rdi
callq 0x6bc50
leaq 0xe0(%rsp), %rdi
callq 0x36a80
movq 0xb0(%rsp), %rdi
callq 0xf890
nop