module PLY.Conf (parseConf, Transformation, Conf(..)) where
import Control.Applicative
import Data.Attoparsec.ByteString.Char8
import Data.ByteString (ByteString)
import Linear.V3
import Linear.Quaternion
import PLY.Internal.Parsers (line, word)
type Transformation a = (V3 a, Quaternion a)
data Conf a = Conf { camera :: Transformation a
, meshes :: [(ByteString, Transformation a)] }
deriving Show
transformation :: Fractional a => Parser (V3 a, Quaternion a)
transformation = (,) <$> vec <*> rotation
where vec = (\[x,y,z] -> V3 x y z) <$> count 3 (skipSpace *> double')
rotation = flip Quaternion <$> vec <*> (skipSpace *> double')
double' = realToFrac <$> double
mesh :: Fractional a => Parser (ByteString, Transformation a)
mesh = (,) <$> ("bmesh " *> word) <*> transformation
conf :: Fractional a => Parser (Conf a)
conf = Conf <$> ("camera " *> transformation <* skipSpace)
<*> (skipSpace *> cybmesh *> many1 (skipSpace *> mesh))
where cybmesh = skipMany ("mesh " *> line)
parseConf :: Fractional a => ByteString -> Either String (Conf a)
parseConf = parseOnly conf