Main MRPT website > C++ reference
MRPT logo

CSetOfLines.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 
00029 #ifndef opengl_CSetOfLines_H
00030 #define opengl_CSetOfLines_H
00031 
00032 #include <mrpt/opengl/CRenderizableDisplayList.h>
00033 #include <mrpt/math/lightweight_geom_data.h>
00034 #include <mrpt/utils/stl_extensions.h>
00035 
00036 namespace mrpt
00037 {
00038         namespace opengl
00039         {
00040                 using mrpt::math::TPoint3D;
00041                 using mrpt::math::TSegment3D;
00042                 class OPENGL_IMPEXP CSetOfLines;
00043 
00044                 // This must be added to any CSerializable derived class:
00045                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CSetOfLines, CRenderizableDisplayList, OPENGL_IMPEXP )
00046 
00047                 /** A set of independent lines (or segments), one line with its own start and end positions (X,Y,Z).
00048                   *  \sa opengl::COpenGLScene
00049                   *  
00050                   *  <div align="center">
00051                   *  <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
00052                   *   <tr> <td> mrpt::opengl::CSetOfLines </td> <td> \image html preview_CSetOfLines.png </td> </tr>
00053                   *  </table>
00054                   *  </div>
00055                   *  
00056                   */
00057                 class OPENGL_IMPEXP CSetOfLines : public CRenderizableDisplayList
00058                 {
00059                         DEFINE_SERIALIZABLE( CSetOfLines )
00060                 protected:
00061                         std::vector<TSegment3D> mSegments;
00062             float                       mLineWidth;
00063 
00064                 public:
00065                         /**
00066                           * Clear the list of segments
00067                           */
00068                         inline void clear()     {
00069                                 mSegments.clear();
00070                                 CRenderizableDisplayList::notifyChange();
00071                         }
00072                         /**
00073                           * Sets the width with which lines will be drawn.
00074                           */
00075                         inline void setLineWidth(float w) {
00076                                 mLineWidth=w;
00077                                 CRenderizableDisplayList::notifyChange();
00078                         }
00079                         /**
00080                           * Gets the width with which lines are drawn.
00081                           */
00082                         float getLineWidth() const {
00083                                 return mLineWidth;
00084                         }
00085                         /**
00086                           * Appends a line to the set.
00087                           */
00088                         inline void appendLine(const mrpt::math::TSegment3D &sgm)       {
00089                                 mSegments.push_back(sgm);
00090                                 CRenderizableDisplayList::notifyChange();
00091                         }
00092                         /**
00093                           * Appends a line to the set, given the coordinates of its bounds.
00094                           */
00095                         inline void appendLine(float x0,float y0,float z0,float x1,float y1,float z1)   {
00096                                 appendLine(TSegment3D(TPoint3D(x0,y0,z0),TPoint3D(x1,y1,z1)));
00097                                 CRenderizableDisplayList::notifyChange();
00098                         }
00099                         /**
00100                           * Appends any iterable collection of lines to the set. Note that this includes another CSetOfLines.
00101                           * \sa appendLine
00102                           */
00103                         template<class T> inline void appendLines(const T &sgms)        {
00104                                 mSegments.insert(mSegments.end(),sgms.begin(),sgms.end());
00105                                 CRenderizableDisplayList::notifyChange();
00106                         }
00107                         /**
00108                           * Appends certain amount of lines, located between two iterators, into the set.
00109                           * \sa appendLine
00110                           */
00111                         template<class T_it> inline void appendLines(const T_it &begin,const T_it &end) {
00112                                 mSegments.reserve(mSegments.size()+(end-begin));
00113                                 mSegments.insert(mSegments.end(),begin,end);
00114                                 CRenderizableDisplayList::notifyChange();
00115                         }
00116                         /**
00117                           * Resizes the set.
00118                           * \sa reserve
00119                           */
00120                         void resize(size_t nLines)      {
00121                                 mSegments.resize(nLines);
00122                                 CRenderizableDisplayList::notifyChange();
00123                         }
00124                         /**
00125                           * 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.
00126                           * \sa resize
00127                           */
00128                         void reserve(size_t r)  {
00129                                 mSegments.reserve(r);
00130                                 CRenderizableDisplayList::notifyChange();
00131                         }
00132                         /**
00133                           * Inserts a line, given its bounds. Works with any pair of objects with access to x, y and z members.
00134                           */
00135                         template<class T,class U> inline void appendLine(T p0,U p1)     {
00136                                 appendLine(p0.x,p0.y,p0.z,p1.x,p1.y,p1.z);
00137                                 CRenderizableDisplayList::notifyChange();
00138                         }
00139                         /**
00140                           * Returns the total count of lines in this set.
00141                           */
00142                         inline size_t getLineCount() const      {
00143                                 return mSegments.size();
00144                         }
00145                         /**
00146                           * Sets a specific line in the set, given its index.
00147                           * \sa appendLine
00148                           */
00149                         void setLineByIndex(size_t index,const TSegment3D &segm);
00150                         /**
00151                           * Sets a specific line in the set, given its index.
00152                           * \sa appendLine
00153                           */
00154                         inline void setLineByIndex(size_t index,double x0,double y0,double z0,double x1,double y1,double z1)    {
00155                                 setLineByIndex(index,TSegment3D(TPoint3D(x0,y0,z0),TPoint3D(x1,y1,z1)));
00156                                 CRenderizableDisplayList::notifyChange();
00157                         }
00158                         /**
00159                           * Gets a specific line in the set, given its index.
00160                           * \sa getLineByIndex
00161                           */
00162                         inline void getLineByIndex(size_t index,double &x0,double &y0,double &z0,double &x1,double &y1,double &z1) const        {
00163                                 ASSERT_(index<mSegments.size())
00164                                 x0 = mSegments[index].point1.x;
00165                                 y0 = mSegments[index].point1.y;
00166                                 z0 = mSegments[index].point1.z;
00167                                 x1 = mSegments[index].point2.x;
00168                                 y1 = mSegments[index].point2.y;
00169                                 z1 = mSegments[index].point2.z;
00170                         }
00171                         /**
00172                           * Class factory
00173                           */
00174                         inline static CSetOfLinesPtr Create(const std::vector<TSegment3D> &sgms)        {
00175                                 return CSetOfLinesPtr(new CSetOfLines(sgms));
00176                         }
00177                         /** Render
00178                           */
00179                         void  render_dl() const;
00180 
00181                         //Iterator management
00182                         typedef std::vector<TSegment3D>::iterator iterator;     //!< Iterator to the set.
00183                         typedef std::vector<TSegment3D>::reverse_iterator reverse_iterator;     //!< Iterator to the set.
00184 
00185                         /**
00186                           * Const iterator to the set.
00187                           */
00188                         typedef std::vector<TSegment3D>::const_iterator const_iterator;
00189                         /**
00190                           * Const reverse iterator to the set.
00191                           */
00192                         typedef std::vector<TSegment3D>::const_reverse_iterator const_reverse_iterator;
00193                         /**
00194                           * Beginning const iterator.
00195                           * \sa end,rbegin,rend
00196                           */
00197                         inline const_iterator begin() const     {
00198                                 return mSegments.begin();
00199                         }
00200                         inline iterator begin() { CRenderizableDisplayList::notifyChange();  return mSegments.begin(); }
00201                         /**
00202                           * Ending const iterator.
00203                           * \sa begin,rend,rbegin
00204                           */
00205                         inline const_iterator end() const       {
00206                                 return mSegments.end();
00207                         }
00208                         inline iterator end() { CRenderizableDisplayList::notifyChange(); return mSegments.end(); }
00209                         /**
00210                           * Beginning const reverse iterator (actually, accesses the end of the set).
00211                           * \sa rend,begin,end
00212                           */
00213                         inline const_reverse_iterator rbegin() const    {
00214                                 return mSegments.rbegin();
00215                         }
00216                         /**
00217                           * Ending const reverse iterator (actually, refers to the starting point of the set).
00218                           * \sa rbegin,end,begin
00219                           */
00220                         inline const_reverse_iterator rend() const      {
00221                                 return mSegments.rend();
00222                         }
00223 
00224                 private:
00225                         /** Constructor
00226                           */
00227                         CSetOfLines():mSegments(),mLineWidth(1.0)       {}
00228                         /**
00229                           * Constructor with a initial set of lines.
00230                           */
00231                         CSetOfLines(const std::vector<TSegment3D> &sgms):mSegments(sgms),mLineWidth(1.0)        {}
00232                         /**
00233                           * Private, virtual destructor: only can be deleted from smart pointers.
00234                           */
00235                         virtual ~CSetOfLines() { }
00236                 };
00237                 /** Inserts a set of segments into the list. Allows call chaining.
00238                   * \sa mrpt::opengl::CSetOfLines::appendLines
00239                   */
00240                 template<class T> inline CSetOfLinesPtr &operator<<(CSetOfLinesPtr &l,const T &s)       {
00241                         l->appendLines(s.begin(),s.end());
00242                         return l;
00243                 }
00244                 /** Inserts a segment into the list. Allows call chaining.
00245                   * \sa mrpt::opengl::CSetOfLines::appendLine(const TSegment &)
00246                   */
00247                 template<> inline CSetOfLinesPtr &operator<<(CSetOfLinesPtr &l,const mrpt::math::TSegment3D &s) {
00248                         l->appendLine(s);
00249                         return l;
00250                 }
00251         } // end namespace
00252 
00253 } // End of namespace
00254 
00255 
00256 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011