HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIM_ForceOrbit.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_ForceOrbit.h"
29 #include <UT/UT_DSOVersion.h>
30 #include <GU/GU_PrimPoly.h>
31 #include <PRM/PRM_Include.h>
32 #include <SIM/SIM_PRMShared.h>
33 #include <SIM/SIM_DopDescription.h>
34 #include <SIM/SIM_GuideShared.h>
35 
36 using namespace HDK_Sample;
37 
38 void
40 {
42 }
43 
45  : BaseClass(factory),
47 {
48 }
49 
51 {
52 }
53 
54 static PRM_Name theGuideShowName(SIM_NAME_SHOWGUIDE, "Show Guide");
55 static PRM_Name theGuideSizeName(SIM_NAME_SCALE, "Scale");
56 static PRM_Name theGuideColorName(SIM_NAME_COLOR, "Color");
57 
58 const SIM_DopDescription *
59 SIM_ForceOrbit::getForceOrbitDopDescription()
60 {
61  static PRM_Name thePointPositionName(SIM_NAME_POINTPOSITION,
62  "Point Position");
63  static PRM_Name thePointMassName(SIM_NAME_POINTMASS,
64  "Point Mass");
65 
66  // define a template for parameters to control the force's behavior
67  static PRM_Template theTemplates[] = {
68  PRM_Template(PRM_XYZ_J, 3, &thePointPositionName),
69  PRM_Template(PRM_FLT_J, 1, &thePointMassName, PRMoneDefaults),
70  PRM_Template()
71  };
72 
73  // define a template for parameters to control the guide geometry
74  static PRM_Template theGuideTemplates[] = {
75  PRM_Template(PRM_TOGGLE, 1, &theGuideShowName, PRMoneDefaults),
76  PRM_Template(PRM_FLT_J, 1, &theGuideSizeName, PRMoneDefaults),
77  PRM_Template(PRM_RGB_J, 3, &theGuideColorName, PRMoneDefaults),
78  PRM_Template()
79  };
80 
81  static SIM_DopDescription theDopDescription(true,
82  "hdk_orbit",
83  "Orbit Force",
84  SIM_FORCES_DATANAME "/Orbit",
85  classname(),
86  theTemplates);
87  theDopDescription.setGuideTemplates(theGuideTemplates);
88  theDopDescription.setDefaultUniqueDataName(1);
89 
90  return &theDopDescription;
91 }
92 
93 SIM_Guide *
95 {
96  return new SIM_GuideShared(this, true);
97 }
98 
99 void
101  const SIM_Options &options,
102  const GU_DetailHandle &gdh,
103  UT_DMatrix4 *xform,
104  const SIM_Time &t) const
105 {
106  // return immediately if no guide geometry should be displayed
107  if( !options.hasOption(theGuideShowName.getToken()) ||
108  !options.getOptionB(theGuideShowName.getToken()) )
109  return;
110 
111  if( !gdh.isNull() )
112  {
114  GU_Detail *gdp = gdl.getGdp();
115 
116  // add a color attribute
117 #if defined(HOUDINI_11)
118  GB_AttributeRef attCd = gdp->addDiffuseAttribute(GEO_PRIMITIVE_DICT);
119 #else
121 #endif
122  UT_Vector3 color(1, 1, 1);
123  if( options.hasOption(theGuideColorName.getToken()) )
124  color = options.getOptionV3(theGuideColorName.getToken());
125 
126  // create three mutualy perpendicular lines to indicate the position
127  GA_Offset ptoff = gdp->appendPointBlock(6);
128  for(int axis = 0; axis < 3; ++axis)
129  {
130  const GA_Offset pt0 = ptoff++;
131  const GA_Offset pt1 = ptoff++;
132 
133  // re-position each point
134  UT_Vector3 pos(0, 0, 0);
135  pos(axis) = -0.5;
136  gdp->setPos3(pt0, pos);
137  pos(axis) = 0.5;
138  gdp->setPos3(pt1, pos);
139 
140  // create a colored line connecting the two points
141 #if defined(HOUDINI_11)
143 #else
145 #endif
146  if(attCd.isValid())
147  attCd.set(poly->getMapOffset(), color);
148  poly->appendVertex(pt0);
149  poly->appendVertex(pt1);
150  }
151  }
152  if( xform )
153  {
154  // scale the guide geometry
155  xform->identity();
156  if( options.hasOption(theGuideSizeName.getToken()) )
157  {
158  fpreal scale = options.getOptionF(theGuideSizeName.getToken());
159  xform->scale(scale, scale, scale);
160  }
161 
162  // position the guide geometry
163  UT_Vector3 pos = getPointPosition();
164  xform->translate(pos.x(), pos.y(), pos.z());
165  }
166 }
167 
168 void
170  const UT_Vector3 &position,
171  const UT_Vector3 &,
172  const UT_Vector3 &,
173  const fpreal mass,
174  UT_Vector3 &force,
175  UT_Vector3 &torque) const
176 {
177  UT_Vector3 pointposition = getPointPosition();
178  fpreal distancesquared;
179 
180  // Note that we don't use any gravitational constant G in this
181  // calculation. This is because if we used a real value of G we'd
182  // have to make our point mass about 10^10 before it would exert a
183  // noticeable force.
184  distancesquared = distance2(position, pointposition);
185  if( !SYSequalZero(distancesquared) )
186  {
187  force = pointposition - position;
188  force *= (mass * getPointMass()) / distancesquared;
189  }
190  else
191  force = 0.0;
192  torque = 0.0;
193  applyNoise(position, force);
194 }
195 
196 void
198  const UT_Vector3 &position,
199  const UT_Vector3 &,
200  const fpreal,
201  const UT_Vector3 &velocity,
202  const UT_Vector3 &angvel,
203  const fpreal mass,
204  UT_Vector3 &force,
205  UT_Vector3 &torque) const
206 {
207  getForceSubclass(object, position, velocity, angvel, mass, force, torque);
208 }
209 
210 void
212  const UT_Vector3 &position,
213  const fpreal,
214  const UT_Vector3 &velocity,
215  const UT_Vector3 &angvel,
216  const fpreal mass,
217  UT_Vector3 &force,
218  UT_Vector3 &torque) const
219 {
220  getForceSubclass(object, position, velocity, angvel, mass, force, torque);
221 }
222 
#define SIM_NAME_SHOWGUIDE
Definition: SIM_Names.h:186
bool getOptionB(const UT_StringRef &name) const
#define SIM_FORCES_DATANAME
Definition: SIM_Names.h:45
GU_Detail * getGdp() const
SIM_Guide * createGuideObjectSubclass() const override
void getForceOnSphereSubclass(const SIM_Object &object, const UT_Vector3 &position, const fpreal radius, const UT_Vector3 &velocity, const UT_Vector3 &angvel, const fpreal mass, UT_Vector3 &force, UT_Vector3 &torque) const override
void getForceOnCircleSubclass(const SIM_Object &object, const UT_Vector3 &position, const UT_Vector3 &normal, const fpreal radius, const UT_Vector3 &velocity, const UT_Vector3 &angvel, const fpreal mass, UT_Vector3 &force, UT_Vector3 &torque) const override
#define IMPLEMENT_DATAFACTORY(DataClass)
SIM_API const UT_StringHolder torque
fpreal64 distance2(const UT_VectorD &v1, const UT_VectorD &v2)
Distance squared (L2) aka quadrance.
Definition: UT_Vector.h:399
constexpr SYS_FORCE_INLINE T & z() noexcept
Definition: UT_Vector3.h:667
GA_Attribute * addDiffuseAttribute(GA_AttributeOwner who, GA_Storage s=GA_STORE_INVALID)
void getForceSubclass(const SIM_Object &object, const UT_Vector3 &position, const UT_Vector3 &velocity, const UT_Vector3 &angvel, const fpreal mass, UT_Vector3 &force, UT_Vector3 &torque) const override
const UT_Vector3D & getOptionV3(const UT_StringRef &name) const
**But if you need a or simply need to know when the task has note that the like this
Definition: thread.h:617
PRM_API const PRM_Type PRM_XYZ_J
#define SIM_NAME_POINTMASS
PRM_API const PRM_Type PRM_RGB_J
void initializeSIM(void *)
GA_Size GA_Offset
Definition: GA_Types.h:646
GA_API const UT_StringHolder scale
SYS_FORCE_INLINE GA_Offset appendPointBlock(GA_Size npoints)
Append new points, returning the first offset of the contiguous block.
Definition: GA_Detail.h:330
GA_Size appendVertex(GA_Offset ptoff) override
void buildGuideGeometrySubclass(const SIM_RootData &root, const SIM_Options &options, const GU_DetailHandle &gdh, UT_DMatrix4 *xform, const SIM_Time &t) const override
We override this method to create the attractor geometry.
void identity()
Set the matrix to identity.
Definition: UT_Matrix4.h:1128
SIM_ForceOrbit(const SIM_DataFactory *factory)
PRM_API const PRM_Type PRM_FLT_J
void applyNoise(const UT_Vector3 &pos, UT_Vector3 &forceortorque) const
GLdouble t
Definition: glad.h:2397
bool SYSequalZero(const UT_Vector3T< T > &v)
Definition: UT_Vector3.h:1069
void scale(T sx, T sy, T sz, T sw=1)
Definition: UT_Matrix4.h:701
GA_API const UT_StringHolder mass
PRM_API PRM_Default PRMoneDefaults[]
SYS_FORCE_INLINE void setPos3(GA_Offset ptoff, const UT_Vector3 &pos)
Set P from a UT_Vector3.
Definition: GA_Detail.h:237
SIM_API const UT_StringHolder force
bool hasOption(const UT_StringRef &name) const
void translate(T dx, T dy, T dz=0)
Definition: UT_Matrix4.h:773
GLuint color
Definition: glcorearb.h:1261
fpreal64 fpreal
Definition: SYS_Types.h:277
SYS_FORCE_INLINE GEO_Primitive * appendPrimitive(const GA_PrimitiveTypeId &type)
Definition: GEO_Detail.h:1236
#define SIM_NAME_SCALE
Definition: SIM_Names.h:183
SIM_API const UT_StringHolder position
SYS_FORCE_INLINE GA_Offset getMapOffset() const
Gets the offset of this primitive in the detail containing it.
Definition: GA_Primitive.h:146
PRM_API const PRM_Type PRM_TOGGLE
#define SIM_NAME_COLOR
Definition: SIM_Names.h:87
#define SIM_NAME_POINTPOSITION
constexpr SYS_FORCE_INLINE T & y() noexcept
Definition: UT_Vector3.h:665
GEO_API const TypeMask GEOPRIMPOLY
fpreal64 getOptionF(const UT_StringRef &name) const
const char * getToken() const
Definition: PRM_Name.h:79
constexpr SYS_FORCE_INLINE T & x() noexcept
Definition: UT_Vector3.h:663
bool isNull() const