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 
60 template <typename MIPField_T>
61 class MIPLinearInterp : boost::noncopyable
62 {
63 public:
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 
74  MIPLinearInterp(const MIPField_T &mip);
75 
76  // Main methods ---
77 
80  value_type sample(const V3d &vsP, const float wsSpotSize) const;
81 
82 private:
83 
84  // Structs ---
85 
86  struct InterpInfo
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 
116  const MIPField_T& m_mip;
118  std::vector<float> m_wsVoxelSize;
121 
122 };
123 
124 //----------------------------------------------------------------------------//
125 // MIPLinearInterp implementations
126 //----------------------------------------------------------------------------//
127 
128 template <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));
139  m_wsVoxelSize.push_back(wsMinVoxelSize * factor);
140  }
141 }
142 
143 //----------------------------------------------------------------------------//
144 
145 template <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
165  V3f mipVsP0, mipVsP1;
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 
176 template <typename MIPField_T>
178 MIPLinearInterp<MIPField_T>::interpInfo(const float wsSpotSize) const
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 
FIELD3D_NAMESPACE_HEADER_CLOSE
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition: ns.h:58
MIPLinearInterp::InterpInfo::InterpInfo
InterpInfo(const size_t l, const size_t u, const float t)
Definition: MIPInterp.h:94
MIPField.h
Contains the MIPField class.
MIPLinearInterp::m_wsVoxelSize
std::vector< float > m_wsVoxelSize
Min world space voxel size for each MIP level.
Definition: MIPInterp.h:118
MIPLinearInterp::m_interp
LinearInterpType m_interp
Linear interpolator.
Definition: MIPInterp.h:120
V3d
Imath::V3d V3d
Definition: SpiMathLib.h:74
MIPLinearInterp::FieldType
MIPField_T::NestedType FieldType
Definition: MIPInterp.h:67
MIPLinearInterp::InterpInfo::InterpInfo
InterpInfo()
Definition: MIPInterp.h:91
V3f
Imath::V3f V3f
Definition: SpiMathLib.h:73
MIPLinearInterp::sample
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
MIPLinearInterp::LinearInterpType
FieldType::LinearInterp LinearInterpType
Definition: MIPInterp.h:68
MIPLinearInterp
Definition: MIPInterp.h:62
FIELD3D_LERPFACTOR
#define FIELD3D_LERPFACTOR
Definition: SpiMathLib.h:92
ns.h
MIPLinearInterp::value_type
FieldType::value_type value_type
Definition: MIPInterp.h:69
MIPLinearInterp::InterpInfo::lerpT
float lerpT
Parametric position between finest and coarser.
Definition: MIPInterp.h:105
MIPLinearInterp::MIPLinearInterp
MIPLinearInterp(const MIPField_T &mip)
Must be constructed with a MIP field to operate on.
Definition: MIPInterp.h:129
MIPLinearInterp::InterpInfo::upper
size_t upper
Coarser level.
Definition: MIPInterp.h:103
FIELD3D_LERP
#define FIELD3D_LERP
Definition: SpiMathLib.h:91
MIPLinearInterp::m_mip
const MIPField_T & m_mip
Const reference to MIP field.
Definition: MIPInterp.h:116
MIPLinearInterp::InterpInfo
Definition: MIPInterp.h:87
FIELD3D_NAMESPACE_OPEN
Definition: FieldMapping.cpp:74
MIPLinearInterp::InterpInfo::lower
size_t lower
Finest level.
Definition: MIPInterp.h:101
MIPLinearInterp::interpInfo
InterpInfo interpInfo(const float wsSpotSize) const
Computes between which levels to interpolate.
Definition: MIPInterp.h:178
detail::min
T min(const T a, const T2 b)
Min operation on mixed types.
Definition: FieldSampler.h:25