driver.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. \author (C) by the GRASS Development Team
  10. Martin Landa <landa.martin gmail.com>
  11. \date 2007-2008
  12. */
  13. #include "driver.h"
  14. /**
  15. \brief Initialize driver
  16. Allocate given structures.
  17. \param[in,out] PseudoDC device where to draw vector objects
  18. \return
  19. */
  20. DisplayDriver::DisplayDriver(void *device)
  21. {
  22. G_gisinit(""); /* GRASS functions */
  23. mapInfo = NULL;
  24. dc = (wxPseudoDC *) device;
  25. points = Vect_new_line_struct();
  26. pointsScreen = new wxList();
  27. cats = Vect_new_cats_struct();
  28. selected = Vect_new_list();
  29. selectedDupl = Vect_new_list();
  30. drawSegments = false;
  31. // avoid GUI crash when G_fatal_error() is called (opening the vector map)
  32. // Vect_set_fatal_error(GV_FATAL_PRINT);
  33. // G_set_error_routine(print_error);
  34. }
  35. /**
  36. \brief Destroy driver
  37. Close the map, deallocate given structures.
  38. \param
  39. \return
  40. */
  41. DisplayDriver::~DisplayDriver()
  42. {
  43. if (mapInfo)
  44. CloseMap();
  45. Vect_destroy_line_struct(points);
  46. delete pointsScreen;
  47. Vect_destroy_cats_struct(cats);
  48. Vect_destroy_list(selected);
  49. Vect_destroy_list(selectedDupl);
  50. }
  51. /**
  52. \brief Set device for drawing
  53. \param[in,out] PseudoDC device where to draw vector objects
  54. \return
  55. */
  56. void DisplayDriver::SetDevice(void *device)
  57. {
  58. dc = (wxPseudoDC *) device;
  59. return;
  60. }
  61. /**
  62. \brief Draw content of the vector map to device
  63. \return number of lines which were drawn
  64. \return -1 on error
  65. */
  66. int DisplayDriver::DrawMap(bool force)
  67. {
  68. if (!mapInfo || !dc)
  69. return -1;
  70. int nlines;
  71. BOUND_BOX mapBox;
  72. struct ilist *listLines;
  73. // ids.clear();
  74. listLines = Vect_new_list();
  75. ResetTopology();
  76. /* nlines = Vect_get_num_lines(mapInfo); */
  77. Vect_get_map_box(mapInfo, &mapBox);
  78. // draw lines inside of current display region
  79. nlines = Vect_select_lines_by_box(mapInfo, &(region.box),
  80. GV_POINTS | GV_LINES, // fixme
  81. listLines);
  82. G_debug(3, "wxDriver.DrawMap(): region: w=%f, e=%f, s=%f, n=%f",
  83. region.box.W, region.box.E, region.box.S, region.box.N);
  84. dc->BeginDrawing();
  85. for (int i = 0; i < listLines->n_values; i++) {
  86. DrawLine(listLines->value[i]);
  87. }
  88. dc->EndDrawing();
  89. // PrintIds();
  90. Vect_destroy_list(listLines);
  91. return listLines->n_values;
  92. }
  93. /**
  94. \brief Draw selected vector objects to the device
  95. \param[in] line id
  96. \return 1 on success
  97. \return -1 on failure (vector object is dead, etc.)
  98. */
  99. int DisplayDriver::DrawLine(int line)
  100. {
  101. if (!dc || !Vect_line_alive (mapInfo, line))
  102. return -1;
  103. int dcId; // 0 | 1 | segment id
  104. int type; // line type
  105. double x, y, z; // screen coordinates
  106. bool draw; // draw object ?
  107. wxPen *pen;
  108. pen = NULL;
  109. draw = false;
  110. // read line
  111. type = Vect_read_line (mapInfo, points, cats, line);
  112. // add ids
  113. // -> node1, line1, vertex1, line2, ..., node2
  114. // struct lineDesc desc = {points->n_points, dcId};
  115. // ids[line] = desc;
  116. // update id for next line
  117. // dcId += points->n_points * 2 - 1;
  118. if (IsSelected(line)) { // line selected ?
  119. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  120. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  121. }
  122. else {
  123. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  124. }
  125. draw = true;
  126. dcId = 1;
  127. topology.highlight++;
  128. }
  129. else {
  130. dcId = 0;
  131. if (type & GV_LINES) {
  132. switch (type) {
  133. case GV_LINE:
  134. pen = new wxPen(settings.line.color, settings.lineWidth, wxSOLID);
  135. topology.line++;
  136. draw = settings.line.enabled;
  137. break;
  138. case GV_BOUNDARY:
  139. int left, right;
  140. Vect_get_line_areas(mapInfo, line,
  141. &left, &right);
  142. if (left == 0 && right == 0) {
  143. pen = new wxPen(settings.boundaryNo.color, settings.lineWidth, wxSOLID);
  144. topology.boundaryNo++;
  145. draw = settings.boundaryNo.enabled;
  146. }
  147. else if (left > 0 && right > 0) {
  148. pen = new wxPen(settings.boundaryTwo.color, settings.lineWidth, wxSOLID);
  149. topology.boundaryTwo++;
  150. draw = settings.boundaryTwo.enabled;
  151. }
  152. else {
  153. pen = new wxPen(settings.boundaryOne.color, settings.lineWidth, wxSOLID);
  154. topology.boundaryOne++;
  155. draw = settings.boundaryOne.enabled;
  156. }
  157. break;
  158. default:
  159. draw = false;
  160. break;
  161. }
  162. }
  163. else if (type & GV_POINTS) {
  164. if (type == GV_POINT && settings.point.enabled) {
  165. pen = new wxPen(settings.point.color, settings.lineWidth, wxSOLID);
  166. topology.point++;
  167. draw = settings.point.enabled;
  168. }
  169. else if (type == GV_CENTROID) {
  170. int cret = Vect_get_centroid_area(mapInfo, line);
  171. if (cret > 0) { // -> area
  172. draw = settings.centroidIn.enabled;
  173. pen = new wxPen(settings.centroidIn.color, settings.lineWidth, wxSOLID);
  174. topology.centroidIn++;
  175. }
  176. else if (cret == 0) {
  177. draw = settings.centroidOut.enabled;
  178. pen = new wxPen(settings.centroidOut.color, settings.lineWidth, wxSOLID);
  179. topology.centroidOut++;
  180. }
  181. else {
  182. draw = settings.centroidDup.enabled;
  183. pen = new wxPen(settings.centroidDup.color, settings.lineWidth, wxSOLID);
  184. topology.centroidDup++;
  185. }
  186. }
  187. }
  188. }
  189. // clear screen points & convert EN -> xy
  190. pointsScreen->Clear();
  191. for (int i = 0; i < points->n_points; i++) {
  192. Cell2Pixel(points->x[i], points->y[i], points->z[i],
  193. &x, &y, &z);
  194. pointsScreen->Append((wxObject*) new wxPoint((int) x, (int) y)); /* TODO: 3D */
  195. }
  196. dc->SetId(dcId); /* 0 | 1 (selected) */
  197. dc->SetPen(*pen);
  198. if (draw) {
  199. if (type & GV_POINTS) {
  200. DrawCross(line, (const wxPoint *) pointsScreen->GetFirst()->GetData());
  201. }
  202. else {
  203. // long int startId = ids[line].startId + 1;
  204. if (dcId > 0 && drawSegments) {
  205. dcId = 2; // first segment
  206. for (size_t i = 0; i < pointsScreen->GetCount() - 1; dcId += 2) {
  207. wxPoint *point_beg = (wxPoint *) pointsScreen->Item(i)->GetData();
  208. wxPoint *point_end = (wxPoint *) pointsScreen->Item(++i)->GetData();
  209. // set bounds for line
  210. // wxRect rect (*point_beg, *point_end);
  211. // dc->SetIdBounds(startId, rect);
  212. dc->SetId(dcId); // set unique id & set bbox for each segment
  213. dc->SetPen(*pen);
  214. wxRect rect (*point_beg, *point_end);
  215. dc->SetIdBounds(dcId, rect);
  216. dc->DrawLine(point_beg->x, point_beg->y,
  217. point_end->x, point_end->y);
  218. }
  219. }
  220. else {
  221. wxPoint points[pointsScreen->GetCount()];
  222. for (size_t i = 0; i < pointsScreen->GetCount(); i++) {
  223. wxPoint *point_beg = (wxPoint *) pointsScreen->Item(i)->GetData();
  224. points[i] = *point_beg;
  225. }
  226. dc->DrawLines(pointsScreen->GetCount(), points);
  227. }
  228. }
  229. }
  230. if (type & GV_LINES) {
  231. DrawLineVerteces(line); // draw vertices
  232. DrawLineNodes(line); // draw nodes
  233. }
  234. delete pen;
  235. return 1;
  236. }
  237. /**
  238. \brief Draw line verteces to the device
  239. Except of first and last vertex, see DrawLineNodes().
  240. \param line id
  241. \return number of verteces which were drawn
  242. \return -1 if drawing vertices is disabled
  243. */
  244. int DisplayDriver::DrawLineVerteces(int line)
  245. {
  246. int dcId;
  247. wxPoint *point;
  248. wxPen *pen;
  249. if (!IsSelected(line) && !settings.vertex.enabled)
  250. return -1;
  251. // determine color
  252. if (!IsSelected(line)) {
  253. pen = new wxPen(settings.vertex.color, settings.lineWidth, wxSOLID);
  254. dcId = 0;
  255. }
  256. else {
  257. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  258. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  259. }
  260. else {
  261. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  262. }
  263. if (drawSegments) {
  264. dcId = 3; // first vertex
  265. }
  266. else {
  267. dcId = 1;
  268. }
  269. }
  270. dc->SetId(dcId); /* 0 | 1 (selected) */
  271. dc->SetPen(*pen);
  272. for (size_t i = 1; i < pointsScreen->GetCount() - 1; i++, dcId += 2) {
  273. point = (wxPoint*) pointsScreen->Item(i)->GetData();
  274. if (IsSelected(line) && drawSegments) {
  275. dc->SetId(dcId);
  276. dc->SetPen(*pen);
  277. wxRect rect (*point, *point);
  278. dc->SetIdBounds(dcId, rect);
  279. }
  280. if (settings.vertex.enabled) {
  281. DrawCross(line, (const wxPoint*) pointsScreen->Item(i)->GetData());
  282. topology.vertex++;
  283. }
  284. }
  285. delete pen;
  286. return pointsScreen->GetCount() - 2;
  287. }
  288. /**
  289. \brief Draw line nodes to the device
  290. \param line id
  291. \return 1
  292. \return -1 if no nodes were drawn
  293. */
  294. int DisplayDriver::DrawLineNodes(int line)
  295. {
  296. int dcId;
  297. int node;
  298. double east, north, depth;
  299. double x, y, z;
  300. int nodes [2];
  301. bool draw;
  302. wxPen *pen;
  303. // draw nodes??
  304. if (!settings.nodeOne.enabled && !settings.nodeTwo.enabled)
  305. return -1;
  306. // get nodes
  307. Vect_get_line_nodes(mapInfo, line, &(nodes[0]), &(nodes[1]));
  308. for (size_t i = 0; i < sizeof(nodes) / sizeof(int); i++) {
  309. node = nodes[i];
  310. // get coordinates
  311. Vect_get_node_coor(mapInfo, node,
  312. &east, &north, &depth);
  313. // convert EN->xy
  314. Cell2Pixel(east, north, depth,
  315. &x, &y, &z);
  316. // determine color
  317. if (IsSelected(line)) {
  318. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  319. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  320. }
  321. else {
  322. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  323. }
  324. draw = true;
  325. if (!drawSegments) {
  326. dcId = 1;
  327. }
  328. else {
  329. // node1, line1, vertex1, line2, vertex2, ..., node2
  330. if (i == 0) // first node
  331. dcId = 1;
  332. else // last node
  333. dcId = 2 * points->n_points - 1;
  334. }
  335. }
  336. else {
  337. dcId = 0;
  338. if (Vect_get_node_n_lines(mapInfo, node) == 1) {
  339. pen = new wxPen(settings.nodeOne.color, settings.lineWidth, wxSOLID);
  340. topology.nodeOne++;
  341. draw = settings.nodeOne.enabled;
  342. }
  343. else {
  344. pen = new wxPen(settings.nodeTwo.color, settings.lineWidth, wxSOLID);
  345. topology.nodeTwo++;
  346. draw = settings.nodeTwo.enabled;
  347. }
  348. }
  349. wxPoint point((int) x, (int) y);
  350. if (IsSelected(line) && drawSegments) {
  351. wxRect rect (point, point);
  352. dc->SetIdBounds(dcId, rect);
  353. }
  354. // draw node if needed
  355. if (draw) {
  356. dc->SetId(dcId);
  357. dc->SetPen(*pen);
  358. DrawCross(line, &point);
  359. }
  360. }
  361. delete pen;
  362. return 1;
  363. }
  364. /*
  365. \brief Close vector map layer
  366. \param void
  367. \return 0 on success
  368. \return non-zero on error
  369. */
  370. int DisplayDriver::CloseMap()
  371. {
  372. int ret;
  373. ret = -1;
  374. if (mapInfo) {
  375. if (mapInfo->mode == GV_MODE_RW) {
  376. /* rebuild topology */
  377. Vect_build_partial(mapInfo, GV_BUILD_NONE, NULL);
  378. Vect_build(mapInfo, NULL);
  379. }
  380. /* close map and store topo/cidx */
  381. ret = Vect_close(mapInfo);
  382. G_free ((void *) mapInfo);
  383. mapInfo = NULL;
  384. }
  385. return ret;
  386. }
  387. /**
  388. \brief Open vector map layer
  389. \param[in] mapname name of vector map
  390. \param[in] mapset name of mapset where the vector map layer is stored
  391. \return topo level
  392. \return -1 on error
  393. */
  394. int DisplayDriver::OpenMap(const char* mapname, const char *mapset, bool update)
  395. {
  396. int ret;
  397. if (!mapInfo)
  398. mapInfo = (struct Map_info *) G_malloc (sizeof (struct Map_info));
  399. // define open level (level 2: topology)
  400. Vect_set_open_level(2);
  401. // avoid GUI crash when G_fatal_error() is called (opening the vector map)
  402. Vect_set_fatal_error(GV_FATAL_PRINT);
  403. // open existing map
  404. if (!update) {
  405. ret = Vect_open_old(mapInfo, (char*) mapname, (char *) mapset);
  406. }
  407. else {
  408. ret = Vect_open_update(mapInfo, (char*) mapname, (char *) mapset);
  409. }
  410. if (ret == -1) { // error
  411. G_free((void *) mapInfo);
  412. mapInfo = NULL;
  413. }
  414. return ret;
  415. }
  416. /**
  417. \brief Reload vector map layer
  418. Close and open again. Needed for modification using v.edit.
  419. TODO: Get rid of that...
  420. \param
  421. \return
  422. */
  423. void DisplayDriver::ReloadMap()
  424. {
  425. // char* name = G_store(Vect_get_map_name(mapInfo)); ???
  426. char* name = G_store(mapInfo->name);
  427. char* mapset = G_store(Vect_get_mapset(mapInfo));
  428. Vect_close(mapInfo);
  429. mapInfo = NULL;
  430. OpenMap(name, mapset, false); // used only for v.edit
  431. //Vect_build_partial(mapInfo, GV_BUILD_NONE, stderr);
  432. //Vect_build(mapInfo, stderr);
  433. return;
  434. }
  435. /*
  436. \brief Conversion from geographic coordinates (east, north)
  437. to screen (x, y)
  438. TODO: 3D stuff...
  439. \param[in] east,north,depth geographical coordinates
  440. \param[out] x, y, z screen coordinates
  441. \return
  442. */
  443. void DisplayDriver::Cell2Pixel(double east, double north, double depth,
  444. double *x, double *y, double *z)
  445. {
  446. double n, w;
  447. /*
  448. *x = int((east - region.map_west) / region.map_res);
  449. *y = int((region.map_north - north) / region.map_res);
  450. */
  451. w = region.center_easting - (region.map_width / 2) * region.map_res;
  452. n = region.center_northing + (region.map_height / 2) * region.map_res;
  453. /*
  454. *x = int((east - w) / region.map_res);
  455. *y = int((n - north) / region.map_res);
  456. */
  457. *x = (east - w) / region.map_res;
  458. *y = (n - north) / region.map_res;
  459. *z = 0;
  460. return;
  461. }
  462. /**
  463. \brief Set geographical region
  464. Region must be upgraded because of Cell2Pixel().
  465. \param[in] north,south,east,west,ns_res,ew_res region settings
  466. \return
  467. */
  468. void DisplayDriver::SetRegion(double north, double south, double east, double west,
  469. double ns_res, double ew_res,
  470. double center_easting, double center_northing,
  471. double map_width, double map_height)
  472. {
  473. region.box.N = north;
  474. region.box.S = south;
  475. region.box.E = east;
  476. region.box.W = west;
  477. region.box.T = PORT_DOUBLE_MAX;
  478. region.box.B = -PORT_DOUBLE_MAX;
  479. region.ns_res = ns_res;
  480. region.ew_res = ew_res;
  481. region.center_easting = center_easting;
  482. region.center_northing = center_northing;
  483. region.map_width = map_width;
  484. region.map_height = map_height;
  485. // calculate real region
  486. region.map_res = (region.ew_res > region.ns_res) ? region.ew_res : region.ns_res;
  487. region.map_west = region.center_easting - (region.map_width / 2.) * region.map_res;
  488. region.map_north = region.center_northing + (region.map_height / 2.) * region.map_res;
  489. return;
  490. }
  491. /**
  492. \brief Draw cross symbol of given size to device content
  493. Used for points, nodes, vertices
  494. \param[in] point coordinates of center
  495. \param[in] size size of the cross symbol
  496. \return 1 on success
  497. \return -1 on failure
  498. */
  499. int DisplayDriver::DrawCross(int line, const wxPoint* point, int size)
  500. {
  501. if (!dc || !point)
  502. return -1;
  503. dc->DrawLine(point->x - size, point->y, point->x + size, point->y);
  504. dc->DrawLine(point->x, point->y - size, point->x, point->y + size);
  505. return 1;
  506. }
  507. /*
  508. \brief Set settings for displaying vector feature
  509. E.g. line width, color, ...
  510. \param[in] lineWidth,... settgings
  511. \return
  512. */
  513. void DisplayDriver::UpdateSettings(unsigned long highlight,
  514. bool ehighlightDupl, unsigned long chighlightDupl,
  515. bool ePoint, unsigned long cPoint, /* enabled, color */
  516. bool eLine, unsigned long cLine,
  517. bool eBoundaryNo, unsigned long cBoundaryNo,
  518. bool eBoundaryOne, unsigned long cBoundaryOne,
  519. bool eBoundaryTwo, unsigned long cBoundaryTwo,
  520. bool eCentroidIn, unsigned long cCentroidIn,
  521. bool eCentroidOut, unsigned long cCentroidOut,
  522. bool eCentroidDup, unsigned long cCentroidDup,
  523. bool eNodeOne, unsigned long cNodeOne,
  524. bool eNodeTwo, unsigned long cNodeTwo,
  525. bool eVertex, unsigned long cVertex,
  526. int lineWidth)
  527. {
  528. settings.highlight.Set(highlight);
  529. settings.highlightDupl.enabled = ehighlightDupl;
  530. settings.highlightDupl.color.Set(chighlightDupl);
  531. settings.point.enabled = ePoint;
  532. settings.point.color.Set(cPoint);
  533. settings.line.enabled = eLine;
  534. settings.line.color.Set(cLine);
  535. settings.boundaryNo.enabled = eBoundaryNo;
  536. settings.boundaryNo.color.Set(cBoundaryNo);
  537. settings.boundaryOne.enabled = eBoundaryOne;
  538. settings.boundaryOne.color.Set(cBoundaryOne);
  539. settings.boundaryTwo.enabled = eBoundaryTwo;
  540. settings.boundaryTwo.color.Set(cBoundaryTwo);
  541. settings.centroidIn.enabled = eCentroidIn;
  542. settings.centroidIn.color.Set(cCentroidIn);
  543. settings.centroidOut.enabled = eCentroidOut;
  544. settings.centroidOut.color.Set(cCentroidOut);
  545. settings.centroidDup.enabled = eCentroidDup;
  546. settings.centroidDup.color.Set(cCentroidDup);
  547. settings.nodeOne.enabled = eNodeOne;
  548. settings.nodeOne.color.Set(cNodeOne);
  549. settings.nodeTwo.enabled = eNodeTwo;
  550. settings.nodeTwo.color.Set(cNodeTwo);
  551. settings.vertex.enabled = eVertex;
  552. settings.vertex.color.Set(cVertex);
  553. settings.lineWidth = lineWidth;
  554. }
  555. /**
  556. \brief Prints gId: dcIds
  557. Useful for debugging purposes.
  558. \param
  559. \return
  560. */
  561. void DisplayDriver::PrintIds()
  562. {
  563. std::cerr << "topology.highlight: " << topology.highlight << std::endl;
  564. std::cerr << "topology.point: " << topology.point << std::endl;
  565. std::cerr << "topology.line: " << topology.line << std::endl;
  566. std::cerr << "topology.boundaryNo: " << topology.boundaryNo << std::endl;
  567. std::cerr << "topology.boundaryOne: " << topology.boundaryOne << std::endl;
  568. std::cerr << "topology.boundaryTwo: " << topology.boundaryTwo << std::endl;
  569. std::cerr << "topology.centroidIn: " << topology.centroidIn << std::endl;
  570. std::cerr << "topology.centroidOut: " << topology.centroidOut << std::endl;
  571. std::cerr << "topology.centroidDup: " << topology.centroidDup << std::endl;
  572. std::cerr << "topology.nodeOne: " << topology.nodeOne << std::endl;
  573. std::cerr << "topology.nodeTwo: " << topology.nodeTwo << std::endl;
  574. std::cerr << "topology.vertex: " << topology.vertex << std::endl;
  575. std::cerr << std::endl << "nobjects: "
  576. << topology.point * 2 + // cross
  577. topology.line +
  578. topology.boundaryNo +
  579. topology.boundaryOne +
  580. topology.boundaryTwo +
  581. topology.centroidIn * 2 +
  582. topology.centroidOut * 2 +
  583. topology.centroidDup * 2 +
  584. topology.nodeOne * 2 +
  585. topology.nodeTwo * 2 +
  586. topology.vertex * 2 << std::endl;
  587. std::cerr << "selected: ";
  588. for (int i = 0; i < selected->n_values; i++) {
  589. std::cerr << selected->value[i] << " ";
  590. }
  591. std::cerr << std::endl;
  592. return;
  593. }
  594. /**
  595. \brief Select vector objects by given bounding box
  596. If line id is already in the list of selected lines, then it will
  597. be excluded from this list.
  598. \param[in] x1,y1,z1,x2,y2,z3 bounding box definition
  599. \param[in] type feature type
  600. \return number of selected features
  601. \return -1 on error
  602. */
  603. int DisplayDriver::SelectLinesByBox(double x1, double y1, double z1,
  604. double x2, double y2, double z2,
  605. int type)
  606. {
  607. if (!mapInfo)
  608. return -1;
  609. int line;
  610. struct ilist *list;
  611. struct line_pnts *bbox;
  612. drawSegments = false;
  613. list = Vect_new_list();
  614. bbox = Vect_new_line_struct();
  615. Vect_append_point(bbox, x1, y1, z1);
  616. Vect_append_point(bbox, x2, y1, z2);
  617. Vect_append_point(bbox, x2, y2, z1);
  618. Vect_append_point(bbox, x1, y2, z2);
  619. Vect_append_point(bbox, x1, y1, z1);
  620. Vect_select_lines_by_polygon(mapInfo, bbox,
  621. 0, NULL, /* isles */
  622. type, list);
  623. for (int i = 0; i < list->n_values; i++) {
  624. line = list->value[i];
  625. if (!IsSelected(line)) {
  626. Vect_list_append(selected, line);
  627. }
  628. else {
  629. Vect_list_delete(selected, line);
  630. }
  631. }
  632. Vect_destroy_line_struct(bbox);
  633. Vect_destroy_list(list);
  634. return list->n_values;
  635. }
  636. /**
  637. \brief Select vector feature by given point in given
  638. threshold
  639. Only one vector object can be selected. Bounding boxes of
  640. all segments are stores.
  641. \param[in] x,y point of searching
  642. \param[in] thresh threshold value where to search
  643. \param[in] type select vector object of given type
  644. \return point on line if line found
  645. */
  646. std::vector<double> DisplayDriver::SelectLineByPoint(double x, double y, double z,
  647. double thresh, int type, int with_z)
  648. {
  649. long int line;
  650. double px, py, pz;
  651. std::vector<double> p;
  652. line = Vect_find_line(mapInfo, x, y, z,
  653. type, thresh, with_z, 0);
  654. if (line > 0) {
  655. if (!IsSelected(line)) {
  656. Vect_list_append(selected, line);
  657. }
  658. else {
  659. Vect_list_delete(selected, line);
  660. }
  661. type = Vect_read_line (mapInfo, points, cats, line);
  662. Vect_line_distance (points, x, y, z, with_z,
  663. &px, &py, &pz,
  664. NULL, NULL, NULL);
  665. p.push_back(px);
  666. p.push_back(py);
  667. if (with_z) {
  668. p.push_back(pz);
  669. }
  670. }
  671. drawSegments = true;
  672. return p;
  673. }
  674. /**
  675. \brief Is vector object selected?
  676. \param[in] line id
  677. \return true if vector object is selected
  678. \return false if vector object is not selected
  679. */
  680. bool DisplayDriver::IsSelected(int line)
  681. {
  682. if (Vect_val_in_list(selected, line))
  683. return true;
  684. return false;
  685. }
  686. /**
  687. \brief Get ids of selected objects
  688. \param[in] grassId if true return GRASS line ids
  689. if false return PseudoDC ids
  690. \return list of ids of selected vector objects
  691. */
  692. std::vector<int> DisplayDriver::GetSelected(bool grassId)
  693. {
  694. if (grassId)
  695. return ListToVector(selected);
  696. std::vector<int> dc_ids;
  697. if (!drawSegments) {
  698. dc_ids.push_back(1);
  699. }
  700. else {
  701. int npoints;
  702. Vect_read_line(mapInfo, points, NULL, selected->value[0]);
  703. npoints = points->n_points;
  704. for (int i = 1; i < 2 * npoints; i++) {
  705. dc_ids.push_back(i);
  706. }
  707. }
  708. return dc_ids;
  709. }
  710. /**
  711. \brief Get feature (grass) ids of duplicated objects
  712. \return list of ids
  713. */
  714. std::map<int, std::vector <int> > DisplayDriver::GetDuplicates()
  715. {
  716. std::map<int, std::vector<int> > ids;
  717. struct line_pnts *APoints, *BPoints;
  718. int line;
  719. APoints = Vect_new_line_struct();
  720. BPoints = Vect_new_line_struct();
  721. Vect_reset_list(selectedDupl);
  722. for (int i = 0; i < selected->n_values; i++) {
  723. line = selected->value[i];
  724. if (IsDuplicated(line))
  725. continue;
  726. Vect_read_line(mapInfo, APoints, NULL, line);
  727. for (int j = 0; j < selected->n_values; j++) {
  728. if (i == j || IsDuplicated(selected->value[j]))
  729. continue;
  730. Vect_read_line(mapInfo, BPoints, NULL, selected->value[j]);
  731. if (Vect_line_check_duplicate (APoints, BPoints, WITHOUT_Z)) {
  732. if (ids.find(i) == ids.end()) {
  733. ids[i] = std::vector<int> ();
  734. ids[i].push_back(selected->value[i]);
  735. Vect_list_append(selectedDupl, selected->value[i]);
  736. }
  737. ids[i].push_back(selected->value[j]);
  738. Vect_list_append(selectedDupl, selected->value[j]);
  739. }
  740. }
  741. }
  742. Vect_destroy_line_struct(APoints);
  743. Vect_destroy_line_struct(BPoints);
  744. return ids;
  745. }
  746. /**
  747. \brief Check for already marked duplicates
  748. \param line line id
  749. \return 1 line already marked as duplicated
  750. \return 0 not duplicated
  751. */
  752. bool DisplayDriver::IsDuplicated(int line)
  753. {
  754. if (Vect_val_in_list(selectedDupl, line))
  755. return true;
  756. return false;
  757. }
  758. /**
  759. \brief Set selected vector objects
  760. \param[in] list of GRASS ids to be set
  761. \return 1
  762. */
  763. int DisplayDriver::SetSelected(std::vector<int> id)
  764. {
  765. VectorToList(selected, id);
  766. if (selected->n_values <= 0)
  767. drawSegments = false;
  768. return 1;
  769. }
  770. /**
  771. \brief Unselect selected vector features
  772. \param[in] list of GRASS feature ids
  773. \return number of selected features
  774. */
  775. int DisplayDriver::UnSelect(std::vector<int> id)
  776. {
  777. bool checkForDupl;
  778. checkForDupl = false;
  779. for (std::vector<int>::const_iterator i = id.begin(), e = id.end();
  780. i != e; ++i) {
  781. if (IsSelected(*i)) {
  782. Vect_list_delete(selected, *i);
  783. }
  784. if (settings.highlightDupl.enabled && IsDuplicated(*i)) {
  785. checkForDupl = true;
  786. }
  787. }
  788. if (checkForDupl) {
  789. GetDuplicates();
  790. }
  791. return selected->n_values;
  792. }
  793. /**
  794. \brief Get PseudoDC vertex id of selected line
  795. Set bounding box for vertices of line.
  796. \param[in] x,y coordinates of click
  797. \param[in] thresh threshold value
  798. \return id of center, left and right vertex
  799. \return 0 no line found
  800. \return -1 on error
  801. */
  802. std::vector<int> DisplayDriver::GetSelectedVertex(double x, double y, double thresh)
  803. {
  804. int startId;
  805. int line, type;
  806. int Gid, DCid;
  807. double vx, vy, vz; // vertex screen coordinates
  808. double dist, minDist;
  809. std::vector<int> returnId;
  810. // only one object can be selected
  811. if (selected->n_values != 1 || !drawSegments)
  812. return returnId;
  813. startId = 1;
  814. line = selected->value[0];
  815. type = Vect_read_line (mapInfo, points, cats, line);
  816. minDist = 0.0;
  817. Gid = -1;
  818. // find the closest vertex (x, y)
  819. DCid = 1;
  820. for(int idx = 0; idx < points->n_points; idx++) {
  821. dist = Vect_points_distance(x, y, 0.0,
  822. points->x[idx], points->y[idx], points->z[idx], 0);
  823. if (idx == 0) {
  824. minDist = dist;
  825. Gid = idx;
  826. }
  827. else {
  828. if (minDist > dist) {
  829. minDist = dist;
  830. Gid = idx;
  831. }
  832. }
  833. Cell2Pixel(points->x[idx], points->y[idx], points->z[idx],
  834. &vx, &vy, &vz);
  835. wxRect rect (wxPoint ((int) vx, (int) vy), wxPoint ((int) vx, (int) vy));
  836. dc->SetIdBounds(DCid, rect);
  837. DCid+=2;
  838. }
  839. if (minDist > thresh)
  840. return returnId;
  841. // desc = &(ids[line]);
  842. // translate id
  843. DCid = Gid * 2 + 1;
  844. // add selected vertex
  845. returnId.push_back(DCid);
  846. // left vertex
  847. if (DCid == startId) {
  848. returnId.push_back(-1);
  849. }
  850. else {
  851. returnId.push_back(DCid - 2);
  852. }
  853. // right vertex
  854. if (DCid == (points->n_points - 1) * 2 + startId) {
  855. returnId.push_back(-1);
  856. }
  857. else {
  858. returnId.push_back(DCid + 2);
  859. }
  860. return returnId;
  861. }
  862. /**
  863. \brief Reset topology structure.
  864. \return
  865. */
  866. void DisplayDriver::ResetTopology()
  867. {
  868. topology.highlight = 0;
  869. topology.point = 0;
  870. topology.line = 0;
  871. topology.boundaryNo = 0;
  872. topology.boundaryOne = 0;
  873. topology.boundaryTwo = 0;
  874. topology.centroidIn = 0;
  875. topology.centroidOut = 0;
  876. topology.centroidDup = 0;
  877. topology.nodeOne = 0;
  878. topology.nodeTwo = 0;
  879. topology.vertex = 0;
  880. return;
  881. }
  882. /**
  883. \brief Convert vect list to std::vector
  884. \param list vect list
  885. \return std::vector
  886. */
  887. std::vector<int> DisplayDriver::ListToVector(struct ilist *list)
  888. {
  889. std::vector<int> vect;
  890. if (!list)
  891. return vect;
  892. for (int i = 0; i < list->n_values; i++) {
  893. vect.push_back(list->value[i]);
  894. }
  895. return vect;
  896. }
  897. /**
  898. \brief Convert std::vector to vect list
  899. \param list vect list
  900. \param vec std::vector instance
  901. \return number of items
  902. \return -1 on error
  903. */
  904. int DisplayDriver::VectorToList(struct ilist *list, const std::vector<int>& vec)
  905. {
  906. if (!list)
  907. return -1;
  908. Vect_reset_list(list);
  909. for (std::vector<int>::const_iterator i = vec.begin(), e = vec.end();
  910. i != e; ++i) {
  911. Vect_list_append(list, *i);
  912. }
  913. return list->n_values;
  914. }
  915. /**
  916. \brief Get bounding box of (opened) vector map layer
  917. \return (w,s,b,e,n,t)
  918. */
  919. std::vector<double> DisplayDriver::GetMapBoundingBox()
  920. {
  921. std::vector<double> region;
  922. BOUND_BOX bbox;
  923. if (!mapInfo) {
  924. return region;
  925. }
  926. Vect_get_map_box(mapInfo, &bbox);
  927. region.push_back(bbox.W);
  928. region.push_back(bbox.S);
  929. region.push_back(bbox.B);
  930. region.push_back(bbox.E);
  931. region.push_back(bbox.N);
  932. region.push_back(bbox.T);
  933. return region;
  934. }
  935. /**
  936. \brief Error messages handling
  937. \param msg message
  938. \param type type message (MSG, WARN, ERR)
  939. \return 0
  940. */
  941. int print_error(const char *msg, int type)
  942. {
  943. fprintf(stderr, "%s", msg);
  944. return 0;
  945. }