driver.cpp 34 KB

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