In my current dual-circuit im implementation, using a very simple way to solve QEF. Since QEF is essentially a least squares approximation, I found the easiest way to calculate QEF by calculating a pseudo-inverse. This pseudoinverse can be calculated using any algebraic library in your language.
This is the code I'm using:
public static Vector<float> CalculateCubeQEF(Vector3[] normals, Vector3[] positions, Vector3 meanPoint) { var A = DenseMatrix.OfRowArrays(normals.Select(e => new[] { eX, eY, eZ }).ToArray()); var b = DenseVector.OfArray(normals.Zip(positions.Select(p => p - meanPoint), Vector3.Dot).ToArray()); var pseudo = PseudoInverse(A); var leastsquares = pseudo.Multiply(b); return leastsquares + DenseVector.OfArray(new[] { meanPoint.X, meanPoint.Y, meanPoint.Z }); }
Function inputs are the intersection and normal points, and the midpoint is the average of these intersection points.
To summarize the math: this function calculates a point lying at the intersection of all planes defined by intersection points and normals. Since this does not have an exact solution, the least squares approximation is calculated, which finds the “least erroneous” point. In addition, the intersection points “move”, so that the midpoint becomes the beginning. This ensures that if there are multiple solutions for QEF, the solution closest to the midpoint is selected.
source share