PageRenderTime 22ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src_c/tools.c

https://bitbucket.org/psteinhoff/pyptv-git
C | 507 lines | 332 code | 109 blank | 66 comment | 85 complexity | 94d8fa442525982f71c88524865bbf16 MD5 | raw file
  1. #include <stdlib.h>
  2. #include "ptv.h"
  3. #include "tools.h"
  4. /* declaration */
  5. void qs_coord2d_x ();
  6. void qs_target_y ();
  7. void qs_coord2d_pnr ();
  8. void qs_con ();
  9. /* Write exterior and interior orientation, and - if available, parameters for
  10. distortion corrections.
  11. Arguments:
  12. Exterior Ex - exterior orientation.
  13. Interior I - interior orientation.
  14. Glass G - glass parameters.
  15. ap_52 addp - optional additional (distortion) parameters. NULL is fine if
  16. add_file is NULL.
  17. char *filename - path of file to contain interior, exterior and glass
  18. orientation data.
  19. char *add_file - path of file to contain added (distortions) parameters.
  20. */
  21. void write_ori (Ex, I, G, ap, filename, add_file)
  22. Exterior Ex;
  23. Interior I;
  24. Glass G;
  25. ap_52 ap;
  26. char *filename, *add_file;
  27. {
  28. FILE *fp;
  29. int i;
  30. fp = fopen (filename, "w");
  31. fprintf (fp, "%11.4f %11.4f %11.4f\n %10.7f %10.7f %10.7f\n\n",
  32. Ex.x0, Ex.y0, Ex.z0, Ex.omega, Ex.phi, Ex.kappa);
  33. for (i=0; i<3; i++) fprintf (fp, " %10.7f %10.7f %10.7f\n",
  34. Ex.dm[i][0], Ex.dm[i][1], Ex.dm[i][2]);
  35. fprintf (fp,"\n %8.4f %8.4f\n %8.4f\n", I.xh, I.yh, I.cc);
  36. fprintf (fp,"\n %20.15f %20.15f %20.15f\n", G.vec_x, G.vec_y, G.vec_z);
  37. fclose (fp);
  38. if (add_file == NULL) return;
  39. fp = fopen (add_file, "w");
  40. fprintf (fp, "%f %f %f %f %f %f %f", ap.k1, ap.k2, ap.k3, ap.p1, ap.p2,
  41. ap.scx, ap.she);
  42. fclose (fp);
  43. }
  44. int read_ori (Ex, I, G, ori_file, addp, add_file, add_fallback)
  45. /* read exterior and interior orientation, and - if available, parameters for
  46. distortion corrections.
  47. Arguments:
  48. Exterior *Ex - output buffer for exterior orientation.
  49. Interior *I - output buffer for interior orientation.
  50. Glass *G - output buffer for glass parameters.
  51. char *ori_file - path of file contatining interior and exterior orientation
  52. data
  53. ap_52 addp - output buffer for additional (distortion) parameters.
  54. char *add_file - path of file contatining added (distortions) parameters.
  55. char *add_fallback - path to file for use if add_file can't be openned.
  56. Returns:
  57. true value on success, false on failure. Failure can happen if add_file
  58. can't be opened, or the fscanf results are wrong, but if the additional
  59. parameters' file or fallback can't be opened, they're just assigned default
  60. values.
  61. */
  62. Exterior *Ex;
  63. Interior *I;
  64. Glass *G;
  65. ap_52 *addp;
  66. char *ori_file, *add_file, *add_fallback;
  67. {
  68. FILE *fp;
  69. int i, scan_res;
  70. if (!(fp = fopen_r(ori_file))) return 0;
  71. /* Exterior */
  72. scan_res = fscanf (fp, "%lf %lf %lf %lf %lf %lf",
  73. &(Ex->x0), &(Ex->y0), &(Ex->z0),
  74. &(Ex->omega), &(Ex->phi), &(Ex->kappa));
  75. if (scan_res != 6) return 0;
  76. /* Exterior rotation matrix */
  77. for (i=0; i<3; i++) {
  78. scan_res = fscanf (fp, " %lf %lf %lf",
  79. &(Ex->dm[i][0]), &(Ex->dm[i][1]), &(Ex->dm[i][2]));
  80. if (scan_res != 3) return 0;
  81. }
  82. /* Interior */
  83. scan_res = fscanf (fp, "%lf %lf %lf", &(I->xh), &(I->yh), &(I->cc));
  84. if (scan_res != 3) return 0;
  85. /* Glass */
  86. scan_res = fscanf (fp, "%lf %lf %lf", &(G->vec_x), &(G->vec_y), &(G->vec_z));
  87. if (scan_res != 3) return 0;
  88. fclose(fp);
  89. /* Additional: */
  90. fp = fopen(add_file, "r");
  91. if ((fp == NULL) && add_fallback) fp = fopen (add_fallback, "r");
  92. if (fp) {
  93. scan_res = fscanf (fp, "%lf %lf %lf %lf %lf %lf %lf",
  94. &(addp->k1), &(addp->k2), &(addp->k3), &(addp->p1), &(addp->p2),
  95. &(addp->scx), &(addp->she));
  96. fclose (fp);
  97. } else {
  98. printf("no addpar fallback used\n"); // Waits for proper logging.
  99. addp->k1 = addp->k2 = addp->k3 = addp->p1 = addp->p2 = addp->she = 0.0;
  100. addp->scx=1.0;
  101. }
  102. return 1;
  103. }
  104. FILE *fopen_r (filename)
  105. char *filename;
  106. /* tries to open a file;
  107. gives a message, if it cannot open it
  108. and waits until it has been created */
  109. {
  110. FILE *fpr;
  111. int count;
  112. fpr = fopen (filename, "r");
  113. if ( ! fpr)
  114. {
  115. printf ("could not open %s, please create this file\n", filename);
  116. /* wait until file can be opened */
  117. while ( ! fpr) fpr = fopen (filename, "r");
  118. /* wait until file really created */
  119. for (count=0; count<100000; count++);
  120. }
  121. return (fpr);
  122. }
  123. void compose_name_plus_nr (basename, str, nr, filename)
  124. char basename[256], str[256], filename[256];
  125. int nr;
  126. {
  127. char nr_ch[256];
  128. // if (nr < 10) sprintf (nr_ch, "00%1d", nr);
  129. // else if (nr < 100) sprintf (nr_ch, "0%2d", nr);
  130. if (nr < 10) sprintf (nr_ch, "%1d", nr);
  131. else if (nr < 100) sprintf (nr_ch, "%2d", nr);
  132. else sprintf (nr_ch, "%3d", nr);
  133. sprintf (filename, "%s%s%s", basename, str, nr_ch);
  134. }
  135. void compose_name_plus_nr_str (basename, str, nr, filename)
  136. char basename[256], str[256], filename[256];
  137. int nr;
  138. {
  139. char nr_ch[256];
  140. if (nr < 10) sprintf (nr_ch, "%1d", nr);
  141. else if (nr < 100) sprintf (nr_ch, "%2d", nr);
  142. else sprintf (nr_ch, "%3d", nr);
  143. sprintf (filename, "%s%s%s", basename, nr_ch, str);
  144. }
  145. /* find nearest neighbours */
  146. int kill_in_list ( nr, num, ms_x, ms_y)
  147. int nr, num;
  148. int ms_x, ms_y;
  149. {
  150. int i, imin = 9999, intx, inty;
  151. double x, y, d, dmin = 9999;
  152. if (zoom_f[nr] > 1)
  153. {
  154. printf ("cannot delete point from zoomed image");
  155. return (0);
  156. }
  157. for (i=0; i<num; i++)
  158. {
  159. x = (double) ms_x - pix[nr][i].x;
  160. y = (double) ms_y - pix[nr][i].y;
  161. d = sqrt (x*x + y*y);
  162. if (d < dmin)
  163. {
  164. dmin = d; imin = i;
  165. }
  166. }
  167. if (dmin > 10) return (-1); /* limit: 10 pixel */
  168. intx = (int) pix[nr][imin].x;
  169. inty = (int) pix[nr][imin].y;
  170. for (i=imin; i<num; i++) pix[nr][i] = pix[nr][i+1];
  171. return (imin);
  172. }
  173. int nearest_neighbour_geo (crd, num, x, y, eps)
  174. coord_2d crd[];
  175. int num;
  176. double x, y, eps;
  177. {
  178. register int j;
  179. int j0, dj, pnr = -999;
  180. double d, dmin=1e20, xmin, xmax, ymin, ymax;
  181. xmin = x - eps; xmax = x + eps; ymin = y - eps; ymax = y + eps;
  182. /* binarized search for start point of candidate search */
  183. for (j0=num/2, dj=num/4; dj>1; dj/=2)
  184. {
  185. if (crd[j0].x < xmin) j0 += dj;
  186. else j0 -= dj;
  187. }
  188. j0 -= 12; if (j0 < 0) j0 = 0; /* due to trunc */
  189. for (j=j0; j<num; j++) /* candidate search */
  190. {
  191. if (crd[j].x > xmax) break; /* finish search */
  192. if (crd[j].y > ymin && crd[j].y < ymax)
  193. {
  194. d = sqrt ((x-crd[j].x)*(x-crd[j].x) + (y-crd[j].y)*(y-crd[j].y));
  195. if (d < dmin)
  196. {
  197. dmin = d; pnr = j;
  198. }
  199. }
  200. }
  201. return (pnr);
  202. }
  203. int nearest_neighbour_pix (pix, num, x, y, eps)
  204. target pix[];
  205. int num;
  206. double x, y, eps;
  207. {
  208. register int j;
  209. int pnr = -999;
  210. double d, dmin=1e20, xmin, xmax, ymin, ymax;
  211. xmin = x - eps; xmax = x + eps; ymin = y - eps; ymax = y + eps;
  212. for (j=0; j<num; j++) /* candidate search */
  213. {
  214. if (pix[j].y>ymin && pix[j].y<ymax && pix[j].x>xmin && pix[j].x<xmax)
  215. {
  216. d = sqrt ((x-pix[j].x)*(x-pix[j].x) + (y-pix[j].y)*(y-pix[j].y));
  217. if (d < dmin)
  218. {
  219. dmin = d; pnr = j;
  220. }
  221. }
  222. }
  223. return (pnr);
  224. }
  225. /***********************************************************************/
  226. /***********************************************************************/
  227. /* sorting routines */
  228. /***********************************************************************/
  229. /* bubble sorts */
  230. void bubble_conlist (item, count)
  231. correspond *item;
  232. int count;
  233. {
  234. int i,j;
  235. correspond temp;
  236. for (i=1; i<count; ++i) for (j=count-1; j>=i; --j)
  237. {
  238. if (item[j-1].corr > item[j].corr)
  239. {
  240. temp = *(item+j-1); *(item+j-1) = *(item+j); *(item+j) = temp;
  241. }
  242. }
  243. }
  244. /***********************************************************************/
  245. /***********************************************************************/
  246. /* quicksort algorithms for several issues */
  247. /***********************************************************************/
  248. /* quicksort of 2d coordinates in x-order */
  249. void quicksort_coord2d_x (crd, num)
  250. coord_2d *crd;
  251. int num;
  252. {
  253. qs_coord2d_x (crd, 0, num-1);
  254. }
  255. void qs_coord2d_x (crd, left, right)
  256. coord_2d *crd;
  257. int left, right;
  258. {
  259. register int i, j;
  260. double xm;
  261. coord_2d temp;
  262. i = left; j = right; xm = crd[(left+right)/2].x;
  263. do
  264. {
  265. while (crd[i].x < xm && i<right) i++;
  266. while (xm < crd[j].x && j>left) j--;
  267. if (i <= j)
  268. {
  269. temp = crd[i];
  270. crd[i] = crd[j];
  271. crd[j] = temp;
  272. i++; j--;
  273. }
  274. }
  275. while (i <= j);
  276. if (left < j) qs_coord2d_x (crd, left, j);
  277. if (i < right) qs_coord2d_x (crd, i, right);
  278. }
  279. /***********************************************************************/
  280. /* quicksort of 2d coordinates in pnr-order */
  281. void quicksort_coord2d_pnr (crd, num)
  282. coord_2d *crd;
  283. int num;
  284. {
  285. qs_coord2d_pnr (crd, 0, num-1);
  286. }
  287. void qs_coord2d_pnr (crd, left, right)
  288. coord_2d *crd;
  289. int left, right;
  290. {
  291. register int i, j;
  292. double pnrm;
  293. coord_2d temp;
  294. i = left; j = right; pnrm = crd[(left+right)/2].pnr;
  295. do
  296. {
  297. while (crd[i].pnr < pnrm && i<right) i++;
  298. while (pnrm < crd[j].pnr && j>left) j--;
  299. if (i <= j)
  300. {
  301. temp = crd[i];
  302. crd[i] = crd[j];
  303. crd[j] = temp;
  304. i++; j--;
  305. }
  306. }
  307. while (i <= j);
  308. if (left < j) qs_coord2d_pnr (crd, left, j);
  309. if (i < right) qs_coord2d_pnr (crd, i, right);
  310. }
  311. /***********************************************************************/
  312. /* quicksort of targets in y-order */
  313. void quicksort_target_y (pix, num)
  314. target *pix;
  315. int num;
  316. {
  317. qs_target_y (pix, 0, num-1);
  318. }
  319. void qs_target_y (pix, left, right)
  320. target *pix;
  321. int left, right;
  322. {
  323. register int i, j;
  324. double ym;
  325. target temp;
  326. i = left; j = right; ym = pix[(left+right)/2].y;
  327. do
  328. {
  329. while (pix[i].y < ym && i<right) i++;
  330. while (ym < pix[j].y && j>left) j--;
  331. if (i <= j)
  332. {
  333. temp = pix[i];
  334. pix[i] = pix[j];
  335. pix[j] = temp;
  336. i++; j--;
  337. }
  338. }
  339. while (i <= j);
  340. if (left < j) qs_target_y (pix, left, j);
  341. if (i < right) qs_target_y (pix, i, right);
  342. }
  343. /***********************************************************************/
  344. /* quicksort for list of correspondences in order of match quality */
  345. /* 4 camera version */
  346. void quicksort_con (con, num)
  347. n_tupel *con;
  348. int num;
  349. {
  350. qs_con (con, 0, num-1);
  351. }
  352. void qs_con (con, left, right)
  353. n_tupel *con;
  354. int left, right;
  355. {
  356. register int i, j;
  357. double xm;
  358. n_tupel temp;
  359. i = left; j = right; xm = con[(left+right)/2].corr;
  360. do
  361. {
  362. while (con[i].corr > xm && i<right) i++;
  363. while (xm > con[j].corr && j>left) j--;
  364. if (i <= j)
  365. {
  366. temp = con[i];
  367. con[i] = con[j];
  368. con[j] = temp;
  369. i++; j--;
  370. }
  371. }
  372. while (i <= j);
  373. if (left < j) qs_con (con, left, j);
  374. if (i < right) qs_con (con, i, right);
  375. }
  376. /***SORTING ALGORIHTMUS****/
  377. void sort(int n, float a[], int b[])
  378. {
  379. int flag = 0, i, itemp;
  380. float ftemp;
  381. do {
  382. flag = 0;
  383. for(i=0; i<(n-1); i++)
  384. if(a[i] > a[i+1]) {
  385. ftemp = a[i];
  386. itemp = b[i];
  387. a[i] = a[i+1];
  388. b[i] = b[i+1];
  389. a[i+1] = ftemp;
  390. b[i+1] = itemp;
  391. flag = 1;
  392. }
  393. }while(flag);
  394. }
  395. /***********************************************************************/