1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2013 The Noise-rs Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A procedural noise generation library for Rust.
//!
//! # Example
//!
//! ```rust
//! use noise::{Brownian3, PermutationTable};
//!
//! let perm_table = PermutationTable::new(12);
//! let noise = Brownian3::new(noise::perlin3, 4).wavelength(32.0);
//! let val = noise.apply(&perm_table, &[42.0, 37.0, 2.0]);
//! ```

#![deny(missing_copy_implementations)]

extern crate num_traits;
extern crate rand;

pub use permutationtable::PermutationTable;
pub use math::{Point2, Point3, Point4};
pub use perlin::{perlin2, perlin3, perlin4};
pub use value::{value2, value3, value4};
pub use open_simplex::{open_simplex2, open_simplex3, open_simplex4};
pub use brownian::{Brownian2, Brownian3, Brownian4};

pub use cell::{range_sqr_euclidian2, range_sqr_euclidian3, range_sqr_euclidian4};
pub use cell::{cell2_seed_point, cell3_seed_point, cell4_seed_point};
pub use cell::{cell2_range, cell3_range, cell4_range};
pub use cell::{cell2_range_inv, cell3_range_inv, cell4_range_inv};
pub use cell::{cell2_value, cell3_value, cell4_value};
pub use cell::{cell2_manhattan, cell3_manhattan, cell4_manhattan};
pub use cell::{cell2_manhattan_inv, cell3_manhattan_inv, cell4_manhattan_inv};
pub use cell::{cell2_manhattan_value, cell3_manhattan_value, cell4_manhattan_value};

mod gradient;
mod math;
mod permutationtable;

mod brownian;
mod perlin;
mod value;
mod open_simplex;
mod cell;

pub mod modules;

/// A trait alias for a 2-dimensional noise function.
///
/// This is useful for succinctly parameterising over valid noise functions.
///
/// # Example
///
/// ```rust
/// use noise::{GenFn2, PermutationTable, Point2};
///
/// fn apply_noise2<F: GenFn2<f32>>(t: &PermutationTable, p: &Point2<f32>, f: F) -> f32 { f(t, p) }
/// ```
pub trait GenFn2<T>: Fn(&PermutationTable, &Point2<T>) -> T {}

/// A trait alias for a 3-dimensional noise function.
///
/// This is useful for succinctly parameterising over valid noise functions.
///
/// # Example
///
/// ```rust
/// use noise::{GenFn3, PermutationTable, Point3};
///
/// fn apply_noise3<F: GenFn3<f32>>(t: &PermutationTable, p: &Point3<f32>, f: F) -> f32 { f(t, p) }
/// ```
pub trait GenFn3<T>: Fn(&PermutationTable, &Point3<T>) -> T {}

/// A trait alias for a 4-dimensional noise function.
///
/// This is useful for succinctly parameterising over valid noise functions.
///
/// # Example
///
/// ```rust
/// use noise::{GenFn4, PermutationTable, Point4};
///
/// fn apply_noise4<F: GenFn4<f32>>(t: &PermutationTable, p: &Point4<f32>, f: F) -> f32 { f(t, p) }
/// ```
pub trait GenFn4<T>: Fn(&PermutationTable, &Point4<T>) -> T {}

impl<T, F> GenFn2<T> for F where F: Fn(&PermutationTable, &Point2<T>) -> T, {}
impl<T, F> GenFn3<T> for F where F: Fn(&PermutationTable, &Point3<T>) -> T, {}
impl<T, F> GenFn4<T> for F where F: Fn(&PermutationTable, &Point4<T>) -> T, {}

/// Base trait for noise modules.
///
/// A noise module is a object that calculates and outputs a value given a
/// n-Dimensional input value, where n is (2,3,4).
///
/// Each type of noise module uses a specific method to calculate an output
/// value. Some of these methods include:
///
/// * Calculating a value using a coherent-noise function or some other
///     mathematical function.
/// * Mathematically changing the output value from another noise module
///     in various ways.
/// * Combining the output values from two noise modules in various ways.
pub trait NoiseModule<T> {
    type Output;

    fn get(&self, point: T) -> Self::Output;
}

impl<'a, T, M: NoiseModule<T>> NoiseModule<T> for &'a M {
    type Output = M::Output;

    #[inline]
    fn get(&self, point: T) -> M::Output {
        M::get(*self, point)
    }
}