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