// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::EstimateColor(gaps::R3SurfelScene const*, gaps::R3Point const&, gaps::R3Vector const&, double)
RNRgb
EstimateColor(const R3SurfelScene *scene,
  const R3Point& position, const R3Vector& normal,
  double timestamp)
{
  // Initialize color
  RNRgb total_color(0,0,0);
  double total_weight = 0;
 
  // Check all images
  for (int i = 0; i < scene->NImages(); i++) {
    R3SurfelImage *image = scene->Image(i);

    // Get/check color channels
    const R2Grid *red_channel = image->Channel(R3_SURFEL_RED_CHANNEL);
    const R2Grid *green_channel = image->Channel(R3_SURFEL_GREEN_CHANNEL);
    const R2Grid *blue_channel = image->Channel(R3_SURFEL_BLUE_CHANNEL);
    if (!red_channel || !green_channel || !blue_channel) continue;

    // Get useful variables
    R3Point viewpoint = image->Viewpoint();
    R3Vector towards = image->Towards();
    R3Vector vector = position - viewpoint;

    // Compute/check depth
    double depth = vector.Dot(towards);
    if (depth <= 1E-3) continue;
    vector.Normalize();

    // Compute/check N dot V
    double angle_weight = 1.0;
    if (!normal.IsZero()) {
      double ndotv = normal.Dot(-vector);
      if (ndotv <= 0) continue;
      angle_weight = ndotv;
    }

    // Compute/check timestamp difference
    double timestamp_weight = 1.0;
    if (timestamp > 0) {
      double image_timestamp = image->Timestamp();
      double timestamp_difference = fabs(image_timestamp - timestamp);
      if (timestamp_difference < 1E-3) timestamp_difference = 1E-3;
      timestamp_weight = 1.0 / timestamp_difference;
    }
   
    // Project 3D world position to 2D pixels
    R2Point image_position = image->ImagePosition(position);
    if (!image->ContainsImagePosition(image_position)) continue;

    // Check depth consistency
    const R2Grid *depth_channel = image->Channel(R3_SURFEL_DEPTH_CHANNEL);
    if (depth_channel) {
      RNScalar max_depth_difference_fraction = 0.1;
      RNScalar image_depth = image->PixelDepth(image_position.X(), image_position.Y());
      if (RNIsNegativeOrZero(image_depth)) continue;
      if (fabs(image_depth - depth) / depth > max_depth_difference_fraction) continue;
    }
    
    // Compute weight
    double depth_weight = 1.0 / depth;
    double weight = timestamp_weight * angle_weight * depth_weight;
    if (weight <= 0) continue;
   
    // Get color at the 2D pixel
    double r = red_channel->GridValue(image_position);
    double g = green_channel->GridValue(image_position);
    double b = blue_channel->GridValue(image_position);
    RNRgb color(r, g, b);
   
    // Add to weighted sums
    total_color += weight * color;
    total_weight += weight;
  }

  // Return weighted average color
  return (total_weight > 0) ? total_color / total_weight : RNgray_rgb;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movsd %xmm0, 0x30(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, %r12
movq %rdi, 0x28(%rsp)
leaq 0x68(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x9a626
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
movslq 0x9c(%r12), %rax
cmpq %rax, %r15
jge 0x23d20
movq 0x90(%r12), %rax
movq (%rax,%r15,8), %rbx
movl 0x2c(%rbx), %eax
testl %eax, %eax
jle 0x23a7a
movq 0x20(%rbx), %rcx
movq (%rcx), %rsi
cmpl $0x1, %eax
jne 0x23a80
xorl %edx, %edx
jmp 0x23a8f
xorl %edx, %edx
xorl %esi, %esi
jmp 0x23a8f
movq 0x8(%rcx), %rdx
cmpl $0x3, %eax
jb 0x23a8f
movq 0x10(%rcx), %r13
jmp 0x23a92
xorl %r13d, %r13d
testq %rsi, %rsi
sete %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
testq %r13, %r13
sete %al
orb %cl, %al
jne 0x23d18
movq %rsi, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movsd %xmm0, 0x8(%rsp)
movq %r12, %r14
leaq 0x30(%rbx), %rsi
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
callq 0x781cc
leaq 0x78(%rbx), %rsi
leaq 0xc8(%rsp), %rbp
movq %rbp, %rdi
callq 0x792e2
leaq 0xb0(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq %r12, %rdx
movq %rdi, %r12
callq 0x78399
movq %r12, %rdi
movq %rbp, %rsi
callq 0x78bf6
movapd %xmm0, %xmm1
movsd 0xa4878(%rip), %xmm0    # 0xc8390
ucomisd %xmm1, %xmm0
jae 0x23d0f
movsd %xmm1, 0x38(%rsp)
movq %r12, %rdi
callq 0x78c7e
movq 0x40(%rsp), %rdi
callq 0x28528
movsd 0xa4606(%rip), %xmm1    # 0xc8148
testl %eax, %eax
jne 0x23b78
leaq 0x80(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x792e2
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x78bf6
movapd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jae 0x23d0f
movsd %xmm1, 0x10(%rsp)
movsd 0x30(%rsp), %xmm0
ucomisd 0xa47f4(%rip), %xmm0    # 0xc8380
movsd 0xa45b4(%rip), %xmm0    # 0xc8148
movq %r14, %r12
jbe 0x23bc7
movsd 0x90(%rbx), %xmm0
subsd 0x30(%rsp), %xmm0
andpd 0xa4821(%rip), %xmm0    # 0xc83d0
movsd 0xa47d9(%rip), %xmm1    # 0xc8390
maxsd %xmm0, %xmm1
movsd 0xa4585(%rip), %xmm0    # 0xc8148
divsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
leaq 0x58(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq 0x48(%rsp), %rdx
callq 0x384d0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x38a9e
testl %eax, %eax
je 0x23d12
cmpl $0x4, 0x2c(%rbx)
jl 0x23c53
movq 0x20(%rbx), %rax
cmpq $0x0, 0x18(%rax)
je 0x23c53
cvttsd2si 0x58(%rsp), %esi
cvttsd2si 0x60(%rsp), %edx
movq %rbx, %rdi
callq 0x2855e
leaq 0x101007(%rip), %rax    # 0x124c28
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jae 0x23d12
movsd 0x38(%rsp), %xmm1
subsd %xmm1, %xmm0
andpd 0xa478f(%rip), %xmm0    # 0xc83d0
divsd %xmm1, %xmm0
ucomisd 0xa4723(%rip), %xmm0    # 0xc8370
ja 0x23d12
movsd 0xa44ed(%rip), %xmm0    # 0xc8148
divsd 0x38(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
mulsd 0x50(%rsp), %xmm1
mulsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jae 0x23d12
movq 0x18(%rsp), %rdi
leaq 0x58(%rsp), %rbx
movq %rbx, %rsi
movsd %xmm1, 0x10(%rsp)
callq 0x28592
movsd %xmm0, 0x18(%rsp)
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x28592
movsd %xmm0, 0x20(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x28592
movapd %xmm0, %xmm2
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
callq 0x9a626
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x289d9
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq 0x9a6d6
movsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
jmp 0x23d18
movq %r14, %r12
movsd 0x8(%rsp), %xmm0
incq %r15
jmp 0x23a46
movapd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x23d46
movapd %xmm1, %xmm0
leaq 0x68(%rsp), %rsi
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x285a0
jmp 0x23d5a
leaq 0x100e13(%rip), %rsi    # 0x124b60
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x9a608
movq %rbx, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq