// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelImage.cpp
// Function: gaps::R3SurfelImage::RenderImage(gaps::R2Image*, gaps::R2Grid*, gaps::R2Grid*, gaps::R2Grid*, gaps::R2Grid*, gaps::R2Grid*, gaps::R2Grid*, gaps::R2Grid*, gaps::R2Grid*, gaps::R2Grid*) const
int R3SurfelImage::
RenderImage(R2Image *color_image,
  R2Grid *depth_image, R2Grid *height_image,
  R2Grid *xnormal_image, R2Grid *ynormal_image, R2Grid *znormal_image,
  R2Grid *label_image, R2Grid *object_image,
  R2Grid *node_image, R2Grid *block_image) const
{
  // Only for scans now
  R3SurfelScan *scan = Scan();
  if (!scan) return 0;
  R3SurfelNode *node = scan->Node();
  if (!node) return 0;
  
  // Initialize images
  R2Grid tmp_image(ImageWidth(), ImageHeight());
  tmp_image.Clear(R2_GRID_UNKNOWN_VALUE);
  if (color_image) *color_image = R2Image(ImageWidth(), ImageHeight());
  if (depth_image) *depth_image = tmp_image;
  if (height_image) *height_image = tmp_image;
  if (xnormal_image) *xnormal_image = tmp_image;
  if (ynormal_image) *ynormal_image = tmp_image;
  if (znormal_image) *znormal_image = tmp_image;
  if (label_image) *label_image = tmp_image;
  if (object_image) *object_image = tmp_image;
  if (node_image) *node_image = tmp_image;
  if (block_image) *block_image = tmp_image;

  // Check stuff
  if (!scene || !scene->Tree()) return 0;

  // Visit leaf nodes
  RNArray<const R3SurfelNode *> stack;
  stack.Insert(node);
  while (!stack.IsEmpty()) {
    const R3SurfelNode *decendent = stack.Tail();
    stack.RemoveTail();
    if (decendent->NParts() > 0) {
      // Visit decendent nodes
      for (int i = 0; i < decendent->NParts(); i++) {
        R3SurfelNode *part = decendent->Part(i);
        stack.Insert(part);
      }
    }
    else {
      // Render all blocks of leaf node
      for (int i = 0; i < decendent->NBlocks(); i++) {
        R3SurfelBlock *block = decendent->Block(i);
        R3SurfelNode *node = (block) ? block->Node() : NULL;
        R3SurfelObject *object = (node) ? node->Object(TRUE) : NULL;
        R3SurfelLabel *label = (object) ? object->CurrentLabel() : NULL;

        // Read block
        scene->Tree()->Database()->ReadBlock(block);

        // Render block
        for (int j = 0; j < block->NSurfels(); j++) {
          const R3Surfel *surfel = block->Surfel(j);

          // Get image position
          R2Point image_position = TransformFromWorldToImage(block->SurfelPosition(j));
          if (!ContainsImagePosition(image_position)) continue;
          int ix = (int) (image_position.X() + 0.5);
          int iy = (int) (image_position.Y() + 0.5);

          // Fill pixel at image index
          if (color_image) color_image->SetPixelRGB(ix, iy, block->SurfelColor(j));
          if (depth_image) depth_image->SetGridValue(ix, iy, (block->SurfelPosition(j) - Viewpoint()).Dot(Towards()));
          if (height_image) height_image->SetGridValue(ix, iy, block->SurfelPosition(j).Z() - Viewpoint().Z());
          if (xnormal_image) xnormal_image->SetGridValue(ix, iy, surfel->NX());
          if (ynormal_image) ynormal_image->SetGridValue(ix, iy, surfel->NY());
          if (znormal_image) znormal_image->SetGridValue(ix, iy, surfel->NZ());
          if (block_image) block_image->SetGridValue(ix, iy, block->DatabaseIndex());
          if (node_image && node) node_image->SetGridValue(ix, iy, node->TreeIndex());
          if (object_image && object) object_image->SetGridValue(ix, iy, object->SceneIndex());
          if (label_image && label) label_image->SetGridValue(ix, iy, label->SceneIndex());
        }

        // Release block
        scene->Tree()->Database()->ReleaseBlock(block);
      }
    }
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rcx, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x18(%rdi), %rax
xorl %r14d, %r14d
testq %rax, %rax
je 0x3e2bf
movq 0x18(%rax), %rbx
testq %rbx, %rbx
je 0x3e2bf
movq %r9, %r15
movq %r8, %r12
movq 0x18(%rsp), %rax
movl 0x98(%rax), %esi
movl 0x9c(%rax), %edx
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
callq 0x91e28
movsd 0xa17dd(%rip), %xmm0    # 0xdf2a0
movq %r14, %rdi
callq 0x93fda
cmpq $0x0, 0x30(%rsp)
je 0x3db0c
movq 0x18(%rsp), %rax
movl 0x98(%rax), %esi
movl 0x9c(%rax), %edx
leaq 0x60(%rsp), %rdi
movl $0x3, %ecx
callq 0xa438e
leaq 0x60(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0xa4694
leaq 0x60(%rsp), %rdi
callq 0xa45f6
cmpq $0x0, 0x28(%rsp)
je 0x3db26
leaq 0xb8(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x92794
cmpq $0x0, 0x20(%rsp)
je 0x3db40
leaq 0xb8(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x92794
testq %r12, %r12
je 0x3db55
leaq 0xb8(%rsp), %rsi
movq %r12, %rdi
callq 0x92794
testq %r15, %r15
je 0x3db6a
leaq 0xb8(%rsp), %rsi
movq %r15, %rdi
callq 0x92794
movq 0x1f0(%rsp), %rdi
testq %rdi, %rdi
je 0x3db84
leaq 0xb8(%rsp), %rsi
callq 0x92794
cmpq $0x0, 0x1f8(%rsp)
je 0x3dba4
leaq 0xb8(%rsp), %rsi
movq 0x1f8(%rsp), %rdi
callq 0x92794
cmpq $0x0, 0x200(%rsp)
je 0x3dbc4
leaq 0xb8(%rsp), %rsi
movq 0x200(%rsp), %rdi
callq 0x92794
cmpq $0x0, 0x208(%rsp)
je 0x3dbe4
leaq 0xb8(%rsp), %rsi
movq 0x208(%rsp), %rdi
callq 0x92794
movq 0x210(%rsp), %rdi
testq %rdi, %rdi
je 0x3dbfe
leaq 0xb8(%rsp), %rsi
callq 0x92794
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
xorl %r14d, %r14d
testq %rax, %rax
je 0x3e2b2
cmpq $0x0, 0x8(%rax)
je 0x3e2b2
leaq 0x38(%rsp), %rdi
callq 0xaaf34
movq %r12, 0x90(%rsp)
movq %r15, 0x98(%rsp)
leaq 0x38(%rsp), %rdi
movl 0xc(%rdi), %edx
movq %rbx, %rsi
callq 0xaafca
movl 0x44(%rsp), %eax
testl %eax, %eax
je 0x3e2a2
movq 0x18(%rsp), %rcx
leaq 0x30(%rcx), %rdx
movq %rdx, 0x80(%rsp)
addq $0x78, %rcx
movq %rcx, 0x78(%rsp)
leaq 0x60(%rsp), %r13
leaq 0x188(%rsp), %r12
movq 0x38(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %rbp
decl %esi
leaq 0x38(%rsp), %rdi
callq 0xab13e
cmpl $0x0, 0x3c(%rbp)
jle 0x3dccd
cmpl $0x0, 0x3c(%rbp)
jle 0x3e296
xorl %ebx, %ebx
movq 0x30(%rbp), %rax
movq (%rax,%rbx,8), %rsi
movl 0x44(%rsp), %edx
leaq 0x38(%rsp), %rdi
callq 0xaafca
incq %rbx
movslq 0x3c(%rbp), %rax
cmpq %rax, %rbx
jl 0x3dca6
jmp 0x3e296
cmpl $0x0, 0x4c(%rbp)
jle 0x3e296
xorl %ebx, %ebx
movq %rbp, 0x88(%rsp)
movq 0x40(%rbp), %rax
movq (%rax,%rbx,8), %r14
testq %r14, %r14
je 0x3dcf7
movq 0xb8(%r14), %r15
jmp 0x3dcfa
xorl %r15d, %r15d
testq %r15, %r15
je 0x3dd10
movq %r15, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x37a16
jmp 0x3dd12
xorl %eax, %eax
testq %rax, %rax
movq %rax, 0x58(%rsp)
je 0x3dd2b
movq %rax, %rdi
callq 0x1791a
movq %rax, 0x48(%rsp)
jmp 0x3dd34
movq $0x0, 0x48(%rsp)
cmpl $0x0, 0xb4(%r14)
jne 0x3dd66
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r14, %rsi
callq *0x88(%rax)
testl %eax, %eax
je 0x3dd6d
incl 0xb4(%r14)
movq %r15, 0x50(%rsp)
movq %rbx, 0xa0(%rsp)
cmpl $0x0, 0x8(%r14)
jle 0x3e207
cmpq $0x0, 0x58(%rsp)
setne %al
cmpq $0x0, 0x50(%rsp)
setne %cl
cmpq $0x0, 0x208(%rsp)
setne %sil
cmpq $0x0, 0x200(%rsp)
setne %dil
cmpq $0x0, 0x1f8(%rsp)
setne %dl
andb %cl, %sil
movb %sil, 0x17(%rsp)
andb %al, %dil
movb %dil, 0x16(%rsp)
cmpq $0x0, 0x48(%rsp)
setne %al
andb %dl, %al
movb %al, 0x15(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movq (%r14), %rbp
movq (%r14), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rbx), %xmm0
addsd 0x10(%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax,%rbx), %xmm1
addsd 0x18(%r14), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax,%rbx), %xmm2
addsd 0x20(%r14), %xmm2
movq %r13, %rdi
callq 0x84d7e
leaq 0xa8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
callq 0x3d1ba
movsd 0xa8(%rsp), %xmm0
movsd 0xa18a7(%rip), %xmm1    # 0xdf6e8
ucomisd %xmm0, %xmm1
ja 0x3e1f3
movsd 0xb0(%rsp), %xmm1
movsd 0xa188c(%rip), %xmm2    # 0xdf6e8
ucomisd %xmm1, %xmm2
ja 0x3e1f3
movq 0x18(%rsp), %rax
xorps %xmm2, %xmm2
cvtsi2sdl 0x98(%rax), %xmm2
addsd 0xa1872(%rip), %xmm2    # 0xdf6f0
ucomisd %xmm2, %xmm0
jae 0x3e1f3
movq 0x18(%rsp), %rax
xorps %xmm2, %xmm2
cvtsi2sdl 0x9c(%rax), %xmm2
addsd 0xa1850(%rip), %xmm2    # 0xdf6f0
ucomisd %xmm2, %xmm1
jae 0x3e1f3
movsd 0xa028e(%rip), %xmm1    # 0xde140
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x10(%rsp)
movsd 0xb0(%rsp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xc(%rsp)
cmpq $0x0, 0x30(%rsp)
je 0x3df33
movq (%r14), %rax
movzbl 0x2c(%rax,%rbx), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0xa047e(%rip), %xmm3    # 0xde370
mulsd %xmm3, %xmm0
movzbl 0x2d(%rax,%rbx), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd %xmm3, %xmm1
movzbl 0x2e(%rax,%rbx), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm3, %xmm2
movq %r13, %rdi
callq 0xaada8
movq 0x30(%rsp), %rdi
movl 0x10(%rsp), %esi
movl 0xc(%rsp), %edx
movq %r13, %rcx
callq 0xa473e
cmpq $0x0, 0x28(%rsp)
je 0x3dfd3
movq (%r14), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rbx), %xmm0
addsd 0x10(%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax,%rbx), %xmm1
addsd 0x18(%r14), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax,%rbx), %xmm2
addsd 0x20(%r14), %xmm2
leaq 0x1a0(%rsp), %r13
movq %r13, %rdi
callq 0x84d7e
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
movq 0x80(%rsp), %rdx
callq 0x84fe0
movq %r12, %rdi
movq 0x78(%rsp), %rsi
callq 0x864ac
leaq 0x60(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x85aae
movq 0x28(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull 0xc(%rsp), %ecx
addl 0x10(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpq $0x0, 0x20(%rsp)
je 0x3e045
movq (%r14), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rbx), %xmm0
addsd 0x10(%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax,%rbx), %xmm1
addsd 0x18(%r14), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax,%rbx), %xmm2
addsd 0x20(%r14), %xmm2
movq %r13, %rdi
callq 0x84d7e
movsd 0x70(%rsp), %xmm0
movq 0x18(%rsp), %rax
subsd 0x40(%rax), %xmm0
movq 0x20(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull 0xc(%rsp), %ecx
addl 0x10(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x90(%rsp), %rcx
testq %rcx, %rcx
je 0x3e088
movswl 0x10(%rbp,%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0xa039a(%rip), %xmm0    # 0xde400
cvtss2sd %xmm0, %xmm0
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull 0xc(%rsp), %ecx
addl 0x10(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x98(%rsp), %rcx
testq %rcx, %rcx
je 0x3e0cb
movswl 0x12(%rbp,%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0xa0357(%rip), %xmm0    # 0xde400
cvtss2sd %xmm0, %xmm0
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull 0xc(%rsp), %ecx
addl 0x10(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x1f0(%rsp), %rcx
testq %rcx, %rcx
je 0x3e10e
movswl 0x14(%rbp,%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0xa0314(%rip), %xmm0    # 0xde400
cvtss2sd %xmm0, %xmm0
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull 0xc(%rsp), %ecx
addl 0x10(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x210(%rsp), %rcx
testq %rcx, %rcx
je 0x3e145
xorps %xmm0, %xmm0
cvtsi2sdl 0xa0(%r14), %xmm0
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull 0xc(%rsp), %ecx
addl 0x10(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpb $0x0, 0x17(%rsp)
je 0x3e17f
movq 0x50(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sdl 0x20(%rax), %xmm0
movq 0x208(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull 0xc(%rsp), %ecx
addl 0x10(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpb $0x0, 0x16(%rsp)
je 0x3e1b9
movq 0x58(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sdl 0x10(%rax), %xmm0
movq 0x200(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull 0xc(%rsp), %ecx
addl 0x10(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpb $0x0, 0x15(%rsp)
je 0x3e1f3
movq 0x48(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sdl 0x10(%rax), %xmm0
movq 0x1f8(%rsp), %rax
movl 0xc(%rsp), %ecx
imull 0xc8(%rax), %ecx
movq 0xb8(%rax), %rax
addl 0x10(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
incq %r15
movslq 0x8(%r14), %rax
addq $0x30, %rbx
cmpq %rax, %r15
jl 0x3dde1
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %r15
cmpl $0x1, 0xb4(%r14)
movq 0x88(%rsp), %rbp
movq 0xa0(%rsp), %rbx
jne 0x3e24d
movq (%r15), %rax
movq 0x8(%r15), %rdx
movl 0x28(%r15), %ecx
movq %r15, %rdi
movq %r14, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x3e286
decl 0xb4(%r14)
jne 0x3e286
testb $0x2, 0x89(%r14)
je 0x3e286
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x18(%rax)
testq %r14, %r14
je 0x3e286
movq %r14, %rdi
callq 0x2ce2e
movl $0xc8, %esi
movq %r14, %rdi
callq 0x134f0
incq %rbx
movslq 0x4c(%rbp), %rax
cmpq %rax, %rbx
jl 0x3dce1
movl 0x44(%rsp), %eax
testl %eax, %eax
jne 0x3dc7b
leaq 0x38(%rsp), %rdi
callq 0xaaf9e
movl $0x1, %r14d
leaq 0xb8(%rsp), %rdi
callq 0x92a48
movl %r14d, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3e2fe
jmp 0x3e2e9
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0xa45f6
jmp 0x3e30b
jmp 0x3e2e9
movq %rax, %rbx
jmp 0x3e30b
jmp 0x3e2fe
jmp 0x3e2fe
jmp 0x3e2fe
jmp 0x3e2fe
jmp 0x3e2fe
jmp 0x3e2fe
jmp 0x3e2fe
jmp 0x3e2fe
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0xaaf9e
leaq 0xb8(%rsp), %rdi
callq 0x92a48
movq %rbx, %rdi
callq 0x138b0