Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bevel function for contiguous suite of edges #13

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 44 additions & 23 deletions src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,43 +109,64 @@ impl Mesh
for i in 0..no_vertices {
mesh.create_vertex(vec3(positions[i*3], positions[i*3+1], positions[i*3+2]));
}

let mut twins = HashMap::<(VertexID,VertexID), HalfEdgeID>::new();
fn sort(a: VertexID, b: VertexID) -> (VertexID,VertexID) {
if a < b {(a,b)}
else {(b,a)}
}

// Create faces and twin connectivity
for face in 0..no_faces {
let v0 = indices[face * 3];
let v1 = indices[face * 3 + 1];
let v2 = indices[face * 3 + 2];

let face = mesh.connectivity_info.create_face(VertexID::new(v0), VertexID::new(v1), VertexID::new(v2));

// mark twin halfedges
let mut walker = mesh.walker_from_face(face);
mesh.connectivity_info.create_face(VertexID::new(v0), VertexID::new(v1), VertexID::new(v2));
}
mesh.twin_alones();

mesh
}


/// twin edges that share the same vertices
pub fn twin_alones(&mut self) {
// dictionnary for twining
let mut twins = HashMap::<(VertexID, VertexID), HalfEdgeID>::new();
fn sort(a: VertexID, b: VertexID) -> (VertexID,VertexID) {
if a < b {(a,b)}
else {(b,a)}
}

// get halfedges ids and begin twining
for face in self.face_iter() {
let mut walker = self.walker_from_face(face);
for _ in 0..3 {
let vertex_id = walker.vertex_id().unwrap();
walker.as_next();
let key = sort(vertex_id, walker.vertex_id().unwrap());
if let Some(twin) = twins.get(&key) {
mesh.connectivity_info.set_halfedge_twin(walker.halfedge_id().unwrap(), *twin);
}
else {
twins.insert(key, walker.halfedge_id().unwrap());
if walker.twin_id().is_none() || walker.clone().as_twin().face_id().is_none() {
let key = sort(vertex_id, walker.vertex_id().unwrap());
if let Some(&twin) = twins.get(&key) {
// if there was an empty halfedge already twined to the edge, remove it
let oldtwin = self.walker_from_halfedge(twin).into_twin();
if oldtwin.halfedge_id().is_some() && oldtwin.face_id().is_none() {
self.connectivity_info.remove_halfedge(oldtwin.halfedge_id().unwrap());
}
let myoldtwin = walker.clone().into_twin();
if myoldtwin.halfedge_id().is_some() && myoldtwin.face_id().is_none() {
self.connectivity_info.remove_halfedge(myoldtwin.halfedge_id().unwrap());
}
// twin halfedges
self.connectivity_info.set_halfedge_twin(walker.halfedge_id().unwrap(), twin);
}
else {
twins.insert(key, walker.halfedge_id().unwrap());
}
}
}
}
for halfedge in mesh.connectivity_info.halfedge_iterator() {
if mesh.connectivity_info.halfedge(halfedge).unwrap().twin.is_none() {
let vertex = mesh.walker_from_halfedge(halfedge).as_previous().vertex_id().unwrap();
mesh.connectivity_info.set_halfedge_twin(halfedge, mesh.connectivity_info.new_halfedge(Some(vertex), None, None));
}
// finish twining, and put empty twins to edges that are connected to only 1 face
for halfedge in self.halfedge_iter() {
if self.connectivity_info.halfedge(halfedge).unwrap().twin.is_none() {
let vertex = self.walker_from_halfedge(halfedge).as_previous().vertex_id().unwrap();
self.connectivity_info.set_halfedge_twin(halfedge, self.connectivity_info.new_halfedge(Some(vertex), None, None));
}
}

mesh
}

fn new_internal(connectivity_info: ConnectivityInfo) -> Mesh
Expand Down
9 changes: 9 additions & 0 deletions src/mesh/connectivity_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ impl ConnectivityInfo {
}
halfedges.remove(halfedge_id);
}

pub fn remove_halfedge_twin(&self, id: HalfEdgeID)
{
let mut halfedges = self.halfedges.borrow_mut();
if let Some(twin) = halfedges.get(id).unwrap().twin {
halfedges.get_mut(twin).unwrap().twin = None;
}
halfedges.get_mut(id).unwrap().twin = None;
}

pub fn remove_face(&self, face_id: FaceID)
{
Expand Down
11 changes: 10 additions & 1 deletion src/mesh/edge_measures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ impl Mesh
let (p0, p1) = self.edge_positions(halfedge_id);
(p0 - p1).magnitude2()
}
}

///
/// Normalized vector representing the direction in which the halfedge points
///
pub fn edge_direction(&self, edge: HalfEdgeID) -> Vec3
{
let (dst,src) = self.edge_vertices(edge);
(self.vertex_position(dst)-self.vertex_position(src)).normalize()
}
}
Loading