| 1 |
/* |
| 2 |
* // Copyright (c) Radzivon Bartoshyk 6/2025. All rights reserved. |
| 3 |
* // |
| 4 |
* // Redistribution and use in source and binary forms, with or without modification, |
| 5 |
* // are permitted provided that the following conditions are met: |
| 6 |
* // |
| 7 |
* // 1. Redistributions of source code must retain the above copyright notice, this |
| 8 |
* // list of conditions and the following disclaimer. |
| 9 |
* // |
| 10 |
* // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
* // this list of conditions and the following disclaimer in the documentation |
| 12 |
* // and/or other materials provided with the distribution. |
| 13 |
* // |
| 14 |
* // 3. Neither the name of the copyright holder nor the names of its |
| 15 |
* // contributors may be used to endorse or promote products derived from |
| 16 |
* // this software without specific prior written permission. |
| 17 |
* // |
| 18 |
* // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
* // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
* // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 |
* // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 |
* // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 |
* // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 |
* // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 |
* // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 |
* // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 |
* // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
#![allow(dead_code)] |
| 30 |
use crate::PointeeSizeExpressible; |
| 31 |
use crate::matan::is_curve_descending; |
| 32 |
use num_traits::AsPrimitive; |
| 33 |
|
| 34 |
pub(crate) fn limit_slope<T: Copy + AsPrimitive<f32> + PartialOrd + PointeeSizeExpressible>( |
| 35 |
curve: &mut [T], |
| 36 |
value_cap: f32, |
| 37 |
) where |
| 38 |
f32: AsPrimitive<T>, |
| 39 |
{ |
| 40 |
let at_begin = (curve.len() as f32 * 0.02 + 0.5).floor() as usize; // Cutoff at 2% |
| 41 |
if at_begin == 0 { |
| 42 |
return; |
| 43 |
} |
| 44 |
let at_end = curve.len() - at_begin - 1; // And 98% |
| 45 |
let (begin_val, end_val) = if is_curve_descending(curve) { |
| 46 |
(value_cap, 0.) |
| 47 |
} else { |
| 48 |
(0., value_cap) |
| 49 |
}; |
| 50 |
let val = curve[at_begin].as_(); |
| 51 |
let slope = (val - begin_val) / at_begin as f32; |
| 52 |
let beta = val - slope * at_begin as f32; |
| 53 |
if T::FINITE { |
| 54 |
for v in curve.iter_mut().take(at_begin) { |
| 55 |
*v = (v.as_() * slope + beta) |
| 56 |
.round() |
| 57 |
.min(value_cap) |
| 58 |
.max(0.0) |
| 59 |
.as_(); |
| 60 |
} |
| 61 |
} else { |
| 62 |
for v in curve.iter_mut().take(at_begin) { |
| 63 |
*v = (v.as_() * slope + beta).min(value_cap).max(0.0).as_(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
let val = curve[at_end].as_(); |
| 68 |
let slope = (end_val - val) / at_begin as f32; |
| 69 |
let beta = val - slope * at_end as f32; |
| 70 |
|
| 71 |
if T::FINITE { |
| 72 |
for v in curve.iter_mut().skip(at_end) { |
| 73 |
*v = (v.as_() * slope + beta) |
| 74 |
.round() |
| 75 |
.min(value_cap) |
| 76 |
.max(0.0) |
| 77 |
.as_(); |
| 78 |
} |
| 79 |
} else { |
| 80 |
for v in curve.iter_mut().skip(at_end) { |
| 81 |
*v = (v.as_() * slope + beta).min(value_cap).max(0.0).as_(); |
| 82 |
} |
| 83 |
} |
| 84 |
} |