Skip to content

Commit

Permalink
Adds documentation for refine, with_angle_limit and `exclude_oute…
Browse files Browse the repository at this point in the history
…r_faces`

Various bug fixes & improvements to the refinement
  • Loading branch information
Stoeoef committed Feb 13, 2022
1 parent b4a8c90 commit c49e41c
Show file tree
Hide file tree
Showing 15 changed files with 2,560 additions and 61 deletions.
19 changes: 18 additions & 1 deletion examples/svg_renderer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,24 @@ fn main() -> Result {
scenario_list::dual_edge_example().save_to_svg("dual_edges", "images/dual_edges.svg")?;
scenario_list::project_point_scenario()
.save_to_svg("project_point", "images/project_point.svg")?;
scenario_list::refinement_scenario().save_to_svg("refinement", "images/refinement.svg")?;
scenario_list::refinement_scenario(true).save_to_svg("refined", "images/refined.svg")?;
scenario_list::refinement_scenario(false).save_to_svg("unrefined", "images/unrefined.svg")?;
scenario_list::angle_limit_scenario(0.0)
.save_to_svg("angle_limit_0", "images/angle_limit_00.svg")?;
scenario_list::angle_limit_scenario(20.0)
.save_to_svg("angle_limit_20", "images/angle_limit_20.svg")?;
scenario_list::angle_limit_scenario(30.0)
.save_to_svg("angle_limit_30", "images/angle_limit_30.svg")?;
scenario_list::angle_limit_scenario(34.0)
.save_to_svg("angle_limit_34", "images/angle_limit_34.svg")?;

scenario_list::exclude_outer_faces_scenario(false).save_to_svg(
"exclude_unrefined",
"images/exclude_outer_faces_unrefined.svg",
)?;
scenario_list::exclude_outer_faces_scenario(true)
.save_to_svg("exclude_refined", "images/exclude_outer_faces_refined.svg")?;

Ok(())
}

Expand Down
95 changes: 82 additions & 13 deletions examples/svg_renderer/scenario_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use super::quicksketch::{
ArrowType, HorizontalAlignment, Point, Sketch, SketchColor, SketchElement, SketchFill,
StrokeStyle, Vector,
};

use cgmath::{Angle, Bounded, Deg, EuclideanSpace, InnerSpace, Point2, Vector2};
use spade::{
handles::{
FixedDirectedEdgeHandle,
VoronoiVertex::{self, Inner, Outer},
},
InsertionError, RefinementParameters, Triangulation as _,
AngleLimit, InsertionError, RefinementParameters, Triangulation as _,
};

use crate::{
Expand Down Expand Up @@ -901,7 +902,52 @@ pub fn project_point_scenario() -> Sketch {
sketch
}

pub fn refinement_scenario() -> Sketch {
pub fn refinement_scenario(do_refine: bool) -> Sketch {
let mut cdt = create_refinement_cdt();

let parameters = RefinementParameters::default();

if do_refine {
cdt.refine(parameters);
}

convert_refinement_cdt(&mut cdt)
}

pub fn exclude_outer_faces_scenario(do_refine: bool) -> Sketch {
let mut cdt = create_refinement_cdt();

let num_additional_vertices = if do_refine { 500 } else { 0 };

let parameters = RefinementParameters::<f64>::default()
.exclude_outer_faces(&cdt)
.with_max_additional_vertices(num_additional_vertices);

let result = cdt.refine(parameters);
for face in &result.excluded_faces {
cdt.face_data_mut(*face).fill = SketchFill::solid(SketchColor::TAN);
}

convert_refinement_cdt(&mut cdt)
}

fn convert_refinement_cdt(cdt: &mut Cdt) -> Sketch {
for vertex in cdt.fixed_vertices() {
cdt.vertex_data_mut(vertex).radius = 0.4;
}
for edge in cdt.fixed_undirected_edges() {
if cdt.is_constraint_edge(edge) {
cdt.undirected_edge_data_mut(edge).data_mut().color = SketchColor::DARK_RED;
} else {
cdt.undirected_edge_data_mut(edge).data_mut().color = SketchColor::DARK_GRAY;
}
}
let mut sketch = convert_triangulation(cdt, &ConversionOptions::default());
sketch.set_width(360);
sketch
}

fn create_refinement_cdt() -> Cdt {
let mut cdt = Cdt::new();
let v0 = VertexType::new(0.0, 0.0);
let v1 = VertexType::new(0.0, 100.0);
Expand Down Expand Up @@ -968,22 +1014,45 @@ pub fn refinement_scenario() -> Sketch {
for window in inner_vertices.windows(2) {
cdt.add_constraint_edge(window[0], window[1]).unwrap();
}
cdt
}

let parameters = RefinementParameters::default();
pub fn angle_limit_scenario(angle_limit_degrees: f64) -> Sketch {
let mut cdt = create_angle_limit_cdt();

cdt.refine(parameters);
cdt.refine(
RefinementParameters::default().with_angle_limit(AngleLimit::from_deg(angle_limit_degrees)),
);
let mut result = convert_refinement_cdt(&mut cdt);
result.set_width(200);
result
}

for vertex in cdt.fixed_vertices() {
cdt.vertex_data_mut(vertex).radius = 0.4;
}
fn create_angle_limit_cdt() -> Cdt {
let mut cdt = Cdt::new();

for edge in cdt.fixed_undirected_edges() {
if cdt.is_constraint_edge(edge) {
cdt.undirected_edge_data_mut(edge).data_mut().color = SketchColor::DARK_RED;
} else {
cdt.undirected_edge_data_mut(edge).data_mut().color = SketchColor::DARK_GRAY;
let num_slices = 22;

cdt.insert(VertexType::new(0.0, 0.0)).unwrap();
for index in 0..num_slices {
if index == 2 || index == 5 || index == 6 || index == 15 || index == 16 || index == 17 {
// Add some arbitrary irregularities to make the result look more interesting
continue;
}

let angle = std::f64::consts::PI * 0.9 * index as f64 / num_slices as f64;
let distance = 50.0;
let (sin, cos) = angle.sin_cos();
cdt.insert(VertexType::new(sin * distance, cos * distance))
.unwrap();
}

let mut handles = cdt.fixed_vertices().collect::<Vec<_>>();

handles.push(handles[0]);
for vertices in handles.windows(2) {
cdt.add_constraint(vertices[0], vertices[1]);
}

convert_triangulation(&cdt, &ConversionOptions::default())
cdt
}
74 changes: 74 additions & 0 deletions images/angle_limit_00.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c49e41c

Please sign in to comment.