Debian Codegraph @

src/Diagrams/CubicSpline/Boehm.hs

raw symbols SCIP
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 -- | @affineCombo a b t x y@ computes an affine combination of x and y
42 -- which lies at parameter t, if x has parameter a and y has parameter b.
43 -- The usual @lerp@ arises by giving x parameter 0 and y parameter 1.
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 -- | @windows k xs@ yields all the length-@k@ windows from @xs@, e.g.
48 -- @windows 3 [a,b,c,d,e] == [[a,b,c], [b,c,d], [c,d,e]]@.
49 windows :: Int -> [a] -> [[a]]
50 windows k = takeWhile ((==k) . length) . map (take k) . tails
51
52 -- | @extend k xs@ extends @xs@ on both ends by prepending @k@ copies
53 -- of its head and appending @k@ copies of its last element. For example,
54 -- @extend 2 [1..5] == [1,1,1,2,3,4,5,5,5]@.
55 extend :: Int -> [a] -> [a]
56 extend k xs = replicate k (head xs) ++ xs ++ replicate k (last xs)
57
58 -- | A "polar point" is a point along with three knot values.
59 -- We consider the "blossom" of a cubic spline, a 3-ary symmetric
60 -- polynomial; a polar point consists of 3 values paired with the
61 -- output of the blossom at those input values. Blossoms have nice
62 -- affine properties so this makes it easy to keep track of how
63 -- points may be combined to yield other points of interest.
64 --
65 -- Invariant: knot values are in nondecreasing order.
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 -- | Precondition: the knots of the two polar points overlap, like abc
72 -- and bcd. The @Int@ should be 0 or 1, indicating which knot to
73 -- replicate (0 means to replicate b, yielding bbc, 1 means to
74 -- replicate c, yielding bcc).
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 -- | Convert a uniform cubic B-spline to a sequence of cubic beziers.
86 -- (/Uniform/ refers to the fact that the knots are assumed to be
87 -- evenly spaced, with no duplicates.) The knots at the end are
88 -- replicated so the cubic spline begins and ends at the first and
89 -- last control points, tangent to the line from the end control
90 -- point to the next.
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 -- | Generate a uniform cubic B-spline from the given control points.
130 -- The spline starts and ends at the first and last control points,
131 -- and is tangent to the line to the second(-to-last) control point.
132 -- It does not necessarily pass through any of the other control
133 -- points.
134 --
135 -- <<diagrams/src_Diagrams_CubicSpline_Boehm_bsplineEx.svg#diagram=bsplineEx&width=300>>
136 --
137 -- > pts = map p2 [(0,0), (2,3), (5,-2), (-4,1), (0,3)]
138 -- > spot = circle 0.2 # fc blue # lw none
139 -- > bsplineEx = mconcat
140 -- > [ position (zip pts (repeat spot))
141 -- > , bspline pts
142 -- > ]
143 -- > # frame 0.5
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