HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GVEX_GeoCache.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: GVEX_GeoCache.h ( GVEX Library, C++)
7  *
8  * COMMENTS: Geometry cache for GVEX functions
9  */
10 
11 #ifndef __GVEX_GeoCache__
12 #define __GVEX_GeoCache__
13 
14 #include "GVEX_API.h"
15 #include <GU/GU_Detail.h>
16 #include <GEO/GEO_Quadric.h>
17 #include <GEO/GEO_PointGrid.h>
18 #include <GA/GA_Handle.h>
19 #include <TS/TS_Expression.h>
20 #include <TS/TS_Primitive.h>
21 #include <VEX/VEX_PodTypes.h>
22 #include <UT/UT_Array.h>
23 #include <UT/UT_BoundingBox.h>
24 #include <UT/UT_VectorTypes.h>
25 #include <SYS/SYS_Types.h>
27 #include <hboost/any.hpp>
28 
29 namespace GEO {
30 template<uint NAXES> class PointBVHT;
31 using PointBVH = PointBVHT<3>;
32 }
33 
34 template <VEX_Precision PREC> class VEX_Instance;
35 template <VEX_Precision PREC> class VEX_ArrayType;
36 class VEX_ProcArray;
37 class GU_Detail;
38 class GU_RayIntersect;
39 class GT_UtilOpenSubdiv;
40 class GEO_MetaExpression;
42 class TS_MetaExpression;
43 class TS_MetaPrimitive;
44 class IMG3D_PCloud;
46 class GU_PrimRTree;
47 class GU_VexGeoInputs;
48 class GU_OceanEvaluator;
52 
53 template <typename T>
55 {
56  static void
57  import(T &result, TS_MetaPrimitive *mprim, const UT_Vector3 &P)
58  {
59  result = mprim->density(P);
60  }
61 };
62 template <>
64 {
65  static void
66  import(int &result, TS_MetaPrimitive *mprim, const UT_Vector3 &P)
67  {
68  result = (int)SYSrint(mprim->density(P));
69  }
70 };
71 template <typename T>
73 {
74  static void
75  import(T &result, const GEO_Primitive *gprim)
76  {
77  result = 0;
78  }
79 };
80 template <>
82 {
83  static void
84  import(UT_Matrix4 &result, const GEO_Primitive *gprim)
85  {
86  if (gprim->getTypeId() == GA_PRIMMETABALL ||
87  gprim->getTypeId() == GA_PRIMMETASQUAD)
88  {
89  static_cast<const GEO_Quadric *>(gprim)->getTransform4(result);
90  }
91  }
92 };
93 
94 
96 {
97 public:
98  const char *myFilename;
99  int myMemory;
100 };
101 
103 {
104 public:
105  GVEX_MetaList();
106  ~GVEX_MetaList();
107 
108  enum
109  {
110  GVEX_META_DENSITY, // meta:density
111  GVEX_META_NUMBER, // meta:prim
112  GVEX_META_TRANSFORM, // meta:transform
113  GVEX_META_ATTRIB, // other attribute
114  };
115 
116  // Initialize lists
117  void init(VEX_Instance<VEX_32> &state, VEX_ProcArray &proc,
118  const VEXvec3<VEX_32> *P, int pinc, const char *file);
119 
120  // Prepare for import from the given attribute channel (the string
121  // instruction offset is specified in channel_name).
122  bool setAttribute(VEX_Instance<VEX_32> &state, VEX_ProcArray &proc,
123  int channel_name);
124 
125  int entries(int pnum) const;
126  template <typename T> void importData(T &result, int pnum, int idx)
127  {
128  if (!myPInc)
129  pnum = 0;
130  UT_ASSERT(pnum >= 0 && pnum < myListSize);
131  UT_Array<TS_MetaPrimitive *> &list = myLists[pnum];
132  const UT_Vector3 &P = myP[pnum];
133 
134  UT_ASSERT(idx >= 0 && idx < list.entries());
135  TS_MetaPrimitive *mprim = list(idx);
136  const GEO_Primitive *gprim = mprim->getGeoPrimPtr();
137 
138  switch (myMode)
139  {
140  case GVEX_META_DENSITY: // meta:density
141  GVEX_MetaDensity<T>::import(result, mprim, P);
142  break;
143 
144  case GVEX_META_NUMBER: // meta:prim
145  result = GA_Size(gprim->getMapIndex());
146  break;
147 
148  case GVEX_META_TRANSFORM: // meta:transform
149  GVEX_MetaTransform<T>::import(result, gprim);
150  break;
151 
152  default: // Other attribute
153  GA_ROHandleT<T> handle(myAttrib);
154  if (handle.isValid())
155  {
156  GA_AttributeOwner owner = myAttrib->getOwner();
158  if (owner == GA_ATTRIB_PRIMITIVE)
159  offset = gprim->getMapOffset();
160  else if (owner == GA_ATTRIB_DETAIL)
161  offset = GA_Offset(0);
162  else if (gprim->getVertexCount() >= 1)
163  {
164  GA_Offset vtxoff = gprim->getVertexOffset(0);
165  if (owner == GA_ATTRIB_VERTEX)
166  offset = vtxoff;
167  else
168  offset = gprim->getDetail().vertexPoint(vtxoff);
169  }
170  else
171  {
172  result = 0;
173  break;
174  }
175  result = handle.get(offset);
176  }
177  else
178  result = 0;
179  break;
180  }
181  }
182 
183  // Iteration interface for metastart() / metanext()
184  void advance() { myCurrent++; }
185  bool atEnd(int proc) { return myCurrent >= entries(proc); }
186  int getCurrent() { return myCurrent; }
187 
188 private:
189  void specializeTemplates();
190  void setListSize(int n);
191  void fillList(const UT_Vector3 &P,
192  const TS_MetaExpressionPtr &expr,
194 
195 private:
196  const GU_Detail *myGdp;
197  const UT_Vector3 *myP;
198  int myPInc;
200  int myListSize;
201  const GA_Attribute *myAttrib;
202  int myMode;
203  int myCurrent;
204 };
205 
206 /// An item in the geometry cache, which can be a pointer to anything. The
207 /// cache will take over ownership of the pointer and delete it at the
208 /// appropriate time.
210 {
211 public:
213  {
214  }
215 
216  template <typename T>
217  GVEX_GeoCacheItem(const T *value) : myItem(UT_SharedPtr<const T>(value))
218  {
219  }
220 
221  /// Access this item's data. The type must match, otherwise an exception
222  /// will be thrown.
223  template <typename T>
224  const T *get() const
225  {
226  return hboost::any_cast< UT_SharedPtr<const T> >(myItem).get();
227  }
228 
229 private:
230  hboost::any myItem;
231 };
232 
233 /// Callback to create a new item for the GVEX_GeoCache.
234 template <VEX_Precision PREC>
236 
238 
240 
242 {
243 public:
244  // Cache controls
245  // The memory available to the cache
246  // Auto-flush turns on automatic flushing of de-referenced geometry
247  // All methods return the previous value.
248  // Pruning the cache will attempt to free up some memory if possible
249  static int64 setCacheMemory(int64 memoryinkb=8192);
250  static int64 getCacheMemorySize();
251  static int setAutoFlush(int flush);
252  static int getAutoFlush();
253  static void clearCache(int out_of_date_only=0);
254  static void clearCache(const char* file);
255  static void pruneCache();
256 
257  static int64 getMemoryUsage();
258  static int64 getPeakUsage();
259 
260  static void setDefaultMetaballGeometry(GU_Detail *gdp);
261 
262  // Cache queries
263  // Find out how many entries are in the geometry cache
264  // Find out information about each entry (it's possible that the gdp
265  // returned might be null).
266  static int getCacheEntries();
267  static void getCacheStat(GVEX_GeoStat stat[]);
268 
269  // Construct a gvex_internalGeo * that refers to a given GU_Detail.
270  // This can be used to pre-populate the vexcache of GU_VexGeoInputs.
271  static void createInternalGeos(GU_VexGeoInputs *geoinputs);
272 
273  // Look up a GVEX_MetaList given the handle
274  static GVEX_MetaList *getMetaList(int id);
275 
276  // Retrieve a new handle id for metaball iteration. Use getMetaList()
277  // to retrieve the object.
278  static int startMetaList(VEX_Instance<VEX_32> &state,
279  VEX_ProcArray &proc,
280  const UT_Vector3 *P,
281  int pinc, const char *file);
282 
283 };
284 
285 template <VEX_Precision PREC>
287 {
288 public:
289  // Internal helper functions for binding geometry to VEX instructions.
290  // The bind function will return the index to access the detail (via the
291  // getDetail/getRayTree methods).
292  static void bindDetail(VEX_Instance<PREC> &state);
293 
294  // Access methods (once the detail has been bound)
295  static const GU_Detail *getDetail(VEX_Instance<PREC> &state,
296  const char *file);
297  static void getPrimBBox(VEX_Instance<PREC> &state,
298  UT_BoundingBox &box,
299  const char *file);
300  static void getPrimBBox(VEX_Instance<PREC> &state,
301  UT_BoundingBox &box,
302  const char *file,
303  const char *group);
304  static void getPointBBox(VEX_Instance<PREC> &state,
305  UT_BoundingBox &box,
306  const char *file);
307  static void getPointBBox(VEX_Instance<PREC> &state,
308  UT_BoundingBox &box,
309  const char *file,
310  const char *group);
311  static GU_RayIntersect *getRayTree(VEX_Instance<PREC> &state,
312  const char *file);
313  static GU_RayIntersect *getRayTree(VEX_Instance<PREC> &state,
314  const char *file,
315  const char *group);
316  static GU_RayIntersect *getMinTree(VEX_Instance<PREC> &state,
317  const char *file);
318  static GU_RayIntersect *getMinTree(VEX_Instance<PREC> &state,
319  const char *file,
320  const char *group);
321  static const openvdb::tools::PointIndexGrid *getPointGrid(VEX_Instance<PREC> &state,
322  float divsize,
323  const char *file,
324  const char *group);
325  static GA_IndexArray *getRingZero(VEX_Instance<PREC> &state,
326  int ptnum,
327  const char *file);
328  static UT_Array<GA_Size> *getPrimVertexArray(VEX_Instance<PREC> &state,
329  const char *file);
330  static UT_Array<GA_IndexArray> *getRingZeroArray(VEX_Instance<PREC> &state,
331  const char *file);
332  static GEO_DetachedHedgeInterface *getHedge(VEX_Instance<PREC> &state,
333  const char *file);
334 
335  static const GT_UtilOpenSubdiv *getOSDEvaluator(VEX_Instance<PREC> &state,
336  const char *file);
337 
338  static TS_MetaExpressionPtr getTSMetaExpr(VEX_Instance<PREC> &state,
339  const char *file);
340  static IMG3D_PCloud *getPCloud(VEX_Instance<PREC> &state,
341  const char *file);
342 
343  static void expandGroup(VEX_Instance<PREC> &state,
344  const char *file,
345  const char *group,
346  int grouptype,
347  bool ordered,
348  exint &alen,
349  const exint *&adata);
350 
351  static GU_PrimRTree *getPrimTree(VEX_Instance<PREC> &state,
352  const char *file);
353  static GU_PrimRTree *getPrimTree(VEX_Instance<PREC> &state,
354  const char *file,
355  const char *group);
356 
357  static GVEX_PointTreeType *getPointTree(VEX_Instance<PREC> &state,
358  const char *file,
359  const char *group,
360  const char *attrib);
361 
362  static GVEX_PointTreeType *getRadTree(VEX_Instance<PREC> &state,
363  const char *file,
364  const char *group,
365  const char *attrib,
366  const char *rad,
367  float radscale);
368 
369  static GU_OceanEvaluator *getOceanEval(VEX_Instance<PREC> &state,
370  const char *file,
371  int phase, int freq, int amp,
372  float hscale, float time,
373  int mode, int downsample);
374 
375  static GU_TetrahedronAdjacency *getTetAdj(VEX_Instance<PREC> &state,
376  const char *file);
377 
378  static GU_SurfaceDistanceCache *getSDCache(VEX_Instance<PREC> &state,
379  const char *file,
380  const char *group,
381  const char *p_attrib,
382  float radius,
383  const char *dist_metric);
384 
385  static GU_WindingNumber3DApprox *getWindingNumber(VEX_Instance<PREC> &state,
386  const char *file);
387  static GU_WindingNumber3DApprox *getWindingNumber(VEX_Instance<PREC> &state,
388  const char *file,
389  const char *group);
390  static GU_WindingNumber2DApprox *getWindingNumber2D(VEX_Instance<PREC> &state,
391  const char *file);
392  static GU_WindingNumber2DApprox *getWindingNumber2D(VEX_Instance<PREC> &state,
393  const char *file,
394  const char *group);
395 
396 
397  /// Access a custom item in the cache. The type must match, otherwise an
398  /// exception will be thrown. The callback will be run to create the item
399  /// if an item with that id does not already exist.
400  template <typename T>
401  static const T *get(const char *id, VEX_Instance<PREC> &state, const char *file,
403  {
404  return getCacheItem(id, state, file, callback).template get<T>();
405  }
406 
407 private:
408  /// Helper function to fetch a custom item from the cache, or create it if
409  /// necessary.
410  static GVEX_GeoCacheItem
411  getCacheItem(const char *id, VEX_Instance<PREC> &state, const char *file,
413 };
414 
417 
418 #endif
GVEX_ConstructCacheItemT< VEX_32 > GVEX_ConstructCacheItem
static void import(T &result, const GEO_Primitive *gprim)
Definition: GVEX_GeoCache.h:75
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
#define GVEX_API
Definition: GVEX_API.h:12
GT_API const UT_StringHolder filename
SYS_FORCE_INLINE GA_Detail & getDetail() const
Definition: GA_Primitive.h:141
SYS_FORCE_INLINE GA_Size getVertexCount() const
Return the number of vertices used by this primitive.
Definition: GA_Primitive.h:232
const char * myFilename
Definition: GVEX_GeoCache.h:98
GT_API const UT_StringHolder time
void importData(T &result, int pnum, int idx)
int64 exint
Definition: SYS_Types.h:125
GVEX_GeoCacheItem(*)(VEX_Instance< PREC > &state, const char *filename) GVEX_ConstructCacheItemT
Callback to create a new item for the GVEX_GeoCache.
**But if you need a result
Definition: thread.h:613
typename VEX_PrecisionResolver< P >::vec3_type VEXvec3
Definition: VEX_PodTypes.h:70
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:235
GVEX_GeoCacheItem(const T *value)
GA_Size GA_Offset
Definition: GA_Types.h:641
PointBVHT< 3 > PointBVH
Definition: GEO_BVH.h:598
GLdouble n
Definition: glcorearb.h:2008
GLintptr offset
Definition: glcorearb.h:665
GVEX_EXTERN_TEMPLATE(GVEX_GeoCacheT< VEX_32 >)
bool any(const vbool4 &v)
Definition: simd.h:3468
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
long long int64
Definition: SYS_Types.h:116
GLuint id
Definition: glcorearb.h:655
SYS_FORCE_INLINE T get(GA_Offset off, int comp=0) const
Definition: GA_Handle.h:203
SYS_FORCE_INLINE GA_Offset vertexPoint(GA_Offset vertex) const
Given a vertex, return the point it references.
Definition: GA_Detail.h:529
fpreal32 SYSrint(fpreal32 val)
Definition: SYS_Floor.h:163
GLenum mode
Definition: glcorearb.h:99
SYS_FORCE_INLINE bool isValid() const
Definition: GA_Handle.h:187
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:648
GA_AttributeOwner
Definition: GA_Types.h:34
SYS_FORCE_INLINE GA_Index getMapIndex() const
Gets the index of this primitive in the detail containing it.
Definition: GA_Primitive.h:151
Grid< PointIndexTree > PointIndexGrid
Point index grid.
Space-partitioning acceleration structure for points. Partitions the points into voxels to accelerate...
SYS_FORCE_INLINE GA_Offset getMapOffset() const
Gets the offset of this primitive in the detail containing it.
Definition: GA_Primitive.h:146
SYS_FORCE_INLINE GA_Offset getVertexOffset(GA_Size primvertexnum) const
Definition: GA_Primitive.h:240
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
Definition: core.h:1131
#define const
Definition: zconf.h:214
static void import(T &result, TS_MetaPrimitive *mprim, const UT_Vector3 &P)
Definition: GVEX_GeoCache.h:57
bool atEnd(int proc)
Class to perform subdivision refinement using OpenSubdivision.