mirror of
https://github.com/servo/servo.git
synced 2025-08-11 08:25:32 +01:00
Never store a squared root in SquaredDistance
This commit is contained in:
parent
df079286c2
commit
6ce5e9a869
4 changed files with 27 additions and 31 deletions
|
@ -166,19 +166,19 @@ impl ComputeSquaredDistance for Color {
|
|||
// All comments from the Animate impl also applies here.
|
||||
if self.foreground_ratio == other.foreground_ratio {
|
||||
if self.is_currentcolor() {
|
||||
Ok(SquaredDistance::Value(0.))
|
||||
Ok(SquaredDistance::from_sqrt(0.))
|
||||
} else {
|
||||
self.color.compute_squared_distance(&other.color)
|
||||
}
|
||||
} else if self.is_currentcolor() && other.is_numeric() {
|
||||
Ok(
|
||||
RGBA::transparent().compute_squared_distance(&other.color)? +
|
||||
SquaredDistance::Value(1.),
|
||||
SquaredDistance::from_sqrt(1.),
|
||||
)
|
||||
} else if self.is_numeric() && other.is_currentcolor() {
|
||||
Ok(
|
||||
self.color.compute_squared_distance(&RGBA::transparent())? +
|
||||
SquaredDistance::Value(1.),
|
||||
SquaredDistance::from_sqrt(1.),
|
||||
)
|
||||
} else {
|
||||
let self_color = self.effective_intermediate_rgba();
|
||||
|
|
|
@ -28,38 +28,43 @@ pub trait ComputeSquaredDistance {
|
|||
|
||||
/// A distance between two animatable values.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum SquaredDistance {
|
||||
/// Represented as the square root of the squared distance.
|
||||
Sqrt(f64),
|
||||
/// Represented as the squared distance itself.
|
||||
Value(f64),
|
||||
pub struct SquaredDistance {
|
||||
value: f64,
|
||||
}
|
||||
|
||||
impl SquaredDistance {
|
||||
/// Returns a squared distance from its square root.
|
||||
#[inline]
|
||||
pub fn from_sqrt(sqrt: f64) -> Self {
|
||||
Self { value: sqrt * sqrt }
|
||||
}
|
||||
}
|
||||
|
||||
impl ComputeSquaredDistance for u16 {
|
||||
#[inline]
|
||||
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
||||
Ok(SquaredDistance::Sqrt(((*self as f64) - (*other as f64)).abs()))
|
||||
Ok(SquaredDistance::from_sqrt(((*self as f64) - (*other as f64)).abs()))
|
||||
}
|
||||
}
|
||||
|
||||
impl ComputeSquaredDistance for i32 {
|
||||
#[inline]
|
||||
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
||||
Ok(SquaredDistance::Sqrt((*self - *other).abs() as f64))
|
||||
Ok(SquaredDistance::from_sqrt((*self - *other).abs() as f64))
|
||||
}
|
||||
}
|
||||
|
||||
impl ComputeSquaredDistance for f32 {
|
||||
#[inline]
|
||||
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
||||
Ok(SquaredDistance::Sqrt((*self - *other).abs() as f64))
|
||||
Ok(SquaredDistance::from_sqrt((*self - *other).abs() as f64))
|
||||
}
|
||||
}
|
||||
|
||||
impl ComputeSquaredDistance for f64 {
|
||||
#[inline]
|
||||
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
||||
Ok(SquaredDistance::Sqrt((*self - *other).abs()))
|
||||
Ok(SquaredDistance::from_sqrt((*self - *other).abs()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,7 +82,7 @@ impl<T> ComputeSquaredDistance for Option<T>
|
|||
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
||||
match (self.as_ref(), other.as_ref()) {
|
||||
(Some(this), Some(other)) => this.compute_squared_distance(other),
|
||||
(None, None) => Ok(SquaredDistance::Value(0.)),
|
||||
(None, None) => Ok(SquaredDistance::from_sqrt(0.)),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
|
@ -94,21 +99,16 @@ impl<T> ComputeSquaredDistance for Size2D<T>
|
|||
|
||||
impl SquaredDistance {
|
||||
/// Returns the square root of this squared distance.
|
||||
#[inline]
|
||||
pub fn sqrt(self) -> f64 {
|
||||
match self {
|
||||
SquaredDistance::Sqrt(this) => this,
|
||||
SquaredDistance::Value(this) => this.sqrt(),
|
||||
}
|
||||
self.value.sqrt()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SquaredDistance> for f64 {
|
||||
#[inline]
|
||||
fn from(distance: SquaredDistance) -> Self {
|
||||
match distance {
|
||||
SquaredDistance::Sqrt(this) => this * this,
|
||||
SquaredDistance::Value(this) => this,
|
||||
}
|
||||
distance.value
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,19 +117,15 @@ impl Add for SquaredDistance {
|
|||
|
||||
#[inline]
|
||||
fn add(self, rhs: Self) -> Self {
|
||||
SquaredDistance::Value(f64::from(self) + f64::from(rhs))
|
||||
SquaredDistance { value: self.value + rhs.value }
|
||||
}
|
||||
}
|
||||
|
||||
impl Sum for SquaredDistance {
|
||||
fn sum<I>(mut iter: I) -> Self
|
||||
fn sum<I>(iter: I) -> Self
|
||||
where
|
||||
I: Iterator<Item = Self>,
|
||||
{
|
||||
let first = match iter.next() {
|
||||
Some(first) => first,
|
||||
None => return SquaredDistance::Value(0.),
|
||||
};
|
||||
iter.fold(first, Add::add)
|
||||
iter.fold(SquaredDistance::from_sqrt(0.), Add::add)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue