Graphics_grey.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* Graphics_grey.cpp
  2. *
  3. * Copyright (C) 1992-2011,2017 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdlib.h>
  19. #include "melder.h"
  20. #include "Graphics.h"
  21. //#define MAXGREYSIDE 12
  22. #define MAXGREYSIDE 1000
  23. #define MAXGREYPATH (2 * MAXGREYSIDE * (MAXGREYSIDE - 1) + 2)
  24. #define MAXGREYEDGECONTOURS (2 * (MAXGREYSIDE - 1))
  25. #define MAXGREYCLOSEDCONTOURS (MAXGREYPATH / 4 + 1)
  26. #define MAXGREYEDGEPOINTS (4 * MAXGREYSIDE)
  27. typedef struct {
  28. integer numberOfPoints;
  29. integer beginRow, beginCol, beginOri;
  30. integer endRow, endCol, endOri;
  31. integer lowerGrey, upperGrey;
  32. double *x, *y;
  33. } structEdgeContour, *EdgeContour;
  34. static EdgeContour EdgeContour_create (integer numberOfPoints) {
  35. EdgeContour result = Melder_calloc (structEdgeContour, 1);
  36. result -> numberOfPoints = numberOfPoints;
  37. result -> x = NUMvector <double> (1, 2 * numberOfPoints);
  38. result -> y = result -> x + numberOfPoints;
  39. return result; // LEAK
  40. }
  41. static void EdgeContour_delete (EdgeContour e) {
  42. NUMvector_free <double> (e -> x, 1);
  43. Melder_free (e);
  44. }
  45. typedef struct {
  46. integer numberOfPoints;
  47. int grey, drawn;
  48. double xmin, xmax, ymin, ymax, *x, *y;
  49. } structClosedContour, *ClosedContour;
  50. static ClosedContour ClosedContour_create (integer numberOfPoints) {
  51. ClosedContour result = Melder_calloc (structClosedContour, 1);
  52. result -> numberOfPoints = numberOfPoints;
  53. result -> x = NUMvector <double> (1, 2 * numberOfPoints);
  54. result -> y = result -> x + numberOfPoints;
  55. return result; // LEAK
  56. }
  57. static void ClosedContour_delete (ClosedContour c) {
  58. NUMvector_free <double> (c -> x, 1);
  59. Melder_free (c);
  60. }
  61. typedef struct {
  62. integer ori, iContour, start, usedAsEntry, grey;
  63. double val;
  64. } structEdgePoint, *EdgePoint;
  65. static Graphics theGraphics;
  66. static integer numberOfEdgeContours;
  67. static EdgeContour *edgeContours;
  68. static integer numberOfEdgePoints;
  69. static structEdgePoint *edgePoints;
  70. static integer numberOfClosedContours;
  71. static ClosedContour *closedContours;
  72. static integer numberOfPoints;
  73. static integer row1, row2, col1, col2;
  74. static integer iBorder, numberOfBorders;
  75. static integer **right, **below;
  76. static double **data, *border, *x, *y;
  77. static double dx, dy, xoff, yoff;
  78. static int empty (integer row, integer col, integer ori)
  79. {
  80. if (ori == 3) { row ++; ori = 1; }
  81. if (ori == 2) { col ++; ori = 4; }
  82. if (ori == 1)
  83. return (data [row] [col] < border [iBorder]) !=
  84. (data [row] [col + 1] < border [iBorder]) &&
  85. ! right [row - row1] [col - col1];
  86. else /* ori == 4 */
  87. return (data [row] [col] < border [iBorder]) !=
  88. (data [row + 1] [col] < border [iBorder]) &&
  89. ! below [row - row1] [col - col1];
  90. }
  91. static integer note (integer row, integer col, integer ori)
  92. {
  93. ++ numberOfPoints;
  94. Melder_assert (numberOfPoints <= MAXGREYPATH);
  95. if (ori == 3) { row ++; ori = 1; }
  96. if (ori == 2) { col ++; ori = 4; }
  97. if (ori == 1)
  98. {
  99. right [row - row1] [col - col1] = 1;
  100. x [numberOfPoints] = xoff + (col + (border [iBorder] - data [row] [col]) /
  101. (data [row] [col + 1] - data [row] [col])) * dx;
  102. y [numberOfPoints] = yoff + row * dy;
  103. }
  104. else /* ori == 4 */
  105. {
  106. below [row - row1] [col - col1] = 1;
  107. x [numberOfPoints] = xoff + col * dx;
  108. y [numberOfPoints] = yoff + (row + (border [iBorder] - data [row] [col]) /
  109. (data [row + 1] [col] - data [row] [col])) * dy;
  110. }
  111. return 1;
  112. }
  113. static void fillGrey (integer numberOfPoints, double *x, double *y, int igrey)
  114. /* "igrey" is in between 1 and numberOfBorders + 1. */
  115. {
  116. Graphics_setGrey (theGraphics, 1.0 - (igrey - 1.0) / numberOfBorders);
  117. Graphics_fillArea (theGraphics, numberOfPoints, & x [1], & y [1]);
  118. }
  119. static void makeEdgeContour (integer row0, integer col0, integer ori0) {
  120. numberOfPoints = 0;
  121. integer row = row0, col = col0, ori = ori0;
  122. note (row0, col0, ori0);
  123. bool edge = false;
  124. do {
  125. bool clockwise = ! (ori & 1);
  126. do { /* Preference for contours perpendicular to x == y. */
  127. ori = (clockwise ? ori : ori + 2) % 4 + 1;
  128. } while (! empty (row, col, ori));
  129. switch (ori) {
  130. case 1: edge = row == row1; break;
  131. case 2: edge = col == col2 - 1; break;
  132. case 3: edge = row == row2 - 1; break;
  133. case 4: edge = col == col1; break;
  134. }
  135. if (! edge) {
  136. switch (ori) {
  137. case 1: row --; break;
  138. case 2: col ++; break;
  139. case 3: row ++; break;
  140. case 4: col --; break;
  141. }
  142. ori = (ori + 1) % 4 + 1;
  143. }
  144. note (row, col, ori);
  145. }
  146. while (! edge);
  147. Melder_assert (numberOfEdgeContours < MAXGREYEDGECONTOURS * numberOfBorders);
  148. EdgeContour e = edgeContours [++ numberOfEdgeContours] = EdgeContour_create (numberOfPoints);
  149. e -> beginRow = row0;
  150. e -> beginCol = col0;
  151. e -> beginOri = ori0;
  152. e -> endRow = row;
  153. e -> endCol = col;
  154. e -> endOri = ori;
  155. bool up = false;
  156. switch (ori0) {
  157. case 1: up = data [row0] [col0 + 1] > data [row0] [col0]; break;
  158. case 2: up = data [row0 + 1] [col0 + 1] > data [row0] [col0 + 1]; break;
  159. case 3: up = data [row0 + 1] [col0] > data [row0 + 1] [col0 + 1]; break;
  160. case 4: up = data [row0] [col0] > data [row0 + 1] [col0]; break;
  161. }
  162. if (up) { e -> lowerGrey = iBorder; e -> upperGrey = iBorder + 1; }
  163. else { e -> lowerGrey = iBorder + 1; e -> upperGrey = iBorder; }
  164. for (integer iPoint = 1; iPoint <= numberOfPoints; iPoint ++) {
  165. e -> x [iPoint] = x [iPoint];
  166. e -> y [iPoint] = y [iPoint];
  167. }
  168. }
  169. static void makeClosedContour (integer row0, integer col0, integer ori0) {
  170. double x1, y1;
  171. ClosedContour c;
  172. numberOfPoints = 0;
  173. integer row = row0, col = col0, ori = ori0;
  174. do {
  175. bool clockwise = ! (ori & 1);
  176. do { /* Preference for contours perpendicular to x == y. */
  177. ori = (clockwise ? ori : ori + 2) % 4 + 1;
  178. } while (! empty (row, col, ori));
  179. switch (ori) {
  180. case 1: row --; break;
  181. case 2: col ++; break;
  182. case 3: row ++; break;
  183. case 4: col --; break;
  184. }
  185. ori = (ori + 1) % 4 + 1;
  186. note (row, col, ori);
  187. }
  188. while (row != row0 || col != col0 || ori != ori0);
  189. Melder_assert (numberOfClosedContours < MAXGREYCLOSEDCONTOURS * numberOfBorders);
  190. c = closedContours [++ numberOfClosedContours] = ClosedContour_create (numberOfPoints);
  191. /* Find a point just inside or outside the contour. */
  192. /* Find out whether it is above or below the contour. */
  193. x1 = x [numberOfPoints];
  194. y1 = y [numberOfPoints];
  195. bool up = false;
  196. if (ori == 3) { row ++; ori = 1; }
  197. else if (ori == 2) { col ++; ori = 4; }
  198. if (ori == 1) {
  199. if (x1 > xoff + (col + 0.5) * dx)
  200. x1 -= 0.01 * dx;
  201. else
  202. x1 += 0.01 * dx;
  203. up = data [row] [col] + ((x1 - xoff) / dx - col) *
  204. (data [row] [col + 1] - data [row] [col]) > border [iBorder];
  205. } else { /* ori == 4 */
  206. if (y1 > yoff + (row + 0.5) * dy)
  207. y1 -= 0.01 * dy;
  208. else
  209. y1 += 0.01 * dy;
  210. up = data [row] [col] + ((y1 - yoff) / dy - row) *
  211. (data [row + 1] [col] - data [row] [col]) > border [iBorder];
  212. }
  213. /* Find out whether the point is inside or outside the contour. */
  214. if (! NUMrotationsPointInPolygon (x1, y1, numberOfPoints, x, y)) up = ! up;
  215. double xmin = 1e308, xmax = -1e308, ymin = 1e308, ymax = -1e308;
  216. c -> grey = up ? iBorder + 1 : iBorder;
  217. for (int i = 1; i <= numberOfPoints; i ++) {
  218. c -> x [i] = x [i];
  219. c -> y [i] = y [i];
  220. if (x [i] < xmin) xmin = x [i];
  221. if (x [i] > xmax) xmax = x [i];
  222. if (y [i] < ymin) ymin = y [i];
  223. if (y [i] > ymax) ymax = y [i];
  224. }
  225. c -> xmin = xmin;
  226. c -> xmax = xmax;
  227. c -> ymin = ymin;
  228. c -> ymax = ymax;
  229. }
  230. static void smallGrey () {
  231. numberOfEdgeContours = 0;
  232. numberOfClosedContours = 0;
  233. for (iBorder = 1; iBorder <= numberOfBorders; iBorder ++) {
  234. for (integer row = 0; row < MAXGREYSIDE; row ++) for (integer col = 0; col < MAXGREYSIDE; col ++)
  235. right [row] [col] = below [row] [col] = 0;
  236. /* Find all the edge contours of this border value. */
  237. for (integer col = col1; col < col2; col ++)
  238. if (empty (row1, col, 1))
  239. makeEdgeContour (row1, col, 1);
  240. for (integer row = row1; row < row2; row ++)
  241. if (empty (row, col2 - 1, 2))
  242. makeEdgeContour (row, col2 - 1, 2);
  243. for (integer col = col2 - 1; col >= col1; col --)
  244. if (empty (row2 - 1, col, 3))
  245. makeEdgeContour (row2 - 1, col, 3);
  246. for (integer row = row2 - 1; row >= row1; row --)
  247. if (empty (row, col1, 4))
  248. makeEdgeContour (row, col1, 4);
  249. /* Find all the closed contours of this border value. */
  250. for (integer row = row1 + 1; row < row2; row ++)
  251. for (integer col = col1; col < col2; col ++)
  252. if (empty (row, col, 1)) makeClosedContour (row, col, 1);
  253. for (integer col = col1 + 1; col < col2; col ++)
  254. for (integer row = row1; row < row2; row ++)
  255. if (empty (row, col, 4)) makeClosedContour (row, col, 4);
  256. }
  257. numberOfEdgePoints = 2 * numberOfEdgeContours + 4;
  258. Melder_assert (numberOfEdgePoints <= MAXGREYEDGEPOINTS * numberOfBorders);
  259. /* Make a list of all points on the edge. */
  260. /* The edge points include the four corner points. */
  261. for (int i = 1; i <= 4; i ++) {
  262. EdgePoint p = & edgePoints [i];
  263. p -> ori = i;
  264. p -> val = 0;
  265. p -> iContour = 0;
  266. p -> start = 0;
  267. p -> usedAsEntry = 0;
  268. p -> grey = -1;
  269. }
  270. /* The edge points include the first points of the edge contours. */
  271. for (int i = 1; i <= numberOfEdgeContours; i ++) {
  272. EdgeContour c = edgeContours [i];
  273. EdgePoint p = & edgePoints [i + i + 3];
  274. switch (p -> ori = c -> beginOri) {
  275. case 1: p -> val = c -> x [1] - xoff - col1 * dx; break;
  276. case 2: p -> val = c -> y [1] - yoff - row1 * dy; break;
  277. case 3: p -> val = xoff + col2 * dx - c -> x [1]; break;
  278. case 4: p -> val = yoff + row2 * dy - c -> y [1]; break;
  279. }
  280. p -> iContour = i;
  281. p -> start = 1;
  282. p -> usedAsEntry = 0;
  283. p -> grey = c -> lowerGrey;
  284. }
  285. /* The edge points include the last points of the edge contours. */
  286. for (int i = 1; i <= numberOfEdgeContours; i ++) {
  287. EdgeContour c = edgeContours [i];
  288. EdgePoint p = & edgePoints [i + i + 4];
  289. switch (p -> ori = c -> endOri) {
  290. case 1: p -> val = c -> x [c -> numberOfPoints] - xoff - col1 * dx; break;
  291. case 2: p -> val = c -> y [c -> numberOfPoints] - yoff - row1 * dy; break;
  292. case 3: p -> val = xoff + col2 * dx - c -> x [c -> numberOfPoints]; break;
  293. case 4: p -> val = yoff + row2 * dy - c -> y [c -> numberOfPoints]; break;
  294. }
  295. p -> iContour = i;
  296. p -> start = 0;
  297. p -> usedAsEntry = 0;
  298. p -> grey = c -> upperGrey;
  299. }
  300. /* Sort the list of edge points with keys Ori and Val. */
  301. for (int i = 1; i < numberOfEdgePoints; i ++) {
  302. structEdgePoint p;
  303. int min = i, j;
  304. for (j = i + 1; j <= numberOfEdgePoints; j ++)
  305. if (edgePoints [min]. ori > edgePoints [j]. ori ||
  306. (edgePoints [min]. ori == edgePoints [j]. ori && edgePoints [min]. val > edgePoints [j]. val))
  307. min = j;
  308. p = edgePoints [i];
  309. edgePoints [i] = edgePoints [min];
  310. edgePoints [min] = p;
  311. }
  312. {
  313. for (int edge0 = 1; edge0 <= numberOfEdgePoints; edge0 ++)
  314. if (edgePoints [edge0].grey > -1 && ! edgePoints [edge0].usedAsEntry) {
  315. int iPoint = 0;
  316. int edge1 = edge0;
  317. int darkness;
  318. do {
  319. /* Follow one edge contour.
  320. */
  321. EdgePoint p = & edgePoints [edge1];
  322. integer iContour = p -> iContour;
  323. EdgeContour c = edgeContours [iContour];
  324. Melder_assert (iContour > 0);
  325. darkness = p -> grey;
  326. p -> usedAsEntry = 1;
  327. if (p -> start) {
  328. for (int i = 1; i <= c -> numberOfPoints; i ++) {
  329. Melder_assert (iPoint < MAXGREYPATH);
  330. x [++ iPoint] = c -> x [i];
  331. y [iPoint] = c -> y [i];
  332. }
  333. for (int i = edge1 + 1; i <= numberOfEdgePoints; i ++)
  334. if (edgePoints [i].iContour == iContour)
  335. edge1 = i;
  336. } else {
  337. int edge1dummy = edge1;
  338. for (integer i = c -> numberOfPoints; i >= 1; i --) {
  339. Melder_assert (iPoint < MAXGREYPATH);
  340. x [++ iPoint] = c -> x [i];
  341. y [iPoint] = c -> y [i];
  342. }
  343. for (int i = 1; i <= edge1dummy - 1; i ++)
  344. if (edgePoints [i].iContour == iContour)
  345. edge1 = i;
  346. }
  347. edge1 = edge1 % numberOfEdgePoints + 1;
  348. /* Round some corners.
  349. */
  350. while (edgePoints [edge1].grey == -1) {
  351. ++ iPoint;
  352. Melder_assert (iPoint <= MAXGREYPATH);
  353. switch (edgePoints [edge1].ori) {
  354. case 1: x [iPoint] = xoff + col1 * dx; y [iPoint] = yoff + row1 * dy; break;
  355. case 2: x [iPoint] = xoff + col2 * dx; y [iPoint] = yoff + row1 * dy; break;
  356. case 3: x [iPoint] = xoff + col2 * dx; y [iPoint] = yoff + row2 * dy; break;
  357. case 4: x [iPoint] = xoff + col1 * dx; y [iPoint] = yoff + row2 * dy; break;
  358. }
  359. edge1 = edge1 % numberOfEdgePoints + 1;
  360. }
  361. }
  362. while (edge1 != edge0);
  363. fillGrey (iPoint, x, y, darkness);
  364. }
  365. }
  366. if (numberOfEdgeContours == 0) {
  367. int i = 1;
  368. while (i <= numberOfBorders && border [i] < data [row1] [col1]) i ++;
  369. x [1] = x [4] = xoff + col1 * dx;
  370. x [2] = x [3] = xoff + col2 * dx;
  371. y [1] = y [2] = yoff + row1 * dy;
  372. y [3] = y [4] = yoff + row2 * dy;
  373. fillGrey (4, x, y, i);
  374. }
  375. /* Iterate over all the closed contours.
  376. * Those that are not enclosed by any other contour, are filled first.
  377. */
  378. {
  379. bool found = false;
  380. do {
  381. for (integer i = 1; i <= numberOfClosedContours; i ++) {
  382. ClosedContour ci = closedContours [i];
  383. if (! ci -> drawn) {
  384. bool enclosed = false;
  385. integer j = 1;
  386. while (j <= numberOfClosedContours && ! enclosed) {
  387. ClosedContour cj = closedContours [j];
  388. if (! cj -> drawn && j != i &&
  389. ci -> xmin > cj -> xmin && ci -> xmax < cj -> xmax &&
  390. ci -> ymin > cj -> ymin && ci -> ymax < cj -> ymax)
  391. enclosed = NUMrotationsPointInPolygon (ci -> x [1], ci -> y [1],
  392. cj -> numberOfPoints, cj -> x, cj -> y);
  393. j ++;
  394. }
  395. if (! enclosed) {
  396. found = true;
  397. fillGrey (ci -> numberOfPoints, ci -> x, ci -> y, ci -> grey);
  398. ci -> drawn = 1;
  399. }
  400. }
  401. }
  402. } while (found);
  403. }
  404. Graphics_setGrey (theGraphics, 0.0);
  405. for (int i = 1; i <= numberOfEdgeContours; i ++)
  406. EdgeContour_delete (edgeContours [i]);
  407. for (int i = 1; i <= numberOfClosedContours; i ++)
  408. ClosedContour_delete (closedContours [i]);
  409. }
  410. void Graphics_grey (Graphics me, double **z,
  411. integer ix1, integer ix2, double x1WC, double x2WC,
  412. integer iy1, integer iy2, double y1WC, double y2WC,
  413. int _numberOfBorders, double borders [])
  414. {
  415. if (ix2 <= ix1 || iy2 <= iy1) return;
  416. /* Static variables. */
  417. theGraphics = me;
  418. numberOfBorders = _numberOfBorders;
  419. data = z;
  420. border = borders;
  421. dx = (x2WC - x1WC) / (ix2 - ix1);
  422. dy = (y2WC - y1WC) / (iy2 - iy1);
  423. xoff = x1WC - ix1 * dx;
  424. yoff = y1WC - iy1 * dy;
  425. if (! right) {
  426. right = NUMmatrix <integer> (0, MAXGREYSIDE - 1, 0, MAXGREYSIDE - 1); // BUG memory
  427. below = NUMmatrix <integer> (0, MAXGREYSIDE - 1, 0, MAXGREYSIDE - 1);
  428. x = NUMvector <double> (1, MAXGREYPATH);
  429. y = NUMvector <double> (1, MAXGREYPATH);
  430. edgeContours = Melder_calloc (EdgeContour, MAXGREYEDGECONTOURS * numberOfBorders) - 1;
  431. closedContours = Melder_calloc (ClosedContour, MAXGREYCLOSEDCONTOURS * numberOfBorders) - 1;
  432. edgePoints = Melder_calloc (structEdgePoint, MAXGREYEDGEPOINTS * numberOfBorders);
  433. }
  434. /* The matrix is subdivided into matrices with side MAXGREYSIDE, so that:
  435. * 1. All the paths will fit into our memory (we have to remember them all).
  436. * 2. The path for filling fits into the PostScript path, which may be max. 1500 points long.
  437. */
  438. for (row1 = iy1; row1 < iy2; row1 += MAXGREYSIDE - 1) {
  439. row2 = row1 + (MAXGREYSIDE - 1);
  440. if (row2 > iy2) row2 = iy2;
  441. for (col1 = ix1; col1 < ix2; col1 += MAXGREYSIDE - 1) {
  442. col2 = col1 + (MAXGREYSIDE - 1);
  443. if (col2 > ix2) col2 = ix2;
  444. smallGrey ();
  445. }
  446. }
  447. }
  448. /* End of file Graphics_grey.cpp */