module DSMC.Util.Vector
( Vec3
, Matrix
, Point
, origin
, (<+>)
, (<->)
, (><)
, (.^)
, (.*)
, norm
, normalize
, invert
, distance
, moveBy
, mxv
, vxv
, dotM
, diag
, addM
, buildCartesian
)
where
import Prelude hiding (reverse)
type Vec3 = (Double, Double, Double)
type Matrix = (Vec3, Vec3, Vec3)
type Point = Vec3
origin :: Point
origin = (0, 0, 0)
(<+>) :: Vec3 -> Vec3 -> Vec3
(<+>) !(x1, y1, z1) !(x2, y2, z2) = (x1 + x2, y1 + y2, z1 + z2)
(<->) :: Vec3 -> Vec3 -> Vec3
(<->) !(x1, y1, z1) !(x2, y2, z2) = (x1 x2, y1 y2, z1 z2)
(><) :: Vec3 -> Vec3 -> Vec3
(><) !(x1, y1, z1) !(x2, y2, z2) =
(y1 * z2 y2 * z1, x2 * z1 x1 * z2, x1 * y2 x2 * y1)
(.^) :: Vec3 -> Double -> Vec3
(.^) !(x, y, z) !s = (x * s, y * s, z * s)
(.*) :: Vec3 -> Vec3 -> Double
(.*) !(x1, y1, z1) !(x2, y2, z2) = x1 * x2 + y1 * y2 + z1 * z2
dotM :: Vec3 -> Vec3 -> Matrix -> Double
dotM !v1 !v2 !m = v1 .* (m `mxv` v2)
mxv :: Matrix -> Vec3 -> Vec3
mxv !(r1, r2, r3) !v = (r1 .* v, r2 .* v, r3 .* v)
diag :: Double -> Matrix
diag !d = ((d, 0, 0), (0, d, 0), (0, 0, d))
vxv :: Vec3 -> Vec3 -> Matrix
vxv !(v11, v12, v13) !v2 = (v2 .^ v11, v2 .^ v12, v2 .^ v13)
distance :: Point -> Point -> Double
distance !v1 !v2 = norm (v1 <-> v2)
norm :: Vec3 -> Double
norm !(x, y, z) = sqrt (x * x + y * y + z * z)
normalize :: Vec3 -> Vec3
normalize !v = v .^ (1 / norm v)
invert :: Vec3 -> Vec3
invert !v = v .^ (1)
moveBy :: Point
-> Vec3
-> Double
-> Point
moveBy !p !v !t = p <+> (v .^ t)
addM :: Matrix -> Matrix -> Matrix
addM !(r11, r12, r13) !(r21, r22, r23) =
(r11 <+> r21, r12 <+> r22, r13 <+> r23)
buildCartesian :: Double -> Double -> (Vec3, Vec3, Vec3)
buildCartesian yaw pitch = (u, v, w)
where u = (cos yaw * cos pitch, sin yaw * cos pitch, sin pitch)
v = ( (sin yaw), cos yaw, 0)
w = u >< v