// File: manikandan5[P]Image-Matching-Stitching-Warping/siftpp/sift.cpp
// Function: VL::Sift::computeKeypointOrientations(float*, VL::Sift::Keypoint)
int
Sift::computeKeypointOrientations(VL::float_t angles [4], Keypoint keypoint)
{
  int const   nbins = 36 ;
  VL::float_t const winFactor = 1.5 ;
  VL::float_t hist [nbins] ;

  // octave
  int o = keypoint.o ;
  VL::float_t xperiod = getOctaveSamplingPeriod(o) ;

  // offsets to move in the Gaussian scale space octave
  const int ow = getOctaveWidth(o) ;
  const int oh = getOctaveHeight(o) ;
  const int xo = 2 ;
  const int yo = xo * ow ;
  const int so = yo * oh ;

  // keypoint fractional geometry
  VL::float_t x     = keypoint.x / xperiod ;
  VL::float_t y     = keypoint.y / xperiod ;
  VL::float_t sigma = keypoint.sigma / xperiod ;
  
  // shall we use keypoints.ix,iy,is here?
  int xi = ((int) (x+0.5)) ; 
  int yi = ((int) (y+0.5)) ;
  int si = keypoint.is ;
  
  VL::float_t const sigmaw = winFactor * sigma ;
  int W = (int) floor(3.0 * sigmaw) ;
  
  // skip the keypoint if it is out of bounds
  if(o  < omin   ||
     o  >=omin+O ||
     xi < 0      || 
     xi > ow-1   || 
     yi < 0      || 
     yi > oh-1   || 
     si < smin+1 || 
     si > smax-2 ) {
    std::cerr<<"!"<<std::endl ;
    return 0 ;
  }
  
  // make sure that the gradient buffer is filled with octave o
  prepareGrad(o) ;

  // clear the SIFT histogram
  std::fill(hist, hist + nbins, 0) ;

  // fill the SIFT histogram
  pixel_t* pt = temp + xi * xo + yi * yo + (si - smin -1) * so ;

#undef at
#define at(dx,dy) (*(pt + (dx)*xo + (dy)*yo))

  for(int ys = std::max(-W, 1-yi) ; ys <= std::min(+W, oh -2 -yi) ; ++ys) {
    for(int xs = std::max(-W, 1-xi) ; xs <= std::min(+W, ow -2 -xi) ; ++xs) {
      
      VL::float_t dx = xi + xs - x;
      VL::float_t dy = yi + ys - y;
      VL::float_t r2 = dx*dx + dy*dy ;

      // limit to a circular window
      if(r2 >= W*W+0.5) continue ;

      VL::float_t wgt = VL::fast_expn( r2 / (2*sigmaw*sigmaw) ) ;
      VL::float_t mod = *(pt + xs*xo + ys*yo) ;
      VL::float_t ang = *(pt + xs*xo + ys*yo + 1) ;

      //      int bin = (int) floor( nbins * ang / (2*M_PI) ) ;
      int bin = (int) floor( nbins * ang / (2*M_PI) ) ;
      hist[bin] += mod * wgt ;        
    }
  }
  
  // smooth the histogram
#if defined VL_LOWE_STRICT
  // Lowe's version apparently has a little issue with orientations
  // around + or - pi, which we reproduce here for compatibility
  for (int iter = 0; iter < 6; iter++) {
    VL::float_t prev  = hist[nbins/2] ;
    for (int i = nbins/2-1; i >= -nbins/2 ; --i) {
      int const j  = (i     + nbins) % nbins ;
      int const jp = (i - 1 + nbins) % nbins ;
      VL::float_t newh = (prev + hist[j] + hist[jp]) / 3.0;
      prev = hist[j] ;
      hist[j] = newh ;
    }
  }
#else
  // this is slightly more correct
  for (int iter = 0; iter < 6; iter++) {
    VL::float_t prev  = hist[nbins-1] ;
    VL::float_t first = hist[0] ;
    int i ;
    for (i = 0; i < nbins - 1; i++) {
      VL::float_t newh = (prev + hist[i] + hist[(i+1) % nbins]) / 3.0;
      prev = hist[i] ;
      hist[i] = newh ;
    }
    hist[i] = (prev + hist[i] + first)/3.0 ;
  }
#endif
  
  // find the histogram maximum
  VL::float_t maxh = * std::max_element(hist, hist + nbins) ;

  // find peaks within 80% from max
  int nangles = 0 ;
  for(int i = 0 ; i < nbins ; ++i) {
    VL::float_t h0 = hist [i] ;
    VL::float_t hm = hist [(i-1+nbins) % nbins] ;
    VL::float_t hp = hist [(i+1+nbins) % nbins] ;
    
    // is this a peak?
    if( h0 > 0.8*maxh && h0 > hm && h0 > hp ) {
      
      // quadratic interpolation
      //      VL::float_t di = -0.5 * (hp - hm) / (hp+hm-2*h0) ; 
      VL::float_t di = -0.5 * (hp - hm) / (hp+hm-2*h0) ; 
      VL::float_t th = 2*M_PI * (i+di+0.5) / nbins ;      
      angles [ nangles++ ] = th ;
      if( nangles == 4 )
        goto enough_angles ;
    }
  }
 enough_angles:
  return nangles ;
}

// Assembly:
subq $0x198, %rsp            # imm = 0x198
leaq 0x1a0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rdi, 0x188(%rsp)
movq %rsi, 0x180(%rsp)
movq 0x188(%rsp), %rdi
movq %rdi, 0x18(%rsp)
movl $0x24, 0x17c(%rsp)
movss 0x29044(%rip), %xmm0    # 0xed458
movss %xmm0, 0x178(%rsp)
movl (%rax), %eax
movl %eax, 0xdc(%rsp)
movl 0xdc(%rsp), %esi
callq 0xc5ce0
movq 0x18(%rsp), %rdi
movss %xmm0, 0xd8(%rsp)
movl 0xdc(%rsp), %esi
callq 0xc5d40
movq 0x18(%rsp), %rdi
movl %eax, 0xd4(%rsp)
movl 0xdc(%rsp), %esi
callq 0xc5df0
movl %eax, %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0xd0(%rsp)
movl $0x2, 0xcc(%rsp)
movl 0xd4(%rsp), %ecx
shll %ecx
movl %ecx, 0xc8(%rsp)
movl 0xc8(%rsp), %ecx
imull 0xd0(%rsp), %ecx
movl %ecx, 0xc4(%rsp)
movss 0x10(%rax), %xmm0
divss 0xd8(%rsp), %xmm0
movss %xmm0, 0xc0(%rsp)
movss 0x14(%rax), %xmm0
divss 0xd8(%rsp), %xmm0
movss %xmm0, 0xbc(%rsp)
movss 0x1c(%rax), %xmm0
divss 0xd8(%rsp), %xmm0
movss %xmm0, 0xb8(%rsp)
movss 0xc0(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x3b4b(%rip), %xmm1     # 0xc8048
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0xb4(%rsp)
movss 0xbc(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x3b27(%rip), %xmm1     # 0xc8048
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0xb0(%rsp)
movl 0xc(%rax), %eax
movl %eax, 0xac(%rsp)
movss 0x28f16(%rip), %xmm0    # 0xed458
mulss 0xb8(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
movss 0xa8(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x3ae7(%rip), %xmm0     # 0xc8050
mulsd %xmm1, %xmm0
callq 0x5900
movq 0x18(%rsp), %rcx
cvttsd2si %xmm0, %eax
movl %eax, 0xa4(%rsp)
movl 0xdc(%rsp), %eax
cmpl 0x14(%rcx), %eax
jl 0xc4612
movq 0x18(%rsp), %rdx
movl 0xdc(%rsp), %eax
movl 0x14(%rdx), %ecx
addl 0xc(%rdx), %ecx
cmpl %ecx, %eax
jge 0xc4612
cmpl $0x0, 0xb4(%rsp)
jl 0xc4612
movl 0xb4(%rsp), %eax
movl 0xd4(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jg 0xc4612
cmpl $0x0, 0xb0(%rsp)
jl 0xc4612
movl 0xb0(%rsp), %eax
movl 0xd0(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jg 0xc4612
movq 0x18(%rsp), %rcx
movl 0xac(%rsp), %eax
movl 0x18(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jl 0xc4612
movq 0x18(%rsp), %rcx
movl 0xac(%rsp), %eax
movl 0x1c(%rcx), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jle 0xc4644
movq 0x3b9d7(%rip), %rdi     # 0xffff0
leaq 0x542b(%rip), %rsi      # 0xc9a4b
callq 0x5520
movq %rax, %rdi
movq 0x3b971(%rip), %rsi     # 0xfffa0
callq 0x5580
movl $0x0, 0x194(%rsp)
jmp 0xc4c81
movq 0x18(%rsp), %rdi
movl 0xdc(%rsp), %esi
callq 0xc4150
leaq 0xe0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
addq $0x90, %rsi
movl $0x0, 0xa0(%rsp)
leaq 0xa0(%rsp), %rdx
callq 0xc63a0
movq 0x18(%rsp), %rdx
movq 0x30(%rdx), %rax
movl 0xb4(%rsp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movl 0xb0(%rsp), %ecx
imull 0xc8(%rsp), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movl 0xac(%rsp), %ecx
subl 0x18(%rdx), %ecx
subl $0x1, %ecx
imull 0xc4(%rsp), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x98(%rsp)
xorl %eax, %eax
subl 0xa4(%rsp), %eax
movl %eax, 0x90(%rsp)
movl $0x1, %eax
subl 0xb0(%rsp), %eax
movl %eax, 0x8c(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x8c(%rsp), %rsi
callq 0x93c30
movl (%rax), %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x88(%rsp)
movl 0xd0(%rsp), %eax
subl $0x2, %eax
subl 0xb0(%rsp), %eax
movl %eax, 0x84(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x84(%rsp), %rsi
callq 0x93c90
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpl (%rcx), %eax
jg 0xc49b4
xorl %eax, %eax
subl 0xa4(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl $0x1, %eax
subl 0xb4(%rsp), %eax
movl %eax, 0x78(%rsp)
leaq 0x7c(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x93c30
movl (%rax), %eax
movl %eax, 0x80(%rsp)
movl 0x80(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0xd4(%rsp), %eax
subl $0x2, %eax
subl 0xb4(%rsp), %eax
movl %eax, 0x70(%rsp)
leaq 0x74(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x93c90
movq %rax, %rcx
movl 0x8(%rsp), %eax
cmpl (%rcx), %eax
jg 0xc499c
movl 0xb4(%rsp), %eax
addl 0x80(%rsp), %eax
cvtsi2ss %eax, %xmm0
subss 0xc0(%rsp), %xmm0
movss %xmm0, 0x6c(%rsp)
movl 0xb0(%rsp), %eax
addl 0x94(%rsp), %eax
cvtsi2ss %eax, %xmm0
subss 0xbc(%rsp), %xmm0
movss %xmm0, 0x68(%rsp)
movss 0x6c(%rsp), %xmm0
movss 0x6c(%rsp), %xmm2
movss 0x68(%rsp), %xmm1
mulss 0x68(%rsp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x64(%rsp)
movss 0x64(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movl 0xa4(%rsp), %eax
imull 0xa4(%rsp), %eax
cvtsi2sd %eax, %xmm1
movsd 0x37c7(%rip), %xmm2     # 0xc8048
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0xc4890
jmp 0xc4986
movss 0x64(%rsp), %xmm0
movss 0x389e(%rip), %xmm1     # 0xc813c
mulss 0xa8(%rsp), %xmm1
mulss 0xa8(%rsp), %xmm1
divss %xmm1, %xmm0
callq 0xc63d0
movss %xmm0, 0x60(%rsp)
movq 0x98(%rsp), %rax
movl 0x80(%rsp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movl 0x94(%rsp), %ecx
imull 0xc8(%rsp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, 0x5c(%rsp)
movq 0x98(%rsp), %rax
movl 0x80(%rsp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movl 0x94(%rsp), %ecx
imull 0xc8(%rsp), %ecx
movslq %ecx, %rcx
movss 0x4(%rax,%rcx,4), %xmm0
movss %xmm0, 0x58(%rsp)
movss 0x28b24(%rip), %xmm0    # 0xed45c
mulss 0x58(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x28b5e(%rip), %xmm1    # 0xed4a8
divsd %xmm1, %xmm0
callq 0x5900
cvttsd2si %xmm0, %eax
movl %eax, 0x54(%rsp)
movss 0x5c(%rsp), %xmm0
movss 0x60(%rsp), %xmm2
movslq 0x54(%rsp), %rax
movss 0xe0(%rsp,%rax,4), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0xe0(%rsp,%rax,4)
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0xc47ab
jmp 0xc499e
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0xc4721
movl $0x0, 0x50(%rsp)
cmpl $0x6, 0x50(%rsp)
jge 0xc4ac5
movss 0x16c(%rsp), %xmm0
movss %xmm0, 0x4c(%rsp)
movss 0xe0(%rsp), %xmm0
movss %xmm0, 0x48(%rsp)
movl $0x0, 0x44(%rsp)
cmpl $0x23, 0x44(%rsp)
jge 0xc4a79
movss 0x4c(%rsp), %xmm0
movslq 0x44(%rsp), %rax
addss 0xe0(%rsp,%rax,4), %xmm0
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl $0x24, %ecx
cltd
idivl %ecx
movslq %edx, %rax
addss 0xe0(%rsp,%rax,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x361d(%rip), %xmm1     # 0xc8050
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x40(%rsp)
movslq 0x44(%rsp), %rax
movss 0xe0(%rsp,%rax,4), %xmm0
movss %xmm0, 0x4c(%rsp)
movss 0x40(%rsp), %xmm0
movslq 0x44(%rsp), %rax
movss %xmm0, 0xe0(%rsp,%rax,4)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0xc49ed
movss 0x4c(%rsp), %xmm0
movslq 0x44(%rsp), %rax
addss 0xe0(%rsp,%rax,4), %xmm0
addss 0x48(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x35b1(%rip), %xmm1     # 0xc8050
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movslq 0x44(%rsp), %rax
movss %xmm0, 0xe0(%rsp,%rax,4)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0xc49bc
leaq 0xe0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
addq $0x90, %rsi
callq 0xc6450
movss (%rax), %xmm0
movss %xmm0, 0x3c(%rsp)
movl $0x0, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
cmpl $0x24, 0x34(%rsp)
jge 0xc4c74
movslq 0x34(%rsp), %rax
movss 0xe0(%rsp,%rax,4), %xmm0
movss %xmm0, 0x30(%rsp)
movl 0x34(%rsp), %eax
subl $0x1, %eax
addl $0x24, %eax
movl $0x24, %ecx
cltd
idivl %ecx
movslq %edx, %rax
movss 0xe0(%rsp,%rax,4), %xmm0
movss %xmm0, 0x2c(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
addl $0x24, %eax
movl $0x24, %ecx
cltd
idivl %ecx
movslq %edx, %rax
movss 0xe0(%rsp,%rax,4), %xmm0
movss %xmm0, 0x28(%rsp)
movss 0x30(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x3c(%rsp), %xmm1
cvtss2sd %xmm1, %xmm2
movsd 0x2891a(%rip), %xmm1    # 0xed498
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xc4c62
movss 0x30(%rsp), %xmm0
ucomiss 0x2c(%rsp), %xmm0
jbe 0xc4c62
movss 0x30(%rsp), %xmm0
ucomiss 0x28(%rsp), %xmm0
jbe 0xc4c62
movss 0x28(%rsp), %xmm0
subss 0x2c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x288aa(%rip), %xmm0    # 0xed470
mulsd %xmm1, %xmm0
movss 0x28(%rsp), %xmm1
addss 0x2c(%rsp), %xmm1
movss 0x30(%rsp), %xmm2
addss %xmm2, %xmm2
subss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rsp)
cvtsi2ssl 0x34(%rsp), %xmm0
addss 0x24(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x343a(%rip), %xmm0     # 0xc8048
addsd %xmm0, %xmm1
movsd 0x2888e(%rip), %xmm0    # 0xed4a8
mulsd %xmm1, %xmm0
movsd 0x2888a(%rip), %xmm1    # 0xed4b0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x20(%rsp)
movss 0x20(%rsp), %xmm0
movq 0x180(%rsp), %rax
movl 0x38(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x38(%rsp)
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
cmpl $0x4, 0x38(%rsp)
jne 0xc4c60
jmp 0xc4c76
jmp 0xc4c62
jmp 0xc4c64
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0xc4afb
jmp 0xc4c76
movl 0x38(%rsp), %eax
movl %eax, 0x194(%rsp)
movl 0x194(%rsp), %eax
addq $0x198, %rsp            # imm = 0x198
retq