Skip to content

Commit

Permalink
Fix bug where remove() was <3 points and invalid DT
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
hugoledoux committed Jul 16, 2024
1 parent 1d87f35 commit b517bd5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ pub enum StartinError {
VertexUnknown,
TinHasNoAttributes,
WrongAttribute,
TriangulationAlreadyInitialised,
}

/// Possibilities for the insertion (with `insert()`)
Expand Down Expand Up @@ -434,7 +433,6 @@ impl Triangulation {
self.update_dt(j);
}
}

match &mut self.attributes {
Some(x) => x.push(json!({})),
_ => (),
Expand Down Expand Up @@ -1463,6 +1461,18 @@ impl Triangulation {
//-- flip31 to remove the vertex
if adjs.len() == 3 {
self.flip31(v);
if self.number_of_vertices() < 3 {
//-- going back to a line, no triangles
//-- wipe it all and start the insert_init_phase again
for i in 0..self.stars.len() {
self.stars[i].link.clear();
}
self.stars[v].pt[0] = f64::NAN;
self.stars[v].pt[1] = f64::NAN;
self.stars[v].pt[2] = f64::NAN;
// self.removed_indices.push(v);
self.is_init = false;
}
Ok(self.stars.len() - 1)
} else {
//-- convex part is filled, and we need to apply a special "flip"
Expand Down Expand Up @@ -1498,7 +1508,6 @@ impl Triangulation {
self.stars[v].pt[1] = f64::NAN;
self.stars[v].pt[2] = f64::NAN;
self.removed_indices.push(v);

for i in 0..1000 {
if adjs[i] != 0 {
self.cur = adjs[i];
Expand Down
18 changes: 18 additions & 0 deletions tests/init_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ fn duplicates() {
assert_eq!(0, dt.number_of_triangles());
}

#[test]
fn remove_and_invalid() {
let mut dt = startin::Triangulation::new();
let _ = dt.insert_one_pt(1.5, 1.3, 4.5);
let _ = dt.insert_one_pt(2.2, 2.3, 4.5);
let _ = dt.insert_one_pt(3.2, 7.3, 4.5);
assert_eq!(3, dt.number_of_vertices());
assert_eq!(1, dt.number_of_triangles());

let _ = dt.remove(2);
assert_eq!(2, dt.number_of_vertices());
assert_eq!(0, dt.number_of_triangles());

let _ = dt.insert_one_pt(4.2, 17.3, 4.5);
assert_eq!(3, dt.number_of_vertices());
assert_eq!(1, dt.number_of_triangles());
}

#[test]
fn grid() {
let mut dt = startin::Triangulation::new();
Expand Down

0 comments on commit b517bd5

Please sign in to comment.