// File: /workspace/github_cpp_makefile_star2_O3/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, 0x48(%rsp)
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x1b048
movq 0x10(%rax), %rcx
movq %rcx, 0x18(%rsp)
testq %rcx, %rcx
je 0x1b048
cmpl $0x0, 0x24(%rax)
jle 0x1b048
movq %rsi, %rbx
movq %rax, 0x38(%rsp)
leaq 0xcbd4e(%rip), %rax     # 0xe67f8
movsd (%rax), %xmm0
movq 0x38(%rsp), %rax
xorl %edx, %edx
movsd %xmm0, 0x70(%rsp)
movq 0x18(%rax), %rax
movq %rdx, 0x50(%rsp)
movq (%rax,%rdx,8), %rdi
cmpl $0x0, 0x3c(%rdi)
jg 0x1b02b
movq %rdi, %rbp
movl $0x1, %esi
movl $0x1, %edx
callq 0x376f4
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rcx
testq %rax, %rax
je 0x1ab26
movq %rax, %r14
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x1ab03
cmpq (%rcx), %rax
jne 0x1aaed
movq %r14, %rdi
callq 0x17872
movl 0x10(%r14), %ecx
movl %ecx, 0x14(%rsp)
testq %rax, %rax
je 0x1ab38
movq %rax, %rdi
callq 0x4048a
movl %eax, 0x10(%rsp)
jmp 0x1ab40
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
jmp 0x1ab40
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
leaq 0xc0(%rsp), %r13
leaq 0xa8(%rsp), %r15
leaq 0x90(%rsp), %r12
movq %rbp, %r14
cmpl $0x0, 0x4c(%rbp)
leaq 0xd8(%rsp), %r8
jle 0x1b02b
xorl %ebp, %ebp
movq %r14, 0x58(%rsp)
movq 0x40(%r14), %rax
movq (%rax,%rbp,8), %rsi
movl 0xb4(%rsi), %eax
testl %eax, %eax
movq %rbp, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
jne 0x1abc3
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r8, %rbp
callq *0x88(%rax)
movq %rbp, %r8
movq 0x20(%rsp), %rbp
testl %eax, %eax
je 0x1b01b
movq 0x8(%rsp), %rsi
movl 0xb4(%rsi), %eax
incl %eax
movl %eax, 0xb4(%rsi)
cmpl $0x0, 0x8(%rsi)
jle 0x1af9b
xorl %ecx, %ecx
movq 0x8(%rsp), %r14
movq (%r14), %rax
movq %rcx, 0x68(%rsp)
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rbp
movl 0x20(%rax,%rbp), %ecx
movl %ecx, 0x34(%rsp)
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rbp), %xmm0
addsd 0x10(%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax,%rbp), %xmm1
addsd 0x18(%r14), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax,%rbp), %xmm2
addsd 0x20(%r14), %xmm2
movq %r8, %rdi
callq 0x83dc2
movq (%r14), %rax
movzbl 0x2c(%rax,%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0xc2731(%rip), %xmm3    # 0xdd370
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 0xa9b1c
movq (%r14), %rax
movswl 0x10(%rax,%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss 0xc277e(%rip), %xmm3    # 0xdd400
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 %r15, %rdi
callq 0x84856
movq (%r14), %rax
movswl 0x16(%rax,%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss 0xc272f(%rip), %xmm3    # 0xdd400
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 %r12, %rdi
callq 0x84856
leaq 0x120(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x8518a
movq (%r14), %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 0xc26ba(%rip), %xmm0    # 0xdd3f0
movaps %xmm0, %xmm1
cmpnleps %xmm2, %xmm1
unpcklps %xmm1, %xmm1            # xmm1 = xmm1[0,0,1,1]
movmskpd %xmm1, %eax
testb $0x1, %al
jne 0x1ad52
movsd 0xc2628(%rip), %xmm1    # 0xdd378
jmp 0x1ad59
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
testb $0x2, %al
movsd %xmm1, 0x78(%rsp)
jne 0x1ad6b
movsd %xmm1, 0x40(%rsp)
jmp 0x1ad79
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
leaq 0xf0(%rsp), %rbp
movq %rbp, %rdi
callq 0xa2542
movq (%rbx), %rax
movq %rbx, %rdi
leaq 0xd8(%rsp), %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq *0x38(%rax)
testq %rax, %rax
je 0x1af68
movq %rax, 0x60(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
leaq 0x120(%rsp), %rbp
xorl %r14d, %r14d
movq %rbx, %r15
leaq 0x150(%rsp), %rbx
leaq 0x138(%rsp), %r13
leaq 0xd8(%rsp), %r12
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
mulsd 0x70(%rsp), %xmm0
divsd 0xc256f(%rip), %xmm0    # 0xdd380
movsd %xmm0, 0x28(%rsp)
callq 0x13950
movsd %xmm0, 0x88(%rsp)
movsd 0x28(%rsp), %xmm0
callq 0x13100
movsd %xmm0, 0x80(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x83dd2
movsd 0x28(%rsp), %xmm0
callq 0x13950
mulsd 0x78(%rsp), %xmm0
movq %r13, %rdi
leaq 0x90(%rsp), %rsi
callq 0x851f2
movq %rbx, %rdi
movq %r13, %rsi
callq 0x841a4
movsd 0x28(%rsp), %xmm0
callq 0x13100
mulsd 0x40(%rsp), %xmm0
movq %r13, %rdi
movq %rbp, %rsi
callq 0x851f2
movq %rbx, %rdi
movq %r13, %rsi
callq 0x841a4
movq %r13, %rdi
movsd 0x88(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
callq 0xa2542
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
leaq 0xa8(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq *0x38(%rax)
movq %rax, 0xf0(%rsp,%r14,8)
incq %r14
cmpq $0x6, %r14
jne 0x1adfb
movl $0x1, %r14d
movq %r15, %rbx
movl 0x10(%rsp), %ebp
movl 0x14(%rsp), %r15d
movl 0x34(%rsp), %r12d
movq 0x60(%rsp), %r13
movq 0xe8(%rsp,%r14,8), %rdx
cmpq $0x6, %r14
movq %r14, %rax
movl $0x0, %ecx
cmoveq %rcx, %rax
movq 0xf0(%rsp,%rax,8), %rcx
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
xorl %r8d, %r8d
callq *0x48(%rax)
movl %r12d, 0x98(%rax)
movl %r15d, 0x9c(%rax)
movl %ebp, 0xa0(%rax)
incq %r14
cmpq $0x7, %r14
jne 0x1af04
leaq 0xc0(%rsp), %r13
leaq 0xa8(%rsp), %r15
leaq 0x90(%rsp), %r12
movq 0x68(%rsp), %rcx
incq %rcx
movq 0x8(%rsp), %r14
movslq 0x8(%r14), %rax
cmpq %rax, %rcx
leaq 0xd8(%rsp), %r8
jl 0x1abdc
movl 0xb4(%r14), %eax
movq 0x58(%rsp), %r14
movq 0x20(%rsp), %rbp
cmpl $0x1, %eax
jne 0x1afd4
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r8, %rbp
movq 0x8(%rsp), %rsi
callq *0x90(%rax)
movq %rbp, %r8
movq 0x20(%rsp), %rbp
testl %eax, %eax
je 0x1b01b
movq 0x8(%rsp), %rax
movl 0xb4(%rax), %eax
decl %eax
movq 0x8(%rsp), %rcx
movl %eax, 0xb4(%rcx)
jne 0x1b01b
testb $0x2, 0x89(%rcx)
je 0x1b01b
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq %rcx, %rsi
callq *0x18(%rax)
movq 0x8(%rsp), %rdi
callq 0x2ca06
movl $0xc8, %esi
movq 0x8(%rsp), %rdi
callq 0x134f0
leaq 0xd8(%rsp), %r8
incq %rbp
movslq 0x4c(%r14), %rax
cmpq %rax, %rbp
jl 0x1ab74
movq 0x50(%rsp), %rdx
incq %rdx
movq 0x38(%rsp), %rcx
movslq 0x24(%rcx), %rax
cmpq %rax, %rdx
movq %rcx, %rax
jl 0x1aabb
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq