| 1 |
{-# LANGUAGE FlexibleContexts #-} |
| 2 |
{-# LANGUAGE TypeFamilies #-} |
| 3 |
{-# LANGUAGE TypeOperators #-} |
| 4 |
|
| 5 |
----------------------------------------------------------------------------- |
| 6 |
-- | |
| 7 |
-- Module : Diagrams.CubicSpline.Boehm |
| 8 |
-- Copyright : (c) 2015 diagrams-lib team (see LICENSE) |
| 9 |
-- License : BSD-style (see LICENSE) |
| 10 |
-- Maintainer : [email protected] |
| 11 |
-- |
| 12 |
-- Boehm's algorithm for converting a cubic B-spline into a sequence |
| 13 |
-- of cubic Bezier curves. |
| 14 |
-- |
| 15 |
-- See |
| 16 |
-- |
| 17 |
-- * Thomas W. Sederberg, /An Introduction to B-Spline Curves/, |
| 18 |
-- <http://web.archive.org/web/20120227050519/http://tom.cs.byu.edu/~455/bs.pdf> |
| 19 |
-- |
| 20 |
-- * Lyle Ramshaw, /Blossoming: A Connect-the-Dots Approach to Splines/, |
| 21 |
-- <http://www.hpl.hp.com/techreports/Compaq-DEC/SRC-RR-19.pdf> |
| 22 |
-- |
| 23 |
----------------------------------------------------------------------------- |
| 24 |
module Diagrams.CubicSpline.Boehm |
| 25 |
( BSpline |
| 26 |
, bsplineToBeziers |
| 27 |
, bspline |
| 28 |
) where |
| 29 |
|
| 30 |
import Data.List (sort, tails) |
| 31 |
import Diagrams.Core (N, Point, V, origin) |
| 32 |
import Diagrams.Located (at, loc, unLoc) |
| 33 |
import Diagrams.Segment (FixedSegment (..), fromFixedSeg) |
| 34 |
import Diagrams.TrailLike (TrailLike, fromLocSegments) |
| 35 |
import Diagrams.Util (iterateN) |
| 36 |
import Linear.Vector (Additive) |
| 37 |
import Linear.Vector.Compat (lerp) |
| 38 |
|
| 39 |
type BSpline v n = [Point v n] |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
affineCombo :: (Additive f, Fractional a) => a -> a -> a -> f a -> f a -> f a |
| 45 |
affineCombo a b t = lerp ((t-a)/(b-a)) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
windows :: Int -> [a] -> [[a]] |
| 50 |
windows k = takeWhile ((==k) . length) . map (take k) . tails |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
extend :: Int -> [a] -> [a] |
| 56 |
extend k xs = replicate k (head xs) ++ xs ++ replicate k (last xs) |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
data PolarPt v n = PP { unPP :: Point v n, _knots :: [n] } |
| 67 |
|
| 68 |
mkPolarPt :: Ord n => Point v n -> [n] -> PolarPt v n |
| 69 |
mkPolarPt pt kts = PP pt (sort kts) |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
combine |
| 76 |
:: (Additive v, Fractional n, Ord n) |
| 77 |
=> Int -> PolarPt v n -> PolarPt v n -> PolarPt v n |
| 78 |
combine k (PP pt1 kts1) (PP pt2 kts2) |
| 79 |
= mkPolarPt |
| 80 |
(affineCombo (head kts1) (last kts2) newKt pt1 pt2) |
| 81 |
(newKt : drop 1 kts1) |
| 82 |
where |
| 83 |
newKt = kts2 !! k |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
bsplineToBeziers |
| 92 |
:: (Additive v, Fractional n, Num n, Ord n) |
| 93 |
=> BSpline v n |
| 94 |
-> [FixedSegment v n] |
| 95 |
bsplineToBeziers controls = beziers |
| 96 |
where |
| 97 |
n = length controls |
| 98 |
numKnots = n + 2 |
| 99 |
knots = iterateN numKnots (+1/(fromIntegral numKnots - 1)) 0 |
| 100 |
|
| 101 |
-- The control points are P(a,b,c), P(b,c,d), P(c,d,e), and so on. |
| 102 |
controls' = zipWith mkPolarPt (extend 2 controls) (windows 3 $ extend 2 knots) |
| 103 |
|
| 104 |
-- The bezier internal control points are affine combinations of |
| 105 |
-- the spline control points. |
| 106 |
bezierControls = map combineC (windows 2 controls') |
| 107 |
combineC [pabc, pbcd] = (combine 0 pabc pbcd, combine 1 pabc pbcd) |
| 108 |
combineC _ = error "combineC must be called on a list of length 2" |
| 109 |
|
| 110 |
-- The bezier end points are affine combinations of the bezier |
| 111 |
-- control points. |
| 112 |
bezierEnds = map combineE (windows 2 bezierControls) |
| 113 |
combineE [(_,pabb),(pbbc,_)] = combine 0 pabb pbbc |
| 114 |
combineE _ = error "combineE must be called on a list of length 2" |
| 115 |
|
| 116 |
-- Finally, we actually put together the generated bezier segments. |
| 117 |
beziers = zipWith mkBezier (drop 1 bezierControls) (windows 2 bezierEnds) |
| 118 |
where |
| 119 |
mkBezier (paab,pabb) [paaa,pbbb] |
| 120 |
= FCubic (unPP paaa) (unPP paab) (unPP pabb) (unPP pbbb) |
| 121 |
mkBezier _ _ = error "mkBezier must be called on a list of length 2" |
| 122 |
|
| 123 |
-- Note that the above algorithm works in any dimension but is |
| 124 |
-- very specific to *cubic* splines. This can of course be |
| 125 |
-- generalized to higher degree splines but keeping track of |
| 126 |
-- everything gets a bit more complicated; to be honest I am not |
| 127 |
-- quite sure how to do it. |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
bspline :: (TrailLike t, V t ~ v, N t ~ n) => BSpline v n -> t |
| 146 |
bspline = fromLocSegments . fixup . map fromFixedSeg . bsplineToBeziers |
| 147 |
where |
| 148 |
fixup [] = [] `at` origin |
| 149 |
fixup (b1:rest) = (unLoc b1 : map unLoc rest) `at` loc b1 |