Search in sources :

Example 1 with AbstractReferentArchetypeVolume

use of org.spongepowered.common.world.volume.buffer.archetype.AbstractReferentArchetypeVolume 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)

Aggregations

ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 BlockState (org.spongepowered.api.block.BlockState)1 Rotation (org.spongepowered.api.util.rotation.Rotation)1 Transformation (org.spongepowered.api.util.transformation.Transformation)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 Vector3d (org.spongepowered.math.vector.Vector3d)1 Vector3i (org.spongepowered.math.vector.Vector3i)1