Ptex
PtexSeparableFilter.cpp
Go to the documentation of this file.
1 /*
2 PTEX SOFTWARE
3 Copyright 2009 Disney Enterprises, Inc. All rights reserved
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in
14  the documentation and/or other materials provided with the
15  distribution.
16 
17  * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
18  Studios" or the names of its contributors may NOT be used to
19  endorse or promote products derived from this software without
20  specific prior written permission from Walt Disney Pictures.
21 
22 Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
23 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
25 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
26 IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 */
35 
36 #include "PtexPlatform.h"
37 #include <math.h>
38 #include <assert.h>
39 
40 #include "PtexSeparableFilter.h"
41 #include "PtexSeparableKernel.h"
42 #include "PtexUtils.h"
43 
44 
45 //#define NOEDGEBLEND // uncomment to disable filtering across edges (for debugging)
46 
47 void PtexSeparableFilter::eval(float* result, int firstChan, int nChannels,
48  int faceid, float u, float v,
49  float uw1, float vw1, float uw2, float vw2,
50  float width, float blur)
51 {
52  // init
53  if (!_tx || nChannels <= 0) return;
54  if (faceid < 0 || faceid >= _tx->numFaces()) return;
56  _dt = _tx->dataType();
57  _firstChanOffset = firstChan*DataSize(_dt);
58  _nchan = PtexUtils::min(nChannels, _ntxchan-firstChan);
59 
60  // get face info
61  const FaceInfo& f = _tx->getFaceInfo(faceid);
62 
63  // if neighborhood is constant, just return constant value of face
64  if (f.isNeighborhoodConstant()) {
65  PtexPtr<PtexFaceData> data ( _tx->getData(faceid, 0) );
66  if (data) {
67  char* d = (char*) data->getData() + _firstChanOffset;
68  Ptex::ConvertToFloat(result, d, _dt, _nchan);
69  }
70  return;
71  }
72 
73  // find filter width as bounding box of vectors w1 and w2
74  float uw = fabsf(uw1) + fabsf(uw2), vw = fabsf(vw1) + fabsf(vw2);
75 
76  // handle border modes
77  switch (_uMode) {
78  case m_clamp: u = PtexUtils::clamp(u, 0.0f, 1.0f); break;
79  case m_periodic: u = u-floorf(u); break;
80  case m_black: break; // do nothing
81  }
82 
83  switch (_vMode) {
84  case m_clamp: v = PtexUtils::clamp(v, 0.0f, 1.0f); break;
85  case m_periodic: v = v-floorf(v);
86  case m_black: break; // do nothing
87  }
88 
89  // build kernel
91  if (f.isSubface()) {
92  // for a subface, build the kernel as if it were on a main face and then downres
93  uw = uw * width + blur * 2.0f;
94  vw = vw * width + blur * 2.0f;
95  buildKernel(k, u*.5f, v*.5f, uw*.5f, vw*.5f,
96  Ptex::Res((int8_t)(f.res.ulog2+1),(int8_t)(f.res.vlog2+1)));
97  if (k.res.ulog2 == 0) k.upresU();
98  if (k.res.vlog2 == 0) k.upresV();
99  k.res.ulog2--; k.res.vlog2--;
100  }
101  else {
102  uw = uw * width + blur;
103  vw = vw * width + blur;
104  buildKernel(k, u, v, uw, vw, f.res);
105  }
106  k.stripZeros();
107 
108  // check kernel (debug only)
109  assert(k.uw > 0 && k.vw > 0);
111  _weight = k.weight();
112 
113  // allocate temporary result
114  _result = (float*) alloca(sizeof(float)*_nchan);
115  memset(_result, 0, sizeof(float)*_nchan);
116 
117  // apply to faces
118  splitAndApply(k, faceid, f);
119 
120  // normalize (both for data type and cumulative kernel weight applied)
121  // and output result
122  float scale = 1.0f / (_weight * OneValue(_dt));
123  for (int i = 0; i < _nchan; i++) result[i] = float(_result[i] * scale);
124 
125  // clear temp result
126  _result = 0;
127 }
128 
129 
131 {
132  // do we need to split? (i.e. does kernel span an edge?)
133  bool splitR = (k.u+k.uw > k.res.u()), splitL = (k.u < 0);
134  bool splitT = (k.v+k.vw > k.res.v()), splitB = (k.v < 0);
135 
136 #ifdef NOEDGEBLEND
137  // for debugging only
138  if (splitR) k.mergeR(_uMode);
139  if (splitL) k.mergeL(_uMode);
140  if (splitT) k.mergeT(_vMode);
141  if (splitB) k.mergeB(_vMode);
142 #else
143  if (splitR || splitL || splitT || splitB) {
144  PtexSeparableKernel ka, kc;
145  if (splitR) {
146  if (f.adjface(e_right) >= 0) {
147  k.splitR(ka);
148  if (splitT) {
149  if (f.adjface(e_top) >= 0) {
150  ka.splitT(kc);
151  applyToCorner(kc, faceid, f, e_top);
152  }
153  else ka.mergeT(_vMode);
154  }
155  if (splitB) {
156  if (f.adjface(e_bottom) >= 0) {
157  ka.splitB(kc);
158  applyToCorner(kc, faceid, f, e_right);
159  }
160  else ka.mergeB(_vMode);
161  }
162  applyAcrossEdge(ka, faceid, f, e_right);
163  }
164  else k.mergeR(_uMode);
165  }
166  if (splitL) {
167  if (f.adjface(e_left) >= 0) {
168  k.splitL(ka);
169  if (splitT) {
170  if (f.adjface(e_top) >= 0) {
171  ka.splitT(kc);
172  applyToCorner(kc, faceid, f, e_left);
173  }
174  else ka.mergeT(_vMode);
175  }
176  if (splitB) {
177  if (f.adjface(e_bottom) >= 0) {
178  ka.splitB(kc);
179  applyToCorner(kc, faceid, f, e_bottom);
180  }
181  else ka.mergeB(_vMode);
182  }
183  applyAcrossEdge(ka, faceid, f, e_left);
184  }
185  else k.mergeL(_uMode);
186  }
187  if (splitT) {
188  if (f.adjface(e_top) >= 0) {
189  k.splitT(ka);
190  applyAcrossEdge(ka, faceid, f, e_top);
191  }
192  else k.mergeT(_vMode);
193  }
194  if (splitB) {
195  if (f.adjface(e_bottom) >= 0) {
196  k.splitB(ka);
197  applyAcrossEdge(ka, faceid, f, e_bottom);
198  }
199  else k.mergeB(_vMode);
200  }
201  }
202 #endif
203 
204  // do local face
205  apply(k, faceid, f);
206 }
207 
208 
210  int faceid, const Ptex::FaceInfo& f, int eid)
211 {
212  int afid = f.adjface(eid), aeid = f.adjedge(eid);
213  const Ptex::FaceInfo* af = &_tx->getFaceInfo(afid);
214  int rot = eid - aeid + 2;
215 
216  // adjust uv coord and res for face/subface boundary
217  bool fIsSubface = f.isSubface(), afIsSubface = af->isSubface();
218  if (fIsSubface != afIsSubface) {
219  if (afIsSubface) {
220  // main face to subface transition
221  // adjust res and offset uv coord for primary subface
222  bool primary = k.adjustMainToSubface(eid);
223  if (!primary) {
224  // advance ajacent face and edge id to secondary subface
225  int neid = (aeid + 3) % 4;
226  afid = af->adjface(neid);
227  aeid = af->adjedge(neid);
228  af = &_tx->getFaceInfo(afid);
229  rot += neid - aeid + 2;
230  }
231  }
232  else {
233  // subface to main face transition
234  // Note: the transform depends on which subface the kernel is
235  // coming from. The "primary" subface is the one the main
236  // face is pointing at. The secondary subface adjustment
237  // happens to be the same as for the primary subface for the
238  // next edge, so the cases can be combined.
239  bool primary = (af->adjface(aeid) == faceid);
240  k.adjustSubfaceToMain(eid - primary);
241  }
242  }
243 
244  // rotate and apply (resplit if going to a subface)
245  k.rotate(rot);
246  if (afIsSubface) splitAndApply(k, afid, *af);
247  else apply(k, afid, *af);
248 }
249 
250 
252  const Ptex::FaceInfo& f, int eid)
253 {
254  // traverse clockwise around corner vertex and gather corner faces
255  int afid = faceid, aeid = eid;
256  const FaceInfo* af = &f;
257  bool prevIsSubface = af->isSubface();
258 
259  const int MaxValence = 10;
260  int cfaceId[MaxValence];
261  int cedgeId[MaxValence];
262  const FaceInfo* cface[MaxValence];
263 
264  int numCorners = 0;
265  for (int i = 0; i < MaxValence; i++) {
266  // advance to next face
267  int prevFace = afid;
268  afid = af->adjface(aeid);
269  aeid = (af->adjedge(aeid) + 1) % 4;
270 
271  // we hit a boundary or reached starting face
272  // note: we need to check edge id too because we might have
273  // a periodic texture (w/ toroidal topology) where all 4 corners
274  // are from the same face
275  if (afid < 0 || (afid == faceid && aeid == eid)) {
276  numCorners = i - 2;
277  break;
278  }
279 
280  // record face info
281  af = &_tx->getFaceInfo(afid);
282  cfaceId[i] = afid;
283  cedgeId[i] = aeid;
284  cface[i] = af;
285 
286  // check to see if corner is a subface "tee"
287  bool isSubface = af->isSubface();
288  if (prevIsSubface && !isSubface && af->adjface((aeid+3)%4) == prevFace)
289  {
290  // adjust the eid depending on whether we started from
291  // the primary or secondary subface.
292  bool primary = (i==1);
293  k.adjustSubfaceToMain(eid + primary * 2);
294  k.rotate(eid - aeid + 3 - primary);
295  splitAndApply(k, afid, *af);
296  return;
297  }
298  prevIsSubface = isSubface;
299  }
300 
301  if (numCorners == 1) {
302  // regular case (valence 4)
303  applyToCornerFace(k, f, eid, cfaceId[1], *cface[1], cedgeId[1]);
304  }
305  else if (numCorners > 1) {
306  // valence 5+, make kernel symmetric and apply equally to each face
307  // first, rotate to standard orientation, u=v=0
308  k.rotate(eid + 2);
309  float initialWeight = k.weight();
310  float newWeight = k.makeSymmetric(initialWeight);
311  for (int i = 1; i <= numCorners; i++) {
312  PtexSeparableKernel kc = k;
313  applyToCornerFace(kc, f, 2, cfaceId[i], *cface[i], cedgeId[i]);
314  }
315  // adjust weight for symmetrification and for additional corners
316  _weight += newWeight * (float)numCorners - initialWeight;
317  }
318  else {
319  // valence 2 or 3, ignore corner face (just adjust weight)
320  _weight -= k.weight();
321  }
322 }
323 
324 
326  int cfid, const Ptex::FaceInfo& cf, int ceid)
327 {
328  // adjust uv coord and res for face/subface boundary
329  bool fIsSubface = f.isSubface(), cfIsSubface = cf.isSubface();
330  if (fIsSubface != cfIsSubface) {
331  if (cfIsSubface) k.adjustMainToSubface(eid + 3);
332  else k.adjustSubfaceToMain(eid + 3);
333  }
334 
335  // rotate and apply (resplit if going to a subface)
336  k.rotate(eid - ceid + 2);
337  if (cfIsSubface) splitAndApply(k, cfid, cf);
338  else apply(k, cfid, cf);
339 }
340 
341 
343 {
344  assert(k.u >= 0 && k.u + k.uw <= k.res.u());
345  assert(k.v >= 0 && k.v + k.vw <= k.res.v());
346 
347  if (k.uw <= 0 || k.vw <= 0) return;
348 
349  // downres kernel if needed
350  while (k.res.u() > f.res.u()) k.downresU();
351  while (k.res.v() > f.res.v()) k.downresV();
352 
353  // get face data, and apply
354  PtexPtr<PtexFaceData> dh ( _tx->getData(faceid, k.res) );
355  if (!dh) return;
356 
357  if (dh->isConstant()) {
358  k.applyConst(_result, (char*)dh->getData()+_firstChanOffset, _dt, _nchan);
359  }
360  else if (dh->isTiled()) {
361  Ptex::Res tileres = dh->tileRes();
363  kt.res = tileres;
364  int tileresu = tileres.u();
365  int tileresv = tileres.v();
366  int ntilesu = k.res.u() / tileresu;
367  for (int v = k.v, vw = k.vw; vw > 0; vw -= kt.vw, v += kt.vw) {
368  int tilev = v / tileresv;
369  kt.v = v % tileresv;
370  kt.vw = PtexUtils::min(vw, tileresv - kt.v);
371  kt.kv = k.kv + v - k.v;
372  for (int u = k.u, uw = k.uw; uw > 0; uw -= kt.uw, u += kt.uw) {
373  int tileu = u / tileresu;
374  kt.u = u % tileresu;
375  kt.uw = PtexUtils::min(uw, tileresu - kt.u);
376  kt.ku = k.ku + u - k.u;
377  PtexPtr<PtexFaceData> th ( dh->getTile(tilev * ntilesu + tileu) );
378  if (th) {
379  if (th->isConstant())
380  kt.applyConst(_result, (char*)th->getData()+_firstChanOffset, _dt, _nchan);
381  else
382  kt.apply(_result, (char*)th->getData()+_firstChanOffset, _dt, _nchan, _ntxchan);
383  }
384  }
385  }
386  }
387  else {
388  k.apply(_result, (char*)dh->getData()+_firstChanOffset, _dt, _nchan, _ntxchan);
389  }
390 }
Left edge, from UV (0,1) to (0,0)
Definition: Ptexture.h:103
int adjface(int eid) const
Access an adjacent face id. The eid value must be 0..3.
Definition: Ptexture.h:267
virtual int numChannels()=0
Number of channels stored in file.
void splitAndApply(PtexSeparableKernel &k, int faceid, const Ptex::FaceInfo &f)
static const int kmax
void apply(PtexSeparableKernel &k, int faceid, const Ptex::FaceInfo &f)
void apply(float *dst, void *data, DataType dt, int nChan, int nTxChan)
void applyConst(float *dst, void *data, DataType dt, int nChan)
static int DataSize(DataType dt)
Look up size of given data type (in bytes).
Definition: Ptexture.h:132
virtual void eval(float *result, int firstchan, int nchannels, int faceid, float u, float v, float uw1, float vw1, float uw2, float vw2, float width, float blur)
Apply filter to a ptex data file.
Top edge, from UV (1,1) to (0,1)
Definition: Ptexture.h:102
void mergeL(BorderMode mode)
static T min(T a, T b)
Definition: PtexUtils.h:116
void mergeB(BorderMode mode)
bool isNeighborhoodConstant() const
Determine if neighborhood of face is constant (by checking a flag).
Definition: Ptexture.h:273
bool adjustMainToSubface(int eid)
float makeSymmetric(float initialWeight)
Platform-specific classes, functions, and includes.
int v() const
V resolution in texels.
Definition: Ptexture.h:181
static float OneValue(DataType dt)
Look up value of given data type that corresponds to the normalized value of 1.0. ...
Definition: Ptexture.h:138
Bottom edge, from UV (0,0) to (1,0)
Definition: Ptexture.h:100
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 T clamp(T x, T lo, T hi)
Definition: PtexUtils.h:122
void mergeR(BorderMode mode)
virtual void getData(int faceid, void *buffer, int stride)=0
Access texture data for a face at highest-resolution.
bool isSubface() const
Determine if face is a subface (by checking a flag).
Definition: Ptexture.h:279
void adjustSubfaceToMain(int eid)
virtual int numFaces()=0
Number of faces stored in file.
void mergeT(BorderMode mode)
EdgeId adjedge(int eid) const
Access an adjacent edge id. The eid value must be 0..3.
Definition: Ptexture.h:264
virtual void buildKernel(PtexSeparableKernel &k, float u, float v, float uw, float vw, Res faceRes)=0
texel beyond border are assumed to be black
Definition: Ptexture.h:93
virtual Ptex::DataType dataType()=0
Type of data stored in file.
void splitL(PtexSeparableKernel &k)
Smart-pointer for acquiring and releasing API objects.
Definition: Ptexture.h:955
void applyToCornerFace(PtexSeparableKernel &k, const Ptex::FaceInfo &f, int eid, int cfaceid, const Ptex::FaceInfo &cf, int ceid)
void splitB(PtexSeparableKernel &k)
int8_t ulog2
log base 2 of u resolution, in texels
Definition: Ptexture.h:162
Right edge, from UV (1,0) to (1,1)
Definition: Ptexture.h:101
void splitT(PtexSeparableKernel &k)
Pixel resolution of a given texture.
Definition: Ptexture.h:161
Res res
Resolution of face.
Definition: Ptexture.h:238
Information about a face, as stored in the Ptex file header.
Definition: Ptexture.h:237
texel access wraps to other side of face
Definition: Ptexture.h:94
void applyAcrossEdge(PtexSeparableKernel &k, int faceid, const Ptex::FaceInfo &f, int eid)
static void ConvertToFloat(float *dst, const void *src, Ptex::DataType dt, int numChannels)
Convert a number of data values from the given data type to float.
Definition: PtexUtils.cpp:107
virtual const Ptex::FaceInfo & getFaceInfo(int faceid)=0
Access resolution and adjacency information about a face.
void applyToCorner(PtexSeparableKernel &k, int faceid, const Ptex::FaceInfo &f, int eid)
texel access is clamped to border
Definition: Ptexture.h:92
void splitR(PtexSeparableKernel &k)