driver_draw.cpp 16 KB

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