packages feed

HGamer3D-Data-0.1.5: HGamer3D/Data/Vector3.hs

-- This source file is part of HGamer3D
-- (A project to enable 3D game development in Haskell)
-- For the latest info, see http://www.althainz.de/HGamer3D.html
--
-- (c) 2011 Peter Althainz
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- Vector3.hs

module HGamer3D.Data.Vector3 

(
	-- export Vector3 Type and its functions again
	
	Vector3 (Vector3),
	vector3,
	uniV3,
	v3X,v3Y,v3Z,
	HGamer3D.Data.Vector3.dotV3,
	HGamer3D.Data.Vector3.normSqV3,
	HGamer3D.Data.Vector3.normV3,
	HGamer3D.Data.Vector3.normalizeV3,
	HGamer3D.Data.Vector3.crossV3,
	HGamer3D.Data.Vector3.scaleV3
	
)

where

import Data.Vec.Base
import Data.Vec.Nat
import Data.Vec.Packed
import Data.Vec.LinAlg

newtype Vector3 = Vector3 Vec3F

vector3 :: Float -> Float -> Float -> Vector3
vector3 a b c = Vector3 ( pack (a :. b :. c :. ()) )

uniV3 :: Float -> Vector3
uniV3 f = Vector3 (pack (vec f))

v3X :: Vector3 -> Float
v3X (Vector3 v3) = get n0 v3

v3Y :: Vector3 -> Float
v3Y (Vector3 v3) = get n1 v3

v3Z :: Vector3 -> Float
v3Z (Vector3 v3) = get n2 v3



liftv :: (a -> Vec3F) -> (a -> Vector3)
liftv f = f1 where
	f1 a1 = (Vector3 (f a1)) 


lift1 :: (Vec3F -> a) -> (Vector3 -> a)
lift1 f = f1 where
	f1 (Vector3 a1) = f a1 

lift1v :: (Vec3F -> Vec3F) -> (Vector3 -> Vector3)
lift1v f = f2 where
	f2 a = (Vector3 (f1 a))
	f1 (Vector3 a1) = f a1 

lift2 :: (Vec3F -> Vec3F -> a) -> (Vector3 -> Vector3 -> a)
lift2 f = f1 where
	f1 (Vector3 a1) (Vector3 b1) = f a1 b1 

lift2v :: (Vec3F -> Vec3F -> Vec3F) -> (Vector3 -> Vector3 -> Vector3)
lift2v f = f2 where
	f2 a b = (Vector3 (f1 a b))
	f1 (Vector3 a1) (Vector3 b1) = f a1 b1 

lift2vp :: (Vec3 Float -> Vec3 Float -> Vec3 Float) -> (Vector3 -> Vector3 -> Vector3)
lift2vp f = f2 where
	f2 a b = (Vector3 (pack (f1 a b)))
	f1 (Vector3 (Vec3F a1 a2 a3)) (Vector3 (Vec3F b1 b2 b3)) = f (a1 :. a2 :. a3 :. ()) (b1 :. b2 :. b3 :. ()) 

instance Show Vector3 where
	show = lift1 (Prelude.show)
	
instance Eq Vector3 where
	(==) = lift2 (Prelude.==)
	 
instance Num Vector3 where
	abs = lift1v (Prelude.abs)
	signum = lift1v (Prelude.signum)
	fromInteger = liftv (Prelude.fromInteger)

	(+) = lift2v (Prelude.+)
	(-) = lift2v (Prelude.-)
	(*) = lift2v (Prelude.*)

dotV3 :: Vector3 -> Vector3 -> Float
dotV3 = lift2 Data.Vec.LinAlg.dot

normSqV3 :: Vector3 -> Float
normSqV3 = lift1 Data.Vec.LinAlg.normSq

normV3 :: Vector3 -> Float
normV3 = lift1 Data.Vec.LinAlg.norm

normalizeV3 :: Vector3 -> Vector3
normalizeV3 = lift1v Data.Vec.LinAlg.normalize

crossV3 :: Vector3 -> Vector3 -> Vector3
crossV3 = lift2vp Data.Vec.LinAlg.cross

scaleV3 :: Float -> Vector3 -> Vector3
scaleV3 f (Vector3 v) = Vector3 (Data.Vec.Base.map (*f) v)