#include "RandomViewCellsHandler.h" #include "ViewCellsManager.h" #include "ViewCellsParser.h" #include "Preprocessor.h" #include "GlRenderer.h" namespace GtpVisibilityPreprocessor { RandomViewCellsHandler::RandomViewCellsHandler(ViewCellsManager *vc): mViewCellsManager(vc) { } RandomViewCellsHandler::~RandomViewCellsHandler() { CLEAR_CONTAINER(mViewCellPointsList); } bool RandomViewCellsHandler::ExportRandomViewCells(const string &filename, const vector &viewCells) { std::ofstream outStream; outStream.open(filename.c_str()); vector::const_iterator vit, vit_end = viewCells.end(); for (vit = viewCells.begin(); vit != vit_end; ++ vit) { ViewCell *vc = (*vit)->first; outStream << "v " << vc->GetId() << endl; SimpleRayContainer viewPoints; SimpleRayContainer::const_iterator pit, pit_end = (*vit)->second.end(); for (pit = (*vit)->second.begin(); pit != pit_end; ++ pit) { const Vector3 pt = (*pit).mOrigin; const Vector3 dir = (*pit).mDirection; outStream << "p " << pt.x << " " << pt.y << " " << pt.z << " " << dir.x << " " << dir.y << " " << dir.z << endl; } } return true; } bool RandomViewCellsHandler::GenerateRandomViewCells(ViewCellPointsList &viewCells, const int nViewCells, const int nViewPoints) { ViewCellContainer rViewCells; cout << "generating " << nViewCells << " random view cells" << endl; ///GenerateRandomViewCells(rViewCells, nViewCells); //cout << "finished" << endl; //for (int i = 0; i < viewCells.size(); ++ i) // cout << "vc " << i << ": " << viewCells[i]->GetId() << endl; cout << "generating " << nViewPoints << " view points per view cell" << endl; int generatedViewCells = 0; int i = 0; while (generatedViewCells < nViewCells) { ++ i; const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetViewCells().size() - 0.5f); ViewCell *viewCell = mViewCellsManager->GetViewCell(idx); cout << "testing view cell: " << viewCell->GetId() << endl; if (!viewCell->Mailed()) { viewCell->Mail(); SimpleRayContainer viewPoints; // generate random view points if (IsValidViewSpace(viewCell) && GenerateViewPoints(viewCell, nViewPoints, viewPoints)) { Debug << "vc: " << viewCell->GetId() << endl; ViewCellPoints *vcPts = new ViewCellPoints(); viewCells.push_back(vcPts); // valid view cell found vcPts->first = viewCell; // generate view points ++ generatedViewCells; SimpleRayContainer::const_iterator pit, pit_end = viewPoints.end(); for (pit = viewPoints.begin(); pit != pit_end; ++ pit) { Debug << "vp: " << (*pit) << endl; vcPts->second.push_back(*pit); } cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl; } else { cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl; } } if (i > nViewCells * 1000000) // safety { cout << "big error" << endl; break; } cout << "processd view cells " << generatedViewCells << " of " << nViewCells << endl << endl; } return true; } bool RandomViewCellsHandler::ImportViewCellsList(const string &filename, vector &viewCells) { ifstream inStream(filename.c_str()); if (!inStream.is_open()) return false; ViewCellPoints *currentViewCell = NULL; AxisAlignedBox3 bbox = mViewCellsManager->GetViewSpaceBox(); string buf; while (!(getline(inStream, buf)).eof()) { switch (buf[0]) { case 'v': { int id; sscanf(buf.c_str(), "v %d", &id); currentViewCell = new ViewCellPoints(); currentViewCell->first = mViewCellsManager->GetViewCellById(id); viewCells.push_back(currentViewCell); break; } case 'p': { Vector3 pt, dir; sscanf(buf.c_str(), "p %f %f %f %f %f %f", &pt.x, &pt.y, &pt.z, &dir.x, &dir.y, &dir.z); // other variant: clamp to box bbox.ClampToBox(pt); SimpleRay ray(pt, dir, 0, 1); // no view cell specified => create dummy view cell if (!currentViewCell) { currentViewCell = new ViewCellPoints(); viewCells.push_back(currentViewCell); currentViewCell->first = NULL; } currentViewCell->second.push_back(ray); break; } default: break; } } return true; } bool RandomViewCellsHandler::ImportViewCellsList(const string &filename) { return ImportViewCellsList(filename, mViewCellPointsList); } bool RandomViewCellsHandler::GenerateViewPoints(ViewCell *viewCell, const int numViewPoints, SimpleRayContainer &viewPoints) { bool success = true; int generatedPts = 0; int i = 0; cout << "generating view points for view cell " << viewCell->GetId() << endl; while (generatedPts < numViewPoints) { SimpleRay pt; if (GenerateViewPoint(viewCell, pt)) { ++ generatedPts; cout << "generated view point " << generatedPts << endl; viewPoints.push_back(pt); } // savety criterium if (++ i > numViewPoints * 3) { return false; } } cout << "view point generation finished" << endl; return true; } bool RandomViewCellsHandler::ExportRandomViewCells(const string &filename) { // export ten view cells with 100 random view points inside each const int numViewCells = 100; // const int numViewPoints = 10; const int numViewPoints = 100; GenerateRandomViewCells(mViewCellPointsList, numViewCells, numViewPoints); //cout << "exporting random view cells" << endl; return ExportRandomViewCells(filename, mViewCellPointsList); } bool RandomViewCellsHandler::GenerateViewPoint(ViewCell *viewCell, SimpleRay &ray) { // do not use this function since it could return different // viewpoints for different executions of the algorithm int tries = 0; Vector3 viewPoint, direction; const int maxTries = 10; while (1) { // hack if (!viewCell->GetMesh()) mViewCellsManager->CreateMesh(viewCell); Mesh *mesh = viewCell->GetMesh(); AxisAlignedBox3 box = mesh->mBox; Vector3 pVector = Vector3(Random(1.0f), Random(1.0f), Random(1.0f)); viewPoint = box.GetPoint(pVector); const Vector3 dVector = Vector3(2.0f * M_PI * Random(1.0f), M_PI * Random(1.0f), 0.0f); direction = Normalize(Vector3(sin(dVector.x), 0.0f, cos(dVector.x))); ViewCell *v = mViewCellsManager->GetViewCell(viewPoint); GlRendererBuffer *renderer = mViewCellsManager->GetPreprocessor()->GetRenderer(); if (v && v->GetValid()) { renderer->SetViewPoint(viewPoint); renderer->SetViewDirection(direction); if (renderer->ValidViewPoint()) { cout << "view point valid " << viewPoint << " " << direction << endl; break; } } if (++ tries > maxTries) { cerr << "error: no view point computed" << endl; return false; } } ray = SimpleRay(viewPoint, direction, 0, 1); //cout << "view point generated: " << viewPoint << " " << direction << endl; return true; } bool RandomViewCellsHandler::IsValidViewSpace(ViewCell *vc) { SimpleRay simpleRay; //check if view point can be generated return GenerateViewPoint(vc, simpleRay); } bool RandomViewCellsHandler::GenerateRandomViewCells(ViewCellContainer &viewCells, const int numViewCells) { int generatedViewCells = 0; //HaltonSequence halton; //float r[1]; ViewCell::NewMail(); while (generatedViewCells < numViewCells) { // savety criterium const int tries = 100000 + generatedViewCells; int i = 0; // generate next view cell while (1) { //halton.GetNext(1, r); //const int idx = (int)(r[0] * mViewCells.size() - 1.0f); const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetViewCells().size() - 0.5f); ViewCell *viewCell = mViewCellsManager->GetViewCell(idx); if (!viewCell->Mailed()) { viewCell->Mail(); // check for valid view space if (IsValidViewSpace(viewCell)) { // valid view cell found viewCells.push_back(viewCell); ++ generatedViewCells; //cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl; break; } else { cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl; } } if (++ i == tries) // no new view cell fond { cerr << "big error! no view cell found" << endl; return false; } } } return true; } ViewCellPointsList *RandomViewCellsHandler::GetViewCellPointsList() { return &mViewCellPointsList; } }