// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::BilateralFilter(double, double)
void R3Grid::
BilateralFilter(RNLength grid_sigma, RNLength value_sigma)
{
  // Make copy of grid
  R3Grid copy(*this);

  // Determine reasonable value sigma
  if (value_sigma == -1) {
    RNInterval range = Range();
    value_sigma = 0.01 * (range.Max() - range.Min());
  }

  // Get convenient variables
  double grid_denom = -2.0 * grid_sigma * grid_sigma;
  double value_denom = -2.0 * value_sigma * value_sigma;
  RNScalar grid_radius = 3 * grid_sigma;
  int r = (int) (grid_radius + 1);
  int r_squared = r * r;

  // Set every sample to be median of surrounding region in input grid
  for (int cz = 0; cz < ZResolution(); cz++) {
    for (int cy = 0; cy < YResolution(); cy++) {
      for (int cx = 0; cx < XResolution(); cx++) {
        // Get current value
        RNScalar value = copy.GridValue(cx, cy, cz);

        // Compute new value
        RNScalar sum = 0;
        RNScalar weight = 0;
        int zmin = cz - r;
        int zmax = cz + r;
        if (zmin < 0) zmin = 0;
        if (zmax >= ZResolution()) zmax = ZResolution() - 1;
        for (int z = zmin; z <= zmax; z++) {
          int ymin = cy - r;
          int ymax = cy + r;
          if (ymin < 0) ymin = 0;
          if (ymax >= YResolution()) ymax = YResolution() - 1;
          int dz = z - cz;
          for (int y = ymin; y <= ymax; y++) {
            int xmin = cx - r;
            int xmax = cx + r;
            if (xmin < 0) xmin = 0;
            if (xmax >= XResolution()) xmax = XResolution() - 1;
            int dy = y - cy;
            for (int x = xmin; x <= xmax; x++) {
              int dx = x - cx;
              int grid_distance_squared = dx*dx + dy*dy + dz*dz;
              if (grid_distance_squared > r_squared) continue;
              RNScalar sample = copy.GridValue(x, y, z);
              RNScalar value_distance_squared = value - sample;
              value_distance_squared *= value_distance_squared;
              RNScalar w = exp(grid_distance_squared/grid_denom) * exp(value_distance_squared/value_denom);
              sum += w * sample;
              weight += w;
            }
          }
        }

        // Set new value
        if (weight > 0) SetGridValue(cx, cy, cz, sum / weight);
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %rdi, %rbx
leaq 0xa8(%rsp), %rdi
movq %rbx, %rsi
callq 0x2bbaa
movsd 0x8(%rsp), %xmm3
ucomisd 0x6572e(%rip), %xmm3    # 0x93020
jne 0x2d920
jp 0x2d920
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0x2bd3a
movsd 0xa0(%rsp), %xmm3
subsd 0x98(%rsp), %xmm3
mulsd 0x66640(%rip), %xmm3    # 0x93f60
movsd 0x658d8(%rip), %xmm0    # 0x93200
movsd 0x20(%rsp), %xmm2
movapd %xmm2, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, 0x68(%rsp)
mulsd %xmm3, %xmm0
mulsd 0x6588c(%rip), %xmm2    # 0x931d8
addsd 0x656b4(%rip), %xmm2    # 0x93008
mulsd %xmm3, %xmm0
movsd %xmm0, 0x70(%rsp)
cvttsd2si %xmm2, %eax
movq %rax, 0x18(%rsp)
movl %eax, %r12d
imull %eax, %r12d
xorl %esi, %esi
movq %rbx, 0x60(%rsp)
cmpl 0x240(%rbx), %esi
jge 0x2dc03
movl %esi, %ecx
movq 0x18(%rsp), %rax
subl %eax, %ecx
addl %esi, %eax
movl %eax, 0x28(%rsp)
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
movl %ecx, 0x2c(%rsp)
movq %rsi, 0x50(%rsp)
cmpl 0x23c(%rbx), %edx
jge 0x2dbfc
movl %edx, %ecx
movq 0x18(%rsp), %rax
subl %eax, %ecx
movq %rdx, 0x10(%rsp)
addl %edx, %eax
movl %eax, 0x30(%rsp)
xorl %r15d, %r15d
testl %ecx, %ecx
cmovlel %r15d, %ecx
movl %ecx, 0x34(%rsp)
xorl %edi, %edi
cmpl 0x238(%rbx), %edi
jge 0x2dbf0
movq 0x2d8(%rsp), %rax
movl 0x2f0(%rsp), %ecx
imull %esi, %ecx
movl 0x2ec(%rsp), %edx
imull 0x10(%rsp), %edx
addl %edi, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x78(%rsp)
movl 0x240(%rbx), %eax
leal -0x1(%rax), %edx
movl 0x28(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %edx
movl %edx, 0x38(%rsp)
movl %edi, %edx
movq 0x18(%rsp), %rax
subl %eax, %edx
testl %edx, %edx
movl $0x0, %ecx
cmovlel %ecx, %edx
movl %edx, 0x40(%rsp)
addl %edi, %eax
movl %eax, 0x3c(%rsp)
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movl 0x2c(%rsp), %ebp
movq %rdi, 0x58(%rsp)
cmpl 0x38(%rsp), %ebp
jg 0x2dbb1
movl 0x23c(%rbx), %eax
leal -0x1(%rax), %edx
movl 0x30(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %edx
movl %edx, 0x48(%rsp)
movl %ebp, %eax
subl %esi, %eax
imull %eax, %eax
movl %eax, 0x44(%rsp)
movl 0x34(%rsp), %r14d
cmpl 0x48(%rsp), %r14d
jg 0x2dba0
movl 0x238(%rbx), %eax
leal -0x1(%rax), %r13d
movl 0x3c(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %r13d
movl %r14d, %ecx
subl 0x10(%rsp), %ecx
imull %ecx, %ecx
addl 0x44(%rsp), %ecx
movl 0x40(%rsp), %eax
movl %eax, %ebx
movl %ecx, 0x4c(%rsp)
cmpl %r13d, %ebx
jg 0x2db93
leal (%r15,%rbx), %eax
imull %eax, %eax
addl %ecx, %eax
cmpl %r12d, %eax
ja 0x2db8c
movq 0x2d8(%rsp), %rcx
movl 0x2f0(%rsp), %edx
imull %ebp, %edx
movl 0x2ec(%rsp), %esi
imull %r14d, %esi
addl %edx, %esi
addl %ebx, %esi
movslq %esi, %rdx
movsd (%rcx,%rdx,8), %xmm2
movsd %xmm2, 0x90(%rsp)
movsd 0x78(%rsp), %xmm3
subsd %xmm2, %xmm3
mulsd %xmm3, %xmm3
divsd 0x70(%rsp), %xmm3
movsd %xmm3, 0x88(%rsp)
movsd %xmm0, 0x8(%rsp)
cvtsi2sd %eax, %xmm0
divsd 0x68(%rsp), %xmm0
movsd %xmm1, 0x20(%rsp)
callq 0xe820
movsd %xmm0, 0x80(%rsp)
movsd 0x88(%rsp), %xmm0
callq 0xe820
movl 0x4c(%rsp), %ecx
movsd 0x20(%rsp), %xmm1
mulsd 0x80(%rsp), %xmm0
movsd 0x90(%rsp), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm2, %xmm1
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
incl %ebx
jmp 0x2dab6
incl %r14d
movq 0x60(%rsp), %rbx
jmp 0x2da7f
incl %ebp
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdi
jmp 0x2da4f
ucomisd 0x6562f(%rip), %xmm0    # 0x931e8
jbe 0x2dbe6
divsd %xmm0, %xmm1
movq 0x230(%rbx), %rax
movl 0x248(%rbx), %ecx
imull %esi, %ecx
movl 0x244(%rbx), %edx
imull 0x10(%rsp), %edx
addl %edi, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movsd %xmm1, (%rax,%rcx,8)
incl %edi
decl %r15d
jmp 0x2d9cf
movq 0x10(%rsp), %rdx
incl %edx
jmp 0x2d9a0
incl %esi
jmp 0x2d975
leaq 0xa8(%rsp), %rdi
callq 0x2bc02
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x2bc02
movq %rbx, %rdi
callq 0xe810