| 1 |
{-# LANGUAGE ConstraintKinds #-} |
| 2 |
{-# LANGUAGE DeriveDataTypeable #-} |
| 3 |
{-# LANGUAGE FlexibleContexts #-} |
| 4 |
{-# LANGUAGE FlexibleInstances #-} |
| 5 |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 6 |
{-# LANGUAGE MultiParamTypeClasses #-} |
| 7 |
{-# LANGUAGE Rank2Types #-} |
| 8 |
{-# LANGUAGE StandaloneDeriving #-} |
| 9 |
{-# LANGUAGE TemplateHaskell #-} |
| 10 |
{-# LANGUAGE TypeFamilies #-} |
| 11 |
{-# LANGUAGE TypeOperators #-} |
| 12 |
{-# LANGUAGE UndecidableInstances #-} |
| 13 |
{-# LANGUAGE ViewPatterns #-} |
| 14 |
|
| 15 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 16 |
|
| 17 |
----------------------------------------------------------------------------- |
| 18 |
-- | |
| 19 |
-- Module : Diagrams.TwoD.Path |
| 20 |
-- Copyright : (c) 2011-2015 diagrams-lib team (see LICENSE) |
| 21 |
-- License : BSD-style (see LICENSE) |
| 22 |
-- Maintainer : [email protected] |
| 23 |
-- |
| 24 |
-- Paths in two dimensions are special since we may stroke them to |
| 25 |
-- create a 2D diagram, and (eventually) perform operations such as |
| 26 |
-- intersection and union. They also have a trace, whereas paths in |
| 27 |
-- higher dimensions do not. |
| 28 |
-- |
| 29 |
----------------------------------------------------------------------------- |
| 30 |
|
| 31 |
module Diagrams.TwoD.Path |
| 32 |
( -- * Constructing path-based diagrams |
| 33 |
|
| 34 |
stroke, stroke' |
| 35 |
, strokePath, strokeP, strokePath', strokeP' |
| 36 |
, strokeTrail, strokeT, strokeTrail', strokeT' |
| 37 |
, strokeLine, strokeLoop |
| 38 |
, strokeLocTrail, strokeLocT, strokeLocLine, strokeLocLoop |
| 39 |
|
| 40 |
-- ** Stroke options |
| 41 |
|
| 42 |
, FillRule(..) |
| 43 |
, getFillRule, fillRule, _fillRule |
| 44 |
, StrokeOpts(..), vertexNames, queryFillRule |
| 45 |
|
| 46 |
-- ** Inside/outside testing |
| 47 |
|
| 48 |
, Crossings (..) |
| 49 |
, isInsideWinding |
| 50 |
, isInsideEvenOdd |
| 51 |
|
| 52 |
-- * Clipping |
| 53 |
|
| 54 |
, Clip(..), _Clip, _clip |
| 55 |
, clipBy, clipTo, clipped |
| 56 |
|
| 57 |
-- * Intersections |
| 58 |
|
| 59 |
, intersectPoints, intersectPoints' |
| 60 |
, intersectPointsP, intersectPointsP' |
| 61 |
, intersectPointsT, intersectPointsT' |
| 62 |
) where |
| 63 |
|
| 64 |
import Control.Applicative (liftA2) |
| 65 |
import Control.Lens hiding (at, transform) |
| 66 |
import qualified Data.Foldable as F |
| 67 |
import Data.Semigroup |
| 68 |
import Data.Typeable |
| 69 |
|
| 70 |
import Data.Default |
| 71 |
|
| 72 |
import Diagrams.Angle |
| 73 |
import Diagrams.Combinators (withEnvelope, withTrace) |
| 74 |
import Diagrams.Core |
| 75 |
import Diagrams.Core.Trace |
| 76 |
import Diagrams.Located (Located, mapLoc, unLoc) |
| 77 |
import Diagrams.Parametric |
| 78 |
import Diagrams.Path |
| 79 |
import Diagrams.Query |
| 80 |
import Diagrams.Segment |
| 81 |
import Diagrams.Solve.Polynomial |
| 82 |
import Diagrams.Trail |
| 83 |
import Diagrams.TrailLike |
| 84 |
import Diagrams.TwoD.Segment |
| 85 |
import Diagrams.TwoD.Types |
| 86 |
import Diagrams.TwoD.Vector |
| 87 |
import Diagrams.Util (tau) |
| 88 |
|
| 89 |
import Linear.Affine |
| 90 |
import Linear.Vector |
| 91 |
|
| 92 |
------------------------------------------------------------ |
| 93 |
-- Trail and path traces --------------------------------- |
| 94 |
------------------------------------------------------------ |
| 95 |
|
| 96 |
-- Only 2D trails and paths have a trace. |
| 97 |
|
| 98 |
-- XXX can the efficiency of this be improved? See the comment in |
| 99 |
-- Diagrams.Path on the Enveloped instance for Trail. |
| 100 |
instance RealFloat n => Traced (Trail V2 n) where |
| 101 |
getTrace = withLine $ |
| 102 |
foldr |
| 103 |
(\seg bds -> moveOriginBy (negated . atEnd $ seg) bds <> getTrace seg) |
| 104 |
mempty |
| 105 |
. lineSegments |
| 106 |
|
| 107 |
instance RealFloat n => Traced (Path V2 n) where |
| 108 |
getTrace = F.foldMap getTrace . op Path |
| 109 |
|
| 110 |
------------------------------------------------------------ |
| 111 |
-- Constructing path-based diagrams ---------------------- |
| 112 |
------------------------------------------------------------ |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
data FillRule |
| 118 |
= Winding |
| 119 |
-- /winding/ /number/. See |
| 120 |
-- <http://en.wikipedia.org/wiki/Nonzero-rule>. |
| 121 |
| EvenOdd |
| 122 |
-- extended infinitely in a particular direction crosses |
| 123 |
-- the path an odd number of times. See |
| 124 |
-- <http://en.wikipedia.org/wiki/Even-odd_rule>. |
| 125 |
deriving (Show, Typeable, Eq, Ord) |
| 126 |
|
| 127 |
instance AttributeClass FillRule |
| 128 |
instance Semigroup FillRule where |
| 129 |
_ <> b = b |
| 130 |
|
| 131 |
instance Default FillRule where |
| 132 |
def = Winding |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
data StrokeOpts a |
| 138 |
= StrokeOpts |
| 139 |
{ _vertexNames :: [[a]] |
| 140 |
|
| 141 |
, _queryFillRule :: FillRule |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
makeLensesWith (generateSignatures .~ False $ lensRules) ''StrokeOpts |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
vertexNames :: Lens (StrokeOpts a) (StrokeOpts a') [[a]] [[a']] |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
queryFillRule :: Lens' (StrokeOpts a) FillRule |
| 164 |
|
| 165 |
instance Default (StrokeOpts a) where |
| 166 |
def = StrokeOpts |
| 167 |
{ _vertexNames = [] |
| 168 |
, _queryFillRule = def |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
stroke :: (InSpace V2 n t, ToPath t, TypeableFloat n, Renderable (Path V2 n) b) |
| 184 |
=> t -> QDiagram b V2 n Any |
| 185 |
stroke = strokeP . toPath |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
stroke' :: (InSpace V2 n t, ToPath t, TypeableFloat n, Renderable (Path V2 n) b, IsName a) |
| 195 |
=> StrokeOpts a -> t -> QDiagram b V2 n Any |
| 196 |
stroke' opts = strokeP' opts . toPath |
| 197 |
|
| 198 |
|
| 199 |
strokeP :: (TypeableFloat n, Renderable (Path V2 n) b) |
| 200 |
=> Path V2 n -> QDiagram b V2 n Any |
| 201 |
strokeP = strokeP' (def :: StrokeOpts ()) |
| 202 |
|
| 203 |
|
| 204 |
strokePath :: (TypeableFloat n, Renderable (Path V2 n) b) |
| 205 |
=> Path V2 n -> QDiagram b V2 n Any |
| 206 |
strokePath = strokeP |
| 207 |
|
| 208 |
instance (TypeableFloat n, Renderable (Path V2 n) b) |
| 209 |
=> TrailLike (QDiagram b V2 n Any) where |
| 210 |
trailLike = strokeP . trailLike |
| 211 |
|
| 212 |
|
| 213 |
strokeP' :: (TypeableFloat n, Renderable (Path V2 n) b, IsName a) |
| 214 |
=> StrokeOpts a -> Path V2 n -> QDiagram b V2 n Any |
| 215 |
strokeP' opts path |
| 216 |
| null (pLines ^. _Wrapped') = mkP pLoops |
| 217 |
| null (pLoops ^. _Wrapped') = mkP pLines |
| 218 |
| otherwise = mkP pLines <> mkP pLoops |
| 219 |
where |
| 220 |
(pLines,pLoops) = partitionPath (isLine . unLoc) path |
| 221 |
mkP p |
| 222 |
= mkQD (Prim p) |
| 223 |
(getEnvelope p) |
| 224 |
(getTrace p) |
| 225 |
(fromNames . concat $ |
| 226 |
zipWith zip (opts^.vertexNames) ((map . map) subPoint (pathVertices p)) |
| 227 |
) |
| 228 |
(Query $ Any . (runFillRule (opts^.queryFillRule)) p) |
| 229 |
|
| 230 |
|
| 231 |
strokePath' :: (TypeableFloat n, Renderable (Path V2 n) b, IsName a) |
| 232 |
=> StrokeOpts a -> Path V2 n -> QDiagram b V2 n Any |
| 233 |
strokePath' = strokeP' |
| 234 |
|
| 235 |
|
| 236 |
strokeTrail :: (TypeableFloat n, Renderable (Path V2 n) b) |
| 237 |
=> Trail V2 n -> QDiagram b V2 n Any |
| 238 |
strokeTrail = stroke . pathFromTrail |
| 239 |
|
| 240 |
|
| 241 |
strokeT :: (TypeableFloat n, Renderable (Path V2 n) b) |
| 242 |
=> Trail V2 n -> QDiagram b V2 n Any |
| 243 |
strokeT = strokeTrail |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
strokeTrail' :: (TypeableFloat n, Renderable (Path V2 n) b, IsName a) |
| 248 |
=> StrokeOpts a -> Trail V2 n -> QDiagram b V2 n Any |
| 249 |
strokeTrail' opts = stroke' opts . pathFromTrail |
| 250 |
|
| 251 |
|
| 252 |
strokeT' :: (TypeableFloat n, Renderable (Path V2 n) b, IsName a) |
| 253 |
=> StrokeOpts a -> Trail V2 n -> QDiagram b V2 n Any |
| 254 |
strokeT' = strokeTrail' |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
strokeLine :: (TypeableFloat n, Renderable (Path V2 n) b) |
| 259 |
=> Trail' Line V2 n -> QDiagram b V2 n Any |
| 260 |
strokeLine = strokeT . wrapLine |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
strokeLoop :: (TypeableFloat n, Renderable (Path V2 n) b) |
| 265 |
=> Trail' Loop V2 n -> QDiagram b V2 n Any |
| 266 |
strokeLoop = strokeT . wrapLoop |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
strokeLocTrail :: (TypeableFloat n, Renderable (Path V2 n) b) |
| 271 |
=> Located (Trail V2 n) -> QDiagram b V2 n Any |
| 272 |
strokeLocTrail = strokeP . trailLike |
| 273 |
|
| 274 |
|
| 275 |
strokeLocT :: (TypeableFloat n, Renderable (Path V2 n) b) |
| 276 |
=> Located (Trail V2 n) -> QDiagram b V2 n Any |
| 277 |
strokeLocT = strokeLocTrail |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
strokeLocLine :: (TypeableFloat n, Renderable (Path V2 n) b) |
| 282 |
=> Located (Trail' Line V2 n) -> QDiagram b V2 n Any |
| 283 |
strokeLocLine = strokeP . trailLike . mapLoc wrapLine |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
strokeLocLoop :: (TypeableFloat n, Renderable (Path V2 n) b) |
| 288 |
=> Located (Trail' Loop V2 n) -> QDiagram b V2 n Any |
| 289 |
strokeLocLoop = strokeP . trailLike . mapLoc wrapLoop |
| 290 |
|
| 291 |
------------------------------------------------------------ |
| 292 |
-- Inside/outside testing |
| 293 |
------------------------------------------------------------ |
| 294 |
|
| 295 |
runFillRule :: RealFloat n => FillRule -> Path V2 n -> Point V2 n -> Bool |
| 296 |
runFillRule Winding = isInsideWinding |
| 297 |
runFillRule EvenOdd = isInsideEvenOdd |
| 298 |
|
| 299 |
|
| 300 |
getFillRule :: FillRule -> FillRule |
| 301 |
getFillRule = id |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
fillRule :: HasStyle a => FillRule -> a -> a |
| 306 |
fillRule = applyAttr |
| 307 |
|
| 308 |
|
| 309 |
_fillRule :: Lens' (Style V2 n) FillRule |
| 310 |
_fillRule = atAttr . non def |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
newtype Crossings = Crossings Int |
| 333 |
deriving (Show, Eq, Ord, Num, Enum, Real, Integral) |
| 334 |
|
| 335 |
instance Semigroup Crossings where |
| 336 |
Crossings a <> Crossings b = Crossings (a + b) |
| 337 |
|
| 338 |
instance Monoid Crossings where |
| 339 |
mempty = Crossings 0 |
| 340 |
mappend = (<>) |
| 341 |
|
| 342 |
instance RealFloat n => HasQuery (Located (Trail V2 n)) Crossings where |
| 343 |
getQuery trail = Query $ \p -> trailCrossings p trail |
| 344 |
|
| 345 |
instance RealFloat n => HasQuery (Located (Trail' l V2 n)) Crossings where |
| 346 |
getQuery trail' = getQuery (mapLoc Trail trail') |
| 347 |
|
| 348 |
instance RealFloat n => HasQuery (Path V2 n) Crossings where |
| 349 |
getQuery = foldMapOf each getQuery |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
isInsideWinding :: HasQuery t Crossings => t -> Point (V t) (N t) -> Bool |
| 362 |
isInsideWinding t = (/= 0) . sample t |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
isInsideEvenOdd :: HasQuery t Crossings => t -> Point (V t) (N t) -> Bool |
| 377 |
isInsideEvenOdd t = odd . sample t |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
trailCrossings :: RealFloat n => Point V2 n -> Located (Trail V2 n) -> Crossings |
| 382 |
|
| 383 |
-- non-loop trails have no inside or outside, so don't contribute crossings |
| 384 |
trailCrossings _ t | not (isLoop (unLoc t)) = 0 |
| 385 |
|
| 386 |
trailCrossings p@(unp2 -> (x,y)) tr |
| 387 |
= F.foldMap test $ fixTrail tr |
| 388 |
where |
| 389 |
test (FLinear a@(unp2 -> (_,ay)) b@(unp2 -> (_,by))) |
| 390 |
| ay <= y && by > y && isLeft a b > 0 = 1 |
| 391 |
| by <= y && ay > y && isLeft a b < 0 = -1 |
| 392 |
| otherwise = 0 |
| 393 |
|
| 394 |
test c@(FCubic (P x1@(V2 _ x1y)) |
| 395 |
(P c1@(V2 _ c1y)) |
| 396 |
(P c2@(V2 _ c2y)) |
| 397 |
(P x2@(V2 _ x2y)) |
| 398 |
) = |
| 399 |
sum . map testT $ ts |
| 400 |
where ts = filter (liftA2 (&&) (>=0) (<=1)) |
| 401 |
$ cubForm (- x1y + 3*c1y - 3*c2y + x2y) |
| 402 |
( 3*x1y - 6*c1y + 3*c2y) |
| 403 |
(-3*x1y + 3*c1y) |
| 404 |
(x1y - y) |
| 405 |
testT t = let (unp2 -> (px,_)) = c `atParam` t |
| 406 |
in if px > x then signFromDerivAt t else 0 |
| 407 |
signFromDerivAt t = |
| 408 |
let v = (3*t*t) *^ ((-1)*^x1 ^+^ 3*^c1 ^-^ 3*^c2 ^+^ x2) |
| 409 |
^+^ (2*t) *^ (3*^x1 ^-^ 6*^c1 ^+^ 3*^c2) |
| 410 |
^+^ ((-3)*^x1 ^+^ 3*^c1) |
| 411 |
ang = v ^. _theta . rad |
| 412 |
in case () of _ | 0 < ang && ang < tau/2 && t < 1 -> 1 |
| 413 |
| -tau/2 < ang && ang < 0 && t > 0 -> -1 |
| 414 |
| otherwise -> 0 |
| 415 |
|
| 416 |
isLeft a b = cross2 (b .-. a) (p .-. a) |
| 417 |
|
| 418 |
------------------------------------------------------------ |
| 419 |
-- Clipping ---------------------------------------------- |
| 420 |
------------------------------------------------------------ |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
newtype Clip n = Clip [Path V2 n] |
| 428 |
deriving (Typeable, Semigroup) |
| 429 |
|
| 430 |
makeWrapped ''Clip |
| 431 |
|
| 432 |
instance Typeable n => AttributeClass (Clip n) |
| 433 |
|
| 434 |
instance AsEmpty (Clip n) where |
| 435 |
_Empty = _Clip . _Empty |
| 436 |
|
| 437 |
type instance V (Clip n) = V2 |
| 438 |
type instance N (Clip n) = n |
| 439 |
|
| 440 |
instance (OrderedField n) => Transformable (Clip n) where |
| 441 |
transform t (Clip ps) = Clip (transform t ps) |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
instance RealFloat n => HasQuery (Clip n) All where |
| 446 |
getQuery (Clip paths) = Query $ \p -> |
| 447 |
F.foldMap (All . flip isInsideWinding p) paths |
| 448 |
|
| 449 |
_Clip :: Iso (Clip n) (Clip n') [Path V2 n] [Path V2 n'] |
| 450 |
_Clip = _Wrapped |
| 451 |
|
| 452 |
|
| 453 |
_clip :: (Typeable n, OrderedField n) => Lens' (Style V2 n) [Path V2 n] |
| 454 |
_clip = atTAttr . non' _Empty . _Clip |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
clipBy :: (HasStyle a, V a ~ V2, N a ~ n, TypeableFloat n) => Path V2 n -> a -> a |
| 463 |
clipBy = applyTAttr . Clip . (:[]) |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
clipTo :: TypeableFloat n |
| 471 |
=> Path V2 n -> QDiagram b V2 n Any -> QDiagram b V2 n Any |
| 472 |
clipTo p d = setTrace intersectionTrace . toEnvelope $ clipBy p d |
| 473 |
where |
| 474 |
envP = appEnvelope . getEnvelope $ p |
| 475 |
envD = appEnvelope . getEnvelope $ d |
| 476 |
toEnvelope = case (envP, envD) of |
| 477 |
(Just eP, Just eD) -> setEnvelope . mkEnvelope $ \v -> min (eP v) (eD v) |
| 478 |
(_, _) -> id |
| 479 |
intersectionTrace = Trace traceIntersections |
| 480 |
traceIntersections pt v = |
| 481 |
-- on boundary of d, inside p |
| 482 |
onSortedList (filter pInside) (appTrace (getTrace d) pt v) <> |
| 483 |
-- or on boundary of p, inside d |
| 484 |
onSortedList (filter dInside) (appTrace (getTrace p) pt v) where |
| 485 |
newPt dist = pt .+^ v ^* dist |
| 486 |
pInside dDist = runFillRule Winding p (newPt dDist) |
| 487 |
dInside pDist = getAny . sample d $ newPt pDist |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
clipped :: TypeableFloat n |
| 492 |
=> Path V2 n -> QDiagram b V2 n Any -> QDiagram b V2 n Any |
| 493 |
clipped p = withTrace p . withEnvelope p . clipBy p |
| 494 |
|
| 495 |
------------------------------------------------------------ |
| 496 |
-- Intersections ----------------------------------------- |
| 497 |
------------------------------------------------------------ |
| 498 |
|
| 499 |
|
| 500 |
intersectPoints :: (InSpace V2 n t, SameSpace t s, ToPath t, ToPath s, OrderedField n) |
| 501 |
=> t -> s -> [P2 n] |
| 502 |
intersectPoints = intersectPoints' 1e-8 |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
intersectPoints' :: (InSpace V2 n t, SameSpace t s, ToPath t, ToPath s, OrderedField n) |
| 507 |
=> n -> t -> s -> [P2 n] |
| 508 |
intersectPoints' eps t s = intersectPointsP' eps (toPath t) (toPath s) |
| 509 |
|
| 510 |
|
| 511 |
intersectPointsP :: OrderedField n => Path V2 n -> Path V2 n -> [P2 n] |
| 512 |
intersectPointsP = intersectPointsP' 1e-8 |
| 513 |
|
| 514 |
|
| 515 |
intersectPointsP' :: OrderedField n => n -> Path V2 n -> Path V2 n -> [P2 n] |
| 516 |
intersectPointsP' eps as bs = do |
| 517 |
a <- pathTrails as |
| 518 |
b <- pathTrails bs |
| 519 |
intersectPointsT' eps a b |
| 520 |
|
| 521 |
|
| 522 |
intersectPointsT :: OrderedField n => Located (Trail V2 n) -> Located (Trail V2 n) -> [P2 n] |
| 523 |
intersectPointsT = intersectPointsT' 1e-8 |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
intersectPointsT' :: OrderedField n => n -> Located (Trail V2 n) -> Located (Trail V2 n) -> [P2 n] |
| 528 |
intersectPointsT' eps as bs = do |
| 529 |
a <- fixTrail as |
| 530 |
b <- fixTrail bs |
| 531 |
intersectPointsS' eps a b |