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

Make using models more convenient #832

Merged
merged 3 commits into from
Jul 18, 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
29 changes: 28 additions & 1 deletion crates/fj-host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::{
collections::{HashMap, HashSet},
ffi::OsStr,
io,
ops::{Deref, DerefMut},
path::{Path, PathBuf},
process::Command,
sync::mpsc,
Expand Down Expand Up @@ -279,14 +280,40 @@ impl Watcher {
}
}

/// Parameters that are passed to a model
/// Parameters that are passed to a model.
#[derive(Debug, Clone, PartialEq)]
pub struct Parameters(pub HashMap<String, String>);

impl Parameters {
/// Construct an empty instance of `Parameters`
pub fn empty() -> Self {
Self(HashMap::new())
}

/// Insert a value into the [`Parameters`] dictionary, implicitly converting
/// the arguments to strings and returning `&mut self` to enable chaining.
pub fn insert(
&mut self,
key: impl Into<String>,
value: impl ToString,
) -> &mut Self {
self.0.insert(key.into(), value.to_string());
self
}
}

impl Deref for Parameters {
type Target = HashMap<String, String>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Parameters {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

/// An error that can occur when loading or reloading a model
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::f64::consts::{PI, TAU};
const GON_RAD: f64 = PI / 200.;

/// An angle
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Angle {
// The value of the angle in radians
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::Shape;
/// # Limitations
///
/// Whether the shapes in the group touch or overlap is not currently checked.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Group {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use fj_proc::*;
use serde::{Deserialize, Serialize};

/// A shape
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub enum Shape {
Expand Down
21 changes: 16 additions & 5 deletions crates/fj/src/shape_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::atomic;
use crate::Shape;

/// A 2-dimensional shape
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub enum Shape2d {
Expand All @@ -28,7 +28,7 @@ impl Shape2d {
}

/// A difference between two shapes
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Difference2d {
Expand Down Expand Up @@ -73,7 +73,7 @@ impl From<Difference2d> for Shape2d {
/// Nothing about these edges is checked right now, but algorithms might assume
/// that the edges are non-overlapping. If you create a `Sketch` with
/// overlapping edges, you're on your own.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Sketch {
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Sketch {
}

/// A chain of elements that is part of a [`Sketch`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub enum Chain {
Expand All @@ -130,7 +130,7 @@ pub enum Chain {
}

/// A circle that is part of a [`Sketch`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Circle {
Expand Down Expand Up @@ -191,6 +191,11 @@ impl PolyChain {
}
}

/// Get a reference to the points in this [`PolyChain`].
fn points(&self) -> &[[f64; 2]] {
unsafe { std::slice::from_raw_parts(self.ptr, self.length) }
}

/// Return the points that define the polygonal chain
pub fn to_points(&self) -> Vec<[f64; 2]> {
// This is sound. All invariants are automatically kept, as the raw
Expand Down Expand Up @@ -229,6 +234,12 @@ impl Clone for PolyChain {
}
}

impl PartialEq for PolyChain {
fn eq(&self, other: &Self) -> bool {
self.points() == other.points()
}
}

impl Drop for PolyChain {
fn drop(&mut self) {
// Decrement the reference counter
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::{Shape, Shape2d};

/// A sweep of a 2-dimensional shape along straight path
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Sweep {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{Angle, Shape};
///
/// See issue:
/// <https://github.com/hannobraun/Fornjot/issues/101>
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Transform {
Expand Down