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

	
	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 = PI/2., 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);
				/*cout << "Exposure plane: \t" << Exposure_plane << "\n";
				cout << "Exposure: \t" << Exposure << "\n";*/

				if (Exposure_plane < Exposure)
				{
					//cout << "Exposure updated: " << Exposure << "\n";			
					Exposure = Exposure_plane;
				}
			}

			

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


}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r14
movq 0x8(%rdi), %rax
subq (%rdi), %rax
pushq $0x18
popq %r15
cqto
idivq %r15
movq %rax, %r12
movslq %r12d, %rax
movl %r12d, %ecx
decl %ecx
movl %ecx, 0xc(%rsp)
leaq -0x8(,%rax,8), %rdi
callq 0x45b0
movq %rax, 0x28(%rsp)
movq %r15, %rdi
callq 0x45b0
movq %rax, %r15
xorl %r13d, %r13d
cmpq $0x3, %r13
je 0xc46db
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 0xc46b1
movslq %r12d, %rbx
pushq $0x1
popq %r12
cmpq %rbx, %r12
jge 0xc4736
pushq $0x18
popq %rdi
callq 0x45b0
movq %rax, %rbp
movq 0x28(%rsp), %rax
movq %rbp, -0x8(%rax,%r12,8)
xorl %r13d, %r13d
cmpq $0x3, %r13
je 0xc4731
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 0xc46ff
incq %r12
jmp 0xc46e2
xorl %r13d, %r13d
movl 0xc(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, 0x48(%rsp)
movl %eax, %r12d
movsd 0x42021(%rip), %xmm0    # 0x106778
movsd %xmm0, 0x10(%rsp)
pushq $0x1
popq %rax
movq %rax, 0x30(%rsp)
cmpq 0x48(%rsp), %r13
je 0xc48b1
movq 0x30(%rsp), %rbx
cmpl %ebx, 0xc(%rsp)
jle 0xc48a4
movq 0x28(%rsp), %rax
movq (%rax,%r13,8), %r14
movq (%rax,%rbx,8), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xc3e35
testb %al, %al
jne 0xc489c
movq %r14, 0x38(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
xorl %ebp, %ebp
xorpd %xmm1, %xmm1
movq %rbx, 0x20(%rsp)
cmpq %rbp, %r12
je 0xc4884
cmpq %rbp, %r13
sete %al
cmpq %rbp, %rbx
sete %cl
orb %al, %cl
jne 0xc487c
movsd %xmm1, 0x40(%rsp)
movq %r13, %rbx
movq %r12, %r13
movq 0x28(%rsp), %rax
movq (%rax,%rbp,8), %r12
movq 0x38(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xc3ed9
movapd %xmm0, %xmm1
ucomisd 0x3bad6(%rip), %xmm0    # 0x1002e0
jbe 0xc4834
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movsd %xmm1, 0x50(%rsp)
callq 0xc4253
movsd 0x50(%rsp), %xmm1
ucomisd 0x18(%rsp), %xmm0
jbe 0xc4834
movsd %xmm0, 0x18(%rsp)
ucomisd 0x3baa4(%rip), %xmm1    # 0x1002e0
jae 0xc486b
movq 0x38(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xc4253
movsd 0x40(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0xc485e
movapd %xmm0, %xmm1
movq %r13, %r12
movq %rbx, %r13
movq 0x20(%rsp), %rbx
jmp 0xc487c
movq %r13, %r12
movq %rbx, %r13
movq 0x20(%rsp), %rbx
movsd 0x40(%rsp), %xmm1
incq %rbp
jmp 0xc47b9
minsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xc489c
movsd %xmm1, 0x10(%rsp)
incq %rbx
jmp 0xc4775
incq %r13
incq 0x30(%rsp)
jmp 0xc4765
movsd 0x10(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq