mirror of
https://github.com/stickynotememo/wolf_renderer.git
synced 2026-07-30 06:46:03 +10:00
Setup ear clipping interface
This commit is contained in:
+43
-11
@@ -21,41 +21,73 @@ pub struct Object {
|
||||
pub edges: Vec<(Point3D, Point3D)>,
|
||||
pub edge_indices: Vec<(usize, usize)>,
|
||||
|
||||
pub faces: Vec<(Point3D, Point3D, Point3D)>,
|
||||
pub face_indices: Vec<(usize, usize, usize)>,
|
||||
pub triangles: Vec<(Point3D, Point3D, Point3D)>,
|
||||
pub triangle_indices: Vec<(usize, usize, usize)>,
|
||||
|
||||
pub faces: Vec<Vec<Point3D>>,
|
||||
pub face_indices: Vec<Vec<usize>>,
|
||||
}
|
||||
|
||||
impl Object {
|
||||
/// Takes a vector of vertices and a vector of indices for each of the `edge_indices` and
|
||||
/// `face_indices` parameters. These indices index the `vertices` array and represent the
|
||||
/// `triangle_indices` parameters. These indices index the `vertices` array and represent the
|
||||
/// endpoints of the line segment (in the case of an edge) or the vertices of the triangle
|
||||
/// (in the case of a face)
|
||||
/// (in the case of a triangle)
|
||||
|
||||
// TODO: use [Yoke](https://crates.io/crates/yoke)
|
||||
pub fn new(vertices: Vec<Point3D>,
|
||||
edge_indices: Vec<(usize, usize)>,
|
||||
face_indices: Vec<(usize, usize, usize)>) -> Self {
|
||||
face_indices: Vec<Vec<usize>>) -> Self {
|
||||
|
||||
let mut ret = Self {
|
||||
vertices,
|
||||
|
||||
edges: vec![],
|
||||
edge_indices,
|
||||
|
||||
triangles: vec![],
|
||||
triangle_indices: vec![],
|
||||
|
||||
faces: vec![vec![]],
|
||||
face_indices
|
||||
};
|
||||
|
||||
let mut ret = Self {vertices, edges: vec![], edge_indices, faces: vec![], face_indices};
|
||||
for (i_start, i_end) in ret.edge_indices.iter() {
|
||||
ret.edges.push((ret.vertices[*i_start].clone(), ret.vertices[*i_end].clone()));
|
||||
}
|
||||
|
||||
for (i_one, i_two, i_three /* The three vertices of the triangle */) in ret.face_indices.iter() {
|
||||
ret.faces.push((ret.vertices[*i_one].clone(), ret.vertices[*i_two].clone(), ret.vertices[*i_three].clone()));
|
||||
for (i_one, i_two, i_three /* The three vertices of the triangle */) in ret.triangle_indices.iter() {
|
||||
ret.triangles.push((ret.vertices[*i_one].clone(), ret.vertices[*i_two].clone(), ret.vertices[*i_three].clone()));
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
// Updates the `edges` and `faces` to correspond to updated vertices.
|
||||
fn ear_clip(polygon: &Vec<Point3D>) -> Vec<(Point3D, Point3D, Point3D)> {
|
||||
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Updates the `edges` and `faces` to correspond to updated vertices.
|
||||
/// Triangulates based on the faces array.
|
||||
pub fn update(&mut self) {
|
||||
// OPTIMISATION: Do we need to re-initalise self.{edges, faces, triangles} every execution?
|
||||
self.edges = vec![];
|
||||
for (i_start, i_end) in self.edge_indices.iter() {
|
||||
self.edges.push((self.vertices[*i_start].clone(), self.vertices[*i_end].clone()));
|
||||
}
|
||||
|
||||
self.faces = vec![];
|
||||
for (i_one, i_two, i_three /* The three vertices of the triangle */) in self.face_indices.iter() {
|
||||
self.faces.push((self.vertices[*i_one].clone(), self.vertices[*i_two].clone(), self.vertices[*i_three].clone()));
|
||||
for polygon_indices in self.face_indices.iter() {
|
||||
self.faces.push(polygon_indices.iter().map(|indice| self.vertices[*indice]).collect());
|
||||
}
|
||||
|
||||
self.triangles = vec![];
|
||||
for face in self.faces.iter() {
|
||||
self.triangles.append(&mut Self::ear_clip(&face));
|
||||
}
|
||||
for (i_one, i_two, i_three /* The three vertices of the triangle */) in self.triangle_indices.iter() {
|
||||
self.triangles.push((self.vertices[*i_one].clone(), self.vertices[*i_two].clone(), self.vertices[*i_three].clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user