Ignore:
Timestamp:
10/02/06 19:07:38 (18 years ago)
Author:
mattausch
Message:

updated view cells loading. probably no optimal for performance

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp

    r1486 r1551  
    260260{ 
    261261        // sort view cells to help associating view cells according to their id 
    262         stable_sort(mViewCells.begin(), mViewCells.end(), vlt); 
     262        //stable_sort(mViewCells.begin(), mViewCells.end(), vlt); 
    263263 
    264264        // not parsing view cells anymore 
     
    393393        { 
    394394                cout << "["; 
    395                 StartViewCellInterior(attributes); 
     395                StartViewCell(attributes, false); 
    396396        } 
    397397 
     
    399399        { 
    400400                cout << "l"; 
    401                 StartViewCellLeaf(attributes); 
     401                StartViewCell(attributes, true); 
    402402        } 
    403403} 
     
    464464 
    465465 
    466 void ViewCellsParseHandlers::StartViewCellPvs(ViewCell *viewCell,  
    467                                                                                           AttributeList&  attributes) 
    468 { 
    469         int len = attributes.getLength(); 
     466void ViewCellsParseHandlers::StartViewCellPvs(ObjectPvs &pvs, const char *ptr) 
     467{ 
     468        // handle obect indices 
    470469        vector<int> objIndices; 
    471  
    472         for (int i = 0; i < len; ++ i)  
    473         { 
    474                 string attrName(StrX(attributes.getName(i)).LocalForm()); 
    475                  
    476                 if (attrName == "pvs")  
    477                 { 
    478                         StrX attrValue(attributes.getValue(i)); 
     470        char *endptr; 
    479471                         
    480                         // handle coordIndex 
    481                         objIndices.clear(); 
    482                         const char *ptr = attrValue.LocalForm(); 
    483                         char *endptr; 
    484                          
    485                         while (1) 
    486                         { 
    487                                 const int index = strtol(ptr, &endptr, 10); 
    488  
    489                                 if (ptr == endptr) break; 
    490  
    491                                 objIndices.push_back(index); 
    492  
    493                                 ptr = endptr; 
    494                         } 
    495  
    496                         //TODO: find objects and add them to pvs 
    497                         // TODO: get view cell with specified id 
    498                         MeshInstance dummyInst(NULL); 
    499  
    500                         vector<int>::const_iterator it, it_end = objIndices.end(); 
    501                         for (it = objIndices.begin(); it != it_end; ++ it) 
    502                         { 
    503                                 const int objId = *it;   
    504                                 dummyInst.SetId(objId); 
    505  
    506                                 ObjectContainer::iterator oit = 
    507                                         lower_bound(mObjects->begin(),  
    508                                                                 mObjects->end(),  
    509                                                                 (Intersectable *)&dummyInst, ilt);       
    510                                                          
    511                                 if ((oit != mObjects->end()) && ((*oit)->GetId() == objId)) 
    512                                 { 
    513                                         // $$JB we should store a float a per object which corresponds 
    514                                         // to sumof pdfs, i.e. its relative visibility 
    515                                         // temporarily set to 1.0f 
    516                                         viewCell->GetPvs().AddSample(*oit, 1.0f);                                
    517                                 } 
    518                                 else 
    519                                 { 
    520                                         cout << "error: object with id " << objId << " does not exist" << endl; 
    521                                 } 
    522                         } 
    523                 } 
    524                 else if (attrName == "id")  
    525                 { 
    526                         StrX attrValue(attributes.getValue(i)); 
    527                          
    528                         const char *ptr = attrValue.LocalForm(); 
    529                         char *endptr = NULL; 
    530                         const int id = strtol(ptr, &endptr, 10); 
    531  
    532                         viewCell->SetId(id); 
    533                 } 
    534                 /*else if (attrName == "active") 
    535                 { 
    536                         StrX attrValue(attributes.getValue(i)); 
    537                          
    538                         const char *ptr = attrValue.LocalForm(); 
    539                         char *endptr = NULL; 
    540                         const bool isActive = (bool)strtol(ptr, &endptr, 10); 
    541  
    542                         if (isActive) 
    543                                 viewCell->SetActive(); 
    544                 }*/ 
    545                 else if (attrName == "mergecost") 
    546                 { 
    547                         StrX attrValue(attributes.getValue(i)); 
    548                          
    549                         const char *ptr = attrValue.LocalForm(); 
    550                         char *endptr = NULL; 
    551                         const float cost = (float)strtod(ptr, &endptr); 
    552  
    553                         viewCell->SetMergeCost(cost); 
     472        while (1) 
     473        {       // read object ids 
     474                const int index = strtol(ptr, &endptr, 10); 
     475                if (ptr == endptr)  
     476                        break; 
     477                objIndices.push_back(index); 
     478                ptr = endptr; 
     479        } 
     480 
     481        // TODO:  
     482        // 1) find objects and add them to pvs 
     483        // 2) get view cell with specified id 
     484        MeshInstance dummyInst(NULL); 
     485 
     486        vector<int>::const_iterator it, it_end = objIndices.end(); 
     487        for (it = objIndices.begin(); it != it_end; ++ it) 
     488        { 
     489                const int objId = *it;   
     490                dummyInst.SetId(objId); 
     491 
     492                ObjectContainer::iterator oit = 
     493                        lower_bound(mObjects->begin(),  
     494                        mObjects->end(),  
     495                        (Intersectable *)&dummyInst, ilt);       
     496 
     497                if ((oit != mObjects->end()) && ((*oit)->GetId() == objId)) 
     498                { 
     499                        // $$JB we should store a float a per object which corresponds 
     500                        // to sumof pdfs, i.e. its relative visibility 
     501                        // temporarily set to 1.0f 
     502                        pvs.AddSample(*oit, 1.0f);                               
     503                } 
     504                else 
     505                { 
     506                        cout << "error: object with id " << objId << " does not exist" << endl; 
    554507                } 
    555508        } 
     
    705658        } 
    706659 
     660        /////////// 
    707661        //-- find associated view cell 
    708662 
     
    728682        { 
    729683                // TODO: get view cell with specified id 
    730                 ViewCellInterior dummyVc; 
    731                 dummyVc.SetId(viewCellId); 
    732  
    733                 ViewCellContainer::iterator vit = 
    734                         lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt); 
    735                          
    736                 BspViewCell *viewCell = dynamic_cast<BspViewCell *>(*vit); 
     684                ViewCellsMap::iterator vit = mViewCells.find(viewCellId); 
     685                BspViewCell *viewCell = dynamic_cast<BspViewCell *>((*vit).second); 
    737686         
    738687                if (viewCell->GetId() == viewCellId) 
     
    740689                        // create new view cell for bsp nodes 
    741690                        leaf->SetViewCell(viewCell); 
    742                         viewCell->mLeaf = leaf; 
     691                        viewCell->mLeaves.push_back(leaf); 
    743692                } 
    744693                else 
     
    793742 
    794743 
    795 void ViewCellsParseHandlers::StartViewCellLeaf(AttributeList& attributes) 
    796 { 
    797         ViewCellLeaf *viewCell = new ViewCellLeaf(); 
    798  
    799         if (mCurrentViewCell) // replace front or (if not NULL) back child 
    800         { 
    801                 ViewCellInterior *interior =  
    802                         dynamic_cast<ViewCellInterior *>(mCurrentViewCell); 
    803                 interior->SetupChildLink(viewCell); 
    804         } 
    805         else // root 
    806         { 
    807                 mViewCellsTree->SetRoot(viewCell); 
    808         } 
    809  
    810         StartViewCellPvs(viewCell, attributes); 
    811  
    812         // collect leaves 
    813         mViewCells.push_back(viewCell); 
    814 } 
    815  
    816  
    817 void ViewCellsParseHandlers::StartViewCellInterior(AttributeList& attributes) 
    818 { 
    819         ViewCellInterior* interior = new ViewCellInterior(); 
     744ViewCell *ViewCellsParseHandlers::GetOrCreateViewCell(const int id, const bool isLeaf) 
     745{ 
     746        ViewCellsMap::iterator vit = mViewCells.find(id); 
     747 
     748        if (vit != mViewCells.end()) 
     749        { 
     750                return (*vit).second; 
     751        } 
     752        else 
     753        { 
     754                ViewCell *vc; 
     755                if (isLeaf) 
     756                { 
     757                        vc = new ViewCellLeaf(); 
     758                } 
     759                else 
     760                { 
     761                        vc = new ViewCellInterior(); 
     762                } 
     763 
     764                vc->SetId(id); 
     765                mViewCells[id] = vc; 
     766                return vc; 
     767        } 
     768} 
     769 
     770 
     771void ViewCellsParseHandlers::StartViewCell(AttributeList& attributes, const bool isLeaf) 
     772{ 
     773        ViewCell *viewCell = NULL; 
    820774         
    821         if (mCurrentViewCell) // replace NULL child of parent with current node 
    822         { 
    823                 ViewCellInterior *current = dynamic_cast<ViewCellInterior *>(mCurrentViewCell); 
    824  
    825                 current->SetupChildLink(interior); 
    826         } 
    827         else 
    828         { 
    829                 mViewCellsTree->SetRoot(interior); 
    830         } 
    831  
    832         mCurrentViewCell = interior; 
    833  
    834         StartViewCellPvs(interior, attributes); 
     775        const int len = attributes.getLength(); 
     776        float mergeCost; 
     777        ObjectPvs pvs; 
     778 
     779        for (int i = 0; i < len; ++ i)  
     780        { 
     781                const string attrName(StrX(attributes.getName(i)).LocalForm()); 
     782         
     783                if (attrName == "id")  
     784                { 
     785                        const StrX attrValue(attributes.getValue(i)); 
     786                        const char *ptr = attrValue.LocalForm(); 
     787                        char *endptr = NULL; 
     788                        const int id = strtol(ptr, &endptr, 10); 
     789 
     790                        // create new view cell, otherwise use reference. 
     791                        viewCell = GetOrCreateViewCell(id, isLeaf); 
     792                         
     793                        if (mCurrentViewCell) // replace front or (if not NULL) back child 
     794                        {        
     795                                ViewCellInterior *interior =  
     796                                        dynamic_cast<ViewCellInterior *>(mCurrentViewCell); 
     797                                interior->SetupChildLink(viewCell); 
     798                        } 
     799                        else  
     800                        {       // set the new root 
     801                                mViewCellsTree->SetRoot(viewCell); 
     802                        } 
     803                         
     804                        if (!isLeaf) 
     805                        { 
     806                                mCurrentViewCell = viewCell; 
     807                        } 
     808                } 
     809                if (attrName == "pvs")  
     810                { 
     811                        StrX attrValue(attributes.getValue(i)); 
     812                        const char *ptr = attrValue.LocalForm(); 
     813 
     814                        // note: id must come before pvs!  
     815                        // otherwise view cell is undefined 
     816                        StartViewCellPvs(pvs, ptr); 
     817                } 
     818                else if (attrName == "active") 
     819                { 
     820                        StrX attrValue(attributes.getValue(i)); 
     821                        const char *ptr = attrValue.LocalForm(); 
     822                        char *endptr = NULL; 
     823                        const bool isActive = (bool)strtol(ptr, &endptr, 10); 
     824 
     825                        if (isActive) 
     826                        { 
     827                                // TODO 
     828                        } 
     829                } 
     830                else if (attrName == "mergecost") 
     831                { 
     832                        StrX attrValue(attributes.getValue(i)); 
     833                         
     834                        const char *ptr = attrValue.LocalForm(); 
     835                        char *endptr = NULL; 
     836                        mergeCost = (float)strtod(ptr, &endptr); 
     837                } 
     838        } 
     839 
     840        viewCell->SetMergeCost(mergeCost); 
     841        viewCell->SetPvs(pvs); 
    835842} 
    836843 
     
    841848        { 
    842849                cout << "hierarchy type: Bsp" << endl; 
    843  
    844850                mVspBspTree = new VspBspTree(); 
    845851 
     
    847853                mVspBspTree->mBox = mViewSpaceBox; 
    848854 
    849                 ViewCellContainer::iterator vit, vit_end = mViewCells.end(); 
    850  
    851                 // reset view cells using the current node type 
     855                ViewCellsMap::iterator vit, vit_end = mViewCells.end(); 
     856 
     857                // remove view cells and exchange them with the  
     858                // view cells specialized for the current hierarchy node type 
    852859                for (vit = mViewCells.begin(); vit != vit_end; ++ vit) 
    853860                { 
    854                         ViewCell *vc = *vit; 
    855  
     861                        ViewCell *vc = (*vit).second; 
     862                        if (!vc->IsLeaf()) // exchange only leaves 
     863                                continue; 
    856864                        BspViewCell *bspVc = new BspViewCell(); 
     865                        bspVc->SetId(vc->GetId()); 
    857866                        bspVc->SetPvs(vc->GetPvs()); 
    858                         bspVc->SetId(vc->GetId()); 
    859867 
    860868                        if (vc->IsRoot()) 
     
    868876 
    869877                        DEL_PTR(vc); 
    870                         (*vit) = bspVc; 
     878                        (*vit).second = bspVc; 
    871879                } 
    872880        } 
     
    874882        { 
    875883                cout << "hierarchy type: Vsp" << endl; 
    876  
    877884                mVspTree = new VspTree(); 
    878885 
     
    880887                mVspTree->mBoundingBox = mViewSpaceBox; 
    881888 
    882                 ViewCellContainer::iterator vit, vit_end = mViewCells.end(); 
     889                ViewCellsMap::iterator vit, vit_end = mViewCells.end(); 
    883890 
    884891                // reset view cells using the current node type 
    885892                for (vit = mViewCells.begin(); vit != vit_end; ++ vit) 
    886893                { 
    887                         ViewCell *vc = *vit; 
     894                        ViewCell *vc = (*vit).second; 
    888895 
    889896                        VspViewCell *vspVc = new VspViewCell(); 
    890897                        vspVc->SetPvs(vc->GetPvs()); 
    891898                        vspVc->SetId(vc->GetId()); 
     899 
     900                        if (!vc->IsLeaf()) // exchange only leaves 
     901                                continue; 
    892902 
    893903                        if (vc->IsRoot()) 
     
    901911                         
    902912                        DEL_PTR(vc); 
    903                         (*vit) = vspVc; 
     913                        (*vit).second = vspVc; 
    904914                } 
    905915 
     
    970980        } 
    971981 
    972         //-- find associated view cell 
     982        ///////////// 
     983        //-- find view cell associated with the id 
     984 
    973985        int viewCellId; 
    974986         
     
    9891001        } 
    9901002         
    991         if (viewCellId >= 0) // valid view cell 
     1003        if (viewCellId >= 0) // valid view cell found 
    9921004        { 
    9931005                // TODO: get view cell with specified id 
    994                 ViewCellInterior dummyVc; 
     1006                /*ViewCellInterior dummyVc; 
    9951007                dummyVc.SetId(viewCellId); 
    9961008 
    997                 //cout << "\nsearching view cell with id " << viewCellId << endl; 
    9981009                ViewCellContainer::iterator vit = 
    9991010                        lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt); 
    1000                          
     1011                */ 
     1012                ViewCellsMap::iterator vit = mViewCells.find(viewCellId); 
    10011013                if (vit == mViewCells.end()) 
    10021014                        cout << "error: view cell " << viewCellId << " not found" << endl; 
    10031015         
    1004                 VspViewCell *viewCell = dynamic_cast<VspViewCell *>(*vit); 
     1016                VspViewCell *viewCell = dynamic_cast<VspViewCell *>((*vit).second); 
    10051017                 
    10061018                if (viewCell->GetId() == viewCellId) 
    10071019                { 
    10081020                        leaf->SetViewCell(viewCell); 
    1009                         viewCell->mLeaf = leaf; 
     1021                        viewCell->mLeaves.push_back(leaf); 
    10101022                } 
    10111023                else 
Note: See TracChangeset for help on using the changeset viewer.