// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %rdi, %rbx
leaq 0xa8(%rsp), %rdi
movq %rbx, %rsi
callq 0x2ee42
movsd 0x10(%rsp), %xmm3
ucomisd 0x70f64(%rip), %xmm3    # 0xa2048
jne 0x31112
jp 0x31112
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0x2efe2
movsd 0xa0(%rsp), %xmm3
subsd 0x98(%rsp), %xmm3
mulsd 0x71e9e(%rip), %xmm3    # 0xa2fb0
movl 0x240(%rbx), %ecx
testl %ecx, %ecx
jle 0x31466
movsd 0x710d8(%rip), %xmm0    # 0xa2200
movsd 0x28(%rsp), %xmm2
movapd %xmm2, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, 0x68(%rsp)
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm0
movsd %xmm0, 0x70(%rsp)
mulsd 0x71082(%rip), %xmm2    # 0xa21d8
addsd 0x70eda(%rip), %xmm2    # 0xa2038
cvttsd2si %xmm2, %eax
movq %rax, 0x20(%rsp)
movl %eax, %r12d
imull %eax, %r12d
movl 0x23c(%rbx), %eax
movq $0x0, (%rsp)
movq %rbx, 0x60(%rsp)
testl %eax, %eax
jle 0x31454
movq (%rsp), %rdx
movl %edx, %esi
movq 0x20(%rsp), %rcx
subl %ecx, %esi
leal (%rdx,%rcx), %edi
xorl %edx, %edx
testl %esi, %esi
movl $0x0, %ecx
movq %rcx, 0x8(%rsp)
cmovlel %edx, %esi
movl 0x238(%rbx), %ecx
movl %esi, 0x38(%rsp)
movl %edi, 0x34(%rsp)
testl %ecx, %ecx
jle 0x3143a
movq 0x8(%rsp), %rcx
movl %ecx, %r8d
movq 0x20(%rsp), %rax
subl %eax, %r8d
addl %ecx, %eax
movl %eax, 0x3c(%rsp)
xorl %ebp, %ebp
testl %r8d, %r8d
cmovlel %ebp, %r8d
xorl %r9d, %r9d
movl %r8d, 0x40(%rsp)
movl 0x240(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl %eax, %edi
cmovll %edi, %ecx
movl %ecx, 0x44(%rsp)
cmpl %ecx, %esi
jg 0x31420
movq 0x2d8(%rsp), %rax
movl 0x2f0(%rsp), %ecx
imull (%rsp), %ecx
movl 0x2ec(%rsp), %edx
imull 0x8(%rsp), %edx
addl %r9d, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x78(%rsp)
movl %r9d, %edx
movq 0x20(%rsp), %rax
subl %eax, %edx
testl %edx, %edx
movl $0x0, %ecx
cmovlel %ecx, %edx
movq %r9, 0x58(%rsp)
addl %r9d, %eax
movl %eax, 0x4c(%rsp)
xorpd %xmm0, %xmm0
movl %esi, %eax
movq %rax, 0x18(%rsp)
xorpd %xmm1, %xmm1
movl %edx, 0x50(%rsp)
movl 0x23c(%rbx), %eax
leal -0x1(%rax), %esi
movl 0x3c(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %esi
movl %esi, 0x54(%rsp)
cmpl %esi, %r8d
jg 0x313c0
movq 0x18(%rsp), %rax
subl (%rsp), %eax
imull %eax, %eax
movl %eax, 0x48(%rsp)
movl %r8d, %r13d
movl 0x238(%rbx), %eax
leal -0x1(%rax), %ebx
movl 0x4c(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %ebx
cmpl %ebx, %edx
jg 0x313a5
movl %r13d, %r15d
subl 0x8(%rsp), %r15d
imull %r15d, %r15d
addl 0x48(%rsp), %r15d
movl %edx, %r14d
leal (%r14,%rbp), %eax
imull %eax, %eax
addl %r15d, %eax
cmpl %r12d, %eax
ja 0x31395
movq 0x2d8(%rsp), %rcx
movl 0x2f0(%rsp), %edx
imull 0x18(%rsp), %edx
movl 0x2ec(%rsp), %esi
imull %r13d, %esi
addl %edx, %esi
addl %r14d, %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, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x68(%rsp), %xmm0
movsd %xmm1, 0x28(%rsp)
callq 0xe7f0
movsd %xmm0, 0x80(%rsp)
movsd 0x88(%rsp), %xmm0
callq 0xe7f0
movsd 0x28(%rsp), %xmm1
mulsd 0x80(%rsp), %xmm0
movsd 0x90(%rsp), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm2, %xmm1
movsd 0x10(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
leal 0x1(%r14), %eax
cmpl %ebx, %r14d
movl %eax, %r14d
jl 0x312c5
leal 0x1(%r13), %eax
cmpl 0x54(%rsp), %r13d
movl %eax, %r13d
movq 0x60(%rsp), %rbx
movl 0x50(%rsp), %edx
jl 0x31297
movq 0x18(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl 0x44(%rsp), %ecx
movq %rax, 0x18(%rsp)
movl 0x40(%rsp), %r8d
jl 0x31266
ucomisd 0x70e04(%rip), %xmm0    # 0xa21e8
movl 0x38(%rsp), %esi
movl 0x34(%rsp), %edi
movq 0x58(%rsp), %r9
jbe 0x31420
divsd %xmm0, %xmm1
movq 0x230(%rbx), %rax
movl 0x248(%rbx), %ecx
imull (%rsp), %ecx
movl 0x244(%rbx), %edx
imull 0x8(%rsp), %edx
addl %r9d, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movsd %xmm1, (%rax,%rcx,8)
incl %r9d
movl 0x238(%rbx), %ecx
decl %ebp
cmpl %ecx, %r9d
jl 0x311e7
movl 0x23c(%rbx), %eax
movq 0x8(%rsp), %rdx
incl %edx
movq %rdx, 0x8(%rsp)
cmpl %eax, %edx
jl 0x311b8
movl 0x240(%rbx), %ecx
movq (%rsp), %rdx
incl %edx
movq %rdx, (%rsp)
cmpl %ecx, %edx
jl 0x31181
leaq 0xa8(%rsp), %rdi
callq 0x2ee9e
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 0x2ee9e
movq %rbx, %rdi
callq 0xe7e0
nop