HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SNOW_Solver.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *----------------------------------------------------------------------------
26  */
27 
28 /*
29  * The simplest network to see something working with this...
30  *
31  * Empty Object SnowVisualize
32  * \ /
33  * Snow VoxelArray
34  * |
35  * Snow Solver (Birth rate .1)
36  */
37 
38 #ifndef __SNOW_Solver_h__
39 #define __SNOW_Solver_h__
40 
41 #include <UT/UT_IStream.h>
42 #include <UT/UT_Map.h>
43 #include <UT/UT_VoxelArray.h>
44 #include <GA/GA_Types.h>
45 #include <GU/GU_DetailHandle.h>
46 #include <SIM/SIM_OptionsUser.h>
47 #include <SIM/SIM_SingleSolver.h>
48 #include <SIM/SIM_Geometry.h>
49 
50 #define SIM_NAME_BIRTHRATE "birthrate"
51 #define SIM_NAME_ORIGINALDEPTH "originaldepth"
52 
53 class SIM_Object;
54 class SIM_Random;
55 
56 typedef unsigned char u8;
57 
58 namespace HDK_Sample {
59 
60 class SNOW_VoxelArray;
61 
62 // This class implemented a computational fluid dynamics solver.
64  public SIM_OptionsUser
65 {
66 public:
67  // Access methods for our configuration data.
70 
71 protected:
72  explicit SNOW_Solver(const SIM_DataFactory *factory);
73  ~SNOW_Solver() override;
74 
76 
78  SIM_Object &object,
79  SIM_ObjectArray &feedbacktoobjects,
80  const SIM_Time &timestep,
81  bool newobject) override;
82 
83  int rand_choice(int numchoice, SIM_Random *rand) const;
84  bool brownianize(int &v, int dv, int max,
85  SIM_Random *rand) const;
86  int clearInDirection(const SNOW_VoxelArray &snow,
87  int sx, int sy, int sz,
88  int dx, int dy, int dz,
89  int &rx, int &ry, int &rz,
90  int maxdist,
91  SIM_Random *rand) const;
92  void clearSnow(SNOW_VoxelArray &snow,
93  int x, int y, int z,
94  SIM_Random *rand) const;
95 
96  void fillRow(SNOW_VoxelArray &snow,
97  fpreal startx, fpreal endx,
98  int y, int z,
99  u8 voxeltype,
100  SIM_Random *rand) const;
101  void applyGeometry(SNOW_VoxelArray &snow,
102  const GU_ConstDetailHandle &gdh,
103  const UT_DMatrix4 &xform,
104  u8 voxletype,
105  SIM_Random *rand) const;
106 
107 private:
108  static const SIM_DopDescription *getSolverSNOWDopDescription();
109 
110  void solveForObject(SIM_Object &object,
111  SNOW_VoxelArray &snow,
112  const SIM_Time &timestep) const;
113  void setVoxelArrayAttributes(
114  SNOW_VoxelArray *voxelarray) const;
115 
116  DECLARE_STANDARD_GETCASTTOTYPE();
117  DECLARE_DATAFACTORY(SNOW_Solver,
119  "SNOW Solver",
120  getSolverSNOWDopDescription());
121 };
122 
123 // Standard defines for voxel elements:
124 #define VOXEL_EMPTY 0
125 #define VOXEL_SNOW 1
126 #define VOXEL_COMPRESSED 2
127 #define VOXEL_WALL 3
128 #define VOXEL_OBJECT 4
129 
130 #define SNOW_NAME_DIVISIONS "div"
131 #define SNOW_NAME_CENTER "t"
132 #define SNOW_NAME_SIZE "size"
133 
134 // This class hold an oriented bounding box tree.
136  // SIM_Geometry already mixes this in.
137  // public SIM_OptionsUser
138 {
139 public:
143 
144  // Read an element: This will return 0 for out of bound x/y/z values.
145  u8 getVoxel(int x, int y, int z) const;
146  void setVoxel(u8 voxel, int x, int y, int z);
147 
148  void collapseAllTiles();
149 
151  { handleModification(); }
152 
153 protected:
154  explicit SNOW_VoxelArray(const SIM_DataFactory *factory);
155  ~SNOW_VoxelArray() override;
156 
157  // Overrides to properly implement this class as a SIM_Data.
158  void initializeSubclass() override;
159  void makeEqualSubclass(const SIM_Data *source) override;
160  // Normally you could override saveSubclass and loadSubclass
161  // and the IO variants would chain to it, but because SIM_Geometry
162  // has implemented the new IO path we need to as well.
163  void saveIOSubclass(std::ostream &os,
164  SIM_DataThreadedIO *io) const override;
165  bool loadIOSubclass(UT_IStream &is,
166  SIM_DataThreadedIO *io) override;
167  void handleModificationSubclass(int code) override;
168 
169  int64 getMemorySizeSubclass() const override;
170 
171  /// Invoked when our parameters are changed, we use it to
172  /// invalidate our array.
173  void optionChangedSubclass(const char *name) override;
174 
175  // Override the getGeometrySubclass function to construct our geometric
176  // representation in a just-in-time fashion.
177  GU_ConstDetailHandle getGeometrySubclass() const override;
178 
179 private:
180  static const SIM_DopDescription *getVoxelArrayDopDescription();
181 
182  GA_Offset createOrFindPoint(GU_Detail *gdp, int x, int y, int z);
183  void buildFace(GU_Detail *gdp,
184  int x0, int y0, int z0,
185  int x1, int y1, int z1,
186  int x2, int y2, int z2,
187  int x3, int y3, int z3);
188  void buildGeometryFromArray();
189  void freeArray() const;
190  void allocateArray() const;
191 
192  mutable GU_DetailHandle myDetailHandle;
193  mutable UT_VoxelArray<u8> *myVoxelArray;
194 
195  UT_Map<exint, GA_Offset> myPointHash;
196 
197  DECLARE_STANDARD_GETCASTTOTYPE();
198  DECLARE_DATAFACTORY(SNOW_VoxelArray, // Our Classname
199  SIM_Geometry, // Base type
200  "hdk_VoxelArray", // DOP Data Type
201  getVoxelArrayDopDescription() // PRM list.
202  );
203 };
204 
205 // This class hold an oriented bounding box tree.
206 class SNOW_Visualize : public SIM_Data,
207  public SIM_OptionsUser
208 {
209 public:
210  GET_GUIDE_FUNC_B(SIM_NAME_SHOWGUIDE, ShowGuide, true);
212  GET_GUIDE_FUNC_B("usebox", UseBox, false);
213 
214 protected:
215  explicit SNOW_Visualize(const SIM_DataFactory *factory);
216  ~SNOW_Visualize() override;
217 
218  // Overrides to properly implement this class as a SIM_Data.
219  void initializeSubclass() override;
220 
221  bool getIsAlternateRepresentationSubclass() const override;
223  const SIM_Data &) override;
224  SIM_Guide *createGuideObjectSubclass() const override;
225  void buildGuideGeometrySubclass(const SIM_RootData &root,
226  const SIM_Options &options,
227  const GU_DetailHandle &gdh,
228  UT_DMatrix4 *xform,
229  const SIM_Time &t
230  ) const override;
231 
232  static void createBoundingBoxGuide(GU_Detail *gdp,
233  const UT_BoundingBox &bbox,
234  const UT_Vector3 &color);
235 
236 private:
237  static const SIM_DopDescription *getVisualizeDopDescription();
238 
239  const SNOW_VoxelArray *myArray;
240 
241  DECLARE_STANDARD_GETCASTTOTYPE();
242  DECLARE_DATAFACTORY(SNOW_Visualize, // Our Classname
243  SIM_Data, // Base type
244  "hdk_SnowVisualize", // DOP Data Type
245  getVisualizeDopDescription() // PRM list.
246  );
247 };
248 
249 } // End the HDK_Sample namespace
250 
251 #endif
252 
#define SIM_NAME_SHOWGUIDE
Definition: SIM_Names.h:186
vint4 max(const vint4 &a, const vint4 &b)
Definition: simd.h:4703
void initAlternateRepresentationSubclass(const SIM_Data &) override
Definition: SNOW_Solver.C:1220
GLuint const GLchar * name
Definition: glew.h:1814
GETSET_DATA_FUNCS_F(SIM_NAME_BIRTHRATE, BirthRate)
void initializeSubclass() override
Definition: SNOW_Solver.C:1206
GET_GUIDE_FUNC_V3(SIM_NAME_COLOR, Color,(1, 1, 1))
GLuint GLfloat GLfloat GLfloat x1
Definition: glew.h:12681
GETSET_DATA_FUNCS_V3(SNOW_NAME_DIVISIONS, Divisions)
void makeEqualSubclass(const SIM_Data *source) override
Makes this geometry equal to some other SIM_Geometry.
Definition: SNOW_Solver.C:876
void initializeSubclass() override
Set initial values on all the geometry attributes.
Definition: SNOW_Solver.C:868
GLsizei GLsizei GLchar * source
Definition: glew.h:1832
int64 getMemorySizeSubclass() const override
Definition: SNOW_Solver.C:981
const GLdouble * v
Definition: glew.h:1391
GLhandleARB obj
Definition: glew.h:6236
void fillRow(SNOW_VoxelArray &snow, fpreal startx, fpreal endx, int y, int z, u8 voxeltype, SIM_Random *rand) const
Definition: SNOW_Solver.C:221
GET_GUIDE_FUNC_B(SIM_NAME_SHOWGUIDE, ShowGuide, true)
GU_ConstDetailHandle getGeometrySubclass() const override
Definition: SNOW_Solver.C:683
bool getIsAlternateRepresentationSubclass() const override
Definition: SNOW_Solver.C:1214
u8 getVoxel(int x, int y, int z) const
Definition: SNOW_Solver.C:664
GLdouble GLdouble z
Definition: glew.h:1559
GA_Size GA_Offset
Definition: GA_Types.h:639
unsigned char u8
Definition: SNOW_Solver.h:54
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
int rand_choice(int numchoice, SIM_Random *rand) const
Definition: SNOW_Solver.C:214
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
Holds pointers to a number of SIM_Object objects.
void saveIOSubclass(std::ostream &os, SIM_DataThreadedIO *io) const override
Definition: SNOW_Solver.C:902
long long int64
Definition: SYS_Types.h:116
GLuint GLfloat GLfloat y0
Definition: glew.h:12681
SNOW_Visualize(const SIM_DataFactory *factory)
Definition: SNOW_Solver.C:1015
GLuint color
Definition: glew.h:7902
int clearInDirection(const SNOW_VoxelArray &snow, int sx, int sy, int sz, int dx, int dy, int dz, int &rx, int &ry, int &rz, int maxdist, SIM_Random *rand) const
Definition: SNOW_Solver.C:129
static void createBoundingBoxGuide(GU_Detail *gdp, const UT_BoundingBox &bbox, const UT_Vector3 &color)
Definition: SNOW_Solver.C:1066
void setVoxel(u8 voxel, int x, int y, int z)
Definition: SNOW_Solver.C:673
void applyGeometry(SNOW_VoxelArray &snow, const GU_ConstDetailHandle &gdh, const UT_DMatrix4 &xform, u8 voxletype, SIM_Random *rand) const
Definition: SNOW_Solver.C:257
#define SIM_NAME_BIRTHRATE
Definition: SNOW_Solver.h:50
SIM_Guide * createGuideObjectSubclass() const override
Definition: SNOW_Solver.C:1057
GLuint GLfloat x0
Definition: glew.h:12681
void optionChangedSubclass(const char *name) override
Definition: SNOW_Solver.C:651
SNOW_Solver(const SIM_DataFactory *factory)
Definition: SNOW_Solver.C:60
bool brownianize(int &v, int dv, int max, SIM_Random *rand) const
Definition: SNOW_Solver.C:108
void clearSnow(SNOW_VoxelArray &snow, int x, int y, int z, SIM_Random *rand) const
Definition: SNOW_Solver.C:170
SNOW_VoxelArray(const SIM_DataFactory *factory)
Definition: SNOW_Solver.C:615
fpreal64 fpreal
Definition: SYS_Types.h:277
void handleModification(int code=-1)
GLuint GLfloat GLfloat GLfloat GLfloat y1
Definition: glew.h:12681
void handleModificationSubclass(int code) override
Definition: SNOW_Solver.C:998
bool loadIOSubclass(UT_IStream &is, SIM_DataThreadedIO *io) override
Definition: SNOW_Solver.C:929
#define SNOW_NAME_CENTER
Definition: SNOW_Solver.h:131
SIM_Result solveSingleObjectSubclass(SIM_Engine &engine, SIM_Object &object, SIM_ObjectArray &feedbacktoobjects, const SIM_Time &timestep, bool newobject) override
Definition: SNOW_Solver.C:582
#define SIM_NAME_COLOR
Definition: SIM_Names.h:87
void buildGuideGeometrySubclass(const SIM_RootData &root, const SIM_Options &options, const GU_DetailHandle &gdh, UT_DMatrix4 *xform, const SIM_Time &t) const override
Definition: SNOW_Solver.C:1131
#define SNOW_NAME_DIVISIONS
Definition: SNOW_Solver.h:130
#define SIM_NAME_ORIGINALDEPTH
Definition: SNOW_Solver.h:51
GETSET_DATA_FUNCS_I(SIM_NAME_ORIGINALDEPTH, OriginalDepth)
SIM_Random * createRandomData(SIM_Object *obj) const
Definition: SNOW_Solver.C:93
GLdouble GLdouble t
Definition: glew.h:1398
#define SNOW_NAME_SIZE
Definition: SNOW_Solver.h:132