// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Utils/R3SurfelViewer.cpp
// Function: gaps::R3SurfelViewer::ComputeVBOBuffers(std::vector<float, std::allocator<float>>&, std::vector<float, std::allocator<float>>&, std::vector<unsigned char, std::allocator<unsigned char>>&) const
void R3SurfelViewer::
ComputeVBOBuffers(std::vector<GLfloat>& surfel_positions,
    std::vector<GLfloat>& surfel_normals, std::vector<GLubyte>& surfel_colors) const
{
  // Count surfels
  int nsurfels = 0;
  for (int i = 0; i < resident_nodes.NNodes(); i++) {
    R3SurfelNode *node = resident_nodes.Node(i);
    if (!NodeVisibility(node)) continue;
    for (int j = 0; j < node->NBlocks(); j++) {
      R3SurfelBlock *block = node->Block(j);
      nsurfels += block->NSurfels() / subsampling_factor;
      if (block->NSurfels() % subsampling_factor > 0) nsurfels++;
    }
  }

  // Allocate in-memory buffers
  surfel_positions.resize(3 * nsurfels);
  surfel_normals.resize(3 * nsurfels);
  surfel_colors.resize(2 * 3 * nsurfels);

  // Fill in-memory buffers 
  int count = 0;
  unsigned int pick_color_offset = 3 * nsurfels;
  for (int i = 0; i < resident_nodes.NNodes(); i++) {
    R3SurfelNode *node = resident_nodes.Node(i);
    if (!NodeVisibility(node)) continue;
    R3SurfelObject *object = node->Object(TRUE, TRUE);
    while (object && object->Parent() && (object->Parent() != scene->RootObject())) object = object->Parent();
    R3SurfelLabel *label = (object) ? object->CurrentLabel() : NULL;
    for (int j = 0; j < node->NBlocks(); j++) {
      R3SurfelBlock *block = node->Block(j);
      const R3Point& block_origin = block->PositionOrigin();
      for (int k = 0; k < block->NSurfels(); k += subsampling_factor) {
        const R3Surfel *surfel = block->Surfel(k);

        // Compute colors
        GLubyte surfel_color[3], pick_color[3];
        CreateColor(surfel_color, surfel_color_scheme,
          surfel, block, node, object, label);
        CreateColor(pick_color, R3_SURFEL_VIEWER_COLOR_BY_PICK_INDEX,
          surfel, block, node, object, label);
        assert(count < nsurfels);

        // Fill buffers
        int i0 = 3 * (count++);
        int i1 = i0+1;
        int i2 = i1+1;
        surfel_positions[i0] = block_origin.X() + surfel->X();
        surfel_positions[i1] = block_origin.Y() + surfel->Y();
        surfel_positions[i2] = block_origin.Z() + surfel->Z();
        surfel_normals[i0] = surfel->NX();
        surfel_normals[i1] = surfel->NY();
        surfel_normals[i2] = surfel->NZ();
        surfel_colors[i0] = surfel_color[0];
        surfel_colors[i1] = surfel_color[1];
        surfel_colors[i2] = surfel_color[2];
        surfel_colors[i0 + pick_color_offset] = pick_color[0];
        surfel_colors[i1 + pick_color_offset] = pick_color[1];
        surfel_colors[i2 + pick_color_offset] = pick_color[2];
      }
    }
  }
  
  // Just checking
  assert(count == nsurfels);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %rbx
movq %rdx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r12
cmpl $0x0, 0x24(%rdi)
jle 0x33a4a
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x18(%r12), %rax
movq (%rax,%r13,8), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x2cf82
testl %eax, %eax
je 0x33a35
movslq 0x4c(%r14), %rcx
testq %rcx, %rcx
jle 0x33a35
movq 0x40(%r14), %rsi
movl 0x48c(%r12), %edi
xorl %r8d, %r8d
movq (%rsi,%r8,8), %rax
movl 0x8(%rax), %eax
cltd
idivl %edi
xorl %r15d, %r15d
testl %edx, %edx
setg %r15b
addl %ebp, %r15d
addl %eax, %r15d
incq %r8
movl %r15d, %ebp
cmpq %r8, %rcx
jne 0x33a0f
jmp 0x33a38
movl %ebp, %r15d
incq %r13
movslq 0x24(%r12), %rax
movl %r15d, %ebp
cmpq %rax, %r13
jl 0x339df
jmp 0x33a4d
xorl %r15d, %r15d
leal (%r15,%r15,2), %eax
movl %eax, 0x18(%rsp)
movslq %eax, %r14
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x345dc
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x345dc
addl %r15d, %r15d
leal (%r15,%r15,2), %eax
movslq %eax, %rsi
movq %rbx, %rdi
callq 0x3460e
cmpl $0x0, 0x24(%r12)
jle 0x33d3e
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %r12, 0x8(%rsp)
movq %rbx, 0x50(%rsp)
movq 0x18(%r12), %rax
movq (%rax,%r13,8), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x2cf82
testl %eax, %eax
movq %r13, 0x40(%rsp)
je 0x33d23
movq %r14, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x4013a
movq 0x8(%r12), %rcx
movq %rax, %r15
testq %rax, %rax
je 0x33afb
movq 0x18(%r15), %rax
testq %rax, %rax
je 0x33af1
movq 0x10(%rcx), %rdx
cmpq (%rdx), %rax
jne 0x33ad7
movq %r15, %rdi
callq 0x47182
jmp 0x33afd
xorl %eax, %eax
cmpl $0x0, 0x4c(%r14)
jle 0x33d23
movq %rax, 0x30(%rsp)
movq %r15, 0x38(%rsp)
xorl %edx, %edx
movq %r14, 0x10(%rsp)
movq 0x40(%r14), %rax
movq %rdx, 0x48(%rsp)
movq (%rax,%rdx,8), %r15
cmpl $0x0, 0x8(%r15)
jle 0x33d09
movslq %ebp, %rax
leaq (%rax,%rax,2), %rdx
movq %rdx, 0x68(%rsp)
leaq 0x8(,%rdx,4), %rdx
movq %rdx, 0x60(%rsp)
leal (%rax,%rax,2), %eax
addl 0x18(%rsp), %eax
movq %rax, 0x58(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %ebp, 0x1c(%rsp)
movq (%r15), %rbx
movslq %r14d, %r14
leaq (%r14,%r14,2), %rbp
shlq $0x4, %rbp
leaq (%rbx,%rbp), %rcx
movq %rcx, 0x70(%rsp)
movq 0x8(%rsp), %rdi
movl 0x360(%rdi), %edx
movq (%rdi), %rax
movq %rdi, %r13
leaq 0x5(%rsp), %rsi
movq %r15, %r8
movq 0x10(%rsp), %r9
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq *0x210(%rax)
addq $0x10, %rsp
movq %r13, %rdi
movq (%r13), %rax
leaq 0x2(%rsp), %rsi
movl $0xe, %edx
movq 0x70(%rsp), %rcx
movq 0x50(%rsp), %r13
movq %r15, %r8
movq 0x10(%rsp), %r9
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq *0x210(%rax)
movaps 0x1078e1(%rip), %xmm1   # 0x13b4c0
addq $0x10, %rsp
cvtss2sd (%rbx,%rbp), %xmm0
addsd 0x10(%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x60(%rsp), %rcx
addq %rcx, %rax
movss %xmm0, -0x8(%rax,%r12,4)
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rbx,%rbp), %xmm0
addsd 0x18(%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x4(%rax,%r12,4)
xorps %xmm0, %xmm0
cvtss2sd 0x8(%rbx,%rbp), %xmm0
addsd 0x20(%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rax,%r12,4)
movq 0x28(%rsp), %rax
movq (%rax), %rax
addq %rcx, %rax
movd 0x10(%rbx,%rbp), %xmm0
pshuflw $0x60, %xmm0, %xmm0     # xmm0 = xmm0[0,0,2,1,4,5,6,7]
psrad $0x10, %xmm0
cvtdq2ps %xmm0, %xmm0
mulps %xmm1, %xmm0
movss 0x10786b(%rip), %xmm1   # 0x13b4d0
movlps %xmm0, -0x8(%rax,%r12,4)
movswl 0x14(%rbx,%rbp), %ecx
movl 0x1c(%rsp), %ebp
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
mulss %xmm1, %xmm0
movss %xmm0, (%rax,%r12,4)
movb 0x5(%rsp), %al
movq (%r13), %rcx
movq 0x68(%rsp), %rdx
addq %rdx, %rcx
movb %al, (%r12,%rcx)
movb 0x6(%rsp), %al
movq (%r13), %rcx
addq %rdx, %rcx
movb %al, 0x1(%r12,%rcx)
movb 0x7(%rsp), %al
movq (%r13), %rcx
addq %rdx, %rcx
movb %al, 0x2(%r12,%rcx)
movb 0x2(%rsp), %al
movq 0x58(%rsp), %rsi
leal (%rsi,%r12), %ecx
movq (%r13), %rdx
movb %al, (%rdx,%rcx)
movb 0x3(%rsp), %al
leal 0x1(%rsi,%r12), %ecx
movq (%r13), %rdx
movb %al, (%rdx,%rcx)
movb 0x4(%rsp), %al
leal 0x2(%rsi,%r12), %ecx
movq (%r13), %rdx
movb %al, (%rdx,%rcx)
movq 0x8(%rsp), %rax
addl 0x48c(%rax), %r14d
addq $0x3, %r12
incl %ebp
cmpl 0x8(%r15), %r14d
jl 0x33b5c
movq 0x48(%rsp), %rdx
incq %rdx
movq 0x10(%rsp), %r14
movslq 0x4c(%r14), %rax
cmpq %rax, %rdx
jl 0x33b19
movq 0x40(%rsp), %r13
incq %r13
movq 0x8(%rsp), %r12
movslq 0x24(%r12), %rax
cmpq %rax, %r13
jl 0x33a9f
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop