// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
cmpl $0x0, 0x24(%rdi)
movq %rdi, 0x10(%rsp)
jle 0x33a04
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x18(%rdi), %rax
movq (%rax,%rbx,8), %r14
movq %r14, %rsi
callq 0x2ce66
testl %eax, %eax
je 0x339eb
movslq 0x4c(%r14), %rcx
testq %rcx, %rcx
jle 0x339eb
movq 0x40(%r14), %rsi
movq 0x10(%rsp), %rdi
movl 0x48c(%rdi), %r9d
xorl %r8d, %r8d
movq (%rsi,%r8,8), %rax
movl 0x8(%rax), %eax
cltd
idivl %r9d
xorl %r15d, %r15d
testl %edx, %edx
setg %r15b
addl %ebp, %r15d
addl %eax, %r15d
incq %r8
movl %r15d, %ebp
cmpq %r8, %rcx
jne 0x339c4
jmp 0x339f3
movl %ebp, %r15d
movq 0x10(%rsp), %rdi
incq %rbx
movslq 0x24(%rdi), %rax
movl %r15d, %ebp
cmpq %rax, %rbx
jl 0x33994
jmp 0x33a07
xorl %r15d, %r15d
leal (%r15,%r15,2), %eax
movl %eax, 0x20(%rsp)
movslq %eax, %r14
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x3457c
movq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x3457c
addl %r15d, %r15d
leal (%r15,%r15,2), %eax
movslq %eax, %rsi
movq %r13, %rdi
callq 0x345ae
movq 0x10(%rsp), %rdi
cmpl $0x0, 0x24(%rdi)
jle 0x33cfe
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %r13, 0x58(%rsp)
movq 0x18(%rdi), %rax
movq (%rax,%r14,8), %r12
movq %r12, %rsi
movq %rdi, %rbx
callq 0x2ce66
testl %eax, %eax
movq %r14, 0x48(%rsp)
je 0x33aad
movq %r12, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x401d4
movq 0x8(%rbx), %rcx
movq %rax, %rbx
testq %rax, %rax
je 0x33ab5
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x33aa3
movq 0x10(%rcx), %rdx
cmpq (%rdx), %rax
jne 0x33a89
movq %rbx, %rdi
callq 0x4713e
jmp 0x33ab7
movq %rbx, %rdi
jmp 0x33ce9
xorl %eax, %eax
movl 0x4c(%r12), %edx
testl %edx, %edx
jle 0x33ce4
movq %rax, 0x38(%rsp)
movq %rbx, 0x40(%rsp)
xorl %eax, %eax
movq 0x10(%rsp), %rdi
movq %r12, 0x18(%rsp)
movq 0x40(%r12), %rcx
movq (%rcx,%rax,8), %r8
cmpl $0x0, 0x8(%r8)
jle 0x33cd3
movq %rax, 0x50(%rsp)
movslq %ebp, %rax
leaq (%rax,%rax,2), %rcx
movq %rcx, 0x70(%rsp)
leaq 0x8(,%rcx,4), %rcx
movq %rcx, 0x68(%rsp)
leal (%rax,%rax,2), %eax
addl 0x20(%rsp), %eax
movq %rax, 0x60(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %ebp, 0x24(%rsp)
movq (%r8), %rbx
movslq %r14d, %r14
leaq (%r14,%r14,2), %rbp
shlq $0x4, %rbp
leaq (%rbx,%rbp), %r15
movl 0x360(%rdi), %edx
movq (%rdi), %rax
leaq 0xd(%rsp), %rsi
movq %r15, %rcx
movq %r8, %r13
movq 0x18(%rsp), %r9
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq *0x210(%rax)
addq $0x10, %rsp
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
leaq 0xa(%rsp), %rsi
movl $0xe, %edx
movq %r15, %rcx
movq 0x58(%rsp), %r15
movq %r13, %r8
movq 0x18(%rsp), %r9
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq *0x210(%rax)
movq %r13, %r8
movaps 0x104982(%rip), %xmm1   # 0x138520
movq 0x20(%rsp), %rdi
addq $0x10, %rsp
cvtss2sd (%rbx,%rbp), %xmm0
addsd 0x10(%r13), %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x68(%rsp), %rcx
addq %rcx, %rax
movss %xmm0, -0x8(%rax,%r12,4)
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rbx,%rbp), %xmm0
addsd 0x18(%r13), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x4(%rax,%r12,4)
xorps %xmm0, %xmm0
cvtss2sd 0x8(%rbx,%rbp), %xmm0
addsd 0x20(%r13), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rax,%r12,4)
movq 0x30(%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 0x104907(%rip), %xmm1   # 0x138530
movlps %xmm0, -0x8(%rax,%r12,4)
movswl 0x14(%rbx,%rbp), %ecx
movl 0x24(%rsp), %ebp
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
mulss %xmm1, %xmm0
movss %xmm0, (%rax,%r12,4)
movb 0xd(%rsp), %al
movq (%r15), %rcx
movq 0x70(%rsp), %rdx
addq %rdx, %rcx
movb %al, (%r12,%rcx)
movb 0xe(%rsp), %al
movq (%r15), %rcx
addq %rdx, %rcx
movb %al, 0x1(%r12,%rcx)
movb 0xf(%rsp), %al
movq (%r15), %rcx
addq %rdx, %rcx
movb %al, 0x2(%r12,%rcx)
movb 0xa(%rsp), %al
movq 0x60(%rsp), %rsi
leal (%rsi,%r12), %ecx
movq (%r15), %rdx
movb %al, (%rdx,%rcx)
movb 0xb(%rsp), %al
leal 0x1(%rsi,%r12), %ecx
movq (%r15), %rdx
movb %al, (%rdx,%rcx)
movb 0xc(%rsp), %al
leal (%rsi,%r12), %ecx
addl $0x2, %ecx
movq (%r15), %rdx
movb %al, (%rdx,%rcx)
addl 0x48c(%rdi), %r14d
addq $0x3, %r12
incl %ebp
cmpl 0x8(%r13), %r14d
jl 0x33b1e
movq 0x18(%rsp), %r12
movl 0x4c(%r12), %edx
movq 0x50(%rsp), %rax
incq %rax
movslq %edx, %rcx
cmpq %rcx, %rax
jl 0x33ada
jmp 0x33ce9
movq 0x10(%rsp), %rdi
movq 0x48(%rsp), %r14
incq %r14
movslq 0x24(%rdi), %rax
cmpq %rax, %r14
jl 0x33a57
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop