driver.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /**
  2. \file driver.cpp
  3. \brief Experimental C++ wxWidgets display driver
  4. This driver is designed for wxPython GRASS GUI (digitization tool).
  5. Draw vector map layer to PseudoDC.
  6. (C) by the GRASS Development Team
  7. This program is free software under the GNU General Public
  8. License (>=v2). Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Martin Landa <landa.martin gmail.com>
  11. \date 2007-2008
  12. */
  13. #include <cmath>
  14. #include "driver.h"
  15. /**
  16. \brief Initialize driver
  17. Allocate given structures.
  18. \param[in,out] PseudoDC device where to draw vector objects
  19. \return
  20. */
  21. DisplayDriver::DisplayDriver(void *device)
  22. {
  23. G_gisinit(""); /* GRASS functions */
  24. mapInfo = NULL;
  25. dc = (wxPseudoDC *) device;
  26. points = Vect_new_line_struct();
  27. pointsScreen = new wxList();
  28. cats = Vect_new_cats_struct();
  29. selected.values = Vect_new_list();
  30. selected.valuesDupl = Vect_new_list();
  31. drawSegments = false;
  32. // avoid GUI crash when G_fatal_error() is called (opening the vector map)
  33. // Vect_set_fatal_error(GV_FATAL_PRINT);
  34. // G_set_error_routine(print_error);
  35. }
  36. /**
  37. \brief Destroy driver
  38. Close the map, deallocate given structures.
  39. \param
  40. \return
  41. */
  42. DisplayDriver::~DisplayDriver()
  43. {
  44. if (mapInfo)
  45. CloseMap();
  46. Vect_destroy_line_struct(points);
  47. delete pointsScreen;
  48. Vect_destroy_cats_struct(cats);
  49. Vect_destroy_list(selected.values);
  50. Vect_destroy_list(selected.valuesDupl);
  51. }
  52. /**
  53. \brief Set device for drawing
  54. \param[in,out] PseudoDC device where to draw vector objects
  55. \return
  56. */
  57. void DisplayDriver::SetDevice(void *device)
  58. {
  59. dc = (wxPseudoDC *) device;
  60. return;
  61. }
  62. /*
  63. \brief Close vector map layer
  64. \param void
  65. \return 0 on success
  66. \return non-zero on error
  67. */
  68. int DisplayDriver::CloseMap()
  69. {
  70. int ret;
  71. ret = -1;
  72. if (mapInfo) {
  73. if (mapInfo->mode == GV_MODE_RW) {
  74. /* rebuild topology */
  75. Vect_build_partial(mapInfo, GV_BUILD_NONE, NULL);
  76. Vect_build(mapInfo, NULL);
  77. }
  78. /* close map and store topo/cidx */
  79. ret = Vect_close(mapInfo);
  80. G_free ((void *) mapInfo);
  81. mapInfo = NULL;
  82. }
  83. return ret;
  84. }
  85. /**
  86. \brief Open vector map layer
  87. \param[in] mapname name of vector map
  88. \param[in] mapset name of mapset where the vector map layer is stored
  89. \return topo level
  90. \return -1 on error
  91. */
  92. int DisplayDriver::OpenMap(const char* mapname, const char *mapset, bool update)
  93. {
  94. int ret;
  95. if (!mapInfo)
  96. mapInfo = (struct Map_info *) G_malloc (sizeof (struct Map_info));
  97. // define open level (level 2: topology)
  98. Vect_set_open_level(2);
  99. // avoid GUI crash when G_fatal_error() is called (opening the vector map)
  100. Vect_set_fatal_error(GV_FATAL_PRINT);
  101. // open existing map
  102. if (!update) {
  103. ret = Vect_open_old(mapInfo, (char*) mapname, (char *) mapset);
  104. }
  105. else {
  106. ret = Vect_open_update(mapInfo, (char*) mapname, (char *) mapset);
  107. }
  108. if (ret == -1) { // error
  109. G_free((void *) mapInfo);
  110. mapInfo = NULL;
  111. }
  112. return ret;
  113. }
  114. /**
  115. \brief Reload vector map layer
  116. Close and open again. Needed for modification using v.edit.
  117. TODO: Get rid of that...
  118. \param
  119. \return
  120. */
  121. void DisplayDriver::ReloadMap()
  122. {
  123. // char* name = G_store(Vect_get_map_name(mapInfo)); ???
  124. char* name = G_store(mapInfo->name);
  125. char* mapset = G_store(Vect_get_mapset(mapInfo));
  126. Vect_close(mapInfo);
  127. mapInfo = NULL;
  128. OpenMap(name, mapset, false); // used only for v.edit
  129. //Vect_build_partial(mapInfo, GV_BUILD_NONE, stderr);
  130. //Vect_build(mapInfo, stderr);
  131. return;
  132. }
  133. /*
  134. \brief Conversion from geographic coordinates (east, north)
  135. to screen (x, y)
  136. TODO: 3D stuff...
  137. \param[in] east,north,depth geographical coordinates
  138. \param[out] x, y, z screen coordinates
  139. \return
  140. */
  141. void DisplayDriver::Cell2Pixel(double east, double north, double depth,
  142. double *x, double *y, double *z)
  143. {
  144. double n, w;
  145. /*
  146. *x = int((east - region.map_west) / region.map_res);
  147. *y = int((region.map_north - north) / region.map_res);
  148. */
  149. w = region.center_easting - (region.map_width / 2) * region.map_res;
  150. n = region.center_northing + (region.map_height / 2) * region.map_res;
  151. /*
  152. *x = int((east - w) / region.map_res);
  153. *y = int((n - north) / region.map_res);
  154. */
  155. if (x)
  156. *x = (east - w) / region.map_res;
  157. if (y)
  158. *y = (n - north) / region.map_res;
  159. if (z)
  160. *z = 0.;
  161. return;
  162. }
  163. /**
  164. \brief Calculate distance in pixels
  165. \todo LL projection
  166. \param dist real distance
  167. */
  168. double DisplayDriver::DistanceInPixels(double dist)
  169. {
  170. double x;
  171. Cell2Pixel(region.map_west + dist, region.map_north, 0.0, &x, NULL, NULL);
  172. return std::sqrt(x * x);
  173. }
  174. /**
  175. \brief Set geographical region
  176. Region must be upgraded because of Cell2Pixel().
  177. \param[in] north,south,east,west,ns_res,ew_res region settings
  178. \return
  179. */
  180. void DisplayDriver::SetRegion(double north, double south, double east, double west,
  181. double ns_res, double ew_res,
  182. double center_easting, double center_northing,
  183. double map_width, double map_height)
  184. {
  185. region.box.N = north;
  186. region.box.S = south;
  187. region.box.E = east;
  188. region.box.W = west;
  189. region.box.T = PORT_DOUBLE_MAX;
  190. region.box.B = -PORT_DOUBLE_MAX;
  191. region.ns_res = ns_res;
  192. region.ew_res = ew_res;
  193. region.center_easting = center_easting;
  194. region.center_northing = center_northing;
  195. region.map_width = map_width;
  196. region.map_height = map_height;
  197. // calculate real region
  198. region.map_res = (region.ew_res > region.ns_res) ? region.ew_res : region.ns_res;
  199. region.map_west = region.center_easting - (region.map_width / 2.) * region.map_res;
  200. region.map_north = region.center_northing + (region.map_height / 2.) * region.map_res;
  201. return;
  202. }
  203. /*
  204. \brief Set settings for displaying vector feature
  205. E.g. line width, color, ...
  206. \param[in] lineWidth,... settgings
  207. \return
  208. */
  209. void DisplayDriver::UpdateSettings(unsigned long highlight,
  210. bool ehighlightDupl, unsigned long chighlightDupl,
  211. bool ePoint, unsigned long cPoint, /* enabled, color */
  212. bool eLine, unsigned long cLine,
  213. bool eBoundaryNo, unsigned long cBoundaryNo,
  214. bool eBoundaryOne, unsigned long cBoundaryOne,
  215. bool eBoundaryTwo, unsigned long cBoundaryTwo,
  216. bool eCentroidIn, unsigned long cCentroidIn,
  217. bool eCentroidOut, unsigned long cCentroidOut,
  218. bool eCentroidDup, unsigned long cCentroidDup,
  219. bool eNodeOne, unsigned long cNodeOne,
  220. bool eNodeTwo, unsigned long cNodeTwo,
  221. bool eVertex, unsigned long cVertex,
  222. bool eArea, unsigned long cArea,
  223. bool eDirection, unsigned long cDirection,
  224. int lineWidth)
  225. {
  226. settings.highlight.Set(highlight);
  227. settings.highlightDupl.enabled = ehighlightDupl;
  228. settings.highlightDupl.color.Set(chighlightDupl);
  229. settings.point.enabled = ePoint;
  230. settings.point.color.Set(cPoint);
  231. settings.line.enabled = eLine;
  232. settings.line.color.Set(cLine);
  233. settings.boundaryNo.enabled = eBoundaryNo;
  234. settings.boundaryNo.color.Set(cBoundaryNo);
  235. settings.boundaryOne.enabled = eBoundaryOne;
  236. settings.boundaryOne.color.Set(cBoundaryOne);
  237. settings.boundaryTwo.enabled = eBoundaryTwo;
  238. settings.boundaryTwo.color.Set(cBoundaryTwo);
  239. settings.centroidIn.enabled = eCentroidIn;
  240. settings.centroidIn.color.Set(cCentroidIn);
  241. settings.centroidOut.enabled = eCentroidOut;
  242. settings.centroidOut.color.Set(cCentroidOut);
  243. settings.centroidDup.enabled = eCentroidDup;
  244. settings.centroidDup.color.Set(cCentroidDup);
  245. settings.nodeOne.enabled = eNodeOne;
  246. settings.nodeOne.color.Set(cNodeOne);
  247. settings.nodeTwo.enabled = eNodeTwo;
  248. settings.nodeTwo.color.Set(cNodeTwo);
  249. settings.vertex.enabled = eVertex;
  250. settings.vertex.color.Set(cVertex);
  251. settings.area.enabled = eArea;
  252. settings.area.color.Set(cArea);
  253. settings.area.color.Set(settings.area.color.Red(),
  254. settings.area.color.Green(),
  255. settings.area.color.Blue(),
  256. 100); /* transparency */
  257. settings.direction.enabled = eDirection;
  258. settings.direction.color.Set(cDirection);
  259. settings.lineWidth = lineWidth;
  260. return;
  261. }
  262. /**
  263. \brief Prints gId: dcIds
  264. Useful for debugging purposes.
  265. \param
  266. \return
  267. */
  268. void DisplayDriver::PrintIds()
  269. {
  270. std::cerr << "topology.highlight: " << topology.highlight << std::endl;
  271. std::cerr << "topology.point: " << topology.point << std::endl;
  272. std::cerr << "topology.line: " << topology.line << std::endl;
  273. std::cerr << "topology.boundaryNo: " << topology.boundaryNo << std::endl;
  274. std::cerr << "topology.boundaryOne: " << topology.boundaryOne << std::endl;
  275. std::cerr << "topology.boundaryTwo: " << topology.boundaryTwo << std::endl;
  276. std::cerr << "topology.centroidIn: " << topology.centroidIn << std::endl;
  277. std::cerr << "topology.centroidOut: " << topology.centroidOut << std::endl;
  278. std::cerr << "topology.centroidDup: " << topology.centroidDup << std::endl;
  279. std::cerr << "topology.nodeOne: " << topology.nodeOne << std::endl;
  280. std::cerr << "topology.nodeTwo: " << topology.nodeTwo << std::endl;
  281. std::cerr << "topology.vertex: " << topology.vertex << std::endl;
  282. std::cerr << std::endl << "nobjects: "
  283. << topology.point * 2 + // cross
  284. topology.line +
  285. topology.boundaryNo +
  286. topology.boundaryOne +
  287. topology.boundaryTwo +
  288. topology.centroidIn * 2 +
  289. topology.centroidOut * 2 +
  290. topology.centroidDup * 2 +
  291. topology.nodeOne * 2 +
  292. topology.nodeTwo * 2 +
  293. topology.vertex * 2 << std::endl;
  294. std::cerr << "selected: ";
  295. for (int i = 0; i < selected.values->n_values; i++) {
  296. std::cerr << selected.values->value[i] << " ";
  297. }
  298. std::cerr << std::endl;
  299. return;
  300. }
  301. /**
  302. \brief Reset topology structure.
  303. \return
  304. */
  305. void DisplayDriver::ResetTopology()
  306. {
  307. topology.highlight = 0;
  308. topology.point = 0;
  309. topology.line = 0;
  310. topology.boundaryNo = 0;
  311. topology.boundaryOne = 0;
  312. topology.boundaryTwo = 0;
  313. topology.centroidIn = 0;
  314. topology.centroidOut = 0;
  315. topology.centroidDup = 0;
  316. topology.nodeOne = 0;
  317. topology.nodeTwo = 0;
  318. topology.vertex = 0;
  319. return;
  320. }
  321. /**
  322. \brief Convert vect list to std::vector
  323. \param list vect list
  324. \return std::vector
  325. */
  326. std::vector<int> DisplayDriver::ListToVector(struct ilist *list)
  327. {
  328. std::vector<int> vect;
  329. if (!list)
  330. return vect;
  331. for (int i = 0; i < list->n_values; i++) {
  332. vect.push_back(list->value[i]);
  333. }
  334. return vect;
  335. }
  336. /**
  337. \brief Convert std::vector to vect list
  338. \param list vect list
  339. \param vec std::vector instance
  340. \return number of items
  341. \return -1 on error
  342. */
  343. int DisplayDriver::VectorToList(struct ilist *list, const std::vector<int>& vec)
  344. {
  345. if (!list)
  346. return -1;
  347. Vect_reset_list(list);
  348. for (std::vector<int>::const_iterator i = vec.begin(), e = vec.end();
  349. i != e; ++i) {
  350. Vect_list_append(list, *i);
  351. }
  352. return list->n_values;
  353. }
  354. /**
  355. \brief Get bounding box of (opened) vector map layer
  356. \return (w,s,b,e,n,t)
  357. */
  358. std::vector<double> DisplayDriver::GetMapBoundingBox()
  359. {
  360. std::vector<double> region;
  361. BOUND_BOX bbox;
  362. if (!mapInfo) {
  363. return region;
  364. }
  365. Vect_get_map_box(mapInfo, &bbox);
  366. region.push_back(bbox.W);
  367. region.push_back(bbox.S);
  368. region.push_back(bbox.B);
  369. region.push_back(bbox.E);
  370. region.push_back(bbox.N);
  371. region.push_back(bbox.T);
  372. return region;
  373. }
  374. /**
  375. \brief Error messages handling
  376. \param msg message
  377. \param type type message (MSG, WARN, ERR)
  378. \return 0
  379. */
  380. int print_error(const char *msg, int type)
  381. {
  382. fprintf(stderr, "%s", msg);
  383. return 0;
  384. }