// File: tomfunkhouser[P]gaps/pkgs/GSV/GSVScan.cpp
// Function: gaps::GSVScan::Grid(int) const
R2Grid *GSVScan::
Grid(int channel) const
{
  // Check dimensions
  int nx = MaxSweepIndex() + 1;
  int ny = MaxBeamIndex() + 1;
  RNBoolean use_sweep_index = TRUE;
  if (nx <= 1) { use_sweep_index = FALSE; nx = NScanlines(); }
  if (nx * ny == 0) return NULL;
                     
  // Allocate image
  R2Grid *grid = new R2Grid(nx, ny);
  if (!grid) {
    RNFail("Unable to allocate grid");
    return NULL;
  }

  // Fill grid
  grid->Clear(R2_GRID_UNKNOWN_VALUE);
  for (int i = 0; i < NScanlines(); i++) {
    GSVScanline *scanline = Scanline(i);
    int ix = (use_sweep_index) ? scanline->SweepIndex() : i;
    const R3Point& sensor_position = scanline->Pose().Viewpoint();
    for (int j = 0; j < scanline->NPoints(); j++) {
      // Get point
      const GSVPoint& point = scanline->Point(j);

      // Compute value
      RNScalar value = R2_GRID_UNKNOWN_VALUE;
      switch (channel) {
      case GSV_POSITION_X_CHANNEL:
        value = point.Position().X();
        break;
      case GSV_POSITION_Y_CHANNEL:
        value = point.Position().Y();
        break;
      case GSV_POSITION_Z_CHANNEL:
        value = point.Position().Z();
        break;
      case GSV_NORMAL_X_CHANNEL:
        value = point.Normal().X();
        break;
      case GSV_NORMAL_Y_CHANNEL:
        value = point.Normal().Y();
        break;
      case GSV_NORMAL_Z_CHANNEL:
        value = point.Normal().Z();
        break;
      case GSV_TANGENT_X_CHANNEL:
        value = point.Tangent().X();
        break;
      case GSV_TANGENT_Y_CHANNEL:
        value = point.Tangent().Y();
        break;
      case GSV_TANGENT_Z_CHANNEL:
        value = point.Tangent().Z();
        break;
      case GSV_RADIUS_0_CHANNEL:
        value = point.Radius(0);
        break;
      case GSV_RADIUS_1_CHANNEL:
        value = point.Radius(1);
        break;
      case GSV_RED_CHANNEL:
        value = point.Color().R();
        break;
      case GSV_GREEN_CHANNEL:
        value = point.Color().G();
        break;
      case GSV_BLUE_CHANNEL:
        value = point.Color().B();
        break;
      case GSV_DISTANCE_CHANNEL:
        value = R3Distance(point.Position(), sensor_position);
        break;
      case GSV_REFLECTIVITY_CHANNEL:
        value = point.Reflectivity();
        break;
      case GSV_POINT_IDENTIFIER_CHANNEL:
        value = point.PointIdentifier();
        break;        
      case GSV_CLUSTER_IDENTIFIER_CHANNEL:
        value = point.ClusterIdentifier();
        break;        
      case GSV_CATEGORY_IDENTIFIER_CHANNEL:
        value = point.CategoryIdentifier();
        break;
      case GSV_CATEGORY_CONFIDENCE_CHANNEL:
        value = point.CategoryConfidence();
        break;
      case GSV_BEAM_INDEX_CHANNEL:
        value = point.BeamIndex();
        break;
      }        

      // Set value
      grid->SetGridValue(ix, point.BeamIndex(), value);
    }
  }
  
  // Return grid
  return grid;
}

// Assembly:
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, 0x1c8(%rsp)
movl %esi, 0x1c4(%rsp)
movq 0x1c8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x54660
movq 0x30(%rsp), %rdi
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
callq 0x546f0
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
movl $0x1, 0x1b8(%rsp)
cmpl $0x1, 0x1c0(%rsp)
jg 0x54d37
movq 0x30(%rsp), %rdi
movl $0x0, 0x1b8(%rsp)
callq 0x3a0d0
movl %eax, 0x1c0(%rsp)
movl 0x1c0(%rsp), %eax
imull 0x1bc(%rsp), %eax
cmpl $0x0, %eax
jne 0x54d5c
movq $0x0, 0x1d0(%rsp)
jmp 0x552e0
movl $0xd0, %edi
callq 0x292e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movl 0x1c0(%rsp), %esi
movl 0x1bc(%rsp), %edx
callq 0x196f50
jmp 0x54d86
movq 0x28(%rsp), %rax
movq %rax, 0x1b0(%rsp)
cmpq $0x0, 0x1b0(%rsp)
jne 0x54de5
leaq 0x21253b(%rip), %rdi    # 0x2672e0
movb $0x0, %al
callq 0x1cbff0
movq $0x0, 0x1d0(%rsp)
jmp 0x552e0
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a8(%rsp)
movl %eax, 0x1a4(%rsp)
movl $0xd0, %esi
callq 0x291f0
jmp 0x552f0
movq 0x1b0(%rsp), %rdi
movsd 0x211303(%rip), %xmm0   # 0x2660f8
callq 0x19aa90
movl $0x0, 0x1a0(%rsp)
movq 0x30(%rsp), %rdi
movl 0x1a0(%rsp), %eax
movl %eax, 0x24(%rsp)
callq 0x3a0d0
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x552d0
movq 0x30(%rsp), %rdi
movl 0x1a0(%rsp), %esi
callq 0x3a0f0
movq %rax, 0x198(%rsp)
cmpl $0x0, 0x1b8(%rsp)
je 0x54e5e
movq 0x198(%rsp), %rdi
callq 0x44c90
movl %eax, 0x20(%rsp)
jmp 0x54e69
movl 0x1a0(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x194(%rsp)
movq 0x198(%rsp), %rdi
callq 0x44c60
movq %rax, %rdi
callq 0x3a330
movq %rax, 0x188(%rsp)
movl $0x0, 0x184(%rsp)
movl 0x184(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x198(%rsp), %rdi
callq 0x3a120
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x552b8
movq 0x198(%rsp), %rdi
movl 0x184(%rsp), %esi
callq 0x3a130
movq %rax, 0x178(%rsp)
movabsq $-0x3f8bf00000000000, %rax # imm = 0xC074100000000000
movq %rax, 0x170(%rsp)
movl 0x1c4(%rsp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, 0x10(%rsp)
subl $0x14, %eax
ja 0x55266
movq 0x10(%rsp), %rax
leaq 0x212367(%rip), %rcx    # 0x26727c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x178(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0x3a200
leaq 0x158(%rsp), %rdi
callq 0x38bb0
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0x3a200
leaq 0x140(%rsp), %rdi
callq 0x38c10
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x3a200
leaq 0x128(%rsp), %rdi
callq 0x38c20
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x3a240
leaq 0x110(%rsp), %rdi
callq 0x38c80
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0xf8(%rsp), %rdi
callq 0x3a240
leaq 0xf8(%rsp), %rdi
callq 0x38c90
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x3a240
leaq 0xe0(%rsp), %rdi
callq 0x38ca0
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x4adc0
leaq 0xc8(%rsp), %rdi
callq 0x38c80
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x4adc0
leaq 0xb0(%rsp), %rdi
callq 0x38c90
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x4adc0
leaq 0x98(%rsp), %rdi
callq 0x38ca0
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rdi
xorl %esi, %esi
callq 0x4ae80
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rdi
movl $0x1, %esi
callq 0x4ae80
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0x80(%rsp), %rdi
callq 0x3a150
leaq 0x80(%rsp), %rdi
callq 0x393b0
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0x68(%rsp), %rdi
callq 0x3a150
leaq 0x68(%rsp), %rdi
callq 0x393c0
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0x50(%rsp), %rdi
callq 0x3a150
leaq 0x50(%rsp), %rdi
callq 0x393d0
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rsi
leaq 0x38(%rsp), %rdi
callq 0x3a200
movq 0x188(%rsp), %rsi
leaq 0x38(%rsp), %rdi
callq 0x10df80
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rdi
callq 0x56640
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rdi
callq 0x56650
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rdi
callq 0x3a1d0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rdi
callq 0x3a1c0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rdi
callq 0x56660
movsd %xmm0, 0x170(%rsp)
jmp 0x55266
movq 0x178(%rsp), %rdi
callq 0x566d0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x170(%rsp)
movq 0x1b0(%rsp), %rax
movq %rax, (%rsp)
movl 0x194(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x178(%rsp), %rdi
callq 0x566d0
movq (%rsp), %rdi
movl 0xc(%rsp), %esi
movl %eax, %edx
movsd 0x170(%rsp), %xmm0
callq 0x566e0
movl 0x184(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x184(%rsp)
jmp 0x54e9c
jmp 0x552ba
movl 0x1a0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a0(%rsp)
jmp 0x54e05
movq 0x1b0(%rsp), %rax
movq %rax, 0x1d0(%rsp)
movq 0x1d0(%rsp), %rax
addq $0x1d8, %rsp            # imm = 0x1D8
retq
movq 0x1a8(%rsp), %rdi
callq 0x29c10
nopl (%rax)