HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
boundsCache.h
Go to the documentation of this file.
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"
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>
37 
38 #include <mutex>
39 
41 
42 /// A wrapper arround UsdGeomBBoxCache.
43 ///
44 /// This singleton class keeps a cache per stage and per purpose.
45 /// It will be flushed when the stage cache is flushed.
46 ///
47 /// Unfortunaly UsdGeomBBoxCaches only store a single frame at
48 /// a time. I considered creating a cache per frame but I thought
49 /// that would defeat optimizations for non animated geometry.
50 
52 public:
53  static GusdBoundsCache& GetInstance();
54 
56  ~GusdBoundsCache() override;
57 
58  bool ComputeWorldBound(
59  const UsdPrim &prim,
61  const TfTokenVector &includedPurposes,
62  UT_BoundingBox &bounds );
63 
65  const UsdPrim &prim,
66  UsdTimeCode time,
67  const TfTokenVector &includedPurposes,
68  UT_BoundingBox &bounds );
69 
70  void Clear() override;
71  int64 Clear(const UT_StringSet& stageNames) override;
72 
73 private:
74 
75  // Key that hashes the stage file name and a set of purposes.
76  struct Key
77  {
78  Key() : hash(0) {}
79 
80  Key(const TfToken &path, TfTokenVector purposes)
81  : path(path), purposes( purposes ), hash(ComputeHash(path,purposes)) {}
82 
83  static std::size_t ComputeHash(const TfToken &path, TfTokenVector purposes)
84  {
85  std::size_t h = hash_value(path);
86  BOOST_NS::hash_combine(h, purposes);
87  return h;
88  }
89 
90  bool operator==(const Key& o) const
91  { return path == o.path &&
92  purposes == o.purposes ; }
93 
94  friend size_t hash_value(const Key& o)
95  { return o.hash; }
96 
97  struct HashCmp
98  {
99  static std::size_t hash(const Key& key)
100  { return key.hash; }
101  static bool equal(const Key& a,
102  const Key& b)
103  { return a == b; }
104  };
105 
106  TfToken path;
107  TfTokenVector purposes;
108  std::size_t hash;
109  };
110 
111  struct Item : public UT_IntrusiveRefCounter<Item>
112  {
113  Item( UsdTimeCode time, const TfTokenVector& includedPurposes )
114  : bboxCache( time, includedPurposes )
115  {
116  }
117 
118  UsdGeomBBoxCache bboxCache;
119  std::mutex lock;
120  };
121 
122  typedef GfBBox3d (UsdGeomBBoxCache::*ComputeFunc)(const UsdPrim& prim);
123 
124  bool _ComputeBound(
125  const UsdPrim &prim,
126  UsdTimeCode time,
127  const TfTokenVector &includedPurposes,
128  ComputeFunc boundFunc,
129  UT_BoundingBox &bounds );
130 
131  typedef UT_IntrusivePtr<Item> ItemHandle;
132 
133  typedef UT_ConcurrentHashMap<Key,ItemHandle,Key::HashCmp> MapType;
134  MapType m_map;
135 };
136 
138 
139 #endif // __GUSD_BOUNDSCACHE_H__
void Clear() override
Clear all caches.
GT_API const UT_StringHolder time
static GusdBoundsCache & GetInstance()
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
A reference counter base class for use with UT_IntrusivePtr.
static bool equal(const Key &a, const Key &b)
Definition: boundsCache.h:101
bool ComputeWorldBound(const UsdPrim &prim, UsdTimeCode time, const TfTokenVector &includedPurposes, UT_BoundingBox &bounds)
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
Definition: token.h:87
Wrapper around hboost::intrusive_ptr.
long long int64
Definition: SYS_Types.h:116
Definition: prim.h:135
static std::size_t hash(const Key &key)
Definition: boundsCache.h:99
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool ComputeUntransformedBound(const UsdPrim &prim, UsdTimeCode time, const TfTokenVector &includedPurposes, UT_BoundingBox &bounds)
size_t hash_value(const CH_ChannelRef &ref)
~GusdBoundsCache() override