// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %ebx
movq %rdi, %r14
movslq 0x34(%rdi), %rax
testq %rax, %rax
jle 0x3ff5f
movq 0x28(%r14), %rcx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movl 0x80(%rsi), %esi
cmpl %r13d, %esi
cmovgl %esi, %r13d
incq %rdx
cmpq %rdx, %rax
jne 0x3ff44
jmp 0x3ff65
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %r14, %rdi
callq 0x3fcc8
movl %eax, %ebp
testl %r13d, %r13d
jg 0x3ff7a
movl 0x34(%r14), %r12d
jmp 0x3ff7e
leal 0x1(%r13), %r12d
testl %r12d, %r12d
sete %al
incl %ebp
sete %cl
orb %al, %cl
jne 0x40382
movl $0xd0, %edi
callq 0x27930
movq %rax, %r15
movq %rax, %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0xe16fc
movsd 0x10e16d(%rip), %xmm0   # 0x14e120
movq %r15, %rdi
callq 0xe38ae
cmpl $0x0, 0x34(%r14)
jle 0x40385
xorl %ecx, %ecx
decl %ebx
movq %r14, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movq 0x28(%r14), %rax
movq (%rax,%rcx,8), %r12
testl %r13d, %r13d
movq %rcx, 0x40(%rsp)
jle 0x3fff4
movl 0x80(%r12), %eax
movl %eax, 0x24(%rsp)
jmp 0x3fff8
movl %ecx, 0x24(%rsp)
cmpl $0x0, 0x30(%r12)
jle 0x40361
leaq 0x40(%r12), %rax
movq %rax, 0x38(%rsp)
movl $0x4c, %r13d
xorl %r14d, %r14d
movq 0x28(%r12), %rbp
movsd 0x10e0fc(%rip), %xmm0   # 0x14e120
cmpl $0x14, %ebx
ja 0x40313
leaq 0x10f124(%rip), %rcx    # 0x14f158
movslq (%rcx,%rbx,4), %rax
addq %rcx, %rax
jmpq *%rax
movsd -0x4c(%rbp,%r13), %xmm0
movsd -0x44(%rbp,%r13), %xmm1
movsd -0x3c(%rbp,%r13), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd5e2e
jmp 0x402b7
xorps %xmm0, %xmm0
cvtsi2sdl -0x14(%rbp,%r13), %xmm0
jmp 0x40313
movsd -0x4c(%rbp,%r13), %xmm0
movsd -0x44(%rbp,%r13), %xmm1
movsd -0x3c(%rbp,%r13), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd5e2e
leaq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x9d528
jmp 0x40313
movzbl -0x4(%rbp,%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x10e010(%rip), %xmm3   # 0x14e0c8
divsd %xmm3, %xmm0
movzbl -0x3(%rbp,%r13), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm3, %xmm1
movzbl -0x2(%rbp,%r13), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
divsd %xmm3, %xmm2
leaq 0x8(%rsp), %rdi
callq 0xfa640
jmp 0x402b7
xorps %xmm0, %xmm0
cvtss2sd -0x2c(%rbp,%r13), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x28(%rbp,%r13), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x24(%rbp,%r13), %xmm2
jmp 0x401d5
movzbl -0x4(%rbp,%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x10dfa3(%rip), %xmm3   # 0x14e0c8
divsd %xmm3, %xmm0
movzbl -0x3(%rbp,%r13), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm3, %xmm1
movzbl -0x2(%rbp,%r13), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
divsd %xmm3, %xmm2
leaq 0x8(%rsp), %rdi
callq 0xfa640
jmp 0x40200
movzwl -0xc(%rbp,%r13), %eax
jmp 0x402d4
movsd -0x4c(%rbp,%r13), %xmm0
movsd -0x44(%rbp,%r13), %xmm1
movsd -0x3c(%rbp,%r13), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd5e2e
jmp 0x4030d
movzbl -0x5(%rbp,%r13), %eax
jmp 0x402c5
xorps %xmm0, %xmm0
cvtss2sd -0x2c(%rbp,%r13), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x28(%rbp,%r13), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x24(%rbp,%r13), %xmm2
jmp 0x402ad
xorps %xmm0, %xmm0
cvtss2sd -0x20(%rbp,%r13), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x1c(%rbp,%r13), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x18(%rbp,%r13), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd68c6
jmp 0x40200
movsd -0x4c(%rbp,%r13), %xmm0
movsd -0x44(%rbp,%r13), %xmm1
movsd -0x3c(%rbp,%r13), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd5e2e
movsd 0x10(%rsp), %xmm0
jmp 0x40313
xorps %xmm0, %xmm0
cvtsi2sdl -0x10(%rbp,%r13), %xmm0
jmp 0x40313
movzbl -0x6(%rbp,%r13), %eax
jmp 0x402c5
xorps %xmm0, %xmm0
cvtss2sd -0x2c(%rbp,%r13), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x28(%rbp,%r13), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x24(%rbp,%r13), %xmm2
jmp 0x40303
movzbl -0x4(%rbp,%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x10de6b(%rip), %xmm3   # 0x14e0c8
divsd %xmm3, %xmm0
movzbl -0x3(%rbp,%r13), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm3, %xmm1
movzbl -0x2(%rbp,%r13), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
divsd %xmm3, %xmm2
leaq 0x8(%rsp), %rdi
callq 0xfa640
jmp 0x4030d
xorps %xmm0, %xmm0
cvtss2sd -0x20(%rbp,%r13), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x1c(%rbp,%r13), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x18(%rbp,%r13), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd68c6
movsd 0x8(%rsp), %xmm0
jmp 0x40313
movzbl -0x1(%rbp,%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
jmp 0x40313
movzwl -0xa(%rbp,%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x10ebc5(%rip), %xmm0   # 0x14eea8
jmp 0x40313
xorps %xmm0, %xmm0
cvtss2sd -0x20(%rbp,%r13), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x1c(%rbp,%r13), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x18(%rbp,%r13), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd68c6
movsd 0x18(%rsp), %xmm0
movzbl -0x6(%rbp,%r13), %eax
imull 0xc8(%r15), %eax
movq 0xb8(%r15), %rcx
addl 0x24(%rsp), %eax
cltq
movsd %xmm0, (%rcx,%rax,8)
incq %r14
movslq 0x30(%r12), %rax
addq $0x50, %r13
cmpq %rax, %r14
jl 0x40017
jmp 0x40361
movzbl (%rbp,%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x10dd69(%rip), %xmm0   # 0x14e0c8
jmp 0x40313
movq 0x40(%rsp), %rcx
incq %rcx
movq 0x30(%rsp), %r14
movslq 0x34(%r14), %rax
cmpq %rax, %rcx
movq 0x28(%rsp), %r13
jl 0x3ffd4
jmp 0x40385
xorl %r15d, %r15d
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0xd0, %esi
movq %r15, %rdi
callq 0x27950
movq %rbx, %rdi
callq 0x28160
nop