driver.cpp 34 KB

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