LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiGraphicsPattern.h

00001 /*
00002  * Copyright (C) 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiGraphicsPattern.h
00027  * authors ....: Jochen Wickel
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 05.02.03
00030  * revisions ..: $Id: ltiGraphicsPattern.h,v 1.5 2006/02/08 11:13:29 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_GRAPHICSPATTERN_H_
00034 #define _LTI_GRAPHICSPATTERN_H_
00035 
00036 #include "ltiIoObject.h"
00037 #include "ltiRectangle.h"
00038 #include "ltiVector.h"
00039 #include "ltiPolygonPoints.h"
00040 #include "ltiPointList.h"
00041 #include "ltiMatrix.h"
00042 
00043 namespace lti {
00044 
00045   /**
00046    * graphicsPattern template class. It is the base class for all
00047    * drawing patterns. The actual content of such a pattern, and if
00048    * it is designed for lines or filled shapes, is defined in the
00049    * subclass.
00050    * Common property of all patterns include its bounding box,
00051    * which is a rectangle that encloses all objects.
00052    * Patterns may be binary, monochromatic, or colored.
00053    * These classes are used by the drawer classes.
00054    */
00055   class graphicsPattern : public ioObject {
00056   public:
00057 
00058     /**
00059      * A comparator used for sorting patterns
00060      */
00061     struct less {
00062       bool operator()(const graphicsPattern& a, const graphicsPattern& b) const {
00063         return a.compareTo(b) < 0;
00064       }
00065     };
00066 
00067 
00068     struct pless {
00069       bool operator()(const graphicsPattern* a, const graphicsPattern* b) const {
00070         return a->compareTo(*b) < 0;
00071       }
00072     };
00073 
00074     /**
00075      * default constructor creates an empty pointList;
00076      */
00077     graphicsPattern();
00078 
00079     /**
00080      * destructor
00081      */
00082     virtual ~graphicsPattern();
00083 
00084     /**
00085      * returns the name of this class: graphicsPattern
00086      */
00087     const char* getTypeName() const {return "graphicsPattern";};
00088 
00089     /**
00090      * Resets this pattern to its default state.
00091      */
00092     virtual void clear()=0;
00093 
00094 
00095     /**
00096      * assigment operator.
00097      * copy the contents of <code>other</code> in this %object.
00098      * @param other the source pointList to be copied.
00099      */
00100     graphicsPattern& copy(const graphicsPattern& other);
00101 
00102     /**
00103      * assigment operator (alias for copy(other)).
00104      * @param other the pointList to be copied
00105      * @return a reference to the actual pointList
00106      */
00107     graphicsPattern& operator=(const graphicsPattern& other);
00108 
00109     /**
00110      * comparison operator (alias for compareTo(other) == 0).
00111      * @param other the pattern to be compared
00112      * @return true if both are equal
00113      */
00114     bool operator==(const graphicsPattern& other) const {
00115       return compareTo(other) == 0;
00116     }
00117 
00118     /**
00119      * comparison operator (alias for compareTo(other) != 0).
00120      * @param other the pattern to be compared
00121      * @return true if both are unequal
00122      */
00123     bool operator!=(const graphicsPattern& other) const {
00124       return compareTo(other) != 0;
00125     }
00126 
00127     /**
00128      * create a clone of this pointList
00129      * @return a pointer to a copy of this pointList
00130      */
00131     virtual object* clone() const=0;
00132 
00133     virtual int compareTo(const graphicsPattern& other) const=0;
00134 
00135     /**
00136      * Returns the bounding box of this pattern.
00137      */
00138     virtual rectangle getBoundingBox() const=0;
00139 
00140     /**
00141      * write the pattern to the given ioHandler
00142      */
00143     virtual bool write(ioHandler& handler,const bool complete = true) const;
00144 
00145     /**
00146      * read the pattern from the given ioHandler
00147      */
00148     virtual bool read(ioHandler& handler,const bool complete = true);
00149 
00150   };
00151 
00152   /**
00153    * Line pattern class. A line pattern is simply an array
00154    * of T, encoding "on" and "off", and an offset, which determines
00155    * the start.
00156    * Example:
00157    * \code
00158    * int p1[4]={2,3,1,2};
00159    * ivector p(4,p1);
00160    * int offset=0;
00161    * linePattern<int> pat(p,offset);
00162    * \endcode
00163    * Then applying this pattern to a line drawing will result in
00164    * the following line:
00165    * \code
00166    * **   *  **   *  **   *  ** ...
00167    * \endcode
00168    */
00169   class linePattern: public graphicsPattern {
00170 
00171   public:
00172 
00173     /**
00174      * Default constructor. Creates a pattern for a continous line.
00175      */
00176     linePattern();
00177 
00178     /**
00179      * Constructor. Constructs a line pattern with the given
00180      * dash intervals and the given offset.
00181      * @param d vector containing the dash definition
00182      * @param o the dash offset
00183      */
00184     linePattern(const ivector& d, int o);
00185 
00186     /**
00187      * Copy constructor.
00188      */
00189     linePattern(const linePattern& o);
00190 
00191     /**
00192      * Destructor.
00193      */
00194     virtual ~linePattern();
00195 
00196     /**
00197      * Returns the bounding box of this pattern. This is not really
00198      * useful except that the returned rectangle's width gives the
00199      * length of one pattern.
00200      */
00201     virtual rectangle getBoundingBox() const;
00202 
00203     /**
00204      * This is the main application function for pixel-base draw functors.
00205      * The method should be
00206      * called with an argument denoting the number of pixel that
00207      * is supposed to be drawn. The method then returns true
00208      * if the pixel is permitted by the pattern, or not.
00209      * @param i the index of the pixel to be drawn.
00210      */
00211     inline bool isSet(int i) const {
00212       return mask.at(i%length);
00213     }
00214 
00215     /**
00216      * Vector-oriented draw functors can use this method to obtain the
00217      * dash definition.
00218      * @param d here the dash definition is  returned
00219      * @param o will receive the offset of the pattern
00220      */
00221     void getDash(ivector& d, int& o) const {
00222       d=dash;
00223       o=offset;
00224     }
00225 
00226     /**
00227      * assigment operator.
00228      * copy the contents of <code>other</code> in this %object.
00229      * @param other the source pointList to be copied.
00230      */
00231     linePattern& copy(const linePattern& other);
00232 
00233     /**
00234      * assigment operator (alias for copy(other)).
00235      * @param other the pointList to be copied
00236      * @return a reference to the actual pointList
00237      */
00238     inline linePattern& operator=(const linePattern& other) {
00239       return copy(other);
00240     }
00241 
00242     virtual object* clone() const;
00243 
00244     virtual int compareTo(const graphicsPattern& other) const;
00245 
00246     virtual void clear();
00247 
00248     /**
00249      * write the pattern to the given ioHandler
00250      */
00251     virtual bool write(ioHandler& handler,const bool complete = true) const;
00252 
00253     /**
00254      * read the pattern from the given ioHandler
00255      */
00256     virtual bool read(ioHandler& handler,const bool complete = true);
00257 
00258     /**
00259      * This method returns a line pattern suitable for dashed lines.
00260      * @param d the width of the intervals.
00261      */
00262     static linePattern* createDashed(int d);
00263 
00264   protected:
00265     ivector dash;
00266     genericVector<bool> mask;
00267     int length;
00268     int offset;
00269   };
00270 
00271   /**
00272    * Fill pattern class. A fill pattern is defined by a set of
00273    * graphical primitives. At present, the only supported
00274    * primitives are borderPoints, areaPoints, and polygonPoints.
00275    *
00276    */
00277   class fillPattern: public graphicsPattern {
00278 
00279   public:
00280     /**
00281      * The type of mask objects that should be used. At present,
00282      * only polygon is supported.
00283      */
00284     typedef enum {
00285       border,
00286       polygon
00287     } contourType;
00288 
00289     typedef tpointList<float> basicObject;
00290 
00291     /**
00292      * The representation of a mask in vector form. The list
00293      * contains pointers to point list objects (maybe different
00294      * subtypes of pointlist). Once added to the mask, the
00295      * objects must not be modified.
00296      */
00297     typedef std::list<const basicObject*> objectList;
00298 
00299 
00300     /**
00301      * Default constructor. This will create a homogeneous area.
00302      */
00303     fillPattern();
00304 
00305     /**
00306      * Copy constructor.
00307      */
00308     fillPattern(const fillPattern& other);
00309 
00310     /**
00311      * Constructor. The given pointList is cast to the type given
00312      * as argument.
00313      *
00314      * @param base the base object of the pattern.
00315      * @param type the type of created contour. If you choose, e.g.
00316      *        Polygon, then all objects that are added get converted to
00317      *        polygons.
00318      * @param scale
00319      */
00320     fillPattern(const basicObject& base, contourType type, float scale=1.0);
00321 
00322     /**
00323      * Destructor.
00324      */
00325     virtual ~fillPattern();
00326 
00327     /**
00328      * Adds a contour to the pattern. The given contour gets converted
00329      * to the type defined in the constructor.
00330      */
00331     void add(const basicObject& o);
00332 
00333     /**
00334      * Sets the pattern mask explicitly. The mask is only used
00335      * for pixel-based drawing where the vector format usually
00336      * yields crap.
00337      */
00338     template <class T>
00339     void setMask(const matrix<T>& m) {
00340       mask.castFrom(m);
00341       pixeldim =mask.size();
00342     }
00343 
00344     // void intersect(const fillPattern& fp);
00345 
00346     // void unite(const fillPattern& fp);
00347 
00348     /**
00349      * Returns true if the given point is allowed by this pattern
00350      * mask.
00351      */
00352     bool isSet(int x,int y) {
00353       return (mask.at(y%pixeldim.y,x%pixeldim.x) != 0);
00354     }
00355 
00356     /**
00357      * Returns true if the given point is allowed by this pattern
00358      * mask.
00359      */
00360     bool isSet(const point& p) {
00361       return isSet(p.x,p.y);
00362     }
00363 
00364     /**
00365      * Returns the list of objects that constitute the pattern.
00366      */
00367     const objectList& getObjects() const {
00368       return objects;
00369     }
00370 
00371     /**
00372      * Returns the bounding box of this pattern.
00373      */
00374     virtual rectangle getBoundingBox() const;
00375 
00376     /**
00377      * assigment operator.
00378      * copy the contents of <code>other</code> in this %object.
00379      * @param other the source basicObject to be copied.
00380      */
00381     fillPattern& copy(const fillPattern& other);
00382 
00383     /**
00384      * assigment operator (alias for copy(other)).
00385      * @param other the basicObject to be copied
00386      * @return a reference to the actual basicObject
00387      */
00388     inline fillPattern& operator=(const fillPattern& other) {
00389       return copy(other);
00390     }
00391 
00392     /**
00393      * create a clone of this basicObject
00394      * @return a pointer to a copy of this basicObject
00395      */
00396     virtual object* clone() const;
00397 
00398     virtual int compareTo(const graphicsPattern& other) const;
00399 
00400     virtual void clear();
00401 
00402     /**
00403      * write the pattern to the given ioHandler
00404      */
00405     virtual bool write(ioHandler& handler,const bool complete = true) const;
00406 
00407     /**
00408      * read the pattern from the given ioHandler
00409      */
00410     virtual bool read(ioHandler& handler,const bool complete = true);
00411 
00412     /**
00413      * Static method to create a hatch pattern. dx and dy
00414      * determine two things: The width and height of the returned pattern,
00415      * and also the slope of the lines. There is only one line per
00416      * pattern.
00417      * Examples:
00418      * \code
00419      * createHatching(2,2,1);
00420      * \endcode
00421      * will create a hatching that looks about:
00422      * \code
00423      *  *
00424      * *
00425      * \endcode
00426      * whereas
00427      * \code
00428      * createHatching(2,-2,1);
00429      * \endcode
00430      * will create a hatching that looks like:
00431      * \code
00432      * *
00433      *  *
00434      * \endcode
00435      * A more complex example:
00436      * \code
00437      * createHatching(16,4,2);
00438      *         ********
00439      *     ********
00440      * ********
00441      * ****        ****
00442      * \endcode
00443      */
00444     static fillPattern* createHatching(int dx, int dy, int w);
00445 
00446     static fillPattern* createCrossHatching(int dx, int dy, int w);
00447 
00448     typedef tpoint<float> pointType;
00449     typedef tpolygonPoints<float> internalObject;
00450 
00451 
00452   protected:
00453     /**
00454      * Updates the pixel mask from the list of mask objects.
00455      * Also recomputes the bounding box.
00456      */
00457     void updateMask();
00458 
00459     /**
00460      * Draws a filled polygon into the given mask
00461      */
00462     template <class T>
00463     void drawObject(matrix<T>& pane, const internalObject& p);
00464 
00465     rectangle bb;
00466     point dim;
00467     point pixeldim;
00468     objectList objects;
00469     // matrix for pixel-based drawing
00470     matrix<ubyte> mask;
00471   };
00472 
00473 
00474 
00475   /**
00476    * read the vector from the given ioHandler.  The complete flag indicates
00477    * if the enclosing begin and end should be also be readed
00478    *
00479    * @ingroup gStorable
00480    */
00481   bool read(ioHandler& handler,graphicsPattern& plst,
00482             const bool complete=true);
00483 
00484   /**
00485    * write the vector in the given ioHandler.  The complete flag indicates
00486    * if the enclosing begin and end should be also be written or not
00487    *
00488    * @ingroup gStorable
00489    */
00490   bool write(ioHandler& handler, const graphicsPattern& plst,
00491              const bool complete=true);
00492 
00493 
00494 } // namespace lti
00495 
00496 namespace std {
00497 
00498   std::ostream& operator<<(std::ostream& s,const lti::graphicsPattern& pts);
00499 }
00500 
00501 #endif

Generated on Sat Apr 10 15:25:36 2010 for LTI-Lib by Doxygen 1.6.1