HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIM_ScalarField.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_ScalarField.h ( SIM Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __SIM_ScalarField__
12 #define __SIM_ScalarField__
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 
23 class UT_IStream;
24 class SIM_Geometry;
25 class SIM_VectorField;
26 class SIM_MatrixField;
27 class SIM_IndexField;
28 
29 /// This class holds a three dimensional scalar field.
31  public SIM_OptionsUser
32 {
33 public:
35 
36  /// Accesses the relative path to the position data associated with
37  /// this geometry.
39 
40  /// Control the number of divisions.
44  GETSET_DATA_FUNCS_V3(SIM_NAME_DIV, RawDivisions);
45  GETSET_DATA_FUNCS_I("uniformdiv", RawUniformDivisions);
46  GETSET_DATA_FUNCS_F("divsize", RawDivisionSize);
49 
50  GETSET_DATA_FUNCS_V3("slicediv", SliceDivisions);
51  GETSET_DATA_FUNCS_V3("sliceoverlapneg", SliceOverlapNeg);
52  GETSET_DATA_FUNCS_V3("sliceoverlappos", SliceOverlapPos);
53  GETSET_DATA_FUNCS_I("slice", Slice)
54 
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_F("initialvalue", InitialValue);
63  GETSET_DATA_FUNCS_B("usefp16", UseFP16);
64  GETSET_DATA_FUNCS_I("border", RawBorder);
65  UT_VoxelBorderType getBorder() const { return (UT_VoxelBorderType) getRawBorder(); }
66  void setBorder(UT_VoxelBorderType border) { setRawBorder(border); }
67 
68  /// Controls the dimensions of where the field is properly defined
69  /// in the field space.
70  void getBBox(UT_BoundingBox &bbox) const;
71 
73  {
74  return getCenter() - getSize()/2;
75  }
76 
77  /// Calculate the size and divisions according to options
78  /// such as 2d or equal sized voxels.
79  UT_Vector3 getDivisions() const;
80  UT_Vector3 getSize() const;
81  UT_Vector3 getCenter() const;
82 
83  /// Adjusts the size/divisions of this field, overriding
84  /// and twod or uniform voxel settings.
85  void setDivisions(const UT_Vector3 &div);
86  void setSize(const UT_Vector3 &size);
87  void setCenter(const UT_Vector3 &center);
88 
89  /// Resizes our field keeping our field data.
90  /// The final size will be an integer number of voxels matching
91  /// our current voxel size. The final center will be an integer
92  /// number of voxel offset from our current center. This allows
93  /// us to do a perfect copy of the data.
94  void resizeKeepData(const UT_Vector3 &size, const UT_Vector3 &center, bool keepdata, const char *address = 0, int port = -1);
95 
96  /// Match this field to the given reference field. We will
97  /// end up with the same size/divisions/twod/uniform,
98  /// but not the same sampling pattern
99  /// This will destroy our field data.
100  void matchField(const SIM_ScalarField *field, bool matchsample = false);
101  void matchField(const SIM_VectorField *field);
102  void matchField(const SIM_MatrixField *field);
103  void matchField(const SIM_IndexField *field);
104 
105  void matchVolume(const GEO_PrimVolume *vol, const UT_DMatrix4 &xform);
106  void matchVDB(const GEO_PrimVDB *vdb, const UT_DMatrix4 &xform);
107 
108  SIM_FieldSample getVoxelSample() const;
109  void setVoxelSample(SIM_FieldSample sample);
110 
111  const UT_Vector3 &getVoxelSize() const { return myField->getVoxelSize(); }
112  void setVoxelSize(const UT_Vector3 &voxelsize)
113  { myField->setVoxelSize(voxelsize); }
114  fpreal getVoxelDiameter() const { return myField->getVoxelDiameter(); }
115 
116  /// Access the field value given a world space location.
117  /// This does trilinear interpolation.
118  fpreal getValue(const UT_Vector3 &pos) const;
119 
120  /// Determines the gradient at the given location.
121  UT_Vector3 getGradient(const UT_Vector3 &pos) const;
122 
123  /// Advects this field by the other given field.
124  void advect(const SIM_VectorField *vel, float timestep,
125  const SIM_RawField *collision,
126  SIM_FieldAdvection advectmethod,
127  float cfl);
128  void advect(sim_PointVelocity getVelocity, float timestep,
129  float voxelsize,
130  const SIM_RawField *collision = 0,
131  float cfl = 1.0f);
132  /// Advects this by the velocity field, storing our min/max
133  /// interpolants into the min/max fields
134  void advectMinMax(SIM_ScalarField *minfield,
135  SIM_ScalarField *maxfield,
136  const SIM_VectorField *vel, float timestep,
137  const SIM_RawField *collision,
138  SIM_FieldAdvection advectmethod,
139  float cfl);
140 
141  /// Enforces the boundary condition with the given collision mask
142  void enforceBoundary(const SIM_ScalarField *collision=0,
143  const SIM_ScalarField *colvalue=0,
144  const SIM_ScalarField *boundvalue=0);
145 
146  /// Converts an integer index into a worldspace position.
147  bool indexToPos(int x, int y, int z, UT_Vector3 &pos) const;
148 
149  /// Converts a worldspace position into an integer index.
150  bool posToIndex(const UT_Vector3 &pos, int &x, int &y, int &z) const;
151 
152  /// Retrieve raw field.
153  const SIM_RawField *getField() const { return myField; };
154  SIM_RawField *getField() { return myField; };
155 
156  /// Sets the field to the given field, gaining ownership of it.
157  void setField(SIM_RawField *field);
158 
159  /// True if we contain any NANs
160  bool hasNan() const { return getField()->hasNan(); }
161 
162  /// True if we have a constant value. Ignores end conditions
163  /// in determining this. Used as a rough guess that the field
164  /// is unused.
165  bool appearsToBeUnused() const
166  { return getField()->field()->isConstant(0); }
167 
168  /// Steals the field, replacing this copy with an empty field and
169  /// returning the old version.
170  SIM_RawField *stealField();
171 
172  void testForNan() const;
173 
174  /// Signals to the field that it has been altered externally.
176  {
177  testForNan();
179  }
181  { getField()->markGridAsChanged(); }
182 
183  /// Recomputes total number of voxels to be stored
184  /// on our options data for ease of reading
185  void updateTotalVoxels();
186 
187  /// Creates a GDP with us as a Volume Primitive inside it.
188  GU_ConstDetailHandle createSmokeRepresentation(const SIM_Data &root) const;
189 
190  /// Adds a volume primitive version of our field to the given
191  /// gdp.
192  void addSmokeRepresentation(const SIM_Data &root, GU_Detail *gdp) const;
193 
194 protected:
195  explicit SIM_ScalarField(const SIM_DataFactory *factory);
196  ~SIM_ScalarField() override;
197 
198  /// Overrides to properly implement this class as a SIM_Data.
199  void initializeSubclass() override;
200  /// myField aware copy constructor.
201  void makeEqualSubclass(const SIM_Data *source) override;
202 
203  /// Saves our attributes, and our internal data if it has been set.
204  void saveSubclass(std::ostream &os) const override;
205  /// Loads our attributes and internal data if it was set when we saved.
206  bool loadSubclass(UT_IStream &is) override;
207 
208  int64 getMemorySizeSubclass() const override;
209 
210  /// Override the setDivisions to rebuild our voxel array on demand.
211  void optionChangedSubclass(const char *name) override;
212 
213 private:
214  /// This method can be used to signal to this field not to attempt rebuilding
215  /// the raw field on option changes.
216  void setSkipFieldRebuildOnOptionChanged(bool skip)
217  {
218  mySkipFieldRebuild = skip;
219  }
220  /// Returns whether or not this field is set to skip rebuilding its raw field
221  /// on option changes.
222  bool getSkipFieldRebuildOnOptionChanged() const
223  {
224  return mySkipFieldRebuild;
225  }
226  /// This flag can be used to signal that the raw fields are not to be rebuilt due
227  /// to option changes.
228  bool mySkipFieldRebuild;
229 
230  friend class SkipFieldRebuildScope;
231 
232 public:
233  /// This helper class can be used to prevent the given field from automatically
234  /// attempting to rebuild its raw fields on option changes. The field will skip
235  /// rebuilds as long as this object remains in scope; the rebuild flag is reset
236  /// and rebuildField() is called when the object goes out of scope.
238  {
239  public:
241  {
242  myField = field;
243  myStashedValue = myField->getSkipFieldRebuildOnOptionChanged();
244  myField->setSkipFieldRebuildOnOptionChanged(true);
245  }
246 
248  {
249  myField->setSkipFieldRebuildOnOptionChanged(myStashedValue);
250  // Only invoke possible rebuilding if the old value was set to not
251  // skip.
252  if (!myStashedValue)
253  myField->rebuildField();
254  }
255 
256  private:
257  SIM_ScalarField* myField;
258  bool myStashedValue;
259  };
260 
261 private:
262  static const SIM_DopDescription *getScalarFieldDopDescription();
263 
264  /// Rebuilds our raw field so it matches our current parameters.
265  void rebuildField();
266 
267  SIM_RawField *myField;
268 
269  /// When creating a new scalar field, we do the following:
270  /// 1) Load default values, triggering rebuildField()
271  /// 2) Call initialize()
272  /// 3) Load actual parameters values, triggering rebuildField()
273  /// We need to set the initial value in the third stage. The
274  /// problem is that if there is a non-zero default and the other
275  /// parameters are all default, stage 3 will be skipped. We thus
276  /// track our last default to avoid this case.
277  fpreal myStashedInitialValue;
278 
280 
282  SIM_Data,
283  "ScalarField",
284  getScalarFieldDopDescription());
285 };
286 #endif
287 
#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)
bool hasNan() const
True if we contain any NANs.
#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)
#define SIM_NAME_TOLERANCE
Definition: SIM_Names.h:202
GLint GLint GLsizei GLint border
Definition: glcorearb.h:108
void pubHandleModification()
Signals to the field that it has been altered externally.
virtual void optionChangedSubclass(const char *name)
#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
const UT_Vector3 & getVoxelSize() const
SIM_RawField * getField()
This class holds a three dimensional scalar field.
GLuint const GLchar * name
Definition: glcorearb.h:786
bool appearsToBeUnused() const
#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
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_Vector3 getOrig() const
SIM_RawField rawfield_type
SkipFieldRebuildScope(SIM_ScalarField *field)
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
virtual void saveSubclass(std::ostream &os) const
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
#define SIM_NAME_UNIFORMVOXELS
Definition: SIM_Names.h:208
#define GETSET_DATA_FUNCS_I(DataName, FuncName)
long long int64
Definition: SYS_Types.h:116
#define SIM_NAME_TWOD
Definition: SIM_Names.h:207
GLuint GLuint64EXT address
Definition: glew.h:14913
fpreal getVoxelDiameter() const
This class holds a three dimensional tensor field.
const SIM_RawField * getField() const
Retrieve raw field.
fpreal64 fpreal
Definition: SYS_Types.h:277
UT_VoxelBorderType getBorder() const
void handleModification(int code=-1)
SIM_FieldAdvection
Definition: SIM_RawField.h:60
#define SIM_API
Definition: SIM_API.h:10
This class holds a three dimensional scalar field.
GLfloat f
Definition: glcorearb.h:1926
void setVoxelSize(const UT_Vector3 &voxelsize)
void setBorder(UT_VoxelBorderType border)
constexpr SYS_FORCE_INLINE T & y() noexcept
Definition: UT_Vector3.h:655
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()