PtexTriangleKernel.cpp
Go to the documentation of this file.
/*
PTEX SOFTWARE
Copyright 2014 Disney Enterprises, Inc. All rights reserved
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
Studios" or the names of its contributors may NOT be used to
endorse or promote products derived from this software without
specific prior written permission from Walt Disney Pictures.
Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*/
#include "PtexPlatform.h"
#include "PtexUtils.h"
#include "PtexHalf.h"
#include "PtexTriangleKernel.h"
PTEX_NAMESPACE_BEGIN
namespace {
inline float gaussian(float x_squared)
{
static const float scale = -0.5f * (PtexTriangleKernelWidth * PtexTriangleKernelWidth);
return (float)exp(scale * x_squared);
}
// apply to 1..4 channels (unrolled channel loop) of packed data (nTxChan==nChan)
// the ellipse equation, Q, is calculated via finite differences (Heckbert '89 pg 57)
template<class T, int nChan>
void Apply(PtexTriangleKernelIter& k, float* result, void* data, int /*nChan*/, int /*nTxChan*/)
{
int nTxChan = nChan;
float DDQ = 2.0f*k.A;
for (int vi = k.v1; vi != k.v2; vi++) {
int xw = k.rowlen - vi;
int x1 = PtexUtils::max(k.u1, xw-k.w2);
int x2 = PtexUtils::min(k.u2, xw-k.w1);
float U = (float)x1 - k.u;
float V = (float)vi - k.v;
float DQ = k.A*(2.0f*U+1.0f)+k.B*V;
float Q = k.A*U*U + (k.B*U + k.C*V)*V;
T* p = static_cast<T*>(data) + (vi * k.rowlen + x1) * nTxChan;
T* pEnd = p + (x2-x1)*nTxChan;
for (; p < pEnd; p += nTxChan) {
if (Q < 1.0f) {
float weight = gaussian(Q)*k.wscale;
k.weight += weight;
PtexUtils::VecAccum<T,nChan>()(result, p, weight);
}
Q += DQ;
DQ += DDQ;
}
}
}
// apply to 1..4 channels (unrolled channel loop) w/ pixel stride
template<class T, int nChan>
void ApplyS(PtexTriangleKernelIter& k, float* result, void* data, int /*nChan*/, int nTxChan)
{
float DDQ = 2.0f*k.A;
for (int vi = k.v1; vi != k.v2; vi++) {
int xw = k.rowlen - vi;
int x1 = PtexUtils::max(k.u1, xw-k.w2);
int x2 = PtexUtils::min(k.u2, xw-k.w1);
float U = (float)x1 - k.u;
float V = (float)vi - k.v;
float DQ = k.A*(2.0f*U+1.0f)+k.B*V;
float Q = k.A*U*U + (k.B*U + k.C*V)*V;
T* p = static_cast<T*>(data) + (vi * k.rowlen + x1) * nTxChan;
T* pEnd = p + (x2-x1)*nTxChan;
for (; p < pEnd; p += nTxChan) {
if (Q < 1.0f) {
float weight = gaussian(Q)*k.wscale;
k.weight += weight;
PtexUtils::VecAccum<T,nChan>()(result, p, weight);
}
Q += DQ;
DQ += DDQ;
}
}
}
// apply to N channels (general case)
template<class T>
void ApplyN(PtexTriangleKernelIter& k, float* result, void* data, int nChan, int nTxChan)
{
float DDQ = 2.0f*k.A;
for (int vi = k.v1; vi != k.v2; vi++) {
int xw = k.rowlen - vi;
int x1 = PtexUtils::max(k.u1, xw-k.w2);
int x2 = PtexUtils::min(k.u2, xw-k.w1);
float U = (float)x1 - k.u;
float V = (float)vi - k.v;
float DQ = k.A*(2.0f*U+1.0f)+k.B*V;
float Q = k.A*U*U + (k.B*U + k.C*V)*V;
T* p = static_cast<T*>(data) + (vi * k.rowlen + x1) * nTxChan;
T* pEnd = p + (x2-x1)*nTxChan;
for (; p < pEnd; p += nTxChan) {
if (Q < 1.0f) {
float weight = gaussian(Q)*k.wscale;
k.weight += weight;
PtexUtils::VecAccumN<T>()(result, p, nChan, weight);
}
Q += DQ;
DQ += DDQ;
}
}
}
}
PtexTriangleKernelIter::ApplyFn
PtexTriangleKernelIter::applyFunctions[] = {
// nChan == nTxChan
ApplyN<uint8_t>, ApplyN<uint16_t>, ApplyN<PtexHalf>, ApplyN<float>,
Apply<uint8_t,1>, Apply<uint16_t,1>, Apply<PtexHalf,1>, Apply<float,1>,
Apply<uint8_t,2>, Apply<uint16_t,2>, Apply<PtexHalf,2>, Apply<float,2>,
Apply<uint8_t,3>, Apply<uint16_t,3>, Apply<PtexHalf,3>, Apply<float,3>,
Apply<uint8_t,4>, Apply<uint16_t,4>, Apply<PtexHalf,4>, Apply<float,4>,
// nChan != nTxChan (need pixel stride)
ApplyN<uint8_t>, ApplyN<uint16_t>, ApplyN<PtexHalf>, ApplyN<float>,
ApplyS<uint8_t,1>, ApplyS<uint16_t,1>, ApplyS<PtexHalf,1>, ApplyS<float,1>,
ApplyS<uint8_t,2>, ApplyS<uint16_t,2>, ApplyS<PtexHalf,2>, ApplyS<float,2>,
ApplyS<uint8_t,3>, ApplyS<uint16_t,3>, ApplyS<PtexHalf,3>, ApplyS<float,3>,
ApplyS<uint8_t,4>, ApplyS<uint16_t,4>, ApplyS<PtexHalf,4>, ApplyS<float,4>,
};
void PtexTriangleKernelIter::applyConst(float* dst, void* data, DataType dt, int nChan)
{
// iterate over texel locations and calculate weight as if texture weren't const
float DDQ = 2.0f*A;
for (int vi = v1; vi != v2; vi++) {
int xw = rowlen - vi;
int x1 = PtexUtils::max(u1, xw-w2);
int x2 = PtexUtils::min(u2, xw-w1);
float U = (float)x1 - u;
float V = (float)vi - v;
float DQ = A*(2.0f*U+1.0f)+B*V;
float Q = A*U*U + (B*U + C*V)*V;
for (int x = x1; x < x2; x++) {
if (Q < 1.0f) {
weight += gaussian(Q)*wscale;
}
Q += DQ;
DQ += DDQ;
}
}
// apply weight to single texel value
PtexUtils::applyConst(weight, dst, data, dt, nChan);
}
PTEX_NAMESPACE_END
PtexHalf.h
Half-precision floating-point type.
PtexPlatform.h
Platform-specific classes, functions, and includes.
PtexTriangleKernel.h
PtexTriangleKernelWidth
PTEX_NAMESPACE_BEGIN const float PtexTriangleKernelWidth
Definition PtexTriangleKernel.h:50
PtexUtils.h
PTEX_NAMESPACE_END
#define PTEX_NAMESPACE_END
Definition PtexVersion.h:62
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::w2
int w2
Definition PtexTriangleKernel.h:59
PtexTriangleKernelIter::u1
int u1
Definition PtexTriangleKernel.h:58
PtexTriangleKernelIter::weight
float weight
Definition PtexTriangleKernel.h:63
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
PTEX_NAMESPACE_BEGIN
Definition PtexSeparableKernel.cpp:42
PTEX_NAMESPACE_BEGIN::gaussian
float gaussian(float x_squared)
Definition PtexTriangleKernel.cpp:44
PTEX_NAMESPACE_BEGIN::ApplyN
void ApplyN(PtexSeparableKernel &k, float *result, void *data, int nChan, int nTxChan)
Definition PtexSeparableKernel.cpp:105
PTEX_NAMESPACE_BEGIN::ApplyS
void ApplyS(PtexSeparableKernel &k, float *result, void *data, int, int nTxChan)
Definition PtexSeparableKernel.cpp:75
PTEX_NAMESPACE_BEGIN::Apply
void Apply(PtexSeparableKernel &k, float *result, void *data, int, int)
Definition PtexSeparableKernel.cpp:45
PtexUtils::applyConst
void applyConst(float weight, float *dst, void *data, Ptex::DataType dt, int nChan)
Definition PtexUtils.h:279
PtexUtils::min
T min(T a, T b)
Definition PtexUtils.h:148
PtexUtils::max
T max(T a, T b)
Definition PtexUtils.h:151
PtexUtils::VecAccumN
Definition PtexUtils.h:247
PtexUtils::VecAccum
Definition PtexUtils.h:233