Search in sources :

Example 1 with Transformation

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));
    });
}
Also used : AbstractReferentArchetypeVolume(org.spongepowered.common.world.volume.buffer.archetype.AbstractReferentArchetypeVolume) ArchetypeVolume(org.spongepowered.api.world.volume.archetype.ArchetypeVolume) SpongeArchetypeVolume(org.spongepowered.common.world.volume.buffer.archetype.SpongeArchetypeVolume) Transformation(org.spongepowered.api.util.transformation.Transformation) BlockState(org.spongepowered.api.block.BlockState) SpongeArchetypeVolume(org.spongepowered.common.world.volume.buffer.archetype.SpongeArchetypeVolume) Vector3d(org.spongepowered.math.vector.Vector3d) StubState(org.spongepowered.common.test.stub.block.StubState) Vector3i(org.spongepowered.math.vector.Vector3i) Rotation(org.spongepowered.api.util.rotation.Rotation) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with Transformation

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.");
}
Also used : Transformation(org.spongepowered.api.util.transformation.Transformation) Vector3d(org.spongepowered.math.vector.Vector3d) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with Transformation

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.");
}
Also used : Transformation(org.spongepowered.api.util.transformation.Transformation) Vector3d(org.spongepowered.math.vector.Vector3d) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with Transformation

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.");
}
Also used : Transformation(org.spongepowered.api.util.transformation.Transformation) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Rotation(org.spongepowered.api.util.rotation.Rotation) Stream(java.util.stream.Stream) Vector3d(org.spongepowered.math.vector.Vector3d) Assertions(org.junit.jupiter.api.Assertions) Axis(org.spongepowered.api.util.Axis) Arguments(org.junit.jupiter.params.provider.Arguments) MethodSource(org.junit.jupiter.params.provider.MethodSource) Angle(org.spongepowered.api.util.Angle) Answer(org.mockito.stubbing.Answer) Transformation(org.spongepowered.api.util.transformation.Transformation) Rotation(org.spongepowered.api.util.rotation.Rotation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with Transformation

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.");
}
Also used : Transformation(org.spongepowered.api.util.transformation.Transformation) Vector3d(org.spongepowered.math.vector.Vector3d) Rotation(org.spongepowered.api.util.rotation.Rotation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Transformation (org.spongepowered.api.util.transformation.Transformation)6 Vector3d (org.spongepowered.math.vector.Vector3d)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)5 Rotation (org.spongepowered.api.util.rotation.Rotation)3 Stream (java.util.stream.Stream)1 Assertions (org.junit.jupiter.api.Assertions)1 Arguments (org.junit.jupiter.params.provider.Arguments)1 Mockito (org.mockito.Mockito)1 Answer (org.mockito.stubbing.Answer)1 BlockState (org.spongepowered.api.block.BlockState)1 Angle (org.spongepowered.api.util.Angle)1 Axis (org.spongepowered.api.util.Axis)1 ArchetypeVolume (org.spongepowered.api.world.volume.archetype.ArchetypeVolume)1 StubState (org.spongepowered.common.test.stub.block.StubState)1 AbstractReferentArchetypeVolume (org.spongepowered.common.world.volume.buffer.archetype.AbstractReferentArchetypeVolume)1 SpongeArchetypeVolume (org.spongepowered.common.world.volume.buffer.archetype.SpongeArchetypeVolume)1 Vector3i (org.spongepowered.math.vector.Vector3i)1