HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SIM_MatrixField.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_MatrixField.h ( SIM Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __SIM_MatrixField__
12 #define __SIM_MatrixField__
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_ScalarField;
26 class SIM_VectorField;
27 class SIM_IndexField;
28 
29 /// This class holds a three dimensional tensor 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  exint getNumSlices() const { UT_Vector3D nslice = getSliceDivisions(); return exint(nslice.x() * nslice.y() * nslice.z()); }
55 
56  GETSET_DATA_FUNCS_I("totalvoxels", TotalVoxels);
57  GETSET_DATA_FUNCS_V3("totalvoxelres", TotalVoxelRes);
58 
60  GETSET_DATA_FUNCS_B("usefp16", UseFP16);
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  GETSET_DATA_FUNCS_M3("initialvalue", InitialValue);
66 
67  /// Controls the dimensions of where the field is properly defined
68  /// in the field space.
69  void getBBox(UT_BoundingBox &bbox) const;
70 
72  {
73  return getCenter() - getSize()/2;
74  }
75 
76  /// Calculate the size and divisions according to options
77  /// such as 2d or equal sized voxels.
78  UT_Vector3 getDivisions() const;
79  UT_Vector3 getSize() const;
80  UT_Vector3 getCenter() const;
81 
82  /// Adjusts the size/divisions of this field, overriding
83  /// and twod or uniform voxel settings.
84  void setDivisions(const UT_Vector3 &div);
85  void setSize(const UT_Vector3 &div);
86  void setCenter(const UT_Vector3 &div);
87 
88  /// Resizes our field keeping our field data.
89  /// The final size will be an integer number of voxels matching
90  /// our current voxel size. The final center will be an integer
91  /// number of voxel offset from our current center. This allows
92  /// us to do a perfect copy of the data.
93  void resizeKeepData(const UT_Vector3 &size, const UT_Vector3 &center, bool keepdata, const char *address = 0, int port = -1);
94 
95  /// Match this field to the given reference field. We will
96  /// end up with the same size/divisions/twod/uniform,
97  /// but not the same sampling pattern
98  void matchField(const SIM_ScalarField *field);
99  void matchField(const SIM_VectorField *field);
100  void matchField(const SIM_MatrixField *field, bool matchsamples=false);
101  void matchField(const SIM_IndexField *field);
102 
103  bool isAligned(const SIM_ScalarField *field) const;
104  bool isAligned(const SIM_VectorField *field) const;
105  bool isAligned(const SIM_RawField *field) const;
106  /// True if we are component wise aligned, the subfields may still
107  /// not be aligned with respect to each other.
108  bool isAligned(const SIM_MatrixField *field) const;
109 
110  SIM_FieldSample getVoxelSample(int i, int j) const;
111  const UT_Vector3 &getVoxelSize(int i, int j) const;
112  fpreal getVoxelDiameter(int i, int j) const;
113  // Independent of sampling pattern, so constant between fields.
114  UT_Vector3 getVoxelSize() const { return getVoxelSize(0, 0); }
115 
116  /// Access the field value given a world space location.
117  /// This does trilinear interpolation.
118  UT_Matrix3 getValue(const UT_Vector3 &pos) const;
119 
120  /// Gets the velocity at the given *voxel* location, interpolating
121  /// if we have corner or face velocities.
122  UT_Matrix3 getCellValue(int x, int y, int z) const;
123 
124  /// Adds a velocity to the given *voxel*. If this is face,
125  /// it is divided in two and spread on each of 6 faces. If it is
126  /// corner, it is divided by 8 and spread along each of 8 corners.
127  void addToCell(int x, int y, int z, const UT_Matrix3 &dv);
128 
129  /// Advects this field by the other given field.
130  void advect(const SIM_VectorField *vel, float timestep,
131  const SIM_RawField *collision,
132  SIM_FieldAdvection advectmethod,
133  float cfl);
134  void advect(sim_PointVelocity getVelocity, float timestep,
135  float voxelsize,
136  const SIM_RawField *collision = 0,
137  float cfl = 1.0f);
138 
139  /// Advects this by the velocity field, storing our min/max
140  /// interpolants into the min/max fields
141  void advectMinMax(SIM_MatrixField *minfield,
142  SIM_MatrixField *maxfield,
143  const SIM_VectorField *vel, float timestep,
144  const SIM_RawField *collision,
145  SIM_FieldAdvection advectmethod,
146  float cfl);
147 
148  /// Enforces boundary conditions on the array.
149  void enforceBoundary(const SIM_ScalarField *collision=0,
150  const SIM_MatrixField *cvel = 0,
151  const SIM_MatrixField *bvel = 0);
152 
153  /// Converts an integer index into a worldspace position.
154  bool indexToPos(int i, int j, int x, int y, int z, UT_Vector3 &pos) const;
155 
156  /// Converts a worldspace position into an integer index.
157  bool posToIndex(int i, int j, const UT_Vector3 &pos, int &x, int &y, int &z) const;
158 
159  /// Retrieve raw field.
160  SIM_RawField *getField(int i, int j) const { return myFields[i][j]; }
161 
162  /// Sets the field to the given field, gaining ownership of it.
163  /// The new field must already match the field it will replace.
164  void setField(int i, int j, SIM_RawField *field);
165 
166  /// True if we contain any NANs
167  bool hasNan() const
168  {
169  int i, j;
170 
171  for (i = 0; i < 3; i++)
172  for (j = 0; j < 3; j++)
173  if (myFields[i][j]->hasNan())
174  return true;
175  return false;
176  }
177 
178  /// True if we have a constant value. Ignores end conditions
179  /// in determining this. Used as a rough guess that the field
180  /// is unused.
181  bool appearsToBeUnused() const
182  {
183  for (int i = 0; i < 3; i++)
184  for (int j = 0; j < 3; j++)
185  if (!getField(i, j)->field()->isConstant(0)) return false;
186  return true;
187  }
188 
189  /// Steals the field, replacing this copy with an empty field and
190  /// Steals the field, replacing this copy with an empty field and
191  /// returning the old version.
192  SIM_RawField *stealField(int i, int j);
193 
194  void testForNan() const;
196  {
197  testForNan();
199  }
201  { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) getField(i, j)->markGridAsChanged(); }
202 
203  /// Recomputes total number of voxels to be stored
204  /// on our options data for ease of reading
205  void updateTotalVoxels();
206 
207  /// Creates a GDP with us as a Volume Primitive inside it.
208  GU_ConstDetailHandle createSmokeRepresentation(const SIM_RootData &root) const;
209 
210  /// Adds a volume primitive version of our field to the given
211  /// gdp.
212  void addSmokeRepresentation(const SIM_RootData &root, GU_Detail *gdp) const;
213 
214 protected:
215  explicit SIM_MatrixField(const SIM_DataFactory *factory);
216  virtual ~SIM_MatrixField();
217 
218  /// Overrides to properly implement this class as a SIM_Data.
219  virtual void initializeSubclass();
220  /// myField aware copy constructor.
221  virtual void makeEqualSubclass(const SIM_Data *source);
222 
223  /// Saves our attributes, and our internal data if it has been set.
224  virtual void saveSubclass(std::ostream &os) const;
225  /// Loads our attributes and internal data if it was set when we saved.
226  virtual bool loadSubclass(UT_IStream &is);
227 
228  virtual int64 getMemorySizeSubclass() const;
229 
230  /// Override the setDivisions to rebuild our voxel array on demand.
231  virtual void optionChangedSubclass(const char *name);
232 
233 private:
234 
235  static const SIM_DopDescription *getMatrixFieldDopDescription();
236 
237  SIM_RawField *myFields[3][3];
238 
239  void rebuildFields();
240 
242 
244  SIM_Data,
245  "MatrixField",
246  getMatrixFieldDopDescription());
247 };
248 #endif
249 
#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)
#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
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 getBorder() const
UT_VoxelBorderType
Definition: UT_VoxelArray.h:69
SIM_RawField * getField(int i, int j) const
Retrieve raw field.
GLint y
Definition: glcorearb.h:102
This class holds a three dimensional scalar field.
#define GETSET_DATA_FUNCS_F(DataName, FuncName)
SYS_FORCE_INLINE T & x(void)
Definition: UT_Vector3.h:581
png_uint_32 i
Definition: png.h:2877
#define GETSET_DATA_FUNCS_M3(DataName, FuncName)
#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
long long int64
Definition: SYS_Types.h:100
GLfloat f
Definition: glcorearb.h:1925
void pubHandleModification()
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)
void setBorder(UT_VoxelBorderType border)
#define SIM_NAME_TWOD
Definition: SIM_Names.h:206
GLuint const GLchar * name
Definition: glcorearb.h:785
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_Vector3 getVoxelSize() 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.
This class holds a three dimensional vector field.
bool appearsToBeUnused() const
UT_Vector3 getOrig() const
SIM_RawField rawfield_type
bool hasNan() const
True if we contain any NANs.
virtual void initializeSubclass()