HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GEO_PrimTetra.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 /*
27  * COMMENTS: This is an HDK example for making a custom tetrahedron primitive type.
28  */
29 
30 #ifndef __HDK_GEO_PrimTetra__
31 #define __HDK_GEO_PrimTetra__
32 
33 #include <GEO/GEO_Primitive.h>
34 #include <GA/GA_Detail.h>
36 #include <GA/GA_Types.h>
37 #include <UT/UT_Interrupt.h>
38 #include <UT/UT_ParallelUtil.h>
39 #include <UT/UT_Vector3.h>
40 
41 namespace HDK_Sample {
42 
44 {
45 protected:
46  /// NOTE: The destructor should only be called from GA_PrimitiveList,
47  /// via a GA_Primitive pointer.
48  ~GEO_PrimTetra() override;
49 public:
50  /// NOTE: To create a new primitive owned by the detail, call
51  /// GA_Detail::appendPrimitive or appendPrimitiveBlock on
52  /// the detail, not this constructor.
54 
55  /// @{
56  /// Required interface methods
57  bool isDegenerate() const override;
58  int getBBox(UT_BoundingBox *bbox) const override;
59  void reverse() override;
60  UT_Vector3 computeNormal() const override;
61  void copyPrimitive(const GEO_Primitive *src) override;
62  void copySubclassData(const GA_Primitive *source) override;
63 
64  // Take the whole set of points into consideration when applying the
65  // point removal operation to this primitive. The method returns 0 if
66  // successful, -1 if it failed because it would have become degenerate,
67  // and -2 if it failed because it would have had to remove the primitive
68  // altogether.
69  int detachPoints(GA_PointGroup &grp) override;
70  /// Before a point is deleted, all primitives using the point will be
71  /// notified. The method should return "false" if it's impossible to
72  /// delete the point. Otherwise, the vertices should be removed.
74  bool dry_run=false) override;
76  bool dry_run=false) override;
77 
78  const GA_PrimitiveJSON *getJSON() const override;
79 
80  /// Evalaute a point given a u,v coordinate (with derivatives)
82  GA_Offset result_vtx,
83  GA_AttributeRefMap &hlist,
84  fpreal u, fpreal v,
85  uint du, uint dv) const override;
86  /// Evalaute position given a u,v coordinate (with derivatives)
88  UT_Vector4 &pos,
89  float u, float v = 0,
90  unsigned du=0, unsigned dv=0) const override
91  {
92  return GEO_Primitive::evaluatePoint(pos, u, v,
93  du, dv);
94  }
95  /// @}
96 
97  /// @{
98  /// Though not strictly required (i.e. not pure virtual), these methods
99  /// should be implemented for proper behaviour.
100  GEO_Primitive *copy(int preserve_shared_pts = 0) const override;
101 
102  // Have we been deactivated and stashed?
103  void stashed(bool beingstashed,
104  GA_Offset offset=GA_INVALID_OFFSET) override;
105  /// @}
106 
107  /// @{
108  /// Optional interface methods. Though not required, implementing these
109  /// will give better behaviour for the new primitive.
110  UT_Vector3 baryCenter() const override;
111  fpreal calcVolume(const UT_Vector3 &refpt) const override;
112  fpreal calcArea() const override;
113  fpreal calcPerimeter() const override;
114  /// @}
115 
116  /// Load the order from a JSON value
117  bool loadOrder(const UT_JSONValue &p);
118 
119  /// @{
120  /// Save/Load vertex list to a JSON stream
122  const GA_SaveMap &map) const;
124  const GA_LoadMap &map);
125  /// @}
126 
127  /// Finds where the specified vertex offset is in this primitive's
128  /// vertex list.
130  {
131  for (GA_Size i = 0; i < 4; i++)
132  {
133  if (getVertexOffset(i) == vtxoff)
134  return i;
135  }
136  return -1;
137  }
138 
139  /// Finds where in this primitive's vertex list, some vertex
140  /// is wired to the specified point offset.
142  {
143  for (GA_Size i = 0; i < 4; i++)
144  {
145  if (getPointOffset(i) == ptoff)
146  return i;
147  }
148  return -1;
149  }
150 
151  /// Report approximate memory usage.
152  int64 getMemoryUsage() const override;
153 
154  /// Count memory usage using a UT_MemoryCounter in order to count
155  /// shared memory correctly.
156  /// NOTE: This should always include sizeof(*this).
157  void countMemory(UT_MemoryCounter &counter) const override;
158 
159  /// Allows you to find out what this primitive type was named.
160  static const GA_PrimitiveTypeId &theTypeId() { return theDefinition->getId(); }
161 
162  /// Must be invoked during the factory callback to add us to the
163  /// list of primitives
164  static void registerMyself(GA_PrimitiveFactory *factory);
165 
166  const GA_PrimitiveDefinition &getTypeDef() const override
167  { return *theDefinition; }
168 
169  /// @{
170  /// Conversion functions
172  GA_PointGroup *usedpts = 0) override;
173  GEO_Primitive *convertNew(GEO_ConvertParms &parms) override;
174  /// @}
175 
176  /// Optional build function
177  static GEO_PrimTetra *build(GA_Detail *gdp, bool appendpts = true);
178 
179  /// Builds tetrahedrons using the specified range of point offsets,
180  /// as dictated by ntets and tetpointnumbers, in parallel.
181  /// tetpointnumbers lists the *offsets* of the points used by
182  /// each tetrahedron *MINUS* startpt, i.e. they are offsets relative to
183  /// startpt, *not* indices relative to startpt. The offset of the first
184  /// tetrahedron is returned, and the rest are at consecutive offsets. All
185  /// tetpointnumbers must be between 0 (inclusive) and npoints (exclusive).
186  ///
187  /// NOTE: Existing primitives *are* allowed to be using the points in
188  /// the specified range already, and the tetrahedrons being created do not
189  /// do not need to use all of the points in the range. However,
190  /// these cases may impact performance.
191  static GA_Offset buildBlock(GA_Detail *detail,
192  const GA_Offset startpt,
193  const GA_Size npoints,
194  const GA_Size ntets,
195  const int *tetpointnumbers);
196 
197  void normal(NormalComp &output) const override;
198 
199  int intersectRay(const UT_Vector3 &o, const UT_Vector3 &d,
200  float tmax = 1E17F, float tol = 1E-12F,
201  float *distance = 0, UT_Vector3 *pos = 0,
202  UT_Vector3 *nml = 0, int accurate = 0,
203  float *u = 0, float *v = 0,
204  int ignoretrim = 1) const override;
205 
206  // NOTE: This functor class must be in the scope of GEO_PrimTetra
207  // so that it can access myVertexList.
208  // It has to be public so that UTparallelForLightItems
209  // can be instantiated for GCC with it.
211  {
212  public:
214  const GA_Offset startvtx)
215  : myPrimitiveList(detail->getPrimitiveList())
216  , myStartPrim(startprim)
217  , myStartVtx(startvtx)
218  {}
219 
221  {
222  char bcnt = 0;
223  UT_Interrupt *boss = UTgetInterrupt();
224 
225  GA_Offset vtxoff = myStartVtx + 4*r.begin();
226  for (GA_Size i = r.begin(); i != r.end(); ++i)
227  {
228  if (!bcnt++ && boss->opInterrupt())
229  break;
230  GA_Offset offset = myStartPrim + i;
231  GEO_PrimTetra *tet = (GEO_PrimTetra *)myPrimitiveList.get(offset);
232  tet->myVertexList.setTrivial(vtxoff, 4);
233  vtxoff += 4;
234  }
235  }
236 
237  private:
238  GA_PrimitiveList &myPrimitiveList;
239  const GA_Offset myStartPrim;
240  const GA_Offset myStartVtx;
241  };
242 
243 protected:
244  /// Declare methods for implementing intrinsic attributes.
245  GA_DECLARE_INTRINSICS(override)
246 
248 private:
249  static GA_PrimitiveDefinition *theDefinition;
250 };
251 
252 }
253 
254 #endif
static void registerMyself(GA_PrimitiveFactory *factory)
GA_Size findPoint(GA_Offset ptoff) const
bool saveVertexArray(UT_JSONWriter &w, const GA_SaveMap &map) const
GEO_Primitive * convert(GEO_ConvertParms &parms, GA_PointGroup *usedpts=0) override
void stashed(bool beingstashed, GA_Offset offset=GA_INVALID_OFFSET) override
SYS_FORCE_INLINE GA_Offset getPointOffset(GA_Size i) const
Definition: GA_Primitive.h:254
void createVertices() const
Declare methods for implementing intrinsic attributes.
GLenum src
Definition: glew.h:2410
static GEO_PrimTetra * build(GA_Detail *gdp, bool appendpts=true)
Optional build function.
Used to pass options and map offset values during saving.
Definition: GA_SaveMap.h:48
bool evaluatePointRefMap(GA_Offset result_vtx, GA_AttributeRefMap &hlist, fpreal u, fpreal v, uint du, uint dv) const override
Evalaute a point given a u,v coordinate (with derivatives)
UT_Vector3 computeNormal() const override
int getBBox(UT_BoundingBox *bbox) const override
int64 getMemoryUsage() const override
Report approximate memory usage.
void setTrivial(ToType startvalue, GA_Size size)
Makes the list a trivial list with the specified start value and size.
int detachPoints(GA_PointGroup &grp) override
GLsizei GLsizei GLchar * source
Definition: glew.h:1832
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:76
#define GA_DECLARE_INTRINSICS(OVERRIDE)
Definition: GA_Primitive.h:80
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:34
GA_DereferenceStatus dereferencePoint(GA_Offset point, bool dry_run=false) override
Abstract base class for a range membership query object.
const GLdouble * v
Definition: glew.h:1391
void normal(NormalComp &output) const override
bool loadOrder(const UT_JSONValue &p)
Load the order from a JSON value.
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:233
void copySubclassData(const GA_Primitive *source) override
#define GA_INVALID_OFFSET
Definition: GA_Types.h:676
GA_OffsetList myVertexList
Definition: GA_Primitive.h:854
GA_Size GA_Offset
Definition: GA_Types.h:639
int opInterrupt(int percent=-1)
static GA_Offset buildBlock(GA_Detail *detail, const GA_Offset startpt, const GA_Size npoints, const GA_Size ntets, const int *tetpointnumbers)
const GA_PrimitiveJSON * getJSON() const override
fpreal calcPerimeter() const override
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1890
GEO_PrimTetra(GA_Detail &d, GA_Offset offset)
Definition: GEO_PrimTetra.C:86
void copyPrimitive(const GEO_Primitive *src) override
Provide a JSON interface to a primitive.
void
Definition: png.h:1083
GLsizei GLsizei GLfloat distance
Definition: glew.h:13640
bool evaluatePoint(GA_Offset result_vtx, GA_AttributeRefMap &map, fpreal u, fpreal v=0, uint du=0, uint dv=0) const
long long int64
Definition: SYS_Types.h:116
A handle to simplify manipulation of multiple attributes.
Options during loading.
Definition: GA_LoadMap.h:42
geo_SetVertexListsParallel(GA_Detail *detail, const GA_Offset startprim, const GA_Offset startvtx)
static const GA_PrimitiveTypeId & theTypeId()
Allows you to find out what this primitive type was named.
const GA_PrimitiveDefinition & getTypeDef() const override
void countMemory(UT_MemoryCounter &counter) const override
int evaluatePointV4(UT_Vector4 &pos, float u, float v=0, unsigned du=0, unsigned dv=0) const override
Evalaute position given a u,v coordinate (with derivatives)
Definition: GEO_PrimTetra.h:87
A list of primitives.
fpreal calcArea() const override
GLfloat GLfloat p
Definition: glew.h:16321
SYS_FORCE_INLINE const GA_PrimitiveTypeId & getId() const
The unique ID assigned by the GA_PrimitiveFactory.
SYS_FORCE_INLINE const GA_Primitive * get(GA_Offset off) const
void operator()(const UT_BlockedRange< GA_Size > &r) const
int intersectRay(const UT_Vector3 &o, const UT_Vector3 &d, float tmax=1E17F, float tol=1E-12F, float *distance=0, UT_Vector3 *pos=0, UT_Vector3 *nml=0, int accurate=0, float *u=0, float *v=0, int ignoretrim=1) const override
GEO_Primitive * convertNew(GEO_ConvertParms &parms) override
GLuint counter
Definition: glew.h:2740
fpreal64 fpreal
Definition: SYS_Types.h:277
UT_API UT_Interrupt * UTgetInterrupt()
Obtain global UT_Interrupt singleton.
GA_DereferenceStatus dereferencePoints(const GA_RangeMemberQuery &pt_q, bool dry_run=false) override
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
SYS_FORCE_INLINE GA_Offset getVertexOffset(GA_Size primvertexnum) const
Definition: GA_Primitive.h:240
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:77
Container class for all geometry.
Definition: GA_Detail.h:95
Definition of a geometric primitive.
bool loadVertexArray(UT_JSONParser &p, const GA_LoadMap &map)
#define const
Definition: zconf.h:214
bool isDegenerate() const override
Declare prior to use.
unsigned int uint
Definition: SYS_Types.h:45
UT_Vector3 baryCenter() const override
GA_Size findVertex(GA_Offset vtxoff) const
GEO_Primitive * copy(int preserve_shared_pts=0) const override
fpreal calcVolume(const UT_Vector3 &refpt) const override
GLintptr offset
Definition: glew.h:1682