HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIM_RadialEmit.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024
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 #include "SIM_RadialEmit.h"
29 #include <UT/UT_DSOVersion.h>
30 #include <UT/UT_Interrupt.h>
31 #include <PRM/PRM_Include.h>
32 #include <SIM/SIM_PRMShared.h>
33 #include <SIM/SIM_DopDescription.h>
34 #include <SIM/SIM_GeometryCopy.h>
35 #include <SIM/SIM_Object.h>
36 #include <GAS/GAS_SubSolver.h>
37 
38 using namespace HDK_Sample;
39 
40 ///
41 /// This is the hook that Houdini grabs from the dll to link in
42 /// this. As such, it merely has to implement the data factory
43 /// for this node.
44 ///
45 void
47 {
49 }
50 
51 /// Standard constructor, note that BaseClass was crated by the
52 /// DECLARE_DATAFACTORY and provides an easy way to chain through
53 /// the class hierarchy.
55  : BaseClass(factory)
56 {
57 }
58 
60 {
61 }
62 
63 /// Used to automatically populate the node which will represent
64 /// this data type.
65 const SIM_DopDescription *
66 SIM_RadialEmit::getDopDescription()
67 {
68  static PRM_Name theGeometryName(GAS_NAME_GEOMETRY, "Geometry");
69 
70  static PRM_Name theCenterName("center", "Center");
71  static PRM_Name theDistanceName("distance", "Distance");
72  static PRM_Name theSpeedName("speed", "Speed");
73  static PRM_Name theBirthRateName("birthrate", "Birth Rate");
74  static PRM_Name theSeedName("seed", "Seed");
75 
76  static PRM_Default speedDefaults[] =
77  {
78  PRM_Default(0),
79  PRM_Default(1),
80  };
81 
82  static PRM_Default distanceDefaults[] =
83  {
84  PRM_Default(0),
85  PRM_Default(1),
86  };
87 
88  static PRM_Template theTemplates[] = {
89  PRM_Template(PRM_STRING, 1, &theGeometryName),
90  PRM_Template(PRM_XYZ_J, 3, &theCenterName),
91  PRM_Template(PRM_FLT_J, 2, &theDistanceName, distanceDefaults),
92  PRM_Template(PRM_FLT_J, 2, &theSpeedName, speedDefaults),
93  PRM_Template(PRM_INT_J, 1, &theBirthRateName, PRMfourDefaults),
94  PRM_Template(PRM_INT_J, 1, &theSeedName),
95  PRM_Template()
96  };
97 
98  static SIM_DopDescription theDopDescription(
99  true, // Should we make a DOP?
100  "hdk_radialemit", // Internal name of the DOP.
101  "Gas Radial Emit", // Label of the DOP
102  "Solver", // Default data name
103  classname(), // The type of this DOP, usually the class.
104  theTemplates); // Template list for generating the DOP
105 
106  // Make this a microsolver:
107  setGasDescription(theDopDescription);
108 
109  return &theDopDescription;
110 }
111 
112 bool
114  SIM_Object *obj,
115  SIM_Time time,
116  SIM_Time timestep)
117 {
118  SIM_GeometryCopy *geo = 0;
119 
121  if (!geo)
122  {
124  "Geometry", UT_ERROR_MESSAGE);
125  return false;
126  }
127 
128  // Assume destination is an attribute we need to modify.
129  SIM_GeometryAutoWriteLock lock(geo);
130  GU_Detail &gdp = lock.getGdp();
131  UT_WorkBuffer msg;
132  uint seed = getSeed();
133 
134  // Velocity to write to.
135  GA_RWHandleV3 v_h(&gdp, GA_ATTRIB_POINT, "v");
136  if (v_h.isInvalid())
137  {
138  // While this alert can be useful, it often ends
139  // up as spam...
140  addError(obj, SIM_MESSAGE, "Missing attribute v", UT_ERROR_MESSAGE);
141  }
142  else
143  {
144  int born = getBirthRate();
145  UT_Vector3 center = getCenter();
146  UT_Vector2 distrange = getDistance();
147  UT_Vector2 speedrange = getSpeed();
148 
149  while (born --> 0)
150  {
151  GA_Offset newpt = gdp.appendPointOffset();
152 
153  UT_Vector3 pos, vel;
154 
155  pos.x() = SYSrandom(seed) - 0.5f;
156  pos.y() = SYSrandom(seed) - 0.5f;
157  pos.z() = SYSrandom(seed) - 0.5f;
158 
159  // We do rejection sampling of points outside the sphere
160  // to ensure a uniform distribution!
161  if (pos.length() > 0.5)
162  {
163  born++;
164  continue;
165  }
166  pos.normalize();
167  vel = pos;
168  pos *= distrange.x() + SYSrandom(seed) * (distrange.y()-distrange.x());
169  pos += center;
170 
171  gdp.setPos3(newpt, pos);
172 
173  vel *= speedrange.x() + SYSrandom(seed) * (speedrange.y()-speedrange.x());
174  v_h.set(newpt, vel);
175  }
176  }
177 
178  // Successful cook
179  return true;
180 }
181 
#define GAS_NAME_GEOMETRY
Definition: GAS_Utils.h:30
PRM_API const PRM_Type PRM_STRING
#define IMPLEMENT_DATAFACTORY(DataClass)
GT_API const UT_StringHolder time
void initializeSIM(void *)
static void setGasDescription(SIM_DopDescription &descr)
SIM_GeometryCopy * getGeometryCopy(SIM_Object *obj, const char *name, bool silent=false)
constexpr SYS_FORCE_INLINE T & z() noexcept
Definition: UT_Vector3.h:667
constexpr SYS_FORCE_INLINE T length() const noexcept
Definition: UT_Vector3.h:361
PRM_API const PRM_Type PRM_XYZ_J
PRM_API const PRM_Type PRM_INT_J
constexpr SYS_FORCE_INLINE T & x() noexcept
Definition: UT_Vector2.h:423
GA_Size GA_Offset
Definition: GA_Types.h:641
void addError(const SIM_RootData *root, int errorcode, const char *errorparm, UT_ErrorSeverity severity) const
Adds an error to our SIM_Engine.
SYS_FORCE_INLINE bool isInvalid() const
Definition: GA_Handle.h:191
PRM_API const PRM_Type PRM_FLT_J
SYS_FORCE_INLINE GA_Offset appendPointOffset()
Definition: GEO_Detail.h:1143
SYS_FORCE_INLINE void setPos3(GA_Offset ptoff, const UT_Vector3 &pos)
Set P from a UT_Vector3.
Definition: GA_Detail.h:237
SYS_FORCE_INLINE void set(GA_Offset off, const T &val) const
Definition: GA_Handle.h:354
SIM_RadialEmit(const SIM_DataFactory *factory)
bool solveGasSubclass(SIM_Engine &engine, SIM_Object *obj, SIM_Time time, SIM_Time timestep) override
SYS_FORCE_INLINE UT_StorageMathFloat_t< T > normalize() noexcept
Definition: UT_Vector3.h:376
PRM_API PRM_Default PRMfourDefaults[]
constexpr SYS_FORCE_INLINE T & y() noexcept
Definition: UT_Vector3.h:665
unsigned int uint
Definition: SYS_Types.h:45
constexpr SYS_FORCE_INLINE T & y() noexcept
Definition: UT_Vector2.h:425
constexpr SYS_FORCE_INLINE T & x() noexcept
Definition: UT_Vector3.h:663
This implements a SIM_Geometry that copies the source geometry.