1 /** 2 * Copyright (c) 2011, Novyon Events 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * - Redistributions of source code must retain the above copyright notice, this 10 * list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * @author Anthyon 29 */ 30 package com.jme3.terrain.noise.filter; 31 32 import java.nio.FloatBuffer; 33 34 public class OptimizedErode extends AbstractFilter { 35 36 private float talus; 37 private int radius; 38 setRadius(int radius)39 public OptimizedErode setRadius(int radius) { 40 this.radius = radius; 41 return this; 42 } 43 getRadius()44 public int getRadius() { 45 return this.radius; 46 } 47 setTalus(float talus)48 public OptimizedErode setTalus(float talus) { 49 this.talus = talus; 50 return this; 51 } 52 getTalus()53 public float getTalus() { 54 return this.talus; 55 } 56 57 @Override getMargin(int size, int margin)58 public int getMargin(int size, int margin) { 59 return super.getMargin(size, margin) + this.radius; 60 } 61 62 @Override filter(float sx, float sy, float base, FloatBuffer buffer, int size)63 public FloatBuffer filter(float sx, float sy, float base, FloatBuffer buffer, int size) { 64 float[] tmp = buffer.array(); 65 float[] retval = new float[tmp.length]; 66 67 for (int y = this.radius + 1; y < size - this.radius; y++) { 68 for (int x = this.radius + 1; x < size - this.radius; x++) { 69 int idx = y * size + x; 70 float h = tmp[idx]; 71 72 float horizAvg = 0; 73 int horizCount = 0; 74 float vertAvg = 0; 75 int vertCount = 0; 76 77 boolean horizT = false; 78 boolean vertT = false; 79 80 for (int i = 0; i >= -this.radius; i--) { 81 int idxV = (y + i) * size + x; 82 int idxVL = (y + i - 1) * size + x; 83 int idxH = y * size + x + i; 84 int idxHL = y * size + x + i - 1; 85 float hV = tmp[idxV]; 86 float hH = tmp[idxH]; 87 88 if (Math.abs(h - hV) > this.talus && Math.abs(h - tmp[idxVL]) > this.talus || vertT) { 89 vertT = true; 90 } else { 91 if (Math.abs(h - hV) <= this.talus) { 92 vertAvg += hV; 93 vertCount++; 94 } 95 } 96 97 if (Math.abs(h - hH) > this.talus && Math.abs(h - tmp[idxHL]) > this.talus || horizT) { 98 horizT = true; 99 } else { 100 if (Math.abs(h - hH) <= this.talus) { 101 horizAvg += hH; 102 horizCount++; 103 } 104 } 105 } 106 107 retval[idx] = 0.5f * (vertAvg / (vertCount > 0 ? vertCount : 1) + horizAvg / (horizCount > 0 ? horizCount : 1)); 108 } 109 } 110 return FloatBuffer.wrap(retval); 111 } 112 113 } 114