- Timestamp:
- 12/02/05 02:02:07 (19 years ago)
- Location:
- trunk/VUT/GtpVisibilityPreprocessor/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
r441 r442 265 265 BspTree::BspTree(): 266 266 mRoot(NULL), 267 mStoreLeavesWithRays(false), 268 mPvsUseLen(false), 267 mPvsUseArea(true), 269 268 mGenerateViewCells(true) 270 269 { … … 438 437 mRoot = new BspLeaf(); 439 438 440 tStack.push(BspTraversalData(mRoot, polys, 0, mRootCell, 441 new BoundedRayContainer(), 0));439 tStack.push(BspTraversalData(mRoot, polys, 0, mRootCell, new BoundedRayContainer(), 0, 440 mBox.SurfaceArea(), new BspNodeGeometry())); 442 441 443 442 while (!tStack.empty()) … … 475 474 mRootCell, 476 475 tData.mRays, 477 tData.mPvs); 476 tData.mPvs, 477 mBox.SurfaceArea(), 478 new BspNodeGeometry()); 478 479 479 480 BspTraversalData backData(interior->GetBack(), … … 482 483 mRootCell, 483 484 tData.mRays, 484 tData.mPvs); 485 tData.mPvs, 486 mBox.SurfaceArea(), 487 new BspNodeGeometry()); 485 488 486 489 if (!mGenerateViewCells) … … 732 735 mRoot = new BspLeaf(); 733 736 737 // constrruct root node geometry 734 738 BspNodeGeometry *cell = new BspNodeGeometry(); 735 739 ConstructGeometry(mRoot, *cell); 736 740 737 741 BspTraversalData tData(mRoot, polys, 0, mRootCell, rays, 738 ComputePvsSize(*rays) );742 ComputePvsSize(*rays), cell->GetArea(), cell); 739 743 740 744 tStack.push(tData); … … 756 760 } 757 761 758 Debug << "here2" << endl;759 762 cout << "finished\n"; 760 763 … … 768 771 ((int)data.mRays->size() <= mTermMinRays) || 769 772 (data.mPvs <= mTermMinPvs) || 773 (data.mArea <= mTermMinArea) || 770 774 (data.mDepth >= mTermMaxDepth) || 771 775 (data.GetAvgRayContribution() < mTermMaxRayContribution)); … … 824 828 825 829 BspTraversalData tFrontData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell, 826 new BoundedRayContainer(), 0 );830 new BoundedRayContainer(), 0, 0, new BspNodeGeometry()); 827 831 BspTraversalData tBackData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell, 828 new BoundedRayContainer(), 0 );832 new BoundedRayContainer(), 0, 0, new BspNodeGeometry()); 829 833 830 834 // create new interior node and two leaf nodes … … 860 864 DEL_PTR(tData.mPolygons); 861 865 DEL_PTR(tData.mRays); 866 DEL_PTR(tData.mGeometry); 862 867 863 868 return interior; … … 935 940 frontData.mPvs = ComputePvsSize(*frontData.mRays); 936 941 backData.mPvs = ComputePvsSize(*backData.mRays); 942 943 // split geometry and compute area 944 if (1) 945 { 946 tData.mGeometry->SplitGeometry(*frontData.mGeometry, 947 *backData.mGeometry, 948 *this, 949 interior->mPlane); 950 951 952 frontData.mArea = frontData.mGeometry->GetArea(); 953 backData.mArea = backData.mGeometry->GetArea(); 954 } 937 955 938 956 // compute accumulated ray length … … 1453 1471 float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, 1454 1472 const BoundedRayContainer &rays, 1455 const int pvs) const 1473 const int pvs, 1474 const float area, 1475 const BspNodeGeometry &cell) const 1456 1476 { 1457 1477 float val = 0; … … 1464 1484 1465 1485 // probability that view point lies in child 1466 float pOverall = 1; 1467 float pFront = 0.5; 1468 float pBack = 0.5; 1486 float pOverall = 0; 1487 float pFront = 0; 1488 float pBack = 0; 1489 1490 const bool pvsUseLen = false; 1469 1491 1470 1492 if (mSplitPlaneStrategy & PVS) … … 1472 1494 // create unique ids for pvs heuristics 1473 1495 GenerateUniqueIdsForPvs(); 1496 1497 if (mPvsUseArea) // use front and back cell areas to approximate volume 1498 { 1499 // construct child geometry with regard to the candidate split plane 1500 BspNodeGeometry frontCell; 1501 BspNodeGeometry backCell; 1502 1503 cell.SplitGeometry(frontCell, backCell, *this, candidatePlane); 1504 1505 pFront = frontCell.GetArea(); 1506 pBack = backCell.GetArea(); 1507 1508 pOverall = area; 1509 } 1474 1510 } 1475 1511 … … 1527 1563 AddObjToPvs(ray->sourceObject.mObject, cf, frontPvs, backPvs); 1528 1564 1529 float len = 1; 1530 if (0){ 1531 if (mPvsUseLen) 1532 len = SqrDistance(entP, extP); 1533 1534 // use length of rays to approximate volume 1535 if (Ray::BACK && Ray::COINCIDENT) 1536 pBack += len; 1537 if (Ray::FRONT && Ray::COINCIDENT) 1538 pFront += len; 1539 if (Ray::FRONT_BACK || Ray::BACK_FRONT) 1565 if (mPvsUseArea) 1540 1566 { 1541 if (mPvsUseLen) 1567 float len = 1; 1568 1569 if (pvsUseLen) 1570 len = SqrDistance(entP, extP); 1571 1572 // use length of rays to approximate volume 1573 if (Ray::BACK && Ray::COINCIDENT) 1574 pBack += len; 1575 if (Ray::FRONT && Ray::COINCIDENT) 1576 pFront += len; 1577 if (Ray::FRONT_BACK || Ray::BACK_FRONT) 1542 1578 { 1543 const Vector3 extp = ray->Extrap(maxT); 1544 const float t = candidatePlane.FindT(ray->GetLoc(), extp); 1579 if (pvsUseLen) 1580 { 1581 const Vector3 extp = ray->Extrap(maxT); 1582 const float t = candidatePlane.FindT(ray->GetLoc(), extp); 1545 1583 1546 const float newT = t * maxT; 1547 const float newLen = SqrDistance(ray->Extrap(newT), extp); 1548 1549 if (Ray::FRONT_BACK) 1550 { 1551 pFront += len - newLen; 1552 pBack += newLen; 1584 const float newT = t * maxT; 1585 const float newLen = SqrDistance(ray->Extrap(newT), extp); 1586 1587 if (Ray::FRONT_BACK) 1588 { 1589 pFront += len - newLen; 1590 pBack += newLen; 1591 } 1592 else 1593 { 1594 pBack += len - newLen; 1595 pFront += newLen; 1596 } 1553 1597 } 1554 1598 else 1555 1599 { 1556 pBack += len - newLen;1557 pFront += newLen;1600 ++ pFront; 1601 ++ pBack; 1558 1602 } 1559 1603 } 1560 else 1561 { 1562 ++ pFront; 1563 ++ pBack; 1564 } 1565 }} 1604 } 1566 1605 } 1567 1606 } … … 1575 1614 val += mBalancedRaysFactor * fabs(sumBalancedRays) / raysSize; 1576 1615 1577 float denom = pOverall * (float)pvs * 2.0f + Limits::Small;1616 const float denom = pOverall * (float)pvs * 2.0f + Limits::Small; 1578 1617 1579 1618 if (mSplitPlaneStrategy & PVS) … … 1583 1622 // give penalty to unbalanced split 1584 1623 if (1) 1585 if (((pFront * 0.2 + Limits::Small) > pBack) || (pFront < (pBack * 0.2 + Limits::Small))) 1624 if (((pFront * 0.2 + Limits::Small) > pBack) || 1625 (pFront < (pBack * 0.2 + Limits::Small))) 1586 1626 val += 0.5; 1587 1627 } … … 1670 1710 (mSplitPlaneStrategy & PVS)) 1671 1711 { 1672 val += SplitPlaneCost(candidatePlane, *data.mRays, data.mPvs); 1712 val += SplitPlaneCost(candidatePlane, *data.mRays, data.mPvs, 1713 data.mArea, *data.mGeometry); 1673 1714 } 1674 1715 … … 1741 1782 ++ mStat.maxRayContribNodes; 1742 1783 1743 //if (data.mGeometry->GetArea() <= mTermMinArea)1744 //++ mStat.minAreaNodes;1784 if (data.mGeometry->GetArea() <= mTermMinArea) 1785 ++ mStat.minAreaNodes; 1745 1786 1746 1787 #ifdef _DEBUG -
trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
r441 r442 364 364 /// rays piercing this node 365 365 BoundedRayContainer *mRays; 366 /// area of current node 367 float mArea; 368 /// geometry of node as induced by planes 369 BspNodeGeometry *mGeometry; 366 370 367 371 /// pvs size … … 382 386 mViewCell(NULL), 383 387 mRays(NULL), 384 mPvs(0) 388 mPvs(0), 389 mArea(0.0), 390 mGeometry(NULL) 385 391 {} 386 392 … … 390 396 ViewCell *viewCell, 391 397 BoundedRayContainer *rays, 392 int pvs): 398 int pvs, 399 float area, 400 BspNodeGeometry *cell): 393 401 mNode(node), 394 402 mPolygons(polys), … … 396 404 mViewCell(viewCell), 397 405 mRays(rays), 398 mPvs(pvs) 406 mPvs(pvs), 407 mArea(area), 408 mGeometry(cell) 399 409 {} 400 410 }; … … 584 594 float SplitPlaneCost(const Plane3 &candidatePlane, 585 595 const BoundedRayContainer &rays, 586 const int pvs) const; 596 const int pvs, 597 const float area, 598 const BspNodeGeometry &cell) const; 587 599 588 600 /** Filters next view cell down the tree and inserts it into the appropriate leaves … … 820 832 float mCtDivCi; 821 833 822 /// if intersected leaves should be stored with a sample823 bool mStoreLeavesWithRays;824 825 834 /// axis aligned split criteria 826 835 float mAaCtDivCi; … … 840 849 841 850 /// if area or accumulated ray lenght should be used for PVS heuristics 842 bool mPvsUse Len;851 bool mPvsUseArea; 843 852 844 853 private: -
trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
r441 r442 10 10 #include "VspKdTree.h" 11 11 #include "Exporter.h" 12 12 #include "VspBspTree.h" 13 13 14 14 … … 1121 1121 return VSP_KD; 1122 1122 } 1123 1124 1125 /**********************************************************************/ 1126 /* VspBspViewCellsManager implementation */ 1127 /**********************************************************************/ 1128 1129 VspBspViewCellsManager::VspBspViewCellsManager(VspBspTree *vspBspTree, 1130 int constructionSamples): 1131 ViewCellsManager(constructionSamples), 1132 mVspBspTree(vspBspTree) 1133 { 1134 mRenderSimulator = NULL;// TODO new VspBspRenderSimulator(bspTree); 1135 //InitRenderSimulator(); 1136 } 1137 1138 bool VspBspViewCellsManager::ViewCellsConstructed() const 1139 { 1140 return mVspBspTree->GetRoot() != NULL; 1141 } 1142 1143 ViewCell *VspBspViewCellsManager::GenerateViewCell(Mesh *mesh) const 1144 { 1145 return new BspViewCell(mesh); 1146 } 1147 1148 int VspBspViewCellsManager::Construct(const ObjectContainer &objects, 1149 const VssRayContainer &rays, 1150 AxisAlignedBox3 *sceneBbox) 1151 { 1152 // if view cells were already constructed 1153 if (ViewCellsConstructed()) 1154 return 0; 1155 1156 Debug << "Constructing bsp view cells" << endl; 1157 1158 int sampleContributions = 0; 1159 1160 VssRayContainer sampleRays; 1161 1162 int limit = min (mConstructionSamples, (int)rays.size()); 1163 1164 for (int i = 0; i < limit; ++ i) 1165 sampleRays.push_back(rays[i]); 1166 1167 mVspBspTree->Construct(sampleRays); 1168 1169 Debug << mVspBspTree->GetStatistics() << endl; 1170 1171 return sampleContributions; 1172 } 1173 1174 int VspBspViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay) 1175 { 1176 // view cells not yet constructed 1177 if (!ViewCellsConstructed()) 1178 return 0; 1179 1180 int contributingSamples = 0; 1181 1182 if (castRay) 1183 mVspBspTree->CastRay(ray); 1184 1185 Intersectable *tObject = 1186 !ray.intersections.empty() ? ray.intersections[0].mObject : NULL; 1187 1188 Intersectable *sObject = ray.sourceObject.mObject; 1189 1190 if (sObject || tObject) 1191 { 1192 // object can be seen from the view cell => add to view cell pvs 1193 for (int j = 0; j < (int)ray.bspIntersections.size(); ++ j) 1194 { 1195 VspBspLeaf *leaf = NULL; //TODO ray.bspIntersections[j].mLeaf; 1196 1197 // if ray not in unbounded space 1198 if (leaf->GetViewCell() != mVspBspTree->GetRootCell()) 1199 { 1200 if (sObject) 1201 { 1202 contributingSamples += 1203 leaf->GetViewCell()->GetPvs().AddSample(sObject); 1204 } 1205 1206 if (tObject) 1207 { 1208 contributingSamples += 1209 leaf->GetViewCell()->GetPvs().AddSample(tObject); 1210 } 1211 } 1212 } 1213 } 1214 1215 // rays passing through this viewcell 1216 if (0) 1217 for (int j = 1; j < ((int)ray.bspIntersections.size() - 1); ++ j) 1218 { 1219 VspBspLeaf *leaf = NULL;// TODO = ray.bspIntersections[j].mLeaf; 1220 1221 if (leaf->GetViewCell() != mVspBspTree->GetRootCell()) 1222 leaf->GetViewCell()-> 1223 AddPassingRay(ray, contributingSamples ? 1 : 0); 1224 } 1225 1226 return contributingSamples; 1227 } 1228 1229 int VspBspViewCellsManager::PostProcess(const ObjectContainer &objects, 1230 const RayContainer &rays) 1231 { 1232 if (!ViewCellsConstructed()) 1233 { 1234 Debug << "view cells not constructed" << endl; 1235 return 0; 1236 } 1237 1238 1239 //-- post processing of bsp view cells 1240 int vcSize = 0; 1241 int pvsSize = 0; 1242 1243 BspViewCellsStatistics vcStats; 1244 mVspBspTree->EvaluateViewCellsStats( vcStats); 1245 Debug << "original view cell partition:\n" << vcStats << endl; 1246 1247 if (1) // export view cells 1248 { 1249 cout << "exporting initial view cells (=leaves) ... "; 1250 Exporter *exporter = Exporter::GetExporter("view_cells.x3d"); 1251 1252 if (exporter) 1253 { 1254 exporter->SetWireframe(); 1255 //exporter->ExportBspLeaves(*mVspBspTree, stat.maxPvs); 1256 1257 if (0) 1258 { 1259 Material m; 1260 m.mDiffuseColor = RgbColor(0, 1, 0); 1261 exporter->SetForcedMaterial(m); 1262 exporter->SetWireframe(); 1263 1264 exporter->ExportGeometry(objects); 1265 } 1266 1267 delete exporter; 1268 } 1269 cout << "finished" << endl; 1270 } 1271 1272 cout << "starting post processing using " << mPostProcessSamples << " samples ... "; 1273 1274 long startTime = GetTime(); 1275 1276 1277 //-- merge or subdivide view cells 1278 int merged = 0; 1279 1280 RayContainer::const_iterator rit, rit_end = rays.end(); 1281 vector<Ray::BspIntersection>::const_iterator iit; 1282 1283 int limit = min((int)rays.size(), mPostProcessSamples); 1284 1285 for (int i = 0; i < limit; ++ i) 1286 { 1287 Ray *ray = rays[i]; 1288 1289 // traverse leaves stored in the rays and compare and merge consecutive 1290 // leaves (i.e., the neighbors in the tree) 1291 if (ray->bspIntersections.size() < 2) 1292 continue; 1293 1294 iit = ray->bspIntersections.begin(); 1295 1296 BspLeaf *previousLeaf = (*iit).mLeaf; 1297 ++ iit; 1298 1299 for (; iit != ray->bspIntersections.end(); ++ iit) 1300 { 1301 BspLeaf *leaf = (*iit).mLeaf; 1302 1303 if (ShouldMerge(leaf, previousLeaf)) 1304 { 1305 MergeBspLeafViewCells(leaf, previousLeaf); 1306 1307 ++ merged; 1308 } 1309 1310 previousLeaf = leaf; 1311 } 1312 } 1313 1314 //-- stats and visualizations 1315 cout << "finished" << endl; 1316 cout << "merged " << merged << " view cells in " 1317 << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl; 1318 1319 return merged; 1320 } 1321 1322 int VspBspViewCellsManager::GetType() const 1323 { 1324 return VSP_BSP; 1325 } 1326 1327 1328 void VspBspViewCellsManager::Visualize(const ObjectContainer &objects, 1329 const RayContainer &sampleRays) 1330 { 1331 if (!ViewCellsConstructed()) 1332 return; 1333 1334 //-- recount pvs 1335 BspViewCellsStatistics vcStats; 1336 mVspBspTree->EvaluateViewCellsStats(vcStats); 1337 1338 if (1) // export view cells 1339 { 1340 cout << "exporting view cells after merge ... "; 1341 /*Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d"); 1342 1343 if (exporter) 1344 { 1345 exporter->ExportBspViewCellPartition(*mVspBspTree, vcStats.maxPvs); 1346 delete exporter; 1347 } 1348 */ 1349 cout << "finished" << endl; 1350 } 1351 1352 //-- visualization of the BSP splits 1353 bool exportSplits = false; 1354 environment->GetBoolValue("BspTree.Visualization.exportSplits", exportSplits); 1355 1356 if (exportSplits) 1357 { 1358 cout << "exporting splits ... "; 1359 ExportSplits(objects, sampleRays); 1360 cout << "finished" << endl; 1361 } 1362 1363 ExportVspBspPvs(objects, sampleRays); 1364 } 1365 1366 1367 void VspBspViewCellsManager::ExportSplits(const ObjectContainer &objects, 1368 const RayContainer &sampleRays) 1369 { 1370 Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d"); 1371 1372 if (exporter) 1373 { 1374 Material m; 1375 m.mDiffuseColor = RgbColor(1, 0, 0); 1376 exporter->SetForcedMaterial(m); 1377 exporter->SetWireframe(); 1378 // TODO 1379 //exporter->ExportBspSplits(*mVspBspTree, true); 1380 1381 // take forced material, else big scenes cannot be viewed 1382 m.mDiffuseColor = RgbColor(0, 1, 0); 1383 exporter->SetForcedMaterial(m); 1384 exporter->SetFilled(); 1385 1386 exporter->ResetForcedMaterial(); 1387 1388 // export rays 1389 if (0) 1390 { 1391 RayContainer outRays; 1392 1393 int raysSize = min((int)sampleRays.size(), mVisualizationSamples); 1394 1395 for (int i = 0; i < raysSize; ++ i) 1396 { 1397 // only rays piercing geometry 1398 if (!sampleRays[i]->intersections.empty()) 1399 outRays.push_back(sampleRays[i]); 1400 } 1401 1402 // export rays 1403 exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0)); 1404 } 1405 1406 if (0) 1407 exporter->ExportGeometry(objects); 1408 1409 delete exporter; 1410 } 1411 } 1412 1413 void VspBspViewCellsManager::ExportBspPvs(const ObjectContainer &objects, 1414 const RayContainer &sampleRays) 1415 { 1416 const int leafOut = 10; 1417 1418 ViewCell::NewMail(); 1419 1420 //-- some rays for output 1421 const int raysOut = min((int)sampleRays.size(), mVisualizationSamples); 1422 cout << "visualization using " << mVisualizationSamples << " samples" << endl; 1423 vector<Ray *> vcRays[leafOut]; 1424 1425 if (0) 1426 { 1427 //-- some random view cells and rays for output 1428 vector<VspBspLeaf *> vspBspLeaves; 1429 1430 for (int i = 0; i < leafOut; ++ i) 1431 vspBspLeaves.push_back(mVspBspTree->GetRandomLeaf()); 1432 1433 for (int i = 0; i < (int)vspBspLeaves.size(); ++ i) 1434 { 1435 cout << "creating output for view cell " << i << " ... "; 1436 // check whether we can add the current ray to the output rays 1437 for (int k = 0; k < raysOut; ++ k) 1438 { 1439 Ray *ray = sampleRays[k]; 1440 1441 for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j) 1442 { 1443 BspLeaf *leaf = ray->bspIntersections[j].mLeaf; 1444 1445 if (vspBspLeaves[i]->GetViewCell() == leaf->GetViewCell()) 1446 { 1447 vcRays[i].push_back(ray); 1448 } 1449 } 1450 } 1451 1452 Intersectable::NewMail(); 1453 1454 BspViewCell *vc = dynamic_cast<BspViewCell *> 1455 (vspBspLeaves[i]->GetViewCell()); 1456 1457 //bspLeaves[j]->Mail(); 1458 char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i); 1459 1460 Exporter *exporter = Exporter::GetExporter(s); 1461 exporter->SetFilled(); 1462 1463 ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin(); 1464 1465 exporter->SetWireframe(); 1466 //exporter->SetFilled(); 1467 1468 Material m;//= RandomMaterial(); 1469 m.mDiffuseColor = RgbColor(0, 1, 0); 1470 exporter->SetForcedMaterial(m); 1471 1472 if (vc->GetMesh()) 1473 exporter->ExportViewCell(vc); 1474 else 1475 { 1476 PolygonContainer cell; 1477 // export view cell geometry 1478 mVspBspTree->ConstructGeometry(vc, cell); 1479 exporter->ExportPolygons(cell); 1480 CLEAR_CONTAINER(cell); 1481 } 1482 1483 Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 1484 << ", piercing rays=" << (int)vcRays[i].size() << endl; 1485 1486 // export rays piercing this view cell 1487 exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0)); 1488 1489 m.mDiffuseColor = RgbColor(1, 0, 0); 1490 exporter->SetForcedMaterial(m); 1491 1492 // exporter->SetWireframe(); 1493 exporter->SetFilled(); 1494 1495 // output PVS of view cell 1496 for (; it != vc->GetPvs().mEntries.end(); ++ it) 1497 { 1498 Intersectable *intersect = (*it).first; 1499 if (!intersect->Mailed()) 1500 { 1501 exporter->ExportIntersectable(intersect); 1502 intersect->Mail(); 1503 } 1504 } 1505 1506 // output rest of the objects 1507 if (0) 1508 { 1509 Material m;//= RandomMaterial(); 1510 m.mDiffuseColor = RgbColor(0, 0, 1); 1511 exporter->SetForcedMaterial(m); 1512 1513 for (int j = 0; j < objects.size(); ++ j) 1514 if (!objects[j]->Mailed()) 1515 { 1516 exporter->SetForcedMaterial(m); 1517 exporter->ExportIntersectable(objects[j]); 1518 objects[j]->Mail(); 1519 } 1520 } 1521 DEL_PTR(exporter); 1522 cout << "finished" << endl; 1523 } 1524 } 1525 else 1526 { 1527 ViewCellContainer viewCells; 1528 1529 mVspBspTree->CollectViewCells(viewCells); 1530 stable_sort(viewCells.begin(), viewCells.end(), vc_gt); 1531 1532 int limit = min(leafOut, (int)viewCells.size()); 1533 1534 for (int i = 0; i < limit; ++ i) 1535 { 1536 cout << "creating output for view cell " << i << " ... "; 1537 1538 Intersectable::NewMail(); 1539 BspViewCell *vc = dynamic_cast<BspViewCell *>(viewCells[i]); 1540 1541 cout << "creating output for view cell " << i << " ... "; 1542 // check whether we can add the current ray to the output rays 1543 for (int k = 0; k < raysOut; ++ k) 1544 { 1545 Ray *ray = sampleRays[k]; 1546 1547 for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j) 1548 { 1549 VspBspLeaf *leaf; // TODO = ray->bspIntersections[j].mLeaf; 1550 1551 if (vc == leaf->GetViewCell()) 1552 { 1553 vcRays[i].push_back(ray); 1554 } 1555 } 1556 } 1557 1558 //bspLeaves[j]->Mail(); 1559 char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i); 1560 1561 Exporter *exporter = Exporter::GetExporter(s); 1562 1563 exporter->SetWireframe(); 1564 1565 Material m;//= RandomMaterial(); 1566 m.mDiffuseColor = RgbColor(0, 1, 0); 1567 exporter->SetForcedMaterial(m); 1568 1569 if (vc->GetMesh()) 1570 exporter->ExportViewCell(vc); 1571 else 1572 { 1573 PolygonContainer cell; 1574 // export view cell 1575 mVspBspTree->ConstructGeometry(vc, cell); 1576 exporter->ExportPolygons(cell); 1577 CLEAR_CONTAINER(cell); 1578 } 1579 1580 1581 Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 1582 << ", piercing rays=" << (int)vcRays[i].size() << endl; 1583 1584 1585 // export rays piercing this view cell 1586 exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0)); 1587 1588 m.mDiffuseColor = RgbColor(1, 0, 0); 1589 exporter->SetForcedMaterial(m); 1590 1591 ViewCellPvsMap::const_iterator it, 1592 it_end = vc->GetPvs().mEntries.end(); 1593 1594 // output PVS of view cell 1595 for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it) 1596 { 1597 Intersectable *intersect = (*it).first; 1598 1599 if (!intersect->Mailed()) 1600 { 1601 Material m = RandomMaterial(); 1602 1603 exporter->SetForcedMaterial(m); 1604 1605 exporter->ExportIntersectable(intersect); 1606 intersect->Mail(); 1607 } 1608 } 1609 1610 DEL_PTR(exporter); 1611 cout << "finished" << endl; 1612 } 1613 } 1614 } 1615 1616 1617 bool VspBspViewCellsManager::MergeVspBspLeafViewCells(VspBspLeaf *front, 1618 VspBspLeaf *back) const 1619 { 1620 BspViewCell *viewCell = 1621 dynamic_cast<BspViewCell *>(MergeViewCells(*front->GetViewCell(), 1622 *back->GetViewCell())); 1623 1624 if (!viewCell) 1625 return false; 1626 1627 // change view cells of all leaves associated with the 1628 // previous view cells 1629 1630 BspViewCell *fVc = front->GetViewCell(); 1631 BspViewCell *bVc = back->GetViewCell(); 1632 1633 // TODO 1634 /*vector<VspBspLeaf *> fLeaves = fVc->mBspLeaves; 1635 vector<VspBspLeaf *> bLeaves = bVc->mBspLeaves; 1636 1637 vector<VspBspLeaf *>::const_iterator it; 1638 1639 for (it = fLeaves.begin(); it != fLeaves.end(); ++ it) 1640 { 1641 (*it)->SetViewCell(viewCell); 1642 viewCell->mBspLeaves.push_back(*it); 1643 } 1644 for (it = bLeaves.begin(); it != bLeaves.end(); ++ it) 1645 { 1646 (*it)->SetViewCell(viewCell); 1647 viewCell->mBspLeaves.push_back(*it); 1648 } 1649 1650 DEL_PTR(fVc); 1651 DEL_PTR(bVc); 1652 */ 1653 return true; 1654 } 1655 1656 bool VspBspViewCellsManager::ShouldMerge(VspBspLeaf *front, VspBspLeaf *back) const 1657 { 1658 ViewCell *fvc = front->GetViewCell(); 1659 ViewCell *bvc = back->GetViewCell(); 1660 1661 if ((fvc == mVspBspTree->GetRootCell()) || 1662 (bvc == mVspBspTree->GetRootCell()) || 1663 (fvc == bvc)) 1664 return false; 1665 1666 int fdiff = fvc->GetPvs().Diff(bvc->GetPvs()); 1667 1668 if (fvc->GetPvs().GetSize() + fdiff < mMaxPvs) 1669 { 1670 if ((fvc->GetPvs().GetSize() < mMinPvs) || 1671 (bvc->GetPvs().GetSize() < mMinPvs) || 1672 ((fdiff < mMinPvsDif) && (bvc->GetPvs().Diff(fvc->GetPvs()) < mMinPvsDif))) 1673 { 1674 return true; 1675 } 1676 } 1677 1678 return false; 1679 } 1680 -
trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
r441 r442 20 20 class VspKdTree; 21 21 class AxisAlignedBox3; 22 class VspBspLeaf; 23 22 24 /** 23 25 Manages different higher order operations on the view cells. … … 316 318 317 319 320 321 /** 322 Manages different higher order operations on the view cells. 323 */ 324 class VspBspViewCellsManager: public ViewCellsManager 325 { 326 327 public: 328 329 VspBspViewCellsManager(VspBspTree *tree, int constructionSamples); 330 331 int Construct(const ObjectContainer &objects, 332 const VssRayContainer &rays, 333 AxisAlignedBox3 *sceneBbox); 334 335 int ComputeSampleContributions(Ray &ray, const bool castRay = false); 336 337 int PostProcess(const ObjectContainer &objects, 338 const RayContainer &rays); 339 340 void Visualize(const ObjectContainer &objects, 341 const RayContainer &sampleRays); 342 343 int GetType() const; 344 345 ViewCell *GenerateViewCell(Mesh *mesh = NULL) const; 346 347 bool ViewCellsConstructed() const; 348 349 protected: 350 351 /** Merges view cells front and back leaf view cell. 352 */ 353 bool MergeVspBspLeafViewCells(VspBspLeaf *front, VspBspLeaf *back) const; 354 355 /** Returns true if front and back leaf should be merged. 356 */ 357 bool ShouldMerge(VspBspLeaf *front, VspBspLeaf *back) const; 358 359 /// the view space partition BSP tree. 360 VspBspTree *mVspBspTree; 361 362 363 private: 364 365 366 /** Exports visualization of the BSP splits. 367 */ 368 void ExportSplits(const ObjectContainer &objects, const RayContainer &sampleRays); 369 370 /** Exports visualization of the BSP PVS. 371 */ 372 void ExportBspPvs(const ObjectContainer &objects, const RayContainer &sampleRays); 373 374 }; 375 318 376 #endif -
trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
r441 r442 218 218 /****************************************************************/ 219 219 220 VspBspTree::VspBspTree( BspViewCell *viewCell):221 mRoot Cell(viewCell),222 m Root(NULL),223 mStoreLeavesWithRays(false), 224 mPvsUseLen(true) 225 { 220 VspBspTree::VspBspTree(): 221 mRoot(NULL), 222 mPvsUseArea(true) 223 { 224 mRootCell = new BspViewCell(); 225 226 226 Randomize(); // initialise random generator for heuristics 227 227 … … 349 349 { 350 350 DEL_PTR(mRoot); 351 DEL_PTR(mRootCell); 351 352 } 352 353 … … 485 486 486 487 VspBspTraversalData tData(mRoot, polys, 0, mRootCell, rays, 487 ComputePvsSize(*rays) );488 ComputePvsSize(*rays), cell->GetArea(), cell); 488 489 489 490 tStack.push(tData); … … 515 516 (((int)data.mRays->size() <= mTermMinRays) || 516 517 (data.mPvs <= mTermMinPvs) || 518 (data.mArea <= mTermMinArea) || 517 519 (data.mDepth >= mTermMaxDepth) || 518 520 (data.GetAvgRayContribution() < mTermMaxRayContribution)); … … 547 549 DEL_PTR(tData.mPolygons); 548 550 DEL_PTR(tData.mRays); 549 //DEL_PTR(tData.mGeometry);551 DEL_PTR(tData.mGeometry); 550 552 551 553 return leaf; … … 556 558 557 559 VspBspTraversalData tFrontData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell, 558 new RayInfoContainer(), 0 );560 new RayInfoContainer(), 0, 0, new VspBspNodeGeometry()); 559 561 VspBspTraversalData tBackData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell, 560 new RayInfoContainer(), 0 );562 new RayInfoContainer(), 0, 0, new VspBspNodeGeometry()); 561 563 562 564 // create new interior node and two leaf nodes … … 582 584 DEL_PTR(tData.mPolygons); 583 585 DEL_PTR(tData.mRays); 584 //DEL_PTR(tData.mGeometry);586 DEL_PTR(tData.mGeometry); 585 587 586 588 return interior; … … 615 617 frontData.mPvs = ComputePvsSize(*frontData.mRays); 616 618 backData.mPvs = ComputePvsSize(*backData.mRays); 619 620 // split geometry and compute area 621 if (1) 622 { 623 tData.mGeometry->SplitGeometry(*frontData.mGeometry, 624 *backData.mGeometry, 625 *this, 626 interior->mPlane); 627 628 629 frontData.mArea = frontData.mGeometry->GetArea(); 630 backData.mArea = backData.mGeometry->GetArea(); 631 } 617 632 618 633 // compute accumulated ray length … … 991 1006 float pBack = 0; 992 1007 1008 const bool pvsUseLen = false; 1009 993 1010 if (mSplitPlaneStrategy & PVS) 1011 { 994 1012 // create unique ids for pvs heuristics 995 1013 GenerateUniqueIdsForPvs(); 996 1014 1015 if (mPvsUseArea) // use front and back cell areas to approximate volume 1016 { 1017 // construct child geometry with regard to the candidate split plane 1018 VspBspNodeGeometry frontCell; 1019 VspBspNodeGeometry backCell; 1020 1021 data.mGeometry->SplitGeometry(frontCell, backCell, *this, candidatePlane); 1022 1023 pFront = frontCell.GetArea(); 1024 pBack = backCell.GetArea(); 1025 1026 pOverall = data.mArea; 1027 } 1028 } 997 1029 998 1030 RayInfoContainer::const_iterator rit, rit_end = data.mRays->end(); … … 1026 1058 AddObjToPvs(ray->mOriginObject, cf, frontPvs, backPvs); 1027 1059 1028 float len = 1; 1029 1030 if (mPvsUseLen) // use front and back cell areas to approximate volume 1031 len = (*rit).SqrSegmentLength(); 1060 // use number or length of rays to approximate volume 1061 if (!mPvsUseArea) 1062 { 1063 float len = 1; 1064 1065 if (pvsUseLen) // use length of rays 1066 len = (*rit).SqrSegmentLength(); 1032 1067 1033 pOverall += len; 1034 1035 // use length of rays to approximate volume 1036 if (cf == 1) 1037 pFront += len; 1038 if (cf == -1) 1039 pBack += len; 1040 if (cf == 0) 1041 { 1042 if (mPvsUseLen) 1068 pOverall += len; 1069 1070 if (cf == 1) 1071 pFront += len; 1072 if (cf == -1) 1073 pBack += len; 1074 if (cf == 0) 1043 1075 { 1044 float newLen = len * 1045 ((*rit).GetMaxT() - (*rit).mRay->mT) / 1046 ((*rit).GetMaxT() - (*rit).GetMinT()); 1047 1048 1049 if (candidatePlane.Side((*rit).ExtrapOrigin()) <= 0) 1076 // use length of rays to approximate volume 1077 if (pvsUseLen) 1050 1078 { 1051 pBack += newLen; 1052 pFront += len - newLen; 1079 float newLen = len * 1080 ((*rit).GetMaxT() - (*rit).mRay->mT) / 1081 ((*rit).GetMaxT() - (*rit).GetMinT()); 1082 1083 if (candidatePlane.Side((*rit).ExtrapOrigin()) <= 0) 1084 { 1085 pBack += newLen; 1086 pFront += len - newLen; 1087 } 1088 else 1089 { 1090 pFront += newLen; 1091 pBack += len - newLen; 1092 } 1053 1093 } 1054 1094 else 1055 1095 { 1056 pFront += newLen;1057 pBack += len - newLen;1096 ++ pFront; 1097 ++ pBack; 1058 1098 } 1059 1099 } 1060 else1061 {1062 ++ pFront;1063 ++ pBack;1064 }1065 1100 } 1066 1101 } … … 1083 1118 // give penalty to unbalanced split 1084 1119 if (0) 1085 if (((pFront * 0.2 + Limits::Small) > pBack) || (pFront < (pBack * 0.2 + Limits::Small))) 1120 if (((pFront * 0.2 + Limits::Small) > pBack) || 1121 (pFront < (pBack * 0.2 + Limits::Small))) 1086 1122 val += 0.5; 1087 1123 } … … 1199 1235 ++ mStat.maxRayContribNodes; 1200 1236 1237 if (data.mGeometry->GetArea() <= mTermMinArea) 1238 ++ mStat.minAreaNodes; 1201 1239 1202 1240 #ifdef _DEBUG … … 1984 2022 app << "===== END OF VspBspViewCells statistics ==========\n"; 1985 2023 } 2024 2025 BspViewCell *VspBspTree::GetRootCell() const 2026 { 2027 return mRootCell; 2028 } -
trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
r441 r442 349 349 /// rays piercing this node 350 350 RayInfoContainer *mRays; 351 /// area of current node 352 float mArea; 353 /// geometry of current node induced by split planes 354 VspBspNodeGeometry *mGeometry; 351 355 352 356 /// pvs size … … 367 371 mViewCell(NULL), 368 372 mRays(NULL), 369 mPvs(0) 373 mPvs(0), 374 mArea(0.0), 375 mGeometry(NULL) 370 376 {} 371 377 … … 375 381 ViewCell *viewCell, 376 382 RayInfoContainer *rays, 377 int pvs): 383 int pvs, 384 float area, 385 VspBspNodeGeometry *cell): 378 386 mNode(node), 379 387 mPolygons(polys), … … 381 389 mViewCell(viewCell), 382 390 mRays(rays), 383 mPvs(pvs) 391 mPvs(pvs), 392 mArea(area), 393 mGeometry(cell) 384 394 {} 385 395 }; … … 388 398 389 399 /** Default constructor creating an empty tree. 390 @param viewCell view cell corresponding to unbounded space391 400 */ 392 VspBspTree( BspViewCell *viewCell);401 VspBspTree(); 393 402 394 403 ~VspBspTree(); … … 478 487 void EvaluateViewCellsStats(VspBspViewCellsStatistics &stat) const; 479 488 489 490 /** Returns view cell corresponding to unbounded space. 491 */ 492 BspViewCell *GetRootCell() const; 480 493 481 494 protected: … … 731 744 float mCtDivCi; 732 745 733 /// if intersected leaves should be stored with a sample734 bool mStoreLeavesWithRays;735 736 746 /// axis aligned split criteria 737 747 float mAaCtDivCi; … … 752 762 753 763 /// if area or accumulated ray lenght should be used for PVS heuristics 754 bool mPvsUse Len;764 bool mPvsUseArea; 755 765 756 766 private:
Note: See TracChangeset
for help on using the changeset viewer.