// File: 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:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0x210(%rsp)
movq %rsi, 0x208(%rsp)
movq 0x210(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x16ce0
movq %rax, 0x200(%rsp)
cmpq $0x0, 0x200(%rsp)
jne 0x22491
jmp 0x22b3f
movq 0x200(%rsp), %rdi
callq 0x16d10
movq %rax, 0x1f8(%rsp)
cmpq $0x0, 0x1f8(%rsp)
jne 0x224b6
jmp 0x22b3f
movl $0x0, 0x1f4(%rsp)
movl 0x1f4(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0x200(%rsp), %rdi
callq 0x16cf0
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x22b3f
movq 0x200(%rsp), %rdi
movl 0x1f4(%rsp), %esi
callq 0x30a90
movq %rax, 0x1e8(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0x31660
cmpl $0x0, %eax
jle 0x2251a
jmp 0x22b29
cmpq $0x0, 0x1e8(%rsp)
je 0x22540
movq 0x1e8(%rsp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0x5f2c0
movq %rax, 0x40(%rsp)
jmp 0x22549
xorl %eax, %eax
movq %rax, 0x40(%rsp)
jmp 0x22549
movq 0x40(%rsp), %rax
movq %rax, 0x1e0(%rsp)
xorl %eax, %eax
cmpq $0x0, 0x1e0(%rsp)
movb %al, 0x3f(%rsp)
je 0x225b1
movq 0x1e0(%rsp), %rdi
callq 0x312c0
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, 0x3f(%rsp)
je 0x225b1
movq 0x1e0(%rsp), %rdi
callq 0x312c0
movq 0x50(%rsp), %rdi
movq %rax, 0x30(%rsp)
callq 0x31050
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
setne %al
movb %al, 0x3f(%rsp)
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x225bb
jmp 0x225d2
movq 0x1e0(%rsp), %rdi
callq 0x312c0
movq %rax, 0x1e0(%rsp)
jmp 0x22556
cmpq $0x0, 0x1e0(%rsp)
je 0x225f1
movq 0x1e0(%rsp), %rdi
callq 0x1a020
movq %rax, 0x28(%rsp)
jmp 0x225fa
xorl %eax, %eax
movq %rax, 0x28(%rsp)
jmp 0x225fa
movq 0x28(%rsp), %rax
movq %rax, 0x1d8(%rsp)
cmpq $0x0, 0x1e0(%rsp)
je 0x22625
movq 0x1e0(%rsp), %rdi
callq 0x1bf60
movl %eax, 0x24(%rsp)
jmp 0x22630
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x24(%rsp)
jmp 0x22630
movl 0x24(%rsp), %eax
movl %eax, 0x1d4(%rsp)
cmpq $0x0, 0x1d8(%rsp)
je 0x22659
movq 0x1d8(%rsp), %rdi
callq 0x70720
movl %eax, 0x20(%rsp)
jmp 0x22664
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x20(%rsp)
jmp 0x22664
movl 0x20(%rsp), %eax
movl %eax, 0x1d0(%rsp)
movl $0x0, 0x1cc(%rsp)
movl 0x1cc(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0x186b0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x22b27
movq 0x1e8(%rsp), %rdi
movl 0x1cc(%rsp), %esi
callq 0x186d0
movq %rax, 0x1c0(%rsp)
movq 0x1f8(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
callq 0x31140
cmpl $0x0, %eax
jne 0x226db
jmp 0x22b11
movl $0x0, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x1c0e0
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0x22afc
movq 0x1c0(%rsp), %rdi
movl 0x1bc(%rsp), %esi
callq 0x31680
movl %eax, 0x1b8(%rsp)
movq 0x1c0(%rsp), %rsi
movl 0x1bc(%rsp), %edx
leaq 0x1a0(%rsp), %rdi
callq 0x316c0
movq 0x1c0(%rsp), %rsi
movl 0x1bc(%rsp), %edx
leaq 0x188(%rsp), %rdi
callq 0x317c0
movq 0x1c0(%rsp), %rsi
movl 0x1bc(%rsp), %edx
leaq 0x170(%rsp), %rdi
callq 0x31800
movq 0x1c0(%rsp), %rsi
movl 0x1bc(%rsp), %edx
leaq 0x158(%rsp), %rdi
callq 0x318a0
leaq 0x140(%rsp), %rdi
leaq 0x170(%rsp), %rsi
leaq 0x158(%rsp), %rdx
callq 0xfa2f0
movq 0x1c0(%rsp), %rdi
movl 0x1bc(%rsp), %esi
xorl %edx, %edx
callq 0x31940
movsd %xmm0, 0x138(%rsp)
movq 0x1c0(%rsp), %rdi
movl 0x1bc(%rsp), %esi
movl $0x1, %edx
callq 0x31940
movsd %xmm0, 0x130(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x138(%rsp), %xmm0
jb 0x22814
movsd 0x181b8d(%rip), %xmm0   # 0x1a4398
movsd %xmm0, 0x138(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x130(%rsp), %xmm0
jb 0x22834
movsd 0x138(%rsp), %xmm0
movsd %xmm0, 0x130(%rsp)
movq 0x208(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x118(%rsp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x133960
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
leaq 0x1a0(%rsp), %rsi
leaq 0x170(%rsp), %rdx
leaq 0x188(%rsp), %rcx
leaq 0x118(%rsp), %r8
xorl %r9d, %r9d
callq *0x38(%rax)
movq %rax, 0x128(%rsp)
cmpq $0x0, 0x128(%rsp)
jne 0x2289a
jmp 0x22ae6
leaq 0xe0(%rsp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x132c0
movl $0x0, 0xdc(%rsp)
cmpl $0x6, 0xdc(%rsp)
jge 0x22a26
leaq 0x186a52(%rip), %rax    # 0x1a9320
movsd (%rax), %xmm0
cvtsi2sdl 0xdc(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x181ab9(%rip), %xmm1   # 0x1a43a0
divsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0xd0(%rsp), %xmm0
callq 0x13a30
movsd %xmm0, 0xc8(%rsp)
movsd 0xd0(%rsp), %xmm0
callq 0x13110
movsd %xmm0, 0xc0(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0xf8390
movsd 0xd0(%rsp), %xmm0
callq 0x13a30
mulsd 0x138(%rsp), %xmm0
leaq 0x90(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x31980
leaq 0xa8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xf8a90
movsd 0xd0(%rsp), %xmm0
callq 0x13110
mulsd 0x130(%rsp), %xmm0
leaq 0x78(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x31980
leaq 0xa8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xf8a90
movsd 0xc8(%rsp), %xmm0
movsd 0xc0(%rsp), %xmm1
leaq 0x68(%rsp), %rdi
callq 0x133960
movq 0x208(%rsp), %rdi
movq (%rdi), %rax
leaq 0xa8(%rsp), %rsi
leaq 0x170(%rsp), %rdx
leaq 0x188(%rsp), %rcx
leaq 0x68(%rsp), %r8
xorl %r9d, %r9d
callq *0x38(%rax)
movq %rax, %rcx
movslq 0xdc(%rsp), %rax
movq %rcx, 0xe0(%rsp,%rax,8)
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x228b9
movl $0x0, 0x64(%rsp)
cmpl $0x6, 0x64(%rsp)
jge 0x22ae4
movq 0x208(%rsp), %rdi
movq 0x128(%rsp), %rsi
movslq 0x64(%rsp), %rax
movq 0xe0(%rsp,%rax,8), %rax
movq %rax, 0x8(%rsp)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl $0x6, %ecx
cltd
idivl %ecx
movl %edx, %eax
movq 0x8(%rsp), %rdx
cltq
movq 0xe0(%rsp,%rax,8), %rcx
movq (%rdi), %rax
xorl %r8d, %r8d
callq *0x48(%rax)
movq %rax, 0x58(%rsp)
movq 0x208(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl 0x1b8(%rsp), %edx
callq 0x319c0
movq 0x208(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl 0x1d4(%rsp), %edx
callq 0x319e0
movq 0x208(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl 0x1d0(%rsp), %edx
callq 0x31a00
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x22a2e
jmp 0x22ae6
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x226e6
movq 0x1f8(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
callq 0x311c0
movl 0x1cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1cc(%rsp)
jmp 0x2267a
jmp 0x22b29
movl 0x1f4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1f4(%rsp)
jmp 0x224c1
addq $0x218, %rsp            # imm = 0x218
retq
nopw (%rax,%rax)