| 1 | /* Polygon_extensions.c |
| 2 | * |
| 3 | * Copyright (C) 1993-2020 David Weenink |
| 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. See the GNU |
| 13 | * 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 | /* |
| 20 | djmw 20020813 GPL header |
| 21 | djmw 20080122 float -> double |
| 22 | djmw 20100407 Sound_to_Polygon, Sounds_to_Polygon_encloseds |
| 23 | djmw 20100418 Polygon_rotate. bug: angle was in degrees now in radians |
| 24 | */ |
| 25 | |
| 26 | #include "Matrix.h" |
| 27 | #include "NUM2.h" |
| 28 | #include "NUMselect.h" |
| 29 | #include "Polygon_extensions.h" |
| 30 | #include "Vector.h" |
| 31 | #include "DoublyLinkedList.h" |
| 32 | |
| 33 | // not for self-intersecting polygons! |
| 34 | static double Polygon_area (Polygon me) { |
| 35 | longdouble area = 0.0; |
| 36 | integer j = my numberOfPoints; |
| 37 | for (integer i = 1; i <= my numberOfPoints; i ++) { |
| 38 | area += (my x [j] + my x [i]) * (my y [j] - my y [i]); |
| 39 | j = i; |
| 40 | } |
| 41 | area *= 0.5; |
| 42 | return fabs (double (area)); // area may have negative sign in counter-clockwise evaluation of area |
| 43 | } |
| 44 | |
| 45 | void Polygon_getExtrema (Polygon me, double *out_xmin, double *out_xmax, double *out_ymin, double *out_ymax) { |
| 46 | const double xmin = num::NUMmin_e (my x.get()); |
| 47 | const double xmax = num::NUMmax_e (my x.get()); |
| 48 | const double ymin = num::NUMmin_e (my y.get()); |
| 49 | const double ymax = num::NUMmax_e (my y.get()); |
| 50 | if (out_xmin) |
| 51 | *out_xmin = xmin; |
| 52 | if (out_xmax) |
| 53 | *out_xmax = xmax; |
| 54 | if (out_ymin) |
| 55 | *out_ymin = ymin; |
| 56 | if (out_ymax) |
| 57 | *out_ymax = ymax; |
| 58 | } |
| 59 | |
| 60 | autoPolygon Polygon_createSimple (constVECVU const& vertices_asXYPairs) { |
| 61 | try { |
| 62 | Melder_require (vertices_asXYPairs.size >= 6, |
| 63 | U"There should be at least 3 points (= x,y pairs) in the Polygon"); |
| 64 | Melder_require (vertices_asXYPairs.size % 2 == 0, |
| 65 | U"One value is missing."); |
| 66 | |
| 67 | const integer numberOfPoints = vertices_asXYPairs.size / 2; |
| 68 | autoPolygon me = Polygon_create (numberOfPoints); |
| 69 | for (integer i = 1; i <= numberOfPoints; i ++) { |
| 70 | my x [i] = vertices_asXYPairs [2 * i - 1]; |
| 71 | my y [i] = vertices_asXYPairs [2 * i]; |
| 72 | if (i > 1 && my x [i] == my x [i - 1] && my y [i] == my y [i - 1]) |
| 73 | Melder_warning (U"Two successive vertices are equal."); |
| 74 | } |
| 75 | return me; |
| 76 | } catch (MelderError) { |
| 77 | Melder_throw (U"Polygon not created."); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | autoPolygon Polygon_createFromRandomPoints (integer numberOfVertices, double xmin, double xmax, double ymin, double ymax) { |
| 82 | try { |
| 83 | autoPolygon me = Polygon_create (numberOfVertices); |
| 84 | for (integer i = 1; i <= numberOfVertices; i ++) { |
| 85 | my x [i] = NUMrandomUniform (xmin, xmax); |
| 86 | my y [i] = NUMrandomUniform (ymin, ymax); |
| 87 | } |
| 88 | return me; |
| 89 | } catch (MelderError) { |
| 90 | Melder_throw (U"Polygon not created."); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void Polygon_translate (Polygon me, double xt, double yt) { |
| 95 | my x.get() += xt; |
| 96 | my y.get() += yt; |
| 97 | } |
| 98 | |
| 99 | /* rotate counterclockwise w.r.t. (xc,yc) */ |
| 100 | void Polygon_rotate (Polygon me, double alpha, double xc, double yc) { |
| 101 | const double f = alpha * NUMpi / 180, cosa = cos (f), sina = sin (f); |
| 102 | |
| 103 | Polygon_translate (me, -xc, -yc); |
| 104 | for (integer i = 1; i <= my numberOfPoints; i ++) { |
| 105 | const double x = my x [i]; |
| 106 | my x [i] = cosa * my x [i] - sina * my y [i]; |
| 107 | my y [i] = sina * x + cosa * my y [i]; |
| 108 | } |
| 109 | Polygon_translate (me, xc, yc); |
| 110 | } |
| 111 | |
| 112 | void Polygon_scale (Polygon me, double xs, double ys) { |
| 113 | my x.get() *= xs; |
| 114 | my y.get() *= ys; |
| 115 | } |
| 116 | |
| 117 | void Polygon_reverseX (Polygon me) { |
| 118 | my x.get() *= -1.0; |
| 119 | } |
| 120 | |
| 121 | void Polygon_reverseY (Polygon me) { |
| 122 | my y.get() *= -1.0; |
| 123 | } |
| 124 | |
| 125 | void Polygon_Categories_draw (Polygon me, Categories thee, Graphics graphics, double xmin, double xmax, double ymin, double ymax, bool garnish) { |
| 126 | if (my numberOfPoints != thy size) |
| 127 | return; |
| 128 | if (xmax == xmin) { |
| 129 | const MelderRealRange xrange = NUMextrema_u (my x.get()); |
| 130 | if (isundef (xrange)) |
| 131 | return; |
| 132 | const double tmp = 0.5 * xrange.isEmpty(); |
| 133 | xmin = xrange.min - tmp; |
| 134 | xmax = xrange.max + tmp; |
| 135 | } |
| 136 | if (ymax == ymin) { |
| 137 | const MelderRealRange yrange = NUMextrema_u (my y.get()); |
| 138 | if (isundef (yrange)) |
| 139 | return; |
| 140 | const double tmp = 0.5 * yrange.isEmpty(); |
| 141 | ymin = yrange.min - tmp; |
| 142 | ymax = yrange.max + tmp; |
| 143 | } |
| 144 | |
| 145 | Graphics_setInner (graphics); |
| 146 | Graphics_setWindow (graphics, xmin, xmax, ymin, ymax); |
| 147 | Graphics_setTextAlignment (graphics, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 148 | |
| 149 | for (integer i = 1; i <= my numberOfPoints; i ++) { |
| 150 | SimpleString category = thy at [i]; |
| 151 | Graphics_text (graphics, my x [i], my y [i], category -> string.get()); |
| 152 | } |
| 153 | Graphics_unsetInner (graphics); |
| 154 | if (garnish) { |
| 155 | Graphics_drawInnerBox (graphics); |
| 156 | Graphics_marksLeft (graphics, 2, true, true, false); |
| 157 | if (ymin * ymax < 0.0) |
| 158 | Graphics_markLeft (graphics, 0.0, true, true, true, nullptr); |
| 159 | Graphics_marksBottom (graphics, 2, true, true, false); |
| 160 | if (xmin * xmax < 0.0) |
| 161 | Graphics_markBottom (graphics, 0.0, true, true, true, nullptr); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void Polygon_drawMarks (Polygon me, Graphics g, double xmin, double xmax, double ymin, double ymax, double size_mm, conststring32 mark) { |
| 166 | Graphics_setInner (g); |
| 167 | if (xmax <= xmin) { /* Autoscaling along x axis. */ |
| 168 | xmax = NUMmax_u (my x.get()); |
| 169 | xmin = NUMmin_u (my x.get()); |
| 170 | if (xmin == xmax) { |
| 171 | xmin -= 1.0; |
| 172 | xmax += 1.0; |
| 173 | } |
| 174 | } |
| 175 | if (ymax <= ymin) { /* Autoscaling along y axis. */ |
| 176 | ymax = NUMmax_u (my y.get()); |
| 177 | ymin = NUMmin_u (my y.get()); |
| 178 | if (ymin == ymax) { |
| 179 | ymin -= 1.0; |
| 180 | ymax += 1.0; |
| 181 | } |
| 182 | } |
| 183 | if (isundef (xmin) || isundef (xmax) || isundef (ymin) || isundef (ymax)) |
| 184 | return; |
| 185 | Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 186 | for (integer i = 1; i <= my numberOfPoints; i ++) |
| 187 | Graphics_mark (g, my x [i], my y [i], size_mm, mark); |
| 188 | Graphics_unsetInner (g); |
| 189 | } |
| 190 | |
| 191 | #define CLIP_Y(y,ymin,ymax) (clip ? ((y) > (ymax) ? (ymax) : (y) < (ymin) ? (ymin) : (y)) : y) |
| 192 | |
| 193 | autoPolygon Sound_to_Polygon (Sound me, integer channel, double tmin, double tmax, double ymin, double ymax, double level) { |
| 194 | try { |
| 195 | const bool clip = ymin < ymax; |
| 196 | Melder_require (channel > 0 && channel <= my ny, |
| 197 | U"Channel does not exist."); |
| 198 | Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 199 | if (tmin < my xmin) |
| 200 | tmin = my xmin; |
| 201 | if (tmax > my xmax) |
| 202 | tmax = my xmax; |
| 203 | Melder_require (tmin < my xmax && tmax > my xmin, |
| 204 | U"Invalid domain."); |
| 205 | |
| 206 | const integer i1 = Sampled_xToHighIndex (me, tmin); |
| 207 | const integer i2 = Sampled_xToLowIndex (me, tmax); |
| 208 | const integer numberOfPoints = i2 - i1 + 1 + 2 + 2; // begin + endpoint + level |
| 209 | autoPolygon him = Polygon_create (numberOfPoints); |
| 210 | |
| 211 | /* |
| 212 | In Vector_getValueAtX the interpolation only returns defined values between the |
| 213 | left and right edges that are calculated as |
| 214 | left = x1 - 0.5 * dx; right = left + my nx * dx. |
| 215 | Given a sound, for example on the domain [0,...], the value of 'left' with the above formula might |
| 216 | not return exactly xmin but instead a very small deviation (due to the imprecise |
| 217 | representation of real numbers in a computer). |
| 218 | Querying for the value at xmin which is outside the interpolation domain then produces an 'undefined'. |
| 219 | We try to avoid this with the following workaround. |
| 220 | */ |
| 221 | const volatile double xmin = my x1 - 0.5 * my dx; |
| 222 | const volatile double xmax = xmin + my nx * my dx; // don't change this to e.g. my x1 + (my nx - 0.5) * my dx; see Vector_getValueAtX() |
| 223 | Melder_clipLeft (xmin, & tmin); |
| 224 | Melder_clipRight (& tmax, xmax); |
| 225 | // End of workaround |
| 226 | integer k = 1; |
| 227 | his x [k] = tmin; |
| 228 | his y [k ++] = CLIP_Y (level, ymin, ymax); |
| 229 | his x [k] = tmin; |
| 230 | double y = Vector_getValueAtX (me, tmin, channel, kVector_valueInterpolation :: LINEAR); |
| 231 | his y [k ++] = CLIP_Y (y, ymin, ymax); |
| 232 | for (integer i = i1; i <= i2; i ++) { |
| 233 | y = my z [channel] [i]; |
| 234 | his x [k] = my x1 + (i - 1) * my dx; |
| 235 | his y [k ++] = CLIP_Y (y, ymin, ymax); |
| 236 | } |
| 237 | his x [k] = tmax; |
| 238 | y = Vector_getValueAtX (me, tmax, channel, kVector_valueInterpolation :: LINEAR); |
| 239 | his y [k ++] = CLIP_Y (y, ymin, ymax); |
| 240 | his x [k] = tmax; |
| 241 | his y [k ++] = CLIP_Y (level, ymin, ymax); |
| 242 | return him; |
| 243 | } catch (MelderError) { |
| 244 | Melder_throw (me, U":no Polygon created."); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* Area inbetween */ |
| 249 | |
| 250 | autoPolygon Sounds_to_Polygon_enclosed (Sound me, Sound thee, integer channel, double tmin, double tmax, double ymin, double ymax) { |
| 251 | try { |
| 252 | const bool clip = ymin < ymax; |
| 253 | Melder_require (channel > 0 && channel <= my ny && channel <= thy ny, |
| 254 | U"Invalid channel."); |
| 255 | |
| 256 | // find overlap in the domains with xmin workaround as in Sound_to_Polygon |
| 257 | const double xmin1 = my x1 - 0.5 * my dx, xmin2 = thy x1 - 0.5 * thy dx; |
| 258 | const double xmin = ( my xmin > thy xmin ? xmin1 : xmin2 ); |
| 259 | const double xmax = ( my xmax < thy xmax ? xmin1 + my nx * my dx : xmin2 + thy nx * thy dx ); |
| 260 | Melder_require (xmax > xmin, |
| 261 | U"Domains should overlap."); |
| 262 | Melder_require (xmin < xmax, |
| 263 | U"Domains should overlap."); |
| 264 | if (tmin >= tmax) { |
| 265 | tmin = xmin; |
| 266 | tmax = xmax; |
| 267 | } |
| 268 | if (tmin < xmin) |
| 269 | tmin = xmin; |
| 270 | if (tmax > xmax) |
| 271 | tmax = xmax; |
| 272 | Melder_require (tmin < xmax && tmax > xmin, |
| 273 | U"Invalid domain."); |
| 274 | |
| 275 | const integer ib1 = Sampled_xToHighIndex (me, tmin); |
| 276 | const integer ie1 = Sampled_xToLowIndex (me, tmax); |
| 277 | const integer n1 = ie1 - ib1 + 1; |
| 278 | const integer ib2 = Sampled_xToHighIndex (thee, tmin); |
| 279 | const integer ie2 = Sampled_xToLowIndex (thee, tmax); |
| 280 | const integer n2 = ie2 - ib2 + 1; |
| 281 | const integer numberOfPoints = n1 + n2 + 4; // me + thee + begin + endpoint + closing |
| 282 | |
| 283 | autoPolygon him = Polygon_create (numberOfPoints); |
| 284 | /* |
| 285 | my starting point at tmin |
| 286 | */ |
| 287 | double y = Vector_getValueAtX (me, tmin, ( my ny == 1 ? 1 : channel ), kVector_valueInterpolation :: LINEAR); |
| 288 | integer k = 1; |
| 289 | his x [k] = tmin; |
| 290 | his y [k ++] = CLIP_Y (y, ymin, ymax); |
| 291 | |
| 292 | // my samples |
| 293 | |
| 294 | for (integer i = ib1; i <= ie1; i ++) { |
| 295 | double t = my x1 + (i - 1) * my dx; |
| 296 | y = ( my z [my ny == 1 ? 1 : channel] [i] ); |
| 297 | his x [k] = t; |
| 298 | his y [k ++] = CLIP_Y (y, ymin, ymax); |
| 299 | } |
| 300 | |
| 301 | // my end point at tmax |
| 302 | |
| 303 | y = Vector_getValueAtX (me, tmax, ( my ny == 1 ? 1 : channel ), kVector_valueInterpolation :: LINEAR); |
| 304 | his x [k] = tmax; |
| 305 | his y [k ++] = y; |
| 306 | |
| 307 | // thy starting point at tmax |
| 308 | |
| 309 | y = Vector_getValueAtX (thee, tmax, ( thy ny == 1 ? 1 : channel ), kVector_valueInterpolation :: LINEAR); |
| 310 | his x [k] = tmax; |
| 311 | his y [k ++] = y; |
| 312 | |
| 313 | // thy samples |
| 314 | |
| 315 | for (integer i = ie2; i >= ib2; i--) { |
| 316 | double t = thy x1 + (i - 1) * thy dx; |
| 317 | y = thy z [( thy ny == 1 ? 1 : channel )] [i]; |
| 318 | his x [k] = t; |
| 319 | his y [k ++] = CLIP_Y (y, ymin, ymax); |
| 320 | } |
| 321 | |
| 322 | // thy end point at tmin |
| 323 | |
| 324 | y = Vector_getValueAtX (thee, tmin, ( thy ny == 1 ? 1 : channel ), kVector_valueInterpolation :: LINEAR); |
| 325 | his x [k] = tmin; |
| 326 | his y [k] = y; |
| 327 | |
| 328 | Melder_assert (k == numberOfPoints); |
| 329 | return him; |
| 330 | } catch (MelderError) { |
| 331 | Melder_throw (me, U": no enclosed Polygon created."); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | #define INTERSECTION_OUTSIDE 0 |
| 336 | #define INTERSECTION_PROPER 1 |
| 337 | #define INTERSECTION_AT_EDGE 2 |
| 338 | #define INTERSECTION_AT_VERTEX 3 |
| 339 | #define INTERSECTION_COPLANAR 4 |
| 340 | #define INTERSECTION_COPLANAR_AT_EDGE 6 |
| 341 | #define INTERSECTION_COPLANAR_AT_VERTEX 7 |
| 342 | |
| 343 | // 12 is from subject 34 from clipping |
| 344 | static int get_collinearIntersectionPoint (double x1, double x2, double x3, double x4, double *xs) { |
| 345 | bool p3inb12 = (x1 < x3 and x3 <= x2) or (x1 > x3 and x3 >= x2); |
| 346 | bool p4inb12 = (x1 < x4 and x4 <= x2) or (x1 > x4 and x4 >= x2); |
| 347 | int intersection = INTERSECTION_AT_VERTEX; |
| 348 | if (x1 < x2) { |
| 349 | if (x3 < x4) { |
| 350 | if (x2 < x3 || x4 < x1) { |
| 351 | intersection = INTERSECTION_OUTSIDE; |
| 352 | } else if (p3inb12) { |
| 353 | // 4 can be inside or outside |
| 354 | *xs = ( p4inb12 ? x4 : x2 ); |
| 355 | } else if (p4inb12) { |
| 356 | // 3 is outside |
| 357 | *xs = x4; |
| 358 | } else { |
| 359 | // 3 and 4 outside 12 segment |
| 360 | *xs = x2; |
| 361 | intersection = INTERSECTION_AT_EDGE; |
| 362 | } |
| 363 | } else { |
| 364 | // x4 < x3 |
| 365 | if (x2 < x4 || x3 < x1) { |
| 366 | intersection = INTERSECTION_OUTSIDE; |
| 367 | } else if (p3inb12) { |
| 368 | // 4 can be inside or outside |
| 369 | *xs = x4; |
| 370 | } else if (p4inb12) { |
| 371 | // 3 outside |
| 372 | *xs = x4; |
| 373 | } else { |
| 374 | *xs = x2; |
| 375 | intersection = INTERSECTION_AT_EDGE; |
| 376 | } |
| 377 | } |
| 378 | } else { |
| 379 | // x2 < x1 |
| 380 | if (x3 < x4) { |
| 381 | if (x1 < x3 || x4 < x2) { |
| 382 | intersection = INTERSECTION_OUTSIDE; |
| 383 | } else if (p3inb12) { |
| 384 | *xs = ( p4inb12 ? x4 : x3 ); |
| 385 | } else if (p4inb12) { |
| 386 | // 3 outside |
| 387 | *xs = x4; |
| 388 | } else { |
| 389 | *xs = x2; |
| 390 | intersection = INTERSECTION_AT_EDGE; |
| 391 | } |
| 392 | } else { |
| 393 | // x4 < x3 |
| 394 | if (x1 < x4 || x3 < x2) { |
| 395 | intersection = INTERSECTION_OUTSIDE; |
| 396 | } else if (p3inb12) { |
| 397 | *xs = x3; |
| 398 | } else if (p4inb12) { |
| 399 | *xs = x4; |
| 400 | } else { |
| 401 | *xs = x3; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | return intersection; |
| 406 | } |
| 407 | |
| 408 | // Line from a to b : |
| 409 | // (1) ab = (1-s)*a+s*b = a + s*(b-a) = a + s * b' |
| 410 | // (2) cd = c+t*(d-c) = c + t * d' |
| 411 | // where b' = b - a and d' = d - c. |
| 412 | // at intersection: |
| 413 | // (3) a + s * b' = c + t * d' |
| 414 | // Cross (3) with d'; |
| 415 | // cross (a, d') + s * cross (b', d') = cross (c, d') |
| 416 | // s = cross (c - a, d') / cross (b', d') |
| 417 | // Cross (3) with b' |
| 418 | // cross (a, b') = cross (c, b') + t * cross (d', b') |
| 419 | // t = cross (c - a, b') / cross (b', d') |
| 420 | // if cross (b', b') then b' and d' are parallel |
| 421 | // After Paul Rourke |
| 422 | // Area of triangle a,b,c = ((bx-ax)(cy-ay) - (cx-ax)(by-ay)) / 2 |
| 423 | #define POS(ax,ay,bx,by,cx,cy) (bx - ax) * (cy - ay) - (cx - ax) * (by-ay); |
| 424 | // if POS(a,b,c) (> 0 ; <0, 0) then c is on (the left of; the right of; collinear with) the line segment (a,b) |
| 425 | |
| 426 | // Register the crossing of line a(1)b(2) with c(3)d(4), if the crossing is degenerate only register the tip. |
| 427 | static int LineSegments_getIntersection (double x1, double y1, double x2, double y2, double x3, double y3, |
| 428 | double x4, double y4, double *mua, double *mub, double eps) { |
| 429 | // bounding box pre-selection |
| 430 | double min12 = std::min (x1, x2); |
| 431 | double max12 = std::max (x1, x2); |
| 432 | double min34 = std::min (x3, x4); |
| 433 | double max34 = std::max (x3, x4); |
| 434 | if (max12 - min34 < -eps or max34 - min12 < -eps) { |
| 435 | return INTERSECTION_OUTSIDE; // eps? |
| 436 | } |
| 437 | min12 = std::min (y1, y2); |
| 438 | max12 = std::max (y1, y2); |
| 439 | min34 = std::min (y3, y4); |
| 440 | max34 = std::max (y3, y4); |
| 441 | if (max12 - min34 < -eps or max34 - min12 < -eps) { |
| 442 | return INTERSECTION_OUTSIDE; |
| 443 | } |
| 444 | |
| 445 | double cad = (x3 - x1) * (y4 - y3) - (y3 - y1) * (x4 - x3); // cross (c - a, d') |
| 446 | double cab = (x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1); // cross (c - a, b') |
| 447 | double bd = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3); // cross (b', d') |
| 448 | |
| 449 | if (fabs (bd) > eps) { |
| 450 | *mua = cad / bd; |
| 451 | *mub = cab / bd; |
| 452 | if (*mua <= eps || *mua > 1.0 + eps || *mub < eps || *mub > 1.0 + eps) |
| 453 | return INTERSECTION_OUTSIDE; |
| 454 | if (*mua > eps && *mua <= 1.0 - eps && *mub >= eps && *mub < 1.0 - eps) { |
| 455 | // This occurs most of the cases (hopefully) |
| 456 | return INTERSECTION_PROPER; |
| 457 | } |
| 458 | // Now eps < mua,mub <= 1+eps |
| 459 | // and at least one of the mu's is near 1, |
| 460 | // the other is in [eps,1] |
| 461 | if (fabs (*mua - 1.0) < eps) |
| 462 | *mua = 1.0; |
| 463 | if (fabs (*mub - 1.0) < eps) |
| 464 | *mub = 1.0; |
| 465 | // is the intersection at an edge or at vertex |
| 466 | if (*mua == 1.0) // end of ab touches cd |
| 467 | return ( *mub == 1.0 ? INTERSECTION_AT_VERTEX : INTERSECTION_AT_EDGE ); |
| 468 | else // ab crosses a vertex |
| 469 | return INTERSECTION_AT_VERTEX; |
| 470 | } else { // ab and cd are parallel or coplanar |
| 471 | if (fabs (cad) > eps and fabs (cab) > eps) |
| 472 | return INTERSECTION_OUTSIDE; |
| 473 | if (x1 == x2) { |
| 474 | x1 = y1; |
| 475 | x2 = y2; |
| 476 | x3 = y3; |
| 477 | x4 = y4; |
| 478 | } |
| 479 | double xs; |
| 480 | int intersection = get_collinearIntersectionPoint (x1, x2, x3, x4, &xs); |
| 481 | if (intersection != INTERSECTION_OUTSIDE) { |
| 482 | *mua = fabs ((xs - x1) / (x2 - x1)); |
| 483 | *mub = fabs ((xs - x3) / (x4 - x3)); |
| 484 | intersection += INTERSECTION_COPLANAR; |
| 485 | } |
| 486 | return intersection; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | // Vertices: |
| 491 | // first node has prev = 0, last node has next = 0; |
| 492 | // entry marks the entrance of the OTHER polygon |
| 493 | |
| 494 | Thing_define (Vertex, Daata) { |
| 495 | double x, y, alpha; |
| 496 | DoublyLinkedNode neighbour; |
| 497 | integer poly_npoints, id; |
| 498 | int intersect, entry; |
| 499 | bool processed; |
| 500 | |
| 501 | void v1_copy (Daata data_to) const |
| 502 | override; |
| 503 | }; |
| 504 | |
| 505 | Thing_implement (Vertex, Daata, 0); |
| 506 | |
| 507 | void structVertex :: v1_copy (Daata thee_Daata) const { |
| 508 | Vertex thee = static_cast <Vertex> (thee_Daata); |
| 509 | thy x = our x; |
| 510 | thy y = our y; |
| 511 | thy alpha = our alpha; |
| 512 | thy neighbour = our neighbour; |
| 513 | thy poly_npoints = our poly_npoints; |
| 514 | thy id = our id; |
| 515 | thy intersect = our intersect; |
| 516 | thy entry = our entry; |
| 517 | thy processed = our processed; |
| 518 | } |
| 519 | |
| 520 | static autoVertex Vertex_create () { |
| 521 | return Thing_new (Vertex); |
| 522 | } |
| 523 | |
| 524 | Thing_define (Vertices, DoublyLinkedList) { |
| 525 | static int s_compareHook (DoublyLinkedNode me, DoublyLinkedNode thee) noexcept; |
| 526 | Data_CompareHook v_getCompareHook () |
| 527 | override { return s_compareHook; } |
| 528 | }; |
| 529 | |
| 530 | inline static autoVertices Vertices_create () { |
| 531 | return Thing_new (Vertices); |
| 532 | } |
| 533 | |
| 534 | Thing_implement (Vertices, DoublyLinkedList, 0); |
| 535 | |
| 536 | #define VERTEX(n) ((Vertex) ((n) -> data.get())) |
| 537 | |
| 538 | int structVertices :: s_compareHook (DoublyLinkedNode me, DoublyLinkedNode thee) noexcept { |
| 539 | return ( VERTEX (me) -> alpha < VERTEX (thee) -> alpha ? -1 : ( VERTEX (me) -> alpha > VERTEX (thee) -> alpha ? 1 : 0 ) ); |
| 540 | } |
| 541 | |
| 542 | static void Vertices_addCopyBack (Vertices me, DoublyLinkedNode n) { |
| 543 | try { |
| 544 | autoDoublyLinkedNode nc = Data_copy (n); |
| 545 | DoublyLinkedList_addBack (me, nc.releaseToAmbiguousOwner()); |
| 546 | } catch (MelderError) { |
| 547 | Melder_throw (me, U": no copy added."); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | static bool pointsInsideInterval (double *x, integer n, integer istart, integer iend, integer *jstart, integer *jend) { |
| 552 | double xmax = x [istart], xmin = x [istart]; |
| 553 | integer imax = istart, imin = istart; |
| 554 | integer iendmod = ( iend > istart ? iend : iend + n ); // circular |
| 555 | for (integer i = istart + 1; i <= iendmod; i ++) { |
| 556 | integer index = (i - 1) % n + 1; // make it circular |
| 557 | if (x [index] > xmax) { |
| 558 | xmax = x [index]; |
| 559 | imax = index; |
| 560 | } else if (x [index] < xmin) { |
| 561 | xmin = x [index]; |
| 562 | imin = index; |
| 563 | } |
| 564 | } |
| 565 | *jstart = imin; |
| 566 | *jend = imax; |
| 567 | if (x [istart] > x [iend]) { |
| 568 | *jstart = imax; |
| 569 | *jend = imin; |
| 570 | } |
| 571 | if (x [istart] == x [*jstart] and x [iend] == x [*jend]) { // if there are duplicates of the extrema |
| 572 | *jstart = istart; |
| 573 | *jend = iend; |
| 574 | } |
| 575 | return *jstart == istart and * jend == iend; |
| 576 | } |
| 577 | |
| 578 | autoPolygon Polygon_circularPermutation (Polygon me, integer nshift) { |
| 579 | try { |
| 580 | autoPolygon thee = Data_copy (me); |
| 581 | if (nshift != 0) { |
| 582 | for (integer i = 1; i <= my numberOfPoints; i ++) { |
| 583 | integer inew = (i + nshift - 1) % my numberOfPoints + 1; |
| 584 | thy x [inew] = my x [i]; |
| 585 | thy y [inew] = my y [i]; |
| 586 | } |
| 587 | } |
| 588 | return thee; |
| 589 | } catch (MelderError) { |
| 590 | Melder_throw (me, U": not circularly permuted."); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | static void _Polygons_copyNonCollinearities (Polygon me, Polygon thee, integer collstart, integer collend) { |
| 595 | // Determine if all collinear point are within the interval [colstart,colend] |
| 596 | integer jstart, jend; |
| 597 | bool allPointsInside = ( my x [collstart] != my x [collend] ? |
| 598 | pointsInsideInterval (my x.asArgumentToFunctionThatExpectsOneBasedArray(), |
| 599 | my numberOfPoints, collstart, collend, &jstart, &jend) : |
| 600 | pointsInsideInterval (my y.asArgumentToFunctionThatExpectsOneBasedArray(), |
| 601 | my numberOfPoints, collstart, collend, &jstart, &jend) ); |
| 602 | if (not allPointsInside) { |
| 603 | if (collstart != jstart) { // also include the extreme point at start |
| 604 | thy numberOfPoints ++; |
| 605 | thy x [thy numberOfPoints] = my x [jstart]; |
| 606 | thy y [thy numberOfPoints] = my y [jstart]; |
| 607 | } |
| 608 | if (collend != jend) { // also include the extreme point at end |
| 609 | thy numberOfPoints ++; |
| 610 | thy x [thy numberOfPoints] = my x [jend]; |
| 611 | thy y [thy numberOfPoints] = my y [jend]; |
| 612 | } |
| 613 | } |
| 614 | thy numberOfPoints ++; |
| 615 | thy x [thy numberOfPoints] = my x [collend]; thy y [thy numberOfPoints] = my y [collend]; |
| 616 | } |
| 617 | |
| 618 | #define AREA(x1,y1,x2,y2,x3,y3) (x1*(y2 - y3)+x2*(y3-y1)+x3*(y1-y2)) |
| 619 | autoPolygon Polygon_simplify (Polygon me) { |
| 620 | try { |
| 621 | autoPolygon p1 = Data_copy (me); |
| 622 | |
| 623 | // pass 1: remove doublets |
| 624 | integer np = 1; |
| 625 | for (integer i = 2; i <= my numberOfPoints; i ++) { |
| 626 | if (my x [i] != p1 -> x [np] || my y [i] != p1 -> y [np]) { |
| 627 | p1 -> x [ ++ np] = my x [i]; |
| 628 | p1 -> y [np] = my y [i]; |
| 629 | } |
| 630 | } |
| 631 | // last and first points! |
| 632 | if (p1 -> x [np] == p1 -> x [1] && p1 -> y [np] == p1 -> y [1]) { |
| 633 | np --; |
| 634 | } |
| 635 | Melder_require (np > 2, U"Not enough points left after doublet removal."); |
| 636 | |
| 637 | p1 -> numberOfPoints = np; |
| 638 | |
| 639 | // pass 2: remove collinearities |
| 640 | |
| 641 | autoPolygon p = Data_copy (p1.get()); |
| 642 | p -> numberOfPoints = 0; |
| 643 | // is there collinearity between the first and the last points of p1? |
| 644 | double eps = 1e-15; |
| 645 | double area = AREA (p1 -> x [np], p1 -> y [np], p1 -> x [1], p1 -> y [1], p1 -> x [2], p1 -> y [2]); |
| 646 | integer collend = 2; |
| 647 | while (fabs (area) < eps && collend < np) { |
| 648 | collend ++; |
| 649 | area = AREA (p1 -> x [collend - 2], p1 -> y [collend - 2], p1 -> x [collend - 1], p1 -> y [collend - 1], p1 -> x [collend], p1 -> y [collend]); |
| 650 | } |
| 651 | collend--; |
| 652 | area = AREA (p1 -> x [np - 1], p1 -> y [np - 1], p1 -> x [np], p1 -> y [np], p1 -> x [1], p1 -> y [1]); |
| 653 | integer collstart = np - 1; |
| 654 | while (fabs (area) < eps && collstart > collend) { |
| 655 | collstart --; |
| 656 | area = AREA (p1 -> x [collstart], p1 -> y [collstart], p1 -> x [collstart + 1], p1 -> y [collstart + 1], p1 -> x [collstart + 2], p1 -> y [collstart + 2]); |
| 657 | } |
| 658 | collstart ++; |
| 659 | if (collend - collstart + p1 -> numberOfPoints > 1) { |
| 660 | _Polygons_copyNonCollinearities (p1.get(), p.get(), collstart, collend); |
| 661 | } else { |
| 662 | p -> numberOfPoints = 1; |
| 663 | p -> x [1] = p1 -> x [1]; |
| 664 | p -> y [1] = p1 -> y [1]; |
| 665 | } |
| 666 | bool collinearity = false; |
| 667 | integer endpos = collstart - 1; |
| 668 | for (integer i = collend + 1; i <= endpos; i ++) { // start was < numberOfPoints |
| 669 | // if i-1, i, i+1 are coplanar, remove i |
| 670 | // area = x1*(y2 - y3)+x2*(y3-y1)+x3*(y1-y2) |
| 671 | area = AREA (p1 -> x [i - 1], p1 -> y [i - 1], p1 -> x [i], p1 -> y [i], p1 -> x [i + 1], p1 -> y [i + 1]); |
| 672 | if (fabs (area) < eps) { |
| 673 | if (collinearity) { |
| 674 | collend = i + 1; |
| 675 | } else { // start of possibly new series |
| 676 | collinearity = true; |
| 677 | collstart = i - 1; collend = i + 1; |
| 678 | } |
| 679 | if (i != endpos) { |
| 680 | continue; |
| 681 | } |
| 682 | } |
| 683 | if (! collinearity) { |
| 684 | p -> numberOfPoints ++; |
| 685 | p -> x [p -> numberOfPoints] = p1 -> x [i]; |
| 686 | p -> y [p -> numberOfPoints] = p1 -> y [i]; |
| 687 | } else { // end of series of collinearities detected |
| 688 | _Polygons_copyNonCollinearities (p1.get(), p.get(), collstart, collend); |
| 689 | collinearity = false; |
| 690 | } |
| 691 | } |
| 692 | if (not collinearity and collend != endpos + 1) { // only if previous series was not collinear |
| 693 | p -> numberOfPoints ++; |
| 694 | p -> x [p -> numberOfPoints] = p1 -> x [endpos + 1]; |
| 695 | p -> y [p -> numberOfPoints] = p1 -> y [endpos + 1]; |
| 696 | } |
| 697 | Melder_require (p -> numberOfPoints > 2, U"Not enough points left after collinear points removal."); |
| 698 | |
| 699 | autoPolygon thee = Data_copy (p.get()); // |
| 700 | return thee; |
| 701 | } catch (MelderError) { |
| 702 | Melder_throw (me, U": not simplified."); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | #undef AREA |
| 707 | |
| 708 | static autoVertices Polygon_to_Vertices (Polygon me, bool close) { |
| 709 | try { |
| 710 | autoVertices thee = Vertices_create (); |
| 711 | for (integer i = 1 ; i <= my numberOfPoints; i ++) { |
| 712 | autoVertex v = Vertex_create (); |
| 713 | v -> x = my x [i]; v -> y = my y [i]; |
| 714 | autoDoublyLinkedNode n = DoublyLinkedNode_create (v.move()); |
| 715 | DoublyLinkedList_addBack (thee.get(), n.releaseToAmbiguousOwner()); |
| 716 | } |
| 717 | Melder_assert (thy numberOfNodes == my numberOfPoints); |
| 718 | if (close) { |
| 719 | Vertices_addCopyBack (thee.get(), thy front); |
| 720 | } |
| 721 | return thee; |
| 722 | } catch (MelderError) { |
| 723 | Melder_throw (me, U": no Vertices created."); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | #define SKIP_INTERSECTION_NODES(n) while(VERTEX(n) -> intersect != 0) n = n -> next; |
| 728 | |
| 729 | |
| 730 | static void Vertices_print (Vertices me, Vertices thee) { |
| 731 | integer ns = 0, nc = 0, nt, nt2; |
| 732 | // MelderInfo_open(); |
| 733 | DoublyLinkedNode n = my front; |
| 734 | MelderInfo_writeLine (U""); |
| 735 | while (n != 0) { |
| 736 | double x = VERTEX (n) -> x, y = VERTEX (n) -> y, alpha = VERTEX (n) -> alpha; |
| 737 | conststring32 type, itype; |
| 738 | if (VERTEX (n) -> intersect == 0) { |
| 739 | type = U"S"; |
| 740 | ns ++; |
| 741 | nt = ns; |
| 742 | itype = U"-"; |
| 743 | nt2 = 0; |
| 744 | } else { |
| 745 | type = U"I"; |
| 746 | nt = VERTEX (n) -> id; |
| 747 | nt2 = VERTEX (VERTEX (n) -> neighbour) -> id; |
| 748 | itype = Melder_integer (VERTEX (n) -> intersect); |
| 749 | } |
| 750 | MelderInfo_write (type, nt, U" I", itype, U", (", x, U", ", y, U"), "); |
| 751 | MelderInfo_write (alpha, U", E", VERTEX (n) -> entry, U"(", nt2, U")\n"); |
| 752 | n = n -> next; |
| 753 | } |
| 754 | MelderInfo_writeLine (U""); |
| 755 | n = thy front; |
| 756 | while (n != 0) { |
| 757 | double x = VERTEX (n) -> x, y = VERTEX (n) -> y, alpha = VERTEX (n) -> alpha; |
| 758 | conststring32 type, itype; |
| 759 | if (VERTEX (n) -> intersect == 0) { |
| 760 | type = U"C"; |
| 761 | nc ++; |
| 762 | nt = nc; |
| 763 | itype = U"-"; |
| 764 | nt2 = 0; |
| 765 | } else { |
| 766 | type = U"I"; |
| 767 | nt = VERTEX (n) -> id; |
| 768 | nt2 = VERTEX (VERTEX (n) -> neighbour) -> id; |
| 769 | itype = Melder_integer (VERTEX (n) -> intersect); |
| 770 | } |
| 771 | MelderInfo_write (type, nt, U" I", itype, U", (", x, U", ", y, U"), "); |
| 772 | MelderInfo_write (alpha, U", E", VERTEX (n) -> entry, U"(", nt2, U")\n"); |
| 773 | n = n -> next; |
| 774 | } |
| 775 | // MelderInfo_close(); |
| 776 | } |
| 777 | |
| 778 | static void Vertices_sortIntersections (Vertices me) { |
| 779 | DoublyLinkedNode ni = my front, first; |
| 780 | bool intersections = false; |
| 781 | while (ni != my back) { |
| 782 | if (VERTEX (ni) -> intersect == 0) { |
| 783 | if (intersections) { // the previous was the last of a series of intersections |
| 784 | DoublyLinkedList_sortPart ( (DoublyLinkedList) me, first, ni -> prev); |
| 785 | // restore myNode pointers |
| 786 | intersections = false; |
| 787 | } |
| 788 | } else { |
| 789 | if (not intersections) { |
| 790 | first = ni; |
| 791 | intersections = true; |
| 792 | } |
| 793 | } |
| 794 | ni = ni -> next; |
| 795 | } |
| 796 | // we might have missed the last series of intersections |
| 797 | if (intersections) { |
| 798 | DoublyLinkedList_sortPart ( (DoublyLinkedList) me, first, ni -> prev); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | static void Vertices_addIntersections (Vertices me, Vertices thee) { |
| 803 | try { |
| 804 | double eps = 1e-15; |
| 805 | integer id = 0; |
| 806 | if (my numberOfNodes < 4 || thy numberOfNodes < 4) { |
| 807 | Melder_throw (U"We need at least three vertices."); |
| 808 | } |
| 809 | DoublyLinkedNode ni = my front; // the node index in me (s) |
| 810 | while (ni != my back) { // until penultimate |
| 811 | double x1 = VERTEX (ni) -> x, y1 = VERTEX (ni) -> y; |
| 812 | double x2 = VERTEX (ni -> next) -> x, y2 = VERTEX (ni -> next) -> y; |
| 813 | DoublyLinkedNode nj = thy front; // the current node index in thee (c) |
| 814 | while (nj != thy back && VERTEX (nj) -> intersect == 0) { |
| 815 | DoublyLinkedNode njn = nj -> next; |
| 816 | SKIP_INTERSECTION_NODES (njn) |
| 817 | double x3 = VERTEX (nj) -> x, y3 = VERTEX (nj) -> y; |
| 818 | double x4 = VERTEX (njn) -> x, y4 = VERTEX (njn) -> y, mua, mub; |
| 819 | int intersection = LineSegments_getIntersection (x1, y1, x2, y2, x3, y3, x4, y4, &mua, &mub, eps); |
| 820 | if (intersection != INTERSECTION_OUTSIDE) { |
| 821 | id ++; |
| 822 | // 1. create the vertices |
| 823 | autoVertex ins = Vertex_create (); |
| 824 | ins -> x = x1 + mua * (x2 - x1); |
| 825 | ins -> y = y1 + mua * (y2 - y1); |
| 826 | ins -> alpha = mua; |
| 827 | ins -> intersect = intersection; |
| 828 | ins -> id = id; |
| 829 | autoVertex inc = Data_copy (ins.get()); |
| 830 | inc -> alpha = mub; |
| 831 | // 2. create the nodes |
| 832 | autoDoublyLinkedNode ns = DoublyLinkedNode_create (autoDaata()); |
| 833 | autoDoublyLinkedNode nc = DoublyLinkedNode_create (autoDaata()); |
| 834 | // 3. link the neighbours + copy the links |
| 835 | DoublyLinkedNode njc = ins -> neighbour = nc.get(); |
| 836 | DoublyLinkedNode nic = inc -> neighbour = ns.get(); |
| 837 | // 4. transfer the vertices to the nodes |
| 838 | ns -> data = ins.move(); |
| 839 | nc -> data = inc.move(); |
| 840 | // 5. add the nodes to the list |
| 841 | DoublyLinkedList_addAfter (me, ni, ns.releaseToAmbiguousOwner()); |
| 842 | DoublyLinkedList_addAfter (thee, nj, nc.releaseToAmbiguousOwner()); |
| 843 | // 6. set node pointer to inserted nodes |
| 844 | ni = nic; nj = njc; |
| 845 | } |
| 846 | nj = nj -> next; |
| 847 | SKIP_INTERSECTION_NODES (nj) |
| 848 | } |
| 849 | ni = ni -> next; |
| 850 | } |
| 851 | if (Melder_debug == -1) { |
| 852 | MelderInfo_open(); |
| 853 | Vertices_print (me, thee); |
| 854 | } |
| 855 | Vertices_sortIntersections (me); |
| 856 | Vertices_sortIntersections (thee); |
| 857 | if (Melder_debug == -1) { |
| 858 | Vertices_print (me, thee); |
| 859 | } |
| 860 | } catch (MelderError) { |
| 861 | Melder_throw (U"Intersections not calculated."); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | #define Polygon_EN 1 |
| 866 | #define Polygon_EX 2 |
| 867 | #define Polygon_ENEX 3 |
| 868 | #define Polygon_EXEN 4 |
| 869 | |
| 870 | static void Vertices_markEntryPoints (Vertices me, int firstLocation) { |
| 871 | int entry = ( firstLocation == Polygon_INSIDE ? Polygon_EX : ( firstLocation == Polygon_OUTSIDE ? Polygon_EN : Polygon_ENEX ) ); // problematic when on boundary |
| 872 | // my back/front can never be an intersection node |
| 873 | for (DoublyLinkedNode ni = my front -> next; ni != my back; ni = ni -> next) { |
| 874 | if (VERTEX (ni) -> intersect == 0) { |
| 875 | continue; |
| 876 | } |
| 877 | VERTEX (ni) -> entry = entry; |
| 878 | entry = ( entry == Polygon_EN ? Polygon_EX : ( entry == Polygon_EX ? Polygon_EN : Polygon_ENEX ) ); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | static autoVertices Verticeses_connectClippingPathsUnion (Vertices me, Vertices thee) { |
| 883 | try { |
| 884 | // find my first vertex outside thee, by searching the first intersection entry |
| 885 | |
| 886 | DoublyLinkedNode firstOutside = my front; |
| 887 | bool firstOutsideSet = true; |
| 888 | for (DoublyLinkedNode ni = my front; ni != 0; ni = ni -> next) { |
| 889 | if (VERTEX (ni) -> intersect == 0) { // |
| 890 | if (firstOutsideSet) { |
| 891 | continue; |
| 892 | } |
| 893 | firstOutside = ni; break; |
| 894 | } else if (VERTEX (ni) -> entry == Polygon_EN) { |
| 895 | break; |
| 896 | } |
| 897 | // We were inside |
| 898 | else { |
| 899 | firstOutsideSet = false; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | autoVertices him = Vertices_create (); |
| 904 | integer poly_npoints = 0; |
| 905 | DoublyLinkedNode current = firstOutside; |
| 906 | bool inside = false, forward = true; |
| 907 | do { |
| 908 | if (VERTEX (current) -> intersect == 0) { |
| 909 | Vertices_addCopyBack (him.get(), current); poly_npoints ++; |
| 910 | } else { // intersection point |
| 911 | // store and jump to other polygon |
| 912 | Vertices_addCopyBack (him.get(), current); poly_npoints ++; |
| 913 | current = VERTEX (current) -> neighbour; inside = not inside; |
| 914 | forward = VERTEX (current) -> entry == Polygon_EX; |
| 915 | } |
| 916 | if (forward) { |
| 917 | current = current -> next; |
| 918 | if (current == 0) { |
| 919 | current = inside ? thy front : my front; |
| 920 | } |
| 921 | } else { |
| 922 | current = current -> prev; |
| 923 | if (current == 0) { |
| 924 | current = ( inside ? thy back : my back ); |
| 925 | } |
| 926 | } |
| 927 | } while (current != firstOutside and current != 0 and poly_npoints < my numberOfNodes); |
| 928 | VERTEX (his front) -> poly_npoints = poly_npoints; |
| 929 | return him; |
| 930 | } catch (MelderError) { |
| 931 | Melder_throw (me, U": no clipping path."); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | //Vertices Verticeses_connectClippingPaths (Vertices me, bool use_myinterior, Vertices thee, bool use_thyinterior); |
| 936 | static autoVertices Verticeses_connectClippingPaths (Vertices me, bool /* use_myinterior */, Vertices thee, bool /* use_thyinterior */) { |
| 937 | try { |
| 938 | autoVertices him = Vertices_create (); |
| 939 | DoublyLinkedNode prevPoly = nullptr; |
| 940 | integer poly_npoints = 0; |
| 941 | for (DoublyLinkedNode ni = my front; ni != 0; ni = ni -> next) { |
| 942 | if ( (VERTEX (ni) -> intersect == 0) || VERTEX (ni) -> processed) { |
| 943 | continue; |
| 944 | } |
| 945 | // Intersection found: start new polygon |
| 946 | DoublyLinkedNode currentPoly = his back; |
| 947 | if (currentPoly == his front) { |
| 948 | Vertices_addCopyBack (him.get(), ni); poly_npoints ++; |
| 949 | prevPoly = his front; |
| 950 | } else { |
| 951 | Vertices_addCopyBack (him.get(), ni); poly_npoints ++; |
| 952 | VERTEX (prevPoly) -> poly_npoints = poly_npoints; |
| 953 | poly_npoints = 0; |
| 954 | prevPoly = currentPoly; |
| 955 | } |
| 956 | DoublyLinkedNode current = ni; |
| 957 | VERTEX (current) -> processed = true; |
| 958 | integer jumps = 0; |
| 959 | do { |
| 960 | if (VERTEX (current) -> entry == Polygon_EN) { |
| 961 | while ( (current = current -> next) != 0 and VERTEX (current) -> intersect == 0) { |
| 962 | Vertices_addCopyBack (him.get(), current); poly_npoints ++; |
| 963 | } |
| 964 | if (current == 0) { // back of list? Goto front |
| 965 | current = ( jumps % 2 == 0 ? my front : thy front ); |
| 966 | while ( (current = current -> next) != 0 and VERTEX (current) -> intersect == 0) { |
| 967 | Vertices_addCopyBack (him.get(), current); poly_npoints ++; |
| 968 | } |
| 969 | Vertices_addCopyBack (him.get(), current); poly_npoints ++; // intersection point |
| 970 | } else if (current == ni) { |
| 971 | break; // done |
| 972 | } else { |
| 973 | current = current -> prev; |
| 974 | } |
| 975 | } else if (VERTEX (current) -> entry == Polygon_EX) { |
| 976 | while ( (current = current -> prev) != 0 and VERTEX (current) -> intersect == 0) { |
| 977 | Vertices_addCopyBack (him.get(), current); poly_npoints ++; |
| 978 | } |
| 979 | if (current == 0) { // start of list? Goto end |
| 980 | current = ( jumps % 2 == 0 ? my back : thy back ); |
| 981 | while ( (current = current -> prev) != 0 and VERTEX (current) -> intersect == 0) { |
| 982 | Vertices_addCopyBack (him.get(), current); poly_npoints ++; |
| 983 | } |
| 984 | Vertices_addCopyBack (him.get(), current); poly_npoints ++; // intersection point |
| 985 | } else if (current == ni) { |
| 986 | break; // done |
| 987 | } else { |
| 988 | current = current -> next; |
| 989 | } |
| 990 | } else { |
| 991 | } |
| 992 | VERTEX (current) -> processed = true; |
| 993 | current = VERTEX (current) -> neighbour; jumps ++; |
| 994 | VERTEX (current) -> processed = true; |
| 995 | } while (current != ni); // polygon closed |
| 996 | } |
| 997 | // last polygon: |
| 998 | VERTEX (prevPoly) -> poly_npoints = poly_npoints; |
| 999 | return him; |
| 1000 | } catch (MelderError) { |
| 1001 | Melder_throw (me, U": no clipping vertices created."); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | // |
| 1006 | static autoPolygon Vertices_to_Polygon (Vertices /* me */, DoublyLinkedNode *ni) { |
| 1007 | DoublyLinkedNode n = *ni; |
| 1008 | try { |
| 1009 | integer i = 1, nPoints = VERTEX (n) -> poly_npoints; |
| 1010 | if (nPoints == 0) { |
| 1011 | Melder_throw (U"No number info."); |
| 1012 | } |
| 1013 | autoPolygon thee = Polygon_create (nPoints); |
| 1014 | thy x [i] = VERTEX (n) -> x; thy y [i] = VERTEX (n) -> y; |
| 1015 | while ((n = n -> next) != 0 && VERTEX (n) -> poly_npoints == 0 && i <= nPoints) { |
| 1016 | i ++; |
| 1017 | thy x [i] = VERTEX (n) -> x; |
| 1018 | thy y [i] = VERTEX (n) -> y; |
| 1019 | } |
| 1020 | *ni = n; |
| 1021 | return thee; |
| 1022 | } catch (MelderError) { |
| 1023 | Melder_throw (U"Polygon not created."); |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | Thing_implement (PolygonBag, Collection, 0); |
| 1028 | |
| 1029 | static autoPolygonBag Vertices_to_Polygons (Vertices me) { |
| 1030 | try { |
| 1031 | autoPolygonBag thee = PolygonBag_create (); |
| 1032 | DoublyLinkedNode node = my front; |
| 1033 | do { |
| 1034 | autoPolygon polygon = Vertices_to_Polygon (me, & node); |
| 1035 | thy addItem_move (polygon.move()); |
| 1036 | } while (node); |
| 1037 | return thee; |
| 1038 | } catch (MelderError) { |
| 1039 | Melder_throw (me, U": no polygon collection created."); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | static autoPolygonBag Polygons_findClippings (Polygon me, bool use_myinterior, Polygon thee, bool use_thyinterior) { |
| 1044 | try { |
| 1045 | autoVertices subject = Polygon_to_Vertices (me, true); |
| 1046 | integer ns = subject -> numberOfNodes; |
| 1047 | autoVertices clip = Polygon_to_Vertices (thee, true); |
| 1048 | // integer nc = clip -> numberOfNodes; |
| 1049 | double eps = 1e-15; |
| 1050 | |
| 1051 | // phase 1: Get all intersections and add them to both lists |
| 1052 | |
| 1053 | Vertices_addIntersections (subject.get(), clip.get()); |
| 1054 | integer nnewnodes = subject -> numberOfNodes - ns; |
| 1055 | int firstLocation = Polygon_getLocationOfPoint (thee, my x [1], my y [1], eps); |
| 1056 | if (nnewnodes == 0) { // no crossings, either one completely inside the other or separate |
| 1057 | autoPolygonBag polygons; |
| 1058 | if (! use_myinterior && ! use_thyinterior && firstLocation == Polygon_INSIDE) { |
| 1059 | autoPolygon copy = Data_copy (thee); |
| 1060 | polygons -> addItem_move (copy.move()); |
| 1061 | } else { |
| 1062 | autoPolygon copy = Data_copy (me); |
| 1063 | polygons -> addItem_move (copy.move()); |
| 1064 | } |
| 1065 | return polygons; |
| 1066 | } |
| 1067 | |
| 1068 | // phase 2: Determine intersections as entry / exit points |
| 1069 | |
| 1070 | Vertices_markEntryPoints (subject.get(), firstLocation); |
| 1071 | |
| 1072 | firstLocation = Polygon_getLocationOfPoint (me, thy x [1], thy y [1], eps); |
| 1073 | Vertices_markEntryPoints (subject.get(), firstLocation); |
| 1074 | if (Melder_debug == -1) { |
| 1075 | Vertices_print (subject.get(), clip.get()); |
| 1076 | MelderInfo_close(); |
| 1077 | Melder_throw (U"Bail out of Polygons_findClippings."); |
| 1078 | } |
| 1079 | |
| 1080 | // phase 3: Determine the clipping paths |
| 1081 | // use_myinterior use_thyinterior result |
| 1082 | // true false diff me - thee |
| 1083 | // false false union |
| 1084 | // true true clip |
| 1085 | // false true diff thee - me |
| 1086 | autoVertices pgs; |
| 1087 | if (! use_myinterior && ! use_thyinterior) { |
| 1088 | pgs = Verticeses_connectClippingPathsUnion (subject.get(), clip.get()); |
| 1089 | } else { |
| 1090 | pgs = Verticeses_connectClippingPaths (subject.get(), use_myinterior, clip.get(), use_thyinterior); |
| 1091 | } |
| 1092 | // phase 4: to Polygons |
| 1093 | |
| 1094 | autoPolygonBag polygons = Vertices_to_Polygons (pgs.get()); |
| 1095 | return polygons; |
| 1096 | } catch (MelderError) { |
| 1097 | Melder_throw (me, U": no union Polygon created."); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | autoPolygonBag Polygons_clip (Polygon subject, Polygon clipper) { |
| 1102 | try { |
| 1103 | autoPolygonBag him = Polygons_findClippings (subject, true, clipper, true); |
| 1104 | return him; |
| 1105 | } catch (MelderError) { |
| 1106 | Melder_throw (subject, U": no union created."); |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | autoPolygon Polygons_union (Polygon me, Polygon thee) { |
| 1111 | try { |
| 1112 | autoPolygonBag him = Polygons_findClippings (me, false, thee, false); |
| 1113 | //Melder_assert (his size == 1); |
| 1114 | autoPolygon p = his subtractItem_move (1); |
| 1115 | return p; |
| 1116 | } catch (MelderError) { |
| 1117 | Melder_throw (me, U": no union created."); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | #define CROSSING (my y [i] < y0) != (my y [ip1] < y0) |
| 1122 | #define AREA { a = (my x [i]-x0)*(my y [ip1]-y0) - (my x [ip1]-x0)*(my y [i]-y0); if (fabs (a) <= eps) return Polygon_EDGE; } |
| 1123 | #define RIGHT_CROSSING (a > 0) == (my y [ip1] > my y [i]) |
| 1124 | #define MODIFY_CROSSING_NUMBER { if (my y [ip1] > my y [i]) nup ++; else nup--; } |
| 1125 | |
| 1126 | int Polygon_getLocationOfPoint (Polygon me, double x0, double y0, double eps) { |
| 1127 | if (my y [1] == y0 and my x [1] == x0) { |
| 1128 | return Polygon_VERTEX; |
| 1129 | } |
| 1130 | |
| 1131 | integer nup = 0; |
| 1132 | for (integer i = 1; i <= my numberOfPoints; i ++) { |
| 1133 | double a; |
| 1134 | integer ip1 = ( i < my numberOfPoints ? i + 1 : 1 ); |
| 1135 | if (my y [ip1] == y0) { |
| 1136 | if (my x [ip1] == x0) { |
| 1137 | return Polygon_VERTEX; |
| 1138 | } else if (my y [i] == y0 && ( my x [ip1] > x0 ) == ( my x [i] < x0 )) { |
| 1139 | return Polygon_EDGE; |
| 1140 | } |
| 1141 | } |
| 1142 | if (CROSSING) { |
| 1143 | if (my x [i] >= x0) { |
| 1144 | if (my x [ip1] > x0) MODIFY_CROSSING_NUMBER |
| 1145 | else { |
| 1146 | AREA |
| 1147 | if (RIGHT_CROSSING) MODIFY_CROSSING_NUMBER |
| 1148 | } |
| 1149 | } else { |
| 1150 | if (my x [ip1] > x0) { |
| 1151 | AREA |
| 1152 | if (RIGHT_CROSSING) MODIFY_CROSSING_NUMBER |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | return ( nup % 2 == 0 ? Polygon_OUTSIDE : Polygon_INSIDE ); |
| 1158 | } |
| 1159 | |
| 1160 | static inline double cross (double x1, double y1, double x2, double y2, double x3, double y3) { |
| 1161 | return (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1); |
| 1162 | } |
| 1163 | |
| 1164 | // Code adapted from http://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain#C |
| 1165 | autoPolygon Polygon_convexHull (Polygon me) { |
| 1166 | try { |
| 1167 | if (my numberOfPoints <= 3) return Data_copy (me); |
| 1168 | |
| 1169 | autoVEC x = raw_VEC (my numberOfPoints), y = raw_VEC (my numberOfPoints); |
| 1170 | autoINTVEC hull = raw_INTVEC (my numberOfPoints + 2); |
| 1171 | for (integer i = 1; i <= my numberOfPoints; i ++) { |
| 1172 | x [i] = my x [i]; |
| 1173 | y [i] = my y [i]; |
| 1174 | } |
| 1175 | NUMsortTogether (x.get(), y.get()); |
| 1176 | // lower hull |
| 1177 | integer n = 1; |
| 1178 | for (integer i = 1; i <= my numberOfPoints; i ++) { |
| 1179 | while (n > 2 && cross (x [hull [n - 2]], y [hull [n - 2]], x [hull [n - 1]], y [hull [n - 1]], x [i], y [i]) <= 0) |
| 1180 | --n; // counter clockwise turn |
| 1181 | hull [n ++] = i; |
| 1182 | } |
| 1183 | // upper hull |
| 1184 | integer t = n + 1; |
| 1185 | for (integer i = my numberOfPoints - 1; i >= 1; i--) { |
| 1186 | while (n >= t && cross (x [hull [n - 2]], y [hull [n - 2]], x [hull [n - 1]], y [hull [n - 1]], x [i], y [i]) <= 0) |
| 1187 | --n; |
| 1188 | hull [n ++] = i; |
| 1189 | } |
| 1190 | autoPolygon thee = Polygon_create (n - 1); |
| 1191 | for (integer i = 1; i <= n - 1; i ++) { |
| 1192 | thy x [i] = x [hull [i]]; |
| 1193 | thy y [i] = y [hull [i]]; |
| 1194 | } |
| 1195 | return thee; |
| 1196 | } catch (MelderError) { |
| 1197 | Melder_throw (me, U": no convex hull polygon created."); |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | double Polygon_getAreaOfConvexHull (Polygon me) { |
| 1202 | try { |
| 1203 | autoPolygon thee = Polygon_convexHull (me); |
| 1204 | return Polygon_area (thee.get()); |
| 1205 | } catch (MelderError) { |
| 1206 | Melder_clearError (); |
| 1207 | return undefined; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /* End of file Polygon_extensions.cpp */ |