politopix  5.0.0
Polytope_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-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 POLYTOPE_Rn
22 #define POLYTOPE_Rn
23 
24 #include <boost/shared_ptr.hpp>
25 #include <stdexcept>
26 #include <exception>
27 #include <iostream>
28 #include <vector>
29 #include "polito_Export.h"
30 #include "PolyhedralCone_Rn.h"
31 #include "Generator_Rn.h"
32 #include "HalfSpace_Rn.h"
33 #include "IO_Polytope.h"
34 
37 
38  public:
41 
43  virtual ~Polytope_Rn() {}
44 
46  static boost::shared_ptr<Polytope_Rn> create() { return boost::shared_ptr<Polytope_Rn>(new Polytope_Rn); }
47 
49  virtual bool isBounded() const {return true;}
50 
52  virtual unsigned int neigbourhoodCondition() const {return (dimension()-1);}
53 
55  virtual unsigned int numberOfGeneratorsPerFacet() const {return (dimension());}
56 
58  virtual void createBoundingSimplex(double M);
59 
61  virtual void createBoundingBox(double M);
62 
63  virtual bool checkEdges() const;
64 
69  bool checkEqualityOfVertices(const boost::shared_ptr<Polytope_Rn>& B, bool printOnScreen = false) const;
70 
73  virtual double createTruncatedGenerator(
74  const boost::shared_ptr<Generator_Rn_SD>& in,
75  const boost::shared_ptr<Generator_Rn_SD>& out,
76  boost::shared_ptr<Generator_Rn_SD> newV, double ay, double az, double b=0.) const;
77 
78  boost::shared_ptr<PolyhedralCone_Rn> getPrimalCone(unsigned int u, const std::vector<unsigned int>& allNgb) const;
79 
82  boost::shared_ptr<PolyhedralCone_Rn> getPrimalCone(unsigned int i) const;
83 
84  boost::shared_ptr<Polytope_Rn> sum(const boost::shared_ptr<Polytope_Rn>& pol2sum) ;
85 
86  boost::shared_ptr<Polytope_Rn> intersect(const boost::shared_ptr<Polytope_Rn>& pol2intersect) ;
87 
88  bool isIncluded(const boost::shared_ptr<Polytope_Rn>& B) const {
90  {for (iteHS_B.begin(); iteHS_B.end()!=true; iteHS_B.next()) {
91  double halfSpaceNorm = std::inner_product(iteHS_B.current()->begin(), iteHS_B.current()->end(), iteHS_B.current()->begin(), 0.);
92  halfSpaceNorm = sqrt(halfSpaceNorm);
94  {for (iteGN_A.begin(); iteGN_A.end()!=true; iteGN_A.next()) {
95  HalfSpace_Rn::State currentState = checkPoint(iteGN_A.current(), iteHS_B.current(), halfSpaceNorm);
96  if (currentState == HalfSpace_Rn::hs_OUT)
97  return false;
98  }}
99  }}
100  return true;
101  }
102 
103  bool isIncluded(const boost::shared_ptr<Polytope_Rn>& B, double& minmaxDistance) const {
104  bool isOut = false;
105  double TOL = Rn::getTolerance();
106  double maxDist = std::numeric_limits<double>::min();
107  double minDist = std::numeric_limits<double>::max();
109  {for (iteHS_B.begin(); iteHS_B.end()!=true; iteHS_B.next()) {
110  double halfSpaceNorm = std::inner_product(iteHS_B.current()->begin(), iteHS_B.current()->end(), iteHS_B.current()->begin(), 0.);
111  halfSpaceNorm = sqrt(halfSpaceNorm);
113  {for (iteGN_A.begin(); iteGN_A.end()!=true; iteGN_A.next()) {
114  double scalarProduct = std::inner_product(iteGN_A.current()->begin(), iteGN_A.current()->end(), iteHS_B.current()->begin(), 0.);
115  double distanceToHyperplane = (scalarProduct+iteHS_B.current()->getConstant()) / halfSpaceNorm;
116  //std::cout << "thisPoint" << i << " = " << thisPoint << std::endl;
117  //std::cout << "dist" << i << " = " << distanceToHyperplane << std::endl;
118  if (distanceToHyperplane < -TOL) {
119  isOut = true;
120  // As distanceToHyperplane, in this case, is always negative, use its opposite.
121  double opp = -distanceToHyperplane;
122  if (opp > maxDist)
123  maxDist = opp;
124  }
125  else {
126  // Here distanceToHyperplane \in [-TOL, +inf[ so turn it into a positive number in case it was negative.
127  if (distanceToHyperplane < 0.)
128  distanceToHyperplane = -distanceToHyperplane;
129  if (distanceToHyperplane < minDist)
130  minDist = distanceToHyperplane;
131  }
132  }}
133  }}
134  // If at least one vertex is out, then exit claiming we have no inclusion.
135  if (isOut == true) {
136  minmaxDistance = maxDist;
137  return false;
138  }
139  else
140  minmaxDistance = minDist;
141  return true;
142  }
143 
144  std::string getName() const {return std::string("Polytope");}
145 
146  std::string getFileExtension() const {return std::string(".ptop");}
147 
148 };
149 
150 
153 
154  public:
156  CappedPolytope_Rn(boost::shared_ptr<Polytope_Rn> pol, const std::set< unsigned int >& set) {
157  _operandCaps = set;
158  _polytope = pol;
159  }
160 
162 
164  virtual ~CappedPolytope_Rn() {}
165 
166  void setPolytope(boost::shared_ptr<Polytope_Rn> pol) {_polytope = pol;}
167 
169  boost::shared_ptr<Polytope_Rn> getPolytope() {return _polytope;}
170 
172  const boost::shared_ptr<PolyhedralCone_Rn>& getPrismaticHPolyhedron() {
173  _prismaticPolyhedron.reset(new PolyhedralCone_Rn());
174  {for (unsigned int i=0; i<_polytope->numberOfHalfSpaces(); ++i) {
175  if (_operandCaps.find(i) == _operandCaps.end())
176  _prismaticPolyhedron->addHalfSpace( _polytope->getHalfSpace(i) );
177  }}
178  return _prismaticPolyhedron;
179  }
180 
182  void resetConstantsOfAllHalfSpaces(double cst) {
183  if (_polytope) {
184  for (unsigned int i=0; i<_polytope->numberOfHalfSpaces(); ++i) {
185  _polytope->getHalfSpace(i)->setConstant(cst);
186  }
187  }
188  if (_prismaticPolyhedron) {
189  for (unsigned int i=0; i<_prismaticPolyhedron->numberOfHalfSpaces(); ++i) {
190  _prismaticPolyhedron->getHalfSpace(i)->setConstant(cst);
191  }
192  }
193  }
194 
196  void resetConstantOfGivenHalfSpace(double cst, unsigned int fctNb) {
197  if (_polytope) {
198  if (fctNb >= _polytope->numberOfHalfSpaces())
199  throw std::out_of_range("CappedPolytope_Rn::resetConstantOfGivenHalfSpace() polytope index out of range");
200  _polytope->getHalfSpace(fctNb)->setConstant(cst);
201  }
202  if (_prismaticPolyhedron) {
203  if (fctNb >= _prismaticPolyhedron->numberOfHalfSpaces())
204  throw std::out_of_range("CappedPolytope_Rn::resetConstantOfGivenHalfSpace() prismaticPolyhedron index out of range");
205  _prismaticPolyhedron->getHalfSpace(fctNb)->setConstant(cst);
206  }
207  }
208 
209  void setCappedFacets(const std::set< unsigned int >& set) {_operandCaps = set;}
210 
211  boost::shared_ptr<CappedPolytope_Rn> sum(const boost::shared_ptr<CappedPolytope_Rn>& pol2sum, double bb_size=1000.) ;
212 
214  void sum(const std::vector< boost::shared_ptr<CappedPolytope_Rn> >& allPolytopes0) {
215  std::vector< boost::shared_ptr<CappedPolytope_Rn> > allPolytopes = allPolytopes0;
216  {for (unsigned int i=0; i<allPolytopes.size()-1; ++i) {
217  boost::shared_ptr<CappedPolytope_Rn> currentSum(new CappedPolytope_Rn());
218  currentSum = allPolytopes[i]->sum(allPolytopes[i+1]);
219  // Check whether the job is finish or not.
220  if (i == allPolytopes.size()-2) {
221  _operandCaps = currentSum->_operandCaps;
222  _polytope = currentSum->_polytope;
223  }
224  else
225  allPolytopes[i+1] = currentSum;
226  }}
227  }
228 
229  void intersect(const std::vector< boost::shared_ptr<CappedPolytope_Rn> >& allPolytopes0) {
230  std::vector< boost::shared_ptr<CappedPolytope_Rn> > allPolytopes = allPolytopes0;
231  {for (unsigned int i=0; i<allPolytopes.size()-1; ++i) {
232  boost::shared_ptr<CappedPolytope_Rn> currentInt(new CappedPolytope_Rn());
233  currentInt = allPolytopes[i]->intersect(allPolytopes[i+1]);
234  // Check whether the job is finish or not.
235  if (i == allPolytopes.size()-2) {
236  _operandCaps = currentInt->_operandCaps;
237  _polytope = currentInt->_polytope;
238  }
239  else
240  allPolytopes[i+1] = currentInt;
241  }}
242  }
243 
244  boost::shared_ptr<CappedPolytope_Rn> intersect(const boost::shared_ptr<CappedPolytope_Rn>& pol2intersect) ;
245 
246  bool isIncluded(const boost::shared_ptr<CappedPolytope_Rn>& B) const { return _polytope->isIncluded(B->_polytope); }
247 
253  static bool isIncluded(const boost::shared_ptr<CappedPolytope_Rn>& firstBody, const boost::shared_ptr<CappedPolytope_Rn>& secondBody, double& minmaxDistance) {
254  return firstBody->_polytope->isIncluded(secondBody->_polytope, minmaxDistance);
255  }
256 
257  int scalingFactor(double factor);
258 
259  bool checkTopologyAndGeometry() const { return _polytope->checkTopologyAndGeometry(); }
260 
261  void save(const std::string& pathA);
262 
263  void load(const std::string& pathA, double bb_size=10000.);
264 
265  void dump(std::ostream &current_ostream) const {
266  current_ostream << std::endl << "#CAPPED FACETS:" << std::endl;
267  std::copy(_operandCaps.begin(), _operandCaps.end(), std::ostream_iterator<unsigned int>(current_ostream, " ") );
268  current_ostream << std::endl;
269  _polytope->dump(current_ostream);
270  }
271 
272  static std::string getName() {return std::string("CappedPolytope");}
273 
274  static std::string getFileExtension() {return std::string(".ctop");}
275 
276  protected:
278  std::set< unsigned int > _operandCaps;
280  boost::shared_ptr<Polytope_Rn> _polytope;
283  boost::shared_ptr<PolyhedralCone_Rn> _prismaticPolyhedron;
284 
285 };
286 
287 #endif // POLYTOPE_Rn
HalfSpace_Rn::hs_OUT
@ hs_OUT
Definition: HalfSpace_Rn.h:47
CappedPolytope_Rn::getPolytope
boost::shared_ptr< Polytope_Rn > getPolytope()
Return the capped polytope under its current description.
Definition: Polytope_Rn.h:169
PolyhedralCone_Rn::createBoundingBox
virtual void createBoundingBox(double)
At the moment this function is useless in the case of polyhedral cones.
Definition: PolyhedralCone_Rn.h:848
PolyhedralCone_Rn::createTruncatedGenerator
virtual double createTruncatedGenerator(const boost::shared_ptr< Generator_Rn_SD > &y, const boost::shared_ptr< Generator_Rn_SD > &z, boost::shared_ptr< Generator_Rn_SD > newG, double ay, double az, double b=0.) const
Create the intersection edge in the truncating algorithm. It is defined by the intersection between a...
Definition: PolyhedralCone_Rn.cpp:361
CappedPolytope_Rn
Model a capped polytope i.e. a polytope where we can partition the facets in two sets: regular facets...
Definition: Polytope_Rn.h:152
CappedPolytope_Rn::resetConstantOfGivenHalfSpace
void resetConstantOfGivenHalfSpace(double cst, unsigned int fctNb)
Reset the constant of a given halfspace.
Definition: Polytope_Rn.h:196
Polytope_Rn::neigbourhoodCondition
virtual unsigned int neigbourhoodCondition() const
Two vertices are neighbours in a polytope <=> they share at least (n-1) facets.
Definition: Polytope_Rn.h:52
PolyhedralCone_Rn
Model a polyhedral cone using its two equivalent definitions : the convex hull and the half-space int...
Definition: PolyhedralCone_Rn.h:47
Polytope_Rn::create
static boost::shared_ptr< Polytope_Rn > create()
Return s shared pointer on an empty polytope.
Definition: Polytope_Rn.h:46
CappedPolytope_Rn::_polytope
boost::shared_ptr< Polytope_Rn > _polytope
Definition: Polytope_Rn.h:280
CappedPolytope_Rn::getFileExtension
static std::string getFileExtension()
Definition: Polytope_Rn.h:274
polito_EXPORT
#define polito_EXPORT
Definition: polito_Export.h:15
PolyhedralCone_Rn.h
CappedPolytope_Rn::CappedPolytope_Rn
CappedPolytope_Rn(boost::shared_ptr< Polytope_Rn > pol, const std::set< unsigned int > &set)
Constructor.
Definition: Polytope_Rn.h:156
CappedPolytope_Rn::~CappedPolytope_Rn
virtual ~CappedPolytope_Rn()
Destructor.
Definition: Polytope_Rn.h:164
Polytope_Rn::getName
std::string getName() const
Definition: Polytope_Rn.h:144
CappedPolytope_Rn::getPrismaticHPolyhedron
const boost::shared_ptr< PolyhedralCone_Rn > & getPrismaticHPolyhedron()
Return the uncapped polytope i.e. the prismatic polyhedron under the H-description.
Definition: Polytope_Rn.h:172
CappedPolytope_Rn::getName
static std::string getName()
Definition: Polytope_Rn.h:272
CappedPolytope_Rn::sum
void sum(const std::vector< boost::shared_ptr< CappedPolytope_Rn > > &allPolytopes0)
Perform the sum of all the capped polytopes stored in the array.
Definition: Polytope_Rn.h:214
CappedPolytope_Rn::isIncluded
static bool isIncluded(const boost::shared_ptr< CappedPolytope_Rn > &firstBody, const boost::shared_ptr< CappedPolytope_Rn > &secondBody, double &minmaxDistance)
Test whether firstBody is inside secondBody.
Definition: Polytope_Rn.h:253
CappedPolytope_Rn::intersect
void intersect(const std::vector< boost::shared_ptr< CappedPolytope_Rn > > &allPolytopes0)
Definition: Polytope_Rn.h:229
CappedPolytope_Rn::resetConstantsOfAllHalfSpaces
void resetConstantsOfAllHalfSpaces(double cst)
Reset all halfspaces constants to the same value.
Definition: Polytope_Rn.h:182
CappedPolytope_Rn::setCappedFacets
void setCappedFacets(const std::set< unsigned int > &set)
Definition: Polytope_Rn.h:209
constIteratorOfListOfGeometricObjects::current
const GEOMETRIC_OBJECT current()
Return the current geometric element.
Definition: GeometricObjectIterator_Rn.h:177
PolyhedralCone_Rn::checkPoint
HalfSpace_Rn::State checkPoint(const Point_Rn &thisPoint) const
Check a point state against the whole polyhedron.
Definition: PolyhedralCone_Rn.cpp:30
CappedPolytope_Rn::_prismaticPolyhedron
boost::shared_ptr< PolyhedralCone_Rn > _prismaticPolyhedron
The uncapped polytope i.e. the polytope without its capped half-spaces. H-description(_prismaticPolyh...
Definition: Polytope_Rn.h:283
constIteratorOfListOfGeometricObjects::end
bool end() const
Tell whether we have reached the end of the list.
Definition: GeometricObjectIterator_Rn.h:174
Polytope_Rn::numberOfGeneratorsPerFacet
virtual unsigned int numberOfGeneratorsPerFacet() const
Each facet in a polytope has got n vertices.
Definition: Polytope_Rn.h:55
Generator_Rn.h
constIteratorOfListOfGeometricObjects
This class is designed to run the list of all geometric objects representing a polytope.
Definition: GeometricObjectIterator_Rn.h:142
Polytope_Rn::~Polytope_Rn
virtual ~Polytope_Rn()
Destructor.
Definition: Polytope_Rn.h:43
HalfSpace_Rn::State
State
Definition: HalfSpace_Rn.h:44
constIteratorOfListOfGeometricObjects::begin
void begin()
Move the iterator at the beginning of the list.
Definition: GeometricObjectIterator_Rn.h:150
PolyhedralCone_Rn::dimension
virtual unsigned int dimension() const
Return the space dimension.
Definition: PolyhedralCone_Rn.h:65
CappedPolytope_Rn::setPolytope
void setPolytope(boost::shared_ptr< Polytope_Rn > pol)
Definition: Polytope_Rn.h:166
CappedPolytope_Rn::dump
void dump(std::ostream &current_ostream) const
Definition: Polytope_Rn.h:265
Polytope_Rn::getFileExtension
std::string getFileExtension() const
Definition: Polytope_Rn.h:146
CappedPolytope_Rn::_operandCaps
std::set< unsigned int > _operandCaps
Contain the number of the facets which are cap facets.
Definition: Polytope_Rn.h:278
Polytope_Rn::isIncluded
bool isIncluded(const boost::shared_ptr< Polytope_Rn > &B) const
Definition: Polytope_Rn.h:88
Polytope_Rn::isBounded
virtual bool isBounded() const
Tell whether this polyhedron is bounded or not, polytopes are bounded.
Definition: Polytope_Rn.h:49
CappedPolytope_Rn::CappedPolytope_Rn
CappedPolytope_Rn()
Definition: Polytope_Rn.h:161
IO_Polytope.h
PolyhedralCone_Rn::createBoundingSimplex
virtual void createBoundingSimplex(double)
At the moment this function is useless in the case of polyhedral cones.
Definition: PolyhedralCone_Rn.h:851
constIteratorOfListOfGeometricObjects::next
void next()
Move the iterator one step forward.
Definition: GeometricObjectIterator_Rn.h:153
CappedPolytope_Rn::isIncluded
bool isIncluded(const boost::shared_ptr< CappedPolytope_Rn > &B) const
Definition: Polytope_Rn.h:246
polito_Export.h
Rn::getTolerance
static polito_EXPORT double getTolerance()
Give the minimum distance between two points.
Definition: Rn.cpp:31
CappedPolytope_Rn::checkTopologyAndGeometry
bool checkTopologyAndGeometry() const
Definition: Polytope_Rn.h:259
PolyhedralCone_Rn::checkEdges
virtual bool checkEdges() const
Always true in the polyhedral cone case.
Definition: PolyhedralCone_Rn.h:677
Polytope_Rn::Polytope_Rn
Polytope_Rn()
Constructor for polytopes i.e. bounded convex polyhedra.
Definition: Polytope_Rn.h:40
Polytope_Rn
Model a polytope using its two equivalent definitions : the convex hull and the half-space intersecti...
Definition: Polytope_Rn.h:36
HalfSpace_Rn.h
Polytope_Rn::isIncluded
bool isIncluded(const boost::shared_ptr< Polytope_Rn > &B, double &minmaxDistance) const
Definition: Polytope_Rn.h:103