我正在研究一个Haskell光线跟踪器 . 我有以下相机类型:
data Cameras = Ortographic | Pinhole {
d :: Float,
zoom :: Float,
eye, lookAt, up :: Vector,
cu, cv, cw :: Vector
} deriving (Show)
以下Camera-typeclass:
class Camera a where
renderPixel :: a -> (Float, Float) -> [Object] -> Float -> Vector
rayDirection :: a -> Vector -> Vector
现在,当我尝试使类型成为类型类的实例时,如下所示:
instance Camera Cameras where
--Ortographic
renderPixel (Ortographic) (x, y) scene numSamples = ...
--Pinhole
rayDirection (Pinhole d _ _ _ _ cu cv cw) (Vector2 u v) =
normalize ((cu<*>u) <+> (cv<*>v) <-> (cw<*>d))
renderPixel (Pinhole d _ _ _ _ cu cv cw) (x, y) scene numSamples = ...
我收到一条错误,上面写着“'renderPixel'的定义冲突”,指向每个摄像头启动renderPixel函数的行 . 我究竟做错了什么?
1 回答
我很确定这两个方程式必须在彼此之后才是正确的 . 也就是说,
rayDirection
函数应该移动到两个renderPixel
方程之前或之后 .由于模式匹配,函数可以有多个方程(线),但它仍然是单个函数,并且您不能在方程之间推送另一个函数定义 .