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