Skip to content

Commit

Permalink
Merge pull request #1148 from urschrei/compose_many
Browse files Browse the repository at this point in the history
Add method to compose an arbitrary number of affine ops
  • Loading branch information
urschrei authored Feb 9, 2024
2 parents 4cd6d9f + 3375904 commit d3aac79
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions geo/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
* Update rstar to v0.12.0
* Implement `CoordsIter` for arrays and slices. This is useful when you'd like to use traits
implemented for `CoordsIter` without re-allocating (e.g., creating a `MultiPoint`).
* Add `compose_many` method to `AffineOps`
* <https://github.com/georust/geo/pull/1148>

## 0.27.0

Expand Down
26 changes: 26 additions & 0 deletions geo/src/algorithm/affine_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ impl<T: CoordNum> AffineTransform<T> {
],
])
}

/// Create a new affine transformation by composing an arbitrary number of `AffineTransform`s.
///
/// This is a **cumulative** operation; the new transform is *added* to the existing transform.
/// ```
/// use geo::AffineTransform;
/// let mut transform = AffineTransform::identity();
///
/// // create two transforms that cancel each other
/// let transform1 = AffineTransform::translate(1.0, 2.0);
/// let transform2 = AffineTransform::translate(-1.0, -2.0);
/// let transforms = vec![transform1, transform2];
///
/// // apply them
/// let outcome = transform.compose_many(&transforms);
/// // we should be back to square one
/// assert!(outcome.is_identity());
/// ```
#[must_use]
pub fn compose_many(&self, transforms: &[Self]) -> Self {
self.compose(&transforms.iter().fold(
AffineTransform::default(),
|acc: AffineTransform<T>, transform| acc.compose(transform),
))
}

/// Create the identity matrix
///
/// The matrix is:
Expand Down

0 comments on commit d3aac79

Please sign in to comment.