HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Filter.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Filter.h ( UT Library, C++)
7  *
8  * COMMENTS: Circular symmetric separable filters. This class allows you to
9  * perform approximate filter weights given different filter
10  * kernels
11  *
12  * The lookup tables are valid for 0-1 and should be called with
13  * the square of the distance... The getWeight() methods can be
14  * used if given simple cartesian coordinates.
15  */
16 
17 #ifndef __UT_Filter__
18 #define __UT_Filter__
19 
20 #include "UT_API.h"
21 #include "UT_FilterType.h"
22 #include <SYS/SYS_Math.h>
23 #include <SYS/SYS_Types.h>
24 #include <string.h> // for memcpy
25 
26 class UT_Filter;
27 
28 // Build a convolution window for a given filter
30 public:
32  : myWeights(myBuffer)
33  , myStart(0)
34  , mySize(0)
35  , myCapacity(BUFSIZE)
36  , myVisible(1)
37  {
38  }
39 
41  {
42  if (myWeights != myBuffer)
43  delete [] myWeights;
44  }
45 
46  UT_FilterWindow(const UT_FilterWindow &) = delete;
47  UT_FilterWindow &operator=(const UT_FilterWindow &) = delete;
48 
49  bool setWeights(const UT_Filter &filter, float center,
50  float radius, int res,
52 
53  // This should be called if houdini13 filtering is enabled
54  template <UT_FilterWrap>
55  bool setWeights(const UT_Filter &filter, float center,
56  float radius, int res);
57 
58  // A more efficient version of setWeights(). However, there seems to be an
59  // issue with computing the rounding when filtering. $RTM/texture seems to
60  // have a more "linear" result, but $RT/itools/stdin_stdout has an obvious
61  // shift in the image. So, this isn't quite ready for prime time.
62  template <UT_FilterWrap>
63  bool setWeightsFast(const UT_Filter &filter, float center,
64  float radius, int res);
65 
66 
67  void blendWithNextMip(int res, UT_FilterWrap mode,
68  float mipinterp);
69 
70  // Trim out zero weights. This is typically not done by default, but if
71  // you're reusing these weights a lot, you might consider trimming.
72  void trimWeights()
73  {
74  if (myWeights[0] == 0 || myWeights[mySize-1] == 0)
75  pruneZeroWeights();
76  }
77 
78  const float *getWeights() const { return myWeights; }
79  int getStart() const { return myStart; }
80  int getEnd() const { return myStart + mySize - 1; }
81  int getSize() const { return mySize; }
82  float getVisible() const { return myVisible; }
83 
84  void dump(const char *msg="") const;
85 
86 private:
87  void pruneZeroWeights();
88 
89  // Ensure we have enough space to fill the weights. Prefer to use the
90  // local buffer, since this allows the filter to operate without memory
91  // allocation.
92  template <bool COPY>
93  void resizeWeights(int prevsize=0)
94  {
95  if (mySize > myCapacity)
96  {
97  float *weights = myWeights;
98  myWeights = new float[mySize];
99  myCapacity = mySize;
100  if (COPY)
101  memcpy(myWeights, weights, prevsize*sizeof(float));
102  if (weights != myBuffer)
103  delete [] weights;
104  }
105  }
106 
107 private:
108  static const int BUFSIZE = 16;
109 
110  float myBuffer[BUFSIZE];
111  float *myWeights;
112  int myStart, mySize;
113  int myCapacity;
114  float myVisible;
115 };
116 
117 
119 {
120 public:
121  // Method to lookup the filter by name.
122  static UT_FilterType lookupFilter(const char *name);
123  static const char *getFilterName(UT_FilterType type);
124  static const char *getFilterLabel(UT_FilterType type);
125 
126  // Get a well known filter type
127  static UT_Filter *getFilter(UT_FilterType type);
128 
129  // Look up the filter wrap type given a string. If the type is unknown,
130  // return def.
131  static UT_FilterWrap lookupWrap(const char *keyword,
132  UT_FilterWrap def);
133 
134  // Get a custom filter kernel
135  static UT_Filter *getFilter(float (*evalfunc)(float, void *),
136  int size, int support = 2,
137  void *data = 0);
138 
139  static void releaseFilter(UT_Filter *filter);
140  void access() { myRefCount++; }
141 
142  /// Fill an array of weights. The @c center variable specifies the center
143  /// of the filter (in resolution space - not 0-1).
144  ///
145  /// For example, if you have a filter with a radius of 1.3 centered at
146  /// pixel 8.4, you should set:
147  /// - @c center = 8.4
148  /// - @c radius = 1.3
149  /// - @c size = floor(8.4+1.3) - floor(8.4-1.3) + 1 = 9-7+1 = 3
150  virtual void fillWeights(float umin, float radius,
151  float *weights, int size) const;
152 
153  // Compute average weight over the span l-r. The valid range for
154  // samples is -0.5F to 0.5F (i.e. a filter width of 1)
155  virtual float getWeight(float l, float r) const = 0;
156 
157  // Retrieve the filter area to the left of l
158  virtual float getArea(float l) const = 0;
159 
160  // Return the filter type
161  UT_FilterType getType() const { return myType; }
162 
163  // Get the filters desired support radius (i.e. 2 for gaussian)
164  float getSupport() const { return mySupport; }
165 
166 public:
167  virtual ~UT_Filter();
168 
169  UT_Filter(const UT_Filter &) = delete;
170  UT_Filter &operator=(const UT_Filter &) = delete;
171 
172 protected:
173  UT_Filter() : myRefCount(0), myType(UT_FILTER_BOX) {}
174 
177  float mySupport;
178 };
179 
180 #endif
float getVisible() const
Definition: UT_Filter.h:82
int myRefCount
Definition: UT_Filter.h:175
int getEnd() const
Definition: UT_Filter.h:80
UT_FilterWrap
Definition: UT_FilterType.h:40
#define UT_API
Definition: UT_API.h:14
int getStart() const
Definition: UT_Filter.h:79
UT_FilterType
Definition: UT_FilterType.h:16
const float * getWeights() const
Definition: UT_Filter.h:78
void trimWeights()
Definition: UT_Filter.h:72
GLuint const GLchar * name
Definition: glcorearb.h:786
int getSize() const
Definition: UT_Filter.h:81
GLenum mode
Definition: glcorearb.h:99
UT_FilterType getType() const
Definition: UT_Filter.h:161
GLsizeiptr size
Definition: glcorearb.h:664
LeafData & operator=(const LeafData &)=delete
float getSupport() const
Definition: UT_Filter.h:164
UT_FilterType myType
Definition: UT_Filter.h:176
float mySupport
Definition: UT_Filter.h:177
void access()
Definition: UT_Filter.h:140
GLboolean r
Definition: glcorearb.h:1222
type
Definition: core.h:1059
Definition: format.h:895
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297