driver_draw.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /**
  2. \file vdigit/driver_draw.cpp
  3. \brief wxvdigit - Display driver (draw methods)
  4. This driver is designed for wxGUI (vector digitizer) - to draw
  5. vector map layer to PseudoDC.
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. (C) 2008-2009 by Martin Landa, and the GRASS development team
  10. \author Martin Landa <landa.martin gmail.com>
  11. */
  12. #include <cmath>
  13. #include "driver.h"
  14. /**
  15. \brief Draw content of the vector map to device
  16. \return number of drawn features
  17. \return -1 on error
  18. */
  19. int DisplayDriver::DrawMap(bool force)
  20. {
  21. if (!mapInfo || !dc || !dcTmp)
  22. return -1;
  23. int nlines;
  24. struct bound_box mapBox;
  25. struct ilist *listLines;
  26. // ids.clear();
  27. listLines = Vect_new_list();
  28. ResetTopology();
  29. /* nlines = Vect_get_num_lines(mapInfo); */
  30. Vect_get_map_box(mapInfo, &mapBox);
  31. // draw lines inside of current display region
  32. nlines = Vect_select_lines_by_box(mapInfo, &(region.box),
  33. GV_POINTS | GV_LINES, // fixme
  34. listLines);
  35. G_debug(3, "wxDriver.DrawMap(): region: w=%f, e=%f, s=%f, n=%f",
  36. region.box.W, region.box.E, region.box.S, region.box.N);
  37. dc->BeginDrawing();
  38. dcTmp->BeginDrawing();
  39. if (settings.area.enabled) {
  40. /* draw area fills first */
  41. int area, centroid, isle;
  42. int num_isles;
  43. bool draw;
  44. struct ilist *listAreas, *listCentroids;
  45. struct line_pnts *points, *ipoints, **isles;
  46. wxBrush *fillArea, *fillAreaSelected, *fillIsle;
  47. fillArea = new wxBrush(settings.area.color);
  48. fillAreaSelected = new wxBrush(settings.highlight);
  49. fillIsle = new wxBrush(*wxWHITE_BRUSH);
  50. listAreas = Vect_new_list();
  51. listCentroids = Vect_new_list();
  52. points = Vect_new_line_struct();
  53. ipoints = NULL;
  54. Vect_select_areas_by_box(mapInfo, &region.box,
  55. listAreas);
  56. for (int i = 0; i < listAreas->n_values; i++) {
  57. area = listAreas->value[i];
  58. if (!Vect_area_alive (mapInfo, area))
  59. return -1;
  60. /* check for other centroids -- only area with one centroid is valid */
  61. centroid = Vect_get_area_centroid(mapInfo, area);
  62. if(centroid > 0) {
  63. /* check for isles */
  64. num_isles = Vect_get_area_num_isles(mapInfo, area); /* TODO */
  65. if (num_isles < 1)
  66. isles = NULL;
  67. else
  68. isles = (struct line_pnts **) G_malloc(num_isles * sizeof(struct line_pnts *));
  69. for (int j = 0; j < num_isles; j++) {
  70. ipoints = Vect_new_line_struct();
  71. isle = Vect_get_area_isle(mapInfo, area, j);
  72. if (!Vect_isle_alive (mapInfo, isle))
  73. return -1;
  74. Vect_get_isle_points(mapInfo, isle, ipoints);
  75. isles[j] = ipoints;
  76. }
  77. Vect_get_area_points(mapInfo, area, points);
  78. /* avoid processing areas with large number of polygon points (ugly) */
  79. if (points->n_points < 5000) {
  80. Vect_select_lines_by_polygon(mapInfo, points,
  81. num_isles, isles, GV_CENTROID, listCentroids);
  82. }
  83. else {
  84. Vect_reset_list(listCentroids);
  85. }
  86. draw = true;
  87. for (int c = 0; c < listCentroids->n_values; c++) {
  88. if(Vect_get_centroid_area(mapInfo, listCentroids->value[c]) < 0) {
  89. draw = false;
  90. break;
  91. }
  92. }
  93. if (draw) {
  94. int cat;
  95. cat = Vect_get_area_cat(mapInfo, area, 1); /* TODO: field */
  96. if (cat > -1 && IsSelected(cat, true)) {
  97. dc->SetBrush(*fillAreaSelected);
  98. }
  99. else {
  100. dc->SetBrush(*fillArea);
  101. }
  102. dc->SetPen(*wxTRANSPARENT_PEN);
  103. DrawArea(points);
  104. for (int j = 0; j < num_isles; j++) {
  105. /* draw isles in white */
  106. dc->SetBrush(*fillIsle);
  107. dc->SetPen(*wxTRANSPARENT_PEN);
  108. DrawArea(isles[j]);
  109. }
  110. }
  111. if(isles) {
  112. for (int j = 0; j < num_isles; j++) {
  113. Vect_destroy_line_struct(isles[j]);
  114. isles[j] = NULL;
  115. }
  116. G_free((void *) isles);
  117. }
  118. }
  119. }
  120. delete fillArea;
  121. delete fillIsle;
  122. Vect_destroy_line_struct(points);
  123. Vect_destroy_list(listAreas);
  124. Vect_destroy_list(listCentroids);
  125. }
  126. for (int i = 0; i < listLines->n_values; i++) {
  127. DrawLine(listLines->value[i]);
  128. }
  129. dcTmp->EndDrawing();
  130. dc->EndDrawing();
  131. /* reset list of selected features by cat
  132. -> list of ids - see IsSelected()
  133. */
  134. selected.field = -1;
  135. Vect_reset_list(selected.cats);
  136. Vect_destroy_list(listLines);
  137. return listLines->n_values;
  138. }
  139. /**
  140. \brief Draw area fill
  141. \param area boundary points
  142. \return 1 on success
  143. \return -1 on failure (vector object is dead, etc.)
  144. */
  145. int DisplayDriver::DrawArea(const line_pnts* points)
  146. {
  147. double x, y, z;
  148. // convert EN -> xy
  149. wxPoint *wxPoints = new wxPoint[points->n_points];
  150. for (int i = 0; i < points->n_points; i++) {
  151. Cell2Pixel(points->x[i], points->y[i], points->z[i],
  152. &x, &y, &z);
  153. wxPoints[i] = wxPoint((int) x, (int) y);
  154. }
  155. // draw polygon
  156. dc->DrawPolygon(points->n_points, wxPoints);
  157. delete [] wxPoints;
  158. return 1;
  159. }
  160. /**
  161. \brief Draw selected vector objects to the device
  162. \param[in] line id
  163. \return 1 on success
  164. \return -1 on failure (vector object is dead, etc.)
  165. */
  166. int DisplayDriver::DrawLine(int line)
  167. {
  168. int dcId; // 0 | 1 | segment id
  169. int type; // line type
  170. double x, y, z; // screen coordinates
  171. bool draw; // draw object ?
  172. wxPen *pen;
  173. gwxPseudoDC *pdc;
  174. pen = NULL;
  175. draw = false;
  176. if (!dc || !dcTmp || !Vect_line_alive (mapInfo, line))
  177. return -1;
  178. // read line
  179. type = Vect_read_line (mapInfo, points, cats, line);
  180. pdc = NULL;
  181. if (IsSelected(line)) { // line selected ?
  182. pdc = dcTmp;
  183. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  184. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  185. }
  186. else {
  187. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  188. }
  189. if (drawSelected) {
  190. draw = true;
  191. }
  192. else {
  193. draw = false;
  194. }
  195. dcId = 1;
  196. topology.highlight++;
  197. }
  198. else {
  199. pdc = dc;
  200. dcId = 0;
  201. if (type & GV_LINES) {
  202. switch (type) {
  203. case GV_LINE:
  204. pen = new wxPen(settings.line.color, settings.lineWidth, wxSOLID);
  205. topology.line++;
  206. draw = settings.line.enabled;
  207. break;
  208. case GV_BOUNDARY:
  209. int left, right;
  210. Vect_get_line_areas(mapInfo, line,
  211. &left, &right);
  212. if (left == 0 && right == 0) {
  213. pen = new wxPen(settings.boundaryNo.color, settings.lineWidth, wxSOLID);
  214. topology.boundaryNo++;
  215. draw = settings.boundaryNo.enabled;
  216. }
  217. else if (left > 0 && right > 0) {
  218. pen = new wxPen(settings.boundaryTwo.color, settings.lineWidth, wxSOLID);
  219. topology.boundaryTwo++;
  220. draw = settings.boundaryTwo.enabled;
  221. }
  222. else {
  223. pen = new wxPen(settings.boundaryOne.color, settings.lineWidth, wxSOLID);
  224. topology.boundaryOne++;
  225. draw = settings.boundaryOne.enabled;
  226. }
  227. break;
  228. default:
  229. draw = false;
  230. break;
  231. }
  232. }
  233. else if (type & GV_POINTS) {
  234. if (type == GV_POINT && settings.point.enabled) {
  235. pen = new wxPen(settings.point.color, settings.lineWidth, wxSOLID);
  236. topology.point++;
  237. draw = settings.point.enabled;
  238. }
  239. else if (type == GV_CENTROID) {
  240. int cret = Vect_get_centroid_area(mapInfo, line);
  241. if (cret > 0) { // -> area
  242. draw = settings.centroidIn.enabled;
  243. pen = new wxPen(settings.centroidIn.color, settings.lineWidth, wxSOLID);
  244. topology.centroidIn++;
  245. }
  246. else if (cret == 0) {
  247. draw = settings.centroidOut.enabled;
  248. pen = new wxPen(settings.centroidOut.color, settings.lineWidth, wxSOLID);
  249. topology.centroidOut++;
  250. }
  251. else {
  252. draw = settings.centroidDup.enabled;
  253. pen = new wxPen(settings.centroidDup.color, settings.lineWidth, wxSOLID);
  254. topology.centroidDup++;
  255. }
  256. }
  257. }
  258. }
  259. // clear screen points & convert EN -> xy
  260. pointsScreen->Clear();
  261. for (int i = 0; i < points->n_points; i++) {
  262. Cell2Pixel(points->x[i], points->y[i], points->z[i],
  263. &x, &y, &z);
  264. pointsScreen->Append((wxObject*) new wxPoint((int) x, (int) y)); /* TODO: 3D */
  265. }
  266. pdc->SetId(dcId); /* 0 | 1 (selected) */
  267. if (draw) {
  268. pdc->SetPen(*pen);
  269. if (type & GV_POINTS) {
  270. DrawCross(pdc, line, (const wxPoint *) pointsScreen->GetFirst()->GetData());
  271. }
  272. else {
  273. // long int startId = ids[line].startId + 1;
  274. if (dcId > 0 && drawSegments) {
  275. dcId = 2; // first segment
  276. for (size_t i = 0; i < pointsScreen->GetCount() - 1; dcId += 2) {
  277. wxPoint *point_beg = (wxPoint *) pointsScreen->Item(i)->GetData();
  278. wxPoint *point_end = (wxPoint *) pointsScreen->Item(++i)->GetData();
  279. // set bounds for line
  280. // wxRect rect (*point_beg, *point_end);
  281. // dc->SetIdBounds(startId, rect);
  282. pdc->SetId(dcId); // set unique id & set bbox for each segment
  283. pdc->SetPen(*pen);
  284. wxRect rect (*point_beg, *point_end);
  285. pdc->SetIdBounds(dcId, rect);
  286. pdc->DrawLine(point_beg->x, point_beg->y,
  287. point_end->x, point_end->y);
  288. }
  289. }
  290. else {
  291. wxPoint *wxPoints = new wxPoint[pointsScreen->GetCount()];
  292. for (size_t i = 0; i < pointsScreen->GetCount(); i++) {
  293. wxPoint *point_beg = (wxPoint *) pointsScreen->Item(i)->GetData();
  294. wxPoints[i] = *point_beg;
  295. }
  296. pdc->DrawLines(pointsScreen->GetCount(), wxPoints);
  297. delete [] wxPoints;
  298. if (!IsSelected(line) && settings.direction.enabled) {
  299. DrawDirectionArrow();
  300. // restore pen
  301. pdc->SetPen(*pen);
  302. }
  303. }
  304. }
  305. }
  306. if (type & GV_LINES) {
  307. DrawLineVerteces(line); // draw vertices
  308. DrawLineNodes(line); // draw nodes
  309. }
  310. delete pen;
  311. return 1;
  312. }
  313. /**
  314. \brief Draw line verteces to the device
  315. Except of first and last vertex, see DrawLineNodes().
  316. \param line id
  317. \return number of verteces which were drawn
  318. \return -1 if drawing vertices is disabled
  319. */
  320. int DisplayDriver::DrawLineVerteces(int line)
  321. {
  322. int dcId;
  323. wxPoint *point;
  324. wxPen *pen;
  325. gwxPseudoDC *pdc;
  326. if (!IsSelected(line) && !settings.vertex.enabled)
  327. return -1;
  328. pdc = NULL;
  329. // determine color
  330. if (!IsSelected(line)) {
  331. pdc = dc;
  332. pen = new wxPen(settings.vertex.color, settings.lineWidth, wxSOLID);
  333. dcId = 0;
  334. }
  335. else {
  336. pdc = dcTmp;
  337. if (!drawSelected) {
  338. return -1;
  339. }
  340. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  341. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  342. }
  343. else {
  344. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  345. }
  346. if (drawSegments) {
  347. dcId = 3; // first vertex
  348. }
  349. else {
  350. dcId = 1;
  351. }
  352. }
  353. pdc->SetId(dcId); /* 0 | 1 (selected) */
  354. pdc->SetPen(*pen);
  355. for (size_t i = 1; i < pointsScreen->GetCount() - 1; i++, dcId += 2) {
  356. point = (wxPoint*) pointsScreen->Item(i)->GetData();
  357. if (IsSelected(line) && drawSegments) {
  358. pdc->SetId(dcId);
  359. pdc->SetPen(*pen);
  360. wxRect rect (*point, *point);
  361. pdc->SetIdBounds(dcId, rect);
  362. }
  363. if (settings.vertex.enabled) {
  364. DrawCross(pdc, line, (const wxPoint*) pointsScreen->Item(i)->GetData());
  365. topology.vertex++;
  366. }
  367. }
  368. delete pen;
  369. return pointsScreen->GetCount() - 2;
  370. }
  371. /**
  372. \brief Draw line nodes to the device
  373. \param line id
  374. \return 1
  375. \return -1 if no nodes were drawn
  376. */
  377. int DisplayDriver::DrawLineNodes(int line)
  378. {
  379. int dcId;
  380. int node;
  381. double east, north, depth;
  382. double x, y, z;
  383. int nodes [2];
  384. bool draw;
  385. wxPen *pen;
  386. gwxPseudoDC *pdc;
  387. pdc = NULL;
  388. // draw nodes??
  389. if (!settings.nodeOne.enabled && !settings.nodeTwo.enabled)
  390. return -1;
  391. // get nodes
  392. Vect_get_line_nodes(mapInfo, line, &(nodes[0]), &(nodes[1]));
  393. for (size_t i = 0; i < sizeof(nodes) / sizeof(int); i++) {
  394. node = nodes[i];
  395. // get coordinates
  396. Vect_get_node_coor(mapInfo, node,
  397. &east, &north, &depth);
  398. // convert EN->xy
  399. Cell2Pixel(east, north, depth,
  400. &x, &y, &z);
  401. // determine color
  402. if (IsSelected(line)) {
  403. pdc = dcTmp;
  404. if (!drawSelected) {
  405. return -1;
  406. }
  407. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  408. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  409. }
  410. else {
  411. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  412. }
  413. draw = true;
  414. if (!drawSegments) {
  415. dcId = 1;
  416. }
  417. else {
  418. // node1, line1, vertex1, line2, vertex2, ..., node2
  419. if (i == 0) // first node
  420. dcId = 1;
  421. else // last node
  422. dcId = 2 * points->n_points - 1;
  423. }
  424. }
  425. else {
  426. pdc = dc;
  427. dcId = 0;
  428. if (Vect_get_node_n_lines(mapInfo, node) == 1) {
  429. pen = new wxPen(settings.nodeOne.color, settings.lineWidth, wxSOLID);
  430. topology.nodeOne++;
  431. draw = settings.nodeOne.enabled;
  432. }
  433. else {
  434. pen = new wxPen(settings.nodeTwo.color, settings.lineWidth, wxSOLID);
  435. topology.nodeTwo++;
  436. draw = settings.nodeTwo.enabled;
  437. }
  438. }
  439. wxPoint point((int) x, (int) y);
  440. if (IsSelected(line) && drawSegments) {
  441. wxRect rect (point, point);
  442. pdc->SetIdBounds(dcId, rect);
  443. }
  444. // draw node if needed
  445. if (draw) {
  446. pdc->SetId(dcId);
  447. pdc->SetPen(*pen);
  448. DrawCross(pdc, line, &point);
  449. }
  450. }
  451. delete pen;
  452. return 1;
  453. }
  454. /**
  455. \brief Draw cross symbol of given size to device content
  456. Used for points, nodes, vertices
  457. \param[in,out] PseudoDC where to draw
  458. \param[in] point coordinates of center
  459. \param[in] size size of the cross symbol
  460. \return 1 on success
  461. \return -1 on failure
  462. */
  463. int DisplayDriver::DrawCross(gwxPseudoDC *pdc, int line, const wxPoint* point, int size)
  464. {
  465. if (!pdc || !point)
  466. return -1;
  467. pdc->DrawLine(point->x - size, point->y, point->x + size, point->y);
  468. pdc->DrawLine(point->x, point->y - size, point->x, point->y + size);
  469. return 1;
  470. }
  471. /**
  472. \brief Draw selected features
  473. \param draw if true draw selected features
  474. */
  475. void DisplayDriver::DrawSelected(bool draw)
  476. {
  477. drawSelected = draw;
  478. return;
  479. }
  480. /**
  481. \brief Draw line direction arrow
  482. \return number of drawn arrows
  483. */
  484. int DisplayDriver::DrawDirectionArrow()
  485. {
  486. int narrows;
  487. int size; // arrow length in pixels
  488. int limit; // segment length limit for drawing symbol (in pixels)
  489. double dist, angle, pos;
  490. double e, n, d, x0, y0, z0, x1, y1, z1;
  491. struct line_pnts *points_seg;
  492. wxPen *pen_arrow;
  493. narrows = 0;
  494. size = 5;
  495. limit = 5; // 5px for line segment
  496. points_seg = Vect_new_line_struct();
  497. pen_arrow = new wxPen(settings.direction.color, settings.lineWidth, wxSOLID);
  498. dc->SetPen(*pen_arrow);
  499. dist = Vect_line_length(points);
  500. if (DistanceInPixels(dist) >= limit) {
  501. while (1) {
  502. pos = (narrows + 1) * 8 * limit * region.map_res;
  503. if (Vect_point_on_line(points, pos,
  504. &e, &n, &d, NULL, NULL) < 1) {
  505. break;
  506. }
  507. Cell2Pixel(e, n, d, &x0, &y0, &z0);
  508. if (Vect_point_on_line(points, pos - 3 * size * region.map_res,
  509. &e, &n, &d, &angle, NULL) < 1) {
  510. break;
  511. }
  512. Cell2Pixel(e, n, d, &x1, &y1, &z1);
  513. DrawArrow(x0, y0, x1, y1, angle, size);
  514. if(narrows > 1e2) // low resolution, break
  515. break;
  516. narrows++;
  517. }
  518. // draw at least one arrow in the middle of line
  519. if (narrows < 1) {
  520. dist /= 2.;
  521. if (Vect_point_on_line(points, dist,
  522. &e, &n, &d, NULL, NULL) > 0) {
  523. Cell2Pixel(e, n, d, &x0, &y0, &z0);
  524. if (Vect_point_on_line(points, dist - 3 * size * region.map_res,
  525. &e, &n, &d, &angle, NULL) > 0) {
  526. Cell2Pixel(e, n, d, &x1, &y1, &z1);
  527. DrawArrow(x0, y0, x1, y1, angle, size);
  528. }
  529. }
  530. }
  531. }
  532. Vect_destroy_line_struct(points_seg);
  533. return narrows;
  534. }
  535. /**
  536. \brief Draw arrow symbol on line
  537. \param x0,y0 arrow origin
  538. \param x1,x1 arrow ending point (on line)
  539. \param angle point ending point angle
  540. \param size arrow size
  541. \return 1
  542. */
  543. int DisplayDriver::DrawArrow(double x0, double y0,
  544. double x1, double y1, double angle,
  545. int size)
  546. {
  547. double x, y;
  548. double angle_symb;
  549. angle_symb = angle - M_PI / 2.;
  550. x = x1 + size * cos(angle_symb);
  551. y = y1 - size * sin(angle_symb);
  552. dc->DrawLine((wxCoord) x, (wxCoord) y, (wxCoord) x0, (wxCoord) y0);
  553. angle_symb = M_PI / 2. + angle;
  554. x = x1 + size * cos(angle_symb);
  555. y = y1 - size * sin(angle_symb);
  556. dc->DrawLine((wxCoord) x0, (wxCoord) y0, (wxCoord) x, (wxCoord) y);
  557. return 1;
  558. }