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) 2022
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  bool getBBox(UT_BoundingBox *bbox) const override;
59  void reverse() override;
60  UT_Vector3D computeNormalD() const override;
61  UT_Vector3 computeNormal() const override;
62  void copyPrimitive(const GEO_Primitive *src) override;
63  void copySubclassData(const GA_Primitive *source) override;
64 
65  // Take the whole set of points into consideration when applying the
66  // point removal operation to this primitive. The method returns 0 if
67  // successful, -1 if it failed because it would have become degenerate,
68  // and -2 if it failed because it would have had to remove the primitive
69  // altogether.
70  int detachPoints(GA_PointGroup &grp) override;
71  /// Before a point is deleted, all primitives using the point will be
72  /// notified. The method should return "false" if it's impossible to
73  /// delete the point. Otherwise, the vertices should be removed.
75  bool dry_run=false) override;
77  bool dry_run=false) override;
78 
79  const GA_PrimitiveJSON *getJSON() const override;
80 
81  /// Evalaute a point given a u,v coordinate (with derivatives)
83  GA_Offset result_vtx,
84  GA_AttributeRefMap &hlist,
85  fpreal u, fpreal v,
86  uint du, uint dv) const override;
87  /// Evalaute position given a u,v coordinate (with derivatives)
89  UT_Vector4 &pos,
90  float u, float v = 0,
91  unsigned du=0, unsigned dv=0) const override
92  {
93  return GEO_Primitive::evaluatePoint(pos, u, v,
94  du, dv);
95  }
96  /// @}
97 
98  /// @{
99  /// Though not strictly required (i.e. not pure virtual), these methods
100  /// should be implemented for proper behaviour.
101  GEO_Primitive *copy(int preserve_shared_pts = 0) const override;
102 
103  // Have we been deactivated and stashed?
104  void stashed(bool beingstashed,
105  GA_Offset offset=GA_INVALID_OFFSET) override;
106  /// @}
107 
108  /// @{
109  /// Optional interface methods. Though not required, implementing these
110  /// will give better behaviour for the new primitive.
111  UT_Vector3 baryCenter() const override;
112  fpreal calcVolume(const UT_Vector3 &refpt) const override;
113  fpreal calcArea() const override;
114  fpreal calcPerimeter() const override;
115  /// @}
116 
117  /// Load the order from a JSON value
118  bool loadOrder(const UT_JSONValue &p);
119 
120  /// @{
121  /// Save/Load vertex list to a JSON stream
123  const GA_SaveMap &map) const;
125  const GA_LoadMap &map);
126  /// @}
127 
128  /// Finds where the specified vertex offset is in this primitive's
129  /// vertex list.
131  {
132  for (GA_Size i = 0; i < 4; i++)
133  {
134  if (getVertexOffset(i) == vtxoff)
135  return i;
136  }
137  return -1;
138  }
139 
140  /// Finds where in this primitive's vertex list, some vertex
141  /// is wired to the specified point offset.
143  {
144  for (GA_Size i = 0; i < 4; i++)
145  {
146  if (getPointOffset(i) == ptoff)
147  return i;
148  }
149  return -1;
150  }
151 
152  /// Report approximate memory usage.
153  int64 getMemoryUsage() const override;
154 
155  /// Count memory usage using a UT_MemoryCounter in order to count
156  /// shared memory correctly.
157  /// NOTE: This should always include sizeof(*this).
158  void countMemory(UT_MemoryCounter &counter) const override;
159 
160  /// Allows you to find out what this primitive type was named.
161  static const GA_PrimitiveTypeId &theTypeId() { return theDefinition->getId(); }
162 
163  /// Must be invoked during the factory callback to add us to the
164  /// list of primitives
165  static void registerMyself(GA_PrimitiveFactory *factory);
166 
167  const GA_PrimitiveDefinition &getTypeDef() const override
168  { return *theDefinition; }
169 
170  /// @{
171  /// Conversion functions
173  GA_PointGroup *usedpts = 0) override;
174  GEO_Primitive *convertNew(GEO_ConvertParms &parms) override;
175  /// @}
176 
177  /// Optional build function
178  static GEO_PrimTetra *build(GA_Detail *gdp, bool appendpts = true);
179 
180  /// Builds tetrahedrons using the specified range of point offsets,
181  /// as dictated by ntets and tetpointnumbers, in parallel.
182  /// tetpointnumbers lists the *offsets* of the points used by
183  /// each tetrahedron *MINUS* startpt, i.e. they are offsets relative to
184  /// startpt, *not* indices relative to startpt. The offset of the first
185  /// tetrahedron is returned, and the rest are at consecutive offsets. All
186  /// tetpointnumbers must be between 0 (inclusive) and npoints (exclusive).
187  ///
188  /// NOTE: Existing primitives *are* allowed to be using the points in
189  /// the specified range already, and the tetrahedrons being created do not
190  /// do not need to use all of the points in the range. However,
191  /// these cases may impact performance.
192  static GA_Offset buildBlock(GA_Detail *detail,
193  const GA_Offset startpt,
194  const GA_Size npoints,
195  const GA_Size ntets,
196  const int *tetpointnumbers);
197 
198  void normal(NormalComp &output) const override;
199  void normal(NormalCompD &output) const override;
200 
201  int intersectRay(const UT_Vector3 &o, const UT_Vector3 &d,
202  float tmax = 1E17F, float tol = 1E-12F,
203  float *distance = 0, UT_Vector3 *pos = 0,
204  UT_Vector3 *nml = 0, int accurate = 0,
205  float *u = 0, float *v = 0,
206  int ignoretrim = 1) const override;
207 
208  // NOTE: This functor class must be in the scope of GEO_PrimTetra
209  // so that it can access myVertexList.
210  // It has to be public so that UTparallelForLightItems
211  // can be instantiated for GCC with it.
213  {
214  public:
216  const GA_Offset startvtx)
217  : myPrimitiveList(detail->getPrimitiveList())
218  , myStartPrim(startprim)
219  , myStartVtx(startvtx)
220  {}
221 
223  {
224  char bcnt = 0;
225  UT_Interrupt *boss = UTgetInterrupt();
226 
227  GA_Offset vtxoff = myStartVtx + 4*r.begin();
228  for (GA_Size i = r.begin(); i != r.end(); ++i)
229  {
230  if (!bcnt++ && boss->opInterrupt())
231  break;
232  GA_Offset offset = myStartPrim + i;
233  GEO_PrimTetra *tet = (GEO_PrimTetra *)myPrimitiveList.get(offset);
234  tet->myVertexList.setTrivial(vtxoff, 4);
235  vtxoff += 4;
236  }
237  }
238 
239  private:
240  GA_PrimitiveList &myPrimitiveList;
241  const GA_Offset myStartPrim;
242  const GA_Offset myStartVtx;
243  };
244 
245 protected:
246  /// Declare methods for implementing intrinsic attributes.
247  GA_DECLARE_INTRINSICS(override)
248 
250 private:
251  static GA_PrimitiveDefinition *theDefinition;
252 };
253 
254 }
255 
256 #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.
GLuint counter
Definition: glew.h:2745
UT_Vector3D computeNormalD() const override
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
void
Definition: png.h:1083
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
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:88
#define GA_DECLARE_INTRINSICS(OVERRIDE)
Definition: GA_Primitive.h:80
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:35
GA_DereferenceStatus dereferencePoint(GA_Offset point, bool dry_run=false) override
Abstract base class for a range membership query object.
void normal(NormalComp &output) const override
GLenum src
Definition: glcorearb.h:1793
bool loadOrder(const UT_JSONValue &p)
Load the order from a JSON value.
bool getBBox(UT_BoundingBox *bbox) const override
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:234
void copySubclassData(const GA_Primitive *source) override
#define GA_INVALID_OFFSET
Definition: GA_Types.h:677
GA_OffsetList myVertexList
Definition: GA_Primitive.h:872
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
GA_Size GA_Offset
Definition: GA_Types.h:640
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)
NormalCompT< float > NormalComp
const GA_PrimitiveJSON * getJSON() const override
fpreal calcPerimeter() const override
GLsizei GLsizei GLfloat distance
Definition: glew.h:13923
const GLdouble * v
Definition: glcorearb.h:837
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
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.
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.
GLfloat GLfloat p
Definition: glew.h:16656
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:88
A list of primitives.
fpreal calcArea() const override
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
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
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
GLintptr offset
Definition: glcorearb.h:665
Definition of a geometric primitive.
bool loadVertexArray(UT_JSONParser &p, const GA_LoadMap &map)
#define const
Definition: zconf.h:214
GLboolean r
Definition: glcorearb.h:1222
NormalCompT< double > NormalCompD
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