// File: /workspace/github_cpp_makefile_star2_O2/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 0x2c6c4
movq %rdi, %r14
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rsi
leaq 0xedfa1(%rip), %rdi     # 0x11a500
movslq (%rdi,%rdx,4), %rdx
addq %rdi, %rdx
jmpq *%rdx
testq %r8, %r8
sete %al
testq %rcx, %rcx
sete %dl
orb %al, %dl
jne 0x2c786
cvtss2sd 0x8(%rcx), %xmm1
addsd 0x20(%r8), %xmm1
jmp 0x2c861
testq %rsi, %rsi
je 0x2c804
movl 0xe8(%rsi), %edx
jmp 0x2c806
testq %r9, %r9
je 0x2c804
movl 0x20(%r9), %edx
jmp 0x2c806
testq %r9, %r9
je 0x2c7a2
movl 0x20(%r9), %eax
incl %eax
jmp 0x2c7a4
testq %rcx, %rcx
je 0x2c7b6
movzbl 0x25(%rcx), %eax
cvtsi2sd %eax, %xmm0
divsd 0xedd91(%rip), %xmm0    # 0x11a370
jmp 0x2c82f
xorl %ebp, %ebp
testq %r9, %r9
je 0x2c5fe
pushq $0x1
popq %rsi
movq %r9, %rdi
callq 0x3d2fe
testq %rax, %rax
je 0x2c5fe
movl 0x10(%rax), %ebp
movq (%r14), %rax
movq 0x200(%rax), %rax
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
jmp 0x2c816
testq %rsi, %rsi
je 0x2c804
movl 0x10(%rsi), %edx
jmp 0x2c806
testq %rsi, %rsi
je 0x2c700
movq %rsi, %rdi
callq 0x432fe
jmp 0x2c6fb
testq %rcx, %rcx
je 0x2c7c0
movswl 0x10(%rcx), %eax
cvtsi2ss %eax, %xmm0
movss 0xede8b(%rip), %xmm1    # 0x11a4e0
mulss %xmm1, %xmm0
movss 0xede83(%rip), %xmm2    # 0x11a4e4
mulss %xmm2, %xmm0
movss 0xede7b(%rip), %xmm3    # 0x11a4e8
addss %xmm3, %xmm0
cvttss2si %xmm0, %eax
movb %al, (%rbx)
movswl 0x12(%rcx), %eax
cvtsi2ss %eax, %xmm0
mulss %xmm1, %xmm0
mulss %xmm2, %xmm0
addss %xmm3, %xmm0
cvttss2si %xmm0, %eax
movb %al, 0x1(%rbx)
movswl 0x14(%rcx), %eax
cvtsi2ss %eax, %xmm0
mulss %xmm1, %xmm0
mulss %xmm2, %xmm0
addss %xmm3, %xmm0
cvttss2si %xmm0, %eax
jmp 0x2c7ae
testq %r8, %r8
je 0x2c804
movl 0xa0(%r8), %edx
jmp 0x2c806
testq %rcx, %rcx
je 0x2c7ce
movb 0x2c(%rcx), %al
movb %al, (%rbx)
movb 0x2d(%rcx), %al
movb %al, 0x1(%rbx)
movb 0x2e(%rcx), %al
movb %al, 0x2(%rbx)
jmp 0x2c8a7
testq %rax, %rax
setne %cl
testq %rsi, %rsi
sete %dl
orb %cl, %dl
jne 0x2c6fb
movq %rsi, %rdi
callq 0x43340
testq %rax, %rax
jne 0x2c751
leaq 0x17ba81(%rip), %rax    # 0x1a8188
jmp 0x2c755
testq %rcx, %rcx
je 0x2c7dc
movswl 0x2a(%rcx), %eax
cvtsi2sd %eax, %xmm0
divsd 0xedc7e(%rip), %xmm0    # 0x11a3a0
cvtsd2ss %xmm0, %xmm1
xorpd %xmm0, %xmm0
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm1
jbe 0x2c82f
cvtss2sd %xmm1, %xmm0
jmp 0x2c7e4
testq %rcx, %rcx
je 0x2c7f3
movzbl 0x24(%rcx), %edx
jmp 0x2c806
addq $0x70, %rax
movsd 0xedc13(%rip), %xmm0    # 0x11a370
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 0x2c7ae
testq %r9, %r9
je 0x2c84b
movq %rsp, %r15
movq %r15, %rdi
movq %r9, %rsi
callq 0x32924
jmp 0x2c85b
xorl %eax, %eax
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, (%rbx)
movb %ah, 0x1(%rbx)
movb %al, 0x2(%rbx)
jmp 0x2c8a7
movsd 0xed852(%rip), %xmm0    # 0x11a010
jmp 0x2c82f
movw $0x8080, (%rbx)         # imm = 0x8080
movb $-0x80, 0x2(%rbx)
jmp 0x2c8a7
movw $0x7f7f, (%rbx)         # imm = 0x7F7F
movb $0x7f, 0x2(%rbx)
jmp 0x2c8a7
movsd 0xed824(%rip), %xmm0    # 0x11a008
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2c822
sqrtsd %xmm0, %xmm0
jmp 0x2c827
testq %rax, %rax
je 0x2c804
movq %rax, %rdi
callq 0x4405e
movl %eax, %edx
jmp 0x2c806
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 0xed7e1(%rip), %xmm0    # 0x11a010
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 %rsi, %rsi
je 0x2c8b2
movq %rsp, %r15
movq %r15, %rdi
callq 0x32942
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 0x2c88b
sqrtsd %xmm0, %xmm0
jmp 0x2c890
callq 0x27700
mulsd 0xed778(%rip), %xmm0    # 0x11a010
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 0x2c861