// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Utils/R3SurfelViewer.cpp
// Function: gaps::R3SurfelViewer::CreateColor(unsigned char*, int, gaps::R3Surfel const*, gaps::R3SurfelBlock*, gaps::R3SurfelNode*, gaps::R3SurfelObject*, gaps::R3SurfelLabel*) const
void R3SurfelViewer::
CreateColor(unsigned char *color, int color_scheme,
  const R3Surfel *surfel, R3SurfelBlock *block, R3SurfelNode *node,
  R3SurfelObject *object, R3SurfelLabel *label) const
{
  // Check color scheme
  switch (color_scheme) {    
  case R3_SURFEL_VIEWER_COLOR_BY_PICK_INDEX: {
    int node_index = (node) ? node->TreeIndex() + 1 : 0;
    *color++ = (node_index >> 16) & 0xFF;
    *color++ = (node_index >> 8) & 0xFF;
    *color++ = node_index & 0xFF;
    break; }

  case R3_SURFEL_VIEWER_COLOR_BY_CURRENT_LABEL: {
    if (!label && object) label = object->CurrentLabel();
    const RNRgb& rgb = (label) ? label->Color() : RNwhite_rgb;
    *color++ = 255 * rgb.R();
    *color++ = 255 * rgb.G();
    *color++ = 255 * rgb.B();
    break; }
    
  case R3_SURFEL_VIEWER_COLOR_BY_GROUND_TRUTH_LABEL: {
    R3SurfelLabel *gtlabel = (object) ? object->GroundTruthLabel() : NULL;
    const RNRgb& rgb = (gtlabel) ? gtlabel->Color() : RNwhite_rgb;
    *color++ = 255 * rgb.R();
    *color++ = 255 * rgb.G();
    *color++ = 255 * rgb.B();
    break; }
    
  case R3_SURFEL_VIEWER_COLOR_BY_SCAN: {
    R3SurfelScan *scan = (node) ? node->Scan() : NULL;
    int scan_index = (scan) ? scan->SceneIndex() : 0;
    CreateColor(color, scan_index);
    break; }
    
  case R3_SURFEL_VIEWER_COLOR_BY_OBJECT: {
    int object_index = (object) ? object->SceneIndex() : 0;
    CreateColor(color, object_index);
    break; }

  case R3_SURFEL_VIEWER_COLOR_BY_NODE: {
    int node_index = (node) ? node->TreeIndex() : 0;
    CreateColor(color, node_index);
    break; }
    
  case R3_SURFEL_VIEWER_COLOR_BY_BLOCK: {
    int block_index = (block) ? block->DatabaseIndex() : 0;
    CreateColor(color, block_index);
    break; }
      
  case R3_SURFEL_VIEWER_COLOR_BY_NORMAL:
    if (surfel) {
      *color++ = 128 + 127 * surfel->NX();
      *color++ = 128 + 127 * surfel->NY();
      *color++ = 128 + 127 * surfel->NZ();
    }
    else {
      *color++ = 128;
      *color++ = 128;
      *color++ = 128;
    }
    break;

  case R3_SURFEL_VIEWER_COLOR_BY_Z: {
    double z = 0;
    if (block && surfel) z = block->PositionOrigin().Z() + surfel->Z();
    else if (node) z = node->Centroid().Z();
    else if (object) z = object->Centroid().Z();
    double dz = (z > center_point.Z()) ? z - center_point.Z() : 0;
    double value = 0.5 * sqrt(dz);
    CreateColor(color, value);
    break; }
      
  case R3_SURFEL_VIEWER_COLOR_BY_ELEVATION: {
    double elevation = 1;
    if (surfel) elevation = surfel->Elevation();
    double value = (elevation > 0) ? 0.5 * sqrt(elevation) : 0;
    CreateColor(color, value);
    break; }

  case R3_SURFEL_VIEWER_COLOR_BY_SURFEL_LABEL: {
    int label_identifier = 0;
    if (surfel) label_identifier = surfel->Attribute() & 0xFF;
    else if (label) label_identifier = label->Identifier();
    CreateColor(color, label_identifier);
    break; }

  case R3_SURFEL_VIEWER_COLOR_BY_CONFIDENCE: {
    double confidence = 0.5;
    if (surfel) confidence = ((surfel->Attribute() >> 8) & 0xFF) / 255.0;
    CreateColor(color, confidence);
    break; }

  case R3_SURFEL_VIEWER_COLOR_BY_OBJECT_ATTRIBUTES: {
    RNFlags flags = (object) ? object->Flags() : RNFlags(0);
    CreateColor(color, (int) flags);
    break; }
      
  default:
    if (surfel) {
      *color++ = surfel->R();
      *color++ = surfel->G();
      *color++ = surfel->B();
    }
    else {
      *color++ = 127;
      *color++ = 127;
      *color++ = 127;
    }
    break;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
addl $-0x2, %edx
cmpl $0xc, %edx
ja 0x2d4c3
movq %rdi, %r14
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rdi
leaq 0x10b1f7(%rip), %rsi    # 0x138550
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
testq %r8, %r8
sete %al
testq %rcx, %rcx
sete %dl
orb %al, %dl
jne 0x2d585
cvtss2sd 0x8(%rcx), %xmm1
addsd 0x20(%r8), %xmm1
jmp 0x2d663
testq %rdi, %rdi
je 0x2d5fd
movl 0xe8(%rdi), %edx
jmp 0x2d5ff
testq %r9, %r9
je 0x2d5fd
movl 0x20(%r9), %edx
jmp 0x2d5ff
testq %r9, %r9
je 0x2d59b
movl 0x20(%r9), %eax
incl %eax
jmp 0x2d59d
testq %rcx, %rcx
je 0x2d5af
movzbl 0x25(%rcx), %eax
cvtsi2sd %eax, %xmm0
divsd 0x10af97(%rip), %xmm0   # 0x138370
jmp 0x2d628
xorl %ebp, %ebp
testq %r9, %r9
je 0x2d3fa
movq %r9, %rdi
movl $0x1, %esi
callq 0x40278
testq %rax, %rax
je 0x2d3fa
movl 0x10(%rax), %ebp
movq (%r14), %rax
movq 0x200(%rax), %rax
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
jmp 0x2d60f
testq %rdi, %rdi
je 0x2d5fd
movl 0x10(%rdi), %edx
jmp 0x2d5ff
testq %rdi, %rdi
je 0x2d4fc
callq 0x47054
jmp 0x2d4f7
testq %rcx, %rcx
je 0x2d5b9
movswl 0x10(%rcx), %eax
cvtsi2ss %eax, %xmm0
movss 0x10b0e2(%rip), %xmm1   # 0x138530
mulss %xmm1, %xmm0
movss 0x10b0da(%rip), %xmm2   # 0x138534
mulss %xmm2, %xmm0
movss 0x10b0d2(%rip), %xmm3   # 0x138538
addss %xmm3, %xmm0
cvttss2si %xmm0, %eax
movb %al, (%rbx)
movswl 0x12(%rcx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss %xmm1, %xmm0
mulss %xmm2, %xmm0
addss %xmm3, %xmm0
cvttss2si %xmm0, %eax
movb %al, 0x1(%rbx)
movswl 0x14(%rcx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss %xmm1, %xmm0
mulss %xmm2, %xmm0
addss %xmm3, %xmm0
cvttss2si %xmm0, %eax
jmp 0x2d5a7
testq %r8, %r8
je 0x2d5fd
movl 0xa0(%r8), %edx
jmp 0x2d5ff
testq %rcx, %rcx
je 0x2d5c7
movb 0x2c(%rcx), %al
movb %al, (%rbx)
movb 0x2d(%rcx), %al
movb %al, 0x1(%rbx)
movb 0x2e(%rcx), %al
movb %al, 0x2(%rbx)
jmp 0x2d6a9
testq %rax, %rax
setne %cl
testq %rdi, %rdi
sete %dl
orb %cl, %dl
jne 0x2d4f7
callq 0x4713e
testq %rax, %rax
jne 0x2d550
leaq 0x18fc65(%rip), %rax    # 0x1bd168
jmp 0x2d554
testq %rcx, %rcx
je 0x2d5d5
movswl 0x2a(%rcx), %eax
cvtsi2sd %eax, %xmm0
divsd 0x10ae82(%rip), %xmm0   # 0x1383a0
cvtsd2ss %xmm0, %xmm1
xorpd %xmm0, %xmm0
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm1
jbe 0x2d628
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
jmp 0x2d5dd
testq %rcx, %rcx
je 0x2d5ec
movzbl 0x24(%rcx), %edx
jmp 0x2d5ff
addq $0x70, %rax
movsd 0x10ae14(%rip), %xmm0   # 0x138370
movsd (%rax), %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
movb %cl, (%rbx)
movsd 0x8(%rax), %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
movb %cl, 0x1(%rbx)
mulsd 0x10(%rax), %xmm0
cvttsd2si %xmm0, %eax
jmp 0x2d5a7
testq %r9, %r9
je 0x2d644
movq %r9, %rdi
callq 0x4003c
jmp 0x2d64e
xorl %eax, %eax
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, (%rbx)
movb %ah, 0x1(%rbx)
movb %al, 0x2(%rbx)
jmp 0x2d6a9
movsd 0x10aa59(%rip), %xmm0   # 0x138010
jmp 0x2d628
movw $0x8080, (%rbx)         # imm = 0x8080
movb $-0x80, 0x2(%rbx)
jmp 0x2d6a9
movw $0x7f7f, (%rbx)         # imm = 0x7F7F
movb $0x7f, 0x2(%rbx)
jmp 0x2d6a9
movsd 0x10aa2b(%rip), %xmm0   # 0x138008
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2d61b
sqrtsd %xmm0, %xmm0
jmp 0x2d620
testq %rax, %rax
je 0x2d5fd
movq %rax, %rdi
callq 0x47f8e
movl %eax, %edx
jmp 0x2d5ff
xorl %edx, %edx
movq (%r14), %rax
movq 0x200(%rax), %rax
movq %r14, %rdi
movq %rbx, %rsi
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmpq *%rax
callq 0x27700
mulsd 0x10a9e8(%rip), %xmm0   # 0x138010
movq (%r14), %rax
movq 0x208(%rax), %rax
movq %r14, %rdi
movq %rbx, %rsi
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmpq *%rax
testq %rdi, %rdi
je 0x2d6b4
callq 0x46944
movq (%rax), %rcx
movq %rsp, %r15
movq %r15, %rdi
movq %rax, %rsi
callq *0x68(%rcx)
movsd 0x10(%r15), %xmm1
movsd 0x2c0(%r14), %xmm0
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
cmpltsd %xmm1, %xmm0
andpd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2d68d
sqrtsd %xmm0, %xmm0
jmp 0x2d692
callq 0x27700
mulsd 0x10a976(%rip), %xmm0   # 0x138010
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x208(%rax)
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm1, %xmm1
jmp 0x2d663