source: GTP/trunk/Lib/Vis/Preprocessing/src/RandomViewCellsHandler.cpp @ 2648

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