politopix  4.1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
GeometricObjectIterator_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) 2012-2015 : 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 GEOMETRIC_OBJECT_ITERATOR_Rn
22 #define GEOMETRIC_OBJECT_ITERATOR_Rn
23 
24 #include <boost/numeric/ublas/io.hpp>
25 #include <boost/shared_ptr.hpp>
26 #include <stdexcept>
27 #include <exception>
28 #include <vector>
29 #include <stack>
30 #include <set>
31 #include "Generator_Rn.h"
32 #include "HalfSpace_Rn.h"
33 #include "Rn.h"
34 
35 using namespace boost::numeric::ublas;
36 
37 
40 template< class GEOMETRIC_OBJECT > class listOfGeometricObjects {
41 
42  public:
45 
47  void push_back(const GEOMETRIC_OBJECT& gn) {_GO.push_back(gn);}
48 
50  const GEOMETRIC_OBJECT& operator [](unsigned int i) const {return _GO[i];}
51 
54  {_GO.clear(); _GO = listOfGN._GO;}
55 
58  {_GO.clear(); _GO = listOfGN._GO;}
59 
61  bool empty() const {return _GO.empty();}
62 
64  unsigned int size() const {return _GO.size();}
65 
67  void clear() {_GO.clear();}
68 
70  void negate() {
71  for (typename std::vector< GEOMETRIC_OBJECT >::iterator it=_GO.begin(); it!=_GO.end(); ++it)
72  (*it)->negate();
73  }
74 
76  unsigned int find(const GEOMETRIC_OBJECT& GO) const throw (std::out_of_range) {
77  unsigned int index=0;
78  typename std::vector< GEOMETRIC_OBJECT >::const_iterator it;
79  for (it=_GO.begin(); it!=_GO.end() && *it!=GO; ++it)
80  index++;
81  if (it == _GO.end()) {
82  std::ostringstream stream_;
83  stream_ << "listOfGeometricObjects::find() The current object is not stored in array";
84  std::string valString = stream_.str();
85  throw std::out_of_range(valString);
86  }
87  return index;
88  }
89 
91  void removeGeometricObjects(const std::set< GEOMETRIC_OBJECT >& setToRemove) {
92  for (typename std::vector< GEOMETRIC_OBJECT >::iterator it=_GO.begin(); it!=_GO.end(); ) {
93  if (setToRemove.find(*it) != setToRemove.end())
94  it = _GO.erase(it);
95  else
96  ++it;
97  }
98  }
99 
101  void removeGeometricObject(unsigned int j) {_GO.erase(_GO.begin()+j);}
102 
104  static bool inferior(const GEOMETRIC_OBJECT& HS1, const GEOMETRIC_OBJECT& HS2) {
105  unsigned int i=0;
106  unsigned int n=HS1->dimension();
107  do {
108  if (HS1->getCoefficient(i) < HS2->getCoefficient(i))
109  return true;
110  else if (HS1->getCoefficient(i) > HS2->getCoefficient(i))
111  return false;
112  else
113  i++;
114  } while (i < n);
115  return true;
116  }
117 
119  static bool superior(const GEOMETRIC_OBJECT& HS1, const GEOMETRIC_OBJECT& HS2) {
120  return !inferior(HS1, HS2);
121  }
122 
123  void lexminSort(unsigned int step) {
124  typename std::vector< GEOMETRIC_OBJECT >::iterator it = _GO.begin();
125  std::advance(it, step);
126  std::sort( it, _GO.end(), &inferior );
127  }
128 
129  void lexmaxSort(unsigned int step) {
130  typename std::vector< GEOMETRIC_OBJECT >::iterator it = _GO.begin();
131  std::advance(it, step);
132  std::sort( it, _GO.end(), &superior );
133  }
134 
135  protected:
137  std::vector< GEOMETRIC_OBJECT > _GO;
138 };
139 
140 
142 template< class GEOMETRIC_OBJECT > class constIteratorOfListOfGeometricObjects {
143 
144  public:
147  {_iterator=0;_step=0;}
148 
150  void begin() {_iterator=_step;}
151 
153  void next() {_iterator++;}
154 
156  void setStep(unsigned int n) {_step=n; advance(n);}
157 
159  void advance(unsigned int n) {
160  if (n > _list.size()) return;
161  _iterator = _iterator+n;
162  //while (_iterator < _list._fullSize && counter < n) {
163  //_iterator++;
164  //if (_list._activeFacets[_iterator] == true) {
165  //counter++;
166  //}
167  //}
168  }
169 
171  bool end() const {return (_iterator==_list.size());}
172 
174  const GEOMETRIC_OBJECT current() {
176  if (_iterator < _list.size())
177  return _list[_iterator];
178  else
179  return GEOMETRIC_OBJECT();
180  }
181 
183  int currentIteratorNumber() const {return _iterator;}
184 
185  protected:
187  unsigned int _iterator;
191  unsigned int _step;
192 };
193 
195 template< class GEOMETRIC_OBJECT > class lexIteratorOfListOfGeometricObjects {
196 
197  public:
200  _iterator(0),_list(l),_alreadySorted(false),_step(0)
201  {}
202 
204  void begin() {_iterator=_step;}
205 
207  void next() {_iterator++;}
208 
210  void setStep(unsigned int n) {_step=n;}
211 
213  bool end() const {return (_iterator==_list.size());}
214 
216  const GEOMETRIC_OBJECT current() {
217  if (_iterator < _list.size())
218  return _list[_iterator];
219  else
220  return GEOMETRIC_OBJECT();
221  }
222 
224  int currentIteratorNumber() const {return _iterator;}
225 
226  protected:
228  unsigned int _iterator;
234  unsigned int _step;
235 
236 };
237 
239 template< class GEOMETRIC_OBJECT > class lexminIteratorOfListOfGeometricObjects :
240  public lexIteratorOfListOfGeometricObjects< GEOMETRIC_OBJECT > {
241 
242  public:
245  lexIteratorOfListOfGeometricObjects< GEOMETRIC_OBJECT >(l) {}
246 
248  void setStep(unsigned int n) {
249  this->_step = n;
250  this->_iterator = 0;
251  if (this->_alreadySorted == false) {
252  this->_list.lexminSort(this->_step);
253  this->_alreadySorted = true;
254  }
255  this->_iterator = this->_step;
256  }
257 
259  void begin() {this->_iterator = this->_step;}
260 
261 };
262 
264 template< class GEOMETRIC_OBJECT > class lexmaxIteratorOfListOfGeometricObjects :
265  public lexIteratorOfListOfGeometricObjects< GEOMETRIC_OBJECT > {
266 
267  public:
270  lexIteratorOfListOfGeometricObjects< GEOMETRIC_OBJECT >(l) {}
271 
273  void setStep(unsigned int n) {
274  this->_step = n;
275  this->_iterator = 0;
276  if (this->_alreadySorted == false) {
277  this->_list.lexmaxSort(this->_step);
278  this->_alreadySorted = true;
279  }
280  this->_iterator = this->_step;
281  }
282 
284  void begin() {this->_iterator = this->_step;}
285 
286 };
287 
288 
289 #endif // GEOMETRIC_OBJECT_ITERATOR_Rn
void negate()
Multiply all generators or half-spaces by -1.
listOfGeometricObjects< GEOMETRIC_OBJECT > & _list
The actual list of geometric elements.
void removeGeometricObjects(const std::set< GEOMETRIC_OBJECT > &setToRemove)
Get rid of all the objects stored in the set.
void lexminSort(unsigned int step)
void begin()
Move the iterator at the beginning of the list.
unsigned int size() const
Get the total number of genuine facets.
const listOfGeometricObjects< GEOMETRIC_OBJECT > & _list
The actual list of geometric elements.
int currentIteratorNumber() const
Return the current position in the list.
void setStep(unsigned int n)
Step forward in the list geometric elements.
bool empty() const
Check whether the set is empty or not.
const GEOMETRIC_OBJECT current()
Return the current geometric element.
void lexmaxSort(unsigned int step)
void setStep(unsigned int n)
Move the iterator at the beginning of the list.
unsigned int _iterator
The current position in the list.
unsigned int find(const GEOMETRIC_OBJECT &GO) const
Find a given object in list..
void begin()
Move the iterator at the beginning of the list.
lexIteratorOfListOfGeometricObjects(listOfGeometricObjects< GEOMETRIC_OBJECT > &l)
Constructor.
bool end() const
Tell whether we have reached the end of the list.
void begin()
Move the iterator at the beginning of the list.
lexmaxIteratorOfListOfGeometricObjects(listOfGeometricObjects< GEOMETRIC_OBJECT > &l)
Constructor.
Insert the half-spaces in the list in a lexicographically order, whether min or max.
bool end() const
Tell whether we have reached the end of the list.
void removeGeometricObject(unsigned int j)
Remove the geometric object number j from the list.
void setStep(unsigned int n)
Step forward in the list geometric elements.
void assign(const listOfGeometricObjects< GEOMETRIC_OBJECT > &listOfGN)
Copies all elements from listOfGN to _GN.
Insert the half-spaces in the list in lexicographically increasing order.
static bool inferior(const GEOMETRIC_OBJECT &HS1, const GEOMETRIC_OBJECT &HS2)
Tell whether a given object is declared inferior to another one.
void setStep(unsigned int n)
Step forward in the list geometric elements.
bool _alreadySorted
Not to do the same job twice.
Insert the half-spaces in the list in lexicographically decreasing order.
This class is designed to run the list of all geometric objects representing a polytope.
void advance(unsigned int n)
Step forward in the list geometric elements.
std::vector< GEOMETRIC_OBJECT > _GO
The full list of half spaces or generators for example.
void next()
Move the iterator one step forward.
This class is designed to contain the list of all generators or half-spaces representing a polytope o...
static bool superior(const GEOMETRIC_OBJECT &HS1, const GEOMETRIC_OBJECT &HS2)
The opposite of the function inferior(HS1, HS2)
constIteratorOfListOfGeometricObjects(const listOfGeometricObjects< GEOMETRIC_OBJECT > &l)
Constructor.
const GEOMETRIC_OBJECT current()
Return the current geometric element.
void begin()
Move the iterator at the beginning of the list.
void clear()
Clear the whole list.
void operator=(const listOfGeometricObjects< GEOMETRIC_OBJECT > &listOfGN)
Copies all elements from listOfGN to _GN.
void push_back(const GEOMETRIC_OBJECT &gn)
Include a new half space in the list.
unsigned int _iterator
The current position in the list.
void next()
Move the iterator one step forward.
lexminIteratorOfListOfGeometricObjects(listOfGeometricObjects< GEOMETRIC_OBJECT > &l)
Constructor.
int currentIteratorNumber() const
Return the current position in the list.