Esempio n. 1
0
        public void ConvertVector_ReturnPlanePoint()
        {
            PlanePoint mainPoint = new PlanePoint(new double[] { 2, 2, 4 });
            Vector     normal    = new Vector(new double[] { 1, 0, 1 });

            Vector[] basis = new[]
            {
                new Vector(new double[] { -1, 0, 1 }),
                new Vector(new double[] { 0, 1, 0 }),
            };
            Hyperplane hyperplane = new Hyperplane(mainPoint, normal);

            hyperplane.Basis = basis;
            Vector vector = new Vector(new double[] { 2, 3 });
            Vector expect = new Vector(new double[] { -2, 3, 2 });

            Vector actual = hyperplane.ConvertVector(vector);

            actual.Should().Be(expect);
        }
        private IConvexHull FindConvexHullNd(IList <PlanePoint> points)
        {
            int dim = points[0].Dim;

            Hyperplane        firstPlane = _planeFinder.FindFirstPlane(points);
            CuratorConvexHull curator    = new CuratorConvexHull(firstPlane);

            foreach (IFace currentFace in curator.GetFaces())
            {
                bool[]            pointsMap   = new bool[points.Count];
                List <PlanePoint> planePoints = new List <PlanePoint>();
                for (int i = 0; i < points.Count; i++)
                {
                    if (!currentFace.Hyperplane.IsPointInPlane(points[i]))
                    {
                        continue;
                    }
                    pointsMap[i] = true;
                    planePoints.Add(currentFace.Hyperplane.ConvertPoint(points[i]));
                }

                currentFace.ConvexHull = FindConvexHull(planePoints);

                if (planePoints.Count == points.Count)
                {
                    return(currentFace.ConvexHull);
                }
                foreach (ICell edge in currentFace.ConvexHull.Cells)
                {
                    Hyperplane facePlane   = currentFace.Hyperplane;
                    Vector     innerVector = facePlane.ConvertVector(-edge.Hyperplane.Normal);
                    PlanePoint mainPoint   = edge.Hyperplane.MainPoint.GetPoint(dim);
                    Vector[]   edgeBasis   = edge.Hyperplane.Basis.Select(facePlane.ConvertVector).ToArray();

                    double minCos     = double.MaxValue;
                    Vector nextVector = default;
                    for (int i = 0; i < points.Count; i++)
                    {
                        if (pointsMap[i])
                        {
                            continue;
                        }
                        Vector newVector = Point.ToVector(mainPoint, points[i]);
                        newVector = edgeBasis.GetOrthonormalVector(newVector);
                        if (Tools.EQ(newVector.Length))
                        {
                            continue;
                        }
                        double newCos = newVector.Cos(innerVector);
                        if (Tools.GT(newCos, minCos))
                        {
                            continue;
                        }
                        minCos     = newCos;
                        nextVector = newVector;
                    }

                    Vector[]   newFaceBasis   = CreateFaceBasis(edgeBasis, nextVector);
                    Hyperplane nextHyperplane = Hyperplane.Create(mainPoint, newFaceBasis);
                    nextHyperplane.SetOrientationNormal(planePoints);

                    curator.AddNewPlane(nextHyperplane, currentFace);
                }
            }

            return(curator.GetConvexHull());
        }