예제 #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);
        }
예제 #2
0
        public static Line3 LeastsSquaresLineFit3(IList <Vector3> points)
        {
            Line3 result = default(Line3);
            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 i = 0; i < count; i++)
            {
                Vector3 vector = points[i] - 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]    = num5 + num7;
            array[0, 1]    = -num3;
            array[0, 2]    = -num4;
            array[1, 0]    = array[0, 1];
            array[1, 1]    = num2 + num7;
            array[1, 2]    = -num6;
            array[2, 0]    = array[0, 2];
            array[2, 1]    = array[1, 2];
            array[2, 2]    = num2 + num5;
            EigenData eigenData = EigenDecomposition.Solve(array, false);

            result.Direction = eigenData.GetEigenvector3(2);
            return(result);
        }
예제 #3
0
        public static Plane3 LeastSquaresPlaneFit3(IList <Vector3> points)
        {
            Vector3 vector = Vector3ex.Zero;
            int     count  = points.Count;

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

            vector *= num;
            float num2 = 0f;
            float num3 = 0f;
            float num4 = 0f;
            float num5 = 0f;
            float num6 = 0f;
            float num7 = 0f;

            for (int i = 0; i < count; i++)
            {
                Vector3 vector2 = points[i] - vector;
                num2 += vector2.x * vector2.x;
                num3 += vector2.x * vector2.y;
                num4 += vector2.x * vector2.z;
                num5 += vector2.y * vector2.y;
                num6 += vector2.y * vector2.z;
                num7 += vector2.z * vector2.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;
            EigenData eigenData   = EigenDecomposition.Solve(array, false);
            Vector3   eigenvector = eigenData.GetEigenvector3(2);

            return(new Plane3(ref eigenvector, ref vector));
        }