| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
curvature :: RealFloat n |
| 109 |
=> Segment Closed V2 n |
| 110 |
-> n |
| 111 |
-> PosInf n |
| 112 |
-- infinite curvature or zero radius of curvature. |
| 113 |
curvature s = toPosInf . over _y sqrt . curvaturePair s |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
squaredCurvature :: RealFloat n => Segment Closed V2 n -> n -> PosInf n |
| 119 |
squaredCurvature s = toPosInf . over _x (join (*)) . curvaturePair s |
| 120 |
|
| 121 |
|
| 122 |
radiusOfCurvature :: RealFloat n |
| 123 |
=> Segment Closed V2 n |
| 124 |
-> n |
| 125 |
-> PosInf n |
| 126 |
-- infinite radius of curvature or zero curvature. |
| 127 |
radiusOfCurvature s = toPosInf . (\(V2 p q) -> V2 (sqrt q) p) . curvaturePair s |
| 128 |
|
| 129 |
|
| 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 |