module Data.Geo.Jord.Tx
(
Tx(..)
, inverse
, Params(..)
, Params7
, Rates
, Params15(..)
, params7
, rates
, paramsAt
, Graph
, graph
, paramsBetween
, apply
) where
import Data.List (find, foldl', sortOn)
import Data.Maybe (mapMaybe)
import qualified Data.Geo.Jord.Math3d as Math3d
import Data.Geo.Jord.Model (Epoch(..), ModelId)
data Tx a =
Tx
{ modelA :: ModelId
, modelB :: ModelId
, params :: a
}
inverse :: (Params a) => Tx a -> Tx a
inverse t = Tx (modelB t) (modelA t) (inverseParams (params t))
class Params a where
idParams :: a
inverseParams :: a -> a
data Params7 =
Params7 !Math3d.V3 !Double !Math3d.V3
deriving (Show)
instance Params Params7 where
idParams = Params7 Math3d.zero 0 Math3d.zero
inverseParams (Params7 c s r) = Params7 (Math3d.scale c (-1.0)) (-s) (Math3d.scale r (-1.0))
instance Params Params15 where
idParams = Params15 (Epoch 0) idParams (Rates Math3d.zero 0 Math3d.zero)
inverseParams (Params15 e p (Rates c s r)) =
Params15 e (inverseParams p) (Rates (Math3d.scale c (-1.0)) (-s) (Math3d.scale r (-1.0)))
data Rates =
Rates !Math3d.V3 !Double !Math3d.V3
deriving (Show)
data Params15 =
Params15 Epoch Params7 Rates
deriving (Show)
params7 ::
(Double, Double, Double)
-> Double
-> (Double, Double, Double)
-> Params7
params7 c s r = Params7 (mmToMetres c) (s / 1e9) (masToRadians r)
rates ::
(Double, Double, Double)
-> Double
-> (Double, Double, Double)
-> Rates
rates c s r = Rates (mmToMetres c) (s / 1e9) (masToRadians r)
mmToMetres :: (Double, Double, Double) -> Math3d.V3
mmToMetres (cx, cy, cz) = Math3d.scale (Math3d.vec3 cx cy cz) (1.0 / 1000.0)
masToRadians :: (Double, Double, Double) -> Math3d.V3
masToRadians (rx, ry, rz) = Math3d.scale (Math3d.vec3 rx ry rz) (pi / (3600.0 * 1000.0 * 180.0))
paramsAt :: Epoch -> Params15 -> Params7
paramsAt (Epoch e) (Params15 (Epoch pe) (Params7 c s r) (Rates rc rs rr)) = Params7 c' s' r'
where
de = e - pe
c' = Math3d.add c (Math3d.scale rc de)
s' = s + de * rs
r' = Math3d.add r (Math3d.scale rr de)
data Connection =
Connection
{ node :: !ModelId
, adjacents :: ![ModelId]
}
data Edge a =
Edge ModelId a ModelId
type Path = [ModelId]
data State =
State [ModelId] [Path]
data Graph a =
Graph ![Connection] ![Edge a]
graph :: (Params a) => [Tx a] -> Graph a
graph = foldl' addTx emptyGraph
paramsBetween :: (Params a) => ModelId -> ModelId -> Graph a -> [a]
paramsBetween m0 m1 g
| m0 == m1 = [idParams]
| null ms = []
| otherwise = findParams ms g
where
ms = dijkstra (State [m0] []) m1 g
emptyGraph :: Graph a
emptyGraph = Graph [] []
addTx :: (Params a) => Graph a -> Tx a -> Graph a
addTx (Graph cs es) t = Graph cs' es'
where
ma = modelA t
mb = modelB t
cs1 = addConnection cs ma mb
cs' = addConnection cs1 mb ma
txp = params t
es' = Edge ma txp mb : Edge mb (inverseParams txp) ma : es
addConnection :: [Connection] -> ModelId -> ModelId -> [Connection]
addConnection cs m1 m2
| null filtered = Connection m1 [m2] : cs
| otherwise =
map
(\c' ->
if node c' == m1
then updated
else c')
cs
where
filtered = filter (\c -> node c == m1) cs
cur = head filtered
updated = cur {adjacents = m2 : adjacents cur}
successors :: ModelId -> Graph a -> [ModelId]
successors m (Graph cs _) = concatMap adjacents (filter (\c -> node c == m) cs)
visit :: ModelId -> [ModelId] -> State -> State
visit f ms (State q0 v0) = State q1 v1
where
toVisit = filter (`notElem` concat v0) ms
fs = filter (\v -> head v == f) v0
q1 = q0 ++ toVisit
updatedPaths = concatMap (\x -> map (: x) toVisit) fs
v1 = updatedPaths ++ filter (\v -> head v /= f) v0
shortest :: ModelId -> ModelId -> [Path] -> [ModelId]
shortest c m ps = reverse (m : s)
where
fs = filter (\v -> head v == c) ps
s = head (sortOn length fs)
dijkstra :: State -> ModelId -> Graph a -> [ModelId]
dijkstra (State [] _) _ _ = []
dijkstra (State [c] []) t g = dijkstra (State [c] [[c]]) t g
dijkstra (State (c:r) v) t g
| t `elem` succs = shortest c t v
| otherwise = dijkstra s'' t g
where
s' = State r v
succs = successors c g
s'' = visit c succs s'
findParams :: [ModelId] -> Graph a -> [a]
findParams ms (Graph _ es)
| length ps == length r = r
| otherwise = []
where
ps = zip ms (tail ms)
r = mapMaybe (`findParam` es) ps
findParam :: (ModelId, ModelId) -> [Edge a] -> Maybe a
findParam p es = fmap (\(Edge _ pa _) -> pa) (find (edgeEq p) es)
edgeEq :: (ModelId, ModelId) -> Edge a -> Bool
edgeEq (m1, m2) (Edge m1' _ m2') = m1 == m1' && m2 == m2'
apply :: Math3d.V3 -> Params7 -> Math3d.V3
apply gc (Params7 c s r) = Math3d.add c (Math3d.scale (Math3d.multM gc (rotation r)) (1.0 + s))
rotation :: Math3d.V3 -> [Math3d.V3]
rotation v = [Math3d.vec3 1.0 (-z) y, Math3d.vec3 z 1.0 (-x), Math3d.vec3 (-y) x 1.0]
where
x = Math3d.v3x v
y = Math3d.v3y v
z = Math3d.v3z v