// File: /workspace/github_cpp_makefile_star2_O1/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, 0x30(%rsp)
movq %rdi, %rbx
leaq 0xa8(%rsp), %rdi
movq %rbx, %rsi
callq 0x2f120
movsd 0x10(%rsp), %xmm3
ucomisd 0x70d82(%rip), %xmm3    # 0xa2048
jne 0x312f4
jp 0x312f4
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0x2f2c8
movsd 0xa0(%rsp), %xmm3
subsd 0x98(%rsp), %xmm3
mulsd 0x71cbc(%rip), %xmm3    # 0xa2fb0
cmpl $0x0, 0x240(%rbx)
jle 0x31639
movsd 0x70ef7(%rip), %xmm0    # 0xa2200
movsd 0x30(%rsp), %xmm2
movapd %xmm2, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, 0x68(%rsp)
mulsd %xmm3, %xmm0
mulsd 0x70eab(%rip), %xmm2    # 0xa21d8
addsd 0x70d03(%rip), %xmm2    # 0xa2038
mulsd %xmm3, %xmm0
movsd %xmm0, 0x70(%rsp)
cvttsd2si %xmm2, %eax
movq %rax, 0x28(%rsp)
movl %eax, %ebp
imull %eax, %ebp
movq $0x0, (%rsp)
movq %rbx, 0x60(%rsp)
cmpl $0x0, 0x23c(%rbx)
jle 0x31623
movq (%rsp), %rcx
movl %ecx, %edx
movq 0x28(%rsp), %rax
subl %eax, %edx
addl %ecx, %eax
movl %eax, 0x38(%rsp)
xorl %ecx, %ecx
testl %edx, %edx
movl $0x0, %eax
movq %rax, 0x8(%rsp)
cmovlel %ecx, %edx
movl %edx, 0x1c(%rsp)
cmpl $0x0, 0x238(%rbx)
jle 0x3160b
movq 0x8(%rsp), %rcx
movl %ecx, %esi
movq 0x28(%rsp), %rax
subl %eax, %esi
addl %ecx, %eax
movl %eax, 0x3c(%rsp)
xorl %r12d, %r12d
testl %esi, %esi
cmovlel %r12d, %esi
xorl %edi, %edi
movl %esi, 0x40(%rsp)
movq %rdi, 0x58(%rsp)
movl 0x240(%rbx), %eax
leal -0x1(%rax), %edx
movl 0x38(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %edx
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movl %edx, 0x44(%rsp)
cmpl %edx, 0x1c(%rsp)
jg 0x315bf
movq 0x2d8(%rsp), %rax
movl 0x2f0(%rsp), %ecx
imull (%rsp), %ecx
movl 0x2ec(%rsp), %edx
imull 0x8(%rsp), %edx
movq 0x58(%rsp), %rdi
addl %edi, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x78(%rsp)
movl %edi, %edx
movq 0x28(%rsp), %rax
subl %eax, %edx
testl %edx, %edx
movl $0x0, %ecx
cmovlel %ecx, %edx
addl %edi, %eax
movl %eax, 0x4c(%rsp)
xorpd %xmm0, %xmm0
movl 0x1c(%rsp), %eax
movq %rax, 0x20(%rsp)
movl %edx, 0x50(%rsp)
movl 0x23c(%rbx), %eax
leal -0x1(%rax), %edi
movl 0x3c(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %edi
movl %edi, 0x54(%rsp)
cmpl %edi, %esi
jg 0x315a4
movq 0x20(%rsp), %rax
subl (%rsp), %eax
imull %eax, %eax
movl %eax, 0x48(%rsp)
movl %esi, %r13d
movl 0x238(%rbx), %eax
leal -0x1(%rax), %ebx
movl 0x4c(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %ebx
cmpl %ebx, %edx
jg 0x31589
movl %r13d, %r15d
subl 0x8(%rsp), %r15d
imull %r15d, %r15d
addl 0x48(%rsp), %r15d
movl %edx, %r14d
leal (%r12,%r14), %eax
imull %eax, %eax
addl %r15d, %eax
cmpl %ebp, %eax
ja 0x31579
movq 0x2d8(%rsp), %rcx
movl 0x2f0(%rsp), %edx
imull 0x20(%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, 0x30(%rsp)
callq 0xe7f0
movsd %xmm0, 0x80(%rsp)
movsd 0x88(%rsp), %xmm0
callq 0xe7f0
movsd 0x30(%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 0x314aa
leal 0x1(%r13), %eax
cmpl 0x54(%rsp), %r13d
movl %eax, %r13d
movq 0x60(%rsp), %rbx
movl 0x50(%rsp), %edx
jl 0x3147c
movq 0x20(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl 0x44(%rsp), %ecx
movq %rax, 0x20(%rsp)
movl 0x40(%rsp), %esi
jl 0x3144c
ucomisd 0x70c21(%rip), %xmm0    # 0xa21e8
movq 0x58(%rsp), %rdi
jbe 0x315fa
divsd %xmm0, %xmm1
movq 0x230(%rbx), %rax
movl 0x248(%rbx), %ecx
imull (%rsp), %ecx
movl 0x244(%rbx), %edx
imull 0x8(%rsp), %edx
addl %edi, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movsd %xmm1, (%rax,%rcx,8)
incl %edi
decl %r12d
cmpl 0x238(%rbx), %edi
jl 0x313bf
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
cmpl 0x23c(%rbx), %eax
jl 0x3138f
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
cmpl 0x240(%rbx), %eax
jl 0x3135a
leaq 0xa8(%rsp), %rdi
callq 0x2f17c
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 0x2f17c
movq %rbx, %rdi
callq 0xe7e0