{- ORMOLU_DISABLE -}
-- Implicit CAD. Copyright (C) 2011, Christopher Olah (chris@colah.ca)
-- Copyright 2014 2015 2016, Julia Longtin (julial@turinglace.com)
-- Released under the GNU AGPLV3+, see LICENSE

module Graphics.Implicit.Export.Render.GetLoops (getLoops) where

-- Explicitly include what we want from Prelude.
import Prelude ((<$>), head, last, (==), Bool(False), (.), null, (<>), Eq, Maybe(Just, Nothing))

import Data.List (partition)

-- | The goal of getLoops is to extract loops from a list of segments.
--   The input is a list of segments.
--   The output a list of loops, where each loop is a list of
--   segments, which each piece representing a "side".
--
-- For example:
-- Given points [[1,2],[5,1],[2,3,4,5], ... ]
-- notice that there is a loop 1,2,3,4,5... <repeat>
-- But we give the output [ [ [1,2], [2,3,4,5], [5,1] ], ... ]
-- so that we have the loop, and also knowledge of how
-- the list is built (the "sides" of it).
--
getLoops :: Eq a => [[a]] -> Maybe [[[a]]]
getLoops :: [[a]] -> Maybe [[[a]]]
getLoops [] = [[[a]]] -> Maybe [[[a]]]
forall a. a -> Maybe a
Just []
getLoops ([a]
a:[[a]]
as) = [[a]] -> [[a]] -> a -> Maybe [[[a]]]
forall a. Eq a => [[a]] -> [[a]] -> a -> Maybe [[[a]]]
getLoops' [[a]]
as [[a]
a] ([a] -> a
forall a. [a] -> a
last [a]
a)

-- | We will be actually doing the loop extraction with
-- getLoops'
--
-- getLoops' has a first argument of the segments as before,
-- but a *second argument* which is the loop presently being
-- built.
--
-- so we begin with the "building loop" being empty.
--
-- see also: 'getLoops'.
getLoops'
    :: Eq a
    => [[a]]   -- ^ input
    -> [[a]]   -- ^ accumulator
    -> a       -- ^ last element in the accumulator
    -> Maybe [[[a]]]

-- If there aren't any segments, and the "building loop" is empty, produce no loops.
getLoops' :: [[a]] -> [[a]] -> a -> Maybe [[[a]]]
getLoops' [] [] a
_ = [[[a]]] -> Maybe [[[a]]]
forall a. a -> Maybe a
Just []

-- If the building loop is empty, stick the first segment we have onto it to give us something to build on.
getLoops' ([a]
x:[[a]]
xs) [] a
_ = [[a]] -> [[a]] -> a -> Maybe [[[a]]]
forall a. Eq a => [[a]] -> [[a]] -> a -> Maybe [[[a]]]
getLoops' [[a]]
xs [[a]
x] ([a] -> a
forall a. [a] -> a
last [a]
x)

-- A loop is finished if its start and end are the same.
-- Return it and start searching for another loop.
getLoops' [[a]]
segs [[a]]
workingLoop a
ultima | [a] -> a
forall a. [a] -> a
head ([[a]] -> [a]
forall a. [a] -> a
head [[a]]
workingLoop) a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
ultima =
    ([[a]]
workingLoop [[a]] -> [[[a]]] -> [[[a]]]
forall a. a -> [a] -> [a]
:) ([[[a]]] -> [[[a]]]) -> Maybe [[[a]]] -> Maybe [[[a]]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [[a]] -> [[a]] -> a -> Maybe [[[a]]]
forall a. Eq a => [[a]] -> [[a]] -> a -> Maybe [[[a]]]
getLoops' [[a]]
segs [] a
ultima

-- Finally, we search for pieces that can continue the working loop,
-- and stick one on if we find it.
-- Otherwise... something is really screwed up.
getLoops' [[a]]
segs [[a]]
workingLoop a
ultima = do
    let
        presEnd :: [[a]] -> a
        presEnd :: [[a]] -> a
presEnd = [a] -> a
forall a. [a] -> a
last ([a] -> a) -> ([[a]] -> [a]) -> [[a]] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[a]] -> [a]
forall a. [a] -> a
last
        connects :: [a] -> Bool
connects (a
x:[a]
_) = a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== [[a]] -> a
forall a. [[a]] -> a
presEnd [[a]]
workingLoop
        -- Handle the empty case.
        connects [] = Bool
False
        -- divide our set into sequences that connect, and sequences that don't.
        ([[a]]
possibleConts, [[a]]
nonConts) = ([a] -> Bool) -> [[a]] -> ([[a]], [[a]])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition [a] -> Bool
connects [[a]]
segs
    case [[a]]
possibleConts of
      [] -> Maybe [[[a]]]
forall a. Maybe a
Nothing
      ([a]
next : [[a]]
conts) -> do
        let unused :: [[a]]
unused = [[a]]
conts [[a]] -> [[a]] -> [[a]]
forall a. Semigroup a => a -> a -> a
<> [[a]]
nonConts
        if [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
next
           then ([[a]]
workingLoop [[a]] -> [[[a]]] -> [[[a]]]
forall a. a -> [a] -> [a]
:) ([[[a]]] -> [[[a]]]) -> Maybe [[[a]]] -> Maybe [[[a]]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [[a]] -> [[a]] -> a -> Maybe [[[a]]]
forall a. Eq a => [[a]] -> [[a]] -> a -> Maybe [[[a]]]
getLoops' [[a]]
segs [] a
ultima
           else [[a]] -> [[a]] -> a -> Maybe [[[a]]]
forall a. Eq a => [[a]] -> [[a]] -> a -> Maybe [[[a]]]
getLoops' [[a]]
unused ([[a]]
workingLoop [[a]] -> [[a]] -> [[a]]
forall a. Semigroup a => a -> a -> a
<> [[a]
next]) ([a] -> a
forall a. [a] -> a
last [a]
next)