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  SIM_RawIndexField *getField() const { return myField; };
122 
123  /// Sets the field to the given field, gaining ownership of it.
124  void setField(SIM_RawIndexField *field);
125 
126  /// Can't have NANs in integer fields.
127  bool testForNan() const { return false; }
128 
129  /// True if we have a constant value. Ignores end conditions
130  /// in determining this. Used as a rough guess that the field
131  /// is unused.
132  bool appearsToBeUnused() const
133  { return getField()->field()->isConstant(0); }
134 
135  /// Steals the field, replacing this copy with an empty field and
136  /// returning the old version.
137  SIM_RawIndexField *stealField();
138 
139  /// Signals to the field that it has been altered externally.
141  {
143  }
144 
145  /// Recomputes total number of voxels to be stored
146  /// on our options data for ease of reading
147  void updateTotalVoxels();
148 
149  /// Creates a GDP with us as a Volume Primitive inside it.
150  GU_ConstDetailHandle createSmokeRepresentation(const SIM_RootData &root) const;
151 
152  /// Adds a volume primitive version of our field to the given
153  /// gdp.
154  void addSmokeRepresentation(const SIM_RootData &root, GU_Detail *gdp) const;
155 protected:
156  explicit SIM_IndexField(const SIM_DataFactory *factory);
157  ~SIM_IndexField() override;
158 
159  /// Overrides to properly implement this class as a SIM_Data.
160  void initializeSubclass() override;
161  /// myField aware copy constructor.
162  void makeEqualSubclass(const SIM_Data *source) override;
163 
164  /// Saves our attributes, and our internal data if it has been set.
165  void saveSubclass(std::ostream &os) const override;
166  /// Loads our attributes and internal data if it was set when we saved.
167  bool loadSubclass(UT_IStream &is) override;
168 
169  exint getMemorySizeSubclass() const override;
170 
171  /// Override the setDivisions to rebuild our voxel array on demand.
172  void optionChangedSubclass(const char *name) override;
173 
174 private:
175  /// This method can be used to signal to this field not to attempt rebuilding
176  /// the raw field on option changes.
177  void setSkipFieldRebuildOnOptionChanged(bool skip)
178  {
179  mySkipFieldRebuild = skip;
180  }
181  /// Returns whether or not this field is set to skip rebuilding its raw field
182  /// on option changes.
183  bool getSkipFieldRebuildOnOptionChanged() const
184  {
185  return mySkipFieldRebuild;
186  }
187  /// This flag can be used to signal that the raw fields are not to be rebuilt due
188  /// to option changes.
189  bool mySkipFieldRebuild;
190 
191  friend class SkipFieldRebuildScope;
192 
193 public:
194  /// This helper class can be used to prevent the given field from automatically
195  /// attempting to rebuild its raw fields on option changes. The field will skip
196  /// rebuilds as long as this object remains in scope; the rebuild flag is reset
197  /// and rebuildField() is called when the object goes out of scope.
199  {
200  public:
202  {
203  myField = field;
204  myStashedValue = myField->getSkipFieldRebuildOnOptionChanged();
205  myField->setSkipFieldRebuildOnOptionChanged(true);
206  }
207 
209  {
210  myField->setSkipFieldRebuildOnOptionChanged(myStashedValue);
211  // Only invoke possible rebuilding if the old value was set to not
212  // skip.
213  if (!myStashedValue)
214  myField->rebuildField();
215  }
216 
217  private:
218  SIM_IndexField* myField;
219  bool myStashedValue;
220  };
221 
222 private:
223  static const SIM_DopDescription *getIndexFieldDopDescription();
224 
225  /// Rebuilds our raw field so it matches our current parameters.
226  void rebuildField();
227 
228  SIM_RawIndexField *myField;
229 
230  /// When creating a new scalar field, we do the following:
231  /// 1) Load default values, triggering rebuildField()
232  /// 2) Call initialize()
233  /// 3) Load actual parameters values, triggering rebuildField()
234  /// We need to set the initial value in the third stage. The
235  /// problem is that if there is a non-zero default and the other
236  /// parameters are all default, stage 3 will be skipped. We thus
237  /// track our last default to avoid this case.
238  exint myStashedInitialValue;
239 
241 
243  SIM_Data,
244  "IndexField",
245  getIndexFieldDopDescription());
246 };
247 #endif
248 
#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:45
GA_API const UT_StringHolder div
virtual bool loadSubclass(UT_IStream &is)
GLsizeiptr size
Definition: glew.h:1681
GLuint const GLchar * name
Definition: glew.h:1814
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:885
#define GETSET_DATA_FUNCS_S(DataName, FuncName)
#define SIM_NAME_DIV
Definition: SIM_Names.h:109
virtual int64 getMemorySizeSubclass() const
int64 exint
Definition: SYS_Types.h:125
UT_VoxelBorderType
Definition: UT_VoxelArray.h:67
GLsizei GLsizei GLchar * source
Definition: glew.h:1832
This class holds a three dimensional scalar field.
#define GETSET_DATA_FUNCS_F(DataName, FuncName)
#define GETSET_DATA_FUNCS_V3(DataName, FuncName)
SIM_FieldSample
Definition: SIM_RawField.h:38
GLdouble GLdouble z
Definition: glew.h:1559
bool testForNan() const
Can't have NANs in integer fields.
GLuint GLuint64EXT address
Definition: glew.h:14600
#define DECLARE_DATAFACTORY(DataClass, SuperClass, Description, DopParms)
Definition: SIM_DataUtils.h:58
#define SIM_NAME_SIZE
Definition: SIM_Names.h:184
UT_VoxelBorderType getBorder() const
SYS_FORCE_INLINE T & y()
Definition: UT_Vector3.h:513
SIM_RawIndexField * getField() const
Retrieve raw field.
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
GLint GLint GLsizei GLsizei GLsizei GLint border
Definition: glew.h:1254
SYS_FORCE_INLINE T & z()
Definition: UT_Vector3.h:515
virtual void saveSubclass(std::ostream &os) const
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
This class holds a three dimensional tensor field.
UT_Vector3 getOrig() const
fpreal64 fpreal
Definition: SYS_Types.h:277
void handleModification(int code=-1)
#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)
SYS_FORCE_INLINE T & x()
Definition: UT_Vector3.h:511
void pubHandleModification()
Signals to the field that it has been altered externally.
bool appearsToBeUnused() const
This class holds a three dimensional vector field.
virtual void initializeSubclass()