HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GT_VolumeUtil.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: GT_VolumeUtil.h (GT Library, C++)
7  *
8  * COMMENTS: Shared utilities between GT_PrimVDB and GT_PrimVolume
9  */
10 
11 #ifndef __GT_VOLUMEUTIL_H_INCLUDED__
12 #define __GT_VOLUMEUTIL_H_INCLUDED__
13 
14 #include "GT_API.h"
15 #include "GT_DANumeric.h"
16 #include "GT_RefineParms.h"
17 
18 #include <UT/UT_Assert.h>
19 #include <UT/UT_Task.h>
20 #include <UT/UT_Vector3.h>
21 #include <UT/UT_VectorTypes.h>
22 #include <SYS/SYS_MathCbrt.h>
23 #include <SYS/SYS_Types.h>
24 
25 
26 /// Utility function for determining whether we should resample volumes in GT.
27 inline bool
29  int xres, int yres, int zres,
30  const GT_RefineParms *parms,
31  fpreal &scale_factor)
32 {
33  const fpreal max_lod = 4.0;
34  const fpreal lod = SYSmin(GT_RefineParms::getLOD(parms), max_lod);
35 
36  const exint min_dim = 64;
37  const fpreal min_voxels = min_dim * min_dim * min_dim * lod;
38  const fpreal num_voxels = exint(xres) * yres * zres;
39 
40  UT_ASSERT(num_voxels > 0);
41  if (num_voxels > 0 && num_voxels < min_voxels)
42  {
43  scale_factor = SYScbrt(num_voxels / min_voxels);
44  return true;
45  }
46 
47  // No resampling needed
48  scale_factor = 1.0;
49  return false;
50 }
51 
52 // Adapter for CopyTask
54 {
55 public:
57 
58  GT_Vec3ArrayProxy(GT_Real32Array &arr) : myArray(&arr)
59  {
60  }
61 
63  {
64  return *reinterpret_cast<UT_Vector3F *>(myArray->getData(i));
65  }
66 
67  void entries(exint num_items)
68  {
69  myArray->resize(num_items);
70  }
71 
72 private:
73  GT_Real32Array * myArray;
74 };
75 
76 // Created by GTappendCopyTask()
77 template <typename DstArrayT, typename SrcArrayT>
78 class GT_CopyTask : public UT_Task
79 {
80 public:
81  static UT_Task *
82  create(DstArrayT &dst, exint dst_start,
83  const SrcArrayT &src, exint num_elems)
84  {
85  return new (allocate_root())
86  GT_CopyTask(dst, dst_start, src, num_elems);
87  }
88 
89 private:
90  GT_CopyTask(DstArrayT &dst, exint dst_start,
91  const SrcArrayT &src, exint num_elems)
92  : myDst(&dst)
93  , myDstStart(dst_start)
94  , mySrc(&src)
95  , myNumElems(num_elems)
96  {
97  }
98 
99  virtual UT_Task *run()
100  {
101  exint bytes = myNumElems * sizeof(typename DstArrayT::value_type);
102  ::memcpy(&(*myDst)(myDstStart), &(*mySrc)(0), bytes);
103  return NULL;
104  }
105 
106  DstArrayT * myDst;
107  exint myDstStart;
108  const SrcArrayT * mySrc;
109  exint myNumElems;
110 };
111 
112 /// Used to create an UT_TaskList of array copy tasks
113 template <typename DstArrayT, typename SrcArrayT>
114 void
116  UT_TaskList &tasks,
117  DstArrayT &dst, exint dst_start,
118  SrcArrayT &src, exint num_elems)
119 {
120  if (num_elems <= 0)
121  return;
123  dst, dst_start, src, num_elems));
124 }
125 
126 /// Copy all the data in the thread chunks into a the destination.
127 template <typename DstArrayT, typename ThreadChunksT>
128 void
129 GTmergeThreadChunks(DstArrayT &dst, const ThreadChunksT &chunks)
130 {
131  exint size = 0;
132  exint max_items = 0;
133 
134  UT_TaskList copy_tasks;
135  typedef typename ThreadChunksT::value_type ChunkPtrT;
136  typedef typename ThreadChunksT::const_iterator ChunksIterT;
137  ChunksIterT end = chunks.end();
138  for (ChunksIterT it = chunks.begin(); it != end; ++it)
139  {
140  const ChunkPtrT &chunk = it.get();
141  if (!chunk)
142  continue;
143 
144  exint src_items = chunk->appendCopyTask(copy_tasks, dst, size);
145  if (src_items > max_items)
146  max_items = src_items;
147  size += src_items;
148  }
149  dst.entries(size);
150  if (max_items > 20000)
151  copy_tasks.spawnRootAndWait();
152  else
153  copy_tasks.runSerial();
154 }
155 
156 #endif // __GT_VOLUMEUTIL_H_INCLUDED__
bool GTvolumeNeedResample(int xres, int yres, int zres, const GT_RefineParms *parms, fpreal &scale_factor)
Utility function for determining whether we should resample volumes in GT.
Definition: GT_VolumeUtil.h:28
void spawnRootAndWait()
Spawn this task list as root tasks and wait.
Definition: UT_Task.h:143
void append(UT_Task &task)
Append a task.
Definition: UT_Task.h:136
void GTmergeThreadChunks(DstArrayT &dst, const ThreadChunksT &chunks)
Copy all the data in the thread chunks into a the destination.
UT_Vector3F value_type
Definition: GT_VolumeUtil.h:56
T * getData(GT_Offset offset)
Raw pointer access to a tuple.
Definition: GT_DANumeric.h:129
GT_Vec3ArrayProxy(GT_Real32Array &arr)
Definition: GT_VolumeUtil.h:58
UT_Vector3F & operator()(exint i)
Definition: GT_VolumeUtil.h:62
png_uint_32 i
Definition: png.h:2877
uint64 value_type
Definition: GA_PrimCompat.h:29
GLsizeiptr size
Definition: glcorearb.h:663
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
int64 exint
Definition: SYS_Types.h:115
GLuint GLuint end
Definition: glcorearb.h:474
void resize(GT_Size size, GT_Size capacity=-1)
Definition: GT_DANumeric.h:73
void GTappendCopyTask(UT_TaskList &tasks, DstArrayT &dst, exint dst_start, SrcArrayT &src, exint num_elems)
Used to create an UT_TaskList of array copy tasks.
GLenum GLenum dst
Definition: glcorearb.h:1792
double fpreal
Definition: SYS_Types.h:269
void entries(exint num_items)
Definition: GT_VolumeUtil.h:67
static UT_Task * create(DstArrayT &dst, exint dst_start, const SrcArrayT &src, exint num_elems)
Definition: GT_VolumeUtil.h:82
An array of numeric values (int32, int64, fpreal16, fpreal32, fpreal64)
Definition: GT_DANumeric.h:23
png_infop int chunk
Definition: png.h:2576
#define SYSmin(a, b)
Definition: SYS_Math.h:1366
GLint lod
Definition: glcorearb.h:2764
void runSerial()
Run this task list in serial.
Definition: UT_Task.h:153
GLenum src
Definition: glcorearb.h:1792