private bool PopulateDimension <T>(Dimension <T> dimension, List <SpacePointSource <T> > points) { if (points.Count == 0) { return(false); } points.Sort(new SpacePointComparer <T>(dimension.Index)); //points.Sort((a, b) => a.InitialPositions[dimension.Index].CompareTo(b.InitialPositions[dimension.Index])); DimensionPoint <T> head = null; DimensionPoint <T> curr = null; DimensionPoint <T> prev = null; for (var i = 0; i < points.Count; ++i) { var sp = points[i]; if (sp == null) { continue; } var position = sp.InitialPositions[dimension.Index]; if (prev != null) { if (dimension.Eq(prev.Position, position)) { prev.AddPoint(sp); continue; } } curr = new DimensionPoint <T>(dimension) { Position = position }; curr.HeadLink = new DimensionLink <T>(0, curr); curr.TailLink = curr.HeadLink; curr.AddPoint(sp); if (prev != null) { prev.HeadLink.AssignNext(curr.HeadLink); } else { head = curr; } prev = curr; } dimension.HeadDimPoint = head; dimension.TailDimPoint = curr; dimension.Count += points.Count; // ReSharper disable PossibleNullReferenceException DimensionLink <T> link = dimension.HeadDimPoint.HeadLink; // ReSharper restore PossibleNullReferenceException // if this is the top level if (link.Next == null || link.Next.Next == null) { return(true); } DimensionLink <T> prevUpper = null; DimensionLink <T> firstUpper = null; while (link != null) { var upper = new DimensionLink <T>((byte)(link.Level + 1)); if (firstUpper == null) { firstUpper = upper; } link.AssignUpper(upper); link.DimPoint.TailLink = upper; if (prevUpper != null) { prevUpper.AssignNext(upper); } // if this is the end if (link.Next == null) { link = firstUpper; firstUpper = null; prevUpper = null; // if this is the top level if (link.Next == null || link.Next.Next == null) { return(true); } continue; } link = link.Next.Next; // the next is the end and it is two links away if (link.Next != null && link.Next.Next == null) { link = link.Next; } prevUpper = upper; } return(true); }
bool ISpaceManager.TryFindDimensionPoint <T>(Dimension <T> dimension, float position, out DimensionPoint <T> left, out DimensionPoint <T> right) { if (dimension.HeadDimPoint == null) { left = null; right = null; return(false); } var candidateLeft = dimension.HeadDimPoint.TailLink; var candidateRight = dimension.TailDimPoint.TailLink; var leftLimit = candidateLeft.DimPoint.Position; var rightLimit = candidateRight.DimPoint.Position; if (position > (leftLimit - dimension.Epsillon) && position < (rightLimit + dimension.Epsillon)) { while (true) { while (candidateLeft.Next != null && candidateLeft.Next != candidateRight) { if ((candidateLeft.Next.DimPoint.Position - dimension.Epsillon) < position) { candidateLeft = candidateLeft.Next; } else { candidateRight = candidateLeft.Next; break; } } if (candidateLeft.Level > 0) { candidateLeft = candidateLeft.Lower; candidateRight = candidateRight.Lower; continue; } if (dimension.Eq(position, candidateLeft.DimPoint.Position)) { left = right = candidateLeft.DimPoint; return(true); } if (dimension.Eq(position, candidateRight.DimPoint.Position)) { left = right = candidateRight.DimPoint; return(true); } break; } left = candidateLeft.DimPoint; right = candidateRight.DimPoint; return(false); } else if (position < leftLimit) { left = null; right = candidateLeft.DimPoint; return(false); } else if (position > rightLimit) { left = candidateRight.DimPoint; right = null; return(false); } left = null; right = null; return(false); }
bool ISpaceManager.Reposition <T>(Dimension <T> dimension, SpacePoint <T> sp, float position) { var point = sp.Dimensions[dimension.Index]; var next = position; var prev = point.Position; // no move if (dimension.Eq(prev, next)) { return(false); } if (next < prev) { if (point.NumberPoints > 1) { if (RemovePointFromDimensions(sp, dimension.Index)) { sp.Dimensions[dimension.Index] = null; _spaceManager.AddPoint(dimension, sp, next); } } else { var link = point.HeadLink; if (link.Prev == null || link.Prev.DimPoint.Position < next) { point.Position = next; } else { if (RemovePointFromDimensions(sp, dimension.Index)) { sp.Dimensions[dimension.Index] = null; _spaceManager.AddPoint(dimension, sp, next); } } } } else // THIS IS: if (prev < next) { if (point.NumberPoints > 1) { if (RemovePointFromDimensions(sp, dimension.Index)) { sp.Dimensions[dimension.Index] = null; _spaceManager.AddPoint(dimension, sp, next); } } else { var link = point.HeadLink; if (link.Next == null || link.Next.DimPoint.Position > next) { point.Position = next; } else { if (RemovePointFromDimensions(sp, dimension.Index)) { sp.Dimensions[dimension.Index] = null; _spaceManager.AddPoint(dimension, sp, next); } } } } return(true); }