// File: 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:
subq $0x288, %rsp            # imm = 0x288
movq %rdi, 0x280(%rsp)
movl %esi, 0x27c(%rsp)
movsd %xmm0, 0x270(%rsp)
movq 0x280(%rsp), %rsi
movq %rsi, 0x48(%rsp)
leaq 0x1a0(%rsp), %rdi
callq 0xa73a0
movq 0x48(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0xa73a0
jmp 0xadf2d
leaq 0x1a0(%rsp), %rdi
xorl %esi, %esi
callq 0xad4e0
jmp 0xadf3e
leaq 0xd0(%rsp), %rdi
movl $0x1, %esi
callq 0xad4e0
jmp 0xadf52
movq 0x48(%rsp), %rdi
movsd 0x85f51(%rip), %xmm0    # 0x133eb0
callq 0xa9d20
cvtsi2sdl 0x27c(%rsp), %xmm1
movsd 0x8509b(%rip), %xmm0    # 0x133010
mulsd %xmm1, %xmm0
cvtsi2sdl 0x27c(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
movq 0x48(%rsp), %rdi
movl 0xb4(%rsp), %eax
movl %eax, 0x40(%rsp)
callq 0xb9260
movl %eax, 0x44(%rsp)
jmp 0xadfb5
movl 0x40(%rsp), %eax
movl 0x44(%rsp), %ecx
cmpl %ecx, %eax
jge 0xae414
movl $0x0, 0xb0(%rsp)
movq 0x48(%rsp), %rdi
movl 0xb0(%rsp), %eax
movl %eax, 0x38(%rsp)
callq 0xb9280
movl %eax, 0x3c(%rsp)
jmp 0xadfeb
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jge 0xae3fc
movl 0xb4(%rsp), %esi
movl 0xb0(%rsp), %edx
leaq 0x1a0(%rsp), %rdi
callq 0xb92a0
movsd %xmm0, 0x30(%rsp)
jmp 0xae01e
movsd 0x30(%rsp), %xmm0
movsd 0x85e84(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xae078
jp 0xae078
jmp 0xae3e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
jmp 0xae436
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0xa7690
jmp 0xae436
movl 0xb4(%rsp), %esi
movl 0xb0(%rsp), %edx
leaq 0xd0(%rsp), %rdi
callq 0xb92a0
movsd %xmm0, 0x28(%rsp)
jmp 0xae09b
movsd 0x28(%rsp), %xmm0
movsd 0x85e07(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xae0b6
jp 0xae0b6
jmp 0xae3e6
xorps %xmm0, %xmm0
movsd %xmm0, 0xa8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xa0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
xorl %eax, %eax
subl 0x27c(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0x27c(%rsp), %eax
jg 0xae354
movl 0x94(%rsp), %eax
imull 0x94(%rsp), %eax
movl %eax, 0x90(%rsp)
movl 0xb4(%rsp), %eax
addl 0x94(%rsp), %eax
movl %eax, 0x8c(%rsp)
cmpl $0x0, 0x8c(%rsp)
jl 0xae15a
movq 0x48(%rsp), %rdi
movl 0x8c(%rsp), %eax
movl %eax, 0x20(%rsp)
callq 0xb9260
movl %eax, 0x24(%rsp)
jmp 0xae14e
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
jl 0xae15f
jmp 0xae33e
xorl %eax, %eax
subl 0x27c(%rsp), %eax
movl %eax, 0x88(%rsp)
movl 0x88(%rsp), %eax
cmpl 0x27c(%rsp), %eax
jg 0xae33c
movl 0x88(%rsp), %eax
imull 0x88(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0xb0(%rsp), %eax
addl 0x88(%rsp), %eax
movl %eax, 0x80(%rsp)
cmpl $0x0, 0x80(%rsp)
jl 0xae1df
movq 0x48(%rsp), %rdi
movl 0x80(%rsp), %eax
movl %eax, 0x18(%rsp)
callq 0xb9280
movl %eax, 0x1c(%rsp)
jmp 0xae1d3
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %ecx
cmpl %ecx, %eax
jl 0xae1e4
jmp 0xae326
movl 0x8c(%rsp), %esi
movl 0x80(%rsp), %edx
leaq 0x1a0(%rsp), %rdi
callq 0xb92a0
movsd %xmm0, 0x10(%rsp)
jmp 0xae207
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm0
movsd 0x85c8f(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xae22e
jp 0xae22e
jmp 0xae326
movl 0x8c(%rsp), %esi
movl 0x80(%rsp), %edx
leaq 0xd0(%rsp), %rdi
callq 0xb92a0
movsd %xmm0, 0x8(%rsp)
jmp 0xae251
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x70(%rsp), %xmm0
movsd 0x85c45(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xae278
jp 0xae278
jmp 0xae326
movl 0x90(%rsp), %ecx
addl 0x84(%rsp), %ecx
xorl %eax, %eax
subl %ecx, %eax
cvtsi2sd %eax, %xmm0
divsd 0xb8(%rsp), %xmm0
callq 0xf8c0
movsd %xmm0, 0x68(%rsp)
movsd 0x68(%rsp), %xmm0
mulsd 0x78(%rsp), %xmm0
movsd 0x78(%rsp), %xmm2
movsd 0xa8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0x68(%rsp), %xmm0
mulsd 0x70(%rsp), %xmm0
movsd 0x70(%rsp), %xmm2
movsd 0xa0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0x68(%rsp), %xmm0
mulsd 0x78(%rsp), %xmm0
movsd 0x70(%rsp), %xmm2
movsd 0x98(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0xae16f
jmp 0xae33e
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0xae0ea
movq 0x48(%rsp), %rdi
movsd 0xa8(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm2
movsd 0x98(%rsp), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm2, %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0xa8(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x60(%rsp), %xmm0
movsd 0x270(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
mulsd %xmm2, %xmm1
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0xb4(%rsp), %esi
movl 0xb0(%rsp), %edx
movsd 0x50(%rsp), %xmm0
callq 0x6bc00
jmp 0xae3e4
jmp 0xae3e6
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0xadfd0
jmp 0xae3fe
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0xadf9a
leaq 0xd0(%rsp), %rdi
callq 0xa7690
leaq 0x1a0(%rsp), %rdi
callq 0xa7690
addq $0x288, %rsp            # imm = 0x288
retq
leaq 0x1a0(%rsp), %rdi
callq 0xa7690
movq 0xc8(%rsp), %rdi
callq 0xf890