Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
AbstractCanvas.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLCore/src/ICLCore/AbstractCanvas.h                   **
00010 ** Module : ICLCore                                                **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/CompatMacros.h>
00034 #include <ICLCore/Img.h>
00035 #include <ICLUtils/Uncopyable.h>
00036 #include <ICLMath/FixedMatrix.h>
00037 #include <ICLCore/Color.h>
00038 #include <ICLUtils/Rect32f.h>
00039 
00040 
00041 namespace icl{
00042 
00043   using namespace math;
00044   using namespace utils;
00045 
00046   namespace core{
00047     
00048 
00049     
00050     class ICLCore_API AbstractCanvas : public utils::Uncopyable{
00051       public:
00052       typedef math::FixedMatrix<float,3,3> Transform;
00053       typedef core::Color4D32f Color;
00054       struct ICLCore_API ClipRect{
00055         ClipRect(float minx=0, float maxx=0, float miny=0, float maxy=0): 
00056           minx(minx),maxx(maxx),miny(miny),maxy(maxy){}
00057         float minx;
00058         float maxx;
00059         float miny;
00060         float maxy;
00061         bool in(float x, float y) const{
00062           return ((x >= minx) && (x<= maxx) 
00063                   && (y >= miny) && (y <= maxy));
00064         }
00065         bool in(const Point32f &p) const{
00066           return in(p.x,p.y);
00067         }
00068       };
00069       
00070       protected:
00071       struct State{
00072         AbstractCanvas::Transform T;
00073         AbstractCanvas::Color linecolor;
00074         AbstractCanvas::Color fillcolor;
00075         float linewidth;
00076         float pointsize;
00077         float symsize;
00078         float fontsize;
00079         std::string fontname;
00080         ClipRect clip;
00081         
00082         inline State():
00083           T(AbstractCanvas::Transform::id()),
00084           linecolor(255,0,0,255),fillcolor(0,0,0,0),
00085           linewidth(1),pointsize(1),symsize(10),
00086           fontsize(10),fontname("arial"){}
00087       };
00088       
00089       State state;
00090       std::vector<State> stack;
00091   
00092       inline utils::Point32f transform(float x, float y) const{
00093         return Point32f(state.T(0,0)*x + state.T(1,0)*y + state.T(2,0),
00094                         state.T(0,1)*x + state.T(1,1)*y + state.T(2,1));
00095       }
00096       inline bool clip(float x, float y) const{
00097         return state.clip.in(x,y);
00098       }
00099       inline bool clip(const Point32f &p) const{
00100         return state.clip.in(p);
00101       }
00102                         
00103       
00104       public:
00105       
00106       AbstractCanvas(){}
00107       virtual ~AbstractCanvas(){}
00108       
00109       virtual void draw_point_internal(const utils::Point32f &p) = 0;
00110       virtual void draw_line_internal(const utils::Point32f &a, 
00111                                       const utils::Point32f &b) = 0;
00112       virtual void fill_triangle_internal(const utils::Point32f &a, 
00113                                           const utils::Point32f &b,
00114                                           const utils::Point32f &c) = 0;
00115       virtual void draw_ellipse_internal(const utils::Point32f &c,
00116                                          const utils::Point32f &axis1,
00117                                          const utils::Point32f &axis2) = 0;
00118       virtual void draw_image_internal(const utils::Point32f &ul, 
00119                                        const utils::Point32f &ur, 
00120                                        const utils::Point32f &lr, 
00121                                        const utils::Point32f &ll,
00122                                        float alpha, scalemode sm) = 0;
00123       
00124       public:
00125 
00126       virtual const Transform &getTransform() const{
00127         return state.T;
00128       }
00129       virtual void getTransform(float &angle, float &tx, float &ty) const{
00130         angle = acos(state.T(0,0));
00131         tx = state.T(2,0);
00132         ty = state.T(2,1);
00133       }
00134 
00135       
00136       virtual Rect32f getClipRect(){
00137         return Rect32f(state.clip.minx, state.clip.miny,
00138                        state.clip.maxx-state.clip.minx,
00139                        state.clip.maxy-state.clip.miny);
00140       }
00141       
00142       virtual void push(){
00143         stack.push_back(state);
00144       }
00145       
00146       virtual void pop() throw (utils::ICLException){
00147         ICLASSERT_THROW(stack.size(), utils::ICLException("AbstractCanvas::pop: stack is empty"));
00148         state = stack.back();
00149         stack.pop_back();
00150       }
00151       
00152       virtual void point(float x, float y);
00153       virtual void line(float x0, float y0, float x1, float y1);
00154       virtual void triangle(float x0, float y0, float x1, float y1, float x2, float y2);
00155       virtual void sym(char c, float x, float y) throw (utils::ICLException);
00156       virtual void linestrip(int n, const float *xs, const float *ys, 
00157                              int xStride=1, int yStride=1, bool loop=false);
00158       virtual void rect(float x, float y, float w, float h);
00159       virtual void circle(float cx, float cy, float r);
00160       virtual void ellipse(float cx, float cy, float rx, float ry);
00161       virtual void image(const ImgBase *image, float xanchor, 
00162                          float yanchor, float alpha, 
00163                          scalemode sm=interpolateLIN, 
00164                          bool centered=false)  throw (utils::ICLException);
00165       virtual void text(const std::string &t, float x, float y, bool centered = false);
00166       
00167       virtual void linecolor(float r, float g, float b, float a=255){
00168         state.linecolor = Color(r,g,b,a);
00169       }
00170 
00171       virtual void fillcolor(float r, float g, float b, float a=255){
00172         state.fillcolor = Color(r,g,b,a);
00173       }
00174       
00175       virtual void linewidth(float width){
00176         state.linewidth = width;
00177       }
00178 
00179       virtual void pointsize(float size){
00180         state.pointsize = size;
00181       }
00182 
00183       virtual void symsize(float size){
00184         state.symsize = size;
00185       }
00186       
00187       virtual void fontsize(float size){
00188         state.fontsize = size;
00189       }
00190       
00191       virtual void fontname(const std::string &fontname){
00192         state.fontname = fontname;
00193       }
00194 
00195       virtual void transform(const Transform &T){
00196         state.T = T*state.T;
00197       }
00198       
00199       virtual void transform(float tx, float ty, float angle){
00200         if(angle){
00201           const float sa = sin(angle), ca = cos(angle);
00202           state.T = Transform(ca,sa,tx,
00203                               -sa,ca,ty,
00204                               0,0,1)*state.T;
00205         }else{
00206           state.T(2,0) += tx;
00207           state.T(2,1) += ty;
00208         }
00209       }
00210       
00212       virtual void reset(){
00213         state.T = Transform::id();
00214       }
00215       
00216       virtual void rotate(float angle){
00217         transform(0,0,angle);
00218       }
00219       virtual void translate(float tx, float ty){
00220         transform(tx,ty,0);
00221       }
00222       virtual void scale(float s){
00223         scale(s,s);
00224       }
00225 
00226       virtual void scale(float sx, float sy){
00227         state.T = Transform(sx,0,0,
00228                             0,sy,0,
00229                             0,0,1) * state.T;
00230       }
00231     };
00232     
00233   }
00234 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines