public static Strut ReversedFromIndex(Configuration config, int index) { var key = new Tuple<int, bool>(index, true); if (!struts.ContainsKey(key)) { struts[key] = new Strut(config, index, true); } return struts[key]; }
public static Strut ReversedFromIndex(Configuration config, int index) { var key = new Tuple <int, bool>(index, true); if (!struts.ContainsKey(key)) { struts[key] = new Strut(config, index, true); } return(struts[key]); }
public static StrutLayout[] ConcentricFromStartingPoints( Configuration config, HashSet <int> startingPoints, int numLayers ) { List <HashSet <int> > curPointsByGroup = new List <HashSet <int> >(); foreach (int point in startingPoints) { HashSet <int> group = new HashSet <int>(); group.Add(point); curPointsByGroup.Add(group); } List <StrutLayoutSegment> spokeSegments = new List <StrutLayoutSegment>(); HashSet <Strut>[] strutsByGroup = new HashSet <Strut>[] { new HashSet <Strut>(), new HashSet <Strut>(), new HashSet <Strut>(), new HashSet <Strut>(), new HashSet <Strut>(), new HashSet <Strut>(), }; List <StrutLayoutSegment> circleSegments = new List <StrutLayoutSegment>(); HashSet <int> usedStrutIndices = new HashSet <int>(); while (numLayers > 0) { HashSet <Strut> layer1 = new HashSet <Strut>(); List <HashSet <int> > nextPointsByGroup = new List <HashSet <int> >(); for (int i = 0; i < curPointsByGroup.Count(); i++) { var group = curPointsByGroup[i]; HashSet <int> newPoints = new HashSet <int>(); foreach (int point in group) { foreach (var connected in edgeDictionary[point]) { int strutIndex = connected.Value.Item1; if (usedStrutIndices.Contains(strutIndex)) { continue; } usedStrutIndices.Add(strutIndex); bool reversed = connected.Value.Item2; Strut strut = reversed ? Strut.ReversedFromIndex(config, strutIndex) : Strut.FromIndex(config, strutIndex); layer1.Add(strut); strutsByGroup[i].Add(strut); int connectedPoint = connected.Key; newPoints.Add(connectedPoint); } } nextPointsByGroup.Add(newPoints); } spokeSegments.Add(new StrutLayoutSegment(layer1)); numLayers--; if (numLayers == 0) { break; } curPointsByGroup = nextPointsByGroup; HashSet <Strut> layer2 = new HashSet <Strut>(); for (int i = 0; i < curPointsByGroup.Count(); i++) { var group = curPointsByGroup[i]; // If we're not a circle, we need to start on one of the edges int currentPoint = group.First(); foreach (int pt1 in group) { var connectedPoints = edgeDictionary[pt1].Keys.Intersect(group); if (connectedPoints.Count() == 1) { currentPoint = pt1; break; } } var pointsLeft = new HashSet <int>(group); HashSet <Strut> circleStruts = new HashSet <Strut>(); while (true) { int?nextPointInLoop = null; var connectedPoints = edgeDictionary[currentPoint].Keys; foreach (int connectedPoint in connectedPoints) { if (!group.Contains(connectedPoint)) { continue; } var strutInfo = edgeDictionary[currentPoint][connectedPoint]; var strutIndex = strutInfo.Item1; if (usedStrutIndices.Contains(strutIndex)) { continue; } usedStrutIndices.Add(strutIndex); var reversed = strutInfo.Item2; Strut strut = reversed ? Strut.ReversedFromIndex(config, strutIndex) : Strut.FromIndex(config, strutIndex); layer2.Add(strut); circleStruts.Add(strut); strutsByGroup[i].Add(strut); if (pointsLeft.Contains(connectedPoint)) { nextPointInLoop = connectedPoint; } break; } pointsLeft.Remove(currentPoint); if (nextPointInLoop.HasValue) { currentPoint = nextPointInLoop.Value; } else { break; } } circleSegments.Add(new StrutLayoutSegment(circleStruts)); } spokeSegments.Add(new StrutLayoutSegment(layer2)); numLayers--; } return(new StrutLayout[] { new StrutLayout(spokeSegments.ToArray()), new StrutLayout( strutsByGroup.Select(set => new StrutLayoutSegment(set)).ToArray() ), new StrutLayout(circleSegments.ToArray()), }); }