| 1 |
/* Graphics_altitude.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2005,2008,2011,2015-2020 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 |
|
| 19 |
#include "Graphics.h" |
| 20 |
|
| 21 |
static int empty (constMATVU const& data, constBOOLMAT const& right, constBOOLMAT const& below, integer row1, integer col1, double height, integer row, integer col, integer ori) { |
| 22 |
if (ori == 3) { |
| 23 |
row ++; |
| 24 |
ori = 1; |
| 25 |
} else if (ori == 2) { |
| 26 |
col ++; |
| 27 |
ori = 4; |
| 28 |
} |
| 29 |
if (ori == 1) |
| 30 |
return (data [row] [col] < height) != (data [row] [col + 1] < height) && |
| 31 |
! right [row - row1 + 1] [col - col1 + 1]; |
| 32 |
else /* ori == 4 */ |
| 33 |
return (data [row] [col] < height) != (data [row + 1] [col] < height) && |
| 34 |
! below [row - row1 + 1] [col - col1 + 1]; |
| 35 |
} |
| 36 |
|
| 37 |
#define MAXALTSIDE 50 |
| 38 |
#define MAXALTPATH (2 * MAXALTSIDE * (MAXALTSIDE - 1) + 2) |
| 39 |
|
| 40 |
static integer numberOfPoints; |
| 41 |
static integer row1, row2, col1, col2; |
| 42 |
static autoBOOLMAT right, below; |
| 43 |
static autoVEC x, y; |
| 44 |
static bool closed; |
| 45 |
static double dx, dy, xoff, yoff; |
| 46 |
|
| 47 |
static int note (constMATVU const& z, double height, integer row, integer col, int ori, bool pleaseForget) { |
| 48 |
++ numberOfPoints; |
| 49 |
Melder_assert (numberOfPoints <= MAXALTPATH); |
| 50 |
if (ori == 3) { |
| 51 |
row ++; |
| 52 |
ori = 1; |
| 53 |
} else if (ori == 2) { |
| 54 |
col ++; |
| 55 |
ori = 4; |
| 56 |
} |
| 57 |
if (ori == 1) { |
| 58 |
if (pleaseForget) |
| 59 |
right [row - row1 + 1] [col - col1 + 1] = true; |
| 60 |
x [numberOfPoints] = xoff + |
| 61 |
(col + (height - z [row] [col]) / (z [row] [col + 1] - z [row] [col])) * dx; |
| 62 |
y [numberOfPoints] = yoff + row * dy; |
| 63 |
} else { /* ori == 4 */ |
| 64 |
if (pleaseForget) |
| 65 |
below [row - row1 + 1] [col - col1 + 1] = true; |
| 66 |
x [numberOfPoints] = xoff + col * dx; |
| 67 |
y [numberOfPoints] = yoff + |
| 68 |
(row + (height - z [row] [col]) / (z [row + 1] [col] - z [row] [col])) * dy; |
| 69 |
} |
| 70 |
return 1; |
| 71 |
} |
| 72 |
|
| 73 |
static void makeContour (Graphics graphics, constMATVU const& z, double height, integer row0, integer col0, int ori0) { |
| 74 |
integer row = row0, col = col0; |
| 75 |
bool edge = false; |
| 76 |
int ori = ori0; |
| 77 |
numberOfPoints = 0; |
| 78 |
note (z, height, row0, col0, ori0, false); |
| 79 |
do { |
| 80 |
bool const clockwise = ! (ori & 1); |
| 81 |
do { /* Preference for contours perpendicular to x == y. */ |
| 82 |
ori = ( clockwise ? ori : ori + 2 ) % 4 + 1; |
| 83 |
} while (! empty (z, right.get(), below.get(), row1, col1, height, row, col, ori)); |
| 84 |
if (! closed) switch (ori) { |
| 85 |
case 1: edge = row == row1; break; |
| 86 |
case 2: edge = col == col2 - 1; break; |
| 87 |
case 3: edge = row == row2 - 1; break; |
| 88 |
case 4: edge = col == col1; break; |
| 89 |
} |
| 90 |
if (! edge) { |
| 91 |
switch (ori) { |
| 92 |
case 1: row --; break; |
| 93 |
case 2: col ++; break; |
| 94 |
case 3: row ++; break; |
| 95 |
case 4: col --; break; |
| 96 |
} |
| 97 |
ori = (ori + 1) % 4 + 1; |
| 98 |
} |
| 99 |
if (! note (z, height, row, col, ori, 1)) |
| 100 |
return; |
| 101 |
} |
| 102 |
while (edge == 0 && (row != row0 || col != col0 || ori != ori0)) |
| 103 |
; |
| 104 |
if (closed) |
| 105 |
note (z, height, row0, col0, ori0, true); // close the contour |
| 106 |
Graphics_polyline (graphics, numberOfPoints, & x [1], & y [1]); |
| 107 |
} |
| 108 |
|
| 109 |
static void smallAlt (Graphics graphics, constMATVU const& z, double height) { |
| 110 |
for (integer row = 1; row <= MAXALTSIDE; row ++) |
| 111 |
for (integer col = 1; col <= MAXALTSIDE; col ++) |
| 112 |
right [row] [col] = below [row] [col] = false; |
| 113 |
|
| 114 |
/* Find all the edge contours of this border value. */ |
| 115 |
|
| 116 |
closed = false; |
| 117 |
for (integer col = col1; col < col2; col ++) |
| 118 |
if (empty (z, right.get(), below.get(), row1, col1, height, row1, col, 1)) |
| 119 |
makeContour (graphics, z, height, row1, col, 1); |
| 120 |
for (integer row = row1; row < row2; row ++) |
| 121 |
if (empty (z, right.get(), below.get(), row1, col1, height, row, col2 - 1, 2)) |
| 122 |
makeContour (graphics, z, height, row, col2 - 1, 2); |
| 123 |
for (integer col = col2 - 1; col >= col1; col --) |
| 124 |
if (empty (z, right.get(), below.get(), row1, col1, height, row2 - 1, col, 3)) |
| 125 |
makeContour (graphics, z, height, row2 - 1, col, 3); |
| 126 |
for (integer row = row2 - 1; row >= row1; row --) |
| 127 |
if (empty (z, right.get(), below.get(), row1, col1, height, row, col1, 4)) |
| 128 |
makeContour (graphics, z, height, row, col1, 4); |
| 129 |
|
| 130 |
/* Find all the closed contours of this border value. */ |
| 131 |
|
| 132 |
closed = true; |
| 133 |
for (integer row = row1 + 1; row < row2; row ++) |
| 134 |
for (integer col = col1; col < col2; col ++) |
| 135 |
if (empty (z, right.get(), below.get(), row1, col1, height, row, col, 1)) |
| 136 |
makeContour (graphics, z, height, row, col, 1); |
| 137 |
for (integer col = col1 + 1; col < col2; col ++) |
| 138 |
for (integer row = row1; row < row2; row ++) |
| 139 |
if (empty (z, right.get(), below.get(), row1, col1, height, row, col, 4)) |
| 140 |
makeContour (graphics, z, height, row, col, 4); |
| 141 |
} |
| 142 |
|
| 143 |
void Graphics_contour (Graphics me, constMATVU const& z, |
| 144 |
double x1WC, double x2WC, double y1WC, double y2WC, |
| 145 |
double height) |
| 146 |
{ |
| 147 |
if (z.nrow <= 1 || z.ncol <= 1) |
| 148 |
return; |
| 149 |
dx = (x2WC - x1WC) / (z.ncol - 1); |
| 150 |
dy = (y2WC - y1WC) / (z.nrow - 1); |
| 151 |
xoff = x1WC - dx; |
| 152 |
yoff = y1WC - dy; |
| 153 |
if (NUMisEmpty (right.get())) { // static! |
| 154 |
right = zero_BOOLMAT (MAXALTSIDE, MAXALTSIDE); |
| 155 |
below = zero_BOOLMAT (MAXALTSIDE, MAXALTSIDE); |
| 156 |
x = zero_VEC (MAXALTPATH); |
| 157 |
y = zero_VEC (MAXALTPATH); |
| 158 |
} |
| 159 |
for (row1 = 1; row1 < z.nrow; row1 += MAXALTSIDE - 1) { |
| 160 |
for (col1 = 1; col1 < z.ncol; col1 += MAXALTSIDE - 1) { |
| 161 |
if ((row2 = row1 + (MAXALTSIDE - 1)) > z.nrow) |
| 162 |
row2 = z.nrow; |
| 163 |
if ((col2 = col1 + (MAXALTSIDE - 1)) > z.ncol) |
| 164 |
col2 = z.ncol; |
| 165 |
smallAlt (me, z, height); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
void Graphics_altitude (Graphics me, constMATVU const& z, |
| 171 |
double x1WC, double x2WC, double y1WC, double y2WC, |
| 172 |
int numberOfBorders, double borders []) |
| 173 |
{ |
| 174 |
if (z.nrow <= 1 || z.ncol <= 1) |
| 175 |
return; |
| 176 |
dx = (x2WC - x1WC) / (z.ncol - 1); |
| 177 |
dy = (y2WC - y1WC) / (z.nrow - 1); |
| 178 |
xoff = x1WC - dx; |
| 179 |
yoff = y1WC - dy; |
| 180 |
if (NUMisEmpty (right.get())) { // static! |
| 181 |
right = zero_BOOLMAT (MAXALTSIDE, MAXALTSIDE); |
| 182 |
below = zero_BOOLMAT (MAXALTSIDE, MAXALTSIDE); |
| 183 |
x = zero_VEC (MAXALTPATH); |
| 184 |
y = zero_VEC (MAXALTPATH); |
| 185 |
} |
| 186 |
for (row1 = 1; row1 < z.nrow; row1 += MAXALTSIDE - 1) { |
| 187 |
for (col1 = 1; col1 < z.ncol; col1 += MAXALTSIDE - 1) { |
| 188 |
if ((row2 = row1 + (MAXALTSIDE - 1)) > z.nrow) |
| 189 |
row2 = z.nrow; |
| 190 |
if ((col2 = col1 + (MAXALTSIDE - 1)) > z.ncol) |
| 191 |
col2 = z.ncol; |
| 192 |
for (int iborder = 1; iborder <= numberOfBorders; iborder ++) |
| 193 |
smallAlt (me, z, borders [iborder]); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/* End of file Graphics_altitude.cpp */ |