// File: /workspace/github_cpp_makefile_star2_O3/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, 0x38(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x80(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0xa9b1c
movl 0x9c(%r12), %eax
testl %eax, %eax
jle 0x254dd
movq %rbx, 0x58(%rsp)
xorl %r14d, %r14d
leaq 0xb0(%rsp), %rbp
xorpd %xmm3, %xmm3
movq 0x90(%r12), %rcx
movq (%rcx,%r14,8), %r15
movl 0x2c(%r15), %ecx
testl %ecx, %ecx
jle 0x2548e
cmpl $0x1, %ecx
je 0x2548e
cmpl $0x3, %ecx
jb 0x2548e
movq 0x20(%r15), %rcx
movq (%rcx), %r8
movq 0x8(%rcx), %rdi
movq 0x10(%rcx), %rsi
testq %r8, %r8
sete %cl
testq %rdi, %rdi
sete %dl
orb %cl, %dl
testq %rsi, %rsi
sete %cl
orb %dl, %cl
jne 0x2548e
movq %r8, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
movq %rsi, 0x60(%rsp)
movsd %xmm3, (%rsp)
leaq 0x30(%r15), %rsi
movq %rbp, %rbx
leaq 0xe0(%rsp), %rbp
movq %rbp, %rdi
callq 0x83dd2
leaq 0x78(%r15), %rsi
leaq 0xc8(%rsp), %r13
movq %r13, %rdi
callq 0x85490
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rbp
callq 0x8401e
movq %rbx, %rdi
movq %r13, %rsi
callq 0x84ae2
movapd %xmm0, %xmm1
movsd 0xb8166(%rip), %xmm0    # 0xdd398
ucomisd %xmm1, %xmm0
jae 0x25481
movsd %xmm1, 0x50(%rsp)
movq %rbp, %rdi
callq 0x84b6c
movq 0x38(%rsp), %rax
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x25281
jp 0x25281
movsd 0x8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jne 0x25281
jp 0x25281
movsd 0x10(%rax), %xmm0
ucomisd %xmm1, %xmm0
movsd 0xb7ecb(%rip), %xmm0    # 0xdd148
jne 0x25281
jnp 0x252ac
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x85490
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x84ae2
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x25481
movsd %xmm0, 0x8(%rsp)
movsd 0x30(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movsd 0xb7e84(%rip), %xmm0    # 0xdd148
jbe 0x252f5
movsd 0x90(%r15), %xmm0
subsd 0x30(%rsp), %xmm0
andpd 0xb80f3(%rip), %xmm0    # 0xdd3d0
movsd 0xb80b3(%rip), %xmm1    # 0xdd398
maxsd %xmm0, %xmm1
movsd 0xb7e57(%rip), %xmm0    # 0xdd148
divsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq 0x40(%rsp), %rdx
callq 0x3ce12
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3d3e4
testl %eax, %eax
je 0x25481
cmpl $0x4, 0x2c(%r15)
movsd (%rsp), %xmm3
movq 0x28(%rsp), %rdi
movsd 0x8(%rsp), %xmm2
movsd 0x50(%rsp), %xmm4
jl 0x253a4
movq 0x20(%r15), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x253a4
cvttsd2si 0x18(%rsp), %ecx
cvttsd2si 0x10(%rsp), %edx
movq 0xb8(%rax), %rsi
imull 0xc8(%rax), %ecx
addl %edx, %ecx
movslq %ecx, %rax
movsd (%rsi,%rax,8), %xmm0
leaq 0x10e890(%rip), %rax    # 0x133c08
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jae 0x25486
subsd %xmm4, %xmm0
andpd 0xb803e(%rip), %xmm0    # 0xdd3d0
divsd %xmm4, %xmm0
ucomisd 0xb7fda(%rip), %xmm0    # 0xdd378
ja 0x25486
movsd 0xb7d9c(%rip), %xmm0    # 0xdd148
divsd %xmm4, %xmm0
mulsd 0x48(%rsp), %xmm2
mulsd %xmm0, %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jae 0x25486
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd %xmm2, 0x8(%rsp)
callq 0x9205c
movsd %xmm0, 0x28(%rsp)
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movq 0x20(%rsp), %rdi
callq 0x9205c
movsd %xmm0, 0x20(%rsp)
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movq 0x60(%rsp), %rdi
callq 0x9205c
movaps %xmm0, %xmm2
leaq 0x68(%rsp), %rdi
movsd 0x28(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
callq 0xa9b1c
movsd 0x68(%rsp), %xmm0
movsd 0x8(%rsp), %xmm3
mulsd %xmm3, %xmm0
movsd 0x70(%rsp), %xmm1
mulsd %xmm3, %xmm1
movsd 0x78(%rsp), %xmm2
mulsd %xmm3, %xmm2
leaq 0x98(%rsp), %rbx
movq %rbx, %rdi
callq 0xa9b1c
leaq 0x80(%rsp), %rdi
movq %rbx, %rsi
callq 0xa9bce
movsd (%rsp), %xmm3
addsd 0x8(%rsp), %xmm3
jmp 0x25486
movsd (%rsp), %xmm3
movl 0x9c(%r12), %eax
incq %r14
movslq %eax, %rcx
cmpq %rcx, %r14
jl 0x25174
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
movq 0x58(%rsp), %rbx
jbe 0x254dd
movsd 0x80(%rsp), %xmm0
movsd 0x88(%rsp), %xmm1
divsd %xmm3, %xmm0
divsd %xmm3, %xmm1
movsd 0x90(%rsp), %xmm2
divsd %xmm3, %xmm2
movq %rbx, %rdi
callq 0xa9b1c
jmp 0x254ec
leaq 0x10e65c(%rip), %rsi    # 0x133b40
movq %rbx, %rdi
callq 0xa9afe
movq %rbx, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq