Field3D
MIPInterp.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------//
2
3/*
4 * Copyright (c) 2009 Sony Pictures Imageworks Inc
5 *
6 * 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
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the
17 * distribution. Neither the name of Sony Pictures Imageworks nor the
18 * names of its contributors may be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36//----------------------------------------------------------------------------//
37
43//----------------------------------------------------------------------------//
44
45#ifndef _INCLUDED_Field3D_MIPInterp_H_
46#define _INCLUDED_Field3D_MIPInterp_H_
47
48#include "MIPField.h"
49
50//----------------------------------------------------------------------------//
51
52#include "ns.h"
53
55
56//----------------------------------------------------------------------------//
57// MIPInterp
58//----------------------------------------------------------------------------//
59
60template <typename MIPField_T>
61class MIPLinearInterp : boost::noncopyable
62{
63public:
64
65 // Typedefs ---
66
67 typedef typename MIPField_T::NestedType FieldType;
68 typedef typename FieldType::LinearInterp LinearInterpType;
69 typedef typename FieldType::value_type value_type;
70
71 // Ctors ---
72
75
76 // Main methods ---
77
80 value_type sample(const V3d &vsP, const float wsSpotSize) const;
81
82private:
83
84 // Structs ---
85
87 {
88
89 // Ctors ---
90
92 : lower(0), upper(0), lerpT(0.0f)
93 { }
94 InterpInfo(const size_t l, const size_t u, const float t)
95 : lower(l), upper(u), lerpT(t)
96 { }
97
98 // Data members ---
99
101 size_t lower;
103 size_t upper;
105 float lerpT;
106 };
107
108 // Utility methods ---
109
111 InterpInfo interpInfo(const float wsSpotSize) const;
112
113 // Data members ---
114
118 std::vector<float> m_wsVoxelSize;
121
122};
123
124//----------------------------------------------------------------------------//
125// MIPLinearInterp implementations
126//----------------------------------------------------------------------------//
127
128template <typename MIPField_T>
130 : m_mip(mip)
131{
132 // Base voxel size (represents finest level)
133 const V3f wsVoxelSize = mip.mapping()->wsVoxelSize(0, 0, 0);
134 const float wsMinVoxelSize =
135 std::min(std::min(wsVoxelSize.x, wsVoxelSize.y), wsVoxelSize.z);
136 // All subsequent levels are a 2x mult on the base voxel size
137 for (size_t i = 0, end = mip.numLevels(); i < end; ++i) {
138 const float factor = std::pow(2.0f, static_cast<float>(i));
140 }
141}
142
143//----------------------------------------------------------------------------//
144
145template <typename MIPField_T>
148 const float wsSpotSize) const
149{
150 const InterpInfo i = interpInfo(wsSpotSize);
151
152 if (i.lower == i.upper) {
153 // Special case where spot size is not in-between levels
154 if (i.lower == 0) {
155 // Special case for 0-level
156 return m_interp.sample(*m_mip.rawMipLevel(0), vsP);
157 } else {
158 // Not the 0-level, so we must transform vsP
159 V3f mipVsP;
160 m_mip.getVsMIPCoord(vsP, i.lower, mipVsP);
161 return m_interp.sample(*m_mip.rawMipLevel(i.lower), mipVsP);
162 }
163 } else {
164 // Quadrilinear interpolation
166 m_mip.getVsMIPCoord(V3f(vsP), i.lower, mipVsP0);
167 m_mip.getVsMIPCoord(V3f(vsP), i.upper, mipVsP1);
168 const value_type v0 = m_interp.sample(*m_mip.rawMipLevel(i.lower), mipVsP0);
169 const value_type v1 = m_interp.sample(*m_mip.rawMipLevel(i.upper), mipVsP1);
170 return FIELD3D_LERP(v0, v1, i.lerpT);
171 }
172}
173
174//----------------------------------------------------------------------------//
175
176template <typename MIPField_T>
179{
180 const size_t numLevels = m_mip.numLevels();
181 // First case, spot size smaller than first level
182 if (wsSpotSize <= m_wsVoxelSize[0]) {
183 return InterpInfo();
184 }
185 // Check in-between sizes
186 for (size_t i = 1, end = numLevels; i < end; ++i) {
187 if (wsSpotSize <= m_wsVoxelSize[i]) {
188 const float lerpT = FIELD3D_LERPFACTOR(wsSpotSize, m_wsVoxelSize[i - 1],
189 m_wsVoxelSize[i]);
190 return InterpInfo(i - 1, i, lerpT);
191 }
192 }
193 // Final case, spot size larger or equal to highest level
194 return InterpInfo(numLevels - 1, numLevels - 1, 0.0f);
195}
196
197//----------------------------------------------------------------------------//
198
200
201//----------------------------------------------------------------------------//
202
203#endif // Include guard
204
Contains the MIPField class.
Imath::V3d V3d
Definition SpiMathLib.h:74
#define FIELD3D_LERP
Definition SpiMathLib.h:91
#define FIELD3D_LERPFACTOR
Definition SpiMathLib.h:92
Imath::V3f V3f
Definition SpiMathLib.h:73
#define FIELD3D_MTX_T
Definition StdMathLib.h:99
LinearInterpType m_interp
Linear interpolator.
Definition MIPInterp.h:120
MIPLinearInterp(const MIPField_T &mip)
Must be constructed with a MIP field to operate on.
Definition MIPInterp.h:129
MIPField_T::NestedType FieldType
Definition MIPInterp.h:67
value_type sample(const V3d &vsP, const float wsSpotSize) const
Performs interpolation. A MIP field interpolation requires a spot size (which may be zero,...
Definition MIPInterp.h:147
std::vector< float > m_wsVoxelSize
Min world space voxel size for each MIP level.
Definition MIPInterp.h:118
FieldType::value_type value_type
Definition MIPInterp.h:69
FieldType::LinearInterp LinearInterpType
Definition MIPInterp.h:68
InterpInfo interpInfo(const float wsSpotSize) const
Computes between which levels to interpolate.
Definition MIPInterp.h:178
const MIPField_T & m_mip
Const reference to MIP field.
Definition MIPInterp.h:116
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition ns.h:58
size_t upper
Coarser level.
Definition MIPInterp.h:103
size_t lower
Finest level.
Definition MIPInterp.h:101
InterpInfo(const size_t l, const size_t u, const float t)
Definition MIPInterp.h:94
float lerpT
Parametric position between finest and coarser.
Definition MIPInterp.h:105