Add TransformObject::Transformed

This makes the trait more flexible. I'm going to need this additional
flexibility for an upcoming change.
This commit is contained in:
Hanno Braun 2024-06-14 20:54:21 +02:00
parent f3432b1f5e
commit 1c356747d9
10 changed files with 49 additions and 15 deletions

View File

@ -10,12 +10,14 @@ use crate::{
use super::{TransformCache, TransformObject};
impl TransformObject for Handle<Curve> {
type Transformed = Self;
fn transform_with_cache(
&self,
_: &Transform,
core: &mut Core,
cache: &mut TransformCache,
) -> Self {
) -> Self::Transformed {
cache
.entry(self)
.or_insert_with(|| {

View File

@ -5,12 +5,14 @@ use crate::{topology::Cycle, Core};
use super::{TransformCache, TransformObject};
impl TransformObject for Cycle {
type Transformed = Self;
fn transform_with_cache(
&self,
transform: &Transform,
core: &mut Core,
cache: &mut TransformCache,
) -> Self {
) -> Self::Transformed {
let half_edges = self.half_edges().iter().map(|half_edge| {
half_edge
.clone()

View File

@ -7,12 +7,14 @@ use crate::{
use super::{TransformCache, TransformObject};
impl TransformObject for Handle<HalfEdge> {
type Transformed = Self;
fn transform_with_cache(
&self,
transform: &Transform,
core: &mut Core,
cache: &mut TransformCache,
) -> Self {
) -> Self::Transformed {
let curve = self
.curve()
.clone()

View File

@ -5,12 +5,14 @@ use crate::{topology::Face, Core};
use super::{TransformCache, TransformObject};
impl TransformObject for Face {
type Transformed = Self;
fn transform_with_cache(
&self,
transform: &Transform,
core: &mut Core,
cache: &mut TransformCache,
) -> Self {
) -> Self::Transformed {
let surface = self
.surface()
.clone()

View File

@ -34,8 +34,15 @@ use super::derive::DeriveFrom;
/// More convenience methods can be added as required. The only reason this
/// hasn't been done so far, is that no one has put in the work yet.
pub trait TransformObject: Sized {
/// The result of the transformation
type Transformed;
/// Transform the object
fn transform(&self, transform: &Transform, core: &mut Core) -> Self {
fn transform(
&self,
transform: &Transform,
core: &mut Core,
) -> Self::Transformed {
let mut cache = TransformCache::default();
self.transform_with_cache(transform, core, &mut cache)
}
@ -46,12 +53,16 @@ pub trait TransformObject: Sized {
transform: &Transform,
core: &mut Core,
cache: &mut TransformCache,
) -> Self;
) -> Self::Transformed;
/// Translate the object
///
/// Convenience wrapper around [`TransformObject::transform`].
fn translate(&self, offset: impl Into<Vector<3>>, core: &mut Core) -> Self {
fn translate(
&self,
offset: impl Into<Vector<3>>,
core: &mut Core,
) -> Self::Transformed {
self.transform(&Transform::translation(offset), core)
}
@ -62,22 +73,27 @@ pub trait TransformObject: Sized {
&self,
axis_angle: impl Into<Vector<3>>,
core: &mut Core,
) -> Self {
) -> Self::Transformed {
self.transform(&Transform::rotation(axis_angle), core)
}
}
impl<T> TransformObject for Handle<T>
where
T: Clone + Insert<Inserted = Handle<T>> + TransformObject + 'static,
T: Clone
+ Insert<Inserted = Handle<T>>
+ TransformObject<Transformed = T>
+ 'static,
Handle<T>: Into<AnyObject<Stored>>,
{
type Transformed = Self;
fn transform_with_cache(
&self,
transform: &Transform,
core: &mut Core,
cache: &mut TransformCache,
) -> Self {
) -> Self::Transformed {
if let Some(object) = cache.get(self) {
return object.clone();
}

View File

@ -3,12 +3,14 @@ use crate::{topology::Region, Core};
use super::TransformObject;
impl TransformObject for Region {
type Transformed = Self;
fn transform_with_cache(
&self,
transform: &fj_math::Transform,
core: &mut Core,
cache: &mut super::TransformCache,
) -> Self {
) -> Self::Transformed {
let exterior = self
.exterior()
.clone()

View File

@ -5,12 +5,14 @@ use crate::{topology::Shell, Core};
use super::{TransformCache, TransformObject};
impl TransformObject for Shell {
type Transformed = Self;
fn transform_with_cache(
&self,
transform: &Transform,
core: &mut Core,
cache: &mut TransformCache,
) -> Self {
) -> Self::Transformed {
let faces = self
.faces()
.iter()

View File

@ -5,12 +5,14 @@ use crate::{topology::Solid, Core};
use super::{TransformCache, TransformObject};
impl TransformObject for Solid {
type Transformed = Self;
fn transform_with_cache(
&self,
transform: &Transform,
core: &mut Core,
cache: &mut TransformCache,
) -> Self {
) -> Self::Transformed {
let shells =
self.shells().iter().cloned().map(|shell| {
shell.transform_with_cache(transform, core, cache)

View File

@ -7,12 +7,14 @@ use crate::{
use super::{TransformCache, TransformObject};
impl TransformObject for Handle<Surface> {
type Transformed = Self;
fn transform_with_cache(
&self,
transform: &Transform,
core: &mut Core,
cache: &mut TransformCache,
) -> Self {
) -> Self::Transformed {
cache
.entry(self)
.or_insert_with(|| {

View File

@ -5,12 +5,14 @@ use crate::{topology::Vertex, Core};
use super::{TransformCache, TransformObject};
impl TransformObject for Vertex {
type Transformed = Self;
fn transform_with_cache(
&self,
_: &Transform,
_: &mut Core,
_: &mut TransformCache,
) -> Self {
) -> Self::Transformed {
// There's nothing to actually transform here, as `Vertex` holds no
// data. We still need this implementation though, as a new `Vertex`
// object must be created to represent the new and transformed vertex.