Skip to content

Commit

Permalink
Added stub for decompose sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasJacob committed May 26, 2024
1 parent 6c57453 commit 45b5b2b
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 109 deletions.
10 changes: 5 additions & 5 deletions src/decompose/face.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use super::ring::Ring;

#[derive(Clone, Debug, PartialEq, PartialOrd)]
#[derive(Debug)]
pub struct Face {
pub exterior: Ring,
pub holes: Vec<Ring>,
}

impl Face {
pub fn from_ring(ring: &Ring) -> Face {
pub fn from_ring(ring: Ring) -> Face {
Face {
exterior: ring.clone(),
exterior: ring,
holes: vec![],
}
}

pub fn add_hole(&mut self, hole: &Face) {
self.holes.push(hole.exterior.clone());
pub fn add_hole(&mut self, hole: Ring) {
self.holes.push(hole);
}
}
15 changes: 15 additions & 0 deletions src/decompose/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::sketch::Sketch;

use self::face::Face;

pub mod face;
pub mod ring;
pub mod segment;

pub fn decompose_sketch(sketch: &Sketch) -> Vec<Face> {
for primitive in sketch.primitives() {
let primitive = primitive.borrow().to_primitive();
}

todo!("Decompose the sketch into faces")
}
73 changes: 3 additions & 70 deletions src/decompose/ring.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,9 @@
use super::circle::Circle;
use crate::primitives::circle::Circle;

use super::segment::Segment;

#[derive(Clone, Debug, PartialEq, PartialOrd)]
#[derive(Debug)]
pub enum Ring {
Circle(Circle),
Segments(Vec<Segment>),
}

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 canonical_form(&self) -> Self {
// sort the segments in order by first finding the segment with the smallest start point
// and then rotating the list so that that segment is first
match self {
Ring::Circle(circle) => Ring::Circle(circle.clone()),
Ring::Segments(segments) => {
let mut canonical_segments: Vec<Segment> = vec![];
let mut min_index = 0;
let mut min_segment = segments.get(0).unwrap();
for (i, segment) in segments.iter().enumerate() {
if segment.get_start() < min_segment.get_start() {
min_index = i;
min_segment = segment;
}
}

for i in 0..segments.len() {
canonical_segments.push(
segments
.get((i + min_index) % segments.len())
.unwrap()
.clone(),
);
}

Ring::Segments(canonical_segments)
}
}
}

pub fn reverse(&self) -> Self {
match self {
Ring::Circle(circle) => Ring::Circle(circle.clone()),
Ring::Segments(segments) => {
let mut reversed_segments: Vec<Segment> = vec![];
for segment in segments.iter().rev() {
reversed_segments.push(segment.reverse());
}
Ring::Segments(reversed_segments)
}
}
}
}
36 changes: 18 additions & 18 deletions src/decompose/segment.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
use super::arc::Arc;
use super::line::Line;
use nalgebra::Vector2;

#[derive(Clone, Debug, PartialEq, PartialOrd)]
use crate::primitives::{arc::Arc, line::Line};

#[derive(Debug)]
pub enum Segment {
Line(Line),
Arc(Arc),
}

impl Segment {
pub fn reverse(&self) -> Self {
pub fn get_start(&self) -> Vector2<f64> {
match self {
Segment::Line(line) => Segment::Line(line.reverse()),
Segment::Arc(arc) => Segment::Arc(arc.reverse()),
Segment::Line(line) => line.start().borrow().data(),
Segment::Arc(arc) => arc.start_point(),
}
}

pub fn get_start(&self) -> u64 {
pub fn get_end(&self) -> Vector2<f64> {
match self {
Segment::Line(line) => line.start,
Segment::Arc(arc) => arc.start,
Segment::Line(line) => line.end().borrow().data(),
Segment::Arc(arc) => arc.end_point(),
}
}

pub fn get_end(&self) -> u64 {
pub fn reverse(&self) -> Self {
match self {
Segment::Line(line) => line.end,
Segment::Arc(arc) => arc.end,
Segment::Line(line) => {
Segment::Line(Line::new(line.end().clone(), line.start().clone()))
}
Segment::Arc(arc) => Segment::Arc(arc.reverse()),
}
}

Expand All @@ -34,11 +37,8 @@ impl Segment {
prior_segment.get_end() == self.get_start()
}

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

pub fn reverse_equals(&self, other: &Self) -> bool {
self == &other.reverse()
pub fn connects(&self, prior_segment: &Segment) -> bool {
// determines if this segment connects to the prior segment
prior_segment.get_end() == self.get_start() || prior_segment.get_end() == self.get_end()
}
}
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#![warn(clippy::panic)]

pub mod constraints;
pub mod decompose;
pub mod error;
#[cfg(test)]
pub mod examples;
pub mod intersections;
pub mod primitives;
pub mod sketch;
pub mod solvers;

#[cfg(test)]
pub mod examples;
16 changes: 13 additions & 3 deletions src/primitives/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nalgebra::{DVector, DVectorView, SMatrix, SMatrixView, SVector, Vector2};

use super::{point2::Point2, Parametric};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Arc {
center: Rc<RefCell<Point2>>,
data: SVector<f64, 3>,
Expand All @@ -30,6 +30,16 @@ impl Arc {
}
}

pub fn reverse(&self) -> Self {
Arc::new(
self.center.clone(),
self.radius(),
!self.clockwise,
self.end_angle(),
self.start_angle(),
)
}

pub fn center(&self) -> Rc<RefCell<Point2>> {
self.center.clone()
}
Expand Down Expand Up @@ -172,7 +182,7 @@ impl Parametric for Arc {
self.data = SVector::from_row_slice(data.as_slice());
}

fn as_primitive(self) -> super::Primitive {
super::Primitive::Arc(self)
fn to_primitive(&self) -> super::Primitive {
super::Primitive::Arc(self.clone())
}
}
5 changes: 3 additions & 2 deletions src/primitives/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use nalgebra::{DVector, DVectorView, SMatrix, SMatrixView, SVector};

use super::{point2::Point2, Parametric};

#[derive(Debug, Clone)]
pub struct Circle {
center: Rc<RefCell<Point2>>,
data: SVector<f64, 1>,
Expand Down Expand Up @@ -74,7 +75,7 @@ impl Parametric for Circle {
DVector::from_row_slice(self.gradient.as_slice())
}

fn as_primitive(self) -> super::Primitive {
super::Primitive::Circle(self)
fn to_primitive(&self) -> super::Primitive {
super::Primitive::Circle(self.clone())
}
}
6 changes: 3 additions & 3 deletions src/primitives/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nalgebra::{DVector, DVectorView, SMatrix, SMatrixView};

use super::{point2::Point2, Parametric};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Line {
start: Rc<RefCell<Point2>>,
end: Rc<RefCell<Point2>>,
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Parametric for Line {
DVector::from_row_slice(&[])
}

fn as_primitive(self) -> super::Primitive {
super::Primitive::Line(self)
fn to_primitive(&self) -> super::Primitive {
super::Primitive::Line(self.clone())
}
}
7 changes: 4 additions & 3 deletions src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ pub trait Parametric {
fn get_data(&self) -> DVector<f64>;
fn set_data(&mut self, data: DVectorView<f64>);
fn get_gradient(&self) -> DVector<f64>;
fn as_primitive(self) -> Primitive;
fn to_primitive(&self) -> Primitive;
}

#[derive(Debug, Clone)]
pub enum Primitive {
Point2(point2::Point2),
Line(line::Line),
Expand Down Expand Up @@ -70,7 +71,7 @@ impl Parametric for Primitive {
}
}

fn as_primitive(self) -> Primitive {
self
fn to_primitive(&self) -> Primitive {
self.clone()
}
}
6 changes: 3 additions & 3 deletions src/primitives/point2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use nalgebra::{DVector, DVectorView, SMatrix, SMatrixView, Vector2};

use super::Parametric;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Point2 {
data: Vector2<f64>,
gradient: Vector2<f64>,
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Parametric for Point2 {
DVector::from_row_slice(self.gradient.as_slice())
}

fn as_primitive(self) -> super::Primitive {
super::Primitive::Point2(self)
fn to_primitive(&self) -> super::Primitive {
super::Primitive::Point2(self.clone())
}
}
4 changes: 4 additions & 0 deletions src/sketch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ impl Sketch {
Ok(())
}

pub fn primitives(&self) -> VecDeque<Rc<RefCell<dyn Parametric>>> {
self.primitives.clone()
}

pub fn get_n_dofs(&self) -> usize {
let mut n_dofs = 0;
for primitive in self.primitives.iter() {
Expand Down

0 comments on commit 45b5b2b

Please sign in to comment.