driver.cpp 12 KB

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