Merge pull request #832 from Michael-F-Bryan/conveniences

Make using models more convenient
This commit is contained in:
Hanno Braun 2022-07-18 18:23:15 +02:00 committed by GitHub
commit 58bc1b2464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 11 deletions

View File

@ -21,6 +21,7 @@ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
ffi::OsStr, ffi::OsStr,
io, io,
ops::{Deref, DerefMut},
path::{Path, PathBuf}, path::{Path, PathBuf},
process::Command, process::Command,
sync::mpsc, sync::mpsc,
@ -279,7 +280,8 @@ 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>); pub struct Parameters(pub HashMap<String, String>);
impl Parameters { impl Parameters {
@ -287,6 +289,31 @@ impl Parameters {
pub fn empty() -> Self { pub fn empty() -> Self {
Self(HashMap::new()) 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 /// An error that can occur when loading or reloading a model

View File

@ -6,7 +6,7 @@ use std::f64::consts::{PI, TAU};
const GON_RAD: f64 = PI / 200.; const GON_RAD: f64 = PI / 200.;
/// An angle /// An angle
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Angle { pub struct Angle {
// The value of the angle in radians // The value of the angle in radians

View File

@ -11,7 +11,7 @@ use crate::Shape;
/// # Limitations /// # Limitations
/// ///
/// Whether the shapes in the group touch or overlap is not currently checked. /// 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))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)] #[repr(C)]
pub struct Group { pub struct Group {

View File

@ -34,7 +34,7 @@ pub use fj_proc::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// A shape /// A shape
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)] #[repr(C)]
pub enum Shape { pub enum Shape {

View File

@ -6,7 +6,7 @@ use std::sync::atomic;
use crate::Shape; use crate::Shape;
/// A 2-dimensional shape /// A 2-dimensional shape
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)] #[repr(C)]
pub enum Shape2d { pub enum Shape2d {
@ -28,7 +28,7 @@ impl Shape2d {
} }
/// A difference between two shapes /// A difference between two shapes
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)] #[repr(C)]
pub struct Difference2d { pub struct Difference2d {
@ -73,7 +73,7 @@ impl From<Difference2d> for Shape2d {
/// Nothing about these edges is checked right now, but algorithms might assume /// Nothing about these edges is checked right now, but algorithms might assume
/// that the edges are non-overlapping. If you create a `Sketch` with /// that the edges are non-overlapping. If you create a `Sketch` with
/// overlapping edges, you're on your own. /// overlapping edges, you're on your own.
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)] #[repr(C)]
pub struct Sketch { pub struct Sketch {
@ -118,7 +118,7 @@ impl Sketch {
} }
/// A chain of elements that is part of a [`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))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)] #[repr(C)]
pub enum Chain { pub enum Chain {
@ -130,7 +130,7 @@ pub enum Chain {
} }
/// A circle that is part of a [`Sketch`] /// A circle that is part of a [`Sketch`]
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)] #[repr(C)]
pub struct Circle { pub struct Circle {
@ -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 /// Return the points that define the polygonal chain
pub fn to_points(&self) -> Vec<[f64; 2]> { pub fn to_points(&self) -> Vec<[f64; 2]> {
// This is sound. All invariants are automatically kept, as the raw // This is sound. All invariants are automatically kept, as the raw
@ -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 { impl Drop for PolyChain {
fn drop(&mut self) { fn drop(&mut self) {
// Decrement the reference counter // Decrement the reference counter

View File

@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::{Shape, Shape2d}; use crate::{Shape, Shape2d};
/// A sweep of a 2-dimensional shape along straight path /// A sweep of a 2-dimensional shape along straight path
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)] #[repr(C)]
pub struct Sweep { pub struct Sweep {

View File

@ -12,7 +12,7 @@ use crate::{Angle, Shape};
/// ///
/// See issue: /// See issue:
/// <https://github.com/hannobraun/Fornjot/issues/101> /// <https://github.com/hannobraun/Fornjot/issues/101>
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)] #[repr(C)]
pub struct Transform { pub struct Transform {