packages feed

static-tensor 0.2.0.0 → 0.2.1.0

raw patch · 3 files changed

+29/−4 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Data.Vector.Static: fromHomogenous :: (Fractional e, IsVector 3 e, IsVector 4 e) => Vector 4 e -> Vector 3 e
+ Data.Vector.Static: norm :: (Normalize n e) => Vector n e -> Vector n e
+ Data.Vector.Static: toHomogenous :: (Num e, IsVector 3 e, IsVector 4 e) => Vector 3 e -> Vector 4 e

Files

ChangeLog.md view
@@ -1,5 +1,10 @@ # Revision history for static-tensor
 
+## 0.2.1.0  -- 2017-12-17
+
+* Added `norm` vector function.
+* Added `fromHomogenous` and `toHomogenous` vector functions.
+
 ## 0.2.0.0  -- 2017-09-20
 
 * **BREAKING:** Changed `PositiveDims` type family to return `Constraint` instead of `Bool`
src/Data/Vector/Static.hs view
@@ -40,6 +40,7 @@     , NormalizedVector
     , Normalize
     , unNormalizedVector
+    , norm
     , vectorLenSquare
     , VectorLenSquare
     , vectorLen
@@ -47,6 +48,8 @@     , dot
     , Dot
     , cross
+    , toHomogenous
+    , fromHomogenous
     -- ** Generating vector instances
     , genVectorInstance
 ) where
@@ -126,6 +129,11 @@     , Scale '[n] e
     )
 
+-- | Normalize vector but don't wrap it in 'NormalizedVector'.
+norm :: (Normalize n e) => Vector n e -> Vector n e
+norm = unNormalizedVector . normalize
+{-# INLINE norm #-}
+
 -- | Dot product of two vectors.
 dot :: (Dot n e) => Vector n e -> Vector n e -> e
 dot v1 v2 = osum $ ozipWith (*) v1 v2
@@ -143,11 +151,23 @@ ---------------------------------------------------------------------------------------------------
 -- | Cross product is only defined for 3-dimensional vectors.
 cross :: (Num e, IsVector 3 e) => Vector 3 e -> Vector 3 e -> Vector 3 e
-cross t0 t1 =
-    withTensor t0 $ \x0 y0 z0 ->
-        withTensor t1 $ \x1 y1 z1 ->
+cross v0 v1 =
+    withTensor v0 $ \x0 y0 z0 ->
+        withTensor v1 $ \x1 y1 z1 ->
             vector @3 (y0 * z1 - z0 * y1) (z0 * x1 - x0 * z1) (x0 * y1 - y0 * x1)
 {-# INLINE cross #-}
+
+---------------------------------------------------------------------------------------------------
+-- | Convert 3-dimensional vector to 4-dimensional vector by setting the last element to @1@.
+toHomogenous :: (Num e, IsVector 3 e, IsVector 4 e) => Vector 3 e -> Vector 4 e
+toHomogenous v = withTensor v $ \x y z -> vector @4 x y z 1
+{-# INLINE toHomogenous #-}
+
+-- | Convert 4-dimensional vector to 3-dimensional vector by dividing first 3 coords by the last.
+--   The last element must not be zero!
+fromHomogenous :: (Fractional e, IsVector 3 e, IsVector 4 e) => Vector 4 e -> Vector 3 e
+fromHomogenous v = withTensor v $ \x y z w -> scale (vector @3 x y z) (1 / w)
+{-# INLINE fromHomogenous #-}
 
 ---------------------------------------------------------------------------------------------------
 -- | Generate instance of a vector.
static-tensor.cabal view
@@ -1,5 +1,5 @@ name:                static-tensor
-version:             0.2.0.0
+version:             0.2.1.0
 synopsis:            Tensors of statically known size
 description:         
     This library provides a toolkit for working with tensors of statically known size and element's type.