HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
functions.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #if !HBOOST_PP_IS_ITERATING
25 
26 #ifndef PXR_BASE_VT_FUNCTIONS_H
27 #define PXR_BASE_VT_FUNCTIONS_H
28 
29 /// \file vt/functions.h
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/vt/api.h"
33 #include "pxr/base/vt/array.h"
34 #include <hboost/preprocessor/enum_params.hpp>
35 #include <hboost/preprocessor/iterate.hpp>
36 #include <hboost/preprocessor/repeat.hpp>
37 #include <vector>
38 
40 
41 #define VT_FUNCTIONS_MAX_ARGS 6
42 
43 // Set up preprocessor iterations to allow various functions to accept multiple
44 // arguments.
45 
46 // VtCat
47 #define HBOOST_PP_ITERATION_PARAMS_1 (4, \
48  (0, VT_FUNCTIONS_MAX_ARGS, "pxr/base/vt/functions.h", 0))
49 #include HBOOST_PP_ITERATE()
50 
51 // ****************************************************************************
52 // Doc headers for functions that are generated through macros. These get
53 // decent doxygen (and epydoc) output even though we can't put them on the
54 // real functions because they're expanded through hboost or cpp macros.
55 // ****************************************************************************
56 
57 // documentation for bool-result array comparison functions
58 #ifdef doxygen
59 
60 /// Returns a bool array specifying, element-by-element, if the two inputs
61 /// contain equal values. The shape of the return array is the same as the
62 /// shape of the largest input array.
63 ///
64 /// If one input is a single element (either a single-element array or a scalar
65 /// of the same type held in the array), it is compared to all the elements
66 /// in the other array. Otherwise both arrays must have the same shape.
67 template<typename T>
69 VtEqual( VtArray<T> const &a, VtArray<T> const &b );
70 template<typename T>
72 VtEqual( T const &a, VtArray<T> const &b );
73 template<typename T>
75 VtEqual( VtArray<T> const &a, T const &b );
76 
77 /// Returns a bool array specifying, element-by-element, if the two inputs
78 /// contain inequal values. The shape of the return array is the same as the
79 /// shape of the largest input array.
80 ///
81 /// If one input is a single element (either a single-element array or a scalar
82 /// of the same type held in the array), it is compared to all the elements
83 /// in the other array. Otherwise both arrays must have the same shape.
84 template<typename T>
86 VtNotEqual( VtArray<T> const &a, VtArray<T> const &b );
87 template<typename T>
89 VtNotEqual( T const &a, VtArray<T> const &b );
90 template<typename T>
92 VtNotEqual( VtArray<T> const &a, T const &b );
93 
94 /// Returns a bool array specifying, element-by-element, if the first input
95 /// contains values greater than those in the second input. The shape of the
96 /// return array is the same as the shape of the largest input array.
97 ///
98 /// If one input is a single element (either a single-element array or a scalar
99 /// of the same type held in the array), it is compared to all the elements
100 /// in the other array. Otherwise both arrays must have the same shape.
101 template<typename T>
103 VtGreater( VtArray<T> const &a, VtArray<T> const &b );
104 template<typename T>
106 VtGreater( T const &a, VtArray<T> const &b );
107 template<typename T>
109 VtGreater( VtArray<T> const &a, T const &b );
110 
111 /// Returns a bool array specifying, element-by-element, if the first input
112 /// contains values less than those in the second input. The shape of the
113 /// return array is the same as the shape of the largest input array.
114 ///
115 /// If one input is a single element (either a single-element array or a scalar
116 /// of the same type held in the array), it is compared to all the elements
117 /// in the other array. Otherwise both arrays must have the same shape.
118 template<typename T>
120 VtLess( VtArray<T> const &a, VtArray<T> const &b );
121 template<typename T>
123 VtLess( T const &a, VtArray<T> const &b );
124 template<typename T>
126 VtLess( VtArray<T> const &a, T const &b );
127 
128 /// Returns a bool array specifying, element-by-element, if the first input
129 /// contains values greater than or equal to those in the second input. The
130 /// shape of the return array is the same as the shape of the largest input
131 /// array.
132 ///
133 /// If one input is a single element (either a single-element array or a scalar
134 /// of the same type held in the array), it is compared to all the elements
135 /// in the other array. Otherwise both arrays must have the same shape.
136 template<typename T>
138 VtGreaterOrEqual( VtArray<T> const &a, VtArray<T> const &b );
139 template<typename T>
141 VtGreaterOrEqual( T const &a, VtArray<T> const &b );
142 template<typename T>
144 VtGreaterOrEqual( VtArray<T> const &a, T const &b );
145 
146 /// Returns a bool array specifying, element-by-element, if the first input
147 /// contains values less than or equal to those in the second input. The shape
148 /// of the return array is the same as the shape of the largest input array.
149 ///
150 /// If one input is a single element (either a single-element array or a scalar
151 /// of the same type held in the array), it is compared to all the elements
152 /// in the other array. Otherwise both arrays must have the same shape.
153 template<typename T>
155 VtLessOrEqual( VtArray<T> const &a, VtArray<T> const &b );
156 template<typename T>
158 VtLessOrEqual( T const &a, VtArray<T> const &b );
159 template<typename T>
161 VtLessOrEqual( VtArray<T> const &a, T const &b );
162 
163 #endif
164 
165 // provide documentation for functions with variable numbers of inputs
166 #ifdef doxygen
167 
168 /// Concatenates arrays.
169 ///
170 /// The result is an array with length equal to the sum of the number
171 /// of elements in the source arrays.
172 template<typename T>
174 VtCat( VtArray<T> const &a0, VtArray<T> const &a1, ... VtArray<T> const &aN);
175 
176 #endif
177 
178 // ****************************************************************************
179 // Fixed-number-of-arguments functions go here (no preprocessor iteration to
180 // handle multiple args)
181 // ****************************************************************************
182 
183 /// Returns true if any element of input array is not VtZero, else false.
184 ///
185 /// Intended to be used to evaluate results of boolean operations on arrays, e.g.
186 /// \code
187 /// a = Vt.StringArray((3,),("foo","bar","baz"))
188 /// t = Vt.AnyTrue(Vt.Equal(a,"bar"))
189 /// \endcode
190 ///
191 /// (This example, if
192 /// you look carefully, evaluates this function not on the strings, but on
193 /// the results of the comparison).
194 template<typename T>
195 bool VtAnyTrue(VtArray<T> const &a)
196 {
197  if (a.empty())
198  return false;
199 
200  for (size_t i = 0; i != a.size(); ++i) {
201  if (a[i] != VtZero<T>())
202  return true;
203  }
204 
205  return false;
206 }
207 
208 
209 /// Returns true if every element of input array is not VtZero, else false.
210 ///
211 /// Intended to be used to evaluate results of boolean operations on arrays, e.g.
212 /// \code
213 /// a = Vt.StringArray((3,),("foo","bar","baz"))
214 /// t = Vt.AllTrue(Vt.Equal(a,"bar"))
215 /// \endcode
216 ///
217 /// (This example, if you look carefully, evaluates this function not on the
218 /// strings, but on the results of the comparison).
219 template<typename T>
220 bool
222 {
223  if (a.empty())
224  return false;
225 
226  for (size_t i = 0; i != a.size(); ++i) {
227  if (a[i] == VtZero<T>())
228  return false;
229  }
230 
231  return true;
232 }
233 
234 // Macro defining functions for element-by-element comparison
235 // operators (i.e. Equal, etc). There are three versions; each returns
236 // a VtBoolArray of the same shape as the largest input.
237 // *) two input arrays:
238 // If one array contains a single element, it is compared to all the elements
239 // in the other array. Otherwise both arrays must have the same shape.
240 // *) scalar and array:
241 // The scalar is compared to all the elements in the array.
242 // *) array and scalar:
243 // The same as scalar and array.
244 #define VTFUNCTION_BOOL(funcname,op) \
245 template<typename T> \
246 VtArray<bool> \
247 funcname(T const &scalar, VtArray<T> const &vec) { \
248  VtArray<bool> ret(vec.size()); \
249  for (size_t i = 0, n = vec.size(); i != n; ++i) { \
250  ret[i] = (scalar op vec[i]); \
251  } \
252  return ret; \
253 } \
254 template<typename T> \
255 VtArray<bool> \
256 funcname(VtArray<T> const &vec, T const &scalar) { \
257  VtArray<bool> ret(vec.size()); \
258  for (size_t i = 0, n = vec.size(); i != n; ++i) { \
259  ret[i] = (vec[i] op scalar); \
260  } \
261  return ret; \
262 } \
263 template<typename T> \
264 VtArray<bool> \
265 funcname(VtArray<T> const &a, VtArray<T> const &b) \
266 { \
267  if (a.empty() || b.empty()) { \
268  return VtArray<bool>(); \
269  } \
270  \
271  if (a.size() == 1) { \
272  return funcname(a[0], b); \
273  } else if (b.size() == 1) { \
274  return funcname(a, b[0]); \
275  } else if (a.size() == b.size()) { \
276  VtArray<bool> ret(a.size()); \
277  for (size_t i = 0, n = a.size(); i != n; ++i) { \
278  ret[i] = (a[i] op b[i]); \
279  } \
280  return ret; \
281  } else { \
282  TF_CODING_ERROR("Non-conforming inputs."); \
283  return VtArray<bool>(); \
284  } \
285 }
286 
287 VTFUNCTION_BOOL(VtEqual,==)
288 VTFUNCTION_BOOL(VtNotEqual,!=)
289 VTFUNCTION_BOOL(VtGreater,>)
290 VTFUNCTION_BOOL(VtLess,<)
291 VTFUNCTION_BOOL(VtGreaterOrEqual,>=)
292 VTFUNCTION_BOOL(VtLessOrEqual,<=)
293 
295 
296 #endif // PXR_BASE_VT_FUNCTIONS_H
297 
298 #else // HBOOST_PP_IS_ITERATING
299 
300 // ****************************************************************************
301 // Variable-number-of-arguments functions go here; preprocessor iteration
302 // includes this file again and again, but turns off the pieces we don't
303 // want to use for a particular function.
304 // ****************************************************************************
305 
306 #define N HBOOST_PP_ITERATION()
307 
308 #if HBOOST_PP_ITERATION_FLAGS() == 0 // VtCat
309 
310 #define VtCat_SIZE(dummy, n, dummy2) newSize += s##n.size();
311 #define VtCat_COPY(dummy, n, dummy2) \
312  for (size_t i = 0; i < s##n.size(); ++i) \
313  ret[offset+i] = s##n[i]; \
314  offset += s##n.size();
315 
316 // real documentation is above (for doxygen purposes)
317 template<typename T>
319 VtCat( HBOOST_PP_ENUM_PARAMS(N, VtArray<T> const &s) )
320 {
321  size_t newSize = 0;
322 
323  HBOOST_PP_REPEAT( N, VtCat_SIZE, ignored )
324 
325  if (newSize == 0)
326  return VtArray<T>();
327 
328  // new array with flattened size
329  VtArray<T> ret(newSize);
330 
331  // fill it with data from old arrays
332 #if N > 0
333  size_t offset = 0;
334  HBOOST_PP_REPEAT( N, VtCat_COPY, ignored )
335 #endif
336 
337  return ret;
338 }
339 
340 #undef VtCat_SIZE
341 #undef VtCat_COPY
342 
343 #endif // HBOOST_PP_ITERATION_FLAGS
344 
345 #undef N
346 
347 #endif // HBOOST_PP_IS_ITERATING
348 
bool VtAnyTrue(VtArray< T > const &a)
Definition: functions.h:195
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
GLintptr offset
Definition: glcorearb.h:665
Definition: types.h:170
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
#define VTFUNCTION_BOOL(funcname, op)
Definition: functions.h:244
bool VtAllTrue(VtArray< T > const &a)
Definition: functions.h:221
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GA_API const UT_StringHolder N