Ptex
PtexTriangleKernel.cpp
Go to the documentation of this file.
1 /*
2 PTEX SOFTWARE
3 Copyright 2014 Disney Enterprises, Inc. All rights reserved
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
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 
22 Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
23 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
25 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
26 IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 OF 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 
43 namespace {
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 
153 void 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 
PtexUtils::min
T min(T a, T b)
Definition: PtexUtils.h:147
PtexTriangleKernelIter::weight
float weight
Definition: PtexTriangleKernel.h:63
PTEX_NAMESPACE_END
#define PTEX_NAMESPACE_END
Definition: PtexVersion.h:62
PtexTriangleKernelIter::v2
int v2
Definition: PtexTriangleKernel.h:59
PtexTriangleKernelIter::applyFunctions
static ApplyFn applyFunctions[40]
Definition: PtexTriangleKernel.h:77
PtexUtils.h
PtexTriangleKernelIter::C
float C
Definition: PtexTriangleKernel.h:60
PtexTriangleKernelWidth
PTEX_NAMESPACE_BEGIN const float PtexTriangleKernelWidth
Definition: PtexTriangleKernel.h:50
PtexTriangleKernelIter::applyConst
void applyConst(float *dst, void *data, DataType dt, int nChan)
Definition: PtexTriangleKernel.cpp:153
PtexTriangleKernelIter::wscale
float wscale
Definition: PtexTriangleKernel.h:62
PtexUtils::VecAccum
Definition: PtexUtils.h:200
Ptex::DataType
DataType
Type of data stored in texture file.
Definition: Ptexture.h:85
PtexTriangleKernelIter::ApplyFn
void(* ApplyFn)(PtexTriangleKernelIter &k, float *dst, void *data, int nChan, int nTxChan)
Definition: PtexTriangleKernel.h:76
PtexTriangleKernelIter::A
float A
Definition: PtexTriangleKernel.h:60
PTEX_NAMESPACE_BEGIN
Definition: PtexSeparableKernel.cpp:42
PtexTriangleKernelIter::u1
int u1
Definition: PtexTriangleKernel.h:58
PTEX_NAMESPACE_BEGIN::Apply
void Apply(PtexTriangleKernelIter &k, float *result, void *data, int, int)
Definition: PtexTriangleKernel.cpp:53
PtexTriangleKernelIter::rowlen
int rowlen
Definition: PtexTriangleKernel.h:56
PtexUtils::applyConst
void applyConst(float weight, float *dst, void *data, Ptex::DataType dt, int nChan)
Definition: PtexUtils.h:246
PtexTriangleKernelIter::u
float u
Definition: PtexTriangleKernel.h:57
PtexUtils::VecAccumN
Definition: PtexUtils.h:214
PtexTriangleKernelIter::u2
int u2
Definition: PtexTriangleKernel.h:59
PtexTriangleKernelIter
Triangle filter kernel iterator (in texel coords)
Definition: PtexTriangleKernel.h:54
PtexTriangleKernelIter::v
float v
Definition: PtexTriangleKernel.h:57
PtexTriangleKernelIter::w1
int w1
Definition: PtexTriangleKernel.h:58
PtexUtils::max
T max(T a, T b)
Definition: PtexUtils.h:150
PtexTriangleKernelIter::v1
int v1
Definition: PtexTriangleKernel.h:58
PTEX_NAMESPACE_BEGIN::gaussian
float gaussian(float x_squared)
Definition: PtexTriangleKernel.cpp:44
PTEX_NAMESPACE_BEGIN::ApplyN
void ApplyN(PtexTriangleKernelIter &k, float *result, void *data, int nChan, int nTxChan)
Definition: PtexTriangleKernel.cpp:108
PtexTriangleKernelIter::w2
int w2
Definition: PtexTriangleKernel.h:59
PtexTriangleKernelIter::B
float B
Definition: PtexTriangleKernel.h:60
PTEX_NAMESPACE_BEGIN::ApplyS
void ApplyS(PtexTriangleKernelIter &k, float *result, void *data, int, int nTxChan)
Definition: PtexTriangleKernel.cpp:81
PtexTriangleKernel.h
PtexPlatform.h
Platform-specific classes, functions, and includes.
PtexHalf.h
Half-precision floating-point type.