AC-Vector-Fancy (empty) → 2.0.0
raw patch · 6 files changed
+314/−0 lines, 6 filesdep +AC-Angledep +AC-Vectordep +basesetup-changed
Dependencies added: AC-Angle, AC-Vector, base
Files
- AC-Vector-Fancy.cabal +27/−0
- Data/Vector/Axis.hs +8/−0
- Data/Vector/Fancy.hs +101/−0
- Data/Vector/Transform/Fancy.hs +166/−0
- License.txt +10/−0
- Setup.hs +2/−0
+ AC-Vector-Fancy.cabal view
@@ -0,0 +1,27 @@+Cabal-Version: >= 1.6 +Name: AC-Vector-Fancy +Version: 2.0.0 +Stability: Experimental +Synopsis: Fancy type system stuff for AC-Vector + +Description: + + Adds various type system tricks to AC-Vector. + (Requires several language extensions, including + type families.) + +Category: Data, Math, Numerical +License: BSD3 +License-file: License.txt +Author: Andrew Coppin +Maintainer: MathematicalOrchid@hotmail.com +Build-Type: Simple +Tested-With: GHC == 6.10.3 + +Library + Exposed-modules: + Data.Vector.Axis, + Data.Vector.Fancy, + Data.Vector.Transform.Fancy + Build-Depends: base >= 4 && < 5, AC-Angle >= 1.0, AC-Vector >= 2.0 + HS-Source-Dirs: .
+ Data/Vector/Axis.hs view
@@ -0,0 +1,8 @@+-- | Types denoting coordinate axies. + +module Data.Vector.Axis where + +data AxisX = AxisX deriving Show +data AxisY = AxisY deriving Show +data AxisZ = AxisZ deriving Show +data AxisW = AxisW deriving Show
+ Data/Vector/Fancy.hs view
@@ -0,0 +1,101 @@+{- | + Various facilities for dealing with vectors generically. +-} + +{-# LANGUAGE MultiParamTypeClasses #-} + +module Data.Vector.Fancy where + +import Data.Vector.Class +import Data.Vector.V1 +import Data.Vector.V2 +import Data.Vector.V3 +import Data.Vector.V4 +import Data.Vector.Axis + +-- | Class for generically reading/writing vector coordinates. +class VectorAxis vector axis where + -- | Read from the specified coordinate axis. + get_coord :: axis -> vector -> Scalar + + -- | Replace the existing value of the given coordinate axis. + set_coord :: axis -> Scalar -> vector -> vector + +-- | This class relates two vector types having consecutive sizes. +class Project lo hi where + -- | Reduce number of dimensions by one. (Return the dropped dimension as a @Scalar@.) + orthographic_down :: hi -> (lo, Scalar) + + -- | Increase number of dimensions by one. (Supply value for new dimension as a @Scalar@.) + orthographic_up :: (lo, Scalar) -> hi + + -- | Perspective-project to N-1 dimensions. (Also return the distance from the camera as a @Scalar@.) + perspective_down :: hi -> (lo, Scalar) + + -- | Inverse-perspective project into N+1 dimension. (Supply the distance from the camera as a @Scalar@.) + perspective_up :: (lo, Scalar) -> hi + + + +instance VectorAxis Vector1 AxisX where + get_coord _ = v1x + set_coord _ x _ = Vector1 x + +instance VectorAxis Vector2 AxisX where + get_coord _ = v2x + set_coord _ x v = v {v2x = x} + +instance VectorAxis Vector3 AxisX where + get_coord _ = v3x + set_coord _ x v = v {v3x = x} + +instance VectorAxis Vector4 AxisX where + get_coord _ = v4x + set_coord _ x v = v {v4x = x} + +instance VectorAxis Vector2 AxisY where + get_coord _ = v2y + set_coord _ y v = v {v2y = y} + +instance VectorAxis Vector3 AxisY where + get_coord _ = v3y + set_coord _ y v = v {v3y = y} + +instance VectorAxis Vector4 AxisY where + get_coord _ = v4y + set_coord _ y v = v {v4y = y} + +instance VectorAxis Vector3 AxisZ where + get_coord _ = v3z + set_coord _ z v = v {v3z = z} + +instance VectorAxis Vector4 AxisZ where + get_coord _ = v4z + set_coord _ z v = v {v4z = z} + +instance VectorAxis Vector4 AxisW where + get_coord _ = v4w + set_coord _ w v = v {v4w = w} + + + +instance Project Vector1 Vector2 where + orthographic_down (Vector2 x y) = (Vector1 x, y) + orthographic_up (Vector1 x, y) = (Vector2 x y) + + perspective_down (Vector2 x y) = (Vector1 (x/y), y) + perspective_up (Vector1 x, y) = (Vector2 (x*y) y) + +instance Project Vector2 Vector3 where + orthographic_down (Vector3 x y z) = (Vector2 x y, z) + orthographic_up (Vector2 x y, z) = (Vector3 x y z) + + perspective_down (Vector3 x y z) = (Vector2 (x/z) (y/z), z) + perspective_up (Vector2 x y, z) = (Vector3 (x*z) (y*z) z) + +instance Project Vector3 Vector4 where + orthographic_down (Vector4 x y z w) = (Vector3 x y z, w) + orthographic_up (Vector3 x y z, w) = (Vector4 x y z w) + + perspective_down (Vector4 x y z w) = (Vector3 (x/w) (y/w) (z/w), w) + perspective_up (Vector3 x y z, w) = (Vector4 (x*w) (y*w) (z*w) w)
+ Data/Vector/Transform/Fancy.hs view
@@ -0,0 +1,166 @@+{- | + Generically handle transforms, and things that are transformable. +-} + +{-# LANGUAGE MultiParamTypeClasses, TypeFamilies #-} + +module Data.Vector.Transform.Fancy where + +import Data.Angle + +import Data.Vector.Class +import Data.Vector.V1 +import Data.Vector.V2 +import Data.Vector.V3 +import Data.Vector.V4 +import Data.Vector.Axis +import Data.Vector.Transform.T1 +import Data.Vector.Transform.T2 +import Data.Vector.Transform.T3 +import Data.Vector.Transform.T4 + +-- | Class for transforms. +class Transform t where + -- | The type of vector that can be transformed. (E.g., for @Transform3@, this would be @Vector3@.) + type Point t :: * + + -- | Transform a vector. + transformP :: t -> Point t -> Point t + + -- | Build transform: translate by the given vector. + translateT :: Point t -> t + + -- | Build transform: scale each coordinate axis according to the given vector. + scaleT :: Point t -> t + + -- | Build transform: scale all axies uniformly. + scaleT_ :: Scalar -> t + +-- | Class for performing rotationes. (The rotations that exist vary with the number of spatial dimensions available.) +class (Transform t) => Rotate t axis1 axis2 where + -- | Build transform: rotate in the plane defined by the two axies. + rotateT :: (Angle a) => axis1 -> axis2 -> a Scalar -> t + +-- | Class for things that can be transformed (by a specific type of transform). +class (Transform t) => Transformable t x where + -- | Transform anything that can be transformed (for a given type of transform). + transform :: t -> x -> x + + + +instance Transform Transform1 where + type Point Transform1 = Vector1 + transformP = transformP1 + translateT (Vector1 x) = Transform1 1 x + scaleT (Vector1 x) = Transform1 x 0 + scaleT_ k = Transform1 k 0 + +instance Transform Transform2 where + type Point Transform2 = Vector2 + transformP = transformP2 + translateT (Vector2 x y) = Transform2 1 0 x 0 1 y + scaleT (Vector2 x y) = Transform2 x 0 0 0 y 0 + scaleT_ k = Transform2 k 0 0 0 k 0 + +instance Transform Transform3 where + type Point Transform3 = Vector3 + transformP = transformP3 + translateT (Vector3 x y z) = Transform3 1 0 0 x 0 1 0 y 0 0 1 z + scaleT (Vector3 x y z) = Transform3 x 0 0 0 0 y 0 0 0 0 z 0 + scaleT_ k = Transform3 k 0 0 0 0 k 0 0 0 0 k 0 + +instance Transform Transform4 where + type Point Transform4 = Vector4 + transformP = transformP4 + translateT (Vector4 x y z w) = Transform4 1 0 0 0 x 0 1 0 0 y 0 0 1 0 z 0 0 0 1 w + scaleT (Vector4 x y z w) = Transform4 x 0 0 0 0 0 y 0 0 0 0 0 z 0 0 0 0 0 w 0 + scaleT_ k = Transform4 k 0 0 0 0 0 k 0 0 0 0 0 k 0 0 0 0 0 k 0 + + + +instance Transformable Transform1 Vector1 where transform = transformP1 +instance Transformable Transform2 Vector2 where transform = transformP2 +instance Transformable Transform3 Vector3 where transform = transformP3 +instance Transformable Transform4 Vector4 where transform = transformP4 + + + +instance Rotate Transform2 AxisX AxisY where + rotateT _ _ a = + let + s = sine a + c = cosine a + s' = negate s + in Transform2 c s' 0 s c 0 + +instance Rotate Transform3 AxisX AxisY where + rotateT _ _ a = + let + s = sine a + c = cosine a + s' = negate s + in Transform3 c s' 0 0 s c 0 0 0 0 1 0 + +instance Rotate Transform3 AxisX AxisZ where + rotateT _ _ a = + let + s = sine a + c = cosine a + s' = negate s + in Transform3 c 0 s' 0 0 1 0 0 s 0 c 0 + +instance Rotate Transform3 AxisY AxisZ where + rotateT _ _ a = + let + s = sine a + c = cosine a + s' = negate s + in Transform3 1 0 0 0 0 c s' 0 0 s c 0 + +instance Rotate Transform4 AxisX AxisY where + rotateT _ _ a = + let + s = sine a + c = cosine a + s' = negate s + in Transform4 c s' 0 0 0 s c 0 0 0 0 0 1 0 0 0 0 0 1 0 + +instance Rotate Transform4 AxisX AxisZ where + rotateT _ _ a = + let + s = sine a + c = cosine a + s' = negate s + in Transform4 c 0 s' 0 0 0 1 0 0 0 s 0 c 0 0 0 0 0 1 0 + +instance Rotate Transform4 AxisX AxisW where + rotateT _ _ a = + let + s = sine a + c = cosine a + s' = negate s + in Transform4 c 0 0 s' 0 0 1 0 0 0 0 0 1 0 0 s 0 0 c 0 + +instance Rotate Transform4 AxisY AxisZ where + rotateT _ _ a = + let + s = sine a + c = cosine a + s' = negate s + in Transform4 1 0 0 0 0 0 c s' 0 0 0 s c 0 0 0 0 0 1 0 + +instance Rotate Transform4 AxisY AxisW where + rotateT _ _ a = + let + s = sine a + c = cosine a + s' = negate s + in Transform4 1 0 0 0 0 0 c 0 s' 0 0 0 1 0 0 0 s 0 c 0 + +instance Rotate Transform4 AxisZ AxisW where + rotateT _ _ a = + let + s = sine a + c = cosine a + s' = negate s + in Transform4 1 0 0 0 0 0 1 0 0 0 0 0 c s' 0 0 0 s c 0
+ License.txt view
@@ -0,0 +1,10 @@+Copyright (c) 2009, Andrew Coppin +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the name of Andrew Coppin nor the names of the contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple +main = defaultMain