// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm0, 0x40(%rsp)
movq %rdi, %r14
movq 0x8(%rdi), %rax
subq (%rdi), %rax
pushq $0x18
popq %r15
cqto
idivq %r15
movq %rax, %r12
movslq %r12d, %rax
leaq -0x8(,%rax,8), %rdi
decq %rax
movq %rax, 0x28(%rsp)
callq 0x45b0
movq %rax, 0x18(%rsp)
movq %r15, %rdi
callq 0x45b0
movq %rax, %r15
xorl %r13d, %r13d
cmpq $0x3, %r13
je 0xc44a7
movq %r14, %rdi
xorl %esi, %esi
callq 0x57adc
movq %rax, %rdi
movq %r13, %rsi
callq 0xaa50
movsd (%rax), %xmm0
movsd %xmm0, (%r15,%r13,8)
incq %r13
jmp 0xc447d
movslq %r12d, %rbx
pushq $0x1
popq %r12
cmpq %rbx, %r12
jge 0xc4502
pushq $0x18
popq %rdi
callq 0x45b0
movq %rax, %rbp
movq 0x18(%rsp), %rax
movq %rbp, -0x8(%rax,%r12,8)
xorl %r13d, %r13d
cmpq $0x3, %r13
je 0xc44fd
movq %r14, %rdi
movq %r12, %rsi
callq 0x57adc
movq %rax, %rdi
movq %r13, %rsi
callq 0xaa50
movsd (%rax), %xmm0
subsd (%r15,%r13,8), %xmm0
movsd %xmm0, (%rbp,%r13,8)
incq %r13
jmp 0xc44cb
incq %r12
jmp 0xc44ae
xorl %ebp, %ebp
movq 0x28(%rsp), %r12
testl %r12d, %r12d
movl $0x0, %eax
cmovgl %r12d, %eax
movl %r12d, %ecx
movq %rcx, 0x50(%rsp)
pushq $0x1
popq %rcx
movq %rcx, 0x20(%rsp)
movq %rax, 0x30(%rsp)
cmpq %rax, %rbp
je 0xc464c
leaq 0x1(%rbp), %rax
movq %rax, 0x38(%rsp)
movq 0x20(%rsp), %rbx
cmpl %ebx, %r12d
jle 0xc4638
movq 0x18(%rsp), %rax
movq (%rax,%rbp,8), %r14
movq (%rax,%rbx,8), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xc3e35
testb %al, %al
jne 0xc4630
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r13d, %r13d
movsd %xmm0, 0x8(%rsp)
cmpq %r13, 0x50(%rsp)
je 0xc4613
cmpq %r13, %rbp
sete %al
cmpq %r13, %rbx
sete %cl
orb %al, %cl
jne 0xc460b
movq 0x18(%rsp), %rax
movq (%rax,%r13,8), %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xc3ed9
movapd %xmm0, %xmm1
ucomisd 0x3bd25(%rip), %xmm0    # 0x1002e0
jbe 0xc45e5
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movsd %xmm1, 0x48(%rsp)
callq 0xc4253
movsd 0x48(%rsp), %xmm1
ucomisd 0x10(%rsp), %xmm0
jbe 0xc45e5
movsd %xmm0, 0x10(%rsp)
ucomisd 0x3bcf3(%rip), %xmm1    # 0x1002e0
jae 0xc460b
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xc4253
ucomisd 0x8(%rsp), %xmm0
jbe 0xc460b
movsd %xmm0, 0x8(%rsp)
incq %r13
jmp 0xc457d
movsd 0x8(%rsp), %xmm1
minsd 0x10(%rsp), %xmm1
movsd 0x40(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movq 0x28(%rsp), %r12
ja 0xc464c
incq %rbx
jmp 0xc4541
incq 0x20(%rsp)
movq 0x38(%rsp), %rbp
movq 0x30(%rsp), %rax
jmp 0xc452a
cmpq %r12, %rbp
setl %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq