{-# OPTIONS_GHC -fplugin Foreign.Storable.Generic.Plugin #-}

module Resource.Mesh.Types
  ( AxisAligned(..)

  , Meta(..)

  , NodeGroup(..)
  , NodePartitions(..)

  , Nodes
  , Node(..)
  , TexturedNodes
  , TexturedNode(..)
  , TextureParams(..) -- XXX: copypasta from LitTextured

  , Measurements(..)
  , measureAa
  , measureAaWith
  , middle
  , middleAa
  , size
  , sizeAa

  , HasRange(..)

  , encodeStorable
  , decodeStorable
  ) where

import RIO

import Codec.Serialise qualified as CBOR
import Codec.Serialise.Decoding qualified as CBOR (Decoder, decodeBytes)
import Codec.Serialise.Encoding qualified as CBOR (Encoding)
import Control.Foldl qualified as L
import Data.ByteString.Unsafe qualified as BS
import Data.Typeable (typeRep, typeRepTyCon)
import Foreign (Storable(..), castPtr)
import Foreign qualified
import Foreign.Storable.Generic (GStorable)
import Geomancy (Transform(..), Vec2, Vec4, withVec3)
import Geomancy.Mat4 qualified as Mat4
import Geomancy.Vec3 qualified as Vec3
import RIO.Vector.Storable qualified as Storable
import System.IO.Unsafe (unsafePerformIO)
import Vulkan.Zero (Zero(..))

import Resource.Model (IndexRange(..))

data AxisAligned a = AxisAligned
  { forall a. AxisAligned a -> a
aaX :: a
  , forall a. AxisAligned a -> a
aaY :: a
  , forall a. AxisAligned a -> a
aaZ :: a
  }
  deriving (AxisAligned a -> AxisAligned a -> Bool
forall a. Eq a => AxisAligned a -> AxisAligned a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AxisAligned a -> AxisAligned a -> Bool
$c/= :: forall a. Eq a => AxisAligned a -> AxisAligned a -> Bool
== :: AxisAligned a -> AxisAligned a -> Bool
$c== :: forall a. Eq a => AxisAligned a -> AxisAligned a -> Bool
Eq, AxisAligned a -> AxisAligned a -> Bool
AxisAligned a -> AxisAligned a -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (AxisAligned a)
forall a. Ord a => AxisAligned a -> AxisAligned a -> Bool
forall a. Ord a => AxisAligned a -> AxisAligned a -> Ordering
forall a. Ord a => AxisAligned a -> AxisAligned a -> AxisAligned a
min :: AxisAligned a -> AxisAligned a -> AxisAligned a
$cmin :: forall a. Ord a => AxisAligned a -> AxisAligned a -> AxisAligned a
max :: AxisAligned a -> AxisAligned a -> AxisAligned a
$cmax :: forall a. Ord a => AxisAligned a -> AxisAligned a -> AxisAligned a
>= :: AxisAligned a -> AxisAligned a -> Bool
$c>= :: forall a. Ord a => AxisAligned a -> AxisAligned a -> Bool
> :: AxisAligned a -> AxisAligned a -> Bool
$c> :: forall a. Ord a => AxisAligned a -> AxisAligned a -> Bool
<= :: AxisAligned a -> AxisAligned a -> Bool
$c<= :: forall a. Ord a => AxisAligned a -> AxisAligned a -> Bool
< :: AxisAligned a -> AxisAligned a -> Bool
$c< :: forall a. Ord a => AxisAligned a -> AxisAligned a -> Bool
compare :: AxisAligned a -> AxisAligned a -> Ordering
$ccompare :: forall a. Ord a => AxisAligned a -> AxisAligned a -> Ordering
Ord, Int -> AxisAligned a -> ShowS
forall a. Show a => Int -> AxisAligned a -> ShowS
forall a. Show a => [AxisAligned a] -> ShowS
forall a. Show a => AxisAligned a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AxisAligned a] -> ShowS
$cshowList :: forall a. Show a => [AxisAligned a] -> ShowS
show :: AxisAligned a -> String
$cshow :: forall a. Show a => AxisAligned a -> String
showsPrec :: Int -> AxisAligned a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> AxisAligned a -> ShowS
Show, forall a b. a -> AxisAligned b -> AxisAligned a
forall a b. (a -> b) -> AxisAligned a -> AxisAligned b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> AxisAligned b -> AxisAligned a
$c<$ :: forall a b. a -> AxisAligned b -> AxisAligned a
fmap :: forall a b. (a -> b) -> AxisAligned a -> AxisAligned b
$cfmap :: forall a b. (a -> b) -> AxisAligned a -> AxisAligned b
Functor, forall a. Eq a => a -> AxisAligned a -> Bool
forall a. Num a => AxisAligned a -> a
forall a. Ord a => AxisAligned a -> a
forall m. Monoid m => AxisAligned m -> m
forall a. AxisAligned a -> Bool
forall a. AxisAligned a -> Int
forall a. AxisAligned a -> [a]
forall a. (a -> a -> a) -> AxisAligned a -> a
forall m a. Monoid m => (a -> m) -> AxisAligned a -> m
forall b a. (b -> a -> b) -> b -> AxisAligned a -> b
forall a b. (a -> b -> b) -> b -> AxisAligned a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => AxisAligned a -> a
$cproduct :: forall a. Num a => AxisAligned a -> a
sum :: forall a. Num a => AxisAligned a -> a
$csum :: forall a. Num a => AxisAligned a -> a
minimum :: forall a. Ord a => AxisAligned a -> a
$cminimum :: forall a. Ord a => AxisAligned a -> a
maximum :: forall a. Ord a => AxisAligned a -> a
$cmaximum :: forall a. Ord a => AxisAligned a -> a
elem :: forall a. Eq a => a -> AxisAligned a -> Bool
$celem :: forall a. Eq a => a -> AxisAligned a -> Bool
length :: forall a. AxisAligned a -> Int
$clength :: forall a. AxisAligned a -> Int
null :: forall a. AxisAligned a -> Bool
$cnull :: forall a. AxisAligned a -> Bool
toList :: forall a. AxisAligned a -> [a]
$ctoList :: forall a. AxisAligned a -> [a]
foldl1 :: forall a. (a -> a -> a) -> AxisAligned a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> AxisAligned a -> a
foldr1 :: forall a. (a -> a -> a) -> AxisAligned a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> AxisAligned a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> AxisAligned a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> AxisAligned a -> b
foldl :: forall b a. (b -> a -> b) -> b -> AxisAligned a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> AxisAligned a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> AxisAligned a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> AxisAligned a -> b
foldr :: forall a b. (a -> b -> b) -> b -> AxisAligned a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> AxisAligned a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> AxisAligned a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> AxisAligned a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> AxisAligned a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> AxisAligned a -> m
fold :: forall m. Monoid m => AxisAligned m -> m
$cfold :: forall m. Monoid m => AxisAligned m -> m
Foldable, Functor AxisAligned
Foldable AxisAligned
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
AxisAligned (m a) -> m (AxisAligned a)
forall (f :: * -> *) a.
Applicative f =>
AxisAligned (f a) -> f (AxisAligned a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> AxisAligned a -> m (AxisAligned b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> AxisAligned a -> f (AxisAligned b)
sequence :: forall (m :: * -> *) a.
Monad m =>
AxisAligned (m a) -> m (AxisAligned a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
AxisAligned (m a) -> m (AxisAligned a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> AxisAligned a -> m (AxisAligned b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> AxisAligned a -> m (AxisAligned b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
AxisAligned (f a) -> f (AxisAligned a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
AxisAligned (f a) -> f (AxisAligned a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> AxisAligned a -> f (AxisAligned b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> AxisAligned a -> f (AxisAligned b)
Traversable, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (AxisAligned a) x -> AxisAligned a
forall a x. AxisAligned a -> Rep (AxisAligned a) x
$cto :: forall a x. Rep (AxisAligned a) x -> AxisAligned a
$cfrom :: forall a x. AxisAligned a -> Rep (AxisAligned a) x
Generic)

instance Applicative AxisAligned where
  pure :: forall a. a -> AxisAligned a
pure a
x = AxisAligned
    { $sel:aaX:AxisAligned :: a
aaX = a
x
    , $sel:aaY:AxisAligned :: a
aaY = a
x
    , $sel:aaZ:AxisAligned :: a
aaZ = a
x
    }

  AxisAligned (a -> b)
funcs <*> :: forall a b. AxisAligned (a -> b) -> AxisAligned a -> AxisAligned b
<*> AxisAligned a
args = AxisAligned
    { $sel:aaX:AxisAligned :: b
aaX = forall a. AxisAligned a -> a
aaX AxisAligned (a -> b)
funcs forall a b. (a -> b) -> a -> b
$ forall a. AxisAligned a -> a
aaX AxisAligned a
args
    , $sel:aaY:AxisAligned :: b
aaY = forall a. AxisAligned a -> a
aaY AxisAligned (a -> b)
funcs forall a b. (a -> b) -> a -> b
$ forall a. AxisAligned a -> a
aaY AxisAligned a
args
    , $sel:aaZ:AxisAligned :: b
aaZ = forall a. AxisAligned a -> a
aaZ AxisAligned (a -> b)
funcs forall a b. (a -> b) -> a -> b
$ forall a. AxisAligned a -> a
aaZ AxisAligned a
args
    }

instance Storable a => Storable (AxisAligned a) where
  alignment :: AxisAligned a -> Int
alignment ~AxisAligned a
_ = forall a. Storable a => a -> Int
alignment (forall a. HasCallStack => String -> a
error String
"AxisAligned.alignment" :: a)

  sizeOf :: AxisAligned a -> Int
sizeOf ~AxisAligned a
_ = Int
3 forall a. Num a => a -> a -> a
* forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => String -> a
error String
"AxisAligned.sizeOf" :: a)

  peek :: Ptr (AxisAligned a) -> IO (AxisAligned a)
peek Ptr (AxisAligned a)
ptr = do
    a
aaX <- forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr (AxisAligned a)
ptr) Int
0
    a
aaY <- forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr (AxisAligned a)
ptr) Int
1
    a
aaZ <- forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr (AxisAligned a)
ptr) Int
2
    pure AxisAligned{a
aaZ :: a
aaY :: a
aaX :: a
$sel:aaZ:AxisAligned :: a
$sel:aaY:AxisAligned :: a
$sel:aaX:AxisAligned :: a
..}

  poke :: Ptr (AxisAligned a) -> AxisAligned a -> IO ()
poke Ptr (AxisAligned a)
ptr AxisAligned{a
aaZ :: a
aaY :: a
aaX :: a
$sel:aaZ:AxisAligned :: forall a. AxisAligned a -> a
$sel:aaY:AxisAligned :: forall a. AxisAligned a -> a
$sel:aaX:AxisAligned :: forall a. AxisAligned a -> a
..} = do
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr (AxisAligned a)
ptr) Int
0 a
aaX
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr (AxisAligned a)
ptr) Int
1 a
aaY
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr (AxisAligned a)
ptr) Int
2 a
aaZ

instance (CBOR.Serialise a) => CBOR.Serialise (AxisAligned a)

-- * Whole-scene metadata

data Meta = Meta
  { -- XXX: full-scene draws
    Meta -> IndexRange
mOpaqueIndices  :: IndexRange
  , Meta -> IndexRange
mBlendedIndices :: IndexRange

    -- XXX: per-node draws
  , Meta -> IndexRange
mOpaqueNodes    :: IndexRange
  , Meta -> IndexRange
mBlendedNodes   :: IndexRange

  , Meta -> Vec4
mBoundingSphere :: Vec4
  , Meta -> Transform
mTransformBB    :: Transform
  , Meta -> AxisAligned Measurements
mMeasurements   :: AxisAligned Measurements
  }
  deriving (Int -> Meta -> ShowS
[Meta] -> ShowS
Meta -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Meta] -> ShowS
$cshowList :: [Meta] -> ShowS
show :: Meta -> String
$cshow :: Meta -> String
showsPrec :: Int -> Meta -> ShowS
$cshowsPrec :: Int -> Meta -> ShowS
Show, forall x. Rep Meta x -> Meta
forall x. Meta -> Rep Meta x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Meta x -> Meta
$cfrom :: forall x. Meta -> Rep Meta x
Generic)

instance GStorable Meta

instance Eq Meta where
  Meta
a == :: Meta -> Meta -> Bool
== Meta
b = forall (t :: * -> *). Foldable t => t Bool -> Bool
and
    [ Meta -> Vec4
mBoundingSphere Meta
a forall a. Eq a => a -> a -> Bool
== Meta -> Vec4
mBoundingSphere Meta
b
    , Meta -> IndexRange
mOpaqueIndices  Meta
a forall a. Eq a => a -> a -> Bool
== Meta -> IndexRange
mOpaqueIndices  Meta
b
    , Meta -> IndexRange
mBlendedIndices Meta
a forall a. Eq a => a -> a -> Bool
== Meta -> IndexRange
mBlendedIndices Meta
b
    , Meta -> IndexRange
mOpaqueNodes    Meta
a forall a. Eq a => a -> a -> Bool
== Meta -> IndexRange
mOpaqueNodes    Meta
b
    , Meta -> IndexRange
mBlendedNodes   Meta
a forall a. Eq a => a -> a -> Bool
== Meta -> IndexRange
mBlendedNodes   Meta
b
    , Meta -> AxisAligned Measurements
mMeasurements   Meta
a forall a. Eq a => a -> a -> Bool
== Meta -> AxisAligned Measurements
mMeasurements   Meta
b

    , forall a. Coercible a Mat4 => a -> [Float]
Mat4.toListRowMajor (Meta -> Transform
mTransformBB Meta
a) forall a. Eq a => a -> a -> Bool
==
      forall a. Coercible a Mat4 => a -> [Float]
Mat4.toListRowMajor (Meta -> Transform
mTransformBB Meta
b)
    ]

instance CBOR.Serialise Meta where
  encode :: Meta -> Encoding
encode Meta{Transform
Vec4
IndexRange
AxisAligned Measurements
mMeasurements :: AxisAligned Measurements
mTransformBB :: Transform
mBoundingSphere :: Vec4
mBlendedNodes :: IndexRange
mOpaqueNodes :: IndexRange
mBlendedIndices :: IndexRange
mOpaqueIndices :: IndexRange
$sel:mMeasurements:Meta :: Meta -> AxisAligned Measurements
$sel:mTransformBB:Meta :: Meta -> Transform
$sel:mBoundingSphere:Meta :: Meta -> Vec4
$sel:mBlendedNodes:Meta :: Meta -> IndexRange
$sel:mOpaqueNodes:Meta :: Meta -> IndexRange
$sel:mBlendedIndices:Meta :: Meta -> IndexRange
$sel:mOpaqueIndices:Meta :: Meta -> IndexRange
..} = forall a. Monoid a => [a] -> a
mconcat
    [ forall a. Serialise a => a -> Encoding
CBOR.encode    IndexRange
mOpaqueIndices
    , forall a. Serialise a => a -> Encoding
CBOR.encode    IndexRange
mBlendedIndices
    , forall a. Serialise a => a -> Encoding
CBOR.encode    IndexRange
mOpaqueNodes
    , forall a. Serialise a => a -> Encoding
CBOR.encode    IndexRange
mBlendedNodes
    , forall a. Storable a => a -> Encoding
encodeStorable Vec4
mBoundingSphere
    , forall a. Storable a => a -> Encoding
encodeStorable Transform
mTransformBB
    , forall a. Serialise a => a -> Encoding
CBOR.encode    AxisAligned Measurements
mMeasurements
    ]

  decode :: forall s. Decoder s Meta
decode = do
    IndexRange
mOpaqueIndices  <- forall a s. Serialise a => Decoder s a
CBOR.decode
    IndexRange
mBlendedIndices <- forall a s. Serialise a => Decoder s a
CBOR.decode
    IndexRange
mOpaqueNodes    <- forall a s. Serialise a => Decoder s a
CBOR.decode
    IndexRange
mBlendedNodes   <- forall a s. Serialise a => Decoder s a
CBOR.decode
    Vec4
mBoundingSphere <- forall a s. (Storable a, Typeable a) => Decoder s a
decodeStorable
    Transform
mTransformBB    <- forall a s. (Storable a, Typeable a) => Decoder s a
decodeStorable
    AxisAligned Measurements
mMeasurements   <- forall a s. Serialise a => Decoder s a
CBOR.decode
    pure Meta{Transform
Vec4
IndexRange
AxisAligned Measurements
mMeasurements :: AxisAligned Measurements
mTransformBB :: Transform
mBoundingSphere :: Vec4
mBlendedNodes :: IndexRange
mOpaqueNodes :: IndexRange
mBlendedIndices :: IndexRange
mOpaqueIndices :: IndexRange
$sel:mMeasurements:Meta :: AxisAligned Measurements
$sel:mTransformBB:Meta :: Transform
$sel:mBoundingSphere:Meta :: Vec4
$sel:mBlendedNodes:Meta :: IndexRange
$sel:mOpaqueNodes:Meta :: IndexRange
$sel:mBlendedIndices:Meta :: IndexRange
$sel:mOpaqueIndices:Meta :: IndexRange
..}

-- * Scene parts

data NodeGroup
  = NodeOpaque
  | NodeBlended
  -- TODO: NodeCutout
  deriving (NodeGroup -> NodeGroup -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NodeGroup -> NodeGroup -> Bool
$c/= :: NodeGroup -> NodeGroup -> Bool
== :: NodeGroup -> NodeGroup -> Bool
$c== :: NodeGroup -> NodeGroup -> Bool
Eq, Eq NodeGroup
NodeGroup -> NodeGroup -> Bool
NodeGroup -> NodeGroup -> Ordering
NodeGroup -> NodeGroup -> NodeGroup
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: NodeGroup -> NodeGroup -> NodeGroup
$cmin :: NodeGroup -> NodeGroup -> NodeGroup
max :: NodeGroup -> NodeGroup -> NodeGroup
$cmax :: NodeGroup -> NodeGroup -> NodeGroup
>= :: NodeGroup -> NodeGroup -> Bool
$c>= :: NodeGroup -> NodeGroup -> Bool
> :: NodeGroup -> NodeGroup -> Bool
$c> :: NodeGroup -> NodeGroup -> Bool
<= :: NodeGroup -> NodeGroup -> Bool
$c<= :: NodeGroup -> NodeGroup -> Bool
< :: NodeGroup -> NodeGroup -> Bool
$c< :: NodeGroup -> NodeGroup -> Bool
compare :: NodeGroup -> NodeGroup -> Ordering
$ccompare :: NodeGroup -> NodeGroup -> Ordering
Ord, Int -> NodeGroup -> ShowS
[NodeGroup] -> ShowS
NodeGroup -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NodeGroup] -> ShowS
$cshowList :: [NodeGroup] -> ShowS
show :: NodeGroup -> String
$cshow :: NodeGroup -> String
showsPrec :: Int -> NodeGroup -> ShowS
$cshowsPrec :: Int -> NodeGroup -> ShowS
Show, Int -> NodeGroup
NodeGroup -> Int
NodeGroup -> [NodeGroup]
NodeGroup -> NodeGroup
NodeGroup -> NodeGroup -> [NodeGroup]
NodeGroup -> NodeGroup -> NodeGroup -> [NodeGroup]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: NodeGroup -> NodeGroup -> NodeGroup -> [NodeGroup]
$cenumFromThenTo :: NodeGroup -> NodeGroup -> NodeGroup -> [NodeGroup]
enumFromTo :: NodeGroup -> NodeGroup -> [NodeGroup]
$cenumFromTo :: NodeGroup -> NodeGroup -> [NodeGroup]
enumFromThen :: NodeGroup -> NodeGroup -> [NodeGroup]
$cenumFromThen :: NodeGroup -> NodeGroup -> [NodeGroup]
enumFrom :: NodeGroup -> [NodeGroup]
$cenumFrom :: NodeGroup -> [NodeGroup]
fromEnum :: NodeGroup -> Int
$cfromEnum :: NodeGroup -> Int
toEnum :: Int -> NodeGroup
$ctoEnum :: Int -> NodeGroup
pred :: NodeGroup -> NodeGroup
$cpred :: NodeGroup -> NodeGroup
succ :: NodeGroup -> NodeGroup
$csucc :: NodeGroup -> NodeGroup
Enum, NodeGroup
forall a. a -> a -> Bounded a
maxBound :: NodeGroup
$cmaxBound :: NodeGroup
minBound :: NodeGroup
$cminBound :: NodeGroup
Bounded)

data NodePartitions a = NodePartitions
  { forall a. NodePartitions a -> a
npOpaque  :: a
  , forall a. NodePartitions a -> a
npBlended :: a
  }
  deriving (NodePartitions a -> NodePartitions a -> Bool
forall a. Eq a => NodePartitions a -> NodePartitions a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NodePartitions a -> NodePartitions a -> Bool
$c/= :: forall a. Eq a => NodePartitions a -> NodePartitions a -> Bool
== :: NodePartitions a -> NodePartitions a -> Bool
$c== :: forall a. Eq a => NodePartitions a -> NodePartitions a -> Bool
Eq, Int -> NodePartitions a -> ShowS
forall a. Show a => Int -> NodePartitions a -> ShowS
forall a. Show a => [NodePartitions a] -> ShowS
forall a. Show a => NodePartitions a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NodePartitions a] -> ShowS
$cshowList :: forall a. Show a => [NodePartitions a] -> ShowS
show :: NodePartitions a -> String
$cshow :: forall a. Show a => NodePartitions a -> String
showsPrec :: Int -> NodePartitions a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> NodePartitions a -> ShowS
Show, forall a b. a -> NodePartitions b -> NodePartitions a
forall a b. (a -> b) -> NodePartitions a -> NodePartitions b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> NodePartitions b -> NodePartitions a
$c<$ :: forall a b. a -> NodePartitions b -> NodePartitions a
fmap :: forall a b. (a -> b) -> NodePartitions a -> NodePartitions b
$cfmap :: forall a b. (a -> b) -> NodePartitions a -> NodePartitions b
Functor, forall a. Eq a => a -> NodePartitions a -> Bool
forall a. Num a => NodePartitions a -> a
forall a. Ord a => NodePartitions a -> a
forall m. Monoid m => NodePartitions m -> m
forall a. NodePartitions a -> Bool
forall a. NodePartitions a -> Int
forall a. NodePartitions a -> [a]
forall a. (a -> a -> a) -> NodePartitions a -> a
forall m a. Monoid m => (a -> m) -> NodePartitions a -> m
forall b a. (b -> a -> b) -> b -> NodePartitions a -> b
forall a b. (a -> b -> b) -> b -> NodePartitions a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => NodePartitions a -> a
$cproduct :: forall a. Num a => NodePartitions a -> a
sum :: forall a. Num a => NodePartitions a -> a
$csum :: forall a. Num a => NodePartitions a -> a
minimum :: forall a. Ord a => NodePartitions a -> a
$cminimum :: forall a. Ord a => NodePartitions a -> a
maximum :: forall a. Ord a => NodePartitions a -> a
$cmaximum :: forall a. Ord a => NodePartitions a -> a
elem :: forall a. Eq a => a -> NodePartitions a -> Bool
$celem :: forall a. Eq a => a -> NodePartitions a -> Bool
length :: forall a. NodePartitions a -> Int
$clength :: forall a. NodePartitions a -> Int
null :: forall a. NodePartitions a -> Bool
$cnull :: forall a. NodePartitions a -> Bool
toList :: forall a. NodePartitions a -> [a]
$ctoList :: forall a. NodePartitions a -> [a]
foldl1 :: forall a. (a -> a -> a) -> NodePartitions a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> NodePartitions a -> a
foldr1 :: forall a. (a -> a -> a) -> NodePartitions a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> NodePartitions a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> NodePartitions a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> NodePartitions a -> b
foldl :: forall b a. (b -> a -> b) -> b -> NodePartitions a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> NodePartitions a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> NodePartitions a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> NodePartitions a -> b
foldr :: forall a b. (a -> b -> b) -> b -> NodePartitions a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> NodePartitions a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> NodePartitions a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> NodePartitions a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> NodePartitions a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> NodePartitions a -> m
fold :: forall m. Monoid m => NodePartitions m -> m
$cfold :: forall m. Monoid m => NodePartitions m -> m
Foldable, Functor NodePartitions
Foldable NodePartitions
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
NodePartitions (m a) -> m (NodePartitions a)
forall (f :: * -> *) a.
Applicative f =>
NodePartitions (f a) -> f (NodePartitions a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> NodePartitions a -> m (NodePartitions b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NodePartitions a -> f (NodePartitions b)
sequence :: forall (m :: * -> *) a.
Monad m =>
NodePartitions (m a) -> m (NodePartitions a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
NodePartitions (m a) -> m (NodePartitions a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> NodePartitions a -> m (NodePartitions b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> NodePartitions a -> m (NodePartitions b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
NodePartitions (f a) -> f (NodePartitions a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
NodePartitions (f a) -> f (NodePartitions a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NodePartitions a -> f (NodePartitions b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NodePartitions a -> f (NodePartitions b)
Traversable)

type Nodes = Storable.Vector Node

data Node = Node
  { Node -> Vec4
nBoundingSphere :: Vec4
  , Node -> Transform
nTransformBB    :: Transform
  , Node -> IndexRange
nRange          :: IndexRange
  , Node -> AxisAligned Measurements
nMeasurements   :: AxisAligned Measurements
  }
  deriving (Int -> Node -> ShowS
[Node] -> ShowS
Node -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Node] -> ShowS
$cshowList :: [Node] -> ShowS
show :: Node -> String
$cshow :: Node -> String
showsPrec :: Int -> Node -> ShowS
$cshowsPrec :: Int -> Node -> ShowS
Show, forall x. Rep Node x -> Node
forall x. Node -> Rep Node x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Node x -> Node
$cfrom :: forall x. Node -> Rep Node x
Generic)

instance Eq Node where
  Node
a == :: Node -> Node -> Bool
== Node
b = forall (t :: * -> *). Foldable t => t Bool -> Bool
and
    [ Node -> Vec4
nBoundingSphere Node
a forall a. Eq a => a -> a -> Bool
== Node -> Vec4
nBoundingSphere Node
b
    , Node -> AxisAligned Measurements
nMeasurements   Node
a forall a. Eq a => a -> a -> Bool
== Node -> AxisAligned Measurements
nMeasurements   Node
b
    , Node -> IndexRange
nRange          Node
a forall a. Eq a => a -> a -> Bool
== Node -> IndexRange
nRange          Node
b

    , forall a. Coercible a Mat4 => a -> [Float]
Mat4.toListRowMajor (Node -> Transform
nTransformBB Node
a) forall a. Eq a => a -> a -> Bool
==
      forall a. Coercible a Mat4 => a -> [Float]
Mat4.toListRowMajor (Node -> Transform
nTransformBB Node
b)
    ]

instance GStorable Node

type TexturedNodes = Storable.Vector TexturedNode

data TexturedNode = TexturedNode
  { TexturedNode -> Node
tnNode              :: Node
  , TexturedNode -> TextureParams
tnBase              :: TextureParams
  , TexturedNode -> TextureParams
tnEmissive          :: TextureParams
  , TexturedNode -> TextureParams
tnNormal            :: TextureParams
  , TexturedNode -> TextureParams
tnOcclusion         :: TextureParams
  , TexturedNode -> TextureParams
tnMetallicRoughness :: TextureParams
  }
  deriving (TexturedNode -> TexturedNode -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TexturedNode -> TexturedNode -> Bool
$c/= :: TexturedNode -> TexturedNode -> Bool
== :: TexturedNode -> TexturedNode -> Bool
$c== :: TexturedNode -> TexturedNode -> Bool
Eq, Int -> TexturedNode -> ShowS
[TexturedNode] -> ShowS
TexturedNode -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TexturedNode] -> ShowS
$cshowList :: [TexturedNode] -> ShowS
show :: TexturedNode -> String
$cshow :: TexturedNode -> String
showsPrec :: Int -> TexturedNode -> ShowS
$cshowsPrec :: Int -> TexturedNode -> ShowS
Show, forall x. Rep TexturedNode x -> TexturedNode
forall x. TexturedNode -> Rep TexturedNode x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TexturedNode x -> TexturedNode
$cfrom :: forall x. TexturedNode -> Rep TexturedNode x
Generic)

instance GStorable TexturedNode

-- XXX: copypasta from LitTextured.Model
data TextureParams = TextureParams
  { TextureParams -> Vec2
tpScale     :: Vec2
  , TextureParams -> Vec2
tpOffset    :: Vec2
  , TextureParams -> Vec4
tpGamma     :: Vec4
  , TextureParams -> Int32
tpSamplerId :: Int32
  , TextureParams -> Int32
tpTextureId :: Int32
  }
  deriving (TextureParams -> TextureParams -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TextureParams -> TextureParams -> Bool
$c/= :: TextureParams -> TextureParams -> Bool
== :: TextureParams -> TextureParams -> Bool
$c== :: TextureParams -> TextureParams -> Bool
Eq, Int -> TextureParams -> ShowS
[TextureParams] -> ShowS
TextureParams -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TextureParams] -> ShowS
$cshowList :: [TextureParams] -> ShowS
show :: TextureParams -> String
$cshow :: TextureParams -> String
showsPrec :: Int -> TextureParams -> ShowS
$cshowsPrec :: Int -> TextureParams -> ShowS
Show, forall x. Rep TextureParams x -> TextureParams
forall x. TextureParams -> Rep TextureParams x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TextureParams x -> TextureParams
$cfrom :: forall x. TextureParams -> Rep TextureParams x
Generic)

instance Zero TextureParams where
  zero :: TextureParams
zero = TextureParams
    { $sel:tpScale:TextureParams :: Vec2
tpScale     = Vec2
1
    , $sel:tpOffset:TextureParams :: Vec2
tpOffset    = Vec2
0
    , $sel:tpGamma:TextureParams :: Vec4
tpGamma     = Vec4
1.0
    , $sel:tpSamplerId:TextureParams :: Int32
tpSamplerId = forall a. Bounded a => a
minBound
    , $sel:tpTextureId:TextureParams :: Int32
tpTextureId = forall a. Bounded a => a
minBound
    }

instance Storable TextureParams where
  alignment :: TextureParams -> Int
alignment ~TextureParams
_ = Int
4

  sizeOf :: TextureParams -> Int
sizeOf ~TextureParams
_ = Int
8 forall a. Num a => a -> a -> a
+ Int
8 forall a. Num a => a -> a -> a
+ Int
16 forall a. Num a => a -> a -> a
+ Int
4 forall a. Num a => a -> a -> a
+ Int
4

  poke :: Ptr TextureParams -> TextureParams -> IO ()
poke Ptr TextureParams
ptr TextureParams{Int32
Vec2
Vec4
tpTextureId :: Int32
tpSamplerId :: Int32
tpGamma :: Vec4
tpOffset :: Vec2
tpScale :: Vec2
$sel:tpTextureId:TextureParams :: TextureParams -> Int32
$sel:tpSamplerId:TextureParams :: TextureParams -> Int32
$sel:tpGamma:TextureParams :: TextureParams -> Vec4
$sel:tpOffset:TextureParams :: TextureParams -> Vec2
$sel:tpScale:TextureParams :: TextureParams -> Vec2
..} = do
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr TextureParams
ptr  Int
0 Vec2
tpScale
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr TextureParams
ptr  Int
8 Vec2
tpOffset
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr TextureParams
ptr Int
16 Vec4
tpGamma
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr TextureParams
ptr Int
32 Int32
tpSamplerId
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr TextureParams
ptr Int
36 Int32
tpTextureId

  peek :: Ptr TextureParams -> IO TextureParams
peek Ptr TextureParams
ptr = do
    Vec2
tpScale     <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr TextureParams
ptr  Int
0
    Vec2
tpOffset    <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr TextureParams
ptr  Int
8
    Vec4
tpGamma     <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr TextureParams
ptr Int
16
    Int32
tpSamplerId <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr TextureParams
ptr Int
32
    Int32
tpTextureId <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr TextureParams
ptr Int
36
    pure TextureParams{Int32
Vec2
Vec4
tpTextureId :: Int32
tpSamplerId :: Int32
tpGamma :: Vec4
tpOffset :: Vec2
tpScale :: Vec2
$sel:tpTextureId:TextureParams :: Int32
$sel:tpSamplerId:TextureParams :: Int32
$sel:tpGamma:TextureParams :: Vec4
$sel:tpOffset:TextureParams :: Vec2
$sel:tpScale:TextureParams :: Vec2
..}

-- * Measurements

data Measurements = Measurements
  { Measurements -> Float
mMin  :: Float
  , Measurements -> Float
mMax  :: Float
  , Measurements -> Float
mMean :: Float
  , Measurements -> Float
mStd  :: Float
  }
  deriving (Measurements -> Measurements -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Measurements -> Measurements -> Bool
$c/= :: Measurements -> Measurements -> Bool
== :: Measurements -> Measurements -> Bool
$c== :: Measurements -> Measurements -> Bool
Eq, Eq Measurements
Measurements -> Measurements -> Bool
Measurements -> Measurements -> Ordering
Measurements -> Measurements -> Measurements
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Measurements -> Measurements -> Measurements
$cmin :: Measurements -> Measurements -> Measurements
max :: Measurements -> Measurements -> Measurements
$cmax :: Measurements -> Measurements -> Measurements
>= :: Measurements -> Measurements -> Bool
$c>= :: Measurements -> Measurements -> Bool
> :: Measurements -> Measurements -> Bool
$c> :: Measurements -> Measurements -> Bool
<= :: Measurements -> Measurements -> Bool
$c<= :: Measurements -> Measurements -> Bool
< :: Measurements -> Measurements -> Bool
$c< :: Measurements -> Measurements -> Bool
compare :: Measurements -> Measurements -> Ordering
$ccompare :: Measurements -> Measurements -> Ordering
Ord, Int -> Measurements -> ShowS
[Measurements] -> ShowS
Measurements -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Measurements] -> ShowS
$cshowList :: [Measurements] -> ShowS
show :: Measurements -> String
$cshow :: Measurements -> String
showsPrec :: Int -> Measurements -> ShowS
$cshowsPrec :: Int -> Measurements -> ShowS
Show, forall x. Rep Measurements x -> Measurements
forall x. Measurements -> Rep Measurements x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Measurements x -> Measurements
$cfrom :: forall x. Measurements -> Rep Measurements x
Generic)

instance Storable Measurements where
  alignment :: Measurements -> Int
alignment ~Measurements
_ = Int
4 -- XXX: 16?

  sizeOf :: Measurements -> Int
sizeOf ~Measurements
_ = Int
4 forall a. Num a => a -> a -> a
* Int
4

  peek :: Ptr Measurements -> IO Measurements
peek Ptr Measurements
ptr = do
    Float
mMin  <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Measurements
ptr  Int
0
    Float
mMax  <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Measurements
ptr  Int
4
    Float
mMean <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Measurements
ptr  Int
8
    Float
mStd  <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Measurements
ptr Int
12
    pure Measurements{Float
mStd :: Float
mMean :: Float
mMax :: Float
mMin :: Float
$sel:mStd:Measurements :: Float
$sel:mMean:Measurements :: Float
$sel:mMax:Measurements :: Float
$sel:mMin:Measurements :: Float
..}

  poke :: Ptr Measurements -> Measurements -> IO ()
poke Ptr Measurements
ptr Measurements{Float
mStd :: Float
mMean :: Float
mMax :: Float
mMin :: Float
$sel:mStd:Measurements :: Measurements -> Float
$sel:mMean:Measurements :: Measurements -> Float
$sel:mMax:Measurements :: Measurements -> Float
$sel:mMin:Measurements :: Measurements -> Float
..} = do
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Measurements
ptr  Int
0 Float
mMin
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Measurements
ptr  Int
4 Float
mMax
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Measurements
ptr  Int
8 Float
mMean
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Measurements
ptr Int
12 Float
mStd

instance CBOR.Serialise Measurements

{-# INLINEABLE middleAa #-}
middleAa :: AxisAligned Measurements -> AxisAligned Float
middleAa :: AxisAligned Measurements -> AxisAligned Float
middleAa = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Measurements -> Float
middle

{-# INLINEABLE middle #-}
middle :: Measurements -> Float
middle :: Measurements -> Float
middle Measurements{Float
mMax :: Float
$sel:mMax:Measurements :: Measurements -> Float
mMax, Float
mMin :: Float
$sel:mMin:Measurements :: Measurements -> Float
mMin} = Float
mMin forall a. Num a => a -> a -> a
* Float
0.5 forall a. Num a => a -> a -> a
+ Float
mMax forall a. Num a => a -> a -> a
* Float
0.5

{-# INLINEABLE sizeAa #-}
sizeAa :: AxisAligned Measurements -> AxisAligned Float
sizeAa :: AxisAligned Measurements -> AxisAligned Float
sizeAa = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Measurements -> Float
size

{-# INLINEABLE size #-}
size :: Measurements -> Float
size :: Measurements -> Float
size Measurements{Float
mMax :: Float
$sel:mMax:Measurements :: Measurements -> Float
mMax, Float
mMin :: Float
$sel:mMin:Measurements :: Measurements -> Float
mMin} = Float
mMax forall a. Num a => a -> a -> a
- Float
mMin

measureAaWith
  :: (Foldable outer, Foldable inner)
  => (a -> inner Vec3.Packed)
  -> outer a
  -> AxisAligned Measurements
measureAaWith :: forall (outer :: * -> *) (inner :: * -> *) a.
(Foldable outer, Foldable inner) =>
(a -> inner Packed) -> outer a -> AxisAligned Measurements
measureAaWith a -> inner Packed
f = forall (f :: * -> *) a b. Foldable f => Fold a b -> f a -> b
L.fold (forall (t :: * -> *) a.
Foldable t =>
(a -> t Packed) -> Fold a (AxisAligned Measurements)
measureAaWithF a -> inner Packed
f)

measureAaWithF :: (Foldable t) => (a -> (t Vec3.Packed)) -> L.Fold a (AxisAligned Measurements)
measureAaWithF :: forall (t :: * -> *) a.
Foldable t =>
(a -> t Packed) -> Fold a (AxisAligned Measurements)
measureAaWithF a -> t Packed
f = forall a b r. (a -> b) -> Fold b r -> Fold a r
L.premap a -> t Packed
f (forall a b r. Handler a b -> Fold b r -> Fold a r
L.handles forall (f :: * -> *) (t :: * -> *) a.
(Contravariant f, Applicative f, Foldable t) =>
(a -> f a) -> t a -> f (t a)
L.folded Fold Packed (AxisAligned Measurements)
measureAaF)

measureAa :: Foldable t => t Vec3.Packed -> AxisAligned Measurements
measureAa :: forall (t :: * -> *).
Foldable t =>
t Packed -> AxisAligned Measurements
measureAa = forall (f :: * -> *) a b. Foldable f => Fold a b -> f a -> b
L.fold Fold Packed (AxisAligned Measurements)
measureAaF

measureAaF :: L.Fold Vec3.Packed (AxisAligned Measurements)
measureAaF :: Fold Packed (AxisAligned Measurements)
measureAaF = forall a. a -> a -> a -> AxisAligned a
AxisAligned
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a b r. (a -> b) -> Fold b r -> Fold a r
L.premap Packed -> Float
packedX Fold Float Measurements
measureF
  forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a b r. (a -> b) -> Fold b r -> Fold a r
L.premap Packed -> Float
packedY Fold Float Measurements
measureF
  forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a b r. (a -> b) -> Fold b r -> Fold a r
L.premap Packed -> Float
packedZ Fold Float Measurements
measureF

measureF :: L.Fold Float Measurements
measureF :: Fold Float Measurements
measureF = do
  Float
mMin  <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> Maybe a -> a
fromMaybe Float
0) forall a. Ord a => Fold a (Maybe a)
L.minimum
  Float
mMax  <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> Maybe a -> a
fromMaybe Float
0) forall a. Ord a => Fold a (Maybe a)
L.maximum
  Float
mMean <- forall a. Fractional a => Fold a a
L.mean
  Float
mStd  <- forall a. Floating a => Fold a a
L.std
  pure Measurements{Float
mStd :: Float
mMean :: Float
mMax :: Float
mMin :: Float
$sel:mStd:Measurements :: Float
$sel:mMean:Measurements :: Float
$sel:mMax:Measurements :: Float
$sel:mMin:Measurements :: Float
..}

-- * Utils

{-# INLINE packedX #-}
packedX :: Vec3.Packed -> Float
packedX :: Packed -> Float
packedX (Vec3.Packed Vec3
pos) = forall r. Vec3 -> (Float -> Float -> Float -> r) -> r
withVec3 Vec3
pos \Float
x Float
_y Float
_z -> Float
x

{-# INLINE packedY #-}
packedY :: Vec3.Packed -> Float
packedY :: Packed -> Float
packedY (Vec3.Packed Vec3
pos) = forall r. Vec3 -> (Float -> Float -> Float -> r) -> r
withVec3 Vec3
pos \Float
_x Float
y Float
_z -> Float
y

{-# INLINE packedZ #-}
packedZ :: Vec3.Packed -> Float
packedZ :: Packed -> Float
packedZ (Vec3.Packed Vec3
pos) = forall r. Vec3 -> (Float -> Float -> Float -> r) -> r
withVec3 Vec3
pos \Float
_x Float
_y Float
z -> Float
z

class HasRange a where
  getRange :: a -> IndexRange
  adjustRange :: a -> Word32 -> a

instance HasRange Node where
  {-# INLINEABLE getRange #-}
  getRange :: Node -> IndexRange
getRange = Node -> IndexRange
nRange

  {-# INLINEABLE adjustRange #-}
  adjustRange :: Node -> Word32 -> Node
adjustRange node :: Node
node@Node{IndexRange
nRange :: IndexRange
$sel:nRange:Node :: Node -> IndexRange
nRange} Word32
newFirstIndex = Node
node
    { $sel:nRange:Node :: IndexRange
nRange = IndexRange
nRange
        { $sel:irFirstIndex:IndexRange :: Word32
irFirstIndex = Word32
newFirstIndex
        }
    }

instance HasRange TexturedNode where
  {-# INLINE getRange #-}
  getRange :: TexturedNode -> IndexRange
getRange = forall a. HasRange a => a -> IndexRange
getRange forall b c a. (b -> c) -> (a -> b) -> a -> c
. TexturedNode -> Node
tnNode

  {-# INLINE adjustRange #-}
  adjustRange :: TexturedNode -> Word32 -> TexturedNode
adjustRange tn :: TexturedNode
tn@TexturedNode{Node
tnNode :: Node
$sel:tnNode:TexturedNode :: TexturedNode -> Node
tnNode} Word32
newFirstIndex = TexturedNode
tn
    { $sel:tnNode:TexturedNode :: Node
tnNode = forall a. HasRange a => a -> Word32 -> a
adjustRange Node
tnNode Word32
newFirstIndex
    }

-- | CBOR.encode helper for storable types (vectors, etc.)
{-# NOINLINE encodeStorable #-}
encodeStorable :: forall a . Storable a => a -> CBOR.Encoding
encodeStorable :: forall a. Storable a => a -> Encoding
encodeStorable a
x = forall a. IO a -> a
unsafePerformIO do
  Ptr a
ptr <- forall a. Storable a => IO (Ptr a)
Foreign.malloc @a
  forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
ptr a
x
  ByteString
buf <- CStringLen -> IO ByteString
BS.unsafePackMallocCStringLen
    ( forall a b. Ptr a -> Ptr b
Foreign.castPtr Ptr a
ptr
    , forall a. Storable a => a -> Int
Foreign.sizeOf (forall a. HasCallStack => a
undefined :: a)
    )
  pure $ forall a. Serialise a => a -> Encoding
CBOR.encode ByteString
buf

-- | CBOR.decode helper for storable types (vectors, etc.)
decodeStorable :: forall a s . (Storable a, Typeable a) => CBOR.Decoder s a
decodeStorable :: forall a s. (Storable a, Typeable a) => Decoder s a
decodeStorable = do
  ByteString
buf <- forall s. Decoder s ByteString
CBOR.decodeBytes
  case ByteString -> Either String a
fromBuf ByteString
buf of
    Left String
err ->
      forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
err
    Right !a
res ->
      forall (f :: * -> *) a. Applicative f => a -> f a
pure a
res
  where
    expected :: Int
expected =
      forall a. Storable a => a -> Int
Foreign.sizeOf (forall a. HasCallStack => a
undefined :: a)

    {-# NOINLINE fromBuf #-}
    fromBuf :: ByteString -> Either String a
    fromBuf :: ByteString -> Either String a
fromBuf ByteString
buf =
      forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$
        forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.unsafeUseAsCStringLen ByteString
buf \(Ptr CChar
ptr, Int
len) ->
          if Int
len forall a. Eq a => a -> a -> Bool
== Int
expected then do
            !a
res <- forall a. Storable a => Ptr a -> IO a
Foreign.peek (forall a b. Ptr a -> Ptr b
Foreign.castPtr Ptr CChar
ptr)
            forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right a
res
          else
            forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat
              [ String
"Storable size mismatch for "
              , forall a. Show a => a -> String
show (TypeRep -> TyCon
typeRepTyCon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep forall a b. (a -> b) -> a -> b
$ forall {k} (t :: k). Proxy t
Proxy @a)
              , String
" (expected: ", forall a. Show a => a -> String
show Int
expected
              , String
", got: ", forall a. Show a => a -> String
show Int
len
              , String
")"
              ]