Example #1
0
        public static Box3 GaussPointsFit3(IList <Vector3> points)
        {
            Box3 result = new Box3(Vector3ex.Zero, Vector3ex.UnitX, Vector3ex.UnitY, Vector3ex.UnitZ, Vector3ex.One);
            int  count  = points.Count;

            result.Center = points[0];
            for (int i = 1; i < count; i++)
            {
                result.Center += points[i];
            }
            float num = 1f / (float)count;

            result.Center *= num;
            float num2 = 0f;
            float num3 = 0f;
            float num4 = 0f;
            float num5 = 0f;
            float num6 = 0f;
            float num7 = 0f;

            for (int j = 0; j < count; j++)
            {
                Vector3 vector = points[j] - result.Center;
                num2 += vector.x * vector.x;
                num3 += vector.x * vector.y;
                num4 += vector.x * vector.z;
                num5 += vector.y * vector.y;
                num6 += vector.y * vector.z;
                num7 += vector.z * vector.z;
            }
            num2          *= num;
            num3          *= num;
            num4          *= num;
            num5          *= num;
            num6          *= num;
            num7          *= num;
            float[,] array = new float[3, 3];
            array[0, 0]    = num2;
            array[0, 1]    = num3;
            array[0, 2]    = num4;
            array[1, 0]    = num3;
            array[1, 1]    = num5;
            array[1, 2]    = num6;
            array[2, 0]    = num4;
            array[2, 1]    = num6;
            array[2, 2]    = num7;
            float[,] symmetricSquareMatrix = array;
            EigenData eigenData = EigenDecomposition.Solve(symmetricSquareMatrix, true);

            result.Extents.x = eigenData.GetEigenvalue(0);
            result.Axis0     = eigenData.GetEigenvector3(0);
            result.Extents.y = eigenData.GetEigenvalue(1);
            result.Axis1     = eigenData.GetEigenvector3(1);
            result.Extents.z = eigenData.GetEigenvalue(2);
            result.Axis2     = eigenData.GetEigenvector3(2);
            return(result);
        }
Example #2
0
        public static Box2 GaussPointsFit2(IList <Vector2> points)
        {
            Box2 result = new Box2(Vector2ex.Zero, Vector2ex.UnitX, Vector2ex.UnitY, Vector2ex.One);
            int  count  = points.Count;

            result.Center = points[0];
            for (int i = 1; i < count; i++)
            {
                result.Center += points[i];
            }
            float num = 1f / (float)count;

            result.Center *= num;
            float num2 = 0f;
            float num3 = 0f;
            float num4 = 0f;

            for (int j = 0; j < count; j++)
            {
                Vector2 vector = points[j] - result.Center;
                num2 += vector.x * vector.x;
                num3 += vector.x * vector.y;
                num4 += vector.y * vector.y;
            }
            num2          *= num;
            num3          *= num;
            num4          *= num;
            float[,] array = new float[2, 2];
            array[0, 0]    = num2;
            array[0, 1]    = num3;
            array[1, 0]    = num3;
            array[1, 1]    = num4;
            float[,] symmetricSquareMatrix = array;
            EigenData eigenData = EigenDecomposition.Solve(symmetricSquareMatrix, true);

            result.Extents.x = eigenData.GetEigenvalue(0);
            result.Extents.y = eigenData.GetEigenvalue(1);
            result.Axis0     = eigenData.GetEigenvector2(0);
            result.Axis1     = eigenData.GetEigenvector2(1);
            return(result);
        }