// File: Luthaf[P]zeopp/OMS.cc
// Function: IsExposedMoleculeThreshold(std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>, double)
bool IsExposedMoleculeThreshold (vector < vector <double> > MoleculeCoordinates, double Threshold  )
{

	int NPoints = (int) MoleculeCoordinates.size();

	double **TranslatedCoordinates = (double **) malloc((NPoints-1)*sizeof(double *));
	double *OriginCoordinates = (double *) malloc (DIM*sizeof(double));
	
	int i,j,k;

	// Angles
	double Theta = 0.;
	double Exposure_pos = 0., Exposure_neg = 0., Exposure_plane = 0.;

	// Plane side
	double Lambda = 0.;
	int S_lambda_old = 0, S_lambda = 0;


	// Assign initial coordinates
	for (i = 0; i < DIM; i++)
		OriginCoordinates[i] = MoleculeCoordinates.at(0).at(i);


	// Displace all the coordinates in order to set the metal molecule at the center
	for(i = 1; i < NPoints; i++)
	{
		TranslatedCoordinates[i-1] = (double *) malloc(sizeof(double)*DIM);
		// Assign values from input to the spherical coordinates vector and translate it to origin
		for (j = 0; j < DIM; j++)
			TranslatedCoordinates[i-1][j] = MoleculeCoordinates.at(i).at(j) - OriginCoordinates[j];
	}

	/* For every two non-metal atoms in the molecule, form a vector (M-O), then a plane, then decide whether the rest of the poins are
	 * - Either in the same plane
 	 * - Either at the same side of the plane
	 */
	for (i = 0; i < NPoints-1; i++)
		for (j = i+1; j < NPoints-1; j++)
		{

			//cout << "Plane " << i << j << "\n";

			// Reinitialize the variables to avoid error
			S_lambda_old = 0;
			S_lambda = 0;
			Lambda = 0;
			Theta = 0.;

			Exposure_pos = 0.;
			Exposure_neg = 0.;

			// For every point except the two composing the plane, compute
			if (!LinearlyDependent(TranslatedCoordinates[i], TranslatedCoordinates[j]))
			{	
				// POINTS ITERATION: Compare the points with the plane selected in previous iterations
				for (k = 0; k < NPoints-1; k++)
					if (k != i && k != j ) 
					{
						// Compute the side of the plane
						Lambda = PlaneSide (TranslatedCoordinates[i], TranslatedCoordinates[j], TranslatedCoordinates[k]);
						//cout << "k: " << k << " Lambda " << Lambda << "\n";

						S_lambda = Sign(Lambda);

						// Store the angles at both sides of the plane 
						// The exposure angle is the minimum of both
						if (S_lambda > 0)
						{
							Theta = PlaneAngle(TranslatedCoordinates[i], TranslatedCoordinates[j], TranslatedCoordinates[k]);
							if (Theta > Exposure_pos)							
								Exposure_pos = Theta;
						}
						if (S_lambda < 0)
						{
							Theta = PlaneAngle(TranslatedCoordinates[i], TranslatedCoordinates[j], TranslatedCoordinates[k]);
							if (Theta > Exposure_neg)							
								Exposure_neg = Theta;
						}

					}

				// Compute exposures (only if the vector forms a plane)
				Exposure_plane = min(Exposure_pos, Exposure_neg);
				if (Exposure_plane < Threshold)
					return true;

			}

			

		}
						
					
	// Maximum angle formed by a point not contained within the same side of the rest of the points
	return false;




}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x78(%rbp)
movq %rdi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
callq 0x22b10
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x56e0
movq %rax, -0x28(%rbp)
movl $0x18, %edi
callq 0x56e0
movq %rax, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x1747fc
movq -0x78(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xce5f0
movq %rax, %rdi
movslq -0x34(%rbp), %rsi
callq 0xfc80
movsd (%rax), %xmm0
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1747c1
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x174899
movl $0x18, %edi
callq 0x56e0
movq %rax, %rdx
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x0, -0x38(%rbp)
cmpl $0x3, -0x38(%rbp)
jge 0x174889
movq -0x78(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0xce5f0
movq %rax, %rdi
movslq -0x38(%rbp), %rsi
callq 0xfc80
movsd (%rax), %xmm0
movq -0x30(%rbp), %rax
movslq -0x38(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x38(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x174834
jmp 0x17488b
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x174803
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x174a81
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x174a71
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x173de0
testb $0x1, %al
jne 0x174a61
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x174a33
movl -0x3c(%rbp), %eax
cmpl -0x34(%rbp), %eax
je 0x174a23
movl -0x3c(%rbp), %eax
cmpl -0x38(%rbp), %eax
je 0x174a23
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
callq 0x173f20
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
callq 0x174190
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jle 0x1749d5
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
callq 0x174470
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
ucomisd -0x50(%rbp), %xmm0
jbe 0x1749d3
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
jmp 0x1749d5
cmpl $0x0, -0x70(%rbp)
jge 0x174a21
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
callq 0x174470
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
ucomisd -0x58(%rbp), %xmm0
jbe 0x174a1f
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
jmp 0x174a21
jmp 0x174a23
jmp 0x174a25
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x174925
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x64f50
movsd (%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x60(%rbp), %xmm1
movsd -0x18(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x174a5f
movb $0x1, -0x1(%rbp)
jmp 0x174a85
jmp 0x174a61
jmp 0x174a63
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1748ba
jmp 0x174a73
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1748a0
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)