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

Revision 1415, 35.4 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"
[1233]15#include "VspTree.h"
16#include "OspTree.h"
[1221]17#include "ObjParser.h"
[1264]18#include "BvHierarchy.h"
[1279]19#include "HierarchyManager.h"
[1287]20#include "VssRay.h"
[1264]21
[1287]22
[1251]23#ifdef GTP_INTERNAL
[1221]24#include "ArchModeler2MLRT.hxx"
[1232]25#endif
[372]26
[1292]27#define DEBUG_RAYCAST 0
28
29
[863]30namespace GtpVisibilityPreprocessor {
[860]31
[1020]32const static bool ADDITIONAL_GEOMETRY_HACK = false;
[860]33
[1145]34//Preprocessor *preprocessor;
[492]35
[750]36
[1001]37// HACK: Artificially modify scene to watch rendercost changes
[750]38static void AddGeometry(SceneGraph *scene)
39{
[1328]40        scene->GetRoot()->UpdateBox();
[752]41
[750]42        AxisAlignedBox3 sceneBox = scene->GetBox();
43
44        int n = 200;
45
[1291]46        if (0)
[750]47        {
[1291]48                // form grid of boxes
49                for (int i = 0; i < n; ++ i)
[750]50                {
[1291]51                        for (int j = 0; j < n; ++ j)
52                        {
53                                const Vector3 scale2((float)j * 0.8f / n + 0.1f,  0.05f, (float)i * 0.8f  / (float)n + 0.1f);
[750]54
[1291]55                                const Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
56
57                                const Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
58                                AxisAlignedBox3 box(pt2, pt2 + boxSize);
59                                Mesh *mesh = CreateMeshFromBox(box);
60
61                                mesh->Preprocess();
62
63                                MeshInstance *mi = new MeshInstance(mesh);
[1328]64                                scene->GetRoot()->mGeometry.push_back(mi);
[1291]65                        }
[750]66                }
67
[1291]68                for (int i = 0; i < n; ++ i)
[750]69                {
[1291]70                        for (int j = 0; j < n; ++ j)
71                        {
72                                const Vector3 scale2(0.15f, (float)j * 0.8f / n + 0.1f, (float)i * 0.8f  / (float)n + 0.1f);
73
74                                Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
75
76                                Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
77                                AxisAlignedBox3 box(pt2, pt2 + boxSize);
78                                Mesh *mesh = CreateMeshFromBox(box);
79
80                                mesh->Preprocess();
81
82                                MeshInstance *mi = new MeshInstance(mesh);
[1328]83                                scene->GetRoot()->mGeometry.push_back(mi);
[1291]84                        }
85                }
86
87                for (int i = 0; i < n; ++ i)
88                {
89                        const Vector3 scale2(2, 0.2f, (float)i * 0.8f  / (float)n + 0.1f);
90
[750]91                        Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
[1291]92
93                        //Vector3 boxSize = sceneBox.Size() * Vector3(0.0025, 0.01, 0.0025);
94                        Vector3 boxSize = sceneBox.Size() * Vector3(0.005f, 0.02f, 0.005f);
95
96                        AxisAlignedBox3 box(pt2 + 0.1f, pt2 + boxSize);
[991]97                        Mesh *mesh = CreateMeshFromBox(box);
[750]98
99                        mesh->Preprocess();
[1291]100
[750]101                        MeshInstance *mi = new MeshInstance(mesh);
[1328]102                        scene->GetRoot()->mGeometry.push_back(mi);
[750]103                }
[1291]104
[1328]105                scene->GetRoot()->UpdateBox();
[750]106        }
107
[840]108        if (1)
109        {
[1221]110                // plane separating view space regions
[1135]111                const Vector3 scale(1.0f, 0.0, 0);
[750]112
[840]113                Vector3 pt = sceneBox.Min() + scale * (sceneBox.Max() - sceneBox.Min());
[750]114
[840]115                Plane3 cuttingPlane(Vector3(1, 0, 0), pt);
116                Mesh *planeMesh = new Mesh();
[1291]117
[840]118                Polygon3 *poly = sceneBox.CrossSection(cuttingPlane);
119                IncludePolyInMesh(*poly, *planeMesh);
[1291]120
[840]121                planeMesh->Preprocess();
[1291]122
[840]123                MeshInstance *planeMi = new MeshInstance(planeMesh);
[1328]124                scene->GetRoot()->mGeometry.push_back(planeMi);
[840]125        }       
[750]126}
127
128
[372]129Preprocessor::Preprocessor():
130mKdTree(NULL),
[409]131mBspTree(NULL),
[445]132mVspBspTree(NULL),
[1264]133mVspTree(NULL),
[1279]134mHierarchyManager(NULL),
[1002]135mViewCellsManager(NULL),
[1251]136mRenderSimulator(NULL),
[1279]137mPass(0),
[1328]138mRayCastMethod(0),
139mSceneGraph(NULL)
[308]140{
[1004]141        Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
[538]142 
[840]143        // renderer will be constructed when the scene graph and viewcell manager will be known
144        renderer = NULL;
[496]145 
[1004]146        Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
[1379]147        Environment::GetSingleton()->GetBoolValue("Preprocessor.loadMeshes", mLoadMeshes);
[1004]148        Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
149        Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
150        Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
151        Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
[1221]152        Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", mRayCastMethod);
[1291]153
[871]154        char buffer[256];
[1004]155        Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile",  buffer);
[1415]156   
[871]157        mVisibilityFileName = buffer;
[1004]158        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter );
159        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
[904]160                                                          mApplyVisibilitySpatialFilter );
[1004]161        Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth);
[878]162
[840]163        Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
[1379]164        Debug << "load meshes: " << mLoadMeshes << endl;
[1344]165
166        if (mRayCastMethod == 0)
167                cout << "ray cast method: internal" << endl;
168        else
169                cout << "ray cast method: intel" << endl;
[372]170}
171
172
173Preprocessor::~Preprocessor()
174{
[1291]175        cout << "cleaning up" << endl;
[752]176
[1291]177        cout << "Deleting view cells manager ... \n";
178        DEL_PTR(mViewCellsManager);
179        cout << "done.\n";
[752]180
[1291]181        cout << "Deleting bsp tree ... \n";
182        DEL_PTR(mBspTree);
183        cout << "done.\n";
[752]184
[1291]185        cout << "Deleting kd tree...\n";
186        DEL_PTR(mKdTree);
187        cout << "done.\n";
[752]188
[1291]189        cout << "Deleting vsp tree...\n";
190        DEL_PTR(mVspTree);
191        cout << "done.\n";
[1145]192
[1291]193        cout << "Deleting hierarchy manager...\n";
194        DEL_PTR(mHierarchyManager);
195        cout << "done.\n";
[1002]196
[1291]197        cout << "Deleting vspbsp tree...\n";
198        DEL_PTR(mVspBspTree);
199        cout << "done.\n";
[1002]200
[1291]201        cout << "Deleting scene graph...\n";
202        DEL_PTR(mSceneGraph);
203        cout << "done.\n";
204
205        DEL_PTR(mRenderSimulator);
206        DEL_PTR(renderer);
[372]207}
208
[387]209int
[419]210SplitFilenames(const string str, vector<string> &filenames)
[387]211{
212        int pos = 0;
213
214        while(1) {
[469]215                int npos = (int)str.find(';', pos);
[387]216               
217                if (npos < 0 || npos - pos < 1)
218                        break;
219                filenames.push_back(string(str, pos, npos - pos));
220                pos = npos + 1;
221        }
222       
223        filenames.push_back(string(str, pos, str.size() - pos));
[440]224        return (int)filenames.size();
[387]225}
226
[750]227
[372]228bool
229Preprocessor::LoadScene(const string filename)
230{
[925]231        // use leaf nodes of the original spatial hierarchy as occludees
[508]232        mSceneGraph = new SceneGraph;
[372]233 
[508]234        Parser *parser;
[387]235        vector<string> filenames;
[1404]236        const int files = SplitFilenames(filename, filenames);
[712]237        cout << "number of input files: " << files << endl;
[387]238        bool result = false;
[1344]239
240        // root for different files
241        mSceneGraph->SetRoot(new SceneGraphNode());
242
[1404]243        // intel ray caster can only trace triangles
244        vector<FaceParentInfo> *fi = (mRayCastMethod == Preprocessor::INTEL_RAYCASTER) ?
245                &mFaceParents : NULL;
246
[387]247        if (files == 1) {
248               
249                if (strstr(filename.c_str(), ".x3d"))
[749]250                  parser = new X3dParser;
[387]251                else
[811]252                  if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb"))
[749]253                        parser = new PlyParser;
[1221]254                  else if (strstr(filename.c_str(), ".obj"))
255                          parser = new ObjParser;
256                  else
257                          parser = new UnigraphicsParser;
[372]258
[1328]259                cout << filename << endl;
[1404]260               
261                result = parser->ParseFile(
[1328]262                                filename,
[1344]263                                mSceneGraph->GetRoot(),
[1379]264                                mLoadMeshes,
[1404]265                                fi);
266                       
[387]267                delete parser;
[372]268
[1404]269        }
270        else {
271                vector<string>::const_iterator fit, fit_end = filenames.end();
[1344]272               
[1404]273                for (fit = filenames.begin(); fit != fit_end; ++ fit)
274                {
275                        const string filename = *fit;
[1328]276
[1404]277                        cout << "parsing file " << filename.c_str() << endl;
278                        if (strstr(filename.c_str(), ".x3d"))
279                                parser = new X3dParser;
280                        else
281                                parser = new UnigraphicsParser;
282
283                        SceneGraphNode *node = new SceneGraphNode();
284                        const bool success = parser->ParseFile(
285                                filename,
286                                node,
287                                mLoadMeshes,
288                                fi);
289
290                        if (success)
291                        {
292                                mSceneGraph->GetRoot()->mChildren.push_back(node);
293                                result = true; // at least one file parsed
294                        }
295
296                        delete parser;
[387]297                }
298        }
[1344]299       
[752]300        if (result)
[1344]301        {
[752]302                // HACK
[1020]303                if (ADDITIONAL_GEOMETRY_HACK)
304                        AddGeometry(mSceneGraph);
[1328]305
[1020]306                mSceneGraph->AssignObjectIds();
[1344]307 
[1020]308                int intersectables, faces;
309                mSceneGraph->GetStatistics(intersectables, faces);
[1344]310 
[1020]311                cout<<filename<<" parsed successfully."<<endl;
312                cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
313                cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
314                mSceneGraph->CollectObjects(&mObjects);
[1328]315                mSceneGraph->GetRoot()->UpdateBox();
[1344]316 
[1020]317                if (0)
318                {
319                        Exporter *exporter = Exporter::GetExporter("testload.x3d");
320                        if (exporter)
321                        {
322                                exporter->ExportGeometry(mObjects);
323                                delete exporter;
324                        }
325                }
[387]326        }
[1328]327
[492]328        return result;
[372]329}
330
331bool
332Preprocessor::ExportPreprocessedData(const string filename)
333{
[871]334 
[931]335  mViewCellsManager->ExportViewCells(filename, true, mObjects);
[871]336 
337  return true;
[372]338}
339
340bool
[871]341Preprocessor::PostProcessVisibility()
342{
343 
[904]344  if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
[997]345        cout<<"Applying visibility filter ...";
[1002]346        cout<<"filter width = " << mVisibilityFilterWidth << endl;
[904]347       
[1002]348        if (!mViewCellsManager)
[1199]349          return false;
350       
[871]351        mViewCellsManager->ApplyFilter(mKdTree,
[904]352                                                                   mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
353                                                                   mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
[997]354        cout << "done." << endl;
[871]355  }
356 
357  // export the preprocessed information to a file
358  if (mExportVisibility)
359        ExportPreprocessedData(mVisibilityFileName);
360 
361  return true;
362}
363
364
365bool
[372]366Preprocessor::BuildKdTree()
367{
368  mKdTree = new KdTree;
[1344]369
[372]370  // add mesh instances of the scene graph to the root of the tree
371  KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
[1344]372       
[372]373  mSceneGraph->CollectObjects(&root->mObjects);
[1344]374 
[1415]375  const long startTime = GetTime();
[1201]376  cout << "building kd tree ... " << endl;
[1344]377
[372]378  mKdTree->Construct();
[1344]379
[1415]380  cout << "finished kd tree construction in " << TimeDiff(startTime, GetTime()) * 1e-3
381           << " secs " << endl;
[1344]382
[372]383  return true;
384}
385
[1415]386
[372]387void
388Preprocessor::KdTreeStatistics(ostream &s)
389{
390  s<<mKdTree->GetStatistics();
391}
392
393void
394Preprocessor::BspTreeStatistics(ostream &s)
395{
396        s << mBspTree->GetStatistics();
397}
398
399bool
400Preprocessor::Export( const string filename,
[492]401                                          const bool scene,
402                                          const bool kdtree,
403                                          const bool bsptree
404                                          )
[372]405{
406  Exporter *exporter = Exporter::GetExporter(filename);
407       
408  if (exporter) {
[1405]409    if (0 && scene)
[1328]410      exporter->ExportScene(mSceneGraph->GetRoot());
[372]411
[1405]412    if (0 && kdtree) {
[372]413      exporter->SetWireframe();
414      exporter->ExportKdTree(*mKdTree);
415    }
416
[1405]417        if (0 && bsptree) {
[372]418                //exporter->SetWireframe();
419                exporter->ExportBspTree(*mBspTree);
420        }
421
422    delete exporter;
423    return true;
424  }
425
426  return false;
427}
[429]428
[508]429
[463]430bool Preprocessor::PrepareViewCells()
431{
[577]432        //-- parse view cells construction method
[1004]433        Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
[577]434        char buf[100];
435       
436        if (mLoadViewCells)
[997]437        {       
[1004]438                Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
[1264]439                cout << "loading view cells from " << buf << endl;
[1004]440                mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, true);
[577]441        }
442        else
443        {
[1288]444                // parse type of view cell container
[1004]445                Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);             
[1002]446            mViewCellsManager = CreateViewCellsManager(buf);
[1112]447
448                // default view space is the extent of the scene
449                mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
[577]450        }
[1112]451       
[997]452        //-- parameters for render heuristics evaluation
[473]453        float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
454
[1004]455        Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
456        Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
457        Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
[694]458       
[473]459        mRenderSimulator =
460                new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
[440]461
[480]462        mViewCellsManager->SetRenderer(mRenderSimulator);
[440]463
[574]464
[538]465        if (mUseGlRenderer || mUseGlDebugger)
[540]466        {
467                // NOTE: render texture should be power of 2 and square
468                // renderer must be initialised
[1145]469                // $$matt
470//              renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
[556]471                //              renderer->makeCurrent();
[746]472               
[540]473        }
[496]474       
[463]475        return true;
[490]476}
477
[1292]478 
479bool
480Preprocessor::ConstructViewCells()
481{
482  // if not already loaded, construct view cells from file
483  if (!mLoadViewCells) {
484        mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
485       
486        // construct view cells using it's own set of samples
487        mViewCellsManager->Construct(this);
488       
489        //-- several visualizations and statistics
490        Debug << "view cells construction finished: " << endl;
491        mViewCellsManager->PrintStatistics(Debug);
492  }
493  return true;
494}
[490]495
[1294]496
[1288]497HierarchyManager *Preprocessor::CreateHierarchyManager(const char *name)
498{
499        HierarchyManager *hierarchyManager;
500
501        if (strcmp(name, "osp") == 0)
502        {
[1291]503                Debug << "hierarchy manager: osp" << endl;
[1288]504                // HACK for testing if per kd evaluation works!!
[1293]505                const bool ishack = false;
[1288]506                if (ishack)
507                        hierarchyManager = new HierarchyManager(mVspTree, mKdTree);
508                else
509                        hierarchyManager = new HierarchyManager(mVspTree, HierarchyManager::KD_BASED_OBJ_SUBDIV);
510        }
511        else if (strcmp(name, "bvh") == 0)
512        {
[1291]513                Debug << "hierarchy manager: bvh" << endl;
[1288]514                hierarchyManager = new HierarchyManager(mVspTree, HierarchyManager::BV_BASED_OBJ_SUBDIV);
515        }
[1291]516        else // only view space partition
517        {
518                Debug << "hierarchy manager: obj" << endl;
519                hierarchyManager = new HierarchyManager(mVspTree, HierarchyManager::NO_OBJ_SUBDIV);
520        }
[1288]521
522        return hierarchyManager;
523}
524
525
[575]526ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
527{
[1264]528        ViewCellsTree *vcTree = new ViewCellsTree;
529
[575]530        if (strcmp(name, "kdTree") == 0)
531        {
[1264]532                mViewCellsManager = new KdViewCellsManager(vcTree, mKdTree);
[575]533        }
534        else if (strcmp(name, "bspTree") == 0)
535        {
536                Debug << "view cell type: Bsp" << endl;
537
[577]538                mBspTree = new BspTree();
[1264]539                mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
[575]540        }
541        else if (strcmp(name, "vspBspTree") == 0)
542        {
543                Debug << "view cell type: VspBsp" << endl;
544
[1004]545                mVspBspTree = new VspBspTree();
[1264]546                mViewCellsManager = new VspBspViewCellsManager(vcTree, mVspBspTree);
[575]547        }
[1006]548        else if (strcmp(name, "vspOspTree") == 0)
[575]549        {
[1022]550                mVspTree = new VspTree();
[1288]551                char buf[100];         
552                Environment::GetSingleton()->GetStringValue("Hierarchy.type", buf);     
[1022]553
[1288]554                HierarchyManager *mHierarchyManager = CreateHierarchyManager(buf);
[1279]555                mViewCellsManager = new VspOspViewCellsManager(vcTree, mHierarchyManager);
[575]556        }
557        else if (strcmp(name, "sceneDependent") == 0)
558        {
[1143]559                Debug << "view cell type: Bsp" << endl;
560
[575]561                //TODO
562                mBspTree = new BspTree();
[1264]563                mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
[575]564        }
565        else
566        {
[664]567                cerr << "Wrong view cells type " << name << "!!!" << endl;
[575]568                exit(1);
569        }
570
571        return mViewCellsManager;
572}
573
574
[491]575// use ascii format to store rays
576#define USE_ASCII 0
577
578
[1145]579static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
[490]580{
581        return obj1->mId < obj2->mId;
582}
583
584
[1414]585bool Preprocessor::LoadKdTree(const string filename)
[1197]586{
[1414]587        mKdTree = new KdTree();
588        return mKdTree->LoadBinTree(filename.c_str(), mObjects);
[1197]589}
590
[1414]591
592bool Preprocessor::ExportKdTree(const string filename)
[1197]593{
[1414]594        return mKdTree->ExportBinTree(filename.c_str());
[1197]595}
596
597
[490]598bool Preprocessor::LoadSamples(VssRayContainer &samples,
599                                                           ObjectContainer &objects) const
600{
601        std::stable_sort(objects.begin(), objects.end(), ilt);
602        char fileName[100];
[1004]603        Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
[490]604       
[491]605    Vector3 origin, termination;
606        // HACK: needed only for lower_bound algorithm to find the
607        // intersected objects
608        MeshInstance sObj(NULL);
609        MeshInstance tObj(NULL);
[490]610
[491]611#if USE_ASCII
[656]612        ifstream samplesIn(fileName);
[490]613        if (!samplesIn.is_open())
614                return false;
615
616        string buf;
617        while (!(getline(samplesIn, buf)).eof())
618        {
[491]619                sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
[490]620                           &origin.x, &origin.y, &origin.z,
[491]621                           &termination.x, &termination.y, &termination.z,
622                           &(sObj.mId), &(tObj.mId));
[490]623               
[491]624                Intersectable *sourceObj = NULL;
625                Intersectable *termObj = NULL;
626               
627                if (sObj.mId >= 0)
[490]628                {
629                        ObjectContainer::iterator oit =
[491]630                                lower_bound(objects.begin(), objects.end(), &sObj, ilt);
631                        sourceObj = *oit;
632                }
633               
634                if (tObj.mId >= 0)
635                {
636                        ObjectContainer::iterator oit =
637                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
638                        termObj = *oit;
639                }
[490]640
[491]641                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
642        }
643#else
644        ifstream samplesIn(fileName, ios::binary);
645        if (!samplesIn.is_open())
646                return false;
647
648        while (1)
649        {
650                 samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
651                 samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
652                 samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
653                 samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
654               
655                 if (samplesIn.eof())
656                        break;
657
658                Intersectable *sourceObj = NULL;
659                Intersectable *termObj = NULL;
660               
661                if (sObj.mId >= 0)
662                {
663                        ObjectContainer::iterator oit =
664                                lower_bound(objects.begin(), objects.end(), &sObj, ilt);
665                        sourceObj = *oit;
[490]666                }
[491]667               
668                if (tObj.mId >= 0)
[490]669                {
[491]670                        ObjectContainer::iterator oit =
671                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
672                        termObj = *oit;
[490]673                }
[491]674
675                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
[490]676        }
[491]677
678#endif
[490]679        samplesIn.close();
680
681        return true;
682}
683
[508]684
685bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
[490]686{
[491]687        char fileName[100];
[1004]688        Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
[491]689       
[490]690
691        VssRayContainer::const_iterator it, it_end = samples.end();
692       
[491]693#if USE_ASCII
694        ofstream samplesOut(fileName);
[490]695        if (!samplesOut.is_open())
696                return false;
697
698        for (it = samples.begin(); it != it_end; ++ it)
699        {
700                VssRay *ray = *it;
[491]701                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
702                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;       
703
[490]704                samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
705                                   << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
[491]706                                   << sourceid << " " << termid << "\n";
[490]707        }
[491]708#else
709        ofstream samplesOut(fileName, ios::binary);
710        if (!samplesOut.is_open())
711                return false;
712
713        for (it = samples.begin(); it != it_end; ++ it)
714        {       
715                VssRay *ray = *it;
716                Vector3 origin(ray->GetOrigin());
717                Vector3 termination(ray->GetTermination());
718               
719                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
720                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;               
721
722                samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
723                samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
724                samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
725                samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
726    }
727#endif
[490]728        samplesOut.close();
[1194]729
[490]730        return true;
731}
[563]732
[1020]733bool Preprocessor::GenerateRays(const int number,
734                                                                const int sampleType,
735                                                                SimpleRayContainer &rays)
736{
737        const int startSize = (int)rays.size();
738        SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
[860]739
[1020]740        if (!strategy)
[1381]741        {
[1020]742                return false;
[1381]743        }
[1020]744
745        for (int i=0; (int)rays.size() - startSize < number; ++ i)
746        {
747                SimpleRay newRay;
748
[1381]749                if (strategy->GenerateSample(newRay))
750                {
751#if 1
[1020]752                        rays.AddRay(newRay);
[1381]753#else
754                        GenerateRayBundle(rays, newRay, 16, 0);
755#endif
756                }       
[1020]757        }
758
759        delete strategy;
760    return true;
[878]761}
[1020]762
763
764SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId) const
765{
766        switch (strategyId)
767        {
768        case OBJECT_BASED_DISTRIBUTION:
769                return new ObjectBasedDistribution(*this);
770        case OBJECT_DIRECTION_BASED_DISTRIBUTION:
771                return new ObjectDirectionBasedDistribution(*this);
772        case DIRECTION_BASED_DISTRIBUTION:
773                return new DirectionBasedDistribution(*this);
774        case DIRECTION_BOX_BASED_DISTRIBUTION:
775                return new DirectionBoxBasedDistribution(*this);
776        case SPATIAL_BOX_BASED_DISTRIBUTION:
777                return new SpatialBoxBasedDistribution(*this);
[1021]778        //case OBJECTS_INTERIOR_DISTRIBUTION:
779        //      return new ObjectsInteriorDistribution(*this);
[1020]780        default: // no valid strategy
[1279]781                Debug << "warning: no valid sampling strategy" << endl;
[1020]782                return NULL;
783        }
[1221]784
[1020]785        // should never come here
786        return NULL;
787}
788
789
[1221]790bool Preprocessor::InitRayCast(const string externKdTree)
791{
[1415]792        bool loadKdTree, exportKdTree;
793
794        Environment::GetSingleton()->GetBoolValue("Preprocessor.loadKdTree", loadKdTree);
795        Environment::GetSingleton()->GetBoolValue("Preprocessor.exportKdTree", exportKdTree);
796
797        char kdtreename[100];
798        Environment::GetSingleton()->GetStringValue("Preprocessor.kdTreeFilename", kdtreename);
799
800       
801        if (!loadKdTree)
802        {
803                /////////////////
804                //-- build new kd tree from scene geometry
805
806                BuildKdTree();
807                KdTreeStatistics(cout);
808        }
809        else
810        {
811                const long startTime = GetTime();
812                cout << "loading kd tree file " << kdtreename << " ... ";
813
814                if (!LoadKdTree(kdtreename))
815                {
816                        cout << "error loading kd tree with filename " << kdtreename << endl;
817                        return false;
818                }
819                cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
820
821                if (0)
822                {
823                        Exporter *exporter = Exporter::GetExporter("dummykd.x3d");
824                       
825                        if (exporter)
826                        {
827                                exporter->ExportKdTree(*mKdTree, true);
828                                delete exporter;
829                        }
830                }
831        }
832
833        if (exportKdTree)
834        {
835                const long startTime = GetTime();
836                cout << "exporting kd tree ... ";
837                if (!ExportKdTree(kdtreename))
838                {
839                        cout << " error exporting kd tree with filename " << kdtreename << endl;
840                }
841                else
842                {
843                        cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
844                }
845        }
846
[1221]847        switch (mRayCastMethod) // use intel ray tracing
848        {
849        case INTEL_RAYCASTER:
[1251]850#ifdef GTP_INTERNAL
[1344]851          cout<<"Ray Cast file: " << externKdTree << endl;
[1272]852          return mlrtaLoadAS(externKdTree.c_str());
[1232]853#endif
[1221]854        case INTERNAL_RAYCASTER:
855        default:
856                break;
857        }
858
859        return true;
[1020]860}
[1221]861
[1251]862
863int Preprocessor::CastIntelDoubleRay(
864                                                                         const Vector3 &viewPoint,
865                                                                         const Vector3 &direction,
866                                                                         const float probability,
867                                                                         VssRayContainer &vssRays,
868                                                                         const AxisAlignedBox3 &box
869                                                                         )
870{
[1294]871#ifdef GTP_INTERNAL
[1344]872        //cout << "intel ray" << endl;
[1251]873        VssRay *vssRay  = NULL;
874        int hits = 0;
[1293]875        int hittriangle;
[1251]876        Vector3 pointA, pointB;
[1292]877        Vector3 normalA, normalB;
878        Intersectable *objectA = NULL, *objectB = NULL;
879        float dist;
880        double normal[3];
[1294]881
[1292]882        hittriangle = mlrtaIntersectAS(&viewPoint.x,
[1344]883                &direction.x,
884                normal,
885                dist);
[1294]886
[1344]887        if (hittriangle != -1 ) {
888                if (hittriangle >= mFaceParents.size())
889                        cerr<<"Warning: triangle index out of range! "<<hittriangle<<endl;
890                else {
891                        objectA = mFaceParents[hittriangle].mObject;
892                        normalA = Vector3(normal[0], normal[1], normal[2]);
893                        // Get the normal of that face
894                        //              Mesh *mesh = ((MeshInstance *)objectA)->GetMesh();
895                        //              normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
896                        //-rays[index+i].mDirection; // $$ temporary
897                        pointA = viewPoint + direction*dist;
898                }
[1292]899        }
[1251]900
[1344]901
[1292]902        Vector3 dir = -direction;
903        hittriangle = mlrtaIntersectAS(&viewPoint.x,
[1344]904                &dir.x,
905                normal,
906                dist);
[1293]907
[1344]908        if (hittriangle != -1 ) {
909                if (hittriangle >= mFaceParents.size())
910                        cerr<<"Warning: triangle index out of range! "<<hittriangle<<endl;
911                else {
912                        objectB = mFaceParents[hittriangle].mObject;
913                        normalB = Vector3(normal[0], normal[1], normal[2]);
914                        // Get the normal of that face
915                        //              Mesh *mesh = ((MeshInstance *)objectB)->GetMesh();
916                        //              normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
917                        //-rays[index+i].mDirection; // $$ temporary
918                        pointB = viewPoint + dir * dist;
919                }
[1251]920        }
[1279]921
[1292]922        return ProcessRay(viewPoint,
[1344]923                direction,
924                objectA, pointA, normalA,
925                objectB, pointB, normalB,
926                probability,
927                vssRays,
928                box
929                );
[1294]930#else
931        return -1;
932#endif
[1221]933}
[1251]934
935
936Intersectable *Preprocessor::CastIntelSingleRay(const Vector3 &viewPoint,
937                                                                                                const Vector3 &direction,
938                                                                                                //const float probability,
939                                                                                                Vector3 &tPoint,
940                                                                                                const AxisAlignedBox3 &box
941                                                                                                )
942{
943        AxisAlignedBox3 sbox = box;
944        sbox.Enlarge(Vector3(-Limits::Small));
945
946        if (!sbox.IsInside(viewPoint))
947                return 0;
948       
[1294]949#ifdef GTP_INTERNAL
[1251]950        float pforg[3];
951        float pfdir[3];
[1272]952        double pfnorm[3];
[1251]953
954        pforg[0] = viewPoint[0]; pforg[1] = viewPoint[1]; pforg[2] = viewPoint[2];
955        pfdir[0] = direction[0]; pfdir[1] = direction[1]; pfdir[2] = direction[2];
956
[1294]957        float dist;
[1251]958        const int hittriangle = mlrtaIntersectAS(pforg, pfdir, pfnorm, dist);
959
960        if (hittriangle == -1)
[1292]961          {
[1251]962                static Ray ray;
963                SetupRay(ray, viewPoint, direction);
[1292]964               
[1251]965                float tmin = 0, tmax;
966                if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
[1292]967                  {
[1251]968                        tPoint = ray.Extrap(tmax);
[1292]969                  }
970               
[1251]971                return NULL;
[1292]972          }
973        else {
974          if (hittriangle >= mFaceParents.size()) {
[1344]975                cerr<<"Warning: triangle index out of range! "<<hittriangle<<endl;
[1292]976                return NULL;
977          }
978          else {
979               
[1251]980                tPoint[0] = pforg[0] + pfdir[0] * dist;
981                tPoint[1] = pforg[1] + pfdir[1] * dist;
982                tPoint[2] = pforg[2] + pfdir[2] * dist;
[1292]983               
[1281]984                return mFaceParents[hittriangle].mObject;
[1292]985          }
[1251]986        }
[1294]987
988#else
989        const int hittriangle = -1;
990        return NULL;
991#endif
992
[1251]993}
994
995
996int Preprocessor::CastInternalRay(
997                                                                  const Vector3 &viewPoint,
998                                                                  const Vector3 &direction,
999                                                                  const float probability,
1000                                                                  VssRayContainer &vssRays,
1001                                                                  const AxisAlignedBox3 &box
1002                                                                  )
1003{
[1359]1004  //cout << "internal ray" << endl;
[1272]1005  int hits = 0;
1006  static Ray ray;
1007  Intersectable *objectA, *objectB;
1008  Vector3 pointA, pointB;
[1251]1009
[1272]1010  //  AxisAlignedBox3 box = Union(mKdTree->GetBox(), mViewCellsManager->GetViewSpaceBox());
1011  AxisAlignedBox3 sbox = box;
1012  sbox.Enlarge(Vector3(-Limits::Small));
1013  if (!sbox.IsInside(viewPoint))
1014        return 0;
[1251]1015       
[1272]1016  SetupRay(ray, viewPoint, direction);
1017  ray.mFlags &= ~Ray::CULL_BACKFACES;
[1251]1018
[1272]1019  // cast ray to KD tree to find intersection with other objects
1020  float bsize = Magnitude(box.Size());
1021 
1022 
1023  if (mKdTree->CastRay(ray)) {
1024        objectA = ray.intersections[0].mObject;
1025        pointA = ray.Extrap(ray.intersections[0].mT);
1026        if (mDetectEmptyViewSpace)
1027          if (DotProd(ray.intersections[0].mNormal, direction) >= 0) {
1028                // discard the sample
1029                return 0;
1030          }
1031       
1032  } else {
1033        objectA = NULL;
1034        // compute intersection with the scene bounding box
1035        float tmin, tmax;
1036        if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
1037          pointA = ray.Extrap(tmax);
[1251]1038        else
[1272]1039          return 0;
1040  }
[1251]1041
[1272]1042  SetupRay(ray, viewPoint, -direction);
1043  ray.mFlags &= ~Ray::CULL_BACKFACES;
1044 
1045  if (mKdTree->CastRay(ray)) {
1046        objectB = ray.intersections[0].mObject;
1047        pointB = ray.Extrap(ray.intersections[0].mT);
1048        if (mDetectEmptyViewSpace)
1049          if (DotProd(ray.intersections[0].mNormal, direction) <= 0) {
1050                // discard the sample
1051                return 0;
1052          }
1053  } else {
1054        objectB = NULL;
1055        float tmin, tmax;
1056        if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
1057          pointB = ray.Extrap(tmax);
[1251]1058        else
[1272]1059          return 0;
1060  }
[1251]1061 
1062 
[1272]1063  VssRay *vssRay  = NULL;
1064  bool validSample = (objectA != objectB);
1065  if (validSample) {
1066        if (objectA) {
1067          vssRay = new VssRay(pointB,
1068                                                  pointA,
1069                                                  objectB,
1070                                                  objectA,
1071                                                  mPass,
1072                                                  probability
1073                                                  );
1074          vssRays.push_back(vssRay);
[1344]1075          //cout << "ray: " << *vssRay << endl;
[1272]1076          hits ++;
[1251]1077        }
1078       
[1272]1079        if (objectB) {
1080          vssRay = new VssRay(pointA,
1081                                                  pointB,
1082                                                  objectA,
1083                                                  objectB,
1084                                                  mPass,
1085                                                  probability
1086                                                  );
1087          vssRays.push_back(vssRay);
[1344]1088          //cout << "ray: " << *vssRay << endl;
[1272]1089          hits ++;
[1251]1090        }
[1272]1091  }
1092 
1093  return hits;
[1251]1094}
1095
1096
[1281]1097int
1098Preprocessor::ProcessRay(
[1283]1099                                                 const Vector3 &viewPoint,
1100                                                 const Vector3 &direction,
[1281]1101                                                 Intersectable *objectA,
1102                                                 Vector3 &pointA,
[1283]1103                                                 const Vector3 &normalA,
[1281]1104                                                 Intersectable *objectB,
1105                                                 Vector3 &pointB,
[1283]1106                                                 const Vector3 &normalB,
[1281]1107                                                 const float probability,
1108                                                 VssRayContainer &vssRays,
1109                                                 const AxisAlignedBox3 &box
1110                                                 )
1111{
1112  int hits=0;
[1292]1113#if DEBUG_RAYCAST
1114  Debug<<"PR ";
1115#endif
[1281]1116  if (objectA == NULL && objectB == NULL)
1117        return 0;
[1292]1118 
[1281]1119  AxisAlignedBox3 sbox = box;
1120  sbox.Enlarge(Vector3(-Limits::Small));
1121
1122  if (!sbox.IsInside(viewPoint))
1123        return 0;
1124
1125  if (objectA == NULL) {
1126        // compute intersection with the scene bounding box
1127        static Ray ray;
1128        SetupRay(ray, viewPoint, direction);
1129       
1130        float tmin, tmax;
1131        if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
1132          pointA = ray.Extrap(tmax);
1133        else
1134          return 0;
1135  } else {
1136        if (mDetectEmptyViewSpace)
1137          if (DotProd(normalA, direction) >= 0) {
1138                // discard the sample
1139                return 0;
1140          }
1141  }
1142
1143  if (objectB == NULL) {
1144        // compute intersection with the scene bounding box
1145        static Ray ray;
1146        SetupRay(ray, viewPoint, -direction);
1147
1148        float tmin, tmax;
1149        if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
1150          pointB = ray.Extrap(tmax);
1151        else
1152          return 0;
1153  } else {
1154        if (mDetectEmptyViewSpace)
1155          if (DotProd(normalB, direction) <= 0) {
1156                // discard the sample
1157                return 0;
1158          }
1159  }
1160 
1161  VssRay *vssRay  = NULL;
1162  bool validSample = (objectA != objectB);
1163  if (validSample) {
1164        if (objectA) {
1165          vssRay = new VssRay(pointB,
1166                                                  pointA,
1167                                                  objectB,
1168                                                  objectA,
1169                                                  mPass,
1170                                                  probability
1171                                                  );
1172          vssRays.push_back(vssRay);
[1344]1173          //cout << "ray: " << *vssRay << endl;
[1281]1174          hits ++;
1175        }
1176       
1177        if (objectB) {
1178          vssRay = new VssRay(pointA,
1179                                                  pointB,
1180                                                  objectA,
1181                                                  objectB,
1182                                                  mPass,
1183                                                  probability
1184                                                  );
1185          vssRays.push_back(vssRay);
[1344]1186          //cout << "ray: " << *vssRay << endl;
[1281]1187          hits ++;
1188        }
1189  }
[1292]1190
1191
[1281]1192  return hits;
1193}
1194
1195void
1196Preprocessor::CastRays16(const int index,
1197                                                 SimpleRayContainer &rays,
1198                                                 VssRayContainer &vssRays,
1199                                                 const AxisAlignedBox3 &sbox)
1200{
1201  int i;
[1344]1202  const int num = 16;
[1292]1203
1204#if DEBUG_RAYCAST
1205  Debug<<"C16 "<<flush;
1206#endif
[1281]1207  if (mRayCastMethod == INTEL_RAYCASTER) {
1208#ifdef GTP_INTERNAL
1209
1210  int forward_hit_triangles[16];
1211  float forward_dist[16];
1212
1213  int backward_hit_triangles[16];
1214  float backward_dist[16];
[1292]1215
1216
[1281]1217  Vector3 min = sbox.Min();
1218  Vector3 max = sbox.Max();
[1344]1219 
[1281]1220  for (i=0; i < num; i++) {
1221        mlrtaStoreRayAS16(&rays[index + i].mOrigin.x,
1222                                          &rays[index + i].mDirection.x,
1223                                          i);
1224  }
1225 
1226  mlrtaTraverseGroupAS16(&min.x,
1227                                                 &max.x,
1228                                                 forward_hit_triangles,
1229                                                 forward_dist);
1230
1231  for (i=0; i < num; i++) {
[1292]1232        Vector3 dir = -rays[index + i].mDirection;
[1281]1233        mlrtaStoreRayAS16(&rays[index+i].mOrigin.x,
1234                                          &dir.x,
1235                                          i);
1236  }
1237 
1238  mlrtaTraverseGroupAS16(&min.x,
1239                                                 &max.x,
1240                                                 backward_hit_triangles,
1241                                                 backward_dist);
1242 
1243
1244  for (i=0; i < num; i++) {
1245        Intersectable *objectA = NULL, *objectB = NULL;
1246        Vector3 pointA, pointB;
1247        Vector3 normalA, normalB;
[1292]1248
[1344]1249        if (forward_hit_triangles[i] != -1 ) {
[1292]1250          if (forward_hit_triangles[i] >= mFaceParents.size())
[1344]1251                cerr<<"Warning: triangle index out of range! "<<forward_hit_triangles[i]<<endl;
1252          else {
[1292]1253                objectA = mFaceParents[forward_hit_triangles[i]].mObject;
1254                // Get the normal of that face
[1344]1255                normalA = objectA->GetNormal(mFaceParents[forward_hit_triangles[i]].mFaceIndex);
[1292]1256                //-rays[index+i].mDirection; // $$ temporary
1257                pointA = rays[index+i].Extrap(forward_dist[i]);
1258          }
[1281]1259        }
[1344]1260         
[1281]1261        if (backward_hit_triangles[i]!=-1) {
[1292]1262          if (backward_hit_triangles[i] >= mFaceParents.size())
[1344]1263                cerr<<"Warning: triangle  index out of range! "<<backward_hit_triangles[i]<<endl;
1264          else {
[1292]1265                objectB = mFaceParents[backward_hit_triangles[i]].mObject;
[1344]1266                normalB = objectB->GetNormal(mFaceParents[forward_hit_triangles[i]].mFaceIndex);
[1292]1267               
[1344]1268                // normalB = rays[index+i].mDirection; // $$ temporary
[1292]1269                pointB = rays[index+i].Extrap(-backward_dist[i]);
1270          }
1271        }
[1344]1272 
[1281]1273        ProcessRay(rays[index+i].mOrigin,
1274                           rays[index+i].mDirection,
1275                           objectA, pointA, normalA,
1276                           objectB, pointB, normalB,
1277                           rays[index+i].mPdf,
1278                           vssRays,
1279                           sbox
1280                           );
1281  }
[1344]1282 
[1281]1283#endif
[1292]1284 
[1281]1285  } else {
1286
1287        for (i=index; i < index + num; i++) {
1288          CastRay(rays[i].mOrigin,
1289                          rays[i].mDirection,
1290                          rays[i].mPdf,
1291                          vssRays,
1292                          sbox);
1293        }
1294  }
[1292]1295#if DEBUG_RAYCAST
1296  Debug<<"C16F\n"<<flush;
1297#endif
[1281]1298}
1299
1300void
1301Preprocessor::CastRays(
1302                                           SimpleRayContainer &rays,
1303                                           VssRayContainer &vssRays
1304                                           )
1305{
1306  long t1 = GetTime();
[1344]1307 
1308  for (int i = 0; i < (int)rays.size(); ) {
1309          // method only available for intel raycaster yet
1310        if (i + 16 < (int)rays.size()) {
1311
[1281]1312          CastRays16(
1313                                 i,
1314                                 rays,
1315                                 vssRays,
1316                                 mViewCellsManager->GetViewSpaceBox());
1317          i += 16;
1318        } else {
1319          CastRay(rays[i].mOrigin,
1320                          rays[i].mDirection,
1321                          rays[i].mPdf,
1322                          vssRays,
1323                          mViewCellsManager->GetViewSpaceBox());
1324          i++;
1325        }
1326        if (i % 10000 == 0)
1327          cout<<".";
1328  }
1329
1330  long t2 = GetTime();
1331  cout<<2*rays.size()/(1e3*TimeDiff(t1, t2))<<"M rays/s"<<endl;
1332}
1333
[1292]1334Intersectable *
1335Preprocessor::CastSimpleRay(
1336                                                        const Vector3 &viewPoint,
1337                                                        const Vector3 &direction,
1338                                                        const AxisAlignedBox3 &box,
1339                                                        Vector3 &point,
1340                                                        Vector3 &normal
1341                                                        )
1342{
1343  Intersectable *result = NULL;
1344  switch (mRayCastMethod)
1345        {
1346        case INTEL_RAYCASTER: {
[1294]1347         
[1292]1348          int hittriangle;
[1294]1349
1350#ifdef GTP_INTERNAL
1351          float dist;
[1292]1352          double n[3];
[1293]1353
[1292]1354          hittriangle = mlrtaIntersectAS(&viewPoint.x,
1355                                                                         &direction.x,
1356                                                                         n,
1357                                                                         dist);
[1293]1358
[1294]1359           if (hittriangle !=-1 ) {
[1292]1360                if (hittriangle >= mFaceParents.size())
[1344]1361                  cerr<<"Warning: triangle index out of range! "<<hittriangle<<endl;
[1292]1362                else {
1363                  result = mFaceParents[hittriangle].mObject;
1364                  normal = Vector3(n[0], n[1], n[2]);
1365                  point = viewPoint + direction*dist;
1366                }
1367          }
[1294]1368#else
[1344]1369          hittriangle = -1;
[1294]1370#endif
1371
[1292]1372          break;
1373        }
1374        case INTERNAL_RAYCASTER:
1375        default: {
1376          static Ray ray;
1377         
1378          ray.intersections.clear();
1379          // do not store anything else then intersections at the ray
1380          ray.Init(viewPoint, direction, Ray::LOCAL_RAY);
1381         
1382          ray.mFlags &= ~Ray::CULL_BACKFACES;
1383         
1384          if (mKdTree->CastRay(ray)) {
1385                result = ray.intersections[0].mObject;
1386                point = ray.Extrap(ray.intersections[0].mT);
1387                normal = ray.intersections[0].mNormal;
1388          }
1389          break;
1390        }
1391        }
1392  return result;
1393}
[1286]1394
[1292]1395int
1396Preprocessor::CastRay(
1397                                          const Vector3 &viewPoint,
1398                                          const Vector3 &direction,
1399                                          const float probability,
1400                                          VssRayContainer &vssRays,
1401                                          const AxisAlignedBox3 &box
1402                                          )
[1251]1403{
[1292]1404#if DEBUG_RAYCAST
1405  Debug<<"CR "<<flush;
1406#endif
1407  switch (mRayCastMethod)
[1251]1408        {
1409        case INTEL_RAYCASTER:
1410                return CastIntelDoubleRay(viewPoint, direction, probability, vssRays, box);
1411        case INTERNAL_RAYCASTER:
1412        default:
1413                return CastInternalRay(viewPoint, direction, probability, vssRays, box);
1414        }
[1381]1415#if DEBUG_RAYCAST       
[1292]1416  Debug<<"CRF "<<flush;
1417#endif 
[1251]1418}
1419
1420
[1381]1421bool Preprocessor::GenerateRayBundle(SimpleRayContainer &rayBundle,                                                                     
1422                                                                         const SimpleRay &mainRay,
1423                                                                         const int number,
1424                                                                         const int pertubType) const
1425{
1426        rayBundle.push_back(mainRay);
1427
1428        const float pertubOrigin = 10.0f;
1429        const float pertubDir = 0.0f;
1430
1431        for (int i = 0; i < number - 1; ++ i)
1432        {
1433                Vector3 pertub;
1434
1435                pertub.x = RandomValue(0.0f, pertubDir);
1436                pertub.y = RandomValue(0.0f, pertubDir);
1437                pertub.z = RandomValue(0.0f, pertubDir);
1438                const Vector3 newDir = mainRay.mDirection + pertub;
1439                //const Vector3 newDir = mainRay.mDirection;
1440
1441                pertub.x = RandomValue(0.0f, pertubOrigin);
1442                pertub.y = RandomValue(0.0f, pertubOrigin);
1443                pertub.z = RandomValue(0.0f, pertubOrigin);
1444                const Vector3 newOrigin = mainRay.mOrigin + pertub;
1445                //const Vector3 newOrigin = mainRay.mOrigin;
1446
1447                rayBundle.push_back(SimpleRay(newOrigin, newDir, 0));
1448        }
1449
1450        return true;
1451}
1452
1453
[1251]1454void Preprocessor::SetupRay(Ray &ray,
1455                                                        const Vector3 &point,
1456                                                        const Vector3 &direction
1457                                                        )
1458{
1459        ray.Clear();
1460        // do not store anything else then intersections at the ray
1461        ray.Init(point, direction, Ray::LOCAL_RAY);
1462}
1463
1464}
Note: See TracBrowser for help on using the repository browser.