{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE PackageImports #-}
module Algorithms.Geometry.LinearProgramming.LP2DRIC( solveBoundedLinearProgram
, solveBoundedLinearProgram'
, maximumOn
, oneDLinearProgramming
, commonIntersection
, cmpHalfPlane
) where
import Algorithms.Geometry.LinearProgramming.Types
import Control.Lens
import Control.Monad (foldM)
import Control.Monad.Random.Class
import Data.Ext
import qualified Data.Foldable as F
import Data.Geometry.Boundary
import Data.Geometry.HalfLine
import Data.Geometry.HalfSpace
import Data.Geometry.HyperPlane
import Data.Geometry.Line
import Data.Geometry.Point
import Data.Geometry.Properties
import Data.Geometry.Vector
import qualified Data.List.NonEmpty as NonEmpty
import Data.Maybe (mapMaybe)
import Data.Util
import Data.Vinyl
import Data.Vinyl.CoRec
import "hgeometry-combinatorial" System.Random.Shuffle
_solveLinearProgram :: MonadRandom m => LinearProgram 2 r -> m (LPSolution 2 r)
_solveLinearProgram :: LinearProgram 2 r -> m (LPSolution 2 r)
_solveLinearProgram = LinearProgram 2 r -> m (LPSolution 2 r)
forall a. HasCallStack => a
undefined
solveBoundedLinearProgram :: (MonadRandom m, Ord r, Fractional r)
=> LinearProgram 2 r -> m (Maybe (Point 2 r))
solveBoundedLinearProgram :: LinearProgram 2 r -> m (Maybe (Point 2 r))
solveBoundedLinearProgram (LinearProgram Vector 2 r
c [HalfSpace 2 r]
hs') = case [HalfSpace 2 r]
hs' of
[] -> Maybe (Point 2 r) -> m (Maybe (Point 2 r))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Point 2 r)
forall a. Maybe a
Nothing
[HalfSpace 2 r
m1] -> Maybe (Point 2 r) -> m (Maybe (Point 2 r))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Point 2 r) -> m (Maybe (Point 2 r)))
-> Maybe (Point 2 r) -> m (Maybe (Point 2 r))
forall a b. (a -> b) -> a -> b
$ Point 2 r -> Maybe (Point 2 r)
forall a. a -> Maybe a
Just (HalfSpace 2 r
m1HalfSpace 2 r
-> Getting (Point 2 r) (HalfSpace 2 r) (Point 2 r) -> Point 2 r
forall s a. s -> Getting a s a -> a
^.(HyperPlane 2 r -> Const (Point 2 r) (HyperPlane 2 r))
-> HalfSpace 2 r -> Const (Point 2 r) (HalfSpace 2 r)
forall (d :: Nat) r (d2 :: Nat) r2.
Iso
(HalfSpace d r)
(HalfSpace d2 r2)
(HyperPlane d r)
(HyperPlane d2 r2)
boundingPlane((HyperPlane 2 r -> Const (Point 2 r) (HyperPlane 2 r))
-> HalfSpace 2 r -> Const (Point 2 r) (HalfSpace 2 r))
-> ((Point 2 r -> Const (Point 2 r) (Point 2 r))
-> HyperPlane 2 r -> Const (Point 2 r) (HyperPlane 2 r))
-> Getting (Point 2 r) (HalfSpace 2 r) (Point 2 r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Point 2 r -> Const (Point 2 r) (Point 2 r))
-> HyperPlane 2 r -> Const (Point 2 r) (HyperPlane 2 r)
forall (d :: Nat) r. Lens' (HyperPlane d r) (Point d r)
inPlane)
(HalfSpace 2 r
m1:HalfSpace 2 r
m2:[HalfSpace 2 r]
hs) -> LinearProgram 2 r -> Maybe (Point 2 r)
forall r.
(Ord r, Fractional r) =>
LinearProgram 2 r -> Maybe (Point 2 r)
solveBoundedLinearProgram' (LinearProgram 2 r -> Maybe (Point 2 r))
-> (Vector (HalfSpace 2 r) -> LinearProgram 2 r)
-> Vector (HalfSpace 2 r)
-> Maybe (Point 2 r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector 2 r -> [HalfSpace 2 r] -> LinearProgram 2 r
forall (d :: Nat) r.
Vector d r -> [HalfSpace d r] -> LinearProgram d r
LinearProgram Vector 2 r
c ([HalfSpace 2 r] -> LinearProgram 2 r)
-> (Vector (HalfSpace 2 r) -> [HalfSpace 2 r])
-> Vector (HalfSpace 2 r)
-> LinearProgram 2 r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([HalfSpace 2 r
m1,HalfSpace 2 r
m2] [HalfSpace 2 r] -> [HalfSpace 2 r] -> [HalfSpace 2 r]
forall a. [a] -> [a] -> [a]
++) ([HalfSpace 2 r] -> [HalfSpace 2 r])
-> (Vector (HalfSpace 2 r) -> [HalfSpace 2 r])
-> Vector (HalfSpace 2 r)
-> [HalfSpace 2 r]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (HalfSpace 2 r) -> [HalfSpace 2 r]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList
(Vector (HalfSpace 2 r) -> Maybe (Point 2 r))
-> m (Vector (HalfSpace 2 r)) -> m (Maybe (Point 2 r))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [HalfSpace 2 r] -> m (Vector (HalfSpace 2 r))
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, MonadRandom m) =>
f a -> m (Vector a)
shuffle [HalfSpace 2 r]
hs
solveBoundedLinearProgram' :: (Ord r, Fractional r)
=> LinearProgram 2 r -> Maybe (Point 2 r)
solveBoundedLinearProgram' :: LinearProgram 2 r -> Maybe (Point 2 r)
solveBoundedLinearProgram' LinearProgram 2 r
lp = let (LPState 2 r
s,[HalfSpace 2 r]
hs) = LinearProgram 2 r -> (LPState 2 r, [HalfSpace 2 r])
forall r.
(Ord r, Fractional r) =>
LinearProgram 2 r -> (LPState 2 r, [HalfSpace 2 r])
initialize LinearProgram 2 r
lp
in (LPState 2 r
-> Getting (Point 2 r) (LPState 2 r) (Point 2 r) -> Point 2 r
forall s a. s -> Getting a s a -> a
^.Getting (Point 2 r) (LPState 2 r) (Point 2 r)
forall (d :: Nat) r. Lens' (LPState d r) (Point d r)
current) (LPState 2 r -> Point 2 r)
-> Maybe (LPState 2 r) -> Maybe (Point 2 r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (LPState 2 r -> HalfSpace 2 r -> Maybe (LPState 2 r))
-> LPState 2 r -> [HalfSpace 2 r] -> Maybe (LPState 2 r)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM LPState 2 r -> HalfSpace 2 r -> Maybe (LPState 2 r)
forall r.
(Fractional r, Ord r) =>
LPState 2 r -> HalfSpace 2 r -> Maybe (LPState 2 r)
step LPState 2 r
s [HalfSpace 2 r]
hs
data LPState d r = LPState { LPState d r -> Vector d r
_obj :: !(Vector d r)
, LPState d r -> [HalfSpace d r]
_seen :: [HalfSpace d r]
, LPState d r -> Point d r
_current :: !(Point d r)
}
deriving instance (Arity d, Show r) => Show (LPState d r)
deriving instance (Arity d, Eq r, Fractional r) => Eq (LPState d r)
obj :: Lens' (LPState d r) (Vector d r)
obj :: (Vector d r -> f (Vector d r)) -> LPState d r -> f (LPState d r)
obj = (LPState d r -> Vector d r)
-> (LPState d r -> Vector d r -> LPState d r)
-> Lens (LPState d r) (LPState d r) (Vector d r) (Vector d r)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens LPState d r -> Vector d r
forall (d :: Nat) r. LPState d r -> Vector d r
_obj (\(LPState Vector d r
_ [HalfSpace d r]
s Point d r
p) Vector d r
o -> Vector d r -> [HalfSpace d r] -> Point d r -> LPState d r
forall (d :: Nat) r.
Vector d r -> [HalfSpace d r] -> Point d r -> LPState d r
LPState Vector d r
o [HalfSpace d r]
s Point d r
p)
seen :: Lens' (LPState d r) [HalfSpace d r]
seen :: ([HalfSpace d r] -> f [HalfSpace d r])
-> LPState d r -> f (LPState d r)
seen = (LPState d r -> [HalfSpace d r])
-> (LPState d r -> [HalfSpace d r] -> LPState d r)
-> Lens (LPState d r) (LPState d r) [HalfSpace d r] [HalfSpace d r]
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens LPState d r -> [HalfSpace d r]
forall (d :: Nat) r. LPState d r -> [HalfSpace d r]
_seen (\(LPState Vector d r
o [HalfSpace d r]
_ Point d r
p) [HalfSpace d r]
s -> Vector d r -> [HalfSpace d r] -> Point d r -> LPState d r
forall (d :: Nat) r.
Vector d r -> [HalfSpace d r] -> Point d r -> LPState d r
LPState Vector d r
o [HalfSpace d r]
s Point d r
p)
current :: Lens' (LPState d r) (Point d r)
current :: (Point d r -> f (Point d r)) -> LPState d r -> f (LPState d r)
current = (LPState d r -> Point d r)
-> (LPState d r -> Point d r -> LPState d r)
-> Lens (LPState d r) (LPState d r) (Point d r) (Point d r)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens LPState d r -> Point d r
forall (d :: Nat) r. LPState d r -> Point d r
_current (\(LPState Vector d r
o [HalfSpace d r]
s Point d r
_) Point d r
p -> Vector d r -> [HalfSpace d r] -> Point d r -> LPState d r
forall (d :: Nat) r.
Vector d r -> [HalfSpace d r] -> Point d r -> LPState d r
LPState Vector d r
o [HalfSpace d r]
s Point d r
p)
step :: (Fractional r, Ord r)
=> LPState 2 r -> HalfSpace 2 r
-> Maybe (LPState 2 r)
step :: LPState 2 r -> HalfSpace 2 r -> Maybe (LPState 2 r)
step LPState 2 r
s HalfSpace 2 r
h | (LPState 2 r
sLPState 2 r
-> Getting (Point 2 r) (LPState 2 r) (Point 2 r) -> Point 2 r
forall s a. s -> Getting a s a -> a
^.Getting (Point 2 r) (LPState 2 r) (Point 2 r)
forall (d :: Nat) r. Lens' (LPState d r) (Point d r)
current) Point 2 r -> HalfSpace 2 r -> Bool
forall g h. IsIntersectableWith g h => g -> h -> Bool
`intersects` HalfSpace 2 r
h = LPState 2 r -> Maybe (LPState 2 r)
forall a. a -> Maybe a
Just (LPState 2 r -> Maybe (LPState 2 r))
-> LPState 2 r -> Maybe (LPState 2 r)
forall a b. (a -> b) -> a -> b
$ LPState 2 r
sLPState 2 r -> (LPState 2 r -> LPState 2 r) -> LPState 2 r
forall a b. a -> (a -> b) -> b
&([HalfSpace 2 r] -> Identity [HalfSpace 2 r])
-> LPState 2 r -> Identity (LPState 2 r)
forall (d :: Nat) r. Lens' (LPState d r) [HalfSpace d r]
seen (([HalfSpace 2 r] -> Identity [HalfSpace 2 r])
-> LPState 2 r -> Identity (LPState 2 r))
-> ([HalfSpace 2 r] -> [HalfSpace 2 r])
-> LPState 2 r
-> LPState 2 r
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (HalfSpace 2 r
hHalfSpace 2 r -> [HalfSpace 2 r] -> [HalfSpace 2 r]
forall a. a -> [a] -> [a]
:)
| Bool
otherwise = (\Point 2 r
p -> LPState 2 r
sLPState 2 r -> (LPState 2 r -> LPState 2 r) -> LPState 2 r
forall a b. a -> (a -> b) -> b
&([HalfSpace 2 r] -> Identity [HalfSpace 2 r])
-> LPState 2 r -> Identity (LPState 2 r)
forall (d :: Nat) r. Lens' (LPState d r) [HalfSpace d r]
seen (([HalfSpace 2 r] -> Identity [HalfSpace 2 r])
-> LPState 2 r -> Identity (LPState 2 r))
-> ([HalfSpace 2 r] -> [HalfSpace 2 r])
-> LPState 2 r
-> LPState 2 r
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (HalfSpace 2 r
hHalfSpace 2 r -> [HalfSpace 2 r] -> [HalfSpace 2 r]
forall a. a -> [a] -> [a]
:)
LPState 2 r -> (LPState 2 r -> LPState 2 r) -> LPState 2 r
forall a b. a -> (a -> b) -> b
&(Point 2 r -> Identity (Point 2 r))
-> LPState 2 r -> Identity (LPState 2 r)
forall (d :: Nat) r. Lens' (LPState d r) (Point d r)
current ((Point 2 r -> Identity (Point 2 r))
-> LPState 2 r -> Identity (LPState 2 r))
-> Point 2 r -> LPState 2 r -> LPState 2 r
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Point 2 r
p)
(Point 2 r -> LPState 2 r)
-> Maybe (Point 2 r) -> Maybe (LPState 2 r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LPState 2 r -> Line 2 r -> Maybe (Point 2 r)
forall r.
(Ord r, Fractional r) =>
LPState 2 r -> Line 2 r -> Maybe (Point 2 r)
maximumOn LPState 2 r
s (HalfSpace 2 r
hHalfSpace 2 r
-> Getting (Line 2 r) (HalfSpace 2 r) (Line 2 r) -> Line 2 r
forall s a. s -> Getting a s a -> a
^.(HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r))
-> HalfSpace 2 r -> Const (Line 2 r) (HalfSpace 2 r)
forall (d :: Nat) r (d2 :: Nat) r2.
Iso
(HalfSpace d r)
(HalfSpace d2 r2)
(HyperPlane d r)
(HyperPlane d2 r2)
boundingPlane((HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r))
-> HalfSpace 2 r -> Const (Line 2 r) (HalfSpace 2 r))
-> ((Line 2 r -> Const (Line 2 r) (Line 2 r))
-> HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r))
-> Getting (Line 2 r) (HalfSpace 2 r) (Line 2 r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Line 2 r -> Const (Line 2 r) (Line 2 r))
-> HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r)
forall r. Num r => Iso' (HyperPlane 2 r) (Line 2 r)
_asLine)
collectOn :: (Ord r, Fractional r)
=> Line 2 r
-> [HalfSpace 2 r]
-> Maybe [HalfLine 2 r]
collectOn :: Line 2 r -> [HalfSpace 2 r] -> Maybe [HalfLine 2 r]
collectOn Line 2 r
l = [Maybe (HalfLine 2 r)] -> Maybe [HalfLine 2 r]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([Maybe (HalfLine 2 r)] -> Maybe [HalfLine 2 r])
-> ([HalfSpace 2 r] -> [Maybe (HalfLine 2 r)])
-> [HalfSpace 2 r]
-> Maybe [HalfLine 2 r]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HalfSpace 2 r -> Maybe (Maybe (HalfLine 2 r)))
-> [HalfSpace 2 r] -> [Maybe (HalfLine 2 r)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (CoRec Identity '[NoIntersection, HalfLine 2 r, Line 2 r]
-> Maybe (Maybe (HalfLine 2 r))
forall r.
Intersection (Line 2 r) (HalfSpace 2 r)
-> Maybe (Maybe (HalfLine 2 r))
collect (CoRec Identity '[NoIntersection, HalfLine 2 r, Line 2 r]
-> Maybe (Maybe (HalfLine 2 r)))
-> (HalfSpace 2 r
-> CoRec Identity '[NoIntersection, HalfLine 2 r, Line 2 r])
-> HalfSpace 2 r
-> Maybe (Maybe (HalfLine 2 r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Line 2 r
l Line 2 r
-> HalfSpace 2 r -> Intersection (Line 2 r) (HalfSpace 2 r)
forall g h. IsIntersectableWith g h => g -> h -> Intersection g h
`intersect`))
where
collect :: Intersection (Line 2 r) (HalfSpace 2 r) -> Maybe (Maybe (HalfLine 2 r))
collect :: Intersection (Line 2 r) (HalfSpace 2 r)
-> Maybe (Maybe (HalfLine 2 r))
collect Intersection (Line 2 r) (HalfSpace 2 r)
r = CoRec Identity '[NoIntersection, HalfLine 2 r, Line 2 r]
-> Handlers
'[NoIntersection, HalfLine 2 r, Line 2 r]
(Maybe (Maybe (HalfLine 2 r)))
-> Maybe (Maybe (HalfLine 2 r))
forall (ts :: [*]) b. CoRec Identity ts -> Handlers ts b -> b
match CoRec Identity '[NoIntersection, HalfLine 2 r, Line 2 r]
Intersection (Line 2 r) (HalfSpace 2 r)
r (Handlers
'[NoIntersection, HalfLine 2 r, Line 2 r]
(Maybe (Maybe (HalfLine 2 r)))
-> Maybe (Maybe (HalfLine 2 r)))
-> Handlers
'[NoIntersection, HalfLine 2 r, Line 2 r]
(Maybe (Maybe (HalfLine 2 r)))
-> Maybe (Maybe (HalfLine 2 r))
forall a b. (a -> b) -> a -> b
$
(NoIntersection -> Maybe (Maybe (HalfLine 2 r)))
-> Handler (Maybe (Maybe (HalfLine 2 r))) NoIntersection
forall b a. (a -> b) -> Handler b a
H (Maybe (Maybe (HalfLine 2 r))
-> NoIntersection -> Maybe (Maybe (HalfLine 2 r))
forall a b. a -> b -> a
const (Maybe (Maybe (HalfLine 2 r))
-> NoIntersection -> Maybe (Maybe (HalfLine 2 r)))
-> Maybe (Maybe (HalfLine 2 r))
-> NoIntersection
-> Maybe (Maybe (HalfLine 2 r))
forall a b. (a -> b) -> a -> b
$ Maybe (HalfLine 2 r) -> Maybe (Maybe (HalfLine 2 r))
forall a. a -> Maybe a
Just Maybe (HalfLine 2 r)
forall a. Maybe a
Nothing)
Handler (Maybe (Maybe (HalfLine 2 r))) NoIntersection
-> Rec
(Handler (Maybe (Maybe (HalfLine 2 r)))) '[HalfLine 2 r, Line 2 r]
-> Handlers
'[NoIntersection, HalfLine 2 r, Line 2 r]
(Maybe (Maybe (HalfLine 2 r)))
forall u (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (HalfLine 2 r -> Maybe (Maybe (HalfLine 2 r)))
-> Handler (Maybe (Maybe (HalfLine 2 r))) (HalfLine 2 r)
forall b a. (a -> b) -> Handler b a
H (Maybe (HalfLine 2 r) -> Maybe (Maybe (HalfLine 2 r))
forall a. a -> Maybe a
Just (Maybe (HalfLine 2 r) -> Maybe (Maybe (HalfLine 2 r)))
-> (HalfLine 2 r -> Maybe (HalfLine 2 r))
-> HalfLine 2 r
-> Maybe (Maybe (HalfLine 2 r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HalfLine 2 r -> Maybe (HalfLine 2 r)
forall a. a -> Maybe a
Just)
Handler (Maybe (Maybe (HalfLine 2 r))) (HalfLine 2 r)
-> Rec (Handler (Maybe (Maybe (HalfLine 2 r)))) '[Line 2 r]
-> Rec
(Handler (Maybe (Maybe (HalfLine 2 r)))) '[HalfLine 2 r, Line 2 r]
forall u (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (Line 2 r -> Maybe (Maybe (HalfLine 2 r)))
-> Handler (Maybe (Maybe (HalfLine 2 r))) (Line 2 r)
forall b a. (a -> b) -> Handler b a
H (Maybe (Maybe (HalfLine 2 r))
-> Line 2 r -> Maybe (Maybe (HalfLine 2 r))
forall a b. a -> b -> a
const Maybe (Maybe (HalfLine 2 r))
forall a. Maybe a
Nothing)
Handler (Maybe (Maybe (HalfLine 2 r))) (Line 2 r)
-> Rec (Handler (Maybe (Maybe (HalfLine 2 r)))) '[]
-> Rec (Handler (Maybe (Maybe (HalfLine 2 r)))) '[Line 2 r]
forall u (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& Rec (Handler (Maybe (Maybe (HalfLine 2 r)))) '[]
forall u (a :: u -> *). Rec a '[]
RNil
cmpHalfPlane :: (Ord r, Num r, Arity d)
=> Vector d r -> Point d r -> Point d r -> Ordering
cmpHalfPlane :: Vector d r -> Point d r -> Point d r -> Ordering
cmpHalfPlane Vector d r
v Point d r
a Point d r
b = case Point d r
a Point d r -> HalfSpace d r -> PointLocationResult
forall r (d :: Nat).
(Num r, Ord r, Arity d) =>
Point d r -> HalfSpace d r -> PointLocationResult
`inHalfSpace` HyperPlane d r -> HalfSpace d r
forall (d :: Nat) r. HyperPlane d r -> HalfSpace d r
HalfSpace (Point d r -> Vector d r -> HyperPlane d r
forall (d :: Nat) r. Point d r -> Vector d r -> HyperPlane d r
HyperPlane Point d r
b Vector d r
v) of
PointLocationResult
Inside -> Ordering
GT
PointLocationResult
OnBoundary -> Ordering
EQ
PointLocationResult
Outside -> Ordering
LT
type OneOrTwo a = Either a (Two a)
flatten :: OneOrTwo a -> [a]
flatten :: OneOrTwo a -> [a]
flatten = (a -> [a]) -> (Two a -> [a]) -> OneOrTwo a -> [a]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[]) (\(Two a
a a
b) -> [a
a,a
b])
commonIntersection :: (Ord r, Num r, Arity d)
=> Line d r
-> NonEmpty.NonEmpty (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a))
(OneOrTwo (Point d r :+ a))
commonIntersection :: Line d r
-> NonEmpty (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
commonIntersection (Line Point d r
_ Vector d r
v) NonEmpty (HalfLine d r :+ a)
hls = case (Maybe (HalfLine d r :+ a)
nh,Maybe (HalfLine d r :+ a)
ph) of
(Maybe (HalfLine d r :+ a)
Nothing,Maybe (HalfLine d r :+ a)
Nothing) -> String
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a. HasCallStack => String -> a
error String
"absurd; this case cannot occur"
(Maybe (HalfLine d r :+ a)
Nothing, Just HalfLine d r :+ a
p) -> OneOrTwo (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a b. b -> Either a b
Right (OneOrTwo (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a)))
-> ((HalfLine d r :+ a) -> OneOrTwo (Point d r :+ a))
-> (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Point d r :+ a) -> OneOrTwo (Point d r :+ a)
forall a b. a -> Either a b
Left ((Point d r :+ a) -> OneOrTwo (Point d r :+ a))
-> ((HalfLine d r :+ a) -> Point d r :+ a)
-> (HalfLine d r :+ a)
-> OneOrTwo (Point d r :+ a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HalfLine d r :+ a) -> Point d r :+ a
forall (d :: Nat) r extra.
(HalfLine d r :+ extra) -> Point d r :+ extra
extract ((HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a)))
-> (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a b. (a -> b) -> a -> b
$ HalfLine d r :+ a
p
(Just HalfLine d r :+ a
n, Maybe (HalfLine d r :+ a)
Nothing) -> OneOrTwo (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a b. b -> Either a b
Right (OneOrTwo (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a)))
-> ((HalfLine d r :+ a) -> OneOrTwo (Point d r :+ a))
-> (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Point d r :+ a) -> OneOrTwo (Point d r :+ a)
forall a b. a -> Either a b
Left ((Point d r :+ a) -> OneOrTwo (Point d r :+ a))
-> ((HalfLine d r :+ a) -> Point d r :+ a)
-> (HalfLine d r :+ a)
-> OneOrTwo (Point d r :+ a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HalfLine d r :+ a) -> Point d r :+ a
forall (d :: Nat) r extra.
(HalfLine d r :+ extra) -> Point d r :+ extra
extract ((HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a)))
-> (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a b. (a -> b) -> a -> b
$ HalfLine d r :+ a
n
(Just HalfLine d r :+ a
n, Just HalfLine d r :+ a
p) -> case Vector d r
-> (HalfLine d r :+ a) -> (HalfLine d r :+ a) -> Ordering
forall (d :: Nat) r extra extra.
(ImplicitPeano (Peano d), Num r, Ord r,
ArityPeano (Peano (FromPeano (Peano d))),
KnownNat (FromPeano (Peano d)), KnownNat d,
Peano (FromPeano (Peano d) + 1)
~ 'S (Peano (FromPeano (Peano d)))) =>
Vector d r
-> (HalfLine d r :+ extra) -> (HalfLine d r :+ extra) -> Ordering
cmpHalfPlane' Vector d r
v HalfLine d r :+ a
n HalfLine d r :+ a
p of
Ordering
LT -> Two (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a b. a -> Either a b
Left (Two (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a)))
-> Two (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a b. (a -> b) -> a -> b
$ (HalfLine d r :+ a)
-> (HalfLine d r :+ a) -> Two (HalfLine d r :+ a)
forall a. a -> a -> Two a
Two HalfLine d r :+ a
n HalfLine d r :+ a
p
Ordering
EQ -> OneOrTwo (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a b. b -> Either a b
Right (OneOrTwo (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a)))
-> ((HalfLine d r :+ a) -> OneOrTwo (Point d r :+ a))
-> (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Point d r :+ a) -> OneOrTwo (Point d r :+ a)
forall a b. a -> Either a b
Left ((Point d r :+ a) -> OneOrTwo (Point d r :+ a))
-> ((HalfLine d r :+ a) -> Point d r :+ a)
-> (HalfLine d r :+ a)
-> OneOrTwo (Point d r :+ a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HalfLine d r :+ a) -> Point d r :+ a
forall (d :: Nat) r extra.
(HalfLine d r :+ extra) -> Point d r :+ extra
extract ((HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a)))
-> (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a b. (a -> b) -> a -> b
$ HalfLine d r :+ a
p
Ordering
GT -> OneOrTwo (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a b. b -> Either a b
Right (OneOrTwo (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a)))
-> (Two (Point d r :+ a) -> OneOrTwo (Point d r :+ a))
-> Two (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Two (Point d r :+ a) -> OneOrTwo (Point d r :+ a)
forall a b. b -> Either a b
Right (Two (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a)))
-> Two (Point d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
forall a b. (a -> b) -> a -> b
$ (Point d r :+ a) -> (Point d r :+ a) -> Two (Point d r :+ a)
forall a. a -> a -> Two a
Two ((HalfLine d r :+ a) -> Point d r :+ a
forall (d :: Nat) r extra.
(HalfLine d r :+ extra) -> Point d r :+ extra
extract HalfLine d r :+ a
p) ((HalfLine d r :+ a) -> Point d r :+ a
forall (d :: Nat) r extra.
(HalfLine d r :+ extra) -> Point d r :+ extra
extract HalfLine d r :+ a
n)
where
extract :: (HalfLine d r :+ extra) -> Point d r :+ extra
extract = ASetter
(HalfLine d r :+ extra)
(Point d r :+ extra)
(HalfLine d r)
(Point d r)
-> (HalfLine d r -> Point d r)
-> (HalfLine d r :+ extra)
-> Point d r :+ extra
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
(HalfLine d r :+ extra)
(Point d r :+ extra)
(HalfLine d r)
(Point d r)
forall core extra core'.
Lens (core :+ extra) (core' :+ extra) core core'
core (HalfLine d r
-> Getting (Point d r) (HalfLine d r) (Point d r) -> Point d r
forall s a. s -> Getting a s a -> a
^.Getting (Point d r) (HalfLine d r) (Point d r)
forall (d :: Nat) r. Lens' (HalfLine d r) (Point d r)
startPoint)
([HalfLine d r :+ a]
pos,[HalfLine d r :+ a]
neg) = ((HalfLine d r :+ a) -> Bool)
-> NonEmpty (HalfLine d r :+ a)
-> ([HalfLine d r :+ a], [HalfLine d r :+ a])
forall a. (a -> Bool) -> NonEmpty a -> ([a], [a])
NonEmpty.partition (\HalfLine d r :+ a
hl -> HalfLine d r :+ a
hl(HalfLine d r :+ a)
-> Getting (Vector d r) (HalfLine d r :+ a) (Vector d r)
-> Vector d r
forall s a. s -> Getting a s a -> a
^.(HalfLine d r -> Const (Vector d r) (HalfLine d r))
-> (HalfLine d r :+ a) -> Const (Vector d r) (HalfLine d r :+ a)
forall core extra core'.
Lens (core :+ extra) (core' :+ extra) core core'
core((HalfLine d r -> Const (Vector d r) (HalfLine d r))
-> (HalfLine d r :+ a) -> Const (Vector d r) (HalfLine d r :+ a))
-> ((Vector d r -> Const (Vector d r) (Vector d r))
-> HalfLine d r -> Const (Vector d r) (HalfLine d r))
-> Getting (Vector d r) (HalfLine d r :+ a) (Vector d r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Vector d r -> Const (Vector d r) (Vector d r))
-> HalfLine d r -> Const (Vector d r) (HalfLine d r)
forall (d :: Nat) r. Lens' (HalfLine d r) (Vector d r)
halfLineDirection Vector d r -> Vector d r -> Bool
forall a. Eq a => a -> a -> Bool
== Vector d r
v) NonEmpty (HalfLine d r :+ a)
hls
ph :: Maybe (HalfLine d r :+ a)
ph = ((HalfLine d r :+ a) -> (HalfLine d r :+ a) -> Ordering)
-> [HalfLine d r :+ a] -> Maybe (HalfLine d r :+ a)
forall a. (a -> a -> Ordering) -> [a] -> Maybe a
maximumBy' (Vector d r
-> (HalfLine d r :+ a) -> (HalfLine d r :+ a) -> Ordering
forall (d :: Nat) r extra extra.
(ImplicitPeano (Peano d), Num r, Ord r,
ArityPeano (Peano (FromPeano (Peano d))),
KnownNat (FromPeano (Peano d)), KnownNat d,
Peano (FromPeano (Peano d) + 1)
~ 'S (Peano (FromPeano (Peano d)))) =>
Vector d r
-> (HalfLine d r :+ extra) -> (HalfLine d r :+ extra) -> Ordering
cmpHalfPlane' Vector d r
v) [HalfLine d r :+ a]
pos
nh :: Maybe (HalfLine d r :+ a)
nh = ((HalfLine d r :+ a) -> (HalfLine d r :+ a) -> Ordering)
-> [HalfLine d r :+ a] -> Maybe (HalfLine d r :+ a)
forall a. (a -> a -> Ordering) -> [a] -> Maybe a
maximumBy' (((HalfLine d r :+ a) -> (HalfLine d r :+ a) -> Ordering)
-> (HalfLine d r :+ a) -> (HalfLine d r :+ a) -> Ordering
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((HalfLine d r :+ a) -> (HalfLine d r :+ a) -> Ordering)
-> (HalfLine d r :+ a) -> (HalfLine d r :+ a) -> Ordering)
-> ((HalfLine d r :+ a) -> (HalfLine d r :+ a) -> Ordering)
-> (HalfLine d r :+ a)
-> (HalfLine d r :+ a)
-> Ordering
forall a b. (a -> b) -> a -> b
$ Vector d r
-> (HalfLine d r :+ a) -> (HalfLine d r :+ a) -> Ordering
forall (d :: Nat) r extra extra.
(ImplicitPeano (Peano d), Num r, Ord r,
ArityPeano (Peano (FromPeano (Peano d))),
KnownNat (FromPeano (Peano d)), KnownNat d,
Peano (FromPeano (Peano d) + 1)
~ 'S (Peano (FromPeano (Peano d)))) =>
Vector d r
-> (HalfLine d r :+ extra) -> (HalfLine d r :+ extra) -> Ordering
cmpHalfPlane' Vector d r
v) [HalfLine d r :+ a]
neg
cmpHalfPlane' :: Vector d r
-> (HalfLine d r :+ extra) -> (HalfLine d r :+ extra) -> Ordering
cmpHalfPlane' Vector d r
vv HalfLine d r :+ extra
a HalfLine d r :+ extra
b = Vector d r -> Point d r -> Point d r -> Ordering
forall r (d :: Nat).
(Ord r, Num r, Arity d) =>
Vector d r -> Point d r -> Point d r -> Ordering
cmpHalfPlane Vector d r
vv (HalfLine d r :+ extra
a(HalfLine d r :+ extra)
-> Getting (Point d r) (HalfLine d r :+ extra) (Point d r)
-> Point d r
forall s a. s -> Getting a s a -> a
^.(HalfLine d r -> Const (Point d r) (HalfLine d r))
-> (HalfLine d r :+ extra)
-> Const (Point d r) (HalfLine d r :+ extra)
forall core extra core'.
Lens (core :+ extra) (core' :+ extra) core core'
core((HalfLine d r -> Const (Point d r) (HalfLine d r))
-> (HalfLine d r :+ extra)
-> Const (Point d r) (HalfLine d r :+ extra))
-> ((Point d r -> Const (Point d r) (Point d r))
-> HalfLine d r -> Const (Point d r) (HalfLine d r))
-> Getting (Point d r) (HalfLine d r :+ extra) (Point d r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Point d r -> Const (Point d r) (Point d r))
-> HalfLine d r -> Const (Point d r) (HalfLine d r)
forall (d :: Nat) r. Lens' (HalfLine d r) (Point d r)
startPoint) (HalfLine d r :+ extra
b(HalfLine d r :+ extra)
-> Getting (Point d r) (HalfLine d r :+ extra) (Point d r)
-> Point d r
forall s a. s -> Getting a s a -> a
^.(HalfLine d r -> Const (Point d r) (HalfLine d r))
-> (HalfLine d r :+ extra)
-> Const (Point d r) (HalfLine d r :+ extra)
forall core extra core'.
Lens (core :+ extra) (core' :+ extra) core core'
core((HalfLine d r -> Const (Point d r) (HalfLine d r))
-> (HalfLine d r :+ extra)
-> Const (Point d r) (HalfLine d r :+ extra))
-> ((Point d r -> Const (Point d r) (Point d r))
-> HalfLine d r -> Const (Point d r) (HalfLine d r))
-> Getting (Point d r) (HalfLine d r :+ extra) (Point d r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Point d r -> Const (Point d r) (Point d r))
-> HalfLine d r -> Const (Point d r) (HalfLine d r)
forall (d :: Nat) r. Lens' (HalfLine d r) (Point d r)
startPoint)
commonIntersection' :: (Ord r, Num r, Arity d)
=> Line d r
-> NonEmpty.NonEmpty (HalfLine d r)
-> [Point d r]
commonIntersection' :: Line d r -> NonEmpty (HalfLine d r) -> [Point d r]
commonIntersection' Line d r
l NonEmpty (HalfLine d r)
hls = (Two (HalfLine d r :+ ()) -> [Point d r])
-> (OneOrTwo (Point d r :+ ()) -> [Point d r])
-> Either (Two (HalfLine d r :+ ())) (OneOrTwo (Point d r :+ ()))
-> [Point d r]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([Point d r] -> Two (HalfLine d r :+ ()) -> [Point d r]
forall a b. a -> b -> a
const []) (((Point d r :+ ()) -> Point d r)
-> [Point d r :+ ()] -> [Point d r]
forall a b. (a -> b) -> [a] -> [b]
map ((Point d r :+ ())
-> Getting (Point d r) (Point d r :+ ()) (Point d r) -> Point d r
forall s a. s -> Getting a s a -> a
^.Getting (Point d r) (Point d r :+ ()) (Point d r)
forall core extra core'.
Lens (core :+ extra) (core' :+ extra) core core'
core) ([Point d r :+ ()] -> [Point d r])
-> (OneOrTwo (Point d r :+ ()) -> [Point d r :+ ()])
-> OneOrTwo (Point d r :+ ())
-> [Point d r]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OneOrTwo (Point d r :+ ()) -> [Point d r :+ ()]
forall a. OneOrTwo a -> [a]
flatten)
(Either (Two (HalfLine d r :+ ())) (OneOrTwo (Point d r :+ ()))
-> [Point d r])
-> Either (Two (HalfLine d r :+ ())) (OneOrTwo (Point d r :+ ()))
-> [Point d r]
forall a b. (a -> b) -> a -> b
$ Line d r
-> NonEmpty (HalfLine d r :+ ())
-> Either (Two (HalfLine d r :+ ())) (OneOrTwo (Point d r :+ ()))
forall r (d :: Nat) a.
(Ord r, Num r, Arity d) =>
Line d r
-> NonEmpty (HalfLine d r :+ a)
-> Either (Two (HalfLine d r :+ a)) (OneOrTwo (Point d r :+ a))
commonIntersection Line d r
l (HalfLine d r -> HalfLine d r :+ ()
forall a. a -> a :+ ()
ext (HalfLine d r -> HalfLine d r :+ ())
-> NonEmpty (HalfLine d r) -> NonEmpty (HalfLine d r :+ ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty (HalfLine d r)
hls)
maximumBy' :: (a -> a -> Ordering) -> [a] -> Maybe a
maximumBy' :: (a -> a -> Ordering) -> [a] -> Maybe a
maximumBy' a -> a -> Ordering
cmp = \case
[] -> Maybe a
forall a. Maybe a
Nothing
[a]
xs -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ (a -> a -> Ordering) -> [a] -> a
forall (t :: * -> *) a.
Foldable t =>
(a -> a -> Ordering) -> t a -> a
F.maximumBy a -> a -> Ordering
cmp [a]
xs
oneDLinearProgramming :: (Ord r, Num r, Arity d)
=> Vector d r -> Line d r -> [HalfLine d r] -> Maybe (Point d r)
oneDLinearProgramming :: Vector d r -> Line d r -> [HalfLine d r] -> Maybe (Point d r)
oneDLinearProgramming Vector d r
c Line d r
l [HalfLine d r]
hls = do
NonEmpty (HalfLine d r)
hls' <- [HalfLine d r] -> Maybe (NonEmpty (HalfLine d r))
forall a. [a] -> Maybe (NonEmpty a)
NonEmpty.nonEmpty [HalfLine d r]
hls
let candidates :: [Point d r]
candidates = Line d r -> NonEmpty (HalfLine d r) -> [Point d r]
forall r (d :: Nat).
(Ord r, Num r, Arity d) =>
Line d r -> NonEmpty (HalfLine d r) -> [Point d r]
commonIntersection' Line d r
l NonEmpty (HalfLine d r)
hls'
(Point d r -> Point d r -> Ordering)
-> [Point d r] -> Maybe (Point d r)
forall a. (a -> a -> Ordering) -> [a] -> Maybe a
maximumBy' (Vector d r -> Point d r -> Point d r -> Ordering
forall r (d :: Nat).
(Ord r, Num r, Arity d) =>
Vector d r -> Point d r -> Point d r -> Ordering
cmpHalfPlane Vector d r
c) [Point d r]
candidates
maximumOn :: (Ord r, Fractional r) => LPState 2 r -> Line 2 r -> Maybe (Point 2 r)
maximumOn :: LPState 2 r -> Line 2 r -> Maybe (Point 2 r)
maximumOn LPState 2 r
s Line 2 r
l = do [HalfLine 2 r]
hls <- Line 2 r -> [HalfSpace 2 r] -> Maybe [HalfLine 2 r]
forall r.
(Ord r, Fractional r) =>
Line 2 r -> [HalfSpace 2 r] -> Maybe [HalfLine 2 r]
collectOn Line 2 r
l ([HalfSpace 2 r] -> Maybe [HalfLine 2 r])
-> [HalfSpace 2 r] -> Maybe [HalfLine 2 r]
forall a b. (a -> b) -> a -> b
$ LPState 2 r
sLPState 2 r
-> Getting [HalfSpace 2 r] (LPState 2 r) [HalfSpace 2 r]
-> [HalfSpace 2 r]
forall s a. s -> Getting a s a -> a
^.Getting [HalfSpace 2 r] (LPState 2 r) [HalfSpace 2 r]
forall (d :: Nat) r. Lens' (LPState d r) [HalfSpace d r]
seen
Vector 2 r -> Line 2 r -> [HalfLine 2 r] -> Maybe (Point 2 r)
forall r (d :: Nat).
(Ord r, Num r, Arity d) =>
Vector d r -> Line d r -> [HalfLine d r] -> Maybe (Point d r)
oneDLinearProgramming (LPState 2 r
sLPState 2 r
-> Getting (Vector 2 r) (LPState 2 r) (Vector 2 r) -> Vector 2 r
forall s a. s -> Getting a s a -> a
^.Getting (Vector 2 r) (LPState 2 r) (Vector 2 r)
forall (d :: Nat) r. Lens' (LPState d r) (Vector d r)
obj) Line 2 r
l [HalfLine 2 r]
hls
initialize :: forall r. (Ord r, Fractional r)
=> LinearProgram 2 r -> (LPState 2 r, [HalfSpace 2 r])
initialize :: LinearProgram 2 r -> (LPState 2 r, [HalfSpace 2 r])
initialize (LinearProgram Vector 2 r
c (HalfSpace 2 r
m1:HalfSpace 2 r
m2:[HalfSpace 2 r]
hs)) = (Vector 2 r -> [HalfSpace 2 r] -> Point 2 r -> LPState 2 r
forall (d :: Nat) r.
Vector d r -> [HalfSpace d r] -> Point d r -> LPState d r
LPState Vector 2 r
c [HalfSpace 2 r
m1,HalfSpace 2 r
m2] Point 2 r
p, [HalfSpace 2 r]
hs)
where
Just Point 2 r
p = forall (ts :: [*]).
NatToInt (RIndex (Point 2 r) ts) =>
CoRec Identity ts -> Maybe (Point 2 r)
forall t (ts :: [*]).
NatToInt (RIndex t ts) =>
CoRec Identity ts -> Maybe t
asA @(Point 2 r)
(CoRec Identity '[NoIntersection, Point 2 r, Line 2 r]
-> Maybe (Point 2 r))
-> CoRec Identity '[NoIntersection, Point 2 r, Line 2 r]
-> Maybe (Point 2 r)
forall a b. (a -> b) -> a -> b
$ (HalfSpace 2 r
m1HalfSpace 2 r
-> Getting (Line 2 r) (HalfSpace 2 r) (Line 2 r) -> Line 2 r
forall s a. s -> Getting a s a -> a
^.(HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r))
-> HalfSpace 2 r -> Const (Line 2 r) (HalfSpace 2 r)
forall (d :: Nat) r (d2 :: Nat) r2.
Iso
(HalfSpace d r)
(HalfSpace d2 r2)
(HyperPlane d r)
(HyperPlane d2 r2)
boundingPlane((HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r))
-> HalfSpace 2 r -> Const (Line 2 r) (HalfSpace 2 r))
-> ((Line 2 r -> Const (Line 2 r) (Line 2 r))
-> HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r))
-> Getting (Line 2 r) (HalfSpace 2 r) (Line 2 r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Line 2 r -> Const (Line 2 r) (Line 2 r))
-> HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r)
forall r. Num r => Iso' (HyperPlane 2 r) (Line 2 r)
_asLine) Line 2 r -> Line 2 r -> Intersection (Line 2 r) (Line 2 r)
forall g h. IsIntersectableWith g h => g -> h -> Intersection g h
`intersect` (HalfSpace 2 r
m2HalfSpace 2 r
-> Getting (Line 2 r) (HalfSpace 2 r) (Line 2 r) -> Line 2 r
forall s a. s -> Getting a s a -> a
^.(HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r))
-> HalfSpace 2 r -> Const (Line 2 r) (HalfSpace 2 r)
forall (d :: Nat) r (d2 :: Nat) r2.
Iso
(HalfSpace d r)
(HalfSpace d2 r2)
(HyperPlane d r)
(HyperPlane d2 r2)
boundingPlane((HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r))
-> HalfSpace 2 r -> Const (Line 2 r) (HalfSpace 2 r))
-> ((Line 2 r -> Const (Line 2 r) (Line 2 r))
-> HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r))
-> Getting (Line 2 r) (HalfSpace 2 r) (Line 2 r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Line 2 r -> Const (Line 2 r) (Line 2 r))
-> HyperPlane 2 r -> Const (Line 2 r) (HyperPlane 2 r)
forall r. Num r => Iso' (HyperPlane 2 r) (Line 2 r)
_asLine)
initialize LinearProgram 2 r
_ = String -> (LPState 2 r, [HalfSpace 2 r])
forall a. HasCallStack => String -> a
error
String
"Algorithms.Geometry.LinearProgramming.LP2DRIC.initialize requires \
\at least two constraints."
_findD :: (Ord r, Fractional r)
=> LinearProgram 2 r -> Maybe (Vector 2 r)
_findD :: LinearProgram 2 r -> Maybe (Vector 2 r)
_findD (LinearProgram Vector 2 r
c [HalfSpace 2 r]
hs) = do [HalfLine 2 r]
hls <- Line 2 r -> [HalfSpace 2 r] -> Maybe [HalfLine 2 r]
forall r.
(Ord r, Fractional r) =>
Line 2 r -> [HalfSpace 2 r] -> Maybe [HalfLine 2 r]
collectOn Line 2 r
nl [HalfSpace 2 r]
hs'
Vector 2 r
d <- Point 2 r -> Vector 2 r
forall (d :: Nat) r. Point d r -> Vector d r
toVec (Point 2 r -> Vector 2 r)
-> Maybe (Point 2 r) -> Maybe (Vector 2 r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Vector 2 r -> Line 2 r -> [HalfLine 2 r] -> Maybe (Point 2 r)
forall r (d :: Nat).
(Ord r, Num r, Arity d) =>
Vector d r -> Line d r -> [HalfLine d r] -> Maybe (Point d r)
oneDLinearProgramming Vector 2 r
v Line 2 r
nl [HalfLine 2 r]
hls
if Vector 2 r
c Vector 2 r -> Vector 2 r -> r
forall (f :: * -> *) a. (Metric f, Num a) => f a -> f a -> a
`dot` Vector 2 r
d r -> r -> Bool
forall a. Ord a => a -> a -> Bool
> r
0 then Vector 2 r -> Maybe (Vector 2 r)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector 2 r
d
else Maybe (Vector 2 r)
forall a. Maybe a
Nothing
where
nl :: Line 2 r
nl@(Line Point 2 r
_ Vector 2 r
v) = Line 2 r -> Line 2 r
forall r. Num r => Line 2 r -> Line 2 r
perpendicularTo (Point 2 r -> Vector 2 r -> Line 2 r
forall (d :: Nat) r. Point d r -> Vector d r -> Line d r
Line (Point 2 r
forall (d :: Nat) r. (Arity d, Num r) => Point d r
origin Point 2 r -> Diff (Point 2) r -> Point 2 r
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ Diff (Point 2) r
Vector 2 r
c) Vector 2 r
c)
hs' :: [HalfSpace 2 r]
hs' = (HalfSpace 2 r -> HalfSpace 2 r)
-> [HalfSpace 2 r] -> [HalfSpace 2 r]
forall a b. (a -> b) -> [a] -> [b]
map HalfSpace 2 r -> HalfSpace 2 r
forall (d1 :: Nat) r1 a. HalfSpace d1 r1 -> a
toHL [HalfSpace 2 r]
hs
toHL :: HalfSpace d1 r1 -> a
toHL HalfSpace d1 r1
h = let _n :: Vector d1 r1
_n = HalfSpace d1 r1
hHalfSpace d1 r1
-> Getting (Vector d1 r1) (HalfSpace d1 r1) (Vector d1 r1)
-> Vector d1 r1
forall s a. s -> Getting a s a -> a
^.(HyperPlane d1 r1 -> Const (Vector d1 r1) (HyperPlane d1 r1))
-> HalfSpace d1 r1 -> Const (Vector d1 r1) (HalfSpace d1 r1)
forall (d :: Nat) r (d2 :: Nat) r2.
Iso
(HalfSpace d r)
(HalfSpace d2 r2)
(HyperPlane d r)
(HyperPlane d2 r2)
boundingPlane((HyperPlane d1 r1 -> Const (Vector d1 r1) (HyperPlane d1 r1))
-> HalfSpace d1 r1 -> Const (Vector d1 r1) (HalfSpace d1 r1))
-> ((Vector d1 r1 -> Const (Vector d1 r1) (Vector d1 r1))
-> HyperPlane d1 r1 -> Const (Vector d1 r1) (HyperPlane d1 r1))
-> Getting (Vector d1 r1) (HalfSpace d1 r1) (Vector d1 r1)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Vector d1 r1 -> Const (Vector d1 r1) (Vector d1 r1))
-> HyperPlane d1 r1 -> Const (Vector d1 r1) (HyperPlane d1 r1)
forall (d :: Nat) r. Lens' (HyperPlane d r) (Vector d r)
normalVec
in a
forall a. HasCallStack => a
undefined
_findUnBoundedHalfLine :: LinearProgram 2 r -> Either (Two (HalfSpace 2 r)) (HalfLine 2 r)
_findUnBoundedHalfLine :: LinearProgram 2 r -> Either (Two (HalfSpace 2 r)) (HalfLine 2 r)
_findUnBoundedHalfLine = LinearProgram 2 r -> Either (Two (HalfSpace 2 r)) (HalfLine 2 r)
forall a. HasCallStack => a
undefined