// File: /workspace/github_cpp_makefile_star2_O1/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 $0x218, %rsp            # imm = 0x218
movsd %xmm0, 0x20(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
leaq 0x148(%rsp), %rdi
movq %r14, %rsi
callq 0x5a63c
leaq 0x78(%rsp), %rdi
movq %r14, %rsi
callq 0x5a63c
leaq 0x148(%rsp), %rdi
xorl %esi, %esi
callq 0x5dc96
leaq 0x78(%rsp), %rdi
movl $0x1, %esi
callq 0x5dc96
movslq 0xcc(%r14), %rax
testq %rax, %rax
jle 0x5e2ac
movq 0xb8(%r14), %rcx
xorl %edx, %edx
movsd 0x44d2f(%rip), %xmm0    # 0xa2fb8
movabsq $-0x3f8bf00000000000, %rsi # imm = 0xC074100000000000
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x5e2a0
jnp 0x5e2a4
movq %rsi, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x5e293
cmpl $0x0, 0xc0(%r14)
jle 0x5e526
movq 0x10(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x43d62(%rip), %xmm1    # 0xa2030
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
movl %eax, %ecx
negl %ecx
movl %ecx, 0xc(%rsp)
leal 0x1(,%rax,2), %eax
movl %eax, 0x18(%rsp)
movl $0x0, 0x8(%rsp)
movsd 0x44cb9(%rip), %xmm2    # 0xa2fb8
cmpl $0x0, 0xc4(%r14)
jle 0x5e50f
xorl %r13d, %r13d
movq 0x200(%rsp), %rax
movl 0x210(%rsp), %ecx
imull %r13d, %ecx
addl 0x8(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5e33b
jnp 0x5e4ff
movq 0x130(%rsp), %rax
movl 0x140(%rsp), %ecx
imull %r13d, %ecx
addl 0x8(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5e366
jnp 0x5e4ff
cmpl $0x0, 0x10(%rsp)
js 0x5e4ab
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
movl 0xc(%rsp), %eax
movl %eax, %r15d
movl %r15d, %ebp
addl 0x8(%rsp), %ebp
js 0x5e497
cmpl 0xc0(%r14), %ebp
jge 0x5e497
movl %r15d, %eax
imull %r15d, %eax
movl %eax, 0x1c(%rsp)
movl 0x18(%rsp), %r12d
movl 0xc(%rsp), %ebx
movl %r13d, %eax
addl %ebx, %eax
js 0x5e48c
cmpl 0xc4(%r14), %eax
jge 0x5e48c
movq 0x200(%rsp), %rcx
movl 0x210(%rsp), %edx
imull %eax, %edx
addl %ebp, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm2, %xmm1
jne 0x5e3ee
jnp 0x5e48c
movq 0x130(%rsp), %rcx
imull 0x140(%rsp), %eax
addl %ebp, %eax
cltq
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5e40f
jnp 0x5e48c
movl %ebx, %eax
imull %ebx, %eax
addl 0x1c(%rsp), %eax
negl %eax
movapd %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x28(%rsp), %xmm0
movapd %xmm3, 0x60(%rsp)
movapd %xmm4, 0x50(%rsp)
movapd %xmm1, 0x30(%rsp)
callq 0xe7f0
movapd 0x50(%rsp), %xmm4
movapd 0x60(%rsp), %xmm3
movsd 0x44b60(%rip), %xmm2    # 0xa2fb8
movapd 0x30(%rsp), %xmm6
movapd %xmm6, %xmm1
movapd 0x40(%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 %ebx
decl %r12d
jne 0x5e3ae
leal 0x1(%r15), %eax
cmpl 0x10(%rsp), %r15d
movl %eax, %r15d
jne 0x5e380
jmp 0x5e4b3
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
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 0x20(%rsp), %xmm0
mulsd %xmm3, %xmm0
subsd %xmm0, %xmm1
movq 0xb8(%r14), %rax
movl 0xc8(%r14), %ecx
imull %r13d, %ecx
addl 0x8(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm1, (%rax,%rcx,8)
incl %r13d
cmpl 0xc4(%r14), %r13d
jl 0x5e310
movl 0x8(%rsp), %eax
incl %eax
movl %eax, 0x8(%rsp)
cmpl 0xc0(%r14), %eax
jl 0x5e2ff
leaq 0x78(%rsp), %rdi
callq 0x5a814
leaq 0x148(%rsp), %rdi
callq 0x5a814
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x5e561
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x5a814
leaq 0x148(%rsp), %rdi
callq 0x5a814
movq %rbx, %rdi
callq 0xe7e0