// File: /workspace/github_cpp_makefile_star2_O2/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 0x18(%rdi), %rax
movl $0x0, 0xc(%rsp)
testq %rax, %rax
je 0x393f6
movq %r8, 0x48(%rsp)
movq %r9, 0x50(%rsp)
movq 0x18(%rax), %rbx
testq %rbx, %rbx
je 0x393f6
movq %rdi, %r12
movl 0x98(%rdi), %esi
movl 0x9c(%rdi), %edx
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
callq 0x83b64
movsd 0x904d5(%rip), %xmm0    # 0xc9290
movq %r14, %rdi
callq 0x85ab4
cmpq $0x0, 0x30(%rsp)
je 0x38e01
movl 0x98(%r12), %esi
movl 0x9c(%r12), %edx
leaq 0x68(%rsp), %rdi
pushq $0x3
popq %rcx
callq 0x945c2
leaq 0x68(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0x948a2
leaq 0x68(%rsp), %rdi
callq 0x94804
cmpq $0x0, 0x28(%rsp)
je 0x38e1b
leaq 0xb8(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x84412
cmpq $0x0, 0x20(%rsp)
je 0x38e35
leaq 0xb8(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x84412
cmpq $0x0, 0x48(%rsp)
je 0x38e4f
leaq 0xb8(%rsp), %rsi
movq 0x48(%rsp), %rdi
callq 0x84412
cmpq $0x0, 0x50(%rsp)
je 0x38e69
leaq 0xb8(%rsp), %rsi
movq 0x50(%rsp), %rdi
callq 0x84412
movq 0x1f0(%rsp), %rdi
testq %rdi, %rdi
je 0x38e83
leaq 0xb8(%rsp), %rsi
callq 0x84412
cmpq $0x0, 0x1f8(%rsp)
je 0x38ea3
leaq 0xb8(%rsp), %rsi
movq 0x1f8(%rsp), %rdi
callq 0x84412
cmpq $0x0, 0x200(%rsp)
je 0x38ec3
leaq 0xb8(%rsp), %rsi
movq 0x200(%rsp), %rdi
callq 0x84412
cmpq $0x0, 0x208(%rsp)
je 0x38ee3
leaq 0xb8(%rsp), %rsi
movq 0x208(%rsp), %rdi
callq 0x84412
movq 0x210(%rsp), %rdi
testq %rdi, %rdi
je 0x38efd
leaq 0xb8(%rsp), %rsi
callq 0x84412
movq 0x8(%r12), %rax
movl $0x0, 0xc(%rsp)
testq %rax, %rax
je 0x393e9
cmpq $0x0, 0x8(%rax)
je 0x393e9
leaq 0x58(%rsp), %rdi
callq 0x9a7ae
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x1784c
leaq 0x30(%r12), %rax
movq %rax, 0xa0(%rsp)
leaq 0x78(%r12), %rax
movq %rax, 0x98(%rsp)
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
movq %r12, 0x80(%rsp)
movslq 0x64(%rsp), %rax
testq %rax, %rax
je 0x393df
movq 0x58(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r14
leaq 0x58(%rsp), %rdi
callq 0x17820
movl 0x3c(%r14), %eax
testl %eax, %eax
jle 0x38fac
xorl %ebx, %ebx
cltq
cmpq %rax, %rbx
jge 0x38f5e
movq 0x30(%r14), %rax
movq (%rax,%rbx,8), %rsi
leaq 0x58(%rsp), %rdi
callq 0x1784c
incq %rbx
movl 0x3c(%r14), %eax
jmp 0x38f8a
xorl %ecx, %ecx
movq %r14, 0x88(%rsp)
movslq 0x4c(%r14), %rax
cmpq %rax, %rcx
jge 0x38f5e
movq 0x40(%r14), %rax
movq (%rax,%rcx,8), %r14
testq %r14, %r14
movq %rcx, 0x90(%rsp)
je 0x39014
movq 0xb8(%r14), %rbx
testq %rbx, %rbx
je 0x39014
movq %rbx, %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x338b4
movq %rbx, 0x40(%rsp)
testq %rax, %rax
je 0x393cc
movq %rax, 0x18(%rsp)
movq %rax, %rdi
callq 0x16d9c
movq %rax, 0x10(%rsp)
movb $0x1, %bl
movb $0x1, %r15b
jmp 0x39034
xorl %ebx, %ebx
movq $0x0, 0x18(%rsp)
movq $0x0, 0x40(%rsp)
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
movq 0x8(%r12), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
movq %r14, %rsi
callq 0x20a64
cmpq $0x0, 0x208(%rsp)
setne %cl
cmpq $0x0, 0x200(%rsp)
setne %dl
cmpq $0x0, 0x1f8(%rsp)
setne %al
andb %r15b, %cl
movb %cl, 0xb(%rsp)
andb %bl, %dl
movb %dl, 0xa(%rsp)
cmpq $0x0, 0x10(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0x9(%rsp)
pushq $0x14
popq %rbp
movq %rbp, 0x38(%rsp)
xorl %r15d, %r15d
movslq 0x8(%r14), %rax
cmpq %rax, %r15
leaq 0x68(%rsp), %rbx
jge 0x3939f
movq (%r14), %r13
movq %rbx, %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0x20b18
leaq 0xa8(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x384d0
movq %r12, %rdi
movq %rbp, %rsi
callq 0x38a9e
testl %eax, %eax
je 0x39389
movsd 0xa8(%rsp), %xmm0
movsd 0x8f047(%rip), %xmm1    # 0xc8138
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r12d
movsd 0xb0(%rsp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebx
cmpq $0x0, 0x30(%rsp)
je 0x39138
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0x20b50
movq 0x30(%rsp), %rdi
movl %r12d, %esi
movl %ebx, %edx
movq %rbp, %rcx
callq 0x94950
cmpq $0x0, 0x28(%rsp)
je 0x391b0
leaq 0x1a0(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0x20b18
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
movq 0xa0(%rsp), %rdx
callq 0x78399
leaq 0x188(%rsp), %rbp
movq %rbp, %rdi
movq 0x98(%rsp), %rsi
callq 0x792e2
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x78bf6
movq 0x28(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %ebx, %ecx
addl %r12d, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpq $0x0, 0x20(%rsp)
je 0x391fb
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0x20b18
movsd 0x78(%rsp), %xmm0
movq 0x80(%rsp), %rax
subsd 0x40(%rax), %xmm0
movq 0x20(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %ebx, %ecx
addl %r12d, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x48(%rsp), %rcx
testq %rcx, %rcx
je 0x3923b
movq 0x38(%rsp), %rax
movswl -0x4(%r13,%rax), %eax
cvtsi2ss %eax, %xmm0
mulss 0x8f1e4(%rip), %xmm0    # 0xc8400
cvtss2sd %xmm0, %xmm0
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %ebx, %ecx
addl %r12d, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x50(%rsp), %rcx
testq %rcx, %rcx
je 0x3927b
movq 0x38(%rsp), %rax
movswl -0x2(%r13,%rax), %eax
cvtsi2ss %eax, %xmm0
mulss 0x8f1a4(%rip), %xmm0    # 0xc8400
cvtss2sd %xmm0, %xmm0
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %ebx, %ecx
addl %r12d, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x1f0(%rsp), %rcx
testq %rcx, %rcx
je 0x392be
movq 0x38(%rsp), %rax
movswl (%r13,%rax), %eax
cvtsi2ss %eax, %xmm0
mulss 0x8f161(%rip), %xmm0    # 0xc8400
cvtss2sd %xmm0, %xmm0
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %ebx, %ecx
addl %r12d, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x210(%rsp), %rcx
testq %rcx, %rcx
je 0x392ef
cvtsi2sdl 0xa0(%r14), %xmm0
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %ebx, %ecx
addl %r12d, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpb $0x0, 0xb(%rsp)
je 0x39323
movq 0x40(%rsp), %rax
cvtsi2sdl 0x20(%rax), %xmm0
movq 0x208(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %ebx, %ecx
addl %r12d, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpb $0x0, 0xa(%rsp)
je 0x39357
movq 0x18(%rsp), %rax
cvtsi2sdl 0x10(%rax), %xmm0
movq 0x200(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %ebx, %ecx
addl %r12d, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpb $0x0, 0x9(%rsp)
je 0x39389
movq 0x10(%rsp), %rax
cvtsi2sdl 0x10(%rax), %xmm0
movq 0x1f8(%rsp), %rax
imull 0xc8(%rax), %ebx
movq 0xb8(%rax), %rax
addl %r12d, %ebx
movslq %ebx, %rcx
movsd %xmm0, (%rax,%rcx,8)
incq %r15
addq $0x30, 0x38(%rsp)
movq 0x80(%rsp), %r12
jmp 0x39094
movq 0x8(%r12), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
movq %r14, %rsi
callq 0x20aa0
movq 0x90(%rsp), %rcx
incq %rcx
movq 0x88(%rsp), %r14
jmp 0x38fb6
movb $0x1, %r15b
xorl %ebx, %ebx
movq $0x0, 0x18(%rsp)
jmp 0x3902b
leaq 0x58(%rsp), %rdi
callq 0x9a81c
leaq 0xb8(%rsp), %rdi
callq 0x846a8
movl 0xc(%rsp), %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x39436
jmp 0x39421
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x94804
jmp 0x39443
jmp 0x39421
movq %rax, %rbx
jmp 0x39443
jmp 0x39436
jmp 0x39436
jmp 0x39436
jmp 0x39436
jmp 0x39436
jmp 0x39436
jmp 0x39436
jmp 0x39436
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x9a81c
leaq 0xb8(%rsp), %rdi
callq 0x846a8
movq %rbx, %rdi
callq 0x138f0