1 | #define USE_THREADS 0
|
---|
2 |
|
---|
3 | #ifdef UNICODE
|
---|
4 | #undef UNICODE
|
---|
5 | #endif
|
---|
6 |
|
---|
7 | #define NOMINMAX
|
---|
8 | #ifdef __WINDOWS__
|
---|
9 | #include <windows.h>
|
---|
10 | #ifdef _CRT_SET
|
---|
11 | #include <crtdbg.h>
|
---|
12 | #endif // _CRT_SET
|
---|
13 | #endif
|
---|
14 | #include <cstdio>
|
---|
15 |
|
---|
16 | #include "Camera.h"
|
---|
17 | #include "PreprocessorFactory.h"
|
---|
18 | #include "Parser.h"
|
---|
19 | #include "Environment.h"
|
---|
20 | #include "MeshKdTree.h"
|
---|
21 | #include "Preprocessor.h"
|
---|
22 | #include "common.h"
|
---|
23 | #include "PreprocessorThread.h"
|
---|
24 | #include "ObjExporter.h"
|
---|
25 | #include "SceneGraph.h"
|
---|
26 | #include "GlobalLinesRenderer.h"
|
---|
27 | #include "RayCaster.h"
|
---|
28 | #include "timer.h"
|
---|
29 | #include "raypack.h"
|
---|
30 |
|
---|
31 | #include "ViewCellsManager.h"
|
---|
32 |
|
---|
33 | #ifdef USE_QT
|
---|
34 | #include "QtPreprocessorThread.h"
|
---|
35 | #include "QtGlViewer.h"
|
---|
36 | #include "QtGlRenderer.h"
|
---|
37 | #else
|
---|
38 | #if USE_THREADS
|
---|
39 | #include "BoostPreprocessorThread.h"
|
---|
40 | #endif
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include "ResourceManager.h"
|
---|
44 | #include "GlRenderer.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | #define USE_EXE_PATH false
|
---|
48 |
|
---|
49 |
|
---|
50 | using namespace GtpVisibilityPreprocessor;
|
---|
51 |
|
---|
52 | //Preprocessor *preprocessor = NULL;
|
---|
53 | extern GlRendererWidget *rendererWidget;
|
---|
54 | //GlobalLinesRenderer *globalLinesRenderer = NULL;
|
---|
55 |
|
---|
56 | // DLL function signature
|
---|
57 | typedef GlRendererWidget *(*importFunction)(Preprocessor *);
|
---|
58 |
|
---|
59 |
|
---|
60 | // Flag if the ray should be single sided (one direction)
|
---|
61 | // or double sided (shooting ray in both directions)
|
---|
62 | static bool castDoubleRays;
|
---|
63 |
|
---|
64 | extern void Cleanup();
|
---|
65 |
|
---|
66 | static string ReplaceSuffix(const string &filename, const string &a, const string &b)
|
---|
67 | {
|
---|
68 | string result = filename;
|
---|
69 |
|
---|
70 | int pos = (int)filename.rfind(a, (int)filename.size() - 1);
|
---|
71 | if (pos == filename.size() - a.size())
|
---|
72 | {
|
---|
73 | result.replace(pos, a.size(), b);
|
---|
74 | }
|
---|
75 |
|
---|
76 | return result;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | static int SplitFilenames(const string &str, vector<string> &filenames)
|
---|
81 | {
|
---|
82 | int pos = 0;
|
---|
83 |
|
---|
84 | while(1) {
|
---|
85 | int npos = (int)str.find(';', pos);
|
---|
86 |
|
---|
87 | if (npos < 0 || npos - pos < 1)
|
---|
88 | break;
|
---|
89 | filenames.push_back(string(str, pos, npos - pos));
|
---|
90 | pos = npos + 1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
94 | return (int)filenames.size();
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | static string GetInternFilename(const string &filename, const string newSuffix)
|
---|
99 | {
|
---|
100 | vector<string> filenames;
|
---|
101 | const int files = SplitFilenames(filename, filenames);
|
---|
102 |
|
---|
103 | vector<string>::const_iterator sit, sit_end = filenames.end();
|
---|
104 | string kdFilename;
|
---|
105 |
|
---|
106 | int i = 0;
|
---|
107 | for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
|
---|
108 | {
|
---|
109 | string currentFile = *sit;
|
---|
110 | string strippedFilename;
|
---|
111 |
|
---|
112 | if (i == 0)
|
---|
113 | {
|
---|
114 | strippedFilename = currentFile;
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | char *str = StripPath(currentFile.c_str());
|
---|
119 | strippedFilename = string(str);
|
---|
120 |
|
---|
121 | delete [] str;
|
---|
122 | }
|
---|
123 |
|
---|
124 | string suffix("_");
|
---|
125 |
|
---|
126 | if (i == (int)filenames.size() - 1)
|
---|
127 | {
|
---|
128 | suffix = newSuffix;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (strstr(strippedFilename.c_str(), ".x3d"))
|
---|
132 | {
|
---|
133 | kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
|
---|
134 | }
|
---|
135 | else if (strstr(strippedFilename.c_str(), ".dat"))
|
---|
136 | {
|
---|
137 | kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
|
---|
138 | }
|
---|
139 | else if (strstr(strippedFilename.c_str(), ".obj"))
|
---|
140 | {
|
---|
141 | kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
|
---|
142 | }
|
---|
143 | else
|
---|
144 | {
|
---|
145 | cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | //cout << "kdfilename: " << kdFilename << endl;
|
---|
150 | return kdFilename;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | // -------------------------------------------------------
|
---|
155 | // Written by Vlastimil Havran
|
---|
156 |
|
---|
157 | // This is for testing RT implementation
|
---|
158 | void
|
---|
159 | TestRTcamera(int argc, char **argv)
|
---|
160 | {
|
---|
161 | int returnCode = 0;
|
---|
162 |
|
---|
163 | InitTiming();
|
---|
164 | Debug.open("debug.log");
|
---|
165 |
|
---|
166 | Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
|
---|
167 | MeshKdTree::ParseEnvironment();
|
---|
168 |
|
---|
169 | char buff[128];
|
---|
170 | Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
|
---|
171 | string preprocessorType(buff);
|
---|
172 |
|
---|
173 | if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
|
---|
174 | {
|
---|
175 | Environment::DelSingleton();
|
---|
176 | cerr << "Unknown preprocessor type" << endl;
|
---|
177 | exit(1);
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
|
---|
182 | string filename(buff);
|
---|
183 |
|
---|
184 | const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
|
---|
185 | const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
|
---|
186 | ".kdm" : ".kdt");
|
---|
187 |
|
---|
188 | if (preprocessor->InitRayCast(externKdTree, internKdTree))
|
---|
189 | {
|
---|
190 | cout << "ray casting initialized!" << endl;
|
---|
191 | }
|
---|
192 | else
|
---|
193 | {
|
---|
194 | cout << "ray casting initialization failed!" << endl;
|
---|
195 | Cleanup();
|
---|
196 | exit(1);
|
---|
197 | }
|
---|
198 |
|
---|
199 | //Debug << "using pvs type " << PVS_TYPE << endl;
|
---|
200 |
|
---|
201 | /////////////
|
---|
202 | //-- load scene
|
---|
203 |
|
---|
204 | if (!preprocessor->LoadScene(filename))
|
---|
205 | {
|
---|
206 | cout << "loading file " << filename << " failed" << endl;
|
---|
207 | Cleanup();
|
---|
208 | exit(1);
|
---|
209 | }
|
---|
210 |
|
---|
211 | ////////////
|
---|
212 | //-- initialize external ray caster
|
---|
213 |
|
---|
214 | if (preprocessor->LoadInternKdTree(internKdTree))
|
---|
215 | {
|
---|
216 | cout << "intern kd tree loaded!" << endl;
|
---|
217 | }
|
---|
218 | else
|
---|
219 | {
|
---|
220 | cout << "loading intern kd tree failed!" << endl;
|
---|
221 | Cleanup();
|
---|
222 | exit(1);
|
---|
223 | }
|
---|
224 |
|
---|
225 | // export objects as obj
|
---|
226 | if (preprocessor->mExportObj)
|
---|
227 | {
|
---|
228 | if (strstr(filename.c_str(), ".obj"))
|
---|
229 | {
|
---|
230 | cerr << "already in obj format" << endl;
|
---|
231 | if (0) preprocessor->ExportObj("test.obj", preprocessor->mObjects);
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 |
|
---|
236 | const string objname = GetInternFilename(filename, ".obj");
|
---|
237 |
|
---|
238 | cout << "exporting scene to " << objname << endl;
|
---|
239 | bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
|
---|
240 |
|
---|
241 |
|
---|
242 | if (success)
|
---|
243 | cout << "finished exporting obj" << endl;
|
---|
244 | else
|
---|
245 | cerr << "error exporting " << objname << endl;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | int width = 1000;
|
---|
250 | int height = 500;
|
---|
251 | float fieldOfView = 115.f;
|
---|
252 | Camera cam(width, height, fieldOfView);
|
---|
253 |
|
---|
254 | AxisAlignedBox3 bboxOrig = preprocessor->mSceneGraph->GetBox();
|
---|
255 | AxisAlignedBox3 bbox = bboxOrig;
|
---|
256 | int sizeDiag = Magnitude(bbox.Diagonal());
|
---|
257 | bbox.Enlarge(sizeDiag * 1.5f);
|
---|
258 |
|
---|
259 | Vector3 origin, dir;
|
---|
260 |
|
---|
261 | //#define WIEN1
|
---|
262 | #define WIEN2
|
---|
263 | //#define ARENA1
|
---|
264 |
|
---|
265 | #ifdef WIEN1
|
---|
266 | // 1099.9 183.0 -387
|
---|
267 | origin = Vector3(1099.9f, 183.0f, -387.0f);
|
---|
268 | dir = Vector3(-0.6f, 0.0001f, -0.8f);
|
---|
269 | #define DIREXISTS
|
---|
270 | #endif
|
---|
271 | #ifdef WIEN2
|
---|
272 | // 935.6 215.8 -1012.3
|
---|
273 | origin = Vector3(935.6, 215.8, -1012.4);
|
---|
274 | dir = Vector3(0.01f, 0.01f, 1.0f);
|
---|
275 | #define DIREXISTS
|
---|
276 | #endif
|
---|
277 | #ifdef ARENA1
|
---|
278 | origin = Vector3(22.16, 19.63, -950.44);
|
---|
279 | dir = Vector3(0.9, 0.001f, -0.5f);
|
---|
280 | #define DIREXISTS
|
---|
281 | #endif
|
---|
282 |
|
---|
283 | cam.SetPosition(origin);
|
---|
284 |
|
---|
285 | #ifndef DIREXISTS
|
---|
286 | Vector3 center = bbox.Center();
|
---|
287 | dir = center - origin;
|
---|
288 | #endif
|
---|
289 |
|
---|
290 | dir.Normalize();
|
---|
291 | cam.SetDirection(dir);
|
---|
292 | cout << "Computing image\n" << endl;
|
---|
293 |
|
---|
294 | #if 1
|
---|
295 | // ray by ray
|
---|
296 | cam.SnapImage("test-rays.tga",
|
---|
297 | preprocessor->mRayCaster,
|
---|
298 | bboxOrig,
|
---|
299 | preprocessor->mSceneGraph);
|
---|
300 | #endif
|
---|
301 | #if 1
|
---|
302 | // using ray packets
|
---|
303 | cam.SnapImagePacket("test-packet.tga",
|
---|
304 | preprocessor->mRayCaster,
|
---|
305 | bboxOrig,
|
---|
306 | preprocessor->mSceneGraph);
|
---|
307 | #endif
|
---|
308 | #if 1
|
---|
309 | // using ray packets
|
---|
310 | cam.SnapImage2("test-oneDir.tga",
|
---|
311 | preprocessor->mRayCaster,
|
---|
312 | bboxOrig,
|
---|
313 | preprocessor->mSceneGraph);
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | cout << "Done\n" << endl;
|
---|
317 | return;
|
---|
318 | }
|
---|
319 |
|
---|
320 | struct RESult {
|
---|
321 | int hitA;
|
---|
322 | float hitAT;
|
---|
323 | int hitB;
|
---|
324 | float hitBT;
|
---|
325 | RESult(int hA, float tA, int hB, float tB):
|
---|
326 | hitA(hA), hitAT(tA), hitB(hB), hitBT(tB) { }
|
---|
327 | };
|
---|
328 |
|
---|
329 | // This is for testing RT implementation
|
---|
330 | void
|
---|
331 | TestRTfromFile(int argc, char **argv)
|
---|
332 | {
|
---|
333 | int returnCode = 0;
|
---|
334 |
|
---|
335 | InitTiming();
|
---|
336 | Debug.open("debug.log");
|
---|
337 |
|
---|
338 | Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
|
---|
339 | MeshKdTree::ParseEnvironment();
|
---|
340 |
|
---|
341 | char buff[128];
|
---|
342 | Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
|
---|
343 | string preprocessorType(buff);
|
---|
344 |
|
---|
345 | if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
|
---|
346 | {
|
---|
347 | Environment::DelSingleton();
|
---|
348 | cerr << "Unknown preprocessor type" << endl;
|
---|
349 | exit(1);
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
|
---|
354 | string filename(buff);
|
---|
355 |
|
---|
356 | const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
|
---|
357 | const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
|
---|
358 | ".kdm" : ".kdt");
|
---|
359 |
|
---|
360 | if (preprocessor->InitRayCast(externKdTree, internKdTree))
|
---|
361 | {
|
---|
362 | cout << "ray casting initialized!" << endl;
|
---|
363 | }
|
---|
364 | else
|
---|
365 | {
|
---|
366 | cout << "ray casting initialization failed!" << endl;
|
---|
367 | Cleanup();
|
---|
368 | exit(1);
|
---|
369 | }
|
---|
370 |
|
---|
371 | //Debug << "using pvs type " << PVS_TYPE << endl;
|
---|
372 |
|
---|
373 | /////////////
|
---|
374 | //-- load scene
|
---|
375 |
|
---|
376 | if (!preprocessor->LoadScene(filename))
|
---|
377 | {
|
---|
378 | cout << "loading file " << filename << " failed" << endl;
|
---|
379 | Cleanup();
|
---|
380 | exit(1);
|
---|
381 | }
|
---|
382 |
|
---|
383 | ////////////
|
---|
384 | //-- initialize external ray caster
|
---|
385 |
|
---|
386 | if (preprocessor->LoadInternKdTree(internKdTree))
|
---|
387 | {
|
---|
388 | cout << "intern kd tree loaded!" << endl;
|
---|
389 | }
|
---|
390 | else
|
---|
391 | {
|
---|
392 | cout << "loading intern kd tree failed!" << endl;
|
---|
393 | Cleanup();
|
---|
394 | exit(1);
|
---|
395 | }
|
---|
396 |
|
---|
397 | // export objects as obj
|
---|
398 | if (preprocessor->mExportObj)
|
---|
399 | {
|
---|
400 | if (strstr(filename.c_str(), ".obj"))
|
---|
401 | {
|
---|
402 | cerr << "already in obj format" << endl;
|
---|
403 | if (0) preprocessor->ExportObj("test.obj", preprocessor->mObjects);
|
---|
404 | }
|
---|
405 | else
|
---|
406 | {
|
---|
407 | const string objname = GetInternFilename(filename, ".obj");
|
---|
408 |
|
---|
409 | cout << "exporting scene to " << objname << endl;
|
---|
410 | bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
|
---|
411 |
|
---|
412 | if (success)
|
---|
413 | cout << "finished exporting obj" << endl;
|
---|
414 | else
|
---|
415 | cerr << "error exporting " << objname << endl;
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | Environment::GetSingleton()->GetStringValue("Rays.file", buff);
|
---|
420 | FILE *fp = fopen(buff, "rb");
|
---|
421 | if (!fp) {
|
---|
422 | cerr << "ERROR: file " << buff << " cannot be opened for reading" << endl;
|
---|
423 | cerr << "EXITING" << endl;
|
---|
424 | exit(3);
|
---|
425 | }
|
---|
426 | cout << "File " << buff << " was opened successfully" << endl;
|
---|
427 | int cntMaxRays = 100000;
|
---|
428 | Environment::GetSingleton()->GetIntValue("Rays.cnt", cntMaxRays);
|
---|
429 | vector<SimpleRay> rays;
|
---|
430 | SimpleRay rayTest;
|
---|
431 | vector<RESult> results;
|
---|
432 |
|
---|
433 | int cntRays = 0;
|
---|
434 | for (int i = 0; cntRays < cntMaxRays; i++) {
|
---|
435 | int ch = fgetc(fp);
|
---|
436 | switch (ch) {
|
---|
437 | case 'G': { // two-sided rays of 16
|
---|
438 | for (int j = 0; j < 16; j++) {
|
---|
439 | int order;
|
---|
440 | float ox, oy, oz, dx, dy, dz;
|
---|
441 | int hitA; float hitAT;
|
---|
442 | int hitB; float hitBT;
|
---|
443 | if (fscanf(fp, "%d %f %f %f %f %f %f %d %f %d %f\n",
|
---|
444 | &order, &ox, &oy, &oz, &dx, &dy, &dz, &hitA, &hitAT, &hitB, &hitBT) != 11) {
|
---|
445 | cerr << "Problem parsing the ray file" << endl;
|
---|
446 | cerr << "EXITING for ray order" << order << endl;
|
---|
447 | goto FINISH;
|
---|
448 | }
|
---|
449 | Vector3 mPosition(ox, oy, oz);
|
---|
450 | Vector3 mDirection(dx, dy, dz);
|
---|
451 | rayTest.Set(mPosition, mDirection, 0, 1.0, true);
|
---|
452 | rays.push_back(rayTest);
|
---|
453 | results.push_back(RESult(hitA, hitAT, hitB, hitBT));
|
---|
454 | cntRays++;
|
---|
455 | }
|
---|
456 | break;
|
---|
457 | }
|
---|
458 | case 'H': // one-sided rays of 16
|
---|
459 | cerr << "Not yet implemented " << endl; abort();
|
---|
460 |
|
---|
461 | case 'D': // two-sided ray
|
---|
462 | cerr << "Not yet implemented " << endl; abort();
|
---|
463 |
|
---|
464 | case 'S': // one-sided ray
|
---|
465 | cerr << "Not yet implemented " << endl; abort();
|
---|
466 | } // switch
|
---|
467 | } // for i
|
---|
468 |
|
---|
469 | FINISH:
|
---|
470 | fclose(fp);
|
---|
471 |
|
---|
472 | Environment::GetSingleton()->GetBoolValue("TestDoubleRays", castDoubleRays);
|
---|
473 |
|
---|
474 | double mult = 1.0;
|
---|
475 | if (castDoubleRays)
|
---|
476 | mult = 2.0;
|
---|
477 |
|
---|
478 | cout << "Starting to shoot " << cntRays * mult << " rays" << endl;
|
---|
479 |
|
---|
480 | RayCaster *raycaster = preprocessor->mRayCaster;
|
---|
481 | VssRayContainer vssRays;
|
---|
482 | AxisAlignedBox3 bboxOrig = preprocessor->mSceneGraph->GetBox();
|
---|
483 |
|
---|
484 | //#define DEBUGRESULTS
|
---|
485 |
|
---|
486 | #ifdef DEBUGRESULTS
|
---|
487 | int cntIntelYesWeNo = 0;
|
---|
488 | int cntIntelNoWeYes = 0;
|
---|
489 | int cntIntelOurDistMismatch = 0;
|
---|
490 | bool printOut = false;
|
---|
491 | double sumDistIntel = 0.;
|
---|
492 | doublecca sumDistOurAlg = 0.;
|
---|
493 | double sumDistAbsDiff = 0.f;
|
---|
494 | #endif
|
---|
495 |
|
---|
496 | cout << "Press a key to start ray shooting" << endl;
|
---|
497 | getchar();
|
---|
498 | cout << "Ray shooting " << cntRays << " rays started - "
|
---|
499 | << (castDoubleRays ? " double " : " single ")
|
---|
500 | << " dir " << endl;
|
---|
501 |
|
---|
502 | long t1 = GetTime();
|
---|
503 | CTimer timer;
|
---|
504 | timer.Reset();
|
---|
505 | timer.Start();
|
---|
506 |
|
---|
507 | SimpleRayContainer raysToTest;
|
---|
508 | for (int i = 0; i < cntRays - 16; i++) {
|
---|
509 | #if 0
|
---|
510 | int res = raycaster->CastRay(rays[i],
|
---|
511 | vssRays,
|
---|
512 | bboxOrig,
|
---|
513 | castDoubleRays, // castDoubleRay,
|
---|
514 | false); // pruneInvalidRays
|
---|
515 | #else
|
---|
516 | raysToTest.erase(raysToTest.begin(), raysToTest.end());
|
---|
517 | for (int j = 0; j < 16; j++, i++) {
|
---|
518 | raysToTest.push_back(rays[i]);
|
---|
519 | }
|
---|
520 | #if 0
|
---|
521 | for (int j = 0; j < 16; j++) {
|
---|
522 | cout << "orig = " << raysToTest[j].mOrigin << " dir = "
|
---|
523 | << raysToTest[j].mDirection << endl;
|
---|
524 | }
|
---|
525 | #endif
|
---|
526 |
|
---|
527 | raycaster->CastRays16(raysToTest,
|
---|
528 | vssRays,
|
---|
529 | bboxOrig,
|
---|
530 | castDoubleRays,
|
---|
531 | false);
|
---|
532 | #endif
|
---|
533 |
|
---|
534 | #ifdef DEBUGRESULTS
|
---|
535 | if (!castDoubleRays) {
|
---|
536 | float T = rays[i].IntersectionRes[0].tdist;
|
---|
537 | if ( (res != results[i].hitA) ||
|
---|
538 | ((res)&&(results[i].hitA)&& (fabs(T - results[i].hitAT)) > 2.0f)
|
---|
539 | )
|
---|
540 | {
|
---|
541 | if (printOut)
|
---|
542 | cout << " i = " << i << " ";
|
---|
543 | if ((res != 0) && (results[i].hitA == 0)) {
|
---|
544 | cntIntelNoWeYes++;
|
---|
545 | if (printOut)
|
---|
546 | cout << "Intel no intersection, our alg intersection at t = " << T << endl;
|
---|
547 | }
|
---|
548 | else {
|
---|
549 | if ((res = 0) && (results[i].hitA)) {
|
---|
550 | cntIntelYesWeNo++;
|
---|
551 | if (printOut)
|
---|
552 | cout << "Intel intersection at = " << results[i].hitAT
|
---|
553 | << " , our alg no intersection = " << endl;
|
---|
554 |
|
---|
555 | }
|
---|
556 | else {
|
---|
557 | cntIntelOurDistMismatch++;
|
---|
558 | sumDistOurAlg += T;
|
---|
559 | sumDistIntel += results[i].hitAT;
|
---|
560 | sumDistAbsDiff += fabs(T - results[i].hitAT);
|
---|
561 | if (printOut)
|
---|
562 | cout << "Intel intersection at = " << results[i].hitAT
|
---|
563 | << " , our alg intersection at = " << T << endl;
|
---|
564 | }
|
---|
565 | }
|
---|
566 | }
|
---|
567 | } // double rays
|
---|
568 | else {
|
---|
569 | // checking for results of double rays - not yet implemented
|
---|
570 | }
|
---|
571 | #endif
|
---|
572 | }
|
---|
573 |
|
---|
574 | timer.Stop();
|
---|
575 | long t2 = GetTime();
|
---|
576 | cout<<"\n#RAY_CAST_TIME = ";
|
---|
577 | cout << TimeDiff(t1, t2)<<" [mikrosec]"
|
---|
578 | << " userTime = " << timer.UserTime() << " realTime = "
|
---|
579 | << timer.RealTime() << endl;
|
---|
580 | ;
|
---|
581 | cout << "Rays shot per milisecond [userTimer] = "
|
---|
582 | << ((double)cntRays * mult/(double)timer.UserTime()) / 1000.f << endl;
|
---|
583 |
|
---|
584 | #ifdef DEBUGRESULTS
|
---|
585 | cout << "cntIntelYesWeNo = " << cntIntelYesWeNo << endl;
|
---|
586 | cout << "cntIntelNoWeYes = " << cntIntelNoWeYes << endl;
|
---|
587 | cout << "cntIntelOurDistMismatch = " << cntIntelOurDistMismatch++ << endl;
|
---|
588 | cout << " sumDistIntel = " << sumDistIntel/(double)cntIntelOurDistMismatch << endl;
|
---|
589 | cout << " sumDistOur = " << sumDistOurAlg/(double)cntIntelOurDistMismatch << endl;
|
---|
590 | cout << " sumAbsDiffDist = " << sumDistAbsDiff/(double)cntIntelOurDistMismatch
|
---|
591 | << endl;
|
---|
592 | #endif
|
---|
593 | cout << "Done\n" << endl;
|
---|
594 | return;
|
---|
595 | }
|
---|
596 |
|
---|
597 | //-------------------------------------------------------------------------
|
---|
598 | // This is for testing RT implementation using ray packets
|
---|
599 | void
|
---|
600 | TestRTfromFilePackets(int argc, char **argv)
|
---|
601 | {
|
---|
602 | int returnCode = 0;
|
---|
603 |
|
---|
604 | InitTiming();
|
---|
605 | Debug.open("debug.log");
|
---|
606 |
|
---|
607 | Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
|
---|
608 | MeshKdTree::ParseEnvironment();
|
---|
609 |
|
---|
610 | char buff[128];
|
---|
611 | Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
|
---|
612 | string preprocessorType(buff);
|
---|
613 |
|
---|
614 | if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
|
---|
615 | {
|
---|
616 | Environment::DelSingleton();
|
---|
617 | cerr << "Unknown preprocessor type" << endl;
|
---|
618 | exit(1);
|
---|
619 | }
|
---|
620 |
|
---|
621 |
|
---|
622 | Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
|
---|
623 | string filename(buff);
|
---|
624 |
|
---|
625 | const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
|
---|
626 | const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
|
---|
627 | ".kdm" : ".kdt");
|
---|
628 |
|
---|
629 | if (preprocessor->InitRayCast(externKdTree, internKdTree))
|
---|
630 | {
|
---|
631 | cout << "ray casting initialized!" << endl;
|
---|
632 | }
|
---|
633 | else
|
---|
634 | {
|
---|
635 | cout << "ray casting initialization failed!" << endl;
|
---|
636 | Cleanup();
|
---|
637 | exit(1);
|
---|
638 | }
|
---|
639 |
|
---|
640 | //Debug << "using pvs type " << PVS_TYPE << endl;
|
---|
641 |
|
---|
642 | /////////////
|
---|
643 | //-- load scene
|
---|
644 |
|
---|
645 | if (!preprocessor->LoadScene(filename))
|
---|
646 | {
|
---|
647 | cout << "loading file " << filename << " failed" << endl;
|
---|
648 | Cleanup();
|
---|
649 | exit(1);
|
---|
650 | }
|
---|
651 |
|
---|
652 | ////////////
|
---|
653 | //-- initialize external ray caster
|
---|
654 |
|
---|
655 | if (preprocessor->LoadInternKdTree(internKdTree))
|
---|
656 | {
|
---|
657 | cout << "intern kd tree loaded!" << endl;
|
---|
658 | }
|
---|
659 | else
|
---|
660 | {
|
---|
661 | cout << "loading intern kd tree failed!" << endl;
|
---|
662 | Cleanup();
|
---|
663 | exit(1);
|
---|
664 | }
|
---|
665 |
|
---|
666 | // export objects as obj
|
---|
667 | if (preprocessor->mExportObj)
|
---|
668 | {
|
---|
669 | if (strstr(filename.c_str(), ".obj"))
|
---|
670 | {
|
---|
671 | cerr << "already in obj format" << endl;
|
---|
672 | if (0) preprocessor->ExportObj("test.obj", preprocessor->mObjects);
|
---|
673 | }
|
---|
674 | else
|
---|
675 | {
|
---|
676 | const string objname = GetInternFilename(filename, ".obj");
|
---|
677 |
|
---|
678 | cout << "exporting scene to " << objname << endl;
|
---|
679 | bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
|
---|
680 |
|
---|
681 | if (success)
|
---|
682 | cout << "finished exporting obj" << endl;
|
---|
683 | else
|
---|
684 | cerr << "error exporting " << objname << endl;
|
---|
685 | }
|
---|
686 | }
|
---|
687 |
|
---|
688 | Environment::GetSingleton()->GetStringValue("Rays.file", buff);
|
---|
689 | FILE *fp = fopen(buff, "rb");
|
---|
690 | if (!fp) {
|
---|
691 | cerr << "ERROR: file " << buff << " cannot be opened for reading" << endl;
|
---|
692 | cerr << "EXITING" << endl;
|
---|
693 | exit(3);
|
---|
694 | }
|
---|
695 | cout << "File " << buff << " was opened successfully" << endl;
|
---|
696 | int cntMaxRays = 100000;
|
---|
697 | Environment::GetSingleton()->GetIntValue("Rays.cnt", cntMaxRays);
|
---|
698 | vector<SimpleRay> rays;
|
---|
699 | SimpleRay rayTest;
|
---|
700 | vector<RESult> results;
|
---|
701 |
|
---|
702 | int cntRays = 0;
|
---|
703 | for (int i = 0; cntRays < cntMaxRays; i++) {
|
---|
704 | int ch = fgetc(fp);
|
---|
705 | switch (ch) {
|
---|
706 | case 'G': { // two-sided rays of 16
|
---|
707 | for (int j = 0; j < 16; j++) {
|
---|
708 | int order;
|
---|
709 | float ox, oy, oz, dx, dy, dz;
|
---|
710 | int hitA; float hitAT;
|
---|
711 | int hitB; float hitBT;
|
---|
712 | if (fscanf(fp, "%d %f %f %f %f %f %f %d %f %d %f\n",
|
---|
713 | &order, &ox, &oy, &oz, &dx, &dy, &dz, &hitA, &hitAT, &hitB, &hitBT) != 11) {
|
---|
714 | cerr << "Problem parsing the ray file" << endl;
|
---|
715 | cerr << "EXITING for ray order" << order << endl;
|
---|
716 | goto FINISH;
|
---|
717 | }
|
---|
718 | Vector3 mPosition(ox, oy, oz);
|
---|
719 | Vector3 mDirection(dx, dy, dz);
|
---|
720 | rayTest.Set(mPosition, mDirection, 0, 1.0, true);
|
---|
721 | rays.push_back(rayTest);
|
---|
722 | results.push_back(RESult(hitA, hitAT, hitB, hitBT));
|
---|
723 | cntRays++;
|
---|
724 | }
|
---|
725 | break;
|
---|
726 | }
|
---|
727 | case 'H': // one-sided rays of 16
|
---|
728 | cerr << "Not yet implemented " << endl; abort();
|
---|
729 |
|
---|
730 | case 'D': // two-sided ray
|
---|
731 | cerr << "Not yet implemented " << endl; abort();
|
---|
732 |
|
---|
733 | case 'S': // one-sided ray
|
---|
734 | cerr << "Not yet implemented " << endl; abort();
|
---|
735 | } // switch
|
---|
736 | } // for i
|
---|
737 | FINISH:
|
---|
738 | fclose(fp);
|
---|
739 |
|
---|
740 | Environment::GetSingleton()->GetBoolValue("TestDoubleRays", castDoubleRays);
|
---|
741 |
|
---|
742 | double mult = 1.0;
|
---|
743 | if (castDoubleRays)
|
---|
744 | mult = 2.0;
|
---|
745 |
|
---|
746 | cout << "Starting to shoot " << cntRays * mult << " rays" << endl;
|
---|
747 |
|
---|
748 | RayCaster *raycaster = preprocessor->mRayCaster;
|
---|
749 | VssRayContainer vssRays;
|
---|
750 | AxisAlignedBox3 bboxOrig = preprocessor->mSceneGraph->GetBox();
|
---|
751 |
|
---|
752 | //#define DEBUGRESULTS
|
---|
753 |
|
---|
754 | #ifdef DEBUGRESULTS
|
---|
755 | int cntIntelYesWeNo = 0;
|
---|
756 | int cntIntelNoWeYes = 0;
|
---|
757 | int cntIntelOurDistMismatch = 0;
|
---|
758 | bool printOut = false;
|
---|
759 | double sumDistIntel = 0.;
|
---|
760 | doublecca sumDistOurAlg = 0.;
|
---|
761 | double sumDistAbsDiff = 0.f;
|
---|
762 | #endif
|
---|
763 |
|
---|
764 | cout << "Press a key to start ray shooting" << endl;
|
---|
765 | getchar();
|
---|
766 | cout << "Ray packs shooting " << cntRays << " rays started - "
|
---|
767 | << (castDoubleRays ? " double " : " single ")
|
---|
768 | << " dir " << endl;
|
---|
769 |
|
---|
770 | long t1 = GetTime();
|
---|
771 | CTimer timer;
|
---|
772 | timer.Reset();
|
---|
773 | timer.Start();
|
---|
774 |
|
---|
775 | #ifdef _USE_HAVRAN_SSE
|
---|
776 | #ifdef __SSE__
|
---|
777 | RayPacket2x2 raysPack;
|
---|
778 |
|
---|
779 | for (int i = 0; i < cntRays - 16; i++) {
|
---|
780 | for (int j = 0; j < 4; j++, i++) {
|
---|
781 | raysPack.SetLoc(j, rays[i].mOrigin);
|
---|
782 | raysPack.SetDir(j, rays[i].mDirection);
|
---|
783 | } // for
|
---|
784 | #if 0
|
---|
785 | for (int j = 0; j < 16; j++) {
|
---|
786 | cout << "orig = " << raysToTest[j].mOrigin << " dir = "
|
---|
787 | << raysToTest[j].mDirection << endl;
|
---|
788 | }
|
---|
789 | #endif
|
---|
790 | raysPack.ComputeDirSign();
|
---|
791 | raycaster->CastRaysPacket2x2(raysPack, castDoubleRays);
|
---|
792 |
|
---|
793 | #ifdef DEBUGRESULTS
|
---|
794 | if (!castDoubleRays) {
|
---|
795 | float T = rays[i].IntersectionRes[0].tdist;
|
---|
796 | if ( (res != results[i].hitA) ||
|
---|
797 | ((res)&&(results[i].hitA)&& (fabs(T - results[i].hitAT)) > 2.0f)
|
---|
798 | )
|
---|
799 | {
|
---|
800 | if (printOut)
|
---|
801 | cout << " i = " << i << " ";
|
---|
802 | if ((res != 0) && (results[i].hitA == 0)) {
|
---|
803 | cntIntelNoWeYes++;
|
---|
804 | if (printOut)
|
---|
805 | cout << "Intel no intersection, our alg intersection at t = " << T << endl;
|
---|
806 | }
|
---|
807 | else {
|
---|
808 | if ((res = 0) && (results[i].hitA)) {
|
---|
809 | cntIntelYesWeNo++;
|
---|
810 | if (printOut)
|
---|
811 | cout << "Intel intersection at = " << results[i].hitAT
|
---|
812 | << " , our alg no intersection = " << endl;
|
---|
813 |
|
---|
814 | }
|
---|
815 | else {
|
---|
816 | cntIntelOurDistMismatch++;
|
---|
817 | sumDistOurAlg += T;
|
---|
818 | sumDistIntel += results[i].hitAT;
|
---|
819 | sumDistAbsDiff += fabs(T - results[i].hitAT);
|
---|
820 | if (printOut)
|
---|
821 | cout << "Intel intersection at = " << results[i].hitAT
|
---|
822 | << " , our alg intersection at = " << T << endl;
|
---|
823 | }
|
---|
824 | }
|
---|
825 | }
|
---|
826 | } // double rays
|
---|
827 | else {
|
---|
828 | // checking for results of double rays - not yet implemented
|
---|
829 | }
|
---|
830 | #endif
|
---|
831 | }
|
---|
832 |
|
---|
833 | #endif // __SSE__
|
---|
834 | #endif // _USE_HAVRAN_SSE
|
---|
835 |
|
---|
836 | timer.Stop();
|
---|
837 | long t2 = GetTime();
|
---|
838 | cout<<"\n#RAY_CAST_TIME = ";
|
---|
839 | cout << TimeDiff(t1, t2)<<" [mikrosec]"
|
---|
840 | << " userTime = " << timer.UserTime() << " realTime = "
|
---|
841 | << timer.RealTime() << endl;
|
---|
842 | ;
|
---|
843 | cout << "Rays shot per milisecond [userTimer] = "
|
---|
844 | << ((double)cntRays * mult/(double)timer.UserTime()) / 1000.f << endl;
|
---|
845 |
|
---|
846 | #ifdef DEBUGRESULTS
|
---|
847 | cout << "cntIntelYesWeNo = " << cntIntelYesWeNo << endl;
|
---|
848 | cout << "cntIntelNoWeYes = " << cntIntelNoWeYes << endl;
|
---|
849 | cout << "cntIntelOurDistMismatch = " << cntIntelOurDistMismatch++ << endl;
|
---|
850 | cout << " sumDistIntel = " << sumDistIntel/(double)cntIntelOurDistMismatch << endl;
|
---|
851 | cout << " sumDistOur = " << sumDistOurAlg/(double)cntIntelOurDistMismatch << endl;
|
---|
852 | cout << " sumAbsDiffDist = " << sumDistAbsDiff/(double)cntIntelOurDistMismatch
|
---|
853 | << endl;
|
---|
854 | #endif
|
---|
855 | cout << "Done\n" << endl;
|
---|
856 | return;
|
---|
857 | }
|
---|