| 1 |
{-# LANGUAGE ConstraintKinds #-} |
| 2 |
{-# LANGUAGE FlexibleContexts #-} |
| 3 |
{-# LANGUAGE GADTs #-} |
| 4 |
{-# LANGUAGE StandaloneDeriving #-} |
| 5 |
{-# LANGUAGE TemplateHaskell #-} |
| 6 |
{-# LANGUAGE TypeFamilies #-} |
| 7 |
{-# LANGUAGE TypeOperators #-} |
| 8 |
{-# LANGUAGE UndecidableInstances #-} |
| 9 |
{-# LANGUAGE ViewPatterns #-} |
| 10 |
|
| 11 |
{-# OPTIONS_GHC -fno-warn-unused-imports #-} |
| 12 |
-- for Data.Semigroup |
| 13 |
|
| 14 |
----------------------------------------------------------------------------- |
| 15 |
-- | |
| 16 |
-- Module : Diagrams.TwoD.Offset |
| 17 |
-- Copyright : (c) 2013 diagrams-lib team (see LICENSE) |
| 18 |
-- License : BSD-style (see LICENSE) |
| 19 |
-- Maintainer : [email protected] |
| 20 |
-- |
| 21 |
-- Compute offsets to segments in two dimensions. More details can be |
| 22 |
-- found in the manual at |
| 23 |
-- <https://diagrams.github.io/doc/manual.html#offsets-of-segments-trails-and-paths>. |
| 24 |
-- |
| 25 |
----------------------------------------------------------------------------- |
| 26 |
module Diagrams.TwoD.Offset |
| 27 |
( |
| 28 |
-- * Offsets |
| 29 |
|
| 30 |
offsetSegment |
| 31 |
|
| 32 |
, OffsetOpts(..), offsetJoin, offsetMiterLimit, offsetEpsilon |
| 33 |
, offsetTrail |
| 34 |
, offsetTrail' |
| 35 |
, offsetPath |
| 36 |
, offsetPath' |
| 37 |
|
| 38 |
-- * Expansions |
| 39 |
|
| 40 |
, ExpandOpts(..), expandJoin, expandMiterLimit, expandCap, expandEpsilon |
| 41 |
, expandTrail |
| 42 |
, expandTrail' |
| 43 |
, expandPath |
| 44 |
, expandPath' |
| 45 |
|
| 46 |
) where |
| 47 |
|
| 48 |
import Control.Applicative |
| 49 |
import Control.Lens hiding (at) |
| 50 |
import Prelude |
| 51 |
|
| 52 |
import Data.Maybe (catMaybes) |
| 53 |
import Data.Monoid |
| 54 |
import Data.Monoid.Inf |
| 55 |
|
| 56 |
import Data.Default |
| 57 |
|
| 58 |
import Diagrams.Core |
| 59 |
|
| 60 |
import Diagrams.Attributes |
| 61 |
import Diagrams.Direction |
| 62 |
import Diagrams.Located |
| 63 |
import Diagrams.Parametric |
| 64 |
import Diagrams.Path |
| 65 |
import Diagrams.Segment |
| 66 |
import Diagrams.Trail hiding (isLoop, offset) |
| 67 |
import Diagrams.TrailLike |
| 68 |
import Diagrams.TwoD.Arc |
| 69 |
import Diagrams.TwoD.Curvature |
| 70 |
import Diagrams.TwoD.Path () |
| 71 |
import Diagrams.TwoD.Types |
| 72 |
import Diagrams.TwoD.Vector hiding (e) |
| 73 |
|
| 74 |
import Linear.Affine |
| 75 |
import Linear.Metric |
| 76 |
import Linear.Vector |
| 77 |
|
| 78 |
unitPerp :: OrderedField n => V2 n -> V2 n |
| 79 |
unitPerp = signorm . perp |
| 80 |
|
| 81 |
perpAtParam :: OrderedField n => Segment Closed V2 n -> n -> V2 n |
| 82 |
perpAtParam (Linear (OffsetClosed a)) _ = negated $ unitPerp a |
| 83 |
perpAtParam cubic t = negated $ unitPerp a |
| 84 |
where |
| 85 |
(Cubic a _ _) = snd $ splitAtParam cubic t |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
data OffsetOpts d = OffsetOpts |
| 117 |
{ _offsetJoin :: LineJoin |
| 118 |
, _offsetMiterLimit :: d |
| 119 |
, _offsetEpsilon :: d |
| 120 |
} |
| 121 |
|
| 122 |
deriving instance Eq d => Eq (OffsetOpts d) |
| 123 |
deriving instance Show d => Show (OffsetOpts d) |
| 124 |
|
| 125 |
makeLensesWith (lensRules & generateSignatures .~ False) ''OffsetOpts |
| 126 |
|
| 127 |
|
| 128 |
offsetJoin :: Lens' (OffsetOpts d) LineJoin |
| 129 |
|
| 130 |
|
| 131 |
offsetMiterLimit :: Lens' (OffsetOpts d) d |
| 132 |
|
| 133 |
|
| 134 |
offsetEpsilon :: Lens' (OffsetOpts d) d |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
instance Fractional d => Default (OffsetOpts d) where |
| 139 |
def = OffsetOpts def 10 0.01 |
| 140 |
|
| 141 |
|
| 142 |
data ExpandOpts d = ExpandOpts |
| 143 |
{ _expandJoin :: LineJoin |
| 144 |
, _expandMiterLimit :: d |
| 145 |
, _expandCap :: LineCap |
| 146 |
, _expandEpsilon :: d |
| 147 |
} deriving (Eq, Show) |
| 148 |
|
| 149 |
makeLensesWith (lensRules & generateSignatures .~ False) ''ExpandOpts |
| 150 |
|
| 151 |
|
| 152 |
expandJoin :: Lens' (ExpandOpts d) LineJoin |
| 153 |
|
| 154 |
|
| 155 |
expandMiterLimit :: Lens' (ExpandOpts d) d |
| 156 |
|
| 157 |
|
| 158 |
expandCap :: Lens' (ExpandOpts d) LineCap |
| 159 |
|
| 160 |
|
| 161 |
expandEpsilon :: Lens' (ExpandOpts d) d |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
instance (Fractional d) => Default (ExpandOpts d) where |
| 169 |
def = ExpandOpts def 10 def 0.01 |
| 170 |
|
| 171 |
offsetSegment :: RealFloat n |
| 172 |
=> n |
| 173 |
-- absolute value of the radius gives a |
| 174 |
-- value that represents the maximum |
| 175 |
-- allowed deviation from the true offset. In |
| 176 |
-- the current implementation each result segment |
| 177 |
-- should be bounded by arcs that are plus or |
| 178 |
-- minus epsilon factor from the radius of curvature of |
| 179 |
-- the offset. |
| 180 |
-> n |
| 181 |
-- on the right of the curve, negative is on the |
| 182 |
-- left. |
| 183 |
-> Segment Closed V2 n |
| 184 |
-> Located (Trail V2 n) |
| 185 |
offsetSegment _ r s@(Linear (OffsetClosed a)) = trailFromSegments [s] `at` origin .+^ va |
| 186 |
where va = (-r) *^ unitPerp a |
| 187 |
|
| 188 |
offsetSegment epsilon r s@(Cubic a b (OffsetClosed c)) = t `at` origin .+^ va |
| 189 |
where |
| 190 |
t = trailFromSegments (go (radiusOfCurvature s 0.5)) |
| 191 |
-- Perpendiculars to handles. |
| 192 |
va = (-r) *^ unitPerp a |
| 193 |
vc = (-r) *^ unitPerp (c ^-^ b) |
| 194 |
-- Split segments. |
| 195 |
ss = (\(x,y) -> [x,y]) $ splitAtParam s 0.5 |
| 196 |
subdivided = concatMap (trailSegments . unLoc . offsetSegment epsilon r) ss |
| 197 |
|
| 198 |
-- Offset with handles scaled based on curvature. |
| 199 |
offset factor = bezier3 (a^*factor) ((b ^-^ c)^*factor ^+^ c ^+^ vc ^-^ va) (c ^+^ vc ^-^ va) |
| 200 |
|
| 201 |
-- We observe a corner. Subdivide right away. |
| 202 |
go (Finite 0) = subdivided |
| 203 |
-- We have some curvature |
| 204 |
go roc |
| 205 |
| close = [o] |
| 206 |
| otherwise = subdivided |
| 207 |
where |
| 208 |
-- We want the multiplicative factor that takes us from the original |
| 209 |
-- segment's radius of curvature roc, to roc + r. |
| 210 |
-- |
| 211 |
-- r + sr = x * sr |
| 212 |
-- |
| 213 |
o = offset $ case roc of |
| 214 |
Infinity -> 1 -- Do the right thing. |
| 215 |
Finite sr -> 1 + r / sr |
| 216 |
|
| 217 |
close = and [epsilon * abs r > norm (p o ^+^ va ^-^ p s ^-^ pp s) |
| 218 |
| t' <- [0.25, 0.5, 0.75] |
| 219 |
, let p = (`atParam` t') |
| 220 |
, let pp = (r *^) . (`perpAtParam` t') |
| 221 |
] |
| 222 |
|
| 223 |
|
| 224 |
-- > import Diagrams.TwoD.Offset |
| 225 |
-- > |
| 226 |
-- > showExample :: Segment Closed V2 Double -> Diagram SVG |
| 227 |
-- > showExample s = pad 1.1 . centerXY $ d # lc blue # lw thick <> d' # lw thick |
| 228 |
-- > where |
| 229 |
-- > d = strokeP . fromSegments $ [s] |
| 230 |
-- > d' = mconcat . zipWith lc colors . map strokeP . explodeTrail |
| 231 |
-- > $ offsetSegment 0.1 (-1) s |
| 232 |
-- > |
| 233 |
-- > colors = cycle [green, red] |
| 234 |
-- > |
| 235 |
-- > cubicOffsetExample :: Diagram SVG |
| 236 |
-- > cubicOffsetExample = hcat . map showExample $ |
| 237 |
-- > [ bezier3 (10 ^& 0) ( 5 ^& 18) (10 ^& 20) |
| 238 |
-- > , bezier3 ( 0 ^& 20) ( 10 ^& 10) ( 5 ^& 10) |
| 239 |
-- > , bezier3 (10 ^& 20) ( 0 ^& 10) (10 ^& 0) |
| 240 |
-- > , bezier3 (10 ^& 20) ((-5) ^& 10) (10 ^& 0) |
| 241 |
-- > ] |
| 242 |
|
| 243 |
-- Similar to (=<<). This is when we want to map a function across something |
| 244 |
-- located, but the result of the mapping will be transformable so we can |
| 245 |
-- collapse the Located into the result. This assumes that Located has the |
| 246 |
-- meaning of merely taking something that cannot be translated and lifting |
| 247 |
-- it into a space with translation. |
| 248 |
bindLoc :: (Transformable b, V a ~ V b, N a ~ N b, V a ~ V2, N a ~ n, Num n) => (a -> b) -> Located a -> b |
| 249 |
bindLoc f = join' . mapLoc f |
| 250 |
where |
| 251 |
join' (viewLoc -> (p,a)) = translate (p .-. origin) a |
| 252 |
|
| 253 |
-- While we build offsets and expansions we will use the [Located (Segment Closed v)] |
| 254 |
-- and [Located (Trail V2 n)] intermediate representations. |
| 255 |
locatedTrailSegments :: OrderedField n |
| 256 |
=> Located (Trail V2 n) -> [Located (Segment Closed V2 n)] |
| 257 |
locatedTrailSegments t = zipWith at (trailSegments (unLoc t)) (trailPoints t) |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
offsetTrail' :: RealFloat n |
| 278 |
=> OffsetOpts n |
| 279 |
-> n |
| 280 |
-- the left for a line and on the inside for a counter-clockwise |
| 281 |
-- loop. |
| 282 |
-> Located (Trail V2 n) |
| 283 |
-> Located (Trail V2 n) |
| 284 |
offsetTrail' opts r t = joinSegments eps j isLoop (opts^.offsetMiterLimit) r ends . offset $ t |
| 285 |
where |
| 286 |
eps = opts^.offsetEpsilon |
| 287 |
offset = map (bindLoc (offsetSegment eps r)) . locatedTrailSegments |
| 288 |
ends | isLoop = (\(a:as) -> as ++ [a]) . trailPoints $ t |
| 289 |
| otherwise = tail . trailPoints $ t |
| 290 |
j = fromLineJoin (opts^.offsetJoin) |
| 291 |
|
| 292 |
isLoop = withTrail (const False) (const True) (unLoc t) |
| 293 |
|
| 294 |
|
| 295 |
offsetTrail :: RealFloat n => n -> Located (Trail V2 n) -> Located (Trail V2 n) |
| 296 |
offsetTrail = offsetTrail' def |
| 297 |
|
| 298 |
|
| 299 |
offsetPath' :: RealFloat n => OffsetOpts n -> n -> Path V2 n -> Path V2 n |
| 300 |
offsetPath' opts r = mconcat |
| 301 |
. map (bindLoc (trailLike . offsetTrail' opts r) . (`at` origin)) |
| 302 |
. op Path |
| 303 |
|
| 304 |
|
| 305 |
offsetPath :: RealFloat n => n -> Path V2 n -> Path V2 n |
| 306 |
offsetPath = offsetPath' def |
| 307 |
|
| 308 |
-- TODO: Include arrowheads on examples to indicate direction so the "left" and |
| 309 |
-- "right" make sense. |
| 310 |
-- |
| 311 |
-- > import Diagrams.TwoD.Offset |
| 312 |
-- > import Data.Default |
| 313 |
-- > |
| 314 |
-- > corner :: (OrderedField n) => Located (Trail V2 n) |
| 315 |
-- > corner = fromVertices (map p2 [(0, 0), (10, 0), (5, 6)]) `at` origin |
| 316 |
-- > |
| 317 |
-- > offsetTrailExample :: Diagram SVG |
| 318 |
-- > offsetTrailExample = pad 1.1 . centerXY . lwO 3 . hcat' (def & sep .~ 1 ) |
| 319 |
-- > . map (uncurry showStyle) |
| 320 |
-- > $ [ (LineJoinMiter, "LineJoinMiter") |
| 321 |
-- > , (LineJoinRound, "LineJoinRound") |
| 322 |
-- > , (LineJoinBevel, "LineJoinBevel") |
| 323 |
-- > ] |
| 324 |
-- > where |
| 325 |
-- > showStyle j s = centerXY (trailLike corner # lc blue |
| 326 |
-- > <> trailLike (offsetTrail' (def & offsetJoin .~ j) 2 corner) # lc green) |
| 327 |
-- > === (strutY 3 <> text s # font "Helvetica" # bold) |
| 328 |
-- > |
| 329 |
-- > offsetTrailLeftExample :: Diagram SVG |
| 330 |
-- > offsetTrailLeftExample = pad 1.1 . centerXY . lwO 3 |
| 331 |
-- > $ (trailLike c # lc blue) |
| 332 |
-- > <> (lc green . trailLike |
| 333 |
-- > . offsetTrail' (def & offsetJoin .~ LineJoinRound) (-2) $ c) |
| 334 |
-- > where |
| 335 |
-- > c = reflectY corner |
| 336 |
-- > |
| 337 |
-- > offsetTrailOuterExample :: Diagram SVG |
| 338 |
-- > offsetTrailOuterExample = pad 1.1 . centerXY . lwO 3 |
| 339 |
-- > $ (trailLike c # lc blue) |
| 340 |
-- > <> (lc green . trailLike |
| 341 |
-- > . offsetTrail' (def & offsetJoin .~ LineJoinRound) 2 $ c) |
| 342 |
-- > where |
| 343 |
-- > c = hexagon 5 |
| 344 |
|
| 345 |
withTrailL :: (Located (Trail' Line V2 n) -> r) -> (Located (Trail' Loop V2 n) -> r) -> Located (Trail V2 n) -> r |
| 346 |
withTrailL f g l = withTrail (f . (`at` p)) (g . (`at` p)) (unLoc l) |
| 347 |
where |
| 348 |
p = loc l |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
expandTrail' :: (OrderedField n, RealFloat n, RealFrac n) |
| 364 |
=> ExpandOpts n |
| 365 |
-> n |
| 366 |
-- For a line this gives a loop of the offset. For a |
| 367 |
-- loop this gives two loops, the outer counter-clockwise |
| 368 |
-- and the inner clockwise. |
| 369 |
-> Located (Trail V2 n) |
| 370 |
-> Path V2 n |
| 371 |
expandTrail' o r t |
| 372 |
| r < 0 = error "expandTrail' with negative radius" |
| 373 |
-- TODO: consider just reversing the path instead of this error. |
| 374 |
| otherwise = withTrailL (pathFromLocTrail . expandLine o r) (expandLoop o r) t |
| 375 |
|
| 376 |
expandLine :: RealFloat n => ExpandOpts n -> n -> Located (Trail' Line V2 n) -> Located (Trail V2 n) |
| 377 |
expandLine opts r (mapLoc wrapLine -> t) = caps cap r s e (f r) (f $ -r) |
| 378 |
where |
| 379 |
eps = opts^.expandEpsilon |
| 380 |
offset r' = map (bindLoc (offsetSegment eps r')) . locatedTrailSegments |
| 381 |
f r' = joinSegments eps (fromLineJoin (opts^.expandJoin)) False (opts^.expandMiterLimit) r' ends |
| 382 |
. offset r' $ t |
| 383 |
ends = tail . trailPoints $ t |
| 384 |
s = atStart t |
| 385 |
e = atEnd t |
| 386 |
cap = fromLineCap (opts^.expandCap) |
| 387 |
|
| 388 |
expandLoop :: RealFloat n => ExpandOpts n -> n -> Located (Trail' Loop V2 n) -> Path V2 n |
| 389 |
expandLoop opts r (mapLoc wrapLoop -> t) = trailLike (f r) <> (trailLike . reverseDomain . f $ -r) |
| 390 |
where |
| 391 |
eps = opts^.expandEpsilon |
| 392 |
offset r' = map (bindLoc (offsetSegment eps r')) . locatedTrailSegments |
| 393 |
f r' = joinSegments eps (fromLineJoin (opts^.expandJoin)) True (opts^.expandMiterLimit) r' ends |
| 394 |
. offset r' $ t |
| 395 |
ends = (\(a:as) -> as ++ [a]) . trailPoints $ t |
| 396 |
|
| 397 |
|
| 398 |
expandTrail :: RealFloat n => n -> Located (Trail V2 n) -> Path V2 n |
| 399 |
expandTrail = expandTrail' def |
| 400 |
|
| 401 |
|
| 402 |
expandPath' :: RealFloat n => ExpandOpts n -> n -> Path V2 n -> Path V2 n |
| 403 |
expandPath' opts r = mconcat |
| 404 |
. map (bindLoc (expandTrail' opts r) . (`at` origin)) |
| 405 |
. op Path |
| 406 |
|
| 407 |
|
| 408 |
expandPath :: RealFloat n => n -> Path V2 n -> Path V2 n |
| 409 |
expandPath = expandPath' def |
| 410 |
|
| 411 |
-- > import Diagrams.TwoD.Offset |
| 412 |
-- > import Data.Default |
| 413 |
-- > |
| 414 |
-- > expandTrailExample :: Diagram SVG |
| 415 |
-- > expandTrailExample = pad 1.1 . centerXY . hcat' (def & sep .~ 1) |
| 416 |
-- > . map (uncurry showStyle) |
| 417 |
-- > $ [ (LineCapButt, "LineCapButt") |
| 418 |
-- > , (LineCapRound, "LineCapRound") |
| 419 |
-- > , (LineCapSquare, "LineCapSquare") |
| 420 |
-- > ] |
| 421 |
-- > where |
| 422 |
-- > showStyle c s = centerXY (trailLike corner # lc white # lw veryThick |
| 423 |
-- > <> stroke (expandTrail' |
| 424 |
-- > (def & expandJoin .~ LineJoinRound |
| 425 |
-- > & expandCap .~ c |
| 426 |
-- > ) 2 corner) |
| 427 |
-- > # lw none # fc green) |
| 428 |
-- > === (strutY 3 <> text s # font "Helvetica" # bold) |
| 429 |
-- > |
| 430 |
-- > expandLoopExample :: Diagram SVG |
| 431 |
-- > expandLoopExample = pad 1.1 . centerXY $ ((strokeLocT t # lw veryThick # lc white) |
| 432 |
-- > <> (stroke t' # lw none # fc green)) |
| 433 |
-- > where |
| 434 |
-- > t = mapLoc glueTrail $ fromVertices (map p2 [(0, 0), (5, 0), (10, 5), (10, 10), (0, 0)]) |
| 435 |
-- > t' = expandTrail' (def & expandJoin .~ LineJoinRound) 1 t |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
caps :: RealFloat n => (n -> Point V2 n -> Point V2 n -> Point V2 n -> Trail V2 n) |
| 448 |
-> n -> Point V2 n -> Point V2 n -> Located (Trail V2 n) -> Located (Trail V2 n) -> Located (Trail V2 n) |
| 449 |
caps cap r s e fs bs = mapLoc glueTrail $ mconcat |
| 450 |
[ cap r s (atStart bs) (atStart fs) |
| 451 |
, unLoc fs |
| 452 |
, cap r e (atEnd fs) (atEnd bs) |
| 453 |
, reverseDomain (unLoc bs) |
| 454 |
] `at` atStart bs |
| 455 |
|
| 456 |
|
| 457 |
fromLineCap :: RealFloat n => LineCap -> n -> Point V2 n -> Point V2 n -> Point V2 n -> Trail V2 n |
| 458 |
fromLineCap c = case c of |
| 459 |
LineCapButt -> capCut |
| 460 |
LineCapRound -> capArc |
| 461 |
LineCapSquare -> capSquare |
| 462 |
|
| 463 |
|
| 464 |
capCut :: RealFloat n => n -> Point V2 n -> Point V2 n -> Point V2 n -> Trail V2 n |
| 465 |
capCut _r _c a b = fromSegments [straight (b .-. a)] |
| 466 |
|
| 467 |
|
| 468 |
capSquare :: RealFloat n => n -> Point V2 n -> Point V2 n -> Point V2 n -> Trail V2 n |
| 469 |
capSquare _r c a b = unLoc $ fromVertices [ a, a .+^ v, b .+^ v, b ] |
| 470 |
where |
| 471 |
v = perp (a .-. c) |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
capArc :: RealFloat n => n -> Point V2 n -> Point V2 n -> Point V2 n -> Trail V2 n |
| 476 |
capArc r c a b = trailLike . moveTo c $ fs |
| 477 |
where |
| 478 |
fs | r < 0 = scale (-r) $ arcCW (dirBetween c a) (dirBetween c b) |
| 479 |
| otherwise = scale r $ arcCCW (dirBetween c a) (dirBetween c b) |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
joinSegments :: RealFloat n |
| 490 |
=> n |
| 491 |
-> (n -> n -> Point V2 n -> Located (Trail V2 n) -> Located (Trail V2 n) -> Trail V2 n) |
| 492 |
-> Bool |
| 493 |
-> n |
| 494 |
-> n |
| 495 |
-> [Point V2 n] |
| 496 |
-> [Located (Trail V2 n)] |
| 497 |
-> Located (Trail V2 n) |
| 498 |
joinSegments _ _ _ _ _ _ [] = mempty `at` origin |
| 499 |
joinSegments _ _ _ _ _ [] _ = mempty `at` origin |
| 500 |
joinSegments epsilon j isLoop ml r es ts@(t:_) = t' |
| 501 |
where |
| 502 |
t' | isLoop = mapLoc (glueTrail . (<> f (take (length ts * 2 - 1) $ ss es (ts ++ [t])))) t |
| 503 |
| otherwise = mapLoc (<> f (ss es ts)) t |
| 504 |
ss es' ts' = concat [[test a b $ j ml r e a b, Just $ unLoc b] | (e,(a,b)) <- zip es' . (zip <*> tail) $ ts'] |
| 505 |
test a b tj |
| 506 |
| atStart b `distance` atEnd a > epsilon = Just tj |
| 507 |
| otherwise = Nothing |
| 508 |
f = mconcat . catMaybes |
| 509 |
|
| 510 |
|
| 511 |
fromLineJoin |
| 512 |
:: RealFloat n => LineJoin -> n -> n -> Point V2 n -> Located (Trail V2 n) -> Located (Trail V2 n) -> Trail V2 n |
| 513 |
fromLineJoin j = case j of |
| 514 |
LineJoinMiter -> joinSegmentIntersect |
| 515 |
LineJoinRound -> joinSegmentArc |
| 516 |
LineJoinBevel -> joinSegmentClip |
| 517 |
|
| 518 |
-- TODO: The joinSegmentCut option is not in our standard line joins. I don't know |
| 519 |
-- how useful it is graphically, I mostly had it as it was useful for debugging |
| 520 |
{- |
| 521 |
-- | Join with segments going back to the original corner. |
| 522 |
joinSegmentCut :: (OrderedField n) => n -> n -> Point V2 n -> Located (Trail V2 n) -> Located (Trail V2 n) -> Trail V2 n |
| 523 |
joinSegmentCut _ _ e a b = fromSegments |
| 524 |
[ straight (e .-. atEnd a) |
| 525 |
, straight (atStart b .-. e) |
| 526 |
] |
| 527 |
-} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
joinSegmentClip :: RealFloat n |
| 533 |
=> n -> n -> Point V2 n -> Located (Trail V2 n) -> Located (Trail V2 n) -> Trail V2 n |
| 534 |
joinSegmentClip _ _ _ a b = fromSegments [straight $ atStart b .-. atEnd a] |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
joinSegmentArc :: RealFloat n |
| 539 |
=> n -> n -> Point V2 n -> Located (Trail V2 n) -> Located (Trail V2 n) -> Trail V2 n |
| 540 |
joinSegmentArc _ r e a b = capArc r e (atEnd a) (atStart b) |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
joinSegmentIntersect |
| 545 |
:: RealFloat n => n -> n -> Point V2 n -> Located (Trail V2 n) -> Located (Trail V2 n) -> Trail V2 n |
| 546 |
joinSegmentIntersect miterLimit r e a b = |
| 547 |
if cross < 0.000001 |
| 548 |
then clip |
| 549 |
else case traceP pa va t of |
| 550 |
-- clip join when we excede the miter limit. We could instead |
| 551 |
-- Join at exactly the miter limit, but standard behavior seems |
| 552 |
-- to be clipping. |
| 553 |
Nothing -> clip |
| 554 |
Just p |
| 555 |
-- If trace gave us garbage... |
| 556 |
| p `distance` pb > abs (miterLimit * r) -> clip |
| 557 |
| otherwise -> unLoc $ fromVertices [ pa, p, pb ] |
| 558 |
where |
| 559 |
t = straight (miter vb) `at` pb |
| 560 |
va = unitPerp (pa .-. e) |
| 561 |
vb = negated $ unitPerp (pb .-. e) |
| 562 |
pa = atEnd a |
| 563 |
pb = atStart b |
| 564 |
miter v = abs (miterLimit * r) *^ v |
| 565 |
clip = joinSegmentClip miterLimit r e a b |
| 566 |
cross = let (xa,ya) = unr2 va; (xb,yb) = unr2 vb in abs (xa * yb - xb * ya) |