driver.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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 = Vect_new_list();
  30. selectedDupl = 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);
  50. Vect_destroy_list(selectedDupl);
  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 Draw content of the vector map to device
  64. \return number of lines which were drawn
  65. \return -1 on error
  66. */
  67. int DisplayDriver::DrawMap(bool force)
  68. {
  69. if (!mapInfo || !dc)
  70. return -1;
  71. int nlines;
  72. BOUND_BOX mapBox;
  73. struct ilist *listLines;
  74. // ids.clear();
  75. listLines = Vect_new_list();
  76. ResetTopology();
  77. /* nlines = Vect_get_num_lines(mapInfo); */
  78. Vect_get_map_box(mapInfo, &mapBox);
  79. // draw lines inside of current display region
  80. nlines = Vect_select_lines_by_box(mapInfo, &(region.box),
  81. GV_POINTS | GV_LINES, // fixme
  82. listLines);
  83. G_debug(3, "wxDriver.DrawMap(): region: w=%f, e=%f, s=%f, n=%f",
  84. region.box.W, region.box.E, region.box.S, region.box.N);
  85. dc->BeginDrawing();
  86. if (settings.area.enabled) {
  87. /* draw area fills first */
  88. int area;
  89. struct ilist *listAreas, *listCentroids;
  90. BOUND_BOX areaBox;
  91. listAreas = Vect_new_list();
  92. listCentroids = Vect_new_list();
  93. Vect_select_areas_by_box(mapInfo, &(region.box),
  94. listAreas);
  95. for (int i = 0; i < listAreas->n_values; i++) {
  96. area = listAreas->value[i];
  97. /* check for other centroids -- only area with one centroid is valid */
  98. Vect_get_area_box(mapInfo, area, &areaBox);
  99. if(Vect_select_lines_by_box(mapInfo, &areaBox,
  100. GV_CENTROID, listCentroids) == 1) {
  101. DrawArea(area);
  102. }
  103. }
  104. Vect_destroy_list(listAreas);
  105. Vect_destroy_list(listCentroids);
  106. }
  107. for (int i = 0; i < listLines->n_values; i++) {
  108. DrawLine(listLines->value[i]);
  109. }
  110. dc->EndDrawing();
  111. // PrintIds();
  112. Vect_destroy_list(listLines);
  113. return listLines->n_values;
  114. }
  115. /**
  116. \brief Draw area fill
  117. \param area area id
  118. \return 1 on success
  119. \return -1 on failure (vector object is dead, etc.)
  120. */
  121. int DisplayDriver::DrawArea(int area)
  122. {
  123. double x, y, z;
  124. struct line_pnts *points;
  125. if (!dc || !Vect_area_alive (mapInfo, area))
  126. return -1;
  127. points = Vect_new_line_struct();
  128. // get boundary points
  129. Vect_get_area_points(mapInfo, area, points);
  130. // convert EN -> xy
  131. wxPoint wxPoints[points->n_points];
  132. for (int i = 0; i < points->n_points; i++) {
  133. Cell2Pixel(points->x[i], points->y[i], points->z[i],
  134. &x, &y, &z);
  135. wxPoints[i] = wxPoint((int) x, (int) y);
  136. }
  137. // draw polygon
  138. dc->SetBrush(wxBrush(settings.area.color));
  139. dc->SetPen(wxPen(settings.area.color));
  140. dc->DrawPolygon(points->n_points, wxPoints);
  141. Vect_destroy_line_struct(points);
  142. return 1;
  143. }
  144. /**
  145. \brief Draw selected vector objects to the device
  146. \param[in] line id
  147. \return 1 on success
  148. \return -1 on failure (vector object is dead, etc.)
  149. */
  150. int DisplayDriver::DrawLine(int line)
  151. {
  152. int dcId; // 0 | 1 | segment id
  153. int type; // line type
  154. double x, y, z; // screen coordinates
  155. bool draw; // draw object ?
  156. wxPen *pen;
  157. pen = NULL;
  158. draw = false;
  159. if (!dc || !Vect_line_alive (mapInfo, line))
  160. return -1;
  161. // read line
  162. type = Vect_read_line (mapInfo, points, cats, line);
  163. // add ids
  164. // -> node1, line1, vertex1, line2, ..., node2
  165. // struct lineDesc desc = {points->n_points, dcId};
  166. // ids[line] = desc;
  167. // update id for next line
  168. // dcId += points->n_points * 2 - 1;
  169. if (IsSelected(line)) { // line selected ?
  170. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  171. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  172. }
  173. else {
  174. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  175. }
  176. if (drawSelected) {
  177. draw = true;
  178. }
  179. else {
  180. draw = false;
  181. }
  182. dcId = 1;
  183. topology.highlight++;
  184. }
  185. else {
  186. dcId = 0;
  187. if (type & GV_LINES) {
  188. switch (type) {
  189. case GV_LINE:
  190. pen = new wxPen(settings.line.color, settings.lineWidth, wxSOLID);
  191. topology.line++;
  192. draw = settings.line.enabled;
  193. break;
  194. case GV_BOUNDARY:
  195. int left, right;
  196. Vect_get_line_areas(mapInfo, line,
  197. &left, &right);
  198. if (left == 0 && right == 0) {
  199. pen = new wxPen(settings.boundaryNo.color, settings.lineWidth, wxSOLID);
  200. topology.boundaryNo++;
  201. draw = settings.boundaryNo.enabled;
  202. }
  203. else if (left > 0 && right > 0) {
  204. pen = new wxPen(settings.boundaryTwo.color, settings.lineWidth, wxSOLID);
  205. topology.boundaryTwo++;
  206. draw = settings.boundaryTwo.enabled;
  207. }
  208. else {
  209. pen = new wxPen(settings.boundaryOne.color, settings.lineWidth, wxSOLID);
  210. topology.boundaryOne++;
  211. draw = settings.boundaryOne.enabled;
  212. }
  213. break;
  214. default:
  215. draw = false;
  216. break;
  217. }
  218. }
  219. else if (type & GV_POINTS) {
  220. if (type == GV_POINT && settings.point.enabled) {
  221. pen = new wxPen(settings.point.color, settings.lineWidth, wxSOLID);
  222. topology.point++;
  223. draw = settings.point.enabled;
  224. }
  225. else if (type == GV_CENTROID) {
  226. int cret = Vect_get_centroid_area(mapInfo, line);
  227. if (cret > 0) { // -> area
  228. draw = settings.centroidIn.enabled;
  229. pen = new wxPen(settings.centroidIn.color, settings.lineWidth, wxSOLID);
  230. topology.centroidIn++;
  231. }
  232. else if (cret == 0) {
  233. draw = settings.centroidOut.enabled;
  234. pen = new wxPen(settings.centroidOut.color, settings.lineWidth, wxSOLID);
  235. topology.centroidOut++;
  236. }
  237. else {
  238. draw = settings.centroidDup.enabled;
  239. pen = new wxPen(settings.centroidDup.color, settings.lineWidth, wxSOLID);
  240. topology.centroidDup++;
  241. }
  242. }
  243. }
  244. }
  245. // clear screen points & convert EN -> xy
  246. pointsScreen->Clear();
  247. for (int i = 0; i < points->n_points; i++) {
  248. Cell2Pixel(points->x[i], points->y[i], points->z[i],
  249. &x, &y, &z);
  250. pointsScreen->Append((wxObject*) new wxPoint((int) x, (int) y)); /* TODO: 3D */
  251. }
  252. dc->SetId(dcId); /* 0 | 1 (selected) */
  253. dc->SetPen(*pen);
  254. if (draw) {
  255. if (type & GV_POINTS) {
  256. DrawCross(line, (const wxPoint *) pointsScreen->GetFirst()->GetData());
  257. }
  258. else {
  259. // long int startId = ids[line].startId + 1;
  260. if (dcId > 0 && drawSegments) {
  261. dcId = 2; // first segment
  262. for (size_t i = 0; i < pointsScreen->GetCount() - 1; dcId += 2) {
  263. wxPoint *point_beg = (wxPoint *) pointsScreen->Item(i)->GetData();
  264. wxPoint *point_end = (wxPoint *) pointsScreen->Item(++i)->GetData();
  265. // set bounds for line
  266. // wxRect rect (*point_beg, *point_end);
  267. // dc->SetIdBounds(startId, rect);
  268. dc->SetId(dcId); // set unique id & set bbox for each segment
  269. dc->SetPen(*pen);
  270. wxRect rect (*point_beg, *point_end);
  271. dc->SetIdBounds(dcId, rect);
  272. dc->DrawLine(point_beg->x, point_beg->y,
  273. point_end->x, point_end->y);
  274. }
  275. }
  276. else {
  277. wxPoint wxPoints[pointsScreen->GetCount()];
  278. for (size_t i = 0; i < pointsScreen->GetCount(); i++) {
  279. wxPoint *point_beg = (wxPoint *) pointsScreen->Item(i)->GetData();
  280. wxPoints[i] = *point_beg;
  281. }
  282. dc->DrawLines(pointsScreen->GetCount(), wxPoints);
  283. if (!IsSelected(line) && settings.direction.enabled) {
  284. DrawDirectionArrow();
  285. // restore pen
  286. dc->SetPen(*pen);
  287. }
  288. }
  289. }
  290. }
  291. if (type & GV_LINES) {
  292. DrawLineVerteces(line); // draw vertices
  293. DrawLineNodes(line); // draw nodes
  294. }
  295. delete pen;
  296. return 1;
  297. }
  298. /**
  299. \brief Draw line verteces to the device
  300. Except of first and last vertex, see DrawLineNodes().
  301. \param line id
  302. \return number of verteces which were drawn
  303. \return -1 if drawing vertices is disabled
  304. */
  305. int DisplayDriver::DrawLineVerteces(int line)
  306. {
  307. int dcId;
  308. wxPoint *point;
  309. wxPen *pen;
  310. if (!IsSelected(line) && !settings.vertex.enabled)
  311. return -1;
  312. // determine color
  313. if (!IsSelected(line)) {
  314. pen = new wxPen(settings.vertex.color, settings.lineWidth, wxSOLID);
  315. dcId = 0;
  316. }
  317. else {
  318. if (!drawSelected) {
  319. return -1;
  320. }
  321. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  322. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  323. }
  324. else {
  325. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  326. }
  327. if (drawSegments) {
  328. dcId = 3; // first vertex
  329. }
  330. else {
  331. dcId = 1;
  332. }
  333. }
  334. dc->SetId(dcId); /* 0 | 1 (selected) */
  335. dc->SetPen(*pen);
  336. for (size_t i = 1; i < pointsScreen->GetCount() - 1; i++, dcId += 2) {
  337. point = (wxPoint*) pointsScreen->Item(i)->GetData();
  338. if (IsSelected(line) && drawSegments) {
  339. dc->SetId(dcId);
  340. dc->SetPen(*pen);
  341. wxRect rect (*point, *point);
  342. dc->SetIdBounds(dcId, rect);
  343. }
  344. if (settings.vertex.enabled) {
  345. DrawCross(line, (const wxPoint*) pointsScreen->Item(i)->GetData());
  346. topology.vertex++;
  347. }
  348. }
  349. delete pen;
  350. return pointsScreen->GetCount() - 2;
  351. }
  352. /**
  353. \brief Draw line nodes to the device
  354. \param line id
  355. \return 1
  356. \return -1 if no nodes were drawn
  357. */
  358. int DisplayDriver::DrawLineNodes(int line)
  359. {
  360. int dcId;
  361. int node;
  362. double east, north, depth;
  363. double x, y, z;
  364. int nodes [2];
  365. bool draw;
  366. wxPen *pen;
  367. // draw nodes??
  368. if (!settings.nodeOne.enabled && !settings.nodeTwo.enabled)
  369. return -1;
  370. // get nodes
  371. Vect_get_line_nodes(mapInfo, line, &(nodes[0]), &(nodes[1]));
  372. for (size_t i = 0; i < sizeof(nodes) / sizeof(int); i++) {
  373. node = nodes[i];
  374. // get coordinates
  375. Vect_get_node_coor(mapInfo, node,
  376. &east, &north, &depth);
  377. // convert EN->xy
  378. Cell2Pixel(east, north, depth,
  379. &x, &y, &z);
  380. // determine color
  381. if (IsSelected(line)) {
  382. if (!drawSelected) {
  383. return -1;
  384. }
  385. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  386. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  387. }
  388. else {
  389. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  390. }
  391. draw = true;
  392. if (!drawSegments) {
  393. dcId = 1;
  394. }
  395. else {
  396. // node1, line1, vertex1, line2, vertex2, ..., node2
  397. if (i == 0) // first node
  398. dcId = 1;
  399. else // last node
  400. dcId = 2 * points->n_points - 1;
  401. }
  402. }
  403. else {
  404. dcId = 0;
  405. if (Vect_get_node_n_lines(mapInfo, node) == 1) {
  406. pen = new wxPen(settings.nodeOne.color, settings.lineWidth, wxSOLID);
  407. topology.nodeOne++;
  408. draw = settings.nodeOne.enabled;
  409. }
  410. else {
  411. pen = new wxPen(settings.nodeTwo.color, settings.lineWidth, wxSOLID);
  412. topology.nodeTwo++;
  413. draw = settings.nodeTwo.enabled;
  414. }
  415. }
  416. wxPoint point((int) x, (int) y);
  417. if (IsSelected(line) && drawSegments) {
  418. wxRect rect (point, point);
  419. dc->SetIdBounds(dcId, rect);
  420. }
  421. // draw node if needed
  422. if (draw) {
  423. dc->SetId(dcId);
  424. dc->SetPen(*pen);
  425. DrawCross(line, &point);
  426. }
  427. }
  428. delete pen;
  429. return 1;
  430. }
  431. /*
  432. \brief Close vector map layer
  433. \param void
  434. \return 0 on success
  435. \return non-zero on error
  436. */
  437. int DisplayDriver::CloseMap()
  438. {
  439. int ret;
  440. ret = -1;
  441. if (mapInfo) {
  442. if (mapInfo->mode == GV_MODE_RW) {
  443. /* rebuild topology */
  444. Vect_build_partial(mapInfo, GV_BUILD_NONE, NULL);
  445. Vect_build(mapInfo, NULL);
  446. }
  447. /* close map and store topo/cidx */
  448. ret = Vect_close(mapInfo);
  449. G_free ((void *) mapInfo);
  450. mapInfo = NULL;
  451. }
  452. return ret;
  453. }
  454. /**
  455. \brief Open vector map layer
  456. \param[in] mapname name of vector map
  457. \param[in] mapset name of mapset where the vector map layer is stored
  458. \return topo level
  459. \return -1 on error
  460. */
  461. int DisplayDriver::OpenMap(const char* mapname, const char *mapset, bool update)
  462. {
  463. int ret;
  464. if (!mapInfo)
  465. mapInfo = (struct Map_info *) G_malloc (sizeof (struct Map_info));
  466. // define open level (level 2: topology)
  467. Vect_set_open_level(2);
  468. // avoid GUI crash when G_fatal_error() is called (opening the vector map)
  469. Vect_set_fatal_error(GV_FATAL_PRINT);
  470. // open existing map
  471. if (!update) {
  472. ret = Vect_open_old(mapInfo, (char*) mapname, (char *) mapset);
  473. }
  474. else {
  475. ret = Vect_open_update(mapInfo, (char*) mapname, (char *) mapset);
  476. }
  477. if (ret == -1) { // error
  478. G_free((void *) mapInfo);
  479. mapInfo = NULL;
  480. }
  481. return ret;
  482. }
  483. /**
  484. \brief Reload vector map layer
  485. Close and open again. Needed for modification using v.edit.
  486. TODO: Get rid of that...
  487. \param
  488. \return
  489. */
  490. void DisplayDriver::ReloadMap()
  491. {
  492. // char* name = G_store(Vect_get_map_name(mapInfo)); ???
  493. char* name = G_store(mapInfo->name);
  494. char* mapset = G_store(Vect_get_mapset(mapInfo));
  495. Vect_close(mapInfo);
  496. mapInfo = NULL;
  497. OpenMap(name, mapset, false); // used only for v.edit
  498. //Vect_build_partial(mapInfo, GV_BUILD_NONE, stderr);
  499. //Vect_build(mapInfo, stderr);
  500. return;
  501. }
  502. /*
  503. \brief Conversion from geographic coordinates (east, north)
  504. to screen (x, y)
  505. TODO: 3D stuff...
  506. \param[in] east,north,depth geographical coordinates
  507. \param[out] x, y, z screen coordinates
  508. \return
  509. */
  510. void DisplayDriver::Cell2Pixel(double east, double north, double depth,
  511. double *x, double *y, double *z)
  512. {
  513. double n, w;
  514. /*
  515. *x = int((east - region.map_west) / region.map_res);
  516. *y = int((region.map_north - north) / region.map_res);
  517. */
  518. w = region.center_easting - (region.map_width / 2) * region.map_res;
  519. n = region.center_northing + (region.map_height / 2) * region.map_res;
  520. /*
  521. *x = int((east - w) / region.map_res);
  522. *y = int((n - north) / region.map_res);
  523. */
  524. if (x)
  525. *x = (east - w) / region.map_res;
  526. if (y)
  527. *y = (n - north) / region.map_res;
  528. if (z)
  529. *z = 0.;
  530. return;
  531. }
  532. /**
  533. \brief Calculate distance in pixels
  534. \todo LL projection
  535. \param dist real distance
  536. */
  537. double DisplayDriver::DistanceInPixels(double dist)
  538. {
  539. double x;
  540. Cell2Pixel(region.map_west + dist, region.map_north, 0.0, &x, NULL, NULL);
  541. return std::sqrt(x * x);
  542. }
  543. /**
  544. \brief Set geographical region
  545. Region must be upgraded because of Cell2Pixel().
  546. \param[in] north,south,east,west,ns_res,ew_res region settings
  547. \return
  548. */
  549. void DisplayDriver::SetRegion(double north, double south, double east, double west,
  550. double ns_res, double ew_res,
  551. double center_easting, double center_northing,
  552. double map_width, double map_height)
  553. {
  554. region.box.N = north;
  555. region.box.S = south;
  556. region.box.E = east;
  557. region.box.W = west;
  558. region.box.T = PORT_DOUBLE_MAX;
  559. region.box.B = -PORT_DOUBLE_MAX;
  560. region.ns_res = ns_res;
  561. region.ew_res = ew_res;
  562. region.center_easting = center_easting;
  563. region.center_northing = center_northing;
  564. region.map_width = map_width;
  565. region.map_height = map_height;
  566. // calculate real region
  567. region.map_res = (region.ew_res > region.ns_res) ? region.ew_res : region.ns_res;
  568. region.map_west = region.center_easting - (region.map_width / 2.) * region.map_res;
  569. region.map_north = region.center_northing + (region.map_height / 2.) * region.map_res;
  570. return;
  571. }
  572. /**
  573. \brief Draw cross symbol of given size to device content
  574. Used for points, nodes, vertices
  575. \param[in] point coordinates of center
  576. \param[in] size size of the cross symbol
  577. \return 1 on success
  578. \return -1 on failure
  579. */
  580. int DisplayDriver::DrawCross(int line, const wxPoint* point, int size)
  581. {
  582. if (!dc || !point)
  583. return -1;
  584. dc->DrawLine(point->x - size, point->y, point->x + size, point->y);
  585. dc->DrawLine(point->x, point->y - size, point->x, point->y + size);
  586. return 1;
  587. }
  588. /*
  589. \brief Set settings for displaying vector feature
  590. E.g. line width, color, ...
  591. \param[in] lineWidth,... settgings
  592. \return
  593. */
  594. void DisplayDriver::UpdateSettings(unsigned long highlight,
  595. bool ehighlightDupl, unsigned long chighlightDupl,
  596. bool ePoint, unsigned long cPoint, /* enabled, color */
  597. bool eLine, unsigned long cLine,
  598. bool eBoundaryNo, unsigned long cBoundaryNo,
  599. bool eBoundaryOne, unsigned long cBoundaryOne,
  600. bool eBoundaryTwo, unsigned long cBoundaryTwo,
  601. bool eCentroidIn, unsigned long cCentroidIn,
  602. bool eCentroidOut, unsigned long cCentroidOut,
  603. bool eCentroidDup, unsigned long cCentroidDup,
  604. bool eNodeOne, unsigned long cNodeOne,
  605. bool eNodeTwo, unsigned long cNodeTwo,
  606. bool eVertex, unsigned long cVertex,
  607. bool eArea, unsigned long cArea,
  608. bool eDirection, unsigned long cDirection,
  609. int lineWidth)
  610. {
  611. settings.highlight.Set(highlight);
  612. settings.highlightDupl.enabled = ehighlightDupl;
  613. settings.highlightDupl.color.Set(chighlightDupl);
  614. settings.point.enabled = ePoint;
  615. settings.point.color.Set(cPoint);
  616. settings.line.enabled = eLine;
  617. settings.line.color.Set(cLine);
  618. settings.boundaryNo.enabled = eBoundaryNo;
  619. settings.boundaryNo.color.Set(cBoundaryNo);
  620. settings.boundaryOne.enabled = eBoundaryOne;
  621. settings.boundaryOne.color.Set(cBoundaryOne);
  622. settings.boundaryTwo.enabled = eBoundaryTwo;
  623. settings.boundaryTwo.color.Set(cBoundaryTwo);
  624. settings.centroidIn.enabled = eCentroidIn;
  625. settings.centroidIn.color.Set(cCentroidIn);
  626. settings.centroidOut.enabled = eCentroidOut;
  627. settings.centroidOut.color.Set(cCentroidOut);
  628. settings.centroidDup.enabled = eCentroidDup;
  629. settings.centroidDup.color.Set(cCentroidDup);
  630. settings.nodeOne.enabled = eNodeOne;
  631. settings.nodeOne.color.Set(cNodeOne);
  632. settings.nodeTwo.enabled = eNodeTwo;
  633. settings.nodeTwo.color.Set(cNodeTwo);
  634. settings.vertex.enabled = eVertex;
  635. settings.vertex.color.Set(cVertex);
  636. settings.area.enabled = eArea;
  637. settings.area.color.Set(cArea);
  638. settings.direction.enabled = eDirection;
  639. settings.direction.color.Set(cDirection);
  640. settings.lineWidth = lineWidth;
  641. }
  642. /**
  643. \brief Prints gId: dcIds
  644. Useful for debugging purposes.
  645. \param
  646. \return
  647. */
  648. void DisplayDriver::PrintIds()
  649. {
  650. std::cerr << "topology.highlight: " << topology.highlight << std::endl;
  651. std::cerr << "topology.point: " << topology.point << std::endl;
  652. std::cerr << "topology.line: " << topology.line << std::endl;
  653. std::cerr << "topology.boundaryNo: " << topology.boundaryNo << std::endl;
  654. std::cerr << "topology.boundaryOne: " << topology.boundaryOne << std::endl;
  655. std::cerr << "topology.boundaryTwo: " << topology.boundaryTwo << std::endl;
  656. std::cerr << "topology.centroidIn: " << topology.centroidIn << std::endl;
  657. std::cerr << "topology.centroidOut: " << topology.centroidOut << std::endl;
  658. std::cerr << "topology.centroidDup: " << topology.centroidDup << std::endl;
  659. std::cerr << "topology.nodeOne: " << topology.nodeOne << std::endl;
  660. std::cerr << "topology.nodeTwo: " << topology.nodeTwo << std::endl;
  661. std::cerr << "topology.vertex: " << topology.vertex << std::endl;
  662. std::cerr << std::endl << "nobjects: "
  663. << topology.point * 2 + // cross
  664. topology.line +
  665. topology.boundaryNo +
  666. topology.boundaryOne +
  667. topology.boundaryTwo +
  668. topology.centroidIn * 2 +
  669. topology.centroidOut * 2 +
  670. topology.centroidDup * 2 +
  671. topology.nodeOne * 2 +
  672. topology.nodeTwo * 2 +
  673. topology.vertex * 2 << std::endl;
  674. std::cerr << "selected: ";
  675. for (int i = 0; i < selected->n_values; i++) {
  676. std::cerr << selected->value[i] << " ";
  677. }
  678. std::cerr << std::endl;
  679. return;
  680. }
  681. /**
  682. \brief Select vector objects by given bounding box
  683. If line id is already in the list of selected lines, then it will
  684. be excluded from this list.
  685. \param[in] x1,y1,z1,x2,y2,z3 bounding box definition
  686. \param[in] type feature type
  687. \return number of selected features
  688. \return -1 on error
  689. */
  690. int DisplayDriver::SelectLinesByBox(double x1, double y1, double z1,
  691. double x2, double y2, double z2,
  692. int type)
  693. {
  694. if (!mapInfo)
  695. return -1;
  696. int line;
  697. struct ilist *list;
  698. struct line_pnts *bbox;
  699. drawSegments = false;
  700. drawSelected = true;
  701. list = Vect_new_list();
  702. bbox = Vect_new_line_struct();
  703. Vect_append_point(bbox, x1, y1, z1);
  704. Vect_append_point(bbox, x2, y1, z2);
  705. Vect_append_point(bbox, x2, y2, z1);
  706. Vect_append_point(bbox, x1, y2, z2);
  707. Vect_append_point(bbox, x1, y1, z1);
  708. Vect_select_lines_by_polygon(mapInfo, bbox,
  709. 0, NULL, /* isles */
  710. type, list);
  711. for (int i = 0; i < list->n_values; i++) {
  712. line = list->value[i];
  713. if (!IsSelected(line)) {
  714. Vect_list_append(selected, line);
  715. }
  716. else {
  717. Vect_list_delete(selected, line);
  718. }
  719. }
  720. Vect_destroy_line_struct(bbox);
  721. Vect_destroy_list(list);
  722. return list->n_values;
  723. }
  724. /**
  725. \brief Select vector feature by given point in given
  726. threshold
  727. Only one vector object can be selected. Bounding boxes of
  728. all segments are stores.
  729. \param[in] x,y point of searching
  730. \param[in] thresh threshold value where to search
  731. \param[in] type select vector object of given type
  732. \return point on line if line found
  733. */
  734. std::vector<double> DisplayDriver::SelectLineByPoint(double x, double y, double z,
  735. double thresh, int type, int with_z)
  736. {
  737. long int line;
  738. double px, py, pz;
  739. std::vector<double> p;
  740. drawSelected = true;
  741. line = Vect_find_line(mapInfo, x, y, z,
  742. type, thresh, with_z, 0);
  743. if (line > 0) {
  744. if (!IsSelected(line)) {
  745. Vect_list_append(selected, line);
  746. }
  747. else {
  748. Vect_list_delete(selected, line);
  749. }
  750. type = Vect_read_line (mapInfo, points, cats, line);
  751. Vect_line_distance (points, x, y, z, with_z,
  752. &px, &py, &pz,
  753. NULL, NULL, NULL);
  754. p.push_back(px);
  755. p.push_back(py);
  756. if (with_z) {
  757. p.push_back(pz);
  758. }
  759. }
  760. drawSegments = true;
  761. return p;
  762. }
  763. /**
  764. \brief Is vector object selected?
  765. \param[in] line id
  766. \return true if vector object is selected
  767. \return false if vector object is not selected
  768. */
  769. bool DisplayDriver::IsSelected(int line)
  770. {
  771. if (Vect_val_in_list(selected, line))
  772. return true;
  773. return false;
  774. }
  775. /**
  776. \brief Get ids of selected objects
  777. \param[in] grassId if true return GRASS line ids
  778. if false return PseudoDC ids
  779. \return list of ids of selected vector objects
  780. */
  781. std::vector<int> DisplayDriver::GetSelected(bool grassId)
  782. {
  783. if (grassId)
  784. return ListToVector(selected);
  785. std::vector<int> dc_ids;
  786. if (!drawSegments) {
  787. dc_ids.push_back(1);
  788. }
  789. else {
  790. int npoints;
  791. Vect_read_line(mapInfo, points, NULL, selected->value[0]);
  792. npoints = points->n_points;
  793. for (int i = 1; i < 2 * npoints; i++) {
  794. dc_ids.push_back(i);
  795. }
  796. }
  797. return dc_ids;
  798. }
  799. /**
  800. \brief Get feature (grass) ids of duplicated objects
  801. \return list of ids
  802. */
  803. std::map<int, std::vector <int> > DisplayDriver::GetDuplicates()
  804. {
  805. std::map<int, std::vector<int> > ids;
  806. struct line_pnts *APoints, *BPoints;
  807. int line;
  808. APoints = Vect_new_line_struct();
  809. BPoints = Vect_new_line_struct();
  810. Vect_reset_list(selectedDupl);
  811. for (int i = 0; i < selected->n_values; i++) {
  812. line = selected->value[i];
  813. if (IsDuplicated(line))
  814. continue;
  815. Vect_read_line(mapInfo, APoints, NULL, line);
  816. for (int j = 0; j < selected->n_values; j++) {
  817. if (i == j || IsDuplicated(selected->value[j]))
  818. continue;
  819. Vect_read_line(mapInfo, BPoints, NULL, selected->value[j]);
  820. if (Vect_line_check_duplicate (APoints, BPoints, WITHOUT_Z)) {
  821. if (ids.find(i) == ids.end()) {
  822. ids[i] = std::vector<int> ();
  823. ids[i].push_back(selected->value[i]);
  824. Vect_list_append(selectedDupl, selected->value[i]);
  825. }
  826. ids[i].push_back(selected->value[j]);
  827. Vect_list_append(selectedDupl, selected->value[j]);
  828. }
  829. }
  830. }
  831. Vect_destroy_line_struct(APoints);
  832. Vect_destroy_line_struct(BPoints);
  833. return ids;
  834. }
  835. /**
  836. \brief Check for already marked duplicates
  837. \param line line id
  838. \return 1 line already marked as duplicated
  839. \return 0 not duplicated
  840. */
  841. bool DisplayDriver::IsDuplicated(int line)
  842. {
  843. if (Vect_val_in_list(selectedDupl, line))
  844. return true;
  845. return false;
  846. }
  847. /**
  848. \brief Set selected vector objects
  849. \param[in] list of GRASS ids to be set
  850. \return 1
  851. */
  852. int DisplayDriver::SetSelected(std::vector<int> id)
  853. {
  854. drawSelected = true;
  855. VectorToList(selected, id);
  856. if (selected->n_values <= 0)
  857. drawSegments = false;
  858. return 1;
  859. }
  860. /**
  861. \brief Unselect selected vector features
  862. \param[in] list of GRASS feature ids
  863. \return number of selected features
  864. */
  865. int DisplayDriver::UnSelect(std::vector<int> id)
  866. {
  867. bool checkForDupl;
  868. checkForDupl = false;
  869. for (std::vector<int>::const_iterator i = id.begin(), e = id.end();
  870. i != e; ++i) {
  871. if (IsSelected(*i)) {
  872. Vect_list_delete(selected, *i);
  873. }
  874. if (settings.highlightDupl.enabled && IsDuplicated(*i)) {
  875. checkForDupl = true;
  876. }
  877. }
  878. if (checkForDupl) {
  879. GetDuplicates();
  880. }
  881. return selected->n_values;
  882. }
  883. /**
  884. \brief Get PseudoDC vertex id of selected line
  885. Set bounding box for vertices of line.
  886. \param[in] x,y coordinates of click
  887. \param[in] thresh threshold value
  888. \return id of center, left and right vertex
  889. \return 0 no line found
  890. \return -1 on error
  891. */
  892. std::vector<int> DisplayDriver::GetSelectedVertex(double x, double y, double thresh)
  893. {
  894. int startId;
  895. int line, type;
  896. int Gid, DCid;
  897. double vx, vy, vz; // vertex screen coordinates
  898. double dist, minDist;
  899. std::vector<int> returnId;
  900. // only one object can be selected
  901. if (selected->n_values != 1 || !drawSegments)
  902. return returnId;
  903. startId = 1;
  904. line = selected->value[0];
  905. type = Vect_read_line (mapInfo, points, cats, line);
  906. minDist = 0.0;
  907. Gid = -1;
  908. // find the closest vertex (x, y)
  909. DCid = 1;
  910. for(int idx = 0; idx < points->n_points; idx++) {
  911. dist = Vect_points_distance(x, y, 0.0,
  912. points->x[idx], points->y[idx], points->z[idx], 0);
  913. if (idx == 0) {
  914. minDist = dist;
  915. Gid = idx;
  916. }
  917. else {
  918. if (minDist > dist) {
  919. minDist = dist;
  920. Gid = idx;
  921. }
  922. }
  923. Cell2Pixel(points->x[idx], points->y[idx], points->z[idx],
  924. &vx, &vy, &vz);
  925. wxRect rect (wxPoint ((int) vx, (int) vy), wxPoint ((int) vx, (int) vy));
  926. dc->SetIdBounds(DCid, rect);
  927. DCid+=2;
  928. }
  929. if (minDist > thresh)
  930. return returnId;
  931. // desc = &(ids[line]);
  932. // translate id
  933. DCid = Gid * 2 + 1;
  934. // add selected vertex
  935. returnId.push_back(DCid);
  936. // left vertex
  937. if (DCid == startId) {
  938. returnId.push_back(-1);
  939. }
  940. else {
  941. returnId.push_back(DCid - 2);
  942. }
  943. // right vertex
  944. if (DCid == (points->n_points - 1) * 2 + startId) {
  945. returnId.push_back(-1);
  946. }
  947. else {
  948. returnId.push_back(DCid + 2);
  949. }
  950. return returnId;
  951. }
  952. /**
  953. \brief Reset topology structure.
  954. \return
  955. */
  956. void DisplayDriver::ResetTopology()
  957. {
  958. topology.highlight = 0;
  959. topology.point = 0;
  960. topology.line = 0;
  961. topology.boundaryNo = 0;
  962. topology.boundaryOne = 0;
  963. topology.boundaryTwo = 0;
  964. topology.centroidIn = 0;
  965. topology.centroidOut = 0;
  966. topology.centroidDup = 0;
  967. topology.nodeOne = 0;
  968. topology.nodeTwo = 0;
  969. topology.vertex = 0;
  970. return;
  971. }
  972. /**
  973. \brief Convert vect list to std::vector
  974. \param list vect list
  975. \return std::vector
  976. */
  977. std::vector<int> DisplayDriver::ListToVector(struct ilist *list)
  978. {
  979. std::vector<int> vect;
  980. if (!list)
  981. return vect;
  982. for (int i = 0; i < list->n_values; i++) {
  983. vect.push_back(list->value[i]);
  984. }
  985. return vect;
  986. }
  987. /**
  988. \brief Convert std::vector to vect list
  989. \param list vect list
  990. \param vec std::vector instance
  991. \return number of items
  992. \return -1 on error
  993. */
  994. int DisplayDriver::VectorToList(struct ilist *list, const std::vector<int>& vec)
  995. {
  996. if (!list)
  997. return -1;
  998. Vect_reset_list(list);
  999. for (std::vector<int>::const_iterator i = vec.begin(), e = vec.end();
  1000. i != e; ++i) {
  1001. Vect_list_append(list, *i);
  1002. }
  1003. return list->n_values;
  1004. }
  1005. /**
  1006. \brief Get bounding box of (opened) vector map layer
  1007. \return (w,s,b,e,n,t)
  1008. */
  1009. std::vector<double> DisplayDriver::GetMapBoundingBox()
  1010. {
  1011. std::vector<double> region;
  1012. BOUND_BOX bbox;
  1013. if (!mapInfo) {
  1014. return region;
  1015. }
  1016. Vect_get_map_box(mapInfo, &bbox);
  1017. region.push_back(bbox.W);
  1018. region.push_back(bbox.S);
  1019. region.push_back(bbox.B);
  1020. region.push_back(bbox.E);
  1021. region.push_back(bbox.N);
  1022. region.push_back(bbox.T);
  1023. return region;
  1024. }
  1025. /**
  1026. \brief Error messages handling
  1027. \param msg message
  1028. \param type type message (MSG, WARN, ERR)
  1029. \return 0
  1030. */
  1031. int print_error(const char *msg, int type)
  1032. {
  1033. fprintf(stderr, "%s", msg);
  1034. return 0;
  1035. }
  1036. /**
  1037. \brief Draw selected features
  1038. \param draw if true draw selected features
  1039. */
  1040. void DisplayDriver::DrawSelected(bool draw)
  1041. {
  1042. drawSelected = draw;
  1043. return;
  1044. }
  1045. /**
  1046. \brief Draw line direction arrow
  1047. \return number of drawn arrows
  1048. */
  1049. int DisplayDriver::DrawDirectionArrow()
  1050. {
  1051. int narrows;
  1052. int size; // arrow length in pixels
  1053. int limit; // segment length limit for drawing symbol (in pixels)
  1054. double dist, angle, pos;
  1055. double e, n, d, x0, y0, z0, x1, y1, z1;
  1056. struct line_pnts *points_seg;
  1057. wxPen *pen_arrow;
  1058. narrows = 0;
  1059. size = 5;
  1060. limit = 5; // 5px for line segment
  1061. points_seg = Vect_new_line_struct();
  1062. pen_arrow = new wxPen(settings.direction.color, settings.lineWidth, wxSOLID);
  1063. dc->SetPen(*pen_arrow);
  1064. dist = Vect_line_length(points);
  1065. if (DistanceInPixels(dist) >= limit) {
  1066. while (1) {
  1067. pos = (narrows + 1) * 8 * limit * region.map_res;
  1068. if (Vect_point_on_line(points, pos,
  1069. &e, &n, &d, NULL, NULL) < 1) {
  1070. break;
  1071. }
  1072. Cell2Pixel(e, n, d, &x0, &y0, &z0);
  1073. if (Vect_point_on_line(points, pos - 3 * size * region.map_res,
  1074. &e, &n, &d, &angle, NULL) < 1) {
  1075. break;
  1076. }
  1077. Cell2Pixel(e, n, d, &x1, &y1, &z1);
  1078. DrawArrow(x0, y0, x1, y1, angle, size);
  1079. if(narrows > 1e2) // low resolution, break
  1080. break;
  1081. narrows++;
  1082. }
  1083. // draw at least one arrow in the middle of line
  1084. if (narrows < 1) {
  1085. dist /= 2.;
  1086. if (Vect_point_on_line(points, dist,
  1087. &e, &n, &d, NULL, NULL) > 0) {
  1088. Cell2Pixel(e, n, d, &x0, &y0, &z0);
  1089. if (Vect_point_on_line(points, dist - 3 * size * region.map_res,
  1090. &e, &n, &d, &angle, NULL) > 0) {
  1091. Cell2Pixel(e, n, d, &x1, &y1, &z1);
  1092. DrawArrow(x0, y0, x1, y1, angle, size);
  1093. }
  1094. }
  1095. }
  1096. }
  1097. Vect_destroy_line_struct(points_seg);
  1098. return narrows;
  1099. }
  1100. /**
  1101. \brief Draw arrow symbol on line
  1102. \param x0,y0 arrow origin
  1103. \param x1,x1 arrow ending point (on line)
  1104. \param angle point ending point angle
  1105. \param size arrow size
  1106. \return 1
  1107. */
  1108. int DisplayDriver::DrawArrow(double x0, double y0,
  1109. double x1, double y1, double angle,
  1110. int size)
  1111. {
  1112. double x, y;
  1113. double angle_symb;
  1114. angle_symb = angle - M_PI / 2.;
  1115. x = x1 + size * std::cos(angle_symb);
  1116. y = y1 - size * std::sin(angle_symb);
  1117. dc->DrawLine((wxCoord) x, (wxCoord) y, (wxCoord) x0, (wxCoord) y0);
  1118. angle_symb = M_PI / 2. + angle;
  1119. x = x1 + size * std::cos(angle_symb);
  1120. y = y1 - size * std::sin(angle_symb);
  1121. dc->DrawLine((wxCoord) x0, (wxCoord) y0, (wxCoord) x, (wxCoord) y);
  1122. return 1;
  1123. }