Skip to content

Commit 0b8b216

Browse files
committed
promote Vertex and eliminate redundant types
1 parent 5b9f906 commit 0b8b216

30 files changed

+256
-270
lines changed

src/bmesh/triangulated.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use crate::bmesh::BMesh;
22
use crate::float_types::Real;
33
use nalgebra::{Point3, Vector3};
4-
use crate::triangulated::{TriVertex, Triangulated3D};
4+
use crate::triangulated::Triangulated3D;
5+
use crate::vertex::Vertex;
56

67
impl<S: Clone + Send + Sync + std::fmt::Debug> Triangulated3D for BMesh<S> {
78
fn visit_triangles<F>(&self, mut f: F)
89
where
9-
F: FnMut([TriVertex; 3]),
10+
F: FnMut([Vertex; 3]),
1011
{
1112
let Some(m) = &self.manifold else { return; };
1213

@@ -33,9 +34,9 @@ impl<S: Clone + Send + Sync + std::fmt::Debug> Triangulated3D for BMesh<S> {
3334
};
3435

3536
f([
36-
TriVertex { position: p[0], normal: n },
37-
TriVertex { position: p[1], normal: n },
38-
TriVertex { position: p[2], normal: n },
37+
Vertex { position: p[0], normal: n },
38+
Vertex { position: p[1], normal: n },
39+
Vertex { position: p[2], normal: n },
3940
]);
4041
}
4142
}

src/io/amf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<S: Clone + Debug + Send + Sync> Mesh<S> {
6161
let mut triangle_indices = Vec::new();
6262

6363
for vertex in triangle {
64-
let vertex_idx = add_unique_vertex_amf(&mut vertices, vertex.pos);
64+
let vertex_idx = add_unique_vertex_amf(&mut vertices, vertex.position);
6565
triangle_indices.push(vertex_idx);
6666
}
6767

@@ -197,7 +197,7 @@ impl<S: Clone + Debug + Send + Sync> Mesh<S> {
197197
let mut triangle_indices = Vec::new();
198198

199199
for vertex in triangle {
200-
let vertex_idx = add_unique_vertex_amf(&mut vertices, vertex.pos);
200+
let vertex_idx = add_unique_vertex_amf(&mut vertices, vertex.position);
201201
triangle_indices.push(vertex_idx);
202202
}
203203

src/io/dxf.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::float_types::Real;
22
use crate::mesh::Mesh;
33
use crate::mesh::polygon::Polygon;
4-
use crate::mesh::vertex::Vertex;
4+
use crate::vertex::Vertex;
55
use crate::sketch::Sketch;
66
use geo::{Polygon as GeoPolygon, line_string};
77
use nalgebra::{Point3, Vector3};
@@ -151,24 +151,24 @@ impl<S: Clone + Debug + Send + Sync> Mesh<S> {
151151
let face = dxf::entities::Face3D::new(
152152
// 3DFACE expects four vertices, but for triangles, the fourth is the same as the third
153153
dxf::Point::new(
154-
tri[0].pos.x as f64,
155-
tri[0].pos.y as f64,
156-
tri[0].pos.z as f64,
154+
tri[0].position.x as f64,
155+
tri[0].position.y as f64,
156+
tri[0].position.z as f64,
157157
),
158158
dxf::Point::new(
159-
tri[1].pos.x as f64,
160-
tri[1].pos.y as f64,
161-
tri[1].pos.z as f64,
159+
tri[1].position.x as f64,
160+
tri[1].position.y as f64,
161+
tri[1].position.z as f64,
162162
),
163163
dxf::Point::new(
164-
tri[2].pos.x as f64,
165-
tri[2].pos.y as f64,
166-
tri[2].pos.z as f64,
164+
tri[2].position.x as f64,
165+
tri[2].position.y as f64,
166+
tri[2].position.z as f64,
167167
),
168168
dxf::Point::new(
169-
tri[2].pos.x as f64,
170-
tri[2].pos.y as f64,
171-
tri[2].pos.z as f64,
169+
tri[2].position.x as f64,
170+
tri[2].position.y as f64,
171+
tri[2].position.z as f64,
172172
), // Duplicate for triangular face
173173
);
174174

src/io/gltf.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,17 @@
55

66
use crate::float_types::{tolerance, Real};
77
use crate::triangulated::Triangulated3D;
8+
use crate::vertex::Vertex;
89
use nalgebra::{Point3, Vector3};
910
use std::fmt::Debug;
1011
use std::io::Write;
1112
use base64::engine::general_purpose::STANDARD as BASE64_ENGINE;
1213
use base64::Engine;
1314

14-
#[derive(Clone)]
15-
struct GltfVertex {
16-
position: Point3<Real>,
17-
normal: Vector3<Real>,
18-
}
19-
2015
/// Add a vertex to the list, reusing an existing one if position and normal
2116
/// are within `tolerance()`.
2217
fn add_unique_vertex_gltf(
23-
vertices: &mut Vec<GltfVertex>,
18+
vertices: &mut Vec<Vertex>,
2419
position: Point3<Real>,
2520
normal: Vector3<Real>,
2621
) -> u32 {
@@ -31,14 +26,14 @@ fn add_unique_vertex_gltf(
3126
return i as u32;
3227
}
3328
}
34-
vertices.push(GltfVertex { position, normal });
29+
vertices.push(Vertex { position, normal });
3530
(vertices.len() - 1) as u32
3631
}
3732

3833
fn build_gltf_buffers<T: Triangulated3D>(
3934
shape: &T,
40-
) -> (Vec<GltfVertex>, Vec<u32>) {
41-
let mut vertices = Vec::<GltfVertex>::new();
35+
) -> (Vec<Vertex>, Vec<u32>) {
36+
let mut vertices = Vec::<Vertex>::new();
4237
let mut indices = Vec::<u32>::new();
4338

4439
shape.visit_triangles(|tri| {
@@ -60,7 +55,7 @@ fn build_gltf_buffers<T: Triangulated3D>(
6055
///
6156
/// All binary data is stored in a single buffer as a base64-embedded data URI.
6257
fn gltf_from_vertices(
63-
vertices: &[GltfVertex],
58+
vertices: &[Vertex],
6459
indices: &[u32],
6560
object_name: &str,
6661
) -> String {

src/io/obj.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use crate::float_types::{Real, tolerance};
66
use crate::mesh::Mesh;
77
use crate::mesh::polygon::Polygon;
8-
use crate::mesh::vertex::Vertex;
8+
use crate::vertex::Vertex;
99
use crate::sketch::Sketch;
1010
use geo::CoordsIter;
1111
use nalgebra::{Point3, Vector3};
@@ -55,7 +55,7 @@ impl<S: Clone + Debug + Send + Sync> Mesh<S> {
5555
let normal_idx = add_unique_normal(&mut normals, normal);
5656

5757
for vertex in triangle {
58-
let vertex_idx = add_unique_vertex(&mut vertices, vertex.pos);
58+
let vertex_idx = add_unique_vertex(&mut vertices, vertex.position);
5959
face_indices.push((vertex_idx, normal_idx));
6060
}
6161

src/io/ply.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@
44
//! a popular format for 3D scanning, research, and mesh processing applications.
55
use crate::float_types::{Real, tolerance};
66
use crate::mesh::Mesh;
7+
use crate::vertex::Vertex;
78
use crate::sketch::Sketch;
89
use geo::CoordsIter;
910
use nalgebra::{Point3, Vector3};
1011
use std::fmt::Debug;
1112
use std::io::Write;
1213

13-
// Helper struct for PLY vertex with normal
14-
#[derive(Clone)]
15-
struct PlyVertex {
16-
position: Point3<Real>,
17-
normal: Vector3<Real>,
18-
}
19-
2014
impl<S: Clone + Debug + Send + Sync> Mesh<S> {
2115
/// Export this Mesh to PLY format as a string
2216
///
@@ -50,7 +44,7 @@ impl<S: Clone + Debug + Send + Sync> Mesh<S> {
5044

5145
for vertex in triangle {
5246
let vertex_idx =
53-
add_unique_vertex_ply(&mut vertices, vertex.pos, vertex.normal);
47+
add_unique_vertex_ply(&mut vertices, vertex.position, vertex.normal);
5448
face_indices.push(vertex_idx);
5549
}
5650

@@ -221,7 +215,7 @@ impl<S: Clone + Debug + Send + Sync> Sketch<S> {
221215
fn add_2d_polygon_to_ply(
222216
&self,
223217
poly2d: &geo::Polygon<Real>,
224-
vertices: &mut Vec<PlyVertex>,
218+
vertices: &mut Vec<Vertex>,
225219
faces: &mut Vec<Vec<usize>>,
226220
) {
227221
// Get the exterior ring
@@ -261,7 +255,7 @@ impl<S: Clone + Debug + Send + Sync> Sketch<S> {
261255

262256
// Helper function to add unique vertex with normal for PLY
263257
fn add_unique_vertex_ply(
264-
vertices: &mut Vec<PlyVertex>,
258+
vertices: &mut Vec<Vertex>,
265259
position: Point3<Real>,
266260
normal: Vector3<Real>,
267261
) -> usize {
@@ -275,6 +269,6 @@ fn add_unique_vertex_ply(
275269
}
276270

277271
// Add new vertex
278-
vertices.push(PlyVertex { position, normal });
272+
vertices.push(Vertex { position, normal });
279273
vertices.len() - 1
280274
}

src/io/svg.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<F: CoordNum> PathBuilder<F> {
4646
}
4747

4848
/// Get the current position to be used for relative moves.
49-
fn get_pos(&self) -> Coord<F> {
49+
fn get_position(&self) -> Coord<F> {
5050
self.inner
5151
.0
5252
.last()
@@ -69,7 +69,7 @@ impl<F: CoordNum> PathBuilder<F> {
6969
.unwrap_or(false);
7070

7171
if start_new_path {
72-
self.inner.0.push(LineString::new(vec![self.get_pos()]));
72+
self.inner.0.push(LineString::new(vec![self.get_position()]));
7373
}
7474

7575
self.inner.0.last_mut().ok_or_else(|| {
@@ -87,8 +87,8 @@ impl<F: CoordNum> PathBuilder<F> {
8787
/// Start a new path at `delta` relative to the last point.
8888
/// If and only if this is the first command, the point is treated as absolute coordinates.
8989
pub fn move_by(&mut self, delta: Coord<F>) {
90-
let pos = self.get_pos();
91-
self.inner.0.push(LineString::new(vec![pos + delta]));
90+
let position = self.get_position();
91+
self.inner.0.push(LineString::new(vec![position + delta]));
9292
}
9393

9494
/// Extend the current path to the `point`.
@@ -102,16 +102,16 @@ impl<F: CoordNum> PathBuilder<F> {
102102
/// Extend the current path by `delta` relative to the current point.
103103
/// Can not be the first command.
104104
pub fn line_by(&mut self, delta: Coord<F>) -> Result<(), IoError> {
105-
let pos = self.get_pos();
105+
let position = self.get_position();
106106
let line = self.get_path_mut_or_fail()?;
107-
line.0.push(pos + delta);
107+
line.0.push(position + delta);
108108
Ok(())
109109
}
110110

111111
/// Extend the current path with a horizontal move to `x`.
112112
/// Can not be the first command.
113113
pub fn hline_to(&mut self, x: F) -> Result<(), IoError> {
114-
let Coord { y, .. } = self.get_pos();
114+
let Coord { y, .. } = self.get_position();
115115
let line = self.get_path_mut_or_fail()?;
116116
line.0.push(Coord { x, y });
117117
Ok(())
@@ -120,7 +120,7 @@ impl<F: CoordNum> PathBuilder<F> {
120120
/// Extend the current path with a horizontal move by `dx` relative to the current point.
121121
/// Can not be the first command.
122122
pub fn hline_by(&mut self, dx: F) -> Result<(), IoError> {
123-
let Coord { x, y } = self.get_pos();
123+
let Coord { x, y } = self.get_position();
124124
let line = self.get_path_mut_or_fail()?;
125125
line.0.push(Coord { x: x + dx, y });
126126
Ok(())
@@ -129,7 +129,7 @@ impl<F: CoordNum> PathBuilder<F> {
129129
/// Extend the current path with a vertical move to `y`.
130130
/// Can not be the first command.
131131
pub fn vline_to(&mut self, y: F) -> Result<(), IoError> {
132-
let Coord { x, .. } = self.get_pos();
132+
let Coord { x, .. } = self.get_position();
133133
let line = self.get_path_mut_or_fail()?;
134134
line.0.push(Coord { x, y });
135135
Ok(())
@@ -138,7 +138,7 @@ impl<F: CoordNum> PathBuilder<F> {
138138
/// Extend the current path with a vertical move by `dy` relative to the current point.
139139
/// Can not be the first command.
140140
pub fn vline_by(&mut self, dy: F) -> Result<(), IoError> {
141-
let Coord { x, y } = self.get_pos();
141+
let Coord { x, y } = self.get_position();
142142
let line = self.get_path_mut_or_fail()?;
143143
line.0.push(Coord { x, y: y + dy });
144144
Ok(())

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
pub mod errors;
3232
pub mod float_types;
3333
pub mod io;
34+
pub mod vertex;
3435
pub mod mesh;
3536
pub mod nurbs;
3637
pub mod sketch;

src/mesh/bsp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::float_types::tolerance;
55

66
#[cfg(not(feature = "parallel"))]
7-
use crate::mesh::vertex::Vertex;
7+
use crate::vertex::Vertex;
88

99
use crate::float_types::Real;
1010
use crate::mesh::plane::{BACK, COPLANAR, FRONT, Plane, SPANNING};
@@ -254,7 +254,7 @@ impl<S: Clone + Send + Sync + Debug> Node<S> {
254254
let types: Vec<_> = poly
255255
.vertices
256256
.iter()
257-
.map(|vertex| slicing_plane.orient_point(&vertex.pos))
257+
.map(|vertex| slicing_plane.orient_point(&vertex.position))
258258
.collect();
259259

260260
let polygon_type = types.iter().fold(0, |acc, &vertex_type| acc | vertex_type);
@@ -282,10 +282,10 @@ impl<S: Clone + Send + Sync + Debug> Node<S> {
282282
let vj = &poly.vertices[j];
283283

284284
if (ti | tj) == SPANNING {
285-
let denom = slicing_plane.normal().dot(&(vj.pos - vi.pos));
285+
let denom = slicing_plane.normal().dot(&(vj.position - vi.position));
286286
if denom.abs() > tolerance() {
287287
let intersection = (slicing_plane.offset()
288-
- slicing_plane.normal().dot(&vi.pos.coords))
288+
- slicing_plane.normal().dot(&vi.position.coords))
289289
/ denom;
290290
Some(vi.interpolate(vj, intersection))
291291
} else {

src/mesh/bsp_parallel.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<S: Clone + Send + Sync + Debug> Node<S> {
184184
let mut types = Vec::with_capacity(vcount);
185185

186186
for vertex in &poly.vertices {
187-
let vertex_type = slicing_plane.orient_point(&vertex.pos);
187+
let vertex_type = slicing_plane.orient_point(&vertex.position);
188188
polygon_type |= vertex_type;
189189
types.push(vertex_type);
190190
}
@@ -211,10 +211,10 @@ impl<S: Clone + Send + Sync + Debug> Node<S> {
211211
if (ti | tj) == SPANNING {
212212
// The param intersection at which plane intersects the edge [vi -> vj].
213213
// Avoid dividing by zero:
214-
let denom = slicing_plane.normal().dot(&(vj.pos - vi.pos));
214+
let denom = slicing_plane.normal().dot(&(vj.position - vi.position));
215215
if denom.abs() > tolerance() {
216216
let intersection = (slicing_plane.offset()
217-
- slicing_plane.normal().dot(&vi.pos.coords))
217+
- slicing_plane.normal().dot(&vi.position.coords))
218218
/ denom;
219219
// Interpolate:
220220
let intersect_vert = vi.interpolate(vj, intersection);

0 commit comments

Comments
 (0)