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

	int NPoints = (int) MoleculeCoordinates.size();
	//vector < vector <double> > MoleculeSphericalCoordinates(Size, vector<double>(DIM) );
	//vector < vector <double> > MoleculeSphericalCoordinates(Size);

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

	double Aux;

	double Lambda = 0.;
	int S_lambda_old = 0, S_lambda;

	bool PlaneFound;


	// 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++)
		{
			Aux = MoleculeCoordinates.at(i).at(j);
			TranslatedCoordinates[i-1][j] = Aux - 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++)
		{

			// Reinitialize the variables to avoid error
			S_lambda_old = 0;
			S_lambda = 0;
			Lambda = 0;
			PlaneFound = true;
			// For every point except the two composing the plane, compute
			if (!LinearlyDependent(TranslatedCoordinates[i], TranslatedCoordinates[j]))
			{		
				for (k = 0; k < NPoints-1; k++)
					if (k != i && k != j ) 
					{
						Lambda = PlaneSide (TranslatedCoordinates[i], TranslatedCoordinates[j], TranslatedCoordinates[k]);

						//cout << "k: " << k << "\nLambda: " << Lambda << "\n";
			
						S_lambda = Sign(Lambda);

						// Update S_lambda_old only the first time that the point is not in the plane				
						if (Lambda != 0 && S_lambda_old == 0)
							S_lambda_old = Sign(Lambda);

						// If the sign changes anytime, stop this loop (the plane has points on both sides)
						if (S_lambda != S_lambda_old)
						{
							PlaneFound = false;
							//cout << "This plane separates the points \n";
							break;
						}
					}
			}
			else 	// If the vectors are linearly dependent the iteration is ignored
			{
				PlaneFound = false;
				//cout << "Setting PlaneFound false \n";
			}

			// Check if a plane with all the points on one side was found
			if (PlaneFound)
				return true;
		}
						
					

	return false;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
movq 0x8(%rdi), %rax
subq (%rdi), %rax
pushq $0x18
popq %r15
cqto
idivq %r15
movq %rax, %r12
movslq %r12d, %rbx
leaq -0x8(,%rbx,8), %rdi
decq %rbx
callq 0x45b0
movq %rax, 0x10(%rsp)
movq %r15, %rdi
callq 0x45b0
movq %rax, %r15
xorl %r13d, %r13d
cmpq $0x3, %r13
je 0xc40f0
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 0xc40c6
movq %rbx, 0x8(%rsp)
movslq %r12d, %rbx
pushq $0x1
popq %r12
cmpq %rbx, %r12
jge 0xc4150
pushq $0x18
popq %rdi
callq 0x45b0
movq %rax, %rbp
movq 0x10(%rsp), %rax
movq %rbp, -0x8(%rax,%r12,8)
xorl %r13d, %r13d
cmpq $0x3, %r13
je 0xc414b
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 0xc4119
incq %r12
jmp 0xc40fc
xorl %r13d, %r13d
movq 0x8(%rsp), %rcx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
movl %ecx, %ebp
pushq $0x1
popq %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
cmpq %rax, %r13
je 0xc423c
leaq 0x1(%r13), %rax
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %r12
cmpl %r12d, 0x8(%rsp)
jle 0xc4228
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rdi
movq (%rax,%r12,8), %rsi
movq %rdi, 0x30(%rsp)
movq %rsi, %rbx
callq 0xc3e35
testb %al, %al
je 0xc41b6
incq %r12
jmp 0xc4188
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpq %r14, %rbp
je 0xc423c
cmpq %r14, %r13
sete %al
cmpq %r14, %r12
sete %cl
orb %al, %cl
jne 0xc4223
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0xc3ed9
xorl %eax, %eax
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
seta %al
testl %r15d, %r15d
leal -0x1(%rax,%rax), %eax
cmovnel %r15d, %eax
ucomisd %xmm1, %xmm0
cmpneqsd %xmm1, %xmm0
movq %xmm0, %rcx
pushq $0x1
popq %rdx
cmoval %edx, %ecx
cmovnel %eax, %r15d
cmovpl %eax, %r15d
cmpl %r15d, %ecx
movl %ecx, %r15d
jne 0xc41b1
incq %r14
jmp 0xc41bc
incq 0x18(%rsp)
movq 0x28(%rsp), %r13
movq 0x20(%rsp), %rax
jmp 0xc4171
cmpq 0x8(%rsp), %r13
setl %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq