| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #include "vmatrix.h" |
| 20 | #include <vglobal.h> |
| 21 | #include <cassert> |
| 22 | #include <cmath> |
| 23 | #include <cstring> |
| 24 |
| 25 | V_BEGIN_NAMESPACE |
| 26 |
| 27 | /* m11 m21 mtx |
| 28 | * m12 m22 mty |
| 29 | * m13 m23 m33 |
| 30 | */ |
| 31 |
| 32 | inline float VMatrix::determinant() const |
| 33 | { |
| 34 | return m11 * (m33 * m22 - mty * m23) - m21 * (m33 * m12 - mty * m13) + |
| 35 | mtx * (m23 * m12 - m22 * m13); |
| 36 | } |
| 37 |
| 38 | bool VMatrix::isAffine() const |
| 39 | { |
| 40 | return type() < MatrixType::Project; |
| 41 | } |
| 42 |
| 43 | bool VMatrix::isIdentity() const |
| 44 | { |
| 45 | return type() == MatrixType::None; |
| 46 | } |
| 47 |
| 48 | bool VMatrix::isInvertible() const |
| 49 | { |
| 50 | return !vIsZero(determinant()); |
| 51 | } |
| 52 |
| 53 | bool VMatrix::isScaling() const |
| 54 | { |
| 55 | return type() >= MatrixType::Scale; |
| 56 | } |
| 57 | bool VMatrix::isRotating() const |
| 58 | { |
| 59 | return type() >= MatrixType::Rotate; |
| 60 | } |
| 61 |
| 62 | bool VMatrix::isTranslating() const |
| 63 | { |
| 64 | return type() >= MatrixType::Translate; |
| 65 | } |
| 66 |
| 67 | VMatrix &VMatrix::operator*=(float num) |
| 68 | { |
| 69 | if (num == 1.) return *this; |
| 70 |
| 71 | m11 *= num; |
| 72 | m12 *= num; |
| 73 | m13 *= num; |
| 74 | m21 *= num; |
| 75 | m22 *= num; |
| 76 | m23 *= num; |
| 77 | mtx *= num; |
| 78 | mty *= num; |
| 79 | m33 *= num; |
| 80 | if (dirty < MatrixType::Scale) dirty = MatrixType::Scale; |
| 81 |
| 82 | return *this; |
| 83 | } |
| 84 |
| 85 | VMatrix &VMatrix::operator/=(float div) |
| 86 | { |
| 87 | if (div == 0) return *this; |
| 88 |
| 89 | div = 1 / div; |
| 90 | return operator*=(div); |
| 91 | } |
| 92 |
| 93 | VMatrix::MatrixType VMatrix::type() const |
| 94 | { |
| 95 | if (dirty == MatrixType::None || dirty < mType) return mType; |
| 96 |
| 97 | switch (dirty) { |
| 98 | case MatrixType::Project: |
| 99 | if (!vIsZero(m13) || !vIsZero(m23) || !vIsZero(m33 - 1)) { |
| 100 | mType = MatrixType::Project; |
| 101 | break; |
| 102 | } |
| 103 | VECTOR_FALLTHROUGH |
| 104 | case MatrixType::Shear: |
| 105 | case MatrixType::Rotate: |
| 106 | if (!vIsZero(m12) || !vIsZero(m21)) { |
| 107 | const float dot = m11 * m12 + m21 * m22; |
| 108 | if (vIsZero(dot)) |
| 109 | mType = MatrixType::Rotate; |
| 110 | else |
| 111 | mType = MatrixType::Shear; |
| 112 | break; |
| 113 | } |
| 114 | VECTOR_FALLTHROUGH |
| 115 | case MatrixType::Scale: |
| 116 | if (!vIsZero(m11 - 1) || !vIsZero(m22 - 1)) { |
| 117 | mType = MatrixType::Scale; |
| 118 | break; |
| 119 | } |
| 120 | VECTOR_FALLTHROUGH |
| 121 | case MatrixType::Translate: |
| 122 | if (!vIsZero(mtx) || !vIsZero(mty)) { |
| 123 | mType = MatrixType::Translate; |
| 124 | break; |
| 125 | } |
| 126 | VECTOR_FALLTHROUGH |
| 127 | case MatrixType::None: |
| 128 | mType = MatrixType::None; |
| 129 | break; |
| 130 | } |
| 131 |
| 132 | dirty = MatrixType::None; |
| 133 | return mType; |
| 134 | } |
| 135 |
| 136 | VMatrix &VMatrix::translate(float dx, float dy) |
| 137 | { |
| 138 | if (dx == 0 && dy == 0) return *this; |
| 139 |
| 140 | switch (type()) { |
| 141 | case MatrixType::None: |
| 142 | mtx = dx; |
| 143 | mty = dy; |
| 144 | break; |
| 145 | case MatrixType::Translate: |
| 146 | mtx += dx; |
| 147 | mty += dy; |
| 148 | break; |
| 149 | case MatrixType::Scale: |
| 150 | mtx += dx * m11; |
| 151 | mty += dy * m22; |
| 152 | break; |
| 153 | case MatrixType::Project: |
| 154 | m33 += dx * m13 + dy * m23; |
| 155 | VECTOR_FALLTHROUGH |
| 156 | case MatrixType::Shear: |
| 157 | case MatrixType::Rotate: |
| 158 | mtx += dx * m11 + dy * m21; |
| 159 | mty += dy * m22 + dx * m12; |
| 160 | break; |
| 161 | } |
| 162 | if (dirty < MatrixType::Translate) dirty = MatrixType::Translate; |
| 163 | return *this; |
| 164 | } |
| 165 |
| 166 | VMatrix &VMatrix::scale(float sx, float sy) |
| 167 | { |
| 168 | if (sx == 1 && sy == 1) return *this; |
| 169 |
| 170 | switch (type()) { |
| 171 | case MatrixType::None: |
| 172 | case MatrixType::Translate: |
| 173 | m11 = sx; |
| 174 | m22 = sy; |
| 175 | break; |
| 176 | case MatrixType::Project: |
| 177 | m13 *= sx; |
| 178 | m23 *= sy; |
| 179 | VECTOR_FALLTHROUGH |
| 180 | case MatrixType::Rotate: |
| 181 | case MatrixType::Shear: |
| 182 | m12 *= sx; |
| 183 | m21 *= sy; |
| 184 | VECTOR_FALLTHROUGH |
| 185 | case MatrixType::Scale: |
| 186 | m11 *= sx; |
| 187 | m22 *= sy; |
| 188 | break; |
| 189 | } |
| 190 | if (dirty < MatrixType::Scale) dirty = MatrixType::Scale; |
| 191 | return *this; |
| 192 | } |
| 193 |
| 194 | VMatrix &VMatrix::shear(float sh, float sv) |
| 195 | { |
| 196 | if (sh == 0 && sv == 0) return *this; |
| 197 |
| 198 | switch (type()) { |
| 199 | case MatrixType::None: |
| 200 | case MatrixType::Translate: |
| 201 | m12 = sv; |
| 202 | m21 = sh; |
| 203 | break; |
| 204 | case MatrixType::Scale: |
| 205 | m12 = sv * m22; |
| 206 | m21 = sh * m11; |
| 207 | break; |
| 208 | case MatrixType::Project: { |
| 209 | float tm13 = sv * m23; |
| 210 | float tm23 = sh * m13; |
| 211 | m13 += tm13; |
| 212 | m23 += tm23; |
| 213 | VECTOR_FALLTHROUGH |
| 214 | } |
| 215 | case MatrixType::Rotate: |
| 216 | case MatrixType::Shear: { |
| 217 | float tm11 = sv * m21; |
| 218 | float tm22 = sh * m12; |
| 219 | float tm12 = sv * m22; |
| 220 | float tm21 = sh * m11; |
| 221 | m11 += tm11; |
| 222 | m12 += tm12; |
| 223 | m21 += tm21; |
| 224 | m22 += tm22; |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | if (dirty < MatrixType::Shear) dirty = MatrixType::Shear; |
| 229 | return *this; |
| 230 | } |
| 231 |
| 232 | static const float deg2rad = float(0.017453292519943295769); // pi/180 |
| 233 | static const float inv_dist_to_plane = 1. / 1024.; |
| 234 |
| 235 | VMatrix &VMatrix::rotate(float a, Axis axis) |
| 236 | { |
| 237 | if (a == 0) return *this; |
| 238 |
| 239 | float sina = 0; |
| 240 | float cosa = 0; |
| 241 | if (a == 90. || a == -270.) |
| 242 | sina = 1.; |
| 243 | else if (a == 270. || a == -90.) |
| 244 | sina = -1.; |
| 245 | else if (a == 180.) |
| 246 | cosa = -1.; |
| 247 | else { |
| 248 | float b = deg2rad * a; // convert to radians |
| 249 | sina = std::sin(b); // fast and convenient |
| 250 | cosa = std::cos(b); |
| 251 | } |
| 252 |
| 253 | if (axis == Axis::Z) { |
| 254 | switch (type()) { |
| 255 | case MatrixType::None: |
| 256 | case MatrixType::Translate: |
| 257 | m11 = cosa; |
| 258 | m12 = sina; |
| 259 | m21 = -sina; |
| 260 | m22 = cosa; |
| 261 | break; |
| 262 | case MatrixType::Scale: { |
| 263 | float tm11 = cosa * m11; |
| 264 | float tm12 = sina * m22; |
| 265 | float tm21 = -sina * m11; |
| 266 | float tm22 = cosa * m22; |
| 267 | m11 = tm11; |
| 268 | m12 = tm12; |
| 269 | m21 = tm21; |
| 270 | m22 = tm22; |
| 271 | break; |
| 272 | } |
| 273 | case MatrixType::Project: { |
| 274 | float tm13 = cosa * m13 + sina * m23; |
| 275 | float tm23 = -sina * m13 + cosa * m23; |
| 276 | m13 = tm13; |
| 277 | m23 = tm23; |
| 278 | VECTOR_FALLTHROUGH |
| 279 | } |
| 280 | case MatrixType::Rotate: |
| 281 | case MatrixType::Shear: { |
| 282 | float tm11 = cosa * m11 + sina * m21; |
| 283 | float tm12 = cosa * m12 + sina * m22; |
| 284 | float tm21 = -sina * m11 + cosa * m21; |
| 285 | float tm22 = -sina * m12 + cosa * m22; |
| 286 | m11 = tm11; |
| 287 | m12 = tm12; |
| 288 | m21 = tm21; |
| 289 | m22 = tm22; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | if (dirty < MatrixType::Rotate) dirty = MatrixType::Rotate; |
| 294 | } else { |
| 295 | VMatrix result; |
| 296 | if (axis == Axis::Y) { |
| 297 | result.m11 = cosa; |
| 298 | result.m13 = -sina * inv_dist_to_plane; |
| 299 | } else { |
| 300 | result.m22 = cosa; |
| 301 | result.m23 = -sina * inv_dist_to_plane; |
| 302 | } |
| 303 | result.mType = MatrixType::Project; |
| 304 | *this = result * *this; |
| 305 | } |
| 306 |
| 307 | return *this; |
| 308 | } |
| 309 |
| 310 | VMatrix VMatrix::operator*(const VMatrix &m) const |
| 311 | { |
| 312 | const MatrixType otherType = m.type(); |
| 313 | if (otherType == MatrixType::None) return *this; |
| 314 |
| 315 | const MatrixType thisType = type(); |
| 316 | if (thisType == MatrixType::None) return m; |
| 317 |
| 318 | VMatrix t; |
| 319 | MatrixType type = vMax(thisType, otherType); |
| 320 | switch (type) { |
| 321 | case MatrixType::None: |
| 322 | break; |
| 323 | case MatrixType::Translate: |
| 324 | t.mtx = mtx + m.mtx; |
| 325 | t.mty += mty + m.mty; |
| 326 | break; |
| 327 | case MatrixType::Scale: { |
| 328 | float m11v = m11 * m.m11; |
| 329 | float m22v = m22 * m.m22; |
| 330 |
| 331 | float m31v = mtx * m.m11 + m.mtx; |
| 332 | float m32v = mty * m.m22 + m.mty; |
| 333 |
| 334 | t.m11 = m11v; |
| 335 | t.m22 = m22v; |
| 336 | t.mtx = m31v; |
| 337 | t.mty = m32v; |
| 338 | break; |
| 339 | } |
| 340 | case MatrixType::Rotate: |
| 341 | case MatrixType::Shear: { |
| 342 | float m11v = m11 * m.m11 + m12 * m.m21; |
| 343 | float m12v = m11 * m.m12 + m12 * m.m22; |
| 344 |
| 345 | float m21v = m21 * m.m11 + m22 * m.m21; |
| 346 | float m22v = m21 * m.m12 + m22 * m.m22; |
| 347 |
| 348 | float m31v = mtx * m.m11 + mty * m.m21 + m.mtx; |
| 349 | float m32v = mtx * m.m12 + mty * m.m22 + m.mty; |
| 350 |
| 351 | t.m11 = m11v; |
| 352 | t.m12 = m12v; |
| 353 | t.m21 = m21v; |
| 354 | t.m22 = m22v; |
| 355 | t.mtx = m31v; |
| 356 | t.mty = m32v; |
| 357 | break; |
| 358 | } |
| 359 | case MatrixType::Project: { |
| 360 | float m11v = m11 * m.m11 + m12 * m.m21 + m13 * m.mtx; |
| 361 | float m12v = m11 * m.m12 + m12 * m.m22 + m13 * m.mty; |
| 362 | float m13v = m11 * m.m13 + m12 * m.m23 + m13 * m.m33; |
| 363 |
| 364 | float m21v = m21 * m.m11 + m22 * m.m21 + m23 * m.mtx; |
| 365 | float m22v = m21 * m.m12 + m22 * m.m22 + m23 * m.mty; |
| 366 | float m23v = m21 * m.m13 + m22 * m.m23 + m23 * m.m33; |
| 367 |
| 368 | float m31v = mtx * m.m11 + mty * m.m21 + m33 * m.mtx; |
| 369 | float m32v = mtx * m.m12 + mty * m.m22 + m33 * m.mty; |
| 370 | float m33v = mtx * m.m13 + mty * m.m23 + m33 * m.m33; |
| 371 |
| 372 | t.m11 = m11v; |
| 373 | t.m12 = m12v; |
| 374 | t.m13 = m13v; |
| 375 | t.m21 = m21v; |
| 376 | t.m22 = m22v; |
| 377 | t.m23 = m23v; |
| 378 | t.mtx = m31v; |
| 379 | t.mty = m32v; |
| 380 | t.m33 = m33v; |
| 381 | } |
| 382 | } |
| 383 |
| 384 | t.dirty = type; |
| 385 | t.mType = type; |
| 386 |
| 387 | return t; |
| 388 | } |
| 389 |
| 390 | VMatrix &VMatrix::operator*=(const VMatrix &o) |
| 391 | { |
| 392 | const MatrixType otherType = o.type(); |
| 393 | if (otherType == MatrixType::None) return *this; |
| 394 |
| 395 | const MatrixType thisType = type(); |
| 396 | if (thisType == MatrixType::None) return operator=(o); |
| 397 |
| 398 | MatrixType t = vMax(thisType, otherType); |
| 399 | switch (t) { |
| 400 | case MatrixType::None: |
| 401 | break; |
| 402 | case MatrixType::Translate: |
| 403 | mtx += o.mtx; |
| 404 | mty += o.mty; |
| 405 | break; |
| 406 | case MatrixType::Scale: { |
| 407 | float m11v = m11 * o.m11; |
| 408 | float m22v = m22 * o.m22; |
| 409 |
| 410 | float m31v = mtx * o.m11 + o.mtx; |
| 411 | float m32v = mty * o.m22 + o.mty; |
| 412 |
| 413 | m11 = m11v; |
| 414 | m22 = m22v; |
| 415 | mtx = m31v; |
| 416 | mty = m32v; |
| 417 | break; |
| 418 | } |
| 419 | case MatrixType::Rotate: |
| 420 | case MatrixType::Shear: { |
| 421 | float m11v = m11 * o.m11 + m12 * o.m21; |
| 422 | float m12v = m11 * o.m12 + m12 * o.m22; |
| 423 |
| 424 | float m21v = m21 * o.m11 + m22 * o.m21; |
| 425 | float m22v = m21 * o.m12 + m22 * o.m22; |
| 426 |
| 427 | float m31v = mtx * o.m11 + mty * o.m21 + o.mtx; |
| 428 | float m32v = mtx * o.m12 + mty * o.m22 + o.mty; |
| 429 |
| 430 | m11 = m11v; |
| 431 | m12 = m12v; |
| 432 | m21 = m21v; |
| 433 | m22 = m22v; |
| 434 | mtx = m31v; |
| 435 | mty = m32v; |
| 436 | break; |
| 437 | } |
| 438 | case MatrixType::Project: { |
| 439 | float m11v = m11 * o.m11 + m12 * o.m21 + m13 * o.mtx; |
| 440 | float m12v = m11 * o.m12 + m12 * o.m22 + m13 * o.mty; |
| 441 | float m13v = m11 * o.m13 + m12 * o.m23 + m13 * o.m33; |
| 442 |
| 443 | float m21v = m21 * o.m11 + m22 * o.m21 + m23 * o.mtx; |
| 444 | float m22v = m21 * o.m12 + m22 * o.m22 + m23 * o.mty; |
| 445 | float m23v = m21 * o.m13 + m22 * o.m23 + m23 * o.m33; |
| 446 |
| 447 | float m31v = mtx * o.m11 + mty * o.m21 + m33 * o.mtx; |
| 448 | float m32v = mtx * o.m12 + mty * o.m22 + m33 * o.mty; |
| 449 | float m33v = mtx * o.m13 + mty * o.m23 + m33 * o.m33; |
| 450 |
| 451 | m11 = m11v; |
| 452 | m12 = m12v; |
| 453 | m13 = m13v; |
| 454 | m21 = m21v; |
| 455 | m22 = m22v; |
| 456 | m23 = m23v; |
| 457 | mtx = m31v; |
| 458 | mty = m32v; |
| 459 | m33 = m33v; |
| 460 | } |
| 461 | } |
| 462 |
| 463 | dirty = t; |
| 464 | mType = t; |
| 465 |
| 466 | return *this; |
| 467 | } |
| 468 |
| 469 | VMatrix VMatrix::adjoint() const |
| 470 | { |
| 471 | float h11, h12, h13, h21, h22, h23, h31, h32, h33; |
| 472 | h11 = m22 * m33 - m23 * mty; |
| 473 | h21 = m23 * mtx - m21 * m33; |
| 474 | h31 = m21 * mty - m22 * mtx; |
| 475 | h12 = m13 * mty - m12 * m33; |
| 476 | h22 = m11 * m33 - m13 * mtx; |
| 477 | h32 = m12 * mtx - m11 * mty; |
| 478 | h13 = m12 * m23 - m13 * m22; |
| 479 | h23 = m13 * m21 - m11 * m23; |
| 480 | h33 = m11 * m22 - m12 * m21; |
| 481 |
| 482 | VMatrix res; |
| 483 | res.m11 = h11; |
| 484 | res.m12 = h12; |
| 485 | res.m13 = h13; |
| 486 | res.m21 = h21; |
| 487 | res.m22 = h22; |
| 488 | res.m23 = h23; |
| 489 | res.mtx = h31; |
| 490 | res.mty = h32; |
| 491 | res.m33 = h33; |
| 492 | res.mType = MatrixType::None; |
| 493 | res.dirty = MatrixType::Project; |
| 494 |
| 495 | return res; |
| 496 | } |
| 497 |
| 498 | VMatrix VMatrix::inverted(bool *invertible) const |
| 499 | { |
| 500 | VMatrix invert; |
| 501 | bool inv = true; |
| 502 |
| 503 | switch (type()) { |
| 504 | case MatrixType::None: |
| 505 | break; |
| 506 | case MatrixType::Translate: |
| 507 | invert.mtx = -mtx; |
| 508 | invert.mty = -mty; |
| 509 | break; |
| 510 | case MatrixType::Scale: |
| 511 | inv = !vIsZero(m11); |
| 512 | inv &= !vIsZero(m22); |
| 513 | if (inv) { |
| 514 | invert.m11 = 1. / m11; |
| 515 | invert.m22 = 1. / m22; |
| 516 | invert.mtx = -mtx * invert.m11; |
| 517 | invert.mty = -mty * invert.m22; |
| 518 | } |
| 519 | break; |
| 520 | default: |
| 521 | // general case |
| 522 | float det = determinant(); |
| 523 | inv = !vIsZero(det); |
| 524 | if (inv) invert = (adjoint() /= det); |
| 525 | // TODO Test above line |
| 526 | break; |
| 527 | } |
| 528 |
| 529 | if (invertible) *invertible = inv; |
| 530 |
| 531 | if (inv) { |
| 532 | // inverting doesn't change the type |
| 533 | invert.mType = mType; |
| 534 | invert.dirty = dirty; |
| 535 | } |
| 536 |
| 537 | return invert; |
| 538 | } |
| 539 |
| 540 | bool VMatrix::operator==(const VMatrix &o) const |
| 541 | { |
| 542 | return fuzzyCompare(o); |
| 543 | } |
| 544 |
| 545 | bool VMatrix::operator!=(const VMatrix &o) const |
| 546 | { |
| 547 | return !operator==(o); |
| 548 | } |
| 549 |
| 550 | bool VMatrix::fuzzyCompare(const VMatrix &o) const |
| 551 | { |
| 552 | return vCompare(m11, o.m11) && vCompare(m12, o.m12) && |
| 553 | vCompare(m21, o.m21) && vCompare(m22, o.m22) && |
| 554 | vCompare(mtx, o.mtx) && vCompare(mty, o.mty); |
| 555 | } |
| 556 |
| 557 | #define V_NEAR_CLIP 0.000001 |
| 558 | #ifdef MAP |
| 559 | #undef MAP |
| 560 | #endif |
| 561 | #define MAP(x, y, nx, ny) \ |
| 562 | do { \ |
| 563 | float FX_ = x; \ |
| 564 | float FY_ = y; \ |
| 565 | switch (t) { \ |
| 566 | case MatrixType::None: \ |
| 567 | nx = FX_; \ |
| 568 | ny = FY_; \ |
| 569 | break; \ |
| 570 | case MatrixType::Translate: \ |
| 571 | nx = FX_ + mtx; \ |
| 572 | ny = FY_ + mty; \ |
| 573 | break; \ |
| 574 | case MatrixType::Scale: \ |
| 575 | nx = m11 * FX_ + mtx; \ |
| 576 | ny = m22 * FY_ + mty; \ |
| 577 | break; \ |
| 578 | case MatrixType::Rotate: \ |
| 579 | case MatrixType::Shear: \ |
| 580 | case MatrixType::Project: \ |
| 581 | nx = m11 * FX_ + m21 * FY_ + mtx; \ |
| 582 | ny = m12 * FX_ + m22 * FY_ + mty; \ |
| 583 | if (t == MatrixType::Project) { \ |
| 584 | float w = (m13 * FX_ + m23 * FY_ + m33); \ |
| 585 | if (w < V_NEAR_CLIP) w = V_NEAR_CLIP; \ |
| 586 | w = 1. / w; \ |
| 587 | nx *= w; \ |
| 588 | ny *= w; \ |
| 589 | } \ |
| 590 | } \ |
| 591 | } while (0) |
| 592 |
| 593 | VRect VMatrix::map(const VRect &rect) const |
| 594 | { |
| 595 | VMatrix::MatrixType t = type(); |
| 596 | if (t <= MatrixType::Translate) |
| 597 | return rect.translated(round(mtx), round(mty)); |
| 598 |
| 599 | if (t <= MatrixType::Scale) { |
| 600 | int x = round(m11 * rect.x() + mtx); |
| 601 | int y = round(m22 * rect.y() + mty); |
| 602 | int w = round(m11 * rect.width()); |
| 603 | int h = round(m22 * rect.height()); |
| 604 | if (w < 0) { |
| 605 | w = -w; |
| 606 | x -= w; |
| 607 | } |
| 608 | if (h < 0) { |
| 609 | h = -h; |
| 610 | y -= h; |
| 611 | } |
| 612 | return {x, y, w, h}; |
| 613 | } else if (t < MatrixType::Project) { |
| 614 | // see mapToPolygon for explanations of the algorithm. |
| 615 | float x = 0, y = 0; |
| 616 | MAP(rect.left(), rect.top(), x, y); |
| 617 | float xmin = x; |
| 618 | float ymin = y; |
| 619 | float xmax = x; |
| 620 | float ymax = y; |
| 621 | MAP(rect.right() + 1, rect.top(), x, y); |
| 622 | xmin = vMin(xmin, x); |
| 623 | ymin = vMin(ymin, y); |
| 624 | xmax = vMax(xmax, x); |
| 625 | ymax = vMax(ymax, y); |
| 626 | MAP(rect.right() + 1, rect.bottom() + 1, x, y); |
| 627 | xmin = vMin(xmin, x); |
| 628 | ymin = vMin(ymin, y); |
| 629 | xmax = vMax(xmax, x); |
| 630 | ymax = vMax(ymax, y); |
| 631 | MAP(rect.left(), rect.bottom() + 1, x, y); |
| 632 | xmin = vMin(xmin, x); |
| 633 | ymin = vMin(ymin, y); |
| 634 | xmax = vMax(xmax, x); |
| 635 | ymax = vMax(ymax, y); |
| 636 | return VRect(round(xmin), round(ymin), |
| 637 | round(xmax) - round(xmin), |
| 638 | round(ymax) - round(ymin)); |
| 639 | } else { |
| 640 | // Not supported |
| 641 | assert(0); |
| 642 | } |
| 643 | return {0, 0, 0, 0}; |
| 644 | } |
| 645 |
| 646 | VRegion VMatrix::map(const VRegion &r) const |
| 647 | { |
| 648 | VMatrix::MatrixType t = type(); |
| 649 | if (t == MatrixType::None) return r; |
| 650 |
| 651 | if (t == MatrixType::Translate) { |
| 652 | VRegion copy(r); |
| 653 | copy.translate(round(mtx), round(mty)); |
| 654 | return copy; |
| 655 | } |
| 656 |
| 657 | if (t == MatrixType::Scale && r.rectCount() == 1) |
| 658 | return VRegion(map(r.boundingRect())); |
| 659 | // handle mapping of region properly |
| 660 | assert(0); |
| 661 | return r; |
| 662 | } |
| 663 |
| 664 | VPointF VMatrix::map(const VPointF &p) const |
| 665 | { |
| 666 | float fx = p.x(); |
| 667 | float fy = p.y(); |
| 668 |
| 669 | float x = 0, y = 0; |
| 670 |
| 671 | VMatrix::MatrixType t = type(); |
| 672 | switch (t) { |
| 673 | case MatrixType::None: |
| 674 | x = fx; |
| 675 | y = fy; |
| 676 | break; |
| 677 | case MatrixType::Translate: |
| 678 | x = fx + mtx; |
| 679 | y = fy + mty; |
| 680 | break; |
| 681 | case MatrixType::Scale: |
| 682 | x = m11 * fx + mtx; |
| 683 | y = m22 * fy + mty; |
| 684 | break; |
| 685 | case MatrixType::Rotate: |
| 686 | case MatrixType::Shear: |
| 687 | case MatrixType::Project: |
| 688 | x = m11 * fx + m21 * fy + mtx; |
| 689 | y = m12 * fx + m22 * fy + mty; |
| 690 | if (t == MatrixType::Project) { |
| 691 | float w = 1. / (m13 * fx + m23 * fy + m33); |
| 692 | x *= w; |
| 693 | y *= w; |
| 694 | } |
| 695 | } |
| 696 | return {x, y}; |
| 697 | } |
| 698 | static std::string type_helper(VMatrix::MatrixType t) |
| 699 | { |
| 700 | switch (t) { |
| 701 | case VMatrix::MatrixType::None: |
| 702 | return "MatrixType::None"; |
| 703 | break; |
| 704 | case VMatrix::MatrixType::Translate: |
| 705 | return "MatrixType::Translate"; |
| 706 | break; |
| 707 | case VMatrix::MatrixType::Scale: |
| 708 | return "MatrixType::Scale"; |
| 709 | break; |
| 710 | case VMatrix::MatrixType::Rotate: |
| 711 | return "MatrixType::Rotate"; |
| 712 | break; |
| 713 | case VMatrix::MatrixType::Shear: |
| 714 | return "MatrixType::Shear"; |
| 715 | break; |
| 716 | case VMatrix::MatrixType::Project: |
| 717 | return "MatrixType::Project"; |
| 718 | break; |
| 719 | } |
| 720 | return ""; |
| 721 | } |
| 722 | std::ostream &operator<<(std::ostream &os, const VMatrix &o) |
| 723 | { |
| 724 | os << "[Matrix: " |
| 725 | << "type =" << type_helper(o.type()) << ", Data : " << o.m11 << " " |
| 726 | << o.m12 << " " << o.m13 << " " << o.m21 << " " << o.m22 << " " << o.m23 |
| 727 | << " " << o.mtx << " " << o.mty << " " << o.m33 << " " |
| 728 | << "]" << std::endl; |
| 729 | return os; |
| 730 | } |
| 731 |
| 732 | V_END_NAMESPACE |
| 733 |