Ptex: PtexTriangleKernel.h Source File

Ptex

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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
114 void splitU(PtexTriangleKernel& ka)
115 {
116 ka = *this;
117 u1 = 0;
118 ka.u2 = 0;
119 }
120
121 void splitV(PtexTriangleKernel& ka)
122 {
123 ka = *this;
124 v1 = 0;
125 ka.v2 = 0;
126 }
127
128 void splitW(PtexTriangleKernel& ka)
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
186 void getIterators(PtexTriangleKernelIter& ke, PtexTriangleKernelIter& ko)
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
PtexTriangleKernel::setABC
void setABC(float AVal, float BVal, float CVal)
Definition: PtexTriangleKernel.h:109
PtexTriangleKernel::w1
float w1
Definition: PtexTriangleKernel.h:84
PtexTriangleKernel::set
void set(float uVal, float vVal, float u1Val, float v1Val, float w1Val, float u2Val, float v2Val, float w2Val)
Definition: PtexTriangleKernel.h:100
PtexTriangleKernelIter::applyConst
void applyConst(float *dst, void *data, DataType dt, int nChan)
Definition: PtexTriangleKernel.cpp:156
Ptex
Common data structures and enums used throughout the API.
Definition: Ptexture.h:73
PtexTriangleKernelIter::rowlen
int rowlen
Definition: PtexTriangleKernel.h:54
PtexTriangleKernel::splitU
void splitU(PtexTriangleKernel &ka)
Definition: PtexTriangleKernel.h:114
PtexTriangleKernelIter::apply
void apply(float *dst, void *data, DataType dt, int nChan, int nTxChan)
Definition: PtexTriangleKernel.h:63
PtexTriangleKernelIter::weight
float weight
Definition: PtexTriangleKernel.h:61
PtexTriangleKernel::splitV
void splitV(PtexTriangleKernel &ka)
Definition: PtexTriangleKernel.h:121
Ptex::DataType
DataType
Type of data stored in texture file.
Definition: Ptexture.h:83
PtexTriangleKernelIter::wscale
float wscale
Definition: PtexTriangleKernel.h:60
PtexTriangleKernel::reorient
void reorient(int eid, int aeid)
Definition: PtexTriangleKernel.h:149
PtexTriangleKernelIter::valid
bool valid
Definition: PtexTriangleKernel.h:59
PtexUtils::min
static T min(T a, T b)
Definition: PtexUtils.h:116
PtexTriangleKernelIter::v
float v
Definition: PtexTriangleKernel.h:55
PtexTriangleKernelIter
Triangle filter kernel iterator (in texel coords)
Definition: PtexTriangleKernel.h:52
PtexTriangleKernel::clampExtent
void clampExtent()
Definition: PtexTriangleKernel.h:176
PtexTriangleKernel::C
float C
Definition: PtexTriangleKernel.h:86
PtexTriangleKernel::u2
float u2
Definition: PtexTriangleKernel.h:85
PtexTriangleKernel::v1
float v1
Definition: PtexTriangleKernel.h:84
PtexTriangleKernel::rotate1
void rotate1()
Definition: PtexTriangleKernel.h:135
PtexTriangleKernel::A
float A
Definition: PtexTriangleKernel.h:86
PtexTriangleKernelIter::v2
int v2
Definition: PtexTriangleKernel.h:57
PtexTriangleKernel::w2
float w2
Definition: PtexTriangleKernel.h:85
PtexTriangleKernelIter::u2
int u2
Definition: PtexTriangleKernel.h:57
PtexTriangleKernel::v2
float v2
Definition: PtexTriangleKernel.h:85
Ptex::Res::u
int u() const
U resolution in texels.
Definition: Ptexture.h:178
PtexTriangleKernelIter::w2
int w2
Definition: PtexTriangleKernel.h:57
Ptex::Res::vlog2
int8_t vlog2
log base 2 of v resolution, in texels
Definition: Ptexture.h:163
PtexTriangleKernelIter::applyFunctions
static ApplyFn applyFunctions[40]
Definition: PtexTriangleKernel.h:75
PtexTriangleKernel::res
Res res
Definition: PtexTriangleKernel.h:82
DataType
DataType
Type of data stored in texture file.
Definition: Ptexture.h:196
PtexTriangleKernel::B
float B
Definition: PtexTriangleKernel.h:86
PtexTriangleKernelIter::C
float C
Definition: PtexTriangleKernel.h:58
PtexTriangleKernelIter::u1
int u1
Definition: PtexTriangleKernel.h:56
PtexTriangleKernelIter::w1
int w1
Definition: PtexTriangleKernel.h:56
PtexTriangleKernel
Triangle filter kernel (in normalized triangle coords)
Definition: PtexTriangleKernel.h:80
PtexTriangleKernel::set
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)
Definition: PtexTriangleKernel.h:88
PtexTriangleKernelIter::B
float B
Definition: PtexTriangleKernel.h:58
PtexTriangleKernelIter::A
float A
Definition: PtexTriangleKernel.h:58
PtexTriangleKernelIter::ApplyFn
void(* ApplyFn)(PtexTriangleKernelIter &k, float *dst, void *data, int nChan, int nTxChan)
Definition: PtexTriangleKernel.h:74
PtexTriangleKernelIter::u
float u
Definition: PtexTriangleKernel.h:55
PtexTriangleKernel::u1
float u1
Definition: PtexTriangleKernel.h:84
PtexTriangleKernel::getIterators
void getIterators(PtexTriangleKernelIter &ke, PtexTriangleKernelIter &ko)
Definition: PtexTriangleKernel.h:186
Ptex::Res::ulog2
int8_t ulog2
log base 2 of u resolution, in texels
Definition: Ptexture.h:162
Ptex::Res
Pixel resolution of a given texture.
Definition: Ptexture.h:161
PtexTriangleKernel::u
float u
Definition: PtexTriangleKernel.h:83
PtexTriangleKernelWidth
static const float PtexTriangleKernelWidth
Definition: PtexTriangleKernel.h:48
PtexTriangleKernel::v
float v
Definition: PtexTriangleKernel.h:83
PtexTriangleKernel::rotate2
void rotate2()
Definition: PtexTriangleKernel.h:142
PtexTriangleKernel::clampRes
void clampRes(Res fres)
Definition: PtexTriangleKernel.h:170
PtexUtils.h
PtexTriangleKernelIter::v1
int v1
Definition: PtexTriangleKernel.h:56
Ptexture.h
Public API classes for reading, writing, caching, and filtering Ptex files.
PtexTriangleKernel::splitW
void splitW(PtexTriangleKernel &ka)
Definition: PtexTriangleKernel.h:128
PtexUtils::max
static T max(T a, T b)
Definition: PtexUtils.h:119