driver_draw.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /**
  2. \file driver_draw.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 Draw content of the vector map to device
  17. \return number of drawn features
  18. \return -1 on error
  19. */
  20. int DisplayDriver::DrawMap(bool force)
  21. {
  22. if (!mapInfo || !dc || !dcTmp)
  23. return -1;
  24. int nlines;
  25. BOUND_BOX mapBox;
  26. struct ilist *listLines;
  27. // ids.clear();
  28. listLines = Vect_new_list();
  29. ResetTopology();
  30. /* nlines = Vect_get_num_lines(mapInfo); */
  31. Vect_get_map_box(mapInfo, &mapBox);
  32. // draw lines inside of current display region
  33. nlines = Vect_select_lines_by_box(mapInfo, &(region.box),
  34. GV_POINTS | GV_LINES, // fixme
  35. listLines);
  36. G_debug(3, "wxDriver.DrawMap(): region: w=%f, e=%f, s=%f, n=%f",
  37. region.box.W, region.box.E, region.box.S, region.box.N);
  38. dc->BeginDrawing();
  39. dcTmp->BeginDrawing();
  40. if (settings.area.enabled) {
  41. /* draw area fills first */
  42. int area, centroid, isle;
  43. int num_isles;
  44. bool draw;
  45. struct ilist *listAreas, *listCentroids;
  46. struct line_pnts *points, *ipoints, **isles;
  47. wxBrush *fillArea, *fillAreaSelected, *fillIsle;
  48. fillArea = new wxBrush(settings.area.color);
  49. fillAreaSelected = new wxBrush(settings.highlight);
  50. fillIsle = new wxBrush(*wxWHITE_BRUSH);
  51. listAreas = Vect_new_list();
  52. listCentroids = Vect_new_list();
  53. points = Vect_new_line_struct();
  54. ipoints = NULL;
  55. Vect_select_areas_by_box(mapInfo, &region.box,
  56. listAreas);
  57. for (int i = 0; i < listAreas->n_values; i++) {
  58. area = listAreas->value[i];
  59. if (!Vect_area_alive (mapInfo, area))
  60. return -1;
  61. /* check for other centroids -- only area with one centroid is valid */
  62. centroid = Vect_get_area_centroid(mapInfo, area);
  63. if(centroid > 0) {
  64. /* check for isles */
  65. num_isles = Vect_get_area_num_isles(mapInfo, area); /* TODO */
  66. if (num_isles < 1)
  67. isles = NULL;
  68. else
  69. isles = (struct line_pnts **) G_malloc(num_isles * sizeof(struct line_pnts *));
  70. for (int j = 0; j < num_isles; j++) {
  71. ipoints = Vect_new_line_struct();
  72. isle = Vect_get_area_isle(mapInfo, area, j);
  73. if (!Vect_isle_alive (mapInfo, isle))
  74. return -1;
  75. Vect_get_isle_points(mapInfo, isle, ipoints);
  76. isles[j] = ipoints;
  77. }
  78. Vect_get_area_points(mapInfo, area, points);
  79. /* avoid processing areas with large number of polygon points (ugly) */
  80. if (points->n_points < 5000) {
  81. Vect_select_lines_by_polygon(mapInfo, points,
  82. num_isles, isles, GV_CENTROID, listCentroids);
  83. }
  84. else {
  85. Vect_reset_list(listCentroids);
  86. }
  87. draw = true;
  88. for (int c = 0; c < listCentroids->n_values; c++) {
  89. if(Vect_get_centroid_area(mapInfo, listCentroids->value[c]) < 0) {
  90. draw = false;
  91. break;
  92. }
  93. }
  94. if (draw) {
  95. int cat;
  96. cat = Vect_get_area_cat(mapInfo, area, 1); /* TODO: field */
  97. if (cat > -1 && IsSelected(cat, true)) {
  98. dc->SetBrush(*fillAreaSelected);
  99. }
  100. else {
  101. dc->SetBrush(*fillArea);
  102. }
  103. dc->SetPen(*wxTRANSPARENT_PEN);
  104. DrawArea(points);
  105. for (int j = 0; j < num_isles; j++) {
  106. /* draw isles in white */
  107. dc->SetBrush(*fillIsle);
  108. dc->SetPen(*wxTRANSPARENT_PEN);
  109. DrawArea(isles[j]);
  110. }
  111. }
  112. if(isles) {
  113. for (int j = 0; j < num_isles; j++) {
  114. Vect_destroy_line_struct(isles[j]);
  115. isles[j] = NULL;
  116. }
  117. G_free((void *) isles);
  118. }
  119. }
  120. }
  121. delete fillArea;
  122. delete fillIsle;
  123. Vect_destroy_line_struct(points);
  124. Vect_destroy_list(listAreas);
  125. Vect_destroy_list(listCentroids);
  126. }
  127. for (int i = 0; i < listLines->n_values; i++) {
  128. DrawLine(listLines->value[i]);
  129. }
  130. dcTmp->EndDrawing();
  131. dc->EndDrawing();
  132. /* reset list of selected features by cat
  133. -> list of ids - see IsSelected()
  134. */
  135. selected.field = -1;
  136. Vect_reset_list(selected.cats);
  137. Vect_destroy_list(listLines);
  138. return listLines->n_values;
  139. }
  140. /**
  141. \brief Draw area fill
  142. \param area boundary points
  143. \return 1 on success
  144. \return -1 on failure (vector object is dead, etc.)
  145. */
  146. int DisplayDriver::DrawArea(const line_pnts* points)
  147. {
  148. double x, y, z;
  149. // convert EN -> xy
  150. wxPoint wxPoints[points->n_points];
  151. for (int i = 0; i < points->n_points; i++) {
  152. Cell2Pixel(points->x[i], points->y[i], points->z[i],
  153. &x, &y, &z);
  154. wxPoints[i] = wxPoint((int) x, (int) y);
  155. }
  156. // draw polygon
  157. dc->DrawPolygon(points->n_points, 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. wxPseudoDC *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[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. if (!IsSelected(line) && settings.direction.enabled) {
  298. DrawDirectionArrow();
  299. // restore pen
  300. pdc->SetPen(*pen);
  301. }
  302. }
  303. }
  304. }
  305. if (type & GV_LINES) {
  306. DrawLineVerteces(line); // draw vertices
  307. DrawLineNodes(line); // draw nodes
  308. }
  309. delete pen;
  310. return 1;
  311. }
  312. /**
  313. \brief Draw line verteces to the device
  314. Except of first and last vertex, see DrawLineNodes().
  315. \param line id
  316. \return number of verteces which were drawn
  317. \return -1 if drawing vertices is disabled
  318. */
  319. int DisplayDriver::DrawLineVerteces(int line)
  320. {
  321. int dcId;
  322. wxPoint *point;
  323. wxPen *pen;
  324. wxPseudoDC *pdc;
  325. if (!IsSelected(line) && !settings.vertex.enabled)
  326. return -1;
  327. pdc = NULL;
  328. // determine color
  329. if (!IsSelected(line)) {
  330. pdc = dc;
  331. pen = new wxPen(settings.vertex.color, settings.lineWidth, wxSOLID);
  332. dcId = 0;
  333. }
  334. else {
  335. pdc = dcTmp;
  336. if (!drawSelected) {
  337. return -1;
  338. }
  339. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  340. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  341. }
  342. else {
  343. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  344. }
  345. if (drawSegments) {
  346. dcId = 3; // first vertex
  347. }
  348. else {
  349. dcId = 1;
  350. }
  351. }
  352. pdc->SetId(dcId); /* 0 | 1 (selected) */
  353. pdc->SetPen(*pen);
  354. for (size_t i = 1; i < pointsScreen->GetCount() - 1; i++, dcId += 2) {
  355. point = (wxPoint*) pointsScreen->Item(i)->GetData();
  356. if (IsSelected(line) && drawSegments) {
  357. pdc->SetId(dcId);
  358. pdc->SetPen(*pen);
  359. wxRect rect (*point, *point);
  360. pdc->SetIdBounds(dcId, rect);
  361. }
  362. if (settings.vertex.enabled) {
  363. DrawCross(pdc, line, (const wxPoint*) pointsScreen->Item(i)->GetData());
  364. topology.vertex++;
  365. }
  366. }
  367. delete pen;
  368. return pointsScreen->GetCount() - 2;
  369. }
  370. /**
  371. \brief Draw line nodes to the device
  372. \param line id
  373. \return 1
  374. \return -1 if no nodes were drawn
  375. */
  376. int DisplayDriver::DrawLineNodes(int line)
  377. {
  378. int dcId;
  379. int node;
  380. double east, north, depth;
  381. double x, y, z;
  382. int nodes [2];
  383. bool draw;
  384. wxPen *pen;
  385. wxPseudoDC *pdc;
  386. pdc = NULL;
  387. // draw nodes??
  388. if (!settings.nodeOne.enabled && !settings.nodeTwo.enabled)
  389. return -1;
  390. // get nodes
  391. Vect_get_line_nodes(mapInfo, line, &(nodes[0]), &(nodes[1]));
  392. for (size_t i = 0; i < sizeof(nodes) / sizeof(int); i++) {
  393. node = nodes[i];
  394. // get coordinates
  395. Vect_get_node_coor(mapInfo, node,
  396. &east, &north, &depth);
  397. // convert EN->xy
  398. Cell2Pixel(east, north, depth,
  399. &x, &y, &z);
  400. // determine color
  401. if (IsSelected(line)) {
  402. pdc = dcTmp;
  403. if (!drawSelected) {
  404. return -1;
  405. }
  406. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  407. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  408. }
  409. else {
  410. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  411. }
  412. draw = true;
  413. if (!drawSegments) {
  414. dcId = 1;
  415. }
  416. else {
  417. // node1, line1, vertex1, line2, vertex2, ..., node2
  418. if (i == 0) // first node
  419. dcId = 1;
  420. else // last node
  421. dcId = 2 * points->n_points - 1;
  422. }
  423. }
  424. else {
  425. pdc = dc;
  426. dcId = 0;
  427. if (Vect_get_node_n_lines(mapInfo, node) == 1) {
  428. pen = new wxPen(settings.nodeOne.color, settings.lineWidth, wxSOLID);
  429. topology.nodeOne++;
  430. draw = settings.nodeOne.enabled;
  431. }
  432. else {
  433. pen = new wxPen(settings.nodeTwo.color, settings.lineWidth, wxSOLID);
  434. topology.nodeTwo++;
  435. draw = settings.nodeTwo.enabled;
  436. }
  437. }
  438. wxPoint point((int) x, (int) y);
  439. if (IsSelected(line) && drawSegments) {
  440. wxRect rect (point, point);
  441. pdc->SetIdBounds(dcId, rect);
  442. }
  443. // draw node if needed
  444. if (draw) {
  445. pdc->SetId(dcId);
  446. pdc->SetPen(*pen);
  447. DrawCross(pdc, line, &point);
  448. }
  449. }
  450. delete pen;
  451. return 1;
  452. }
  453. /**
  454. \brief Draw cross symbol of given size to device content
  455. Used for points, nodes, vertices
  456. \param[in,out] PseudoDC where to draw
  457. \param[in] point coordinates of center
  458. \param[in] size size of the cross symbol
  459. \return 1 on success
  460. \return -1 on failure
  461. */
  462. int DisplayDriver::DrawCross(wxPseudoDC *pdc, int line, const wxPoint* point, int size)
  463. {
  464. if (!pdc || !point)
  465. return -1;
  466. pdc->DrawLine(point->x - size, point->y, point->x + size, point->y);
  467. pdc->DrawLine(point->x, point->y - size, point->x, point->y + size);
  468. return 1;
  469. }
  470. /**
  471. \brief Draw selected features
  472. \param draw if true draw selected features
  473. */
  474. void DisplayDriver::DrawSelected(bool draw)
  475. {
  476. drawSelected = draw;
  477. return;
  478. }
  479. /**
  480. \brief Draw line direction arrow
  481. \return number of drawn arrows
  482. */
  483. int DisplayDriver::DrawDirectionArrow()
  484. {
  485. int narrows;
  486. int size; // arrow length in pixels
  487. int limit; // segment length limit for drawing symbol (in pixels)
  488. double dist, angle, pos;
  489. double e, n, d, x0, y0, z0, x1, y1, z1;
  490. struct line_pnts *points_seg;
  491. wxPen *pen_arrow;
  492. narrows = 0;
  493. size = 5;
  494. limit = 5; // 5px for line segment
  495. points_seg = Vect_new_line_struct();
  496. pen_arrow = new wxPen(settings.direction.color, settings.lineWidth, wxSOLID);
  497. dc->SetPen(*pen_arrow);
  498. dist = Vect_line_length(points);
  499. if (DistanceInPixels(dist) >= limit) {
  500. while (1) {
  501. pos = (narrows + 1) * 8 * limit * region.map_res;
  502. if (Vect_point_on_line(points, pos,
  503. &e, &n, &d, NULL, NULL) < 1) {
  504. break;
  505. }
  506. Cell2Pixel(e, n, d, &x0, &y0, &z0);
  507. if (Vect_point_on_line(points, pos - 3 * size * region.map_res,
  508. &e, &n, &d, &angle, NULL) < 1) {
  509. break;
  510. }
  511. Cell2Pixel(e, n, d, &x1, &y1, &z1);
  512. DrawArrow(x0, y0, x1, y1, angle, size);
  513. if(narrows > 1e2) // low resolution, break
  514. break;
  515. narrows++;
  516. }
  517. // draw at least one arrow in the middle of line
  518. if (narrows < 1) {
  519. dist /= 2.;
  520. if (Vect_point_on_line(points, dist,
  521. &e, &n, &d, NULL, NULL) > 0) {
  522. Cell2Pixel(e, n, d, &x0, &y0, &z0);
  523. if (Vect_point_on_line(points, dist - 3 * size * region.map_res,
  524. &e, &n, &d, &angle, NULL) > 0) {
  525. Cell2Pixel(e, n, d, &x1, &y1, &z1);
  526. DrawArrow(x0, y0, x1, y1, angle, size);
  527. }
  528. }
  529. }
  530. }
  531. Vect_destroy_line_struct(points_seg);
  532. return narrows;
  533. }
  534. /**
  535. \brief Draw arrow symbol on line
  536. \param x0,y0 arrow origin
  537. \param x1,x1 arrow ending point (on line)
  538. \param angle point ending point angle
  539. \param size arrow size
  540. \return 1
  541. */
  542. int DisplayDriver::DrawArrow(double x0, double y0,
  543. double x1, double y1, double angle,
  544. int size)
  545. {
  546. double x, y;
  547. double angle_symb;
  548. angle_symb = angle - M_PI / 2.;
  549. x = x1 + size * std::cos(angle_symb);
  550. y = y1 - size * std::sin(angle_symb);
  551. dc->DrawLine((wxCoord) x, (wxCoord) y, (wxCoord) x0, (wxCoord) y0);
  552. angle_symb = M_PI / 2. + angle;
  553. x = x1 + size * std::cos(angle_symb);
  554. y = y1 - size * std::sin(angle_symb);
  555. dc->DrawLine((wxCoord) x0, (wxCoord) y0, (wxCoord) x, (wxCoord) y);
  556. return 1;
  557. }