Beispiel #1
0
        public static IEnumerable <T> ClosestData <T>(this DomainTree <T> tree,
                                                      DomainBox searchBox,
                                                      bool tightBox      = false,
                                                      double maxDistance = double.PositiveInfinity,
                                                      double tolerance   = Tolerance.Distance)
        {
            if (searchBox == null)
            {
                return(new List <T>());
            }

            Func <DomainTree <T>, double> evaluationMethod = (x) => x.DomainBox?.SquareDistance(searchBox) ?? double.PositiveInfinity;

            Func <DomainTree <T>, double> worstCaseMethod;

            if (tightBox)
            {
                worstCaseMethod = (x) => x.DomainBox?.FurthestTightSquareDistance(searchBox) ?? double.PositiveInfinity;
            }
            else
            {
                worstCaseMethod = (x) => x.DomainBox?.FurthestSquareDistance(searchBox) ?? double.PositiveInfinity;
            }

            return(ClosestData <DomainTree <T>, T>(tree, evaluationMethod, worstCaseMethod, maxDistance * maxDistance, tolerance * tolerance));
        }
        public static double FurthestTightSquareDistance(this DomainBox box1, DomainBox box2)
        {
            // Find in which axis the distance between the outer bounderies of the boxes is the least
            int    index = 0;
            double min   = double.MaxValue;

            for (int i = 0; i < box1.Domains.Length; i++)
            {
                double temp = ShortestEdgeDistance(box1.Domains[i], box2.Domains[i]);
                if (temp < min)
                {
                    min   = temp;
                    index = i;
                }
            }

            double sq = min * min;

            for (int i = 0; i < box1.Domains.Length; i++)
            {
                if (i != index)
                {
                    sq += Math.Pow(FurthestEdgeDistance(box1.Domains[i], box2.Domains[i]), 2);
                }
            }

            return(sq);
        }
Beispiel #3
0
        public static double FurthestTightSquareDistance(this DomainBox box1, DomainBox box2)
        {
            if (box1 == null || box2 == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Cannot query the furthest square distance if either domain box is null.");
                return(0);
            }

            // Find in which axis the distance between the outer bounderies of the boxes is the least
            int    index = 0;
            double min   = double.MaxValue;

            for (int i = 0; i < box1.Domains.Length; i++)
            {
                double temp = ShortestEdgeDistance(box1.Domains[i], box2.Domains[i]);
                if (temp < min)
                {
                    min   = temp;
                    index = i;
                }
            }

            double sq = min * min;

            for (int i = 0; i < box1.Domains.Length; i++)
            {
                if (i != index)
                {
                    sq += Math.Pow(FurthestEdgeDistance(box1.Domains[i], box2.Domains[i]), 2);
                }
            }

            return(sq);
        }
Beispiel #4
0
 public static bool IsInRange(this DomainBox box1, DomainBox box2, double tolerance = Tolerance.Distance)
 {
     if (box1 == null || box2 == null)
     {
         return(false); //Cannot be in range if either is null
     }
     return(SquareDistance(box1, box2) < (tolerance * tolerance));
 }
Beispiel #5
0
 public static DomainTree <T> DomainTreeLeaf <T>(T data, DomainBox domainBox)
 {
     return(new DomainTree <T>()
     {
         Values = new List <T>()
         {
             data
         }, DomainBox = domainBox
     });
 }
Beispiel #6
0
        public static double SquareDistance(this DomainBox box1, DomainBox box2)
        {
            if (box1 == null || box2 == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Cannot query the square distance if either domain box is null.");
                return(0);
            }

            return(box1.Domains.Zip(box2.Domains, (a, b) => Math.Pow(Distance(a, b), 2)).Sum());
        }
Beispiel #7
0
        public static IEnumerable <T> ItemsInRange <T>(this DomainTree <T> tree, DomainBox box, double tolerance = Tolerance.Distance)
        {
            if (box == null)
            {
                return(new List <T>());
            }

            Func <DomainTree <T>, bool> isWithinSearch = x => x.DomainBox?.IsInRange(box, tolerance) ?? false;

            return(ItemsInRange <DomainTree <T>, T>(tree, isWithinSearch));
        }
Beispiel #8
0
        private static Output <List <DomainTree <T> >, List <DomainTree <T> > > SplitList <T>(List <DomainTree <T> > list, int sampleSize = 60)
        {
            sampleSize = Math.Min(list.Count, sampleSize);

            int step = (int)Math.Floor((decimal)(list.Count / sampleSize));
            // find centre
            DomainBox box = list[0].DomainBox;

            for (int i = 1; i < sampleSize; i++)
            {
                box += list[i * step].DomainBox;
            }

            int    index = -1;
            double max   = double.MinValue;

            for (int i = 0; i < box.Domains.Length; i++)
            {
                double temp = box.Domains[i].Max - box.Domains[i].Min;
                if (temp > max)
                {
                    max   = temp;
                    index = i;
                }
            }

            double centre = (box.Domains[index].Min + box.Domains[index].Max) / 2;

            // split into two
            List <DomainTree <T> > one = new List <DomainTree <T> >();
            List <DomainTree <T> > two = new List <DomainTree <T> >();

            // do the splitting

            foreach (DomainTree <T> data in list)
            {
                if ((data.DomainBox.Domains[index].Min + data.DomainBox.Domains[index].Min) / 2 < centre)
                {
                    one.Add(data);
                }
                else
                {
                    two.Add(data);
                }
            }

            return(new Output <List <DomainTree <T> >, List <DomainTree <T> > >()
            {
                Item1 = one, Item2 = two
            });
        }
Beispiel #9
0
        public static DomainBox DomainBox(IEnumerable <double[]> values)
        {
            if (!values.Any())
            {
                return(null);
            }

            DomainBox result = new DomainBox()
            {
                Domains = values.Select(x => Domain(x)).ToArray()
            };

            if (result.Domains.Any(x => x == null))
            {
                return(null);
            }

            return(result);
        }
Beispiel #10
0
 public static double SquareDistance(this DomainBox box1, DomainBox box2)
 {
     return(box1.Domains.Zip(box2.Domains, (a, b) => Math.Pow(Distance(a, b), 2)).Sum());
 }
Beispiel #11
0
 public static bool IsInRange(this DomainBox box1, DomainBox box2, double tolerance = Tolerance.Distance)
 {
     return(SquareDistance(box1, box2) < (tolerance * tolerance));
 }