Loading...
Searching...
No Matches
bboxCache.h
1//
2// Copyright 2016 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_USD_USD_GEOM_BBOX_CACHE_H
8#define PXR_USD_USD_GEOM_BBOX_CACHE_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/usdGeom/api.h"
12#include "pxr/usd/usdGeom/xformCache.h"
14#include "pxr/usd/usd/attributeQuery.h"
15#include "pxr/base/gf/bbox3d.h"
16#include "pxr/base/tf/hash.h"
17#include "pxr/base/tf/hashmap.h"
19
20#include <optional>
21
22PXR_NAMESPACE_OPEN_SCOPE
23
24
25class UsdGeomModelAPI;
26
71{
72public:
90 USDGEOM_API
92 bool useExtentsHint=false, bool ignoreVisibility=false);
93
95 USDGEOM_API
97
99 USDGEOM_API
101
103 USDGEOM_API
105
115 USDGEOM_API
117
129 USDGEOM_API
131 const UsdPrim &prim,
132 const SdfPathSet &pathsToSkip,
133 const GfMatrix4d &primOverride,
134 const TfHashMap<SdfPath, GfMatrix4d, SdfPath::Hash> &ctmOverrides);
135
136
144 USDGEOM_API
146 const UsdPrim &relativeToAncestorPrim);
147
156 USDGEOM_API
158
169 USDGEOM_API
171
188 USDGEOM_API
190 const UsdPrim &prim,
191 const SdfPathSet &pathsToSkip,
192 const TfHashMap<SdfPath, GfMatrix4d, SdfPath::Hash> &ctmOverrides);
193
199 USDGEOM_API
200 bool
202 const UsdGeomPointInstancer& instancer,
203 int64_t const *instanceIdBegin,
204 size_t numIds,
205 GfBBox3d *result);
206
211 const UsdGeomPointInstancer& instancer, int64_t instanceId) {
212 GfBBox3d ret;
213 ComputePointInstanceWorldBounds(instancer, &instanceId, 1, &ret);
214 return ret;
215 }
216
227 USDGEOM_API
228 bool
230 const UsdGeomPointInstancer &instancer,
231 int64_t const *instanceIdBegin,
232 size_t numIds,
233 const UsdPrim &relativeToAncestorPrim,
234 GfBBox3d *result);
235
240 const UsdGeomPointInstancer &instancer,
241 int64_t instanceId,
242 const UsdPrim &relativeToAncestorPrim) {
243 GfBBox3d ret;
245 instancer, &instanceId, 1, relativeToAncestorPrim, &ret);
246 return ret;
247 }
248
257 USDGEOM_API
258 bool
260 const UsdGeomPointInstancer& instancer,
261 int64_t const *instanceIdBegin,
262 size_t numIds,
263 GfBBox3d *result);
264
268 const UsdGeomPointInstancer& instancer,
269 int64_t instanceId) {
270 GfBBox3d ret;
271 ComputePointInstanceLocalBounds(instancer, &instanceId, 1, &ret);
272 return ret;
273 }
274
275
286 USDGEOM_API
287 bool
289 const UsdGeomPointInstancer& instancer,
290 int64_t const *instanceIdBegin,
291 size_t numIds,
292 GfBBox3d *result);
293
298 const UsdGeomPointInstancer& instancer,
299 int64_t instanceId) {
300 GfBBox3d ret;
302 instancer, &instanceId, 1, &ret);
303 return ret;
304 }
305
307 USDGEOM_API
308 void Clear();
309
318 USDGEOM_API
319 void SetIncludedPurposes(const TfTokenVector& includedPurposes);
320
322 const TfTokenVector& GetIncludedPurposes() { return _includedPurposes; }
323
326 bool GetUseExtentsHint() const {
327 return _useExtentsHint;
328 }
329
332 bool GetIgnoreVisibility() const {
333 return _ignoreVisibility;
334 }
335
339 USDGEOM_API
341
343 UsdTimeCode GetTime() const { return _time; }
344
353 void SetBaseTime(UsdTimeCode baseTime) {
354 _baseTime = baseTime;
355 }
356
360 return _baseTime.value_or(GetTime());
361 }
362
366 _baseTime = std::nullopt;
367 }
368
371 bool HasBaseTime() const {
372 return static_cast<bool>(_baseTime);
373 }
374
375private:
376 // Worker task.
377 class _BBoxTask;
378
379 // Helper object for computing bounding boxes for instance prototypes.
380 class _PrototypeBBoxResolver;
381
382 // Map of purpose tokens to associated bboxes.
383 typedef std::map<TfToken, GfBBox3d, TfTokenFastArbitraryLessThan>
384 _PurposeToBBoxMap;
385
386 // Each individual prim will have it's own entry in the bbox cache. When
387 // instancing is involved we store the prototype prims and their children in
388 // the cache for use by each prim that instances each prototype. However,
389 // because of the way we compute and inherit purpose, we may end up needed
390 // to compute multitple different bboxes for prototypes and their children
391 // if the prims that instance them would cause these prototypes to inherit a
392 // different purpose value when the prims under the prototype don't have an
393 // authored purpose of their own.
394 //
395 // This struct is here to represent a prim and the purpose that it would
396 // inherit from the prim that instances it. It is used as the key for the
397 // map of prim's to the cached entries, allowing prims in prototypes to have
398 // more than one bbox cache entry for each distinct context needed to
399 // appropriately compute for all instances. instanceInheritablePurpose will
400 // always be empty for prims that aren't prototypes or children of
401 // prototypes, meaning that prims not in prototypes will only have one
402 // context each.
403 struct _PrimContext {
404 // The prim itself
405 UsdPrim prim;
406
407 // The purpose that would be inherited from the instancing prim if this
408 // prim does not have an explicit purpose.
409 TfToken instanceInheritablePurpose;
410
411 _PrimContext() = default;
412 explicit _PrimContext(const UsdPrim &prim_,
413 const TfToken &purpose = TfToken())
414 : prim(prim_), instanceInheritablePurpose(purpose) {};
415
416 bool operator==(const _PrimContext &rhs) const {
417 return prim == rhs.prim &&
418 instanceInheritablePurpose == rhs.instanceInheritablePurpose;
419 }
420
421 // Convenience stringify for debugging.
422 std::string ToString() const;
423 };
424
425 template<typename TransformType>
426 GfBBox3d _ComputeBoundWithOverridesHelper(
427 const UsdPrim &prim,
428 const SdfPathSet &pathsToSkip,
429 const TransformType &primOverride,
430 const TfHashMap<SdfPath, GfMatrix4d, SdfPath::Hash> &ctmOverrides);
431
432 bool
433 _ComputePointInstanceBoundsHelper(
434 const UsdGeomPointInstancer &instancer,
435 int64_t const *instanceIdBegin,
436 size_t numIds,
437 GfMatrix4d const &xform,
438 GfBBox3d *result);
439
440 // Returns true if the \p prim should be included during child bounds
441 // accumulation.
442 bool _ShouldIncludePrim(const UsdPrim& prim);
443
444 // True if \p attr or \p query may return different values given different
445 // time queries. Note that a true result implies the attribute may have no
446 // value, a default value or a single time sample value.
447 bool _IsVarying(const UsdAttribute& attr);
448 bool _IsVarying(const UsdAttributeQuery& query);
449
450 // Populate the local bbox for the requested prim, without the
451 // local-to-world transform or local transform applied. Return true when
452 // bbox volume > 0.
453 bool _Resolve(const UsdPrim& prim, _PurposeToBBoxMap *bboxes);
454
455 // Resolves a single prim. This method must be thread safe. Assumes the
456 // cache entry has been created for \p prim.
457 //
458 // \p inverseComponentCtm is used to combine all the child bboxes in
459 // component-relative space.
460 void _ResolvePrim(const _BBoxTask* task,
461 const _PrimContext& prim,
462 const GfMatrix4d &inverseComponentCtm);
463
464 struct _Entry {
465 _Entry()
466 : isComplete(false)
467 , isVarying(false)
468 , isIncluded(false)
469 { }
470
471 // The cached bboxes for the various values of purpose token.
472 _PurposeToBBoxMap bboxes;
473
474 // Queries for attributes that need to be re-computed at each
475 // time for this entry. This will be invalid for non-varying entries.
476 std::shared_ptr<UsdAttributeQuery[]> queries;
477
478 // Computed purpose info of the prim that's associated with the entry.
479 // This data includes the prim's actual computed purpose as well as
480 // whether this purpose is inheritable by child prims.
482
483 // True when data in the entry is valid.
484 bool isComplete;
485
486 // True when the entry varies over time.
487 bool isVarying;
488
489 // True when the entry is visible.
490 bool isIncluded;
491 };
492
493 // Returns the cache entry for the given \p prim if one already exists.
494 // If no entry exists, creates (but does not resolve) entries for
495 // \p prim and all of its descendents. In this case, the prototype prims
496 // whose bounding boxes need to be resolved in order to resolve \p prim
497 // will be returned in \p prototypePrimContexts.
498 _Entry* _FindOrCreateEntriesForPrim(
499 const _PrimContext& prim,
500 std::vector<_PrimContext> *prototypePrimContexts);
501
502 // Returns the combined bounding box for the currently included set of
503 // purposes given a _PurposeToBBoxMap.
504 GfBBox3d _GetCombinedBBoxForIncludedPurposes(
505 const _PurposeToBBoxMap &bboxes);
506
507 // Populates \p bbox with the bounding box computed from the authored
508 // extents hint. Based on the included purposes, the extents in the
509 // extentsHint attribute are combined together to compute the bounding box.
510 bool _GetBBoxFromExtentsHint(
511 const UsdGeomModelAPI &geomModel,
512 const UsdAttributeQuery &extentsHintQuery,
513 _PurposeToBBoxMap *bboxes);
514
515 // Returns whether the children of the given prim can be pruned
516 // from the traversal to pre-populate entries.
517 bool _ShouldPruneChildren(const UsdPrim &prim, _Entry *entry);
518
519 // Helper function for computing a prim's purpose info efficiently by
520 // using the parent entry's cached computed purpose info and caching it
521 // its cache entry.
522 // Optionally this can recursively compute and cache the purposes for any
523 // existing parent entries in the cache that haven't had their purposes
524 // computed yet.
525 template <bool IsRecursive>
526 void _ComputePurposeInfo(_Entry *entry, const _PrimContext &prim);
527
528 // Helper to determine if we should use extents hints for \p prim.
529 inline bool _UseExtentsHintForPrim(UsdPrim const &prim) const;
530
531 // Specialize TfHashAppend for TfHash
532 template <typename HashState>
533 friend void TfHashAppend(HashState& h, const _PrimContext &key)
534 {
535 h.Append(key.prim);
536 h.Append(key.instanceInheritablePurpose);
537 }
538
539 // Need hash_value for boost to key cache entries by prim context.
540 friend size_t hash_value(const _PrimContext &key) { return TfHash{}(key); }
541
542 typedef TfHash _PrimContextHash;
543 typedef TfHashMap<_PrimContext, _Entry, _PrimContextHash> _PrimBBoxHashMap;
544
545 // Finds the cache entry for the prim context if it exists.
546 _Entry *_FindEntry(const _PrimContext &primContext)
547 {
548 return TfMapLookupPtr(_bboxCache, primContext);
549 }
550
551 // Returns the cache entry for the prim context, adding it if doesn't
552 // exist.
553 _Entry *_InsertEntry(const _PrimContext &primContext)
554 {
555 return &(_bboxCache[primContext]);
556 }
557
558 WorkDispatcher _dispatcher;
559 UsdTimeCode _time;
560 std::optional<UsdTimeCode> _baseTime;
561 TfTokenVector _includedPurposes;
562 UsdGeomXformCache _ctmCache;
563 _PrimBBoxHashMap _bboxCache;
564 bool _useExtentsHint;
565 bool _ignoreVisibility;
566};
567
568
569PXR_NAMESPACE_CLOSE_SCOPE
570
571#endif // PXR_USD_USD_GEOM_BBOX_CACHE_H
Basic type: arbitrarily oriented 3D bounding box.
Definition bbox3d.h:67
Stores a 4x4 matrix of double elements.
Definition matrix4d.h:71
A user-extensible hashing mechanism for use with runtime hash tables.
Definition hash.h:472
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:71
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition attribute.h:183
Object for efficiently making repeated queries for attribute values.
Caches bounds by recursively computing and aggregating bounds of children in world space and aggregat...
Definition bboxCache.h:71
GfBBox3d ComputePointInstanceWorldBound(const UsdGeomPointInstancer &instancer, int64_t instanceId)
Compute the bound of the given point instance in world space.
Definition bboxCache.h:210
bool GetUseExtentsHint() const
Returns whether authored extent hints are used to compute bounding boxes.
Definition bboxCache.h:326
USDGEOM_API bool ComputePointInstanceRelativeBounds(const UsdGeomPointInstancer &instancer, int64_t const *instanceIdBegin, size_t numIds, const UsdPrim &relativeToAncestorPrim, GfBBox3d *result)
Compute the bounds of the given point instances in the space of an ancestor prim relativeToAncestorPr...
USDGEOM_API void SetIncludedPurposes(const TfTokenVector &includedPurposes)
Indicate the set of includedPurposes to use when resolving child bounds.
USDGEOM_API bool ComputePointInstanceWorldBounds(const UsdGeomPointInstancer &instancer, int64_t const *instanceIdBegin, size_t numIds, GfBBox3d *result)
Compute the bound of the given point instances in world space.
UsdTimeCode GetBaseTime() const
Return the base time if set, otherwise GetTime().
Definition bboxCache.h:359
USDGEOM_API UsdGeomBBoxCache & operator=(UsdGeomBBoxCache const &other)
Copy assignment.
UsdTimeCode GetTime() const
Get the current time from which this cache is reading values.
Definition bboxCache.h:343
bool GetIgnoreVisibility() const
Returns whether prim visibility should be ignored when computing bounding boxes.
Definition bboxCache.h:332
bool HasBaseTime() const
Return true if this cache has a baseTime that's been explicitly set, false otherwise.
Definition bboxCache.h:371
GfBBox3d ComputePointInstanceRelativeBound(const UsdGeomPointInstancer &instancer, int64_t instanceId, const UsdPrim &relativeToAncestorPrim)
Compute the bound of the given point instance in the space of an ancestor prim relativeToAncestorPrim...
Definition bboxCache.h:239
USDGEOM_API bool ComputePointInstanceUntransformedBounds(const UsdGeomPointInstancer &instancer, int64_t const *instanceIdBegin, size_t numIds, GfBBox3d *result)
Computes the bound of the given point instances, but does not include the transform (if any) authored...
GfBBox3d ComputePointInstanceUntransformedBound(const UsdGeomPointInstancer &instancer, int64_t instanceId)
Computes the bound of the given point instances, but does not include the instancer's transform.
Definition bboxCache.h:297
void ClearBaseTime()
Clear this cache's baseTime if one has been set.
Definition bboxCache.h:365
void SetBaseTime(UsdTimeCode baseTime)
Set the base time value for this bbox cache.
Definition bboxCache.h:353
USDGEOM_API void SetTime(UsdTimeCode time)
Use the new time when computing values and may clear any existing values cached for the previous time...
USDGEOM_API ~UsdGeomBBoxCache()
Destructor.
USDGEOM_API void Clear()
Clears all pre-cached values.
USDGEOM_API GfBBox3d ComputeWorldBound(const UsdPrim &prim)
Compute the bound of the given prim in world space, leveraging any pre-existing, cached bounds.
USDGEOM_API GfBBox3d ComputeUntransformedBound(const UsdPrim &prim, const SdfPathSet &pathsToSkip, const TfHashMap< SdfPath, GfMatrix4d, SdfPath::Hash > &ctmOverrides)
This is an overloaded member function, provided for convenience. It differs from the above function o...
USDGEOM_API GfBBox3d ComputeWorldBoundWithOverrides(const UsdPrim &prim, const SdfPathSet &pathsToSkip, const GfMatrix4d &primOverride, const TfHashMap< SdfPath, GfMatrix4d, SdfPath::Hash > &ctmOverrides)
Computes the bound of the prim's descendents in world space while excluding the subtrees rooted at th...
USDGEOM_API GfBBox3d ComputeRelativeBound(const UsdPrim &prim, const UsdPrim &relativeToAncestorPrim)
Compute the bound of the given prim in the space of an ancestor prim, relativeToAncestorPrim,...
USDGEOM_API bool ComputePointInstanceLocalBounds(const UsdGeomPointInstancer &instancer, int64_t const *instanceIdBegin, size_t numIds, GfBBox3d *result)
Compute the oriented bounding boxes of the given point instances.
const TfTokenVector & GetIncludedPurposes()
Get the current set of included purposes.
Definition bboxCache.h:322
USDGEOM_API UsdGeomBBoxCache(UsdTimeCode time, TfTokenVector includedPurposes, bool useExtentsHint=false, bool ignoreVisibility=false)
Construct a new BBoxCache for a specific time and set of includedPurposes.
USDGEOM_API GfBBox3d ComputeUntransformedBound(const UsdPrim &prim)
Computes the bound of the prim's children leveraging any pre-existing, cached bounds,...
USDGEOM_API GfBBox3d ComputeLocalBound(const UsdPrim &prim)
Computes the oriented bounding box of the given prim, leveraging any pre-existing,...
GfBBox3d ComputePointInstanceLocalBound(const UsdGeomPointInstancer &instancer, int64_t instanceId)
Compute the oriented bounding boxes of the given point instances.
Definition bboxCache.h:267
USDGEOM_API UsdGeomBBoxCache(UsdGeomBBoxCache const &other)
Copy constructor.
UsdGeomModelAPI extends the generic UsdModelAPI schema with geometry specific concepts such as cached...
Definition modelAPI.h:154
Encodes vectorized instancing of multiple, potentially animated, prototypes (object/instance masters)...
A caching mechanism for transform matrices.
Definition xformCache.h:41
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition prim.h:117
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition timeCode.h:72
A work dispatcher runs concurrent tasks.
Definition dispatcher.h:228
Container::mapped_type * TfMapLookupPtr(Container &map, Key const &key)
Checks if an item exists in a map or TfHashMap, without copying it.
Definition stl.h:124
Value type containing information about a prim's computed effective purpose as well as storing whethe...
Definition imageable.h:383
std::vector< TfToken > TfTokenVector
Convenience types.
Definition token.h:440