Debian Codegraph @

src/Diagrams/TwoD/Arc.hs

raw symbols SCIP
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 -- | @bezierFromSweepQ1 s@ constructs a 'Cubic' segment that starts in
59 -- the positive y direction and sweeps counterclockwise through an
60 -- angle @s@. The approximation is only valid for angles in the
61 -- first quadrant.
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 -- | @bezierFromSweep s@ constructs a series of 'Cubic' segments that
69 -- start in the positive y direction and sweep counter clockwise
70 -- through the angle @s@. If @s@ is negative, it will start in the
71 -- negative y direction and sweep clockwise. When @s@ is less than
72 -- 0.0001 the empty list results. If the sweep is greater than @fullTurn@
73 -- later segments will overlap earlier segments.
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 -- | Given a start direction @d@ and a sweep angle @s@, @'arcT' d s@
103 -- is the 'Trail' of a radius one arc starting at @d@ and sweeping out
104 -- the angle @s@ counterclockwise (for positive s). The resulting
105 -- @Trail@ is allowed to wrap around and overlap itself.
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 -- | Given a start direction @d@ and a sweep angle @s@, @'arc' d s@ is the
112 -- path of a radius one arc starting at @d@ and sweeping out the angle
113 -- @s@ counterclockwise (for positive s). The resulting
114 -- @Trail@ is allowed to wrap around and overlap itself.
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 -- | Given a radus @r@, a start direction @d@ and an angle @s@,
119 -- @'arc'' r d s@ is the path of a radius @(abs r)@ arc starting at
120 -- @d@ and sweeping out the angle @s@ counterclockwise (for positive
121 -- s). The origin of the arc is its center.
122 --
123 -- <<diagrams/src_Diagrams_TwoD_Arc_arc'Ex.svg#diagram=arc'Ex&width=300>>
124 --
125 -- > arc'Ex = mconcat [ arc' r xDir (1/4 @@ turn) | r <- [0.5,-1,1.5] ]
126 -- > # centerXY # pad 1.1
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 -- | Given a start direction @s@ and end direction @e@, @arcCCW s e@ is the
138 -- path of a radius one arc counterclockwise between the two directions.
139 -- The origin of the arc is its center.
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 -- | Like 'arcAngleCCW' but clockwise.
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 -- | Create a circular wedge of the given radius, beginning at the
151 -- given direction and extending through the given angle.
152 --
153 -- <<diagrams/src_Diagrams_TwoD_Arc_wedgeEx.svg#diagram=wedgeEx&width=400>>
154 --
155 -- > wedgeEx = hcat' (with & sep .~ 0.5)
156 -- > [ wedge 1 xDir (1/4 @@ turn)
157 -- > , wedge 1 (rotate (7/30 @@ turn) xDir) (4/30 @@ turn)
158 -- > , wedge 1 (rotate (1/8 @@ turn) xDir) (3/4 @@ turn)
159 -- > ]
160 -- > # fc blue
161 -- > # centerXY # pad 1.1
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 -- | @arcBetween p q height@ creates an arc beginning at @p@ and
169 -- ending at @q@, with its midpoint at a distance of @abs height@
170 -- away from the straight line from @p@ to @q@. A positive value of
171 -- @height@ results in an arc to the left of the line from @p@ to
172 -- @q@; a negative value yields one to the right.
173 --
174 -- <<diagrams/src_Diagrams_TwoD_Arc_arcBetweenEx.svg#diagram=arcBetweenEx&width=300>>
175 --
176 -- > arcBetweenEx = mconcat
177 -- > [ arcBetween origin (p2 (2,1)) ht | ht <- [-0.2, -0.1 .. 0.2] ]
178 -- > # centerXY # pad 1.1
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 -- | Create an annular wedge of the given radii, beginning at the
201 -- first direction and extending through the given sweep angle.
202 -- The radius of the outer circle is given first.
203 --
204 -- <<diagrams/src_Diagrams_TwoD_Arc_annularWedgeEx.svg#diagram=annularWedgeEx&width=400>>
205 --
206 -- > annularWedgeEx = hsep 0.50
207 -- > [ annularWedge 1 0.5 xDir (1/4 @@ turn)
208 -- > , annularWedge 1 0.3 (rotate (7/30 @@ turn) xDir) (4/30 @@ turn)
209 -- > , annularWedge 1 0.7 (rotate (1/8 @@ turn) xDir) (3/4 @@ turn)
210 -- > ]
211 -- > # fc blue
212 -- > # centerXY # pad 1.1
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