Tracklets.hs
module Frontend.Tracklets
( TrackletSOA
, trackletSOA
, unTrackletSOA
) where
import Prelude
import Data.Argonaut.Core (Json)
import Data.Argonaut.Decode (class DecodeJson, JsonDecodeError(..), decodeJson, (.:))
import Data.Array (length, (!!))
import Data.DateTime (DateTime)
import Data.Either (Either, note)
import Data.Maybe (Maybe(..))
import Frontend.Util (secondsToDateTime)
type TrackletSOAData =
{ id :: Int
, t :: Array Number
, x :: Array Number
, y :: Array Number
, t0 :: DateTime
}
newtype TrackletSOA = TrackletSOA TrackletSOAData
trackletSOA :: TrackletSOAData -> Maybe TrackletSOA
trackletSOA {id, t, x, y, t0}
| length t == length x && length x == length y = Just $ TrackletSOA {id, t, x, y, t0}
| otherwise = Nothing
unTrackletSOA :: TrackletSOA -> TrackletSOAData
unTrackletSOA (TrackletSOA d) = d
instance showTrackletSOA :: Show TrackletSOA where
show (TrackletSOA {t0, t, x, y}) = "Tracklet with " <> show (length t) <> " points starting at " <> show t0
instance decodeJsonTracklets :: DecodeJson TrackletSOA where
decodeJson :: Json -> Either JsonDecodeError TrackletSOA
decodeJson json = do
tracklet <- decodeJson json
id <- tracklet .: "id"
t <- tracklet .: "t"
x <- tracklet .: "x"
y <- tracklet .: "y"
t0s <- note (TypeMismatch "Empty tracklet.") $ t !! 0
t0 <- note (TypeMismatch "Invalid timestamp.") $ secondsToDateTime t0s
note (TypeMismatch "Lengths of {t, x, y} arrays are not equal.") $ trackletSOA
{id, t: (_ - t0s) <$> t, x, y, t0}