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 #include <fstream.h>
00027 #include <UT/UT_DSOVersion.h>
00028 #include <CH/CH_LocalVariable.h>
00029 #include <PRM/PRM_Include.h>
00030 #include <PRM/PRM_SpareData.h>
00031 #include <OP/OP_OperatorTable.h>
00032 #include <OP/OP_Director.h>
00033 #include <DOP/DOP_Node.h>
00034 #include <DOP/DOP_Parent.h>
00035 #include <GU/GU_Detail.h>
00036 #include <GU/GU_PrimVolume.h>
00037 #include <SIM/SIM_Object.h>
00038 #include <SIM/SIM_ScalarField.h>
00039 #include <SIM/SIM_VectorField.h>
00040 #include <SIM/SIM_MatrixField.h>
00041 #include <ROP/ROP_Error.h>
00042 #include <ROP/ROP_Templates.h>
00043 #include <UT/UT_IOTable.h>
00044 #include "ROP_DopField.h"
00045
00046 using namespace HDK_Sample;
00047
00048 static PRM_Name dopPathName("doppath", "DOP Path");
00049 static PRM_Name theObjectName("dopobject", "DOP Object");
00050 static PRM_Name theDataName("dopdata", "DOP Data");
00051 static PRM_Name theFileName("file", "Output File");
00052 static PRM_Default theFileDefault(0, "$HIP/$F.bgeo");
00053 static PRM_Name alfprogressName("alfprogress", "Alfred Style Progress");
00054
00055
00056 static PRM_Name collateName("collatevector", "Collate Vector Fields");
00057
00058 static PRM_Template f3dTemplates[] = {
00059 PRM_Template(PRM_STRING, PRM_TYPE_DYNAMIC_PATH, 1, &dopPathName,
00060 0, 0, 0, 0, &PRM_SpareData::dopPath),
00061 PRM_Template(PRM_FILE, 1, &theFileName, &theFileDefault,0,
00062 0, 0, &PRM_SpareData::fileChooserModeWrite),
00063 PRM_Template(PRM_TOGGLE, 1, &alfprogressName, PRMzeroDefaults),
00064
00065 PRM_Template(PRM_STRING, 1, &theObjectName),
00066 PRM_Template(PRM_STRING, 1, &theDataName),
00067 PRM_Template()
00068
00069 };
00070
00071 static PRM_Template *
00072 getTemplates()
00073 {
00074 static PRM_Template *theTemplate = 0;
00075
00076 if (theTemplate)
00077 return theTemplate;
00078
00079 theTemplate = new PRM_Template[ROP_DOPFIELD_MAXPARMS+1];
00080 theTemplate[ROP_DOPFIELD_RENDER] = theRopTemplates[ROP_RENDER_TPLATE];
00081 theTemplate[ROP_DOPFIELD_RENDER_CTRL] = theRopTemplates[ROP_RENDERDIALOG_TPLATE];
00082 theTemplate[ROP_DOPFIELD_TRANGE] = theRopTemplates[ROP_TRANGE_TPLATE];
00083 theTemplate[ROP_DOPFIELD_FRANGE] = theRopTemplates[ROP_FRAMERANGE_TPLATE];
00084 theTemplate[ROP_DOPFIELD_TAKE] = theRopTemplates[ROP_TAKENAME_TPLATE];
00085 theTemplate[ROP_DOPFIELD_DOPPATH] = f3dTemplates[0];
00086 theTemplate[ROP_DOPFIELD_DOPOUTPUT] = f3dTemplates[1];
00087 theTemplate[ROP_DOPFIELD_DOPOBJECT] = f3dTemplates[3];
00088 theTemplate[ROP_DOPFIELD_DOPDATA] = f3dTemplates[4];
00089 theTemplate[ROP_DOPFIELD_INITSIM] = theRopTemplates[ROP_IFD_INITSIM_TPLATE];
00090 theTemplate[ROP_DOPFIELD_ALFPROGRESS] = f3dTemplates[2];
00091 theTemplate[ROP_DOPFIELD_TPRERENDER] = theRopTemplates[ROP_TPRERENDER_TPLATE];
00092 theTemplate[ROP_DOPFIELD_PRERENDER] = theRopTemplates[ROP_PRERENDER_TPLATE];
00093 theTemplate[ROP_DOPFIELD_LPRERENDER] = theRopTemplates[ROP_LPRERENDER_TPLATE];
00094 theTemplate[ROP_DOPFIELD_TPREFRAME] = theRopTemplates[ROP_TPREFRAME_TPLATE];
00095 theTemplate[ROP_DOPFIELD_PREFRAME] = theRopTemplates[ROP_PREFRAME_TPLATE];
00096 theTemplate[ROP_DOPFIELD_LPREFRAME] = theRopTemplates[ROP_LPREFRAME_TPLATE];
00097 theTemplate[ROP_DOPFIELD_TPOSTFRAME] = theRopTemplates[ROP_TPOSTFRAME_TPLATE];
00098 theTemplate[ROP_DOPFIELD_POSTFRAME] = theRopTemplates[ROP_POSTFRAME_TPLATE];
00099 theTemplate[ROP_DOPFIELD_LPOSTFRAME] = theRopTemplates[ROP_LPOSTFRAME_TPLATE];
00100 theTemplate[ROP_DOPFIELD_TPOSTRENDER] = theRopTemplates[ROP_TPOSTRENDER_TPLATE];
00101 theTemplate[ROP_DOPFIELD_POSTRENDER] = theRopTemplates[ROP_POSTRENDER_TPLATE];
00102 theTemplate[ROP_DOPFIELD_LPOSTRENDER] = theRopTemplates[ROP_LPOSTRENDER_TPLATE];
00103 theTemplate[ROP_DOPFIELD_MAXPARMS] = PRM_Template();
00104
00105 UT_ASSERT(PRM_Template::countTemplates(theTemplate) == ROP_DOPFIELD_MAXPARMS);
00106
00107 return theTemplate;
00108 }
00109
00110 OP_TemplatePair *
00111 ROP_DopField::getTemplatePair()
00112 {
00113 static OP_TemplatePair *ropPair = 0;
00114 if (!ropPair)
00115 {
00116 ropPair = new OP_TemplatePair(getTemplates());
00117 }
00118 return ropPair;
00119 }
00120
00121 OP_VariablePair *
00122 ROP_DopField::getVariablePair()
00123 {
00124 static OP_VariablePair *pair = 0;
00125 if (!pair)
00126 pair = new OP_VariablePair(ROP_Node::myVariableList);
00127 return pair;
00128 }
00129
00130 OP_Node *
00131 ROP_DopField::myConstructor(OP_Network *net, const char *name, OP_Operator *op)
00132 {
00133 return new ROP_DopField(net, name, op);
00134 }
00135
00136 ROP_DopField::ROP_DopField(OP_Network *net, const char *name, OP_Operator *entry)
00137 : ROP_Node(net, name, entry)
00138 {
00139 }
00140
00141
00142 ROP_DopField::~ROP_DopField()
00143 {
00144 }
00145
00146
00147
00148
00149
00150 int
00151 ROP_DopField::startRender(int , fpreal tstart, fpreal tend)
00152 {
00153 int rcode = 1;
00154
00155 myEndTime = tend;
00156 myStartTime = tstart;
00157
00158 if (INITSIM())
00159 {
00160 initSimulationOPs();
00161 OPgetDirector()->bumpSkipPlaybarBasedSimulationReset(1);
00162 }
00163
00164 if (error() < UT_ERROR_ABORT)
00165 {
00166 if( !executePreRenderScript(tstart) )
00167 return 0;
00168 }
00169
00170 return rcode;
00171 }
00172
00173 static void
00174 addField(GU_Detail *gdp, const SIM_RawField *rawfield)
00175 {
00176 GU_PrimVolume *vol;
00177
00178 vol = (GU_PrimVolume *) GU_PrimVolume::build(gdp);
00179
00180 vol->getVertex().getPt()->setPos(
00181 rawfield->getOrig() + rawfield->getSize()*0.5 );
00182
00183 UT_Matrix3 scale;
00184 scale.identity();
00185 scale.scale(rawfield->getSize().x(), rawfield->getSize().y(), rawfield->getSize().z());
00186 scale.scale(0.5, 0.5, 0.5);
00187 vol->setTransform(scale);
00188
00189
00190
00191
00192
00193 vol->stealVoxels(rawfield->fieldNC());
00194 }
00195
00196 ROP_RENDER_CODE
00197 ROP_DopField::renderFrame(fpreal time, UT_Interrupt *)
00198 {
00199 OP_Node *op;
00200 DOP_Parent *dopparent;
00201 UT_String doppath, savepath;
00202 UT_String dopobject, dopdata;
00203
00204 if( !executePreFrameScript(time) )
00205 return ROP_ABORT_RENDER;
00206
00207 DOPPATH(doppath, time);
00208
00209 if( !doppath.isstring() )
00210 {
00211 addError(ROP_MESSAGE, "Invalid DOP path");
00212 return ROP_ABORT_RENDER;
00213 }
00214
00215 op = findNode(doppath);
00216 if (!op)
00217 {
00218 addError(ROP_COOK_ERROR, (const char *)doppath);
00219 return ROP_ABORT_RENDER;
00220 }
00221
00222 dopparent = op ? op->castToDOPParent() : 0;
00223 if( !dopparent )
00224 {
00225 addError(ROP_COOK_ERROR, (const char *)doppath);
00226 return ROP_ABORT_RENDER;
00227 }
00228
00229 DOPOBJECT(dopobject, time);
00230 DOPDATA(dopdata, time);
00231 OUTPUT(savepath, time);
00232
00233
00234 time = DOPsetBestTime(dopparent, time);
00235
00236 OP_Context context(time);
00237
00238 const SIM_Object *object;
00239 object = dopparent->findObjectFromString(dopobject, 0, 0, time);
00240
00241 if (!object)
00242 {
00243 addError(ROP_COOK_ERROR, (const char *)dopobject);
00244 return ROP_ABORT_RENDER;
00245 }
00246
00247 const SIM_Data *data;
00248
00249 data = object->getConstNamedSubData(dopdata);
00250
00251 if (!data)
00252 {
00253 addError(ROP_COOK_ERROR, (const char *) dopdata);
00254 return ROP_ABORT_RENDER;
00255 }
00256
00257
00258 GU_Detail *gdp = new GU_Detail();
00259
00260 const SIM_ScalarField *scalarfield = SIM_DATA_CASTCONST(data, SIM_ScalarField);
00261 if (scalarfield)
00262 {
00263 addField(gdp, scalarfield->getField());
00264 }
00265 const SIM_VectorField *vectorfield = SIM_DATA_CASTCONST(data, SIM_VectorField);
00266 if (vectorfield)
00267 {
00268 for (int i = 0; i < 3; i++)
00269 {
00270 addField(gdp, vectorfield->getField(i));
00271 }
00272 }
00273 const SIM_MatrixField *matrixfield = SIM_DATA_CASTCONST(data, SIM_MatrixField);
00274 if (matrixfield)
00275 {
00276 for (int i = 0; i < 3; i++)
00277 for (int j = 0; j < 3; j++)
00278 {
00279 addField(gdp, matrixfield->getField(i, j));
00280 }
00281 }
00282
00283 if (!gdp->save((const char *)savepath, 0, 0).success())
00284 {
00285 addError(ROP_SAVE_ERROR, (const char *)savepath);
00286 return ROP_ABORT_RENDER;
00287 }
00288
00289
00290
00291
00292 if (ALFPROGRESS() && (myEndTime != myStartTime))
00293 {
00294 fpreal fpercent = (time - myStartTime) / (myEndTime - myStartTime);
00295 int percent = (int)SYSrint(fpercent * 100);
00296 percent = SYSclamp(percent, 0, 100);
00297 fprintf(stdout, "ALF_PROGRESS %d%%\n", percent);
00298 fflush(stdout);
00299 }
00300
00301 if (error() < UT_ERROR_ABORT)
00302 {
00303 if( !executePostFrameScript(time) )
00304 return ROP_ABORT_RENDER;
00305 }
00306
00307 return ROP_CONTINUE_RENDER;
00308 }
00309
00310 ROP_RENDER_CODE
00311 ROP_DopField::endRender()
00312 {
00313 if (INITSIM())
00314 OPgetDirector()->bumpSkipPlaybarBasedSimulationReset(-1);
00315
00316 if (error() < UT_ERROR_ABORT)
00317 {
00318 if( !executePostRenderScript(myEndTime) )
00319 return ROP_ABORT_RENDER;
00320 }
00321 return ROP_CONTINUE_RENDER;
00322 }
00323
00324 void
00325 newDriverOperator(OP_OperatorTable *table)
00326 {
00327 table->addOperator(new OP_Operator("hdk_dopfield",
00328 "Dop Field",
00329 ROP_DopField::myConstructor,
00330 ROP_DopField::getTemplatePair(),
00331 0,
00332 0,
00333 ROP_DopField::getVariablePair(),
00334 OP_FLAG_GENERATOR));
00335 }
00336