2014-07-10 05:51:17 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.command;
|
|
|
|
|
2018-12-23 16:19:33 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
2014-07-10 05:51:17 +00:00
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
|
|
|
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
|
|
|
import com.sk89q.worldedit.extent.transform.BlockTransformExtent;
|
|
|
|
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
|
|
|
|
import com.sk89q.worldedit.function.operation.Operation;
|
2019-01-09 07:13:44 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
|
|
|
import com.sk89q.worldedit.math.MutableVector;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.math.Vector3;
|
2014-07-10 05:51:17 +00:00
|
|
|
import com.sk89q.worldedit.math.transform.AffineTransform;
|
|
|
|
import com.sk89q.worldedit.math.transform.CombinedTransform;
|
|
|
|
import com.sk89q.worldedit.math.transform.Transform;
|
|
|
|
import com.sk89q.worldedit.regions.CuboidRegion;
|
|
|
|
import com.sk89q.worldedit.regions.Region;
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
2014-07-10 05:51:17 +00:00
|
|
|
/**
|
|
|
|
* Helper class to 'bake' a transform into a clipboard.
|
2018-08-12 14:03:07 +00:00
|
|
|
* <p>
|
2014-07-10 05:51:17 +00:00
|
|
|
* <p>This class needs a better name and may need to be made more generic.</p>
|
|
|
|
*
|
|
|
|
* @see Clipboard
|
|
|
|
* @see Transform
|
|
|
|
*/
|
2018-08-12 14:03:07 +00:00
|
|
|
public class FlattenedClipboardTransform {
|
2014-07-10 05:51:17 +00:00
|
|
|
|
|
|
|
private final Clipboard original;
|
|
|
|
private final Transform transform;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance.
|
|
|
|
*
|
2018-08-12 14:03:07 +00:00
|
|
|
* @param original the original clipboard
|
2014-07-10 05:51:17 +00:00
|
|
|
* @param transform the transform
|
|
|
|
*/
|
2018-06-19 00:53:15 +00:00
|
|
|
private FlattenedClipboardTransform(Clipboard original, Transform transform) {
|
2014-07-10 05:51:17 +00:00
|
|
|
checkNotNull(original);
|
|
|
|
checkNotNull(transform);
|
|
|
|
this.original = original;
|
|
|
|
this.transform = transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the transformed region.
|
|
|
|
*
|
|
|
|
* @return the transformed region
|
|
|
|
*/
|
|
|
|
public Region getTransformedRegion() {
|
|
|
|
Region region = original.getRegion();
|
2018-12-23 16:19:33 +00:00
|
|
|
Vector3 minimum = region.getMinimumPoint().toVector3();
|
|
|
|
Vector3 maximum = region.getMaximumPoint().toVector3();
|
2014-07-10 05:51:17 +00:00
|
|
|
|
|
|
|
Transform transformAround =
|
|
|
|
new CombinedTransform(
|
|
|
|
new AffineTransform().translate(original.getOrigin().multiply(-1)),
|
|
|
|
transform,
|
|
|
|
new AffineTransform().translate(original.getOrigin()));
|
|
|
|
|
2018-12-23 16:19:33 +00:00
|
|
|
Vector3[] corners = new Vector3[] {
|
|
|
|
minimum,
|
|
|
|
maximum,
|
|
|
|
minimum.withX(maximum.getX()),
|
|
|
|
minimum.withY(maximum.getY()),
|
|
|
|
minimum.withZ(maximum.getZ()),
|
|
|
|
maximum.withX(minimum.getX()),
|
|
|
|
maximum.withY(minimum.getY()),
|
|
|
|
maximum.withZ(minimum.getZ()) };
|
2014-07-10 05:51:17 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < corners.length; i++) {
|
2018-10-19 20:13:32 +00:00
|
|
|
corners[i] = transformAround.apply(corners[i]);
|
2014-07-10 05:51:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 07:13:44 +00:00
|
|
|
MutableVector newMinimum = new MutableVector(corners[0]);
|
|
|
|
MutableVector newMaximum = new MutableVector(corners[0]);
|
|
|
|
// MutableVector cbv = new MutableVector();
|
2014-07-10 05:51:17 +00:00
|
|
|
for (int i = 1; i < corners.length; i++) {
|
2019-01-09 07:13:44 +00:00
|
|
|
MutableVector cbv = new MutableVector(corners[i]);
|
|
|
|
newMinimum = newMinimum.setComponents(newMinimum.getMinimum(cbv));
|
|
|
|
newMaximum = newMaximum.setComponents(newMaximum.getMaximum(cbv));
|
2014-07-10 05:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// After transformation, the points may not really sit on a block,
|
|
|
|
// so we should expand the region for edge cases
|
2018-08-12 14:03:07 +00:00
|
|
|
newMinimum.mutX(Math.ceil(Math.floor(newMinimum.getX())));
|
|
|
|
newMinimum.mutY(Math.ceil(Math.floor(newMinimum.getY())));
|
|
|
|
newMinimum.mutZ(Math.ceil(Math.floor(newMinimum.getZ())));
|
2014-07-10 05:51:17 +00:00
|
|
|
|
2018-10-19 20:13:32 +00:00
|
|
|
return new CuboidRegion(BlockVector3.at(newMinimum.getX(), newMinimum.getY(), newMinimum.getZ()), BlockVector3.at(newMaximum.getX(), newMaximum.getY(), newMaximum.getZ()));
|
2014-07-10 05:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an operation to copy from the original clipboard to the given extent.
|
|
|
|
*
|
|
|
|
* @param target the target
|
|
|
|
* @return the operation
|
|
|
|
*/
|
|
|
|
public Operation copyTo(Extent target) {
|
2018-08-12 14:03:07 +00:00
|
|
|
Extent extent = original;
|
|
|
|
if (transform != null && !transform.isIdentity()) extent = new BlockTransformExtent(original, transform);
|
2014-07-10 05:51:17 +00:00
|
|
|
ForwardExtentCopy copy = new ForwardExtentCopy(extent, original.getRegion(), original.getOrigin(), target, original.getOrigin());
|
|
|
|
copy.setTransform(transform);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance to bake the transform with.
|
|
|
|
*
|
2018-08-12 14:03:07 +00:00
|
|
|
* @param original the original clipboard
|
2014-07-10 05:51:17 +00:00
|
|
|
* @param transform the transform
|
|
|
|
* @return a builder
|
|
|
|
*/
|
2018-06-19 00:53:15 +00:00
|
|
|
public static FlattenedClipboardTransform transform(Clipboard original, Transform transform) {
|
|
|
|
return new FlattenedClipboardTransform(original, transform);
|
2014-07-10 05:51:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 16:36:39 +00:00
|
|
|
|
2014-07-10 05:51:17 +00:00
|
|
|
}
|