force-layout 0.1.0.2 → 0.2
raw patch · 3 files changed
+32/−20 lines, 3 filesdep +lensdep −fclabelsPVP ok
version bump matches the API change (PVP)
Dependencies added: lens
Dependencies removed: fclabels
API changes (from Hackage documentation)
- Physics.ForceLayout: force :: Arrow arr => Lens arr (Particle v) v
+ Physics.ForceLayout: force :: Lens (Particle v_a4gP) (Particle v_a4gP) v_a4gP v_a4gP
- Physics.ForceLayout: forces :: Arrow arr => Lens arr (Ensemble v) [([Edge], Point v -> Point v -> v)]
+ Physics.ForceLayout: forces :: Lens (Ensemble v_a5c9) (Ensemble v_a5c9) [([Edge], Point v_a5c9 -> Point v_a5c9 -> v_a5c9)] [([Edge], Point v_a5c9 -> Point v_a5c9 -> v_a5c9)]
- Physics.ForceLayout: particles :: Arrow arr => Lens arr (Ensemble v) (Map PID (Particle v))
+ Physics.ForceLayout: particles :: Lens (Ensemble v_a5c9) (Ensemble v_a5c9) (Map PID (Particle v_a5c9)) (Map PID (Particle v_a5c9))
- Physics.ForceLayout: pos :: Arrow arr => Lens arr (Particle v) (Point v)
+ Physics.ForceLayout: pos :: Lens (Particle v_a4gP) (Particle v_a4gP) (Point v_a4gP) (Point v_a4gP)
- Physics.ForceLayout: vel :: Arrow arr => Lens arr (Particle v) v
+ Physics.ForceLayout: vel :: Lens (Particle v_a4gP) (Particle v_a4gP) v_a4gP v_a4gP
Files
- CHANGES +8/−0
- force-layout.cabal +2/−2
- src/Physics/ForceLayout.hs +22/−18
CHANGES view
@@ -1,3 +1,11 @@+* 0.2: 5 December 2012++ - Switch from fclabels to lens.++ Note this requires a major version bump because the accessors for+ Particle and Ensemble are exported, and they have completely+ different types. But the API has not otherwise changed.+ * 0.1.0.2: 23 August 2012 - Bump upper bounds to allow base-4.6 and containers-0.5
force-layout.cabal view
@@ -1,5 +1,5 @@ name: force-layout-version: 0.1.0.2+version: 0.2 synopsis: Simple force-directed layout description: Simulation engine for doing simple force-based layout, /e.g./ for trees or graphs. See the diagrams-contrib package@@ -23,7 +23,7 @@ newtype ==0.2.*, vector-space >=0.7 && <0.9, vector-space-points >= 0.1.1 && < 0.2,- fclabels >= 1.0 && < 1.2,+ lens >= 3 && < 4, containers >=0.4 && < 0.6 hs-source-dirs: src default-language: Haskell2010
src/Physics/ForceLayout.hs view
@@ -1,7 +1,6 @@-{-# LANGUAGE TemplateHaskell- , ScopedTypeVariables- , FlexibleContexts- #-}+{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE FlexibleContexts #-} ----------------------------------------------------------------------------- -- |@@ -15,6 +14,11 @@ -- -- To use, just create an 'Ensemble' like so: --+-- > import Physics.ForceLayout+-- > import qualified Data.Map as M+-- > import Data.AffineSpace.Point+-- >+-- > e :: Ensemble (Double, Double) -- > e = Ensemble [ (edges, hookeForce 0.05 4) -- > , (allPairs, coulombForce 1) -- > ]@@ -32,6 +36,7 @@ -- all intermediate states) or 'forceLayout' (to get only the ending -- state): --+-- > e' :: Ensemble (Double, Double) -- > e' = forceLayout (FLOpts { damping = 0.8 -- > , energyLimit = Just 0.001 -- > , stepLimit = Nothing@@ -40,7 +45,7 @@ -- > e -- -- See the diagrams-contrib package--- (<http://patch-tag.com/r/byorgey/diagrams-contrib/home>) for more+-- (<http://github.com/diagrams/diagrams-contrib/>) for more -- examples. ----------------------------------------------------------------------------- @@ -76,19 +81,18 @@ ) where import Control.Monad-import Control.Newtype+import Control.Newtype (ala) import Data.AffineSpace import Data.AffineSpace.Point import Data.Foldable (foldMap) import qualified Data.Foldable as F-import Data.Label (mkLabels)-import qualified Data.Label as L import qualified Data.Map as M import Data.Maybe import Data.Monoid import Data.VectorSpace hiding (Sum)-import Prelude +import Control.Lens hiding (ala)+ ------------------------------------------------------------ -- Particles ------------------------------------------------------------@@ -101,7 +105,7 @@ } deriving (Eq, Show) -mkLabels [''Particle]+makeLenses ''Particle -- | Create an initial particle at rest at a particular location. initParticle :: AdditiveGroup v => Point v -> Particle v@@ -126,7 +130,7 @@ , _particles :: M.Map PID (Particle v) } -mkLabels [''Ensemble]+makeLenses ''Ensemble ------------------------------------------------------------ -- Simulation internals@@ -135,20 +139,20 @@ -- | Simulate one time step for an entire ensemble, with the given -- damping factor. ensembleStep :: VectorSpace v => Scalar v -> Ensemble v -> Ensemble v-ensembleStep d = (L.modify particles . M.map) (particleStep d) . recalcForces+ensembleStep d = (over particles . M.map) (particleStep d) . recalcForces -- | Simulate one time step for a particle (assuming the force acting -- on it has already been computed), with the given damping factor. particleStep :: VectorSpace v => Scalar v -> Particle v -> Particle v particleStep d = stepPos . stepVel- where stepVel p = L.set vel (d *^ (L.get vel p ^+^ L.get force p)) p- stepPos p = L.modify pos (.+^ L.get vel p) p+ where stepVel p = vel .~ (d *^ (p^.vel ^+^ p^.force)) $ p+ stepPos p = pos %~ (.+^ p^.vel) $ p -- | Recalculate all the forces acting in the next time step of an -- ensemble. recalcForces :: forall v. AdditiveGroup v => Ensemble v -> Ensemble v recalcForces = calcForces . zeroForces- where zeroForces = L.modify particles . M.map $ L.set force zeroV+ where zeroForces = (particles %~) . M.map $ force .~ zeroV calcForces (Ensemble fs ps) = Ensemble fs (ala Endo foldMap (concatMap (\(es, f) -> (map (mkForce f) es)) fs) ps)@@ -156,14 +160,14 @@ mkForce f (i1, i2) m = case (M.lookup i1 m, M.lookup i2 m) of (Just p1, Just p2) ->- ( M.adjust (L.modify force (^+^ f (L.get pos p1) (L.get pos p2))) i1- . M.adjust (L.modify force (^-^ f (L.get pos p1) (L.get pos p2))) i2)+ ( M.adjust (force %~ (^+^ f (p1^.pos) (p2^.pos))) i1+ . M.adjust (force %~ (^-^ f (p1^.pos) (p2^.pos))) i2) m _ -> m -- | Compute the total kinetic energy of an ensemble. kineticEnergy :: (InnerSpace v, Num (Scalar v)) => Ensemble v -> Scalar v-kineticEnergy = ala Sum F.foldMap . fmap (magnitudeSq . L.get vel) . L.get particles+kineticEnergy = ala Sum F.foldMap . fmap (magnitudeSq . view vel) . view particles ------------------------------------------------------------ -- Simulation