source: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp @ 1022

Revision 1022, 18.6 KB checked in by mattausch, 18 years ago (diff)
RevLine 
[372]1#include "SceneGraph.h"
2#include "Exporter.h"
3#include "UnigraphicsParser.h"
4#include "X3dParser.h"
5#include "Preprocessor.h"
6#include "ViewCell.h"
7#include "Environment.h"
[439]8#include "ViewCellsManager.h"
[445]9#include "ViewCellBsp.h"
10#include "VspBspTree.h"
[469]11#include "RenderSimulator.h"
[496]12#include "GlRenderer.h"
[749]13#include "PlyParser.h"
[1020]14#include "SamplingStrategy.h"
[1022]15#include "VspOspTree.h"
[372]16
[1020]17
18
[863]19namespace GtpVisibilityPreprocessor {
[860]20
[1020]21const static bool ADDITIONAL_GEOMETRY_HACK = false;
[860]22
[492]23Preprocessor *preprocessor;
24
[750]25
[1001]26// HACK: Artificially modify scene to watch rendercost changes
[750]27static void AddGeometry(SceneGraph *scene)
28{
[752]29        scene->mRoot->UpdateBox();
30
[750]31        AxisAlignedBox3 sceneBox = scene->GetBox();
32
33        int n = 200;
34
35        if (0){
[840]36        // form grid of boxes
[750]37        for (int i = 0; i < n; ++ i)
38        {
39                for (int j = 0; j < n; ++ j)
40                {
41                        const Vector3 scale2((float)j * 0.8 / n + 0.1,  0.05, (float)i * 0.8  / (float)n + 0.1);
42               
[840]43                        const Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
[750]44               
[840]45                        const Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
[750]46                        AxisAlignedBox3 box(pt2, pt2 + boxSize);
[991]47                        Mesh *mesh = CreateMeshFromBox(box);
[750]48
49                        mesh->Preprocess();
50               
51                        MeshInstance *mi = new MeshInstance(mesh);
52                        scene->mRoot->mGeometry.push_back(mi);
53                }
54        }
55
56        for (int i = 0; i < n; ++ i)
57        {
58                for (int j = 0; j < n; ++ j)
59                {
60                        const Vector3 scale2(0.15, (float)j * 0.8 / n + 0.1, (float)i * 0.8  / (float)n + 0.1);
61               
62                        Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
63               
64                        Vector3 boxSize = sceneBox.Size() * Vector3(0.0025, 0.01, 0.0025);
65                        AxisAlignedBox3 box(pt2, pt2 + boxSize);
[991]66                        Mesh *mesh = CreateMeshFromBox(box);
[750]67
68                        mesh->Preprocess();
69               
70                        MeshInstance *mi = new MeshInstance(mesh);
71                        scene->mRoot->mGeometry.push_back(mi);
72                }
73        }
74
75        for (int i = 0; i < n; ++ i)
76        {
77                const Vector3 scale2(2, 0.2, (float)i * 0.8  / (float)n + 0.1);
78               
79                Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
80               
81                //Vector3 boxSize = sceneBox.Size() * Vector3(0.0025, 0.01, 0.0025);
82                Vector3 boxSize = sceneBox.Size() * Vector3(0.005, 0.02, 0.005);
[1020]83
[750]84                AxisAlignedBox3 box(pt2 + 0.1, pt2 + boxSize);
[991]85                Mesh *mesh = CreateMeshFromBox(box);
[750]86
87                mesh->Preprocess();
88               
89                MeshInstance *mi = new MeshInstance(mesh);
90                scene->mRoot->mGeometry.push_back(mi);
91        }
92       
93        scene->mRoot->UpdateBox();
94        }
95       
[840]96        // plane separating view space regions
97        if (1)
98        {
99                const Vector3 scale(1.0, 0.0, 0);
[750]100
[840]101                Vector3 pt = sceneBox.Min() + scale * (sceneBox.Max() - sceneBox.Min());
[750]102
[840]103                Plane3 cuttingPlane(Vector3(1, 0, 0), pt);
104                Mesh *planeMesh = new Mesh();
105               
106                Polygon3 *poly = sceneBox.CrossSection(cuttingPlane);
107                IncludePolyInMesh(*poly, *planeMesh);
108               
109                planeMesh->Preprocess();
110               
111                MeshInstance *planeMi = new MeshInstance(planeMesh);
112                scene->mRoot->mGeometry.push_back(planeMi);
113        }       
[750]114}
115
116
[372]117Preprocessor::Preprocessor():
118mKdTree(NULL),
[409]119mBspTree(NULL),
[445]120mVspBspTree(NULL),
[1002]121mViewCellsManager(NULL),
122mRenderSimulator(NULL)
[308]123{
[1004]124        Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
[538]125 
[840]126        // renderer will be constructed when the scene graph and viewcell manager will be known
127        renderer = NULL;
[496]128 
[1004]129        Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
130        Environment::GetSingleton()->GetBoolValue("Preprocessor.loadPolygonsAsMeshes", mLoadPolygonsAsMeshes);
131        Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
132        Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
133        Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
134        Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
[599]135
[871]136        char buffer[256];
[1004]137        Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile",  buffer);
[871]138        mVisibilityFileName = buffer;
[1004]139        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter );
140        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
[904]141                                                          mApplyVisibilitySpatialFilter );
[1004]142        Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth);
[878]143
[840]144        Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
145        Debug << "load polygons as meshes: " << mLoadPolygonsAsMeshes << endl;
[372]146}
147
148
149Preprocessor::~Preprocessor()
150{
[752]151  cout << "cleaning up" << endl;
152
153  cout << "Deleting view cells manager ... \n";
[496]154  DEL_PTR(mViewCellsManager);
[752]155  cout << "done.\n";
156
157  cout << "Deleting bsp tree ... \n";
[496]158  DEL_PTR(mBspTree);
[752]159  cout << "done.\n";
160
[840]161  cout << "Deleting kd tree...\n";
[496]162  DEL_PTR(mKdTree);
[840]163  cout << "done.\n";
[1006]164 
165#if 0
166  cout << "Deleting vsp osp tree...\n";
167  DEL_PTR(mVspOspTree);
[840]168  cout << "done.\n";
[1006]169#endif
[752]170
[840]171  cout << "Deleting vspbsp tree...\n";
[496]172  DEL_PTR(mVspBspTree);
[840]173  cout << "done.\n";
[1002]174
175   cout << "Deleting scene graph...\n";
176  DEL_PTR(mSceneGraph);
177  cout << "done.\n";
178
179  DEL_PTR(mRenderSimulator);
180  DEL_PTR(renderer);
[372]181}
182
[387]183int
[419]184SplitFilenames(const string str, vector<string> &filenames)
[387]185{
186        int pos = 0;
187
188        while(1) {
[469]189                int npos = (int)str.find(';', pos);
[387]190               
191                if (npos < 0 || npos - pos < 1)
192                        break;
193                filenames.push_back(string(str, pos, npos - pos));
194                pos = npos + 1;
195        }
196       
197        filenames.push_back(string(str, pos, str.size() - pos));
[440]198        return (int)filenames.size();
[387]199}
200
[750]201
[372]202bool
203Preprocessor::LoadScene(const string filename)
204{
[925]205        // use leaf nodes of the original spatial hierarchy as occludees
[508]206        mSceneGraph = new SceneGraph;
[372]207 
[508]208        Parser *parser;
[387]209        vector<string> filenames;
210        int files = SplitFilenames(filename, filenames);
[712]211        cout << "number of input files: " << files << endl;
[387]212        bool result = false;
213        if (files == 1) {
214               
215                if (strstr(filename.c_str(), ".x3d"))
[749]216                  parser = new X3dParser;
[387]217                else
[811]218                  if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb"))
[749]219                        parser = new PlyParser;
220                  else
[387]221                        parser = new UnigraphicsParser;
[372]222
[387]223                cout<<filename<<endl;
[658]224                result = parser->ParseFile(filename, &mSceneGraph->mRoot, mLoadPolygonsAsMeshes);
[372]225
[387]226                delete parser;
[372]227
[387]228        } else {
229                // root for different files
230                mSceneGraph->mRoot = new SceneGraphNode;
231                for (int i= 0; i < filenames.size(); i++) {
232                        if (strstr(filenames[i].c_str(), ".x3d"))
233                                parser = new X3dParser;
234                        else
235                                parser = new UnigraphicsParser;
236                       
237                        SceneGraphNode *node;
238                        if (parser->ParseFile(filenames[i], &node)) {
239                                mSceneGraph->mRoot->mChildren.push_back(node);
240                                // at least one file parsed
241                                result = true;
242                        }
243                        delete parser;
244                }
245        }
[372]246       
247
[752]248        if (result)
[925]249        {
[752]250                // HACK
[1020]251                if (ADDITIONAL_GEOMETRY_HACK)
252                        AddGeometry(mSceneGraph);
[752]253               
[1020]254                mSceneGraph->AssignObjectIds();
255       
256                int intersectables, faces;
257                mSceneGraph->GetStatistics(intersectables, faces);
258       
259                cout<<filename<<" parsed successfully."<<endl;
260                cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
261                cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
262                mSceneGraph->CollectObjects(&mObjects);
263                mSceneGraph->mRoot->UpdateBox();
[752]264
[1020]265                if (0)
266                {
267                        Exporter *exporter = Exporter::GetExporter("testload.x3d");
[658]268
[1020]269                        if (exporter)
270                        {
271                                exporter->ExportGeometry(mObjects);
272                                delete exporter;
273                        }
274                }
[387]275        }
[372]276       
[492]277       
278        return result;
[372]279}
280
281bool
282Preprocessor::ExportPreprocessedData(const string filename)
283{
[871]284 
[931]285  mViewCellsManager->ExportViewCells(filename, true, mObjects);
[871]286 
287  return true;
[372]288}
289
290bool
[871]291Preprocessor::PostProcessVisibility()
292{
293 
[904]294  if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
[997]295        cout<<"Applying visibility filter ...";
[1002]296        cout<<"filter width = " << mVisibilityFilterWidth << endl;
[904]297       
[1002]298        if (!mViewCellsManager)
299                return false;
300
[871]301        mViewCellsManager->ApplyFilter(mKdTree,
[904]302                                                                   mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
303                                                                   mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
[997]304        cout << "done." << endl;
[871]305  }
306 
307  // export the preprocessed information to a file
308  if (mExportVisibility)
309        ExportPreprocessedData(mVisibilityFileName);
310 
311  return true;
312}
313
314
315bool
[372]316Preprocessor::BuildKdTree()
317{
318  mKdTree = new KdTree;
319  // add mesh instances of the scene graph to the root of the tree
320  KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
321  mSceneGraph->CollectObjects(&root->mObjects);
322 
323  mKdTree->Construct();
324  return true;
325}
326
327void
328Preprocessor::KdTreeStatistics(ostream &s)
329{
330  s<<mKdTree->GetStatistics();
331}
332
333void
334Preprocessor::BspTreeStatistics(ostream &s)
335{
336        s << mBspTree->GetStatistics();
337}
338
339bool
340Preprocessor::Export( const string filename,
[492]341                                          const bool scene,
342                                          const bool kdtree,
343                                          const bool bsptree
344                                          )
[372]345{
346  Exporter *exporter = Exporter::GetExporter(filename);
347       
348  if (exporter) {
349    if (scene)
350      exporter->ExportScene(mSceneGraph->mRoot);
351
352    if (kdtree) {
353      exporter->SetWireframe();
354      exporter->ExportKdTree(*mKdTree);
355    }
356
357        if (bsptree) {
358                //exporter->SetWireframe();
359                exporter->ExportBspTree(*mBspTree);
360        }
361
362    delete exporter;
363    return true;
364  }
365
366  return false;
367}
[429]368
[508]369
[463]370bool Preprocessor::PrepareViewCells()
371{
[577]372        //-- parse view cells construction method
[1004]373        Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
[577]374        char buf[100];
375       
376        if (mLoadViewCells)
[997]377        {       
[1004]378                Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
379                mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, true);
[577]380        }
381        else
382        {
383                //-- parse type of view cell container
[1004]384                Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);             
[1002]385            mViewCellsManager = CreateViewCellsManager(buf);
[577]386        }
[439]387
[997]388        //-- parameters for render heuristics evaluation
[473]389        float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
390
[1004]391        Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
392        Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
393        Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
[694]394       
[473]395        mRenderSimulator =
396                new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
[440]397
[480]398        mViewCellsManager->SetRenderer(mRenderSimulator);
[440]399
[997]400        // default view space is the extent of the scene
401        mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
[574]402
[538]403        if (mUseGlRenderer || mUseGlDebugger)
[540]404        {
405                // NOTE: render texture should be power of 2 and square
406                // renderer must be initialised
407                renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
[556]408                //              renderer->makeCurrent();
[746]409               
[540]410        }
[496]411       
[463]412        return true;
[490]413}
414
415
[575]416ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
417{
418        if (strcmp(name, "kdTree") == 0)
419        {
[1004]420                mViewCellsManager = new KdViewCellsManager(mKdTree);
[575]421        }
422        else if (strcmp(name, "bspTree") == 0)
423        {
424                Debug << "view cell type: Bsp" << endl;
425
[577]426                mBspTree = new BspTree();
[1004]427                mViewCellsManager = new BspViewCellsManager(mBspTree);
[575]428        }
429        else if (strcmp(name, "vspBspTree") == 0)
430        {
431                Debug << "view cell type: VspBsp" << endl;
432
[1004]433                mVspBspTree = new VspBspTree();
434                mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
[575]435        }
[1006]436        else if (strcmp(name, "vspOspTree") == 0)
[575]437        {
[1022]438                mVspTree = new VspTree();
439                mOspTree = new OspTree();
440
441                mViewCellsManager = new VspOspViewCellsManager(mVspTree, mOspTree);
[575]442        }
443        else if (strcmp(name, "sceneDependent") == 0)
444        {
445                //TODO
446                mBspTree = new BspTree();
447
448                Debug << "view cell type: Bsp" << endl;
449               
[1004]450                mViewCellsManager = new BspViewCellsManager(mBspTree);
[575]451        }
452        else
453        {
[664]454                cerr << "Wrong view cells type " << name << "!!!" << endl;
[575]455                exit(1);
456        }
457
458        return mViewCellsManager;
459}
460
461
[491]462// use ascii format to store rays
463#define USE_ASCII 0
464
465
[490]466inline bool ilt(Intersectable *obj1, Intersectable *obj2)
467{
468        return obj1->mId < obj2->mId;
469}
470
471
472bool Preprocessor::LoadSamples(VssRayContainer &samples,
473                                                           ObjectContainer &objects) const
474{
475        std::stable_sort(objects.begin(), objects.end(), ilt);
476        char fileName[100];
[1004]477        Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
[490]478       
[491]479    Vector3 origin, termination;
480        // HACK: needed only for lower_bound algorithm to find the
481        // intersected objects
482        MeshInstance sObj(NULL);
483        MeshInstance tObj(NULL);
[490]484
[491]485#if USE_ASCII
[656]486        ifstream samplesIn(fileName);
[490]487        if (!samplesIn.is_open())
488                return false;
489
490        string buf;
491        while (!(getline(samplesIn, buf)).eof())
492        {
[491]493                sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
[490]494                           &origin.x, &origin.y, &origin.z,
[491]495                           &termination.x, &termination.y, &termination.z,
496                           &(sObj.mId), &(tObj.mId));
[490]497               
[491]498                Intersectable *sourceObj = NULL;
499                Intersectable *termObj = NULL;
500               
501                if (sObj.mId >= 0)
[490]502                {
503                        ObjectContainer::iterator oit =
[491]504                                lower_bound(objects.begin(), objects.end(), &sObj, ilt);
505                        sourceObj = *oit;
506                }
507               
508                if (tObj.mId >= 0)
509                {
510                        ObjectContainer::iterator oit =
511                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
512                        termObj = *oit;
513                }
[490]514
[491]515                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
516        }
517#else
518        ifstream samplesIn(fileName, ios::binary);
519        if (!samplesIn.is_open())
520                return false;
521
522        while (1)
523        {
524                 samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
525                 samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
526                 samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
527                 samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
528               
529                 if (samplesIn.eof())
530                        break;
531
532                Intersectable *sourceObj = NULL;
533                Intersectable *termObj = NULL;
534               
535                if (sObj.mId >= 0)
536                {
537                        ObjectContainer::iterator oit =
538                                lower_bound(objects.begin(), objects.end(), &sObj, ilt);
539                        sourceObj = *oit;
[490]540                }
[491]541               
542                if (tObj.mId >= 0)
[490]543                {
[491]544                        ObjectContainer::iterator oit =
545                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
546                        termObj = *oit;
[490]547                }
[491]548
549                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
[490]550        }
[491]551
552#endif
[490]553        samplesIn.close();
554
555        return true;
556}
557
[508]558
559bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
[490]560{
[491]561        char fileName[100];
[1004]562        Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
[491]563       
[490]564
565        VssRayContainer::const_iterator it, it_end = samples.end();
566       
[491]567#if USE_ASCII
568        ofstream samplesOut(fileName);
[490]569        if (!samplesOut.is_open())
570                return false;
571
572        for (it = samples.begin(); it != it_end; ++ it)
573        {
574                VssRay *ray = *it;
[491]575                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
576                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;       
577
[490]578                samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
579                                   << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
[491]580                                   << sourceid << " " << termid << "\n";
[490]581        }
[491]582#else
583        ofstream samplesOut(fileName, ios::binary);
584        if (!samplesOut.is_open())
585                return false;
586
587        for (it = samples.begin(); it != it_end; ++ it)
588        {       
589                VssRay *ray = *it;
590                Vector3 origin(ray->GetOrigin());
591                Vector3 termination(ray->GetTermination());
592               
593                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
594                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;               
595
596                samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
597                samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
598                samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
599                samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
600    }
601#endif
[490]602        samplesOut.close();
603        return true;
604}
[563]605
[1020]606#if 0 // matt: implemented interface samplestrategy
[563]607bool
608Preprocessor::GenerateRays(
609                                                   const int number,
610                                                   const int sampleType,
611                                                   SimpleRayContainer &rays
612                                                   )
613{
614  Vector3 origin, direction;
[837]615  int startSize = (int)rays.size();
616  for (int i=0; (int)rays.size() - startSize  < number; i ++) {
[563]617        // now get the direction
618        switch (sampleType) {
619        case OBJECT_BASED_DISTRIBUTION: {
[576]620          mViewCellsManager->GetViewPoint(origin);
[563]621          Vector3 point;
622          Vector3 normal;
623          int i = RandomValue(0, mObjects.size() - 1);
624          Intersectable *object = mObjects[i];
625          object->GetRandomSurfacePoint(point, normal);
626          direction = point - origin;
627        }
628          break;
[576]629        case OBJECT_DIRECTION_BASED_DISTRIBUTION: {
630          int i = RandomValue(0, mObjects.size() - 1);
631          Intersectable *object = mObjects[i];
632          Vector3 normal;
633          object->GetRandomSurfacePoint(origin, normal);
634          direction = UniformRandomVector(normal);
635          origin += 0.1f*direction;
636        }
637          break;
[563]638        case DIRECTION_BASED_DISTRIBUTION:
[576]639          mViewCellsManager->GetViewPoint(origin);
[563]640          direction = UniformRandomVector();
641          break;
642        case DIRECTION_BOX_BASED_DISTRIBUTION: {
[576]643          mViewCellsManager->GetViewPoint(origin);
[563]644          float alpha = RandomValue(0.0f, 2*M_PI);
645          float beta = RandomValue(-M_PI/2, M_PI/2);
646          direction = VssRay::GetDirection(alpha, beta);
647          break;
648        }
649        case SPATIAL_BOX_BASED_DISTRIBUTION:
[576]650          mViewCellsManager->GetViewPoint(origin);
[563]651          direction = mKdTree->GetBox().GetRandomPoint() - origin;
652          break;
653        default:
654          // unsuported distribution type
655          return false;
656        }
657        // $$ jb the pdf is yet not correct for all sampling methods!
658        float pdf = 1.0f;
659        float c = Magnitude(direction);
660        if (c > Limits::Small) {
661          direction*=1.0f/c;
662          rays.AddRay(SimpleRay(origin, direction, pdf));
663        }
664  }
665  return true;
666}
[1020]667#endif
668bool Preprocessor::GenerateRays(const int number,
669                                                                const int sampleType,
670                                                                SimpleRayContainer &rays)
671{
672        Vector3 origin, direction;
673       
674        const int startSize = (int)rays.size();
675        SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
[860]676
[1020]677        if (!strategy)
678                return false;
679
680        for (int i=0; (int)rays.size() - startSize < number; ++ i)
681        {
682                SimpleRay newRay;
683                bool success = strategy->GenerateSample(newRay);
684
685                if (success)
686                        rays.AddRay(newRay);
687        }
688
689        delete strategy;
690
691    return true;
[878]692}
[1020]693
694
695SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId) const
696{
697        switch (strategyId)
698        {
699        case OBJECT_BASED_DISTRIBUTION:
700                return new ObjectBasedDistribution(*this);
701        case OBJECT_DIRECTION_BASED_DISTRIBUTION:
702                return new ObjectDirectionBasedDistribution(*this);
703        case DIRECTION_BASED_DISTRIBUTION:
704                return new DirectionBasedDistribution(*this);
705        case DIRECTION_BOX_BASED_DISTRIBUTION:
706                return new DirectionBoxBasedDistribution(*this);
707        case SPATIAL_BOX_BASED_DISTRIBUTION:
708                return new SpatialBoxBasedDistribution(*this);
[1021]709        //case OBJECTS_INTERIOR_DISTRIBUTION:
710        //      return new ObjectsInteriorDistribution(*this);
[1020]711        default: // no valid strategy
712                return NULL;
713        }
714        // should never come here
715        return NULL;
716}
717
718
719}
Note: See TracBrowser for help on using the repository browser.