| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// Anti-Grain Geometry - Version 2.4 |
| 3 |
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) |
| 4 |
// |
| 5 |
// Permission to copy, use, modify, sell and distribute this software |
| 6 |
// is granted provided this copyright notice appears in all copies. |
| 7 |
// This software is provided "as is" without express or implied |
| 8 |
// warranty, and with no claim as to its suitability for any purpose. |
| 9 |
// |
| 10 |
//---------------------------------------------------------------------------- |
| 11 |
// Contact: [email protected] |
| 12 |
// [email protected] |
| 13 |
// http://www.antigrain.com |
| 14 |
//---------------------------------------------------------------------------- |
| 15 |
// |
| 16 |
// Perspective 2D transformations |
| 17 |
// |
| 18 |
//---------------------------------------------------------------------------- |
| 19 |
#ifndef AGG_TRANS_PERSPECTIVE_INCLUDED |
| 20 |
#define AGG_TRANS_PERSPECTIVE_INCLUDED |
| 21 |
|
| 22 |
#include "agg_trans_affine.h" |
| 23 |
|
| 24 |
namespace agg |
| 25 |
{ |
| 26 |
//=======================================================trans_perspective |
| 27 |
struct trans_perspective |
| 28 |
{ |
| 29 |
double sx, shy, w0, shx, sy, w1, tx, ty, w2; |
| 30 |
|
| 31 |
//------------------------------------------------------- Construction |
| 32 |
// Identity matrix |
| 33 |
trans_perspective() : |
| 34 |
sx (1), shy(0), w0(0), |
| 35 |
shx(0), sy (1), w1(0), |
| 36 |
tx (0), ty (0), w2(1) {} |
| 37 |
|
| 38 |
// Custom matrix |
| 39 |
trans_perspective(double v0, double v1, double v2, |
| 40 |
double v3, double v4, double v5, |
| 41 |
double v6, double v7, double v8) : |
| 42 |
sx (v0), shy(v1), w0(v2), |
| 43 |
shx(v3), sy (v4), w1(v5), |
| 44 |
tx (v6), ty (v7), w2(v8) {} |
| 45 |
|
| 46 |
// Custom matrix from m[9] |
| 47 |
explicit trans_perspective(const double* m) : |
| 48 |
sx (m[0]), shy(m[1]), w0(m[2]), |
| 49 |
shx(m[3]), sy (m[4]), w1(m[5]), |
| 50 |
tx (m[6]), ty (m[7]), w2(m[8]) {} |
| 51 |
|
| 52 |
// From affine |
| 53 |
explicit trans_perspective(const trans_affine& a) : |
| 54 |
sx (a.sx ), shy(a.shy), w0(0), |
| 55 |
shx(a.shx), sy (a.sy ), w1(0), |
| 56 |
tx (a.tx ), ty (a.ty ), w2(1) {} |
| 57 |
|
| 58 |
// Rectangle to quadrilateral |
| 59 |
trans_perspective(double x1, double y1, double x2, double y2, |
| 60 |
const double* quad); |
| 61 |
|
| 62 |
// Quadrilateral to rectangle |
| 63 |
trans_perspective(const double* quad, |
| 64 |
double x1, double y1, double x2, double y2); |
| 65 |
|
| 66 |
// Arbitrary quadrilateral transformations |
| 67 |
trans_perspective(const double* src, const double* dst); |
| 68 |
|
| 69 |
//-------------------------------------- Quadrilateral transformations |
| 70 |
// The arguments are double[8] that are mapped to quadrilaterals: |
| 71 |
// x1,y1, x2,y2, x3,y3, x4,y4 |
| 72 |
bool quad_to_quad(const double* qs, const double* qd); |
| 73 |
|
| 74 |
bool rect_to_quad(double x1, double y1, |
| 75 |
double x2, double y2, |
| 76 |
const double* q); |
| 77 |
|
| 78 |
bool quad_to_rect(const double* q, |
| 79 |
double x1, double y1, |
| 80 |
double x2, double y2); |
| 81 |
|
| 82 |
// Map square (0,0,1,1) to the quadrilateral and vice versa |
| 83 |
bool square_to_quad(const double* q); |
| 84 |
bool quad_to_square(const double* q); |
| 85 |
|
| 86 |
|
| 87 |
//--------------------------------------------------------- Operations |
| 88 |
// Reset - load an identity matrix |
| 89 |
const trans_perspective& reset(); |
| 90 |
|
| 91 |
// Invert matrix. Returns false in degenerate case |
| 92 |
bool invert(); |
| 93 |
|
| 94 |
// Direct transformations operations |
| 95 |
const trans_perspective& translate(double x, double y); |
| 96 |
const trans_perspective& rotate(double a); |
| 97 |
const trans_perspective& scale(double s); |
| 98 |
const trans_perspective& scale(double x, double y); |
| 99 |
|
| 100 |
// Multiply the matrix by another one |
| 101 |
const trans_perspective& multiply(const trans_perspective& m); |
| 102 |
|
| 103 |
// Multiply "m" by "this" and assign the result to "this" |
| 104 |
const trans_perspective& premultiply(const trans_perspective& m); |
| 105 |
|
| 106 |
// Multiply matrix to inverse of another one |
| 107 |
const trans_perspective& multiply_inv(const trans_perspective& m); |
| 108 |
|
| 109 |
// Multiply inverse of "m" by "this" and assign the result to "this" |
| 110 |
const trans_perspective& premultiply_inv(const trans_perspective& m); |
| 111 |
|
| 112 |
// Multiply the matrix by another one |
| 113 |
const trans_perspective& multiply(const trans_affine& m); |
| 114 |
|
| 115 |
// Multiply "m" by "this" and assign the result to "this" |
| 116 |
const trans_perspective& premultiply(const trans_affine& m); |
| 117 |
|
| 118 |
// Multiply the matrix by inverse of another one |
| 119 |
const trans_perspective& multiply_inv(const trans_affine& m); |
| 120 |
|
| 121 |
// Multiply inverse of "m" by "this" and assign the result to "this" |
| 122 |
const trans_perspective& premultiply_inv(const trans_affine& m); |
| 123 |
|
| 124 |
//--------------------------------------------------------- Load/Store |
| 125 |
void store_to(double* m) const; |
| 126 |
const trans_perspective& load_from(const double* m); |
| 127 |
|
| 128 |
//---------------------------------------------------------- Operators |
| 129 |
// Multiply the matrix by another one |
| 130 |
const trans_perspective& operator *= (const trans_perspective& m) |
| 131 |
{ |
| 132 |
return multiply(m); |
| 133 |
} |
| 134 |
const trans_perspective& operator *= (const trans_affine& m) |
| 135 |
{ |
| 136 |
return multiply(m); |
| 137 |
} |
| 138 |
|
| 139 |
// Multiply the matrix by inverse of another one |
| 140 |
const trans_perspective& operator /= (const trans_perspective& m) |
| 141 |
{ |
| 142 |
return multiply_inv(m); |
| 143 |
} |
| 144 |
const trans_perspective& operator /= (const trans_affine& m) |
| 145 |
{ |
| 146 |
return multiply_inv(m); |
| 147 |
} |
| 148 |
|
| 149 |
// Multiply the matrix by another one and return |
| 150 |
// the result in a separete matrix. |
| 151 |
trans_perspective operator * (const trans_perspective& m) const |
| 152 |
{ |
| 153 |
return trans_perspective(*this).multiply(m); |
| 154 |
} |
| 155 |
trans_perspective operator * (const trans_affine& m) const |
| 156 |
{ |
| 157 |
return trans_perspective(*this).multiply(m); |
| 158 |
} |
| 159 |
|
| 160 |
// Multiply the matrix by inverse of another one |
| 161 |
// and return the result in a separete matrix. |
| 162 |
trans_perspective operator / (const trans_perspective& m) const |
| 163 |
{ |
| 164 |
return trans_perspective(*this).multiply_inv(m); |
| 165 |
} |
| 166 |
trans_perspective operator / (const trans_affine& m) const |
| 167 |
{ |
| 168 |
return trans_perspective(*this).multiply_inv(m); |
| 169 |
} |
| 170 |
|
| 171 |
// Calculate and return the inverse matrix |
| 172 |
trans_perspective operator ~ () const |
| 173 |
{ |
| 174 |
trans_perspective ret = *this; |
| 175 |
ret.invert(); |
| 176 |
return ret; |
| 177 |
} |
| 178 |
|
| 179 |
// Equal operator with default epsilon |
| 180 |
bool operator == (const trans_perspective& m) const |
| 181 |
{ |
| 182 |
return is_equal(m, affine_epsilon); |
| 183 |
} |
| 184 |
|
| 185 |
// Not Equal operator with default epsilon |
| 186 |
bool operator != (const trans_perspective& m) const |
| 187 |
{ |
| 188 |
return !is_equal(m, affine_epsilon); |
| 189 |
} |
| 190 |
|
| 191 |
//---------------------------------------------------- Transformations |
| 192 |
// Direct transformation of x and y |
| 193 |
void transform(double* x, double* y) const; |
| 194 |
|
| 195 |
// Direct transformation of x and y, affine part only |
| 196 |
void transform_affine(double* x, double* y) const; |
| 197 |
|
| 198 |
// Direct transformation of x and y, 2x2 matrix only, no translation |
| 199 |
void transform_2x2(double* x, double* y) const; |
| 200 |
|
| 201 |
// Inverse transformation of x and y. It works slow because |
| 202 |
// it explicitly inverts the matrix on every call. For massive |
| 203 |
// operations it's better to invert() the matrix and then use |
| 204 |
// direct transformations. |
| 205 |
void inverse_transform(double* x, double* y) const; |
| 206 |
|
| 207 |
|
| 208 |
//---------------------------------------------------------- Auxiliary |
| 209 |
const trans_perspective& from_affine(const trans_affine& a); |
| 210 |
double determinant() const; |
| 211 |
double determinant_reciprocal() const; |
| 212 |
|
| 213 |
bool is_valid(double epsilon = affine_epsilon) const; |
| 214 |
bool is_identity(double epsilon = affine_epsilon) const; |
| 215 |
bool is_equal(const trans_perspective& m, |
| 216 |
double epsilon = affine_epsilon) const; |
| 217 |
|
| 218 |
// Determine the major affine parameters. Use with caution |
| 219 |
// considering possible degenerate cases. |
| 220 |
double scale() const; |
| 221 |
double rotation() const; |
| 222 |
void translation(double* dx, double* dy) const; |
| 223 |
void scaling(double* x, double* y) const; |
| 224 |
void scaling_abs(double* x, double* y) const; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
//-------------------------------------------------------------------- |
| 229 |
class iterator_x |
| 230 |
{ |
| 231 |
double den; |
| 232 |
double den_step; |
| 233 |
double nom_x; |
| 234 |
double nom_x_step; |
| 235 |
double nom_y; |
| 236 |
double nom_y_step; |
| 237 |
|
| 238 |
public: |
| 239 |
double x; |
| 240 |
double y; |
| 241 |
|
| 242 |
iterator_x() {} |
| 243 |
iterator_x(double px, double py, double step, const trans_perspective& m) : |
| 244 |
den(px * m.w0 + py * m.w1 + m.w2), |
| 245 |
den_step(m.w0 * step), |
| 246 |
nom_x(px * m.sx + py * m.shx + m.tx), |
| 247 |
nom_x_step(step * m.sx), |
| 248 |
nom_y(px * m.shy + py * m.sy + m.ty), |
| 249 |
nom_y_step(step * m.shy), |
| 250 |
x(nom_x / den), |
| 251 |
y(nom_y / den) |
| 252 |
{} |
| 253 |
|
| 254 |
void operator ++ () |
| 255 |
{ |
| 256 |
den += den_step; |
| 257 |
nom_x += nom_x_step; |
| 258 |
nom_y += nom_y_step; |
| 259 |
double d = 1.0 / den; |
| 260 |
x = nom_x * d; |
| 261 |
y = nom_y * d; |
| 262 |
} |
| 263 |
}; |
| 264 |
|
| 265 |
//-------------------------------------------------------------------- |
| 266 |
iterator_x begin(double x, double y, double step) const |
| 267 |
{ |
| 268 |
return iterator_x(x, y, step, *this); |
| 269 |
} |
| 270 |
}; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
//------------------------------------------------------------------------ |
| 286 |
inline bool trans_perspective::square_to_quad(const double* q) |
| 287 |
{ |
| 288 |
double dx = q[0] - q[2] + q[4] - q[6]; |
| 289 |
double dy = q[1] - q[3] + q[5] - q[7]; |
| 290 |
if(dx == 0.0 && dy == 0.0) |
| 291 |
{ |
| 292 |
// Affine case (parallelogram) |
| 293 |
//--------------- |
| 294 |
sx = q[2] - q[0]; |
| 295 |
shy = q[3] - q[1]; |
| 296 |
w0 = 0.0; |
| 297 |
shx = q[4] - q[2]; |
| 298 |
sy = q[5] - q[3]; |
| 299 |
w1 = 0.0; |
| 300 |
tx = q[0]; |
| 301 |
ty = q[1]; |
| 302 |
w2 = 1.0; |
| 303 |
} |
| 304 |
else |
| 305 |
{ |
| 306 |
double dx1 = q[2] - q[4]; |
| 307 |
double dy1 = q[3] - q[5]; |
| 308 |
double dx2 = q[6] - q[4]; |
| 309 |
double dy2 = q[7] - q[5]; |
| 310 |
double den = dx1 * dy2 - dx2 * dy1; |
| 311 |
if(den == 0.0) |
| 312 |
{ |
| 313 |
// Singular case |
| 314 |
//--------------- |
| 315 |
sx = shy = w0 = shx = sy = w1 = tx = ty = w2 = 0.0; |
| 316 |
return false; |
| 317 |
} |
| 318 |
// General case |
| 319 |
//--------------- |
| 320 |
double u = (dx * dy2 - dy * dx2) / den; |
| 321 |
double v = (dy * dx1 - dx * dy1) / den; |
| 322 |
sx = q[2] - q[0] + u * q[2]; |
| 323 |
shy = q[3] - q[1] + u * q[3]; |
| 324 |
w0 = u; |
| 325 |
shx = q[6] - q[0] + v * q[6]; |
| 326 |
sy = q[7] - q[1] + v * q[7]; |
| 327 |
w1 = v; |
| 328 |
tx = q[0]; |
| 329 |
ty = q[1]; |
| 330 |
w2 = 1.0; |
| 331 |
} |
| 332 |
return true; |
| 333 |
} |
| 334 |
|
| 335 |
//------------------------------------------------------------------------ |
| 336 |
inline bool trans_perspective::invert() |
| 337 |
{ |
| 338 |
double d0 = sy * w2 - w1 * ty; |
| 339 |
double d1 = w0 * ty - shy * w2; |
| 340 |
double d2 = shy * w1 - w0 * sy; |
| 341 |
double d = sx * d0 + shx * d1 + tx * d2; |
| 342 |
if(d == 0.0) |
| 343 |
{ |
| 344 |
sx = shy = w0 = shx = sy = w1 = tx = ty = w2 = 0.0; |
| 345 |
return false; |
| 346 |
} |
| 347 |
d = 1.0 / d; |
| 348 |
trans_perspective a = *this; |
| 349 |
sx = d * d0; |
| 350 |
shy = d * d1; |
| 351 |
w0 = d * d2; |
| 352 |
shx = d * (a.w1 *a.tx - a.shx*a.w2); |
| 353 |
sy = d * (a.sx *a.w2 - a.w0 *a.tx); |
| 354 |
w1 = d * (a.w0 *a.shx - a.sx *a.w1); |
| 355 |
tx = d * (a.shx*a.ty - a.sy *a.tx); |
| 356 |
ty = d * (a.shy*a.tx - a.sx *a.ty); |
| 357 |
w2 = d * (a.sx *a.sy - a.shy*a.shx); |
| 358 |
return true; |
| 359 |
} |
| 360 |
|
| 361 |
//------------------------------------------------------------------------ |
| 362 |
inline bool trans_perspective::quad_to_square(const double* q) |
| 363 |
{ |
| 364 |
if(!square_to_quad(q)) return false; |
| 365 |
invert(); |
| 366 |
return true; |
| 367 |
} |
| 368 |
|
| 369 |
//------------------------------------------------------------------------ |
| 370 |
inline bool trans_perspective::quad_to_quad(const double* qs, |
| 371 |
const double* qd) |
| 372 |
{ |
| 373 |
trans_perspective p; |
| 374 |
if(! quad_to_square(qs)) return false; |
| 375 |
if(!p.square_to_quad(qd)) return false; |
| 376 |
multiply(p); |
| 377 |
return true; |
| 378 |
} |
| 379 |
|
| 380 |
//------------------------------------------------------------------------ |
| 381 |
inline bool trans_perspective::rect_to_quad(double x1, double y1, |
| 382 |
double x2, double y2, |
| 383 |
const double* q) |
| 384 |
{ |
| 385 |
double r[8]; |
| 386 |
r[0] = r[6] = x1; |
| 387 |
r[2] = r[4] = x2; |
| 388 |
r[1] = r[3] = y1; |
| 389 |
r[5] = r[7] = y2; |
| 390 |
return quad_to_quad(r, q); |
| 391 |
} |
| 392 |
|
| 393 |
//------------------------------------------------------------------------ |
| 394 |
inline bool trans_perspective::quad_to_rect(const double* q, |
| 395 |
double x1, double y1, |
| 396 |
double x2, double y2) |
| 397 |
{ |
| 398 |
double r[8]; |
| 399 |
r[0] = r[6] = x1; |
| 400 |
r[2] = r[4] = x2; |
| 401 |
r[1] = r[3] = y1; |
| 402 |
r[5] = r[7] = y2; |
| 403 |
return quad_to_quad(q, r); |
| 404 |
} |
| 405 |
|
| 406 |
//------------------------------------------------------------------------ |
| 407 |
inline trans_perspective::trans_perspective(double x1, double y1, |
| 408 |
double x2, double y2, |
| 409 |
const double* quad) |
| 410 |
{ |
| 411 |
rect_to_quad(x1, y1, x2, y2, quad); |
| 412 |
} |
| 413 |
|
| 414 |
//------------------------------------------------------------------------ |
| 415 |
inline trans_perspective::trans_perspective(const double* quad, |
| 416 |
double x1, double y1, |
| 417 |
double x2, double y2) |
| 418 |
{ |
| 419 |
quad_to_rect(quad, x1, y1, x2, y2); |
| 420 |
} |
| 421 |
|
| 422 |
//------------------------------------------------------------------------ |
| 423 |
inline trans_perspective::trans_perspective(const double* src, |
| 424 |
const double* dst) |
| 425 |
{ |
| 426 |
quad_to_quad(src, dst); |
| 427 |
} |
| 428 |
|
| 429 |
//------------------------------------------------------------------------ |
| 430 |
inline const trans_perspective& trans_perspective::reset() |
| 431 |
{ |
| 432 |
sx = 1; shy = 0; w0 = 0; |
| 433 |
shx = 0; sy = 1; w1 = 0; |
| 434 |
tx = 0; ty = 0; w2 = 1; |
| 435 |
return *this; |
| 436 |
} |
| 437 |
|
| 438 |
//------------------------------------------------------------------------ |
| 439 |
inline const trans_perspective& |
| 440 |
trans_perspective::multiply(const trans_perspective& a) |
| 441 |
{ |
| 442 |
trans_perspective b = *this; |
| 443 |
sx = a.sx *b.sx + a.shx*b.shy + a.tx*b.w0; |
| 444 |
shx = a.sx *b.shx + a.shx*b.sy + a.tx*b.w1; |
| 445 |
tx = a.sx *b.tx + a.shx*b.ty + a.tx*b.w2; |
| 446 |
shy = a.shy*b.sx + a.sy *b.shy + a.ty*b.w0; |
| 447 |
sy = a.shy*b.shx + a.sy *b.sy + a.ty*b.w1; |
| 448 |
ty = a.shy*b.tx + a.sy *b.ty + a.ty*b.w2; |
| 449 |
w0 = a.w0 *b.sx + a.w1 *b.shy + a.w2*b.w0; |
| 450 |
w1 = a.w0 *b.shx + a.w1 *b.sy + a.w2*b.w1; |
| 451 |
w2 = a.w0 *b.tx + a.w1 *b.ty + a.w2*b.w2; |
| 452 |
return *this; |
| 453 |
} |
| 454 |
|
| 455 |
//------------------------------------------------------------------------ |
| 456 |
inline const trans_perspective& |
| 457 |
trans_perspective::multiply(const trans_affine& a) |
| 458 |
{ |
| 459 |
trans_perspective b = *this; |
| 460 |
sx = a.sx *b.sx + a.shx*b.shy + a.tx*b.w0; |
| 461 |
shx = a.sx *b.shx + a.shx*b.sy + a.tx*b.w1; |
| 462 |
tx = a.sx *b.tx + a.shx*b.ty + a.tx*b.w2; |
| 463 |
shy = a.shy*b.sx + a.sy *b.shy + a.ty*b.w0; |
| 464 |
sy = a.shy*b.shx + a.sy *b.sy + a.ty*b.w1; |
| 465 |
ty = a.shy*b.tx + a.sy *b.ty + a.ty*b.w2; |
| 466 |
return *this; |
| 467 |
} |
| 468 |
|
| 469 |
//------------------------------------------------------------------------ |
| 470 |
inline const trans_perspective& |
| 471 |
trans_perspective::premultiply(const trans_perspective& b) |
| 472 |
{ |
| 473 |
trans_perspective a = *this; |
| 474 |
sx = a.sx *b.sx + a.shx*b.shy + a.tx*b.w0; |
| 475 |
shx = a.sx *b.shx + a.shx*b.sy + a.tx*b.w1; |
| 476 |
tx = a.sx *b.tx + a.shx*b.ty + a.tx*b.w2; |
| 477 |
shy = a.shy*b.sx + a.sy *b.shy + a.ty*b.w0; |
| 478 |
sy = a.shy*b.shx + a.sy *b.sy + a.ty*b.w1; |
| 479 |
ty = a.shy*b.tx + a.sy *b.ty + a.ty*b.w2; |
| 480 |
w0 = a.w0 *b.sx + a.w1 *b.shy + a.w2*b.w0; |
| 481 |
w1 = a.w0 *b.shx + a.w1 *b.sy + a.w2*b.w1; |
| 482 |
w2 = a.w0 *b.tx + a.w1 *b.ty + a.w2*b.w2; |
| 483 |
return *this; |
| 484 |
} |
| 485 |
|
| 486 |
//------------------------------------------------------------------------ |
| 487 |
inline const trans_perspective& |
| 488 |
trans_perspective::premultiply(const trans_affine& b) |
| 489 |
{ |
| 490 |
trans_perspective a = *this; |
| 491 |
sx = a.sx *b.sx + a.shx*b.shy; |
| 492 |
shx = a.sx *b.shx + a.shx*b.sy; |
| 493 |
tx = a.sx *b.tx + a.shx*b.ty + a.tx; |
| 494 |
shy = a.shy*b.sx + a.sy *b.shy; |
| 495 |
sy = a.shy*b.shx + a.sy *b.sy; |
| 496 |
ty = a.shy*b.tx + a.sy *b.ty + a.ty; |
| 497 |
w0 = a.w0 *b.sx + a.w1 *b.shy; |
| 498 |
w1 = a.w0 *b.shx + a.w1 *b.sy; |
| 499 |
w2 = a.w0 *b.tx + a.w1 *b.ty + a.w2; |
| 500 |
return *this; |
| 501 |
} |
| 502 |
|
| 503 |
//------------------------------------------------------------------------ |
| 504 |
const trans_perspective& |
| 505 |
trans_perspective::multiply_inv(const trans_perspective& m) |
| 506 |
{ |
| 507 |
trans_perspective t = m; |
| 508 |
t.invert(); |
| 509 |
return multiply(t); |
| 510 |
} |
| 511 |
|
| 512 |
//------------------------------------------------------------------------ |
| 513 |
const trans_perspective& |
| 514 |
trans_perspective::multiply_inv(const trans_affine& m) |
| 515 |
{ |
| 516 |
trans_affine t = m; |
| 517 |
t.invert(); |
| 518 |
return multiply(t); |
| 519 |
} |
| 520 |
|
| 521 |
//------------------------------------------------------------------------ |
| 522 |
const trans_perspective& |
| 523 |
trans_perspective::premultiply_inv(const trans_perspective& m) |
| 524 |
{ |
| 525 |
trans_perspective t = m; |
| 526 |
t.invert(); |
| 527 |
return *this = t.multiply(*this); |
| 528 |
} |
| 529 |
|
| 530 |
//------------------------------------------------------------------------ |
| 531 |
const trans_perspective& |
| 532 |
trans_perspective::premultiply_inv(const trans_affine& m) |
| 533 |
{ |
| 534 |
trans_perspective t(m); |
| 535 |
t.invert(); |
| 536 |
return *this = t.multiply(*this); |
| 537 |
} |
| 538 |
|
| 539 |
//------------------------------------------------------------------------ |
| 540 |
inline const trans_perspective& |
| 541 |
trans_perspective::translate(double x, double y) |
| 542 |
{ |
| 543 |
tx += x; |
| 544 |
ty += y; |
| 545 |
return *this; |
| 546 |
} |
| 547 |
|
| 548 |
//------------------------------------------------------------------------ |
| 549 |
inline const trans_perspective& trans_perspective::rotate(double a) |
| 550 |
{ |
| 551 |
multiply(trans_affine_rotation(a)); |
| 552 |
return *this; |
| 553 |
} |
| 554 |
|
| 555 |
//------------------------------------------------------------------------ |
| 556 |
inline const trans_perspective& trans_perspective::scale(double s) |
| 557 |
{ |
| 558 |
multiply(trans_affine_scaling(s)); |
| 559 |
return *this; |
| 560 |
} |
| 561 |
|
| 562 |
//------------------------------------------------------------------------ |
| 563 |
inline const trans_perspective& trans_perspective::scale(double x, double y) |
| 564 |
{ |
| 565 |
multiply(trans_affine_scaling(x, y)); |
| 566 |
return *this; |
| 567 |
} |
| 568 |
|
| 569 |
//------------------------------------------------------------------------ |
| 570 |
inline void trans_perspective::transform(double* px, double* py) const |
| 571 |
{ |
| 572 |
double x = *px; |
| 573 |
double y = *py; |
| 574 |
double m = 1.0 / (x*w0 + y*w1 + w2); |
| 575 |
*px = m * (x*sx + y*shx + tx); |
| 576 |
*py = m * (x*shy + y*sy + ty); |
| 577 |
} |
| 578 |
|
| 579 |
//------------------------------------------------------------------------ |
| 580 |
inline void trans_perspective::transform_affine(double* x, double* y) const |
| 581 |
{ |
| 582 |
double tmp = *x; |
| 583 |
*x = tmp * sx + *y * shx + tx; |
| 584 |
*y = tmp * shy + *y * sy + ty; |
| 585 |
} |
| 586 |
|
| 587 |
//------------------------------------------------------------------------ |
| 588 |
inline void trans_perspective::transform_2x2(double* x, double* y) const |
| 589 |
{ |
| 590 |
double tmp = *x; |
| 591 |
*x = tmp * sx + *y * shx; |
| 592 |
*y = tmp * shy + *y * sy; |
| 593 |
} |
| 594 |
|
| 595 |
//------------------------------------------------------------------------ |
| 596 |
inline void trans_perspective::inverse_transform(double* x, double* y) const |
| 597 |
{ |
| 598 |
trans_perspective t(*this); |
| 599 |
if(t.invert()) t.transform(x, y); |
| 600 |
} |
| 601 |
|
| 602 |
//------------------------------------------------------------------------ |
| 603 |
inline void trans_perspective::store_to(double* m) const |
| 604 |
{ |
| 605 |
*m++ = sx; *m++ = shy; *m++ = w0; |
| 606 |
*m++ = shx; *m++ = sy; *m++ = w1; |
| 607 |
*m++ = tx; *m++ = ty; *m++ = w2; |
| 608 |
} |
| 609 |
|
| 610 |
//------------------------------------------------------------------------ |
| 611 |
inline const trans_perspective& trans_perspective::load_from(const double* m) |
| 612 |
{ |
| 613 |
sx = *m++; shy = *m++; w0 = *m++; |
| 614 |
shx = *m++; sy = *m++; w1 = *m++; |
| 615 |
tx = *m++; ty = *m++; w2 = *m++; |
| 616 |
return *this; |
| 617 |
} |
| 618 |
|
| 619 |
//------------------------------------------------------------------------ |
| 620 |
inline const trans_perspective& |
| 621 |
trans_perspective::from_affine(const trans_affine& a) |
| 622 |
{ |
| 623 |
sx = a.sx; shy = a.shy; w0 = 0; |
| 624 |
shx = a.shx; sy = a.sy; w1 = 0; |
| 625 |
tx = a.tx; ty = a.ty; w2 = 1; |
| 626 |
return *this; |
| 627 |
} |
| 628 |
|
| 629 |
//------------------------------------------------------------------------ |
| 630 |
inline double trans_perspective::determinant() const |
| 631 |
{ |
| 632 |
return sx * (sy * w2 - ty * w1) + |
| 633 |
shx * (ty * w0 - shy * w2) + |
| 634 |
tx * (shy * w1 - sy * w0); |
| 635 |
} |
| 636 |
|
| 637 |
//------------------------------------------------------------------------ |
| 638 |
inline double trans_perspective::determinant_reciprocal() const |
| 639 |
{ |
| 640 |
return 1.0 / determinant(); |
| 641 |
} |
| 642 |
|
| 643 |
//------------------------------------------------------------------------ |
| 644 |
inline bool trans_perspective::is_valid(double epsilon) const |
| 645 |
{ |
| 646 |
return fabs(sx) > epsilon && fabs(sy) > epsilon && fabs(w2) > epsilon; |
| 647 |
} |
| 648 |
|
| 649 |
//------------------------------------------------------------------------ |
| 650 |
inline bool trans_perspective::is_identity(double epsilon) const |
| 651 |
{ |
| 652 |
return is_equal_eps(sx, 1.0, epsilon) && |
| 653 |
is_equal_eps(shy, 0.0, epsilon) && |
| 654 |
is_equal_eps(w0, 0.0, epsilon) && |
| 655 |
is_equal_eps(shx, 0.0, epsilon) && |
| 656 |
is_equal_eps(sy, 1.0, epsilon) && |
| 657 |
is_equal_eps(w1, 0.0, epsilon) && |
| 658 |
is_equal_eps(tx, 0.0, epsilon) && |
| 659 |
is_equal_eps(ty, 0.0, epsilon) && |
| 660 |
is_equal_eps(w2, 1.0, epsilon); |
| 661 |
} |
| 662 |
|
| 663 |
//------------------------------------------------------------------------ |
| 664 |
inline bool trans_perspective::is_equal(const trans_perspective& m, |
| 665 |
double epsilon) const |
| 666 |
{ |
| 667 |
return is_equal_eps(sx, m.sx, epsilon) && |
| 668 |
is_equal_eps(shy, m.shy, epsilon) && |
| 669 |
is_equal_eps(w0, m.w0, epsilon) && |
| 670 |
is_equal_eps(shx, m.shx, epsilon) && |
| 671 |
is_equal_eps(sy, m.sy, epsilon) && |
| 672 |
is_equal_eps(w1, m.w1, epsilon) && |
| 673 |
is_equal_eps(tx, m.tx, epsilon) && |
| 674 |
is_equal_eps(ty, m.ty, epsilon) && |
| 675 |
is_equal_eps(w2, m.w2, epsilon); |
| 676 |
} |
| 677 |
|
| 678 |
//------------------------------------------------------------------------ |
| 679 |
inline double trans_perspective::scale() const |
| 680 |
{ |
| 681 |
double x = 0.707106781 * sx + 0.707106781 * shx; |
| 682 |
double y = 0.707106781 * shy + 0.707106781 * sy; |
| 683 |
return sqrt(x*x + y*y); |
| 684 |
} |
| 685 |
|
| 686 |
//------------------------------------------------------------------------ |
| 687 |
inline double trans_perspective::rotation() const |
| 688 |
{ |
| 689 |
double x1 = 0.0; |
| 690 |
double y1 = 0.0; |
| 691 |
double x2 = 1.0; |
| 692 |
double y2 = 0.0; |
| 693 |
transform(&x1, &y1); |
| 694 |
transform(&x2, &y2); |
| 695 |
return atan2(y2-y1, x2-x1); |
| 696 |
} |
| 697 |
|
| 698 |
//------------------------------------------------------------------------ |
| 699 |
void trans_perspective::translation(double* dx, double* dy) const |
| 700 |
{ |
| 701 |
*dx = tx; |
| 702 |
*dy = ty; |
| 703 |
} |
| 704 |
|
| 705 |
//------------------------------------------------------------------------ |
| 706 |
void trans_perspective::scaling(double* x, double* y) const |
| 707 |
{ |
| 708 |
double x1 = 0.0; |
| 709 |
double y1 = 0.0; |
| 710 |
double x2 = 1.0; |
| 711 |
double y2 = 1.0; |
| 712 |
trans_perspective t(*this); |
| 713 |
t *= trans_affine_rotation(-rotation()); |
| 714 |
t.transform(&x1, &y1); |
| 715 |
t.transform(&x2, &y2); |
| 716 |
*x = x2 - x1; |
| 717 |
*y = y2 - y1; |
| 718 |
} |
| 719 |
|
| 720 |
//------------------------------------------------------------------------ |
| 721 |
void trans_perspective::scaling_abs(double* x, double* y) const |
| 722 |
{ |
| 723 |
*x = sqrt(sx * sx + shx * shx); |
| 724 |
*y = sqrt(shy * shy + sy * sy); |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
} |
| 729 |
|
| 730 |
#endif |
| 731 |
|