boundingboxes (empty) → 0.1
raw patch · 4 files changed
+155/−0 lines, 4 filesdep +basedep +lensdep +linearsetup-changed
Dependencies added: base, lens, linear
Files
- Data/BoundingBox/Dim2.hs +101/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- boundingboxes.cabal +22/−0
+ Data/BoundingBox/Dim2.hs view
@@ -0,0 +1,101 @@+{-# LANGUAGE DeriveFunctor, DeriveFoldable, DeriveTraversable, DeriveDataTypeable, Rank2Types #-} +----------------------------------------------------------------------------- +-- | +-- Module : Data.BoundingBox.Dim2 +-- Copyright : (C) 2014 Fumiaki Kinoshita +-- License : BSD-style (see the file LICENSE) +-- +-- Maintainer : Fumiaki Kinoshita <fumiexcel@gmail.com> +-- Stability : provisional +-- Portability : non-portable +-- The type and accessors for 2D bounding boxes +---------------------------------------------------------------------------- +module Data.BoundingBox.Dim2 ( + BoundingBox(..) + , _TLBR + , _BLTR + , Reference(..) + , position + , size + ) where + +import Linear +import Control.Lens +import Data.Foldable +import Data.Typeable + +data BoundingBox a = BoundingBox a a a a deriving (Show, Eq, Ord, Functor, Foldable, Traversable, Read, Typeable) + +-- | The type of reference points. +-- @ +-- TL--T--TR +-- | | +-- L C R +-- | | +-- BL--B--BR +-- @ +data Reference = TL | T | TR + | L | C | R + | BL | B | BR + deriving (Show, Eq, Ord, Read) + +-- | +-- @ +-- fst----+ +-- | | +-- +----snd +-- @ +_TLBR :: Iso' (BoundingBox a) (V2 a, V2 a) +_TLBR = iso (\(BoundingBox x0 y0 x1 y1) -> (V2 x0 y0, V2 x1 y1)) (\(V2 x0 y0, V2 x1 y1) -> BoundingBox x0 y0 x1 y1) + +-- | +-- @ +-- +----snd +-- | | +-- fst----+ +-- @ +_BLTR :: Iso' (BoundingBox a) (V2 a, V2 a) +_BLTR = iso (\(BoundingBox x0 y0 x1 y1) -> (V2 x0 y1, V2 x1 y0)) (\(V2 x0 y1, V2 x1 y0) -> BoundingBox x0 y0 x1 y1) + +position :: Fractional a => Reference -> Lens' (BoundingBox a) (V2 a) +position ref f (BoundingBox x0 y0 x1 y1) = f (V2 x0 y0 + offset) + <&> \v -> let V2 x y = v - offset in BoundingBox x y (x + w) (y + h) where + w = x1 - x0 + h = y1 - y0 + offset = case ref of + TL -> V2 0 0 + T -> V2 (w / 2) 0 + TR -> V2 w 0 + L -> V2 0 (h / 2) + C -> V2 (w / 2) (h / 2) + R -> V2 w (h / 2) + BL -> V2 0 h + B -> V2 (w / 2) h + BR -> V2 w h + +size :: Fractional a => Reference -> Lens' (BoundingBox a) (V2 a) +size ref f (BoundingBox x0 y0 x1 y1) = f (V2 w h) + <&> \(V2 w' h') -> BoundingBox (x0 - p * (w' - w)) (y0 - q * (h' - h)) (x1 + (1 - p) * (w' - w)) (y1 + (1 - q) * (h' - h)) + where + w = x1 - x0 + h = y1 - y0 + p = case ref of + TL -> 0 + T -> 0.5 + TR -> 1 + L -> 0 + C -> 0.5 + R -> 1 + BL -> 0 + B -> 0.5 + BR -> 1 + q = case ref of + TL -> 0 + L -> 0.5 + BL -> 1 + T -> 0 + C -> 0.5 + B -> 1 + TR -> 0 + R -> 0.5 + BR -> 1
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Fumiaki Kinoshita + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Fumiaki Kinoshita nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple +main = defaultMain
+ boundingboxes.cabal view
@@ -0,0 +1,22 @@+name: boundingboxes +version: 0.1 +synopsis: The type for 2D bounding box +-- description: +homepage: https://github.com/fumieval/boundingboxes +license: BSD3 +license-file: LICENSE +author: Fumiaki Kinoshita +maintainer: fumiexcel@gmail.com +-- copyright: +category: Data +build-type: Simple +-- extra-source-files: +cabal-version: >=1.10 + +library + exposed-modules: Data.BoundingBox.Dim2 + -- other-modules: + other-extensions: DeriveFunctor, DeriveFoldable, DeriveTraversable, DeriveDataTypeable, Rank2Types + build-depends: base == 4.*, linear >= 1.0, lens >= 3.8 && < 5 + -- hs-source-dirs: + default-language: Haskell2010