// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %ecx, %ebp
movq %rdx, 0x18(%rsp)
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
leaq 0x20(%rdi), %rsi
leaq 0x20(%rsp), %rdi
callq 0x827e
leaq 0x20(%rbx), %rsi
leaq 0x38(%rsp), %rdi
callq 0x827e
testb %bpl, %bpl
je 0x72c8
movq 0x10(%rsp), %rax
leaq 0x58(%rax), %rsi
leaq 0x90(%rsp), %rdi
callq 0x827e
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %r14
movq %r14, %rsi
callq 0x83ec
movq %r14, %rdi
callq 0x7f7a
leaq 0x58(%rbx), %rsi
leaq 0x90(%rsp), %rdi
callq 0x827e
leaq 0x38(%rsp), %rdi
leaq 0x90(%rsp), %r14
movq %r14, %rsi
callq 0x83ec
movq %r14, %rdi
callq 0x7f7a
movl %ebp, 0xc(%rsp)
movq %rbx, 0x50(%rsp)
xorl %ebx, %ebx
pushq $0x28
popq %r14
leaq 0xe0(%rsp), %rbp
movl $0x0, 0x8(%rsp)
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
cqto
idivq %r14
cmpq %rbx, %rax
jbe 0x7465
imulq $0x28, %rbx, %r13
xorl %r12d, %r12d
movsd 0x4cd12(%rip), %xmm0    # 0x54020
movapd %xmm0, %xmm2
movapd %xmm0, %xmm3
pushq $-0x1
popq %r15
movq 0x38(%rsp), %rcx
movq 0x40(%rsp), %rax
subq %rcx, %rax
cqto
idivq %r14
cmpq %r12, %rax
jbe 0x73dd
imulq $0x28, %r12, %rdx
movq 0x10(%rcx,%rdx), %rax
movq 0x18(%rcx,%rdx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq 0x20(%rsp), %rdx
addq %r13, %rdx
addq $0x10, %rdx
xorpd %xmm0, %xmm0
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x737f
movq (%rdx), %rdi
movss (%rdi,%rsi,4), %xmm1
subss (%rax,%rsi,4), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rsi
jmp 0x735c
ucomisd 0x4cca9(%rip), %xmm0    # 0x54030
jb 0x738f
sqrtsd %xmm0, %xmm0
jmp 0x73ac
movsd %xmm2, 0x60(%rsp)
movsd %xmm3, 0x58(%rsp)
callq 0x4400
movsd 0x58(%rsp), %xmm3
movsd 0x60(%rsp), %xmm2
ucomisd %xmm0, %xmm2
jbe 0x73bf
movapd %xmm0, %xmm1
movapd %xmm2, %xmm0
movl %r12d, %r15d
jmp 0x73cd
movapd %xmm2, %xmm1
ucomisd %xmm0, %xmm3
ja 0x73cd
movapd %xmm3, %xmm0
incq %r12
movapd %xmm1, %xmm2
movapd %xmm0, %xmm3
jmp 0x731a
divsd %xmm3, %xmm2
movsd 0x4cc3f(%rip), %xmm0    # 0x54028
ucomisd %xmm2, %xmm0
jbe 0x745d
addq 0x20(%rsp), %r13
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
callq 0x7f9c
movslq %r15d, %rax
imulq $0x28, %rax, %rsi
addq 0x38(%rsp), %rsi
movq %rbp, %rdi
callq 0x7f9c
leaq 0x90(%rsp), %r12
movq %r12, %rdi
leaq 0x68(%rsp), %rsi
movq %rbp, %rdx
callq 0x8706
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x84fc
incl 0x8(%rsp)
movq %r12, %rdi
callq 0x7fb0
leaq 0xf0(%rsp), %rdi
callq 0x848c
leaq 0x78(%rsp), %rdi
callq 0x848c
incq %rbx
jmp 0x72e7
movzbl 0xc(%rsp), %ecx
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x7fca
leaq 0x38(%rsp), %rdi
callq 0x7f7a
leaq 0x20(%rsp), %rdi
callq 0x7f7a
movl 0x8(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x74e6
jmp 0x74e6
jmp 0x74e6
movq %rax, %rbx
jmp 0x74f3
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x7fb0
jmp 0x74c8
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x848c
jmp 0x74da
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x848c
jmp 0x74e9
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x7f7a
leaq 0x20(%rsp), %rdi
callq 0x7f7a
movq %rbx, %rdi
callq 0x4950
nop