Ignore:
Timestamp:
08/25/06 18:25:09 (18 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r1284 r1286  
    8989  , mCurrentBspNode(NULL) 
    9090  , mCurrentVspNode(NULL) 
    91   , mCurrentKdNode(NULL) 
     91  , mCurrentOspNode(NULL) 
     92  , mCurrentBvhNode(NULL) 
    9293  , mObjects(objects) 
    9394  , mBoundingBoxConverter(bconverter) 
     
    114115 
    115116        if (element == "BoundingBoxes") 
     117        { 
    116118                EndBoundingBoxes(); 
     119        } 
    117120 
    118121        if (element == "ViewCells") 
     122        {        
    119123                EndViewCells(); 
    120  
    121         // inside the view cell description 
    122         if (mCurrentState == PARSE_VIEWCELLS) 
    123         { 
    124                 if (element == "Interior") 
    125                         EndViewCellInterior(); 
    126         } 
    127         else 
    128         { 
    129                 if (element == "Interior") 
    130                         EndBspInterior(); 
     124        } 
     125 
     126        if (element == "ObjectSpaceHierarchy") 
     127        { 
     128                EndObjectSpaceHierarchy(); 
    131129        } 
    132130 
    133131        // finished, create view cells manager 
    134132        if (element == "VisibilitySolution") 
     133        { 
    135134                CreateViewCellsManager(); 
     135        } 
     136 
     137        if (element == "Interior") 
     138        { 
     139                switch (mCurrentState) 
     140                { 
     141                case PARSE_VIEWCELLS: 
     142                        EndViewCellInterior(); 
     143                        break; 
     144                case PARSE_OBJECTSPACE_HIERARCHY: 
     145                        EndObjectSpaceHierarchyInterior(); 
     146                        break; 
     147                case PARSE_VIEWSPACE_HIERARCHY:  
     148                        EndViewSpaceHierarchyInterior(); 
     149                        break; 
     150                default: 
     151                        break; 
     152                } 
     153        } 
     154} 
     155 
     156 
     157void ViewCellsParseHandlers::EndObjectSpaceHierarchy() 
     158{ 
     159        if (mObjectSpaceHierarchyType == OSP) 
     160        { 
     161                mHierarchyManager->mOspTree->InsertObjects( 
     162                        mHierarchyManager->mOspTree->mRoot, *mObjects); 
     163        } 
     164} 
     165 
     166 
     167void ViewCellsParseHandlers::EndViewSpaceHierarchyInterior() 
     168{ 
     169        switch (mViewSpaceHierarchyType) 
     170        { 
     171        case BSP: 
     172                EndBspInterior(); 
     173                break; 
     174        case VSP: 
     175                EndVspInterior(); 
     176                break;   
     177        default: 
     178                Debug << "not implemented view space hierarchy type " << mViewSpaceHierarchyType << endl; 
     179                break; 
     180        } 
     181} 
     182 
     183 
     184void ViewCellsParseHandlers::EndObjectSpaceHierarchyInterior() 
     185{ 
     186        switch (mObjectSpaceHierarchyType) 
     187        { 
     188        case OSP: 
     189                EndOspInterior(); 
     190                break; 
     191        case BVH: 
     192                EndBvhInterior(); 
     193                break;   
     194        default: 
     195                Debug << "not implemented object space hierarchy type " << mViewSpaceHierarchyType << endl; 
     196                break; 
     197        } 
    136198} 
    137199 
     
    143205        {       cout << "]"; 
    144206                mCurrentBspNode = mCurrentBspNode->GetParent(); 
     207        } 
     208} 
     209 
     210 
     211void ViewCellsParseHandlers::EndBvhInterior() 
     212{ 
     213        // go one up in the tree 
     214        if (mCurrentBvhNode->GetParent()) 
     215        {       cout << "]"; 
     216                mCurrentBvhNode = mCurrentBvhNode->GetParent(); 
     217        } 
     218} 
     219 
     220 
     221void ViewCellsParseHandlers::EndOspInterior() 
     222{ 
     223        // go one up in the tree 
     224        if (mCurrentOspNode->mParent) 
     225        {       cout << "]"; 
     226                mCurrentOspNode = mCurrentOspNode->mParent; 
    145227        } 
    146228} 
     
    243325                StartVspInterior(attributes); 
    244326        } 
    245  
    246327        if (element == "Leaf")  
    247328        { 
     
    289370                                                                                                                        AttributeList& attributes) 
    290371{ 
    291         //if (!mViewCellsManager)return; 
    292  
    293372        //-- use cell type according to the chosen method 
    294  
    295373        switch (mViewSpaceHierarchyType) 
    296374        { 
    297                 case BSP: 
    298                         StartBspElement(element, attributes); 
    299                         break; 
    300                 case VSP: 
    301                         StartVspElement(element, attributes); 
    302                         break; 
    303                 default: 
    304                         Debug << "not implemented" << endl; 
    305                         break; 
     375        case BSP: 
     376                StartBspElement(element, attributes); 
     377                break; 
     378        case VSP: 
     379                StartVspElement(element, attributes); 
     380                break; 
     381        default: 
     382                Debug << "not implemented" << endl; 
     383                break; 
    306384        } 
    307385} 
     
    412490 
    413491 
    414 void ViewCellsParseHandlers::ParseViewCellPvs(ViewCell *viewCell,  
     492void ViewCellsParseHandlers::StartViewCellPvs(ViewCell *viewCell,  
    415493                                                                                          AttributeList&  attributes) 
    416494{ 
     
    433511                        while (1) 
    434512                        { 
    435                                 int index = strtol(ptr, &endptr, 10); 
    436  
    437                                 if (ptr == endptr) 
    438                                         break; 
     513                                const int index = strtol(ptr, &endptr, 10); 
     514 
     515                                if (ptr == endptr) break; 
    439516 
    440517                                objIndices.push_back(index); 
     
    454531 
    455532                                ObjectContainer::iterator oit = 
    456                                         lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);       
     533                                        lower_bound(mObjects->begin(),  
     534                                                                mObjects->end(),  
     535                                                                (Intersectable *)&dummyInst, ilt);       
    457536                                                         
    458537                                if ((oit != mObjects->end()) && ((*oit)->GetId() == objId)) 
     
    551630void ViewCellsParseHandlers::StartObjectSpaceHierarchy(AttributeList& attributes) 
    552631{ 
    553         int len = attributes.getLength(); 
    554  
     632        const int len = attributes.getLength(); 
    555633        Vector3 bmin, bmax; 
    556 cout << "here3" << endl; 
     634 
    557635        for (int i = 0; i < len; ++ i)  
    558636        { 
     
    567645                        { 
    568646                                cout << "\nobject space hierarchy: Osp" << endl; 
     647 
    569648                                mHierarchyManager =  
    570649                                        new HierarchyManager(mVspTree, HierarchyManager::KD_BASED_OBJ_SUBDIV); 
     650 
     651                                mObjectSpaceHierarchyType = OSP; 
     652 
     653                                //std::stable_sort(objects.begin(), objects.end(), ilt); 
     654                                mHierarchyManager->mOspTree->mBoundingBox.Initialize(); 
     655                                ObjectContainer::const_iterator oit, oit_end = mObjects->end(); 
     656                                 
     657                                //-- compute bounding box 
     658                                for (oit = mObjects->begin(); oit != oit_end; ++ oit) 
     659                                { 
     660                                        Intersectable *obj = *oit; 
     661                                        // compute bounding box of view space 
     662                                        mHierarchyManager->mOspTree->mBoundingBox.Include(obj->GetBox()); 
     663                                } 
    571664                        } 
    572665                        else if (strcmp(ptr, "bvh") == 0) 
    573666                        { 
    574667                                cout << "\nobject space hierarchy: Bvh" << endl; 
     668                                mObjectSpaceHierarchyType = BVH; 
    575669                                mHierarchyManager =  
    576670                                        new HierarchyManager(mVspTree, HierarchyManager::BV_BASED_OBJ_SUBDIV); 
     
    579673        } 
    580674} 
    581  
    582675 
    583676 
     
    675768        { 
    676769                // add to invalid view space 
    677                 if (mViewSpaceHierarchyType== ViewCellsManager::VSP_BSP) 
    678                 { 
    679                         leaf->SetViewCell(mVspBspTree->GetOrCreateOutOfBoundsCell()); 
    680                         leaf->SetTreeValid(false); 
    681                         mVspBspTree->PropagateUpValidity(leaf); 
    682                 } 
     770                leaf->SetViewCell(mVspBspTree->GetOrCreateOutOfBoundsCell()); 
     771                leaf->SetTreeValid(false); 
     772                mVspBspTree->PropagateUpValidity(leaf); 
    683773        } 
    684774} 
     
    736826        } 
    737827 
    738         ParseViewCellPvs(viewCell, attributes); 
     828        StartViewCellPvs(viewCell, attributes); 
    739829 
    740830        // collect leaves 
     
    746836{ 
    747837        ViewCellInterior* interior = new ViewCellInterior(); 
    748         cout << "here7"<<endl; 
     838         
    749839        if (mCurrentViewCell) // replace NULL child of parent with current node 
    750840        { 
     
    760850        mCurrentViewCell = interior; 
    761851 
    762         ParseViewCellPvs(interior, attributes); 
     852        StartViewCellPvs(interior, attributes); 
    763853} 
    764854 
     
    767857{ 
    768858        ViewCellContainer::iterator it, it_end = mViewCells.end(); 
    769         Debug << endl; 
     859        cout << "\n====================" << endl << endl; 
    770860        for (it = mViewCells.begin(); it != it_end; ++ it) 
    771861        { 
    772                 Debug << (*it)->GetId() << " "; 
    773         } 
    774 Debug << endl; 
     862                cout << (*it)->GetId() << " "; 
     863        } 
     864cout << endl; 
    775865        if (mViewSpaceHierarchyType == BSP) 
    776866        { 
     
    799889                        else 
    800890                        { 
    801                 vc->GetParent()->SetupChildLink(bspVc); 
    802                                 vc->GetParent()->RemoveChildLink(vc); 
     891                vc->GetParent()->ReplaceChildLink(vc, bspVc); 
    803892                        } 
    804893 
     
    806895                        (*vit) = bspVc; 
    807896                } 
    808  
    809897        } 
    810898        else if (mViewSpaceHierarchyType == VSP)  
     
    834922                        else 
    835923                        { 
    836                 vc->GetParent()->SetupChildLink(vspVc); 
    837                                 vc->GetParent()->RemoveChildLink(vc); 
     924                                vc->GetParent()->ReplaceChildLink(vc, vspVc); 
    838925                        } 
    839926                         
     
    849936                } 
    850937        } 
    851         Debug << "************************" << endl; 
     938        cout << "************************" << endl; 
    852939        it_end = mViewCells.end(); 
    853940        for (it = mViewCells.begin(); it != it_end; ++ it) 
    854941        { 
    855                 Debug << (*it)->GetId() << " "; 
    856         } 
    857         Debug << endl; 
     942                cout << (*it)->GetId() << " "; 
     943        } 
     944        cout << endl; 
    858945        cout << "\nview space box: " << mViewSpaceBox << endl; 
    859946} 
     
    862949void ViewCellsParseHandlers::CreateViewCellsManager(/*const char *name*/) 
    863950{ 
    864         if (mViewSpaceHierarchyType == BSP) //  
     951        if (mViewSpaceHierarchyType == BSP) 
    865952        { 
    866953                Debug << "creating view cells manager: VspBsp" << endl; 
    867954 
    868                 //mVspBspTree = new VspBspTree(); 
    869955                mViewCellsManager = new VspBspViewCellsManager(mViewCellsTree, mVspBspTree); 
    870956        } 
     
    9501036                if (vit == mViewCells.end()) 
    9511037                        cout << "error: view cell " << viewCellId << " not found" << endl; 
    952  
     1038         
    9531039                VspViewCell *viewCell = dynamic_cast<VspViewCell *>(*vit); 
    954          
     1040                 
    9551041                if (viewCell->GetId() == viewCellId) 
    9561042                { 
     
    9641050        } 
    9651051        else 
    966         { 
     1052        {        
    9671053                // add to invalid view space 
    9681054                leaf->SetViewCell(mVspTree->GetOrCreateOutOfBoundsCell()); 
     
    9861072                if (attrName == "plane")  
    9871073                { 
    988                         sscanf(ptr, "%d %f", &plane.mAxis, &plane.mPosition); 
     1074                        sscanf(ptr, "%f %d", &plane.mPosition, &plane.mAxis); 
    9891075                } 
    9901076        } 
     
    9941080        if (mCurrentVspNode) // replace NULL child of parent with current node 
    9951081        { 
    996                 VspInterior *current = dynamic_cast<VspInterior *>(mCurrentVspNode); 
    997  
    998                 current->ReplaceChildLink(NULL, interior); 
    999                 interior->SetParent(current); 
     1082                VspInterior *parent = dynamic_cast<VspInterior *>(mCurrentVspNode); 
     1083 
     1084                parent->ReplaceChildLink(NULL, interior); 
     1085                interior->SetParent(parent); 
     1086                 
     1087                AxisAlignedBox3 frontBox, backBox; 
     1088 
     1089                parent->GetBoundingBox().Split(parent->GetPlane().mAxis, parent->GetPlane().mPosition, frontBox, backBox); 
     1090                if (parent->GetFront() == interior) 
     1091                        interior->SetBoundingBox(frontBox); 
     1092                else 
     1093                        interior->SetBoundingBox(backBox); 
    10001094        } 
    10011095        else 
    10021096        { 
    10031097                mVspTree->mRoot = interior; 
     1098                interior->SetBoundingBox(mVspTree->GetBoundingBox()); 
    10041099        } 
    10051100 
     
    10211116                if (attrName == "plane")  
    10221117                { 
    1023                         sscanf(ptr, "%d %f", 
    1024                                    &plane.mAxis, &plane.mPosition); 
     1118                        sscanf(ptr, "%f %d", &plane.mPosition, &plane.mAxis); 
    10251119                } 
    10261120        } 
     
    10311125        interior->mPosition = plane.mPosition; 
    10321126 
    1033         if (mCurrentKdNode) // replace NULL child of parent with current node 
    1034         { 
    1035                 KdInterior *parent = dynamic_cast<KdInterior *>(mCurrentKdNode); 
     1127        if (mCurrentOspNode) // replace NULL child of parent with current node 
     1128        { 
     1129                KdInterior *parent = dynamic_cast<KdInterior *>(mCurrentOspNode); 
    10361130                parent->ReplaceChildLink(NULL, interior); 
    10371131                interior->mParent = parent; 
     1132 
     1133                AxisAlignedBox3 frontBox, backBox; 
     1134 
     1135                parent->mBox.Split(parent->mAxis, parent->mPosition, frontBox, backBox); 
     1136                if (parent->mFront == interior) 
     1137                        interior->mBox = frontBox; 
     1138                else 
     1139                        interior->mBox = backBox; 
    10381140        } 
    10391141        else 
    10401142        { 
    10411143                mHierarchyManager->mOspTree->mRoot = interior; 
    1042         } 
    1043  
    1044         mCurrentKdNode = interior; 
     1144                interior->mBox = mHierarchyManager->mOspTree->mBoundingBox; 
     1145        } 
     1146 
     1147        mCurrentOspNode = interior; 
    10451148} 
    10461149 
     
    10491152{ 
    10501153        KdLeaf * leaf =  
    1051                 new KdLeaf(dynamic_cast<KdInterior *>(mCurrentKdNode), NULL); 
    1052  
    1053         if (mCurrentKdNode) // replace front or (if not NULL) back child 
    1054         { 
    1055                 dynamic_cast<KdInterior *>(mCurrentKdNode)->ReplaceChildLink(NULL, leaf); 
     1154                new KdLeaf(dynamic_cast<KdInterior *>(mCurrentOspNode), NULL); 
     1155 
     1156        if (mCurrentOspNode) // replace front or (if not NULL) back child 
     1157        { 
     1158                dynamic_cast<KdInterior *>(mCurrentOspNode)->ReplaceChildLink(NULL, leaf); 
    10561159        } 
    10571160        else 
     
    10631166 
    10641167void ViewCellsParseHandlers::StartBvhLeaf(AttributeList& attributes) 
    1065 {/* 
    1066         KdLeaf * leaf =  
    1067                 new KdLeaf(dynamic_cast<KdInterior *>(mCurrentKdNode), NULL); 
    1068  
    1069         if (mCurrentKdNode) // replace front or (if not NULL) back child 
    1070         { 
    1071                 dynamic_cast<KdInterior *>(mCurrentKdNode)->ReplaceChildLink(NULL, leaf); 
    1072         } 
    1073         else 
    1074         { 
    1075                 mHierarchyManager->mBvHierarchy->mRoot = leaf; 
    1076         }*/ 
    1077 } 
    1078  
    1079  
    1080 void ViewCellsParseHandlers::StartBvhInterior(AttributeList& attributes) 
    1081 {/* 
    1082         AxisAlignedPlane plane; 
    1083         int len = attributes.getLength(); 
     1168{ 
     1169        const int len = attributes.getLength(); 
     1170        Vector3 minBox, maxBox; 
     1171 
     1172        ObjectContainer objects; 
    10841173 
    10851174        for (int i = 0; i < len; ++ i)  
     
    10891178                const char *ptr = attrValue.LocalForm(); 
    10901179 
    1091                 if (attrName == "plane")  
    1092                 { 
    1093                         sscanf(ptr, "%d %f", 
    1094                                    &plane.mAxis, &plane.mPosition); 
    1095                 } 
    1096         } 
    1097  
    1098         KdInterior* interior = new KdInterior(NULL); 
    1099          
    1100         interior->mAxis = plane.mAxis; 
    1101         interior->mPosition = plane.mPosition; 
    1102  
    1103         if (mCurrentKdNode) // replace NULL child of parent with current node 
    1104         { 
    1105                 KdInterior *parent = dynamic_cast<KdInterior *>(mCurrentKdNode); 
     1180                if (attrName == "min")  
     1181                { 
     1182                        sscanf(ptr, "%f %f %f", &minBox); 
     1183                } 
     1184                if (attrName == "max")  
     1185                { 
     1186                        sscanf(ptr, "%f %f %f", &maxBox); 
     1187                } 
     1188                if (attrName == "objects") 
     1189                { 
     1190                        StartBvhLeafObjects(objects, ptr); 
     1191                } 
     1192        } 
     1193 
     1194        BvhLeaf * leaf =  
     1195                new BvhLeaf(AxisAlignedBox3(minBox, maxBox),  
     1196                                        dynamic_cast<BvhInterior *>(mCurrentBvhNode), (int)objects.size()); 
     1197 
     1198        leaf->mObjects = objects; 
     1199 
     1200        if (mCurrentBvhNode) // replace front or (if not NULL) back child 
     1201        { 
     1202                dynamic_cast<BvhInterior *>(mCurrentBvhNode)->ReplaceChildLink(NULL, leaf); 
     1203        } 
     1204        else 
     1205        { 
     1206                mHierarchyManager->mBvHierarchy->mRoot = leaf; 
     1207        } 
     1208} 
     1209 
     1210 
     1211void ViewCellsParseHandlers::StartBvhLeafObjects(ObjectContainer &objects,  
     1212                                                                                                 const char *ptr) 
     1213{ 
     1214        vector<int> objIndices; 
     1215 
     1216        char *endptr; 
     1217                         
     1218        while (1) 
     1219        { 
     1220                const int index = strtol(ptr, &endptr, 10); 
     1221 
     1222                if (ptr == endptr) break; 
     1223 
     1224                objIndices.push_back(index); 
     1225 
     1226                ptr = endptr; 
     1227        } 
     1228 
     1229        //TODO: find objects and add them to pvs 
     1230        // TODO: get view cell with specified id 
     1231        MeshInstance dummyInst(NULL); 
     1232 
     1233        vector<int>::const_iterator it, it_end = objIndices.end(); 
     1234 
     1235        for (it = objIndices.begin(); it != it_end; ++ it) 
     1236        { 
     1237                const int objId = *it;   
     1238                dummyInst.SetId(objId); 
     1239 
     1240                ObjectContainer::iterator oit = 
     1241                        lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);       
     1242                                                         
     1243                if ((oit != mObjects->end()) && ((*oit)->GetId() == objId)) 
     1244                { 
     1245                        objects.push_back(*oit);         
     1246                } 
     1247                else 
     1248                { 
     1249                        cout << "error: object with id " << objId << " does not exist" << endl; 
     1250                } 
     1251        } 
     1252} 
     1253 
     1254 
     1255void ViewCellsParseHandlers::StartBvhInterior(AttributeList& attributes) 
     1256{ 
     1257        const int len = attributes.getLength(); 
     1258        Vector3 minBox, maxBox; 
     1259 
     1260        for (int i = 0; i < len; ++ i)  
     1261        { 
     1262                string attrName(StrX(attributes.getName(i)).LocalForm()); 
     1263                StrX attrValue(attributes.getValue(i)); 
     1264                const char *ptr = attrValue.LocalForm(); 
     1265 
     1266                if (attrName == "min")  
     1267                { 
     1268                        sscanf(ptr, "%f %f %f", &minBox); 
     1269                } 
     1270                if (attrName == "max")  
     1271                { 
     1272                        sscanf(ptr, "%f %f %f", &maxBox); 
     1273                } 
     1274        } 
     1275 
     1276        BvhInterior* interior = new BvhInterior(AxisAlignedBox3(minBox, maxBox)); 
     1277 
     1278        if (mCurrentBvhNode) // replace NULL child of parent with current node 
     1279        { 
     1280                BvhInterior *parent = dynamic_cast<BvhInterior *>(mCurrentBvhNode); 
    11061281                parent->ReplaceChildLink(NULL, interior); 
    1107                 interior->mParent = parent; 
     1282                interior->SetParent(parent); 
    11081283        } 
    11091284        else 
    11101285        { 
    1111                 mOspTree->mRoot = interior; 
    1112         } 
    1113  
    1114         mCurrentKdNode = interior;*/ 
    1115 } 
    1116  
     1286                mBvHierarchy->mRoot = interior; 
     1287        } 
     1288 
     1289        mCurrentBvhNode = interior; 
     1290} 
    11171291 
    11181292 
Note: See TracChangeset for help on using the changeset viewer.