All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
refiner.h
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 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
63 
64 
65 class GusdRefinerCollector;
66 
67 class GUSD_API GusdRefiner : public GT_Refine
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 {
75  SdfPath path;
76  GT_PrimitiveHandle prim;
77  UT_Matrix4D xform;
78  TfToken purpose;
79  GusdWriteCtrlFlags writeCtrlFlags;
80 
81  GprimArrayEntry() {}
82  GprimArrayEntry(
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 
97 
106  GusdRefiner(
107  GusdRefinerCollector& collector,
108  const SdfPath& pathPrefix,
109  const std::string& pathAttrName,
110  const UT_Matrix4D& localToWorldXform );
111 
112  virtual ~GusdRefiner() {}
113 
114  virtual bool allowThreading() const override { return false; }
115 
116  virtual 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 
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.
129  bool m_refinePackedPrims;
130 
131  // Use the "usdprimpath" intrinsic for the name of USD packed prims.
132  // Used when writing overlays.
133  bool m_useUSDIntrinsicNames;
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.
139  bool m_forceGroupTopPackedPrim;
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.
143  bool m_buildPointInstancer;
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.
148  bool m_buildPrototypes;
149 
150  // If we are overlaying a point instancer, this is set to the type of
151  // of point instancer we need to overlay (old - "PxPointInstancer" or new
152  // "PointInstancer").
153  TfToken m_pointInstancerType;
154 
155  GusdWriteCtrlFlags m_writeCtrlFlags;
156 
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.
187 class GusdRefinerCollector {
188 public:
189 
190  using GprimArrayEntry = GusdRefiner::GprimArrayEntry;
191  using GprimArray = GusdRefiner::GprimArray;
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 {
206  GT_PrimitiveHandle prim;
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 
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 
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 
233 
234  // The results of the refine
235  GusdRefiner::GprimArray m_gprims;
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 
247 PXR_NAMESPACE_CLOSE_SCOPE
248 
249 #endif // __GUSD_GT_REFINER_H__
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
Class used to refine GT prims so that they can be written to a USD file.
Definition: refiner.h:67