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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use super::implicit_base::{ImplicitModuleBase, ScalarParameter};
use super::utility::{lerp, quintic_blend};
use super::ImplicitModule;

use std::rc::Rc;
use std::cell::RefCell;

pub struct ImplicitSelect {
    base: ImplicitModuleBase,
    low: ScalarParameter,
    high: ScalarParameter,
    control: ScalarParameter,
    threshold: ScalarParameter,
    falloff: ScalarParameter,
}

impl ImplicitSelect {
    pub fn new() -> ImplicitSelect {
        ImplicitSelect {
            base: Default::default(),
            low: ScalarParameter::Value(0.0),
            high: ScalarParameter::Value(0.0),
            control: ScalarParameter::Value(0.0),
            threshold: ScalarParameter::Value(0.0),
            falloff: ScalarParameter::Value(0.0),
        }
    }

    pub fn set_low_source_module(&mut self, b: Rc<RefCell<ImplicitModule>>) {
        self.low = ScalarParameter::Source(b);
    }

    pub fn set_low_source_value(&mut self, v: f64) {
        self.low = ScalarParameter::Value(v);
    }

    pub fn set_high_source_module(&mut self, b: Rc<RefCell<ImplicitModule>>) {
        self.high = ScalarParameter::Source(b);
    }

    pub fn set_high_source_value(&mut self, v: f64) {
        self.high = ScalarParameter::Value(v);
    }

    pub fn set_control_source_module(&mut self, b: Rc<RefCell<ImplicitModule>>) {
        self.control = ScalarParameter::Source(b);
    }

    pub fn set_control_source_value(&mut self, v: f64) {
        self.control = ScalarParameter::Value(v);
    }

    pub fn set_threshold_source_module(&mut self, b: Rc<RefCell<ImplicitModule>>) {
        self.threshold = ScalarParameter::Source(b);
    }

    pub fn set_threshold_source_value(&mut self, v: f64) {
        self.threshold = ScalarParameter::Value(v);
    }

    pub fn set_falloff_source_module(&mut self, b: Rc<RefCell<ImplicitModule>>) {
        self.falloff = ScalarParameter::Source(b);
    }

    pub fn set_falloff_source_value(&mut self, v: f64) {
        self.falloff = ScalarParameter::Value(v);
    }
}

impl ImplicitModule for ImplicitSelect {
    fn set_seed(&mut self, _: u32) {}

    fn get_2d(&mut self, x: f64, y: f64) -> f64 {
        let control = self.control.get_2d(x, y);
        let falloff = self.falloff.get_2d(x, y);
        let threshold = self.threshold.get_2d(x, y);

        if falloff > 0.0 {
            if control < (threshold - falloff) {
                self.low.get_2d(x, y)
            } else if control > (threshold + falloff) {
                self.high.get_2d(x, y)
            } else {
                let lower = threshold - falloff;
                let upper = threshold + falloff;
                let blend = quintic_blend((control - lower) / (upper - lower));
                lerp(blend, self.low.get_2d(x, y), self.high.get_2d(x, y))
            }
        } else {
            if control < threshold {
                self.low.get_2d(x, y)
            } else {
                self.high.get_2d(x, y)
            }
        }
    }
    fn get_3d(&mut self, x: f64, y: f64, z: f64) -> f64 {
        let control = self.control.get_3d(x, y, z);
        let falloff = self.falloff.get_3d(x, y, z);
        let threshold = self.threshold.get_3d(x, y, z);

        if falloff > 0.0 {
            if control < (threshold - falloff) {
                self.low.get_3d(x, y, z)
            } else if control > (threshold + falloff) {
                self.high.get_3d(x, y, z)
            } else {
                let lower = threshold - falloff;
                let upper = threshold + falloff;
                let blend = quintic_blend((control - lower) / (upper - lower));
                lerp(blend, self.low.get_3d(x, y, z), self.high.get_3d(x, y, z))
            }
        } else {
            if control < threshold {
                self.low.get_3d(x, y, z)
            } else {
                self.high.get_3d(x, y, z)
            }
        }
    }
    fn get_4d(&mut self, x: f64, y: f64, z: f64, w: f64) -> f64 {
        let control = self.control.get_4d(x, y, z, w);
        let falloff = self.falloff.get_4d(x, y, z, w);
        let threshold = self.threshold.get_4d(x, y, z, w);

        if falloff > 0.0 {
            if control < (threshold - falloff) {
                self.low.get_4d(x, y, z, w)
            } else if control > (threshold + falloff) {
                self.high.get_4d(x, y, z, w)
            } else {
                let lower = threshold - falloff;
                let upper = threshold + falloff;
                let blend = quintic_blend((control - lower) / (upper - lower));
                lerp(blend,
                     self.low.get_4d(x, y, z, w),
                     self.high.get_4d(x, y, z, w))
            }
        } else {
            if control < threshold {
                self.low.get_4d(x, y, z, w)
            } else {
                self.high.get_4d(x, y, z, w)
            }
        }
    }
    fn get_6d(&mut self, x: f64, y: f64, z: f64, w: f64, u: f64, v: f64) -> f64 {
        let control = self.control.get_6d(x, y, z, w, u, v);
        let falloff = self.falloff.get_6d(x, y, z, w, u, v);
        let threshold = self.threshold.get_6d(x, y, z, w, u, v);

        if falloff > 0. {
            if control < (threshold - falloff) {
                self.low.get_6d(x, y, z, w, u, v)
            } else if control > (threshold + falloff) {
                self.high.get_6d(x, y, z, w, u, v)
            } else {
                let lower = threshold - falloff;
                let upper = threshold + falloff;
                let blend = quintic_blend((control - lower) / (upper - lower));
                lerp(blend,
                     self.low.get_6d(x, y, z, w, u, v),
                     self.high.get_6d(x, y, z, w, u, v))
            }
        } else {
            if control < threshold {
                self.low.get_6d(x, y, z, w, u, v)
            } else {
                self.high.get_6d(x, y, z, w, u, v)
            }
        }
    }

    fn spacing(&self) -> f64 {
        self.base.spacing
    }

    fn set_deriv_spacing(&mut self, s: f64) {
        self.base.spacing = s;
    }
}