HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  void setVoxelSize(const UT_Vector3 &voxelsize)
116  { for (int i = 0; i < 3; i++)
117  for (int j = 0; j < 3; j++)
118  myFields[i][j]->setVoxelSize(voxelsize);
119  }
120 
121  /// Access the field value given a world space location.
122  /// This does trilinear interpolation.
123  UT_Matrix3 getValue(const UT_Vector3 &pos) const;
124 
125  /// Gets the velocity at the given *voxel* location, interpolating
126  /// if we have corner or face velocities.
127  UT_Matrix3 getCellValue(int x, int y, int z) const;
128 
129  /// Adds a velocity to the given *voxel*. If this is face,
130  /// it is divided in two and spread on each of 6 faces. If it is
131  /// corner, it is divided by 8 and spread along each of 8 corners.
132  void addToCell(int x, int y, int z, const UT_Matrix3 &dv);
133 
134  /// Advects this field by the other given field.
135  void advect(const SIM_VectorField *vel, float timestep,
136  const SIM_RawField *collision,
137  SIM_FieldAdvection advectmethod,
138  float cfl);
139  void advect(sim_PointVelocity getVelocity, float timestep,
140  float voxelsize,
141  const SIM_RawField *collision = 0,
142  float cfl = 1.0f);
143 
144  /// Advects this by the velocity field, storing our min/max
145  /// interpolants into the min/max fields
146  void advectMinMax(SIM_MatrixField *minfield,
147  SIM_MatrixField *maxfield,
148  const SIM_VectorField *vel, float timestep,
149  const SIM_RawField *collision,
150  SIM_FieldAdvection advectmethod,
151  float cfl);
152 
153  /// Enforces boundary conditions on the array.
154  void enforceBoundary(const SIM_ScalarField *collision=0,
155  const SIM_MatrixField *cvel = 0,
156  const SIM_MatrixField *bvel = 0);
157 
158  /// Converts an integer index into a worldspace position.
159  bool indexToPos(int i, int j, int x, int y, int z, UT_Vector3 &pos) const;
160 
161  /// Converts a worldspace position into an integer index.
162  bool posToIndex(int i, int j, const UT_Vector3 &pos, int &x, int &y, int &z) const;
163 
164  /// Retrieve raw field.
165  SIM_RawField *getField(int i, int j) const { return myFields[i][j]; }
166 
167  /// Sets the field to the given field, gaining ownership of it.
168  /// The new field must already match the field it will replace.
169  void setField(int i, int j, SIM_RawField *field);
170 
171  /// True if we contain any NANs
172  bool hasNan() const
173  {
174  int i, j;
175 
176  for (i = 0; i < 3; i++)
177  for (j = 0; j < 3; j++)
178  if (myFields[i][j]->hasNan())
179  return true;
180  return false;
181  }
182 
183  /// True if we have a constant value. Ignores end conditions
184  /// in determining this. Used as a rough guess that the field
185  /// is unused.
186  bool appearsToBeUnused() const
187  {
188  for (int i = 0; i < 3; i++)
189  for (int j = 0; j < 3; j++)
190  if (!getField(i, j)->field()->isConstant(0)) return false;
191  return true;
192  }
193 
194  /// Steals the field, replacing this copy with an empty field and
195  /// Steals the field, replacing this copy with an empty field and
196  /// returning the old version.
197  SIM_RawField *stealField(int i, int j);
198 
199  void testForNan() const;
201  {
202  testForNan();
204  }
206  { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) getField(i, j)->markGridAsChanged(); }
207 
208  /// Recomputes total number of voxels to be stored
209  /// on our options data for ease of reading
210  void updateTotalVoxels();
211 
212  /// Creates a GDP with us as a Volume Primitive inside it.
213  GU_ConstDetailHandle createSmokeRepresentation(const SIM_RootData &root) const;
214 
215  /// Adds a volume primitive version of our field to the given
216  /// gdp.
217  void addSmokeRepresentation(const SIM_RootData &root, GU_Detail *gdp) const;
218 
219 protected:
220  explicit SIM_MatrixField(const SIM_DataFactory *factory);
221  virtual ~SIM_MatrixField();
222 
223  /// Overrides to properly implement this class as a SIM_Data.
224  virtual void initializeSubclass();
225  /// myField aware copy constructor.
226  virtual void makeEqualSubclass(const SIM_Data *source);
227 
228  /// Saves our attributes, and our internal data if it has been set.
229  virtual void saveSubclass(std::ostream &os) const;
230  /// Loads our attributes and internal data if it was set when we saved.
231  virtual bool loadSubclass(UT_IStream &is);
232 
233  virtual int64 getMemorySizeSubclass() const;
234 
235  /// Override the setDivisions to rebuild our voxel array on demand.
236  virtual void optionChangedSubclass(const char *name);
237 
238 private:
239 
240  static const SIM_DopDescription *getMatrixFieldDopDescription();
241 
242  SIM_RawField *myFields[3][3];
243 
244  void rebuildFields();
245 
247 
249  SIM_Data,
250  "MatrixField",
251  getMatrixFieldDopDescription());
252 };
253 #endif
254 
#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:67
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:498
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:502
#define SIM_NAME_SIZE
Definition: SIM_Names.h:183
long long int64
Definition: SYS_Types.h:107
GLfloat f
Definition: glcorearb.h:1925
void pubHandleModification()
int64 exint
Definition: SYS_Types.h:116
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:500
double fpreal
Definition: SYS_Types.h:270
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.
void setVoxelSize(const UT_Vector3 &voxelsize)
virtual void initializeSubclass()