HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_Primitive.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: GT_Primitive.h ( GT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GT_Primitive__
12 #define __GT_Primitive__
13 
14 #include "GT_API.h"
15 #include "GT_AttributeList.h"
16 #include "GT_DataArray.h"
17 #include "GT_Handles.h"
18 #include "GT_PrimitiveTypes.h"
19 #include "GT_Transform.h"
20 #include "GT_Types.h"
22 #include <GA/GA_Names.h>
23 #include <UT/UT_BoundingBox.h>
24 #include <UT/UT_IntrusivePtr.h>
25 #include <UT/UT_VectorTypes.h>
26 
27 class GT_Refine;
28 class GT_RefineParms;
30 class UT_JSONWriter;
31 class UT_StringRef;
32 
33 /// @brief The base class for all GT primitive types
34 ///
35 /// Primitives represent geometric entities like spheres and polygons.
36 /// There is a list of @em well-known primitives defined in
37 /// GT_PrimitiveTypes.h. These are returned by the @c getPrimitiveType() method.
38 ///
39 /// Each primitive must be able to compute a bounding box. Since a primitive
40 /// may be part of a sequence of primitives, the primitive must be able to
41 /// compute the bounding boxes for each segment.
42 ///
44  : public UT_IntrusiveRefCounter<GT_Primitive>
45 {
46 public:
47  GT_Primitive();
49  virtual ~GT_Primitive();
50 
51  virtual const char *className() const = 0;
52 
53  /// The GT library has pre-defined primitive types (see GT_PrimitiveType)
54  /// Custom primitives may ask for an identifier which is guaranteed
55  /// to be unique. These integers are created at run time and may have
56  /// different values from run to run. They will always be greater or equal
57  /// to GT_MAX_PRIM_TYPES.
58  static int createPrimitiveTypeId();
59 
60  /// Return the primitive type.
61  /// By default this returns GT_PRIM_UNDEFINED,
62  virtual int getPrimitiveType() const;
63 
64  /// Return a unique primitive ID and true, if an ID can be generated (false
65  /// otherwise).
66  virtual bool getUniqueID(int64 &id) const;
67 
68  /// An array of bounding boxes is passed in. There is a bounding box for
69  /// each segement. Each bounding box should be enlarged appropriately.
70  virtual void enlargeBounds(UT_BoundingBox boxes[],
71  int nsegments) const = 0;
72 
73  /// This is used when computing bounds for rendering (not just geometric
74  /// bounds). This normally just calls @c enlargeBounds(), but for some
75  /// primitives, their bounds can be influenced by attribute values (i.e.
76  /// the "width" in point mesh primitives). This allows proper bounds to be
77  /// passed to mantra.
78  /// Users may want to call @c enlargeWidth()
79  virtual void enlargeRenderBounds(UT_BoundingBox boxes[],
80  int nsegments) const;
81 
82  /// This method is used to find the minimum/maximum velocity bounds. By
83  /// default, the shared/vertex attributes are scanned for the attribute and
84  /// its range is used. Only the first segment is queried.
85  ///
86  /// If unable to compute the velocity range, the @c min and @c max should
87  /// be set to 0.
88  virtual void getVelocityRange(
90  const UT_StringRef &attribute_name = GA_Names::v) const;
91 
92  /// Refine the primitive.
93  /// - @c refiner @n
94  /// The refiner object is responsible for processing primitives, and
95  /// also giving the primitive hints on how to refine itself.
96  /// The method should returns true if new primitives were added to the
97  /// refiner.
98  virtual bool refine(GT_Refine &refiner,
99  const GT_RefineParms *parms=NULL) const;
100 
101  /// Return the number of motion segments defined on the geometry
102  /// By default, this simply returns the number of segments on P.
103  virtual int getMotionSegments() const = 0;
104 
105  /// Return an approximate memory usage. Since data may be shared, this
106  /// will always be an over-estimation.
107  virtual int64 getMemoryUsage() const = 0;
108 
109  /// Return a transform applied to the entire primitive. By default, this
110  /// returns an identity transform.
112  { return myTransform; }
113  /// Set the transform for a the primitive.
115  { myTransform = x; }
116 
117  /// Create a clone of this primitive, the clone may share references to the
118  /// same data.
120  { return doSoftCopy(); }
121 
122  /// @{
123  /// Virtual methods to access the attribute data for primitives
124  /// Some primitives may not implement these methods, in which case, the
125  /// returned value will be an empty list.
126  virtual const GT_AttributeListHandle &getPointAttributes() const;
127  virtual const GT_AttributeListHandle &getVertexAttributes() const;
128  virtual const GT_AttributeListHandle &getUniformAttributes() const;
129  virtual const GT_AttributeListHandle &getDetailAttributes() const;
130  const GT_AttributeListHandle &getAttributeList(GT_Owner owner) const;
131  /// @}
132 
133  /// Returns true if a data array with 'name' is found in 'owner_scope',
134  /// and optionally its storage and tuple_size.
135  virtual bool hasDataArray(const UT_StringRef &name,
136  GT_Owner owner_scope[],
137  int num_owners,
138  GT_Storage *storage = NULL,
139  GT_Size *tuple_size = NULL) const;
140 
141  /// update any cached data for geometry and its attributes
142  virtual bool updateGeoPrim(const GU_ConstDetailHandle &dtl,
143  const GT_RefineParms &parms);
144 
145  /// Return true if the primitive represents geometry at frame 'fr'.
146  void setStaticGeometry(bool static_geo)
147  { myStaticGeoState=static_geo ? 1 : 0; }
148  bool isStaticGeometry() const { return myStaticGeoState==1; }
149  bool isFrameInfoAvailable() const
150  { return myStaticGeoState>=0; }
151 
152  /// Return the data ID hash derived from attribute lists. If cache_data_id
153  /// is true, cache the result for future calls. This should only be done
154  /// if the primitive and its attributes will not change (such as packed
155  /// primitive contents).
156  bool getDataIdHash(int64 &hash, int segment=0,
157  bool cache_data_id = false) const;
158 
159  /// Returns the topology version for the primitive.
160  virtual bool getTopologyVersion(int64 &version) const;
161 
162  /// Find an attribute of a given name. The search order is:
163  /// - getVertexAttributes()
164  /// - getPointAttributes()
165  /// - getUniformAttributes()
166  /// - getDetailAttributes()
167  GT_DataArrayHandle findAttribute(const UT_StringRef &name,
168  GT_Owner &owner,
169  int segment) const;
170 
171  /// print out all attribute lists
172  void dumpAttributeLists(const char *label, bool data_too) const;
173 
174  /// Dump the primitive. If the primitive's save() method isn't implemented
175  /// (i.e. returns false), the attributes will be dumped.
176  void dumpPrimitive() const;
177 
178  /// Save all attribute lists to a JSON stream. The attribute lists are
179  /// saved as a dictionary/map, keyed on the class name.
180  bool saveAttributeLists(UT_JSONWriter &w) const;
181 
182  /// Save the primitive to a JSON stream. The default method does nothing
183  /// and returns false.
184  virtual bool save(UT_JSONWriter &w) const;
185 
186  /// This method is used to pass information about refinement to the Houdini
187  /// viewport. This returns a default GT_ViewportRefineOptions.
188  virtual const GT_ViewportRefineOptions &viewportRefineOptions() const;
189 
190  /// The jsonWriter class can be used by sub-classes to simplify saving
191  /// primitives to a JSON object. For example: @code
192  /// jsonWriter w("PolygonMesh");
193  /// myVertexList.save(*w);
194  /// myFaceOffsets.save(*w);
195  /// saveAttributeLists(*w);
196  /// @endcode
197  /// The constructor automatically starts an array. The destructor closes
198  /// the array.
200  {
201  public:
202  jsonWriter(UT_JSONWriter &w, const char *primitive_name);
203  ~jsonWriter();
204  UT_JSONWriter &operator*() { return myWriter; }
205  private:
206  UT_JSONWriter &myWriter;
207  };
208 
209  /// @{
210  /// Methods defined on GT_Primitive which are a short-cut to avoid a
211  /// static/dynamic cast of the primitive when we know that the primitive is
212  /// a polygon mesh. These are not part of the interface of GT_Primitive
213  /// per se.
214  virtual const GT_DataArrayHandle &getVertexList() const;
215  virtual GT_DataArrayHandle createPointNormals(int segment=0,
216  const UT_StringRef &P = GA_Names::P,
217  bool normalize=true,
218  const fpreal32 *pntdata = NULL,
219  GT_Storage store =GT_STORE_REAL32) const;
220  /// @}
221 
222  /// Refine a detail or primitive. The refine parms may be NULL. If the
223  /// refinement fails, the returned handle will be NULL.
224  /// @{
225  static GT_PrimitiveHandle refineDetail(const GU_ConstDetailHandle &detail,
226  const GT_RefineParms *parms);
227  static GT_PrimitiveHandle refinePrimitive(const GT_PrimitiveHandle &primh,
228  const GT_RefineParms *parms);
229  /// @}
230 
231  /// The harden method will return a NULL pointer if hardening isn't
232  /// implemented for the primitive in question. Otherwise, any references
233  /// will be hardened into local data for the primitive.
235  { return doHarden(); }
236 
237  /// Create a transformed copy. This will transform a @c clone() of the
238  /// primitive by the given transform. It's possible @c this primitive
239  /// might be returned if the clone doesn't need to be transformed. The @c
240  /// force parameter will force a clone to be created.
241  GT_PrimitiveHandle copyTransformed(const GT_TransformHandle &x,
242  bool force = false) const;
243 
244  /// Copy the transform from the source primitive for transforming. This is
245  /// only required when you're not using a copy c-tor on the primitive
246  /// (which automatically copies the transform).
247  void refineCopyTransformFrom(const GT_Primitive &src);
248 
249  /// The attributeMerge method will return a new primitive, but with the
250  /// given attributes merged in from the source primitive. If the method
251  /// isn't implementd on the sub-class, or if the primitive types don't
252  /// match, or if there are other errors, the method will return the
253  /// original primitive (i.e. @c this).
255  const UT_StringMMPattern *vertex_pattern,
256  const UT_StringMMPattern *point_pattern,
257  const UT_StringMMPattern *uniform_pattern,
258  const UT_StringMMPattern *detail_pattern) const
259  {
260  return doAttributeMerge(src, vertex_pattern, point_pattern,
261  uniform_pattern, detail_pattern);
262  }
263 
264 public:
265  /// @{
266  /// For memory tracking, we override the new/delete operators
267  static void *operator new(size_t size);
268  static void *operator new(size_t size, void *p);
269  static void operator delete(void *p, size_t size);
270  /// @}
271 
272  /// Methods for GEO/GU support.
273  /// @{
274  /// The default methods for these simply refine until a primitive can
275  /// handle the method.
276  virtual fpreal computePerimeter(int seg=0) const;
277  virtual fpreal computeSurfaceArea(int seg=0) const;
278  virtual fpreal computeVolume(const UT_Vector3 &ref_P, int seg=0) const;
279  /// @}
280 
281  /// Hardening the primitive will return a copy of the primitive, but
282  /// without any dangling references. The default implementation returns a
283  /// NULL ptr.
284  virtual GT_PrimitiveHandle doHarden() const;
285 
286  /// Create a copy of the primitive, referencing all the source data
287  /// This can return a NULL pointer, but it would be better to implement it
288  /// properly.
289  virtual GT_PrimitiveHandle doSoftCopy() const = 0;
290 
291  /// The virtual implementation of attribute merging
292  virtual GT_PrimitiveHandle doAttributeMerge(const GT_Primitive &src,
293  const UT_StringMMPattern *vertex,
294  const UT_StringMMPattern *point,
295  const UT_StringMMPattern *uniform,
296  const UT_StringMMPattern *detail) const;
297 
298  /// This method will "merge" two attribute lists given
299  /// The @c changed variable will be set to true if there are modifications,
300  /// otherwise, it will remain unchanged.
302  mergeAttributeLists(bool &changed,
303  const GT_AttributeListHandle &src,
304  const GT_AttributeListHandle &merge,
306  const UT_StringMMPattern *alternate = NULL);
307 
308 
309  /// Convenience method to compute the velocity bounds given a data array
310  /// Returns @c false if the data array handle wasn't valid. If invalid, @c
311  /// vmin and @c vmax will be set to 0.
312  static bool computeVelocityRange(UT_Vector3 &vmin, UT_Vector3 &vmax,
313  const GT_DataArrayHandle &v);
314 
315  /// Search attributes for a "width" or "pscale" attribute. If one is
316  /// found, enlarge the bounding boxes by the maximum value of the
317  /// attribute. If no attributes are found, use the @c defwidth.
318  /// Returns @c true if an attribute was found
319  /// @note If the @c defwidth is less than 0, the "mantra" default width
320  /// will be used. Pass in 0 to leave the boxes unchanged.
321  bool enlargeWidth(UT_BoundingBox boxes[],
322  int nsegments,
323  fpreal defwidth=-1) const;
324 
325 protected:
326  /// Convenience function to compute the point bounding box given an
327  /// attribute list. This tests whether "P" is in homogeneous coordinates
328  /// or not.
329  void enlargeP(UT_BoundingBox &box,
330  const GT_AttributeListHandle &list,
331  int segment) const;
332 
333  /// Convenience method to enlarge a bounding box given a position attribute
334  void enlargeP(UT_BoundingBox &B, const GT_DataArrayHandle &P) const;
335 
336  /// Convenience method to enlarge a bounding box given P in homogeneous
337  /// coordinates.
338  void enlargePw(UT_BoundingBox &B, const GT_DataArrayHandle &P) const;
339 
340 private:
341  // Keep the 32 bit int first in the structure. Since we inherit from
342  // GA_IntrusiveRefCounter, this alignment makes the object smaller.
343  mutable int myCachedDataIdHashSegment : 30,
344  myStaticGeoState : 2;
345  GT_TransformHandle myTransform;
346  mutable int64 myCachedDataIdHash;
347 };
348 
349 #endif
SIM_API const UT_StringHolder vertex
GT_PrimitiveHandle harden() const
Definition: GT_Primitive.h:234
GT_Storage
Definition: GT_Types.h:19
UT_JSONWriter & operator*()
Definition: GT_Primitive.h:204
GT_PrimitiveHandle clone() const
Definition: GT_Primitive.h:119
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
GR_API RE_CacheVersion getTopologyVersion(const GT_Primitive *prim, RE_CacheVersion geo_version)
Return the cache version for the topology of primitive 'prim'.
const GLdouble * v
Definition: glcorearb.h:837
#define GT_API
Definition: GT_API.h:13
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
GA_API const UT_StringHolder P
A reference counter base class for use with UT_IntrusivePtr.
float fpreal32
Definition: SYS_Types.h:200
GA_API const UT_StringHolder v
void setStaticGeometry(bool static_geo)
Return true if the primitive represents geometry at frame 'fr'.
Definition: GT_Primitive.h:146
GT_PrimitiveHandle attributeMerge(const GT_Primitive &src, const UT_StringMMPattern *vertex_pattern, const UT_StringMMPattern *point_pattern, const UT_StringMMPattern *uniform_pattern, const UT_StringMMPattern *detail_pattern) const
Definition: GT_Primitive.h:254
bool isFrameInfoAvailable() const
Definition: GT_Primitive.h:149
long long int64
Definition: SYS_Types.h:116
const GT_TransformHandle & getPrimitiveTransform() const
Definition: GT_Primitive.h:111
GLuint const GLchar * name
Definition: glcorearb.h:786
GLushort pattern
Definition: glad.h:2583
The base class for all GT primitive types.
Definition: GT_Primitive.h:43
GLint GLenum GLint x
Definition: glcorearb.h:409
GT_API const UT_StringHolder version
Processes primitives generated by refinement process.
Definition: GT_Refine.h:20
GT_Owner
Definition: GT_Types.h:90
void setPrimitiveTransform(const GT_TransformHandle &x)
Set the transform for a the primitive.
Definition: GT_Primitive.h:114
int64 GT_Size
Definition: GT_Types.h:128
GLsizeiptr size
Definition: glcorearb.h:664
SIM_API const UT_StringHolder force
bool isStaticGeometry() const
Definition: GT_Primitive.h:148
fpreal64 fpreal
Definition: SYS_Types.h:277
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
constexpr T normalize(UT_FixedVector< T, D > &a) noexcept
GLenum src
Definition: glcorearb.h:1793