-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Three-dimensional vectors of doubles with basic operations -- -- A class of 3-vectors with a set of basic methods for geometry -- operations on vectors and an associated matrix type. Instances are -- provided for use with Data.Vector.Unboxed and -- Data.Vector.Storable as container types. @package simple-vec3 @version 0.2 module Data.Vec3.Class -- | Three-dimensional vector, with an associated matrix type. class Vec3 v where data Matrix v origin = fromXYZ (0, 0, 0) zipWith f v1 v2 = fromXYZ (f x1 x2, f y1 y2, f z1 z2) where (x1, y1, z1) = toXYZ v1 (x2, y2, z2) = toXYZ v2 (<+>) = zipWith (+) (<->) = zipWith (-) (><) v1 v2 = fromXYZ (y1 * z2 - y2 * z1, x2 * z1 - x1 * z2, x1 * y2 - x2 * y1) where (x1, y1, z1) = toXYZ v1 (x2, y2, z2) = toXYZ v2 (.^) v s = fromXYZ (x * s, y * s, z * s) where (x, y, z) = toXYZ v (.*) v1 v2 = x + y + z where (x, y, z) = toXYZ $ zipWith (*) v1 v2 norm v = sqrt (v .* v) normalize v = v .^ (1 / norm v) distance v1 v2 = norm (v1 <-> v2) invert v = origin <-> v dotM v1 v2 m = v1 .* (m `mxv` v2) mxv m v = fromXYZ (r1 .* v, r2 .* v, r3 .* v) where (r1, r2, r3) = toRows m diag d = fromRows (fromXYZ (d, 0, 0), fromXYZ (0, d, 0), fromXYZ (0, 0, d)) vxv v1 v2 = fromRows (v2 .^ v11, v2 .^ v12, v2 .^ v13) where (v11, v12, v13) = toXYZ v1 addM m1 m2 = fromRows (r11 <+> r21, r12 <+> r22, r13 <+> r23) where (r11, r12, r13) = toRows m1 (r21, r22, r23) = toRows m2 where { data family Matrix v; } -- | Origin point (0, 0, 0). origin :: Vec3 v => v -- | Construct a new vector from components. fromXYZ :: Vec3 v => (Double, Double, Double) -> v -- | Deconstruct a vector into components. toXYZ :: Vec3 v => v -> (Double, Double, Double) -- | Zip two vectors elementwise. zipWith :: Vec3 v => (Double -> Double -> Double) -> v -> v -> v -- | Add two vectors. (<+>) :: Vec3 v => v -> v -> v -- | Subtract two vectors. (<->) :: Vec3 v => v -> v -> v -- | Cross product. (><) :: Vec3 v => v -> v -> v -- | Scale a vector. (.^) :: Vec3 v => v -> Double -> v -- | Dot product. (.*) :: Vec3 v => v -> v -> Double -- | Euclidean norm of a vector. norm :: Vec3 v => v -> Double -- | Produce unit vector with the same direction as the original one. normalize :: Vec3 v => v -> v -- | Distance between two points. distance :: Vec3 v => v -> v -> Double -- | Invert the direction of a vector. invert :: Vec3 v => v -> v -- | Construct a new matrix from rows. fromRows :: Vec3 v => (v, v, v) -> Matrix v -- | Deconstruct a matrix into rows. toRows :: Vec3 v => Matrix v -> (v, v, v) -- | Generic vector dot product. -- -- Multiply the transpose of the first vector by the given matrix, then -- multiply the result by the second vector. -- --
-- [ a11 a12 a13 ] [ v2x ] -- [ ] [ ] -- [ v1x v1y v1z ] . [ a21 a22 a23 ] . [ v2y ] = s -- [ ] [ ] -- [ a31 a32 a33 ] [ v2z ] --dotM :: Vec3 v => v -> v -> Matrix v -> Double -- | Multiply a matrix and a vector. -- --
-- [ a11 a12 a13 ] [ v2x ] [ rx ] -- [ ] [ ] [ ] -- [ a21 a22 a23 ] . [ v2y ] = [ ry ] -- [ ] [ ] [ ] -- [ a31 a32 a33 ] [ v2z ] [ rz ] --mxv :: Vec3 v => Matrix v -> v -> v -- | Build a diagonal matrix from a number d. -- --
-- [ d 0 0 ] -- [ ] -- [ 0 d 0 ] -- [ ] -- [ 0 0 d ] --diag :: Vec3 v => Double -> Matrix v -- | Transpose a vector and multiply it by another vector, producing a -- matrix. -- --
-- [ v1x ] [ r11 r12 r13 ] -- [ ] [ ] -- [ v1y ] . [ v2x v2y v2z ] = [ r21 r22 r23 ] -- [ ] [ ] -- [ v1z ] [ r31 r32 r33 ] --vxv :: Vec3 v => v -> v -> Matrix v -- | Add two matrices. addM :: Vec3 v => Matrix v -> Matrix v -> Matrix v module Data.Vec3.Unboxed -- | Vec3 implementation with Unbox instance based on tuples, -- suitable for use with Data.Vector.Unboxed. -- -- This represents 3-vector as a triple of doubles, using the default -- Unbox instance for tuples as provided by Data.Vector.Unboxed, -- which wraps a vector of tuples as a tuple of vectors. -- --
-- interface: [d1 (x, y, z); d2 (x, y, z) ...], length = N -- | | | | | | -- storage(x): [d1x-+ | | ; d2x-+ | | ...], length = N -- storage(y): [d1y----+ | ; d2y----+ | ...], length = N -- storage(z): [d1z-------+ ; d2z-------+ ...], length = N --newtype UVec3 UVec3 :: (Double, Double, Double) -> UVec3 instance Data.Vector.Unboxed.Base.Unbox (Data.Vec3.Class.Matrix Data.Vec3.Unboxed.UVec3) instance Data.Vector.Generic.Mutable.Base.MVector Data.Vector.Unboxed.Base.MVector (Data.Vec3.Class.Matrix Data.Vec3.Unboxed.UVec3) instance Data.Vector.Generic.Base.Vector Data.Vector.Unboxed.Base.Vector (Data.Vec3.Class.Matrix Data.Vec3.Unboxed.UVec3) instance GHC.Show.Show (Data.Vec3.Class.Matrix Data.Vec3.Unboxed.UVec3) instance GHC.Classes.Eq (Data.Vec3.Class.Matrix Data.Vec3.Unboxed.UVec3) instance Data.Vector.Unboxed.Base.Unbox Data.Vec3.Unboxed.UVec3 instance Data.Vector.Generic.Mutable.Base.MVector Data.Vector.Unboxed.Base.MVector Data.Vec3.Unboxed.UVec3 instance Data.Vector.Generic.Base.Vector Data.Vector.Unboxed.Base.Vector Data.Vec3.Unboxed.UVec3 instance Data.Vec3.Class.Vec3 Data.Vec3.Unboxed.UVec3 instance GHC.Show.Show Data.Vec3.Unboxed.UVec3 instance GHC.Classes.Eq Data.Vec3.Unboxed.UVec3 -- | Vec3 implementation with Unbox instance based on single -- array storage scheme, suitable for use with -- Data.Vector.Unboxed. module Data.Vec3.Unboxed.Contiguous -- | Vec3 implementation with Unbox instance based on a -- single contiguous array storage scheme, suitable for use with -- Data.Vector.Unboxed. -- -- Unbox instance provides the required index transformations. -- --
-- interface: [d1 x y z ; d2 x y z ...], length = N = M / 3 -- | | | | | | -- storage: [ d1x d2y d2z ; d2x d2y d2z ...], length = M --data UVec3 UVec3 :: !Double -> !Double -> !Double -> UVec3 instance GHC.Show.Show Data.Vec3.Unboxed.Contiguous.UVec3 instance GHC.Classes.Eq Data.Vec3.Unboxed.Contiguous.UVec3 instance Data.Vec3.Class.Vec3 Data.Vec3.Unboxed.Contiguous.UVec3 instance Data.Vector.Generic.Mutable.Base.MVector Data.Vector.Unboxed.Base.MVector Data.Vec3.Unboxed.Contiguous.UVec3 instance Data.Vector.Generic.Base.Vector Data.Vector.Unboxed.Base.Vector Data.Vec3.Unboxed.Contiguous.UVec3 instance Data.Vector.Unboxed.Base.Unbox Data.Vec3.Unboxed.Contiguous.UVec3 -- | Vec3 class and implementations. module Data.Vec3 -- | Three-dimensional vector, with an associated matrix type. class Vec3 v where data Matrix v origin = fromXYZ (0, 0, 0) zipWith f v1 v2 = fromXYZ (f x1 x2, f y1 y2, f z1 z2) where (x1, y1, z1) = toXYZ v1 (x2, y2, z2) = toXYZ v2 (<+>) = zipWith (+) (<->) = zipWith (-) (><) v1 v2 = fromXYZ (y1 * z2 - y2 * z1, x2 * z1 - x1 * z2, x1 * y2 - x2 * y1) where (x1, y1, z1) = toXYZ v1 (x2, y2, z2) = toXYZ v2 (.^) v s = fromXYZ (x * s, y * s, z * s) where (x, y, z) = toXYZ v (.*) v1 v2 = x + y + z where (x, y, z) = toXYZ $ zipWith (*) v1 v2 norm v = sqrt (v .* v) normalize v = v .^ (1 / norm v) distance v1 v2 = norm (v1 <-> v2) invert v = origin <-> v dotM v1 v2 m = v1 .* (m `mxv` v2) mxv m v = fromXYZ (r1 .* v, r2 .* v, r3 .* v) where (r1, r2, r3) = toRows m diag d = fromRows (fromXYZ (d, 0, 0), fromXYZ (0, d, 0), fromXYZ (0, 0, d)) vxv v1 v2 = fromRows (v2 .^ v11, v2 .^ v12, v2 .^ v13) where (v11, v12, v13) = toXYZ v1 addM m1 m2 = fromRows (r11 <+> r21, r12 <+> r22, r13 <+> r23) where (r11, r12, r13) = toRows m1 (r21, r22, r23) = toRows m2 where { data family Matrix v; } -- | Origin point (0, 0, 0). origin :: Vec3 v => v -- | Construct a new vector from components. fromXYZ :: Vec3 v => (Double, Double, Double) -> v -- | Deconstruct a vector into components. toXYZ :: Vec3 v => v -> (Double, Double, Double) -- | Zip two vectors elementwise. zipWith :: Vec3 v => (Double -> Double -> Double) -> v -> v -> v -- | Add two vectors. (<+>) :: Vec3 v => v -> v -> v -- | Subtract two vectors. (<->) :: Vec3 v => v -> v -> v -- | Cross product. (><) :: Vec3 v => v -> v -> v -- | Scale a vector. (.^) :: Vec3 v => v -> Double -> v -- | Dot product. (.*) :: Vec3 v => v -> v -> Double -- | Euclidean norm of a vector. norm :: Vec3 v => v -> Double -- | Produce unit vector with the same direction as the original one. normalize :: Vec3 v => v -> v -- | Distance between two points. distance :: Vec3 v => v -> v -> Double -- | Invert the direction of a vector. invert :: Vec3 v => v -> v -- | Construct a new matrix from rows. fromRows :: Vec3 v => (v, v, v) -> Matrix v -- | Deconstruct a matrix into rows. toRows :: Vec3 v => Matrix v -> (v, v, v) -- | Generic vector dot product. -- -- Multiply the transpose of the first vector by the given matrix, then -- multiply the result by the second vector. -- --
-- [ a11 a12 a13 ] [ v2x ] -- [ ] [ ] -- [ v1x v1y v1z ] . [ a21 a22 a23 ] . [ v2y ] = s -- [ ] [ ] -- [ a31 a32 a33 ] [ v2z ] --dotM :: Vec3 v => v -> v -> Matrix v -> Double -- | Multiply a matrix and a vector. -- --
-- [ a11 a12 a13 ] [ v2x ] [ rx ] -- [ ] [ ] [ ] -- [ a21 a22 a23 ] . [ v2y ] = [ ry ] -- [ ] [ ] [ ] -- [ a31 a32 a33 ] [ v2z ] [ rz ] --mxv :: Vec3 v => Matrix v -> v -> v -- | Build a diagonal matrix from a number d. -- --
-- [ d 0 0 ] -- [ ] -- [ 0 d 0 ] -- [ ] -- [ 0 0 d ] --diag :: Vec3 v => Double -> Matrix v -- | Transpose a vector and multiply it by another vector, producing a -- matrix. -- --
-- [ v1x ] [ r11 r12 r13 ] -- [ ] [ ] -- [ v1y ] . [ v2x v2y v2z ] = [ r21 r22 r23 ] -- [ ] [ ] -- [ v1z ] [ r31 r32 r33 ] --vxv :: Vec3 v => v -> v -> Matrix v -- | Add two matrices. addM :: Vec3 v => Matrix v -> Matrix v -> Matrix v -- | Vec3 implementation with Storable instance based on a -- single contiguous array storage scheme, suitable for use with -- Data.Vector.Storable. -- -- Unbox instance provides the required index transformations. -- --
-- interface: [d1 x y z ; d2 x y z ...], length = N = M / 3 -- | | | | | | -- storage: [ d1x d2y d2z ; d2x d2y d2z ...], length = M --data SVec3 SVec3 :: !CDouble -> !CDouble -> !CDouble -> SVec3 -- | Vec3 implementation with Unbox instance based on tuples, -- suitable for use with Data.Vector.Unboxed. -- -- This represents 3-vector as a triple of doubles, using the default -- Unbox instance for tuples as provided by Data.Vector.Unboxed, -- which wraps a vector of tuples as a tuple of vectors. -- --
-- interface: [d1 (x, y, z); d2 (x, y, z) ...], length = N -- | | | | | | -- storage(x): [d1x-+ | | ; d2x-+ | | ...], length = N -- storage(y): [d1y----+ | ; d2y----+ | ...], length = N -- storage(z): [d1z-------+ ; d2z-------+ ...], length = N --newtype UVec3 UVec3 :: (Double, Double, Double) -> UVec3 instance GHC.Show.Show Data.Vec3.SVec3 instance GHC.Classes.Eq Data.Vec3.SVec3 instance GHC.Show.Show (Data.Vec3.Class.Matrix Data.Vec3.SVec3) instance GHC.Classes.Eq (Data.Vec3.Class.Matrix Data.Vec3.SVec3) instance Foreign.Storable.Storable Data.Vec3.SVec3 instance Data.Vec3.Class.Vec3 Data.Vec3.SVec3 instance Test.QuickCheck.Arbitrary.Arbitrary Data.Vec3.SVec3