9 Commits
0.1 ... 0.1.1

11 changed files with 164 additions and 7 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
/target
**/target
**/*.rs.bk
Cargo.lock

View File

@ -12,6 +12,13 @@ os:
script:
- rustc --version
- cargo --version
- echo "Testing default crate configuration"
- cargo build --verbose
- cargo test --lib --verbose
- cargo test --verbose
- cd examples && cargo check --verbose
- echo "Testing feature serialization"
- cargo build --verbose --features serialization
- cargo test --verbose --features serialization
- echo "Testing without std"
- cargo build --verbose --no-default-features
- cargo test --verbose --no-default-features

13
CHANGELOG.md Normal file
View File

@ -0,0 +1,13 @@
## 0.1.1
> Wed 8th August 2018
- Add a feature gate, `"serialization"`, that can be used to automatically derive `Serialize` and
`Deserialize` from the [serde](https://crates.io/crates/serde) crate.
- Enhance the documentation.
# 0.1
> Sunday 5th August 2018
- Initial revision.

View File

@ -1,6 +1,6 @@
[package]
name = "splines"
version = "0.1.0"
version = "0.1.1"
license = "BSD-3-Clause"
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
description = "Spline interpolation made easy"
@ -17,5 +17,18 @@ is-it-maintained-issue-resolution = { repository = "phaazon/splines" }
is-it-maintained-open-issues = { repository = "phaazon/splines" }
maintenance = { status = "actively-developed" }
[features]
default = ["std"]
serialization = ["serde", "serde_derive"]
std = []
[dependencies]
cgmath = "0.16"
[dependencies.serde]
version = "1"
optional = true
[dependencies.serde_derive]
version = "1"
optional = true

View File

@ -1,4 +1,19 @@
# splines
This crate provides [splines](https://en.wikipedia.org/wiki/Spline_(mathematics)), mathematic curves
defined piecewise through control keys a.k.a. knots.
Feel free to dig in the [online documentation](https://docs.rs/splines) for further information.
## A note on features
This crate has features! Heres a comprehensive list of what you can enable:
- **Serialization / deserialization.**
+ This feature implements both the `Serialize` and `Deserialize` traits from `serde`.
+ Enable with the `"serialization"` feature.
- **Standard library / no stdandard library**
+ Its possible to compile against the standard library or go on your own without it.
+ Compiling with the standard library is enabled by default.
+ Use `defaut-features = []` in your `Cargo.toml` to disable.
+ Enable explicitly with the `"std"` feataure.

View File

@ -0,0 +1,7 @@
[package]
name = "hello-world"
version = "0.1.0"
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
[dependencies]
splines = "0.1"

View File

@ -0,0 +1,11 @@
extern crate splines;
use splines::{Interpolation, Key, Spline};
fn main() {
let keys = vec![Key::new(0., 0., Interpolation::default()), Key::new(5., 1., Interpolation::default())];
let spline = Spline::from_vec(keys);
println!("value at 0: {}", spline.clamped_sample(0.));
println!("value at 3: {}", spline.clamped_sample(3.));
}

View File

@ -0,0 +1,11 @@
[package]
name = "serialization"
version = "0.1.0"
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
[dependencies]
serde_json = "1"
[dependencies.splines]
version = "0.1"
features = ["serialization"]

View File

@ -0,0 +1,30 @@
#[macro_use] extern crate serde_json;
extern crate splines;
use serde_json::{Value, from_value};
use splines::Spline;
fn main() {
let value = json!{
[
{
"t": 0,
"interpolation": "linear",
"value": 0
},
{
"t": 1,
"interpolation": { "step": 0.5 },
"value": 1
},
{
"t": 5,
"interpolation": "cosine",
"value": 10
},
]
};
let spline = from_value::<Spline<f32>>(value);
println!("{:?}", spline);
}

9
examples/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[workspace]
members = [
"01-hello-world",
"02-serialization"
]
[patch.crates-io]
splines = { path = ".." }

View File

@ -61,15 +61,51 @@
//! assert_eq!(spline.clamped_sample(-0.9), 0.); // clamped to the first key
//! assert_eq!(spline.clamped_sample(1.1), 10.); // clamped to the last key
//! ```
//! # Features and customization
//!
//! Feel free to have a look at the rest of the documentation for advanced usage.
//! This crate was written with features baked in and hidden behind feature-gates. The idea is that
//! the default configuration (i.e. you just add `"spline = …"` to your `Cargo.toml`) will always
//! give you the minimal, core and raw concepts of what splines, keys / knots and interpolation
//! modes are. However, you might want more. Instead of letting other people do the extra work to
//! add implementations for very famous and useful traits and do it in less efficient way, because
//! they wouldnt have access to the internals of this crate, its possible to enable features in an
//! ad hoc way.
//!
//! This mechanism is not final and this is currently an experiment to see how people like it or
//! not. Its especially important to see how it copes with the documentation.
//!
//! So heres a list of currently supported features and how to enable them:
//!
//! - **Serialization / deserialization.**
//! + This feature implements both the `Serialize` and `Deserialize` traits from `serde`.
//! + Enable with the `"serialization"` feature.
//! - **Standard library / no stdandard library**
//! + Its possible to compile against the standard library or go on your own without it.
//! + Compiling with the standard library is enabled by default.
//! + Use `defaut-features = []` in your `Cargo.toml` to disable.
//! + Enable explicitly with the `"std"` feataure.
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]
// on no_std, we also need the alloc crate for Vec
#[cfg(not(feature = "std"))] extern crate alloc;
extern crate cgmath;
#[cfg(feature = "serialization")] extern crate serde;
#[cfg(feature = "serialization")] #[macro_use] extern crate serde_derive;
use cgmath::{InnerSpace, Quaternion, Vector2, Vector3, Vector4};
use std::cmp::Ordering;
use std::f32::consts;
use std::ops::{Add, Div, Mul, Sub};
#[cfg(feature = "std")] use std::cmp::Ordering;
#[cfg(feature = "std")] use std::f32::consts;
#[cfg(feature = "std")] use std::ops::{Add, Div, Mul, Sub};
#[cfg(not(feature = "std"))] use alloc::vec::Vec;
#[cfg(not(feature = "std"))] use core::cmp::Ordering;
#[cfg(not(feature = "std"))] use core::f32::consts;
#[cfg(not(feature = "std"))] use core::ops::{Add, Div, Mul, Sub};
/// A spline control point.
///
@ -77,6 +113,8 @@ use std::ops::{Add, Div, Mul, Sub};
/// interpolation hint used to determine how to interpolate values on the segment defined by this
/// key and the next one if existing.
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
pub struct Key<T> {
/// Interpolation parameter at which the [`Key`] should be reached.
pub t: f32,
@ -99,6 +137,8 @@ impl<T> Key<T> {
/// Interpolation mode.
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
pub enum Interpolation {
/// Hold a [`Key`] until the time passes the normalized step threshold, in which case the next
/// key is used.
@ -125,6 +165,7 @@ impl Default for Interpolation {
/// Spline curve used to provide interpolation between control points (keys).
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
pub struct Spline<T>(Vec<Key<T>>);
impl<T> Spline<T> {