driver.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  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, bool breakLines)
  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. settings.breakLines = breakLines;
  695. return;
  696. }
  697. /**
  698. \brief Prints gId: dcIds
  699. Useful for debugging purposes.
  700. \param
  701. \return
  702. */
  703. void DisplayDriver::PrintIds()
  704. {
  705. std::cerr << "topology.highlight: " << topology.highlight << std::endl;
  706. std::cerr << "topology.point: " << topology.point << std::endl;
  707. std::cerr << "topology.line: " << topology.line << std::endl;
  708. std::cerr << "topology.boundaryNo: " << topology.boundaryNo << std::endl;
  709. std::cerr << "topology.boundaryOne: " << topology.boundaryOne << std::endl;
  710. std::cerr << "topology.boundaryTwo: " << topology.boundaryTwo << std::endl;
  711. std::cerr << "topology.centroidIn: " << topology.centroidIn << std::endl;
  712. std::cerr << "topology.centroidOut: " << topology.centroidOut << std::endl;
  713. std::cerr << "topology.centroidDup: " << topology.centroidDup << std::endl;
  714. std::cerr << "topology.nodeOne: " << topology.nodeOne << std::endl;
  715. std::cerr << "topology.nodeTwo: " << topology.nodeTwo << std::endl;
  716. std::cerr << "topology.vertex: " << topology.vertex << std::endl;
  717. std::cerr << std::endl << "nobjects: "
  718. << topology.point * 2 + // cross
  719. topology.line +
  720. topology.boundaryNo +
  721. topology.boundaryOne +
  722. topology.boundaryTwo +
  723. topology.centroidIn * 2 +
  724. topology.centroidOut * 2 +
  725. topology.centroidDup * 2 +
  726. topology.nodeOne * 2 +
  727. topology.nodeTwo * 2 +
  728. topology.vertex * 2 << std::endl;
  729. std::cerr << "selected: ";
  730. for (int i = 0; i < selected->n_values; i++) {
  731. std::cerr << selected->value[i] << " ";
  732. }
  733. std::cerr << std::endl;
  734. return;
  735. }
  736. /**
  737. \brief Select vector objects by given bounding box
  738. If line id is already in the list of selected lines, then it will
  739. be excluded from this list.
  740. \param[in] x1,y1,z1,x2,y2,z3 bounding box definition
  741. \param[in] type feature type
  742. \return number of selected features
  743. \return -1 on error
  744. */
  745. int DisplayDriver::SelectLinesByBox(double x1, double y1, double z1,
  746. double x2, double y2, double z2,
  747. int type)
  748. {
  749. if (!mapInfo)
  750. return -1;
  751. int line;
  752. struct ilist *list;
  753. struct line_pnts *bbox;
  754. drawSegments = false;
  755. drawSelected = true;
  756. list = Vect_new_list();
  757. bbox = Vect_new_line_struct();
  758. Vect_append_point(bbox, x1, y1, z1);
  759. Vect_append_point(bbox, x2, y1, z2);
  760. Vect_append_point(bbox, x2, y2, z1);
  761. Vect_append_point(bbox, x1, y2, z2);
  762. Vect_append_point(bbox, x1, y1, z1);
  763. Vect_select_lines_by_polygon(mapInfo, bbox,
  764. 0, NULL, /* isles */
  765. type, list);
  766. for (int i = 0; i < list->n_values; i++) {
  767. line = list->value[i];
  768. if (!IsSelected(line)) {
  769. Vect_list_append(selected, line);
  770. }
  771. else {
  772. Vect_list_delete(selected, line);
  773. }
  774. }
  775. Vect_destroy_line_struct(bbox);
  776. Vect_destroy_list(list);
  777. return list->n_values;
  778. }
  779. /**
  780. \brief Select vector feature by given point in given
  781. threshold
  782. Only one vector object can be selected. Bounding boxes of
  783. all segments are stores.
  784. \param[in] x,y point of searching
  785. \param[in] thresh threshold value where to search
  786. \param[in] type select vector object of given type
  787. \return point on line if line found
  788. */
  789. std::vector<double> DisplayDriver::SelectLineByPoint(double x, double y, double z,
  790. double thresh, int type, int with_z)
  791. {
  792. long int line;
  793. double px, py, pz;
  794. std::vector<double> p;
  795. drawSelected = true;
  796. line = Vect_find_line(mapInfo, x, y, z,
  797. type, thresh, with_z, 0);
  798. if (line > 0) {
  799. if (!IsSelected(line)) {
  800. Vect_list_append(selected, line);
  801. }
  802. else {
  803. Vect_list_delete(selected, line);
  804. }
  805. type = Vect_read_line (mapInfo, points, cats, line);
  806. Vect_line_distance (points, x, y, z, with_z,
  807. &px, &py, &pz,
  808. NULL, NULL, NULL);
  809. p.push_back(px);
  810. p.push_back(py);
  811. if (with_z) {
  812. p.push_back(pz);
  813. }
  814. }
  815. drawSegments = true;
  816. return p;
  817. }
  818. /**
  819. \brief Is vector object selected?
  820. \param[in] line id
  821. \return true if vector object is selected
  822. \return false if vector object is not selected
  823. */
  824. bool DisplayDriver::IsSelected(int line)
  825. {
  826. if (Vect_val_in_list(selected, line))
  827. return true;
  828. return false;
  829. }
  830. /**
  831. \brief Get ids of selected objects
  832. \param[in] grassId if true return GRASS line ids
  833. if false return PseudoDC ids
  834. \return list of ids of selected vector objects
  835. */
  836. std::vector<int> DisplayDriver::GetSelected(bool grassId)
  837. {
  838. if (grassId)
  839. return ListToVector(selected);
  840. std::vector<int> dc_ids;
  841. if (!drawSegments) {
  842. dc_ids.push_back(1);
  843. }
  844. else {
  845. int npoints;
  846. Vect_read_line(mapInfo, points, NULL, selected->value[0]);
  847. npoints = points->n_points;
  848. for (int i = 1; i < 2 * npoints; i++) {
  849. dc_ids.push_back(i);
  850. }
  851. }
  852. return dc_ids;
  853. }
  854. /**
  855. \brief Get feature (grass) ids of duplicated objects
  856. \return list of ids
  857. */
  858. std::map<int, std::vector <int> > DisplayDriver::GetDuplicates()
  859. {
  860. std::map<int, std::vector<int> > ids;
  861. struct line_pnts *APoints, *BPoints;
  862. int line;
  863. APoints = Vect_new_line_struct();
  864. BPoints = Vect_new_line_struct();
  865. Vect_reset_list(selectedDupl);
  866. for (int i = 0; i < selected->n_values; i++) {
  867. line = selected->value[i];
  868. if (IsDuplicated(line))
  869. continue;
  870. Vect_read_line(mapInfo, APoints, NULL, line);
  871. for (int j = 0; j < selected->n_values; j++) {
  872. if (i == j || IsDuplicated(selected->value[j]))
  873. continue;
  874. Vect_read_line(mapInfo, BPoints, NULL, selected->value[j]);
  875. if (Vect_line_check_duplicate (APoints, BPoints, WITHOUT_Z)) {
  876. if (ids.find(i) == ids.end()) {
  877. ids[i] = std::vector<int> ();
  878. ids[i].push_back(selected->value[i]);
  879. Vect_list_append(selectedDupl, selected->value[i]);
  880. }
  881. ids[i].push_back(selected->value[j]);
  882. Vect_list_append(selectedDupl, selected->value[j]);
  883. }
  884. }
  885. }
  886. Vect_destroy_line_struct(APoints);
  887. Vect_destroy_line_struct(BPoints);
  888. return ids;
  889. }
  890. /**
  891. \brief Check for already marked duplicates
  892. \param line line id
  893. \return 1 line already marked as duplicated
  894. \return 0 not duplicated
  895. */
  896. bool DisplayDriver::IsDuplicated(int line)
  897. {
  898. if (Vect_val_in_list(selectedDupl, line))
  899. return true;
  900. return false;
  901. }
  902. /**
  903. \brief Set selected vector objects
  904. \param[in] list of GRASS ids to be set
  905. \return 1
  906. */
  907. int DisplayDriver::SetSelected(std::vector<int> id)
  908. {
  909. drawSelected = true;
  910. VectorToList(selected, id);
  911. if (selected->n_values <= 0)
  912. drawSegments = false;
  913. return 1;
  914. }
  915. /**
  916. \brief Unselect selected vector features
  917. \param[in] list of GRASS feature ids
  918. \return number of selected features
  919. */
  920. int DisplayDriver::UnSelect(std::vector<int> id)
  921. {
  922. bool checkForDupl;
  923. checkForDupl = false;
  924. for (std::vector<int>::const_iterator i = id.begin(), e = id.end();
  925. i != e; ++i) {
  926. if (IsSelected(*i)) {
  927. Vect_list_delete(selected, *i);
  928. }
  929. if (settings.highlightDupl.enabled && IsDuplicated(*i)) {
  930. checkForDupl = true;
  931. }
  932. }
  933. if (checkForDupl) {
  934. GetDuplicates();
  935. }
  936. return selected->n_values;
  937. }
  938. /**
  939. \brief Get PseudoDC vertex id of selected line
  940. Set bounding box for vertices of line.
  941. \param[in] x,y coordinates of click
  942. \param[in] thresh threshold value
  943. \return id of center, left and right vertex
  944. \return 0 no line found
  945. \return -1 on error
  946. */
  947. std::vector<int> DisplayDriver::GetSelectedVertex(double x, double y, double thresh)
  948. {
  949. int startId;
  950. int line, type;
  951. int Gid, DCid;
  952. double vx, vy, vz; // vertex screen coordinates
  953. double dist, minDist;
  954. std::vector<int> returnId;
  955. // only one object can be selected
  956. if (selected->n_values != 1 || !drawSegments)
  957. return returnId;
  958. startId = 1;
  959. line = selected->value[0];
  960. type = Vect_read_line (mapInfo, points, cats, line);
  961. minDist = 0.0;
  962. Gid = -1;
  963. // find the closest vertex (x, y)
  964. DCid = 1;
  965. for(int idx = 0; idx < points->n_points; idx++) {
  966. dist = Vect_points_distance(x, y, 0.0,
  967. points->x[idx], points->y[idx], points->z[idx], 0);
  968. if (idx == 0) {
  969. minDist = dist;
  970. Gid = idx;
  971. }
  972. else {
  973. if (minDist > dist) {
  974. minDist = dist;
  975. Gid = idx;
  976. }
  977. }
  978. Cell2Pixel(points->x[idx], points->y[idx], points->z[idx],
  979. &vx, &vy, &vz);
  980. wxRect rect (wxPoint ((int) vx, (int) vy), wxPoint ((int) vx, (int) vy));
  981. dc->SetIdBounds(DCid, rect);
  982. DCid+=2;
  983. }
  984. if (minDist > thresh)
  985. return returnId;
  986. // desc = &(ids[line]);
  987. // translate id
  988. DCid = Gid * 2 + 1;
  989. // add selected vertex
  990. returnId.push_back(DCid);
  991. // left vertex
  992. if (DCid == startId) {
  993. returnId.push_back(-1);
  994. }
  995. else {
  996. returnId.push_back(DCid - 2);
  997. }
  998. // right vertex
  999. if (DCid == (points->n_points - 1) * 2 + startId) {
  1000. returnId.push_back(-1);
  1001. }
  1002. else {
  1003. returnId.push_back(DCid + 2);
  1004. }
  1005. return returnId;
  1006. }
  1007. /**
  1008. \brief Reset topology structure.
  1009. \return
  1010. */
  1011. void DisplayDriver::ResetTopology()
  1012. {
  1013. topology.highlight = 0;
  1014. topology.point = 0;
  1015. topology.line = 0;
  1016. topology.boundaryNo = 0;
  1017. topology.boundaryOne = 0;
  1018. topology.boundaryTwo = 0;
  1019. topology.centroidIn = 0;
  1020. topology.centroidOut = 0;
  1021. topology.centroidDup = 0;
  1022. topology.nodeOne = 0;
  1023. topology.nodeTwo = 0;
  1024. topology.vertex = 0;
  1025. return;
  1026. }
  1027. /**
  1028. \brief Convert vect list to std::vector
  1029. \param list vect list
  1030. \return std::vector
  1031. */
  1032. std::vector<int> DisplayDriver::ListToVector(struct ilist *list)
  1033. {
  1034. std::vector<int> vect;
  1035. if (!list)
  1036. return vect;
  1037. for (int i = 0; i < list->n_values; i++) {
  1038. vect.push_back(list->value[i]);
  1039. }
  1040. return vect;
  1041. }
  1042. /**
  1043. \brief Convert std::vector to vect list
  1044. \param list vect list
  1045. \param vec std::vector instance
  1046. \return number of items
  1047. \return -1 on error
  1048. */
  1049. int DisplayDriver::VectorToList(struct ilist *list, const std::vector<int>& vec)
  1050. {
  1051. if (!list)
  1052. return -1;
  1053. Vect_reset_list(list);
  1054. for (std::vector<int>::const_iterator i = vec.begin(), e = vec.end();
  1055. i != e; ++i) {
  1056. Vect_list_append(list, *i);
  1057. }
  1058. return list->n_values;
  1059. }
  1060. /**
  1061. \brief Get bounding box of (opened) vector map layer
  1062. \return (w,s,b,e,n,t)
  1063. */
  1064. std::vector<double> DisplayDriver::GetMapBoundingBox()
  1065. {
  1066. std::vector<double> region;
  1067. BOUND_BOX bbox;
  1068. if (!mapInfo) {
  1069. return region;
  1070. }
  1071. Vect_get_map_box(mapInfo, &bbox);
  1072. region.push_back(bbox.W);
  1073. region.push_back(bbox.S);
  1074. region.push_back(bbox.B);
  1075. region.push_back(bbox.E);
  1076. region.push_back(bbox.N);
  1077. region.push_back(bbox.T);
  1078. return region;
  1079. }
  1080. /**
  1081. \brief Error messages handling
  1082. \param msg message
  1083. \param type type message (MSG, WARN, ERR)
  1084. \return 0
  1085. */
  1086. int print_error(const char *msg, int type)
  1087. {
  1088. fprintf(stderr, "%s", msg);
  1089. return 0;
  1090. }
  1091. /**
  1092. \brief Draw selected features
  1093. \param draw if true draw selected features
  1094. */
  1095. void DisplayDriver::DrawSelected(bool draw)
  1096. {
  1097. drawSelected = draw;
  1098. return;
  1099. }
  1100. /**
  1101. \brief Draw line direction arrow
  1102. \return number of drawn arrows
  1103. */
  1104. int DisplayDriver::DrawDirectionArrow()
  1105. {
  1106. int narrows;
  1107. int size; // arrow length in pixels
  1108. int limit; // segment length limit for drawing symbol (in pixels)
  1109. double dist, angle, pos;
  1110. double e, n, d, x0, y0, z0, x1, y1, z1;
  1111. struct line_pnts *points_seg;
  1112. wxPen *pen_arrow;
  1113. narrows = 0;
  1114. size = 5;
  1115. limit = 5; // 5px for line segment
  1116. points_seg = Vect_new_line_struct();
  1117. pen_arrow = new wxPen(settings.direction.color, settings.lineWidth, wxSOLID);
  1118. dc->SetPen(*pen_arrow);
  1119. dist = Vect_line_length(points);
  1120. if (DistanceInPixels(dist) >= limit) {
  1121. while (1) {
  1122. pos = (narrows + 1) * 8 * limit * region.map_res;
  1123. if (Vect_point_on_line(points, pos,
  1124. &e, &n, &d, NULL, NULL) < 1) {
  1125. break;
  1126. }
  1127. Cell2Pixel(e, n, d, &x0, &y0, &z0);
  1128. if (Vect_point_on_line(points, pos - 3 * size * region.map_res,
  1129. &e, &n, &d, &angle, NULL) < 1) {
  1130. break;
  1131. }
  1132. Cell2Pixel(e, n, d, &x1, &y1, &z1);
  1133. DrawArrow(x0, y0, x1, y1, angle, size);
  1134. if(narrows > 1e2) // low resolution, break
  1135. break;
  1136. narrows++;
  1137. }
  1138. // draw at least one arrow in the middle of line
  1139. if (narrows < 1) {
  1140. dist /= 2.;
  1141. if (Vect_point_on_line(points, dist,
  1142. &e, &n, &d, NULL, NULL) > 0) {
  1143. Cell2Pixel(e, n, d, &x0, &y0, &z0);
  1144. if (Vect_point_on_line(points, dist - 3 * size * region.map_res,
  1145. &e, &n, &d, &angle, NULL) > 0) {
  1146. Cell2Pixel(e, n, d, &x1, &y1, &z1);
  1147. DrawArrow(x0, y0, x1, y1, angle, size);
  1148. }
  1149. }
  1150. }
  1151. }
  1152. Vect_destroy_line_struct(points_seg);
  1153. return narrows;
  1154. }
  1155. /**
  1156. \brief Draw arrow symbol on line
  1157. \param x0,y0 arrow origin
  1158. \param x1,x1 arrow ending point (on line)
  1159. \param angle point ending point angle
  1160. \param size arrow size
  1161. \return 1
  1162. */
  1163. int DisplayDriver::DrawArrow(double x0, double y0,
  1164. double x1, double y1, double angle,
  1165. int size)
  1166. {
  1167. double x, y;
  1168. double angle_symb;
  1169. angle_symb = angle - M_PI / 2.;
  1170. x = x1 + size * std::cos(angle_symb);
  1171. y = y1 - size * std::sin(angle_symb);
  1172. dc->DrawLine((wxCoord) x, (wxCoord) y, (wxCoord) x0, (wxCoord) y0);
  1173. angle_symb = M_PI / 2. + angle;
  1174. x = x1 + size * std::cos(angle_symb);
  1175. y = y1 - size * std::sin(angle_symb);
  1176. dc->DrawLine((wxCoord) x0, (wxCoord) y0, (wxCoord) x, (wxCoord) y);
  1177. return 1;
  1178. }