politopix  5.0.0
HalfSpace_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-2021 : 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 HALFSPACE_Rn
22 #define HALFSPACE_Rn
23 
24 #include <vector>
25 #include <numeric>
26 #include <stdexcept>
27 #include <exception>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/numeric/ublas/vector.hpp>
30 #include <boost/numeric/ublas/io.hpp>
31 #include "Rn.h"
32 
33 class Generator_Rn_SD;
34 
35 using namespace boost::numeric::ublas;
36 
37 
41 
42  public:
43 
44  enum State {
45  hs_ON = 0,
46  hs_IN = 1,
47  hs_OUT = 2,
48  hs_UNKNOWN = 3,
49  hs_IN_OR_OUT = 4, // During the truncation process, some generators cannot have the state ON.
50  hs_OUT_TO_ON = 5}; // During the truncation process, for convenience, we can shift some generators states from OUT to ON.
51 
54 
55  HalfSpace_Rn(unsigned int n);
56 
57  virtual ~HalfSpace_Rn() {}
58 
59  void setCoefficients(const vector<double>& vec) {_coefficients = vec;}
60 
61  double getCoefficient(unsigned int i) const;
62 
63  void setCoefficient(unsigned int i, double c);
64 
65  void setConstant(double c) {_constant = c;}
66 
67  double getConstant() const {return _constant;}
68 
69  void negate() {_coefficients *= -1.;}
70 
71  int dimension() const {return _coefficients.size();}
72 
73  std::string getSideAsText() const {return std::string(">=");}
74 
75  boost::numeric::ublas::vector<double>::const_iterator begin() const {return _coefficients.begin();}
76 
77  boost::numeric::ublas::vector<double>::const_iterator end() const {return _coefficients.end();}
78 
79  const boost::numeric::ublas::vector<double>& vect() const {return _coefficients;}
80 
81  static std::string getStateAsText(const HalfSpace_Rn::State&);
82 
83  double computeDistancePointHyperplane(const boost::numeric::ublas::vector<double>& thisPoint) const {
84  double halfSpaceNorm = std::inner_product(_coefficients.begin(), _coefficients.end(), _coefficients.begin(), 0.);
85  halfSpaceNorm = sqrt(halfSpaceNorm);
86  double scalarProduct = std::inner_product(thisPoint.begin(), thisPoint.end(), _coefficients.begin(), 0.);
87  double distanceToHyperplane = (scalarProduct+_constant) / halfSpaceNorm;
88  return distanceToHyperplane;
89  }
90 
91  double computeDistancePointHyperplane(const boost::numeric::ublas::vector<double>& thisPoint, double halfSpaceNorm) const {
92  double scalarProduct = std::inner_product(thisPoint.begin(), thisPoint.end(), _coefficients.begin(), 0.);
93  double distanceToHyperplane = (scalarProduct+_constant) / halfSpaceNorm;
94  return distanceToHyperplane;
95  }
96 
98  const boost::numeric::ublas::vector<double>& thisPoint,
99  boost::numeric::ublas::vector<double>& projectedPoint,
100  double halfSpaceNorm) const {
101  double scalarProduct = std::inner_product(thisPoint.begin(), thisPoint.end(), _coefficients.begin(), 0.);
102  double distanceToHyperplane = (scalarProduct+_constant) / halfSpaceNorm;
103  projectedPoint = thisPoint - distanceToHyperplane*_coefficients;
104  return distanceToHyperplane;
105  }
106 
107  virtual void dump(std::ostream &this_ostream) const {
108  this_ostream << "(" << getConstant() << ", ";
109  unsigned int RnDIM = _coefficients.size();
110  {for (unsigned int ii=0; ii<RnDIM; ++ii) {
111  this_ostream << getCoefficient(ii);
112  if (ii != RnDIM-1)
113  this_ostream << ", ";
114  }}
115  this_ostream << ")";
116  }
117 
118  void print() const {
119  dump(std::cout);
120  std::cout << std::endl;
121  }
122 
125  virtual bool testEmptyness(const std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_IN,
126  const std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_OUT,
127  const std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_ON) {
128  //std::cout << "DD" << std::endl;
129  return (GN_IN.empty());
130  }
131 
134  virtual bool testEmptyness(unsigned int GN_IN, unsigned int GN_OUT, unsigned int GN_ON) {
135  //std::cout << "DD" << std::endl;
136  return (GN_IN == 0);
137  }
138 
143  virtual void noGeneratorsOUT(std::vector< boost::shared_ptr<Generator_Rn_SD> >& listOfGeneratorSD,
144  const std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_ON) {
145  //std::cout << "DD" << std::endl;
146  }
147 
152  //virtual void noGeneratorsOUT(std::vector< std::pair< Generator_Rn_SD, Generator_Rn_SD > >& allNeighbours) {
153  //std::cout << "DD" << std::endl;
154  //}
155 
158  virtual void prepareSetOfGenerators(std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_IN, std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_OUT) {
159  //std::cout << "DD" << std::endl;
160  }
161 
162 
163  protected:
165  boost::numeric::ublas::vector<double> _coefficients;
167  double _constant;
168 
169 };
170 
171 
173 
174  public:
176  _coefficients = HS.vect();
177  _constant = HS.getConstant();
178  double halfSpaceNorm = sqrt(std::inner_product(_coefficients.begin(), _coefficients.end(), _coefficients.begin(), 0.));
179  _coefficients = _coefficients / halfSpaceNorm;
180  _constant = _constant / halfSpaceNorm;
181  }
182 
183 };
184 
185 
189 
190  public:
192  Hyperplane_Rn(unsigned int n):HalfSpace_Rn(n) {}
193 
195  virtual ~Hyperplane_Rn() {}
196 
197  virtual void dump(std::ostream &this_ostream) const {
198  this_ostream << "(" << getConstant() << "; ";
199  unsigned int RnDIM = _coefficients.size();
200  {for (unsigned int ii=0; ii<RnDIM; ++ii) {
201  this_ostream << getCoefficient(ii);
202  if (ii != RnDIM-1)
203  this_ostream << "; ";
204  }}
205  this_ostream << ")";
206  }
207 
210  virtual bool testEmptyness(const std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_IN,
211  const std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_OUT,
212  const std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_ON) {
213  //std::cout << "FlatDD" << std::endl;
214  return ((GN_IN.empty() && GN_ON.empty()) || (GN_OUT.empty() && GN_ON.empty()));
215  }
216 
219  virtual bool testEmptyness(unsigned int GN_IN, unsigned int GN_OUT, unsigned int GN_ON) {
220  //std::cout << "FlatDD" << std::endl;
221  return ((GN_IN==0 && GN_ON==0) || (GN_OUT==0 && GN_ON==0));
222  }
223 
228  virtual void noGeneratorsOUT(std::vector< boost::shared_ptr<Generator_Rn_SD> >& listOfGeneratorSD,
229  const std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_ON) {
230  //std::cout << "FlatDD" << std::endl;
231  listOfGeneratorSD = GN_ON;
232  }
233 
238  //virtual void noGeneratorsOUT(std::vector< Segment_Rn >& allNeighbours);
239 
244  virtual void prepareSetOfGenerators(std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_IN, std::vector< boost::shared_ptr<Generator_Rn_SD> >& GN_OUT) {
245  //std::cout << "FlatDD" << std::endl;
246  GN_OUT.insert(GN_OUT.end(), GN_IN.begin(), GN_IN.end());
247  GN_IN.clear();
248  }
249 
250 };
251 
252 #endif // HALFSPACE_Rn
HalfSpace_Rn::computeDistancePointHyperplane
double computeDistancePointHyperplane(const boost::numeric::ublas::vector< double > &thisPoint, boost::numeric::ublas::vector< double > &projectedPoint, double halfSpaceNorm) const
Definition: HalfSpace_Rn.h:97
NormedHalfSpace_Rn::NormedHalfSpace_Rn
NormedHalfSpace_Rn(HalfSpace_Rn HS)
Definition: HalfSpace_Rn.h:175
HalfSpace_Rn::setCoefficients
void setCoefficients(const vector< double > &vec)
Definition: HalfSpace_Rn.h:59
HalfSpace_Rn::_constant
double _constant
The second member constant.
Definition: HalfSpace_Rn.h:167
HalfSpace_Rn::prepareSetOfGenerators
virtual void prepareSetOfGenerators(std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_IN, std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_OUT)
Specific to the double description algorithm When chopping a polyhedral body with a hyperplane,...
Definition: HalfSpace_Rn.h:158
Hyperplane_Rn::prepareSetOfGenerators
virtual void prepareSetOfGenerators(std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_IN, std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_OUT)
Specific to the double description algorithm When chopping a polyhedral body with a hyperplane,...
Definition: HalfSpace_Rn.h:244
polito_EXPORT
#define polito_EXPORT
Definition: polito_Export.h:15
HalfSpace_Rn::HalfSpace_Rn
HalfSpace_Rn()
Constructor.
Definition: HalfSpace_Rn.h:53
HalfSpace_Rn::setConstant
void setConstant(double c)
Definition: HalfSpace_Rn.h:65
HalfSpace_Rn::vect
const boost::numeric::ublas::vector< double > & vect() const
Definition: HalfSpace_Rn.h:79
HalfSpace_Rn::negate
void negate()
Definition: HalfSpace_Rn.h:69
HalfSpace_Rn::end
boost::numeric::ublas::vector< double >::const_iterator end() const
Definition: HalfSpace_Rn.h:77
HalfSpace_Rn::computeDistancePointHyperplane
double computeDistancePointHyperplane(const boost::numeric::ublas::vector< double > &thisPoint, double halfSpaceNorm) const
Definition: HalfSpace_Rn.h:91
HalfSpace_Rn::print
void print() const
Definition: HalfSpace_Rn.h:118
HalfSpace_Rn
A half-space whose frontier is a linear (n-1) dimension space. _constant + _coefficients[0]....
Definition: HalfSpace_Rn.h:40
Rn.h
HalfSpace_Rn::_coefficients
boost::numeric::ublas::vector< double > _coefficients
The normal vector.
Definition: HalfSpace_Rn.h:165
HalfSpace_Rn::noGeneratorsOUT
virtual void noGeneratorsOUT(std::vector< boost::shared_ptr< Generator_Rn_SD > > &listOfGeneratorSD, const std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_ON)
Specific to the double description algorithm When chopping a polyhedral body with a hyperplane,...
Definition: HalfSpace_Rn.h:143
Hyperplane_Rn::~Hyperplane_Rn
virtual ~Hyperplane_Rn()
Destructor.
Definition: HalfSpace_Rn.h:195
HalfSpace_Rn::testEmptyness
virtual bool testEmptyness(unsigned int GN_IN, unsigned int GN_OUT, unsigned int GN_ON)
Specific to the double description algorithm Test whether an intersection with a half-space has gener...
Definition: HalfSpace_Rn.h:134
NormedHalfSpace_Rn
Definition: HalfSpace_Rn.h:172
HalfSpace_Rn::getSideAsText
std::string getSideAsText() const
Definition: HalfSpace_Rn.h:73
Hyperplane_Rn
A linear (n-1) dimension space in.
Definition: HalfSpace_Rn.h:188
HalfSpace_Rn::State
State
Definition: HalfSpace_Rn.h:44
Generator_Rn_SD
A n-coordinates generator for internal data structure. It can be a vertex or an edge whether it is em...
Definition: Generator_Rn.h:280
HalfSpace_Rn::computeDistancePointHyperplane
double computeDistancePointHyperplane(const boost::numeric::ublas::vector< double > &thisPoint) const
Definition: HalfSpace_Rn.h:83
HalfSpace_Rn::begin
boost::numeric::ublas::vector< double >::const_iterator begin() const
Definition: HalfSpace_Rn.h:75
Hyperplane_Rn::noGeneratorsOUT
virtual void noGeneratorsOUT(std::vector< boost::shared_ptr< Generator_Rn_SD > > &listOfGeneratorSD, const std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_ON)
Specific to the double description algorithm When chopping a polyhedral body with a hyperplane,...
Definition: HalfSpace_Rn.h:228
Hyperplane_Rn::Hyperplane_Rn
Hyperplane_Rn(unsigned int n)
Constructor for polytopes i.e. bounded convex polyhedra.
Definition: HalfSpace_Rn.h:192
HalfSpace_Rn::testEmptyness
virtual bool testEmptyness(const std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_IN, const std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_OUT, const std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_ON)
Specific to the double description algorithm Test whether an intersection with a half-space has gener...
Definition: HalfSpace_Rn.h:125
HalfSpace_Rn::~HalfSpace_Rn
virtual ~HalfSpace_Rn()
Definition: HalfSpace_Rn.h:57
HalfSpace_Rn::getConstant
double getConstant() const
Definition: HalfSpace_Rn.h:67
HalfSpace_Rn::dimension
int dimension() const
Definition: HalfSpace_Rn.h:71
Hyperplane_Rn::dump
virtual void dump(std::ostream &this_ostream) const
Definition: HalfSpace_Rn.h:197
Hyperplane_Rn::testEmptyness
virtual bool testEmptyness(unsigned int GN_IN, unsigned int GN_OUT, unsigned int GN_ON)
Specific to the double description algorithm Test whether an intersection with a hyperplane has gener...
Definition: HalfSpace_Rn.h:219
HalfSpace_Rn::dump
virtual void dump(std::ostream &this_ostream) const
Definition: HalfSpace_Rn.h:107
Hyperplane_Rn::testEmptyness
virtual bool testEmptyness(const std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_IN, const std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_OUT, const std::vector< boost::shared_ptr< Generator_Rn_SD > > &GN_ON)
Specific to the double description algorithm Test whether an intersection with a hyperplane has gener...
Definition: HalfSpace_Rn.h:210