--------------------------------------------------------------------------------
-- |
-- Module      :  RedViz
-- Copyright   :  (c) Vladimir Lopatin 2022
-- License     :  BSD-3-Clause
--
-- Maintainer  :  Vladimir Lopatin <madjestic13@gmail.com>
-- Stability   :  provisional
-- Portability :  portable
--
-- The tooling around Vertex Array Objects
--
--------------------------------------------------------------------------------

module Graphics.RedViz.VAO
  (
    VAO
  , toVAO
  ) where

-- import Data.Vector as V
import Data.Massiv.Array as A
import GHC.Float

-- import Debug.Trace   as DT

type VAO = [[[Float]]]

toVAO
  :: [[Int]]
  -> [Float]
  -> [(Double, Double, Double)]
  -> [(Double, Double, Double)]
  -> [(Double, Double, Double)]
  -> [(Double, Double, Double)]
  -> VAO

toVAO :: [[Int]]
-> [Float]
-> [(Double, Double, Double)]
-> [(Double, Double, Double)]
-> [(Double, Double, Double)]
-> [(Double, Double, Double)]
-> VAO
toVAO [[Int]]
idxs [Float]
as [(Double, Double, Double)]
cds [(Double, Double, Double)]
ns [(Double, Double, Double)]
ts [(Double, Double, Double)]
ps = VAO
vaos
  where
    as' :: [[Float]]
as'  = (Float -> [Float]) -> [Float] -> [[Float]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Float
a -> [Float
a]) [Float]
as                                :: [[Float]]
    cds' :: [[Float]]
cds' = ((Double, Double, Double) -> [Float])
-> [(Double, Double, Double)] -> [[Float]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Double
r,Double
g,Double
b)   -> (Double -> Float) -> [Double] -> [Float]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> Float
double2Float [Double
r,Double
g,Double
b]) [(Double, Double, Double)]
cds :: [[Float]]
    ns' :: [[Float]]
ns'  = ((Double, Double, Double) -> [Float])
-> [(Double, Double, Double)] -> [[Float]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Double
x,Double
y,Double
z)   -> (Double -> Float) -> [Double] -> [Float]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> Float
double2Float [Double
x,Double
y,Double
z]) [(Double, Double, Double)]
ns
    ts' :: [[Float]]
ts'  = ((Double, Double, Double) -> [Float])
-> [(Double, Double, Double)] -> [[Float]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Double
u,Double
v,Double
w)   -> (Double -> Float) -> [Double] -> [Float]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> Float
double2Float [Double
u,Double
v,Double
w]) [(Double, Double, Double)]
ts
    ps' :: [[Float]]
ps'  = ((Double, Double, Double) -> [Float])
-> [(Double, Double, Double)] -> [[Float]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Double
x,Double
y,Double
z)   -> (Double -> Float) -> [Double] -> [Float]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> Float
double2Float [Double
x,Double
y,Double
z]) [(Double, Double, Double)]
ps

    indices :: Array U Ix2 Int
indices = Comp -> [ListItem Ix2 Int] -> Array U Ix2 Int
forall r ix e.
(HasCallStack, Ragged L ix e, Manifest r e) =>
Comp -> [ListItem ix e] -> Array r ix e
fromLists' Comp
Par [[Int]]
[ListItem Ix2 Int]
idxs :: (Array U Ix2 Int)
    as'' :: Array U Ix2 Float
as'' = Comp -> [ListItem Ix2 Float] -> Array U Ix2 Float
forall r ix e.
(HasCallStack, Ragged L ix e, Manifest r e) =>
Comp -> [ListItem ix e] -> Array r ix e
fromLists' Comp
Par [[Float]]
[ListItem Ix2 Float]
as'     :: (Array U Ix2 Float)
    cds'' :: Array U Ix2 Float
cds''= Comp -> [ListItem Ix2 Float] -> Array U Ix2 Float
forall r ix e.
(HasCallStack, Ragged L ix e, Manifest r e) =>
Comp -> [ListItem ix e] -> Array r ix e
fromLists' Comp
Par [[Float]]
[ListItem Ix2 Float]
cds'    :: (Array U Ix2 Float)
    ns'' :: Array U Ix2 Float
ns'' = Comp -> [ListItem Ix2 Float] -> Array U Ix2 Float
forall r ix e.
(HasCallStack, Ragged L ix e, Manifest r e) =>
Comp -> [ListItem ix e] -> Array r ix e
fromLists' Comp
Par [[Float]]
[ListItem Ix2 Float]
ns'     :: (Array U Ix2 Float)
    ts'' :: Array U Ix2 Float
ts'' = Comp -> [ListItem Ix2 Float] -> Array U Ix2 Float
forall r ix e.
(HasCallStack, Ragged L ix e, Manifest r e) =>
Comp -> [ListItem ix e] -> Array r ix e
fromLists' Comp
Par [[Float]]
[ListItem Ix2 Float]
ts'     :: (Array U Ix2 Float)
    ps'' :: Array U Ix2 Float
ps'' = Comp -> [ListItem Ix2 Float] -> Array U Ix2 Float
forall r ix e.
(HasCallStack, Ragged L ix e, Manifest r e) =>
Comp -> [ListItem ix e] -> Array r ix e
fromLists' Comp
Par [[Float]]
[ListItem Ix2 Float]
ps'     :: (Array U Ix2 Float)

    cList' :: [[Float]]
cList' = Array U Ix2 Float -> [[Float]]
forall r e ix.
(Source r e, Index ix, Index (Lower ix)) =>
Array r ix e -> [[e]]
toLists2 (Array U Ix2 Float -> [[Float]])
-> (Array DL Ix2 Float -> Array U Ix2 Float)
-> Array DL Ix2 Float
-> [[Float]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. U -> Array DL Ix2 Float -> Array U Ix2 Float
forall r e r' ix.
(Manifest r e, Load r' ix e) =>
r -> Array r' ix e -> Array r ix e
computeAs U
U (Array DL Ix2 Float -> [[Float]])
-> Array DL Ix2 Float -> [[Float]]
forall a b. (a -> b) -> a -> b
$ Dim -> [Array U Ix2 Float] -> Array DL Ix2 Float
forall (f :: * -> *) r ix e.
(HasCallStack, Foldable f, Index ix, Source r e) =>
Dim -> f (Array r ix e) -> Array DL ix e
concat' Dim
1 [Array U Ix2 Float
as'', Array U Ix2 Float
cds'', Array U Ix2 Float
ns'', Array U Ix2 Float
ts'', Array U Ix2 Float
ps''] :: [[Float]]
    
    mat :: Array U Ix2 Float
mat = Comp -> [ListItem Ix2 Float] -> Array U Ix2 Float
forall r ix e.
(HasCallStack, Ragged L ix e, Manifest r e) =>
Comp -> [ListItem ix e] -> Array r ix e
fromLists' Comp
Par [[Float]]
[ListItem Ix2 Float]
cList' :: (Array U Ix2 Float)
    cListOpt :: VAO
cListOpt =
      Array P Ix2 Float -> [[Float]]
forall r e ix.
(Source r e, Index ix, Index (Lower ix)) =>
Array r ix e -> [[e]]
toLists2 (Array P Ix2 Float -> [[Float]])
-> (Array D Ix2 Float -> Array P Ix2 Float)
-> Array D Ix2 Float
-> [[Float]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. P -> Array D Ix2 Float -> Array P Ix2 Float
forall r e r' ix.
(Manifest r e, Load r' ix e) =>
r -> Array r' ix e -> Array r ix e
computeAs P
P (Array D Ix2 Float -> [[Float]]) -> [Array D Ix2 Float] -> VAO
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
      (Int -> Array D Ix2 Float) -> [Int] -> [Array D Ix2 Float]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Int
row -> Sz Ix2 -> (Ix2 -> Ix2) -> Array U Ix2 Float -> Array D Ix2 Float
forall r ix ix' e.
(HasCallStack, Source r e, Index ix, Index ix') =>
Sz ix' -> (ix' -> ix) -> Array r ix e -> Array D ix' e
backpermute' (Ix2 -> Sz Ix2
forall ix. Index ix => ix -> Sz ix
Sz ([Int] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length ([[Int]]
idxs [[Int]] -> Int -> [Int]
forall a. [a] -> Int -> a
!! Int
row) Int -> Int -> Ix2
:. Int
13)) (\(Int
i :. Int
j) -> ((Array U Ix2 Int
indices Array U Ix2 Int -> Int -> Array U (Lower Ix2) Int
forall r ix e.
(HasCallStack, Index ix, Index (Lower ix), Source r e) =>
Array r ix e -> Int -> Array r (Lower ix) e
!> Int
row) Array U Int Int -> Int -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! Int
i) Int -> Int -> Ix2
:. Int
j) Array U Ix2 Float
mat) [Int
0 .. Int -> Int -> Int
forall a. Integral a => a -> a -> a
div (Array U Ix2 Int -> Int
forall ix r e. (Index ix, Size r) => Array r ix e -> Int
elemsCount Array U Ix2 Int
indices) (Array U Int Int -> Int
forall ix r e. (Index ix, Size r) => Array r ix e -> Int
elemsCount (Array U Ix2 Int
indices Array U Ix2 Int -> Int -> Array U (Lower Ix2) Int
forall r ix e.
(HasCallStack, Index ix, Index (Lower ix), Source r e) =>
Array r ix e -> Int -> Array r (Lower ix) e
!> Int
0))Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1]
    --vaos = (DT.trace ("cListOpt" ++ show cListOpt) $ cListOpt)
    vaos :: VAO
vaos = VAO
cListOpt