Ticket #13517: trac_#13517_new_class_initial_version.patch

File trac_#13517_new_class_initial_version.patch, 11.8 KB (added by moritz, 9 years ago)

first working version

  • sage/geometry/all.py

    # HG changeset patch
    # User Moritz Firsching <moritz@math.fu-berlin.de>
    # Date 1350389840 -7200
    # Node ID 2125ef2462f393d34841d63e2703348639f52758
    # Parent  d06cf4b2215d37d3a87a58f65ac53234502dd471
    Trac #13517: creating a new class and basic functionality
    
    diff --git a/sage/geometry/all.py b/sage/geometry/all.py
    a b  
    1717
    1818
    1919import toric_plotter
     20
     21from voronoi_diagram import voronoi_diagram
     22 No newline at end of file
  • new file sage/geometry/voronoi_diagram.py

    diff --git a/sage/geometry/voronoi_diagram.py b/sage/geometry/voronoi_diagram.py
    new file mode 100644
    - +  
     1r"""
     2Voronoi diagram
     3
     4This module provides the class :class: `voronoi_diagram` for computing the
     5Voronoi diagram of a finite list of points in \RR^d.
     6"""
     7
     8#*****************************************************************************
     9#       Copyright (C) 2012 Moritz Firsching <moritz@math.fu-berlin.de>
     10#
     11#  Distributed under the terms of the GNU General Public License (GPL)
     12#
     13#                  http://www.gnu.org/licenses/
     14#*****************************************************************************
     15
     16from sage.structure.sage_object import SageObject
     17from sage.geometry.polyhedron.constructor import Polyhedron
     18from sage.all import RDF
     19from sage.geometry.triangulation.point_configuration import PointConfiguration
     20from sage.modules.all import vector
     21from sage.plot.all import line, point, rainbow, plot
     22
     23
     24class voronoi_diagram(SageObject):
     25    r"""
     26    base class for the  Voronoi diagram.
     27    Computes the Voronoi diagram of a list of points
     28
     29    INPUT:
     30
     31    - ``points`` a list of points. Any valid input for the
     32    :class:`PointConfiguration` will do.
     33
     34    EXAMPLES:
     35        Get the Voronoi diagram for some points in \RR^3
     36        ::
     37        sage: V=voronoi_diagram([[1,3,.3],[2,-2,1],[-1,2,-.1]]); V
     38        The Voronoi diagram of 3 points.
     39
     40    ALGORITHM:
     41
     42    We use hyperplanes tangent to the paraboloid one dimension higher to
     43    get a convex polyhedron and then project back to one dimension lower
     44
     45    See for example [M2002]
     46
     47    REFERENCES:
     48
     49    ..  [M2002]
     50        Jiri Matousek,
     51        "Lectures on Discrete Geometry", Springer, Ch.5.7, p.118.
     52
     53    AUTHORS:
     54
     55    - Moritz Firsching (2012-09-21)
     56    """
     57    def __init__(self, points):
     58        r"""
     59        Initialize a :class:`voronoi_diagram` object.
     60
     61        EXAMPLES::
     62
     63            sage: V=voronoi_diagram([[1,3,.3],[2,-2,1],[-1,2,-.1]]); V
     64            The Voronoi diagram of 3 points.
     65            sage: voronoi_diagram([])
     66            The empty Voronoi diagram.
     67        """
     68        self._P=[]
     69        self._points=PointConfiguration(points)
     70        self._n=self._points.n_points()
     71        if self._n:
     72            self._d=self._points.ambient_dim()
     73            e=[([sum(vector(i)[k]**2 for k in
     74            range(self._d))]+[(-2)*vector(i)[l] for l in range(self._d)]+[1])
     75            for i in self._points]
     76            e=[[RDF(i) for i in k] for k in e]
     77            p=Polyhedron(ieqs = e, base_ring=RDF)
     78        for i in range(self._n):
     79            equ=p.Hrepresentation(i)
     80            pvert=[[u[k] for k in range(self._d)] for u in equ.incident() if
     81            u.is_vertex()]
     82            prays=[[u[k] for k in range(self._d)] for u in equ.incident() if
     83            u.is_ray()]
     84            pline=[[u[k] for k in range(self._d)] for u in equ.incident() if
     85            u.is_line()]
     86            (self._P).append(Polyhedron(vertices=pvert, lines=pline, rays=prays,
     87             base_ring=RDF))
     88
     89    def points(self):
     90        r""" Returns the input points (as a PointConfiguration).
     91
     92        EXAMPLES::
     93
     94            sage: V=voronoi_diagram([[.5,3],[2,5],[4,5],[4,-1]]); V.points()
     95            A point configuration in QQ^2 consisting of 4 points. The
     96            triangulations of this point configuration are assumed to be
     97            connected, not necessarily
     98            fine, not necessarily regular.
     99        """
     100        return self._points
     101
     102    def ambient_dim(self):
     103        r"""
     104        Returns the ambient dimension of the points.
     105
     106        EXAMPLES::
     107            sage: V=voronoi_diagram([[.5,3],[2,5],[4,5],[4,-1]])
     108            sage: V.ambient_dim()
     109            2
     110            sage: V=voronoi_diagram([[1,2,3,4,5,6]]); V.ambient_dim()
     111            6
     112        """
     113        return self._d
     114
     115    def regions(self):
     116        r"""
     117        Returns the Voronoi regions of the Voronoi diagram as a list of
     118        polyhedra.
     119
     120        EXAMPLES::
     121            sage: V=voronoi_diagram([[1,3,.3],[2,-2,1],[-1,2,-.1]]); V.regions()
     122            [A 3-dimensional polyhedron in RDF^3 defined as the convex hull of 1
     123             vertex, 2 rays, 1 line, A 3-dimensional polyhedron in RDF^3 defined
     124              as the
     125            convex hull of 1 vertex, 2 rays, 1 line, A 3-dimensional polyhedron
     126            in RDF^3 defined
     127            as the convex hull of 1 vertex, 2 rays, 1 line]
     128        """
     129        return self._P
     130
     131    def base_ring(self):
     132        r"""
     133        Returns the base_ring of the regions of the Voronoi diagram.
     134
     135        EXAMPLES::
     136            sage: V=voronoi_diagram([[1,3,1],[2,-2,1],[-1,2,1/2]])
     137            sage: V.base_ring()
     138            Real Double Field
     139
     140        .. TODO:: After support for rational Voronoi diagram has been added
     141        modify this funtion
     142        """
     143        return RDF.base_ring()
     144
     145    def _repr_(self):
     146        r"""
     147        Return a description of the Voronoi diagram.
     148
     149        EXAMPLES::
     150            sage: V=voronoi_diagram([[1,3,.3],[2,-2,1],[-1,2,-.1]]); V
     151            The Voronoi diagram of 3 points.
     152            sage: voronoi_diagram([])
     153            The empty Voronoi diagram.
     154
     155        """
     156        desc = ''
     157        if self._n:
     158            desc+= 'The Voronoi diagram of '
     159            desc+=str(self._n)
     160            desc+= ' points.'
     161        else:
     162            desc+='The empty Voronoi diagram.'
     163
     164        return desc
     165
     166
     167    def plot(self, **kwds):
     168        """
     169        Return a graphical representation for 2-dimensional Voronoi diagrams.
     170
     171        INPUT:
     172
     173        - ``**kwds`` -- optional keyword parameters, passed on as arguments for
     174        plot().
     175
     176        OUTPUT:
     177
     178        A graphics object.
     179
     180        EXAMPLES::
     181            sage: P=[[0.671, 0.650],[0.258, 0.767], [0.562, 0.406],\
     182            [0.254, 0.709], [0.493, 0.879]]
     183
     184            sage: V=voronoi_diagram(P); S=V.plot()
     185
     186            sage: show(S,xmin=0,xmax=1,ymin=0,ymax=1, aspect_ratio=1,\
     187            axes=false)
     188
     189        Trying to plot a Voronoi diagram of dimension other than 2 gives an
     190        error::
     191            sage: voronoi_diagram([[1,2,3],[6,5,4]]).plot()
     192            Traceback (most recent call last):
     193            ...
     194            NotImplementedError: Plotting of 3-dimensional Voronoi diagrams not
     195                implemented
     196
     197        """
     198        if self.ambient_dim()==2:
     199            S=line([])
     200            for i,j in enumerate(self._points):
     201                S+=(self._P[i]).render_solid(color=rainbow(self._n)[i],
     202                zorder=1)
     203                S+=point(j, color=rainbow(self._n)[i], pointsize=10,zorder=3)
     204                S+=point(vector(j), color='black',pointsize=20,zorder=2)
     205            return plot(S,**kwds)
     206        raise NotImplementedError('Plotting of '+str(self.ambient_dim())+
     207                                  '-dimensional Voronoi diagrams not'+
     208                                  ' implemented')
     209 No newline at end of file
  • new file sage/geometry/voronoi_diagram.py.out

    diff --git a/sage/geometry/voronoi_diagram.py.out b/sage/geometry/voronoi_diagram.py.out
    new file mode 100644
    - +  
     1r"""
     2Voronoi diagram
     3
     4This module provides the class :class: `voronoi_diagram` for computing the
     5Voronoi diagram of a finite list of points in \RR^d.
     6"""
     7
     8#*****************************************************************************
     9#       Copyright (C) 2012 Moritz Firsching <moritz@math.fu-berlin.de>
     10#
     11#  Distributed under the terms of the GNU General Public License (GPL)
     12#
     13#                  http://www.gnu.org/licenses/
     14#*****************************************************************************
     15
     16from sage.structure.sage_object import SageObject
     17from sage.geometry.polyhedron.constructor import Polyhedron
     18from sage.all import RDF
     19from sage.geometry.triangulation.point_configuration import PointConfiguration
     20from sage.modules.all import vector
     21
     22class voronoi_diagram(SageObject):
     23    r"""
     24    base class for the  Voronoi diagram.
     25    Computes the Voronoi diagram of a list of points
     26
     27    INPUT:
     28
     29    - ``points`` a list of points. Any valid input for the :class:`PointConfiguration` will do.
     30
     31    EXAMPLES:
     32        Get the Voronoi diagram for some points in \RR^3
     33        ::
     34        sage: V=voronoi_diagram([[1,3,.3],[2,-2,1],[-1,2,-.1]]); V
     35        The Voronoi diagram of 3 points.
     36
     37    ALGORITHM:
     38
     39    We use hyperplanes tangent to the paraboloid one dimension higher to
     40    get a convex polyhedron and then project back to one dimension lower
     41
     42    See for example [M2002]
     43
     44    REFERENCES:
     45
     46    ..  [M2002]
     47        Jiri Matousek,
     48        "Lectures on Discrete Geometry", Springer, Ch.5.7, p.118.
     49
     50    AUTHORS:
     51
     52    - Moritz Firsching (2012-09-21)
     53    """
     54    def __init__(self, points):
     55        r"""
     56        Initialize a :class:`voronoi_diagram` object.
     57
     58        EXAMPLES::
     59
     60            sage: V=voronoi_diagram([[1,3,.3],[2,-2,1],[-1,2,-.1]]); V
     61            The Voronoi diagram of 3 points.
     62            sage: voronoi_diagram([])
     63            The empty Voronoi diagram.
     64        """
     65        self._P=[]
     66        self.points=PointConfiguration(points)
     67        self._n=self.points.n_points()
     68        if self._n:
     69            d=self.points.ambient_dim()
     70            e=[([sum(vector(i)[k]**2 for k in range(d))]+[(-2)*vector(i)[l] for l in range(d)]+[1]) for i in self.points]
     71            e=[[RDF(i) for i in k] for k in e]
     72            p=Polyhedron(ieqs = e, base_ring=RDF)
     73        for i in range(self._n):
     74            equ=p.Hrepresentation(i)
     75            pvert=[[u[k] for k in range(d)] for u in equ.incident() if u.is_vertex()]
     76            prays=[[u[k] for k in range(d)] for u in equ.incident() if u.is_ray()]
     77            pline=[[u[k] for k in range(d)] for u in equ.incident() if u.is_line()]
     78            (self._P).append(Polyhedron(vertices=pvert, lines=pline, rays=prays, base_ring=RDF))
     79
     80    def regions(self):
     81        r"""
     82        Returns the Voronoi regions of the Voronoi diagram as a list of polyhedra.
     83
     84        EXAMPLES::
     85            sage: V=voronoi_diagram([[1,3,.3],[2,-2,1],[-1,2,-.1]]); V.regions()
     86            [A 3-dimensional polyhedron in RDF^3 defined as the convex hull of 1 vertex, 2 rays, 1 line, A 3-dimensional polyhedron in RDF^3 defined as the convex hull of 1 vertex, 2 rays, 1 line, A 3-dimensional polyhedron in RDF^3 defined as the convex hull of 1 vertex, 2 rays, 1 line]
     87        """
     88        return self._P
     89
     90    def base_ring(self):
     91        r"""
     92        Returns the base_ring of the regions of the Voronoi diagram.
     93
     94        EXAMPLES::
     95            sage: V=voronoi_diagram([[1,3,1],[2,-2,1],[-1,2,1/2]]); V.base_ring()
     96            Real Double Field
     97
     98        .. TODO:: After support for rational Voronoi diagram has been added modify this funtion
     99        """
     100        return RDF.base_ring()
     101
     102    def _repr_(self):
     103        r"""
     104        Return a description of the Voronoi diagram.
     105
     106        EXAMPLES::
     107            sage: V=voronoi_diagram([[1,3,.3],[2,-2,1],[-1,2,-.1]]); V
     108            The Voronoi diagram of 3 points.
     109            sage: voronoi_diagram([])
     110            The empty Voronoi diagram.
     111
     112        """
     113        desc = ''
     114        if self._n:
     115            desc+= 'The Voronoi diagram of '
     116            desc+=str(self._n)
     117            desc+= ' points.'
     118        else:
     119            desc+='The empty Voronoi diagram.'
     120
     121        return desc