HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
refiner.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_REFINER_H__
25 #define __GUSD_REFINER_H__
26 
27 #include "gusd/api.h"
28 
29 #include <GT/GT_Refine.h>
30 #include <GT/GT_RefineParms.h>
31 #include <GU/GU_DetailHandle.h>
32 #include <UT/UT_SharedPtr.h>
33 
34 #include "pxr/pxr.h"
35 #include "pxr/usd/usdGeom/tokens.h"
36 #include "pxr/usd/sdf/path.h"
37 
38 #include "writeCtrlFlags.h"
39 
41 
42 /// \class GusdRefiner
43 /// \brief Class used to refine GT prims so that they can be written to a USD file.
44 ///
45 /// When we write a USD file, we create a GT_GEODetail prim from the current detail,
46 /// then refine it using a GusdRefiner.
47 ///
48 /// The basic idea is that the refiner looks at each prim, if it is a type that can
49 /// be written to USD it adds it to the "gprim array", if not it continues to refine it.
50 ///
51 /// The refiner supports namespace hierarchy. Some prims types are added to the
52 /// the gprim array and then add thier children as well. Packed prims do this. The
53 /// packed prim becomes a group node in USD. A PackedF3DGroup is similar.
54 ///
55 /// The refiner calculates the primPath (location in the USD file). This can come from
56 /// an attribute on the prim being refined or it can be computed. The computed
57 /// path is based on a prefix provided by the client, a prim name and possibly
58 /// a hierarchy pf group names supplied by packed prims.
59 ///
60 /// The gprim array can contain prims from several OBJ nodes. The obj nodes provide
61 /// a coordinate space and a set of options. We stash this stuff with the prims
62 /// in the prim array.
63 
64 
66 
68 {
69 public:
70 
71  // A struct representing GT prims refined to a USD prim.
72  // localXform is the transform from the prim's space to its parent.
73  // parentXform is the transform from the prim's parent's space to World.
74  struct GprimArrayEntry {
80 
83  const SdfPath& path,
84  const GT_PrimitiveHandle& prim,
85  const UT_Matrix4D& xform,
86  const TfToken& purpose,
87  const GusdWriteCtrlFlags& writeCtrlFlags )
88  : path( path )
89  , prim( prim )
90  , xform( xform )
91  , purpose(purpose)
92  , writeCtrlFlags(writeCtrlFlags) {}
93  };
94  using GprimArray = std::vector<GprimArrayEntry>;
95 
96  ////////////////////////////////////////////////////////////////////////////
97 
98  /// Construct a refiner for refining the prims in a detail.
99  /// Typically the ROP constructs a refiner for its cooked detail,
100  /// and then as we process GT prims, if a GEO Packed Prim is encountered,
101  /// We create a new refiner and recurse.
102  /// We need to keep track of the transform as we recurse through packed prims. Note
103  /// that we only write packed prims that have been tagged with a prim path. We
104  /// kee track of the transform of the last group we wrote in parentToWorldXform
105  /// \p localToWorldXform is initialized to the OBJ Node's transform by the ROP.
106  GusdRefiner(
107  GusdRefinerCollector& collector,
108  const SdfPath& pathPrefix,
109  const std::string& pathAttrName,
110  const UT_Matrix4D& localToWorldXform );
111 
112  ~GusdRefiner() override {}
113 
114  bool allowThreading() const override { return false; }
115 
116  void addPrimitive( const GT_PrimitiveHandle& gtPrim ) override;
117 
118  void refineDetail(
119  const GU_ConstDetailHandle& detail,
120  const GT_RefineParms& parms );
121 
122  const GprimArray& finish();
123 
124  //////////////////////////////////////////////////////////////////////////
125 
126  // If true, refine packed prims, otherwise return the prim on the
127  // prim array. This is set to false when we just want to capture
128  // the prims transform.
130 
131  // Use the "usdprimpath" intrinsic for the name of USD packed prims.
132  // Used when writing overlays.
134 
135 
136  // Normally we only write geometry packed prims as groups if they have
137  // been named. Force top level groups to always be written. This is so we
138  // can be assured we have a place to write instance ids.
140 
141  // Set true if we have usdinstancepath or instancepath set. If true and we
142  // have packed usd, packed prims or points we will build a point instancer.
144 
145  // If true, build prototypes which means ignoring the instancepath and not
146  // building a point isntancer, and putting all geometry under the given
147  // prototypes scope.
149 
150  // If we are overlaying a point instancer, this is set to the type of
151  // of point instancer we need to overlay. This can only be "PointInstancer"
152  // now, but used to support an older "PxPointInstancer".
154 
156 
157  /////////////////////////////////////////////////////////////////////////////
158 
159 private:
160 
161  // Convert a prim's name into a prim path taking into account prefix and
162  // modifying to be a valid Usd prim path.
163  std::string createPrimPath( const std::string& primName);
164 
165  // Place to collect refined prims
166  GusdRefinerCollector& m_collector;
167 
168  // Refine parms are passed to refineDetail and then held on to.
169  GT_RefineParms m_refineParms;
170 
171  // Prefix added to all relative prim paths.
172  SdfPath m_pathPrefix;
173 
174  // The name of the attribute that specifies what USD object to write to.
175  const std::string& m_pathAttrName;
176 
177  // The coordinate space accumulated as we recurse into packed geometry prims.
178  UT_Matrix4D m_localToWorldXform;
179 
180  // false if we have recursed into a packed prim.
181  bool m_isTopLevel;
182 };
183 
184 // As we recurse down a packed prim hierarchy, we create a new refiner at each
185 // level so we can carry the appropriate parametera. However, we need a object
186 // shared by all the refiners to collect the refined prims.
188 public:
189 
192 
193  // Struct used to keep names unique
194  struct NameInfo {
195  size_t firstIdx; // index into gprim array of first use of name
196  size_t count; // number of times name has been used.
197 
198  NameInfo() : firstIdx( -1 ), count(0) {}
199  NameInfo( size_t idx ) : firstIdx(idx), count( 0 ) {}
200  };
201 
202  // Struct to store instance prims in.
203  // A GT_PrimInstance may represent several point instancer array entries.
204  // Index identifies which one.
205  struct InstPrimEntry {
207  size_t index;
208 
209  InstPrimEntry() : prim(NULL), index(0) {}
210  InstPrimEntry( GT_PrimitiveHandle p, int i=0 ) : prim( p ), index(i) {}
211  };
212 
213  ////////////////////////////////////////////////////////////////////////////
214 
215  SdfPath add(
216  const SdfPath& path,
217  bool explicitPrimPath,
218  GT_PrimitiveHandle prim,
219  const UT_Matrix4D& xform,
220  const TfToken & purpose,
221  const GusdWriteCtrlFlags& writeCtrlFlags );
222 
223  /// Add a prim to be added to a point instancer during finish
224  void addInstPrim( const SdfPath& path, GT_PrimitiveHandle p, int index=0 );
225 
226  // Complete refining all prims.
227  // When constructing point instancers, the refiner/collector gathers and
228  // holds on to all packed prims that are added. When finish is called,
229  // a GT_PrimPointMesh is created and added for each point instancer.
230  void finish( GusdRefiner& refiner );
231 
232  ////////////////////////////////////////////////////////////////////////////
233 
234  // The results of the refine
236 
237  // Map used to generate unique names for each prim
238  std::map<SdfPath,NameInfo> m_names;
239 
240  // We can refine several point instancers in one session. They are partitioned
241  // by a "srcPrimPath" intrinsic on USD packed prims. This map is used to
242  // sort the prims. If a prim does note have a srcPrimPath, it is added to
243  // a entry with a empty path.
244  std::map<SdfPath,std::vector<InstPrimEntry>> m_instancePrims;
245 };
246 
248 
249 #endif // __GUSD_GT_REFINER_H__
UT_Matrix4D xform
Definition: refiner.h:77
bool m_buildPointInstancer
Definition: refiner.h:143
bool m_buildPrototypes
Definition: refiner.h:148
bool m_useUSDIntrinsicNames
Definition: refiner.h:133
std::map< SdfPath, std::vector< InstPrimEntry > > m_instancePrims
Definition: refiner.h:244
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GusdRefiner::GprimArray m_gprims
Definition: refiner.h:235
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
void addInstPrim(const SdfPath &path, GT_PrimitiveHandle p, int index=0)
Add a prim to be added to a point instancer during finish.
bool m_refinePackedPrims
Definition: refiner.h:129
virtual void addPrimitive(const GT_PrimitiveHandle &prim)=0
TfToken purpose
Definition: refiner.h:78
size_t index
Definition: refiner.h:207
bool allowThreading() const override
Return true if the refinement allows threaded refinement of primitives.
Definition: refiner.h:114
GT_PrimitiveHandle prim
Definition: refiner.h:206
GprimArrayEntry(const SdfPath &path, const GT_PrimitiveHandle &prim, const UT_Matrix4D &xform, const TfToken &purpose, const GusdWriteCtrlFlags &writeCtrlFlags)
Definition: refiner.h:82
GT_PrimitiveHandle prim
Definition: refiner.h:76
SdfPath path
Definition: refiner.h:75
InstPrimEntry(GT_PrimitiveHandle p, int i=0)
Definition: refiner.h:210
GusdWriteCtrlFlags m_writeCtrlFlags
Definition: refiner.h:155
Definition: token.h:87
InstPrimEntry()
Definition: refiner.h:209
Definition: refiner.h:205
~GusdRefiner() override
Definition: refiner.h:112
Definition: path.h:291
std::vector< GprimArrayEntry > GprimArray
Definition: refiner.h:94
GprimArrayEntry()
Definition: refiner.h:81
GusdRefiner::GprimArray GprimArray
Definition: refiner.h:191
TfToken m_pointInstancerType
Definition: refiner.h:153
Processes primitives generated by refinement process.
Definition: GT_Refine.h:20
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
GusdWriteCtrlFlags writeCtrlFlags
Definition: refiner.h:79
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Class used to refine GT prims so that they can be written to a USD file.
Definition: refiner.h:67
bool m_forceGroupTopPackedPrim
Definition: refiner.h:139
#define GUSD_API
Definition: api.h:40
Definition: refiner.h:74
GLint GLsizei count
Definition: glcorearb.h:405
void finish(GusdRefiner &refiner)
SdfPath add(const SdfPath &path, bool explicitPrimPath, GT_PrimitiveHandle prim, const UT_Matrix4D &xform, const TfToken &purpose, const GusdWriteCtrlFlags &writeCtrlFlags)
std::map< SdfPath, NameInfo > m_names
Definition: refiner.h:238