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)); }
public void SetApexHeight(Point2D apexPoint, float value, int lod) { Preconditions.Assert(lod >= SubmapInfo.LodFactor, "Cant set apex height. Lod factor is too small"); var pixelSize = (int)Math.Pow(2, lod - SubmapInfo.LodFactor); if (Equals(apexPoint, SubmapInfo.DownLeftPoint)) { HeightmapArray.SetDownLeftApexMarginHeight(value, pixelSize); } else if (Equals(apexPoint, SubmapInfo.DownRightPoint)) { HeightmapArray.SetDownRightApexMarginHeight(value, pixelSize); } else if (Equals(apexPoint, SubmapInfo.TopLeftPoint)) { HeightmapArray.SetTopLeftApexMarginHeight(value, pixelSize); } else if (Equals(apexPoint, SubmapInfo.TopRightPoint)) { HeightmapArray.SetTopRightApexMarginHeight(value, pixelSize); } else { Preconditions.Fail(string.Format("Point {0} is not apex point", apexPoint)); } }
public HeightmapMargin SetLength(int newWorkingLength) { if (WorkingLength > newWorkingLength) { Preconditions.Assert(WorkingLength % newWorkingLength == 0, "New margin is not multiplication of current margin"); var newPointSize = Length / newWorkingLength; float [] newValues = new float[newWorkingLength + 1]; for (var i = 0; i < newWorkingLength; i++) { newValues[i] = _marginValues.Skip(i * newPointSize).Take(newPointSize).Sum() / newPointSize; //average } newValues[newWorkingLength] = _marginValues[WorkingLength]; return(new HeightmapMargin(newValues)); } else if (WorkingLength < newWorkingLength) { Preconditions.Assert(newWorkingLength % WorkingLength == 0, "New margin is not multiplication of current margin"); var newPointSize = newWorkingLength / WorkingLength; float [] newValues = new float[newWorkingLength + 1]; for (var i = 0; i < WorkingLength; i++) { for (var j = 0; j < newPointSize; j++) { newValues[i * newPointSize + j] = Mathf.Lerp(_marginValues[i], _marginValues[i + 1], (float)j / newPointSize); } } newValues[newWorkingLength] = _marginValues[WorkingLength]; return(new HeightmapMargin(newValues)); } return(new HeightmapMargin(_marginValues)); }
public HeightmapPosition GetPositionOnOtherHeightmap(int otherHeightmapWidth, int otherHeightmapHeight) { Preconditions.Assert(otherHeightmapHeight <= _heightmapSizeHeight, " Prawopodobnie margin jest barny z bardziej skomplikowanej heightmapy do mniej skomplikowanej."); Preconditions.Assert(otherHeightmapWidth <= _heightmapSizeWidth, " Prawopodobnie margin jest barny z bardziej skomplikowanej heightmapy do mniej skomplikowanej."); int xDivisor = _heightmapSizeWidth / otherHeightmapWidth; int yDivisor = _heightmapSizeHeight / otherHeightmapHeight; return(new HeightmapPosition((int)Math.Floor((double)_xPos / xDivisor), (int)Math.Floor((double)_yPos / yDivisor), otherHeightmapWidth, otherHeightmapHeight)); }
public float GetHeight(Point2D apexPoint) { Preconditions.Assert(SubmapInfo.IsPointPartOfSubmap(apexPoint), String.Format("Point {0} is not part of submap ", apexPoint)); int globalOffsetX = apexPoint.X - SubmapInfo.DownLeftX; int globalOffsetY = apexPoint.Y - SubmapInfo.DownLeftY; int offsetX = (int)(HeightmapArray.WorkingWidth * Mathf.InverseLerp(0, SubmapInfo.Width, globalOffsetX)); int offsetY = (int)(HeightmapArray.WorkingHeight * Mathf.InverseLerp(0, SubmapInfo.Height, globalOffsetY)); return(HeightmapArray.GetHeight(offsetX, offsetY)); }
public HeightmapPosition(int xPos, int yPos, int heightmapSizeWidth, int heightmapSizeHeight) { Preconditions.Assert(xPos >= 0 && xPos < heightmapSizeWidth, " xPos must be between 0 and heightmapSizeWidth"); Preconditions.Assert(yPos >= 0 && yPos < heightmapSizeHeight, " yPos must be between 0 and heightmapSizeHeight"); _xPos = xPos; _yPos = yPos; _heightmapSizeWidth = heightmapSizeWidth; _heightmapSizeHeight = heightmapSizeHeight; }
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 MarginPosition GetCommonSegment(MarginPosition other) { Preconditions.Assert(HaveCommonElementWith(other), string.Format("Cant get common element of margins {0} and {1} - they have no common element ", this, other)); if (IsHorizontal) { return(new MarginPosition( new Point2D(Math.Max(StartPoint.X, other.StartPoint.X), StartPoint.Y), new Point2D(Math.Min(EndPoint.X, other.EndPoint.X), EndPoint.Y))); } else { return(new MarginPosition( new Point2D(StartPoint.X, Math.Max(StartPoint.Y, other.StartPoint.Y)), new Point2D(EndPoint.X, Math.Min(EndPoint.Y, other.EndPoint.Y)))); } }
public HeightmapArray simplyfy(int lodFactor) { Preconditions.Assert(lodFactor > 0, "lodFactor must be bigger than 0"); int newWorkingWidth = WorkingWidth / (int)Math.Pow(2, lodFactor - 1); int newWorkingHeight = WorkingHeight / (int)Math.Pow(2, lodFactor - 1); Preconditions.Assert(newWorkingWidth >= 4, "Minimal heightmap width dimension must be at least 4, and would be " + newWorkingWidth); Preconditions.Assert(newWorkingHeight >= 4, "Minimal heightmap height dimension must be at least 4, and would be " + newWorkingHeight); if (lodFactor != 1) { return(new HeightmapArray(simplyfy(newWorkingWidth, newWorkingHeight))); } else { return(this); } }
public void SetBottomMargin(HeightmapMarginWithInfo margin) { Preconditions.Assert(margin.Position.IsHorizontal, string.Format("Bottom margin {0} cant be set as is not horizontal", margin)); HeightmapArray.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)); HeightmapArray.SetLeftMargin(margin.HeightmapMargin); }
private void AssertPositionsAreCorrect(Point2D endPos, Point2D startPos) { Preconditions.Assert(!Equals(startPos, endPos), string.Format("Start pos {0} and end pos {1} of margin can't be the same", startPos, endPos)); Preconditions.Assert(startPos.X == endPos.X || startPos.Y == endPos.Y, string.Format("Start pos {0} and end pos {1} are not horizontal nor vertical", startPos, endPos)); }
private void AssertMarginHasProperLength(HeightmapMargin margin, int workingLength) { Preconditions.Assert(margin.WorkingLength == workingLength, "Cant set margin. It has wrong length. Old working length " + workingLength + " new working length " + margin.WorkingLength); }