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 !BOOST_PP_IS_ITERATING
25 
26 #ifndef VT_FUNCTIONS_H
27 #define VT_FUNCTIONS_H
28 
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/vt/api.h"
33 #include "pxr/base/vt/array.h"
34 #include <boost/preprocessor/enum_params.hpp>
35 #include <boost/preprocessor/iterate.hpp>
36 #include <boost/preprocessor/repeat.hpp>
37 #include <vector>
38 
39 PXR_NAMESPACE_OPEN_SCOPE
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 BOOST_PP_ITERATION_PARAMS_1 (4, \
48  (0, VT_FUNCTIONS_MAX_ARGS, "pxr/base/vt/functions.h", 0))
49 #include BOOST_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 boost or cpp macros.
55 // ****************************************************************************
56 
57 // documentation for bool-result array comparison functions
58 #ifdef doxygen
59 
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 
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 
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 
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 
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 
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 
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 
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 
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 
294 PXR_NAMESPACE_CLOSE_SCOPE
295 
296 #endif // VT_FUNCTIONS_H
297 
298 #else // BOOST_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 BOOST_PP_ITERATION()
307 
308 #if BOOST_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( BOOST_PP_ENUM_PARAMS(N, VtArray<T> const &s) )
320 {
321  size_t newSize = 0;
322 
323  BOOST_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  BOOST_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 // BOOST_PP_ITERATION_FLAGS
344 
345 #undef N
346 
347 #endif // BOOST_PP_IS_ITERATING
348 
VtArray< T > VtGreater(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the first input contains values greater than ...
VtArray< T > VtEqual(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the two inputs contain equal values...
void if(!TfPyIsInitialized())
Invokes wrapFunc to wrap type T if T is not already wrapped.
Definition: pyUtils.h:210
bool VtAnyTrue(VtArray< T > const &a)
Returns true if any element of input array is not VtZero, else false.
Definition: functions.h:195
VtArray< T > VtCat(VtArray< T > const &a0, VtArray< T > const &a1,...VtArray< T > const &aN)
Concatenates arrays.
VtArray< T > VtLessOrEqual(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the first input contains values less than or ...
VtArray< T > VtNotEqual(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the two inputs contain inequal values...
size_t size() const
Return the total number of elements in this array.
Definition: array.h:428
VtArray< T > VtGreaterOrEqual(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the first input contains values greater than ...
bool VtAllTrue(VtArray< T > const &a)
Returns true if every element of input array is not VtZero, else false.
Definition: functions.h:221
VtArray< T > VtLess(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the first input contains values less than tho...
bool empty() const
Return true if this array contains no elements, false otherwise.
Definition: array.h:444