diff --git a/Example App/SplineTestApp/SplineTestApp/DetailViewController.swift b/Example App/SplineTestApp/SplineTestApp/DetailViewController.swift index 78e9d73..a3a43bd 100644 --- a/Example App/SplineTestApp/SplineTestApp/DetailViewController.swift +++ b/Example App/SplineTestApp/SplineTestApp/DetailViewController.swift @@ -90,8 +90,8 @@ class DetailViewController: UIViewController { let function: (Double) -> Double if arguments.count > 1 { let spline = Spline( - values: values, arguments: arguments, + values: values, boundaryCondition: chosenOption == 0 ? .smooth : .fixedTangentials(dAtStart: 0, dAtEnd: 0) ) diff --git a/README.md b/README.md index 80c5767..994af73 100644 --- a/README.md +++ b/README.md @@ -71,8 +71,31 @@ Import the package in your *.swift file: ```swift import SwiftSplines ``` +### Example A : Dampening a signal -Make sure your data values conforom to +If you want a function that dampens a signal in the range of [0,3], like +``` +f(0.1) = 0.3, +f(0.4) = 0.6, +f(1) = 1, +f(2) = 1.6 +f(2.5) = 2 +f'(0.1) = 0 +f'(2.5) = 0 +``` +you could define: +```swift +// private let dampingFunction: (Double) -> Double +private let dampingFunction = Spline( + arguments: [0.1, 0.4, 1, 2, 2.5], + values: [0.3, 0.6, 1, 1.6, 2], + boundaryCondition: .fixedTangentials(dAtStart: 0, dAtEnd: 0.0) +).f +``` + +### Example B: Connect custom vector data + +Make sure your data values conform to ```swift public protocol DataPoint { associatedtype Scalar: FloatingPoint & DoubleConvertable @@ -83,16 +106,18 @@ public protocol DataPoint { static func * (left: Scalar, right: Self) -> Self static func + (left: Self, right: Self) -> Self } + +extension MyVector: DataPoint { ... } ``` (`Float`, `CGFloat`, `Double`, `CGPoint` conform to DataPoint as part of the package) Then you can create your spline functions by: ```swift -let values: [CGPoint] = ... +let values: [MyVector] = ... let spline = Spline(values: values) -func calculate(t: Double) -> CGPoint { +func calculate(t: Double) -> MyVector { return spline.f(t: t) } ``` diff --git a/Sources/SwiftSplines/Spline.swift b/Sources/SwiftSplines/Spline.swift index c9928b9..4cf7127 100644 --- a/Sources/SwiftSplines/Spline.swift +++ b/Sources/SwiftSplines/Spline.swift @@ -40,8 +40,8 @@ public struct Spline { /// by default they are 0 ... n /// - boundaryCondition: the chosen `BoundaryCondition` public init( - values: [P], arguments: [P.Scalar]? = nil, + values: [P], boundaryCondition: BoundaryCondition = .smooth ) { if let arguments = arguments, arguments.count != values.count { @@ -50,8 +50,8 @@ public struct Spline { let args = arguments ?? values.enumerated().map({ P.Scalar($0.0) }) self.init( - values: values, arguments: args, + values: values, derivatives: Self.computeDerivatives(from: values, boundaryCondition: boundaryCondition), boundaryCondition: boundaryCondition ) @@ -62,8 +62,8 @@ public struct Spline { /// - Parameter points: the control points /// - Parameter derivatives: f'(t) at the control points public init( - values: [P], arguments: [P.Scalar], + values: [P], derivatives: [P], boundaryCondition: BoundaryCondition = .smooth ) {