writeVTK.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /***************************************************************************
  2. *
  3. * MODULE: v.out.vtk
  4. * AUTHOR(S): Soeren Gebbert
  5. *
  6. * PURPOSE: v.out.vtk: writes ASCII VTK file
  7. * this module is based on v.out.ascii
  8. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public
  11. * License (>=v2). Read the file COPYING that comes with GRASS
  12. * for details.
  13. *
  14. ****************************************************************************/
  15. #include <stdlib.h>
  16. #include <grass/Vect.h>
  17. #include <grass/gis.h>
  18. #include <grass/glocale.h>
  19. #include "writeVTK.h"
  20. #include "local_proto.h"
  21. #include <grass/config.h>
  22. /*Prototype */
  23. /*Formated coordinates output */
  24. static void write_point_coordinates(struct line_pnts *Points, int dp,
  25. double scale, FILE * ascii);
  26. /* ************************************************************************* */
  27. /* This function writes the vtk points and coordinates ********************* */
  28. /* ************************************************************************* */
  29. int write_vtk_points(FILE * ascii, struct Map_info *Map, VTKInfo * info,
  30. int *types, int typenum, int dp, double scale)
  31. {
  32. int type, cur, i, k, centroid;
  33. int pointoffset = 0;
  34. int lineoffset = 0;
  35. int polygonoffset = 0;
  36. static struct line_pnts *Points;
  37. struct line_cats *Cats;
  38. Points = Vect_new_line_struct(); /* init line_pnts struct */
  39. Cats = Vect_new_cats_struct();
  40. G_message("Writing the coordinates");
  41. /*For every available vector type */
  42. for (k = 0; k < typenum; k++) {
  43. /*POINT KERNEL CENTROID */
  44. if (types[k] == GV_POINT || types[k] == GV_KERNEL ||
  45. types[k] == GV_CENTROID) {
  46. /*Get the number of the points to generate */
  47. info->typeinfo[types[k]]->pointoffset = pointoffset;
  48. info->typeinfo[types[k]]->numpoints =
  49. Vect_get_num_primitives(Map, types[k]);
  50. pointoffset += info->typeinfo[types[k]]->numpoints;
  51. info->typeinfo[types[k]]->numvertices =
  52. info->typeinfo[types[k]]->numpoints;
  53. info->maxnumvertices += info->typeinfo[types[k]]->numpoints;
  54. info->maxnumpoints += info->typeinfo[types[k]]->numpoints;
  55. /*
  56. * printf("Points Type %i Number %i offset %i\n", types[k],
  57. * info->typeinfo[types[k]]->numpoints,
  58. * info->typeinfo[types[k]]->pointoffset);
  59. */
  60. }
  61. }
  62. for (k = 0; k < typenum; k++) {
  63. /*LINE BOUNDARY */
  64. if (types[k] == GV_LINE || types[k] == GV_BOUNDARY) {
  65. info->typeinfo[types[k]]->pointoffset = pointoffset;
  66. info->typeinfo[types[k]]->lineoffset = lineoffset;
  67. /*count the number of line_nodes and lines */
  68. Vect_rewind(Map);
  69. while (1) {
  70. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  71. break;
  72. if (type == -2) /* EOF */
  73. break;
  74. if (type == types[k]) {
  75. info->typeinfo[types[k]]->numpoints += Points->n_points;
  76. info->typeinfo[types[k]]->numlines++;
  77. }
  78. }
  79. pointoffset += info->typeinfo[types[k]]->numpoints;
  80. lineoffset += info->typeinfo[types[k]]->lineoffset;
  81. info->maxnumpoints += info->typeinfo[types[k]]->numpoints;
  82. info->maxnumlinepoints += info->typeinfo[types[k]]->numpoints;
  83. info->maxnumlines += info->typeinfo[types[k]]->numlines;
  84. /*
  85. * printf("Lines Type %i Number %i offset %i\n", types[k],
  86. * info->typeinfo[types[k]]->numlines,
  87. * info->typeinfo[types[k]]->lineoffset);
  88. */
  89. }
  90. }
  91. for (k = 0; k < typenum; k++) {
  92. /*FACE */
  93. if (types[k] == GV_FACE) {
  94. info->typeinfo[types[k]]->pointoffset = pointoffset;
  95. info->typeinfo[types[k]]->polygonoffset = polygonoffset;
  96. /*count the number of line_nodes and lines */
  97. Vect_rewind(Map);
  98. while (1) {
  99. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  100. break;
  101. if (type == -2) /* EOF */
  102. break;
  103. if (type == types[k]) {
  104. info->typeinfo[types[k]]->numpoints += Points->n_points;
  105. info->typeinfo[types[k]]->numpolygons++;
  106. }
  107. }
  108. pointoffset += info->typeinfo[types[k]]->numpoints;
  109. polygonoffset += info->typeinfo[types[k]]->numpolygons;
  110. info->maxnumpoints += info->typeinfo[types[k]]->numpoints;
  111. info->maxnumpolygonpoints += info->typeinfo[types[k]]->numpoints;
  112. info->maxnumpolygons += info->typeinfo[types[k]]->numpolygons;
  113. /*
  114. * printf("Polygons Type %i Number %i offset %i\n", types[k],
  115. * info->typeinfo[types[k]]->numpolygons,
  116. * info->typeinfo[types[k]]->polygonoffset);
  117. */
  118. }
  119. }
  120. for (k = 0; k < typenum; k++) {
  121. /*AREA */
  122. if (types[k] == GV_AREA) {
  123. info->typeinfo[types[k]]->numpolygons = Vect_get_num_areas(Map);
  124. info->typeinfo[types[k]]->pointoffset = pointoffset;
  125. info->typeinfo[types[k]]->polygonoffset = polygonoffset;
  126. /*Count the coordinate points */
  127. Vect_rewind(Map);
  128. for (i = 1; i <= info->typeinfo[types[k]]->numpolygons; i++) {
  129. centroid = Vect_get_area_centroid(Map, i);
  130. if (centroid > 0) {
  131. Vect_read_line(Map, NULL, Cats, centroid);
  132. }
  133. Vect_get_area_points(Map, i, Points);
  134. info->typeinfo[types[k]]->numpoints += Points->n_points;
  135. }
  136. pointoffset += info->typeinfo[types[k]]->numpoints;
  137. polygonoffset += info->typeinfo[types[k]]->numpolygons;
  138. info->maxnumpoints += info->typeinfo[types[k]]->numpoints;
  139. info->maxnumpolygonpoints += info->typeinfo[types[k]]->numpoints;
  140. info->maxnumpolygons += info->typeinfo[types[k]]->numpolygons;
  141. /*
  142. * printf("Polygons Type %i Number %i offset %i\n", types[k],
  143. * info->typeinfo[types[k]]->numpolygons,
  144. * info->typeinfo[types[k]]->polygonoffset);
  145. */
  146. }
  147. }
  148. /*
  149. * printf("Maxnum points %i \n", info->maxnumpoints);
  150. * printf("Maxnum vertices %i \n", info->maxnumvertices);
  151. * printf("Maxnum lines %i \n", info->maxnumlines);
  152. * printf("Maxnum line points %i \n", info->maxnumlinepoints);
  153. * printf("Maxnum polygons %i \n", info->maxnumpolygons);
  154. * printf("Maxnum polygon points %i \n", info->maxnumpolygonpoints);
  155. */
  156. /*break if nothing to generate */
  157. if (info->maxnumpoints == 0)
  158. G_fatal_error(_("No coordinates to generate the output! Maybe an empty vector type chosen?"));
  159. /************************************************/
  160. /*Write the coordinates into the vtk ascii file */
  161. /************************************************/
  162. fprintf(ascii, "POINTS %i float\n", info->maxnumpoints);
  163. /*For every available vector type */
  164. for (k = 0; k < typenum; k++) {
  165. /*POINT KERNEL CENTROID */
  166. if (types[k] == GV_POINT || types[k] == GV_KERNEL ||
  167. types[k] == GV_CENTROID) {
  168. Vect_rewind(Map);
  169. /*Write the coordinates */
  170. cur = 0;
  171. while (1) {
  172. if (cur <= info->typeinfo[types[k]]->numpoints)
  173. G_percent(cur, info->typeinfo[types[k]]->numpoints, 2);
  174. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  175. break;
  176. if (type == -2) /* EOF */
  177. break;
  178. if (type == types[k]) {
  179. write_point_coordinates(Points, dp, scale, ascii);
  180. if (Cats->n_cats == 0)
  181. info->typeinfo[types[k]]->generatedata = 0; /*No data generation */
  182. }
  183. cur++;
  184. }
  185. }
  186. }
  187. for (k = 0; k < typenum; k++) {
  188. /*LINE BOUNDARY */
  189. if (types[k] == GV_LINE || types[k] == GV_BOUNDARY) {
  190. Vect_rewind(Map);
  191. cur = 0;
  192. while (1) {
  193. if (cur <= info->typeinfo[types[k]]->numlines)
  194. G_percent(cur, info->typeinfo[types[k]]->numlines, 2);
  195. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  196. break;
  197. if (type == -2) /* EOF */
  198. break;
  199. if (type == types[k]) {
  200. write_point_coordinates(Points, dp, scale, ascii);
  201. }
  202. cur++;
  203. }
  204. }
  205. }
  206. for (k = 0; k < typenum; k++) {
  207. /* FACE */
  208. if (types[k] == GV_FACE) {
  209. Vect_rewind(Map);
  210. cur = 0;
  211. while (1) {
  212. if (cur <= info->typeinfo[types[k]]->numpolygons)
  213. G_percent(cur, info->typeinfo[types[k]]->numpolygons, 2);
  214. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  215. break;
  216. if (type == -2) /* EOF */
  217. break;
  218. if (type == types[k]) {
  219. write_point_coordinates(Points, dp, scale, ascii);
  220. }
  221. cur++;
  222. }
  223. }
  224. }
  225. for (k = 0; k < typenum; k++) {
  226. /* AREA */
  227. if (types[k] == GV_AREA) {
  228. Vect_rewind(Map);
  229. for (i = 1; i <= info->typeinfo[types[k]]->numpolygons; i++) {
  230. centroid = Vect_get_area_centroid(Map, i);
  231. if (centroid > 0) {
  232. Vect_read_line(Map, NULL, Cats, centroid);
  233. }
  234. Vect_get_area_points(Map, i, Points);
  235. write_point_coordinates(Points, dp, scale, ascii);
  236. }
  237. }
  238. }
  239. return 1;
  240. }
  241. /* ************************************************************************* */
  242. /* This function writes the vtk cells ************************************** */
  243. /* ************************************************************************* */
  244. int write_vtk_cells(FILE * ascii, struct Map_info *Map, VTKInfo * info,
  245. int *types, int typenum)
  246. {
  247. int type, i, j, k, centroid;
  248. static struct line_pnts *Points;
  249. struct line_cats *Cats;
  250. /*The keywords may only be written once! */
  251. int vertkeyword = 1;
  252. int linekeyword = 1;
  253. int polykeyword = 1;
  254. G_message("Writing vtk cells");
  255. Points = Vect_new_line_struct(); /* init line_pnts struct */
  256. Cats = Vect_new_cats_struct();
  257. /*For every available vector type */
  258. for (k = 0; k < typenum; k++) {
  259. /*POINT KERNEL CENTROID */
  260. if (types[k] == GV_POINT || types[k] == GV_KERNEL ||
  261. types[k] == GV_CENTROID) {
  262. Vect_rewind(Map);
  263. /*Write the vertices */
  264. if (info->typeinfo[types[k]]->numpoints > 0) {
  265. if (vertkeyword) {
  266. fprintf(ascii, "VERTICES %i %i\n", info->maxnumvertices,
  267. info->maxnumvertices * 2);
  268. vertkeyword = 0;
  269. }
  270. for (i = 0; i < info->typeinfo[types[k]]->numpoints; i++) {
  271. fprintf(ascii, "1 %i\n",
  272. i + info->typeinfo[types[k]]->pointoffset);
  273. }
  274. fprintf(ascii, "\n");
  275. }
  276. }
  277. }
  278. for (k = 0; k < typenum; k++) {
  279. /*LINE BOUNDARY */
  280. if (types[k] == GV_LINE || types[k] == GV_BOUNDARY) {
  281. Vect_rewind(Map);
  282. if (info->maxnumlines > 0) {
  283. if (linekeyword) {
  284. fprintf(ascii, "LINES %i %i\n", info->maxnumlines,
  285. info->maxnumlinepoints + info->maxnumlines);
  286. linekeyword = 0;
  287. }
  288. Vect_rewind(Map);
  289. i = 0;
  290. while (1) {
  291. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  292. break;
  293. if (type == -2) /* EOF */
  294. break;
  295. if (type == types[k]) {
  296. /*Check for data generation */
  297. if (Cats->n_cats == 0)
  298. info->typeinfo[types[k]]->generatedata = 0; /*No data generation */
  299. fprintf(ascii, "%i", Points->n_points);
  300. while (Points->n_points--) {
  301. fprintf(ascii, " %i",
  302. i +
  303. info->typeinfo[types[k]]->pointoffset);
  304. i++;
  305. }
  306. fprintf(ascii, "\n");
  307. }
  308. }
  309. }
  310. }
  311. }
  312. for (k = 0; k < typenum; k++) {
  313. /*LINE BOUNDARY FACE */
  314. if (types[k] == GV_FACE) {
  315. Vect_rewind(Map);
  316. if (info->maxnumpolygons > 0) {
  317. if (polykeyword) {
  318. fprintf(ascii, "POLYGONS %i %i\n",
  319. info->maxnumpolygons,
  320. info->maxnumpolygonpoints + info->maxnumpolygons);
  321. polykeyword = 0;
  322. }
  323. Vect_rewind(Map);
  324. i = 0;
  325. while (1) {
  326. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  327. break;
  328. if (type == -2) /* EOF */
  329. break;
  330. if (type == types[k]) {
  331. /*Check for data generation */
  332. if (Cats->n_cats == 0)
  333. info->typeinfo[types[k]]->generatedata = 0; /*No data generation */
  334. fprintf(ascii, "%i", Points->n_points);
  335. while (Points->n_points--) {
  336. fprintf(ascii, " %i",
  337. i +
  338. info->typeinfo[types[k]]->pointoffset);
  339. i++;
  340. }
  341. fprintf(ascii, "\n");
  342. }
  343. }
  344. }
  345. }
  346. }
  347. for (k = 0; k < typenum; k++) {
  348. /*AREA */
  349. if (types[k] == GV_AREA) {
  350. Vect_rewind(Map);
  351. if (info->maxnumpolygons > 0) {
  352. if (polykeyword) {
  353. fprintf(ascii, "POLYGONS %i %i\n",
  354. info->maxnumpolygons,
  355. info->maxnumpolygonpoints + info->maxnumpolygons);
  356. polykeyword = 0;
  357. }
  358. j = 0;
  359. for (i = 1; i <= info->typeinfo[types[k]]->numpolygons; i++) {
  360. centroid = Vect_get_area_centroid(Map, i);
  361. if (centroid > 0) {
  362. Vect_read_line(Map, NULL, Cats, centroid);
  363. }
  364. Vect_get_area_points(Map, i, Points);
  365. /*Check for data generation */
  366. if (Cats->n_cats == 0)
  367. info->typeinfo[types[k]]->generatedata = 0; /*No data generation */
  368. fprintf(ascii, "%i", Points->n_points);
  369. while (Points->n_points--) {
  370. fprintf(ascii, " %i",
  371. j + info->typeinfo[types[k]]->pointoffset);
  372. j++;
  373. }
  374. fprintf(ascii, "\n");
  375. }
  376. }
  377. }
  378. }
  379. return 1;
  380. }
  381. /* ************************************************************************* */
  382. /* This function writes the categories as vtk cell data ******************** */
  383. /* ************************************************************************* */
  384. int write_vtk_cat_data(FILE * ascii, struct Map_info *Map, VTKInfo * info,
  385. int layer, int *types, int typenum, int dp)
  386. {
  387. int type, cat, i, k, centroid;
  388. static struct line_pnts *Points;
  389. struct line_cats *Cats;
  390. /*The keywords may only be written once! */
  391. int numcelldata =
  392. info->maxnumvertices + info->maxnumlines + info->maxnumpolygons;
  393. Points = Vect_new_line_struct(); /* init line_pnts struct */
  394. Cats = Vect_new_cats_struct();
  395. G_message("Writing category cell data");
  396. if (numcelldata > 0) {
  397. /*Write the pointdata */
  398. fprintf(ascii, "CELL_DATA %i\n", numcelldata);
  399. fprintf(ascii, "SCALARS cat_%s int 1\n", Map->name);
  400. fprintf(ascii, "LOOKUP_TABLE default\n");
  401. /*For every available vector type */
  402. for (k = 0; k < typenum; k++) {
  403. /*POINT KERNEL CENTROID */
  404. if (types[k] == GV_POINT || types[k] == GV_KERNEL ||
  405. types[k] == GV_CENTROID) {
  406. Vect_rewind(Map);
  407. while (1) {
  408. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  409. break;
  410. if (type == -2) /* EOF */
  411. break;
  412. if (type == types[k]) {
  413. Vect_cat_get(Cats, layer, &cat);
  414. fprintf(ascii, " %d", cat);
  415. }
  416. }
  417. }
  418. }
  419. for (k = 0; k < typenum; k++) {
  420. /*LINE BOUNDARY */
  421. if (types[k] == GV_LINE || types[k] == GV_BOUNDARY) {
  422. Vect_rewind(Map);
  423. while (1) {
  424. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  425. break;
  426. if (type == -2) /* EOF */
  427. break;
  428. if (type == types[k]) {
  429. Vect_cat_get(Cats, layer, &cat);
  430. fprintf(ascii, " %d", cat);
  431. }
  432. }
  433. }
  434. }
  435. for (k = 0; k < typenum; k++) {
  436. /*FACE */
  437. if (types[k] == GV_FACE) {
  438. Vect_rewind(Map);
  439. while (1) {
  440. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  441. break;
  442. if (type == -2) /* EOF */
  443. break;
  444. if (type == types[k]) {
  445. Vect_cat_get(Cats, layer, &cat);
  446. fprintf(ascii, " %d", cat);
  447. }
  448. }
  449. }
  450. }
  451. for (k = 0; k < typenum; k++) {
  452. /*AREA */
  453. if (types[k] == GV_AREA) {
  454. Vect_rewind(Map);
  455. for (i = 1; i <= info->typeinfo[types[k]]->numpolygons; i++) {
  456. centroid = Vect_get_area_centroid(Map, i);
  457. if (centroid > 0) {
  458. Vect_read_line(Map, NULL, Cats, centroid);
  459. }
  460. Vect_cat_get(Cats, layer, &cat);
  461. fprintf(ascii, " %d", cat);
  462. }
  463. }
  464. }
  465. }
  466. return 1;
  467. }
  468. int write_vtk_db_data(FILE * ascii, struct Map_info *Map, VTKInfo * info,
  469. int layer, int *types, int typenum, int dp)
  470. {
  471. G_message("Writing database cell/point data");
  472. return 1;
  473. }
  474. /* ************************************************************************* */
  475. /* This function writes the point coordinates and the geometric feature **** */
  476. /* ************************************************************************* */
  477. int write_vtk(FILE * ascii, struct Map_info *Map, int layer, int *types,
  478. int typenum, int dp, double scale)
  479. {
  480. VTKInfo *info;
  481. VTKTypeInfo **typeinfo;
  482. int i;
  483. int infonum =
  484. GV_POINT + GV_KERNEL + GV_CENTROID + GV_LINE + GV_BOUNDARY + GV_FACE +
  485. GV_AREA;
  486. /*Initiate the typeinfo structure for every supported type */
  487. typeinfo = (VTKTypeInfo **) calloc(infonum, sizeof(VTKTypeInfo *));
  488. for (i = 0; i < infonum; i++) {
  489. typeinfo[i] = (VTKTypeInfo *) calloc(1, sizeof(VTKTypeInfo));
  490. typeinfo[i]->numpoints = 0;
  491. typeinfo[i]->pointoffset = 0;
  492. typeinfo[i]->numvertices = 0;
  493. typeinfo[i]->verticesoffset = 0;
  494. typeinfo[i]->numlines = 0;
  495. typeinfo[i]->lineoffset = 0;
  496. typeinfo[i]->numpolygons = 0;
  497. typeinfo[i]->polygonoffset = 0;
  498. typeinfo[i]->generatedata = 1;
  499. }
  500. /*Initiate the info structure */
  501. info = (VTKInfo *) calloc(infonum, sizeof(VTKInfo));
  502. info->maxnumpoints = 0;
  503. info->maxnumvertices = 0;
  504. info->maxnumlines = 0;
  505. info->maxnumlinepoints = 0;
  506. info->maxnumpolygons = 0;
  507. info->maxnumpolygonpoints = 0;
  508. info->typeinfo = typeinfo;
  509. /*1. write the points */
  510. write_vtk_points(ascii, Map, info, types, typenum, dp, scale);
  511. /*2. write the cells */
  512. write_vtk_cells(ascii, Map, info, types, typenum);
  513. /*3. write the cat data */
  514. write_vtk_cat_data(ascii, Map, info, layer, types, typenum, dp);
  515. /*4. write the DB data */
  516. /* not yet implemented
  517. write_vtk_db_data(ascii, Map, info, layer, types, typenum, dp);
  518. */
  519. /*Release the memory */
  520. for (i = 0; i < infonum; i++) {
  521. free(typeinfo[i]);
  522. }
  523. free(typeinfo);
  524. free(info);
  525. return 1;
  526. }
  527. /* ************************************************************************* */
  528. /* This function writes the point coordinates ****************************** */
  529. /* ************************************************************************* */
  530. void write_point_coordinates(struct line_pnts *Points, int dp, double scale,
  531. FILE * ascii)
  532. {
  533. char *xstring = NULL, *ystring = NULL, *zstring = NULL;
  534. double *xptr, *yptr, *zptr;
  535. xptr = Points->x;
  536. yptr = Points->y;
  537. zptr = Points->z;
  538. while (Points->n_points--) {
  539. G_asprintf(&xstring, "%.*f", dp, *xptr++ - x_extent);
  540. G_trim_decimal(xstring);
  541. G_asprintf(&ystring, "%.*f", dp, *yptr++ - y_extent);
  542. G_trim_decimal(ystring);
  543. G_asprintf(&zstring, "%.*f", dp, scale * (*zptr++));
  544. G_trim_decimal(zstring);
  545. fprintf(ascii, "%s %s %s \n", xstring, ystring, zstring);
  546. }
  547. return;
  548. }