// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %rbx
subq $0x20, %rsp
movq %rsi, %rbx
addl $-0x2, %edx
cmpl $0xc, %edx
ja 0x2d5e3
movq %rdi, %r14
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rdi
leaq 0x10e062(%rip), %rsi    # 0x13b4f0
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
testq %r8, %r8
sete %al
testq %rcx, %rcx
sete %dl
orb %al, %dl
jne 0x2d658
cvtss2sd 0x8(%rcx), %xmm1
addsd 0x20(%r8), %xmm1
jmp 0x2d7ae
testq %rdi, %rdi
je 0x2d744
movl 0xe8(%rdi), %edx
jmp 0x2d746
testq %r9, %r9
je 0x2d744
movl 0x20(%r9), %edx
jmp 0x2d746
testq %r9, %r9
je 0x2d66e
movl 0x20(%r9), %eax
incl %eax
jmp 0x2d670
testq %rcx, %rcx
je 0x2d67c
movzbl 0x25(%rcx), %eax
cvtsi2sd %eax, %xmm0
divsd 0x10de62(%rip), %xmm0   # 0x13b370
jmp 0x2d772
xorl %r15d, %r15d
testq %r9, %r9
je 0x2d689
movq %r9, %rdi
movl $0x1, %esi
callq 0x40210
jmp 0x2d68b
testq %rdi, %rdi
je 0x2d744
movl 0x10(%rdi), %edx
jmp 0x2d746
testq %rdi, %rdi
je 0x2d6ac
callq 0x4708c
jmp 0x2d6ae
testq %rcx, %rcx
je 0x2d6f7
movswl 0x10(%rcx), %eax
cvtsi2ss %eax, %xmm0
movss 0x10df62(%rip), %xmm1   # 0x13b4d0
mulss %xmm1, %xmm0
movss 0x10df5a(%rip), %xmm2   # 0x13b4d4
mulss %xmm2, %xmm0
movss 0x10df52(%rip), %xmm3   # 0x13b4d8
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 0x2d6ef
testq %r8, %r8
je 0x2d744
movl 0xa0(%r8), %edx
jmp 0x2d746
testq %rcx, %rcx
je 0x2d705
movb 0x2c(%rcx), %al
movb %al, (%rbx)
movb 0x2d(%rcx), %al
movb %al, 0x1(%rbx)
movb 0x2e(%rcx), %al
movb %al, 0x2(%rbx)
jmp 0x2d7f4
testq %rax, %rax
setne %cl
testq %rdi, %rdi
sete %dl
orb %cl, %dl
jne 0x2d6ae
callq 0x47182
jmp 0x2d6ae
testq %rcx, %rcx
je 0x2d713
movswl 0x2a(%rcx), %eax
cvtsi2sd %eax, %xmm0
divsd 0x10dd67(%rip), %xmm0   # 0x13b3a0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm1
jmp 0x2d71b
testq %rcx, %rcx
je 0x2d733
movzbl 0x24(%rcx), %edx
jmp 0x2d746
testq %r9, %r9
je 0x2d78d
movq %r9, %rdi
callq 0x3ff5c
jmp 0x2d797
xorl %eax, %eax
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, (%rbx)
movb %ah, 0x1(%rbx)
jmp 0x2d6ef
movsd 0x10d98c(%rip), %xmm0   # 0x13b010
jmp 0x2d772
xorl %eax, %eax
testq %rax, %rax
je 0x2d694
movl 0x10(%rax), %r15d
movq (%r14), %rax
movq 0x200(%rax), %rax
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
jmp 0x2d756
xorl %eax, %eax
testq %rax, %rax
jne 0x2d6bc
leaq 0x192aae(%rip), %rax    # 0x1c0168
jmp 0x2d6c0
addq $0x70, %rax
movsd 0x10dca8(%rip), %xmm0   # 0x13b370
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
movb %al, 0x2(%rbx)
jmp 0x2d7f4
movw $0x8080, (%rbx)         # imm = 0x8080
movb $-0x80, 0x2(%rbx)
jmp 0x2d7f4
movw $0x7f7f, (%rbx)         # imm = 0x7F7F
movb $0x7f, 0x2(%rbx)
jmp 0x2d7f4
movsd 0x10d8ed(%rip), %xmm1   # 0x13b008
xorps %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x2d772
ucomisd %xmm0, %xmm1
jb 0x2d761
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x2d76a
testq %rax, %rax
je 0x2d744
movq %rax, %rdi
callq 0x47ff8
movl %eax, %edx
jmp 0x2d746
xorl %edx, %edx
movq (%r14), %rax
movq 0x200(%rax), %rax
movq %r14, %rdi
movq %rbx, %rsi
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
jmpq *%rax
movapd %xmm1, %xmm0
callq 0x27700
mulsd 0x10d89e(%rip), %xmm0   # 0x13b010
movq (%r14), %rax
movq 0x208(%rax), %rax
movq %r14, %rdi
movq %rbx, %rsi
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
jmpq *%rax
testq %rdi, %rdi
je 0x2d7fe
callq 0x46926
movq (%rax), %rcx
leaq 0x8(%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 0x2d7d8
sqrtsd %xmm0, %xmm0
jmp 0x2d7dd
callq 0x27700
mulsd 0x10d82b(%rip), %xmm0   # 0x13b010
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x208(%rax)
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq
xorpd %xmm1, %xmm1
jmp 0x2d7ae