Skip to content

Commit

Permalink
Merge pull request #203 from hollinwilkins/feature/revert-pass-by-ref
Browse files Browse the repository at this point in the history
Feature/revert pass by ref
  • Loading branch information
Razaekel authored Jun 15, 2019
2 parents 055fc83 + 54a5ae1 commit 7ba934b
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 104 deletions.
105 changes: 70 additions & 35 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<'a, Source: 'a> {
pub struct Cache<Source> {
/// Outputs the value to be cached.
pub source: &'a Source,
pub source: Source,

value: Cell<Option<f64>>,

point: RefCell<Vec<f64>>,
}

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

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

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

impl<'a, Source> NoiseFn<Point4<f64>> for Cache<'a, Source>
impl<Source> NoiseFn<Point4<f64>> for Cache<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<'a, Source: 'a, XDisplace: 'a, YDisplace: 'a, ZDisplace: 'a, UDisplace: 'a> {
pub struct Displace<Source, XDisplace, YDisplace, ZDisplace, UDisplace> {
/// Source function that outputs a value
pub source: &'a Source,
pub source: Source,

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

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

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

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

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

#[cfg_attr(rustfmt, rustfmt_skip)]
impl<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point2<f64>>
for Displace<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace>
impl<Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point2<f64>>
for Displace<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<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point2<f64>
}

#[cfg_attr(rustfmt, rustfmt_skip)]
impl<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point3<f64>>
for Displace<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace>
impl<Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point3<f64>>
for Displace<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<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point3<f64>
}

#[cfg_attr(rustfmt, rustfmt_skip)]
impl<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point4<f64>>
for Displace<'a, Source, XDisplace, YDisplace, ZDisplace, UDisplace>
impl<Source, XDisplace, YDisplace, ZDisplace, UDisplace> NoiseFn<Point4<f64>>
for Displace<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<'a, Source: 'a> {
pub struct RotatePoint<Source> {
/// Source function that outputs a value
pub source: &'a Source,
pub source: 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<'a, Source: 'a> {
pub u_angle: f64,
}

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

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

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

impl<'a, Source> NoiseFn<Point4<f64>> for RotatePoint<'a, Source>
impl<Source> NoiseFn<Point4<f64>> for RotatePoint<Source>
where
Source: NoiseFn<Point4<f64>>,
{
Expand Down
56 changes: 49 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<'a, Source: 'a> {
pub struct ScalePoint<Source> {
/// Source function that outputs a value
pub source: &'a Source,
pub source: 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<'a, Source: 'a> {
pub u_scale: f64,
}

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

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

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

impl<'a, Source> NoiseFn<Point4<f64>> for ScalePoint<'a, Source>
impl<Source> NoiseFn<Point4<f64>> for ScalePoint<Source>
where
Source: NoiseFn<Point4<f64>>,
{
Expand All @@ -122,3 +122,45 @@ where
])
}
}

#[cfg(test)]
mod tests {
use super::*;
use super::super::super::Perlin;

#[test]
fn test_pass_by_ref() {
let source = Perlin::new();
let transformed_by_ref = ScalePoint::new(&source).
set_x_scale(0.8).
set_y_scale(0.1).
set_z_scale(0.4).
set_u_scale(0.2);
let mut zero_count = 0;

for x in 0..10 {
for y in 0..10 {
for z in 0..10 {
for u in 0..10 {
let point: [f64; 4] = [
(x as f64) / 10.0,
(y as f64) / 10.0,
(z as f64) / 10.0,
(u as f64) / 10.0
];
let source_value = source.get(point);
let transform_value = transformed_by_ref.get(point);

if source_value != 0.0 {
assert_ne!(source_value, transform_value);
} else {
zero_count += 1;
}
}
}
}
}

assert!(zero_count < 10 * 10 * 10 * 10);
}
}
Loading

0 comments on commit 7ba934b

Please sign in to comment.