All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
boundsCache.h
1 //
2 // Copyright 2017 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 #ifndef __GUSD_BOUNDSCACHE_H__
25 #define __GUSD_BOUNDSCACHE_H__
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/prim.h"
29 #include "pxr/usd/usdGeom/bboxCache.h"
30 #include "pxr/base/tf/token.h"
31 
32 #include "USD_DataCache.h"
33 
34 #include <UT/UT_BoundingBox.h>
35 #include <UT/UT_IntrusivePtr.h>
36 #include <UT/UT_ConcurrentHashMap.h>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
48 
49 class GusdBoundsCache : public GusdUSD_DataCache {
50 public:
51  static GusdBoundsCache& GetInstance();
52 
54  virtual ~GusdBoundsCache();
55 
56  bool ComputeWorldBound(
57  const UsdPrim &prim,
58  UsdTimeCode time,
59  const TfTokenVector &includedPurposes,
60  UT_BoundingBox &bounds );
61 
62  bool ComputeUntransformedBound(
63  const UsdPrim &prim,
64  UsdTimeCode time,
65  const TfTokenVector &includedPurposes,
66  UT_BoundingBox &bounds );
67 
68  virtual void Clear() override;
69  virtual int64 Clear(const UT_StringSet& stageNames) override;
70 
71 private:
72 
73  // Key that hashes the stage file name and a set of purposes.
74  struct Key
75  {
76  Key() : hash(0) {}
77 
78  Key(const TfToken &path, TfTokenVector purposes)
79  : path(path), purposes( purposes ), hash(ComputeHash(path,purposes)) {}
80 
81  static std::size_t ComputeHash(const TfToken &path, TfTokenVector purposes)
82  {
83  std::size_t h = hash_value(path);
84  boost::hash_combine(h, purposes);
85  return h;
86  }
87 
88  bool operator==(const Key& o) const
89  { return path == o.path &&
90  purposes == o.purposes ; }
91 
92  friend size_t hash_value(const Key& o)
93  { return o.hash; }
94 
95  struct HashCmp
96  {
97  static std::size_t hash(const Key& key)
98  { return key.hash; }
99  static bool equal(const Key& a,
100  const Key& b)
101  { return a == b; }
102  };
103 
104  TfToken path;
105  TfTokenVector purposes;
106  std::size_t hash;
107  };
108 
109  struct Item : public UT_IntrusiveRefCounter<Item>
110  {
111  Item( UsdTimeCode time, const TfTokenVector& includedPurposes )
112  : bboxCache( time, includedPurposes )
113  {
114  }
115 
116  UsdGeomBBoxCache bboxCache;
117  std::mutex lock;
118  };
119 
120  typedef GfBBox3d (UsdGeomBBoxCache::*ComputeFunc)(const UsdPrim& prim);
121 
122  bool _ComputeBound(
123  const UsdPrim &prim,
124  UsdTimeCode time,
125  const TfTokenVector &includedPurposes,
126  ComputeFunc boundFunc,
127  UT_BoundingBox &bounds );
128 
129  typedef UT_IntrusivePtr<Item> ItemHandle;
130 
131  typedef UT_ConcurrentHashMap<Key,ItemHandle,Key::HashCmp> MapType;
132  MapType m_map;
133 };
134 
135 PXR_NAMESPACE_CLOSE_SCOPE
136 
137 #endif // __GUSD_BOUNDSCACHE_H__
Caches bounds by recursively computing and aggregating bounds of children in world space and aggregat...
Definition: bboxCache.h:87
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:131
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
A wrapper arround UsdGeomBBoxCache.
Definition: boundsCache.h:49
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.
Basic type: arbitrarily oriented 3D bounding box.
Definition: bbox3d.h:84