// File: manikandan5[P]Image-Matching-Stitching-Warping/./SiftHelpers.h
// Function: Image::MatchSIFT(Image const&, Image const&, std::vector<std::pair<SiftDescriptor, SiftDescriptor>, std::allocator<std::pair<SiftDescriptor, SiftDescriptor>>>&, bool)
static int MatchSIFT(const Image& queryImage,const Image& image,SiftDescriptorMap& mapping,bool reduced = false)
	{
		vector<SiftDescriptor> queryDescriptors = queryImage.getDescriptors();
		vector<SiftDescriptor> imageDescriptors = image.getDescriptors();
		
		if(reduced)
		{
			queryDescriptors = queryImage.getNewDescriptors();
			imageDescriptors = image.getNewDescriptors();
		}
		
		int count = 0;
			
		for(int i = 0;i<queryDescriptors.size();++i)
		{
			double min = DBL_MAX;
			double second_min = DBL_MAX;
			int descriptor = -1;
			for(int j =0;j<imageDescriptors.size();++j)
			{
				double sum = 0;
				//Euclidean Distance 
				for(int k = 0;k<imageDescriptors[j].descriptor.size();++k)
				{
					sum+= pow(queryDescriptors[i].descriptor[k] - imageDescriptors[j].descriptor[k],2);
				}
				
				sum = sqrt(sum);
				if(sum < min)
				{
					second_min = min;
					min = sum;
					descriptor = j;
				}
				else if(sum < second_min)
				{
					second_min = sum;
				}
			}
			
			//ratio of the Euclidean distances between the closest match and the second-closest match, and comparing to a threshold.
			if(min/second_min < 0.7)
			{
				count++;
			
				SiftDescriptor q = queryDescriptors[i];
				SiftDescriptor im = imageDescriptors[descriptor];
				mapping.push_back(make_pair(q,im));
			}
			
		}
		
		//cout<<"Count = "<<count<<endl;
		Image::DrawImage(queryImage,image,mapping,reduced);
		
		return count;
	}

// Assembly:
subq $0x1a8, %rsp            # imm = 0x1A8
movb %cl, %al
movq %rdi, 0x1a0(%rsp)
movq %rsi, 0x198(%rsp)
movq %rdx, 0x190(%rsp)
andb $0x1, %al
movb %al, 0x18f(%rsp)
movq 0x1a0(%rsp), %rsi
leaq 0x170(%rsp), %rdi
callq 0xcbc0
movq 0x198(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0xcbc0
jmp 0xb1d6
testb $0x1, 0x18f(%rsp)
je 0xb28c
movq 0x1a0(%rsp), %rsi
leaq 0x130(%rsp), %rdi
callq 0xcbf0
jmp 0xb1fb
leaq 0x170(%rsp), %rdi
leaq 0x130(%rsp), %rsi
movq %rsi, 0x40(%rsp)
callq 0xcc20
movq 0x40(%rsp), %rdi
callq 0xcc60
movq 0x198(%rsp), %rsi
leaq 0x118(%rsp), %rdi
callq 0xcbf0
jmp 0xb236
leaq 0x158(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0xcc20
leaq 0x118(%rsp), %rdi
callq 0xcc60
jmp 0xb28c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xb69f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xb692
movl $0x0, 0x114(%rsp)
movl $0x0, 0x110(%rsp)
movslq 0x110(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x170(%rsp), %rdi
callq 0xccc0
movq %rax, %rcx
movq 0x38(%rsp), %rax
cmpq %rcx, %rax
jae 0xb637
movsd 0xbcd53(%rip), %xmm0    # 0xc8028
movsd %xmm0, 0x108(%rsp)
movsd 0xbcd42(%rip), %xmm0    # 0xc8028
movsd %xmm0, 0x100(%rsp)
movl $0xffffffff, 0xfc(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0xf8(%rsp)
movslq 0xf8(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x158(%rsp), %rdi
callq 0xccc0
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jae 0xb4d7
xorps %xmm0, %xmm0
movsd %xmm0, 0xf0(%rsp)
movl $0x0, 0xec(%rsp)
movslq 0xec(%rsp), %rax
movq %rax, 0x28(%rsp)
movslq 0xf8(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0xcce0
movq %rax, %rdi
addq $0x10, %rdi
callq 0xcd00
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jae 0xb430
movslq 0x110(%rsp), %rsi
leaq 0x170(%rsp), %rdi
callq 0xcce0
movq %rax, %rdi
addq $0x10, %rdi
movslq 0xec(%rsp), %rsi
callq 0xcd40
movss (%rax), %xmm0
movss %xmm0, 0x1c(%rsp)
movslq 0xf8(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0xcce0
movq %rax, %rdi
addq $0x10, %rdi
movslq 0xec(%rsp), %rsi
callq 0xcd40
movss 0x1c(%rsp), %xmm0
movss (%rax), %xmm1
subss %xmm1, %xmm0
movl $0x2, %edi
callq 0xcd20
movsd %xmm0, 0x20(%rsp)
jmp 0xb402
movsd 0x20(%rsp), %xmm0
addsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0xb347
movsd 0xf0(%rsp), %xmm0
callq 0x5410
movsd %xmm0, 0xf0(%rsp)
movsd 0xf0(%rsp), %xmm1
movsd 0x108(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xb493
movsd 0x108(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0x108(%rsp)
movl 0xf8(%rsp), %eax
movl %eax, 0xfc(%rsp)
jmp 0xb4bf
movsd 0xf0(%rsp), %xmm1
movsd 0x100(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xb4bd
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
jmp 0xb4bf
jmp 0xb4c1
movl 0xf8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf8(%rsp)
jmp 0xb305
movsd 0x108(%rsp), %xmm1
divsd 0x100(%rsp), %xmm1
movsd 0xbcb3f(%rip), %xmm0    # 0xc8030
ucomisd %xmm1, %xmm0
jbe 0xb61f
movl 0x114(%rsp), %eax
incl %eax
movl %eax, 0x114(%rsp)
movslq 0x110(%rsp), %rsi
leaq 0x170(%rsp), %rdi
callq 0xcce0
movq %rax, %rsi
leaq 0xc0(%rsp), %rdi
callq 0xcd60
jmp 0xb532
movslq 0xfc(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0xcce0
movq %rax, %rsi
leaq 0x98(%rsp), %rdi
callq 0xcd60
jmp 0xb559
movq 0x190(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0x98(%rsp), %rdx
callq 0xcdd0
jmp 0xb582
movq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xcda0
jmp 0xb593
leaq 0x48(%rsp), %rdi
callq 0xce10
leaq 0x98(%rsp), %rdi
callq 0xce40
leaq 0xc0(%rsp), %rdi
callq 0xce40
jmp 0xb61f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xb610
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xb603
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0x48(%rsp), %rdi
callq 0xce10
leaq 0x98(%rsp), %rdi
callq 0xce40
leaq 0xc0(%rsp), %rdi
callq 0xce40
jmp 0xb692
jmp 0xb621
movl 0x110(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x110(%rsp)
jmp 0xb2a2
movq 0x1a0(%rsp), %rdi
movq 0x198(%rsp), %rsi
movq 0x190(%rsp), %rdx
movzbl 0x18f(%rsp), %ecx
andl $0x1, %ecx
callq 0xce60
jmp 0xb661
movl 0x114(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x158(%rsp), %rdi
callq 0xcc60
leaq 0x170(%rsp), %rdi
callq 0xcc60
movl 0xc(%rsp), %eax
addq $0x1a8, %rsp            # imm = 0x1A8
retq
leaq 0x158(%rsp), %rdi
callq 0xcc60
leaq 0x170(%rsp), %rdi
callq 0xcc60
movq 0x150(%rsp), %rdi
callq 0x5970
nopl (%rax)