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  SIM_RawField *getField() const { return myField; };
154 
155  /// Sets the field to the given field, gaining ownership of it.
156  void setField(SIM_RawField *field);
157 
158  /// True if we contain any NANs
159  bool hasNan() const { return getField()->hasNan(); }
160 
161  /// True if we have a constant value. Ignores end conditions
162  /// in determining this. Used as a rough guess that the field
163  /// is unused.
164  bool appearsToBeUnused() const
165  { return getField()->field()->isConstant(0); }
166 
167  /// Steals the field, replacing this copy with an empty field and
168  /// returning the old version.
169  SIM_RawField *stealField();
170 
171  void testForNan() const;
172 
173  /// Signals to the field that it has been altered externally.
175  {
176  testForNan();
178  }
180  { getField()->markGridAsChanged(); }
181 
182  /// Recomputes total number of voxels to be stored
183  /// on our options data for ease of reading
184  void updateTotalVoxels();
185 
186  /// Creates a GDP with us as a Volume Primitive inside it.
187  GU_ConstDetailHandle createSmokeRepresentation(const SIM_Data &root) const;
188 
189  /// Adds a volume primitive version of our field to the given
190  /// gdp.
191  void addSmokeRepresentation(const SIM_Data &root, GU_Detail *gdp) const;
192 
193 protected:
194  explicit SIM_ScalarField(const SIM_DataFactory *factory);
195  ~SIM_ScalarField() override;
196 
197  /// Overrides to properly implement this class as a SIM_Data.
198  void initializeSubclass() override;
199  /// myField aware copy constructor.
200  void makeEqualSubclass(const SIM_Data *source) override;
201 
202  /// Saves our attributes, and our internal data if it has been set.
203  void saveSubclass(std::ostream &os) const override;
204  /// Loads our attributes and internal data if it was set when we saved.
205  bool loadSubclass(UT_IStream &is) override;
206 
207  int64 getMemorySizeSubclass() const override;
208 
209  /// Override the setDivisions to rebuild our voxel array on demand.
210  void optionChangedSubclass(const char *name) override;
211 
212 private:
213  /// This method can be used to signal to this field not to attempt rebuilding
214  /// the raw field on option changes.
215  void setSkipFieldRebuildOnOptionChanged(bool skip)
216  {
217  mySkipFieldRebuild = skip;
218  }
219  /// Returns whether or not this field is set to skip rebuilding its raw field
220  /// on option changes.
221  bool getSkipFieldRebuildOnOptionChanged() const
222  {
223  return mySkipFieldRebuild;
224  }
225  /// This flag can be used to signal that the raw fields are not to be rebuilt due
226  /// to option changes.
227  bool mySkipFieldRebuild;
228 
229  friend class SkipFieldRebuildScope;
230 
231 public:
232  /// This helper class can be used to prevent the given field from automatically
233  /// attempting to rebuild its raw fields on option changes. The field will skip
234  /// rebuilds as long as this object remains in scope; the rebuild flag is reset
235  /// and rebuildField() is called when the object goes out of scope.
237  {
238  public:
240  {
241  myField = field;
242  myStashedValue = myField->getSkipFieldRebuildOnOptionChanged();
243  myField->setSkipFieldRebuildOnOptionChanged(true);
244  }
245 
247  {
248  myField->setSkipFieldRebuildOnOptionChanged(myStashedValue);
249  // Only invoke possible rebuilding if the old value was set to not
250  // skip.
251  if (!myStashedValue)
252  myField->rebuildField();
253  }
254 
255  private:
256  SIM_ScalarField* myField;
257  bool myStashedValue;
258  };
259 
260 private:
261  static const SIM_DopDescription *getScalarFieldDopDescription();
262 
263  /// Rebuilds our raw field so it matches our current parameters.
264  void rebuildField();
265 
266  SIM_RawField *myField;
267 
268  /// When creating a new scalar field, we do the following:
269  /// 1) Load default values, triggering rebuildField()
270  /// 2) Call initialize()
271  /// 3) Load actual parameters values, triggering rebuildField()
272  /// We need to set the initial value in the third stage. The
273  /// problem is that if there is a non-zero default and the other
274  /// parameters are all default, stage 3 will be skipped. We thus
275  /// track our last default to avoid this case.
276  fpreal myStashedInitialValue;
277 
279 
281  SIM_Data,
282  "ScalarField",
283  getScalarFieldDopDescription());
284 };
285 #endif
286 
#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:45
GA_API const UT_StringHolder div
virtual bool loadSubclass(UT_IStream &is)
#define SIM_NAME_TOLERANCE
Definition: SIM_Names.h:202
GLsizeiptr size
Definition: glew.h:1681
GLuint const GLchar * name
Definition: glew.h:1814
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: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
const UT_Vector3 & getVoxelSize() const
This class holds a three dimensional scalar field.
bool appearsToBeUnused() const
#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
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_Vector3 getOrig() const
SYS_FORCE_INLINE T & y()
Definition: UT_Vector3.h:513
SIM_RawField rawfield_type
GLclampf f
Definition: glew.h:3499
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
SkipFieldRebuildScope(SIM_ScalarField *field)
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
#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
fpreal getVoxelDiameter() const
This class holds a three dimensional tensor 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.
void setVoxelSize(const UT_Vector3 &voxelsize)
SYS_FORCE_INLINE T & x()
Definition: UT_Vector3.h:511
void setBorder(UT_VoxelBorderType border)
SIM_RawField * getField() const
Retrieve raw field.
This class holds a three dimensional vector field.
virtual void initializeSubclass()