Changeset 2598 for GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
- Timestamp:
- 01/15/08 15:37:02 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
r2593 r2598 116 116 mSamplesStat.Reset(); 117 117 //mStats.open("mystats.log"); 118 119 mRandomViewCellsHandler = new RandomViewCellsHandler(this); 118 120 } 119 121 … … 157 159 158 160 return (int)passSamples.size(); 161 } 162 163 164 ViewCellsManager::~ViewCellsManager() 165 { 166 // HACK: if view cells tree does not 167 // handle view cells, we have to do it here 168 // question: rather create view cells resource manager? 169 if (!ViewCellsTreeConstructed()) 170 { 171 CLEAR_CONTAINER(mViewCells); 172 } 173 else 174 { 175 DEL_PTR(mViewCellsTree); 176 } 177 178 DEL_PTR(mMixtureDistribution); 179 DEL_PTR(mRandomViewCellsHandler); 159 180 } 160 181 … … 341 362 } 342 363 return NULL; 343 }344 345 346 bool ViewCellsManager::ExportRandomViewCells(const string &filename,347 const vector<ViewCellPoints *> &viewCells)348 {349 std::ofstream outStream;350 outStream.open(filename.c_str());351 352 vector<ViewCellPoints *>::const_iterator vit, vit_end = viewCells.end();353 354 for (vit = viewCells.begin(); vit != vit_end; ++ vit)355 {356 ViewCell *vc = (*vit)->first;357 358 outStream << "v " << vc->GetId() << endl;359 360 SimpleRayContainer viewPoints;361 362 SimpleRayContainer::const_iterator pit, pit_end = (*vit)->second.end();363 364 for (pit = (*vit)->second.begin(); pit != pit_end; ++ pit)365 {366 const Vector3 pt = (*pit).mOrigin;367 const Vector3 dir = (*pit).mDirection;368 369 outStream << "p " << pt.x << " " << pt.y << " " << pt.z370 << " " << dir.x << " " << dir.y << " " << dir.z << endl;371 }372 }373 374 return true;375 }376 377 378 bool ViewCellsManager::GenerateRandomViewCells(vector<ViewCellPoints *> &viewCells,379 const int nViewCells,380 const int nViewPoints)381 {382 ViewCellContainer rViewCells;383 384 cout << "generating " << nViewCells << " random view cells" << endl;385 ///GenerateRandomViewCells(rViewCells, nViewCells);386 387 //cout << "finished" << endl;388 389 //for (int i = 0; i < viewCells.size(); ++ i)390 // cout << "vc " << i << ": " << viewCells[i]->GetId() << endl;391 392 cout << "generating " << nViewPoints << " view points per view cell" << endl;393 394 int generatedViewCells = 0;395 int i = 0;396 while (generatedViewCells < nViewCells)397 {398 ++ i;399 400 const int idx = (int)RandomValue(0.0f, (float)mViewCells.size() - 0.5f);401 402 ViewCell *viewCell = GetViewCell(idx);403 404 cout << "testing view cell: " << viewCell->GetId() << endl;405 406 if (!viewCell->Mailed())407 {408 viewCell->Mail();409 410 SimpleRayContainer viewPoints;411 412 // generate random view points413 if (IsValidViewSpace(viewCell) &&414 GenerateViewPoints(viewCell, nViewPoints, viewPoints))415 {416 Debug << "vc: " << viewCell->GetId() << endl;417 418 ViewCellPoints *vcPts = new ViewCellPoints();419 viewCells.push_back(vcPts);420 421 // valid view cell found422 vcPts->first = viewCell;423 424 // generate view points425 ++ generatedViewCells;426 427 SimpleRayContainer::const_iterator pit, pit_end = viewPoints.end();428 429 for (pit = viewPoints.begin(); pit != pit_end; ++ pit)430 {431 Debug << "vp: " << (*pit) << endl;432 vcPts->second.push_back(*pit);433 }434 cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;435 }436 else437 {438 cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;439 }440 }441 442 if (i > nViewCells * 1000000) // safety443 {444 cout << "big error" << endl;445 break;446 }447 cout << "processd view cells " << generatedViewCells << " of " << nViewCells << endl << endl;448 }449 450 return true;451 }452 453 454 bool ViewCellsManager::ImportRandomViewCells(const string &filename,455 vector<ViewCellPoints *> &viewCells)456 {457 ifstream inStream(filename.c_str());458 if (!inStream.is_open())459 return false;460 461 ViewCellPoints *currentViewCell = NULL;462 463 string buf;464 while (!(getline(inStream, buf)).eof())465 {466 switch (buf[0])467 {468 case 'v':469 {470 int id;471 sscanf(buf.c_str(), "v %d", &id);472 473 currentViewCell = new ViewCellPoints();474 currentViewCell->first = GetViewCellById(id);475 476 viewCells.push_back(currentViewCell);477 break;478 }479 case 'p':480 {481 Vector3 pt, dir;482 sscanf(buf.c_str(), "p %f %f %f %f %f %f", &pt.x, &pt.y, &pt.z, &dir.x, &dir.y, &dir.z);483 484 SimpleRay ray(pt, dir, 0, 1);485 currentViewCell->second.push_back(ray);486 break;487 }488 default:489 break;490 }491 }492 493 return true;494 }495 496 497 bool ViewCellsManager::ImportRandomViewCells(const string &filename)498 {499 return ImportRandomViewCells(filename, mViewCellPoints);500 }501 502 503 bool ViewCellsManager::GenerateViewPoints(ViewCell *viewCell,504 const int numViewPoints,505 SimpleRayContainer &viewPoints)506 {507 bool success = true;508 int generatedPts = 0;509 int i = 0;510 511 cout << "generating view points for view cell " << viewCell->GetId() << endl;512 513 while (generatedPts < numViewPoints)514 {515 SimpleRay pt;516 517 if (GenerateViewPoint(viewCell, pt))518 {519 ++ generatedPts;520 cout << "generated view point " << generatedPts << endl;521 viewPoints.push_back(pt);522 }523 524 // savety criterium525 if (++ i > numViewPoints * 3)526 {527 return false;528 }529 }530 531 cout << "view point generation finished" << endl;532 533 return true;534 }535 536 537 bool ViewCellsManager::ExportRandomViewCells(const string &filename)538 {539 const int numViewCells = 100;540 const int numViewPoints = 10;541 542 preprocessor->mViewCellsManager->543 GenerateRandomViewCells(mViewCellPoints, numViewCells, numViewPoints);544 545 //cout << "exporting random view cells" << endl;546 return preprocessor->mViewCellsManager->ExportRandomViewCells(filename, mViewCellPoints);547 }548 549 550 bool ViewCellsManager::GenerateViewPoint(ViewCell *viewCell,551 SimpleRay &ray)552 {553 // do not use this function since it could return different554 // viewpoints for different executions of the algorithm555 556 int tries = 0;557 Vector3 viewPoint, direction;558 const int maxTries = 10;559 560 while (1)561 {562 // hack563 if (!viewCell->GetMesh())564 CreateMesh(viewCell);565 566 Mesh *mesh = viewCell->GetMesh();567 AxisAlignedBox3 box = mesh->mBox;568 569 Vector3 pVector = Vector3(Random(1.0f),570 Random(1.0f),571 Random(1.0f));572 573 viewPoint = box.GetPoint(pVector);574 575 const Vector3 dVector = Vector3(2 * M_PI * Random(1.0f), M_PI * Random(1.0f), 0.0f);576 direction = Normalize(Vector3(sin(dVector.x), 0.0f, cos(dVector.x)));577 578 ViewCell *v = GetViewCell(viewPoint);579 580 if (v && v->GetValid())581 {582 mPreprocessor->GetRenderer()->mViewPoint = viewPoint;583 mPreprocessor->GetRenderer()->mViewDirection = direction;584 585 if (mPreprocessor->GetRenderer()->ValidViewPoint())586 {587 cout << "view point valid " << viewPoint << " " << direction << endl;588 break;589 }590 591 }592 593 if (++ tries > maxTries)594 {595 cerr << "error: no view point computed" << endl;596 return false;597 }598 }599 600 ray = SimpleRay(viewPoint, direction, 0, 1);601 //cout << "view point generated: " << viewPoint << " " << direction << endl;602 603 return true;604 }605 606 607 bool ViewCellsManager::IsValidViewSpace(ViewCell *vc)608 {609 SimpleRay simpleRay;610 //check if view point can be generated611 return GenerateViewPoint(vc, simpleRay);612 }613 614 615 bool ViewCellsManager::GenerateRandomViewCells(ViewCellContainer &viewCells,616 const int numViewCells)617 {618 int generatedViewCells = 0;619 //HaltonSequence halton;620 //float r[1];621 622 ViewCell::NewMail();623 624 while (generatedViewCells < numViewCells)625 {626 // savety criterium627 const int tries = 100000 + generatedViewCells;628 int i = 0;629 630 // generate next view cell631 while (1)632 {633 //halton.GetNext(1, r);634 //const int idx = (int)(r[0] * mViewCells.size() - 1.0f);635 const int idx = (int)RandomValue(0.0f, (float)mViewCells.size() - 0.5f);636 637 ViewCell *viewCell = GetViewCell(idx);638 639 if (!viewCell->Mailed())640 {641 viewCell->Mail();642 643 // check for valid view space644 if (IsValidViewSpace(viewCell))645 {646 // valid view cell found647 viewCells.push_back(viewCell);648 649 ++ generatedViewCells;650 //cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;651 break;652 }653 else654 {655 cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;656 }657 }658 659 if (++ i == tries) // no new view cell fond660 {661 cerr << "big error! no view cell found" << endl;662 return false;663 }664 }665 }666 667 return true;668 }669 670 671 ViewCellsManager::~ViewCellsManager()672 {673 // HACK: if view cells tree does not674 // handle view cells, we have to do it here675 // question: rather create view cells resource manager?676 if (!ViewCellsTreeConstructed())677 {678 CLEAR_CONTAINER(mViewCells);679 }680 else681 {682 DEL_PTR(mViewCellsTree);683 }684 685 DEL_PTR(mMixtureDistribution);686 CLEAR_CONTAINER(mViewCellPoints);687 364 } 688 365 … … 7387 7064 } 7388 7065 7389 } 7066 7067 ViewCellPointsList *ViewCellsManager::GetViewCellPointsList() 7068 { 7069 return mRandomViewCellsHandler->GetViewCellPointsList(); 7070 } 7071 7072 7073 bool ViewCellsManager::ExportRandomViewCells(const string &filename) 7074 { 7075 // export ten view cells with 100 random view points inside each 7076 const int numViewCells = 100; 7077 const int numViewPoints = 10; 7078 7079 //cout << "exporting random view cells" << endl; 7080 return mRandomViewCellsHandler->ExportRandomViewCells(filename); 7081 } 7082 7083 7084 bool ViewCellsManager::ImportViewCellsList(const string &filename) 7085 { 7086 return mRandomViewCellsHandler->ImportViewCellsList(filename); 7087 } 7088 7089 7090 }
Note: See TracChangeset
for help on using the changeset viewer.