// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %r9
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ebx, %ebx
movslq 0x24(%r9), %rax
cmpq %rax, %r12
jge 0x31ed8
movq 0x18(%r9), %rax
movq (%rax,%r12,8), %r14
movq %r9, %rdi
movq %r14, %rsi
movq %r9, %r13
callq 0x2c136
testl %eax, %eax
je 0x31ed0
movl 0x4c(%r14), %ecx
movl 0x48c(%r13), %esi
testl %ecx, %ecx
cmovlel %r15d, %ecx
xorl %edi, %edi
movq %r13, %r9
cmpq %rdi, %rcx
je 0x31ed3
movq 0x40(%r14), %rax
movq (%rax,%rdi,8), %rax
movl 0x8(%rax), %eax
cltd
idivl %esi
xorl %r8d, %r8d
testl %edx, %edx
setg %r8b
addl %r8d, %ebx
addl %eax, %ebx
incq %rdi
jmp 0x31eaa
movq %r13, %r9
incq %r12
jmp 0x31e71
leal (%rbx,%rbx,2), %eax
movl %eax, 0x20(%rsp)
movslq %eax, %r14
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movq %r9, 0x10(%rsp)
callq 0x32d34
movq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x32d34
imull $0x6, %ebx, %eax
movslq %eax, %rsi
movq 0x38(%rsp), %rdi
callq 0x32d66
movq 0x10(%rsp), %rdi
xorl %ecx, %ecx
xorl %ebp, %ebp
movslq 0x24(%rdi), %rax
cmpq %rax, %rcx
jge 0x32198
movq 0x18(%rdi), %rax
movq %rcx, 0x58(%rsp)
movq (%rax,%rcx,8), %r14
movq %r14, %rsi
movq %rdi, %rbx
callq 0x2c136
testl %eax, %eax
je 0x31f8a
movq %r14, %rdi
pushq $0x1
popq %rdx
movl %edx, %esi
callq 0x3d228
movq 0x8(%rbx), %rcx
movq %rbx, %rdi
testq %rax, %rax
movq %rax, 0x50(%rsp)
je 0x31f97
movq %rax, %rsi
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x31f76
movq 0x10(%rcx), %rdx
cmpq (%rdx), %rax
jne 0x31f57
movq %rsi, %rdi
callq 0x43340
movq 0x10(%rsp), %rdi
movq %rax, 0x40(%rsp)
jmp 0x31fa0
movq %rbx, %rdi
movq 0x58(%rsp), %rcx
incq %rcx
jmp 0x31f1a
movq $0x0, 0x40(%rsp)
xorl %ecx, %ecx
movq %r14, 0x48(%rsp)
movslq 0x4c(%r14), %rax
cmpq %rax, %rcx
jge 0x31f8d
movq 0x40(%r14), %rax
movq %rcx, 0x60(%rsp)
movq (%rax,%rcx,8), %r15
movslq %ebp, %rax
imulq $0xc, %rax, %rcx
addq $0x8, %rcx
movq %rcx, 0x78(%rsp)
leaq (%rax,%rax,2), %rcx
movq %rcx, 0x70(%rsp)
leal (%rax,%rax,2), %eax
addl 0x20(%rsp), %eax
movq %rax, 0x68(%rsp)
xorl %ebx, %ebx
xorl %eax, %eax
cmpl 0x8(%r15), %eax
jge 0x32186
movq (%r15), %r14
movl %eax, 0x1c(%rsp)
movslq 0x1c(%rsp), %rax
imulq $0x30, %rax, %r13
movl %ebp, 0x24(%rsp)
leaq (%r14,%r13), %rcx
movq %rcx, 0x80(%rsp)
movl 0x360(%rdi), %edx
movq (%rdi), %rax
leaq 0xd(%rsp), %rsi
movq %r15, %r8
movq 0x48(%rsp), %r12
movq %r12, %r9
movq 0x40(%rsp), %rbp
pushq %rbp
pushq 0x58(%rsp)
callq *0x210(%rax)
addq $0x10, %rsp
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
leaq 0xa(%rsp), %rsi
pushq $0xe
popq %rdx
movq 0x80(%rsp), %rcx
movq %r15, %r8
movq %r12, %r9
pushq %rbp
pushq 0x58(%rsp)
callq *0x210(%rax)
movq 0x20(%rsp), %rdi
addq $0x10, %rsp
cvtss2sd (%r14,%r13), %xmm0
addsd 0x10(%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x78(%rsp), %rcx
addq %rcx, %rax
movss %xmm0, -0x8(%rax,%rbx,4)
cvtss2sd 0x4(%r14,%r13), %xmm0
addsd 0x18(%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x4(%rax,%rbx,4)
cvtss2sd 0x8(%r14,%r13), %xmm0
addsd 0x20(%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rax,%rbx,4)
movq 0x30(%rsp), %rax
movq (%rax), %rax
addq %rcx, %rax
movd 0x10(%r14,%r13), %xmm0
pshuflw $0x60, %xmm0, %xmm0     # xmm0 = xmm0[0,0,2,1,4,5,6,7]
psrad $0x10, %xmm0
cvtdq2ps %xmm0, %xmm0
mulps 0xe83e3(%rip), %xmm0    # 0x11a4d0
movlps %xmm0, -0x8(%rax,%rbx,4)
movswl 0x14(%r14,%r13), %ecx
cvtsi2ss %ecx, %xmm0
mulss 0xe83dc(%rip), %xmm0    # 0x11a4e0
movss %xmm0, (%rax,%rbx,4)
movb 0xd(%rsp), %al
movq 0x38(%rsp), %rsi
movq (%rsi), %rcx
movq 0x70(%rsp), %rdx
addq %rdx, %rcx
movb %al, (%rbx,%rcx)
movb 0xe(%rsp), %al
movq (%rsi), %rcx
addq %rdx, %rcx
movb %al, 0x1(%rbx,%rcx)
movb 0xf(%rsp), %al
movq (%rsi), %rcx
addq %rdx, %rcx
movb %al, 0x2(%rbx,%rcx)
movb 0xa(%rsp), %al
movq 0x68(%rsp), %r8
leal (%r8,%rbx), %ecx
movq (%rsi), %rdx
movb %al, (%rdx,%rcx)
movb 0xb(%rsp), %al
leal 0x1(%r8,%rbx), %ecx
movq (%rsi), %rdx
movb %al, (%rdx,%rcx)
movb 0xc(%rsp), %al
leal 0x2(%r8,%rbx), %ecx
movq (%rsi), %rdx
movb %al, (%rdx,%rcx)
movl 0x1c(%rsp), %eax
addl 0x48c(%rdi), %eax
movl 0x24(%rsp), %ebp
incl %ebp
addq $0x3, %rbx
jmp 0x31fe6
movq 0x60(%rsp), %rcx
incq %rcx
movq 0x48(%rsp), %r14
jmp 0x31fa7
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq