Loading...
Searching...
No Matches
indexedWeightsOperand.h
Go to the documentation of this file.
1//
2// Copyright 2025 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_EXEC_VDF_INDEXED_WEIGHTS_OPERAND_H
8#define PXR_EXEC_VDF_INDEXED_WEIGHTS_OPERAND_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/vdf/api.h"
16
17#include <vector>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
39class VdfIndexedWeightsOperand : public VdfIndexedWeights
40{
42
43public:
63 Union,
64 Intersection,
65 };
66
73 VDF_API
75 SetOperation setOperation,
76 const VdfIndexedWeights *externalWeights = NULL);
77
84 VDF_API
85 void Swap(VdfIndexedWeights *v);
86
98 VDF_API
99 void PruneZeros(const std::vector<This> &operands);
100
113 VDF_API
114 void Fill(
115 const std::vector<This> &operands,
116 double fillWeight,
117 bool nonZeroSetOperation);
118
121 VDF_API
122 size_t GetNumMathErrors() const;
123
128 VDF_API
130
134 This operator-() const {
135 This w(*this);
136 return w *= -1.0;
137 }
138
142 This operator+(double s) const {
143 This w(*this);
144 return w += s;
145 }
146
150 This operator-(double s) const {
151 This w(*this);
152 return w -= s;
153 }
154
158 This operator*(double s) const {
159 This w(*this);
160 return w *= s;
161 }
162
166 This operator/(double s) const {
167 This w(*this);
168 return w /= s;
169 }
170
174 This operator+(const This &v) const {
175 This w(*this);
176 return w += v;
177 }
178
183 This operator-(const This &v) const {
184 This w(*this);
185 return w -= v;
186 }
187
191 This operator*(const This &v) const {
192 This w(*this);
193 return w *= v;
194 }
195
199 This operator/(const This &v) const {
200 This w(*this);
201 return w /= v;
202 }
203
214
215 VDF_API This operator<(const This &v) const;
216 VDF_API This operator<=(const This &v) const;
217 VDF_API This operator>(const This &v) const;
218 VDF_API This operator>=(const This &v) const;
219 VDF_API This operator==(const This &v) const;
220 VDF_API This operator!=(const This &v) const;
221 VDF_API This operator<(double x) const;
222 VDF_API This operator<=(double x) const;
223 VDF_API This operator>(double x) const;
224 VDF_API This operator>=(double x) const;
225 VDF_API This operator==(double x) const;
226 VDF_API This operator!=(double x) const;
227
229
236
237 VDF_API This acos() const;
238 VDF_API This acosh() const;
239 VDF_API This asin() const;
240 VDF_API This asinh() const;
241 VDF_API This atan() const;
242 VDF_API This atanh() const;
243 VDF_API This atan2(const This &v) const;
244 VDF_API This ceil() const;
245 VDF_API This cos() const;
246 VDF_API This cosh() const;
247 VDF_API This exp() const;
248 VDF_API This fabs() const;
249 VDF_API This floor() const;
250 VDF_API This fmod(float denominator) const;
251 VDF_API This log() const;
252 VDF_API This log10() const;
253 VDF_API This pow(float exponent) const;
254 VDF_API This sin() const;
255 VDF_API This sinh() const;
256 VDF_API This sqrt() const;
257 VDF_API This tan() const;
258 VDF_API This tanh() const;
259
261
265
270 VDF_API
271 This min(const This &v) const;
272
277 VDF_API
278 This max(const This &v) const;
279
283 VDF_API
284 This min(float min) const;
285
289 VDF_API
290 This max(float max) const;
291
295 VDF_API
296 This clamp(float min, float max) const;
297
302 VDF_API
303 This smoothstep(float min, float max,
304 float slope0=0, float slope1=0) const;
305
310 VDF_API
311 This smoothramp(float min, float max,
312 float shoulder0, float shoulder1) const;
313
318 VDF_API
319 This lerp(const This &v, float a) const;
320
325 VDF_API
326 This lerp(const This &v, const This &a) const;
327
329
333
336 bool HasExternalWeights() const {
337 return _externalWeights;
338 }
339
341
342private:
343 // Operators ---------------------------------------------------------------
344
345 // These mutating arithmetic-assignment operators are kept private and only
346 // used by the public operators to generate a modified copied to return.
347
348 // Adds the weights of v to our weights.
349 VDF_API
350 This &operator+=(const This &v);
351
352 // Subtracts the weights of v from our weights.
353 VDF_API
354 This &operator-=(const This &v);
355
356 // Scales our weights by the weights of v.
357 VDF_API
358 This &operator*=(const This &v);
359
360 // Divides our weights by the weights of v.
361 VDF_API
362 This &operator/=(const This &v);
363
364 // Adds a scalar to all our weights.
365 VDF_API
366 This &operator+=(double s);
367
368 // Subtracts a scalar from all our weights.
369 VDF_API
370 This &operator-=(double s);
371
372 // Scales all our weights by a scalar.
373 VDF_API
374 This &operator*=(double s);
375
376 // Divides all our weights by a scalar.
377 VDF_API
378 This& operator/=(double s);
379
380 // Math library implementation helpers -------------------------------------
381
382 // Common implementation for math functions. A new VdfIndexedWeightOperand
383 // is returned having a copy of this object's weights, mutated by calling
384 // \p modify on each weight. If template parameter \p CheckForMathErrors
385 // is \c true, the result of each call is checked for math errors (as
386 // defined above) and the return object is flagged if present. This check
387 // is opt-in due to the potential cost; callers should take great care in
388 // the state of this check based on the requirements of \c ModifyFn.
389 //
390 // \c ModifyFn should have the effective call signature of a unary function
391 // of type \c float.
392 template <bool CheckForMathErrors, typename ModifyFn>
393 This _ApplyFunctionToCopy(ModifyFn modify) const;
394
395 // Read/write helpers ------------------------------------------------------
396
397 // Makes a local copy of the external weights (that can be modified).
398 void _CopyExternalWeights();
399
400 // Returns the non-const indices.
401 std::vector<int> &_GetWriteIndices() {
402 return VdfIndexedWeights::_GetWriteIndices();
403 }
404
405 // Returns the indices.
406 const std::vector<int> &_GetReadIndices() const {
407 return _externalWeights
408 ? VdfIndexedWeights::_GetReadIndices(_externalWeights)
409 : VdfIndexedWeights::_GetReadIndices();
410 }
411
412 // Returns the non-const data.
413 std::vector<float> &_GetWriteData() {
414 return VdfIndexedWeights::_GetWriteData();
415 }
416
417 // Returns the data.
418 const std::vector<float> &_GetReadData() const {
419 return _externalWeights
420 ? VdfIndexedWeights::_GetReadData(_externalWeights)
421 : VdfIndexedWeights::_GetReadData();
422 }
423
424 // Allow access to the free function overloads that can't be implemented
425 // in terms of other public operators due to the write semantics of this
426 // class or for performance reasons.
427 friend VDF_API This operator-(double, const This&);
428 friend VDF_API This operator/(double, const This&);
429
430private:
431 // The set operation to be used for binary operators.
432 SetOperation _setOperation;
433
434 // The external weights (NULL when we do not have external weights).
435 // Note that it is fine to copy this pointer as part of copy construction/
436 // assignment since that just means to share the same external weights.
437 const VdfIndexedWeights *_externalWeights;
438
439 // Flag indicating that there might be math errors (to avoid always having
440 // to check all weights).
441 bool _mayHaveMathErrors;
442};
443
445//
447VDF_API VdfIndexedWeightsOperand operator-(double, const VdfIndexedWeightsOperand&);
448VDF_API VdfIndexedWeightsOperand operator*(double, const VdfIndexedWeightsOperand&);
449VDF_API VdfIndexedWeightsOperand operator/(double, const VdfIndexedWeightsOperand&);
450
451VDF_API VdfIndexedWeightsOperand operator>(double, const VdfIndexedWeightsOperand&);
452VDF_API VdfIndexedWeightsOperand operator<(double, const VdfIndexedWeightsOperand&);
453VDF_API VdfIndexedWeightsOperand operator>=(double, const VdfIndexedWeightsOperand&);
454VDF_API VdfIndexedWeightsOperand operator<=(double, const VdfIndexedWeightsOperand&);
455VDF_API VdfIndexedWeightsOperand operator==(double, const VdfIndexedWeightsOperand&);
456VDF_API VdfIndexedWeightsOperand operator!=(double, const VdfIndexedWeightsOperand&);
457
458PXR_NAMESPACE_CLOSE_SCOPE
459
460#endif // #ifndef PXR_EXEC_VDF_INDEXED_WEIGHTS_OPERAND_H
Used to perform math operations on VdfIndexedWeights.
VDF_API This clamp(float min, float max) const
Returns a new VdfIndexedWeightsOperand where each indexed weight is clamped between the scalars min a...
VDF_API VdfIndexedWeightsOperand(SetOperation setOperation, const VdfIndexedWeights *externalWeights=NULL)
Creates an indexed weights operand with the given setOperation and optional external weights.
VDF_API This min(const This &v) const
Range-of-weights methods.
VDF_API This min(float min) const
Returns a new VdfIndexedWeightsOperand where each indexed weight is the minimum of that weight in thi...
This operator*(double s) const
Returns a new VdfIndexedWeightsOperand having the weights of this object multiplied by scalar s.
VDF_API This smoothramp(float min, float max, float shoulder0, float shoulder1) const
Returns a new VdfIndexedWeightsOperand where each indexed weight is smoothramped between the scalars ...
VDF_API This smoothstep(float min, float max, float slope0=0, float slope1=0) const
Returns a new VdfIndexedWeightsOperand where each indexed weight is smoothsteped between the scalars ...
This operator+(const This &v) const
Returns a new VdfIndexedWeightsOperand having the weights of VdfIndexedWeightsOperand v added to the ...
SetOperation
The set operation used by binary operations.
VDF_API This max(float max) const
Returns a new VdfIndexedWeightsOperand where each indexed weight is the minimum of that weight in thi...
VDF_API size_t GetNumMathErrors() const
Returns the number of math errors (weights which are inf or NaN).
VDF_API void Swap(VdfIndexedWeights *v)
Swaps the indexed weights held by this opernad with the given indexed weights.
This operator*(const This &v) const
Returns a new VdfIndexedWeightsOperand having the weights of this object multiplied by the weights of...
bool HasExternalWeights() const
Returns whether or not this object references external weights.
VDF_API This lerp(const This &v, const This &a) const
Returns a new VdfIndexedWeightsOperand where each indexed weight is lerped from itself and a correspo...
VDF_API This max(const This &v) const
Returns a new VdfIndexedWeightsOperand where each indexed weight is the maximum of that weight in thi...
VDF_API void Fill(const std::vector< This > &operands, double fillWeight, bool nonZeroSetOperation)
Fills this operand with fillWeight according to the set operation and the indices in operands.
VDF_API void PruneZeros(const std::vector< This > &operands)
Prunes zeros according to the set operation and the indices in operands.
This operator+(double s) const
Returns a new VdfIndexedWeightsOperand having the scalar s added to the weights of this object.
This operator/(double s) const
Returns a new VdfIndexedWeightsOperand having the weights of this object divided by scalar s.
VDF_API This acos() const
Standard math library functions.
This operator-(const This &v) const
Returns a new VdfIndexedWeightsOperand having the weights of VdfIndexedWeightsOperand v subtracted fr...
This operator-() const
Returns a new VdfIndexedWeightsOperand having the weights of this object negated.
VDF_API void ClearMathErrors()
Clears any pending math errors.
VDF_API This operator<(const This &v) const
Component-wise comparisons.
VDF_API This lerp(const This &v, float a) const
Returns a new VdfIndexedWeightsOperand where each indexed weight is lerped from itself and a correspo...
This operator-(double s) const
Returns a new VdfIndexedWeightsOperand having the scalar s subtracted from the weights of this object...
This operator/(const This &v) const
Returns a new VdfIndexedWeightsOperand having the weights of this object divided by the weights of Vd...
VDF_API VdfIndexedWeightsOperand operator+(double, const VdfIndexedWeightsOperand &)
Declare the double arithmetic and comparison free function overloads.