Ejemplo n.º 1
0
        private static TriangleStorage CalcCentersDistances(List <VectorCenter> centers)
        {
            var matr           = new TriangleStorage(centers.Count);
            var processorCount = Environment.ProcessorCount;

            Parallel.For(0, processorCount, new ParallelOptions()
            {
                MaxDegreeOfParallelism = processorCount
            }, index =>
            {
                for (int i = 0; i < centers.Count; i++)
                {
                    if ((i % processorCount) != index)
                    {
                        continue;
                    }
                    for (int j = i + 1; j < centers.Count; j++)
                    {
                        matr[i, j] = centers[i].Distance(centers[j].Center);
                    }
                }
            });
            return(matr);
        }
Ejemplo n.º 2
0
        private static Dictionary <int, List <VectorCenter> > GroupByDistance(List <VectorCenter> centers, double distMax, TriangleStorage matr)
        {
            int cgroups = 0;
            var groups  = new Dictionary <VectorCenter, int>();

            double bestDist = 0;

            for (int idx = 0; idx < centers.Count; idx++)
            {
                if (groups.ContainsKey(centers[idx]))
                {
                    continue;
                }
                int bestIndex = -1;
                // looking for closest
                for (int sidx = 0; sidx < centers.Count; sidx++)
                {
                    if (idx == sidx)
                    {
                        continue;
                    }
                    var d = matr[idx, sidx];
                    if (d > distMax || (bestIndex != -1 && bestDist <= d))
                    {
                        continue;
                    }
                    bestIndex = sidx;
                    bestDist  = d;
                }
                // if not found - new group
                if (bestIndex == -1)
                {
                    cgroups++;
                    groups[centers[idx]] = cgroups;
                }
                else if (!groups.ContainsKey(centers[bestIndex]))
                {
                    cgroups++;
                    groups[centers[idx]]       = cgroups;
                    groups[centers[bestIndex]] = cgroups;
                }
                else
                {
                    groups[centers[idx]] = groups[centers[bestIndex]];
                }
            }
            var grouped = new Dictionary <int, List <VectorCenter> >();

            foreach (var pair in groups)
            {
                if (!grouped.ContainsKey(pair.Value))
                {
                    grouped[pair.Value] = new List <VectorCenter>();
                }
                grouped[pair.Value].Add(pair.Key);
            }
            int sum = grouped.Sum(x => x.Value.Count);

            return(grouped);
        }