politopix  5.0.0
PolyhedralAlgorithms_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) 2015-2020 : Delos Vincent
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU 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 General Public License for more details.
13 //
14 // You should have received a copy of the GNU 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 INC_POLY_ALGO
22 #define INC_POLY_ALGO
23 
24 #include <boost/numeric/ublas/vector.hpp>
25 #include "polito_Export.h"
26 #include "Rn.h"
27 #include "Polytope_Rn.h"
28 #include "IO_Polytope.h"
29 #include "Generator_Rn.h"
30 #include "NormalFan_Rn.h"
31 #include "HalfSpace_Rn.h"
32 #include "PolyhedralCone_Rn.h"
33 #include "VolumeOfPolytopes_Rn.h"
34 #include "DoubleDescription_Rn.h"
37 
38 using namespace std;
39 
40 typedef std::vector< unsigned int > ListOfFaces;
41 
45 
46  public:
47 
49  FaceEnumeration(const boost::shared_ptr<Polytope_Rn>& A):_polytope(A) {}
50 
73  static void Compute(const boost::shared_ptr<Polytope_Rn>& A);
74 
75  static void Compute(const boost::shared_ptr<Polytope_Rn>& A, FaceEnumeration& FE);
76 
77  const std::vector< std::vector< ListOfFaces > >& getFacesWithVertices() {
78  if (_allFacesWithVertices.empty() == false)
79  return _allFacesWithVertices;
80  throw std::domain_error("FaceEnumeration::getFacesWithVertices() empty DS allFacesWithVertices.");
81  }
82 
83  const std::vector< std::vector< ListOfFaces > >& getFacesWithFacets() {
84  if (_allFacesWithFacets.empty() == false)
85  return _allFacesWithFacets;
86  throw std::domain_error("FaceEnumeration::getFacesWithFacets() empty DS allFacesWithFacets.");
87  }
88 
89  void clear() {_allFacesWithFacets.clear(); _allFacesWithVertices.clear();}
90 
91  void printFacesWithVerticesToSage(std::ostream &this_ostream) const;
92 
93  void printFacesWithVertices(std::ostream &this_ostream) const;
94 
95  void printFacesWithFacets(std::ostream &this_ostream) const;
96 
98  static void save(const std::string& filename, const std::vector< std::vector< ListOfFaces > >& latt);
99 
101  static void load(const std::string& filename, std::vector< std::vector< ListOfFaces > >& latt);
102 
109  static void load(std::istream& this_stream, std::vector< std::vector< ListOfFaces > >& latt);
110 
117  static void save(std::ostream& this_stream, const std::vector< std::vector< ListOfFaces > >& latt);
118 
119  void save(std::ostream& this_stream) const { save(this_stream, _allFacesWithVertices); }
120 
121 
122  protected:
123  static void ComputeWithFacets(const boost::shared_ptr<Polytope_Rn>& A, FaceEnumeration& FaceEnum);
124 
125  static void ComputeWithVertices(const boost::shared_ptr<Polytope_Rn>& A, FaceEnumeration& FaceEnum);
126 
127  std::vector< std::vector< ListOfFaces > > _allFacesWithFacets;
128 
129  std::vector< std::vector< ListOfFaces > > _allFacesWithVertices;
130 
131  const boost::shared_ptr<Polytope_Rn>& _polytope;
132 
133 };
134 
135 
139 
140  public:
141 
144  {}
145 
148  const boost::shared_ptr<Polytope_Rn>& A,
149  const boost::shared_ptr<Polytope_Rn>& B):_firstOperand(A),_secondOperand(B)
150  {}
151 
154  const boost::shared_ptr<Polytope_Rn>& A,
155  const boost::shared_ptr<Polytope_Rn>& B,
156  boost::shared_ptr<Polytope_Rn>& C):_firstOperand(A),_secondOperand(B),_sum(C)
157  { C = compute(); }
158 
160  MinkowskiSum(
161  const std::vector< boost::shared_ptr<Polytope_Rn> >& arrayOfPolytopes,
162  boost::shared_ptr<Polytope_Rn>& C);
163 
166  const boost::shared_ptr<Polytope_Rn>& A,
167  const boost::shared_ptr<Polytope_Rn>& B,
168  boost::shared_ptr<Polytope_Rn>& C,
169  const std::vector< std::vector<int> >& genitorsOfGeneratorsA,
170  const std::vector< std::vector<int> >& genitorsOfGeneratorsB,
171  std::vector< std::vector<int> >& traceGenerators):_firstOperand(A),_secondOperand(B),_sum(C)
172  {
173  C = compute();
174  traceGenerators.clear();
175  //traceGenerators.resize(C->numberOfGenerators());
176  unsigned int countOK=0;
177  {for (unsigned int i=0; i<_MinkowskiDecomposition.size(); ++i) {
178  if (_MinkowskiDecompositionOK[i] == true) {
179  // vertex_i = vertex_a + vertex_b
180  unsigned int a = _MinkowskiDecomposition[i].first;
181  unsigned int b = _MinkowskiDecomposition[i].second;
182  std::vector<int> genitorsAB;
183  genitorsAB.insert(genitorsAB.end(), genitorsOfGeneratorsA[a].begin(), genitorsOfGeneratorsA[a].end());
184  genitorsAB.insert(genitorsAB.end(), genitorsOfGeneratorsB[b].begin(), genitorsOfGeneratorsB[b].end());
185  traceGenerators.push_back(genitorsAB);
186  ++countOK;
187  }
188  }}
189  }
190 
193  boost::shared_ptr<Polytope_Rn> rebuildSum(
194  const std::set< unsigned int >& firstOperandCaps,
195  const std::set< unsigned int >& secondOperandCaps,
196  std::set< unsigned int >& newCaps,
197  double bb_size=1000.);
198 
199  protected:
201  boost::shared_ptr<Polytope_Rn> compute();
202 
204  void computeCommonRefinement(
205  const std::vector< boost::shared_ptr<Generator_Rn> >& listOfGenerators_A,
206  const std::vector< boost::shared_ptr<Generator_Rn> >& listOfGenerators_B,
207  std::vector< boost::shared_ptr<Generator_Rn> >& listOfGenerators_C,
208  const std::vector< boost::shared_ptr<PolyhedralCone_Rn> >& listOfDualCones_A,
209  const std::vector< boost::shared_ptr<PolyhedralCone_Rn> >& listOfDualCones_B,
210  std::vector< boost::shared_ptr<PolyhedralCone_Rn> >& listOfDualCones_C);
211 
213  void processNormalFan0();
214 
216  void processNormalFan1();
217 
219  void processNormalFan2();
220 
222  void computeCapHalfSpaces(
223  const std::set< unsigned int >& firstOperandCaps,
224  const std::set< unsigned int >& secondOperandCaps,
225  std::set< unsigned int >& sumCaps) const;
226 
227  protected:
228  const boost::shared_ptr<Polytope_Rn> _firstOperand;
229  const boost::shared_ptr<Polytope_Rn> _secondOperand;
230  boost::shared_ptr<Polytope_Rn> _sum;
231 
232  // Give the relation between a vertex of A and all of its associated Minkowski vertices i.e. its polyhedrical cap
233  // A2C[i] = [ c_u, c_v, ... ] }
234  // } => c_u = a_i + b_j
235  // B2C[j] = [ c_u, c_w, ... ] }
237  std::vector< std::vector<unsigned int> > _A2C;
239  std::vector< std::vector<unsigned int> > _B2C;
240 
243  std::vector< std::vector<unsigned int> > _neighboursA;
246  std::vector< std::vector<unsigned int> > _neighboursB;
247 
248  // minkowskiVertices(i,j)==1 <=> a_i + b_j is a Minkowski vertex.
249 
250  // MinkowskiDecomposition[k] = (a_i, b_j) <=> c_k = a_i + b_j is a Minkowski vertex.
251  // To store each Minkowski vertex decomposition
253  std::vector< std::pair< unsigned int, unsigned int > > _MinkowskiDecomposition;
255  std::vector< bool > _MinkowskiDecompositionOK;
256 
258  std::vector< boost::shared_ptr<PolyhedralCone_Rn> > _NF_Cones;
260  std::vector< boost::shared_ptr<Generator_Rn> > _NF_Vertices;
261 
262 };
263 
264 
268 
269 public:
270 
273  const boost::shared_ptr<Polytope_Rn>& A,
274  const boost::shared_ptr<Polytope_Rn>& B,
275  boost::shared_ptr<Polytope_Rn>& C,
276  const std::set< unsigned int >& firstOperandCaps,
277  const std::set< unsigned int >& secondOperandCaps,
278  std::set< unsigned int >& newCaps,
279  double bb_size=1000.):MinkowskiSum(A,B,C)
280  { C = rebuildSum(firstOperandCaps, secondOperandCaps, newCaps, bb_size); }
281 
282 
283 protected:
284 
287  boost::shared_ptr<Polytope_Rn> rebuildSum(
288  const std::set< unsigned int >& firstOperandCaps,
289  const std::set< unsigned int >& secondOperandCaps,
290  std::set< unsigned int >& newCaps,
291  double bb_size=1000.);
292 
294  void computeCapHalfSpaces(
295  const std::set< unsigned int >& firstOperandCaps,
296  const std::set< unsigned int >& secondOperandCaps,
297  std::set< unsigned int >& sumCaps);
298 
299 };
300 
301 
305 
306 public:
307 
310  const boost::shared_ptr<Polytope_Rn>& A,
311  const boost::shared_ptr<Polytope_Rn>& B,
312  boost::shared_ptr<Polytope_Rn>& C,
313  const std::set< unsigned int >& firstOperandCaps,
314  const std::set< unsigned int >& secondOperandCaps,
315  std::set< unsigned int >& newCaps,
316  double bb_size=1000.);
317 
318 };
319 
320 
324 
325 public:
326 
331  static int Translate(boost::shared_ptr<Polytope_Rn>& pol, const boost::numeric::ublas::vector<double>& v2t);
332 
337  static int GravityCenter(boost::shared_ptr<Polytope_Rn>& pol, boost::numeric::ublas::vector<double>& gravity_center);
338 
344  static int computeScalingFactorForInclusion(boost::shared_ptr<Polytope_Rn>& pol1, boost::shared_ptr<Polytope_Rn>& pol2, double& sfactor);
345 
350  static int scalingFactor(boost::shared_ptr<Polytope_Rn>& pol, double sfactor);
351 
356  static int scalingFactor(boost::shared_ptr<PolyhedralCone_Rn>& pol, double sfactor);
357 
362  static int PolarPolytope(
363  const boost::shared_ptr<Polytope_Rn>& original_pol,
364  boost::shared_ptr<Polytope_Rn>& polar_pol,
365  bool forceComputation = true, double bb_size=1000.);
366 
372  static int projectPolytopeOnCanonicalHyperplanes(
373  const std::set< unsigned int >& listOfHyperplanes,
374  const boost::shared_ptr<Polytope_Rn>& original_pol,
375  boost::shared_ptr<Polytope_Rn>& proj_pol);
376 
382  static int extrudeInCanonicalDirections(
383  const std::set< unsigned int >& originalSpaceDirections,
384  unsigned int dimensionOfTotalSpace,
385  const boost::shared_ptr<Polytope_Rn>& original_polytope,
386  boost::shared_ptr<PolyhedralCone_Rn>& extruded_polyhedron);
387 
388 };
389 
390 
394 
395 public:
396 
400  static int Compute(boost::shared_ptr<Polytope_Rn>& pol, double bb_size=1000.);
401 
402 };
403 
404 
407 class NormedHS {
408 
409  public:
410  NormedHS(const boost::numeric::ublas::vector<double>& nv, double cst, unsigned int nb) {
411  _currentNormedVector = nv;
412  _constant = cst;
413  _numberOfNormedHS = nb;
414  }
415 
416  const boost::numeric::ublas::vector<double>& getNormedVector() const {return _currentNormedVector;}
417 
418  double getConstant() const {return _constant;}
419 
420  double getNumberOfNormedHS() const {return _numberOfNormedHS;}
421 
422  void dump(std::ostream &this_ostream) const {
423  this_ostream << "{" << _numberOfNormedHS << ": " << _constant << " " << _currentNormedVector << "}" << std::endl;
424  }
425 
426  protected:
428  boost::numeric::ublas::vector<double> _currentNormedVector;
430  double _constant;
431  unsigned int _numberOfNormedHS;
432 
433 };
434 
435 
437 
438  public:
439  SortHalfSpaces(boost::shared_ptr<Polytope_Rn>& pol);
440 
441  void dump(std::ostream &this_ostream, const std::vector< std::pair<double, NormedHS > >& listOfNormedVectors) const {
442  std::vector< std::pair<double, NormedHS > >::const_iterator iteV = listOfNormedVectors.begin();
443  for (; iteV != listOfNormedVectors.end(); ++iteV) {
444  this_ostream << iteV->first << " ";
445  iteV->second.dump(this_ostream);
446  }
447  }
448 
449 };
450 
451 
455 
456 public:
457 
462  static void gnuplot2D(const boost::shared_ptr<Polytope_Rn>& polygon, const std::string& name, double col, std::ostream& out);
463 
464 };
465 
466 #endif // INC_POLY_ALGO
NormedHS::_currentNormedVector
boost::numeric::ublas::vector< double > _currentNormedVector
The normal vector.
Definition: PolyhedralAlgorithms_Rn.h:428
NormedHS::_numberOfNormedHS
unsigned int _numberOfNormedHS
Definition: PolyhedralAlgorithms_Rn.h:431
NormedHS::getNormedVector
const boost::numeric::ublas::vector< double > & getNormedVector() const
Definition: PolyhedralAlgorithms_Rn.h:416
PseudoSumWithoutCaps
Compute the Minkowski sum of two polytopes and then remove all cap half-spaces to truncate again.
Definition: PolyhedralAlgorithms_Rn.h:267
GeometricObjectIterator_Rn.h
MinkowskiSum::MinkowskiSum
MinkowskiSum()
Sets up the data structure for the computation of the Minkowski sum of two polytopes.
Definition: PolyhedralAlgorithms_Rn.h:143
MinkowskiSum::_neighboursB
std::vector< std::vector< unsigned int > > _neighboursB
Store the neighbours of each vertex of B.
Definition: PolyhedralAlgorithms_Rn.h:246
FaceEnumeration::getFacesWithFacets
const std::vector< std::vector< ListOfFaces > > & getFacesWithFacets()
Definition: PolyhedralAlgorithms_Rn.h:83
NormedHS::getNumberOfNormedHS
double getNumberOfNormedHS() const
Definition: PolyhedralAlgorithms_Rn.h:420
MinkowskiSum::_neighboursA
std::vector< std::vector< unsigned int > > _neighboursA
Store the neighbours of each vertex of A.
Definition: PolyhedralAlgorithms_Rn.h:243
NormedHS::NormedHS
NormedHS(const boost::numeric::ublas::vector< double > &nv, double cst, unsigned int nb)
Definition: PolyhedralAlgorithms_Rn.h:410
polito_EXPORT
#define polito_EXPORT
Definition: polito_Export.h:15
PolyhedralCone_Rn.h
MinkowskiSum::_firstOperand
const boost::shared_ptr< Polytope_Rn > _firstOperand
Definition: PolyhedralAlgorithms_Rn.h:228
MinkowskiSum::_NF_Vertices
std::vector< boost::shared_ptr< Generator_Rn > > _NF_Vertices
The list of C vertices.
Definition: PolyhedralAlgorithms_Rn.h:260
MinkowskiSum::MinkowskiSum
MinkowskiSum(const boost::shared_ptr< Polytope_Rn > &A, const boost::shared_ptr< Polytope_Rn > &B, boost::shared_ptr< Polytope_Rn > &C, const std::vector< std::vector< int > > &genitorsOfGeneratorsA, const std::vector< std::vector< int > > &genitorsOfGeneratorsB, std::vector< std::vector< int > > &traceGenerators)
Compute the Minkowski sum of two polytopes.
Definition: PolyhedralAlgorithms_Rn.h:165
MinkowskiSum::_secondOperand
const boost::shared_ptr< Polytope_Rn > _secondOperand
Definition: PolyhedralAlgorithms_Rn.h:229
NormedHS::getConstant
double getConstant() const
Definition: PolyhedralAlgorithms_Rn.h:418
FaceEnumeration::_polytope
const boost::shared_ptr< Polytope_Rn > & _polytope
Definition: PolyhedralAlgorithms_Rn.h:131
FaceEnumeration::_allFacesWithFacets
std::vector< std::vector< ListOfFaces > > _allFacesWithFacets
Definition: PolyhedralAlgorithms_Rn.h:127
MinkowskiSum::_MinkowskiDecompositionOK
std::vector< bool > _MinkowskiDecompositionOK
Tell whether _MinkowskiDecomposition had to be considered or not.
Definition: PolyhedralAlgorithms_Rn.h:255
MinkowskiSum::_sum
boost::shared_ptr< Polytope_Rn > _sum
Definition: PolyhedralAlgorithms_Rn.h:230
VolumeOfPolytopes_Rn.h
PolyhedralAlgorithms_Rn.h
FaceEnumeration
Combinatorial face enumeration for polytopes.
Definition: PolyhedralAlgorithms_Rn.h:44
FaceEnumeration::_allFacesWithVertices
std::vector< std::vector< ListOfFaces > > _allFacesWithVertices
Definition: PolyhedralAlgorithms_Rn.h:129
Visualization
2D visualization tools
Definition: PolyhedralAlgorithms_Rn.h:454
MinkowskiSum::_NF_Cones
std::vector< boost::shared_ptr< PolyhedralCone_Rn > > _NF_Cones
The normal fan polyhedrical cones list.
Definition: PolyhedralAlgorithms_Rn.h:258
Rn.h
MinkowskiSum::_B2C
std::vector< std::vector< unsigned int > > _B2C
Store the polyhedrical cap in C of each vertex of B.
Definition: PolyhedralAlgorithms_Rn.h:239
MinkowskiSum::_A2C
std::vector< std::vector< unsigned int > > _A2C
Store the polyhedrical cap in C of each vertex of A.
Definition: PolyhedralAlgorithms_Rn.h:237
Generator_Rn.h
ListOfFaces
std::vector< unsigned int > ListOfFaces
Definition: PolyhedralAlgorithms_Rn.h:40
FaceEnumeration::FaceEnumeration
FaceEnumeration(const boost::shared_ptr< Polytope_Rn > &A)
General Face Enumeration Algorithm.
Definition: PolyhedralAlgorithms_Rn.h:49
NormedHS::dump
void dump(std::ostream &this_ostream) const
Definition: PolyhedralAlgorithms_Rn.h:422
NormedHS
A normed half-space whose frontier is a linear (n-1) dimension space. _constant + _currentNormedVec...
Definition: PolyhedralAlgorithms_Rn.h:407
MinkowskiSum::_MinkowskiDecomposition
std::vector< std::pair< unsigned int, unsigned int > > _MinkowskiDecomposition
Store the genitors in A and B of each vertex of C.
Definition: PolyhedralAlgorithms_Rn.h:253
PseudoSumWithoutCaps::PseudoSumWithoutCaps
PseudoSumWithoutCaps(const boost::shared_ptr< Polytope_Rn > &A, const boost::shared_ptr< Polytope_Rn > &B, boost::shared_ptr< Polytope_Rn > &C, const std::set< unsigned int > &firstOperandCaps, const std::set< unsigned int > &secondOperandCaps, std::set< unsigned int > &newCaps, double bb_size=1000.)
Compute the Minkowski sum of two polytopes and then remove all cap half-spaces to truncate again.
Definition: PolyhedralAlgorithms_Rn.h:272
PseudoIntersectionWithoutCaps
Remove all cap half-spaces and then compute the intersection of two capped polytopes.
Definition: PolyhedralAlgorithms_Rn.h:304
DoubleDescription_Rn.h
DoubleDescriptionFromGenerators
Compute the V-description from the H-description.
Definition: PolyhedralAlgorithms_Rn.h:393
TopGeomTools
Basic tools for topology and geometry: translations, polarity, ...
Definition: PolyhedralAlgorithms_Rn.h:323
FaceEnumeration::clear
void clear()
Definition: PolyhedralAlgorithms_Rn.h:89
NormedHS::_constant
double _constant
The second member constant.
Definition: PolyhedralAlgorithms_Rn.h:430
MinkowskiSum::MinkowskiSum
MinkowskiSum(const boost::shared_ptr< Polytope_Rn > &A, const boost::shared_ptr< Polytope_Rn > &B)
Sets up the data structure for the computation of the Minkowski sum of two polytopes.
Definition: PolyhedralAlgorithms_Rn.h:147
FaceEnumeration::getFacesWithVertices
const std::vector< std::vector< ListOfFaces > > & getFacesWithVertices()
Definition: PolyhedralAlgorithms_Rn.h:77
SortHalfSpaces::dump
void dump(std::ostream &this_ostream, const std::vector< std::pair< double, NormedHS > > &listOfNormedVectors) const
Definition: PolyhedralAlgorithms_Rn.h:441
Polytope_Rn.h
IO_Polytope.h
polito_Export.h
SortHalfSpaces
Definition: PolyhedralAlgorithms_Rn.h:436
NormalFan_Rn.h
MinkowskiSum::MinkowskiSum
MinkowskiSum(const boost::shared_ptr< Polytope_Rn > &A, const boost::shared_ptr< Polytope_Rn > &B, boost::shared_ptr< Polytope_Rn > &C)
Compute the Minkowski sum of two polytopes.
Definition: PolyhedralAlgorithms_Rn.h:153
HalfSpace_Rn.h
FaceEnumeration::save
void save(std::ostream &this_stream) const
Definition: PolyhedralAlgorithms_Rn.h:119
MinkowskiSum
Compute the Minkowski sum of two polytopes.
Definition: PolyhedralAlgorithms_Rn.h:138