[2598] | 1 | #include "RandomViewCellsHandler.h"
|
---|
| 2 | #include "ViewCellsManager.h"
|
---|
| 3 | #include "ViewCellsParser.h"
|
---|
| 4 | #include "Preprocessor.h"
|
---|
| 5 | #include "GlRenderer.h"
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | namespace GtpVisibilityPreprocessor {
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | RandomViewCellsHandler::RandomViewCellsHandler(ViewCellsManager *vc): mViewCellsManager(vc)
|
---|
| 12 | {
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | RandomViewCellsHandler::~RandomViewCellsHandler()
|
---|
| 17 | {
|
---|
| 18 | CLEAR_CONTAINER(mViewCellPointsList);
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | bool RandomViewCellsHandler::ExportRandomViewCells(const string &filename,
|
---|
| 23 | const vector<ViewCellPoints *> &viewCells)
|
---|
| 24 | {
|
---|
| 25 | std::ofstream outStream;
|
---|
| 26 | outStream.open(filename.c_str());
|
---|
| 27 |
|
---|
| 28 | vector<ViewCellPoints *>::const_iterator vit, vit_end = viewCells.end();
|
---|
| 29 |
|
---|
| 30 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
| 31 | {
|
---|
| 32 | ViewCell *vc = (*vit)->first;
|
---|
| 33 |
|
---|
| 34 | outStream << "v " << vc->GetId() << endl;
|
---|
| 35 |
|
---|
| 36 | SimpleRayContainer viewPoints;
|
---|
| 37 |
|
---|
| 38 | SimpleRayContainer::const_iterator pit, pit_end = (*vit)->second.end();
|
---|
| 39 |
|
---|
| 40 | for (pit = (*vit)->second.begin(); pit != pit_end; ++ pit)
|
---|
| 41 | {
|
---|
| 42 | const Vector3 pt = (*pit).mOrigin;
|
---|
| 43 | const Vector3 dir = (*pit).mDirection;
|
---|
| 44 |
|
---|
| 45 | outStream << "p " << pt.x << " " << pt.y << " " << pt.z
|
---|
| 46 | << " " << dir.x << " " << dir.y << " " << dir.z << endl;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | return true;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | bool RandomViewCellsHandler::GenerateRandomViewCells(ViewCellPointsList &viewCells,
|
---|
| 55 | const int nViewCells,
|
---|
| 56 | const int nViewPoints)
|
---|
| 57 | {
|
---|
| 58 | ViewCellContainer rViewCells;
|
---|
| 59 |
|
---|
| 60 | cout << "generating " << nViewCells << " random view cells" << endl;
|
---|
| 61 | ///GenerateRandomViewCells(rViewCells, nViewCells);
|
---|
| 62 |
|
---|
| 63 | //cout << "finished" << endl;
|
---|
| 64 |
|
---|
| 65 | //for (int i = 0; i < viewCells.size(); ++ i)
|
---|
| 66 | // cout << "vc " << i << ": " << viewCells[i]->GetId() << endl;
|
---|
| 67 |
|
---|
| 68 | cout << "generating " << nViewPoints << " view points per view cell" << endl;
|
---|
| 69 |
|
---|
| 70 | int generatedViewCells = 0;
|
---|
| 71 | int i = 0;
|
---|
| 72 | while (generatedViewCells < nViewCells)
|
---|
| 73 | {
|
---|
| 74 | ++ i;
|
---|
| 75 |
|
---|
| 76 | const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetViewCells().size() - 0.5f);
|
---|
| 77 |
|
---|
| 78 | ViewCell *viewCell = mViewCellsManager->GetViewCell(idx);
|
---|
| 79 |
|
---|
| 80 | cout << "testing view cell: " << viewCell->GetId() << endl;
|
---|
| 81 |
|
---|
| 82 | if (!viewCell->Mailed())
|
---|
| 83 | {
|
---|
| 84 | viewCell->Mail();
|
---|
| 85 |
|
---|
| 86 | SimpleRayContainer viewPoints;
|
---|
| 87 |
|
---|
| 88 | // generate random view points
|
---|
| 89 | if (IsValidViewSpace(viewCell) &&
|
---|
| 90 | GenerateViewPoints(viewCell, nViewPoints, viewPoints))
|
---|
| 91 | {
|
---|
| 92 | Debug << "vc: " << viewCell->GetId() << endl;
|
---|
| 93 |
|
---|
| 94 | ViewCellPoints *vcPts = new ViewCellPoints();
|
---|
| 95 | viewCells.push_back(vcPts);
|
---|
| 96 |
|
---|
| 97 | // valid view cell found
|
---|
| 98 | vcPts->first = viewCell;
|
---|
| 99 |
|
---|
| 100 | // generate view points
|
---|
| 101 | ++ generatedViewCells;
|
---|
| 102 |
|
---|
| 103 | SimpleRayContainer::const_iterator pit, pit_end = viewPoints.end();
|
---|
| 104 |
|
---|
| 105 | for (pit = viewPoints.begin(); pit != pit_end; ++ pit)
|
---|
| 106 | {
|
---|
| 107 | Debug << "vp: " << (*pit) << endl;
|
---|
| 108 | vcPts->second.push_back(*pit);
|
---|
| 109 | }
|
---|
| 110 | cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;
|
---|
| 111 | }
|
---|
| 112 | else
|
---|
| 113 | {
|
---|
| 114 | cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | if (i > nViewCells * 1000000) // safety
|
---|
| 119 | {
|
---|
| 120 | cout << "big error" << endl;
|
---|
| 121 | break;
|
---|
| 122 | }
|
---|
| 123 | cout << "processd view cells " << generatedViewCells << " of " << nViewCells << endl << endl;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | return true;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 | bool RandomViewCellsHandler::ImportViewCellsList(const string &filename,
|
---|
[2648] | 131 | vector<ViewCellPoints *> &viewCells)
|
---|
[2598] | 132 | {
|
---|
| 133 | ifstream inStream(filename.c_str());
|
---|
| 134 | if (!inStream.is_open())
|
---|
| 135 | return false;
|
---|
| 136 |
|
---|
| 137 | ViewCellPoints *currentViewCell = NULL;
|
---|
| 138 |
|
---|
[2686] | 139 | AxisAlignedBox3 bbox = mViewCellsManager->GetViewSpaceBox();
|
---|
| 140 |
|
---|
[2695] | 141 | string buf;
|
---|
| 142 |
|
---|
[2598] | 143 | while (!(getline(inStream, buf)).eof())
|
---|
| 144 | {
|
---|
| 145 | switch (buf[0])
|
---|
| 146 | {
|
---|
| 147 | case 'v':
|
---|
| 148 | {
|
---|
| 149 | int id;
|
---|
| 150 | sscanf(buf.c_str(), "v %d", &id);
|
---|
| 151 |
|
---|
| 152 | currentViewCell = new ViewCellPoints();
|
---|
| 153 | currentViewCell->first = mViewCellsManager->GetViewCellById(id);
|
---|
[2648] | 154 |
|
---|
[2686] | 155 | viewCells.push_back(currentViewCell);
|
---|
| 156 |
|
---|
[2598] | 157 | break;
|
---|
| 158 | }
|
---|
| 159 | case 'p':
|
---|
| 160 | {
|
---|
| 161 | Vector3 pt, dir;
|
---|
| 162 | sscanf(buf.c_str(), "p %f %f %f %f %f %f", &pt.x, &pt.y, &pt.z, &dir.x, &dir.y, &dir.z);
|
---|
| 163 |
|
---|
[2696] | 164 | // other variant: clamp to box
|
---|
| 165 | bbox.ClampToBox(pt);
|
---|
| 166 |
|
---|
[2598] | 167 | SimpleRay ray(pt, dir, 0, 1);
|
---|
[2648] | 168 |
|
---|
[2686] | 169 | // no view cell specified => create dummy view cell
|
---|
| 170 | if (!currentViewCell)
|
---|
| 171 | {
|
---|
| 172 | currentViewCell = new ViewCellPoints();
|
---|
| 173 | viewCells.push_back(currentViewCell);
|
---|
| 174 | currentViewCell->first = NULL;
|
---|
| 175 | }
|
---|
[2648] | 176 |
|
---|
[2686] | 177 | currentViewCell->second.push_back(ray);
|
---|
| 178 |
|
---|
[2598] | 179 | break;
|
---|
| 180 | }
|
---|
| 181 | default:
|
---|
| 182 | break;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | return true;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | bool RandomViewCellsHandler::ImportViewCellsList(const string &filename)
|
---|
| 191 | {
|
---|
| 192 | return ImportViewCellsList(filename, mViewCellPointsList);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 |
|
---|
| 196 | bool RandomViewCellsHandler::GenerateViewPoints(ViewCell *viewCell,
|
---|
| 197 | const int numViewPoints,
|
---|
| 198 | SimpleRayContainer &viewPoints)
|
---|
| 199 | {
|
---|
| 200 | bool success = true;
|
---|
| 201 | int generatedPts = 0;
|
---|
| 202 | int i = 0;
|
---|
| 203 |
|
---|
| 204 | cout << "generating view points for view cell " << viewCell->GetId() << endl;
|
---|
| 205 |
|
---|
| 206 | while (generatedPts < numViewPoints)
|
---|
| 207 | {
|
---|
| 208 | SimpleRay pt;
|
---|
| 209 |
|
---|
| 210 | if (GenerateViewPoint(viewCell, pt))
|
---|
| 211 | {
|
---|
| 212 | ++ generatedPts;
|
---|
| 213 | cout << "generated view point " << generatedPts << endl;
|
---|
| 214 | viewPoints.push_back(pt);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | // savety criterium
|
---|
| 218 | if (++ i > numViewPoints * 3)
|
---|
| 219 | {
|
---|
| 220 | return false;
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | cout << "view point generation finished" << endl;
|
---|
| 225 |
|
---|
| 226 | return true;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 |
|
---|
| 230 | bool RandomViewCellsHandler::ExportRandomViewCells(const string &filename)
|
---|
| 231 | {
|
---|
| 232 | // export ten view cells with 100 random view points inside each
|
---|
[2647] | 233 | const int numViewCells = 100;
|
---|
| 234 | // const int numViewPoints = 10;
|
---|
| 235 | const int numViewPoints = 100;
|
---|
[2598] | 236 |
|
---|
| 237 | GenerateRandomViewCells(mViewCellPointsList, numViewCells, numViewPoints);
|
---|
| 238 |
|
---|
| 239 | //cout << "exporting random view cells" << endl;
|
---|
| 240 | return ExportRandomViewCells(filename, mViewCellPointsList);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | bool RandomViewCellsHandler::GenerateViewPoint(ViewCell *viewCell,
|
---|
| 245 | SimpleRay &ray)
|
---|
| 246 | {
|
---|
| 247 | // do not use this function since it could return different
|
---|
| 248 | // viewpoints for different executions of the algorithm
|
---|
| 249 |
|
---|
| 250 | int tries = 0;
|
---|
| 251 | Vector3 viewPoint, direction;
|
---|
| 252 | const int maxTries = 10;
|
---|
| 253 |
|
---|
| 254 | while (1)
|
---|
| 255 | {
|
---|
| 256 | // hack
|
---|
| 257 | if (!viewCell->GetMesh())
|
---|
| 258 | mViewCellsManager->CreateMesh(viewCell);
|
---|
| 259 |
|
---|
| 260 | Mesh *mesh = viewCell->GetMesh();
|
---|
| 261 | AxisAlignedBox3 box = mesh->mBox;
|
---|
| 262 |
|
---|
| 263 | Vector3 pVector = Vector3(Random(1.0f),
|
---|
| 264 | Random(1.0f),
|
---|
| 265 | Random(1.0f));
|
---|
| 266 |
|
---|
| 267 | viewPoint = box.GetPoint(pVector);
|
---|
| 268 |
|
---|
[2695] | 269 | const Vector3 dVector = Vector3(2.0f * M_PI * Random(1.0f), M_PI * Random(1.0f), 0.0f);
|
---|
[2598] | 270 | direction = Normalize(Vector3(sin(dVector.x), 0.0f, cos(dVector.x)));
|
---|
| 271 |
|
---|
| 272 | ViewCell *v = mViewCellsManager->GetViewCell(viewPoint);
|
---|
| 273 |
|
---|
| 274 | GlRendererBuffer *renderer = mViewCellsManager->GetPreprocessor()->GetRenderer();
|
---|
| 275 |
|
---|
| 276 | if (v && v->GetValid())
|
---|
| 277 | {
|
---|
| 278 | renderer->SetViewPoint(viewPoint);
|
---|
| 279 | renderer->SetViewDirection(direction);
|
---|
| 280 |
|
---|
| 281 | if (renderer->ValidViewPoint())
|
---|
| 282 | {
|
---|
| 283 | cout << "view point valid " << viewPoint << " " << direction << endl;
|
---|
| 284 | break;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | if (++ tries > maxTries)
|
---|
| 290 | {
|
---|
| 291 | cerr << "error: no view point computed" << endl;
|
---|
| 292 | return false;
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | ray = SimpleRay(viewPoint, direction, 0, 1);
|
---|
| 297 | //cout << "view point generated: " << viewPoint << " " << direction << endl;
|
---|
| 298 |
|
---|
| 299 | return true;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 |
|
---|
| 303 | bool RandomViewCellsHandler::IsValidViewSpace(ViewCell *vc)
|
---|
| 304 | {
|
---|
| 305 | SimpleRay simpleRay;
|
---|
| 306 | //check if view point can be generated
|
---|
| 307 | return GenerateViewPoint(vc, simpleRay);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 |
|
---|
| 311 | bool RandomViewCellsHandler::GenerateRandomViewCells(ViewCellContainer &viewCells,
|
---|
| 312 | const int numViewCells)
|
---|
| 313 | {
|
---|
| 314 | int generatedViewCells = 0;
|
---|
| 315 | //HaltonSequence halton;
|
---|
| 316 | //float r[1];
|
---|
| 317 |
|
---|
| 318 | ViewCell::NewMail();
|
---|
| 319 |
|
---|
| 320 | while (generatedViewCells < numViewCells)
|
---|
| 321 | {
|
---|
| 322 | // savety criterium
|
---|
| 323 | const int tries = 100000 + generatedViewCells;
|
---|
| 324 | int i = 0;
|
---|
| 325 |
|
---|
| 326 | // generate next view cell
|
---|
| 327 | while (1)
|
---|
| 328 | {
|
---|
| 329 | //halton.GetNext(1, r);
|
---|
| 330 | //const int idx = (int)(r[0] * mViewCells.size() - 1.0f);
|
---|
| 331 | const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetViewCells().size() - 0.5f);
|
---|
| 332 |
|
---|
| 333 | ViewCell *viewCell = mViewCellsManager->GetViewCell(idx);
|
---|
| 334 |
|
---|
| 335 | if (!viewCell->Mailed())
|
---|
| 336 | {
|
---|
| 337 | viewCell->Mail();
|
---|
| 338 |
|
---|
| 339 | // check for valid view space
|
---|
| 340 | if (IsValidViewSpace(viewCell))
|
---|
| 341 | {
|
---|
| 342 | // valid view cell found
|
---|
| 343 | viewCells.push_back(viewCell);
|
---|
| 344 |
|
---|
| 345 | ++ generatedViewCells;
|
---|
| 346 | //cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;
|
---|
| 347 | break;
|
---|
| 348 | }
|
---|
| 349 | else
|
---|
| 350 | {
|
---|
| 351 | cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | if (++ i == tries) // no new view cell fond
|
---|
| 356 | {
|
---|
| 357 | cerr << "big error! no view cell found" << endl;
|
---|
| 358 | return false;
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | return true;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 |
|
---|
| 367 | ViewCellPointsList *RandomViewCellsHandler::GetViewCellPointsList()
|
---|
| 368 | {
|
---|
| 369 | return &mViewCellPointsList;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | }
|
---|