h o m e d o c u m e n t a t i o n c l a s s h i e r a r c h y

BasicStrokeShaders.h

00001 //
00002 //  Filename         : BasicStrokeShaders.h
00003 //  Author           : Stephane Grabli
00004 //  Purpose          : Class gathering basic stroke shaders
00005 //  Date of creation : 17/12/2002
00006 //
00008 
00009 
00010 //
00011 //  Copyright (C) : Please refer to the COPYRIGHT file distributed 
00012 //   with this source distribution. 
00013 //
00014 //  This program is free software; you can redistribute it and/or
00015 //  modify it under the terms of the GNU General Public License
00016 //  as published by the Free Software Foundation; either version 2
00017 //  of the License, or (at your option) any later version.
00018 //
00019 //  This program 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 this program; if not, write to the Free Software
00026 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 //
00029 
00030 #ifndef  BASIC_STROKE_SHADERS_H
00031 # define BASIC_STROKE_SHADERS_H
00032 
00033 # include "Stroke.h"
00034 # include "../geometry/Geom.h"
00035 # include "../geometry/Bezier.h"
00036 # include "StrokeShader.h"
00037 # include <fstream>
00038 
00039 using namespace std;
00040 using namespace Geometry;
00041 
00042 namespace StrokeShaders {
00043 
00044   //
00045   //  Thickness modifiers
00046   //
00048 
00052   class LIB_STROKE_EXPORT ConstantThicknessShader : public StrokeShader
00053   {
00054   public:
00060     ConstantThicknessShader(float thickness) : StrokeShader() {
00061       _thickness = thickness;
00062     }
00064     virtual ~ConstantThicknessShader() {}
00066     virtual string getName() const {
00067       return "ConstantThicknessShader";
00068     }
00070     virtual void shade(Stroke& stroke) const;
00071 
00072   private:
00073     float _thickness;
00074   };
00075 
00076   /* [ Thickness Shader ].
00077    *  Assigns an absolute constant external thickness to every
00078    *  vertices of the Stroke. The external thickness of a point
00079    *  is its thickness from the point to the strip border
00080    *  in the direction pointing outside the object the
00081    *  Stroke delimitates.
00082    */
00083   class LIB_STROKE_EXPORT ConstantExternThicknessShader : public StrokeShader
00084   {
00085   public:
00086 
00087     ConstantExternThicknessShader(float thickness) : StrokeShader() {
00088       _thickness = thickness;
00089     }
00090 
00091     virtual ~ConstantExternThicknessShader() {}
00092 
00093     virtual string getName() const {
00094       return "ConstantExternThicknessShader";
00095     }
00096 
00097     virtual void shade(Stroke& stroke) const;
00098 
00099   private:
00100 
00101     float _thickness;
00102   };
00103 
00112   class LIB_STROKE_EXPORT IncreasingThicknessShader : public StrokeShader
00113   {
00114   public:
00121     IncreasingThicknessShader(float iThicknessMin, float iThicknessMax)
00122       : StrokeShader()
00123     {
00124       _ThicknessMin = iThicknessMin;
00125       _ThicknessMax = iThicknessMax;
00126     }
00128     virtual ~IncreasingThicknessShader() {}
00130     virtual void shade(Stroke& stroke) const;
00131 
00132   private:
00133 
00134     float _ThicknessMin;
00135     float _ThicknessMax;
00136   };
00137 
00143   class LIB_STROKE_EXPORT ConstrainedIncreasingThicknessShader : public StrokeShader
00144   {
00145   private:
00146     float _ThicknessMin;
00147     float _ThicknessMax;
00148     float _ratio;
00149   public:
00159     ConstrainedIncreasingThicknessShader(float iThicknessMin, float iThicknessMax, float iRatio)
00160       : StrokeShader()
00161     {
00162       _ThicknessMin = iThicknessMin;
00163       _ThicknessMax = iThicknessMax;
00164       _ratio = iRatio;
00165     }
00167     virtual ~ConstrainedIncreasingThicknessShader() {}
00169     virtual void shade(Stroke& stroke) const;
00170   };
00171 
00172   /*  [ Thickness Shader ].
00173    *  Modifys the thickness in a relative way
00174    *  depending on its length.
00175    */
00176   class LIB_STROKE_EXPORT LengthDependingThicknessShader : public StrokeShader
00177   {
00178   private:
00179     float _minThickness;
00180     float _maxThickness;
00181     // We divide the strokes in 4 categories:
00182     // l > 300
00183     // 100 < l < 300
00184     // 50 < l < 100
00185     // l < 50
00186   public:
00187     LengthDependingThicknessShader(float iMinThickness, float iMaxThickness)
00188       : StrokeShader()
00189     {
00190       _minThickness = iMinThickness;
00191       _maxThickness = iMaxThickness;
00192     }
00193     virtual ~LengthDependingThicknessShader() {}
00194 
00195     virtual void shade(Stroke& stroke) const;
00196   };
00197 
00203   class LIB_STROKE_EXPORT ThicknessVariationPatternShader : public StrokeShader
00204   {
00205   public:
00206 
00218     ThicknessVariationPatternShader(const string pattern_name,
00219                                     float iMinThickness = 1.f,
00220                                     float iMaxThickness = 5.f,
00221                                     bool stretch = true);
00223     virtual ~ThicknessVariationPatternShader()
00224     {
00225       if(0 != _aThickness)
00226         {
00227           delete [] _aThickness;
00228           _aThickness = 0;
00229         }
00230     }
00232     virtual void shade(Stroke& stroke) const;
00233 
00234   private:
00235 
00236     float*      _aThickness; // array of thickness values, in % of the max (i.e comprised between 0 and 1)
00237     unsigned    _size;
00238     float       _minThickness;
00239     float       _maxThickness;
00240     bool        _stretch;
00241   };
00242 
00247   class LIB_STROKE_EXPORT ThicknessNoiseShader : public StrokeShader
00248   {
00249   private:
00250     float _amplitude;
00251     float _scale;
00252   public:
00253     ThicknessNoiseShader();
00260     ThicknessNoiseShader(float iAmplitude, float iPeriod);
00262     virtual void shade(Stroke& stroke) const;
00263   };
00264 
00265 
00266   //
00267   //  Color shaders
00268   //
00270 
00273   class LIB_STROKE_EXPORT ConstantColorShader : public StrokeShader
00274   {
00275   public:
00286     ConstantColorShader(float iR, float iG, float iB, float iAlpha=1.f)
00287       : StrokeShader()
00288     {
00289       _color[0] = iR;
00290       _color[1] = iG;
00291       _color[2] = iB;
00292       _color[3] = iAlpha;
00293     }
00294 
00295     virtual string getName() const {
00296       return "ConstantColorShader";
00297     }
00299     virtual void shade(Stroke& stroke) const;
00300 
00301   private:
00302 
00303     float _color[4];
00304   };
00305 
00312   class LIB_STROKE_EXPORT IncreasingColorShader : public StrokeShader
00313   {
00314   private:
00315     float _colorMin[4];
00316     float _colorMax[4];
00317   public:
00336     IncreasingColorShader(float iRm, float iGm, float iBm, float iAlpham, 
00337                                 float iRM, float iGM, float iBM, float iAlphaM)
00338       : StrokeShader() 
00339     {
00340       _colorMin[0] = iRm;
00341       _colorMin[1] = iGm;
00342       _colorMin[2] = iBm;
00343       _colorMin[3] = iAlpham;
00344 
00345       _colorMax[0] = iRM;
00346       _colorMax[1] = iGM;
00347       _colorMax[2] = iBM;
00348       _colorMax[3] = iAlphaM;
00349     }
00351     virtual void shade(Stroke& stroke) const;
00352   };
00353 
00359   class LIB_STROKE_EXPORT ColorVariationPatternShader : public StrokeShader
00360   {
00361   public:
00369     ColorVariationPatternShader(const string pattern_name, bool stretch = true);
00371     virtual ~ColorVariationPatternShader()
00372     {
00373       if(0 != _aVariation)
00374         {
00375           delete [] _aVariation;
00376           _aVariation = 0;
00377         }
00378     }
00380     virtual void shade(Stroke& stroke) const;
00381 
00382   private:
00383 
00384     float*      _aVariation; // array of coef values, in % of the max (i.e comprised between 0 and 1)
00385     unsigned    _size;
00386     bool        _stretch;
00387   };
00388 
00389   /* [ Color Shader ].
00390    *  Assigns a color to the stroke depending
00391    *  on the material of the shape to which ot belongs
00392    *  to. (Disney shader)
00393    */
00394   class LIB_STROKE_EXPORT MaterialColorShader : public StrokeShader
00395   {
00396   private:
00397     float _coefficient;
00398   public:
00399     MaterialColorShader(float coeff=1.f)
00400       : StrokeShader()
00401     {_coefficient=coeff;}
00402 
00403     virtual void shade(Stroke& stroke) const;
00404   };
00405 
00406   class LIB_STROKE_EXPORT CalligraphicColorShader : public StrokeShader
00407   {
00408   private:
00409     int _textureId;
00410     Vec2d _orientation;
00411   public:
00412     CalligraphicColorShader(
00413                             const Vec2d &iOrientation)
00414       : StrokeShader()
00415     { 
00416       _orientation=iOrientation;
00417       _orientation.normalize();
00418     } 
00419     virtual void shade(Stroke& stroke) const;
00420 
00421   };
00422 
00426   class LIB_STROKE_EXPORT ColorNoiseShader : public StrokeShader
00427   {
00428   private:
00429     float _amplitude; 
00430     float _scale;     
00431 
00432   public:
00433     ColorNoiseShader();
00440     ColorNoiseShader(float iAmplitude, float iPeriod);
00442     virtual void shade(Stroke& stroke) const;
00443   };
00444 
00445   //
00446   //  Texture Shaders
00447   //
00449 
00466   class LIB_STROKE_EXPORT TextureAssignerShader : public StrokeShader // FIXME
00467   {
00468   private:
00469     int _textureId;
00470   public:
00475     TextureAssignerShader(int id)
00476       : StrokeShader()
00477     {
00478       _textureId = id;
00479     }
00481     virtual void shade(Stroke& stroke) const;
00482   
00483   };
00488   class LIB_STROKE_EXPORT StrokeTextureShader : public StrokeShader
00489   {
00490   private:
00491     string _texturePath;
00492     Stroke::MediumType _mediumType;
00493     bool _tips; // 0 or 1
00494     
00495   public:
00522     StrokeTextureShader(const string textureFile, Stroke::MediumType mediumType = Stroke::OPAQUE_MEDIUM, bool iTips = false)
00523       : StrokeShader()
00524     {
00525       _texturePath = textureFile;
00526       _mediumType = mediumType;
00527       _tips = iTips;
00528     }
00530     virtual void shade(Stroke& stroke) const;
00531   
00532   };
00533 
00534 
00535   //
00536   //  Geometry Shaders
00537   //
00539 
00543   class LIB_STROKE_EXPORT BackboneStretcherShader : public StrokeShader
00544   {
00545   private:
00546     float _amount; 
00547   public:
00552     BackboneStretcherShader(float iAmount=2.f)
00553       : StrokeShader()
00554     {
00555       _amount = iAmount;
00556     }
00558     virtual void shade(Stroke& stroke) const;
00559   };
00560 
00565   class LIB_STROKE_EXPORT SamplingShader: public StrokeShader
00566   {
00567   private:
00568     float _sampling;
00569   public:
00575     SamplingShader(float  sampling)
00576       : StrokeShader()
00577     {
00578       _sampling = sampling;
00579     }
00581     virtual void shade(Stroke& stroke) const;
00582   };
00583 
00584 
00585   class LIB_STROKE_EXPORT ExternalContourStretcherShader : public StrokeShader
00586   {
00587   private:
00588     float _amount; 
00589   public:
00590     ExternalContourStretcherShader(float iAmount=2.f)
00591       : StrokeShader()
00592     {
00593       _amount = iAmount;
00594     }
00595 
00596     virtual void shade(Stroke& stroke) const;
00597   };
00598 
00599   // B-Spline stroke shader
00600   class LIB_STROKE_EXPORT BSplineShader: public StrokeShader
00601   { 
00602   public:
00603     BSplineShader()
00604       : StrokeShader()
00605     {}
00606 
00607     virtual void shade(Stroke& stroke) const;
00608   };
00609 
00610 
00611   // Bezier curve stroke shader
00618   class LIB_STROKE_EXPORT BezierCurveShader : public StrokeShader
00619   { 
00620   private:
00621     float _error;
00622   public:
00629     BezierCurveShader(float error = 4.0)
00630       : StrokeShader()
00631     {_error=error;}
00632 
00634     virtual void shade(Stroke& stroke) const;
00635   };
00636 
00637   /* Shader to inflate the curves. It keeps the extreme
00638    *  points positions and moves the other ones along 
00639    *  the 2D normal. The displacement value is proportional
00640    *  to the 2d curvature at the considered point (the higher 
00641    *  the curvature, the smaller the displacement) and to a value 
00642    *  specified by the user.
00643    */
00644   class LIB_STROKE_EXPORT InflateShader : public StrokeShader
00645   {
00646   private:
00647     float _amount; 
00648     float _curvatureThreshold;
00649   public:
00658     InflateShader(float iAmount,float iThreshold)
00659       : StrokeShader()
00660     {
00661       _amount = iAmount;
00662       _curvatureThreshold = iThreshold;
00663     }
00665     virtual void shade(Stroke& stroke) const;
00666   };
00667 
00677   class LIB_STROKE_EXPORT PolygonalizationShader : public StrokeShader
00678   {
00679   private:
00680     float _error;
00681   public:
00691     PolygonalizationShader(float iError) : StrokeShader() 
00692     {_error = iError;}
00694     virtual void shade(Stroke& stroke) const;
00695   };
00696 
00697 
00707   class LIB_STROKE_EXPORT GuidingLinesShader : public StrokeShader
00708   {
00709   private:
00710     float _offset;
00711   public:
00721     GuidingLinesShader(float iOffset) : StrokeShader()
00722     {_offset = iOffset;}
00724     virtual void shade(Stroke& stroke) const;
00725   };
00726 
00730   class LIB_STROKE_EXPORT TipRemoverShader : public StrokeShader 
00731   {
00732   public:
00738     TipRemoverShader (real tipLength);
00740     virtual ~TipRemoverShader () {}
00742     virtual void shade(Stroke &stroke) const;
00743 
00744   protected:
00745 
00746     real _tipLength; 
00747   };
00748 
00752   class LIB_STROKE_EXPORT streamShader : public StrokeShader
00753   {
00754   public:
00756     virtual ~streamShader() {}
00758     virtual string getName() const {
00759       return "streamShader";
00760     }
00762     virtual void shade(Stroke& stroke) const;
00763   };
00764 
00768   class LIB_STROKE_EXPORT fstreamShader : public StrokeShader
00769   {
00770   protected:
00771     mutable ofstream _stream;
00772   public:
00774     fstreamShader(const char *iFileName) : StrokeShader(){
00775       _stream .open(iFileName);
00776       if(!_stream.is_open()){
00777         cout << "couldn't open file " << iFileName << endl;
00778       }
00779     }
00781     virtual ~fstreamShader() {_stream.close();}
00783     virtual string getName() const {
00784       return "fstreamShader";
00785     }
00787     virtual void shade(Stroke& stroke) const;
00788   };
00789 } // end of namespace StrokeShaders
00790 
00791 #endif // BASIC_STROKE_SHADERS_H