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 w *= -1.0;
137 return w;
138 }
139
143 This operator+(double s) const {
144 This w(*this);
145 w += s;
146 return w;
147 }
148
152 This operator-(double s) const {
153 This w(*this);
154 w -= s;
155 return w;
156 }
157
161 This operator*(double s) const {
162 This w(*this);
163 w *= s;
164 return w;
165 }
166
170 This operator/(double s) const {
171 This w(*this);
172 w /= s;
173 return w;
174 }
175
179 This operator+(const This &v) const {
180 This w(*this);
181 w += v;
182 return w;
183 }
184
189 This operator-(const This &v) const {
190 This w(*this);
191 w -= v;
192 return w;
193 }
194
198 This operator*(const This &v) const {
199 This w(*this);
200 w *= v;
201 return w;
202 }
203
207 This operator/(const This &v) const {
208 This w(*this);
209 w /= v;
210 return w;
211 }
212
223
224 VDF_API This operator<(const This &v) const;
225 VDF_API This operator<=(const This &v) const;
226 VDF_API This operator>(const This &v) const;
227 VDF_API This operator>=(const This &v) const;
228 VDF_API This operator==(const This &v) const;
229 VDF_API This operator!=(const This &v) const;
230 VDF_API This operator<(double x) const;
231 VDF_API This operator<=(double x) const;
232 VDF_API This operator>(double x) const;
233 VDF_API This operator>=(double x) const;
234 VDF_API This operator==(double x) const;
235 VDF_API This operator!=(double x) const;
236
238
245
246 VDF_API This acos() const;
247 VDF_API This acosh() const;
248 VDF_API This asin() const;
249 VDF_API This asinh() const;
250 VDF_API This atan() const;
251 VDF_API This atanh() const;
252 VDF_API This atan2(const This &v) const;
253 VDF_API This ceil() const;
254 VDF_API This cos() const;
255 VDF_API This cosh() const;
256 VDF_API This exp() const;
257 VDF_API This fabs() const;
258 VDF_API This floor() const;
259 VDF_API This fmod(float denominator) const;
260 VDF_API This log() const;
261 VDF_API This log10() const;
262 VDF_API This pow(float exponent) const;
263 VDF_API This sin() const;
264 VDF_API This sinh() const;
265 VDF_API This sqrt() const;
266 VDF_API This tan() const;
267 VDF_API This tanh() const;
268
270
274
279 VDF_API
280 This min(const This &v) const;
281
286 VDF_API
287 This max(const This &v) const;
288
292 VDF_API
293 This min(float min) const;
294
298 VDF_API
299 This max(float max) const;
300
304 VDF_API
305 This clamp(float min, float max) const;
306
311 VDF_API
312 This smoothstep(float min, float max,
313 float slope0=0, float slope1=0) const;
314
319 VDF_API
320 This smoothramp(float min, float max,
321 float shoulder0, float shoulder1) const;
322
327 VDF_API
328 This lerp(const This &v, float a) const;
329
334 VDF_API
335 This lerp(const This &v, const This &a) const;
336
338
342
345 bool HasExternalWeights() const {
346 return _externalWeights;
347 }
348
350
351private:
352 // Operators ---------------------------------------------------------------
353
354 // These mutating arithmetic-assignment operators are kept private and only
355 // used by the public operators to generate a modified copied to return.
356
357 // Adds the weights of v to our weights.
358 VDF_API
359 This &operator+=(const This &v);
360
361 // Subtracts the weights of v from our weights.
362 VDF_API
363 This &operator-=(const This &v);
364
365 // Scales our weights by the weights of v.
366 VDF_API
367 This &operator*=(const This &v);
368
369 // Divides our weights by the weights of v.
370 VDF_API
371 This &operator/=(const This &v);
372
373 // Adds a scalar to all our weights.
374 VDF_API
375 This &operator+=(double s);
376
377 // Subtracts a scalar from all our weights.
378 VDF_API
379 This &operator-=(double s);
380
381 // Scales all our weights by a scalar.
382 VDF_API
383 This &operator*=(double s);
384
385 // Divides all our weights by a scalar.
386 VDF_API
387 This& operator/=(double s);
388
389 // Math library implementation helpers -------------------------------------
390
391 // Common implementation for math functions. A new VdfIndexedWeightOperand
392 // is returned having a copy of this object's weights, mutated by calling
393 // \p modify on each weight. If template parameter \p CheckForMathErrors
394 // is \c true, the result of each call is checked for math errors (as
395 // defined above) and the return object is flagged if present. This check
396 // is opt-in due to the potential cost; callers should take great care in
397 // the state of this check based on the requirements of \c ModifyFn.
398 //
399 // \c ModifyFn should have the effective call signature of a unary function
400 // of type \c float.
401 template <bool CheckForMathErrors, typename ModifyFn>
402 This _ApplyFunctionToCopy(ModifyFn modify) const;
403
404 // Read/write helpers ------------------------------------------------------
405
406 // Makes a local copy of the external weights (that can be modified).
407 void _CopyExternalWeights();
408
409 // Returns the non-const indices.
410 std::vector<int> &_GetWriteIndices() {
411 return VdfIndexedWeights::_GetWriteIndices();
412 }
413
414 // Returns the indices.
415 const std::vector<int> &_GetReadIndices() const {
416 return _externalWeights
417 ? VdfIndexedWeights::_GetReadIndices(_externalWeights)
418 : VdfIndexedWeights::_GetReadIndices();
419 }
420
421 // Returns the non-const data.
422 std::vector<float> &_GetWriteData() {
423 return VdfIndexedWeights::_GetWriteData();
424 }
425
426 // Returns the data.
427 const std::vector<float> &_GetReadData() const {
428 return _externalWeights
429 ? VdfIndexedWeights::_GetReadData(_externalWeights)
430 : VdfIndexedWeights::_GetReadData();
431 }
432
433 // Allow access to the free function overloads that can't be implemented
434 // in terms of other public operators due to the write semantics of this
435 // class or for performance reasons.
436 friend VDF_API This operator-(double, const This&);
437 friend VDF_API This operator/(double, const This&);
438
439private:
440 // The set operation to be used for binary operators.
441 SetOperation _setOperation;
442
443 // The external weights (NULL when we do not have external weights).
444 // Note that it is fine to copy this pointer as part of copy construction/
445 // assignment since that just means to share the same external weights.
446 const VdfIndexedWeights *_externalWeights;
447
448 // Flag indicating that there might be math errors (to avoid always having
449 // to check all weights).
450 bool _mayHaveMathErrors;
451};
452
454//
456VDF_API VdfIndexedWeightsOperand operator-(double, const VdfIndexedWeightsOperand&);
457VDF_API VdfIndexedWeightsOperand operator*(double, const VdfIndexedWeightsOperand&);
458VDF_API VdfIndexedWeightsOperand operator/(double, const VdfIndexedWeightsOperand&);
459
460VDF_API VdfIndexedWeightsOperand operator>(double, const VdfIndexedWeightsOperand&);
461VDF_API VdfIndexedWeightsOperand operator<(double, const VdfIndexedWeightsOperand&);
462VDF_API VdfIndexedWeightsOperand operator>=(double, const VdfIndexedWeightsOperand&);
463VDF_API VdfIndexedWeightsOperand operator<=(double, const VdfIndexedWeightsOperand&);
464VDF_API VdfIndexedWeightsOperand operator==(double, const VdfIndexedWeightsOperand&);
465VDF_API VdfIndexedWeightsOperand operator!=(double, const VdfIndexedWeightsOperand&);
466
467PXR_NAMESPACE_CLOSE_SCOPE
468
469#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.