HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIM_IndexField.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: SIM_IndexField.h ( SIM Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __SIM_IndexField__
12 #define __SIM_IndexField__
13 
14 #include "SIM_API.h"
15 
16 #include <UT/UT_VoxelArray.h>
17 
18 #include "SIM_Names.h"
19 #include "SIM_OptionsUser.h"
20 #include "SIM_DataUtils.h"
21 #include "SIM_RawField.h"
22 #include "SIM_RawIndexField.h"
23 
24 class UT_IStream;
25 class SIM_Geometry;
26 class SIM_ScalarField;
27 class SIM_VectorField;
28 class SIM_MatrixField;
29 
30 /// This class holds a three dimensional scalar field.
32  public SIM_OptionsUser
33 {
34 public:
36 
37  /// Accesses the relative path to the position data associated with
38  /// this geometry.
40 
41  /// Control the number of divisions.
45  GETSET_DATA_FUNCS_V3(SIM_NAME_DIV, RawDivisions);
46  GETSET_DATA_FUNCS_I("uniformdiv", RawUniformDivisions);
47  GETSET_DATA_FUNCS_F("divsize", RawDivisionSize);
50 
51  GETSET_DATA_FUNCS_V3("slicediv", SliceDivisions);
52  GETSET_DATA_FUNCS_V3("sliceoverlapneg", SliceOverlapNeg);
53  GETSET_DATA_FUNCS_V3("sliceoverlappos", SliceOverlapPos);
54  GETSET_DATA_FUNCS_I("slice", Slice)
55  exint getNumSlices() const { UT_Vector3D nslice = getSliceDivisions(); return exint(nslice.x() * nslice.y() * nslice.z()); }
56 
57  GETSET_DATA_FUNCS_I("totalvoxels", TotalVoxels);
58  GETSET_DATA_FUNCS_V3("totalvoxelres", TotalVoxelRes);
59 
61  GETSET_DATA_FUNCS_I("initialvalue", InitialValue);
62  GETSET_DATA_FUNCS_I("border", RawBorder);
63  UT_VoxelBorderType getBorder() const { return (UT_VoxelBorderType) getRawBorder(); }
64  void setBorder(UT_VoxelBorderType border) { setRawBorder(border); }
65 
66  /// Controls the dimensions of where the field is properly defined
67  /// in the field space.
68  void getBBox(UT_BoundingBox &bbox) const;
69 
71  {
72  return getCenter() - getSize()/2;
73  }
74 
75  /// Calculate the size and divisions according to options
76  /// such as 2d or equal sized voxels.
77  UT_Vector3 getDivisions() const;
78  UT_Vector3 getSize() const;
79  UT_Vector3 getCenter() const;
80 
81  /// Adjusts the size/divisions of this field, overriding
82  /// and twod or uniform voxel settings.
83  void setDivisions(const UT_Vector3 &div);
84  void setSize(const UT_Vector3 &div);
85  void setCenter(const UT_Vector3 &div);
86 
87  /// Resizes our field keeping our field data.
88  /// The final size will be an integer number of voxels matching
89  /// our current voxel size. The final center will be an integer
90  /// number of voxel offset from our current center. This allows
91  /// us to do a perfect copy of the data.
92  void resizeKeepData(const UT_Vector3 &size, const UT_Vector3 &center, bool keepdata, const char *address = 0, int port = -1);
93 
94  /// Match this field to the given reference field. We will
95  /// end up with the same size/divisions/twod/uniform,
96  /// but not the same sampling pattern
97  void matchField(const SIM_ScalarField *field);
98  void matchField(const SIM_VectorField *field);
99  void matchField(const SIM_MatrixField *field);
100  void matchField(const SIM_IndexField *field, bool matchsamples = false);
101 
102  SIM_FieldSample getVoxelSample() const;
103 
104  const UT_Vector3 &getVoxelSize() const;
105  fpreal getVoxelDiameter() const;
106  void setVoxelSize(const UT_Vector3 &voxelsize)
107  { myField->setVoxelSize(voxelsize); }
108 
109  /// Access the field value given a world space location.
110  /// This does closest point matching.
111  exint getValue(const UT_Vector3 &pos) const;
112 
113  /// Converts an integer index into a worldspace position.
114  bool indexToPos(int x, int y, int z, UT_Vector3 &pos) const;
115  bool indexToPos(exint x, exint y, exint z, UT_Vector3D &pos) const;
116 
117  /// Converts a worldspace position into an integer index.
118  bool posToIndex(const UT_Vector3 &pos, int &x, int &y, int &z) const;
119 
120  /// Retrieve raw field.
121  const SIM_RawIndexField *getField() const { return myField; };
122  SIM_RawIndexField *getField() { return myField; };
123 
124  /// Sets the field to the given field, gaining ownership of it.
125  void setField(SIM_RawIndexField *field);
126 
127  /// Can't have NANs in integer fields.
128  bool testForNan() const { return false; }
129 
130  /// True if we have a constant value. Ignores end conditions
131  /// in determining this. Used as a rough guess that the field
132  /// is unused.
133  bool appearsToBeUnused() const
134  { return getField()->field()->isConstant(0); }
135 
136  /// Steals the field, replacing this copy with an empty field and
137  /// returning the old version.
138  SIM_RawIndexField *stealField();
139 
140  /// Signals to the field that it has been altered externally.
142  {
144  }
145 
146  /// Recomputes total number of voxels to be stored
147  /// on our options data for ease of reading
148  void updateTotalVoxels();
149 
150  /// Creates a GDP with us as a Volume Primitive inside it.
151  GU_ConstDetailHandle createSmokeRepresentation(const SIM_RootData &root) const;
152 
153  /// Adds a volume primitive version of our field to the given
154  /// gdp.
155  void addSmokeRepresentation(const SIM_RootData &root, GU_Detail *gdp) const;
156 protected:
157  explicit SIM_IndexField(const SIM_DataFactory *factory);
158  ~SIM_IndexField() override;
159 
160  /// Overrides to properly implement this class as a SIM_Data.
161  void initializeSubclass() override;
162  /// myField aware copy constructor.
163  void makeEqualSubclass(const SIM_Data *source) override;
164 
165  /// Saves our attributes, and our internal data if it has been set.
166  void saveSubclass(std::ostream &os) const override;
167  /// Loads our attributes and internal data if it was set when we saved.
168  bool loadSubclass(UT_IStream &is) override;
169 
170  exint getMemorySizeSubclass() const override;
171 
172  /// Override the setDivisions to rebuild our voxel array on demand.
173  void optionChangedSubclass(const char *name) override;
174 
175 private:
176  /// This method can be used to signal to this field not to attempt rebuilding
177  /// the raw field on option changes.
178  void setSkipFieldRebuildOnOptionChanged(bool skip)
179  {
180  mySkipFieldRebuild = skip;
181  }
182  /// Returns whether or not this field is set to skip rebuilding its raw field
183  /// on option changes.
184  bool getSkipFieldRebuildOnOptionChanged() const
185  {
186  return mySkipFieldRebuild;
187  }
188  /// This flag can be used to signal that the raw fields are not to be rebuilt due
189  /// to option changes.
190  bool mySkipFieldRebuild;
191 
192  friend class SkipFieldRebuildScope;
193 
194 public:
195  /// This helper class can be used to prevent the given field from automatically
196  /// attempting to rebuild its raw fields on option changes. The field will skip
197  /// rebuilds as long as this object remains in scope; the rebuild flag is reset
198  /// and rebuildField() is called when the object goes out of scope.
200  {
201  public:
203  {
204  myField = field;
205  myStashedValue = myField->getSkipFieldRebuildOnOptionChanged();
206  myField->setSkipFieldRebuildOnOptionChanged(true);
207  }
208 
210  {
211  myField->setSkipFieldRebuildOnOptionChanged(myStashedValue);
212  // Only invoke possible rebuilding if the old value was set to not
213  // skip.
214  if (!myStashedValue)
215  myField->rebuildField();
216  }
217 
218  private:
219  SIM_IndexField* myField;
220  bool myStashedValue;
221  };
222 
223 private:
224  static const SIM_DopDescription *getIndexFieldDopDescription();
225 
226  /// Rebuilds our raw field so it matches our current parameters.
227  void rebuildField();
228 
229  SIM_RawIndexField *myField;
230 
231  /// When creating a new scalar field, we do the following:
232  /// 1) Load default values, triggering rebuildField()
233  /// 2) Call initialize()
234  /// 3) Load actual parameters values, triggering rebuildField()
235  /// We need to set the initial value in the third stage. The
236  /// problem is that if there is a non-zero default and the other
237  /// parameters are all default, stage 3 will be skipped. We thus
238  /// track our last default to avoid this case.
239  exint myStashedInitialValue;
240 
242 
244  SIM_Data,
245  "IndexField",
246  getIndexFieldDopDescription());
247 };
248 #endif
249 
#define SIM_NAME_VOXELPLANE
Definition: SIM_Names.h:218
#define SIM_NAME_POSITIONPATH
Definition: SIM_Names.h:169
virtual void makeEqualSubclass(const SIM_Data *source)
#define SIM_NAME_VOXELSAMPLE
Definition: SIM_Names.h:217
#define DECLARE_STANDARD_GETCASTTOTYPE()
Definition: SIM_DataUtils.h:50
GA_API const UT_StringHolder div
virtual bool loadSubclass(UT_IStream &is)
GLint GLint GLsizei GLint border
Definition: glcorearb.h:108
virtual void optionChangedSubclass(const char *name)
SIM_RawIndexField rawfield_type
#define GETSET_DATA_FUNCS_B(DataName, FuncName)
#define SIM_NAME_CENTER
Definition: SIM_Names.h:82
void skip(T &in, int n)
Definition: ImfXdr.h:711
#define GETSET_DATA_FUNCS_S(DataName, FuncName)
#define SIM_NAME_DIV
Definition: SIM_Names.h:109
virtual int64 getMemorySizeSubclass() const
constexpr SYS_FORCE_INLINE T & z() noexcept
Definition: UT_Vector3.h:657
int64 exint
Definition: SYS_Types.h:125
UT_VoxelBorderType
Definition: UT_VoxelArray.h:67
This class holds a three dimensional scalar field.
GLuint const GLchar * name
Definition: glcorearb.h:786
#define GETSET_DATA_FUNCS_F(DataName, FuncName)
#define GETSET_DATA_FUNCS_V3(DataName, FuncName)
SIM_FieldSample
Definition: SIM_RawField.h:38
GLint GLenum GLint x
Definition: glcorearb.h:409
bool testForNan() const
Can't have NANs in integer fields.
GLsizeiptr size
Definition: glcorearb.h:664
#define DECLARE_DATAFACTORY(DataClass, SuperClass, Description, DopParms)
Definition: SIM_DataUtils.h:63
#define SIM_NAME_SIZE
Definition: SIM_Names.h:184
UT_VoxelBorderType getBorder() const
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
virtual void saveSubclass(std::ostream &os) const
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
void setVoxelSize(const UT_Vector3 &voxelsize)
#define SIM_NAME_UNIFORMVOXELS
Definition: SIM_Names.h:208
#define GETSET_DATA_FUNCS_I(DataName, FuncName)
#define SIM_NAME_TWOD
Definition: SIM_Names.h:207
GLuint GLuint64EXT address
Definition: glew.h:14913
This class holds a three dimensional tensor field.
SIM_RawIndexField * getField()
UT_Vector3 getOrig() const
fpreal64 fpreal
Definition: SYS_Types.h:277
void handleModification(int code=-1)
const SIM_RawIndexField * getField() const
Retrieve raw field.
#define SIM_API
Definition: SIM_API.h:10
This class holds a three dimensional scalar field.
void setBorder(UT_VoxelBorderType border)
SkipFieldRebuildScope(SIM_IndexField *field)
void pubHandleModification()
Signals to the field that it has been altered externally.
constexpr SYS_FORCE_INLINE T & y() noexcept
Definition: UT_Vector3.h:655
bool appearsToBeUnused() const
This class holds a three dimensional vector field.
GLint y
Definition: glcorearb.h:103
constexpr SYS_FORCE_INLINE T & x() noexcept
Definition: UT_Vector3.h:653
virtual void initializeSubclass()