From: MenTaLguY <me...@ry...> - 2007-03-15 19:02:07
|
I've been thinking about Curve more since last night and I think Mike is correct that the Curve implementations and the various function types ought to be unified. As usual, formulating the problem in Haskell has been helpful: module Geom2.Curve ( Curve (Curve), ICurve (..) ) where import Control.Monad import Data.Maybe import Data.Typeable import Data.Dynamic import Geom2.D2 import Geom2.Point -- type Point = D2 Double import Geom2.Rect -- type Rect = D2 Range import Geom2.Function class ICurve c where initialPoint :: c -> Point finalPoint :: c -> Point fastBounds :: c -> Rect exactBounds :: c -> Rect winding :: Point -> c -> Maybe Int subdivide :: Double -> c -> (Curve, Curve) pointAt :: Double -> c -> Point pointAndDerivativesAt :: Double -> Int -> c -> [Point] toSBasis :: c -> D2 SBasis data Curve = forall c. (ICurve c, Typeable c) => Curve c fromCurve :: (ICurve c, Typeable c) => Curve -> c fromCurve (Curve c) = fromDynamic $ toDyn c instance ICurve Curve where initialPoint (Curve c) = initialPoint c finalPoint (Curve c) = finalPoint c fastBounds (Curve c) = fastBounds c exactBounds (Curve c) = exactBounds c winding p (Curve c) = winding p c subdivide t (Curve c) = subdivide t c pointAt t (Curve c) = pointAt t c pointAndDerivativesAt t n (Curve c) = pointAndDerivativesAt t n c toSBasis (Curve c) = toSBasis c -- and then we should be able to define for all D2'ed functions... instance (Function f) => ICurve (D2 f) where -- etc... In C++, I don't know whether it would work best for e.g. D2<SBasis> to derive from Curve, or whether we keep SBasisCurve, which derives from Curve and D2<SBasis> both. |