HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  fpreal getVoxelDiameter() const { return myField->getVoxelDiameter(); }
113 
114  /// Access the field value given a world space location.
115  /// This does trilinear interpolation.
116  fpreal getValue(const UT_Vector3 &pos) const;
117 
118  /// Determines the gradient at the given location.
119  UT_Vector3 getGradient(const UT_Vector3 &pos) const;
120 
121  /// Advects this field by the other given field.
122  void advect(const SIM_VectorField *vel, float timestep,
123  const SIM_RawField *collision,
124  SIM_FieldAdvection advectmethod,
125  float cfl);
126  void advect(sim_PointVelocity getVelocity, float timestep,
127  float voxelsize,
128  const SIM_RawField *collision = 0,
129  float cfl = 1.0f);
130  /// Advects this by the velocity field, storing our min/max
131  /// interpolants into the min/max fields
132  void advectMinMax(SIM_ScalarField *minfield,
133  SIM_ScalarField *maxfield,
134  const SIM_VectorField *vel, float timestep,
135  const SIM_RawField *collision,
136  SIM_FieldAdvection advectmethod,
137  float cfl);
138 
139  /// Enforces the boundary condition with the given collision mask
140  void enforceBoundary(const SIM_ScalarField *collision=0,
141  const SIM_ScalarField *colvalue=0,
142  const SIM_ScalarField *boundvalue=0);
143 
144  /// Converts an integer index into a worldspace position.
145  bool indexToPos(int x, int y, int z, UT_Vector3 &pos) const;
146 
147  /// Converts a worldspace position into an integer index.
148  bool posToIndex(const UT_Vector3 &pos, int &x, int &y, int &z) const;
149 
150  /// Retrieve raw field.
151  SIM_RawField *getField() const { return myField; };
152 
153  /// Sets the field to the given field, gaining ownership of it.
154  void setField(SIM_RawField *field);
155 
156  /// True if we contain any NANs
157  bool hasNan() const { return getField()->hasNan(); }
158 
159  /// True if we have a constant value. Ignores end conditions
160  /// in determining this. Used as a rough guess that the field
161  /// is unused.
162  bool appearsToBeUnused() const
163  { return getField()->field()->isConstant(0); }
164 
165  /// Steals the field, replacing this copy with an empty field and
166  /// returning the old version.
167  SIM_RawField *stealField();
168 
169  void testForNan() const;
170 
171  /// Signals to the field that it has been altered externally.
173  {
174  testForNan();
176  }
178  { getField()->markGridAsChanged(); }
179 
180  /// Recomputes total number of voxels to be stored
181  /// on our options data for ease of reading
182  void updateTotalVoxels();
183 
184  /// Creates a GDP with us as a Volume Primitive inside it.
185  GU_ConstDetailHandle createSmokeRepresentation(const SIM_RootData &root) const;
186 
187  /// Adds a volume primitive version of our field to the given
188  /// gdp.
189  void addSmokeRepresentation(const SIM_RootData &root, GU_Detail *gdp) const;
190 
191 protected:
192  explicit SIM_ScalarField(const SIM_DataFactory *factory);
193  virtual ~SIM_ScalarField();
194 
195  /// Overrides to properly implement this class as a SIM_Data.
196  virtual void initializeSubclass();
197  /// myField aware copy constructor.
198  virtual void makeEqualSubclass(const SIM_Data *source);
199 
200  /// Saves our attributes, and our internal data if it has been set.
201  virtual void saveSubclass(std::ostream &os) const;
202  /// Loads our attributes and internal data if it was set when we saved.
203  virtual bool loadSubclass(UT_IStream &is);
204 
205  virtual int64 getMemorySizeSubclass() const;
206 
207  /// Override the setDivisions to rebuild our voxel array on demand.
208  virtual void optionChangedSubclass(const char *name);
209 
210 private:
211  static const SIM_DopDescription *getScalarFieldDopDescription();
212 
213  /// Rebuilds our raw field so it matches our current parameters.
214  void rebuildField();
215 
216  SIM_RawField *myField;
217 
218  /// When creating a new scalar field, we do the following:
219  /// 1) Load default values, triggering rebuildField()
220  /// 2) Call initialize()
221  /// 3) Load actual parameters values, triggering rebuildField()
222  /// We need to set the initial value in the third stage. The
223  /// problem is that if there is a non-zero default and the other
224  /// parameters are all default, stage 3 will be skipped. We thus
225  /// track our last default to avoid this case.
226  fpreal myStashedInitialValue;
227 
229 
231  SIM_Data,
232  "ScalarField",
233  getScalarFieldDopDescription());
234 };
235 #endif
236 
#define SIM_NAME_VOXELPLANE
Definition: SIM_Names.h:217
#define SIM_NAME_POSITIONPATH
Definition: SIM_Names.h:168
virtual void makeEqualSubclass(const SIM_Data *source)
bool hasNan() const
True if we contain any NANs.
#define SIM_NAME_VOXELSAMPLE
Definition: SIM_Names.h:216
#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:201
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
#define GETSET_DATA_FUNCS_S(DataName, FuncName)
#define SIM_NAME_DIV
Definition: SIM_Names.h:108
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
virtual int64 getMemorySizeSubclass() const
UT_VoxelBorderType
Definition: UT_VoxelArray.h:69
const UT_Vector3 & getVoxelSize() const
GLint y
Definition: glcorearb.h:102
This class holds a three dimensional scalar field.
bool appearsToBeUnused() const
#define GETSET_DATA_FUNCS_F(DataName, FuncName)
SYS_FORCE_INLINE T & x(void)
Definition: UT_Vector3.h:581
#define GETSET_DATA_FUNCS_V3(DataName, FuncName)
GLsizeiptr size
Definition: glcorearb.h:663
SIM_FieldSample
Definition: SIM_RawField.h:38
UT_Vector3(* sim_PointVelocity)(const UT_Vector3 &, int)
Definition: SIM_RawField.h:95
#define DECLARE_DATAFACTORY(DataClass, SuperClass, Description, DopParms)
Definition: SIM_DataUtils.h:58
SYS_FORCE_INLINE T & z(void)
Definition: UT_Vector3.h:585
#define SIM_NAME_SIZE
Definition: SIM_Names.h:183
UT_Vector3 getOrig() const
long long int64
Definition: SYS_Types.h:100
GLfloat f
Definition: glcorearb.h:1925
SIM_RawField rawfield_type
int64 exint
Definition: SYS_Types.h:109
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:802
virtual void saveSubclass(std::ostream &os) const
#define SIM_NAME_UNIFORMVOXELS
Definition: SIM_Names.h:207
#define GETSET_DATA_FUNCS_I(DataName, FuncName)
#define SIM_NAME_TWOD
Definition: SIM_Names.h:206
GLuint const GLchar * name
Definition: glcorearb.h:785
fpreal getVoxelDiameter() const
This class holds a three dimensional tensor field.
SYS_FORCE_INLINE T & y(void)
Definition: UT_Vector3.h:583
double fpreal
Definition: SYS_Types.h:263
GLint GLint GLsizei GLint border
Definition: glcorearb.h:107
UT_VoxelBorderType getBorder() const
void handleModification(int code=-1)
SIM_FieldAdvection
Definition: SIM_RawField.h:60
#define SIM_API
Definition: SIM_API.h:10
GLint GLenum GLint x
Definition: glcorearb.h:408
This class holds a three dimensional scalar field.
void setBorder(UT_VoxelBorderType border)
SIM_RawField * getField() const
Retrieve raw field.
This class holds a three dimensional vector field.
virtual void initializeSubclass()