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

ltiDraw2DDistribution.h

00001 /*
00002  * Copyright (C) 2002, 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-Lib: Image Processing and Computer Vision Library
00026  * file .......: ltiDraw2DDistribution.h
00027  * authors ....: Jens Paustenbach
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 16.9.2002
00030  * revisions ..: $Id: ltiDraw2DDistribution.h,v 1.10 2006/02/07 18:46:29 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_DRAW2_D_DISTRIBUTION_H_
00034 #define _LTI_DRAW2_D_DISTRIBUTION_H_
00035 
00036 
00037 #include "ltiFunctor.h"
00038 #include "ltiMatrix.h"
00039 #include "ltiDrawBase.h"
00040 #include <map>
00041 #include <vector>
00042 #include "ltiBoundsFunctor.h"
00043 #include "ltiSTLIoInterface.h"
00044 #include "ltiTypeInfo.h"
00045 
00046 namespace lti {
00047   /**
00048    *  This class draws a 2-D distribution into an image or a channel.
00049    *
00050    *  <B>If you want to draw the distribution into an eps picture it
00051    *  is mandatory to specify a size for the eps picture.</B> For example:
00052    *  \code
00053    *  epsdraw<cmykColor> drawer(std::out,point(800,800));
00054    *  \endcode
00055    *
00056    *  The apply methods draws each point from the given matrices into the
00057    *  given image.
00058    *  It is expected that each row of the matrix represents one point
00059    *  If there is also a vector with ids corresponding to each point in
00060    *  the matrices each id is drawn in an other color or style.
00061    *  First all colors are permuted. (In channels this is only white).
00062    *  After this, if there are more ids than colors available, the style
00063    *  of the points is permuted. If there is no id-vector you can assign
00064    *  a color an the style that will be used.
00065    *  According to the parameter hold the old points that are drawn before
00066    *  are saved. If an instance of this class is used later the old points
00067    *  that were saved will appear to in the image.
00068    */
00069   template<class T>
00070   class draw2DDistribution : public functor {
00071   public:
00072 
00073     /**
00074      * the parameters for the class draw2DDistribution
00075      */
00076     class parameters : public functor::parameters {
00077     public:
00078       /**
00079        * default constructor
00080        */
00081       parameters() 
00082         : functor::parameters() {
00083         coSystem = false;
00084         scaleEqual = true;
00085         //border=40;
00086         background=getDefaultBackground();
00087         axisColor=getDefaultAxisColor();
00088 
00089         scale=tpoint<float>(20.0,20.0);
00090         offset=tpoint<float>(400.0,400.0);
00091         autoscale=true;
00092 
00093         initPalette();
00094         initMarker();
00095       };
00096 
00097       /**
00098        * copy constructor
00099        * @param other the parameters object to be copied
00100        */
00101       parameters(const parameters& other) 
00102         : functor::parameters() {
00103         copy(other);
00104       };
00105 
00106       /**
00107        * destructor
00108        */
00109       ~parameters() {};
00110 
00111       /**
00112        * returns name of this type
00113        */
00114       const char* getTypeName() const {
00115         return "draw2DDistribution::parameters";
00116       };
00117 
00118       /**
00119        * copy the contents of a parameters object
00120        * @param other the parameters object to be copied
00121        * @return a reference to this parameters object
00122        */
00123       parameters& copy(const parameters& other){
00124 # ifndef _LTI_MSC_6
00125         // MS Visual C++ 6 is not able to compile this...
00126         functor::parameters::copy(other);
00127 # else
00128         // ...so we have to use this workaround.
00129         // Conditional on that, copy may not be virtual.
00130         functor::parameters& (functor::parameters::* p_copy)
00131           (const functor::parameters&) =
00132           functor::parameters::copy;
00133         (this->*p_copy)(other);
00134 # endif
00135 
00136 
00137         coSystem = other.coSystem;
00138         scaleEqual = other.scaleEqual;
00139         background = other.background;
00140         axisColor = other.axisColor;
00141         marker = other.marker;
00142         palette = other.palette;
00143         scale = other.scale;
00144         offset = other.offset;
00145         autoscale = other.autoscale;
00146 
00147         return *this;
00148       };
00149 
00150       /**
00151        * copy the contents of a parameters object
00152        * @param other the parameters object to be copied
00153        * @return a reference to this parameters object
00154        */
00155       parameters& operator=(const parameters& other) {
00156         return copy(other);
00157       };
00158 
00159 
00160       /**
00161        * returns a pointer to a clone of the parameters
00162        */
00163       virtual functor::parameters* clone() const {
00164         return new parameters(*this);
00165       };
00166 
00167 # ifndef _LTI_MSC_6
00168       /**
00169        * write the parameters in the given ioHandler
00170        * @param handler the ioHandler to be used
00171        * @param complete if true (the default) the enclosing begin/end will
00172        *        be also written, otherwise only the data block will be written.
00173        * @return true if write was successful
00174        */
00175       bool write(ioHandler& handler,const bool complete=true) const
00176 # else
00177       bool writeMS(ioHandler& handler,const bool complete=true) const
00178 # endif
00179       {
00180         bool b = true;
00181         if (complete) {
00182           b = handler.writeBegin();
00183         }
00184 
00185         if (b) {
00186 
00187           lti::write(handler,"coSystem",coSystem);
00188           lti::write(handler,"scaleEqual",scaleEqual);
00189           lti::write(handler,"background",background);
00190           lti::write(handler,"axisColor",axisColor);
00191           lti::write(handler,"marker",marker);
00192           lti::write(handler,"palette",palette);
00193 
00194           lti::write(handler,"scale",scale);
00195           lti::write(handler,"offset",offset);
00196           lti::write(handler,"autoscale",autoscale);
00197         }
00198 
00199 # ifndef _LTI_MSC_6
00200         // This is the standard C++ code, which MS Visual C++ 6 is not able to
00201         // compile...
00202         b = b && functor::parameters::write(handler,false);
00203 # else
00204         bool (functor::parameters::* p_writeMS)(ioHandler&,const bool) const =
00205           functor::parameters::writeMS;
00206         b = b && (this->*p_writeMS)(handler,false);
00207 # endif
00208 
00209         if (complete) {
00210           b = b && handler.writeEnd();
00211         }
00212 
00213         return b;
00214       }
00215 
00216 # ifdef _LTI_MSC_6
00217       /**
00218        * write the parameters in the given ioHandler
00219        * @param handler the ioHandler to be used
00220        * @param complete if true (the default) the enclosing begin/end will
00221        *        be also written, otherwise only the data block will be written.
00222        * @return true if write was successful
00223        */
00224       bool write(ioHandler& handler,const bool complete) const {
00225         // ...we need this workaround to cope with another really awful MSVC bug.
00226         return writeMS(handler,complete);
00227       }
00228 # endif
00229 
00230       /**
00231        * read the parameters from the given ioHandler
00232        * @param handler the ioHandler to be used
00233        * @param complete if true (the default) the enclosing begin/end will
00234        *        be also written, otherwise only the data block will be written.
00235        * @return true if write was successful
00236        */
00237 # ifndef _LTI_MSC_6
00238       bool read(ioHandler& handler,const bool complete=true)
00239 # else
00240       bool readMS(ioHandler& handler,const bool complete=true)
00241 # endif
00242       {
00243         bool b = true;
00244         if (complete) {
00245           b = handler.readBegin();
00246         }
00247 
00248         if (b) {
00249           lti::read(handler,"coSystem",coSystem);
00250           lti::read(handler,"scaleEqual",scaleEqual);
00251           lti::read(handler,"background",background);
00252           lti::read(handler,"axisColor",axisColor);
00253 
00254           lti::read(handler,"marker",marker);
00255           lti::read(handler,"palette",palette);
00256 
00257           lti::read(handler,"scale",scale);
00258           lti::read(handler,"offset",offset);
00259           lti::read(handler,"autoscale",autoscale);
00260         }
00261 
00262 # ifndef _LTI_MSC_6
00263         // This is the standard C++ code, which MS Visual C++ 6 is not able to
00264         // compile...
00265         b = b && functor::parameters::read(handler,false);
00266 # else
00267         bool (functor::parameters::* p_readMS)(ioHandler&,const bool) =
00268           functor::parameters::readMS;
00269         b = b && (this->*p_readMS)(handler,false);
00270 # endif
00271 
00272         if (complete) {
00273           b = b && handler.readEnd();
00274         }
00275 
00276         return b;
00277       }
00278 
00279 # ifdef _LTI_MSC_6
00280       /**
00281        * read the parameters from the given ioHandler
00282        * @param handler the ioHandler to be used
00283        * @param complete if true (the default) the enclosing begin/end will
00284        *        be also written, otherwise only the data block will be written.
00285        * @return true if write was successful
00286        */
00287       bool read(ioHandler& handler,const bool complete) {
00288         // ...we need this workaround to cope with another really awful MSVC
00289         // bug.
00290         return readMS(handler,complete);
00291       }
00292 # endif
00293 
00294       // ------------------------------------------------
00295       // the parameters
00296       // ------------------------------------------------
00297 
00298 
00299       /**
00300        * if true a coordiate system is drawn. Default false.
00301        */
00302       bool coSystem;
00303 
00304 
00305       /**
00306        * if true, the axis will be scaled equal, otherwise the space is used
00307        * as well as possible. Default true.
00308        */
00309       bool scaleEqual;
00310 
00311       /**
00312        * the scaling factor. Default (20,20)
00313        */
00314       tpoint<float> scale;
00315 
00316       /**
00317        * All pixels are move by this offset. If you set both components of
00318        * offset to the half of the picture size, the drawn pixels are centred.
00319        * Default (400,400).
00320        */
00321       tpoint<float> offset;
00322 
00323       /**
00324        * If true, the optimal scaling factor is computed, so that all points
00325        * of the distribution are in the picture. Default true.
00326        */
00327       bool autoscale;
00328 
00329       /**
00330        * the backgroundColor of the picture. Default black.
00331        */
00332       T background;
00333 
00334       /**
00335        * the drawing color of the coordinate system. Default white.
00336        */
00337       T axisColor;
00338 
00339       /**
00340        * the color palette for drawing
00341        */
00342       std::vector<T> palette;
00343 
00344       /**
00345        * the markers for the different ids
00346        */
00347       std::vector<char> marker;
00348 
00349     private:
00350       // this needs to be specialized
00351       T getDefaultAxisColor() {
00352         return T(0);
00353       };
00354 
00355       T getDefaultBackground() {
00356         return T(0);
00357       };
00358 
00359       /**
00360        * Initialise the default color Palette
00361        */
00362       void initPalette()
00363 
00364 // Visual C++ .NET 2003 defines _LTI_MSC_6 with 1310
00365 // and it uses also ANSI code
00366 # ifdef _LTI_MSC_6
00367       {
00368         // insert complemenatry color to background
00369         palette.push_back(getDefaultAxisColor());
00370     palette.push_back(typeInfo<T>::suggestedNorm()/3);
00371     palette.push_back(typeInfo<T>::suggestedNorm()*2/3);
00372       }
00373 #endif
00374       ;
00375 
00376       /**
00377        * Initialise the default markers
00378        */
00379       void initMarker() {
00380         marker.push_back('o');
00381         marker.push_back('s');
00382         marker.push_back('d');
00383         marker.push_back('^');
00384         marker.push_back('v');
00385         marker.push_back('<');
00386         marker.push_back('>');
00387       }
00388 
00389     };
00390 
00391 
00392     // ------------------------------------------------
00393     // draw2DDistribution
00394     // ------------------------------------------------
00395 
00396     /**
00397      * default constructor
00398      */
00399     draw2DDistribution();
00400 
00401     /**
00402      * copy constructor
00403      * @param other the object to be copied
00404      */
00405     draw2DDistribution(const draw2DDistribution& other);
00406 
00407     /**
00408      * destructor
00409      */
00410     virtual ~draw2DDistribution();
00411 
00412     /**
00413      * returns the name of this type ("draw2DDistribution")
00414      */
00415     virtual const char* getTypeName() const;
00416 
00417     /**
00418      * Draws the points in dist with the given style into an image
00419      * @param drawer the drawering object that is used.
00420      * @param dist dmatrix with the source data.
00421      * @param hold if true the old image is not deleted
00422      * @param style the style with which the points are drawn
00423      * @return true if apply successful or false otherwise.
00424      */
00425     bool apply(drawBase<T>& drawer, const dmatrix& dist, const bool& hold=false,
00426                const char* style="ow0") const;
00427 
00428     /**
00429      * Draws two dimensions of dist.
00430      * @see apply(drawer,dist,hold,style)
00431      * @param drawer the drawer object used.  Usually it is an object
00432      *        of a class inherited from drawBase<T>, like draw<T>.
00433      * @param dist dmatrix with the source data.
00434      * @param first first dimension taken
00435      * @param second second dimension
00436      * @param hold if true the old image is not deleted
00437      * @param style the style with which the points are drawn
00438      * @return true if apply successful or false otherwise.
00439      */
00440     bool apply(drawBase<T>& drawer,
00441                const dmatrix& dist,
00442                const int first,
00443                const int second,
00444                const bool hold=false,
00445                const char* style="ow") const;
00446 
00447     /**
00448      * Draws the point in the two vectors, taking the first as abscissa
00449      * and the second as ordinate.
00450      */
00451     bool apply(drawBase<T>& drawer,const dvector& x,const dvector& y,
00452                const bool hold=false,const char* style="ow") const;
00453 
00454     /**
00455      * Draws the points in dist into an image. For each new ID a new style
00456      * is used.
00457      * @param drawer that drawing object that is used
00458      * @param dist dmatrix with the source data.
00459      * @param ids the ID's accoding to the points in dist
00460      * @param hold if true the old image is not deleted
00461      * @return true if apply successful or false otherwise.
00462      */
00463     bool apply(drawBase<T>& drawer, const dmatrix& dist,const ivector& ids,
00464                const bool& hold=false) const;
00465 
00466 
00467     /**
00468      * Draws the points in dist1 and dist2 into an image. In ids1 and ids2
00469      * the ids according to the points in dist1 and dist2 are expected.
00470      * For each new ID a new style is used. If points in dist1 and dist2
00471      * have equal ids, the pionts will be drawn with the same symbol and color,
00472      * but the symbol will be filled.
00473      * @param drawer drawing object that is used.
00474      * @param dist1 dmatrix with the source data.
00475      * @param ids1 the ID's accoding to the points in dist
00476      * @param dist2 dmatrix with the source data.
00477      * @param ids2 the ID's accoding to the points in dist
00478      * @param hold if true the old image is not deleted
00479      * @return true if apply successful or false otherwise.
00480      */
00481     bool apply(drawBase<T>& drawer, const dmatrix& dist1,const ivector& ids1,
00482                const dmatrix& dist2,const ivector& ids2,
00483                const bool& hold=false) const;
00484 
00485     /**
00486      * copy data of "other" functor.
00487      * @param other the functor to be copied
00488      * @return a reference to this functor object
00489      */
00490     draw2DDistribution<T>& copy(const draw2DDistribution<T>& other);
00491 
00492     /**
00493      * alias for copy member
00494      * @param other the functor to be copied
00495      * @return a reference to this functor object
00496      */
00497     draw2DDistribution<T>& operator=(const draw2DDistribution<T>& other);
00498 
00499     /**
00500      * returns a pointer to a clone of this functor.
00501      */
00502     virtual functor* clone() const;
00503 
00504     /**
00505      * returns used parameters
00506      */
00507     const parameters& getParameters() const;
00508 
00509   protected:
00510 
00511     /**
00512      * the distribution with the test points
00513      */
00514     dmatrix *testDist;
00515 
00516     /**
00517      * the ids of the points in testDist
00518      */
00519     ivector *testIds;
00520 
00521     /**
00522      * the distribution with the test points
00523      */
00524     dmatrix *trainDist;
00525 
00526     /**
00527      * the ids of the points in testDist
00528      */
00529     ivector *trainIds;
00530 
00531     /**
00532      * map with the styles according to the ids
00533      */
00534     std::map<int,char* > *styles;
00535 
00536     /**
00537      * map with the color according to the ids
00538      */
00539     std::map<int,T > *colorMap;
00540 
00541 
00542   };
00543 
00544 }
00545 
00546 #endif

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