{-# LANGUAGE FlexibleContexts , TypeFamilies , ViewPatterns #-}------------------------------------------------------------------------------- |-- Module : Diagrams.ThreeD.Vector-- Copyright : (c) 2013 diagrams-lib team (see LICENSE)-- License : BSD-style (see LICENSE)-- Maintainer : diagrams-discuss@googlegroups.com---- Three-dimensional vectors.-------------------------------------------------------------------------------module Diagrams.ThreeD.Vector ( -- * Special 2D vectors unitX, unitY, unitZ, unit_X, unit_Y, unit_Z, -- * Converting between vectors and angles direction, fromDirection, angleBetween, angleBetweenDirs ) whereimport Control.Lens ((^.))import Data.VectorSpaceimport Data.Crossimport Diagrams.ThreeD.Typesimport Diagrams.Coordinates-- | The unit vector in the positive X direction.unitX :: R3unitX = 1 ^& 0 ^& 0-- | The unit vector in the positive Y direction.unitY :: R3unitY = 0 ^& 1 ^& 0-- | The unit vector in the positive Z direction.unitZ :: R3unitZ = 0 ^& 0 ^& 1-- | The unit vector in the negative X direction.unit_X :: R3unit_X = (-1) ^& 0 ^& 0-- | The unit vector in the negative Y direction.unit_Y :: R3unit_Y = 0 ^& (-1) ^& 0-- | The unit vector in the negative Z direction.unit_Z :: R3unit_Z = 0 ^& 0 ^& (-1)-- | @direction v@ is the direction in which @v@ points. Returns an-- unspecified value when given the zero vector as input.direction :: Direction d => R3 -> ddirection v | r == 0 = fromSpherical $ Spherical zero zero | otherwise = fromSpherical $ Spherical θ φ where r = magnitude v (x,y,z) = unr3 v φ = asin (z / r) @@ rad θ = atan2 y x @@ rad zero = 0 @@ rad-- | @fromDirection d@ is the unit vector in the direction @d@.fromDirection :: Direction d => d -> R3fromDirection (toSpherical -> (Spherical θ' φ')) = r3 (x,y,z) where θ = θ'^.rad φ = φ'^.rad x = cos θ * cos φ y = sin θ * cos φ z = sin φ-- | compute the positive angle between the two vectors in their common planeangleBetween :: R3 -> R3 -> AngleangleBetween v1 v2 = atan2 (magnitude $ cross3 v1 v2) (v1 <.> v2) @@ rad-- | compute the positive angle between the two vectors in their common planeangleBetweenDirs :: Direction d => d -> d -> AngleangleBetweenDirs d1 d2 = angleBetween (fromDirection d1) (fromDirection d2)