// File: /workspace/github_cpp_makefile_star2_O3/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, 0x8(%rsp)
movq %rdi, %r14
leaq 0x148(%rsp), %rdi
movq %r14, %rsi
callq 0x59934
leaq 0x78(%rsp), %rdi
movq %r14, %rsi
callq 0x59934
leaq 0x148(%rsp), %rdi
xorl %esi, %esi
callq 0x5cf1c
leaq 0x78(%rsp), %rdi
movl $0x1, %esi
callq 0x5cf1c
movslq 0xcc(%r14), %rax
testq %rax, %rax
jle 0x5d4e4
movq 0xb8(%r14), %rcx
xorl %edx, %edx
movsd 0x45af7(%rip), %xmm0    # 0xa2fb8
movabsq $-0x3f8bf00000000000, %rsi # imm = 0xC074100000000000
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x5d4d8
jnp 0x5d4dc
movq %rsi, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x5d4cb
movl 0xc0(%r14), %ecx
testl %ecx, %ecx
jle 0x5d757
movq 0x8(%rsp), %rdx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
movsd 0x44b29(%rip), %xmm1    # 0xa2030
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
movl %edx, %eax
negl %eax
movl %eax, 0x4(%rsp)
movl 0xc4(%r14), %eax
leal 0x1(,%rdx,2), %edx
movl %edx, 0x14(%rsp)
xorl %esi, %esi
movsd 0x45a7f(%rip), %xmm2    # 0xa2fb8
testl %eax, %eax
jle 0x5d74d
xorl %r13d, %r13d
movl %esi, 0x18(%rsp)
movq 0x200(%rsp), %rcx
movl 0x210(%rsp), %edx
imull %r13d, %edx
addl %esi, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5d571
jnp 0x5d73a
movq 0x130(%rsp), %rcx
movl 0x140(%rsp), %edx
imull %r13d, %edx
addl %esi, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5d59a
jnp 0x5d73a
cmpl $0x0, 0x8(%rsp)
js 0x5d6e8
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
movl 0x4(%rsp), %eax
movl %eax, %r15d
movl %r15d, %ebp
addl %esi, %ebp
js 0x5d6c9
cmpl 0xc0(%r14), %ebp
jge 0x5d6c9
movl %r15d, %eax
imull %r15d, %eax
movl %eax, 0x1c(%rsp)
movl 0x14(%rsp), %r12d
movl 0x4(%rsp), %ebx
movl %r13d, %eax
addl %ebx, %eax
js 0x5d6be
cmpl 0xc4(%r14), %eax
jge 0x5d6be
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 0x5d620
jnp 0x5d6be
movq 0x130(%rsp), %rcx
imull 0x140(%rsp), %eax
addl %ebp, %eax
cltq
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5d641
jnp 0x5d6be
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 0x4592e(%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 0x5d5e0
leal 0x1(%r15), %eax
cmpl 0x8(%rsp), %r15d
movl %eax, %r15d
movl 0x18(%rsp), %esi
jne 0x5d5b4
movl 0xc4(%r14), %eax
jmp 0x5d6f0
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), %rcx
movl 0xc8(%r14), %edx
imull %r13d, %edx
addl %esi, %edx
movslq %edx, %rdx
movsd %xmm1, (%rcx,%rdx,8)
incl %r13d
cmpl %eax, %r13d
jl 0x5d548
movl 0xc0(%r14), %ecx
incl %esi
cmpl %ecx, %esi
jl 0x5d539
leaq 0x78(%rsp), %rdi
callq 0x59b0c
leaq 0x148(%rsp), %rdi
callq 0x59b0c
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x5d792
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x59b0c
leaq 0x148(%rsp), %rdi
callq 0x59b0c
movq %rbx, %rdi
callq 0xe7e0
nop