| 1 |
{-# LANGUAGE ConstraintKinds #-} |
| 2 |
{-# LANGUAGE FlexibleContexts #-} |
| 3 |
{-# LANGUAGE TypeFamilies #-} |
| 4 |
{-# LANGUAGE TypeOperators #-} |
| 5 |
{-# LANGUAGE ViewPatterns #-} |
| 6 |
|
| 7 |
{-# OPTIONS_GHC -fno-warn-unused-imports #-} |
| 8 |
-- for Data.Semigroup |
| 9 |
|
| 10 |
----------------------------------------------------------------------------- |
| 11 |
-- | |
| 12 |
-- Module : Diagrams.TwoD.Arc |
| 13 |
-- Copyright : (c) 2011 diagrams-lib team (see LICENSE) |
| 14 |
-- License : BSD-style (see LICENSE) |
| 15 |
-- Maintainer : [email protected] |
| 16 |
-- |
| 17 |
-- Two-dimensional arcs, approximated by cubic bezier curves. |
| 18 |
-- |
| 19 |
----------------------------------------------------------------------------- |
| 20 |
|
| 21 |
module Diagrams.TwoD.Arc |
| 22 |
( arc |
| 23 |
, arc' |
| 24 |
, arcT |
| 25 |
|
| 26 |
, arcCCW |
| 27 |
, arcCW |
| 28 |
|
| 29 |
, bezierFromSweep |
| 30 |
|
| 31 |
, wedge |
| 32 |
, arcBetween |
| 33 |
, annularWedge |
| 34 |
) where |
| 35 |
|
| 36 |
import Diagrams.Angle |
| 37 |
import Diagrams.Core |
| 38 |
import Diagrams.Direction |
| 39 |
import Diagrams.Located (at) |
| 40 |
import Diagrams.Segment |
| 41 |
import Diagrams.Trail |
| 42 |
import Diagrams.TrailLike |
| 43 |
import Diagrams.TwoD.Transform |
| 44 |
import Diagrams.TwoD.Types |
| 45 |
import Diagrams.TwoD.Vector (e, unitX, unitY, unit_Y) |
| 46 |
import Diagrams.Util (( # )) |
| 47 |
|
| 48 |
import Control.Lens ((&), (<>~), (^.)) |
| 49 |
import Data.Semigroup |
| 50 |
|
| 51 |
import Linear.Affine |
| 52 |
import Linear.Metric |
| 53 |
import Linear.Vector |
| 54 |
|
| 55 |
-- For details of this approximation see: |
| 56 |
-- http://www.tinaja.com/glib/bezcirc2.pdf |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
bezierFromSweepQ1 :: Floating n => Angle n -> Segment Closed V2 n |
| 63 |
bezierFromSweepQ1 s = mapSegmentVectors (^-^ unitX) . rotate (s ^/ 2) $ bezier3 c2 c1 p0 |
| 64 |
where p0@(V2 x y) = e (s ^/ 2) |
| 65 |
c1 = V2 ((4-x)/3) ((1-x)*(3-x)/(3*y)) |
| 66 |
c2 = reflectY c1 |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
bezierFromSweep :: OrderedField n => Angle n -> [Segment Closed V2 n] |
| 75 |
bezierFromSweep s |
| 76 |
| s < zero = fmap reflectY . bezierFromSweep $ negated s |
| 77 |
| s < 0.0001 @@ rad = [] |
| 78 |
| s < fullTurn^/4 = [bezierFromSweepQ1 s] |
| 79 |
| otherwise = bezierFromSweepQ1 (fullTurn^/4) |
| 80 |
: map (rotateBy (1/4)) (bezierFromSweep (max (s ^-^ fullTurn^/4) zero)) |
| 81 |
|
| 82 |
{- |
| 83 |
~~~~ Note [segment spacing] |
| 84 |
|
| 85 |
There are a few obvious options for segment spacing: |
| 86 |
A. Evenly space segments each with sweep less than or equal |
| 87 |
to one quarter of a circle. This has the benefit of a better approximation |
| 88 |
(at least I think it is better). |
| 89 |
B. Use as much of the sweep in quarter-circle sized segments and one for |
| 90 |
the remainder. This potentially gives more opportunities for |
| 91 |
consistency (though not as much as option C) as the error in |
| 92 |
approximation would more often match the error from another arc |
| 93 |
in the diagram. |
| 94 |
C. Like option B but fixing the orientation and having a remnant at |
| 95 |
the beginning and the end. |
| 96 |
|
| 97 |
Option B is implemented and this note is for posterity if anyone comes |
| 98 |
across a situation with large enough arcs that they can actually see |
| 99 |
the approximation error. |
| 100 |
-} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
arcT :: OrderedField n => Direction V2 n -> Angle n -> Trail V2 n |
| 107 |
arcT start sweep = trailFromSegments bs |
| 108 |
where |
| 109 |
bs = map (rotateTo start) . bezierFromSweep $ sweep |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
arc :: (InSpace V2 n t, OrderedField n, TrailLike t) => Direction V2 n -> Angle n -> t |
| 116 |
arc start sweep = trailLike $ arcT start sweep `at` P (fromDirection start) |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
arc' :: (InSpace V2 n t, OrderedField n, TrailLike t) => n -> Direction V2 n -> Angle n -> t |
| 128 |
arc' (abs -> r) start sweep = trailLike $ scale r ts `at` P (r *^ fromDirection start) |
| 129 |
where ts = arcT start sweep |
| 130 |
|
| 131 |
arcCCWT :: RealFloat n => Direction V2 n -> Direction V2 n -> Trail V2 n |
| 132 |
arcCCWT start end = trailFromSegments bs |
| 133 |
where |
| 134 |
bs = map (rotateTo start) . bezierFromSweep $ sweep |
| 135 |
sweep = normalizeAngle $ end ^. _theta ^-^ start ^. _theta |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
arcCCW :: (InSpace V2 n t, RealFloat n, TrailLike t) => Direction V2 n -> Direction V2 n -> t |
| 141 |
arcCCW start end = trailLike $ arcCCWT start end `at` P (fromDirection start) |
| 142 |
|
| 143 |
|
| 144 |
arcCW :: (InSpace V2 n t, RealFloat n, TrailLike t) => Direction V2 n -> Direction V2 n -> t |
| 145 |
arcCW start end = trailLike $ |
| 146 |
-- flipped arguments to get the path we want |
| 147 |
-- then reverse the trail to get the cw direction. |
| 148 |
reverseTrail (arcCCWT end start) `at` P (fromDirection start) |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
wedge :: (InSpace V2 n t, OrderedField n, TrailLike t) => n -> Direction V2 n -> Angle n -> t |
| 163 |
wedge r d s = trailLike . (`at` origin) . glueTrail . wrapLine |
| 164 |
$ fromOffsets [r *^ fromDirection d] |
| 165 |
<> arc d s # scale r |
| 166 |
<> fromOffsets [r *^ negated (rotate s $ fromDirection d)] |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
arcBetween :: (TrailLike t, V t ~ V2, N t ~ n, RealFloat n) => Point V2 n -> Point V2 n -> n -> t |
| 180 |
arcBetween p q ht = trailLike (a # rotate (v^._theta) # moveTo p) |
| 181 |
where |
| 182 |
h = abs ht |
| 183 |
isStraight = h < 0.00001 |
| 184 |
v = q .-. p |
| 185 |
d = norm (q .-. p) |
| 186 |
th = acosA ((d*d - 4*h*h)/(d*d + 4*h*h)) |
| 187 |
r = d/(2*sinA th) |
| 188 |
mid | ht >= 0 = direction unitY |
| 189 |
| otherwise = direction unit_Y |
| 190 |
st = mid & _theta <>~ negated th |
| 191 |
a | isStraight |
| 192 |
= fromOffsets [d *^ unitX] |
| 193 |
| otherwise |
| 194 |
= arc st (2 *^ th) |
| 195 |
# scale r |
| 196 |
# translateY ((if ht > 0 then negate else id) (r - h)) |
| 197 |
# translateX (d/2) |
| 198 |
# (if ht > 0 then reverseLocTrail else id) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
annularWedge :: (TrailLike t, V t ~ V2, N t ~ n, RealFloat n) => |
| 214 |
n -> n -> Direction V2 n -> Angle n -> t |
| 215 |
annularWedge r1' r2' d1 s = trailLike . (`at` o) . glueTrail . wrapLine |
| 216 |
$ fromOffsets [(r1' - r2') *^ fromDirection d1] |
| 217 |
<> arc d1 s # scale r1' |
| 218 |
<> fromOffsets [(r1' - r2') *^ negated (fromDirection d2)] |
| 219 |
<> arc d2 (negated s) # scale r2' |
| 220 |
where o = origin # translate (r2' *^ fromDirection d1) |
| 221 |
d2 = d1 & _theta <>~ s |
| 222 |
|