HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GEO_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: Geometry Library (C++)
7  *
8  * COMMENTS:
9  * The className method is used for saving/loading so there should
10  * be no spaces in the name.
11  *
12  */
13 
14 #pragma once
15 
16 #ifndef __GEO_Primitive_H__
17 #define __GEO_Primitive_H__
18 
19 #include "GEO_API.h"
20 #include <iosfwd>
21 #include <UT/UT_Matrix4.h>
22 #include <UT/UT_BoundingBox.h>
23 #include <UT/UT_BoundingSphere.h>
24 #include <GA/GA_Primitive.h>
25 #include "GEO_PrimType.h"
26 
27 class GA_AttributeRefMap;
28 class GA_Detail;
31 class GEO_ConvertParms;
32 class GEO_Detail;
33 class GEO_MetaPrim;
34 class GEO_Vertex;
35 
36 template<typename T, bool B> class GA_EdgeT;
38 
40 
42 {
43 protected:
44  /// NOTE: The constructor should only be called from subclass
45  /// constructors.
48  : GA_Primitive(*d, offset)
49  {}
50 
51  /// NOTE: The destructor should only be called from subclass
52  /// destructors; only GEO_Detail::destroyStashed() should be
53  /// calling delete on GEO_Primitive pointers, and GEO_Detail
54  /// is a friend.
55  virtual ~GEO_Primitive() {}
56 
57 public:
58  virtual GA_PrimCompat::TypeMask getPrimitiveId() const;
59 
60  /// Copy attribute data from the source primitive. Also, copy over all the
61  /// group membership information from the source primitive.
63  bool copy_groups=true)
64  {
65  copyAttributeData(src);
66  if (copy_groups)
67  copyGroupMembership(src);
68  }
70  GA_AttributeRefMap &gah,
71  bool copy_groups=true)
72  {
73  copyAttributeData(src, gah);
74  if (copy_groups)
75  copyGroupMembership(src);
76  }
77  /// NOTE: The copying of groups only works if src is in the same detail
79  GA_PrimitiveWrangler &wrangler,
80  bool copy_groups=true)
81  {
82  copyAttributeData(src, wrangler);
83  if (copy_groups)
84  copyGroupMembership(src);
85  }
86 
87  /// Copy the attribute data over from the source primitive. Often you will
88  /// want to call copyAttributesAndGroups() instead.
89  void copyAttributeData(const GEO_Primitive &src);
90  void copyAttributeData(const GEO_Primitive &src,
91  GA_AttributeRefMap &gah);
92  void copyAttributeData(const GEO_Primitive &src,
93  GA_PrimitiveWrangler &wrangler);
94 
95  void copyAttributeValues(const GEO_Primitive &src,
96  GA_PrimitiveWrangler &wrangler);
97 
98  /// Copy the group membership from the source primitive to the current
99  /// primitive. Both primitives must be in the same detail.
100  void copyGroupMembership(const GEO_Primitive &src);
101 
102  // NOTE: The point *indices* will be the same as in src.
103  // If this and src are in the same detail, this means that
104  // the point offsets will also be the same. There must be
105  // sufficient points in this' detail.
106  // NOTE: To remap the points, you can iterate through the vertices
107  // of the primitive after copying, and call getPointOffset
108  // and setPointOffset.
109  //
110  // copyPrimitive(), unlike copy(), will not copy the primitive attributes.
111  // (see copyAttributeData() or copyAttributesAndGroups()).
112 #if GA_PRIMITIVE_VERTEXLIST
113  virtual void copyPrimitive(const GEO_Primitive *src);
114 #else
115  virtual void copyPrimitive(const GEO_Primitive *src)=0;
116 #endif
117  virtual GEO_Primitive *copy(int preserve_shared_pts = 0) const;
118 
119  // Transforms the matrix associated with this primitive. The
120  // translate component is ignored: Translate the vertices of
121  // the primitive to translate the primitive.
122  // This only works with quadrics (sphere, tube, metaballs) and volumes.
123  virtual void transform(const UT_Matrix4 &);
124 
125  // Convert the real domain values of the primitive to unit values to be
126  // used in the evaluation methods below:
127  virtual void realToUnitPair(float ureal, float vreal,
128  float &uunit, float &vunit) const;
129 
130  // Convert the unit values of the primitive to the real domain values
131  virtual void unitToRealPair(float uunit, float vunit,
132  float &ureal, float &vreal) const;
133 
134  // Map the normalized length (distance value [0,1]) parameter to the unit
135  // parameterization of the primitve
136  virtual void unitLengthToUnitPair(float ulength, float vlength,
137  float &uparm, float &vparm)const;
138 
139  virtual void unitToUnitLengthPair(float uparm, float vparm,
140  float &ulength, float &vlength)
141  const;
142 
143  // Evaluate a set of attributes at the bary center of the primitive,
144  // returning true on success, false on failure.
145  bool evaluateBaryCenter(GA_Offset result_vtx,
146  GA_AttributeRefMap &map) const
147  { return evaluateBaryCenterRefMap(result_vtx, map); }
148 
149  // Evaluate a set of attributes at a u,v position. Optionally, evaluate
150  // the derivative in the u or v direction.
151  // The method returns true if the point could be evaluated
152  bool evaluatePoint(GA_Offset result_vtx,
153  GA_AttributeRefMap &map,
154  fpreal u, fpreal v=0,
155  uint du=0, uint dv=0) const
156  { return evaluatePointRefMap(result_vtx, map, u, v, du, dv); }
157 
158  // Evaluate the position, the derivative or the normal at domain point
159  // (u,v), where u and v MUST be in [0,1]. "v" and "dv" will be ignored
160  // when dealing with one-dimensional types such as circles and polygons.
161  // Return 0 if OK and -1 otherwise. The normal is not normalized.
162  int evaluatePoint( UT_Vector4 &pos, float u, float v = 0,
163  unsigned du=0, unsigned dv=0) const
164  { return evaluatePointV4(pos, u, v, du, dv); }
165 
166  virtual int evaluateNormalVector(UT_Vector3 &nml, float u,
167  float v = 0, float w = 0) const;
168 
169  // Evaluate the position at domain point (u,v) in the interior of the
170  // geometry. This calls evaluatePoint by default, but has a different
171  // implementation for triangles and quadrilaterals, where barycentric
172  // coords or bilinear interpolants are used to get an interior point,
173  // rather than a point on the perimeter of the poly. This was added
174  // so particles can stick to triangles and quads.
176  GA_AttributeRefMap &map,
177  fpreal u, fpreal v, fpreal w = 0) const
178  { return evaluateInteriorPointRefMap(
179  result_vtx, map, u, v, w); }
181  fpreal u, fpreal v, fpreal w = 0) const
182  { return evaluateInteriorPointV4(pos, u, v, w); }
183 
184 
185  /// Finds the weightings of the vertices that will compute an interior
186  /// point given the u,v,w coordinates. The offsets are into the gdp's
187  /// vertex list, not indices into this primitive's vertex list.
188  /// Weights are normalized.
189  /// Note this cannot be used to recover the Position as it often has
190  /// special case logic (as in spheres, or rational splines), in those
191  /// cases use evaluateInteriorPoint(UT_Vector4 &pos, ...)
192  /// It has the advantage over the other evaluateInteriorPoints in that
193  /// it allows you to avoid creating any temporary vertices.
194  /// Behaviour for non-sublcassed types is to return the 0th vertex.
195  virtual void computeInteriorPointWeights(
196  UT_Array<GA_Offset> &vtxlist,
197  UT_FloatArray &weightlist,
198  fpreal u, fpreal v, fpreal w) const;
199 
201  {
202  // We cannot do a static_cast because we have only forward
203  // declared GEO_Detail and GA_Detail
204  return reinterpret_cast<GEO_Detail *>(&getDetail());
205  }
206  static GA_PrimCompat::TypeMask getPrimitiveMaskH9(const char *maskstr);
207 
208  /// Compute the bounding box of the primitive. Return 0 if unable to
209  /// compute bounds (1 on successful computation)
210  virtual int getBBox(UT_BoundingBox *bbox) const = 0;
211 
212  /// @{
213  /// If the attribute is "P" the base-class will call getBBox()
214  virtual bool enlargeBoundingBox(UT_BoundingRect &b,
215  const GA_Attribute *P) const;
216  virtual bool enlargeBoundingBox(UT_BoundingBox &b,
217  const GA_Attribute *P) const;
218  /// @}
219 
220  /// Return a normal vector for the primitive
221  virtual UT_Vector3 computeNormal() const = 0;
222 
223  /// Reverse the order of vertices
224  virtual void reverse() = 0;
225 
226  /// Return the center of the primitive. This defaults to the center of the
227  /// bounding box. A better approximation might be the average value of the
228  /// vertex positions (which could be different).
229  virtual UT_Vector3 baryCenter() const;
230 
231  /// Enlarge the bounding sphere with the primitive's bounds.
232  /// By default, this will enlarge the bounding sphere by the primitive's
233  /// bounding box (i.e. getBBox()). It may be possible to have a tighter
234  /// bound (i.e. the vertex hull for many primitives is a much better
235  /// metric).
236  virtual void addToBSphere(UT_BoundingSphere *bsphere) const;
237 
238  /// Method to isolate a pasted surface
239  virtual void isolate() { /* No implementation here. */ }
240 
241  // Take the whole set of points into consideration when applying the
242  // point removal operation to this primitive. The method returns 0 if
243  // successful, -1 if it failed because it would have become degenerate,
244  // and -2 if it failed because it would have had to remove the primitive
245  // altogether.
246  virtual int detachPoints (GA_PointGroup &grp) = 0;
247 
248  virtual bool hasEdge(const GA_Edge &edge) const;
249 
250  /// These functions are only implemented to get guide points/edges
251  /// for spheres, tubes, and circles. The ID meaning is type-specific.
252  /// @{
253  virtual bool hasGuideEdge(int edgeid, UT_Vector3 &a,
254  UT_Vector3 &b) const;
255  virtual bool hasXsectPoint(int pointid, UT_Vector3 &p) const;
256  /// @}
257 
258  /// Apply the function to each vertex of the primitive. Break
259  /// when the function returns true. Return the value
260  /// of the last function applied (false if never called).
261 #if !GA_PRIMITIVE_VERTEXLIST
262  virtual bool vertexApply(bool (*apply)(GA_Offset vtx, void *),
263  void *data = 0) const;
264 #endif
265 
266  SYS_DEPRECATED_HDK(13.0)
267  GEO_Vertex getVertexElement(GA_Size i) const;
268 
269 //
270 // Method to get the tessera data for meta-primitives
271  virtual GEO_MetaPrim *castToMetaPrim(void);
272  virtual const GEO_MetaPrim *castToMetaPrim(void) const;
273 
274  // Return the surrounding values of the real-space u,v parameters.
275  // Returns 1 if succesful, 0 if out-of-range.
276 
277  virtual int parametricBBox(float u, float v,
278  float *u0, float *u1,
279  float *v0, float *v1);
280 
281  // Returns distance between two points in parameter space, aware
282  // of any possible wrapping.
283  virtual float uvDist(float u1, float v1, float u2, float v2) const;
284 
285  // Intersects a ray with the bounding box, iteratively growing it until
286  // an intersection is found or maxtries is reached.
287  int bboxIntersectRay(const UT_Vector3 &rayorig,
288  const UT_Vector3 &raydir,
289  int maxtries = 10,
290  float tmax = 1E17F,
291  float *distance = 0,
292  UT_Vector3 *nml=0) const;
293 
294  /// @{
295  /// If the method isn't known, or doesn't make sense, we return 0.
296  virtual fpreal calcVolume(const UT_Vector3 &) const { return 0; }
297  virtual fpreal calcArea() const { return 0; }
298  virtual fpreal calcPerimeter() const { return 0; }
299  /// @}
300 
301  // Is this primitive a GEO_Quadric?
302  virtual bool isQuadric() const { return false; }
303 
304  // Compute normals on points using a subclass of NormalComp. This
305  // class receives the point offset and vertex normal at that point, and
306  // will either add or subtract the normal from the existing data.
308  public:
309  virtual ~NormalComp() {}
310  virtual void add(GA_Offset offset, const UT_Vector3 &nml) {}
311  virtual void sub(GA_Offset offset, const UT_Vector3 &nml) {}
312  };
313 
314  // Compute point normals into an array, indexed by the point order
315  // number
317  public:
319  : myGdp(gdp)
320  , myOutput(output) {}
321  virtual void add(GA_Offset offset, const UT_Vector3 &nml)
322  { myOutput(myGdp.pointIndex(offset)) += nml; }
323  virtual void sub(GA_Offset offset, const UT_Vector3 &nml)
324  { myOutput(myGdp.pointIndex(offset)) -= nml; }
325 
326  private:
327  const GA_Detail &myGdp;
328  UT_Vector3Array &myOutput;
329  };
330 
331  // Compute normals into a normal attribute
333  public:
335  : myHandle(attr.getAttribute()) {}
337  : myHandle(attr) {}
338  virtual void add(GA_Offset offset, const UT_Vector3 &nml)
339  { myHandle.add(offset, nml); }
340  virtual void sub(GA_Offset offset, const UT_Vector3 &nml)
341  { myHandle.add(offset, -nml); }
342 
343  GA_RWHandleV3 &getHandle() { return myHandle; }
344 
345  protected:
347  };
348 
349  // Compute normals into a normal attribute
351  {
352  public:
354  : myParent(parent)
355  , myLock(lock)
356  , myBufferEntries(0)
357  {}
359  {
360  flush();
361  }
362  virtual void add(GA_Offset offset, const UT_Vector3 &nml)
363  {
364  myOffsetBuffer[myBufferEntries] = offset;
365  myDataBuffer[myBufferEntries] = nml;
366  ++myBufferEntries;
367  if (myBufferEntries == theBufferSize)
368  flush();
369  }
370 
371  virtual void sub(GA_Offset offset, const UT_Vector3 &nml)
372  {
373  myOffsetBuffer[myBufferEntries] = offset;
374  myDataBuffer[myBufferEntries] = -nml;
375  ++myBufferEntries;
376  if (myBufferEntries == theBufferSize)
377  flush();
378  }
379 
380  void flush()
381  {
382  UT_AutoLock lock(myLock);
383  for (GA_Size i = 0; i < myBufferEntries; ++i)
384  {
385  myParent.add(myOffsetBuffer[i], myDataBuffer[i]);
386  }
387  myBufferEntries = 0;
388  }
389 
390  private:
391  static const GA_Size theBufferSize = 1024;
392 
393  NormalComp &myParent;
394  UT_Lock &myLock;
395  GA_Size myBufferEntries;
396  GA_Offset myOffsetBuffer[theBufferSize];
397  UT_Vector3 myDataBuffer[theBufferSize];
398  };
399 
400  virtual void normal(NormalComp &output) const = 0;
401 
402  // Conversion Methods
403 
404  // This method converts the primitive to the specified toType and deletes
405  // the old primitive.
406 
407  virtual GEO_Primitive *convert(GEO_ConvertParms &parms,
408  GA_PointGroup *usedpts = 0) = 0;
409 
410  // This method creates a new primitive by converting the old primitive to
411  // the specified toType.
412 
413  virtual GEO_Primitive *convertNew(GEO_ConvertParms &parms) = 0;
414 
415  // The default implementation just checks the bounding box and
416  // deletes the primitive if the bounding box is beyond the plane.
417  // NOTE: normal should be normalized
418  virtual void clip(UT_Vector3 normal, float distance = 0,
419  GA_PrimitiveGroup *clipgrp = NULL);
420 
421  // The default implementation just intersects against the bounding box
422  virtual int intersectRay(const UT_Vector3 &o, const UT_Vector3 &d,
423  float tmax = 1E17F, float tol = 1E-12F,
424  float *distance = 0, UT_Vector3 *pos = 0,
425  UT_Vector3 *nml = 0, int accurate = 0,
426  float *u = 0, float *v = 0,
427  int ignoretrim = 1) const;
428 
429 
430  /// Convience objects to pass as arguments to saveH9()/loadH9().
432 
433 protected:
434  /// All subclasses should call this method to register the primitive
435  /// intrinsics.
436  /// @see GA_IntrinsicManager
439  { return GA_Primitive::registerIntrinsics(defn); }
440 
441  virtual bool evaluatePointRefMap(GA_Offset result_vtx,
442  GA_AttributeRefMap &map,
443  fpreal u, fpreal v=0,
444  uint du=0, uint dv=0) const = 0;
445 
446  /// Evaluate the position for the given parametric coordinates (with the
447  /// given derivatives). Return 0 if successful, or -1 if failure.
448  /// The default implementation returns {0,0,0,0};
449  virtual int evaluatePointV4( UT_Vector4 &pos, float u, float v = 0,
450  unsigned du=0, unsigned dv=0) const;
451 
452  /// By default, this will call evaluateInteriorPointRefMap with u=.5 and
453  /// v=.5. This is likely not the perfect solution. At the current time,
454  /// this is only used in POPs to birth points at primitive centers.
455  virtual bool evaluateBaryCenterRefMap(GA_Offset result_vtx,
456  GA_AttributeRefMap &map) const;
457 
458  virtual bool evaluateInteriorPointRefMap(GA_Offset result_vtx,
459  GA_AttributeRefMap &map,
460  fpreal u, fpreal v, fpreal w = 0) const;
461  virtual int evaluateInteriorPointV4(UT_Vector4 &pos,
462  fpreal u, fpreal v, fpreal w = 0) const;
463 
464 private:
465  friend class GEO_Detail; // Allow detail to get at private stuff
466 
467  // TODO: Eliminate, deprecate, or switch to saving json format.
468  friend std::ostream &operator<<(std::ostream &os, const GEO_Primitive &d)
469  {
470  d.saveH9(os, 0,
473  return os;
474  }
475 };
476 
478 
479 #endif
Definition of a geometry attribute.
Definition: GA_Attribute.h:189
NormalCompArray(const GA_Detail &gdp, UT_Vector3Array &output)
#define SYS_DEPRECATED_PUSH_DISABLE()
const GLdouble * v
Definition: glcorearb.h:836
void copyAttributesAndGroups(const GEO_Primitive &src, GA_AttributeRefMap &gah, bool copy_groups=true)
Definition: GEO_Primitive.h:69
#define SYS_DEPRECATED_POP_DISABLE()
bool evaluateInteriorPoint(GA_Offset result_vtx, GA_AttributeRefMap &map, fpreal u, fpreal v, fpreal w=0) const
void copyAttributesAndGroups(const GEO_Primitive &src, bool copy_groups=true)
Definition: GEO_Primitive.h:62
NormalCompBuffered(NormalComp &parent, UT_Lock &lock)
virtual void add(GA_Offset offset, const UT_Vector3 &nml)
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
SYS_FORCE_INLINE GEO_Detail * getParent() const
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:817
3D Vector class.
png_uint_32 i
Definition: png.h:2877
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:211
#define GA_INVALID_OFFSET
Definition: GA_Types.h:654
virtual void sub(GA_Offset offset, const UT_Vector3 &nml)
virtual void add(GA_Offset offset, const UT_Vector3 &nml)
GA_Size GA_Offset
Definition: GA_Types.h:617
NormalCompAttr(const GA_RWHandleV3 &attr)
virtual void add(GA_Offset offset, const UT_Vector3 &nml)
This class provides a way to manage a reference to an attribute permitting Read-Write access...
virtual void sub(GA_Offset offset, const UT_Vector3 &nml)
T distance(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2)
Definition: UT_Vector4.h:634
virtual fpreal calcArea() const
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLintptr offset
Definition: glcorearb.h:664
int evaluateInteriorPoint(UT_Vector4 &pos, fpreal u, fpreal v, fpreal w=0) const
#define GEO_API
Definition: GEO_API.h:10
bool evaluatePoint(GA_Offset result_vtx, GA_AttributeRefMap &map, fpreal u, fpreal v=0, uint du=0, uint dv=0) const
A handle to simplify manipulation of multiple attributes.
NormalCompAttr(const GA_RWAttributeRef &attr)
GLboolean * data
Definition: glcorearb.h:130
#define SYS_DEPRECATED_HDK(__V__)
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
GA_API const UT_StringHolder transform
virtual fpreal calcPerimeter() const
unsigned int uint
Definition: SYS_Types.h:39
virtual void sub(GA_Offset offset, const UT_Vector3 &nml)
virtual bool saveH9(std::ostream &os, bool binary, const UT_Array< GA_AttribSaveDataH9 > &prim_attribs, const UT_Array< GA_AttribSaveDataH9 > &vtx_attribs) const
GLfloat v0
Definition: glcorearb.h:815
double fpreal
Definition: SYS_Types.h:269
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER T clip(const T &p, const Box< T > &box)
Definition: ImathBoxAlgo.h:89
SYS_FORCE_INLINE GEO_Primitive(GA_Detail *d, GA_Offset offset=GA_INVALID_OFFSET)
Definition: GEO_Primitive.h:47
int evaluatePoint(UT_Vector4 &pos, float u, float v=0, unsigned du=0, unsigned dv=0) const
virtual ~GEO_Primitive()
Definition: GEO_Primitive.h:55
static const UT_Array< GA_AttribSaveDataH9 > & theEmptySaveAttribs
Convience objects to pass as arguments to saveH9()/loadH9().
virtual void add(GA_Offset offset, const UT_Vector3 &nml)
bool evaluateBaryCenter(GA_Offset result_vtx, GA_AttributeRefMap &map) const
GLfloat GLfloat v1
Definition: glcorearb.h:816
Container class for all geometry.
Definition: GA_Detail.h:96
friend std::ostream & operator<<(std::ostream &os, const GEO_Primitive &d)
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
Definition of a geometric primitive.
#define const
Definition: zconf.h:214
virtual void sub(GA_Offset offset, const UT_Vector3 &nml)
virtual bool isQuadric() const
void copyAttributesAndGroups(const GEO_Primitive &src, GA_PrimitiveWrangler &wrangler, bool copy_groups=true)
NOTE: The copying of groups only works if src is in the same detail.
Definition: GEO_Primitive.h:78
static GA_IntrinsicManager::Registrar registerIntrinsics(GA_PrimitiveDefinition &defn)
virtual void isolate()
Method to isolate a pasted surface.
GLenum src
Definition: glcorearb.h:1792