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 
39 
40 /// A wrapper arround UsdGeomBBoxCache.
41 ///
42 /// This singleton class keeps a cache per stage and per purpose.
43 /// It will be flushed when the stage cache is flushed.
44 ///
45 /// Unfortunaly UsdGeomBBoxCaches only store a single frame at
46 /// a time. I considered creating a cache per frame but I thought
47 /// that would defeat optimizations for non animated geometry.
48 
50 public:
51  static GusdBoundsCache& GetInstance();
52 
54  ~GusdBoundsCache() override;
55 
56  bool ComputeWorldBound(
57  const UsdPrim &prim,
59  const TfTokenVector &includedPurposes,
60  UT_BoundingBox &bounds );
61 
63  const UsdPrim &prim,
64  UsdTimeCode time,
65  const TfTokenVector &includedPurposes,
66  UT_BoundingBox &bounds );
67 
68  void Clear() override;
69  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_NS::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 
136 
137 #endif // __GUSD_BOUNDSCACHE_H__
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
void Clear() override
Clear all caches.
GT_API const UT_StringHolder time
static GusdBoundsCache & GetInstance()
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
A reference counter base class for use with UT_IntrusivePtr.
static bool equal(const Key &a, const Key &b)
Definition: boundsCache.h:99
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
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
Wrapper around hboost::intrusive_ptr.
long long int64
Definition: SYS_Types.h:116
Definition: prim.h:132
static std::size_t hash(const Key &key)
Definition: boundsCache.h:97
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
#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