// File: /workspace/github_cpp_makefile_star2_O1/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, 0x38(%rsp)
movq 0x8(%rdi), %rcx
testq %rcx, %rcx
je 0x1b154
movq 0x10(%rcx), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x1b154
cmpl $0x0, 0x24(%rcx)
jle 0x1b154
movq %rsi, %r12
leaq 0xccc23(%rip), %rax     # 0xe7808
movsd (%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
leaq 0xf0(%rsp), %r13
leaq 0x138(%rsp), %r14
leaq 0xd8(%rsp), %r15
leaq 0x108(%rsp), %rbx
xorl %edx, %edx
movq %rcx, 0x40(%rsp)
movq 0x18(%rcx), %rax
movq %rdx, 0x48(%rsp)
movq (%rax,%rdx,8), %rdi
cmpl $0x0, 0x3c(%rdi)
jg 0x1b13a
movq %rdi, 0x18(%rsp)
movl $0x1, %esi
movl $0x1, %edx
callq 0x37a16
movq 0x38(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %rax, %rbp
testq %rax, %rax
je 0x1ac6a
movq 0x18(%rbp), %rax
testq %rax, %rax
je 0x1ac60
cmpq (%rcx), %rax
jne 0x1ac4a
movq %rbp, %rdi
callq 0x1791a
jmp 0x1ac6c
xorl %eax, %eax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
testq %rbp, %rbp
movq 0x18(%rsp), %rcx
je 0x1ac8a
movl 0x10(%rbp), %edx
movl %edx, 0xc(%rsp)
testq %rax, %rax
je 0x1ac9e
movq %rax, %rdi
callq 0x40828
movq 0x18(%rsp), %rcx
movl %eax, %esi
movl %esi, 0x28(%rsp)
cmpl $0x0, 0x4c(%rcx)
jle 0x1b13a
xorl %edx, %edx
movq 0x40(%rcx), %rax
movq %rdx, 0x50(%rsp)
movq (%rax,%rdx,8), %rdx
cmpl $0x0, 0xb4(%rdx)
movq %rdx, (%rsp)
jne 0x1aced
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq (%rsp), %rsi
callq *0x88(%rax)
movq (%rsp), %rdx
testl %eax, %eax
je 0x1b120
incl 0xb4(%rdx)
cmpl $0x0, 0x8(%rdx)
leaq 0x88(%rsp), %rdi
jle 0x1b0b8
xorl %ecx, %ecx
movq (%rdx), %rax
movq %rcx, 0x60(%rsp)
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rbp
movl 0x20(%rax,%rbp), %eax
movl %eax, 0x2c(%rsp)
movq (%rdx), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rbp), %xmm0
addsd 0x10(%rdx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax,%rbp), %xmm1
addsd 0x18(%rdx), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax,%rbp), %xmm2
addsd 0x20(%rdx), %xmm2
callq 0x84d7e
movq (%rsp), %rax
movq (%rax), %rax
movzbl 0x2c(%rax,%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0xc3605(%rip), %xmm3    # 0xde370
mulsd %xmm3, %xmm0
movzbl 0x2d(%rax,%rbp), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd %xmm3, %xmm1
movzbl 0x2e(%rax,%rbp), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm3, %xmm2
movq %r13, %rdi
callq 0xaada8
movq (%rsp), %rax
movq (%rax), %rax
movswl 0x10(%rax,%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss 0xc364e(%rip), %xmm3    # 0xde400
mulss %xmm3, %xmm0
cvtss2sd %xmm0, %xmm0
movswl 0x12(%rax,%rbp), %ecx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
movswl 0x14(%rax,%rbp), %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss %xmm3, %xmm1
cvtss2sd %xmm1, %xmm1
mulss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
movq %r14, %rdi
callq 0x85816
movq (%rsp), %rax
movq (%rax), %rax
movswl 0x16(%rax,%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss 0xc35fb(%rip), %xmm3    # 0xde400
mulss %xmm3, %xmm0
cvtss2sd %xmm0, %xmm0
movswl 0x18(%rax,%rbp), %ecx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
movswl 0x1a(%rax,%rbp), %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss %xmm3, %xmm1
cvtss2sd %xmm1, %xmm1
mulss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
movq %r15, %rdi
callq 0x85816
leaq 0x120(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x8616a
movq (%rsp), %rax
movq (%rax), %rax
movd 0x1c(%rax,%rbp), %xmm0
xorps %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 0xc3582(%rip), %xmm0    # 0xde3f0
movaps %xmm0, %xmm1
cmpnleps %xmm2, %xmm1
unpcklps %xmm1, %xmm1            # xmm1 = xmm1[0,0,1,1]
movmskpd %xmm1, %eax
testb $0x1, %al
jne 0x1ae8a
movsd 0xc34f0(%rip), %xmm1    # 0xde378
jmp 0x1ae91
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
testb $0x2, %al
movsd %xmm1, 0x70(%rsp)
jne 0x1aea3
movsd %xmm1, 0x30(%rsp)
jmp 0x1aeb1
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
callq 0xa3776
movq (%r12), %rax
movq %r12, %rdi
leaq 0x88(%rsp), %rsi
movq %r14, %rdx
movq %r13, %rcx
movq %r15, %r8
xorl %r9d, %r9d
callq *0x38(%rax)
testq %rax, %rax
je 0x1b08f
movq %rax, 0x58(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movq %r14, %r15
xorl %r13d, %r13d
movq %r12, %r14
leaq 0x150(%rsp), %rbp
leaq 0x88(%rsp), %r12
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
mulsd 0x68(%rsp), %xmm0
divsd 0xc3443(%rip), %xmm0    # 0xde380
movsd %xmm0, 0x20(%rsp)
callq 0x13950
movsd %xmm0, 0x80(%rsp)
movsd 0x20(%rsp), %xmm0
callq 0x13100
movsd %xmm0, 0x78(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x84d8e
movsd 0x20(%rsp), %xmm0
callq 0x13950
mulsd 0x70(%rsp), %xmm0
movq %rbx, %rdi
leaq 0xd8(%rsp), %rsi
callq 0x861e6
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x85166
movsd 0x20(%rsp), %xmm0
callq 0x13100
mulsd 0x30(%rsp), %xmm0
movq %rbx, %rdi
leaq 0x120(%rsp), %rsi
callq 0x861e6
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x85166
movq %rbx, %rdi
movsd 0x80(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
callq 0xa3776
movq (%r14), %rax
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
leaq 0xf0(%rsp), %rcx
movq %rbx, %r8
xorl %r9d, %r9d
callq *0x38(%rax)
movq %rax, 0xa0(%rsp,%r13,8)
incq %r13
cmpq $0x6, %r13
jne 0x1af27
movl $0x1, %r13d
movq %r14, %r12
movl 0xc(%rsp), %r14d
movl 0x28(%rsp), %r15d
movl 0x2c(%rsp), %ebp
movq 0x58(%rsp), %rbx
movq 0x98(%rsp,%r13,8), %rdx
cmpq $0x6, %r13
movq %r13, %rax
movl $0x0, %ecx
cmoveq %rcx, %rax
movq 0xa0(%rsp,%rax,8), %rcx
movq (%r12), %rax
movq %r12, %rdi
movq %rbx, %rsi
xorl %r8d, %r8d
callq *0x48(%rax)
movl %ebp, 0x98(%rax)
movl %r14d, 0x9c(%rax)
movl %r15d, 0xa0(%rax)
incq %r13
cmpq $0x7, %r13
jne 0x1b02a
leaq 0xf0(%rsp), %r13
leaq 0x138(%rsp), %r14
leaq 0x108(%rsp), %rbx
movq 0x60(%rsp), %rcx
incq %rcx
movq (%rsp), %rdx
movslq 0x8(%rdx), %rax
cmpq %rax, %rcx
leaq 0xd8(%rsp), %r15
leaq 0x88(%rsp), %rdi
jl 0x1ad07
cmpl $0x1, 0xb4(%rdx)
jne 0x1b0e2
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq (%rsp), %rsi
callq *0x90(%rax)
movq (%rsp), %rdx
testl %eax, %eax
je 0x1b120
decl 0xb4(%rdx)
jne 0x1b120
testb $0x2, 0x89(%rdx)
je 0x1b120
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq (%rsp), %rsi
callq *0x18(%rax)
movq (%rsp), %rbp
testq %rbp, %rbp
je 0x1b120
movq %rbp, %rdi
callq 0x2ce2e
movl $0xc8, %esi
movq %rbp, %rdi
callq 0x134f0
movq 0x50(%rsp), %rdx
incq %rdx
movq 0x18(%rsp), %rcx
movslq 0x4c(%rcx), %rax
cmpq %rax, %rdx
jl 0x1acae
movq 0x48(%rsp), %rdx
incq %rdx
movq 0x40(%rsp), %rcx
movslq 0x24(%rcx), %rax
cmpq %rax, %rdx
jl 0x1ac16
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq