politopix  4.1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Voronoi_Rn.h
Go to the documentation of this file.
1 // politopix allows to make computations on polytopes such as finding vertices, intersecting, Minkowski sums, ...
2 // Copyright (C) 2011-2017 : Delos Vincent
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
19 // I2M (UMR CNRS 5295 / University of Bordeaux)
20 
21 #ifndef VORONOI_Rn
22 #define VORONOI_Rn
23 
24 #include <boost/shared_ptr.hpp>
25 #include <stdexcept>
26 #include <exception>
27 #include <iostream>
28 #include <utility>
29 #include <vector>
30 #include "Rn.h"
31 #include "Generator_Rn.h"
32 #include "HalfSpace_Rn.h"
33 #include "Polytope_Rn.h"
34 
35 
39 class Voronoi_Rn {
40 
41  public:
42 
46  CellByCell = 1,
55 
57  //Voronoi_Rn() { _inputSpace=0; }
58 
62  Voronoi_Rn(const boost::shared_ptr<Polytope_Rn>& inputSpace, const std::vector<Point_Rn>& listOfPoints);
63 
65  virtual ~Voronoi_Rn() {}
66 
68  virtual bool compute() throw (std::length_error)=0;
69 
70  //virtual void updateFacetNeighbours(const std::vector< boost::shared_ptr<HalfSpace_Rn> >& allHalfSpaces,
71  //boost::shared_ptr<Polytope_Rn> newVoronoiCell, unsigned int counter,
72  //unsigned int newTruncationStep)=0;
73 
74 
76  boost::shared_ptr<HalfSpace_Rn> computeMidPlane(std::vector<Point_Rn>::const_iterator seed1, std::vector<Point_Rn>::const_iterator seed2);
77 
78  const std::vector< boost::shared_ptr<Polytope_Rn> >& getVoronoiCells() const {return _listOfVoronoiCells;}
79  std::vector< boost::shared_ptr<Polytope_Rn> > getVoronoiCells() {return _listOfVoronoiCells;}
80 
81  const std::vector< Point_Rn >& getListOfSeeds() const {return _listOfSeeds;}
82 
83  // CHECK POLYHEDRON
84  bool checkTopologyAndGeometry() const throw (std::domain_error);
85 
87  void dump(std::ostream& out) const;
88 
89  void gnuplot(std::ostream& out) const throw (std::domain_error);
90 
91  protected:
93  const boost::shared_ptr<Polytope_Rn>& _inputSpace;
95  const std::vector< Point_Rn >& _listOfSeeds;
97  std::vector< boost::shared_ptr<Polytope_Rn> > _listOfVoronoiCells;
98 };
99 
103 
104  public:
106  //IncrementalVoronoi() { _inputSpace=0; }
107 
111  IncrementalVoronoi(const boost::shared_ptr<Polytope_Rn>& is, const std::vector<Point_Rn>& lop):Voronoi_Rn(is,lop) {}
112 
114  virtual ~IncrementalVoronoi() {}
115 
117  virtual bool compute() throw (std::length_error);
118 
119  //virtual void updateFacetNeighbours(const std::vector< boost::shared_ptr<HalfSpace_Rn> >& allHalfSpaces,
120  //boost::shared_ptr<Polytope_Rn> newVoronoiCell, unsigned int counter,
121  //unsigned int newTruncationStep) {}
122 
123 };
124 
127 class CellByCellVoronoi : public Voronoi_Rn {
128 
129  public:
131  //CellByCellVoronoi() { _inputSpace=0; }
132 
136  CellByCellVoronoi(const boost::shared_ptr<Polytope_Rn>& is, const std::vector<Point_Rn>& lop):Voronoi_Rn(is,lop) {}
137 
139  virtual ~CellByCellVoronoi() {}
140 
142  virtual bool compute() throw (std::length_error);
143 
144  //virtual void updateFacetNeighbours(const std::vector< boost::shared_ptr<HalfSpace_Rn> >& allHalfSpaces,
145  //boost::shared_ptr<Polytope_Rn> newVoronoiCell, unsigned int counter,
146  //unsigned int newTruncationStep) {}
147 
148 };
149 
150 // \class CellByCellVoronoiNgb
151 // \brief Computes the Voronoi diagram cell by cell and the neighbourhood relationships between them.
152 //class CellByCellVoronoiNgb : public CellByCellVoronoi {
153 
154 // public:
156  //CellByCellVoronoiNgb() { _inputSpace=0; }
157 
161  //CellByCellVoronoiNgb(const boost::shared_ptr<Polytope_Rn>& is, const std::vector<Point_Rn>& lop):CellByCellVoronoi(is,lop) {}
162 
164  //virtual ~CellByCellVoronoiNgb() {}
165 
167  //virtual bool compute() throw (std::length_error);
168 
170  //virtual void updateFacetNeighbours(
171  //const std::vector< boost::shared_ptr<HalfSpace_Rn> >& allHalfSpaces,
172  //boost::shared_ptr<Polytope_Rn> newVoronoiCell, unsigned int counter,
173  //unsigned int newTruncationStep);
174 
175  //protected:
179  //std::vector< std::vector< std::pair<unsigned int,unsigned int> > > _facetNeighbours;
180 
181 //};
182 
186 
187  public:
189  //CellByCellVoronoiDist() { _inputSpace=0; }
190 
194  CellByCellVoronoiDist(const boost::shared_ptr<Polytope_Rn>& is, const std::vector<Point_Rn>& lop):CellByCellVoronoi(is,lop) {
195  _percentageOfSortedHalfSpaces = 100.;
196  }
197 
202  CellByCellVoronoiDist(const boost::shared_ptr<Polytope_Rn>& is, const std::vector<Point_Rn>& lop, double percent):CellByCellVoronoi(is,lop) {
203  _percentageOfSortedHalfSpaces = percent;
204  }
205 
208 
210  virtual bool compute() throw (std::length_error);
211 
213  void processDistances(
214  std::vector< std::pair< double, unsigned int > >& allDistances,
215  std::vector<Point_Rn>::const_iterator newVoronoiSeed,
216  boost::shared_ptr<Polytope_Rn> newVoronoiCell);
217 
219  void setAverageNumberOfNeighbours(double pc) { _percentageOfSortedHalfSpaces = pc; }
220 
221  protected:
225  std::vector< boost::shared_ptr<HalfSpace_Rn> > _allHalfSpaces;
226 };
227 
231 
232  public:
234  //CellByCellVoronoiDistNgb() { _inputSpace=0; }
235 
239  CellByCellVoronoiDistNgb(const boost::shared_ptr<Polytope_Rn>& is, const std::vector<Point_Rn>& lop):CellByCellVoronoiDist(is,lop) {}
240 
243 
245  virtual bool compute() throw (std::length_error);
246 
248  virtual bool compute(
249  std::vector< Point_Rn >::const_iterator iteBeg,
250  std::vector< Point_Rn >::const_iterator iteEnd) throw (std::length_error);
251 
252  const std::vector< std::vector< std::pair<boost::shared_ptr<HalfSpace_Rn>, unsigned int> > >&
253  getFacetNeighbours() const { return _facetNeighbours;}
254 
255  virtual void allocateFacetNeighbours() { _facetNeighbours.resize(_listOfSeeds.size()); }
256  void dumpFacetNeighbours(std::ostream &this_ostream) const;
257 
258  virtual void updateFacetNeighbours(
259  const std::vector< std::pair< double, unsigned int > >& allDistances,
260  boost::shared_ptr<Polytope_Rn> newVoronoiCell,
261  unsigned int counter, unsigned int newTruncationStep);
262 
263  protected:
265  std::vector< std::vector< std::pair<boost::shared_ptr<HalfSpace_Rn>, unsigned int> > > _facetNeighbours;
266 
267 };
268 
272 
273  public:
275 
281  const std::vector< Point_Rn >& listOfSeeds,
282  const std::vector< boost::shared_ptr<Polytope_Rn> >& listOfCells,
283  const std::vector<int>& listOfProp) {
284  _listOfSeeds = listOfSeeds;
285  _listOfVoronoiCells = listOfCells;
286  _listOfSeedProperties = listOfProp;
287  allocateFacetNeighbours();
288  }
289 
293  VoronoiWithProperties(const CellByCellVoronoiDistNgb& CBCVDN, const std::vector<int>& listOfProp) {
294  _listOfSeeds = CBCVDN.getListOfSeeds();
295  _facetNeighbours = CBCVDN.getFacetNeighbours();
296  _listOfVoronoiCells = CBCVDN.getVoronoiCells();
297  _listOfSeedProperties = listOfProp;
298  }
299 
302 
306  static void loadProp(const std::string& filename, std::vector<int>& propList) throw (std::ios_base::failure);
307 
308  void loadCell(const std::string& filename) throw (std::ios_base::failure);
309 
310  void loadSeeds(const std::string& filename) throw (std::ios_base::failure);
311 
312  void loadSeeds(const std::vector<Point_Rn>& listOfPoints3D) {_listOfSeeds = listOfPoints3D;}
313 
314  void loadProperties(const std::string& filename) throw (std::ios_base::failure);
315 
316  void loadFacetNeighbours(const std::string& filename) throw (std::length_error,std::ios_base::failure);
317 
318  //void dumpFacetNeighbours(std::ostream &this_ostream);
319 
320  void allocateFacetNeighbours() { _facetNeighbours.resize(_listOfSeeds.size()); }
321 
322  void dumpNeighbours(std::ostream &this_ostream) const;
323  void dumpFacetNeighbours(std::ostream &this_ostream) const;
324 
325  void addHalfSpaceAndNeighbour(unsigned int cellCounter, boost::shared_ptr<HalfSpace_Rn> hs, unsigned int ngbCell) {
326  std::pair< boost::shared_ptr<HalfSpace_Rn>, unsigned int > curHalfspace_ngbPolytope(hs, ngbCell);
327  _facetNeighbours[cellCounter].push_back(curHalfspace_ngbPolytope);
328  }
329 
330  // Unite different Voronoi cells or polytopes which share at least one common facet.
332  //virtual bool compute() throw (std::length_error);
333 
335  bool fuseCellsWithSameProperty(std::vector< std::vector< unsigned int > >& newListOfNonConvexPolytopes) throw (std::length_error);
336 
338  bool fuseCellsWithSameProperty(std::vector< std::vector< boost::shared_ptr<Polytope_Rn> > >& newListOfNonConvexPolytopes) throw (std::length_error);
339 
340  protected:
342  std::vector< Point_Rn > _listOfSeeds;
344  std::vector< boost::shared_ptr<Polytope_Rn> > _listOfVoronoiCells;
346  std::vector< std::vector< std::pair<boost::shared_ptr<HalfSpace_Rn>, unsigned int> > > _facetNeighbours;
348  std::vector<int> _listOfSeedProperties;
349 
350 };
351 
352 #endif // VORONOI_Rn
CellByCellVoronoiDistNgb(const boost::shared_ptr< Polytope_Rn > &is, const std::vector< Point_Rn > &lop)
Constructor.
Definition: Voronoi_Rn.h:239
IncrementalVoronoi(const boost::shared_ptr< Polytope_Rn > &is, const std::vector< Point_Rn > &lop)
Constructor.
Definition: Voronoi_Rn.h:111
virtual ~CellByCellVoronoi()
Destructor.
Definition: Voronoi_Rn.h:139
Creation of a n-coordinate geometric point designed to be shared by its neighbour faces...
Definition: Point_Rn.h:34
virtual ~Voronoi_Rn()
Destructor.
Definition: Voronoi_Rn.h:65
Computes the Voronoi diagram cell by cell.
Definition: Voronoi_Rn.h:127
const std::vector< boost::shared_ptr< Polytope_Rn > > & getVoronoiCells() const
Definition: Voronoi_Rn.h:78
double _percentageOfSortedHalfSpaces
The amount of half-spaces we want to sort.
Definition: Voronoi_Rn.h:223
void dump(std::ostream &out) const
Dump the cell structure on the given output.
Definition: Voronoi_Rn.cpp:62
Constructor.
Definition: Voronoi_Rn.h:185
Model a polytope using its two equivalent definitions : the convex hull and the half-space intersecti...
Definition: Polytope_Rn.h:34
boost::shared_ptr< HalfSpace_Rn > computeMidPlane(std::vector< Point_Rn >::const_iterator seed1, std::vector< Point_Rn >::const_iterator seed2)
Compute the half-space containing seed1, in between seed1 and seed2, according to the growing seed pr...
Definition: Voronoi_Rn.cpp:40
virtual ~CellByCellVoronoiDistNgb()
Destructor.
Definition: Voronoi_Rn.h:242
VoronoiWithProperties(const std::vector< Point_Rn > &listOfSeeds, const std::vector< boost::shared_ptr< Polytope_Rn > > &listOfCells, const std::vector< int > &listOfProp)
Definition: Voronoi_Rn.h:280
A half-space whose frontier is a linear (n-1) dimension space. _constant + _coefficients[0].x1 + ... + _coefficients[n-1].xn >= 0.
Definition: HalfSpace_Rn.h:37
Refers to the class CellByCellDistNgb (100% of the distances between seeds to be sorted) ...
Definition: Voronoi_Rn.h:49
const std::vector< Point_Rn > & _listOfSeeds
The list of input points.
Definition: Voronoi_Rn.h:95
Do the same as CellByCellVoronoiDist with the neighbourhood computation.
Definition: Voronoi_Rn.h:230
const std::vector< Point_Rn > & getListOfSeeds() const
Definition: Voronoi_Rn.h:81
void gnuplot(std::ostream &out) const
Definition: Voronoi_Rn.cpp:82
CellByCellVoronoiDist(const boost::shared_ptr< Polytope_Rn > &is, const std::vector< Point_Rn > &lop)
Constructor.
Definition: Voronoi_Rn.h:194
Fuse Voronoi cells when they share the same property and then get non convex polytopes.
Definition: Voronoi_Rn.h:271
VoronoiWithProperties(const CellByCellVoronoiDistNgb &CBCVDN, const std::vector< int > &listOfProp)
Definition: Voronoi_Rn.h:293
CellByCellVoronoiDist(const boost::shared_ptr< Polytope_Rn > &is, const std::vector< Point_Rn > &lop, double percent)
Definition: Voronoi_Rn.h:202
Refers to the class CellByCellVoronoi.
Definition: Voronoi_Rn.h:47
std::vector< boost::shared_ptr< Polytope_Rn > > _listOfVoronoiCells
The list of polytopes partitioning the whole space.
Definition: Voronoi_Rn.h:97
Refers to the class CellByCellDistNgb ( 1% of the distances between seeds to be sorted) ...
Definition: Voronoi_Rn.h:50
void loadSeeds(const std::vector< Point_Rn > &listOfPoints3D)
Definition: Voronoi_Rn.h:312
Voronoi_Rn(const boost::shared_ptr< Polytope_Rn > &inputSpace, const std::vector< Point_Rn > &listOfPoints)
Refers to the class WithProperties.
Definition: Voronoi_Rn.cpp:35
void allocateFacetNeighbours()
Definition: Voronoi_Rn.h:320
bool checkTopologyAndGeometry() const
Definition: Voronoi_Rn.cpp:57
const boost::shared_ptr< Polytope_Rn > & _inputSpace
The original space to be divided.
Definition: Voronoi_Rn.h:93
Refers to the class CellByCellDistNgb ( 30% of the distances between seeds to be sorted) ...
Definition: Voronoi_Rn.h:53
virtual ~VoronoiWithProperties()
Destructor.
Definition: Voronoi_Rn.h:301
Refers to the class CellByCellVoronoiDist.
Definition: Voronoi_Rn.h:48
Refers to the class IncrementalVoronoi.
Definition: Voronoi_Rn.h:46
virtual bool compute()=0
Run the whole algorithm.
Compute a n-dimensional Voronoi diagram. It is a partitioning of a space into regions based on distan...
Definition: Voronoi_Rn.h:39
Refers to the class CellByCellDistNgb ( 40% of the distances between seeds to be sorted) ...
Definition: Voronoi_Rn.h:54
TypeOfAlgorithm
Choose the kind of algorithm to be run.
Definition: Voronoi_Rn.h:44
At the end of the ith iteration, the Voronoi diagram of i seeds is computed.
Definition: Voronoi_Rn.h:102
Refers to the class CellByCellDistNgb ( 10% of the distances between seeds to be sorted) ...
Definition: Voronoi_Rn.h:51
std::vector< boost::shared_ptr< Polytope_Rn > > getVoronoiCells()
Definition: Voronoi_Rn.h:79
std::vector< std::vector< std::pair< boost::shared_ptr< HalfSpace_Rn >, unsigned int > > > _facetNeighbours
For the current cell, store its neighbour numbers. _facetNeighbours[i] = { (H0, i0), (H1, i1), ... }.
Definition: Voronoi_Rn.h:265
const std::vector< std::vector< std::pair< boost::shared_ptr< HalfSpace_Rn >, unsigned int > > > & getFacetNeighbours() const
Definition: Voronoi_Rn.h:253
std::vector< boost::shared_ptr< HalfSpace_Rn > > _allHalfSpaces
For each seed, the list of the half-spaces used to build its cell.
Definition: Voronoi_Rn.h:225
CellByCellVoronoi(const boost::shared_ptr< Polytope_Rn > &is, const std::vector< Point_Rn > &lop)
Constructor.
Definition: Voronoi_Rn.h:136
Refers to the class CellByCellDistNgb ( 20% of the distances between seeds to be sorted) ...
Definition: Voronoi_Rn.h:52
virtual ~CellByCellVoronoiDist()
Destructor.
Definition: Voronoi_Rn.h:207
virtual void allocateFacetNeighbours()
Definition: Voronoi_Rn.h:255
void addHalfSpaceAndNeighbour(unsigned int cellCounter, boost::shared_ptr< HalfSpace_Rn > hs, unsigned int ngbCell)
Definition: Voronoi_Rn.h:325
virtual ~IncrementalVoronoi()
Destructor.
Definition: Voronoi_Rn.h:114