X-Git-Url: https://svn.cri.ensmp.fr/git/linpy.git/blobdiff_plain/45951a9f688b1a3dda02979cacb93747b1422709..2c669de47805cf0b8bd1c8bcf3958f52b1902926:/pypol/polyhedra.py diff --git a/pypol/polyhedra.py b/pypol/polyhedra.py index 3db8f40..37f16e0 100644 --- a/pypol/polyhedra.py +++ b/pypol/polyhedra.py @@ -1,4 +1,3 @@ - import functools import math import numbers @@ -6,6 +5,7 @@ import numbers from . import islhelper from .islhelper import mainctx, libisl +from .geometry import GeometricObject, Point from .linexprs import Expression, Symbol, Rational from .domains import Domain @@ -32,11 +32,7 @@ class Polyhedron(Domain): if inequalities is not None: raise TypeError('too many arguments') return cls.fromstring(equalities) - elif isinstance(equalities, Polyhedron): - if inequalities is not None: - raise TypeError('too many arguments') - return equalities - elif isinstance(equalities, Domain): + elif isinstance(equalities, GeometricObject): if inequalities is not None: raise TypeError('too many arguments') return equalities.aspolyhedron() @@ -87,6 +83,19 @@ class Polyhedron(Domain): def aspolyhedron(self): return self + def __contains__(self, point): + if not isinstance(point, Point): + raise TypeError('point must be a Point instance') + if self.symbols != point.symbols: + raise ValueError('arguments must belong to the same space') + for equality in self.equalities: + if equality.subs(point.coordinates()) != 0: + return False + for inequality in self.inequalities: + if inequality.subs(point.coordinates()) < 0: + return False + return True + def subs(self, symbol, expression=None): equalities = [equality.subs(symbol, expression) for equality in self.equalities] @@ -233,6 +242,17 @@ class Polyhedron(Domain): angles[m] = angle return sorted(points, key=angles.get) + def faces(self): + vertices = self.vertices() + faces = [] + for constraint in self.constraints: + face = [] + for vertex in vertices: + if constraint.subs(vertex.coordinates()) == 0: + face.append(vertex) + faces.append(face) + return faces + def plot(self): import matplotlib.pyplot as plt from matplotlib.path import Path