PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/test/test.cpp

https://bitbucket.org/remotereality/com.remotereality.sandbox.unwrapping
C++ | 588 lines | 417 code | 106 blank | 65 comment | 66 complexity | d527240155b07c475725c6ea3489b161 MD5 | raw file
  1. // test.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <fstream>
  5. #include <string>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <algorithm>
  9. #include <iterator>
  10. #define DIR -1
  11. #define OUT_WIDTH 1920
  12. #define OUT_HEIGHT 1080
  13. #define OUT_WIDTH_HALF 960
  14. #define OUT_HEIGHT_HALF 540
  15. #define DONUT_WIDTH 2048
  16. #define DONUT_HEIGHT 2048
  17. unsigned int g_image_size_in;
  18. unsigned int g_image_size_out;
  19. using namespace std;
  20. unsigned int * g_image_in; // use 32 bit integer
  21. unsigned int * g_image_out; // use 32 bit integer
  22. unsigned int * g_mapping; // use 32 bit integer
  23. // global variables
  24. //int g_dir = -1;
  25. int g_r = 600;
  26. float g_theta = 60 * (3.14159265/180);
  27. float g_phi = 135 * (3.14159265/180);
  28. float g_gamma = 0 * (3.14159265/180);
  29. //int OUT_WIDTH = 960;
  30. //int OUT_HEIGHT = 540;
  31. int g_prsrad = 600; // Radius for Perspective, Set to ~60% of int g_donrad
  32. int g_donrad = 1024; // Radius for full donut
  33. int g_center_x = 1024; // center of donut
  34. int g_center_y = 1024; // center of donut
  35. unsigned int g_image_in_x_min = 2048;
  36. unsigned int g_image_in_x_max = 0;
  37. unsigned int g_image_in_y_min = 2048;
  38. unsigned int g_image_in_y_max = 0;
  39. void readSrcDonutFile()
  40. {
  41. int pixelcount = 0 ;
  42. std::string line ;
  43. std::string token;
  44. std::ifstream infile( "C:\\0\\project\\hummingbird\\test\\donut_bw.txt" ) ;
  45. unsigned int * image_in = g_image_in;
  46. if ( infile )
  47. {
  48. while ( getline( infile , line ) )
  49. {
  50. if(pixelcount++ >= g_image_size_in)
  51. break;
  52. stringstream ss(line);
  53. while (ss >> token)
  54. {
  55. if(pixelcount++ >= g_image_size_in)
  56. break;
  57. unsigned char pixel = (unsigned char)atoi(token.c_str());
  58. *image_in = pixel;
  59. image_in++;
  60. }
  61. }
  62. }
  63. infile.close( ) ;
  64. }
  65. void createMapping(int step)
  66. {
  67. printf ("--------create mapping------------\n");
  68. //unsigned int * image_out = g_image_out;
  69. unsigned int * mapping = g_mapping;
  70. float phi = step * (3.14159265/180);
  71. float pz2 = sin(g_theta);
  72. float pz1 = cos(g_theta);
  73. float px2 = sin(phi);
  74. float py2 = cos(phi);
  75. float rts = g_r * pz2;
  76. float px1 = rts * py2;
  77. float px3 = pz1 * py2;
  78. float py1 = rts * px2;
  79. float py3 = pz1 * px2;
  80. pz1 = g_r * pz1;
  81. int r2 = g_r * g_r;
  82. int dr2 = g_donrad * g_donrad;
  83. float ux, uy, uz, vx, vy, vz;
  84. int vdel = OUT_HEIGHT_HALF;
  85. for( int h = 0; h < OUT_HEIGHT; h++)
  86. {
  87. int udel = - OUT_WIDTH_HALF;
  88. for (int w = 0; w < OUT_WIDTH; w++)
  89. {
  90. if((h == 0 ) && ( w == 0))
  91. {
  92. #ifdef DIR
  93. ux = px1 + udel * px2 + vdel * px3;
  94. #else
  95. ux = px1 + udel * px2 - vdel * px3;
  96. #endif
  97. #ifdef DIR
  98. uy = py1 -udel * py2 + vdel * py3;
  99. #else
  100. uy = py1 -udel * py2 - vdel * py3;
  101. #endif
  102. #ifdef DIR
  103. uz = -pz1 + vdel * pz2;
  104. #else
  105. float z = pz1 + vdel * pz2;
  106. #endif
  107. vx = ux;
  108. vy = uy;
  109. vz = uz;
  110. }
  111. else if(w == 0) // v decrease
  112. {
  113. vx -= px3;
  114. vy -= py3;
  115. vz -= pz2;
  116. ux = vx;
  117. uy = vy;
  118. uz = vz;
  119. }
  120. else // w increase
  121. {
  122. ux += px2;
  123. uy -= py2;
  124. }
  125. // printf("*(%d, %d) x=%d, y=%d, z=%d\n", udel, vdel, x, y, z);
  126. #ifdef DIR
  127. float zclamp = -uz;
  128. #else
  129. float zclamp = uz;
  130. #endif
  131. // Debug precomputes
  132. //float xp1 = (g_r * sin(g_theta) * cos(phi));
  133. //float xp2 = (1 * cos(g_gamma) * sin(phi)) +
  134. // (-1 * sin(g_gamma) * g_dir * cos(g_theta) * cos(phi));
  135. //float xp3 = (-1 * sin(g_gamma) * sin(phi)) +
  136. // (-1 * cos(g_gamma) * g_dir * cos(g_theta) * cos(phi));
  137. //float yp1 = (g_r * sin(g_theta) * sin(phi));
  138. //float yp2 = (-1 * cos(g_gamma) * cos(phi)) +
  139. // (-1 * sin(g_gamma) * g_dir * cos(g_theta) * sin(phi));
  140. //float yp3 = (1 * sin(g_gamma) * cos(phi)) +
  141. // (-1 * cos(g_gamma) * g_dir * cos(g_theta) * sin(phi));
  142. //float zp1 = (g_r * g_dir * cos(g_theta));
  143. //float zp2 = (1 * sin(g_gamma) * sin(g_theta));
  144. //float zp3 = (1 * cos(g_gamma) * sin(g_theta));
  145. int dont = udel * udel + vdel * vdel + r2;
  146. float dont1 = sqrt((float)dont);
  147. #ifdef DIR
  148. dont1 -= uz;
  149. #else
  150. dont1 += uz;
  151. #endif
  152. dont1 = 1/dont1;
  153. dont1 = g_prsrad * dont1;
  154. float xdist = (int)(ux) * dont1;
  155. float ydist = (int)(uy) * dont1;
  156. float xdon = g_center_x + xdist;
  157. float ydon = g_center_y - ydist;
  158. // printf "xp1 = $xp1, xp2 = $xp2, xp3 = $xp3\n";
  159. // printf "yp1 = float yp1, yp2 = float yp2, yp3 = float yp3\n";
  160. // printf "zp1 = $zp1, zp2 = $zp2, zp3 = $zp3\n";
  161. // printf "XDON = $xdon, YDON = float ydon\n";
  162. // <STDIN>;
  163. //float xu, xv, yu, yv;
  164. // if (h == 0 && w == 0)
  165. //{
  166. // xu = x2 + x4;
  167. // xv = x3 - x5;
  168. // yu = y4 - y2;
  169. // yv = -y3 - y5;
  170. // // printf STAT "$step $xu $xv float yu float yv\n";
  171. // };
  172. // Check for out of bounds.
  173. int ixdon = (int)(xdon+0.5);
  174. int iydon = (int)(ydon+0.5);
  175. // todo: try remove this condition
  176. if (((xdist*xdist) + (ydist* ydist)) > dr2)
  177. {
  178. *mapping = 0;
  179. }
  180. else
  181. {
  182. *mapping = ixdon + (iydon << 16);
  183. }
  184. mapping++;
  185. // Print co-ordinates of four corner pixels
  186. if (h == 0 && w == 0 )
  187. {
  188. if(ixdon < g_image_in_x_min)
  189. g_image_in_x_min = ixdon;
  190. if(ixdon > g_image_in_x_max)
  191. g_image_in_x_max = ixdon;
  192. if(iydon < g_image_in_y_min)
  193. g_image_in_y_min = iydon;
  194. if(iydon > g_image_in_y_max)
  195. g_image_in_y_max = iydon;
  196. printf ("XA = %6.2f YA = %6.2f\n", xdon, ydon);
  197. };
  198. if (h == 0 && w == OUT_WIDTH-1)
  199. {
  200. if(ixdon < g_image_in_x_min)
  201. g_image_in_x_min = ixdon;
  202. if(ixdon > g_image_in_x_max)
  203. g_image_in_x_max = ixdon;
  204. if(iydon < g_image_in_y_min)
  205. g_image_in_y_min = iydon;
  206. if(iydon > g_image_in_y_max)
  207. g_image_in_y_max = iydon;
  208. printf ("XB = %6.2f YB = %6.2f\n", xdon, ydon);
  209. };
  210. if (h == OUT_HEIGHT-1 && w == OUT_WIDTH-1)
  211. {
  212. if(ixdon < g_image_in_x_min)
  213. g_image_in_x_min = ixdon;
  214. if(ixdon > g_image_in_x_max)
  215. g_image_in_x_max = ixdon;
  216. if(iydon < g_image_in_y_min)
  217. g_image_in_y_min = iydon;
  218. if(iydon > g_image_in_y_max)
  219. g_image_in_y_max = iydon;
  220. printf ("XC = %6.2f YC = %6.2f\n", xdon, ydon);
  221. printf ("-------------------------------\n");
  222. };
  223. if (h == OUT_HEIGHT-1 && w == 0 )
  224. {
  225. if(ixdon < g_image_in_x_min)
  226. g_image_in_x_min = ixdon;
  227. if(ixdon > g_image_in_x_max)
  228. g_image_in_x_max = ixdon;
  229. if(iydon < g_image_in_y_min)
  230. g_image_in_y_min = iydon;
  231. if(iydon > g_image_in_y_max)
  232. g_image_in_y_max = iydon;
  233. printf ("XD = %6.2f YD = %6.2f\n", xdon, ydon);
  234. };
  235. udel++;
  236. // Insert border colors for debug image
  237. //if (h == 0) { $border{int(float ydon+0.5)}{int($xdon+0.5)} = 1; };
  238. //if (h != 0 && w == OUT_WIDTH-1) { $border{int(float ydon+0.5)}{int($xdon+0.5)} = 2; };
  239. //if (h == OUT_HEIGHT-1) { $border{int(float ydon+0.5)}{int($xdon+0.5)} = 3; };
  240. //if (h != 0 && line != OUT_HEIGHT-1 && w == 0) { $border{int(float ydon+0.5)}{int($xdon+0.5)} = 4; };
  241. }
  242. vdel--;
  243. }
  244. printf ("(%d, %d), (%d, %d), (%d, %d), (%d, %d)\n", g_image_in_x_min, g_image_in_y_min,
  245. g_image_in_x_max, g_image_in_y_min,
  246. g_image_in_x_min, g_image_in_y_max,
  247. g_image_in_x_max, g_image_in_y_max);
  248. }
  249. #define SINE 193
  250. #define COSINE 203
  251. #define MUL_FLOAT 2
  252. #define ASSIGN_V 1
  253. #define PLUS 0.25
  254. #define CWRITE 1
  255. #define SQRT 1
  256. #define DIVIDBY1 1
  257. #define IFCON 1
  258. #define FORCON 1
  259. #define COMPARE 1
  260. void createMapping_profile()
  261. {
  262. float pz2 = SINE;
  263. float pz1 = COSINE;
  264. float px2 = SINE;
  265. float py2 = COSINE;
  266. float rts = MUL_FLOAT;
  267. float px1 = MUL_FLOAT;
  268. float px3 = MUL_FLOAT;
  269. float py1 = MUL_FLOAT;
  270. float py3 = MUL_FLOAT;
  271. float cycles = pz2 + pz1 + px2 + py2 + rts + px1 + px3 + py1 + py3;
  272. pz1 = MUL_FLOAT;
  273. int r2 = MUL_FLOAT;
  274. int dr2 = MUL_FLOAT;
  275. cycles += pz1 + r2 + dr2;
  276. for( int h = OUT_HEIGHT; h > 0; h--)
  277. {
  278. cycles += PLUS;
  279. cycles += FORCON;
  280. for (int w = - OUT_WIDTH_HALF; w < OUT_WIDTH - OUT_WIDTH_HALF; w++)
  281. {
  282. cycles += PLUS;
  283. cycles += FORCON;
  284. cycles += IFCON;
  285. if((h == 0 ) && ( w == 0))
  286. {
  287. #ifdef DIR
  288. cycles += PLUS + MUL_FLOAT + PLUS + MUL_FLOAT;
  289. #else
  290. ux = px1 + udel * px2 - vdel * px3;
  291. #endif
  292. #ifdef DIR
  293. cycles += PLUS + MUL_FLOAT + PLUS + MUL_FLOAT;
  294. #else
  295. uy = py1 -udel * py2 - vdel * py3;
  296. #endif
  297. #ifdef DIR
  298. cycles += PLUS + MUL_FLOAT;
  299. #else
  300. float z = pz1 + vdel * pz2;
  301. #endif
  302. //vx = ux;
  303. //vy = uy;
  304. //vz = uz;
  305. }
  306. else if(w == 0) // v decrease
  307. {
  308. cycles += PLUS;
  309. cycles += PLUS;
  310. cycles += PLUS;
  311. //ux = vx;
  312. //uy = vy;
  313. //uz = vz;
  314. }
  315. else // w increase
  316. {
  317. cycles += PLUS;
  318. cycles += PLUS;
  319. }
  320. #ifdef DIR
  321. //float zclamp = -uz;
  322. #else
  323. float zclamp = uz;
  324. #endif
  325. cycles += MUL_FLOAT + PLUS + MUL_FLOAT + PLUS;
  326. cycles += SQRT;
  327. #ifdef DIR
  328. //dont1 -= uz;
  329. #else
  330. dont1 += uz;
  331. #endif
  332. cycles += DIVIDBY1;
  333. cycles += MUL_FLOAT;
  334. cycles += MUL_FLOAT;
  335. cycles += MUL_FLOAT;
  336. cycles += PLUS;
  337. cycles += PLUS;
  338. cycles += PLUS;
  339. cycles += PLUS;
  340. //// todo: try remove this condition
  341. //if (((xdist*xdist) + (ydist* ydist)) > dr2)
  342. //{
  343. // *mapping = 0;
  344. //}
  345. //else
  346. {
  347. cycles += PLUS;
  348. cycles += CWRITE;
  349. }
  350. //mapping++;
  351. }
  352. }
  353. printf ("cycles = %f\n", cycles);
  354. }
  355. void createPersp()
  356. {
  357. unsigned int * image_out = g_image_out;
  358. unsigned int * mapping = g_mapping;
  359. for( int h = 0; h < OUT_HEIGHT; h++)
  360. {
  361. for (int w = 0; w < OUT_WIDTH; w++)
  362. {
  363. int index = *mapping;
  364. // todo: try remove this condition
  365. if (index == 0)
  366. {
  367. *image_out = 0;
  368. }
  369. else
  370. {
  371. index = (index & 0xffff) + ((index >> 16) & 0xffff) * DONUT_WIDTH;
  372. *image_out = *(g_image_in + index);
  373. };
  374. image_out++;
  375. mapping++;
  376. }
  377. }
  378. }
  379. void writeDonut()
  380. {
  381. unsigned int * image_in = g_image_in;
  382. FILE *f;
  383. int filesize = 54 + 3* DONUT_WIDTH* DONUT_HEIGHT;
  384. unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
  385. unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};
  386. unsigned char bmppad[3] = {0,0,0};
  387. bmpfileheader[ 2] = (unsigned char)(filesize );
  388. bmpfileheader[ 3] = (unsigned char)(filesize>> 8);
  389. bmpfileheader[ 4] = (unsigned char)(filesize>>16);
  390. bmpfileheader[ 5] = (unsigned char)(filesize>>24);
  391. bmpinfoheader[ 4] = (unsigned char)( DONUT_WIDTH );
  392. bmpinfoheader[ 5] = (unsigned char)( DONUT_WIDTH>> 8);
  393. bmpinfoheader[ 6] = (unsigned char)( DONUT_WIDTH>>16);
  394. bmpinfoheader[ 7] = (unsigned char)( DONUT_WIDTH>>24);
  395. int rh = -DONUT_HEIGHT;
  396. bmpinfoheader[ 8] = (unsigned char)( rh );
  397. bmpinfoheader[ 9] = (unsigned char)( rh>> 8);
  398. bmpinfoheader[10] = (unsigned char)( rh>>16);
  399. bmpinfoheader[11] = (unsigned char)( rh>>24);
  400. f = fopen("C:\\0\\project\\hummingbird\\test\\donut.bmp","wb");
  401. fwrite(bmpfileheader,1,14,f);
  402. fwrite(bmpinfoheader,1,40,f);
  403. for( int h = 0; h < DONUT_HEIGHT; h++)
  404. {
  405. for(int w = 0; w < DONUT_WIDTH; w++)
  406. {
  407. fwrite((void*)image_in, 1, 1, f);
  408. fwrite((void*)image_in, 1, 1, f);
  409. fwrite((void*)image_in, 1, 1, f);
  410. image_in++;
  411. }
  412. fwrite(bmppad,1,(4-(DONUT_WIDTH*3)%4)%4,f);
  413. }
  414. fclose(f);
  415. }
  416. void writePersp(int step)
  417. {
  418. unsigned int * image_out = g_image_out;
  419. FILE *f;
  420. int filesize = 54 + 3* OUT_WIDTH* OUT_HEIGHT;
  421. unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
  422. unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};
  423. unsigned char bmppad[3] = {0,0,0};
  424. bmpfileheader[ 2] = (unsigned char)(filesize );
  425. bmpfileheader[ 3] = (unsigned char)(filesize>> 8);
  426. bmpfileheader[ 4] = (unsigned char)(filesize>>16);
  427. bmpfileheader[ 5] = (unsigned char)(filesize>>24);
  428. bmpinfoheader[ 4] = (unsigned char)( OUT_WIDTH );
  429. bmpinfoheader[ 5] = (unsigned char)( OUT_WIDTH>> 8);
  430. bmpinfoheader[ 6] = (unsigned char)( OUT_WIDTH>>16);
  431. bmpinfoheader[ 7] = (unsigned char)( OUT_WIDTH>>24);
  432. int rh = -OUT_HEIGHT;
  433. bmpinfoheader[ 8] = (unsigned char)( rh );
  434. bmpinfoheader[ 9] = (unsigned char)( rh>> 8);
  435. bmpinfoheader[10] = (unsigned char)( rh>>16);
  436. bmpinfoheader[11] = (unsigned char)( rh>>24);
  437. char ofile[256];
  438. sprintf (ofile, "C:\\0\\project\\hummingbird\\test\\persp_%d.bmp", step);
  439. f = fopen(ofile,"wb");
  440. fwrite(bmpfileheader,1,14,f);
  441. fwrite(bmpinfoheader,1,40,f);
  442. for( int h = 0; h < OUT_HEIGHT; h++)
  443. {
  444. for(int w = 0; w < OUT_WIDTH; w++)
  445. {
  446. fwrite((void*)image_out, 1, 1, f);
  447. fwrite((void*)image_out, 1, 1, f);
  448. fwrite((void*)image_out, 1, 1, f);
  449. image_out++;
  450. }
  451. fwrite(bmppad,1,(4-(OUT_WIDTH*3)%4)%4,f);
  452. }
  453. fclose(f);
  454. }
  455. int _tmain(int argc, _TCHAR* argv[])
  456. {
  457. g_image_size_in = DONUT_WIDTH * DONUT_HEIGHT;
  458. g_image_size_out = OUT_WIDTH * OUT_HEIGHT;
  459. g_image_in = new unsigned int[g_image_size_in];
  460. g_image_out = new unsigned int[g_image_size_out];
  461. g_mapping = new unsigned int[g_image_size_out];
  462. //writeDonut();
  463. int step = 135;
  464. //for (int step = 0; step <= 360; step += 45)
  465. {
  466. createMapping(step);
  467. //createMapping_profile();
  468. //return 0;
  469. readSrcDonutFile();
  470. for(int i = 0; i < 3; i++)
  471. {
  472. createPersp();
  473. writePersp(step++);
  474. printf ("%d\n", step);
  475. }
  476. }
  477. delete g_image_in;
  478. delete g_image_out;
  479. delete g_mapping;
  480. return 0;
  481. }
  482. // read out a block of data once and reuse those data as much as possible - statistics to find out how to read out block of data