HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GEO_TriMesh.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: GEO_PrimTriMesh.h (GEO Library, C++)
7  *
8  * COMMENTS: This is the base class for all triangle mesh types.
9  */
10 
11 #pragma once
12 
13 #ifndef __GEO_TriMesh__
14 #define __GEO_TriMesh__
15 
16 #include "GEO_API.h"
17 #include "GEO_Primitive.h"
18 #include "GEO_Vertex.h"
19 #include <UT/UT_Array.h>
20 #include <SYS/SYS_Inline.h>
21 #include <iosfwd>
22 
23 class GA_Detail;
24 class UT_JSONWriter;
25 class UT_JSONParser;
26 class UT_JSONValue;
27 class GA_SaveMap;
28 class GA_LoadMap;
29 
31 
33 {
34 protected:
35  /// NOTE: The constructor should only be called from subclass
36  /// constructors.
39  : GEO_Primitive(d, offset)
40  {}
41 
42 #if !GA_PRIMITIVE_VERTEXLIST
43  /// NOTE: The destructor should only be called from subclass
44  /// destructors.
45  virtual ~GEO_TriMesh();
46 #endif
47 public:
48  virtual int getBBox(UT_BoundingBox *bbox) const;
49  virtual void addToBSphere(UT_BoundingSphere *bsphere) const;
50  virtual UT_Vector3 baryCenter() const;
51  virtual bool saveH9(std::ostream &os, bool binary,
52  const UT_Array<GA_AttribSaveDataH9> &prim_attribs,
53  const UT_Array<GA_AttribSaveDataH9> &vtx_attribs)
54  const;
55  virtual bool loadH9(UT_IStream &is,
56  const UT_Array<GA_AttribLoadDataH9> &prim_attribs,
57  const UT_Array<GA_AttribLoadDataH9> &vtx_attribs);
58 
59  /// @{
60  /// Method to load/save basis values. Default methods return false (fail)
61  virtual bool jsonSaveBasis(UT_JSONWriter &w) const;
62  virtual bool jsonSaveBasis(UT_JSONValue &v) const;
63  virtual bool jsonLoadBasis(UT_JSONParser &p);
64  virtual bool jsonLoadBasis(UT_JSONParser &p, const UT_JSONValue &v);
65  /// @}
66 
67  /// @{
68  /// Save/Load vertex list to a JSON stream
69  bool saveVertexArray(UT_JSONWriter &w,
70  const GA_SaveMap &map) const;
71  bool loadVertexArray(UT_JSONParser &p,
72  const GA_LoadMap &map);
73  /// @}
74 
75 #if !GA_PRIMITIVE_VERTEXLIST
76  SYS_DEPRECATED_HDK(13.0)
77  virtual void copyPrimitive(const GEO_Primitive *src);
78  virtual GEO_Primitive *copy(int preserve_shared_pts = 0) const;
79 
80  virtual void copyUnwiredForMerge(const GA_Primitive *src,
81  const GA_MergeMap &map);
82 
83  virtual void addPointRefToGroup(GA_PointGroup &grp) const final;
84 #endif
85  virtual bool changePointRef(GA_Offset from, GA_Offset to);
86 
87  virtual bool isDegenerate() const;
88 
89  // Adjust the size of the vertex array.
90 #if GA_PRIMITIVE_VERTEXLIST
92  void setSize(GA_Size nvertices)
93  { setNumVertices(nvertices); }
94 #else
95  void setSize(GA_Size sz);
96 #endif
97 
98  // Take the whole set of points into consideration when applying the
99  // point removal operation to this primitive. The method returns 0 if
100  // successful, -1 if it failed because it would have become degenerate,
101  // and -2 if it failed because it would have had to remove the primitive
102  // altogether.
103  virtual int detachPoints(GA_PointGroup &grp);
104 
105  /// Before a point is deleted, all primitives using the point will be
106  /// notified. The method should return "false" if it's impossible to
107  /// delete the point. Otherwise, the vertices should be removed.
108  virtual GA_DereferenceStatus dereferencePoint(GA_Offset point,
109  bool dry_run=false);
110  virtual GA_DereferenceStatus dereferencePoints(
111  const GA_RangeMemberQuery &pt_q,
112  bool dry_run=false);
113 
114  // Insert or delete vertices. The insertion methods return the index if
115  // successful and -1 otherwise. The deletion methods return 0 if ok and
116  // -1 otherwise.
117  virtual GA_Size insertVertex(GA_Offset ppt, GA_Size where=0);
118  virtual GA_Size appendVertex(GA_Offset ppt);
119  virtual int deleteVertex(GA_Size num);
120 
121  /// Remove all vertices listed in the array.
122  /// The array must be in order, contain no duplicates, and all
123  /// numbers must be in the range [0, getVertexCount()).
124  virtual void deleteVertices(const UT_Array<GA_Size> &nums);
125 
126  /// return the index of a vertex within our vertex list
127  GA_Size findVertex(GA_Offset vtx) const { return myVertexList.find(vtx); }
128 
129  /// Steal a vertex from its current primitive and insert it into our vertex list.
130  ///
131  /// @param vtx Vertex to be stolen
132  /// @param insert_before_vtx (Optional) the vertex before which to insert the stolen vertex
133  /// If unspecified (negative) or if the vertex doesn't exist, the stolen vertex is
134  /// appended at the end of the vertex list.
135  /// @return The position at which vtx is inserted if successful, or -1 otherwise.
136  virtual GA_Size stealVertex(GA_Offset vtx,
137  GA_Offset insert_before_vtx = GA_INVALID_OFFSET);
138 
139  // Query the number of vertices in the array. This number may be smaller
140  // than the actual size of the array.
141 #if !GA_PRIMITIVE_VERTEXLIST
142  virtual GA_Size getVertexCount() const final
143  { return myVertexList.size(); }
144 #endif
146  { return myVertexList.size(); }
147 
148 #if !GA_PRIMITIVE_VERTEXLIST
149  virtual GA_Offset getVertexOffset(GA_Size index) const final
150  {
151  UT_ASSERT_P(index >= 0 && index < myVertexList.size());
152  return myVertexList(index);
153  }
154 #endif
156  {
157  UT_ASSERT_P(index >= 0 && index < myVertexList.size());
158  return myVertexList(index);
159  }
160 
161 #if !GA_PRIMITIVE_VERTEXLIST
163  { return getDetail().vertexIndex(getFastVertexOffset(i)); }
165  { return getDetail().vertexPoint(getFastVertexOffset(i)); }
167  { return getDetail().pointIndex(getPointOffset(i)); }
169  { return getDetail().getPos3(getPointOffset(i)); }
170  SYS_FORCE_INLINE void setPos3(GA_Size i, const UT_Vector3 &pos) const
171  { getDetail().setPos3(getPointOffset(i), pos); }
173  { return getDetail().getPos4(getPointOffset(i)); }
174  SYS_FORCE_INLINE void setPos4(GA_Size i, const UT_Vector4 &pos) const
175  { getDetail().setPos4(getPointOffset(i), pos); }
176 
177  /// A trivial vertex list is a uniform ascending list, so
178  /// getFastVertexOffset(i) == getFastVertexOffset(0) + i
179  bool isVertexListTrivial() const { return myVertexList.isTrivial(); }
180  /// A trivial point list is doubly-trivial:
181  /// getPointOffset(i) == getPointOffset(0) + i
182  /// This requires testing every point so is slower than isVertexListTrivial.
183  bool isPointListTrivial() const;
184 
185  virtual void beginVertex(const_iterator &i) const final;
186  virtual void nextVertex(const_iterator &i) const final;
187 #endif
188 
189  // Find the index of the given vertex that refers to pt.
190  // Return -1 if not found.
191  GA_Size find(GA_Offset pt) const;
192 
193  // Subscript operators. The () operator does not check the boundaries.
194  SYS_DEPRECATED_HDK(13.0)
195  const GEO_Vertex operator()(GA_Size i) const
196  { return getVertexElement(i); }
197  SYS_DEPRECATED_HDK(13.0)
198  GEO_Vertex operator()(GA_Size i)
199  { return getVertexElement(i); }
200  SYS_DEPRECATED_HDK(13.0)
201  const GEO_Vertex operator[](GA_Size i) const
202  { return getVertexElement(i); }
203  SYS_DEPRECATED_HDK(13.0)
204  GEO_Vertex operator[](GA_Size i)
205  { return getVertexElement(i); }
206 
208  {
209  if (i < myVertexList.entries())
210  wireVertex(myVertexList(i), pt);
211  }
212 
213 #if !GA_PRIMITIVE_VERTEXLIST
214  // Have we been deactivated and stashed?
215  virtual void stashed(bool beingstashed,
217 #endif
218 
219  // Methods to handle vertex attributes for the attribute dictionary
220  virtual bool vertexApply(bool (*apply)(GA_Offset vtx, void *),
221  void *data = 0) const final;
222 
223  // Map the normalized length (distance value [0,1]) parameter to the unit
224  // parameterization of the primitve
225  virtual void unitLengthToUnitPair(float ulength, float vlength,
226  float &uparm, float &vparm)const;
227 
228  virtual void unitToUnitLengthPair(float uparm, float vparm,
229  float &ulength, float &vlength)
230  const;
231 
232  /// Replaces the entire vertex list.
233  /// Use with *utmost* caution. It probably won't work with Nurbs or
234  /// Polysoups, and possibly other primitives and is only tested for
235  /// Polygons.
236  ///
237  /// @param destroy_existing
238  /// If set, existing vertex offsets are destroyed.
239  /// @param update_topology
240  /// If set, new vertices are wired to the primitive.
241  void assignVertexList(const GA_OffsetList &list,
242  bool destroy_existing = true,
243  bool update_topology = true);
244 
245 protected:
246 #if !GA_PRIMITIVE_VERTEXLIST
247  virtual void clearForDeletion();
248 #endif
249 
251  { return GA_FAMILY_NONE; }
252 
253  /// All subclasses should call this method to register the curve intrinsics.
254  /// @see GA_IntrinsicManager
257  { return GEO_Primitive::registerIntrinsics(defn); }
258 
259  virtual bool savePrivateH9(std::ostream &os, bool binary) const = 0;
260  virtual bool loadPrivateH9(UT_IStream &is) = 0;
261 
262  // Check the validity of the data. Meant to be called especially at loading
263  // time.
264  virtual bool validate(void) const;
265 
266  // Allow derived classes to override the minimum number of vertices needed
267  // for a non-degenerate primitive.
268  virtual GA_Size getMinVertexCount() const { return 3; }
269 
270  // Insert a vertex multiple times in the list. Return the index.
271  GA_Size multipleInsert(GA_Size where,
272  GA_Size count,
273  bool appendPoints = true);
274 
275  /// @warning vertexPoint() doesn't check the bounds. Use with caution.
277  { return getDetail().vertexPoint(myVertexList(i)); }
278 
279  /// @warning swapVertices() doesn't check the bounds, nor that j != i. Use
280  /// with caution.
282  {
283  GA_Offset tmp = myVertexList(i);
284  myVertexList.set(i, myVertexList(j));
285  myVertexList.set(j, tmp);
286  }
287 
288 #if !GA_PRIMITIVE_VERTEXLIST
289  /// Defragmentation
290  virtual void swapVertexOffsets(const GA_Defragment &defrag);
291 #endif
292 
293 #if !GA_PRIMITIVE_VERTEXLIST
295 #endif
296 
297  virtual bool evaluatePointRefMap(GA_Offset result_vertex,
298  GA_AttributeRefMap &hlist,
299  fpreal u, fpreal v, uint du, uint dv) const;
300  virtual int evaluatePointV4( UT_Vector4 &pos, float u, float v = 0,
301  unsigned du=0, unsigned dv=0) const
302  { return GEO_Primitive::evaluatePointV4(pos, u, v, du, dv); }
303 
304  virtual bool evaluateBaryCenterRefMap(GA_Offset result_vertex,
305  GA_AttributeRefMap &hlist) const;
306 
307  /// Release the vertex vtx, i.e. deletes it from myVertexList but doesn't delete
308  /// the offset itself. Consequently, topology information and attributes are preserved.
309  /// It's meant to be used by stealVertex
310  /// @see stealVertex
311  /// @note overriding from GA_Primitive
312  virtual GA_Offset releaseVertex(GA_Offset vtx);
313 
314 #if !GA_PRIMITIVE_VERTEXLIST
315  /// Report approximate memory usage for myVertexList for subclasses.
316  int64 getBaseMemoryUsage() const;
317 
318  /// This is called by the subclasses to count the
319  /// memory used by myVertexList
320  void countBaseMemory(UT_MemoryCounter &counter) const;
321 #endif
322 
323 private:
324 
325 
326  friend std::ostream &operator<<(std::ostream &os, const GEO_TriMesh &d)
327  {
328  d.saveH9(os, 0,
331  return os;
332  }
333 };
334 
336 
337 #endif
338 
void setVertexPoint(GA_Size i, GA_Offset pt)
Definition: GEO_TriMesh.h:207
static GA_IntrinsicManager::Registrar registerIntrinsics(GA_PrimitiveDefinition &defn)
Definition: GEO_TriMesh.h:256
SYS_FORCE_INLINE void setPos3(GA_Offset ptoff, const UT_Vector3 &P)
Set P from a UT_Vector3.
Definition: GA_Detail.h:203
Used to pass options and map offset values during saving.
Definition: GA_SaveMap.h:48
#define SYS_DEPRECATED_PUSH_DISABLE()
const GLdouble * v
Definition: glcorearb.h:836
UT_Vector4 getPos4(GA_Offset ptoff) const
The ptoff passed is the point offset.
Definition: GA_Detail.h:248
#define SYS_DEPRECATED_POP_DISABLE()
SYS_FORCE_INLINE GA_Index getVertexIndex(GA_Size i) const
Definition: GEO_TriMesh.h:162
const GLuint GLenum const void * binary
Definition: glcorearb.h:1923
SYS_FORCE_INLINE UT_Vector3 getPos3(GA_Size i) const
Definition: GEO_TriMesh.h:168
The merge map keeps track of information when merging details.
Definition: GA_MergeMap.h:53
SYS_FORCE_INLINE GEO_TriMesh(GA_Detail *d, GA_Offset offset=GA_INVALID_OFFSET)
Definition: GEO_TriMesh.h:38
bool isVertexListTrivial() const
Definition: GEO_TriMesh.h:179
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:72
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
Abstract base class for a range membership query object.
SYS_FORCE_INLINE UT_Vector3 getPos3(GA_Offset ptoff) const
The ptoff passed is the point offset.
Definition: GA_Detail.h:170
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
GA_PrimitiveFamilyMask
#define GA_INVALID_OFFSET
Definition: GA_Types.h:654
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:101
SYS_FORCE_INLINE GA_Offset getFastVertexOffset(GA_Size index) const
Definition: GEO_TriMesh.h:155
GA_Size GA_Offset
Definition: GA_Types.h:617
long long int64
Definition: SYS_Types.h:106
GA_Offset getPointOffset() const
Definition: GA_LoadMap.h:171
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLintptr offset
Definition: glcorearb.h:664
#define GEO_API
Definition: GEO_API.h:10
SYS_FORCE_INLINE GA_Offset getPointOffset(GA_Size i) const
Definition: GEO_TriMesh.h:164
A handle to simplify manipulation of multiple attributes.
SYS_FORCE_INLINE GA_Index vertexIndex(GA_Offset offset) const
Given a vertex's data offset, return its index.
Definition: GA_Detail.h:459
SYS_FORCE_INLINE GA_Offset vertexPoint(GA_Size i) const
Definition: GEO_TriMesh.h:276
Options during loading.
Definition: GA_LoadMap.h:42
Defragmentation of IndexMaps.
Definition: GA_Defragment.h:45
SYS_FORCE_INLINE GA_Offset vertexPoint(GA_Offset vertex) const
Given a vertex, return the point it references.
Definition: GA_Detail.h:469
GLboolean * data
Definition: glcorearb.h:130
#define SYS_DEPRECATED_HDK(__V__)
GA_Size GA_Index
Define the strictness of GA_Offset/GA_Index.
Definition: GA_Types.h:611
GA_Detail & getDetail() const
Get the geometry being loaded.
Definition: GA_LoadMap.h:59
unsigned int uint
Definition: SYS_Types.h:39
GLint GLsizei count
Definition: glcorearb.h:404
virtual bool saveH9(std::ostream &os, bool binary, const UT_Array< GA_AttribSaveDataH9 > &prim_attribs, const UT_Array< GA_AttribSaveDataH9 > &vtx_attribs) const
SYS_FORCE_INLINE GA_Index pointIndex(GA_Offset offset) const
Given a point's data offset, return its index.
Definition: GA_Detail.h:296
void swapVertices(GA_Size i, GA_Size j)
Definition: GEO_TriMesh.h:281
SYS_FORCE_INLINE UT_Vector4 getPos4(GA_Size i) const
Definition: GEO_TriMesh.h:172
SYS_FORCE_INLINE void setPos3(GA_Size i, const UT_Vector3 &pos) const
Definition: GEO_TriMesh.h:170
double fpreal
Definition: SYS_Types.h:269
virtual GA_Offset getVertexOffset(GA_Size index) const final
Definition: GEO_TriMesh.h:149
SYS_FORCE_INLINE void setPos4(GA_Size i, const UT_Vector4 &pos) const
Definition: GEO_TriMesh.h:174
GA_OffsetList myVertexList
Definition: GEO_TriMesh.h:294
SYS_FORCE_INLINE GA_Index getPointIndex(GA_Size i) const
Definition: GEO_TriMesh.h:166
static const UT_Array< GA_AttribSaveDataH9 > & theEmptySaveAttribs
Convience objects to pass as arguments to saveH9()/loadH9().
GA_Size findVertex(GA_Offset vtx) const
return the index of a vertex within our vertex list
Definition: GEO_TriMesh.h:127
GLuint index
Definition: glcorearb.h:785
virtual GA_Size getVertexCount() const final
Definition: GEO_TriMesh.h:142
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:75
Container class for all geometry.
Definition: GA_Detail.h:96
virtual int evaluatePointV4(UT_Vector4 &pos, float u, float v=0, unsigned du=0, unsigned dv=0) const
Definition: GEO_TriMesh.h:300
png_infop png_uint_32 int num
Definition: png.h:2158
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
SYS_FORCE_INLINE GA_Size getFastVertexCount() const
Definition: GEO_TriMesh.h:145
Definition of a geometric primitive.
#define const
Definition: zconf.h:214
friend std::ostream & operator<<(std::ostream &os, const GEO_TriMesh &d)
Definition: GEO_TriMesh.h:326
static GA_IntrinsicManager::Registrar registerIntrinsics(GA_PrimitiveDefinition &defn)
virtual GA_Size getMinVertexCount() const
Definition: GEO_TriMesh.h:268
void setPos4(GA_Offset ptoff, const UT_Vector4 &P)
Set P from a UT_Vector4.
Definition: GA_Detail.h:254
virtual int evaluatePointV4(UT_Vector4 &pos, float u, float v=0, unsigned du=0, unsigned dv=0) const
static GA_PrimitiveFamilyMask buildFamilyMask()
Definition: GEO_TriMesh.h:250
GLenum src
Definition: glcorearb.h:1792