driver_draw.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. 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[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. return 1;
  158. }
  159. /**
  160. \brief Draw selected vector objects to the device
  161. \param[in] line id
  162. \return 1 on success
  163. \return -1 on failure (vector object is dead, etc.)
  164. */
  165. int DisplayDriver::DrawLine(int line)
  166. {
  167. int dcId; // 0 | 1 | segment id
  168. int type; // line type
  169. double x, y, z; // screen coordinates
  170. bool draw; // draw object ?
  171. wxPen *pen;
  172. wxPseudoDC *pdc;
  173. pen = NULL;
  174. draw = false;
  175. if (!dc || !dcTmp || !Vect_line_alive (mapInfo, line))
  176. return -1;
  177. // read line
  178. type = Vect_read_line (mapInfo, points, cats, line);
  179. pdc = NULL;
  180. if (IsSelected(line)) { // line selected ?
  181. pdc = dcTmp;
  182. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  183. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  184. }
  185. else {
  186. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  187. }
  188. if (drawSelected) {
  189. draw = true;
  190. }
  191. else {
  192. draw = false;
  193. }
  194. dcId = 1;
  195. topology.highlight++;
  196. }
  197. else {
  198. pdc = dc;
  199. dcId = 0;
  200. if (type & GV_LINES) {
  201. switch (type) {
  202. case GV_LINE:
  203. pen = new wxPen(settings.line.color, settings.lineWidth, wxSOLID);
  204. topology.line++;
  205. draw = settings.line.enabled;
  206. break;
  207. case GV_BOUNDARY:
  208. int left, right;
  209. Vect_get_line_areas(mapInfo, line,
  210. &left, &right);
  211. if (left == 0 && right == 0) {
  212. pen = new wxPen(settings.boundaryNo.color, settings.lineWidth, wxSOLID);
  213. topology.boundaryNo++;
  214. draw = settings.boundaryNo.enabled;
  215. }
  216. else if (left > 0 && right > 0) {
  217. pen = new wxPen(settings.boundaryTwo.color, settings.lineWidth, wxSOLID);
  218. topology.boundaryTwo++;
  219. draw = settings.boundaryTwo.enabled;
  220. }
  221. else {
  222. pen = new wxPen(settings.boundaryOne.color, settings.lineWidth, wxSOLID);
  223. topology.boundaryOne++;
  224. draw = settings.boundaryOne.enabled;
  225. }
  226. break;
  227. default:
  228. draw = false;
  229. break;
  230. }
  231. }
  232. else if (type & GV_POINTS) {
  233. if (type == GV_POINT && settings.point.enabled) {
  234. pen = new wxPen(settings.point.color, settings.lineWidth, wxSOLID);
  235. topology.point++;
  236. draw = settings.point.enabled;
  237. }
  238. else if (type == GV_CENTROID) {
  239. int cret = Vect_get_centroid_area(mapInfo, line);
  240. if (cret > 0) { // -> area
  241. draw = settings.centroidIn.enabled;
  242. pen = new wxPen(settings.centroidIn.color, settings.lineWidth, wxSOLID);
  243. topology.centroidIn++;
  244. }
  245. else if (cret == 0) {
  246. draw = settings.centroidOut.enabled;
  247. pen = new wxPen(settings.centroidOut.color, settings.lineWidth, wxSOLID);
  248. topology.centroidOut++;
  249. }
  250. else {
  251. draw = settings.centroidDup.enabled;
  252. pen = new wxPen(settings.centroidDup.color, settings.lineWidth, wxSOLID);
  253. topology.centroidDup++;
  254. }
  255. }
  256. }
  257. }
  258. // clear screen points & convert EN -> xy
  259. pointsScreen->Clear();
  260. for (int i = 0; i < points->n_points; i++) {
  261. Cell2Pixel(points->x[i], points->y[i], points->z[i],
  262. &x, &y, &z);
  263. pointsScreen->Append((wxObject*) new wxPoint((int) x, (int) y)); /* TODO: 3D */
  264. }
  265. pdc->SetId(dcId); /* 0 | 1 (selected) */
  266. if (draw) {
  267. pdc->SetPen(*pen);
  268. if (type & GV_POINTS) {
  269. DrawCross(pdc, line, (const wxPoint *) pointsScreen->GetFirst()->GetData());
  270. }
  271. else {
  272. // long int startId = ids[line].startId + 1;
  273. if (dcId > 0 && drawSegments) {
  274. dcId = 2; // first segment
  275. for (size_t i = 0; i < pointsScreen->GetCount() - 1; dcId += 2) {
  276. wxPoint *point_beg = (wxPoint *) pointsScreen->Item(i)->GetData();
  277. wxPoint *point_end = (wxPoint *) pointsScreen->Item(++i)->GetData();
  278. // set bounds for line
  279. // wxRect rect (*point_beg, *point_end);
  280. // dc->SetIdBounds(startId, rect);
  281. pdc->SetId(dcId); // set unique id & set bbox for each segment
  282. pdc->SetPen(*pen);
  283. wxRect rect (*point_beg, *point_end);
  284. pdc->SetIdBounds(dcId, rect);
  285. pdc->DrawLine(point_beg->x, point_beg->y,
  286. point_end->x, point_end->y);
  287. }
  288. }
  289. else {
  290. wxPoint wxPoints[pointsScreen->GetCount()];
  291. for (size_t i = 0; i < pointsScreen->GetCount(); i++) {
  292. wxPoint *point_beg = (wxPoint *) pointsScreen->Item(i)->GetData();
  293. wxPoints[i] = *point_beg;
  294. }
  295. pdc->DrawLines(pointsScreen->GetCount(), wxPoints);
  296. if (!IsSelected(line) && settings.direction.enabled) {
  297. DrawDirectionArrow();
  298. // restore pen
  299. pdc->SetPen(*pen);
  300. }
  301. }
  302. }
  303. }
  304. if (type & GV_LINES) {
  305. DrawLineVerteces(line); // draw vertices
  306. DrawLineNodes(line); // draw nodes
  307. }
  308. delete pen;
  309. return 1;
  310. }
  311. /**
  312. \brief Draw line verteces to the device
  313. Except of first and last vertex, see DrawLineNodes().
  314. \param line id
  315. \return number of verteces which were drawn
  316. \return -1 if drawing vertices is disabled
  317. */
  318. int DisplayDriver::DrawLineVerteces(int line)
  319. {
  320. int dcId;
  321. wxPoint *point;
  322. wxPen *pen;
  323. wxPseudoDC *pdc;
  324. if (!IsSelected(line) && !settings.vertex.enabled)
  325. return -1;
  326. pdc = NULL;
  327. // determine color
  328. if (!IsSelected(line)) {
  329. pdc = dc;
  330. pen = new wxPen(settings.vertex.color, settings.lineWidth, wxSOLID);
  331. dcId = 0;
  332. }
  333. else {
  334. pdc = dcTmp;
  335. if (!drawSelected) {
  336. return -1;
  337. }
  338. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  339. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  340. }
  341. else {
  342. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  343. }
  344. if (drawSegments) {
  345. dcId = 3; // first vertex
  346. }
  347. else {
  348. dcId = 1;
  349. }
  350. }
  351. pdc->SetId(dcId); /* 0 | 1 (selected) */
  352. pdc->SetPen(*pen);
  353. for (size_t i = 1; i < pointsScreen->GetCount() - 1; i++, dcId += 2) {
  354. point = (wxPoint*) pointsScreen->Item(i)->GetData();
  355. if (IsSelected(line) && drawSegments) {
  356. pdc->SetId(dcId);
  357. pdc->SetPen(*pen);
  358. wxRect rect (*point, *point);
  359. pdc->SetIdBounds(dcId, rect);
  360. }
  361. if (settings.vertex.enabled) {
  362. DrawCross(pdc, line, (const wxPoint*) pointsScreen->Item(i)->GetData());
  363. topology.vertex++;
  364. }
  365. }
  366. delete pen;
  367. return pointsScreen->GetCount() - 2;
  368. }
  369. /**
  370. \brief Draw line nodes to the device
  371. \param line id
  372. \return 1
  373. \return -1 if no nodes were drawn
  374. */
  375. int DisplayDriver::DrawLineNodes(int line)
  376. {
  377. int dcId;
  378. int node;
  379. double east, north, depth;
  380. double x, y, z;
  381. int nodes [2];
  382. bool draw;
  383. wxPen *pen;
  384. wxPseudoDC *pdc;
  385. pdc = NULL;
  386. // draw nodes??
  387. if (!settings.nodeOne.enabled && !settings.nodeTwo.enabled)
  388. return -1;
  389. // get nodes
  390. Vect_get_line_nodes(mapInfo, line, &(nodes[0]), &(nodes[1]));
  391. for (size_t i = 0; i < sizeof(nodes) / sizeof(int); i++) {
  392. node = nodes[i];
  393. // get coordinates
  394. Vect_get_node_coor(mapInfo, node,
  395. &east, &north, &depth);
  396. // convert EN->xy
  397. Cell2Pixel(east, north, depth,
  398. &x, &y, &z);
  399. // determine color
  400. if (IsSelected(line)) {
  401. pdc = dcTmp;
  402. if (!drawSelected) {
  403. return -1;
  404. }
  405. if (settings.highlightDupl.enabled && IsDuplicated(line)) {
  406. pen = new wxPen(settings.highlightDupl.color, settings.lineWidth, wxSOLID);
  407. }
  408. else {
  409. pen = new wxPen(settings.highlight, settings.lineWidth, wxSOLID);
  410. }
  411. draw = true;
  412. if (!drawSegments) {
  413. dcId = 1;
  414. }
  415. else {
  416. // node1, line1, vertex1, line2, vertex2, ..., node2
  417. if (i == 0) // first node
  418. dcId = 1;
  419. else // last node
  420. dcId = 2 * points->n_points - 1;
  421. }
  422. }
  423. else {
  424. pdc = dc;
  425. dcId = 0;
  426. if (Vect_get_node_n_lines(mapInfo, node) == 1) {
  427. pen = new wxPen(settings.nodeOne.color, settings.lineWidth, wxSOLID);
  428. topology.nodeOne++;
  429. draw = settings.nodeOne.enabled;
  430. }
  431. else {
  432. pen = new wxPen(settings.nodeTwo.color, settings.lineWidth, wxSOLID);
  433. topology.nodeTwo++;
  434. draw = settings.nodeTwo.enabled;
  435. }
  436. }
  437. wxPoint point((int) x, (int) y);
  438. if (IsSelected(line) && drawSegments) {
  439. wxRect rect (point, point);
  440. pdc->SetIdBounds(dcId, rect);
  441. }
  442. // draw node if needed
  443. if (draw) {
  444. pdc->SetId(dcId);
  445. pdc->SetPen(*pen);
  446. DrawCross(pdc, line, &point);
  447. }
  448. }
  449. delete pen;
  450. return 1;
  451. }
  452. /**
  453. \brief Draw cross symbol of given size to device content
  454. Used for points, nodes, vertices
  455. \param[in,out] PseudoDC where to draw
  456. \param[in] point coordinates of center
  457. \param[in] size size of the cross symbol
  458. \return 1 on success
  459. \return -1 on failure
  460. */
  461. int DisplayDriver::DrawCross(wxPseudoDC *pdc, int line, const wxPoint* point, int size)
  462. {
  463. if (!pdc || !point)
  464. return -1;
  465. pdc->DrawLine(point->x - size, point->y, point->x + size, point->y);
  466. pdc->DrawLine(point->x, point->y - size, point->x, point->y + size);
  467. return 1;
  468. }
  469. /**
  470. \brief Draw selected features
  471. \param draw if true draw selected features
  472. */
  473. void DisplayDriver::DrawSelected(bool draw)
  474. {
  475. drawSelected = draw;
  476. return;
  477. }
  478. /**
  479. \brief Draw line direction arrow
  480. \return number of drawn arrows
  481. */
  482. int DisplayDriver::DrawDirectionArrow()
  483. {
  484. int narrows;
  485. int size; // arrow length in pixels
  486. int limit; // segment length limit for drawing symbol (in pixels)
  487. double dist, angle, pos;
  488. double e, n, d, x0, y0, z0, x1, y1, z1;
  489. struct line_pnts *points_seg;
  490. wxPen *pen_arrow;
  491. narrows = 0;
  492. size = 5;
  493. limit = 5; // 5px for line segment
  494. points_seg = Vect_new_line_struct();
  495. pen_arrow = new wxPen(settings.direction.color, settings.lineWidth, wxSOLID);
  496. dc->SetPen(*pen_arrow);
  497. dist = Vect_line_length(points);
  498. if (DistanceInPixels(dist) >= limit) {
  499. while (1) {
  500. pos = (narrows + 1) * 8 * limit * region.map_res;
  501. if (Vect_point_on_line(points, pos,
  502. &e, &n, &d, NULL, NULL) < 1) {
  503. break;
  504. }
  505. Cell2Pixel(e, n, d, &x0, &y0, &z0);
  506. if (Vect_point_on_line(points, pos - 3 * size * region.map_res,
  507. &e, &n, &d, &angle, NULL) < 1) {
  508. break;
  509. }
  510. Cell2Pixel(e, n, d, &x1, &y1, &z1);
  511. DrawArrow(x0, y0, x1, y1, angle, size);
  512. if(narrows > 1e2) // low resolution, break
  513. break;
  514. narrows++;
  515. }
  516. // draw at least one arrow in the middle of line
  517. if (narrows < 1) {
  518. dist /= 2.;
  519. if (Vect_point_on_line(points, dist,
  520. &e, &n, &d, NULL, NULL) > 0) {
  521. Cell2Pixel(e, n, d, &x0, &y0, &z0);
  522. if (Vect_point_on_line(points, dist - 3 * size * region.map_res,
  523. &e, &n, &d, &angle, NULL) > 0) {
  524. Cell2Pixel(e, n, d, &x1, &y1, &z1);
  525. DrawArrow(x0, y0, x1, y1, angle, size);
  526. }
  527. }
  528. }
  529. }
  530. Vect_destroy_line_struct(points_seg);
  531. return narrows;
  532. }
  533. /**
  534. \brief Draw arrow symbol on line
  535. \param x0,y0 arrow origin
  536. \param x1,x1 arrow ending point (on line)
  537. \param angle point ending point angle
  538. \param size arrow size
  539. \return 1
  540. */
  541. int DisplayDriver::DrawArrow(double x0, double y0,
  542. double x1, double y1, double angle,
  543. int size)
  544. {
  545. double x, y;
  546. double angle_symb;
  547. angle_symb = angle - M_PI / 2.;
  548. x = x1 + size * std::cos(angle_symb);
  549. y = y1 - size * std::sin(angle_symb);
  550. dc->DrawLine((wxCoord) x, (wxCoord) y, (wxCoord) x0, (wxCoord) y0);
  551. angle_symb = M_PI / 2. + angle;
  552. x = x1 + size * std::cos(angle_symb);
  553. y = y1 - size * std::sin(angle_symb);
  554. dc->DrawLine((wxCoord) x0, (wxCoord) y0, (wxCoord) x, (wxCoord) y);
  555. return 1;
  556. }