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

Convert Color into a struct #862

Merged
merged 3 commits into from
Jul 22, 2022
Merged
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
10 changes: 9 additions & 1 deletion crates/fj-interop/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,12 @@ pub struct Triangle {
}

/// RGBA color
pub type Color = [u8; 4];
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Color(pub [u8; 4]);

impl Default for Color {
fn default() -> Self {
// The default color is red. This is an arbitrary choice.
Self([255, 0, 0, 255])
}
}
10 changes: 6 additions & 4 deletions crates/fj-kernel/src/algorithms/sweep.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use fj_interop::mesh::Color;
use fj_math::{Point, Scalar, Transform, Triangle, Vector};

use crate::{
Expand All @@ -16,7 +17,7 @@ pub fn sweep(
source: Sketch,
path: impl Into<Vector<3>>,
tolerance: Tolerance,
color: [u8; 4],
color: Color,
) -> Solid {
let path = path.into();

Expand Down Expand Up @@ -98,7 +99,7 @@ fn create_non_continuous_side_face(
path: Vector<3>,
is_sweep_along_negative_direction: bool,
vertices_bottom: [GlobalVertex; 2],
color: [u8; 4],
color: Color,
target: &mut Vec<Face>,
) {
let vertices = {
Expand Down Expand Up @@ -168,7 +169,7 @@ fn create_continuous_side_face(
edge: Edge,
path: Vector<3>,
tolerance: Tolerance,
color: [u8; 4],
color: Color,
target: &mut Vec<Face>,
) {
let translation = Transform::translation(path);
Expand Down Expand Up @@ -198,6 +199,7 @@ fn create_continuous_side_face(

#[cfg(test)]
mod tests {
use fj_interop::mesh::Color;
use fj_math::{Point, Scalar, Vector};

use crate::{
Expand Down Expand Up @@ -298,7 +300,7 @@ mod tests {
let sketch = Sketch::from_faces([face]);

let solid =
super::sweep(sketch, direction, tolerance, [255, 0, 0, 255]);
super::sweep(sketch, direction, tolerance, Color([255, 0, 0, 255]));

let expected_vertices: Vec<_> = expected_vertices
.into_iter()
Expand Down
7 changes: 4 additions & 3 deletions crates/fj-kernel/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Convenient API to build objects

use fj_interop::mesh::Color;
use fj_math::Point;

use crate::objects::{Cycle, Face, Surface};
Expand All @@ -10,7 +11,7 @@ pub struct FaceBuilder {
surface: Surface,
exterior: Option<Vec<Point<2>>>,
interiors: Vec<Vec<Point<2>>>,
color: Option<[u8; 4]>,
color: Option<Color>,
}

impl FaceBuilder {
Expand Down Expand Up @@ -51,7 +52,7 @@ impl FaceBuilder {
}

/// Define the color of the face
pub fn with_color(mut self, color: [u8; 4]) -> Self {
pub fn with_color(mut self, color: Color) -> Self {
self.color = Some(color);
self
}
Expand All @@ -72,7 +73,7 @@ impl FaceBuilder {
interiors.push(cycle);
}

let color = self.color.unwrap_or([255, 0, 0, 255]);
let color = self.color.unwrap_or_default();

Face::new(surface, exteriors, interiors, color)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/fj-kernel/src/objects/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Face {
surface: Surface,
exteriors: impl IntoIterator<Item = Cycle>,
interiors: impl IntoIterator<Item = Cycle>,
color: [u8; 4],
color: Color,
) -> Self {
let exteriors = exteriors.into_iter().collect();
let interiors = interiors.into_iter().collect();
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Face {
}

/// Access the color of the face
pub fn color(&self) -> [u8; 4] {
pub fn color(&self) -> Color {
self.brep().color
}

Expand Down Expand Up @@ -110,7 +110,7 @@ struct BRep {
surface: Surface,
exteriors: Vec<Cycle>,
interiors: Vec<Cycle>,
color: [u8; 4],
color: Color,
}

type TriRep = Vec<(Triangle<3>, Color)>;
9 changes: 7 additions & 2 deletions crates/fj-operations/src/difference_2d.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fj_interop::debug::DebugInfo;
use fj_interop::{debug::DebugInfo, mesh::Color};
use fj_kernel::{
algorithms::Tolerance,
iter::ObjectIters,
Expand Down Expand Up @@ -70,7 +70,12 @@ impl Shape for fj::Difference2d {
}
}

faces.push(Face::new(*surface, exteriors, interiors, self.color()));
faces.push(Face::new(
*surface,
exteriors,
interiors,
Color(self.color()),
));
}

let difference = Sketch::from_faces(faces);
Expand Down
6 changes: 3 additions & 3 deletions crates/fj-operations/src/sketch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fj_interop::debug::DebugInfo;
use fj_interop::{debug::DebugInfo, mesh::Color};
use fj_kernel::{
algorithms::Tolerance,
objects::{Cycle, Edge, Face, Sketch, Surface},
Expand Down Expand Up @@ -28,15 +28,15 @@ impl Shape for fj::Sketch {
Edge::circle_from_radius(Scalar::from_f64(circle.radius()));
let cycle = Cycle { edges: vec![edge] };

Face::new(surface, vec![cycle], Vec::new(), self.color())
Face::new(surface, vec![cycle], Vec::new(), Color(self.color()))
}
fj::Chain::PolyChain(poly_chain) => {
let points =
poly_chain.to_points().into_iter().map(Point::from);

Face::builder(surface)
.with_exterior_polygon(points)
.with_color(self.color())
.with_color(Color(self.color()))
.build()
}
};
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-operations/src/sweep.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fj_interop::debug::DebugInfo;
use fj_interop::{debug::DebugInfo, mesh::Color};
use fj_kernel::{
algorithms::{sweep, Tolerance},
objects::Solid,
Expand All @@ -22,7 +22,7 @@ impl Shape for fj::Sweep {
let path = Vector::from(self.path());
let color = self.shape().color();

let solid = sweep(sketch.into_inner(), path, tolerance, color);
let solid = sweep(sketch.into_inner(), path, tolerance, Color(color));
validate(solid, config)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/fj-viewer/src/graphics/vertices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl From<&Mesh<fj_math::Point<3>>> for Vertices {
.map(|(vertex, normal, color)| Vertex {
position: vertex.into(),
normal: normal.into(),
color: color.map(|v| f32::from(v) / 255.0),
color: color.0.map(|v| f32::from(v) / 255.0),
})
.collect();

Expand Down