// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::HarrisCornerFilter(int, double)
void R2Grid::
HarrisCornerFilter(int radius, RNScalar kappa)
{
  // Compute gradients
  R2Grid xgradient(*this);  
  R2Grid ygradient(*this);  
  xgradient.Gradient(RN_X);
  ygradient.Gradient(RN_Y);

  // Initialize this grid
  Clear(R2_GRID_UNKNOWN_VALUE);

  // Get convenient variables
  RNScalar twice_squared_sigma = 0.5 * radius * radius;

  // Create harris response function
  for (int cx = 0; cx < XResolution(); cx++) {
    for (int cy = 0; cy < YResolution(); cy++) {
      // Check this pixel
      if (xgradient.GridValue(cx, cy) == R2_GRID_UNKNOWN_VALUE) continue;
      if (ygradient.GridValue(cx, cy) == R2_GRID_UNKNOWN_VALUE) continue;

      // Compute covariance matrix of window centered at pixel
      RNScalar dx2 = 0; 
      RNScalar dy2 = 0; 
      RNScalar dxdy = 0; 
      for (int s = -radius; s <= radius; s++) {
        int s2 = s*s;
        int ix = cx + s;
        if ((ix < 0) || (ix >= XResolution())) continue;
        for (int t = -radius; t <= radius; t++) {
          int t2 = t*t;
          int iy = cy + t;
          if ((iy < 0) || (iy >= YResolution())) continue;
          RNScalar dx = xgradient.GridValue(ix, iy);
          if (dx == R2_GRID_UNKNOWN_VALUE) continue;
          RNScalar dy = ygradient.GridValue(ix, iy);
          if (dy == R2_GRID_UNKNOWN_VALUE) continue;
          RNScalar w = exp(-(s2 + t2)/twice_squared_sigma);
          dx2 += w * dx*dx;
          dy2 += w * dy*dy;
          dxdy += w * dx*dy;
        }
      }

      // Compute eigenvalues
      // RNScalar det = dx2*dy2 - dxdy*dxdy;
      // RNScalar trace = dx2 + dy2;
      // RNScalar gap_squared = trace*trace - 4.0*det;
      // if (gap_squared < 0) continue;
      // RNScalar lambda1 = 0.5*(trace + sqrt(gap_squared));
      // RNScalar lambda2 = 0.5*(trace - sqrt(gap_squared));
      
      // Compute harris response from covariance matrix
      RNScalar det = dx2*dy2 - dxdy*dxdy;
      RNScalar trace = dx2 + dy2;
      RNScalar R = det - kappa*trace*trace;

      // Set grid value
      SetGridValue(cx, cy, R);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movsd %xmm0, 0x10(%rsp)
movl %esi, %ebx
movq %rdi, %r14
leaq 0x138(%rsp), %rdi
movq %r14, %rsi
callq 0x51fb4
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq 0x51fb4
leaq 0x138(%rsp), %rdi
xorl %esi, %esi
callq 0x5518a
leaq 0x68(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x5518a
movsd 0x3e889(%rip), %xmm0    # 0x93f68
movq %r14, %rdi
callq 0x53578
movsd 0x3e879(%rip), %xmm2    # 0x93f68
cvtsi2sd %ebx, %xmm0
movsd 0x3d915(%rip), %xmm1    # 0x93010
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movl %ebx, %eax
negl %eax
movl %eax, 0x4(%rsp)
xorl %edx, %edx
cmpl 0xc0(%r14), %edx
jge 0x55916
xorl %r12d, %r12d
movl %edx, 0x8(%rsp)
cmpl 0xc4(%r14), %r12d
jge 0x5590f
movq 0x1f0(%rsp), %rax
movl 0x200(%rsp), %ecx
imull %r12d, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5575d
jnp 0x55907
movq 0x120(%rsp), %rax
movl 0x130(%rsp), %ecx
imull %r12d, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x55786
jnp 0x55907
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
movl 0x4(%rsp), %ebp
cmpl %ebx, %ebp
jg 0x558bd
movl %ebp, %r13d
addl %edx, %r13d
js 0x558b6
cmpl 0xc0(%r14), %r13d
jge 0x558b6
movl %ebp, %eax
imull %ebp, %eax
movl %eax, 0xc(%rsp)
movl 0x4(%rsp), %r15d
cmpl %ebx, %r15d
jg 0x558b2
movl %r12d, %eax
addl %r15d, %eax
js 0x558aa
cmpl 0xc4(%r14), %eax
jge 0x558aa
movq 0x1f0(%rsp), %rcx
movl 0x200(%rsp), %edx
imull %eax, %edx
addl %r13d, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm2, %xmm1
jne 0x5580c
jnp 0x558aa
movq 0x120(%rsp), %rcx
imull 0x130(%rsp), %eax
addl %r13d, %eax
cltq
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5582e
jnp 0x558aa
movl %r15d, %eax
imull %r15d, %eax
addl 0xc(%rsp), %eax
negl %eax
movapd %xmm0, 0x30(%rsp)
cvtsi2sd %eax, %xmm0
divsd 0x18(%rsp), %xmm0
movapd %xmm3, 0x50(%rsp)
movapd %xmm4, 0x40(%rsp)
movapd %xmm1, 0x20(%rsp)
callq 0xe820
movapd 0x40(%rsp), %xmm4
movapd 0x50(%rsp), %xmm3
movsd 0x3e6f2(%rip), %xmm2    # 0x93f68
movapd 0x20(%rsp), %xmm6
movapd %xmm6, %xmm1
movapd 0x30(%rsp), %xmm5
unpcklpd %xmm5, %xmm1            # xmm1 = xmm1[0],xmm5[0]
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm1, %xmm0
mulsd %xmm0, %xmm6
addsd %xmm6, %xmm4
movapd %xmm5, %xmm1
unpcklpd %xmm5, %xmm1            # xmm1 = xmm1[0],xmm5[0]
mulpd %xmm0, %xmm1
addpd %xmm1, %xmm3
incl %r15d
jmp 0x557c1
movl 0x8(%rsp), %edx
incl %ebp
jmp 0x55792
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
unpckhpd %xmm3, %xmm3            # xmm3 = xmm3[1,1]
movapd %xmm4, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm0, %xmm1
addsd %xmm4, %xmm3
movapd %xmm3, %xmm0
mulsd 0x10(%rsp), %xmm0
mulsd %xmm3, %xmm0
subsd %xmm0, %xmm1
movq 0xb8(%r14), %rax
movl 0xc8(%r14), %ecx
imull %r12d, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movsd %xmm1, (%rax,%rcx,8)
incl %r12d
jmp 0x55727
incl %edx
jmp 0x55713
leaq 0x68(%rsp), %rdi
callq 0x5216c
leaq 0x138(%rsp), %rdi
callq 0x5216c
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x55951
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x5216c
leaq 0x138(%rsp), %rdi
callq 0x5216c
movq %rbx, %rdi
callq 0xe810