-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1049 from hannobraun/approx
Clean up and document approximation code
- Loading branch information
Showing
8 changed files
with
149 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,85 @@ | ||
//! Edge approximation | ||
//! | ||
//! The approximation of a curve is its first vertex, combined with the | ||
//! approximation of its curve. The second vertex is left off, as edge | ||
//! approximations are usually used to build cycle approximations, and this way, | ||
//! the caller doesn't have to call with duplicate vertices. | ||
use fj_math::{Point, Scalar}; | ||
|
||
use crate::objects::Edge; | ||
use crate::objects::{Edge, GlobalVertex, SurfaceVertex, Vertex}; | ||
|
||
use super::{curve::RangeOnCurve, Approx}; | ||
|
||
impl Approx for Edge { | ||
impl Approx for &Edge { | ||
type Approximation = Vec<(Point<2>, Point<3>)>; | ||
type Params = (); | ||
|
||
fn approx( | ||
&self, | ||
tolerance: super::Tolerance, | ||
(): Self::Params, | ||
) -> Self::Approximation { | ||
fn approx(self, tolerance: super::Tolerance) -> Self::Approximation { | ||
// The range is only used for circles right now. | ||
let boundary = match self.vertices().get() { | ||
Some(vertices) => vertices.map(|vertex| { | ||
(vertex.position(), vertex.global_form().position()) | ||
}), | ||
Some(vertices) => vertices.map(|&vertex| vertex), | ||
None => { | ||
// Creating vertices from nothing, just for the sake of | ||
// approximation is a bit weird. But this code is a temporary | ||
// fallback anyway. It'll do for now, and it will likely be | ||
// removed soon. | ||
|
||
let start_curve = Point::from([Scalar::ZERO]); | ||
let end_curve = Point::from([Scalar::TAU]); | ||
|
||
// We're dealing with a circle here. Start and end are identical | ||
// points, in global coordinates. | ||
let point_global = self | ||
.global_form() | ||
.curve() | ||
.kind() | ||
.point_from_curve_coords(start_curve); | ||
let vertex_global = { | ||
let point_global = self | ||
.global_form() | ||
.curve() | ||
.kind() | ||
.point_from_curve_coords(start_curve); | ||
|
||
GlobalVertex::from_position(point_global) | ||
}; | ||
|
||
[(start_curve, point_global), (end_curve, point_global)] | ||
let [start_surface, end_surface] = [start_curve, end_curve] | ||
.map(|point_curve| { | ||
let point_surface = self | ||
.curve() | ||
.kind() | ||
.point_from_curve_coords(point_curve); | ||
SurfaceVertex::new( | ||
point_surface, | ||
*self.curve().surface(), | ||
vertex_global, | ||
) | ||
}); | ||
|
||
let a = Vertex::new( | ||
start_curve, | ||
*self.curve(), | ||
start_surface, | ||
vertex_global, | ||
); | ||
let b = Vertex::new( | ||
end_curve, | ||
*self.curve(), | ||
end_surface, | ||
vertex_global, | ||
); | ||
|
||
[a, b] | ||
} | ||
}; | ||
|
||
let range = RangeOnCurve { boundary }; | ||
|
||
self.curve().approx(tolerance, range) | ||
let mut points = (self.curve(), range).approx(tolerance); | ||
points.insert( | ||
0, | ||
( | ||
range.start().surface_form().position(), | ||
range.start().global_form().position(), | ||
), | ||
); | ||
|
||
points | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,21 @@ | ||
//! Approximation of objects | ||
mod curve; | ||
mod cycle; | ||
mod edge; | ||
mod face; | ||
mod tolerance; | ||
pub mod curve; | ||
pub mod cycle; | ||
pub mod edge; | ||
pub mod face; | ||
pub mod tolerance; | ||
|
||
pub use self::{ | ||
cycle::CycleApprox, | ||
face::FaceApprox, | ||
tolerance::{InvalidTolerance, Tolerance}, | ||
}; | ||
pub use self::tolerance::{InvalidTolerance, Tolerance}; | ||
|
||
/// Approximate an object | ||
pub trait Approx { | ||
/// The approximation of the object | ||
type Approximation; | ||
|
||
/// Additional parameters required for the approximation | ||
type Params; | ||
|
||
/// Approximate the object | ||
/// | ||
/// `tolerance` defines how far the approximation is allowed to deviate from | ||
/// the actual object. | ||
fn approx( | ||
&self, | ||
tolerance: Tolerance, | ||
params: Self::Params, | ||
) -> Self::Approximation; | ||
fn approx(self, tolerance: Tolerance) -> Self::Approximation; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
//! Tolerance value for approximation | ||
//! | ||
//! See [`Tolerance`]. | ||
use fj_math::Scalar; | ||
|
||
/// A tolerance value | ||
|
Oops, something went wrong.