use of org.spongepowered.api.util.transformation.Transformation in project SpongeCommon by SpongePowered.
the class VolumeTransformationTest method testTransformationsOfPositions.
@MethodSource("testTransformationsOfPositions")
@ParameterizedTest
void testTransformationsOfPositions(final Vector3i min, final Vector3i max, final Vector3i origin, final Vector3i testForRoundTrip, final int rotationCount, final StubRotations wanted) {
final SpongeArchetypeVolume volume = VolumeTransformationTest.fillVolume(min, max, origin);
final Vector3i size = volume.size();
final Vector3i relativeMin = volume.min();
final Vector3d center = volume.logicalCenter();
ArchetypeVolume intermediary = volume;
for (int i = 0; i < rotationCount; i++) {
intermediary = intermediary.transform(Transformation.builder().origin(center).rotate(wanted).build());
}
Rotation expected = Rotations.NONE.get();
for (int i = 0; i < rotationCount; i++) {
expected = expected.and(wanted);
}
final Transformation expectedTransform = Transformation.builder().origin(center).rotate(expected).build();
final Transformation inverse = expectedTransform.inverse();
final ArchetypeVolume rotated = intermediary;
if (rotationCount > 0) {
final Vector3d preliminaryTransformed = expectedTransform.transformPosition(testForRoundTrip.toDouble());
Vector3i unTransformed = preliminaryTransformed.round().toInt();
for (int i = 0; i < rotationCount; i++) {
unTransformed = ((AbstractReferentArchetypeVolume) rotated).inverseTransform(unTransformed.x(), unTransformed.y(), unTransformed.z());
}
Assertions.assertEquals(testForRoundTrip, unTransformed);
}
for (int x = 0; x < size.x(); x++) {
for (int y = 0; y < size.y(); y++) {
for (int z = 0; z < size.z(); z++) {
final int relativeX = x + relativeMin.x();
final int relativeY = y + relativeMin.y();
final int relativeZ = z + relativeMin.z();
final Vector3d rawRelativePosition = new Vector3d(relativeX, relativeY, relativeZ);
final BlockState untransformedState = volume.block(relativeX, relativeY, relativeZ);
final Vector3i transformedPosition = expectedTransform.transformPosition(rawRelativePosition).toInt();
final BlockState transformedState = rotated.block(transformedPosition.x(), transformedPosition.y(), transformedPosition.z());
Assertions.assertEquals(untransformedState, transformedState, () -> String.format("Block Check Failed!\nOriginal(%d, %d, %d): %s\nTransformed(%d, %d, %d): %s\n", relativeX, relativeY, relativeZ, untransformedState, transformedPosition.x(), transformedPosition.y(), transformedPosition.z(), transformedState));
}
}
}
if (rotationCount < 0) {
return;
}
// At this point, we should have an abstract referent volume at least
rotated.blockStateStream(rotated.min(), rotated.max(), StreamOptions.lazily()).forEach((rotatedRef, type, x, y, z) -> {
final Vector3d transformedPos = new Vector3d(x, y, z);
// We have this offset in the stream, so we have to undo it here.
final Vector3d invertedTransformedPos = inverse.transformPosition(transformedPos.add(VolumePositionTranslators.BLOCK_OFFSET)).sub(VolumePositionTranslators.BLOCK_OFFSET);
final Vector3i invertedBlockPos = invertedTransformedPos.toInt();
final Vector3i expectedPos;
Assertions.assertTrue(type instanceof StubState, () -> String.format("expected state to be a stub state for pos: [%f, %f, %f] but got %s", x, y, z, type));
Assertions.assertNotEquals(((StubState) type).deducedPos, VolumeTransformationTest.INVALID_STUB_POSITION, () -> String.format("expected to have a positioned stub state: [%f, %f, %f] but got %s", x, y, z, type));
expectedPos = ((StubState) type).deducedPos;
Assertions.assertEquals(expectedPos, invertedBlockPos, () -> String.format("expected untransformed position %s for state %s does not match reverse transformed position: %s", expectedPos, type, invertedBlockPos));
final BlockState block = volume.block(expectedPos.x(), expectedPos.y(), expectedPos.z());
Assertions.assertEquals(type, block, () -> String.format("Expected deduced state to be equal from the original target volume but had a mismatch: Original target %s does not match %s", block, type));
});
}
use of org.spongepowered.api.util.transformation.Transformation in project SpongeCommon by SpongePowered.
the class TransformationTest method testTranslationOnly.
@ParameterizedTest
@MethodSource
void testTranslationOnly(final Vector3d original, final Vector3d expected) {
// given this builder
final SpongeTransformationBuilder transformationBuilder = TransformationTest.createZeroBuilder();
// when translating by (1,1,1)
final Transformation transformation = transformationBuilder.translate(Vector3d.ONE).build();
// then perform the transformation
final Vector3d result = transformation.transformPosition(original);
Assertions.assertEquals(expected, result, "Did not get expected rotation.");
}
use of org.spongepowered.api.util.transformation.Transformation in project SpongeCommon by SpongePowered.
the class TransformationTest method testMirrorInXDirection.
@ParameterizedTest
@MethodSource
void testMirrorInXDirection(final Vector3d original, final Vector3d expected) {
// given this builder
final SpongeTransformationBuilder transformationBuilder = TransformationTest.createZeroBuilder();
// when rotating by 90 degrees
final Transformation transformation = transformationBuilder.mirror(Axis.X).build();
// then perform the transformation
final Vector3d result = transformation.transformPosition(original);
Assertions.assertEquals(expected, result, "Did not get expected mirroring.");
Assertions.assertTrue(transformation.mirror(Axis.X), "Did not get X axis was mirrored.");
Assertions.assertFalse(transformation.mirror(Axis.Z), "Did not get Z axis was not mirrored.");
}
use of org.spongepowered.api.util.transformation.Transformation in project SpongeCommon by SpongePowered.
the class TransformationTest method testRotatingAroundOrigin180DegreesAroundYAxisWithTwoSteps.
@ParameterizedTest
@MethodSource("testRotatingAroundOrigin180DegreesAroundYAxis")
void testRotatingAroundOrigin180DegreesAroundYAxisWithTwoSteps(final Vector3d original, final Vector3d expected) {
// and this rotation
final Rotation mockRotation = Mockito.mock(Rotation.class, Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
Mockito.when(mockRotation.angle()).thenReturn(Angle.fromDegrees(90));
Mockito.when(mockRotation.and(Mockito.any(Rotation.class))).thenAnswer((Answer<Rotation>) invocation -> {
final Rotation rotation = invocation.getArgument(0);
final Rotation newMock = Mockito.mock(Rotation.class, Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
Mockito.when(newMock.angle()).thenAnswer((Answer<Angle>) x -> Angle.fromDegrees(rotation.angle().degrees() + 90));
return newMock;
});
final Transformation transformation = this.performRotationTest(mockRotation, original, expected, Vector3d.ZERO, 2);
Assertions.assertEquals(180, transformation.rotation().angle().degrees(), "Did not get expected angle.");
}
use of org.spongepowered.api.util.transformation.Transformation in project SpongeCommon by SpongePowered.
the class TransformationTest method testRotationThenTranslation.
@ParameterizedTest
@MethodSource
void testRotationThenTranslation(final Vector3d original, final Vector3d expected) {
// given this builder
final SpongeTransformationBuilder transformationBuilder = new SpongeTransformationBuilder();
// and this rotation
final Rotation mockRotation = Mockito.mock(Rotation.class, Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
Mockito.when(mockRotation.angle()).thenReturn(Angle.fromDegrees(90));
// when rotating by 90 degrees with this translation
final Transformation transformation = transformationBuilder.rotate(mockRotation).translate(Vector3d.ONE).build();
// then perform the transformation
final Vector3d result = transformation.transformPosition(original);
Assertions.assertEquals(expected, result, "Did not get expected result.");
}
Aggregations