00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "SIM_ForceOrbit.h"
00029 #include <UT/UT_DSOVersion.h>
00030 #include <GU/GU_PrimPoly.h>
00031 #include <PRM/PRM_Include.h>
00032 #include <SIM/SIM_PRMShared.h>
00033 #include <SIM/SIM_DopDescription.h>
00034 #include <SIM/SIM_GuideShared.h>
00035
00036 using namespace HDK_Sample;
00037
00038 void
00039 initializeSIM(void *)
00040 {
00041 IMPLEMENT_DATAFACTORY(SIM_ForceOrbit);
00042 }
00043
00044 SIM_ForceOrbit::SIM_ForceOrbit(const SIM_DataFactory *factory)
00045 : BaseClass(factory),
00046 SIM_OptionsUser(this)
00047 {
00048 }
00049
00050 SIM_ForceOrbit::~SIM_ForceOrbit()
00051 {
00052 }
00053
00054 static PRM_Name theGuideShowName(SIM_NAME_SHOWGUIDE, "Show Guide");
00055 static PRM_Name theGuideSizeName(SIM_NAME_SCALE, "Scale");
00056 static PRM_Name theGuideColorName(SIM_NAME_COLOR, "Color");
00057
00058 const SIM_DopDescription *
00059 SIM_ForceOrbit::getForceOrbitDopDescription()
00060 {
00061 static PRM_Name thePointPositionName(SIM_NAME_POINTPOSITION,
00062 "Point Position");
00063 static PRM_Name thePointMassName(SIM_NAME_POINTMASS,
00064 "Point Mass");
00065
00066
00067 static PRM_Template theTemplates[] = {
00068 PRM_Template(PRM_XYZ_J, 3, &thePointPositionName),
00069 PRM_Template(PRM_FLT_J, 1, &thePointMassName, PRMoneDefaults),
00070 PRM_Template()
00071 };
00072
00073
00074 static PRM_Template theGuideTemplates[] = {
00075 PRM_Template(PRM_TOGGLE, 1, &theGuideShowName, PRMoneDefaults),
00076 PRM_Template(PRM_FLT_J, 1, &theGuideSizeName, PRMoneDefaults),
00077 PRM_Template(PRM_RGB_J, 3, &theGuideColorName, PRMoneDefaults),
00078 PRM_Template()
00079 };
00080
00081 static SIM_DopDescription theDopDescription(true,
00082 "hdk_orbit",
00083 "Orbit Force",
00084 SIM_FORCES_DATANAME "/Orbit",
00085 classname(),
00086 theTemplates);
00087 theDopDescription.setGuideTemplates(theGuideTemplates);
00088 theDopDescription.setDefaultUniqueDataName(1);
00089
00090 return &theDopDescription;
00091 }
00092
00093 SIM_Guide *
00094 SIM_ForceOrbit::createGuideObjectSubclass() const
00095 {
00096 return new SIM_GuideShared(this, true);
00097 }
00098
00099 void
00100 SIM_ForceOrbit::buildGuideGeometrySubclass(const SIM_RootData &root,
00101 const SIM_Options &options,
00102 const GU_DetailHandle &gdh,
00103 UT_DMatrix4 *xform,
00104 const SIM_Time &t) const
00105 {
00106
00107 if( !options.hasOption(theGuideShowName.getToken()) ||
00108 !options.getOptionB(theGuideShowName.getToken()) )
00109 return;
00110
00111 if( !gdh.isNull() )
00112 {
00113 GU_DetailHandleAutoWriteLock gdl(gdh);
00114 GU_Detail *gdp = gdl.getGdp();
00115
00116
00117 GB_AttributeRef attCd = gdp->addDiffuseAttribute(GEO_PRIMITIVE_DICT);
00118 UT_Vector3 color(1, 1, 1);
00119 if( options.hasOption(theGuideColorName.getToken()) )
00120 color = options.getOptionV3(theGuideColorName.getToken());
00121
00122
00123 for(int axis = 0; axis < 3; ++axis)
00124 {
00125 GEO_Point *pt0 = gdp->appendPoint();
00126 GEO_Point *pt1 = gdp->appendPoint();
00127
00128
00129 UT_Vector4 pos(0, 0, 0);
00130 pos(axis) = -0.5;
00131 pt0->setPos(pos);
00132 pos(axis) = 0.5;
00133 pt1->setPos(pos);
00134
00135
00136 GU_PrimPoly *poly = (GU_PrimPoly *)gdp->appendPrimitive(GEOPRIMPOLY);
00137 if(attCd.isValid())
00138 poly->setValue<UT_Vector3>(attCd, color);
00139 poly->appendVertex(pt0);
00140 poly->appendVertex(pt1);
00141 }
00142 }
00143 if( xform )
00144 {
00145
00146 xform->identity();
00147 if( options.hasOption(theGuideSizeName.getToken()) )
00148 {
00149 fpreal scale = options.getOptionF(theGuideSizeName.getToken());
00150 xform->scale(scale, scale, scale);
00151 }
00152
00153
00154 UT_Vector3 pos = getPointPosition();
00155 xform->translate(pos.x(), pos.y(), pos.z());
00156 }
00157 }
00158
00159 void
00160 SIM_ForceOrbit::getForceSubclass(const SIM_Object &,
00161 const UT_Vector3 &position,
00162 const UT_Vector3 &,
00163 const UT_Vector3 &,
00164 const fpreal mass,
00165 UT_Vector3 &force,
00166 UT_Vector3 &torque) const
00167 {
00168 UT_Vector3 pointposition = getPointPosition();
00169 fpreal distancesquared;
00170
00171
00172
00173
00174
00175 distancesquared = distance2(position, pointposition);
00176 if( !UTequalZero(distancesquared) )
00177 {
00178 force = pointposition - position;
00179 force *= (mass * getPointMass()) / distancesquared;
00180 }
00181 else
00182 force = 0.0;
00183 torque = 0.0;
00184 applyNoise(position, force);
00185 }
00186
00187 void
00188 SIM_ForceOrbit::getForceOnCircleSubclass(const SIM_Object &object,
00189 const UT_Vector3 &position,
00190 const UT_Vector3 &,
00191 const fpreal,
00192 const UT_Vector3 &velocity,
00193 const UT_Vector3 &angvel,
00194 const fpreal mass,
00195 UT_Vector3 &force,
00196 UT_Vector3 &torque) const
00197 {
00198 getForceSubclass(object, position, velocity, angvel, mass, force, torque);
00199 }
00200
00201 void
00202 SIM_ForceOrbit::getForceOnSphereSubclass(const SIM_Object &object,
00203 const UT_Vector3 &position,
00204 const fpreal,
00205 const UT_Vector3 &velocity,
00206 const UT_Vector3 &angvel,
00207 const fpreal mass,
00208 UT_Vector3 &force,
00209 UT_Vector3 &torque) const
00210 {
00211 getForceSubclass(object, position, velocity, angvel, mass, force, torque);
00212 }
00213