// File: Luthaf[P]zeopp/OMS.cc
// Function: PlaneSide(double*, double*, double*)
double PlaneSide (double *Vector1, double *Vector2, double *Point)
{

	// Elements for system resolution
	Matrix3d A;
	Vector3d b(Point[X_COORD], Point[Y_COORD], Point[Z_COORD]);
	Vector3d x;

	// Vectors defining the plane
	Vector3d v1(Vector1[X_COORD], Vector1[Y_COORD], Vector1[Z_COORD]);
	Vector3d v2(Vector2[X_COORD], Vector2[Y_COORD], Vector2[Z_COORD]);
	Vector3d n(Vector1[X_COORD], Vector1[Y_COORD], Vector1[Z_COORD]);
	
	n = n.cross(v2);


	/*cout << "Vector 1: \n" << v1 << "\n";
	cout << "Vector 2: \n" << v2 << "\n";
	cout << "Normal: \n" << n << "\n";

	cout << "B: \n" << b << "\n";*/

	/**** Fill the matrix ****/
	// Each column contains one vector, so the system solved returns the coordinates of the point in the system v1,v2,n
	for (int i = 0; i < DIM; i++)
		for (int j=0; j<DIM; j++)
			if (i == 0)			
				A(j,i) = v1(j);
			else if (i == 1) 
				A(j,i) = v2(j);
			else 	
				A(j,i) = n(j);

	//cout << "A\n" << A << "\n";

	/**** Compute the system ****/
	x = A.colPivHouseholderQr().solve(b);


	//cout << "x: \n" << x << "\n";

	/**** Return the value of the third element (lambda parameter of the normal) ****/
	return x(Z_COORD);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x175210
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %rdx
addq $0x8, %rdx
movq -0x18(%rbp), %rcx
addq $0x10, %rcx
leaq -0x78(%rbp), %rdi
callq 0x174e70
leaq -0x90(%rbp), %rdi
callq 0x175230
movq -0x8(%rbp), %rsi
movq -0x8(%rbp), %rdx
addq $0x8, %rdx
movq -0x8(%rbp), %rcx
addq $0x10, %rcx
leaq -0xa8(%rbp), %rdi
callq 0x174e70
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movq -0x10(%rbp), %rcx
addq $0x10, %rcx
leaq -0xc0(%rbp), %rdi
callq 0x174e70
movq -0x8(%rbp), %rsi
movq -0x8(%rbp), %rdx
addq $0x8, %rdx
movq -0x8(%rbp), %rcx
addq $0x10, %rcx
leaq -0xd8(%rbp), %rdi
callq 0x174e70
leaq -0xf0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
callq 0x174f00
leaq -0xd8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x175150
movl $0x0, -0xf4(%rbp)
cmpl $0x3, -0xf4(%rbp)
jge 0x174131
movl $0x0, -0xf8(%rbp)
cmpl $0x3, -0xf8(%rbp)
jge 0x17411b
cmpl $0x0, -0xf4(%rbp)
jne 0x174074
movslq -0xf8(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x175180
movsd (%rax), %xmm0
movsd %xmm0, -0x1f0(%rbp)
movslq -0xf8(%rbp), %rsi
movslq -0xf4(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x175250
movsd -0x1f0(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x174105
cmpl $0x1, -0xf4(%rbp)
jne 0x1740c1
movslq -0xf8(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x175180
movsd (%rax), %xmm0
movsd %xmm0, -0x1f8(%rbp)
movslq -0xf8(%rbp), %rsi
movslq -0xf4(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x175250
movsd -0x1f8(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x174103
movslq -0xf8(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x175180
movsd (%rax), %xmm0
movsd %xmm0, -0x200(%rbp)
movslq -0xf8(%rbp), %rsi
movslq -0xf4(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x175250
movsd -0x200(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x174105
jmp 0x174107
movl -0xf8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf8(%rbp)
jmp 0x174017
jmp 0x17411d
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x174000
leaq -0x1e8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x175310
leaq -0x1e8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x175350
movq %rax, -0x108(%rbp)
movq %rdx, -0x100(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x1753d0
leaq -0x90(%rbp), %rdi
movl $0x2, %esi
callq 0x175180
movsd (%rax), %xmm0
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq