HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XUSD_AutoCollection.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Side Effects Software Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef __XUSD_AutoCollection_h__
19 #define __XUSD_AutoCollection_h__
20 
21 #include "HUSD_API.h"
22 #include "HUSD_TimeCode.h"
23 #include "HUSD_Utils.h"
24 #include "XUSD_PathSet.h"
25 #include <UT/UT_Array.h>
26 #include <UT/UT_StringHolder.h>
27 #include <UT/UT_StringMap.h>
28 #include <pxr/usd/sdf/path.h>
29 #include <pxr/usd/usd/stage.h>
30 
31 extern "C" {
32  SYS_VISIBILITY_EXPORT extern void newAutoCollection(void *unused);
33 };
34 class HUSD_AutoAnyLock;
35 
37 
39 
41 {
42 public:
44  { }
46  { }
47 
48  virtual bool canCreateAutoCollection(const char *token) const = 0;
49  virtual XUSD_AutoCollection *create(
50  const UT_StringHolder &collectionname,
51  const UT_StringArray &orderedargs,
52  const UT_StringMap<UT_StringHolder> &namedargs,
53  HUSD_AutoAnyLock &lock,
55  int nodeid,
56  const HUSD_TimeCode &timecode) const = 0;
57 };
58 
59 template <class AutoCollection>
61 {
62 public:
64  : myCollectionName(cname)
65  { }
67  { }
68 
70  const char *cname) const override
71  {
72  return (myCollectionName == cname);
73  }
75  const UT_StringHolder &collectionname,
76  const UT_StringArray &orderedargs,
77  const UT_StringMap<UT_StringHolder> &namedargs,
78  HUSD_AutoAnyLock &lock,
80  int nodeid,
81  const HUSD_TimeCode &timecode) const override
82  {
83  if (collectionname == myCollectionName)
84  return new AutoCollection(collectionname, orderedargs, namedargs,
85  lock, demands, nodeid, timecode);
86 
87  return nullptr;
88  }
89 
90 private:
91  UT_StringHolder myCollectionName;
92 };
93 
95 {
96 public:
98  const UT_StringHolder &collectionname,
99  const UT_StringArray &orderedargs,
100  const UT_StringMap<UT_StringHolder> &namedargs,
101  HUSD_AutoAnyLock &lock,
103  int nodeid,
104  const HUSD_TimeCode &timecode);
105  virtual ~XUSD_AutoCollection();
106 
107  // Determines whether or not this class works in random access mode.
108  virtual bool randomAccess() const = 0;
109 
110  // INSTANCE ID CONTRACT (applies to every `instance_ids` output below):
111  // When a point instancer is matched per-instance, the id array stored for
112  // that instancer MUST be sorted in ascending order and contain no
113  // duplicates. The pattern matching layer relies on this invariant: it is
114  // carried unchanged into the XUSD_InstanceMatchData payload, and
115  // XUSD_PathPattern::combineMatchData performs union/intersection/difference
116  // as a linear merge of two sorted, duplicate-free arrays. Implementations
117  // that gather ids in instancer (authored) order must sort and de-duplicate
118  // before returning, e.g. with UT_Array::sortAndRemoveDuplicates(); the
119  // shared matchPointInstances() helper already does this.
120 
121  // A non-random access auto collection does its own traversal of the stage
122  // all at once when the auto collection is created, generating a full set
123  // of all matching paths at once. See the INSTANCE ID CONTRACT above for the
124  // ordering requirement on `instance_ids`.
125  virtual void matchPrimitives(XUSD_PathSet &matches,
126  UT_StringMap<UT_Array<int64>> *instance_ids)
127  const
128  { UT_ASSERT(false); }
129 
130  // A random access auto collection gets no benefit from being executed
131  // as a depth first traversal of the whole stage. It will be called for
132  // each primitive as part of the overall pattern matching process. See the
133  // INSTANCE ID CONTRACT above for the ordering requirement on `instance_ids`.
135  bool *prune_branch,
136  UT_Array<int64> *instance_ids = nullptr) const
137  { UT_ASSERT(false); return false; }
138 
139  virtual bool getMayBeTimeVarying() const
140  { return myMayBeTimeVaryingSubPattern; }
141 
143  { return myTokenParsingError; }
144 
145  // Static functions for common argument parsing tasks.
146  static bool parseBool(const UT_StringRef &str);
147  static bool parseInt(const UT_StringRef &str,
148  exint &i);
149  static bool parseFloat(const UT_StringRef &str,
150  fpreal64 &flt);
151  static bool parseVector2(const UT_StringRef &str,
152  UT_Vector2D &vec);
153  static bool parseVector3(const UT_StringRef &str,
154  UT_Vector3D &vec);
155  static bool parseVector4(const UT_StringRef &str,
156  UT_Vector4D &vec);
157  static bool parseTimeRange(const UT_StringRef &str,
158  fpreal64 &tstart,
159  fpreal64 &tend,
160  fpreal64 &tstep);
161 
162  // Parse a source pattern string.
163  // Note that `timevaryingflag` will only ever be *set* (to `true`),
164  // (if the pattern is timevarying of course), but never *unset* (to `false`)
165  static bool parsePattern(const UT_StringRef &str,
166  HUSD_AutoAnyLock &lock,
168  int nodeid,
169  const HUSD_TimeCode &timecode,
170  bool respect_instance_proxy_demands,
171  XUSD_PathSet &paths,
172  bool *timevaryingflag,
174  *instance_ids = nullptr);
175  // Parse a source pattern string, but only return the first result.
176  static bool parsePatternSingleResult(const UT_StringRef &str,
177  HUSD_AutoAnyLock &lock,
179  int nodeid,
180  const HUSD_TimeCode &timecode,
181  bool respect_instance_proxy_demands,
182  SdfPath &path,
183  bool *timevaryingflag);
184 
185  static bool canCreateAutoCollection(const char *token);
186  static XUSD_AutoCollection *create(const char *token,
187  HUSD_AutoAnyLock &lock,
189  int nodeid,
190  const HUSD_TimeCode &timecode);
191  static void registerPlugins();
192  static void registerPlugin(XUSD_AutoCollectionFactory *factory);
193 
194 protected:
200  int myNodeId;
204 
205 private:
206  // Proeprty related plugins were broken out into a separate file just to
207  // keep file size under control, but this meant having a separate plugin
208  // registration function for collections in XUSD_PropertAutoCollection.C.
209  static void registerPropertyPlugins();
210 
211  static UT_Array<XUSD_AutoCollectionFactory *> theFactories;
212 };
213 
215 {
216 public:
218  const UT_StringHolder &collectionname,
219  const UT_StringArray &orderedargs,
220  const UT_StringMap<UT_StringHolder> &namedargs,
221  HUSD_AutoAnyLock &lock,
223  int nodeid,
224  const HUSD_TimeCode &timecode);
225  ~XUSD_SimpleAutoCollection() override;
226 
227  bool randomAccess() const override
228  { return false; }
229  void matchPrimitives(XUSD_PathSet &matches,
230  UT_StringMap<UT_Array<int64>> *instance_ids)
231  const override;
232  // If this fills `instance_ids` for a point instancer, the ids must be
233  // sorted and duplicate-free (see the INSTANCE ID CONTRACT in
234  // XUSD_AutoCollection).
235  virtual bool matchPrimitive(const UsdPrim &prim,
236  bool *prune_branch,
237  UT_Array<int64> *instance_ids) const = 0;
238 };
239 
241 {
242 public:
244  const UT_StringHolder &collectionname,
245  const UT_StringArray &orderedargs,
246  const UT_StringMap<UT_StringHolder> &namedargs,
247  HUSD_AutoAnyLock &lock,
249  int nodeid,
250  const HUSD_TimeCode &timecode);
252 
253  bool randomAccess() const override
254  { return true; }
256  bool *prune_branch,
257  UT_Array<int64> *instance_ids = nullptr)
258  const override;
259  // If this fills `instance_ids` for a point instancer, the ids must be
260  // sorted and duplicate-free (see the INSTANCE ID CONTRACT in
261  // XUSD_AutoCollection).
262  virtual bool matchPrimitive(const UsdPrim &prim,
263  bool *prune_branch,
264  UT_Array<int64> *instance_ids) const = 0;
265 };
266 
268 
269 #endif
270 
virtual bool matchRandomAccessPrimitive(const SdfPath &path, bool *prune_branch, UT_Array< int64 > *instance_ids=nullptr) const
bool randomAccess() const override
#define SYS_VISIBILITY_EXPORT
const UT_StringHolder & getTokenParsingError() const
HUSD_TimeCode myHusdTimeCode
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
int64 exint
Definition: SYS_Types.h:125
#define HUSD_API
Definition: HUSD_API.h:31
virtual XUSD_AutoCollection * create(const UT_StringHolder &collectionname, const UT_StringArray &orderedargs, const UT_StringMap< UT_StringHolder > &namedargs, HUSD_AutoAnyLock &lock, HUSD_PrimTraversalDemands demands, int nodeid, const HUSD_TimeCode &timecode) const =0
virtual bool getMayBeTimeVarying() const
HUSD_PrimTraversalDemands
Definition: HUSD_Utils.h:39
double fpreal64
Definition: SYS_Types.h:201
virtual void matchPrimitives(XUSD_PathSet &matches, UT_StringMap< UT_Array< int64 >> *instance_ids) const
UT_StringArray myOrderedArgs
UT_StringHolder myTokenParsingError
Definition: prim.h:116
XUSD_AutoCollection * create(const UT_StringHolder &collectionname, const UT_StringArray &orderedargs, const UT_StringMap< UT_StringHolder > &namedargs, HUSD_AutoAnyLock &lock, HUSD_PrimTraversalDemands demands, int nodeid, const HUSD_TimeCode &timecode) const override
Definition: path.h:280
UT_StringMap< UT_StringHolder > myNamedArgs
HUSD_AutoAnyLock & myLock
HUSD_PrimTraversalDemands myDemands
bool canCreateAutoCollection(const char *cname) const override
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
SYS_VISIBILITY_EXPORT void newAutoCollection(void *unused)
virtual bool canCreateAutoCollection(const char *token) const =0
XUSD_SimpleAutoCollectionFactory(const char *cname)