politopix  4.1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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-2016 : 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 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() throw (std::domain_error) {
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() throw (std::domain_error) {
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  throw (std::ios_base::failure);
103 
110  static void load(std::istream& this_stream, std::vector< std::vector< ListOfFaces > >& latt)
111  throw (std::out_of_range);
112 
119  static void save(std::ostream& this_stream, const std::vector< std::vector< ListOfFaces > >& latt);
120 
121  void save(std::ostream& this_stream) const { save(this_stream, _allFacesWithVertices); }
122 
123 
124  protected:
125  static void ComputeWithFacets(const boost::shared_ptr<Polytope_Rn>& A, FaceEnumeration& FaceEnum);
126 
127  static void ComputeWithVertices(const boost::shared_ptr<Polytope_Rn>& A, FaceEnumeration& FaceEnum);
128 
129  std::vector< std::vector< ListOfFaces > > _allFacesWithFacets;
130 
131  std::vector< std::vector< ListOfFaces > > _allFacesWithVertices;
132 
133  const boost::shared_ptr<Polytope_Rn>& _polytope;
134 
135 };
136 
137 
141 
142  public:
143 
146  {}
147 
150  const boost::shared_ptr<Polytope_Rn>& A,
151  const boost::shared_ptr<Polytope_Rn>& B):_firstOperand(A),_secondOperand(B)
152  {}
153 
156  const boost::shared_ptr<Polytope_Rn>& A,
157  const boost::shared_ptr<Polytope_Rn>& B,
158  boost::shared_ptr<Polytope_Rn>& C):_firstOperand(A),_secondOperand(B),_sum(C)
159  { C = compute(); }
160 
162  MinkowskiSum(
163  const std::vector< boost::shared_ptr<Polytope_Rn> >& arrayOfPolytopes,
164  boost::shared_ptr<Polytope_Rn>& C);
165 
168  const boost::shared_ptr<Polytope_Rn>& A,
169  const boost::shared_ptr<Polytope_Rn>& B,
170  boost::shared_ptr<Polytope_Rn>& C,
171  const std::vector< std::vector<int> >& genitorsOfGeneratorsA,
172  const std::vector< std::vector<int> >& genitorsOfGeneratorsB,
173  std::vector< std::vector<int> >& traceGenerators):_firstOperand(A),_secondOperand(B),_sum(C)
174  {
175  C = compute();
176  traceGenerators.clear();
177  //traceGenerators.resize(C->numberOfGenerators());
178  unsigned int countOK=0;
179  {for (unsigned int i=0; i<_MinkowskiDecomposition.size(); ++i) {
180  if (_MinkowskiDecompositionOK[i] == true) {
181  // vertex_i = vertex_a + vertex_b
182  unsigned int a = _MinkowskiDecomposition[i].first;
183  unsigned int b = _MinkowskiDecomposition[i].second;
184  std::vector<int> genitorsAB;
185  genitorsAB.insert(genitorsAB.end(), genitorsOfGeneratorsA[a].begin(), genitorsOfGeneratorsA[a].end());
186  genitorsAB.insert(genitorsAB.end(), genitorsOfGeneratorsB[b].begin(), genitorsOfGeneratorsB[b].end());
187  traceGenerators.push_back(genitorsAB);
188  ++countOK;
189  }
190  }}
191  }
192 
195  boost::shared_ptr<Polytope_Rn> rebuildSum(
196  const std::set< unsigned int >& firstOperandCaps,
197  const std::set< unsigned int >& secondOperandCaps,
198  std::set< unsigned int >& newCaps,
199  double bb_size=1000.);
200 
201  protected:
203  boost::shared_ptr<Polytope_Rn> compute() throw (std::domain_error);
204 
206  void computeCommonRefinement(
207  const std::vector< boost::shared_ptr<Generator_Rn> >& listOfGenerators_A,
208  const std::vector< boost::shared_ptr<Generator_Rn> >& listOfGenerators_B,
209  std::vector< boost::shared_ptr<Generator_Rn> >& listOfGenerators_C,
210  const std::vector< boost::shared_ptr<PolyhedralCone_Rn> >& listOfDualCones_A,
211  const std::vector< boost::shared_ptr<PolyhedralCone_Rn> >& listOfDualCones_B,
212  std::vector< boost::shared_ptr<PolyhedralCone_Rn> >& listOfDualCones_C) throw (std::domain_error);
213 
215  void processNormalFan0();
216 
218  void processNormalFan1();
219 
221  void processNormalFan2();
222 
224  void computeCapHalfSpaces(
225  const std::set< unsigned int >& firstOperandCaps,
226  const std::set< unsigned int >& secondOperandCaps,
227  std::set< unsigned int >& sumCaps) const throw (std::domain_error);
228 
229  protected:
230  const boost::shared_ptr<Polytope_Rn> _firstOperand;
231  const boost::shared_ptr<Polytope_Rn> _secondOperand;
232  boost::shared_ptr<Polytope_Rn> _sum;
233 
234  // Give the relation between a vertex of A and all of its associated Minkowski vertices i.e. its polyhedrical cap
235  // A2C[i] = [ c_u, c_v, ... ] }
236  // } => c_u = a_i + b_j
237  // B2C[j] = [ c_u, c_w, ... ] }
239  std::vector< std::vector<unsigned int> > _A2C;
241  std::vector< std::vector<unsigned int> > _B2C;
242 
245  std::vector< std::vector<unsigned int> > _neighboursA;
248  std::vector< std::vector<unsigned int> > _neighboursB;
249 
250  // minkowskiVertices(i,j)==1 <=> a_i + b_j is a Minkowski vertex.
251 
252  // MinkowskiDecomposition[k] = (a_i, b_j) <=> c_k = a_i + b_j is a Minkowski vertex.
253  // To store each Minkowski vertex decomposition
255  std::vector< std::pair< unsigned int, unsigned int > > _MinkowskiDecomposition;
257  std::vector< bool > _MinkowskiDecompositionOK;
258 
260  std::vector< boost::shared_ptr<PolyhedralCone_Rn> > _NF_Cones;
262  std::vector< boost::shared_ptr<Generator_Rn> > _NF_Vertices;
263 
264 };
265 
266 
270 
271 public:
272 
275  const boost::shared_ptr<Polytope_Rn>& A,
276  const boost::shared_ptr<Polytope_Rn>& B,
277  boost::shared_ptr<Polytope_Rn>& C,
278  const std::set< unsigned int >& firstOperandCaps,
279  const std::set< unsigned int >& secondOperandCaps,
280  std::set< unsigned int >& newCaps,
281  double bb_size=1000.):MinkowskiSum(A,B,C)
282  { C = rebuildSum(firstOperandCaps, secondOperandCaps, newCaps, bb_size); }
283 
284 
285 protected:
286 
289  boost::shared_ptr<Polytope_Rn> rebuildSum(
290  const std::set< unsigned int >& firstOperandCaps,
291  const std::set< unsigned int >& secondOperandCaps,
292  std::set< unsigned int >& newCaps,
293  double bb_size=1000.);
294 
296  void computeCapHalfSpaces(
297  const std::set< unsigned int >& firstOperandCaps,
298  const std::set< unsigned int >& secondOperandCaps,
299  std::set< unsigned int >& sumCaps) throw (std::domain_error);
300 
301 };
302 
303 
307 
308 public:
309 
312  const boost::shared_ptr<Polytope_Rn>& A,
313  const boost::shared_ptr<Polytope_Rn>& B,
314  boost::shared_ptr<Polytope_Rn>& C,
315  const std::set< unsigned int >& firstOperandCaps,
316  const std::set< unsigned int >& secondOperandCaps,
317  std::set< unsigned int >& newCaps,
318  double bb_size=1000.);
319 
320 };
321 
322 
326 
327 public:
328 
333  static int Translate(boost::shared_ptr<Polytope_Rn>& pol, const boost::numeric::ublas::vector<double>& v2t);
334 
339  static int GravityCenter(boost::shared_ptr<Polytope_Rn>& pol, boost::numeric::ublas::vector<double>& gravity_center);
340 
345  static int scalingFactor(boost::shared_ptr<Polytope_Rn>& pol, double factor);
346 
351  static int PolarPolytope(
352  const boost::shared_ptr<Polytope_Rn>& original_pol,
353  boost::shared_ptr<Polytope_Rn>& polar_pol,
354  bool forceComputation = true, double bb_size=1000.) throw (invalid_argument);
355 
361  static int projectPolytopeOnCanonicalHyperplanes(
362  const std::set< unsigned int >& listOfHyperplanes,
363  const boost::shared_ptr<Polytope_Rn>& original_pol,
364  boost::shared_ptr<Polytope_Rn>& proj_pol) throw (invalid_argument);
365 
371  static int extrudeInCanonicalDirections(
372  const std::set< unsigned int >& originalSpaceDirections,
373  unsigned int dimensionOfTotalSpace,
374  const boost::shared_ptr<Polytope_Rn>& original_polytope,
375  boost::shared_ptr<PolyhedralCone_Rn>& extruded_polyhedron) throw (invalid_argument);
376 
377 };
378 
379 
383 
384 public:
385 
389  static int Compute(boost::shared_ptr<Polytope_Rn>& pol, double bb_size=1000.)
390  throw (invalid_argument, out_of_range, ios_base::failure, logic_error);
391 
392 };
393 
394 
398 
399 public:
400 
405  static void gnuplot2D(const boost::shared_ptr<Polytope_Rn>& polygon, const std::string& name, double col, std::ostream& out) throw (std::domain_error);
406 
407 };
408 
409 #endif // INC_POLY_ALGO
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.
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.
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...
const std::vector< std::vector< ListOfFaces > > & getFacesWithFacets()
Compute the Minkowski sum of two polytopes and then remove all cap half-spaces to truncate again...
MinkowskiSum()
Sets up the data structure for the computation of the Minkowski sum of two polytopes.
Model a polytope using its two equivalent definitions : the convex hull and the half-space intersecti...
Definition: Polytope_Rn.h:34
2D visualization tools
std::vector< std::vector< ListOfFaces > > _allFacesWithFacets
Model a polyhedral cone using its two equivalent definitions : the convex hull and the half-space int...
Compute the Minkowski sum of two polytopes.
std::vector< std::vector< ListOfFaces > > _allFacesWithVertices
Remove all cap half-spaces and then compute the intersection of two capped polytopes.
std::vector< unsigned int > ListOfFaces
Compute the V-description from the H-description.
const boost::shared_ptr< Polytope_Rn > & _polytope
Combinatorial face enumeration for polytopes.
FaceEnumeration(const boost::shared_ptr< Polytope_Rn > &A)
General Face Enumeration Algorithm.
A n-coordinates generator, which can be a vertex or an edge whether it is contained by a polytope or ...
Definition: Generator_Rn.h:38
#define polito_EXPORT
Definition: polito_Export.h:15
const std::vector< std::vector< ListOfFaces > > & getFacesWithVertices()
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.
Basic tools for topology and geometry: translations, polarity, ...
void save(std::ostream &this_stream) const