protected override void AddDoor(Vector3Int from, Vector3Int to) { var length = doors.HybridDoorModeData.DefaultLength; var doorLine = new DoorLineGrid2D() { From = from, To = to, Length = length, }; var line = new OrthogonalLineGrid2D(from.ToCustomIntVector2(), to.ToCustomIntVector2()); if (doors.HybridDoorModeData.DoorLines.Any(x => x == doorLine)) { return; } if (line.Length >= length - 1) { Undo.RecordObject(doors, "Added door positions"); doors.HybridDoorModeData.DoorLines.Add(doorLine); EditorUtility.SetDirty(doors); } }
public bool Deserialize(IParser reader, Type expectedType, Func <IParser, Type, object> nestedObjectDeserializer, out object value) { if (expectedType != typeof(OrthogonalLineGrid2D)) { value = null; return(false); } var valueObject = nestedObjectDeserializer(reader, typeof(List <Vector2Int>)); if (valueObject == null) { throw new ParsingException($"Given element could not be parsed into {nameof(OrthogonalLineGrid2D)}."); } var intVector2List = (List <Vector2Int>)valueObject; if (intVector2List.Count != 2) { throw new ParsingException($"Given element could not be parsed into {nameof(OrthogonalLineGrid2D)}. There must be exactly 2 elements of type {nameof(Vector2Int)} in the array."); } value = new OrthogonalLineGrid2D(intVector2List[0], intVector2List[1]); return(true); }
public void Contains_Outside_ReturnsMinusOne() { { var line = new OrthogonalLineGrid2D(new Vector2Int(4, 2), new Vector2Int(10, 2)); var point = new Vector2Int(3, 2); // TODO: why is it on the polygon? foreach (var rotation in PolygonGrid2D.PossibleRotations) { var rotatedLine = line.Rotate(rotation); var rotatedPoint = point.RotateAroundCenter(rotation); var actualIndex = rotatedLine.Contains(rotatedPoint); Assert.AreEqual(-1, actualIndex); } } { var line = new OrthogonalLineGrid2D(new Vector2Int(4, 2), new Vector2Int(10, 2)); var point = new Vector2Int(12, 2); // TODO: why is it on the polygon? foreach (var rotation in PolygonGrid2D.PossibleRotations) { var rotatedLine = line.Rotate(rotation); var rotatedPoint = point.RotateAroundCenter(rotation); var actualIndex = rotatedLine.Contains(rotatedPoint); Assert.AreEqual(-1, actualIndex); } } }
public void OverlapAlongLine_ComplexCase() { var p1 = GetPlusShape(); var p2 = new PolygonGrid2DBuilder() .AddPoint(0, 0) .AddPoint(0, 8) .AddPoint(8, 8) .AddPoint(8, 2) .AddPoint(6, 2) .AddPoint(6, 6) .AddPoint(2, 6) .AddPoint(2, 0) .Build(); var line = new OrthogonalLineGrid2D(new Vector2Int(0, -2), new Vector2Int(15, -2)); var result = polygonOverlap.OverlapAlongLine(p1, p2, line); var expected = new List <Tuple <Vector2Int, bool> >() { Tuple.Create(new Vector2Int(0, -2), true), Tuple.Create(new Vector2Int(2, -2), false), Tuple.Create(new Vector2Int(3, -2), true), Tuple.Create(new Vector2Int(6, -2), false), }; Assert.IsTrue(expected.SequenceEqual(result)); }
private static void DrawDoorLine(DoorLineGrid2D doorLine, Grid grid, Color color, string label = null) { var line = new OrthogonalLineGrid2D(doorLine.From.ToCustomIntVector2(), doorLine.To.ToCustomIntVector2()); var fromSolid = line.From; var toSolid = line.From; if (line.Length > 0) { toSolid += (doorLine.Length - 1) * line.GetDirectionVector(); } var toDotted = line.To; var doorsCount = line.Length - doorLine.Length + 2; if (doorsCount > 0) { var finalLabel = $"{doorsCount} door{(doorsCount != 1 ? "s" : "")}\nSize {doorLine.Length}"; if (label != null) { finalLabel += $"\n{label}"; } DrawRectangleOutline(grid, fromSolid.ToUnityIntVector3(), toSolid.ToUnityIntVector3(), color, new Vector2(0.2f, 0.2f)); } }
public void Rotate_InvalidDegrees_Throws() { var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(5, 0)); Assert.Throws <ArgumentException>(() => line.Rotate(1)); Assert.Throws <ArgumentException>(() => line.Rotate(15)); Assert.Throws <ArgumentException>(() => line.Rotate(-181)); }
public void PartitionByIntersection_PerpendicularIntersection_Throws() { var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(10, 0)); var intersection = new List <OrthogonalLineGrid2D>() { new OrthogonalLineGrid2D(new Vector2Int(5, -1), new Vector2Int(5, 1)) }; Assert.Throws <ArgumentException>(() => orthogonalLineIntersection.PartitionByIntersection(line, intersection)); }
public void OverlapAlongLine_Rectangles_OverlapStart2() { var p1 = PolygonGrid2D.GetSquare(5); var p2 = PolygonGrid2D.GetRectangle(2, 3) + new Vector2Int(0, -3); var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(0, 10)); var result = polygonOverlap.OverlapAlongLine(p1, p2, line); Assert.AreEqual(0, result.Count); }
public void PartitionByIntersection_OverlappingIntersections_Throws() { var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(10, 0)); var intersection = new List <OrthogonalLineGrid2D>() { new OrthogonalLineGrid2D(new Vector2Int(4, 0), new Vector2Int(6, 0)), new OrthogonalLineGrid2D(new Vector2Int(5, 0), new Vector2Int(7, 0)), }; Assert.Throws <ArgumentException>(() => orthogonalLineIntersection.PartitionByIntersection(line, intersection)); }
public void GetPoints_Bottom_ReturnsPoints() { var line = new OrthogonalLineGrid2D(new Vector2Int(2, 4), new Vector2Int(2, 2)); var expectedPoints = new List <Vector2Int>() { new Vector2Int(2, 4), new Vector2Int(2, 3), new Vector2Int(2, 2), }; Assert.IsTrue(line.GetPoints().SequenceEqual(expectedPoints)); }
public void Shrink_Invalid_Throws() { { var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(5, 0)); Assert.Throws <ArgumentException>(() => line.Shrink(3)); } { var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(-6, 0)); Assert.Throws <ArgumentException>(() => line.Shrink(4, 3)); } }
public void GetDirection_ReturnsDirection() { var top = new OrthogonalLineGrid2D(new Vector2Int(2, 2), new Vector2Int(2, 4)); var bottom = new OrthogonalLineGrid2D(new Vector2Int(2, 4), new Vector2Int(2, 2)); var right = new OrthogonalLineGrid2D(new Vector2Int(5, 3), new Vector2Int(8, 3)); var left = new OrthogonalLineGrid2D(new Vector2Int(8, 3), new Vector2Int(5, 3)); Assert.AreEqual(OrthogonalLineGrid2D.Direction.Top, top.GetDirection()); Assert.AreEqual(OrthogonalLineGrid2D.Direction.Bottom, bottom.GetDirection()); Assert.AreEqual(OrthogonalLineGrid2D.Direction.Right, right.GetDirection()); Assert.AreEqual(OrthogonalLineGrid2D.Direction.Left, left.GetDirection()); }
public void GetPoints_Left_ReturnsPoints() { var line = new OrthogonalLineGrid2D(new Vector2Int(8, 3), new Vector2Int(5, 3)); var expectedPoints = new List <Vector2Int>() { new Vector2Int(8, 3), new Vector2Int(7, 3), new Vector2Int(6, 3), new Vector2Int(5, 3), }; Assert.IsTrue(line.GetPoints().SequenceEqual(expectedPoints)); }
public void OverlapAlongLine_Rectangles_OverlapEnd() { var p1 = PolygonGrid2D.GetSquare(5); var p2 = PolygonGrid2D.GetRectangle(2, 3) + new Vector2Int(0, 8); var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(0, 10)); var result = polygonOverlap.OverlapAlongLine(p1, p2, line); var expected = new List <Tuple <Vector2Int, bool> >() { Tuple.Create(new Vector2Int(0, 4), true), }; Assert.IsTrue(expected.SequenceEqual(result)); }
public void OverlapAlongLine_LAndL3() { var p1 = GetLShape(); var p2 = GetLShape(); var line = new OrthogonalLineGrid2D(new Vector2Int(3, 5), new Vector2Int(3, -2)); var result = polygonOverlap.OverlapAlongLine(p1, p2, line); var expected = new List <Tuple <Vector2Int, bool> >() { Tuple.Create(new Vector2Int(3, 2), true), }; Assert.IsTrue(expected.SequenceEqual(result)); }
public void OverlapAlongLine_SquareAndL() { var p1 = PolygonGrid2D.GetSquare(6); var p2 = GetLShape(); var line = new OrthogonalLineGrid2D(new Vector2Int(-2, 3), new Vector2Int(5, 3)); var result = polygonOverlap.OverlapAlongLine(p1, p2, line); var expected = new List <Tuple <Vector2Int, bool> >() { Tuple.Create(new Vector2Int(-2, 3), true), Tuple.Create(new Vector2Int(3, 3), false), }; Assert.IsTrue(expected.SequenceEqual(result)); }
public GraphBasedGenerator.Grid2D.DoorLineGrid2D ToInternal() { var line = new OrthogonalLineGrid2D(From.ToCustomIntVector2(), To.ToCustomIntVector2()); if (Length > 1) { line = line.Shrink(0, Length - 1); } return(new GraphBasedGenerator.Grid2D.DoorLineGrid2D( line, Length - 1, null, DoorType.Undirected)); }
public ConfigurationSpace GetConfigurationSpaceOverCorridor(PolygonGrid2D polygon, List <DoorLineGrid2D> doorLines, PolygonGrid2D fixedPolygon, List <DoorLineGrid2D> fixedDoorLines, PolygonGrid2D corridor, List <DoorLineGrid2D> corridorDoorLines) { var fixedAndCorridorConfigurationSpace = GetConfigurationSpace(corridor, corridorDoorLines, fixedPolygon, fixedDoorLines); var newCorridorDoorLines = new List <DoorLineGrid2D>(); corridorDoorLines = DoorUtils.MergeDoorLines(corridorDoorLines); foreach (var corridorPositionLine in fixedAndCorridorConfigurationSpace.Lines) { foreach (var corridorDoorLine in corridorDoorLines) { var rotation = corridorDoorLine.Line.ComputeRotation(); var rotatedLine = corridorDoorLine.Line.Rotate(rotation); var rotatedCorridorLine = corridorPositionLine.Rotate(rotation).GetNormalized(); if (rotatedCorridorLine.GetDirection() == OrthogonalLineGrid2D.Direction.Right) { var correctPositionLine = (rotatedCorridorLine + rotatedLine.From); var correctLengthLine = new OrthogonalLineGrid2D(correctPositionLine.From, correctPositionLine.To + rotatedLine.Length * rotatedLine.GetDirectionVector(), rotatedCorridorLine.GetDirection()); var correctRotationLine = correctLengthLine.Rotate(-rotation); // TODO: problem with corridors overlapping newCorridorDoorLines.Add(new DoorLineGrid2D(correctRotationLine, corridorDoorLine.Length, corridorDoorLine.DoorSocket)); } else if (rotatedCorridorLine.GetDirection() == OrthogonalLineGrid2D.Direction.Top) { foreach (var corridorPosition in rotatedCorridorLine.GetPoints()) { var transformedDoorLine = rotatedLine + corridorPosition; var newDoorLine = transformedDoorLine.Rotate(-rotation); // TODO: problem with corridors overlapping // TODO: problem with too many small lines instead of bigger lines newCorridorDoorLines.Add(new DoorLineGrid2D(newDoorLine, corridorDoorLine.Length, corridorDoorLine.DoorSocket)); } } } } var configurationSpace = GetConfigurationSpace(polygon, doorLines, fixedPolygon, newCorridorDoorLines); // configurationSpace.ReverseDoors = null; return(new ConfigurationSpace() { Lines = configurationSpace.Lines.ToList() }); }
public void Rotate_ReturnsRotated() { { var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(5, 0)); var expected = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(0, -5)); Assert.AreEqual(expected, line.Rotate(90)); Assert.AreEqual(expected, line.Rotate(-270)); } { var line = new OrthogonalLineGrid2D(new Vector2Int(-2, -2), new Vector2Int(-2, 5)); var expected = new OrthogonalLineGrid2D(new Vector2Int(2, 2), new Vector2Int(2, -5)); Assert.AreEqual(expected, line.Rotate(180)); Assert.AreEqual(expected, line.Rotate(-180)); } }
private SimpleDoorModeSettingsGrid2D GetSettings(OrthogonalLineGrid2D line) { if (Mode == SettingsMode.Basic) { var data = this; return(new SimpleDoorModeSettingsGrid2D() { Length = data.DoorLength, Margin1 = data.DistanceFromCorners, Margin2 = data.DistanceFromCorners, }); } return(line.GetDirectionVector().X != 0 ? HorizontalDoors : VerticalDoors); }
public void PartitionByIntersection_OneLineIntersection_ReturnsTwoLines() { var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(10, 0)); var intersection = new List <OrthogonalLineGrid2D>() { new OrthogonalLineGrid2D(new Vector2Int(4, 0), new Vector2Int(6, 0)) }; var expectedPartitions = new List <OrthogonalLineGrid2D>() { new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(3, 0)), new OrthogonalLineGrid2D(new Vector2Int(7, 0), new Vector2Int(10, 0)), }; var partitions = orthogonalLineIntersection.PartitionByIntersection(line, intersection); Assert.That(partitions, Is.EquivalentTo(expectedPartitions)); }
public void TryGetIntersection_HorizontalAndVertical() { { // No intersection - one above the other var line1 = new OrthogonalLineGrid2D(new Vector2Int(1, 1), new Vector2Int(5, 1)); var line2 = new OrthogonalLineGrid2D(new Vector2Int(3, 2), new Vector2Int(3, 7)); Assert.IsFalse(orthogonalLineIntersection.TryGetIntersection(line1, line2, out var intersection1)); Assert.IsFalse(orthogonalLineIntersection.TryGetIntersection(line1.SwitchOrientation(), line2.SwitchOrientation(), out var intersection2)); Assert.IsFalse(orthogonalLineIntersection.TryGetIntersection(line2, line1, out var intersection3)); Assert.IsFalse(orthogonalLineIntersection.TryGetIntersection(line2.SwitchOrientation(), line1.SwitchOrientation(), out var intersection4)); } { // No intersection - one next to the other var line1 = new OrthogonalLineGrid2D(new Vector2Int(1, 1), new Vector2Int(5, 1)); var line2 = new OrthogonalLineGrid2D(new Vector2Int(6, 2), new Vector2Int(6, 7)); Assert.IsFalse(orthogonalLineIntersection.TryGetIntersection(line1, line2, out var intersection1)); Assert.IsFalse(orthogonalLineIntersection.TryGetIntersection(line1.SwitchOrientation(), line2.SwitchOrientation(), out var intersection2)); Assert.IsFalse(orthogonalLineIntersection.TryGetIntersection(line2, line1, out var intersection3)); Assert.IsFalse(orthogonalLineIntersection.TryGetIntersection(line2.SwitchOrientation(), line1.SwitchOrientation(), out var intersection4)); } { // Intersection is one point var line1 = new OrthogonalLineGrid2D(new Vector2Int(1, 1), new Vector2Int(5, 1)); var line2 = new OrthogonalLineGrid2D(new Vector2Int(3, -2), new Vector2Int(3, 5)); var expected = new OrthogonalLineGrid2D(new Vector2Int(3, 1), new Vector2Int(3, 1)); Assert.IsTrue(orthogonalLineIntersection.TryGetIntersection(line1, line2, out var intersection1)); Assert.AreEqual(expected, intersection1); Assert.IsTrue(orthogonalLineIntersection.TryGetIntersection(line1.SwitchOrientation(), line2.SwitchOrientation(), out var intersection2)); Assert.AreEqual(expected, intersection2); Assert.IsTrue(orthogonalLineIntersection.TryGetIntersection(line2, line1, out var intersection3)); Assert.AreEqual(expected, intersection3); Assert.IsTrue(orthogonalLineIntersection.TryGetIntersection(line2.SwitchOrientation(), line1.SwitchOrientation(), out var intersection4)); Assert.AreEqual(expected, intersection4); } }
public void Shrink_Valid_ReturnsShrinked() { { var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(5, 0)); var expected = new OrthogonalLineGrid2D(new Vector2Int(1, 0), new Vector2Int(3, 0)); var shrinked = line.Shrink(1, 2); Assert.AreEqual(expected, shrinked); } { var line = new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(0, 6)); var expected = new OrthogonalLineGrid2D(new Vector2Int(0, 2), new Vector2Int(0, 5)); var shrinked = line.Shrink(2, 1); Assert.AreEqual(expected, shrinked); } }
public static DoorLineGrid2D TransformDoorLine(DoorLineGrid2D doorLine, TransformationGrid2D transformation) { var doorPosition = doorLine.Line; if (doorPosition.GetDirection() == OrthogonalLineGrid2D.Direction.Undefined) { throw new InvalidOperationException("Cannot fix door direction when original direction is undefined"); } switch (transformation) { case TransformationGrid2D.Identity: return(doorLine); case TransformationGrid2D.Rotate90: return(new DoorLineGrid2D(doorPosition.Rotate(90), doorLine.Length, doorLine.DoorSocket)); case TransformationGrid2D.Rotate180: return(new DoorLineGrid2D(doorPosition.Rotate(180), doorLine.Length, doorLine.DoorSocket)); case TransformationGrid2D.Rotate270: return(new DoorLineGrid2D(doorPosition.Rotate(270), doorLine.Length, doorLine.DoorSocket)); } // Other transformations need to switch door directions var firstStartPoint = doorPosition.From.Transform(transformation); var lastStartPoint = doorPosition.To.Transform(transformation); var length = doorLine.Length; var transformedDirection = TransformDirection(doorPosition.GetDirection(), transformation); var transformedLine = new OrthogonalLineGrid2D(firstStartPoint, lastStartPoint, transformedDirection); var lastEndPoint = lastStartPoint + length * transformedLine.GetDirectionVector(); var newDirection = OrthogonalLineGrid2D.GetOppositeDirection(transformedDirection); var newDoorPosition = new OrthogonalLineGrid2D(lastEndPoint, lastEndPoint + transformedLine.Length * transformedLine.SwitchOrientation().GetDirectionVector(), newDirection); if (newDoorPosition.Length != doorPosition.Length) { throw new InvalidOperationException(); } return(new DoorLineGrid2D(newDoorPosition, doorLine.Length, doorLine.DoorSocket)); }
public void OverlapAlongLine_LAndL2() { var p1 = GetLShape(); var p2 = new PolygonGrid2DBuilder() .AddPoint(0, 0) .AddPoint(0, 9) .AddPoint(3, 9) .AddPoint(3, 3) .AddPoint(6, 3) .AddPoint(6, 0) .Build(); var line = new OrthogonalLineGrid2D(new Vector2Int(3, 8), new Vector2Int(3, -2)); var result = polygonOverlap.OverlapAlongLine(p1, p2, line); var expected = new List <Tuple <Vector2Int, bool> >() { Tuple.Create(new Vector2Int(3, 2), true), }; Assert.IsTrue(expected.SequenceEqual(result)); }
public void OverlapAlongLine_SquareAndL3() { var p1 = PolygonGrid2D.GetSquare(6); var p2 = new PolygonGrid2DBuilder() .AddPoint(0, 0) .AddPoint(0, 6) .AddPoint(6, 6) .AddPoint(6, 3) .AddPoint(3, 3) .AddPoint(3, 0) .Build(); var line = new OrthogonalLineGrid2D(new Vector2Int(3, 2), new Vector2Int(3, -5)); var result = polygonOverlap.OverlapAlongLine(p1, p2, line); var expected = new List <Tuple <Vector2Int, bool> >() { Tuple.Create(new Vector2Int(3, 2), true), Tuple.Create(new Vector2Int(3, -3), false), }; Assert.IsTrue(expected.SequenceEqual(result)); }
private IEnumerable <DoorLine> GetDoorLine(PolygonGrid2D polygon, OrthogonalLineGrid2D doorPosition) { var found = false; foreach (var side in polygon.GetLines()) { if (side.Contains(doorPosition.From) == -1 || side.Contains(doorPosition.To) == -1) { continue; } var isGoodDirection = doorPosition.From + doorPosition.Length * side.GetDirectionVector() == doorPosition.To; var from = isGoodDirection ? doorPosition.From : doorPosition.To; found = true; yield return(new DoorLine(new OrthogonalLineGrid2D(from, from, side.GetDirection()), doorPosition.Length)); } if (found == false) { throw new ArgumentException($"The door line {doorPosition.ToStringShort()} is not on the outline of the polygon {polygon}. Make sure that all the door lines of a manual door mode are on the outline of the polygon."); } }
public OutlineSegment(OrthogonalLineGrid2D line, bool isDoor) { Line = line; IsDoor = isDoor; }
private OrthogonalLineGrid2D FastAddition(OrthogonalLineGrid2D line, Vector2Int position) { return(new OrthogonalLineGrid2D(line.From + position, line.To + position)); }
public void RotateDirection_ReturnsRotated() { Assert.AreEqual(OrthogonalLineGrid2D.Direction.Bottom, OrthogonalLineGrid2D.RotateDirection(OrthogonalLineGrid2D.Direction.Right, 90)); Assert.AreEqual(OrthogonalLineGrid2D.Direction.Top, OrthogonalLineGrid2D.RotateDirection(OrthogonalLineGrid2D.Direction.Bottom, -180)); }