Debian Codegraph @

src/Diagrams/TwoD/Curvature.hs

raw symbols SCIP
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE GADTs #-}
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module : Diagrams.TwoD.Curvature
6 -- Copyright : (c) 2013 diagrams-lib team (see LICENSE)
7 -- License : BSD-style (see LICENSE)
8 -- Maintainer : [email protected]
9 --
10 -- Compute curvature for segments in two dimensions.
11 --
12 -----------------------------------------------------------------------------
13
14 module Diagrams.TwoD.Curvature
15 (
16 curvature
17 , radiusOfCurvature
18 , squaredCurvature
19 , squaredRadiusOfCurvature
20 ) where
21
22 import Control.Lens (over)
23 import Control.Monad
24 import Data.Monoid.Inf
25
26 import Diagrams.Segment
27 import Diagrams.Tangent
28 import Diagrams.TwoD.Types
29
30 import Linear.Vector
31
32 -- | Curvature measures how curved the segment is at a point. One intuition
33 -- for the concept is how much you would turn the wheel when driving a car
34 -- along the curve. When the wheel is held straight there is zero curvature.
35 -- When turning a corner to the left we will have positive curvature. When
36 -- turning to the right we will have negative curvature.
37 --
38 -- Another way to measure this idea is to find the largest circle that we can
39 -- push up against the curve and have it touch (locally) at exactly the point
40 -- and not cross the curve. This is a tangent circle. The radius of that
41 -- circle is the \"Radius of Curvature\" and it is the reciprocal of curvature.
42 -- Note that if the circle is on the \"left\" of the curve, we have a positive
43 -- radius, and if it is to the right we have a negative radius. Straight
44 -- segments have an infinite radius which leads us to our representation. We
45 -- result in a pair of numerator and denominator so we can include infinity and
46 -- zero for both the radius and the curvature.
47 --
48 --
49 -- Lets consider the following curve:
50 --
51 -- <<diagrams/src_Diagrams_TwoD_Curvature_diagramA.svg#diagram=diagramA&height=200&width=400>>
52 --
53 -- The curve starts with positive curvature,
54 --
55 -- <<diagrams/src_Diagrams_TwoD_Curvature_diagramPos.svg#diagram=diagramPos&height=200&width=400>>
56 --
57 -- approaches zero curvature
58 --
59 -- <<diagrams/src_Diagrams_TwoD_Curvature_diagramZero.svg#diagram=diagramZero&height=200&width=400>>
60 --
61 -- then has negative curvature
62 --
63 -- <<diagrams/src_Diagrams_TwoD_Curvature_diagramNeg.svg#diagram=diagramNeg&height=200&width=400>>
64 --
65 -- > {-# LANGUAGE GADTs #-}
66 -- >
67 -- > import Diagrams.TwoD.Curvature
68 -- > import Data.Monoid.Inf
69 -- > import Diagrams.Coordinates
70 -- >
71 -- > segmentA :: Segment Closed V2 Double
72 -- > segmentA = Cubic (12 ^& 0) (8 ^& 10) (OffsetClosed (20 ^& 8))
73 -- >
74 -- > curveA = lw thick . strokeP . fromSegments $ [segmentA]
75 -- >
76 -- > diagramA = pad 1.1 . centerXY $ curveA
77 -- >
78 -- > diagramPos = diagramWithRadius 0.2
79 -- >
80 -- > diagramZero = diagramWithRadius 0.45
81 -- >
82 -- > diagramNeg = diagramWithRadius 0.8
83 -- >
84 -- > diagramWithRadius t = pad 1.1 . centerXY
85 -- > $ curveA
86 -- > <> showCurvature segmentA t
87 -- > # withEnvelope (curveA :: D V2 Double)
88 -- > # lc red
89 -- >
90 -- > showCurvature :: Segment Closed V2 Double -> Double -> Diagram SVG
91 -- > showCurvature bez@(Cubic b c (OffsetClosed d)) t
92 -- > | v == (0,0) = mempty
93 -- > | otherwise = go (radiusOfCurvature bez t)
94 -- > where
95 -- > v@(x,y) = unr2 $ firstDerivative b c d t
96 -- > vp = (-y) ^& x
97 -- >
98 -- > firstDerivative b c d t = let tt = t*t in (3*(3*tt-4*t+1))*^b + (3*(2-3*t)*t)*^c + (3*tt)*^d
99 -- >
100 -- > go Infinity = mempty
101 -- > go (Finite r) = (circle (abs r) # translate vpr
102 -- > <> strokeP (origin ~~ (origin .+^ vpr)))
103 -- > # moveTo (origin .+^ atParam bez t)
104 -- > where
105 -- > vpr = signorm vp ^* r
106 -- >
107 --
108 curvature :: RealFloat n
109 => Segment Closed V2 n -- ^ Segment to measure on.
110 -> n -- ^ Parameter to measure at.
111 -> PosInf n -- ^ Result is a @PosInf@ value where @PosInfty@ represents
112 -- infinite curvature or zero radius of curvature.
113 curvature s = toPosInf . over _y sqrt . curvaturePair s
114
115 -- | With @squaredCurvature@ we can compute values in spaces that do not support
116 -- 'sqrt' and it is just as useful for relative ordering of curvatures or looking
117 -- for zeros.
118 squaredCurvature :: RealFloat n => Segment Closed V2 n -> n -> PosInf n
119 squaredCurvature s = toPosInf . over _x (join (*)) . curvaturePair s
120
121 -- | Reciprocal of @curvature@.
122 radiusOfCurvature :: RealFloat n
123 => Segment Closed V2 n -- ^ Segment to measure on.
124 -> n -- ^ Parameter to measure at.
125 -> PosInf n -- ^ Result is a @PosInf@ value where @PosInfty@ represents
126 -- infinite radius of curvature or zero curvature.
127 radiusOfCurvature s = toPosInf . (\(V2 p q) -> V2 (sqrt q) p) . curvaturePair s
128
129 -- | Reciprocal of @squaredCurvature@
130 squaredRadiusOfCurvature :: RealFloat n => Segment Closed V2 n -> n -> PosInf n
131 squaredRadiusOfCurvature s = toPosInf . (\(V2 p q) -> (V2 q (p * p))) . curvaturePair s
132
133 -- Package up problematic values with the appropriate infinity.
134 toPosInf :: RealFloat a => V2 a -> PosInf a
135 toPosInf (V2 _ 0) = Infinity
136 toPosInf (V2 p q)
137 | isInfinite r || isNaN r = Infinity
138 | otherwise = Finite r
139 where r = p / q
140
141 -- Internal function that is not quite curvature or squaredCurvature but lets
142 -- us get there by either taking the square root of the numerator or squaring
143 -- the denominator respectively.
144 curvaturePair :: Num n
145 => Segment Closed V2 n -> n -> V2 n
146 curvaturePair (Linear _) _ = V2 0 1 -- Linear segments always have zero curvature (infinite radius).
147 curvaturePair seg@(Cubic b c (OffsetClosed d)) t
148 = V2 (x'*y'' - y'*x'') ((x'*x' + y'*y')^(3 :: Int))
149 where
150 (V2 x' y' ) = seg `tangentAtParam` t
151 (V2 x'' y'') = secondDerivative
152 secondDerivative = (6*(3*t-2))*^b ^+^ (6-18*t)*^c ^+^ (6*t)*^d
153
154 -- TODO: We should be able to generalize this to higher dimensions. See
155 -- <http://en.wikipedia.org/wiki/Curvature>
156 --
157 -- TODO: I'm not sure what the best way to generalize squaredCurvature to other spaces is.
158
159 -- curvaturePair :: (Num t, Num (Scalar t), VectorSpace t)
160 -- => Segment Closed (t, t) -> Scalar t -> (t, t)
161 -- curvaturePair (Linear _) _ = (0,1) -- Linear segments always have zero curvature (infinite radius).
162 -- curvaturePair seg@(Cubic b c (OffsetClosed d)) t = ((x'*y'' - y'*x''), (x'*x' + y'*y')^(3 :: Integer))
163 -- where
164 -- (x' ,y' ) = seg `tangentAtParam` t
165 -- (x'',y'') = secondDerivative
166 -- secondDerivative = (6*(3*t-2))*^b ^+^ (6-18*t)*^c ^+^ (6*t)*^d