Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Filter.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/CinderMath.h"
26 
27 namespace cinder {
28 
29 class FilterBase {
30  public:
31  FilterBase( float aSupport ) : mSupport( aSupport ) {}
32  virtual ~FilterBase() {};
33 
34  float getSupport() const { return mSupport; }
35  void setSupport( float aSupport ) { mSupport = aSupport; }
36 
37  virtual float operator()( float x ) const = 0;
38  protected:
39  float mSupport;
40 };
41 
42 // box, pulse, nearest-neighbor, Fourier window, 1st order (constant) b-spline
43 class FilterBox : public FilterBase {
44  public:
45  FilterBox( float aSupport = 0.5f ) : FilterBase( aSupport ) {}
46 
47  virtual float operator()( float x ) const {
48  if ( x < -0.5f ) return 0.0f;
49  else if ( x < 0.5f ) return 1.0f;
50  return 0.0f;
51  }
52 };
53 
54 // triangle, Bartlett window, 2nd order (linear) b-spline
55 class FilterTriangle : public FilterBase {
56  public:
57  FilterTriangle( float aSupport = 1.0f ) : FilterBase( aSupport ) {}
58 
59  virtual float operator()( float x ) const {
60  if ( x < -1.0f ) return 0.0f;
61  else if ( x < 0.0f ) return 1.0f + x;
62  else if ( x < 1.0f ) return 1.0f - x;
63  return 0.0f;
64  }
65 };
66 
67 // 3rd order (quadratic) b-spline
68 class FilterQuadratic : public FilterBase {
69  public:
70  FilterQuadratic( float aSupport = 1.5f ) : FilterBase( aSupport ) {}
71 
72  virtual float operator()( float x ) const {
73  float t;
74 
75  if ( x < -1.5f ) return 0.0f;
76  else if ( x < -0.5f ) { t = x + 1.5f; return 0.5f * t * t; }
77  else if ( x < 0.5f ) return 0.75f - x * x;
78  else if ( x < 1.5f ) { t = x - 1.5f; return 0.5f * t * t; }
79  return 0.0f;
80  }
81 };
82 
83 // 4th order (cubic) b-spline
84 class FilterCubic : public FilterBase {
85  public:
86  FilterCubic( float aSupport = 2.0f ) : FilterBase( aSupport ) {}
87 
88  virtual float operator()( float x ) const {
89  float t;
90 
91  if ( x < -2.0f ) return 0.0f;
92  else if ( x < -1.0f ) { t = 2.0f + x; return t * t * t / 6.0f; }
93  else if ( x < 0.0f ) return ( 4.0f + x * x * ( -6.0f + x * -3.0f ) ) / 6.0f;
94  else if ( x < 1.0f ) return ( 4.0f + x * x * ( -6.0f + x * 3.0f ) ) / 6.0f;
95  else if ( x < 2.0f ) { t = 2.0f - x; return t * t * t / 6.0f; }
96  return 0.0f;
97  }
98 };
99 
100 // Catmull-Rom spline, Overhauser spline
101 class FilterCatmullRom : public FilterBase {
102  public:
103  FilterCatmullRom( float aSupport = 2.0f ) : FilterBase( aSupport ) {}
104 
105  virtual float operator()( float x ) const {
106  if ( x < -2.0f ) return 0.0f;
107  else if ( x < -1.0f ) return 0.5f * ( 4.0f + x * ( 8.0f + x * ( 5.0f + x ) ) );
108  else if ( x < 0.0f ) return 0.5f * ( 2.0f + x * x * ( -5.0f + x * -3.0f ) );
109  else if ( x < 1.0f ) return 0.5f * ( 2.0f + x * x * ( -5.0f + x * 3.0f ) );
110  else if ( x < 2.0f ) return 0.5f * ( 4.0f + x * ( -8.0f + x * ( 5.0f - x ) ) );
111  return 0.0f;
112  }
113 };
114 
115 // Mitchell & Netravali's two-parameter cubic
116 // see Mitchell&Netravali, "Reconstruction Filters in Computer Graphics", SIGGRAPH 88
117 class FilterMitchell : public FilterBase {
118  public:
119  FilterMitchell( float aSupport = 2.0f, float b = 0.3333333333f, float c = 0.3333333333f ) : FilterBase( aSupport ) {
120  mP0 = ( 6.0f - 2.0f * b ) / 6.0f;
121  mP2 = ( -18.0f + 12.0f * b + 6.0f * c ) / 6.0f;
122  mP3 = ( 12.0f - 9.0f * b - 6.0f * c ) / 6.0f;
123  mQ0 = ( 8.0f * b + 24.0f * c ) / 6.0f;
124  mQ1 = ( - 12.0f * b - 48.0f * c ) / 6.0f;
125  mQ2 = ( 6.0f * b + 30.0f * c ) / 6.0f;
126  mQ3 = ( -b - 6.0f * c ) / 6.0f;
127  }
128 
129  virtual float operator()( float x ) const {
130  if ( x < -2.0f ) return 0.;
131  else if ( x < -1.0f ) return mQ0 - x * ( mQ1 - x * ( mQ2 - x * mQ3 ) );
132  else if ( x < 0.0f ) return mP0 + x * x * ( mP2 - x * mP3 );
133  else if ( x < 1.0f ) return mP0 + x * x * ( mP2 + x * mP3 );
134  else if ( x < 2.0f ) return mQ0 + x * ( mQ1 + x * ( mQ2 + x * mQ3 ) );
135  return 0.0f;
136  }
137 
138  private:
139  float mQ0, mQ1, mQ2, mQ3;
140  float mP0, mP2, mP3;
141 };
142 
143 // sinc filter, windowed by blackman
145  public:
146  FilterSincBlackman( float aSupport = 4.0f ) : FilterBase( aSupport ) {}
147 
148  virtual float operator()( float x ) const {
149  float v( ( x == 0.0f ) ? 1.0f : math<float>::sin( 3.14159265358979323846f * x ) / ( 3.14159265358979323846f * x ) );
150  // blackman
151  x /= mSupport;
152  return v * ( 0.42f + 0.50f * math<float>::cos( 3.14159265358979323846f * x ) + 0.08f * math<float>::cos( 6.2831853071795862f * x ) );
153  }
154 };
155 
156 // sinc filter, windowed by blackman
157 class FilterGaussian : public FilterBase {
158  public:
159  FilterGaussian( float aSupport = 1.25f ) : FilterBase( aSupport ) {}
160 
161  virtual float operator()( float x ) const {
162  return ( math<float>::exp( -2.0f * x * x ) * math<float>::sqrt( 2.0f / 3.14159265358979323846f ) );
163  }
164 };
165 
166 #if ! defined( CINDER_COCOA_TOUCH )
168  public:
169  FilterBesselBlackman( float aSupport = 3.2383f ) : FilterBase( aSupport ) {}
170 
171  virtual float operator()( float x ) const {
172 #if (defined (CINDER_MSW) || defined( CINDER_WINRT ))
173  // According to VS.Net 2K5, j1 was depcreated, use _j1 instead.
174  float v( ( x == 0.0f ) ? ( 3.14159265358979323846f / 4.0f ) : static_cast<float>( _j1( 3.14159265358979323846f * x ) ) / ( 2.0f * x ) );
175 #else
176  float v( ( x == 0.0f ) ? ( 3.14159265358979323846f / 4.0f ) : static_cast<float>( j1( 3.14159265358979323846f * x ) ) / ( 2.0f * x ) );
177 #endif
178  // always bet on blackman
179  x /= mSupport;
180  return v * ( 0.42f + 0.50f * math<float>::cos( 3.14159265358979323846f * x ) + 0.08f * math<float>::cos( 6.2831853071795862f * x ) );
181  }
182 };
183 #endif
184 
185 } // namespace cinder
FilterCubic(float aSupport=2.0f)
Definition: Filter.h:86
FilterTriangle(float aSupport=1.0f)
Definition: Filter.h:57
Definition: CinderMath.h:40
static T cos(T x)
Definition: CinderMath.h:46
Definition: Filter.h:117
virtual float operator()(float x) const
Definition: Filter.h:171
virtual float operator()(float x) const =0
FilterBase(float aSupport)
Definition: Filter.h:31
float mSupport
Definition: Filter.h:39
FilterSincBlackman(float aSupport=4.0f)
Definition: Filter.h:146
Definition: Filter.h:157
virtual ~FilterBase()
Definition: Filter.h:32
FilterMitchell(float aSupport=2.0f, float b=0.3333333333f, float c=0.3333333333f)
Definition: Filter.h:119
Definition: Filter.h:101
virtual float operator()(float x) const
Definition: Filter.h:129
Definition: Filter.h:144
Definition: Filter.h:43
virtual float operator()(float x) const
Definition: Filter.h:161
void setSupport(float aSupport)
Definition: Filter.h:35
FilterBesselBlackman(float aSupport=3.2383f)
Definition: Filter.h:169
virtual float operator()(float x) const
Definition: Filter.h:105
Definition: Filter.h:84
FilterGaussian(float aSupport=1.25f)
Definition: Filter.h:159
GLenum GLint x
Definition: GLee.h:987
virtual float operator()(float x) const
Definition: Filter.h:72
const GLdouble * v
Definition: GLee.h:1384
GLboolean GLboolean GLboolean b
Definition: GLee.h:2964
Definition: Filter.h:167
float getSupport() const
Definition: Filter.h:34
FilterCatmullRom(float aSupport=2.0f)
Definition: Filter.h:103
const GLubyte * c
Definition: GLee.h:8491
FilterBox(float aSupport=0.5f)
Definition: Filter.h:45
Definition: Filter.h:29
Definition: Filter.h:68
virtual float operator()(float x) const
Definition: Filter.h:47
virtual float operator()(float x) const
Definition: Filter.h:59
Definition: Filter.h:55
FilterQuadratic(float aSupport=1.5f)
Definition: Filter.h:70
GLdouble GLdouble t
Definition: GLee.h:1426
GLclampf f
Definition: GLee.h:15307
virtual float operator()(float x) const
Definition: Filter.h:148
virtual float operator()(float x) const
Definition: Filter.h:88