Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extension method to compute transformed point position from room
- Loading branch information
1 parent
7097cb0
commit c0cf3c5
Showing
6 changed files
with
209 additions
and
3 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
MapGeneration.IntegrationTests/MapGeneration.IntegrationTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="nunit" Version="3.12.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MapGeneration\MapGeneration.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
50 changes: 50 additions & 0 deletions
50
MapGeneration.IntegrationTests/Utils/RoomExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Linq; | ||
using GeneralAlgorithms.DataStructures.Common; | ||
using GeneralAlgorithms.DataStructures.Polygons; | ||
using MapGeneration.Core.Doors.DoorModes; | ||
using MapGeneration.Core.MapDescriptions; | ||
using MapGeneration.Utils; | ||
using NUnit.Framework; | ||
|
||
namespace MapGeneration.IntegrationTests.Utils | ||
{ | ||
[TestFixture] | ||
public class RoomExtensionsTests | ||
{ | ||
[Test] | ||
public void TransformPointToNewPosition_RectanglePoints() | ||
{ | ||
// Create a rectangular room shape | ||
// Move it away from (0, 0) so that we can properly test the functionality | ||
var rectangleShape = GridPolygon.GetRectangle(10, 4) + new IntVector2(10, 10); | ||
|
||
// Create points to be transformed | ||
// These could be for example traps, spawn points, etc. | ||
// We use points of the rectangle here because it is easy to check that the transformation is correct. | ||
var pointsToTransform = rectangleShape.GetPoints(); | ||
|
||
var mapDescription = new MapDescription<int>(); | ||
|
||
// Create simple graph with 2 vertices and 1 edge | ||
mapDescription.AddRoom(0); | ||
mapDescription.AddRoom(1); | ||
mapDescription.AddPassage(0, 1); | ||
|
||
// Add the rectangle shape | ||
mapDescription.AddRoomShapes(new RoomDescription(rectangleShape, new OverlapMode(1, 0))); | ||
|
||
var layoutGenerator = LayoutGeneratorFactory.GetDefaultChainBasedGenerator<int>(); | ||
var layout = layoutGenerator.GetLayouts(mapDescription, 1)[0]; | ||
|
||
foreach (var room in layout.Rooms) | ||
{ | ||
// The points were chosen to be the points of the polygon, so after transforming them, they should | ||
// be equal to the room.Shape + room.Position points | ||
var transformedPoints = pointsToTransform.Select(x => room.TransformPointToNewPosition(x)); | ||
var expectedPoints = (room.Shape + room.Position).GetPoints(); | ||
|
||
Assert.That(transformedPoints, Is.EquivalentTo(expectedPoints)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Collections.Generic; | ||
using GeneralAlgorithms.DataStructures.Common; | ||
using GeneralAlgorithms.DataStructures.Polygons; | ||
using MapGeneration.Core.Doors.DoorModes; | ||
using MapGeneration.Core.MapDescriptions; | ||
using MapGeneration.Core.MapLayouts; | ||
using MapGeneration.Utils; | ||
using NUnit.Framework; | ||
|
||
namespace MapGeneration.Tests.Utils | ||
{ | ||
[TestFixture] | ||
public class RoomExtensionsTests | ||
{ | ||
[Test] | ||
public void TransformPointToNewPosition_Identity() | ||
{ | ||
var room = GetRoom(new IntVector2(0, 0), new IntVector2(0, 0), Transformation.Identity, null); | ||
|
||
var point = new IntVector2(5, 5); | ||
var transformedPoint = room.TransformPointToNewPosition(point); | ||
|
||
Assert.That(transformedPoint, Is.EqualTo(new IntVector2(5, 5))); | ||
} | ||
|
||
[Test] | ||
public void TransformPointToNewPosition_Rotate90() | ||
{ | ||
var room = GetRoom(new IntVector2(0, 0), new IntVector2(0, 0), Transformation.Rotate90, null); | ||
|
||
var point = new IntVector2(10, 5); | ||
var transformedPoint = room.TransformPointToNewPosition(point); | ||
|
||
Assert.That(transformedPoint, Is.EqualTo(new IntVector2(5, 0))); | ||
} | ||
|
||
[Test] | ||
public void TransformPointToNewPosition_Identity_WithPosition() | ||
{ | ||
var room = GetRoom(new IntVector2(10, 10), new IntVector2(0, 0), Transformation.Identity, null); | ||
|
||
var point = new IntVector2(5, 5); | ||
var transformedPoint = room.TransformPointToNewPosition(point); | ||
|
||
Assert.That(transformedPoint, Is.EqualTo(new IntVector2(15, 15))); | ||
} | ||
|
||
[Test] | ||
public void TransformPointToNewPosition_Rotate180_WithPosition() | ||
{ | ||
var room = GetRoom(new IntVector2(10, 10), new IntVector2(0, 0), Transformation.Rotate180, null); | ||
|
||
var point = new IntVector2(10, 5); | ||
var transformedPoint = room.TransformPointToNewPosition(point); | ||
|
||
Assert.That(transformedPoint, Is.EqualTo(new IntVector2(10, 15))); | ||
} | ||
|
||
[Test] | ||
public void TransformPointToNewPosition_Rotate180_WithPosition_WithOrigin() | ||
{ | ||
var room = GetRoom(new IntVector2(10, 10), new IntVector2(7, 10), Transformation.Rotate180, null); | ||
|
||
var point = new IntVector2(10, 5); | ||
var transformedPoint = room.TransformPointToNewPosition(point); | ||
|
||
Assert.That(transformedPoint, Is.EqualTo(new IntVector2(17, 25))); | ||
} | ||
|
||
private Room<int> GetRoom(IntVector2 position, IntVector2 roomShapeOrigin, Transformation transformation, List<Transformation> transformations) | ||
{ | ||
return new Room<int>(0, | ||
GridPolygon.GetSquare(10), | ||
position, | ||
false, | ||
new RoomDescription(GridPolygon.GetSquare(10) + roomShapeOrigin, new OverlapMode(1, 0)), | ||
transformation, | ||
transformations); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using GeneralAlgorithms.DataStructures.Common; | ||
using MapGeneration.Interfaces.Core.MapLayouts; | ||
|
||
namespace MapGeneration.Utils | ||
{ | ||
public static class RoomExtensions | ||
{ | ||
/// <summary> | ||
/// Transforms a given point to a position such that if the point was in some relative position to the | ||
/// RoomShape of the associated MapDescription, the returned point will be in that some relative position | ||
/// with respect to the position and transformation of the RoomShape in the generated layout. | ||
/// </summary> | ||
/// <remarks> | ||
/// This function is useful e.g. if we have some trap or spawn positions defined for our room shapes and | ||
/// then need to know where to place them in the generated layout. | ||
/// | ||
/// If the room is invariant to some transformation (e.g. does not change when rotated by 90 degrees), then | ||
/// all such transformations are contained in the room.Transformation collection. All these transformations | ||
/// can be used for the "transformation" parameter if we do not want to use the room.Transformation transformation. | ||
/// </remarks> | ||
/// <typeparam name="TNode"></typeparam> | ||
/// <param name="room"></param> | ||
/// <param name="point">Point to be transformed to the new position</param> | ||
/// <param name="transformation">Transformation to be used. It must be one of the room.Transformations. Defaults to room.Transformation.</param> | ||
/// <returns></returns> | ||
public static IntVector2 TransformPointToNewPosition<TNode>(this IRoom<TNode> room, IntVector2 point, Transformation? transformation = null) | ||
{ | ||
if (transformation != null && room.Transformations.Contains(transformation.Value)) | ||
{ | ||
throw new ArgumentException("Transformation must be one of the possible room transformations", nameof(transformation)); | ||
} | ||
|
||
transformation = transformation ?? room.Transformation; | ||
|
||
var transformedShape = room.RoomDescription.Shape.Transform(transformation.Value); | ||
|
||
return (point.Transform(transformation.Value) - transformedShape.BoundingRectangle.A) + room.Position; | ||
} | ||
} | ||
} |