Ptex
PtexTriangleKernel.h
Go to the documentation of this file.
1 #ifndef PtexTriangleKernel_h
2 #define PtexTriangleKernel_h
3 
4 /*
5 PTEX SOFTWARE
6 Copyright 2009 Disney Enterprises, Inc. All rights reserved
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
10 met:
11 
12  * Redistributions of source code must retain the above copyright
13  notice, this list of conditions and the following disclaimer.
14 
15  * Redistributions in binary form must reproduce the above copyright
16  notice, this list of conditions and the following disclaimer in
17  the documentation and/or other materials provided with the
18  distribution.
19 
20  * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
21  Studios" or the names of its contributors may NOT be used to
22  endorse or promote products derived from this software without
23  specific prior written permission from Walt Disney Pictures.
24 
25 Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
26 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
27 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
28 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
29 IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
30 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37 */
38 
39 #include <assert.h>
40 #include <algorithm>
41 #include <numeric>
42 #include "Ptexture.h"
43 #include "PtexUtils.h"
44 
45 // kernel width as a multiple of filter width (should be between 3 and 4)
46 // for values below 3, the gaussian is not close to zero and a contour will be formed
47 // larger values are more expensive (proportional to width-squared)
48 static const float PtexTriangleKernelWidth = 3.5f;
49 
50 
52 class PtexTriangleKernelIter : public Ptex {
53  public:
54  int rowlen; // row length (in u)
55  float u, v; // uv center in texels
56  int u1, v1, w1; // uvw lower bounds
57  int u2, v2, w2; // uvw upper bounds
58  float A,B,C; // ellipse coefficients (F = 1)
59  bool valid; // footprint is valid (non-empty)
60  float wscale; // amount to scale weights by (proportional to texel area)
61  float weight; // accumulated weight
62 
63  void apply(float* dst, void* data, DataType dt, int nChan, int nTxChan)
64  {
65  // dispatch specialized apply function
66  ApplyFn fn = applyFunctions[(nChan!=nTxChan)*20 + ((unsigned)nChan<=4)*nChan*4 + dt];
67  fn(*this, dst, data, nChan, nTxChan);
68  }
69 
70  void applyConst(float* dst, void* data, DataType dt, int nChan);
71 
72  private:
73 
74  typedef void (*ApplyFn)(PtexTriangleKernelIter& k, float* dst, void* data, int nChan, int nTxChan);
75  static ApplyFn applyFunctions[40];
76 };
77 
78 
80 class PtexTriangleKernel : public Ptex {
81  public:
82  Res res; // desired resolution
83  float u, v; // uv filter center
84  float u1, v1, w1; // uvw lower bounds
85  float u2, v2, w2; // uvw upper bounds
86  float A,B,C; // ellipse coefficients (F = A*C-B*B/4)
87 
88  void set(Res resVal, float uVal, float vVal,
89  float u1Val, float v1Val, float w1Val,
90  float u2Val, float v2Val, float w2Val,
91  float AVal, float BVal, float CVal)
92  {
93  res = resVal;
94  u = uVal; v = vVal;
95  u1 = u1Val; v1 = v1Val; w1 = w1Val;
96  u2 = u2Val; v2 = v2Val; w2 = w2Val;
97  A = AVal; B = BVal; C = CVal;
98  }
99 
100  void set(float uVal, float vVal,
101  float u1Val, float v1Val, float w1Val,
102  float u2Val, float v2Val, float w2Val)
103  {
104  u = uVal; v = vVal;
105  u1 = u1Val; v1 = v1Val; w1 = w1Val;
106  u2 = u2Val; v2 = v2Val; w2 = w2Val;
107  }
108 
109  void setABC(float AVal, float BVal, float CVal)
110  {
111  A = AVal; B = BVal; C = CVal;
112  }
113 
115  {
116  ka = *this;
117  u1 = 0;
118  ka.u2 = 0;
119  }
120 
122  {
123  ka = *this;
124  v1 = 0;
125  ka.v2 = 0;
126  }
127 
129  {
130  ka = *this;
131  w1 = 0;
132  ka.w2 = 0;
133  }
134 
135  void rotate1()
136  {
137  // rotate ellipse where u'=w, v'=u, w'=v
138  // (derived by converting to Barycentric form, rotating, and converting back)
139  setABC(C, 2.0f*C-B, A+C-B);
140  }
141 
142  void rotate2()
143  {
144  // rotate ellipse where u'=v, v'=w, w'=u
145  // (derived by converting to Barycentric form, rotating, and converting back)
146  setABC(A+C-B, 2.0f*A-B, A);
147  }
148 
149  void reorient(int eid, int aeid)
150  {
151  float w = 1.0f-u-v;
152 
153 #define C(eid, aeid) (eid*3 + aeid)
154  switch (C(eid, aeid)) {
155  case C(0, 0): set(1.0f-u, -v, 1.0f-u2, -v2, 1.0f-w2, 1.0f-u1, -v1, 1.0f-w1); break;
156  case C(0, 1): set(1.0f-w, 1.0f-u, 1.0f-w2, 1.0f-u2, -v2, 1.0f-w1, 1.0f-u1, -v1); rotate1(); break;
157  case C(0, 2): set( -v, 1.0f-w, -v2, 1.0f-w2, 1.0f-u2, -v1, 1.0f-w1, 1.0f-u1); rotate2(); break;
158 
159  case C(1, 0): set(1.0f-v, -w, 1.0f-v2, -w2, 1.0f-u2, 1.0f-v1, -w1, 1.0f-u1); rotate2(); break;
160  case C(1, 1): set(1.0f-u, 1.0f-v, 1.0f-u2, 1.0f-v2, -w2, 1.0f-u1, 1.0f-v1, -w1); break;
161  case C(1, 2): set( -w, 1.0f-u, -w2, 1.0f-u2, 1.0f-v2, -w1, 1.0f-u1, 1.0f-v1); rotate1(); break;
162 
163  case C(2, 0): set(1.0f-w, -u, 1.0f-w2, -u2, 1.0f-v2, 1.0f-w1, -u1, 1.0f-v1); rotate1(); break;
164  case C(2, 1): set(1.0f-v, 1.0f-w, 1.0f-v2, 1.0f-w2, -u2, 1.0f-v1, 1.0f-w1, -u1); rotate2(); break;
165  case C(2, 2): set( -u, 1.0f-v, -u2, 1.0f-v2, 1.0f-w2, -u1, 1.0f-v1, 1.0f-w1); break;
166 #undef C
167  }
168  }
169 
170  void clampRes(Res fres)
171  {
172  res.ulog2 = PtexUtils::min(res.ulog2, fres.ulog2);
173  res.vlog2 = res.ulog2;
174  }
175 
176  void clampExtent()
177  {
178  u1 = PtexUtils::max(u1, 0.0f);
179  v1 = PtexUtils::max(v1, 0.0f);
180  w1 = PtexUtils::max(w1, 0.0f);
181  u2 = PtexUtils::min(u2, 1.0f-(v1+w1));
182  v2 = PtexUtils::min(v2, 1.0f-(w1+u1));
183  w2 = PtexUtils::min(w2, 1.0f-(u1+v1));
184  }
185 
187  {
188  int resu = res.u();
189 
190  // normalize coefficients for texel units
191  float Finv = 1.0f/((float)resu*(float)resu*(A*C - 0.25f * B * B));
192  float Ak = A*Finv, Bk = B*Finv, Ck = C*Finv;
193 
194  // build even iterator
195  ke.rowlen = resu;
196  ke.wscale = 1.0f/((float)resu*(float)resu);
197  float scale = (float)ke.rowlen;
198  ke.u = u * scale - float(1/3.0);
199  ke.v = v * scale - float(1/3.0);
200  ke.u1 = int(ceilf(u1 * scale - float(1/3.0)));
201  ke.v1 = int(ceilf(v1 * scale - float(1/3.0)));
202  ke.w1 = int(ceilf(w1 * scale - float(1/3.0)));
203  ke.u2 = int(ceilf(u2 * scale - float(1/3.0)));
204  ke.v2 = int(ceilf(v2 * scale - float(1/3.0)));
205  ke.w2 = int(ceilf(w2 * scale - float(1/3.0)));
206  ke.A = Ak; ke.B = Bk; ke.C = Ck;
207  ke.valid = (ke.u2 > ke.u1 && ke.v2 > ke.v1 && ke.w2 > ke.w1);
208  ke.weight = 0;
209 
210  // build odd iterator: flip kernel across diagonal (u = 1-v, v = 1-u, w = -w)
211  ko.rowlen = ke.rowlen;
212  ko.wscale = ke.wscale;
213  ko.u = (1.0f-v) * scale - float(1/3.0);
214  ko.v = (1.0f-u) * scale - float(1/3.0);
215  ko.u1 = int(ceilf((1.0f-v2) * scale - float(1/3.0)));
216  ko.v1 = int(ceilf((1.0f-u2) * scale - float(1/3.0)));
217  ko.w1 = int(ceilf((-w2) * scale - float(1/3.0)));
218  ko.u2 = int(ceilf((1.0f-v1) * scale - float(1/3.0)));
219  ko.v2 = int(ceilf((1.0f-u1) * scale - float(1/3.0)));
220  ko.w2 = int(ceilf((-w1) * scale - float(1/3.0)));
221  ko.A = Ck; ko.B = Bk; ko.C = Ak;
222  ko.valid = (ko.u2 > ko.u1 && ko.v2 > ko.v1 && ko.w2 > ko.w1);
223  ko.weight = 0;
224  }
225 };
226 
227 #endif
void setABC(float AVal, float BVal, float CVal)
void set(float uVal, float vVal, float u1Val, float v1Val, float w1Val, float u2Val, float v2Val, float w2Val)
void applyConst(float *dst, void *data, DataType dt, int nChan)
Common data structures and enums used throughout the API.
Definition: Ptexture.h:73
void splitU(PtexTriangleKernel &ka)
void apply(float *dst, void *data, DataType dt, int nChan, int nTxChan)
void splitV(PtexTriangleKernel &ka)
DataType
Type of data stored in texture file.
Definition: Ptexture.h:83
void reorient(int eid, int aeid)
static T min(T a, T b)
Definition: PtexUtils.h:116
Triangle filter kernel iterator (in texel coords)
int u() const
U resolution in texels.
Definition: Ptexture.h:178
int8_t vlog2
log base 2 of v resolution, in texels
Definition: Ptexture.h:163
static ApplyFn applyFunctions[40]
DataType
Type of data stored in texture file.
Definition: Ptexture.h:196
Triangle filter kernel (in normalized triangle coords)
void set(Res resVal, float uVal, float vVal, float u1Val, float v1Val, float w1Val, float u2Val, float v2Val, float w2Val, float AVal, float BVal, float CVal)
void(* ApplyFn)(PtexTriangleKernelIter &k, float *dst, void *data, int nChan, int nTxChan)
void getIterators(PtexTriangleKernelIter &ke, PtexTriangleKernelIter &ko)
int8_t ulog2
log base 2 of u resolution, in texels
Definition: Ptexture.h:162
Pixel resolution of a given texture.
Definition: Ptexture.h:161
static const float PtexTriangleKernelWidth
void clampRes(Res fres)
Public API classes for reading, writing, caching, and filtering Ptex files.
void splitW(PtexTriangleKernel &ka)
static T max(T a, T b)
Definition: PtexUtils.h:119