// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelScene.cpp
// Function: gaps::R3SurfelScene::CreateMesh(gaps::R3Mesh&) const
void R3SurfelScene::
CreateMesh(R3Mesh& mesh) const
{
  // Get convenient variables
  R3SurfelTree *tree = Tree();
  if (!tree) return;
  R3SurfelDatabase *database = tree->Database();
  if (!database) return;
  
  // Create disc of triangles for each surfel
  for (int i = 0; i < tree->NNodes(); i++) {
    R3SurfelNode *node = tree->Node(i);
    if (node->NParts() > 0) continue;
    R3SurfelObject *object = (node) ? node->Object(TRUE, TRUE) : NULL;
    while (object && object->Parent() && (object->Parent() != RootObject())) object = object->Parent();
    R3SurfelLabel *label = (object) ? object->CurrentLabel() : NULL;
    int segment_identifier = (object) ? object->SceneIndex() : -1;
    int label_identifier = (label) ? label->Identifier() : -1;
    for (int j = 0; j < node->NBlocks(); j++) {
      R3SurfelBlock *block = node->Block(j);

      // Read block
      if (!database->ReadBlock(block)) continue;

      // Create disc of triangles for each surfel
      for (int k = 0; k < block->NSurfels(); k++) {
        int surfel_identifier = block->SurfelIdentifier(k);
        R3Point position = block->SurfelPosition(k);
        RNRgb color = block->SurfelColor(k);
        R3Vector normal = block->SurfelNormal(k);
        R3Vector tangent1 = block->SurfelTangent(k);
        R3Vector tangent2 = normal % tangent1;
        double r1 = block->SurfelRadius(k, 0);
        double r2 = block->SurfelRadius(k, 1);
        if (r1 <= 0) r1 = 0.1;
        if (r2 <= 0) r2 = r1;

        // Create central vertex
        R3MeshVertex *central_vertex = mesh.CreateVertex(position, normal, color, R2Point(0,0));
        if (!central_vertex) continue;
        
        // Create side vertices
        static const int nsides = 6;
        R3MeshVertex *side_vertices[nsides] = { NULL };
        for (int j = 0; j < nsides; j++) {
          double angle = RN_TWO_PI*j/nsides;
          RNScalar c = cos(angle);
          RNScalar s = sin(angle);
          R3Point p = position;
          p += cos(angle) * r1 * tangent1;
          p += sin(angle) * r2 * tangent2;
          R2Point texcoords(c, s);
          side_vertices[j] = mesh.CreateVertex(p, normal, color, texcoords);
        }

        // Create faces
        for (int j = 0; j < nsides; j++) {
          R3MeshFace *face = mesh.CreateFace(central_vertex, side_vertices[j], side_vertices[(j+1)%nsides]);
          mesh.SetFaceMaterial(face, surfel_identifier);
          mesh.SetFaceSegment(face, segment_identifier);
          mesh.SetFaceCategory(face, label_identifier);
        }
      }

      // Release block
      database->ReleaseBlock(block);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdi, 0x50(%rsp)
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x19f46
movq 0x10(%rax), %rcx
movq %rcx, 0x40(%rsp)
testq %rcx, %rcx
je 0x19f46
movq %rsi, %rbx
movq %rax, 0x28(%rsp)
leaq 0xb7bee(%rip), %rax     # 0xd1778
movsd (%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
leaq 0x150(%rsp), %r15
leaq 0x138(%rsp), %r12
xorl %ecx, %ecx
movq 0x28(%rsp), %rax
movslq 0x24(%rax), %rax
cmpq %rax, %rcx
movq 0x28(%rsp), %rax
jge 0x19f46
movq 0x18(%rax), %rax
movq (%rax,%rcx,8), %rdi
cmpl $0x0, 0x3c(%rdi)
jle 0x19bd0
incq %rcx
jmp 0x19ba6
movq %rcx, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
pushq $0x1
popq %rdx
movl %edx, %esi
callq 0x338b4
movq 0x50(%rsp), %rcx
movq 0x10(%rcx), %rcx
testq %rax, %rax
je 0x19c22
movq %rax, %r14
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x19c03
cmpq (%rcx), %rax
jne 0x19bed
movq %r14, %rdi
callq 0x16d9c
movl 0x10(%r14), %ecx
movl %ecx, 0x14(%rsp)
testq %rax, %rax
je 0x19c2b
movq %rax, %rdi
callq 0x3b59e
jmp 0x19c2e
pushq $-0x1
popq %rax
movl %eax, 0x14(%rsp)
jmp 0x19c2e
pushq $-0x1
popq %rax
movl %eax, 0x4(%rsp)
xorl %esi, %esi
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rdx
movslq 0x4c(%rdx), %rax
cmpq %rax, %rsi
jge 0x19bcb
movq 0x40(%rdx), %rax
movq %rsi, 0x58(%rsp)
movq (%rax,%rsi,8), %r13
movq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x20a64
testl %eax, %eax
je 0x19f39
xorl %r14d, %r14d
movq %r13, 0x60(%rsp)
movslq 0x8(%r13), %rax
cmpq %rax, %r14
jge 0x19f2c
movq (%r13), %rax
imulq $0x30, %r14, %rcx
movq %rcx, 0x8(%rsp)
movl 0x20(%rax,%rcx), %eax
movl %eax, 0x24(%rsp)
leaq 0xf0(%rsp), %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0x20b18
leaq 0xd8(%rsp), %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0x20b50
leaq 0x88(%rsp), %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0x20b6c
leaq 0x120(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, 0x68(%rsp)
movl %r14d, %edx
callq 0x20bc0
leaq 0x108(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movq %rbp, %rdx
callq 0x7902e
movq (%r13), %rax
movq 0x8(%rsp), %rcx
movd 0x1c(%rax,%rcx), %xmm0
pxor %xmm2, %xmm2
punpcklwd %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1],xmm0[2],xmm2[2],xmm0[3],xmm2[3]
cvtdq2ps %xmm0, %xmm0
mulps 0xae6d0(%rip), %xmm0    # 0xc83f0
movaps %xmm0, %xmm1
cmpnleps %xmm2, %xmm1
unpcklps %xmm1, %xmm1            # xmm1 = xmm1[0,0,1,1]
movmskpd %xmm1, %eax
testb $0x1, %al
jne 0x19d3c
movsd 0xae636(%rip), %xmm1    # 0xc8370
jmp 0x19d40
cvtss2sd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
testb $0x2, %al
movl 0x24(%rsp), %r14d
jne 0x19d57
movsd 0x18(%rsp), %xmm0
jmp 0x19d5f
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
leaq 0xa0(%rsp), %r13
movq %r13, %rdi
callq 0x93a30
movq (%rbx), %rax
movq %rbx, %rdi
leaq 0xf0(%rsp), %rsi
leaq 0x88(%rsp), %rdx
leaq 0xd8(%rsp), %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq *0x38(%rax)
testq %rax, %rax
je 0x19f1a
movq %rax, %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
xorl %r13d, %r13d
cmpq $0x6, %r13
je 0x19ebf
cvtsi2sd %r13d, %xmm0
mulsd 0x70(%rsp), %xmm0
divsd 0xae58f(%rip), %xmm0    # 0xc8378
movsd %xmm0, 0x8(%rsp)
callq 0x13990
movsd %xmm0, 0x80(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x13100
movsd %xmm0, 0x78(%rsp)
movq %r15, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x781cc
movsd 0x8(%rsp), %xmm0
callq 0x13990
mulsd 0x18(%rsp), %xmm0
movq %r12, %rdi
leaq 0x120(%rsp), %rsi
callq 0x79072
movq %r15, %rdi
movq %r12, %rsi
callq 0x7850c
movsd 0x8(%rsp), %xmm0
callq 0x13100
mulsd 0x48(%rsp), %xmm0
movq %r12, %rdi
leaq 0x108(%rsp), %rsi
callq 0x79072
movq %r15, %rdi
movq %r12, %rsi
callq 0x7850c
movq %r12, %rdi
movsd 0x80(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
callq 0x93a30
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x88(%rsp), %rdx
leaq 0xd8(%rsp), %rcx
movq %r12, %r8
xorl %r9d, %r9d
callq *0x38(%rax)
movq %rax, 0xa0(%rsp,%r13,8)
incq %r13
jmp 0x19dcc
xorl %eax, %eax
cmpq $0x6, %rax
je 0x19f1a
movq 0xa0(%rsp,%rax,8), %rdx
leaq 0x1(%rax), %r13
cmpq $0x5, %rax
movq %r13, %rax
movl $0x0, %ecx
cmoveq %rcx, %rax
movq 0xa0(%rsp,%rax,8), %rcx
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
xorl %r8d, %r8d
callq *0x48(%rax)
movl %r14d, 0x98(%rax)
movl 0x14(%rsp), %ecx
movl %ecx, 0x9c(%rax)
movl 0x4(%rsp), %ecx
movl %ecx, 0xa0(%rax)
movq %r13, %rax
jmp 0x19ec1
movq 0x68(%rsp), %r14
incq %r14
movq 0x60(%rsp), %r13
jmp 0x19c71
movq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x20aa0
movq 0x58(%rsp), %rsi
incq %rsi
jmp 0x19c34
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq