driver.cpp 12 KB

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