Home | History | Annotate | Download | only in basis
      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.basis;
     31 
     32 import java.nio.FloatBuffer;
     33 import java.util.ArrayList;
     34 import java.util.List;
     35 
     36 import com.jme3.terrain.noise.Basis;
     37 import com.jme3.terrain.noise.filter.AbstractFilter;
     38 import com.jme3.terrain.noise.modulator.Modulator;
     39 
     40 public class FilteredBasis extends AbstractFilter implements Basis {
     41 
     42 	private Basis basis;
     43 	private List<Modulator> modulators = new ArrayList<Modulator>();
     44 	private float scale;
     45 
     46 	public FilteredBasis() {}
     47 
     48 	public FilteredBasis(Basis basis) {
     49 		this.basis = basis;
     50 	}
     51 
     52 	public Basis getBasis() {
     53 		return this.basis;
     54 	}
     55 
     56 	public void setBasis(Basis basis) {
     57 		this.basis = basis;
     58 	}
     59 
     60 	@Override
     61 	public FloatBuffer filter(float sx, float sy, float base, FloatBuffer data, int size) {
     62 		return data;
     63 	}
     64 
     65 	@Override
     66 	public void init() {
     67 		this.basis.init();
     68 	}
     69 
     70 	@Override
     71 	public Basis setScale(float scale) {
     72 		this.scale = scale;
     73 		return this;
     74 	}
     75 
     76 	@Override
     77 	public float getScale() {
     78 		return this.scale;
     79 	}
     80 
     81 	@Override
     82 	public Basis addModulator(Modulator modulator) {
     83 		this.modulators.add(modulator);
     84 		return this;
     85 	}
     86 
     87 	@Override
     88 	public float value(float x, float y, float z) {
     89 		throw new UnsupportedOperationException(
     90 				"Method value cannot be called on FilteredBasis and its descendants. Use getBuffer instead!");
     91 	}
     92 
     93 	@Override
     94 	public FloatBuffer getBuffer(float sx, float sy, float base, int size) {
     95 		int margin = this.getMargin(size, 0);
     96 		int workSize = size + 2 * margin;
     97 		FloatBuffer retval = this.basis.getBuffer(sx - margin, sy - margin, base, workSize);
     98 		return this.clip(this.doFilter(sx, sy, base, retval, workSize), workSize, size, margin);
     99 	}
    100 
    101 	public FloatBuffer clip(FloatBuffer buf, int origSize, int newSize, int offset) {
    102 		FloatBuffer result = FloatBuffer.allocate(newSize * newSize);
    103 
    104 		float[] orig = buf.array();
    105 		for (int i = offset; i < offset + newSize; i++) {
    106 			result.put(orig, i * origSize + offset, newSize);
    107 		}
    108 
    109 		return result;
    110 	}
    111 }
    112