Example #1
0
        public unsafe static OrientedBox Create(byte *positions, int vertexCount, int stride)
        {
            OrientedBox box = new OrientedBox();

            var minValues = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
            var maxValues = new Vector3(float.MinValue, float.MinValue, float.MinValue);

            //Compute Covariance Matrix
            Matrix c = Matrix.CorrelationMatrix(positions, vertexCount, stride);

            Vector3[] eigenVectors;
            Vector3   eigenValues;

            eigenVectors = Matrix.ComputeEigenVectors(c, 1.0e-10f, out eigenValues);

            var r = eigenVectors[0];
            var s = eigenVectors[1];
            var T = eigenVectors[2];

            Vector3 t;

            for (int i = 0; i < vertexCount; i++)
            {
                Vector3 *pter = (Vector3 *)(positions + i * stride);
                t         = new Vector3(Vector3.Dot(*pter, r), Vector3.Dot(*pter, s), Vector3.Dot(*pter, T));
                minValues = Vector3.Min(minValues, t);
                maxValues = Vector3.Max(maxValues, t);
            }

            t            = 0.5f * (minValues + maxValues);
            box.extends  = 0.5f * (maxValues - minValues);
            box.center   = t.X * r + t.Y * s + t.Z * T;
            box.rotation = new Matrix(r, s, T, new Vector3());
            box.Update(Matrix.Identity);

            return(box);
        }
Example #2
0
        public static OrientedBox Create(IEnumerable <Vector3> positions)
        {
            OrientedBox box = new OrientedBox();

            var minValues = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
            var maxValues = new Vector3(float.MinValue, float.MinValue, float.MinValue);

            //Compute Covariance Matrix
            Matrix c = Matrix.CorrelationMatrix(positions);

            Vector3[] eigenVectors;
            Vector3   eigenValues;

            eigenVectors = Matrix.ComputeEigenVectors(c, 1.0e-10f, out eigenValues);

            var r = eigenVectors[0];
            var s = eigenVectors[1];
            var T = eigenVectors[2];

            Vector3 t;

            foreach (var v in positions)
            {
                t         = new Vector3(Vector3.Dot(v, r), Vector3.Dot(v, s), Vector3.Dot(v, T));
                minValues = Vector3.Min(minValues, t);
                maxValues = Vector3.Max(maxValues, t);
            }

            t            = 0.5f * (minValues + maxValues);
            box.extends  = 0.5f * (maxValues - minValues);
            box.center   = t.X * r + t.Y * s + t.Z * T;
            box.rotation = new Matrix(r, s, T, new Vector3());
            box.Update(Matrix.Identity);

            return(box);
        }