Ptex
PtexTriangleKernel.cpp
Go to the documentation of this file.
1/*
2PTEX SOFTWARE
3Copyright 2014 Disney Enterprises, Inc. All rights reserved
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
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
22Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
23CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
25FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
26IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
27CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
31THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34*/
35
36#include "PtexPlatform.h"
37#include "PtexUtils.h"
38#include "PtexHalf.h"
39#include "PtexTriangleKernel.h"
40
42
43namespace {
44 inline float gaussian(float x_squared)
45 {
46 static const float scale = -0.5f * (PtexTriangleKernelWidth * PtexTriangleKernelWidth);
47 return (float)exp(scale * x_squared);
48 }
49
50 // apply to 1..4 channels (unrolled channel loop) of packed data (nTxChan==nChan)
51 // the ellipse equation, Q, is calculated via finite differences (Heckbert '89 pg 57)
52 template<class T, int nChan>
53 void Apply(PtexTriangleKernelIter& k, float* result, void* data, int /*nChan*/, int /*nTxChan*/)
54 {
55 int nTxChan = nChan;
56 float DDQ = 2.0f*k.A;
57 for (int vi = k.v1; vi != k.v2; vi++) {
58 int xw = k.rowlen - vi;
59 int x1 = PtexUtils::max(k.u1, xw-k.w2);
60 int x2 = PtexUtils::min(k.u2, xw-k.w1);
61 float U = (float)x1 - k.u;
62 float V = (float)vi - k.v;
63 float DQ = k.A*(2.0f*U+1.0f)+k.B*V;
64 float Q = k.A*U*U + (k.B*U + k.C*V)*V;
65 T* p = static_cast<T*>(data) + (vi * k.rowlen + x1) * nTxChan;
66 T* pEnd = p + (x2-x1)*nTxChan;
67 for (; p < pEnd; p += nTxChan) {
68 if (Q < 1.0f) {
69 float weight = gaussian(Q)*k.wscale;
70 k.weight += weight;
71 PtexUtils::VecAccum<T,nChan>()(result, p, weight);
72 }
73 Q += DQ;
74 DQ += DDQ;
75 }
76 }
77 }
78
79 // apply to 1..4 channels (unrolled channel loop) w/ pixel stride
80 template<class T, int nChan>
81 void ApplyS(PtexTriangleKernelIter& k, float* result, void* data, int /*nChan*/, int nTxChan)
82 {
83 float DDQ = 2.0f*k.A;
84 for (int vi = k.v1; vi != k.v2; vi++) {
85 int xw = k.rowlen - vi;
86 int x1 = PtexUtils::max(k.u1, xw-k.w2);
87 int x2 = PtexUtils::min(k.u2, xw-k.w1);
88 float U = (float)x1 - k.u;
89 float V = (float)vi - k.v;
90 float DQ = k.A*(2.0f*U+1.0f)+k.B*V;
91 float Q = k.A*U*U + (k.B*U + k.C*V)*V;
92 T* p = static_cast<T*>(data) + (vi * k.rowlen + x1) * nTxChan;
93 T* pEnd = p + (x2-x1)*nTxChan;
94 for (; p < pEnd; p += nTxChan) {
95 if (Q < 1.0f) {
96 float weight = gaussian(Q)*k.wscale;
97 k.weight += weight;
98 PtexUtils::VecAccum<T,nChan>()(result, p, weight);
99 }
100 Q += DQ;
101 DQ += DDQ;
102 }
103 }
104 }
105
106 // apply to N channels (general case)
107 template<class T>
108 void ApplyN(PtexTriangleKernelIter& k, float* result, void* data, int nChan, int nTxChan)
109 {
110 float DDQ = 2.0f*k.A;
111 for (int vi = k.v1; vi != k.v2; vi++) {
112 int xw = k.rowlen - vi;
113 int x1 = PtexUtils::max(k.u1, xw-k.w2);
114 int x2 = PtexUtils::min(k.u2, xw-k.w1);
115 float U = (float)x1 - k.u;
116 float V = (float)vi - k.v;
117 float DQ = k.A*(2.0f*U+1.0f)+k.B*V;
118 float Q = k.A*U*U + (k.B*U + k.C*V)*V;
119 T* p = static_cast<T*>(data) + (vi * k.rowlen + x1) * nTxChan;
120 T* pEnd = p + (x2-x1)*nTxChan;
121 for (; p < pEnd; p += nTxChan) {
122 if (Q < 1.0f) {
123 float weight = gaussian(Q)*k.wscale;
124 k.weight += weight;
125 PtexUtils::VecAccumN<T>()(result, p, nChan, weight);
126 }
127 Q += DQ;
128 DQ += DDQ;
129 }
130 }
131 }
132}
133
134
137 // nChan == nTxChan
138 ApplyN<uint8_t>, ApplyN<uint16_t>, ApplyN<PtexHalf>, ApplyN<float>,
139 Apply<uint8_t,1>, Apply<uint16_t,1>, Apply<PtexHalf,1>, Apply<float,1>,
140 Apply<uint8_t,2>, Apply<uint16_t,2>, Apply<PtexHalf,2>, Apply<float,2>,
141 Apply<uint8_t,3>, Apply<uint16_t,3>, Apply<PtexHalf,3>, Apply<float,3>,
142 Apply<uint8_t,4>, Apply<uint16_t,4>, Apply<PtexHalf,4>, Apply<float,4>,
143
144 // nChan != nTxChan (need pixel stride)
145 ApplyN<uint8_t>, ApplyN<uint16_t>, ApplyN<PtexHalf>, ApplyN<float>,
146 ApplyS<uint8_t,1>, ApplyS<uint16_t,1>, ApplyS<PtexHalf,1>, ApplyS<float,1>,
147 ApplyS<uint8_t,2>, ApplyS<uint16_t,2>, ApplyS<PtexHalf,2>, ApplyS<float,2>,
148 ApplyS<uint8_t,3>, ApplyS<uint16_t,3>, ApplyS<PtexHalf,3>, ApplyS<float,3>,
149 ApplyS<uint8_t,4>, ApplyS<uint16_t,4>, ApplyS<PtexHalf,4>, ApplyS<float,4>,
150};
151
152
153void PtexTriangleKernelIter::applyConst(float* dst, void* data, DataType dt, int nChan)
154{
155 // iterate over texel locations and calculate weight as if texture weren't const
156 float DDQ = 2.0f*A;
157 for (int vi = v1; vi != v2; vi++) {
158 int xw = rowlen - vi;
159 int x1 = PtexUtils::max(u1, xw-w2);
160 int x2 = PtexUtils::min(u2, xw-w1);
161 float U = (float)x1 - u;
162 float V = (float)vi - v;
163 float DQ = A*(2.0f*U+1.0f)+B*V;
164 float Q = A*U*U + (B*U + C*V)*V;
165 for (int x = x1; x < x2; x++) {
166 if (Q < 1.0f) {
167 weight += gaussian(Q)*wscale;
168 }
169 Q += DQ;
170 DQ += DDQ;
171 }
172 }
173
174 // apply weight to single texel value
175 PtexUtils::applyConst(weight, dst, data, dt, nChan);
176}
177
Half-precision floating-point type.
Platform-specific classes, functions, and includes.
PTEX_NAMESPACE_BEGIN const float PtexTriangleKernelWidth
#define PTEX_NAMESPACE_END
Definition PtexVersion.h:62
Triangle filter kernel iterator (in texel coords)
static ApplyFn applyFunctions[40]
void(* ApplyFn)(PtexTriangleKernelIter &k, float *dst, void *data, int nChan, int nTxChan)
void applyConst(float *dst, void *data, DataType dt, int nChan)
float gaussian(float x_squared)
void ApplyN(PtexSeparableKernel &k, float *result, void *data, int nChan, int nTxChan)
void ApplyS(PtexSeparableKernel &k, float *result, void *data, int, int nTxChan)
void Apply(PtexSeparableKernel &k, float *result, void *data, int, int)
void applyConst(float weight, float *dst, void *data, Ptex::DataType dt, int nChan)
Definition PtexUtils.h:279
T min(T a, T b)
Definition PtexUtils.h:148
T max(T a, T b)
Definition PtexUtils.h:151