2020-08-05 20:23:16 +00:00
|
|
|
{-# LANGUAGE AllowAmbiguousTypes #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE DeriveTraversable #-}
|
|
|
|
{-# LANGUAGE DerivingStrategies #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2020-08-04 06:15:06 +00:00
|
|
|
|
|
|
|
module Math.Bezier.Cubic
|
|
|
|
( Bezier(..)
|
|
|
|
, bezier, bezier'
|
2020-08-10 14:38:27 +00:00
|
|
|
, subdivide
|
2020-08-12 20:43:47 +00:00
|
|
|
, closestPoint
|
2020-08-04 06:15:06 +00:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
-- base
|
2020-08-12 20:43:47 +00:00
|
|
|
import Data.List.NonEmpty
|
|
|
|
( NonEmpty(..) )
|
2020-08-04 06:15:06 +00:00
|
|
|
import GHC.Generics
|
|
|
|
( Generic )
|
|
|
|
|
|
|
|
-- acts
|
|
|
|
import Data.Act
|
2020-08-05 20:23:16 +00:00
|
|
|
( Torsor
|
2020-08-04 06:15:06 +00:00
|
|
|
( (-->) )
|
|
|
|
)
|
|
|
|
|
|
|
|
-- MetaBrush
|
2020-08-12 20:43:47 +00:00
|
|
|
import qualified Math.Bezier.Quadratic as Quadratic
|
|
|
|
( Bezier(Bezier), bezier )
|
2020-08-04 06:15:06 +00:00
|
|
|
import Math.Module
|
|
|
|
( Module (..)
|
|
|
|
, lerp
|
2020-08-12 20:43:47 +00:00
|
|
|
, Inner(..), squaredNorm
|
2020-08-04 06:15:06 +00:00
|
|
|
)
|
2020-08-12 20:43:47 +00:00
|
|
|
import Math.RealRoots
|
|
|
|
( realRoots )
|
2020-08-04 06:15:06 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Points defining a cubic Bézier curve.
|
|
|
|
--
|
|
|
|
-- @ p0 @ and @ p3 @ are endpoints, whereas @ p1 @ and @ p2 @ are control points.
|
|
|
|
data Bezier p
|
|
|
|
= Bezier
|
|
|
|
{ p0 :: !p
|
|
|
|
, p1 :: !p
|
|
|
|
, p2 :: !p
|
|
|
|
, p3 :: !p
|
|
|
|
}
|
|
|
|
deriving stock ( Show, Generic, Functor, Foldable, Traversable )
|
|
|
|
|
|
|
|
-- | Cubic Bézier curve.
|
|
|
|
bezier :: forall v r p. ( Torsor v p, Module r v ) => Bezier p -> r -> p
|
|
|
|
bezier ( Bezier { .. } ) t =
|
|
|
|
lerp @v t
|
2020-08-05 20:23:16 +00:00
|
|
|
( Quadratic.bezier @v ( Quadratic.Bezier p0 p1 p2 ) t )
|
|
|
|
( Quadratic.bezier @v ( Quadratic.Bezier p1 p2 p3 ) t )
|
2020-08-04 06:15:06 +00:00
|
|
|
|
|
|
|
-- | Derivative of cubic Bézier curve.
|
|
|
|
bezier' :: forall v r p. ( Torsor v p, Module r v ) => Bezier p -> r -> v
|
|
|
|
bezier' ( Bezier { .. } ) t
|
|
|
|
= ( 3 *^ )
|
|
|
|
$ lerp @v t
|
|
|
|
( lerp @v t ( p0 --> p1 ) ( p1 --> p2 ) )
|
|
|
|
( lerp @v t ( p1 --> p2 ) ( p2 --> p3 ) )
|
2020-08-10 14:38:27 +00:00
|
|
|
|
|
|
|
-- | Subdivide a cubic Bézier curve into two parts.
|
|
|
|
subdivide :: forall v r p. ( Torsor v p, Module r v ) => Bezier p -> r -> ( Bezier p, Bezier p )
|
|
|
|
subdivide ( Bezier { .. } ) t = ( Bezier p0 q1 q2 pt, Bezier pt r1 r2 p3 )
|
|
|
|
where
|
|
|
|
pt, s, q1, q2, r1, r2 :: p
|
|
|
|
q1 = lerp @v t p0 p1
|
|
|
|
s = lerp @v t p1 p2
|
|
|
|
r2 = lerp @v t p2 p3
|
|
|
|
q2 = lerp @v t q1 s
|
|
|
|
r1 = lerp @v t s r2
|
|
|
|
pt = lerp @v t q2 r1
|
2020-08-12 20:43:47 +00:00
|
|
|
|
|
|
|
-- | Finds the closest point to a given point on a cubic Bézier curve.
|
|
|
|
closestPoint :: forall v r p. ( Torsor v p, Inner r v, RealFloat r ) => Bezier p -> p -> ( r, p )
|
|
|
|
closestPoint pts@( Bezier { .. } ) c = pickClosest ( 0 :| 1 : roots )
|
|
|
|
where
|
|
|
|
roots :: [ r ]
|
|
|
|
roots = filter ( \ r -> r > 0 && r < 1 ) ( realRoots [ a0, a1, a2, a3, a4, a5 ] )
|
|
|
|
|
|
|
|
v, v', v'', v''' :: v
|
|
|
|
v = c --> p0
|
|
|
|
v' = p0 --> p1
|
|
|
|
v'' = p1 --> p0 ^+^ p1 --> p2
|
|
|
|
v''' = p0 --> p3 ^+^ 3 *^ ( p2 --> p1 )
|
|
|
|
|
|
|
|
a0, a1, a2, a3, a4, a5 :: r
|
|
|
|
a0 = v ^.^ v'
|
|
|
|
a1 = 3 * squaredNorm v' + 2 * v ^.^ v''
|
|
|
|
a2 = 9 * v' ^.^ v'' + 3 * v ^.^ v'''
|
|
|
|
a3 = 6 * squaredNorm v'' + 4 * v' ^.^ v'''
|
|
|
|
a4 = 5 * v'' ^.^ v'''
|
|
|
|
a5 = squaredNorm v'''
|
|
|
|
|
|
|
|
pickClosest :: NonEmpty r -> ( r, p )
|
|
|
|
pickClosest ( s :| ss ) = go s q nm0 ss
|
|
|
|
where
|
|
|
|
q :: p
|
|
|
|
q = bezier @v pts s
|
|
|
|
nm0 :: r
|
|
|
|
nm0 = squaredNorm ( c --> q :: v )
|
|
|
|
go t p _ [] = ( t, p )
|
|
|
|
go t p nm ( t' : ts )
|
|
|
|
| nm' < nm = go t' p' nm' ts
|
|
|
|
| otherwise = go t p nm ts
|
|
|
|
where
|
|
|
|
p' :: p
|
|
|
|
p' = bezier @v pts t'
|
|
|
|
nm' :: r
|
|
|
|
nm' = squaredNorm ( c --> p' :: v )
|