Compare commits
41 Commits
Author | SHA1 | Date | |
---|---|---|---|
4a2f349954 | |||
85ac489636 | |||
aea9011296 | |||
04247d8706 | |||
0fcdbacaf3 | |||
89dfb61272 | |||
1bcf1de99e | |||
4630f44d6c | |||
efe9272816 | |||
036d7df3eb | |||
a33dbf9fde | |||
dfa1e6a745 | |||
f04ea0fefa | |||
8ceb8d768c | |||
c93109e28b | |||
d80de42d2f | |||
2e6a5a0dfb | |||
62147d5348 | |||
2dfc11c908 | |||
0c23df7bf0 | |||
3b6ddc5ea6 | |||
824afef513 | |||
f2b356b78d | |||
955050ecee | |||
22e75c6901 | |||
425433cd5b | |||
cc0a9580ab | |||
05e131baad | |||
0a15fb48a3 | |||
ebc6e16aef | |||
cae599e0d7 | |||
336c1c7e80 | |||
ea29e08836 | |||
3ab98420c8 | |||
1bfd9a0e7c | |||
7846177471 | |||
6f65be125b | |||
5d0ebc0777 | |||
4fdbfa6189 | |||
7dbc85a312 | |||
03031a1e92 |
12
.dependabot/config.yml
Normal file
12
.dependabot/config.yml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
version: 1
|
||||||
|
update_configs:
|
||||||
|
- package_manager: "rust:cargo"
|
||||||
|
directory: "."
|
||||||
|
update_schedule: "live"
|
||||||
|
target_branch: "master"
|
||||||
|
default_reviewers:
|
||||||
|
- "phaazon"
|
||||||
|
default_assignees:
|
||||||
|
- "phaazon"
|
||||||
|
default_labels:
|
||||||
|
- "dependency-update"
|
41
.github/workflows/ci.yaml
vendored
41
.github/workflows/ci.yaml
vendored
@ -1,5 +1,5 @@
|
|||||||
name: CI
|
name: CI
|
||||||
on: [push]
|
on: [push, pull_request]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-linux:
|
build-linux:
|
||||||
@ -8,12 +8,10 @@ jobs:
|
|||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
cargo build --verbose
|
cargo build --verbose --all-features
|
||||||
cargo build --verbose --features bezier
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: |
|
||||||
cargo test --verbose
|
cargo test --verbose --all-features
|
||||||
cargo test --verbose --features bezier
|
|
||||||
|
|
||||||
build-windows:
|
build-windows:
|
||||||
runs-on: windows-latest
|
runs-on: windows-latest
|
||||||
@ -21,31 +19,36 @@ jobs:
|
|||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
cargo build --verbose
|
cargo build --verbose --all-features
|
||||||
cargo build --verbose --features bezier
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: |
|
||||||
cargo test --verbose
|
cargo test --verbose --all-features
|
||||||
cargo test --verbose --features bezier
|
|
||||||
|
|
||||||
build-macosx:
|
build-macosx:
|
||||||
runs-on: macosx-latest
|
runs-on: macOS-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
|
- name: Rust requirements
|
||||||
|
run: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
cargo build --verbose
|
. ~/.cargo/env
|
||||||
cargo build --verbose --features bezier
|
cargo build --verbose --all-features
|
||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: |
|
||||||
cargo test --verbose
|
. ~/.cargo/env
|
||||||
cargo test --verbose --features bezier
|
cargo test --verbose --all-features
|
||||||
|
|
||||||
check-readme:
|
quality:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- name: Install cargo-sync-readme
|
- name: Install dependencies
|
||||||
run: cargo install --force cargo-sync-readme
|
run: |
|
||||||
- name: Check
|
cargo install --force cargo-sync-readme
|
||||||
run: cargo sync-readme -c
|
rustup component add rustfmt
|
||||||
|
- name: cargo sync-readme
|
||||||
|
run: |
|
||||||
|
cargo sync-readme -c
|
||||||
|
- name: rustfmt
|
||||||
|
run: cargo fmt -- --check
|
||||||
|
76
CHANGELOG.md
76
CHANGELOG.md
@ -1,6 +1,80 @@
|
|||||||
|
# 3.3
|
||||||
|
|
||||||
|
> Thu Apr 0th 2020
|
||||||
|
|
||||||
|
- Add support for `nalgebra-0.21`.
|
||||||
|
|
||||||
|
# 3.2
|
||||||
|
|
||||||
|
> Thu Mar 19th 2020
|
||||||
|
|
||||||
|
- Add support for `nalgebra-0.20`.
|
||||||
|
- Add support for `float-cmp-0.6`.
|
||||||
|
|
||||||
|
# 3.1
|
||||||
|
|
||||||
|
> Sat Jan 26th 2020
|
||||||
|
|
||||||
|
- Add support for `nalgebra-0.19`.
|
||||||
|
|
||||||
|
# 3.0
|
||||||
|
|
||||||
|
> Tue Oct 22th 2019
|
||||||
|
|
||||||
|
## Major changes
|
||||||
|
|
||||||
|
- Sampling now requires the value of the key to be `Linear<T>` for `Interpolate<T>`. That is needed
|
||||||
|
to ease some interpolation mode (especially Bézier).
|
||||||
|
|
||||||
|
## Patch changes
|
||||||
|
|
||||||
|
- Fix Bézier interpolation when the next key is Bézier too.
|
||||||
|
|
||||||
|
# 2.2
|
||||||
|
|
||||||
|
> Mon Oct 17th 2019
|
||||||
|
|
||||||
|
- Add `Interpolation::StrokeBezier`.
|
||||||
|
|
||||||
|
# 2.1.1
|
||||||
|
|
||||||
|
> Mon Oct 17th 2019
|
||||||
|
|
||||||
|
- Licensing support in the crate.
|
||||||
|
|
||||||
|
# 2.1
|
||||||
|
|
||||||
|
> Mon Sep 30th 2019
|
||||||
|
|
||||||
|
- Add `Spline::sample_with_key` and `Spline::clamped_sample_with_key`. Those methods allow one to
|
||||||
|
perform the regular `Spline::sample` and `Spline::clamped_sample` but also retreive the base
|
||||||
|
key that was used to perform the interpolation. The key can be inspected to get the base time,
|
||||||
|
interpolation, etc. The next key is also returned, if present.
|
||||||
|
|
||||||
|
# 2.0.1
|
||||||
|
|
||||||
|
> Tue Sep 24th 2019
|
||||||
|
|
||||||
|
- Fix the cubic Bézier curve interpolation. The “output” tangent is now taken by mirroring the
|
||||||
|
next key’s tangent around its control point.
|
||||||
|
|
||||||
|
# 2.0
|
||||||
|
|
||||||
|
> Mon Sep 23rd 2019
|
||||||
|
|
||||||
|
## Major changes
|
||||||
|
|
||||||
|
- Add support for [Bézier curves](https://en.wikipedia.org/wiki/B%C3%A9zier_curve).
|
||||||
|
- Because of Bézier curves, the `Interpolation` type now has one more type variable to know how we
|
||||||
|
should interpolate with Bézier.
|
||||||
|
|
||||||
|
## Minor changes
|
||||||
|
|
||||||
|
- Add `Spline::get`, `Spline::get_mut` and `Spline::replace`.
|
||||||
|
|
||||||
# 1.0
|
# 1.0
|
||||||
|
|
||||||
> Sun Sep 22th 2019
|
> Sun Sep 22nd 2019
|
||||||
|
|
||||||
## Major changes
|
## Major changes
|
||||||
|
|
||||||
|
20
Cargo.toml
20
Cargo.toml
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "splines"
|
name = "splines"
|
||||||
version = "1.1.0"
|
version = "3.3.0"
|
||||||
license = "BSD-3-Clause"
|
license = "BSD-3-Clause"
|
||||||
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
|
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
|
||||||
description = "Spline interpolation made easy"
|
description = "Spline interpolation made easy"
|
||||||
@ -21,19 +21,29 @@ maintenance = { status = "actively-developed" }
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["std"]
|
default = ["std"]
|
||||||
bezier = []
|
|
||||||
impl-cgmath = ["cgmath"]
|
impl-cgmath = ["cgmath"]
|
||||||
impl-nalgebra = ["alga", "nalgebra", "num-traits"]
|
impl-nalgebra = ["nalgebra", "num-traits", "simba"]
|
||||||
serialization = ["serde", "serde_derive"]
|
serialization = ["serde", "serde_derive"]
|
||||||
std = []
|
std = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
alga = { version = "0.9", optional = true }
|
|
||||||
cgmath = { version = "0.17", optional = true }
|
cgmath = { version = "0.17", optional = true }
|
||||||
nalgebra = { version = ">=0.14, <0.19", optional = true }
|
nalgebra = { version = "0.21", optional = true }
|
||||||
num-traits = { version = "0.2", optional = true }
|
num-traits = { version = "0.2", optional = true }
|
||||||
serde = { version = "1", optional = true }
|
serde = { version = "1", optional = true }
|
||||||
serde_derive = { version = "1", optional = true }
|
serde_derive = { version = "1", optional = true }
|
||||||
|
simba = { version = "0.1.2", optional = true }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
float-cmp = "0.6"
|
||||||
|
serde_json = "1"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "hello-world"
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "serialization"
|
||||||
|
required-features = ["serialization"]
|
||||||
|
30
LICENSE
Normal file
30
LICENSE
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
Copyright (c) 2019, Dimitri Sabadie <dimitri.sabadie@gmail.com>
|
||||||
|
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following
|
||||||
|
disclaimer in the documentation and/or other materials provided
|
||||||
|
with the distribution.
|
||||||
|
|
||||||
|
* Neither the name of Dimitri Sabadie <dimitri.sabadie@gmail.com> nor the names of other
|
||||||
|
contributors may be used to endorse or promote products derived
|
||||||
|
from this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@ -98,12 +98,6 @@ So here’s a list of currently supported features and how to enable them:
|
|||||||
- Compiling with the standard library is enabled by default.
|
- Compiling with the standard library is enabled by default.
|
||||||
- Use `default-features = []` in your `Cargo.toml` to disable.
|
- Use `default-features = []` in your `Cargo.toml` to disable.
|
||||||
- Enable explicitly with the `"std"` feature.
|
- Enable explicitly with the `"std"` feature.
|
||||||
- **Extra interpolation modes.**
|
|
||||||
- In order not to introduce breaking changes, some feature-gates are added to augment the
|
|
||||||
[`Interpolation`] enum.
|
|
||||||
- Those feature-gates will disappear on the next major release of the crate.
|
|
||||||
- The following lists all currently available:
|
|
||||||
- `"bezier"`: [Bézier curves](https://en.wikipedia.org/wiki/B%C3%A9zier_curve).
|
|
||||||
|
|
||||||
[`Interpolation`]: crate::interpolation::Interpolation
|
[`Interpolation`]: crate::interpolation::Interpolation
|
||||||
|
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "hello-world"
|
|
||||||
version = "0.2.0"
|
|
||||||
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
splines = "1.0.0-rc.2"
|
|
@ -1,8 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "serialization"
|
|
||||||
version = "0.2.0"
|
|
||||||
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
serde_json = "1"
|
|
||||||
splines = { version = "1.0.0-rc.2", features = ["serialization"] }
|
|
@ -1,9 +0,0 @@
|
|||||||
[workspace]
|
|
||||||
|
|
||||||
members = [
|
|
||||||
"01-hello-world",
|
|
||||||
"02-serialization"
|
|
||||||
]
|
|
||||||
|
|
||||||
[patch.crates-io]
|
|
||||||
splines = { path = ".." }
|
|
@ -3,7 +3,10 @@ extern crate splines;
|
|||||||
use splines::{Interpolation, Key, Spline};
|
use splines::{Interpolation, Key, Spline};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let keys = vec![Key::new(0., 0., Interpolation::default()), Key::new(5., 1., Interpolation::default())];
|
let keys = vec![
|
||||||
|
Key::new(0., 0., Interpolation::default()),
|
||||||
|
Key::new(5., 1., Interpolation::default()),
|
||||||
|
];
|
||||||
let spline = Spline::from_vec(keys);
|
let spline = Spline::from_vec(keys);
|
||||||
|
|
||||||
println!("value at 0: {:?}", spline.clamped_sample(0.));
|
println!("value at 0: {:?}", spline.clamped_sample(0.));
|
@ -1,11 +1,12 @@
|
|||||||
#[macro_use] extern crate serde_json;
|
#[macro_use]
|
||||||
|
extern crate serde_json;
|
||||||
extern crate splines;
|
extern crate splines;
|
||||||
|
|
||||||
use serde_json::from_value;
|
use serde_json::from_value;
|
||||||
use splines::Spline;
|
use splines::Spline;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let value = json!{
|
let value = json! {
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"t": 0,
|
"t": 0,
|
15
rustfmt.toml
Normal file
15
rustfmt.toml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
fn_args_layout = "Tall"
|
||||||
|
force_explicit_abi = true
|
||||||
|
hard_tabs = false
|
||||||
|
max_width = 100
|
||||||
|
merge_derives = true
|
||||||
|
newline_style = "Unix"
|
||||||
|
remove_nested_parens = true
|
||||||
|
reorder_imports = true
|
||||||
|
reorder_modules = true
|
||||||
|
tab_spaces = 2
|
||||||
|
use_field_init_shorthand = true
|
||||||
|
use_small_heuristics = "Default"
|
||||||
|
use_try_shorthand = true
|
@ -1,8 +1,10 @@
|
|||||||
use cgmath::{
|
use cgmath::{
|
||||||
BaseFloat, BaseNum, InnerSpace, Quaternion, Vector1, Vector2, Vector3, Vector4, VectorSpace
|
BaseFloat, BaseNum, InnerSpace, Quaternion, Vector1, Vector2, Vector3, Vector4, VectorSpace,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::interpolate::{Additive, Interpolate, Linear, One, cubic_hermite_def};
|
use crate::interpolate::{
|
||||||
|
cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def, Additive, Interpolate, Linear, One,
|
||||||
|
};
|
||||||
|
|
||||||
macro_rules! impl_interpolate_vec {
|
macro_rules! impl_interpolate_vec {
|
||||||
($($t:tt)*) => {
|
($($t:tt)*) => {
|
||||||
@ -29,6 +31,16 @@ macro_rules! impl_interpolate_vec {
|
|||||||
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
||||||
cubic_hermite_def(x, a, b, y, t)
|
cubic_hermite_def(x, a, b, y, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
|
||||||
|
quadratic_bezier_def(a, u, b, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
|
||||||
|
cubic_bezier_def(a, u, v, b, t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -38,7 +50,10 @@ impl_interpolate_vec!(Vector2);
|
|||||||
impl_interpolate_vec!(Vector3);
|
impl_interpolate_vec!(Vector3);
|
||||||
impl_interpolate_vec!(Vector4);
|
impl_interpolate_vec!(Vector4);
|
||||||
|
|
||||||
impl<T> Linear<T> for Quaternion<T> where T: BaseFloat {
|
impl<T> Linear<T> for Quaternion<T>
|
||||||
|
where
|
||||||
|
T: BaseFloat,
|
||||||
|
{
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn outer_mul(self, t: T) -> Self {
|
fn outer_mul(self, t: T) -> Self {
|
||||||
self * t
|
self * t
|
||||||
@ -51,7 +66,10 @@ impl<T> Linear<T> for Quaternion<T> where T: BaseFloat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Interpolate<T> for Quaternion<T>
|
impl<T> Interpolate<T> for Quaternion<T>
|
||||||
where Self: InnerSpace<Scalar = T>, T: Additive + BaseFloat + One {
|
where
|
||||||
|
Self: InnerSpace<Scalar = T>,
|
||||||
|
T: Additive + BaseFloat + One,
|
||||||
|
{
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn lerp(a: Self, b: Self, t: T) -> Self {
|
fn lerp(a: Self, b: Self, t: T) -> Self {
|
||||||
a.nlerp(b, t)
|
a.nlerp(b, t)
|
||||||
@ -61,4 +79,14 @@ where Self: InnerSpace<Scalar = T>, T: Additive + BaseFloat + One {
|
|||||||
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
||||||
cubic_hermite_def(x, a, b, y, t)
|
cubic_hermite_def(x, a, b, y, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
|
||||||
|
quadratic_bezier_def(a, u, b, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
|
||||||
|
cubic_bezier_def(a, u, v, b, t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,14 +28,22 @@
|
|||||||
//! [`Trigo`]: crate::interpolate::Trigo
|
//! [`Trigo`]: crate::interpolate::Trigo
|
||||||
//! [num-traits]: https://crates.io/crates/num-traits
|
//! [num-traits]: https://crates.io/crates/num-traits
|
||||||
|
|
||||||
#[cfg(feature = "std")] use std::f32;
|
#[cfg(not(feature = "std"))]
|
||||||
#[cfg(not(feature = "std"))] use core::f32;
|
use core::f32;
|
||||||
#[cfg(not(feature = "std"))] use core::intrinsics::cosf32;
|
#[cfg(not(feature = "std"))]
|
||||||
#[cfg(feature = "std")] use std::f64;
|
use core::f64;
|
||||||
#[cfg(not(feature = "std"))] use core::f64;
|
#[cfg(not(feature = "std"))]
|
||||||
#[cfg(not(feature = "std"))] use core::intrinsics::cosf64;
|
use core::intrinsics::cosf32;
|
||||||
#[cfg(feature = "std")] use std::ops::{Add, Mul, Sub};
|
#[cfg(not(feature = "std"))]
|
||||||
#[cfg(not(feature = "std"))] use core::ops::{Add, Mul, Sub};
|
use core::intrinsics::cosf64;
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
use core::ops::{Add, Mul, Sub};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use std::f32;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use std::f64;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use std::ops::{Add, Mul, Sub};
|
||||||
|
|
||||||
/// Keys that can be interpolated in between. Implementing this trait is required to perform
|
/// Keys that can be interpolated in between. Implementing this trait is required to perform
|
||||||
/// sampling on splines.
|
/// sampling on splines.
|
||||||
@ -45,7 +53,7 @@
|
|||||||
/// instance to know which trait your type must implement to be usable.
|
/// instance to know which trait your type must implement to be usable.
|
||||||
///
|
///
|
||||||
/// [`Spline::sample`]: crate::spline::Spline::sample
|
/// [`Spline::sample`]: crate::spline::Spline::sample
|
||||||
pub trait Interpolate<T>: Sized + Copy {
|
pub trait Interpolate<T>: Sized + Copy + Linear<T> {
|
||||||
/// Linear interpolation.
|
/// Linear interpolation.
|
||||||
fn lerp(a: Self, b: Self, t: T) -> Self;
|
fn lerp(a: Self, b: Self, t: T) -> Self;
|
||||||
|
|
||||||
@ -68,17 +76,9 @@ pub trait Interpolate<T>: Sized + Copy {
|
|||||||
/// Set of types that support additions and subtraction.
|
/// Set of types that support additions and subtraction.
|
||||||
///
|
///
|
||||||
/// The [`Copy`] trait is also a supertrait as it’s likely to be used everywhere.
|
/// The [`Copy`] trait is also a supertrait as it’s likely to be used everywhere.
|
||||||
pub trait Additive:
|
pub trait Additive: Copy + Add<Self, Output = Self> + Sub<Self, Output = Self> {}
|
||||||
Copy +
|
|
||||||
Add<Self, Output = Self> +
|
|
||||||
Sub<Self, Output = Self> {
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Additive for T
|
impl<T> Additive for T where T: Copy + Add<Self, Output = Self> + Sub<Self, Output = Self> {}
|
||||||
where T: Copy +
|
|
||||||
Add<Self, Output = Self> +
|
|
||||||
Sub<Self, Output = Self> {
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set of additive types that support outer multiplication and division, making them linear.
|
/// Set of additive types that support outer multiplication and division, making them linear.
|
||||||
pub trait Linear<T>: Additive {
|
pub trait Linear<T>: Additive {
|
||||||
@ -101,7 +101,7 @@ macro_rules! impl_linear_simple {
|
|||||||
self / t
|
self / t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_linear_simple!(f32);
|
impl_linear_simple!(f32);
|
||||||
@ -119,7 +119,7 @@ macro_rules! impl_linear_cast {
|
|||||||
self / t as $q
|
self / t as $q
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_linear_cast!(f32, f64);
|
impl_linear_cast!(f32, f64);
|
||||||
@ -139,7 +139,7 @@ macro_rules! impl_one_float {
|
|||||||
1.
|
1.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_one_float!(f32);
|
impl_one_float!(f32);
|
||||||
@ -198,8 +198,10 @@ impl Trigo for f64 {
|
|||||||
///
|
///
|
||||||
/// `V` is the value being interpolated. `T` is the sampling value (also sometimes called time).
|
/// `V` is the value being interpolated. `T` is the sampling value (also sometimes called time).
|
||||||
pub fn cubic_hermite_def<V, T>(x: (V, T), a: (V, T), b: (V, T), y: (V, T), t: T) -> V
|
pub fn cubic_hermite_def<V, T>(x: (V, T), a: (V, T), b: (V, T), y: (V, T), t: T) -> V
|
||||||
where V: Linear<T>,
|
where
|
||||||
T: Additive + Mul<T, Output = T> + One {
|
V: Linear<T>,
|
||||||
|
T: Additive + Mul<T, Output = T> + One,
|
||||||
|
{
|
||||||
// some stupid generic constants, because Rust doesn’t have polymorphic literals…
|
// some stupid generic constants, because Rust doesn’t have polymorphic literals…
|
||||||
let one_t = T::one();
|
let one_t = T::one();
|
||||||
let two_t = one_t + one_t; // lolololol
|
let two_t = one_t + one_t; // lolololol
|
||||||
@ -215,15 +217,20 @@ where V: Linear<T>,
|
|||||||
let m0 = (b.0 - x.0).outer_div(b.1 - x.1);
|
let m0 = (b.0 - x.0).outer_div(b.1 - x.1);
|
||||||
let m1 = (y.0 - a.0).outer_div(y.1 - a.1);
|
let m1 = (y.0 - a.0).outer_div(y.1 - a.1);
|
||||||
|
|
||||||
a.0.outer_mul(two_t3 - three_t2 + one_t) + m0.outer_mul(t3 - t2 * two_t + t) + b.0.outer_mul(three_t2 - two_t3) + m1.outer_mul(t3 - t2)
|
a.0.outer_mul(two_t3 - three_t2 + one_t)
|
||||||
|
+ m0.outer_mul(t3 - t2 * two_t + t)
|
||||||
|
+ b.0.outer_mul(three_t2 - two_t3)
|
||||||
|
+ m1.outer_mul(t3 - t2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Default implementation of [`Interpolate::quadratic_bezier`].
|
/// Default implementation of [`Interpolate::quadratic_bezier`].
|
||||||
///
|
///
|
||||||
/// `V` is the value being interpolated. `T` is the sampling value (also sometimes called time).
|
/// `V` is the value being interpolated. `T` is the sampling value (also sometimes called time).
|
||||||
pub fn quadratic_bezier_def<V, T>(a: V, u: V, b: V, t: T) -> V
|
pub fn quadratic_bezier_def<V, T>(a: V, u: V, b: V, t: T) -> V
|
||||||
where V: Linear<T>,
|
where
|
||||||
T: Additive + Mul<T, Output = T> + One {
|
V: Linear<T>,
|
||||||
|
T: Additive + Mul<T, Output = T> + One,
|
||||||
|
{
|
||||||
let one_t = T::one() - t;
|
let one_t = T::one() - t;
|
||||||
let one_t_2 = one_t * one_t;
|
let one_t_2 = one_t * one_t;
|
||||||
u + (a - u).outer_mul(one_t_2) + (b - u).outer_mul(t * t)
|
u + (a - u).outer_mul(one_t_2) + (b - u).outer_mul(t * t)
|
||||||
@ -233,14 +240,19 @@ where V: Linear<T>,
|
|||||||
///
|
///
|
||||||
/// `V` is the value being interpolated. `T` is the sampling value (also sometimes called time).
|
/// `V` is the value being interpolated. `T` is the sampling value (also sometimes called time).
|
||||||
pub fn cubic_bezier_def<V, T>(a: V, u: V, v: V, b: V, t: T) -> V
|
pub fn cubic_bezier_def<V, T>(a: V, u: V, v: V, b: V, t: T) -> V
|
||||||
where V: Linear<T>,
|
where
|
||||||
T: Additive + Mul<T, Output = T> + One {
|
V: Linear<T>,
|
||||||
|
T: Additive + Mul<T, Output = T> + One,
|
||||||
|
{
|
||||||
let one_t = T::one() - t;
|
let one_t = T::one() - t;
|
||||||
let one_t_2 = one_t * one_t;
|
let one_t_2 = one_t * one_t;
|
||||||
let one_t_3 = one_t_2 * one_t;
|
let one_t_3 = one_t_2 * one_t;
|
||||||
let three = T::one() + T::one() + T::one();
|
let three = T::one() + T::one() + T::one();
|
||||||
|
|
||||||
a.outer_mul(one_t_3) + u.outer_mul(three * one_t_2 * t) + v.outer_mul(three * one_t * t * t) + b.outer_mul(t * t * t)
|
a.outer_mul(one_t_3)
|
||||||
|
+ u.outer_mul(three * one_t_2 * t)
|
||||||
|
+ v.outer_mul(three * one_t * t * t)
|
||||||
|
+ b.outer_mul(t * t * t)
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_interpolate_simple {
|
macro_rules! impl_interpolate_simple {
|
||||||
@ -262,33 +274,45 @@ macro_rules! impl_interpolate_simple {
|
|||||||
cubic_bezier_def(a, u, v, b, t)
|
cubic_bezier_def(a, u, v, b, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_interpolate_simple!(f32);
|
impl_interpolate_simple!(f32);
|
||||||
impl_interpolate_simple!(f64);
|
impl_interpolate_simple!(f64);
|
||||||
|
|
||||||
//macro_rules! impl_interpolate_via {
|
macro_rules! impl_interpolate_via {
|
||||||
// ($t:ty, $v:ty) => {
|
($t:ty, $v:ty) => {
|
||||||
// impl Interpolate<$t> for $v {
|
impl Interpolate<$t> for $v {
|
||||||
// fn lerp(a: Self, b: Self, t: $t) -> Self {
|
fn lerp(a: Self, b: Self, t: $t) -> Self {
|
||||||
// a * (1. - t as $v) + b * t as $v
|
a * (1. - t as $v) + b * t as $v
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// fn cubic_hermite((x, xt): (Self, $t), (a, at): (Self, $t), (b, bt): (Self, $t), (y, yt): (Self, $t), t: $t) -> Self {
|
fn cubic_hermite(
|
||||||
// cubic_hermite_def((x, xt as $v), (a, at as $v), (b, bt as $v), (y, yt as $v), t as $v)
|
(x, xt): (Self, $t),
|
||||||
// }
|
(a, at): (Self, $t),
|
||||||
//
|
(b, bt): (Self, $t),
|
||||||
// fn quadratic_bezier(a: Self, u: Self, b: Self, t: $t) -> Self {
|
(y, yt): (Self, $t),
|
||||||
// $t::quadratic_bezier(a as $t, u as $t, b as $t, t)
|
t: $t,
|
||||||
// }
|
) -> Self {
|
||||||
//
|
cubic_hermite_def(
|
||||||
// fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: $t) -> Self {
|
(x, xt as $v),
|
||||||
// $t::cubic_bezier(a as $t, u as $t, v as $t, b as $t, t)
|
(a, at as $v),
|
||||||
// }
|
(b, bt as $v),
|
||||||
// }
|
(y, yt as $v),
|
||||||
// }
|
t as $v,
|
||||||
//}
|
)
|
||||||
//
|
}
|
||||||
//impl_interpolate_via!(f32, f64);
|
|
||||||
//impl_interpolate_via!(f64, f32);
|
fn quadratic_bezier(a: Self, u: Self, b: Self, t: $t) -> Self {
|
||||||
|
quadratic_bezier_def(a, u, b, t as $v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: $t) -> Self {
|
||||||
|
cubic_bezier_def(a, u, v, b, t as $v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_interpolate_via!(f32, f64);
|
||||||
|
impl_interpolate_via!(f64, f32);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
//! Available interpolation modes.
|
//! Available interpolation modes.
|
||||||
|
|
||||||
#[cfg(feature = "serialization")] use serde_derive::{Deserialize, Serialize};
|
#[cfg(feature = "serialization")]
|
||||||
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// Available kind of interpolations.
|
/// Available kind of interpolations.
|
||||||
///
|
///
|
||||||
@ -9,7 +10,7 @@
|
|||||||
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
|
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
|
||||||
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
|
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
|
||||||
pub enum Interpolation<T, V> {
|
pub enum Interpolation<T, V> {
|
||||||
/// Hold a [`Key<T, _>`] until the sampling value passes the normalized step threshold, in which
|
/// Hold a [`Key`] until the sampling value passes the normalized step threshold, in which
|
||||||
/// case the next key is used.
|
/// case the next key is used.
|
||||||
///
|
///
|
||||||
/// > Note: if you set the threshold to `0.5`, the first key will be used until half the time
|
/// > Note: if you set the threshold to `0.5`, the first key will be used until half the time
|
||||||
@ -17,7 +18,7 @@ pub enum Interpolation<T, V> {
|
|||||||
/// > first key will be kept until the next key. Set it to `0.` and the first key will never be
|
/// > first key will be kept until the next key. Set it to `0.` and the first key will never be
|
||||||
/// > used.
|
/// > used.
|
||||||
///
|
///
|
||||||
/// [`Key<T, _>`]: crate::key::Key
|
/// [`Key`]: crate::key::Key
|
||||||
Step(T),
|
Step(T),
|
||||||
/// Linear interpolation between a key and the next one.
|
/// Linear interpolation between a key and the next one.
|
||||||
Linear,
|
Linear,
|
||||||
@ -39,11 +40,21 @@ pub enum Interpolation<T, V> {
|
|||||||
/// tangent used for the next control point is defined as the segment connecting that control
|
/// tangent used for the next control point is defined as the segment connecting that control
|
||||||
/// point and the current control point’s associated point. This is called _quadratic Bézer
|
/// point and the current control point’s associated point. This is called _quadratic Bézer
|
||||||
/// interpolation_ and it kicks ass too, but a bit less than cubic.
|
/// interpolation_ and it kicks ass too, but a bit less than cubic.
|
||||||
#[cfg(feature = "bezier")]
|
|
||||||
Bezier(V),
|
Bezier(V),
|
||||||
#[cfg(not(any(feature = "bezier")))]
|
/// A special Bézier interpolation using an _input tangent_ and an _output tangent_.
|
||||||
|
///
|
||||||
|
/// With this kind of interpolation, a control point has an input tangent, which has the same role
|
||||||
|
/// as the one defined by [`Interpolation::Bezier`], and an output tangent, which has the same
|
||||||
|
/// role defined by the next key’s [`Interpolation::Bezier`] if present, normally.
|
||||||
|
///
|
||||||
|
/// What it means is that instead of setting the output tangent as the next key’s Bézier tangent,
|
||||||
|
/// this interpolation mode allows you to manually set the output tangent. That will yield more
|
||||||
|
/// control on the tangents but might generate discontinuities. Use with care.
|
||||||
|
///
|
||||||
|
/// Stroke Bézier interpolation is always a cubic Bézier interpolation by default.
|
||||||
|
StrokeBezier(V, V),
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
_V(std::marker::PhantomData<V>),
|
__NonExhaustive,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, V> Default for Interpolation<T, V> {
|
impl<T, V> Default for Interpolation<T, V> {
|
||||||
@ -52,4 +63,3 @@ impl<T, V> Default for Interpolation<T, V> {
|
|||||||
Interpolation::Linear
|
Interpolation::Linear
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
src/iter.rs
14
src/iter.rs
@ -11,9 +11,13 @@ use crate::{Key, Spline};
|
|||||||
/// Iterator over spline keys.
|
/// Iterator over spline keys.
|
||||||
///
|
///
|
||||||
/// This iterator type is guaranteed to iterate over sorted keys.
|
/// This iterator type is guaranteed to iterate over sorted keys.
|
||||||
pub struct Iter<'a, T, V> where T: 'a, V: 'a {
|
pub struct Iter<'a, T, V>
|
||||||
|
where
|
||||||
|
T: 'a,
|
||||||
|
V: 'a,
|
||||||
|
{
|
||||||
spline: &'a Spline<T, V>,
|
spline: &'a Spline<T, V>,
|
||||||
i: usize
|
i: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T, V> Iterator for Iter<'a, T, V> {
|
impl<'a, T, V> Iterator for Iter<'a, T, V> {
|
||||||
@ -35,10 +39,6 @@ impl<'a, T, V> IntoIterator for &'a Spline<T, V> {
|
|||||||
type IntoIter = Iter<'a, T, V>;
|
type IntoIter = Iter<'a, T, V>;
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
Iter {
|
Iter { spline: self, i: 0 }
|
||||||
spline: self,
|
|
||||||
i: 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
src/key.rs
11
src/key.rs
@ -6,7 +6,8 @@
|
|||||||
//! Splines constructed with this crate have the property that it’s possible to change the
|
//! Splines constructed with this crate have the property that it’s possible to change the
|
||||||
//! interpolation mode on a key-based way, allowing you to implement and encode complex curves.
|
//! interpolation mode on a key-based way, allowing you to implement and encode complex curves.
|
||||||
|
|
||||||
#[cfg(feature = "serialization")] use serde_derive::{Deserialize, Serialize};
|
#[cfg(feature = "serialization")]
|
||||||
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::interpolation::Interpolation;
|
use crate::interpolation::Interpolation;
|
||||||
|
|
||||||
@ -26,12 +27,16 @@ pub struct Key<T, V> {
|
|||||||
/// Carried value.
|
/// Carried value.
|
||||||
pub value: V,
|
pub value: V,
|
||||||
/// Interpolation mode.
|
/// Interpolation mode.
|
||||||
pub interpolation: Interpolation<T, V>
|
pub interpolation: Interpolation<T, V>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, V> Key<T, V> {
|
impl<T, V> Key<T, V> {
|
||||||
/// Create a new key.
|
/// Create a new key.
|
||||||
pub fn new(t: T, value: V, interpolation: Interpolation<T, V>) -> Self {
|
pub fn new(t: T, value: V, interpolation: Interpolation<T, V>) -> Self {
|
||||||
Key { t, value, interpolation }
|
Key {
|
||||||
|
t,
|
||||||
|
value,
|
||||||
|
interpolation,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
src/lib.rs
15
src/lib.rs
@ -99,12 +99,6 @@
|
|||||||
//! - Compiling with the standard library is enabled by default.
|
//! - Compiling with the standard library is enabled by default.
|
||||||
//! - Use `default-features = []` in your `Cargo.toml` to disable.
|
//! - Use `default-features = []` in your `Cargo.toml` to disable.
|
||||||
//! - Enable explicitly with the `"std"` feature.
|
//! - Enable explicitly with the `"std"` feature.
|
||||||
//! - **Extra interpolation modes.**
|
|
||||||
//! - In order not to introduce breaking changes, some feature-gates are added to augment the
|
|
||||||
//! [`Interpolation`] enum.
|
|
||||||
//! - Those feature-gates will disappear on the next major release of the crate.
|
|
||||||
//! - The following lists all currently available:
|
|
||||||
//! - `"bezier"`: [Bézier curves](https://en.wikipedia.org/wiki/B%C3%A9zier_curve).
|
|
||||||
//!
|
//!
|
||||||
//! [`Interpolation`]: crate::interpolation::Interpolation
|
//! [`Interpolation`]: crate::interpolation::Interpolation
|
||||||
|
|
||||||
@ -112,14 +106,17 @@
|
|||||||
#![cfg_attr(not(feature = "std"), feature(alloc))]
|
#![cfg_attr(not(feature = "std"), feature(alloc))]
|
||||||
#![cfg_attr(not(feature = "std"), feature(core_intrinsics))]
|
#![cfg_attr(not(feature = "std"), feature(core_intrinsics))]
|
||||||
|
|
||||||
#[cfg(not(feature = "std"))] extern crate alloc;
|
#[cfg(not(feature = "std"))]
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
#[cfg(feature = "impl-cgmath")] mod cgmath;
|
#[cfg(feature = "impl-cgmath")]
|
||||||
|
mod cgmath;
|
||||||
pub mod interpolate;
|
pub mod interpolate;
|
||||||
pub mod interpolation;
|
pub mod interpolation;
|
||||||
pub mod iter;
|
pub mod iter;
|
||||||
pub mod key;
|
pub mod key;
|
||||||
#[cfg(feature = "impl-nalgebra")] mod nalgebra;
|
#[cfg(feature = "impl-nalgebra")]
|
||||||
|
mod nalgebra;
|
||||||
pub mod spline;
|
pub mod spline;
|
||||||
|
|
||||||
pub use crate::interpolate::Interpolate;
|
pub use crate::interpolate::Interpolate;
|
||||||
|
@ -1,14 +1,22 @@
|
|||||||
use alga::general::{ClosedAdd, ClosedDiv, ClosedMul, ClosedSub};
|
|
||||||
use nalgebra::{Scalar, Vector, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6};
|
use nalgebra::{Scalar, Vector, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6};
|
||||||
use num_traits as nt;
|
use num_traits as nt;
|
||||||
|
use simba::scalar::{ClosedAdd, ClosedDiv, ClosedMul, ClosedSub};
|
||||||
use std::ops::Mul;
|
use std::ops::Mul;
|
||||||
|
|
||||||
use crate::interpolate::{Interpolate, Linear, Additive, One, cubic_hermite_def};
|
use crate::interpolate::{
|
||||||
|
cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def, Additive, Interpolate, Linear, One,
|
||||||
|
};
|
||||||
|
|
||||||
macro_rules! impl_interpolate_vector {
|
macro_rules! impl_interpolate_vector {
|
||||||
($($t:tt)*) => {
|
($($t:tt)*) => {
|
||||||
// implement Linear
|
// implement Linear
|
||||||
impl<T> Linear<T> for $($t)*<T> where T: Scalar + ClosedAdd + ClosedSub + ClosedMul + ClosedDiv {
|
impl<T> Linear<T> for $($t)*<T>
|
||||||
|
where T: Scalar +
|
||||||
|
Copy +
|
||||||
|
ClosedAdd +
|
||||||
|
ClosedSub +
|
||||||
|
ClosedMul +
|
||||||
|
ClosedDiv {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn outer_mul(self, t: T) -> Self {
|
fn outer_mul(self, t: T) -> Self {
|
||||||
self * t
|
self * t
|
||||||
@ -40,6 +48,16 @@ macro_rules! impl_interpolate_vector {
|
|||||||
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
||||||
cubic_hermite_def(x, a, b, y, t)
|
cubic_hermite_def(x, a, b, y, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
|
||||||
|
quadratic_bezier_def(a, u, b, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
|
||||||
|
cubic_bezier_def(a, u, v, b, t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
206
src/spline.rs
206
src/spline.rs
@ -1,13 +1,19 @@
|
|||||||
//! Spline curves and operations.
|
//! Spline curves and operations.
|
||||||
|
|
||||||
#[cfg(feature = "serialization")] use serde_derive::{Deserialize, Serialize};
|
#[cfg(not(feature = "std"))]
|
||||||
#[cfg(not(feature = "std"))] use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
#[cfg(feature = "std")] use std::cmp::Ordering;
|
#[cfg(not(feature = "std"))]
|
||||||
#[cfg(feature = "std")] use std::ops::{Div, Mul};
|
use core::cmp::Ordering;
|
||||||
#[cfg(not(feature = "std"))] use core::ops::{Div, Mul};
|
#[cfg(not(feature = "std"))]
|
||||||
#[cfg(not(feature = "std"))] use core::cmp::Ordering;
|
use core::ops::{Div, Mul};
|
||||||
|
#[cfg(feature = "serialization")]
|
||||||
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use std::ops::{Div, Mul};
|
||||||
|
|
||||||
use crate::interpolate::{Interpolate, Additive, One, Trigo};
|
use crate::interpolate::{Additive, Interpolate, One, Trigo};
|
||||||
use crate::interpolation::Interpolation;
|
use crate::interpolation::Interpolation;
|
||||||
use crate::key::Key;
|
use crate::key::Key;
|
||||||
|
|
||||||
@ -29,13 +35,21 @@ pub struct Spline<T, V>(pub(crate) Vec<Key<T, V>>);
|
|||||||
|
|
||||||
impl<T, V> Spline<T, V> {
|
impl<T, V> Spline<T, V> {
|
||||||
/// Internal sort to ensure invariant of sorting keys is valid.
|
/// Internal sort to ensure invariant of sorting keys is valid.
|
||||||
fn internal_sort(&mut self) where T: PartialOrd {
|
fn internal_sort(&mut self)
|
||||||
self.0.sort_by(|k0, k1| k0.t.partial_cmp(&k1.t).unwrap_or(Ordering::Less));
|
where
|
||||||
|
T: PartialOrd,
|
||||||
|
{
|
||||||
|
self
|
||||||
|
.0
|
||||||
|
.sort_by(|k0, k1| k0.t.partial_cmp(&k1.t).unwrap_or(Ordering::Less));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new spline out of keys. The keys don’t have to be sorted even though it’s recommended
|
/// Create a new spline out of keys. The keys don’t have to be sorted even though it’s recommended
|
||||||
/// to provide ascending sorted ones (for performance purposes).
|
/// to provide ascending sorted ones (for performance purposes).
|
||||||
pub fn from_vec(keys: Vec<Key<T, V>>) -> Self where T: PartialOrd {
|
pub fn from_vec(keys: Vec<Key<T, V>>) -> Self
|
||||||
|
where
|
||||||
|
T: PartialOrd,
|
||||||
|
{
|
||||||
let mut spline = Spline(keys);
|
let mut spline = Spline(keys);
|
||||||
spline.internal_sort();
|
spline.internal_sort();
|
||||||
spline
|
spline
|
||||||
@ -48,7 +62,11 @@ impl<T, V> Spline<T, V> {
|
|||||||
///
|
///
|
||||||
/// It’s valid to use any iterator that implements `Iterator<Item = Key<T>>`. However, you should
|
/// It’s valid to use any iterator that implements `Iterator<Item = Key<T>>`. However, you should
|
||||||
/// use [`Spline::from_vec`] if you are passing a [`Vec`].
|
/// use [`Spline::from_vec`] if you are passing a [`Vec`].
|
||||||
pub fn from_iter<I>(iter: I) -> Self where I: Iterator<Item = Key<T, V>>, T: PartialOrd {
|
pub fn from_iter<I>(iter: I) -> Self
|
||||||
|
where
|
||||||
|
I: Iterator<Item = Key<T, V>>,
|
||||||
|
T: PartialOrd,
|
||||||
|
{
|
||||||
Self::from_vec(iter.collect())
|
Self::from_vec(iter.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +87,8 @@ impl<T, V> Spline<T, V> {
|
|||||||
self.0.is_empty()
|
self.0.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sample a spline at a given time.
|
/// Sample a spline at a given time, returning the interpolated value along with its associated
|
||||||
|
/// key.
|
||||||
///
|
///
|
||||||
/// The current implementation, based on immutability, cannot perform in constant time. This means
|
/// The current implementation, based on immutability, cannot perform in constant time. This means
|
||||||
/// that sampling’s processing complexity is currently *O(log n)*. It’s possible to achieve *O(1)*
|
/// that sampling’s processing complexity is currently *O(log n)*. It’s possible to achieve *O(1)*
|
||||||
@ -83,10 +102,11 @@ impl<T, V> Spline<T, V> {
|
|||||||
/// sampling impossible. For instance, [`Interpolation::CatmullRom`] requires *four* keys. If
|
/// sampling impossible. For instance, [`Interpolation::CatmullRom`] requires *four* keys. If
|
||||||
/// you’re near the beginning of the spline or its end, ensure you have enough keys around to make
|
/// you’re near the beginning of the spline or its end, ensure you have enough keys around to make
|
||||||
/// the sampling.
|
/// the sampling.
|
||||||
///
|
pub fn sample_with_key(&self, t: T) -> Option<(V, &Key<T, V>, Option<&Key<T, V>>)>
|
||||||
pub fn sample(&self, t: T) -> Option<V>
|
where
|
||||||
where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd,
|
T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd,
|
||||||
V: Interpolate<T> {
|
V: Additive + Interpolate<T>,
|
||||||
|
{
|
||||||
let keys = &self.0;
|
let keys = &self.0;
|
||||||
let i = search_lower_cp(keys, t)?;
|
let i = search_lower_cp(keys, t)?;
|
||||||
let cp0 = &keys[i];
|
let cp0 = &keys[i];
|
||||||
@ -95,14 +115,17 @@ impl<T, V> Spline<T, V> {
|
|||||||
Interpolation::Step(threshold) => {
|
Interpolation::Step(threshold) => {
|
||||||
let cp1 = &keys[i + 1];
|
let cp1 = &keys[i + 1];
|
||||||
let nt = normalize_time(t, cp0, cp1);
|
let nt = normalize_time(t, cp0, cp1);
|
||||||
Some(if nt < threshold { cp0.value } else { cp1.value })
|
let value = if nt < threshold { cp0.value } else { cp1.value };
|
||||||
|
|
||||||
|
Some((value, cp0, Some(cp1)))
|
||||||
}
|
}
|
||||||
|
|
||||||
Interpolation::Linear => {
|
Interpolation::Linear => {
|
||||||
let cp1 = &keys[i + 1];
|
let cp1 = &keys[i + 1];
|
||||||
let nt = normalize_time(t, cp0, cp1);
|
let nt = normalize_time(t, cp0, cp1);
|
||||||
|
let value = Interpolate::lerp(cp0.value, cp1.value, nt);
|
||||||
|
|
||||||
Some(Interpolate::lerp(cp0.value, cp1.value, nt))
|
Some((value, cp0, Some(cp1)))
|
||||||
}
|
}
|
||||||
|
|
||||||
Interpolation::Cosine => {
|
Interpolation::Cosine => {
|
||||||
@ -110,8 +133,9 @@ impl<T, V> Spline<T, V> {
|
|||||||
let cp1 = &keys[i + 1];
|
let cp1 = &keys[i + 1];
|
||||||
let nt = normalize_time(t, cp0, cp1);
|
let nt = normalize_time(t, cp0, cp1);
|
||||||
let cos_nt = (T::one() - (nt * T::pi()).cos()) / two_t;
|
let cos_nt = (T::one() - (nt * T::pi()).cos()) / two_t;
|
||||||
|
let value = Interpolate::lerp(cp0.value, cp1.value, cos_nt);
|
||||||
|
|
||||||
Some(Interpolate::lerp(cp0.value, cp1.value, cos_nt))
|
Some((value, cp0, Some(cp1)))
|
||||||
}
|
}
|
||||||
|
|
||||||
Interpolation::CatmullRom => {
|
Interpolation::CatmullRom => {
|
||||||
@ -124,35 +148,54 @@ impl<T, V> Spline<T, V> {
|
|||||||
let cpm0 = &keys[i - 1];
|
let cpm0 = &keys[i - 1];
|
||||||
let cpm1 = &keys[i + 2];
|
let cpm1 = &keys[i + 2];
|
||||||
let nt = normalize_time(t, cp0, cp1);
|
let nt = normalize_time(t, cp0, cp1);
|
||||||
|
let value = Interpolate::cubic_hermite(
|
||||||
|
(cpm0.value, cpm0.t),
|
||||||
|
(cp0.value, cp0.t),
|
||||||
|
(cp1.value, cp1.t),
|
||||||
|
(cpm1.value, cpm1.t),
|
||||||
|
nt,
|
||||||
|
);
|
||||||
|
|
||||||
Some(Interpolate::cubic_hermite((cpm0.value, cpm0.t), (cp0.value, cp0.t), (cp1.value, cp1.t), (cpm1.value, cpm1.t), nt))
|
Some((value, cp0, Some(cp1)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "bezier")]
|
Interpolation::Bezier(u) | Interpolation::StrokeBezier(_, u) => {
|
||||||
Interpolation::Bezier(u) => {
|
|
||||||
// We need to check the next control point to see whether we want quadratic or cubic Bezier.
|
// We need to check the next control point to see whether we want quadratic or cubic Bezier.
|
||||||
let cp1 = &keys[i + 1];
|
let cp1 = &keys[i + 1];
|
||||||
let nt = normalize_time(t, cp0, cp1);
|
let nt = normalize_time(t, cp0, cp1);
|
||||||
|
|
||||||
if let Interpolation::Bezier(v) = cp1.interpolation {
|
let value = match cp1.interpolation {
|
||||||
Some(Interpolate::cubic_bezier(cp0.value, u, v, cp1.value, nt))
|
Interpolation::Bezier(v) => {
|
||||||
//let one_nt = T::one() - nt;
|
Interpolate::cubic_bezier(cp0.value, u, cp1.value + cp1.value - v, cp1.value, nt)
|
||||||
//let one_nt_2 = one_nt * one_nt;
|
}
|
||||||
//let one_nt_3 = one_nt_2 * one_nt;
|
|
||||||
//let three_one_nt_2 = one_nt_2 + one_nt_2 + one_nt_2; // one_nt_2 * 3
|
Interpolation::StrokeBezier(v, _) => {
|
||||||
//let r = cp0.value * one_nt_3;
|
Interpolate::cubic_bezier(cp0.value, u, v, cp1.value, nt)
|
||||||
} else {
|
}
|
||||||
Some(Interpolate::quadratic_bezier(cp0.value, u, cp1.value, nt))
|
|
||||||
}
|
_ => Interpolate::quadratic_bezier(cp0.value, u, cp1.value, nt),
|
||||||
|
};
|
||||||
|
|
||||||
|
Some((value, cp0, Some(cp1)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(any(feature = "bezier")))]
|
Interpolation::__NonExhaustive => unreachable!(),
|
||||||
Interpolation::_V(_) => unreachable!()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sample a spline at a given time with clamping.
|
/// Sample a spline at a given time.
|
||||||
|
///
|
||||||
|
pub fn sample(&self, t: T) -> Option<V>
|
||||||
|
where
|
||||||
|
T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd,
|
||||||
|
V: Additive + Interpolate<T>,
|
||||||
|
{
|
||||||
|
self.sample_with_key(t).map(|(v, _, _)| v)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sample a spline at a given time with clamping, returning the interpolated value along with its
|
||||||
|
/// associated key.
|
||||||
///
|
///
|
||||||
/// # Return
|
/// # Return
|
||||||
///
|
///
|
||||||
@ -162,22 +205,29 @@ impl<T, V> Spline<T, V> {
|
|||||||
/// # Error
|
/// # Error
|
||||||
///
|
///
|
||||||
/// This function returns [`None`] if you have no key.
|
/// This function returns [`None`] if you have no key.
|
||||||
pub fn clamped_sample(&self, t: T) -> Option<V>
|
pub fn clamped_sample_with_key(&self, t: T) -> Option<(V, &Key<T, V>, Option<&Key<T, V>>)>
|
||||||
where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd,
|
where
|
||||||
V: Interpolate<T> {
|
T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd,
|
||||||
|
V: Additive + Interpolate<T>,
|
||||||
|
{
|
||||||
if self.0.is_empty() {
|
if self.0.is_empty() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.sample(t).or_else(move || {
|
self.sample_with_key(t).or_else(move || {
|
||||||
let first = self.0.first().unwrap();
|
let first = self.0.first().unwrap();
|
||||||
if t <= first.t {
|
if t <= first.t {
|
||||||
Some(first.value)
|
let second = if self.0.len() >= 2 {
|
||||||
|
Some(&self.0[1])
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
Some((first.value, &first, second))
|
||||||
} else {
|
} else {
|
||||||
let last = self.0.last().unwrap();
|
let last = self.0.last().unwrap();
|
||||||
|
|
||||||
if t >= last.t {
|
if t >= last.t {
|
||||||
Some(last.value)
|
Some((last.value, &last, None))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -185,8 +235,20 @@ impl<T, V> Spline<T, V> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sample a spline at a given time with clamping.
|
||||||
|
pub fn clamped_sample(&self, t: T) -> Option<V>
|
||||||
|
where
|
||||||
|
T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd,
|
||||||
|
V: Additive + Interpolate<T>,
|
||||||
|
{
|
||||||
|
self.clamped_sample_with_key(t).map(|(v, _, _)| v)
|
||||||
|
}
|
||||||
|
|
||||||
/// Add a key into the spline.
|
/// Add a key into the spline.
|
||||||
pub fn add(&mut self, key: Key<T, V>) where T: PartialOrd {
|
pub fn add(&mut self, key: Key<T, V>)
|
||||||
|
where
|
||||||
|
T: PartialOrd,
|
||||||
|
{
|
||||||
self.0.push(key);
|
self.0.push(key);
|
||||||
self.internal_sort();
|
self.internal_sort();
|
||||||
}
|
}
|
||||||
@ -199,21 +261,67 @@ impl<T, V> Spline<T, V> {
|
|||||||
Some(self.0.remove(index))
|
Some(self.0.remove(index))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update a key and return the key already present.
|
||||||
|
///
|
||||||
|
/// The key is updated — if present — with the provided function.
|
||||||
|
///
|
||||||
|
/// # Notes
|
||||||
|
///
|
||||||
|
/// That function makes sense only if you want to change the interpolator (i.e. [`Key::t`]) of
|
||||||
|
/// your key. If you just want to change the interpolation mode or the carried value, consider
|
||||||
|
/// using the [`Spline::get_mut`] method instead as it will be way faster.
|
||||||
|
pub fn replace<F>(&mut self, index: usize, f: F) -> Option<Key<T, V>>
|
||||||
|
where
|
||||||
|
F: FnOnce(&Key<T, V>) -> Key<T, V>,
|
||||||
|
T: PartialOrd,
|
||||||
|
{
|
||||||
|
let key = self.remove(index)?;
|
||||||
|
self.add(f(&key));
|
||||||
|
Some(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a key at a given index.
|
||||||
|
pub fn get(&self, index: usize) -> Option<&Key<T, V>> {
|
||||||
|
self.0.get(index)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mutably get a key at a given index.
|
||||||
|
pub fn get_mut(&mut self, index: usize) -> Option<KeyMut<T, V>> {
|
||||||
|
self.0.get_mut(index).map(|key| KeyMut {
|
||||||
|
value: &mut key.value,
|
||||||
|
interpolation: &mut key.interpolation,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A mutable [`Key`].
|
||||||
|
///
|
||||||
|
/// Mutable keys allow to edit the carried values and the interpolation mode but not the actual
|
||||||
|
/// interpolator value as it would invalidate the internal structure of the [`Spline`]. If you
|
||||||
|
/// want to achieve this, you’re advised to use [`Spline::replace`].
|
||||||
|
pub struct KeyMut<'a, T, V> {
|
||||||
|
/// Carried value.
|
||||||
|
pub value: &'a mut V,
|
||||||
|
/// Interpolation mode to use for that key.
|
||||||
|
pub interpolation: &'a mut Interpolation<T, V>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalize a time ([0;1]) given two control points.
|
// Normalize a time ([0;1]) given two control points.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub(crate) fn normalize_time<T, V>(
|
pub(crate) fn normalize_time<T, V>(t: T, cp: &Key<T, V>, cp1: &Key<T, V>) -> T
|
||||||
t: T,
|
where
|
||||||
cp: &Key<T, V>,
|
T: Additive + Div<T, Output = T> + PartialEq,
|
||||||
cp1: &Key<T, V>
|
{
|
||||||
) -> T where T: Additive + Div<T, Output = T> + PartialEq {
|
|
||||||
assert!(cp1.t != cp.t, "overlapping keys");
|
assert!(cp1.t != cp.t, "overlapping keys");
|
||||||
(t - cp.t) / (cp1.t - cp.t)
|
(t - cp.t) / (cp1.t - cp.t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the lower control point corresponding to a given time.
|
// Find the lower control point corresponding to a given time.
|
||||||
fn search_lower_cp<T, V>(cps: &[Key<T, V>], t: T) -> Option<usize> where T: PartialOrd {
|
fn search_lower_cp<T, V>(cps: &[Key<T, V>], t: T) -> Option<usize>
|
||||||
|
where
|
||||||
|
T: PartialOrd,
|
||||||
|
{
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let len = cps.len();
|
let len = cps.len();
|
||||||
|
|
||||||
@ -223,7 +331,7 @@ fn search_lower_cp<T, V>(cps: &[Key<T, V>], t: T) -> Option<usize> where T: Part
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
let cp = &cps[i];
|
let cp = &cps[i];
|
||||||
let cp1 = &cps[i+1];
|
let cp1 = &cps[i + 1];
|
||||||
|
|
||||||
if t >= cp1.t {
|
if t >= cp1.t {
|
||||||
if i >= len - 2 {
|
if i >= len - 2 {
|
||||||
|
41
tests/mod.rs
41
tests/mod.rs
@ -1,7 +1,9 @@
|
|||||||
use splines::{Interpolation, Key, Spline};
|
use splines::{Interpolation, Key, Spline};
|
||||||
|
|
||||||
#[cfg(feature = "impl-cgmath")] use cgmath as cg;
|
#[cfg(feature = "cgmath")]
|
||||||
#[cfg(feature = "impl-nalgebra")] use nalgebra as na;
|
use cgmath as cg;
|
||||||
|
#[cfg(feature = "nalgebra")]
|
||||||
|
use nalgebra as na;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn step_interpolation_f32() {
|
fn step_interpolation_f32() {
|
||||||
@ -16,6 +18,8 @@ fn step_interpolation_f32() {
|
|||||||
assert_eq!(spline.sample(0.9), Some(10.));
|
assert_eq!(spline.sample(0.9), Some(10.));
|
||||||
assert_eq!(spline.sample(1.), None);
|
assert_eq!(spline.sample(1.), None);
|
||||||
assert_eq!(spline.clamped_sample(1.), Some(10.));
|
assert_eq!(spline.clamped_sample(1.), Some(10.));
|
||||||
|
assert_eq!(spline.sample_with_key(0.2), Some((10., &start, Some(&end))));
|
||||||
|
assert_eq!(spline.clamped_sample_with_key(1.), Some((10., &end, None)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -31,6 +35,8 @@ fn step_interpolation_f64() {
|
|||||||
assert_eq!(spline.sample(0.9), Some(10.));
|
assert_eq!(spline.sample(0.9), Some(10.));
|
||||||
assert_eq!(spline.sample(1.), None);
|
assert_eq!(spline.sample(1.), None);
|
||||||
assert_eq!(spline.clamped_sample(1.), Some(10.));
|
assert_eq!(spline.clamped_sample(1.), Some(10.));
|
||||||
|
assert_eq!(spline.sample_with_key(0.2), Some((10., &start, Some(&end))));
|
||||||
|
assert_eq!(spline.clamped_sample_with_key(1.), Some((10., &end, None)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -145,7 +151,34 @@ fn several_interpolations_several_keys() {
|
|||||||
assert_eq!(spline.clamped_sample(11.), Some(4.));
|
assert_eq!(spline.clamped_sample(11.), Some(4.));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "impl-cgmath")]
|
#[cfg(feature = "cgmath")]
|
||||||
|
#[test]
|
||||||
|
fn stroke_bezier_straight() {
|
||||||
|
use float_cmp::approx_eq;
|
||||||
|
|
||||||
|
let keys = vec![
|
||||||
|
Key::new(
|
||||||
|
0.0,
|
||||||
|
cg::Vector2::new(0., 1.),
|
||||||
|
Interpolation::StrokeBezier(cg::Vector2::new(0., 1.), cg::Vector2::new(0., 1.)),
|
||||||
|
),
|
||||||
|
Key::new(
|
||||||
|
5.0,
|
||||||
|
cg::Vector2::new(5., 1.),
|
||||||
|
Interpolation::StrokeBezier(cg::Vector2::new(5., 1.), cg::Vector2::new(5., 1.)),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
let spline = Spline::from_vec(keys);
|
||||||
|
|
||||||
|
assert!(approx_eq!(f32, spline.clamped_sample(0.0).unwrap().y, 1.));
|
||||||
|
assert!(approx_eq!(f32, spline.clamped_sample(1.0).unwrap().y, 1.));
|
||||||
|
assert!(approx_eq!(f32, spline.clamped_sample(2.0).unwrap().y, 1.));
|
||||||
|
assert!(approx_eq!(f32, spline.clamped_sample(3.0).unwrap().y, 1.));
|
||||||
|
assert!(approx_eq!(f32, spline.clamped_sample(4.0).unwrap().y, 1.));
|
||||||
|
assert!(approx_eq!(f32, spline.clamped_sample(5.0).unwrap().y, 1.));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "cgmath")]
|
||||||
#[test]
|
#[test]
|
||||||
fn cgmath_vector_interpolation() {
|
fn cgmath_vector_interpolation() {
|
||||||
use splines::Interpolate;
|
use splines::Interpolate;
|
||||||
@ -159,7 +192,7 @@ fn cgmath_vector_interpolation() {
|
|||||||
assert_eq!(Interpolate::lerp(start, end, 0.5), mid);
|
assert_eq!(Interpolate::lerp(start, end, 0.5), mid);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "impl-nalgebra")]
|
#[cfg(feature = "nalgebra")]
|
||||||
#[test]
|
#[test]
|
||||||
fn nalgebra_vector_interpolation() {
|
fn nalgebra_vector_interpolation() {
|
||||||
use splines::Interpolate;
|
use splines::Interpolate;
|
||||||
|
Reference in New Issue
Block a user