Moved HeightMap to math.convolution.

This commit is contained in:
sk89q 2014-03-30 12:15:39 -07:00
parent b07fd594e9
commit dd3f32b8f1
3 changed files with 183 additions and 180 deletions

View File

@ -28,6 +28,7 @@ import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.expression.ExpressionException; import com.sk89q.worldedit.expression.ExpressionException;
import com.sk89q.worldedit.math.convolution.GaussianKernel; import com.sk89q.worldedit.math.convolution.GaussianKernel;
import com.sk89q.worldedit.math.convolution.HeightMap;
import com.sk89q.worldedit.math.convolution.HeightMapFilter; import com.sk89q.worldedit.math.convolution.HeightMapFilter;
import com.sk89q.worldedit.function.GroundFunction; import com.sk89q.worldedit.function.GroundFunction;
import com.sk89q.worldedit.function.generator.FloraGenerator; import com.sk89q.worldedit.function.generator.FloraGenerator;

View File

@ -1,179 +1,181 @@
// $Id$ // $Id$
/* /*
* WorldEditLibrary * WorldEditLibrary
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors * Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit; package com.sk89q.worldedit.math.convolution;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.math.convolution.HeightMapFilter; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID;
/** import com.sk89q.worldedit.regions.Region;
* Allows applications of Kernels onto the region's heightmap.
* Currently only used for smoothing (with a GaussianKernel). /**
* * Allows applications of Kernels onto the region's heightmap.
* @author Grum * Currently only used for smoothing (with a GaussianKernel).
*/ *
* @author Grum
public class HeightMap { */
private int[] data;
private int width; public class HeightMap {
private int height; private int[] data;
private int width;
private Region region; private int height;
private EditSession session;
private Region region;
/** private EditSession session;
* Constructs the HeightMap
* /**
* @param session * Constructs the HeightMap
* @param region *
*/ * @param session
public HeightMap(EditSession session, Region region) { * @param region
this(session, region, false); */
} public HeightMap(EditSession session, Region region) {
this(session, region, false);
/** }
* Constructs the HeightMap
* /**
* @param session * Constructs the HeightMap
* @param region *
* @param naturalOnly ignore non-natural blocks * @param session
*/ * @param region
public HeightMap(EditSession session, Region region, boolean naturalOnly) { * @param naturalOnly ignore non-natural blocks
this.session = session; */
this.region = region; public HeightMap(EditSession session, Region region, boolean naturalOnly) {
this.session = session;
this.width = region.getWidth(); this.region = region;
this.height = region.getLength();
this.width = region.getWidth();
int minX = region.getMinimumPoint().getBlockX(); this.height = region.getLength();
int minY = region.getMinimumPoint().getBlockY();
int minZ = region.getMinimumPoint().getBlockZ(); int minX = region.getMinimumPoint().getBlockX();
int maxY = region.getMaximumPoint().getBlockY(); int minY = region.getMinimumPoint().getBlockY();
int minZ = region.getMinimumPoint().getBlockZ();
// Store current heightmap data int maxY = region.getMaximumPoint().getBlockY();
data = new int[width * height];
for (int z = 0; z < height; ++z) { // Store current heightmap data
for (int x = 0; x < width; ++x) { data = new int[width * height];
data[z * width + x] = session.getHighestTerrainBlock(x + minX, z + minZ, minY, maxY, naturalOnly); for (int z = 0; z < height; ++z) {
} for (int x = 0; x < width; ++x) {
} data[z * width + x] = session.getHighestTerrainBlock(x + minX, z + minZ, minY, maxY, naturalOnly);
} }
}
/** }
* Apply the filter 'iterations' amount times.
* /**
* @param filter * Apply the filter 'iterations' amount times.
* @param iterations *
* @return number of blocks affected * @param filter
* @throws MaxChangedBlocksException * @param iterations
*/ * @return number of blocks affected
* @throws MaxChangedBlocksException
public int applyFilter(HeightMapFilter filter, int iterations) throws MaxChangedBlocksException { */
int[] newData = new int[data.length];
System.arraycopy(data, 0, newData, 0, data.length); public int applyFilter(HeightMapFilter filter, int iterations) throws MaxChangedBlocksException {
int[] newData = new int[data.length];
for (int i = 0; i < iterations; ++i) { System.arraycopy(data, 0, newData, 0, data.length);
newData = filter.filter(newData, width, height);
} for (int i = 0; i < iterations; ++i) {
newData = filter.filter(newData, width, height);
return apply(newData); }
}
return apply(newData);
/** }
* Apply a raw heightmap to the region
* /**
* @param data * Apply a raw heightmap to the region
* @return number of blocks affected *
* @throws MaxChangedBlocksException * @param data
*/ * @return number of blocks affected
* @throws MaxChangedBlocksException
public int apply(int[] data) throws MaxChangedBlocksException { */
Vector minY = region.getMinimumPoint();
int originX = minY.getBlockX(); public int apply(int[] data) throws MaxChangedBlocksException {
int originY = minY.getBlockY(); Vector minY = region.getMinimumPoint();
int originZ = minY.getBlockZ(); int originX = minY.getBlockX();
int originY = minY.getBlockY();
int maxY = region.getMaximumPoint().getBlockY(); int originZ = minY.getBlockZ();
BaseBlock fillerAir = new BaseBlock(BlockID.AIR);
int maxY = region.getMaximumPoint().getBlockY();
int blocksChanged = 0; BaseBlock fillerAir = new BaseBlock(BlockID.AIR);
// Apply heightmap int blocksChanged = 0;
for (int z = 0; z < height; ++z) {
for (int x = 0; x < width; ++x) { // Apply heightmap
int index = z * width + x; for (int z = 0; z < height; ++z) {
int curHeight = this.data[index]; for (int x = 0; x < width; ++x) {
int index = z * width + x;
// Clamp newHeight within the selection area int curHeight = this.data[index];
int newHeight = Math.min(maxY, data[index]);
// Clamp newHeight within the selection area
// Offset x,z to be 'real' coordinates int newHeight = Math.min(maxY, data[index]);
int X = x + originX;
int Z = z + originZ; // Offset x,z to be 'real' coordinates
int X = x + originX;
// We are keeping the topmost blocks so take that in account for the scale int Z = z + originZ;
double scale = (double) (curHeight - originY) / (double) (newHeight - originY);
// We are keeping the topmost blocks so take that in account for the scale
// Depending on growing or shrinking we need to start at the bottom or top double scale = (double) (curHeight - originY) / (double) (newHeight - originY);
if (newHeight > curHeight) {
// Set the top block of the column to be the same type (this might go wrong with rounding) // Depending on growing or shrinking we need to start at the bottom or top
BaseBlock existing = session.getBlock(new Vector(X, curHeight, Z)); if (newHeight > curHeight) {
// Set the top block of the column to be the same type (this might go wrong with rounding)
// Skip water/lava BaseBlock existing = session.getBlock(new Vector(X, curHeight, Z));
if (existing.getType() != BlockID.WATER && existing.getType() != BlockID.STATIONARY_WATER
&& existing.getType() != BlockID.LAVA && existing.getType() != BlockID.STATIONARY_LAVA) { // Skip water/lava
session.setBlock(new Vector(X, newHeight, Z), existing); if (existing.getType() != BlockID.WATER && existing.getType() != BlockID.STATIONARY_WATER
++blocksChanged; && existing.getType() != BlockID.LAVA && existing.getType() != BlockID.STATIONARY_LAVA) {
session.setBlock(new Vector(X, newHeight, Z), existing);
// Grow -- start from 1 below top replacing airblocks ++blocksChanged;
for (int y = newHeight - 1 - originY; y >= 0; --y) {
int copyFrom = (int) (y * scale); // Grow -- start from 1 below top replacing airblocks
session.setBlock(new Vector(X, originY + y, Z), session.getBlock(new Vector(X, originY + copyFrom, Z))); for (int y = newHeight - 1 - originY; y >= 0; --y) {
++blocksChanged; int copyFrom = (int) (y * scale);
} session.setBlock(new Vector(X, originY + y, Z), session.getBlock(new Vector(X, originY + copyFrom, Z)));
} ++blocksChanged;
} else if (curHeight > newHeight) { }
// Shrink -- start from bottom }
for (int y = 0; y < newHeight - originY; ++y) { } else if (curHeight > newHeight) {
int copyFrom = (int) (y * scale); // Shrink -- start from bottom
session.setBlock(new Vector(X, originY + y, Z), session.getBlock(new Vector(X, originY + copyFrom, Z))); for (int y = 0; y < newHeight - originY; ++y) {
++blocksChanged; int copyFrom = (int) (y * scale);
} session.setBlock(new Vector(X, originY + y, Z), session.getBlock(new Vector(X, originY + copyFrom, Z)));
++blocksChanged;
// Set the top block of the column to be the same type }
// (this could otherwise go wrong with rounding)
session.setBlock(new Vector(X, newHeight, Z), session.getBlock(new Vector(X, curHeight, Z))); // Set the top block of the column to be the same type
++blocksChanged; // (this could otherwise go wrong with rounding)
session.setBlock(new Vector(X, newHeight, Z), session.getBlock(new Vector(X, curHeight, Z)));
// Fill rest with air ++blocksChanged;
for (int y = newHeight + 1; y <= curHeight; ++y) {
session.setBlock(new Vector(X, y, Z), fillerAir); // Fill rest with air
++blocksChanged; for (int y = newHeight + 1; y <= curHeight; ++y) {
} session.setBlock(new Vector(X, y, Z), fillerAir);
} ++blocksChanged;
} }
} }
}
// Drop trees to the floor -- TODO }
return blocksChanged; // Drop trees to the floor -- TODO
}
} return blocksChanged;
}
}

View File

@ -20,7 +20,7 @@
package com.sk89q.worldedit.tools.brushes; package com.sk89q.worldedit.tools.brushes;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.HeightMap; import com.sk89q.worldedit.math.convolution.HeightMap;
import com.sk89q.worldedit.MaxChangedBlocksException; import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldVector; import com.sk89q.worldedit.WorldVector;