module Data.Geometry.Vector( module Data.Geometry.Vector.VectorFixed
, module LV
, Affine(..)
, qdA, distanceA
, dot, norm
, isScalarMultipleOf
, scalarMultiple
) where
import qualified Data.Foldable as F
import Data.Geometry.Vector.VectorFixed
import Data.Geometry.Vector.VectorFixed as GV
import Data.Maybe
import qualified Data.Vector.Fixed as FV
import Linear.Affine (Affine(..), qdA, distanceA)
import Linear.Metric (dot,norm)
import Linear.Vector as LV
isScalarMultipleOf :: (Eq r, Fractional r, GV.Arity d)
=> Vector d r -> Vector d r -> Bool
u `isScalarMultipleOf` v = isJust $ scalarMultiple u v
scalarMultiple :: (Eq r, Fractional r, GV.Arity d)
=> Vector d r -> Vector d r -> Maybe r
scalarMultiple u v
| allZero u || allZero v = Just 0
| otherwise = scalarMultiple' u v
allZero :: (GV.Arity d, Eq r, Num r) => Vector d r -> Bool
allZero = F.all (== 0)
data ScalarMultiple r = No | Maybe | Yes r deriving (Eq,Show)
instance Eq r => Monoid (ScalarMultiple r) where
mempty = Maybe
No `mappend` _ = No
_ `mappend` No = No
Maybe `mappend` x = x
x `mappend` Maybe = x
(Yes x) `mappend` (Yes y)
| x == y = Yes x
| otherwise = No
scalarMultiple' :: (Eq r, Fractional r, GV.Arity d)
=> Vector d r -> Vector d r -> Maybe r
scalarMultiple' u v = g . F.foldr mappend mempty $ FV.zipWith f u v
where
f 0 0 = Maybe
f _ 0 = No
f ui vi = Yes $ ui / vi
g No = Nothing
g Maybe = error "scalarMultiple': found a Maybe, which means the vectors either have length zero, or one of them is all Zero!"
g (Yes x) = Just x