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 <SYS/SYS_Hash.h>
35 #include <UT/UT_BoundingBox.h>
36 #include <UT/UT_IntrusivePtr.h>
38 
39 #include <mutex>
40 
42 
43 /// A wrapper arround UsdGeomBBoxCache.
44 ///
45 /// This singleton class keeps a cache per stage and per purpose.
46 /// It will be flushed when the stage cache is flushed.
47 ///
48 /// Unfortunaly UsdGeomBBoxCaches only store a single frame at
49 /// a time. I considered creating a cache per frame but I thought
50 /// that would defeat optimizations for non animated geometry.
51 
53 public:
54  static GusdBoundsCache& GetInstance();
55 
57  ~GusdBoundsCache() override;
58 
59  bool ComputeWorldBound(
60  const UsdPrim &prim,
62  const TfTokenVector &includedPurposes,
63  UT_BoundingBox &bounds );
64 
66  const UsdPrim &prim,
67  UsdTimeCode time,
68  const TfTokenVector &includedPurposes,
69  UT_BoundingBox &bounds );
70 
71  void Clear() override;
72  int64 Clear(const UT_StringSet& stageNames) override;
73 
74 private:
75 
76  // Key that hashes the stage file name and a set of purposes.
77  struct Key
78  {
79  Key() : hash(0) {}
80 
81  Key(const TfToken &path, TfTokenVector purposes)
82  : path(path), purposes( purposes ), hash(ComputeHash(path,purposes)) {}
83 
84  static std::size_t ComputeHash(const TfToken &path, TfTokenVector purposes)
85  {
86  std::size_t h = hash_value(path);
87  SYShashCombine(h, purposes);
88  return h;
89  }
90 
91  bool operator==(const Key& o) const
92  { return path == o.path &&
93  purposes == o.purposes ; }
94 
95  friend size_t hash_value(const Key& o)
96  { return o.hash; }
97 
98  struct HashCmp
99  {
100  static std::size_t hash(const Key& key)
101  { return key.hash; }
102  static bool equal(const Key& a,
103  const Key& b)
104  { return a == b; }
105  };
106 
107  TfToken path;
108  TfTokenVector purposes;
109  std::size_t hash;
110  };
111 
112  struct Item : public UT_IntrusiveRefCounter<Item>
113  {
114  Item( UsdTimeCode time, const TfTokenVector& includedPurposes )
115  : bboxCache( time, includedPurposes )
116  {
117  }
118 
119  UsdGeomBBoxCache bboxCache;
120  std::mutex lock;
121  };
122 
123  typedef GfBBox3d (UsdGeomBBoxCache::*ComputeFunc)(const UsdPrim& prim);
124 
125  bool _ComputeBound(
126  const UsdPrim &prim,
127  UsdTimeCode time,
128  const TfTokenVector &includedPurposes,
129  ComputeFunc boundFunc,
130  UT_BoundingBox &bounds );
131 
132  typedef UT_IntrusivePtr<Item> ItemHandle;
133 
134  typedef UT_ConcurrentHashMap<Key,ItemHandle,Key::HashCmp> MapType;
135  MapType m_map;
136 };
137 
139 
140 #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:102
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:133
static std::size_t hash(const Key &key)
Definition: boundsCache.h:100
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
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:1432
#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