Skip to content

Commit

Permalink
Make Transformers pass-by-ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Razaekel committed Mar 24, 2018
1 parent c192450 commit d2abec9
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 139 deletions.
105 changes: 35 additions & 70 deletions examples/complexplanet.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/displace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
let constant = Constant::new(0.0);
let cylinders = Cylinders::new();
let perlin = Perlin::new();
let displace = Displace::new(cylinders, cboard, perlin, constant, constant);
let displace = Displace::new(&cylinders, &cboard, &perlin, &constant, &constant);

PlaneMapBuilder::new(&displace)
.build()
Expand Down
2 changes: 1 addition & 1 deletion examples/rotate_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use noise::utils::*;

fn main() {
let cylinders = Cylinders::new();
let rotate_point = RotatePoint::new(cylinders).set_x_angle(60.0);
let rotate_point = RotatePoint::new(&cylinders).set_x_angle(60.0);

PlaneMapBuilder::new(&rotate_point)
.build()
Expand Down
2 changes: 1 addition & 1 deletion examples/scale_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use noise::utils::*;

fn main() {
let cboard = Checkerboard::new();
let scale_point = ScalePoint::new(cboard).set_all_scales(1.0, 2.0, 3.0, 1.0);
let scale_point = ScalePoint::new(&cboard).set_all_scales(1.0, 2.0, 3.0, 1.0);

PlaneMapBuilder::new(&scale_point)
.set_size(500, 500)
Expand Down
2 changes: 1 addition & 1 deletion examples/texturegranite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
let combined_granite = Add::new(&primary_granite, &scaled_grains);

// Finally, perturb the granite texture to add realism.
let final_granite = Turbulence::new(combined_granite)
let final_granite = Turbulence::new(&combined_granite)
.set_seed(2)
.set_frequency(4.0)
.set_power(1.0 / 8.0)
Expand Down
6 changes: 3 additions & 3 deletions examples/texturejade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ fn main() {
// aligned with any axis. This produces more variation in the secondary
// jade texture since the texture is parallel to the y-axis.
let rotated_base_secondary_jade =
RotatePoint::new(base_secondary_jade).set_angles(90.0, 25.0, 5.0, 0.0);
RotatePoint::new(&base_secondary_jade).set_angles(90.0, 25.0, 5.0, 0.0);

// Slightly perturb the secondary jade texture for more realism.
let perturbed_base_secondary_jade = Turbulence::new(rotated_base_secondary_jade)
let perturbed_base_secondary_jade = Turbulence::new(&rotated_base_secondary_jade)
.set_seed(1)
.set_frequency(4.0)
.set_power(1.0 / 4.0)
Expand All @@ -42,7 +42,7 @@ fn main() {

// Finally, perturb the combined jade texture to produce the final jade
// texture. A low roughness produces nice veins.
let final_jade = Turbulence::new(combined_jade)
let final_jade = Turbulence::new(&combined_jade)
.set_seed(2)
.set_frequency(4.0)
.set_power(1.0 / 16.0)
Expand Down
2 changes: 1 addition & 1 deletion examples/textureslime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() {
.set_falloff(0.5);

// Finally, perturb the slime texture to add realism.
let final_slime = Turbulence::new(slime_chooser)
let final_slime = Turbulence::new(&slime_chooser)
.set_seed(3)
.set_frequency(8.0)
.set_power(1.0 / 32.0)
Expand Down
10 changes: 5 additions & 5 deletions examples/texturewood.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {

// Stretch the perlin noise in the same direction as the center of the log. Should
// produce a nice wood-grain texture.
let scaled_base_wood_grain = ScalePoint::new(wood_grain_noise).set_y_scale(0.25);
let scaled_base_wood_grain = ScalePoint::new(&wood_grain_noise).set_y_scale(0.25);

// Scale the wood-grain values so that they can be added to the base wood texture.
let wood_grain = ScaleBias::new(&scaled_base_wood_grain)
Expand All @@ -28,20 +28,20 @@ fn main() {
let combined_wood = Add::new(&base_wood, &wood_grain);

// Slightly perturb the wood to create a more realistic texture.
let perturbed_wood = Turbulence::new(combined_wood)
let perturbed_wood = Turbulence::new(&combined_wood)
.set_seed(1)
.set_frequency(4.0)
.set_power(1.0 / 256.0)
.set_roughness(4);

// Cut the wood texture a small distance from the center of the log.
let translated_wood = TranslatePoint::new(perturbed_wood).set_y_translation(1.48);
let translated_wood = TranslatePoint::new(&perturbed_wood).set_y_translation(1.48);

// Set the cut on a angle to produce a more interesting texture.
let rotated_wood = RotatePoint::new(translated_wood).set_angles(84.0, 0.0, 0.0, 0.0);
let rotated_wood = RotatePoint::new(&translated_wood).set_angles(84.0, 0.0, 0.0, 0.0);

// Finally, perturb the wood texture again to produce the final texture.
let final_wood = Turbulence::new(rotated_wood)
let final_wood = Turbulence::new(&rotated_wood)
.set_seed(2)
.set_frequency(2.0)
.set_power(1.0 / 64.0)
Expand Down
2 changes: 1 addition & 1 deletion examples/translate_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use noise::utils::*;

fn main() {
let cboard = Checkerboard::new();
let translate_point = TranslatePoint::new(cboard).set_all_translations(0.5, 0.5, 0.0, 0.0);
let translate_point = TranslatePoint::new(&cboard).set_all_translations(0.5, 0.5, 0.0, 0.0);

PlaneMapBuilder::new(&translate_point)
.build()
Expand Down
14 changes: 7 additions & 7 deletions src/noise_fns/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ use std::cell::{Cell, RefCell};
/// function will redundantly calculate the same output value once for each
/// noise function in which it is included.
#[derive(Clone, Debug)]
pub struct Cache<Source> {
pub struct Cache<'a, Source: 'a> {
/// Outputs the value to be cached.
pub source: Source,
pub source: &'a Source,

value: Cell<Option<f64>>,

point: RefCell<Vec<f64>>,
}

impl<Source> Cache<Source> {
pub fn new(source: Source) -> Self {
impl<'a, Source> Cache<'a, Source> {
pub fn new(source: &'a Source) -> Self {
Cache {
source,
value: Cell::new(None),
Expand All @@ -35,7 +35,7 @@ impl<Source> Cache<Source> {
}
}

impl<Source> NoiseFn<Point2<f64>> for Cache<Source>
impl<'a, Source> NoiseFn<Point2<f64>> for Cache<'a, Source>
where
Source: NoiseFn<Point2<f64>>,
{
Expand All @@ -56,7 +56,7 @@ where
}
}

impl<Source> NoiseFn<Point3<f64>> for Cache<Source>
impl<'a, Source> NoiseFn<Point3<f64>> for Cache<'a, Source>
where
Source: NoiseFn<Point3<f64>>,
{
Expand All @@ -77,7 +77,7 @@ where
}
}

impl<Source> NoiseFn<Point4<f64>> for Cache<Source>
impl<'a, Source> NoiseFn<Point4<f64>> for Cache<'a, Source>
where
Source: NoiseFn<Point4<f64>>,
{
Expand Down
38 changes: 19 additions & 19 deletions src/noise_fns/transformers/displace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@ use noise_fns::NoiseFn;

/// Noise function that uses multiple source functions to displace each coordinate
/// of the input value before returning the output value from the `source` function.
pub struct Displace<Source, XDisplace, YDisplace, ZDisplace, UDisplace> {
pub struct Displace<'a, Source: 'a, XDisplace: 'a, YDisplace: 'a, ZDisplace: 'a, UDisplace: 'a> {
/// Source function that outputs a value
pub source: Source,
pub source: &'a Source,

/// Displacement function that displaces the _x_ coordinate of the input
/// value.
pub x_displace: XDisplace,
pub x_displace: &'a XDisplace,

/// Displacement function that displaces the _y_ coordinate of the input
/// value.
pub y_displace: YDisplace,
pub y_displace: &'a YDisplace,

/// Displacement function that displaces the _z_ coordinate of the input
/// value. Only needed for 3d or higher noise.
pub z_displace: ZDisplace,
pub z_displace: &'a ZDisplace,

/// Displacement function that displaces the _u_ coordinate of the input
/// value. Only needed for 4d or higher noise.
pub u_displace: UDisplace,
pub u_displace: &'a UDisplace,
}

impl<Source, XDisplace, YDisplace, ZDisplace, UDisplace>
Displace<Source, XDisplace, YDisplace, ZDisplace, UDisplace>
impl<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace>
Displace<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace>
{
pub fn new(
source: Source,
x_displace: XDisplace,
y_displace: YDisplace,
z_displace: ZDisplace,
u_displace: UDisplace,
source: &'a Source,
x_displace: &'a XDisplace,
y_displace: &'a YDisplace,
z_displace: &'a ZDisplace,
u_displace: &'a UDisplace,
) -> Self {
Displace {
source,
Expand All @@ -45,8 +45,8 @@ impl<Source, XDisplace, YDisplace, ZDisplace, UDisplace>
}

#[cfg_attr(rustfmt, rustfmt_skip)]
impl<Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point2<f64>>
for Displace<Source, XDisplace, YDisplace, ZDisplace, UDisplace>
impl<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point2<f64>>
for Displace<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace>
where Source: NoiseFn<Point2<f64>>,
XDisplace: NoiseFn<Point2<f64>>,
YDisplace: NoiseFn<Point2<f64>>,
Expand All @@ -65,8 +65,8 @@ impl<Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point2<f64>>
}

#[cfg_attr(rustfmt, rustfmt_skip)]
impl<Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point3<f64>>
for Displace<Source, XDisplace, YDisplace, ZDisplace, UDisplace>
impl<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point3<f64>>
for Displace<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace>
where Source: NoiseFn<Point3<f64>>,
XDisplace: NoiseFn<Point3<f64>>,
YDisplace: NoiseFn<Point3<f64>>,
Expand All @@ -88,8 +88,8 @@ impl<Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point3<f64>>
}

#[cfg_attr(rustfmt, rustfmt_skip)]
impl<Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point4<f64>>
for Displace<Source, XDisplace, YDisplace, ZDisplace, UDisplace>
impl<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point4<f64>>
for Displace<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace>
where Source: NoiseFn<Point4<f64>>,
XDisplace: NoiseFn<Point4<f64>>,
YDisplace: NoiseFn<Point4<f64>>,
Expand Down
14 changes: 7 additions & 7 deletions src/noise_fns/transformers/rotate_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use noise_fns::NoiseFn;
///
/// The coordinate system of the input value is assumed to be "right-handed"
/// (_x_ increases to the right, _y_ increases upward, and _z_ increases inward).
pub struct RotatePoint<Source> {
pub struct RotatePoint<'a, Source: 'a> {
/// Source function that outputs a value
pub source: Source,
pub source: &'a Source,

/// _x_ rotation angle applied to the input value, in degrees. The
/// default angle is set to 0.0 degrees.
Expand All @@ -30,8 +30,8 @@ pub struct RotatePoint<Source> {
pub u_angle: f64,
}

impl<Source> RotatePoint<Source> {
pub fn new(source: Source) -> Self {
impl<'a, Source> RotatePoint<'a, Source> {
pub fn new(source: &'a Source) -> Self {
RotatePoint {
source,
x_angle: 0.0,
Expand Down Expand Up @@ -78,7 +78,7 @@ impl<Source> RotatePoint<Source> {
}
}

impl<Source> NoiseFn<Point2<f64>> for RotatePoint<Source>
impl<'a, Source> NoiseFn<Point2<f64>> for RotatePoint<'a, Source>
where
Source: NoiseFn<Point2<f64>>,
{
Expand All @@ -98,7 +98,7 @@ where
}
}

impl<Source> NoiseFn<Point3<f64>> for RotatePoint<Source>
impl<'a, Source> NoiseFn<Point3<f64>> for RotatePoint<'a, Source>
where
Source: NoiseFn<Point3<f64>>,
{
Expand Down Expand Up @@ -132,7 +132,7 @@ where
}
}

impl<Source> NoiseFn<Point4<f64>> for RotatePoint<Source>
impl<'a, Source> NoiseFn<Point4<f64>> for RotatePoint<'a, Source>
where
Source: NoiseFn<Point4<f64>>,
{
Expand Down
14 changes: 7 additions & 7 deletions src/noise_fns/transformers/scale_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use noise_fns::NoiseFn;
///
/// The get() method multiplies the coordinates of the input value with a
/// scaling factor before returning the output value from the source function.
pub struct ScalePoint<Source> {
pub struct ScalePoint<'a, Source: 'a> {
/// Source function that outputs a value
pub source: Source,
pub source: &'a Source,

/// Scaling factor applied to the _x_ coordinate of the input value. The
/// default scaling factor is set to 1.0.
Expand All @@ -27,8 +27,8 @@ pub struct ScalePoint<Source> {
pub u_scale: f64,
}

impl<Source> ScalePoint<Source> {
pub fn new(source: Source) -> Self {
impl<'a, Source> ScalePoint<'a, Source> {
pub fn new(source: &'a Source) -> Self {
ScalePoint {
source,
x_scale: 1.0,
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<Source> ScalePoint<Source> {
}
}

impl<Source> NoiseFn<Point2<f64>> for ScalePoint<Source>
impl<'a, Source> NoiseFn<Point2<f64>> for ScalePoint<'a, Source>
where
Source: NoiseFn<Point2<f64>>,
{
Expand All @@ -96,7 +96,7 @@ where
}
}

impl<Source> NoiseFn<Point3<f64>> for ScalePoint<Source>
impl<'a, Source> NoiseFn<Point3<f64>> for ScalePoint<'a, Source>
where
Source: NoiseFn<Point3<f64>>,
{
Expand All @@ -109,7 +109,7 @@ where
}
}

impl<Source> NoiseFn<Point4<f64>> for ScalePoint<Source>
impl<'a, Source> NoiseFn<Point4<f64>> for ScalePoint<'a, Source>
where
Source: NoiseFn<Point4<f64>>,
{
Expand Down
14 changes: 7 additions & 7 deletions src/noise_fns/transformers/translate_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use noise_fns::NoiseFn;
///
/// The get() method moves the coordinates of the input value by a translation
/// amount before returning the output value from the source function.
pub struct TranslatePoint<Source> {
pub struct TranslatePoint<'a, Source: 'a> {
/// Source function that outputs a value
pub source: Source,
pub source: &'a Source,

/// Translation amount applied to the _x_ coordinate of the input value.
/// The default translation amount is set to 0.0.
Expand All @@ -27,8 +27,8 @@ pub struct TranslatePoint<Source> {
pub u_translation: f64,
}

impl<Source> TranslatePoint<Source> {
pub fn new(source: Source) -> Self {
impl<'a, Source> TranslatePoint<'a, Source> {
pub fn new(source: &'a Source) -> Self {
TranslatePoint {
source,
x_translation: 0.0,
Expand Down Expand Up @@ -104,7 +104,7 @@ impl<Source> TranslatePoint<Source> {
}
}

impl<Source> NoiseFn<Point2<f64>> for TranslatePoint<Source>
impl<'a, Source> NoiseFn<Point2<f64>> for TranslatePoint<'a, Source>
where
Source: NoiseFn<Point2<f64>>,
{
Expand All @@ -114,7 +114,7 @@ where
}
}

impl<Source> NoiseFn<Point3<f64>> for TranslatePoint<Source>
impl<'a, Source> NoiseFn<Point3<f64>> for TranslatePoint<'a, Source>
where
Source: NoiseFn<Point3<f64>>,
{
Expand All @@ -127,7 +127,7 @@ where
}
}

impl<Source> NoiseFn<Point4<f64>> for TranslatePoint<Source>
impl<'a, Source> NoiseFn<Point4<f64>> for TranslatePoint<'a, Source>
where
Source: NoiseFn<Point4<f64>>,
{
Expand Down
Loading

0 comments on commit d2abec9

Please sign in to comment.