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