// File: /workspace/github_cpp_makefile_star2_O3/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 0x3fb2f
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 0x3fb14
jmp 0x3fb35
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %r14, %rdi
callq 0x3f864
movl %eax, %ebp
testl %r13d, %r13d
jg 0x3fb4a
movl 0x34(%r14), %r12d
jmp 0x3fb4e
leal 0x1(%r13), %r12d
testl %r12d, %r12d
sete %al
incl %ebp
sete %cl
orb %al, %cl
jne 0x3ff56
movl $0xd0, %edi
callq 0x27930
movq %rax, %r15
movq %rax, %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0xdfbd0
movsd 0x10c595(%rip), %xmm0   # 0x14c118
movq %r15, %rdi
callq 0xe1d50
movl 0x34(%r14), %eax
testl %eax, %eax
jle 0x3ff59
xorl %edx, %edx
decl %ebx
movq %r14, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movq 0x28(%r14), %rcx
movq (%rcx,%rdx,8), %r12
testl %r13d, %r13d
jle 0x3fbc0
movl 0x80(%r12), %ecx
movl %ecx, 0x24(%rsp)
jmp 0x3fbc4
movl %edx, 0x24(%rsp)
cmpl $0x0, 0x30(%r12)
jle 0x3ff45
movq %rdx, 0x38(%rsp)
leaq 0x40(%r12), %rax
movq %rax, 0x40(%rsp)
movl $0x4c, %r14d
xorl %r13d, %r13d
movq 0x28(%r12), %rbp
movsd 0x10c523(%rip), %xmm0   # 0x14c118
cmpl $0x14, %ebx
ja 0x3fee4
leaq 0x10d583(%rip), %rcx    # 0x14d188
movslq (%rcx,%rbx,4), %rax
addq %rcx, %rax
jmpq *%rax
movsd -0x4c(%rbp,%r14), %xmm0
movsd -0x44(%rbp,%r14), %xmm1
movsd -0x3c(%rbp,%r14), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd43de
jmp 0x3fe88
xorps %xmm0, %xmm0
cvtsi2sdl -0x14(%rbp,%r14), %xmm0
jmp 0x3fee4
movsd -0x4c(%rbp,%r14), %xmm0
movsd -0x44(%rbp,%r14), %xmm1
movsd -0x3c(%rbp,%r14), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd43de
leaq 0x8(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x9c174
jmp 0x3fee4
movzbl -0x4(%rbp,%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x10c42f(%rip), %xmm3   # 0x14c0b8
divsd %xmm3, %xmm0
movzbl -0x3(%rbp,%r14), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm3, %xmm1
movzbl -0x2(%rbp,%r14), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
divsd %xmm3, %xmm2
leaq 0x8(%rsp), %rdi
callq 0xf89f4
jmp 0x3fe88
xorps %xmm0, %xmm0
cvtss2sd -0x2c(%rbp,%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x28(%rbp,%r14), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x24(%rbp,%r14), %xmm2
jmp 0x3fda6
movzbl -0x4(%rbp,%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x10c3c2(%rip), %xmm3   # 0x14c0b8
divsd %xmm3, %xmm0
movzbl -0x3(%rbp,%r14), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm3, %xmm1
movzbl -0x2(%rbp,%r14), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
divsd %xmm3, %xmm2
leaq 0x8(%rsp), %rdi
callq 0xf89f4
jmp 0x3fdd1
movzwl -0xc(%rbp,%r14), %eax
jmp 0x3fea5
movsd -0x4c(%rbp,%r14), %xmm0
movsd -0x44(%rbp,%r14), %xmm1
movsd -0x3c(%rbp,%r14), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd43de
jmp 0x3fede
movzbl -0x5(%rbp,%r14), %eax
jmp 0x3fe96
xorps %xmm0, %xmm0
cvtss2sd -0x2c(%rbp,%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x28(%rbp,%r14), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x24(%rbp,%r14), %xmm2
jmp 0x3fe7e
xorps %xmm0, %xmm0
cvtss2sd -0x20(%rbp,%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x1c(%rbp,%r14), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x18(%rbp,%r14), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd4e72
jmp 0x3fdd1
movsd -0x4c(%rbp,%r14), %xmm0
movsd -0x44(%rbp,%r14), %xmm1
movsd -0x3c(%rbp,%r14), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd43de
movsd 0x10(%rsp), %xmm0
jmp 0x3fee4
xorps %xmm0, %xmm0
cvtsi2sdl -0x10(%rbp,%r14), %xmm0
jmp 0x3fee4
movzbl -0x6(%rbp,%r14), %eax
jmp 0x3fe96
xorps %xmm0, %xmm0
cvtss2sd -0x2c(%rbp,%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x28(%rbp,%r14), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x24(%rbp,%r14), %xmm2
jmp 0x3fed4
movzbl -0x4(%rbp,%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x10c28a(%rip), %xmm3   # 0x14c0b8
divsd %xmm3, %xmm0
movzbl -0x3(%rbp,%r14), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm3, %xmm1
movzbl -0x2(%rbp,%r14), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
divsd %xmm3, %xmm2
leaq 0x8(%rsp), %rdi
callq 0xf89f4
jmp 0x3fede
xorps %xmm0, %xmm0
cvtss2sd -0x20(%rbp,%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x1c(%rbp,%r14), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x18(%rbp,%r14), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd4e72
movsd 0x8(%rsp), %xmm0
jmp 0x3fee4
movzbl -0x1(%rbp,%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
jmp 0x3fee4
movzwl -0xa(%rbp,%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x10cff4(%rip), %xmm0   # 0x14cea8
jmp 0x3fee4
xorps %xmm0, %xmm0
cvtss2sd -0x20(%rbp,%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x1c(%rbp,%r14), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x18(%rbp,%r14), %xmm2
leaq 0x8(%rsp), %rdi
callq 0xd4e72
movsd 0x18(%rsp), %xmm0
movzbl -0x6(%rbp,%r14), %eax
imull 0xc8(%r15), %eax
movq 0xb8(%r15), %rcx
addl 0x24(%rsp), %eax
cltq
movsd %xmm0, (%rcx,%rax,8)
incq %r13
movslq 0x30(%r12), %rax
addq $0x50, %r14
cmpq %rax, %r13
jl 0x3fbe8
jmp 0x3ff32
movzbl (%rbp,%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x10c188(%rip), %xmm0   # 0x14c0b8
jmp 0x3fee4
movq 0x30(%rsp), %r14
movl 0x34(%r14), %eax
movq 0x28(%rsp), %r13
movq 0x38(%rsp), %rdx
incq %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jl 0x3fba5
jmp 0x3ff59
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 0x28150
nop