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

Improve error validation infrastructure of Shape API #330

Merged
merged 10 commits into from
Mar 10, 2022
76 changes: 43 additions & 33 deletions src/kernel/algorithms/approximation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ mod tests {
let c = Point::from([3., 5., 8.]);
let d = Point::from([5., 8., 13.]);

let v1 = shape.geometry().add_point(a);
let v2 = shape.geometry().add_point(d);
let v1 = shape.geometry().add_point(a).unwrap();
let v2 = shape.geometry().add_point(d).unwrap();

let v1 = shape.vertices().add(Vertex { point: v1 });
let v2 = shape.vertices().add(Vertex { point: v2 });
let v1 = shape.vertices().add(Vertex { point: v1 }).unwrap();
let v2 = shape.vertices().add(Vertex { point: v2 }).unwrap();

let points = vec![b, c];

Expand Down Expand Up @@ -201,17 +201,20 @@ mod tests {
let b = Point::from([2., 3., 5.]);
let c = Point::from([3., 5., 8.]);

let v1 = shape.geometry().add_point(a);
let v2 = shape.geometry().add_point(b);
let v3 = shape.geometry().add_point(c);
let v1 = shape.geometry().add_point(a).unwrap();
let v2 = shape.geometry().add_point(b).unwrap();
let v3 = shape.geometry().add_point(c).unwrap();

let v1 = shape.vertices().add(Vertex { point: v1 });
let v2 = shape.vertices().add(Vertex { point: v2 });
let v3 = shape.vertices().add(Vertex { point: v3 });
let v1 = shape.vertices().add(Vertex { point: v1 }).unwrap();
let v2 = shape.vertices().add(Vertex { point: v2 }).unwrap();
let v3 = shape.vertices().add(Vertex { point: v3 }).unwrap();

let ab = shape.edges().add_line_segment([v1.clone(), v2.clone()]);
let bc = shape.edges().add_line_segment([v2, v3.clone()]);
let ca = shape.edges().add_line_segment([v3, v1]);
let ab = shape
.edges()
.add_line_segment([v1.clone(), v2.clone()])
.unwrap();
let bc = shape.edges().add_line_segment([v2, v3.clone()]).unwrap();
let ca = shape.edges().add_line_segment([v3, v1]).unwrap();

let cycle = Cycle {
edges: vec![ab, bc, ca],
Expand Down Expand Up @@ -243,26 +246,33 @@ mod tests {
let c = Point::from([3., 5., 8.]);
let d = Point::from([5., 8., 13.]);

let v1 = shape.geometry().add_point(a);
let v2 = shape.geometry().add_point(b);
let v3 = shape.geometry().add_point(c);
let v4 = shape.geometry().add_point(d);

let v1 = shape.vertices().add(Vertex { point: v1 });
let v2 = shape.vertices().add(Vertex { point: v2 });
let v3 = shape.vertices().add(Vertex { point: v3 });
let v4 = shape.vertices().add(Vertex { point: v4 });

let ab = shape.edges().add_line_segment([v1.clone(), v2.clone()]);
let bc = shape.edges().add_line_segment([v2, v3.clone()]);
let cd = shape.edges().add_line_segment([v3, v4.clone()]);
let da = shape.edges().add_line_segment([v4, v1]);

let abcd = shape.cycles().add(Cycle {
edges: vec![ab, bc, cd, da],
});

let surface = shape.geometry().add_surface(Surface::x_y_plane());
let v1 = shape.geometry().add_point(a).unwrap();
let v2 = shape.geometry().add_point(b).unwrap();
let v3 = shape.geometry().add_point(c).unwrap();
let v4 = shape.geometry().add_point(d).unwrap();

let v1 = shape.vertices().add(Vertex { point: v1 }).unwrap();
let v2 = shape.vertices().add(Vertex { point: v2 }).unwrap();
let v3 = shape.vertices().add(Vertex { point: v3 }).unwrap();
let v4 = shape.vertices().add(Vertex { point: v4 }).unwrap();

let ab = shape
.edges()
.add_line_segment([v1.clone(), v2.clone()])
.unwrap();
let bc = shape.edges().add_line_segment([v2, v3.clone()]).unwrap();
let cd = shape.edges().add_line_segment([v3, v4.clone()]).unwrap();
let da = shape.edges().add_line_segment([v4, v1]).unwrap();

let abcd = shape
.cycles()
.add(Cycle {
edges: vec![ab, bc, cd, da],
})
.unwrap();

let surface =
shape.geometry().add_surface(Surface::x_y_plane()).unwrap();
let face = Face::Face {
surface,
cycles: vec![abcd],
Expand Down
76 changes: 47 additions & 29 deletions src/kernel/algorithms/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ pub fn sweep_shape(
// Create the new vertices.
let mut vertices = HashMap::new();
for vertex_orig in shape_orig.vertices().all() {
let point = shape.geometry().add_point(vertex_orig.point() + path);
let vertex = shape.vertices().add(Vertex { point });
let point = shape
.geometry()
.add_point(vertex_orig.point() + path)
.unwrap();
let vertex = shape.vertices().add(Vertex { point }).unwrap();
vertices.insert(vertex_orig, vertex);
}

Expand All @@ -39,7 +42,8 @@ pub fn sweep_shape(
for edge_orig in shape_orig.edges().all() {
let curve = shape
.geometry()
.add_curve(edge_orig.curve().transform(&translation));
.add_curve(edge_orig.curve().transform(&translation))
.unwrap();

let vertices = edge_orig.vertices.clone().map(|vs| {
vs.map(|vertex_orig| {
Expand All @@ -49,7 +53,7 @@ pub fn sweep_shape(
})
});

let edge = shape.edges().add(Edge { curve, vertices });
let edge = shape.edges().add(Edge { curve, vertices }).unwrap();
edges.insert(edge_orig, edge);
}

Expand All @@ -66,7 +70,7 @@ pub fn sweep_shape(
})
.collect();

let cycle = shape.cycles().add(Cycle { edges });
let cycle = shape.cycles().add(Cycle { edges }).unwrap();
cycles.insert(cycle_orig, cycle);
}

Expand All @@ -83,7 +87,8 @@ pub fn sweep_shape(

let surface = shape
.geometry()
.add_surface(face_orig.surface().transform(&translation));
.add_surface(face_orig.surface().transform(&translation))
.unwrap();

let cycles = cycles_orig
.iter()
Expand All @@ -94,7 +99,7 @@ pub fn sweep_shape(
})
.collect();

shape.faces().add(Face::Face { surface, cycles });
shape.faces().add(Face::Face { surface, cycles }).unwrap();
}

// We could use `vertices` to create the side edges and faces here, but the
Expand Down Expand Up @@ -129,7 +134,7 @@ pub fn sweep_shape(
}

for face in side_faces {
shape.faces().add(face);
shape.faces().add(face).unwrap();
}

shape
Expand Down Expand Up @@ -195,33 +200,46 @@ mod tests {
fn new([a, b, c]: [impl Into<Point<3>>; 3]) -> Self {
let mut shape = Shape::new();

let a = shape.geometry().add_point(a.into());
let b = shape.geometry().add_point(b.into());
let c = shape.geometry().add_point(c.into());

let a = shape.vertices().add(Vertex { point: a });
let b = shape.vertices().add(Vertex { point: b });
let c = shape.vertices().add(Vertex { point: c });

let ab = shape.edges().add_line_segment([a.clone(), b.clone()]);
let bc = shape.edges().add_line_segment([b.clone(), c.clone()]);
let ca = shape.edges().add_line_segment([c.clone(), a.clone()]);

let cycles = shape.cycles().add(Cycle {
edges: vec![ab, bc, ca],
});

let surface = shape.geometry().add_surface(Surface::Swept(
Swept::plane_from_points(
let a = shape.geometry().add_point(a.into()).unwrap();
let b = shape.geometry().add_point(b.into()).unwrap();
let c = shape.geometry().add_point(c.into()).unwrap();

let a = shape.vertices().add(Vertex { point: a }).unwrap();
let b = shape.vertices().add(Vertex { point: b }).unwrap();
let c = shape.vertices().add(Vertex { point: c }).unwrap();

let ab = shape
.edges()
.add_line_segment([a.clone(), b.clone()])
.unwrap();
let bc = shape
.edges()
.add_line_segment([b.clone(), c.clone()])
.unwrap();
let ca = shape
.edges()
.add_line_segment([c.clone(), a.clone()])
.unwrap();

let cycles = shape
.cycles()
.add(Cycle {
edges: vec![ab, bc, ca],
})
.unwrap();

let surface = shape
.geometry()
.add_surface(Surface::Swept(Swept::plane_from_points(
[a, b, c].map(|vertex| vertex.point()),
),
));
)))
.unwrap();
let abc = Face::Face {
surface,
cycles: vec![cycles],
};

let face = shape.faces().add(abc);
let face = shape.faces().add(abc).unwrap();

Self { shape, face }
}
Expand Down
28 changes: 18 additions & 10 deletions src/kernel/algorithms/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,43 @@ pub fn transform_shape(mut original: Shape, transform: &Transform) -> Shape {
for edge in &cycle.edges {
let curve = transformed
.geometry()
.add_curve(edge.curve().transform(transform));
.add_curve(edge.curve().transform(transform))
.unwrap();

let vertices =
edge.vertices().clone().map(|vertices| {
vertices.map(|vertex| {
let point =
transformed.geometry().add_point(
let point = transformed
.geometry()
.add_point(
transform.transform_point(
&vertex.point(),
),
);
)
.unwrap();

transformed.vertices().add(Vertex { point })
transformed
.vertices()
.add(Vertex { point })
.unwrap()
})
});

let edge = Edge { curve, vertices };
let edge = transformed.edges().add(edge);
let edge = transformed.edges().add(edge).unwrap();

edges.push(edge);
}

cycles_trans
.push(transformed.cycles().add(Cycle { edges }));
cycles_trans.push(
transformed.cycles().add(Cycle { edges }).unwrap(),
);
}

let surface = transformed
.geometry()
.add_surface(surface.transform(transform));
.add_surface(surface.transform(transform))
.unwrap();

Face::Face {
cycles: cycles_trans,
Expand All @@ -78,7 +86,7 @@ pub fn transform_shape(mut original: Shape, transform: &Transform) -> Shape {
}
};

transformed.faces().add(face);
transformed.faces().add(face).unwrap();
}

transformed
Expand Down
Loading