Skip to content

Commit

Permalink
Merge pull request #17 from dzervas/sketch-helpers
Browse files Browse the repository at this point in the history
Add constraints getter
  • Loading branch information
TobiasJacob authored May 30, 2024
2 parents c84466b + 618996f commit 2b61c03
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
41 changes: 40 additions & 1 deletion src/decompose/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::primitives::circle::Circle;

use super::segment::Segment;

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialOrd, Serialize, Deserialize)]
pub enum Ring {
Circle(Circle),
Segments(Vec<Segment>),
Expand All @@ -29,6 +29,29 @@ impl Ring {
}
}

pub fn adjacent_edges(&self, other: &Self) -> Option<(Vec<usize>, Vec<usize>)> {
match (self, other) {
(Ring::Segments(segments_a), Ring::Segments(segments_b)) => {
let mut edge_indices_a: Vec<usize> = vec![];
let mut edge_indices_b: Vec<usize> = vec![];
for (index_a, segment_a) in segments_a.iter().enumerate() {
for (index_b, segment_b) in segments_b.iter().enumerate() {
if segment_a.reverse_equals(segment_b) {
edge_indices_a.push(index_a);
edge_indices_b.push(index_b);
}
}
}
if edge_indices_a.len() == 0 {
return None;
} else {
Some((edge_indices_a, edge_indices_b))
}
}
_ => None,
}
}

pub fn as_polygon(&self) -> Polygon {
match self {
Ring::Circle(circle) => {
Expand Down Expand Up @@ -64,3 +87,19 @@ impl Ring {
}
}
}

impl PartialEq for Ring {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Ring::Circle(circle_a), Ring::Circle(circle_b)) => circle_a == circle_b,
(Ring::Segments(segments_a), Ring::Segments(segments_b)) => {
segments_a.len() == segments_b.len()
&& segments_a
.iter()
.zip(segments_b.iter())
.all(|(a, b)| a == b)
}
_ => false,
}
}
}
8 changes: 6 additions & 2 deletions src/decompose/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ impl Segment {
}
}

pub fn equals_or_reverse_equals(&self, other: &Segment) -> bool {
self == other || self == &other.reverse()
pub fn reverse_equals(&self, other: &Self) -> bool {
self == &other.reverse()
}

pub fn equals_or_reverse_equals(&self, other: &Self) -> bool {
self == other || self.reverse_equals(other)
}
}
13 changes: 9 additions & 4 deletions src/sketch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ impl Sketch {
}

pub fn primitives(&self) -> BTreeMap<u64, PrimitiveCell> {
self.primitives
.iter()
.map(|(k, v)| (*k, v.clone()))
.collect()
self.primitives.clone()
}

pub fn constraints(&self) -> VecDeque<ConstraintCell> {
self.constraints.clone()
}

pub fn get_n_dofs(&self) -> usize {
Expand Down Expand Up @@ -251,6 +252,10 @@ impl Sketch {
.map(|(k, _)| *k)
}

pub fn get_primitive_by_id(&self, id: u64) -> Option<&PrimitiveCell> {
self.primitives.get(&id)
}

pub fn get_faces(&self) -> Vec<Face> {
decompose_sketch(self)
}
Expand Down

0 comments on commit 2b61c03

Please sign in to comment.