Changeset 1006 for GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
- Timestamp:
- 06/09/06 01:26:46 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
r1004 r1006 8 8 #include "ViewCellBsp.h" 9 9 #include "KdTree.h" 10 #include "VspKdTree.h"10 //#include "VspOspTree.h" 11 11 #include "Exporter.h" 12 12 #include "VspBspTree.h" … … 1828 1828 Vector3 termination = hray.Extrap(tmax); 1829 1829 1830 // traverse the view space subdivision 1830 1831 CastLineSegment(origin, termination, viewcells); 1831 1832 1832 // copy viewcells memory efficiently1833 1833 //const bool storeViewcells = !addRays; 1834 1834 //return 0; 1835 1835 if (storeViewCells) 1836 1836 { 1837 // copy viewcells memory efficiently 1837 1838 ray.mViewCells.reserve(viewcells.size()); 1838 1839 ray.mViewCells = viewcells; … … 2090 2091 } 2091 2092 2092 2093 2093 return true; 2094 2094 } … … 2173 2173 2174 2174 2175 void ViewCellsManager::SetScalarPvsSize(ViewCell *vc, const int pvsSize) const 2175 void ViewCellsManager::SetScalarPvsSize(ViewCell *vc, 2176 const int pvsSize) const 2176 2177 { 2177 2178 vc->mPvsSize = pvsSize; … … 3346 3347 } 3347 3348 3348 3349 /**********************************************************************/3350 /* VspKdViewCellsManager implementation */3351 /**********************************************************************/3352 3353 3354 VspKdViewCellsManager::VspKdViewCellsManager(VspKdTree *vspKdTree):3355 ViewCellsManager(), mVspKdTree(vspKdTree)3356 {3357 Environment::GetSingleton()->GetIntValue("VspKdTree.Construction.samples", mInitialSamples);3358 mVspKdTree->SetViewCellsManager(this);3359 }3360 3361 float VspKdViewCellsManager::GetProbability(ViewCell *viewCell)3362 {3363 // compute view cell area / volume as subsititute for probability3364 if (0)3365 return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();3366 else3367 return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();3368 }3369 3370 3371 3372 3373 void VspKdViewCellsManager::CollectViewCells()3374 {3375 mVspKdTree->CollectViewCells(mViewCells);3376 }3377 3378 3379 int VspKdViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,3380 const VssRayContainer &rays)3381 {3382 // if view cells already constructed3383 if (ViewCellsConstructed())3384 return 0;3385 3386 VssRayContainer constructionRays;3387 VssRayContainer savedRays;3388 3389 GetRaySets(rays,3390 mInitialSamples,3391 constructionRays,3392 &savedRays);3393 3394 Debug << "constructing vsp kd tree using "3395 << (int)constructionRays.size() << " samples" << endl;3396 3397 mVspKdTree->Construct(constructionRays, &mViewSpaceBox);3398 Debug << mVspKdTree->GetStatistics() << endl;3399 3400 // export leaf building blocks3401 ExportLeaves(objects, rays);3402 3403 // finally merge kd leaf building blocks to view cells3404 const int merged = mVspKdTree->MergeViewCells(rays);3405 3406 // collapse siblings belonging to the same view cell3407 mVspKdTree->RefineViewCells(rays);3408 3409 // collapse siblings belonging to the same view cell3410 mVspKdTree->CollapseTree();3411 3412 // evaluale view cell stats3413 ResetViewCells();3414 3415 Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;3416 3417 long startTime = GetTime();3418 3419 // recast rest of rays3420 ComputeSampleContributions(savedRays, true, false);3421 3422 Debug << "Computed remaining ray contribution in " << TimeDiff(startTime, GetTime()) * 1e-33423 << " secs" << endl;3424 3425 return merged;3426 }3427 3428 bool VspKdViewCellsManager::ViewCellsConstructed() const3429 {3430 return mVspKdTree->GetRoot() != NULL;3431 }3432 3433 3434 ViewCell *VspKdViewCellsManager::GenerateViewCell(Mesh *mesh) const3435 {3436 return new VspKdViewCell(mesh);3437 }3438 3439 int VspKdViewCellsManager::PostProcess(const ObjectContainer &objects,3440 const VssRayContainer &rays)3441 {3442 if (!ViewCellsConstructed())3443 return 0;3444 3445 // recalculate stats3446 EvaluateViewCellsStats();3447 3448 return 0;3449 }3450 3451 3452 void VspKdViewCellsManager::ExportLeaves(const ObjectContainer &objects,3453 const VssRayContainer &sampleRays)3454 {3455 if (!ViewCellsConstructed())3456 return;3457 3458 //-- export leaf building blocks3459 Exporter *exporter = Exporter::GetExporter("vspkdtree.x3d");3460 if (!exporter)3461 return;3462 3463 if (mExportGeometry)3464 exporter->ExportGeometry(objects);3465 3466 //exporter->SetWireframe();3467 //exporter->ExportVspKdTree(*mVspKdTree, mVspKdTree->GetStatistics().maxPvsSize);3468 exporter->ExportVspKdTree(*mVspKdTree);3469 3470 if (mExportRays)3471 {3472 const float prob = (float)mVisualizationSamples3473 / ((float)sampleRays.size() + Limits::Small);3474 3475 exporter->SetWireframe();3476 3477 //-- collect uniformly distributed rays3478 VssRayContainer rays;3479 3480 for (int i = 0; i < sampleRays.size(); ++ i)3481 {3482 if (RandomValue(0,1) < prob)3483 rays.push_back(sampleRays[i]);3484 }3485 exporter->ExportRays(rays, RgbColor(1, 0, 0));3486 }3487 3488 delete exporter;3489 }3490 3491 3492 void VspKdViewCellsManager::Visualize(const ObjectContainer &objects,3493 const VssRayContainer &sampleRays)3494 {3495 if (!ViewCellsConstructed())3496 return;3497 3498 //-- export single view cells3499 for (int i = 0; i < 10; ++ i)3500 {3501 char s[64];3502 sprintf(s, "vsp_viewcell%04d.x3d", i);3503 Exporter *exporter = Exporter::GetExporter(s);3504 const int idx =3505 (int)RandomValue(0.0, (Real)((int)mViewCells.size() - 1));3506 3507 VspKdViewCell *vc = dynamic_cast<VspKdViewCell *>(mViewCells[idx]);3508 3509 //-- export geometry3510 Material m;3511 m.mDiffuseColor = RgbColor(0, 1, 1);3512 3513 exporter->SetForcedMaterial(m);3514 exporter->SetWireframe();3515 3516 ExportViewCellGeometry(exporter, vc);3517 3518 //-- export stored rays3519 3520 if (mExportRays)3521 {3522 ViewCellContainer leaves;3523 mViewCellsTree->CollectLeaves(vc, leaves);3524 3525 ViewCellContainer::const_iterator it,3526 it_end = leaves.end();3527 3528 for (it = leaves.begin(); it != it_end; ++ it)3529 {3530 VspKdViewCell *vspKdVc = dynamic_cast<VspKdViewCell *>(*it);3531 VspKdLeaf *leaf = vspKdVc->mLeaf;3532 AxisAlignedBox3 box = mVspKdTree->GetBBox(leaf);3533 3534 VssRayContainer vssRays;3535 3536 VssRayContainer castRays;3537 VssRayContainer initRays;3538 3539 leaf->GetRays(vssRays);3540 3541 VssRayContainer::const_iterator it, it_end = vssRays.end();3542 const float prop = 200.0f / (float)vssRays.size();3543 3544 for (it = vssRays.begin(); it != it_end; ++ it)3545 {3546 if (Random(1) < prop)3547 if ((*it)->mTerminationObject == NULL)3548 castRays.push_back(*it);3549 else3550 initRays.push_back(*it);3551 }3552 3553 exporter->ExportRays(castRays, RgbColor(1, 0, 0));3554 exporter->ExportRays(initRays, RgbColor(0, 1, 0));3555 }3556 }3557 3558 //-- output PVS of view cell3559 m.mDiffuseColor = RgbColor(1, 0, 0);3560 exporter->SetForcedMaterial(m);3561 3562 Intersectable::NewMail();3563 3564 ObjectPvsMap::const_iterator it,3565 it_end = vc->GetPvs().mEntries.end();3566 3567 exporter->SetFilled();3568 3569 for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)3570 {3571 Intersectable *intersect = (*it).first;3572 3573 if (!intersect->Mailed())3574 {3575 Material m = RandomMaterial();3576 exporter->SetForcedMaterial(m);3577 3578 exporter->ExportIntersectable(intersect);3579 intersect->Mail();3580 }3581 }3582 3583 delete exporter;3584 }3585 3586 //-- export final view cells3587 Exporter *exporter = Exporter::GetExporter("vspkdtree_merged.x3d");3588 3589 3590 ExportViewCellsForViz(exporter);3591 3592 if (mExportGeometry)3593 {3594 exporter->SetFilled();3595 exporter->ExportGeometry(objects);3596 }3597 3598 if (mExportRays)3599 {3600 const float prob = (float)mVisualizationSamples3601 / ((float)sampleRays.size() + Limits::Small);3602 3603 exporter->SetWireframe();3604 3605 VssRayContainer rays;3606 3607 for (int i = 0; i < sampleRays.size(); ++ i)3608 {3609 if (RandomValue(0,1) < prob)3610 rays.push_back(sampleRays[i]);3611 }3612 exporter->ExportRays(rays, RgbColor(1, 0, 0));3613 }3614 3615 delete exporter;3616 }3617 3618 int VspKdViewCellsManager::GetType() const3619 {3620 return VSP_KD;3621 }3622 3623 3624 int VspKdViewCellsManager::CastLineSegment(const Vector3 &origin,3625 const Vector3 &termination,3626 ViewCellContainer &viewcells)3627 {3628 return mVspKdTree->CastLineSegment(origin, termination, viewcells);3629 }3630 3631 3632 void VspKdViewCellsManager::ExportColor(Exporter *exporter,3633 ViewCell *vc) const3634 {3635 if (mColorCode == 0) // Random color3636 return;3637 3638 float importance = 0;3639 3640 switch (mColorCode)3641 {3642 case 1: // pvs3643 {3644 importance = (float)mViewCellsTree->GetPvsSize(vc) /3645 (float)mCurrentViewCellsStats.maxPvs;3646 }3647 break;3648 case 2: // merged leaves3649 {3650 const int lSize = mViewCellsTree->GetNumInitialViewCells(vc);3651 importance = (float)lSize /3652 (float)mCurrentViewCellsStats.maxLeaves;3653 }3654 break;3655 case 3: // merged tree depth difference3656 {3657 //importance = (float)GetMaxTreeDiff(vc) /3658 // (float)(mVspBspTree->GetStatistics().maxDepth * 2);3659 }3660 break;3661 default:3662 break;3663 }3664 3665 Material m;3666 m.mDiffuseColor.b = 1.0f;3667 m.mDiffuseColor.r = importance;3668 m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r;3669 //Debug << "importance: " << importance << endl;3670 exporter->SetForcedMaterial(m);3671 }3672 3673 3674 void VspKdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,3675 ViewCell *vc,3676 const Plane3 *clipPlane) const3677 {3678 VspKdViewCell *kdVc = dynamic_cast<VspKdViewCell *>(vc);3679 3680 Mesh m;3681 3682 ViewCellContainer leaves;3683 mViewCellsTree->CollectLeaves(vc, leaves);3684 3685 ViewCellContainer::const_iterator it, it_end = leaves.end();3686 3687 for (it = leaves.begin(); it != it_end; ++ it)3688 {3689 VspKdLeaf *l = dynamic_cast<VspKdViewCell *>(*it)->mLeaf;3690 IncludeBoxInMesh(mVspKdTree->GetBBox(l), m);3691 }3692 3693 exporter->ExportMesh(&m);3694 }3695 3696 3697 void VspKdViewCellsManager::CreateMesh(ViewCell *vc)3698 {3699 //TODO3700 }3701 3702 3703 void VspKdViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,3704 vector<MergeCandidate> &candidates)3705 {3706 // TODO3707 }3708 3709 3710 3349 /**************************************************************************/ 3711 3350 /* VspBspViewCellsManager implementation */ … … 4343 3982 4344 3983 //exporter->SetFilled(); 4345 bool b = mUseClipPlaneForViz; 3984 // HACK: export without clip plane 3985 const bool b = mUseClipPlaneForViz; 4346 3986 mUseClipPlaneForViz = false; 4347 3987 ExportViewCellsForViz(exporter);
Note: See TracChangeset
for help on using the changeset viewer.