public HeightmapMarginWithInfo UpdateWherePossible(HeightmapMarginWithInfo newMargin)
        {
            //Preconditions.Assert( _heightmapMargin.Length == newMargin.HeightmapLength, todo change not to length but to lods
            //    string.Format("Current margin length is {0} != new margin length == {1} ", _heightmapMargin.Length, newMargin.HeightmapLength));
            Preconditions.Assert(
                (newMargin.Position.IsHorizontal && Position.IsHorizontal) ||
                (newMargin.Position.IsVertical && Position.IsVertical),
                string.Format("Current and new margins are one vertical one horizontal: Old {0} new {1}",
                              _heightmapMargin, newMargin));

            bool haveCommonElements = Position.HaveCommonElementWith(newMargin.Position);

            Preconditions.Assert(haveCommonElements,
                                 string.Format("Current {0} and new {1} margin dont have common elements", HeightmapMargin, newMargin));

            MarginPosition commonSegment = Position.GetCommonSegment(newMargin.Position);

            var ourStartPercent = Position.InvLerp(commonSegment.StartPoint);
            var ourStartOffset  = (int)Math.Round((double)HeightmapWorkingLength * ourStartPercent);

            var ourEndPercent = Position.InvLerp(commonSegment.EndPoint);
            var ourEndOffset  = (int)Math.Round((double)HeightmapWorkingLength * ourEndPercent);

            var theirStartPercent = newMargin.Position.InvLerp(commonSegment.StartPoint);
            var theirStartOffset  = (int)Math.Round((double)newMargin.HeightmapWorkingLength * theirStartPercent);

            var theirEndPercent = newMargin.Position.InvLerp(commonSegment.EndPoint);
            var theirEndOffset  = (int)Math.Round((double)newMargin.HeightmapWorkingLength * theirEndPercent);

            return(new HeightmapMarginWithInfo(
                       SetMarginSubElement(ourStartOffset, ourEndOffset, theirStartOffset, theirEndOffset, newMargin),
                       Position, LodFactor));
        }
        private HeightmapMargin SetMarginSubElement(int ourStartOffset, int ourEndOffset, int theirStartOffset,
                                                    int theirEndOffset, HeightmapMarginWithInfo newMargin)
        {
            Preconditions.Assert(
                (ourEndOffset - ourStartOffset) == (theirEndOffset - theirStartOffset),
                String.Format(
                    "Cant set subelement. Offset lengths are not equal. Our start {0} end {1} their start {2} end {3} ",
                    ourStartOffset, ourEndOffset, theirStartOffset, theirEndOffset));
            var length    = ourEndOffset - ourStartOffset;
            var newValues = new float[_heightmapMargin.Length];

            Array.Copy(_heightmapMargin.MarginValues, newValues, newValues.Length);
            for (int i = 0; i < length; i++)
            {
                newValues[ourStartOffset + i] = newMargin.HeightmapMargin.MarginValues[theirStartOffset + i];
            }
            return(new HeightmapMargin(newValues));
        }
 public void SetBottomMargin(HeightmapMarginWithInfo margin)
 {
     Preconditions.Assert(margin.Position.IsHorizontal,
                          string.Format("Bottom margin {0} cant be set as is not horizontal", margin));
     _submap.Heightmap.SetBottomMargin(margin.HeightmapMargin);
 }
 public void SetLeftMargin(HeightmapMarginWithInfo margin)
 {
     Preconditions.Assert(margin.Position.IsVertical,
                          string.Format("Left margin {0} cant be set as is not vertical", margin));
     _submap.Heightmap.SetLeftMargin(margin.HeightmapMargin);
 }