Implement to_transform_3d_matrix for computing distance of mismatched transform lists.

We could use this method to convert a TransformList into a Matrix, and
use this matrix for computing distance for Stylo and rendering the transform
for Servo.

This is an equivalent of nsStyleTransformMatrix::ReadTransforms in Gecko.
This commit is contained in:
Boris Chiou 2017-08-22 10:25:35 +08:00
parent 772a8464eb
commit 5c2d8507be
5 changed files with 215 additions and 146 deletions

View file

@ -8,7 +8,6 @@
use app_units::Au;
use cssparser::Parser;
use euclid::Point3D;
#[cfg(feature = "gecko")] use gecko_bindings::bindings::RawServoAnimationValueMap;
#[cfg(feature = "gecko")] use gecko_bindings::structs::RawGeckoGfxMatrix4x4;
#[cfg(feature = "gecko")] use gecko_bindings::structs::nsCSSPropertyID;
@ -53,6 +52,7 @@ use values::computed::{NonNegativeNumber, Number, NumberOrPercentage, Percentage
use values::computed::{PositiveIntegerOrAuto, ToComputedValue};
use values::computed::length::{NonNegativeLengthOrAuto, NonNegativeLengthOrNormal};
use values::computed::length::NonNegativeLengthOrPercentage;
use values::computed::transform::DirectionVector;
use values::distance::{ComputeSquaredDistance, SquaredDistance};
use values::generics::NonNegative;
use values::generics::effects::Filter;
@ -1107,7 +1107,7 @@ impl ToAnimatedZero for TransformOperation {
Ok(TransformOperation::Scale(1.0, 1.0, 1.0))
},
TransformOperation::Rotate(x, y, z, a) => {
let (x, y, z, _) = get_normalized_vector_and_angle(x, y, z, a);
let (x, y, z, _) = DirectionVector::get_normalized_vector_and_angle(x, y, z, a);
Ok(TransformOperation::Rotate(x, y, z, Angle::zero()))
},
TransformOperation::Perspective(..) |
@ -1184,8 +1184,10 @@ impl Animate for TransformOperation {
&TransformOperation::Rotate(fx, fy, fz, fa),
&TransformOperation::Rotate(tx, ty, tz, ta),
) => {
let (fx, fy, fz, fa) = get_normalized_vector_and_angle(fx, fy, fz, fa);
let (tx, ty, tz, ta) = get_normalized_vector_and_angle(tx, ty, tz, ta);
let (fx, fy, fz, fa) =
DirectionVector::get_normalized_vector_and_angle(fx, fy, fz, fa);
let (tx, ty, tz, ta) =
DirectionVector::get_normalized_vector_and_angle(tx, ty, tz, ta);
if (fx, fy, fz) == (tx, ty, tz) {
let ia = fa.animate(&ta, procedure)?;
Ok(TransformOperation::Rotate(fx, fy, fz, ia))
@ -1598,11 +1600,6 @@ pub struct MatrixDecomposed3D {
pub quaternion: Quaternion,
}
/// A wrapper of Point3D to represent the direction vector (rotate axis) for Rotate3D.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct DirectionVector(Point3D<f64>);
impl Quaternion {
/// Return a quaternion from a unit direction vector and angle (unit: radian).
#[inline]
@ -1642,47 +1639,6 @@ impl ComputeSquaredDistance for Quaternion {
}
}
impl DirectionVector {
/// Create a DirectionVector.
#[inline]
fn new(x: f32, y: f32, z: f32) -> Self {
DirectionVector(Point3D::new(x as f64, y as f64, z as f64))
}
/// Return the normalized direction vector.
#[inline]
fn normalize(&mut self) -> bool {
let len = self.length();
if len > 0. {
self.0.x = self.0.x / len;
self.0.y = self.0.y / len;
self.0.z = self.0.z / len;
true
} else {
false
}
}
/// Get the length of this vector.
#[inline]
fn length(&self) -> f64 {
self.0.to_array().iter().fold(0f64, |sum, v| sum + v * v).sqrt()
}
}
/// Return the normalized direction vector and its angle.
// A direction vector that cannot be normalized, such as [0,0,0], will cause the
// rotation to not be applied. i.e. Use an identity matrix or rotate3d(0, 0, 1, 0).
fn get_normalized_vector_and_angle(x: f32, y: f32, z: f32, angle: Angle)
-> (f32, f32, f32, Angle) {
let mut vector = DirectionVector::new(x, y, z);
if vector.normalize() {
(vector.0.x as f32, vector.0.y as f32, vector.0.z as f32, angle)
} else {
(0., 0., 1., Angle::zero())
}
}
/// Decompose a 3D matrix.
/// https://drafts.csswg.org/css-transforms/#decomposing-a-3d-matrix
fn decompose_3d_matrix(mut matrix: ComputedMatrix) -> Result<MatrixDecomposed3D, ()> {
@ -2347,8 +2303,10 @@ impl ComputeSquaredDistance for TransformOperation {
&TransformOperation::Rotate(fx, fy, fz, fa),
&TransformOperation::Rotate(tx, ty, tz, ta),
) => {
let (fx, fy, fz, angle1) = get_normalized_vector_and_angle(fx, fy, fz, fa);
let (tx, ty, tz, angle2) = get_normalized_vector_and_angle(tx, ty, tz, ta);
let (fx, fy, fz, angle1) =
DirectionVector::get_normalized_vector_and_angle(fx, fy, fz, fa);
let (tx, ty, tz, angle2) =
DirectionVector::get_normalized_vector_and_angle(tx, ty, tz, ta);
if (fx, fy, fz) == (tx, ty, tz) {
angle1.compute_squared_distance(&angle2)
} else {
@ -2395,10 +2353,10 @@ impl ComputeSquaredDistance for TransformOperation {
impl ComputeSquaredDistance for TransformList {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
let this = self.0.as_ref().map_or(&[][..], |l| l);
let other = other.0.as_ref().map_or(&[][..], |l| l);
let list1 = self.0.as_ref().map_or(&[][..], |l| l);
let list2 = other.0.as_ref().map_or(&[][..], |l| l);
this.iter().zip_longest(other).map(|it| {
let squared_dist: Result<SquaredDistance, _> = list1.iter().zip_longest(list2).map(|it| {
match it {
EitherOrBoth::Both(this, other) => {
this.compute_squared_distance(other)
@ -2407,7 +2365,16 @@ impl ComputeSquaredDistance for TransformList {
list.to_animated_zero()?.compute_squared_distance(list)
},
}
}).sum()
}).sum();
// Roll back to matrix interpolation if there is any Err(()) in the transform lists, such
// as mismatched transform functions.
if let Err(_) = squared_dist {
let matrix1: ComputedMatrix = self.to_transform_3d_matrix(None).ok_or(())?.into();
let matrix2: ComputedMatrix = other.to_transform_3d_matrix(None).ok_or(())?.into();
return matrix1.compute_squared_distance(&matrix2);
}
squared_dist
}
}