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,
|
---|
131 | vector<ViewCellPoints *> &viewCells)
|
---|
132 | {
|
---|
133 | ifstream inStream(filename.c_str());
|
---|
134 | if (!inStream.is_open())
|
---|
135 | return false;
|
---|
136 |
|
---|
137 | ViewCellPoints *currentViewCell = NULL;
|
---|
138 |
|
---|
139 | AxisAlignedBox3 bbox = mViewCellsManager->GetViewSpaceBox();
|
---|
140 |
|
---|
141 | string buf;
|
---|
142 | while (!(getline(inStream, buf)).eof())
|
---|
143 | {
|
---|
144 | switch (buf[0])
|
---|
145 | {
|
---|
146 | case 'v':
|
---|
147 | {
|
---|
148 | int id;
|
---|
149 | sscanf(buf.c_str(), "v %d", &id);
|
---|
150 |
|
---|
151 | currentViewCell = new ViewCellPoints();
|
---|
152 | currentViewCell->first = mViewCellsManager->GetViewCellById(id);
|
---|
153 |
|
---|
154 | viewCells.push_back(currentViewCell);
|
---|
155 |
|
---|
156 | break;
|
---|
157 | }
|
---|
158 | case 'p':
|
---|
159 | {
|
---|
160 | Vector3 pt, dir;
|
---|
161 | sscanf(buf.c_str(), "p %f %f %f %f %f %f", &pt.x, &pt.y, &pt.z, &dir.x, &dir.y, &dir.z);
|
---|
162 |
|
---|
163 | if (0)
|
---|
164 | {
|
---|
165 | // add new point to view space box
|
---|
166 | if (!bbox.IsInside(pt))
|
---|
167 | bbox.Include(pt);
|
---|
168 | }
|
---|
169 | else
|
---|
170 | {
|
---|
171 | // other variant: clamp to box
|
---|
172 | bbox.ClampToBox(pt);
|
---|
173 | }
|
---|
174 |
|
---|
175 | SimpleRay ray(pt, dir, 0, 1);
|
---|
176 |
|
---|
177 | // no view cell specified => create dummy view cell
|
---|
178 | if (!currentViewCell)
|
---|
179 | {
|
---|
180 | currentViewCell = new ViewCellPoints();
|
---|
181 | viewCells.push_back(currentViewCell);
|
---|
182 | currentViewCell->first = NULL;
|
---|
183 | }
|
---|
184 |
|
---|
185 | currentViewCell->second.push_back(ray);
|
---|
186 |
|
---|
187 | break;
|
---|
188 | }
|
---|
189 | default:
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | // set updated view space box containing all view points
|
---|
195 | mViewCellsManager->SetViewSpaceBox(bbox);
|
---|
196 |
|
---|
197 | return true;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | bool RandomViewCellsHandler::ImportViewCellsList(const string &filename)
|
---|
202 | {
|
---|
203 | return ImportViewCellsList(filename, mViewCellPointsList);
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | bool RandomViewCellsHandler::GenerateViewPoints(ViewCell *viewCell,
|
---|
208 | const int numViewPoints,
|
---|
209 | SimpleRayContainer &viewPoints)
|
---|
210 | {
|
---|
211 | bool success = true;
|
---|
212 | int generatedPts = 0;
|
---|
213 | int i = 0;
|
---|
214 |
|
---|
215 | cout << "generating view points for view cell " << viewCell->GetId() << endl;
|
---|
216 |
|
---|
217 | while (generatedPts < numViewPoints)
|
---|
218 | {
|
---|
219 | SimpleRay pt;
|
---|
220 |
|
---|
221 | if (GenerateViewPoint(viewCell, pt))
|
---|
222 | {
|
---|
223 | ++ generatedPts;
|
---|
224 | cout << "generated view point " << generatedPts << endl;
|
---|
225 | viewPoints.push_back(pt);
|
---|
226 | }
|
---|
227 |
|
---|
228 | // savety criterium
|
---|
229 | if (++ i > numViewPoints * 3)
|
---|
230 | {
|
---|
231 | return false;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | cout << "view point generation finished" << endl;
|
---|
236 |
|
---|
237 | return true;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | bool RandomViewCellsHandler::ExportRandomViewCells(const string &filename)
|
---|
242 | {
|
---|
243 | // export ten view cells with 100 random view points inside each
|
---|
244 | const int numViewCells = 100;
|
---|
245 | // const int numViewPoints = 10;
|
---|
246 | const int numViewPoints = 100;
|
---|
247 |
|
---|
248 | GenerateRandomViewCells(mViewCellPointsList, numViewCells, numViewPoints);
|
---|
249 |
|
---|
250 | //cout << "exporting random view cells" << endl;
|
---|
251 | return ExportRandomViewCells(filename, mViewCellPointsList);
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | bool RandomViewCellsHandler::GenerateViewPoint(ViewCell *viewCell,
|
---|
256 | SimpleRay &ray)
|
---|
257 | {
|
---|
258 | // do not use this function since it could return different
|
---|
259 | // viewpoints for different executions of the algorithm
|
---|
260 |
|
---|
261 | int tries = 0;
|
---|
262 | Vector3 viewPoint, direction;
|
---|
263 | const int maxTries = 10;
|
---|
264 |
|
---|
265 | while (1)
|
---|
266 | {
|
---|
267 | // hack
|
---|
268 | if (!viewCell->GetMesh())
|
---|
269 | mViewCellsManager->CreateMesh(viewCell);
|
---|
270 |
|
---|
271 | Mesh *mesh = viewCell->GetMesh();
|
---|
272 | AxisAlignedBox3 box = mesh->mBox;
|
---|
273 |
|
---|
274 | Vector3 pVector = Vector3(Random(1.0f),
|
---|
275 | Random(1.0f),
|
---|
276 | Random(1.0f));
|
---|
277 |
|
---|
278 | viewPoint = box.GetPoint(pVector);
|
---|
279 |
|
---|
280 | const Vector3 dVector = Vector3(2 * M_PI * Random(1.0f), M_PI * Random(1.0f), 0.0f);
|
---|
281 | direction = Normalize(Vector3(sin(dVector.x), 0.0f, cos(dVector.x)));
|
---|
282 |
|
---|
283 | ViewCell *v = mViewCellsManager->GetViewCell(viewPoint);
|
---|
284 |
|
---|
285 | GlRendererBuffer *renderer = mViewCellsManager->GetPreprocessor()->GetRenderer();
|
---|
286 |
|
---|
287 | if (v && v->GetValid())
|
---|
288 | {
|
---|
289 | renderer->SetViewPoint(viewPoint);
|
---|
290 | renderer->SetViewDirection(direction);
|
---|
291 |
|
---|
292 | if (renderer->ValidViewPoint())
|
---|
293 | {
|
---|
294 | cout << "view point valid " << viewPoint << " " << direction << endl;
|
---|
295 | break;
|
---|
296 | }
|
---|
297 |
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (++ tries > maxTries)
|
---|
301 | {
|
---|
302 | cerr << "error: no view point computed" << endl;
|
---|
303 | return false;
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | ray = SimpleRay(viewPoint, direction, 0, 1);
|
---|
308 | //cout << "view point generated: " << viewPoint << " " << direction << endl;
|
---|
309 |
|
---|
310 | return true;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | bool RandomViewCellsHandler::IsValidViewSpace(ViewCell *vc)
|
---|
315 | {
|
---|
316 | SimpleRay simpleRay;
|
---|
317 | //check if view point can be generated
|
---|
318 | return GenerateViewPoint(vc, simpleRay);
|
---|
319 | }
|
---|
320 |
|
---|
321 |
|
---|
322 | bool RandomViewCellsHandler::GenerateRandomViewCells(ViewCellContainer &viewCells,
|
---|
323 | const int numViewCells)
|
---|
324 | {
|
---|
325 | int generatedViewCells = 0;
|
---|
326 | //HaltonSequence halton;
|
---|
327 | //float r[1];
|
---|
328 |
|
---|
329 | ViewCell::NewMail();
|
---|
330 |
|
---|
331 | while (generatedViewCells < numViewCells)
|
---|
332 | {
|
---|
333 | // savety criterium
|
---|
334 | const int tries = 100000 + generatedViewCells;
|
---|
335 | int i = 0;
|
---|
336 |
|
---|
337 | // generate next view cell
|
---|
338 | while (1)
|
---|
339 | {
|
---|
340 | //halton.GetNext(1, r);
|
---|
341 | //const int idx = (int)(r[0] * mViewCells.size() - 1.0f);
|
---|
342 | const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetViewCells().size() - 0.5f);
|
---|
343 |
|
---|
344 | ViewCell *viewCell = mViewCellsManager->GetViewCell(idx);
|
---|
345 |
|
---|
346 | if (!viewCell->Mailed())
|
---|
347 | {
|
---|
348 | viewCell->Mail();
|
---|
349 |
|
---|
350 | // check for valid view space
|
---|
351 | if (IsValidViewSpace(viewCell))
|
---|
352 | {
|
---|
353 | // valid view cell found
|
---|
354 | viewCells.push_back(viewCell);
|
---|
355 |
|
---|
356 | ++ generatedViewCells;
|
---|
357 | //cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;
|
---|
358 | break;
|
---|
359 | }
|
---|
360 | else
|
---|
361 | {
|
---|
362 | cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | if (++ i == tries) // no new view cell fond
|
---|
367 | {
|
---|
368 | cerr << "big error! no view cell found" << endl;
|
---|
369 | return false;
|
---|
370 | }
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | return true;
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 | ViewCellPointsList *RandomViewCellsHandler::GetViewCellPointsList()
|
---|
379 | {
|
---|
380 | return &mViewCellPointsList;
|
---|
381 | }
|
---|
382 |
|
---|
383 | }
|
---|