Skip to content

Commit

Permalink
Additional documentation and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Stoeoef committed Nov 19, 2023
1 parent e0889d7 commit f2c2a5b
Show file tree
Hide file tree
Showing 11 changed files with 1,382 additions and 7 deletions.
15 changes: 15 additions & 0 deletions examples/svg_renderer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ type Result = core::result::Result<(), Box<dyn std::error::Error>>;
/// Used for rendering SVGs for documentation. These are inlined (via #[doc = include_str!(...)])
/// into the doc comment of a few items. That makes sure they will be visible even for offline users.
fn main() -> Result {
scenario_list::refinement_maximum_area_scenario(None).save_to_svg(
"refinement_maximum_area_no_limit",
"images/refinement_maximum_area_no_limit.svg",
)?;

scenario_list::refinement_maximum_area_scenario(Some(200.0)).save_to_svg(
"refinement_maximum_area_200",
"images/refinement_maximum_area_200.svg",
)?;

scenario_list::refinement_maximum_area_scenario(Some(100.0)).save_to_svg(
"refinement_maximum_area_100",
"images/refinement_maximum_area_100.svg",
)?;

scenario_list::shape_iterator_scenario(true, true).save_to_svg(
"shape_iterator_circle_vertices",
"images/shape_iterator_circle_vertices.svg",
Expand Down
25 changes: 25 additions & 0 deletions examples/svg_renderer/scenario_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,3 +1113,28 @@ fn create_angle_limit_cdt() -> Cdt {
cdt.add_constraint_edges(vertices, true).unwrap();
cdt
}

pub fn refinement_maximum_area_scenario(max_area: Option<f64>) -> Sketch {
let triangulation = big_triangulation().unwrap();
let mut cdt = Cdt::from(triangulation);

cdt.refine(RefinementParameters::default().with_max_allowed_area(max_area.unwrap_or(2000.)));

let mut result = convert_refinement_cdt(&mut cdt);

result.set_width(290);
let description = if let Some(max_area) = max_area {
format!("Max area: {max_area}")
} else {
"No max area".to_string()
};

result.add(
SketchElement::text(description)
.font_size(10.0)
.angle(Deg(-33.2))
.position(Point2::new(-75.0, -20.0)),
);

result
}
665 changes: 665 additions & 0 deletions images/refinement_maximum_area_100.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
403 changes: 403 additions & 0 deletions images/refinement_maximum_area_200.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
213 changes: 213 additions & 0 deletions images/refinement_maximum_area_no_limit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion images/shape_iterator_rectangle_vertices.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 24 additions & 2 deletions src/cdt.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{delaunay_core::Dcel, intersection_iterator::LineIntersectionIterator};
use crate::{handles::*, intersection_iterator::Intersection};
use crate::{
HasPosition, HintGenerator, InsertionError, LastUsedVertexHintGenerator, Point2, Triangulation,
TriangulationExt,
DelaunayTriangulation, HasPosition, HintGenerator, InsertionError, LastUsedVertexHintGenerator,
Point2, Triangulation, TriangulationExt,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -233,6 +233,28 @@ where
}
}

impl<V, DE, UE, F, L> From<DelaunayTriangulation<V, DE, UE, F, L>>
for ConstrainedDelaunayTriangulation<V, DE, UE, F, L>
where
V: HasPosition,
DE: Default,
UE: Default,
F: Default,
L: HintGenerator<<V as HasPosition>::Scalar>,
{
fn from(value: DelaunayTriangulation<V, DE, UE, F, L>) -> Self {
let dcel = value.dcel;
let s = dcel.map_undirected_edges(|edge| CdtEdge(false, edge));
let lookup = value.hint_generator;

ConstrainedDelaunayTriangulation {
s,
num_constraints: 0,
lookup,
}
}
}

impl<V, DE, UE, F, L> ConstrainedDelaunayTriangulation<V, DE, UE, F, L>
where
V: HasPosition,
Expand Down
19 changes: 19 additions & 0 deletions src/delaunay_core/dcel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ impl<V, DE, UE, F> Dcel<V, DE, UE, F> {
self.faces.truncate(1); // Keep outer face
}

pub fn map_undirected_edges<M, UE2>(self, f: M) -> Dcel<V, DE, UE2, F>
where
M: Fn(UE) -> UE2,
{
Dcel {
vertices: self.vertices,
edges: self
.edges
.into_iter()
.map(|edge_data| EdgeEntry {
entries: edge_data.entries,
directed_data: edge_data.directed_data,
undirected_data: f(edge_data.undirected_data),
})
.collect(),
faces: self.faces,
}
}

pub fn num_vertices(&self) -> usize {
self.vertices.len()
}
Expand Down
2 changes: 1 addition & 1 deletion src/delaunay_core/hint_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl<S: SpadeNum> HintGenerator<S> for LastUsedVertexHintGenerator {
/// ```
pub type HierarchyHintGenerator<S> = HierarchyHintGeneratorWithBranchFactor<S, 16>;

#[derive(Debug)]
#[derive(Debug, Clone)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
Expand Down
7 changes: 6 additions & 1 deletion src/delaunay_core/refinement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ impl<S: SpadeNum + Float> RefinementParameters<S> {
/// By specifying an upper area bound for triangles, the resulting triangle sizes can be made more similar
/// as any large triangle above the bound will be split into smaller parts.
///
/// # Examples of different maximum area values
#[doc = concat!(include_str!("../../images/refinement_maximum_area_no_limit.svg"))]
#[doc = concat!(include_str!("../../images/refinement_maximum_area_200.svg"))]
#[doc = concat!(include_str!("../../images/refinement_maximum_area_100.svg"))]
///
/// Should be set to something larger than [Self::with_min_required_area]. If this method is not called, no upper area
/// bound check will be performed.
pub fn with_max_allowed_area(mut self, max_area: S) -> Self {
Expand Down Expand Up @@ -534,7 +539,7 @@ where
let num_initial_vertices: usize = self.num_vertices();
let num_additional_vertices = parameters
.max_additional_vertices
.unwrap_or_else(|| num_initial_vertices * 10);
.unwrap_or(num_initial_vertices * 10);
let max_allowed_vertices = num_initial_vertices + num_additional_vertices;

let mut refinement_complete = true;
Expand Down
12 changes: 10 additions & 2 deletions src/delaunay_triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ where
F: Default,
L: HintGenerator<<V as HasPosition>::Scalar>,
{
dcel: Dcel<V, DE, UE, F>,
hint_generator: L,
pub(crate) dcel: Dcel<V, DE, UE, F>,
pub(crate) hint_generator: L,
}

impl<V, DE, UE, F, L> DelaunayTriangulation<V, DE, UE, F, L>
Expand Down Expand Up @@ -400,4 +400,12 @@ mod test {
assert!(d.nearest_neighbor(Point2::new(0.0, 1.0)).is_some());
Ok(())
}

#[test]
#[allow(clippy::redundant_clone)]
#[allow(unused_must_use)]
fn test_clone_is_implemented() {
// Just needs to compile
DelaunayTriangulation::<Point2<f64>>::new().clone();
}
}

0 comments on commit f2c2a5b

Please sign in to comment.