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 2014 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 PTEX_NAMESPACE_BEGIN
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)
50 const float PtexTriangleKernelWidth = 3.5f;
51
52
54 class PtexTriangleKernelIter {
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);
77 static ApplyFn applyFunctions[40];
78};
79
80
82 class PtexTriangleKernel {
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
116 void splitU(PtexTriangleKernel& ka)
117 {
118 ka = *this;
119 u1 = 0;
120 ka.u2 = 0;
121 }
122
123 void splitV(PtexTriangleKernel& ka)
124 {
125 ka = *this;
126 v1 = 0;
127 ka.v2 = 0;
128 }
129
130 void splitW(PtexTriangleKernel& ka)
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 = std::min(res.ulog2, fres.ulog2);
175 res.vlog2 = res.ulog2;
176 }
177
178 void clampExtent()
179 {
180 u1 = std::max(u1, 0.0f);
181 v1 = std::max(v1, 0.0f);
182 w1 = std::max(w1, 0.0f);
183 u2 = std::min(u2, 1.0f-(v1+w1));
184 v2 = std::min(v2, 1.0f-(w1+u1));
185 w2 = std::min(w2, 1.0f-(u1+v1));
186 }
187
188 void getIterators(PtexTriangleKernelIter& ke, PtexTriangleKernelIter& ko)
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(std::ceil(u1 * scale - float(1/3.0)));
203 ke.v1 = int(std::ceil(v1 * scale - float(1/3.0)));
204 ke.w1 = int(std::ceil(w1 * scale - float(1/3.0)));
205 ke.u2 = int(std::ceil(u2 * scale - float(1/3.0)));
206 ke.v2 = int(std::ceil(v2 * scale - float(1/3.0)));
207 ke.w2 = int(std::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(std::ceil((1.0f-v2) * scale - float(1/3.0)));
218 ko.v1 = int(std::ceil((1.0f-u2) * scale - float(1/3.0)));
219 ko.w1 = int(std::ceil((-w2) * scale - float(1/3.0)));
220 ko.u2 = int(std::ceil((1.0f-v1) * scale - float(1/3.0)));
221 ko.v2 = int(std::ceil((1.0f-u1) * scale - float(1/3.0)));
222 ko.w2 = int(std::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
229 PTEX_NAMESPACE_END
230
231#endif
PtexTriangleKernelWidth
PTEX_NAMESPACE_BEGIN const float PtexTriangleKernelWidth
Definition PtexTriangleKernel.h:50
PtexUtils.h
PTEX_NAMESPACE_END
#define PTEX_NAMESPACE_END
Definition PtexVersion.h:62
Ptexture.h
Public API classes for reading, writing, caching, and filtering Ptex files.
PtexTriangleKernelIter
Triangle filter kernel iterator (in texel coords).
Definition PtexTriangleKernel.h:54
PtexTriangleKernelIter::A
float A
Definition PtexTriangleKernel.h:60
PtexTriangleKernelIter::v1
int v1
Definition PtexTriangleKernel.h:58
PtexTriangleKernelIter::applyFunctions
static ApplyFn applyFunctions[40]
Definition PtexTriangleKernel.h:136
PtexTriangleKernelIter::ApplyFn
void(* ApplyFn)(PtexTriangleKernelIter &k, float *dst, void *data, int nChan, int nTxChan)
Definition PtexTriangleKernel.h:76
PtexTriangleKernelIter::v2
int v2
Definition PtexTriangleKernel.h:59
PtexTriangleKernelIter::rowlen
int rowlen
Definition PtexTriangleKernel.h:56
PtexTriangleKernelIter::w1
int w1
Definition PtexTriangleKernel.h:58
PtexTriangleKernelIter::u
float u
Definition PtexTriangleKernel.h:57
PtexTriangleKernelIter::C
float C
Definition PtexTriangleKernel.h:60
PtexTriangleKernelIter::apply
void apply(float *dst, void *data, DataType dt, int nChan, int nTxChan)
Definition PtexTriangleKernel.h:65
PtexTriangleKernelIter::w2
int w2
Definition PtexTriangleKernel.h:59
PtexTriangleKernelIter::u1
int u1
Definition PtexTriangleKernel.h:58
PtexTriangleKernelIter::weight
float weight
Definition PtexTriangleKernel.h:63
PtexTriangleKernelIter::valid
bool valid
Definition PtexTriangleKernel.h:61
PtexTriangleKernelIter::u2
int u2
Definition PtexTriangleKernel.h:59
PtexTriangleKernelIter::applyConst
void applyConst(float *dst, void *data, DataType dt, int nChan)
Definition PtexTriangleKernel.cpp:153
PtexTriangleKernelIter::v
float v
Definition PtexTriangleKernel.h:57
PtexTriangleKernelIter::B
float B
Definition PtexTriangleKernel.h:60
PtexTriangleKernelIter::wscale
float wscale
Definition PtexTriangleKernel.h:62
PtexTriangleKernel
Triangle filter kernel (in normalized triangle coords).
Definition PtexTriangleKernel.h:82
PtexTriangleKernel::reorient
void reorient(int eid, int aeid)
Definition PtexTriangleKernel.h:151
PtexTriangleKernel::B
float B
Definition PtexTriangleKernel.h:88
PtexTriangleKernel::u
float u
Definition PtexTriangleKernel.h:85
PtexTriangleKernel::clampExtent
void clampExtent()
Definition PtexTriangleKernel.h:178
PtexTriangleKernel::u2
float u2
Definition PtexTriangleKernel.h:87
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:90
PtexTriangleKernel::set
void set(float uVal, float vVal, float u1Val, float v1Val, float w1Val, float u2Val, float v2Val, float w2Val)
Definition PtexTriangleKernel.h:102
PtexTriangleKernel::u1
float u1
Definition PtexTriangleKernel.h:86
PtexTriangleKernel::C
float C
Definition PtexTriangleKernel.h:88
PtexTriangleKernel::splitW
void splitW(PtexTriangleKernel &ka)
Definition PtexTriangleKernel.h:130
PtexTriangleKernel::v1
float v1
Definition PtexTriangleKernel.h:86
PtexTriangleKernel::splitV
void splitV(PtexTriangleKernel &ka)
Definition PtexTriangleKernel.h:123
PtexTriangleKernel::splitU
void splitU(PtexTriangleKernel &ka)
Definition PtexTriangleKernel.h:116
PtexTriangleKernel::res
Res res
Definition PtexTriangleKernel.h:84
PtexTriangleKernel::A
float A
Definition PtexTriangleKernel.h:88
PtexTriangleKernel::getIterators
void getIterators(PtexTriangleKernelIter &ke, PtexTriangleKernelIter &ko)
Definition PtexTriangleKernel.h:188
PtexTriangleKernel::rotate2
void rotate2()
Definition PtexTriangleKernel.h:144
PtexTriangleKernel::v
float v
Definition PtexTriangleKernel.h:85
PtexTriangleKernel::w1
float w1
Definition PtexTriangleKernel.h:86
PtexTriangleKernel::v2
float v2
Definition PtexTriangleKernel.h:87
PtexTriangleKernel::w2
float w2
Definition PtexTriangleKernel.h:87
PtexTriangleKernel::setABC
void setABC(float AVal, float BVal, float CVal)
Definition PtexTriangleKernel.h:111
PtexTriangleKernel::clampRes
void clampRes(Res fres)
Definition PtexTriangleKernel.h:172
PtexTriangleKernel::rotate1
void rotate1()
Definition PtexTriangleKernel.h:137
PTEX_NAMESPACE_BEGIN
Definition PtexSeparableKernel.cpp:42