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://www.mrpt.org/                                |
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                   * \ingroup mrpt_opengl_grp
00057                   */
00058                 class OPENGL_IMPEXP CSetOfLines : public CRenderizableDisplayList
00059                 {
00060                         DEFINE_SERIALIZABLE( CSetOfLines )
00061                 protected:
00062                         std::vector<TSegment3D> mSegments;
00063             float                       mLineWidth;
00064 
00065                 public:
00066                         /**
00067                           * Clear the list of segments
00068                           */
00069                         inline void clear()     {
00070                                 mSegments.clear();
00071                                 CRenderizableDisplayList::notifyChange();
00072                         }
00073                         /**
00074                           * Sets the width with which lines will be drawn.
00075                           */
00076                         inline void setLineWidth(float w) {
00077                                 mLineWidth=w;
00078                                 CRenderizableDisplayList::notifyChange();
00079                         }
00080                         /**
00081                           * Gets the width with which lines are drawn.
00082                           */
00083                         float getLineWidth() const {
00084                                 return mLineWidth;
00085                         }
00086                         /**
00087                           * Appends a line to the set.
00088                           */
00089                         inline void appendLine(const mrpt::math::TSegment3D &sgm)       {
00090                                 mSegments.push_back(sgm);
00091                                 CRenderizableDisplayList::notifyChange();
00092                         }
00093                         /**
00094                           * Appends a line to the set, given the coordinates of its bounds.
00095                           */
00096                         inline void appendLine(float x0,float y0,float z0,float x1,float y1,float z1)   {
00097                                 appendLine(TSegment3D(TPoint3D(x0,y0,z0),TPoint3D(x1,y1,z1)));
00098                                 CRenderizableDisplayList::notifyChange();
00099                         }
00100                         /**
00101                           * Appends any iterable collection of lines to the set. Note that this includes another CSetOfLines.
00102                           * \sa appendLine
00103                           */
00104                         template<class T> inline void appendLines(const T &sgms)        {
00105                                 mSegments.insert(mSegments.end(),sgms.begin(),sgms.end());
00106                                 CRenderizableDisplayList::notifyChange();
00107                         }
00108                         /**
00109                           * Appends certain amount of lines, located between two iterators, into the set.
00110                           * \sa appendLine
00111                           */
00112                         template<class T_it> inline void appendLines(const T_it &begin,const T_it &end) {
00113                                 mSegments.reserve(mSegments.size()+(end-begin));
00114                                 mSegments.insert(mSegments.end(),begin,end);
00115                                 CRenderizableDisplayList::notifyChange();
00116                         }
00117                         /**
00118                           * Resizes the set.
00119                           * \sa reserve
00120                           */
00121                         void resize(size_t nLines)      {
00122                                 mSegments.resize(nLines);
00123                                 CRenderizableDisplayList::notifyChange();
00124                         }
00125                         /**
00126                           * 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.
00127                           * \sa resize
00128                           */
00129                         void reserve(size_t r)  {
00130                                 mSegments.reserve(r);
00131                                 CRenderizableDisplayList::notifyChange();
00132                         }
00133                         /**
00134                           * Inserts a line, given its bounds. Works with any pair of objects with access to x, y and z members.
00135                           */
00136                         template<class T,class U> inline void appendLine(T p0,U p1)     {
00137                                 appendLine(p0.x,p0.y,p0.z,p1.x,p1.y,p1.z);
00138                                 CRenderizableDisplayList::notifyChange();
00139                         }
00140                         /**
00141                           * Returns the total count of lines in this set.
00142                           */
00143                         inline size_t getLineCount() const      {
00144                                 return mSegments.size();
00145                         }
00146                         /**
00147                           * Sets a specific line in the set, given its index.
00148                           * \sa appendLine
00149                           */
00150                         void setLineByIndex(size_t index,const TSegment3D &segm);
00151                         /**
00152                           * Sets a specific line in the set, given its index.
00153                           * \sa appendLine
00154                           */
00155                         inline void setLineByIndex(size_t index,double x0,double y0,double z0,double x1,double y1,double z1)    {
00156                                 setLineByIndex(index,TSegment3D(TPoint3D(x0,y0,z0),TPoint3D(x1,y1,z1)));
00157                                 CRenderizableDisplayList::notifyChange();
00158                         }
00159                         /**
00160                           * Gets a specific line in the set, given its index.
00161                           * \sa getLineByIndex
00162                           */
00163                         inline void getLineByIndex(size_t index,double &x0,double &y0,double &z0,double &x1,double &y1,double &z1) const        {
00164                                 ASSERT_(index<mSegments.size())
00165                                 x0 = mSegments[index].point1.x;
00166                                 y0 = mSegments[index].point1.y;
00167                                 z0 = mSegments[index].point1.z;
00168                                 x1 = mSegments[index].point2.x;
00169                                 y1 = mSegments[index].point2.y;
00170                                 z1 = mSegments[index].point2.z;
00171                         }
00172                         /**
00173                           * Class factory
00174                           */
00175                         inline static CSetOfLinesPtr Create(const std::vector<TSegment3D> &sgms)        {
00176                                 return CSetOfLinesPtr(new CSetOfLines(sgms));
00177                         }
00178                         /** Render
00179                           */
00180                         void  render_dl() const;
00181 
00182                         //Iterator management
00183                         typedef std::vector<TSegment3D>::iterator iterator;     //!< Iterator to the set.
00184                         typedef std::vector<TSegment3D>::reverse_iterator reverse_iterator;     //!< Iterator to the set.
00185 
00186                         /**
00187                           * Const iterator to the set.
00188                           */
00189                         typedef std::vector<TSegment3D>::const_iterator const_iterator;
00190                         /**
00191                           * Const reverse iterator to the set.
00192                           */
00193                         typedef std::vector<TSegment3D>::const_reverse_iterator const_reverse_iterator;
00194                         /**
00195                           * Beginning const iterator.
00196                           * \sa end,rbegin,rend
00197                           */
00198                         inline const_iterator begin() const     {
00199                                 return mSegments.begin();
00200                         }
00201                         inline iterator begin() { CRenderizableDisplayList::notifyChange();  return mSegments.begin(); }
00202                         /**
00203                           * Ending const iterator.
00204                           * \sa begin,rend,rbegin
00205                           */
00206                         inline const_iterator end() const       {
00207                                 return mSegments.end();
00208                         }
00209                         inline iterator end() { CRenderizableDisplayList::notifyChange(); return mSegments.end(); }
00210                         /**
00211                           * Beginning const reverse iterator (actually, accesses the end of the set).
00212                           * \sa rend,begin,end
00213                           */
00214                         inline const_reverse_iterator rbegin() const    {
00215                                 return mSegments.rbegin();
00216                         }
00217                         /**
00218                           * Ending const reverse iterator (actually, refers to the starting point of the set).
00219                           * \sa rbegin,end,begin
00220                           */
00221                         inline const_reverse_iterator rend() const      {
00222                                 return mSegments.rend();
00223                         }
00224 
00225                 private:
00226                         /** Constructor
00227                           */
00228                         CSetOfLines():mSegments(),mLineWidth(1.0)       {}
00229                         /**
00230                           * Constructor with a initial set of lines.
00231                           */
00232                         CSetOfLines(const std::vector<TSegment3D> &sgms):mSegments(sgms),mLineWidth(1.0)        {}
00233                         /**
00234                           * Private, virtual destructor: only can be deleted from smart pointers.
00235                           */
00236                         virtual ~CSetOfLines() { }
00237                 };
00238                 /** Inserts a set of segments into the list. Allows call chaining.
00239                   * \sa mrpt::opengl::CSetOfLines::appendLines
00240                   */
00241                 template<class T> inline CSetOfLinesPtr &operator<<(CSetOfLinesPtr &l,const T &s)       {
00242                         l->appendLines(s.begin(),s.end());
00243                         return l;
00244                 }
00245                 /** Inserts a segment into the list. Allows call chaining.
00246                   * \sa mrpt::opengl::CSetOfLines::appendLine(const TSegment &)
00247                   */
00248                 template<> inline CSetOfLinesPtr &operator<<(CSetOfLinesPtr &l,const mrpt::math::TSegment3D &s) {
00249                         l->appendLine(s);
00250                         return l;
00251                 }
00252         } // end namespace
00253 
00254 } // End of namespace
00255 
00256 
00257 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011