Main MRPT website > C++ reference
MRPT logo
CSetOfLines.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 
29 #ifndef opengl_CSetOfLines_H
30 #define opengl_CSetOfLines_H
31 
35 
36 namespace mrpt
37 {
38  namespace opengl
39  {
43 
44  // This must be added to any CSerializable derived class:
46 
47  /** A set of independent lines (or segments), one line with its own start and end positions (X,Y,Z).
48  * \sa opengl::COpenGLScene
49  *
50  * <div align="center">
51  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
52  * <tr> <td> mrpt::opengl::CSetOfLines </td> <td> \image html preview_CSetOfLines.png </td> </tr>
53  * </table>
54  * </div>
55  *
56  * \ingroup mrpt_opengl_grp
57  */
59  {
61  protected:
62  std::vector<TSegment3D> mSegments;
63  float mLineWidth;
64 
65  public:
66  /**
67  * Clear the list of segments
68  */
69  inline void clear() {
70  mSegments.clear();
72  }
73  /**
74  * Sets the width with which lines will be drawn.
75  */
76  inline void setLineWidth(float w) {
77  mLineWidth=w;
79  }
80  /**
81  * Gets the width with which lines are drawn.
82  */
83  float getLineWidth() const {
84  return mLineWidth;
85  }
86  /**
87  * Appends a line to the set.
88  */
89  inline void appendLine(const mrpt::math::TSegment3D &sgm) {
90  mSegments.push_back(sgm);
92  }
93  /**
94  * Appends a line to the set, given the coordinates of its bounds.
95  */
96  inline void appendLine(float x0,float y0,float z0,float x1,float y1,float z1) {
97  appendLine(TSegment3D(TPoint3D(x0,y0,z0),TPoint3D(x1,y1,z1)));
99  }
100  /**
101  * Appends any iterable collection of lines to the set. Note that this includes another CSetOfLines.
102  * \sa appendLine
103  */
104  template<class T> inline void appendLines(const T &sgms) {
105  mSegments.insert(mSegments.end(),sgms.begin(),sgms.end());
107  }
108  /**
109  * Appends certain amount of lines, located between two iterators, into the set.
110  * \sa appendLine
111  */
112  template<class T_it> inline void appendLines(const T_it &begin,const T_it &end) {
113  mSegments.reserve(mSegments.size()+(end-begin));
114  mSegments.insert(mSegments.end(),begin,end);
116  }
117  /**
118  * Resizes the set.
119  * \sa reserve
120  */
121  void resize(size_t nLines) {
122  mSegments.resize(nLines);
124  }
125  /**
126  * Reserves an amount of lines to the set. This method should be used when some known amount of lines is going to be inserted, so that only a memory allocation is needed.
127  * \sa resize
128  */
129  void reserve(size_t r) {
130  mSegments.reserve(r);
132  }
133  /**
134  * Inserts a line, given its bounds. Works with any pair of objects with access to x, y and z members.
135  */
136  template<class T,class U> inline void appendLine(T p0,U p1) {
137  appendLine(p0.x,p0.y,p0.z,p1.x,p1.y,p1.z);
139  }
140  /**
141  * Returns the total count of lines in this set.
142  */
143  inline size_t getLineCount() const {
144  return mSegments.size();
145  }
146  /**
147  * Sets a specific line in the set, given its index.
148  * \sa appendLine
149  */
150  void setLineByIndex(size_t index,const TSegment3D &segm);
151  /**
152  * Sets a specific line in the set, given its index.
153  * \sa appendLine
154  */
155  inline void setLineByIndex(size_t index,double x0,double y0,double z0,double x1,double y1,double z1) {
156  setLineByIndex(index,TSegment3D(TPoint3D(x0,y0,z0),TPoint3D(x1,y1,z1)));
158  }
159  /**
160  * Gets a specific line in the set, given its index.
161  * \sa getLineByIndex
162  */
163  inline void getLineByIndex(size_t index,double &x0,double &y0,double &z0,double &x1,double &y1,double &z1) const {
164  ASSERT_(index<mSegments.size())
165  x0 = mSegments[index].point1.x;
166  y0 = mSegments[index].point1.y;
167  z0 = mSegments[index].point1.z;
168  x1 = mSegments[index].point2.x;
169  y1 = mSegments[index].point2.y;
170  z1 = mSegments[index].point2.z;
171  }
172  /**
173  * Class factory
174  */
175  inline static CSetOfLinesPtr Create(const std::vector<TSegment3D> &sgms) {
176  return CSetOfLinesPtr(new CSetOfLines(sgms));
177  }
178  /** Render
179  */
180  void render_dl() const;
181 
182  //Iterator management
183  typedef std::vector<TSegment3D>::iterator iterator; //!< Iterator to the set.
184  typedef std::vector<TSegment3D>::reverse_iterator reverse_iterator; //!< Iterator to the set.
185 
186  /**
187  * Const iterator to the set.
188  */
190  /**
191  * Const reverse iterator to the set.
192  */
193  typedef std::vector<TSegment3D>::const_reverse_iterator const_reverse_iterator;
194  /**
195  * Beginning const iterator.
196  * \sa end,rbegin,rend
197  */
198  inline const_iterator begin() const {
199  return mSegments.begin();
200  }
201  inline iterator begin() { CRenderizableDisplayList::notifyChange(); return mSegments.begin(); }
202  /**
203  * Ending const iterator.
204  * \sa begin,rend,rbegin
205  */
206  inline const_iterator end() const {
207  return mSegments.end();
208  }
209  inline iterator end() { CRenderizableDisplayList::notifyChange(); return mSegments.end(); }
210  /**
211  * Beginning const reverse iterator (actually, accesses the end of the set).
212  * \sa rend,begin,end
213  */
214  inline const_reverse_iterator rbegin() const {
215  return mSegments.rbegin();
216  }
217  /**
218  * Ending const reverse iterator (actually, refers to the starting point of the set).
219  * \sa rbegin,end,begin
220  */
221  inline const_reverse_iterator rend() const {
222  return mSegments.rend();
223  }
224 
225  private:
226  /** Constructor
227  */
228  CSetOfLines():mSegments(),mLineWidth(1.0) {}
229  /**
230  * Constructor with a initial set of lines.
231  */
232  CSetOfLines(const std::vector<TSegment3D> &sgms):mSegments(sgms),mLineWidth(1.0) {}
233  /**
234  * Private, virtual destructor: only can be deleted from smart pointers.
235  */
236  virtual ~CSetOfLines() { }
237  };
238  /** Inserts a set of segments into the list. Allows call chaining.
239  * \sa mrpt::opengl::CSetOfLines::appendLines
240  */
241  template<class T> inline CSetOfLinesPtr &operator<<(CSetOfLinesPtr &l,const T &s) {
242  l->appendLines(s.begin(),s.end());
243  return l;
244  }
245  /** Inserts a segment into the list. Allows call chaining.
246  * \sa mrpt::opengl::CSetOfLines::appendLine(const TSegment &)
247  */
249  l->appendLine(s);
250  return l;
251  }
252  } // end namespace
253 
254 } // End of namespace
255 
256 
257 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013