HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SNOW_Solver.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009
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  virtual ~SNOW_Solver();
74 
76 
78  SIM_Object &object,
79  SIM_ObjectArray &feedbacktoobjects,
80  const SIM_Time &timestep,
81  bool newobject);
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  virtual ~SNOW_VoxelArray();
156 
157  // Overrides to properly implement this class as a SIM_Data.
158  virtual void initializeSubclass();
159  virtual void makeEqualSubclass(const SIM_Data *source);
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  virtual void saveIOSubclass(std::ostream &os, SIM_DataThreadedIO *io) const;
164  virtual bool loadIOSubclass(UT_IStream &is, SIM_DataThreadedIO *io);
165  virtual void handleModificationSubclass(int code);
166 
167  virtual int64 getMemorySizeSubclass() const;
168 
169  /// Invoked when our parameters are changed, we use it to
170  /// invalidate our array.
171  virtual void optionChangedSubclass(const char *name);
172 
173  // Override the getGeometrySubclass function to construct our geometric
174  // representation in a just-in-time fashion.
176 
177 private:
178  static const SIM_DopDescription *getVoxelArrayDopDescription();
179 
180  GA_Offset createOrFindPoint(GU_Detail *gdp, int x, int y, int z);
181  void buildFace(GU_Detail *gdp,
182  int x0, int y0, int z0,
183  int x1, int y1, int z1,
184  int x2, int y2, int z2,
185  int x3, int y3, int z3);
186  void buildGeometryFromArray();
187  void freeArray() const;
188  void allocateArray() const;
189 
190  mutable GU_DetailHandle myDetailHandle;
191  mutable UT_VoxelArray<u8> *myVoxelArray;
192 
193  UT_Map<exint, GA_Offset> myPointHash;
194 
195  DECLARE_STANDARD_GETCASTTOTYPE();
196  DECLARE_DATAFACTORY(SNOW_VoxelArray, // Our Classname
197  SIM_Geometry, // Base type
198  "hdk_VoxelArray", // DOP Data Type
199  getVoxelArrayDopDescription() // PRM list.
200  );
201 };
202 
203 // This class hold an oriented bounding box tree.
204 class SNOW_Visualize : public SIM_Data,
205  public SIM_OptionsUser
206 {
207 public:
208  GET_GUIDE_FUNC_B(SIM_NAME_SHOWGUIDE, ShowGuide, true);
210  GET_GUIDE_FUNC_B("usebox", UseBox, false);
211 
212 protected:
213  explicit SNOW_Visualize(const SIM_DataFactory *factory);
214  virtual ~SNOW_Visualize();
215 
216  // Overrides to properly implement this class as a SIM_Data.
217  virtual void initializeSubclass();
218 
219  virtual bool getIsAlternateRepresentationSubclass() const;
220  virtual void initAlternateRepresentationSubclass(const SIM_Data &);
221  virtual SIM_Guide *createGuideObjectSubclass() const;
222  virtual void buildGuideGeometrySubclass(const SIM_RootData &root,
223  const SIM_Options &options,
224  const GU_DetailHandle &gdh,
225  UT_DMatrix4 *xform,
226  const SIM_Time &t) const;
227 
228  static void createBoundingBoxGuide(GU_Detail *gdp,
229  const UT_BoundingBox &bbox,
230  const UT_Vector3 &color);
231 
232 private:
233  static const SIM_DopDescription *getVisualizeDopDescription();
234 
235  const SNOW_VoxelArray *myArray;
236 
237  DECLARE_STANDARD_GETCASTTOTYPE();
238  DECLARE_DATAFACTORY(SNOW_Visualize, // Our Classname
239  SIM_Data, // Base type
240  "hdk_SnowVisualize", // DOP Data Type
241  getVisualizeDopDescription() // PRM list.
242  );
243 };
244 
245 } // End the HDK_Sample namespace
246 
247 #endif
248 
#define SIM_NAME_SHOWGUIDE
Definition: SIM_Names.h:185
virtual void buildGuideGeometrySubclass(const SIM_RootData &root, const SIM_Options &options, const GU_DetailHandle &gdh, UT_DMatrix4 *xform, const SIM_Time &t) const
Definition: SNOW_Solver.C:1131
GETSET_DATA_FUNCS_F(SIM_NAME_BIRTHRATE, BirthRate)
virtual int64 getMemorySizeSubclass() const
Definition: SNOW_Solver.C:981
const GLdouble * v
Definition: glcorearb.h:836
GET_GUIDE_FUNC_V3(SIM_NAME_COLOR, Color,(1, 1, 1))
GLuint color
Definition: glcorearb.h:1260
virtual void makeEqualSubclass(const SIM_Data *source)
Makes this geometry equal to some other SIM_Geometry.
Definition: SNOW_Solver.C:876
virtual bool getIsAlternateRepresentationSubclass() const
Definition: SNOW_Solver.C:1214
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
virtual bool loadIOSubclass(UT_IStream &is, SIM_DataThreadedIO *io)
Definition: SNOW_Solver.C:929
GETSET_DATA_FUNCS_V3(SNOW_NAME_DIVISIONS, Divisions)
GLint y
Definition: glcorearb.h:102
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)
virtual void saveIOSubclass(std::ostream &os, SIM_DataThreadedIO *io) const
Definition: SNOW_Solver.C:902
u8 getVoxel(int x, int y, int z) const
Definition: SNOW_Solver.C:664
const hboost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:132
GA_Size GA_Offset
Definition: GA_Types.h:617
long long int64
Definition: SYS_Types.h:100
unsigned char u8
Definition: SNOW_Solver.h:54
virtual SIM_Result solveSingleObjectSubclass(SIM_Engine &engine, SIM_Object &object, SIM_ObjectArray &feedbacktoobjects, const SIM_Time &timestep, bool newobject)
Definition: SNOW_Solver.C:582
int rand_choice(int numchoice, SIM_Random *rand) const
Definition: SNOW_Solver.C:214
Holds pointers to a number of SIM_Object objects.
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:802
SNOW_Visualize(const SIM_DataFactory *factory)
Definition: SNOW_Solver.C:1015
GLuint const GLchar * name
Definition: glcorearb.h:785
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
SNOW_Solver(const SIM_DataFactory *factory)
Definition: SNOW_Solver.C:60
double fpreal
Definition: SYS_Types.h:263
virtual void handleModificationSubclass(int code)
Definition: SNOW_Solver.C:998
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
void handleModification(int code=-1)
virtual SIM_Guide * createGuideObjectSubclass() const
Definition: SNOW_Solver.C:1057
GLint GLenum GLint x
Definition: glcorearb.h:408
virtual void initializeSubclass()
Definition: SNOW_Solver.C:1206
virtual void optionChangedSubclass(const char *name)
Definition: SNOW_Solver.C:651
#define SNOW_NAME_CENTER
Definition: SNOW_Solver.h:131
virtual void initializeSubclass()
Set initial values on all the geometry attributes.
Definition: SNOW_Solver.C:868
#define SIM_NAME_COLOR
Definition: SIM_Names.h:87
#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
virtual GU_ConstDetailHandle getGeometrySubclass() const
Definition: SNOW_Solver.C:683
#define SNOW_NAME_SIZE
Definition: SNOW_Solver.h:132
virtual void initAlternateRepresentationSubclass(const SIM_Data &)
Definition: SNOW_Solver.C:1220