Field3D
MinMaxUtil.h
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 
3 /*
4  * Copyright (c) 2015 Sony Pictures Imageworks Inc,
5  * Pixar Animation Studios
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the
18  * distribution. Neither the name of Sony Pictures Imageworks nor the
19  * names of its contributors may be used to endorse or promote
20  * products derived from this software without specific prior written
21  * permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34  * OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 //----------------------------------------------------------------------------//
38 
43 //----------------------------------------------------------------------------//
44 
45 #ifndef _INCLUDED_Field3D_MinMaxUtil_H_
46 #define _INCLUDED_Field3D_MinMaxUtil_H_
47 
48 //----------------------------------------------------------------------------//
49 
50 #include <vector>
51 
52 #include <boost/thread/thread.hpp>
53 #include <boost/thread/mutex.hpp>
54 
55 #include "MIPUtil.h"
56 
57 //----------------------------------------------------------------------------//
58 
59 #include "ns.h"
60 
62 
63 //----------------------------------------------------------------------------//
64 // Functions
65 //----------------------------------------------------------------------------//
66 
68 template <typename MIPField_T>
69 std::pair<typename MIPField_T::Ptr, typename MIPField_T::Ptr>
70 makeMinMax(const typename MIPField_T::NestedType &base,
71  const float resMult, const size_t numThreads);
72 
73 //----------------------------------------------------------------------------//
74 // Constants
75 //----------------------------------------------------------------------------//
76 
78 extern const char* k_minSuffix;
80 extern const char* k_maxSuffix;
81 
82 //----------------------------------------------------------------------------//
83 // Function implementations
84 //----------------------------------------------------------------------------//
85 
86 template <typename MIPField_T>
87 std::pair<typename MIPField_T::Ptr, typename MIPField_T::Ptr>
88 makeMinMax(const typename MIPField_T::NestedType &base,
89  const float resMult, const size_t numThreads)
90 {
91  typedef typename MIPField_T::Ptr MipPtr;
92  typedef typename MIPField_T::NestedType Field;
93  typedef typename MIPField_T::NestedType::Ptr FieldPtr;
94 
95  // Storage for results
96  std::pair<MipPtr, MipPtr> result;
97 
98  // First, downsample the field into a min and max representation ---
99 
100  V3i srcRes = base.dataWindow().size() + Field3D::V3i(1);
101  V3i res = V3f(srcRes) * std::min(1.0f, resMult);
102 
103  // Corner case handling
104  res.x = std::max(res.x, 2);
105  res.y = std::max(res.y, 2);
106  res.z = std::max(res.z, 2);
107 
108  // Storage for min/max fields
109  FieldPtr minSrc(new Field);
110  FieldPtr maxSrc(new Field);
111 
112  // Resample
113  resample(base, *minSrc, res, MinFilter());
114  resample(base, *maxSrc, res, MaxFilter());
115 
116  // Second, generate MIP representations ---
117 
118  result.first = makeMIP<MIPField_T, MinFilter>(*minSrc, 2, numThreads);
119  result.second = makeMIP<MIPField_T, MaxFilter>(*maxSrc, 2, numThreads);
120 
121  return result;
122 }
123 
124 //----------------------------------------------------------------------------//
125 
127 
128 //----------------------------------------------------------------------------//
129 
130 #endif // Include guard
FIELD3D_NAMESPACE_HEADER_CLOSE
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition: ns.h:58
FieldRes::dataWindow
const Box3i & dataWindow() const
Returns the data window. Any coordinate inside this window is safe to pass to value() in the Field su...
Definition: Field.h:253
V3i
Imath::V3i V3i
Definition: SpiMathLib.h:71
k_minSuffix
const char * k_minSuffix
The standard 'min' suffix - "_min".
Definition: MinMaxUtil.cpp:55
resample
FIELD3D_NAMESPACE_OPEN bool resample(const Field_T &src, Field_T &tgt, const V3i &newRes, const FilterOp_T &filter)
Resamples the source field into the target field, such that the new data window is @dataWindow.
Definition: Resample.h:560
V3f
Imath::V3f V3f
Definition: SpiMathLib.h:73
MaxFilter
Definition: Resample.h:210
ns.h
detail::max
T max(const T a, const T2 b)
Max operation on mixed types.
Definition: FieldSampler.h:32
makeMinMax
FIELD3D_NAMESPACE_OPEN std::pair< typename MIPField_T::Ptr, typename MIPField_T::Ptr > makeMinMax(const typename MIPField_T::NestedType &base, const float resMult, const size_t numThreads)
Constructs a min/max MIP representation of the given field.
Definition: MinMaxUtil.h:88
k_maxSuffix
const char * k_maxSuffix
The standard 'max' suffix - "_max".
Definition: MinMaxUtil.cpp:56
FIELD3D_NAMESPACE_OPEN
Definition: FieldMapping.cpp:74
Field
Definition: Field.h:390
detail::min
T min(const T a, const T2 b)
Min operation on mixed types.
Definition: FieldSampler.h:25
MIPUtil.h
Contains MIP-related utility functions.
MinFilter
Definition: Resample.h:154