File size: 28,175 Bytes
9375c9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 |
// Copyright (C) 2003 Davis E. King ([email protected])
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_POINT_TrANSFORMS_ABSTRACT_Hh_
#ifdef DLIB_POINT_TrANSFORMS_ABSTRACT_Hh_
#include "../matrix/matrix_abstract.h"
#include "vector_abstract.h"
#include "rectangle_abstract.h"
#include "drectangle_abstract.h"
#include <vector>
namespace dlib
{
// ----------------------------------------------------------------------------------------
class point_transform_affine
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an object that takes 2D points or vectors and
applies an affine transformation to them.
THREAD SAFETY
It is safe for multiple threads to make concurrent accesses to this object
without synchronization.
!*/
public:
point_transform_affine (
);
/*!
ensures
- This object will perform the identity transform. That is, given a point
as input it will return the same point as output.
!*/
point_transform_affine (
const matrix<double,2,2>& m,
const dlib::vector<double,2>& b
);
/*!
ensures
- #get_m() == m
- #get_b() == b
- When (*this)(p) is invoked it will return a point P such that:
- P == m*p + b
!*/
const dlib::vector<double,2> operator() (
const dlib::vector<double,2>& p
) const;
/*!
ensures
- applies the affine transformation defined by this object's constructor
to p and returns the result.
!*/
const matrix<double,2,2>& get_m(
) const;
/*!
ensures
- returns the transformation matrix used by this object.
!*/
const dlib::vector<double,2>& get_b(
) const;
/*!
ensures
- returns the offset vector used by this object.
!*/
};
void serialize (const point_transform_affine& item, std::ostream& out);
void deserialize (point_transform_affine& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
point_transform_affine operator* (
const point_transform_affine& lhs,
const point_transform_affine& rhs
);
/*!
ensures
- returns a transformation TFORM(x) that is equivalent to lhs(rhs(x)). That
is, for all valid x: TFORM(x) == lhs(rhs(x)).
!*/
// ----------------------------------------------------------------------------------------
point_transform_affine inv (
const point_transform_affine& trans
);
/*!
ensures
- If trans is an invertible transformation then this function returns a new
transformation that is the inverse of trans.
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
point_transform_affine find_affine_transform (
const std::vector<dlib::vector<T,2> >& from_points,
const std::vector<dlib::vector<T,2> >& to_points
);
/*!
requires
- from_points.size() == to_points.size()
- from_points.size() >= 3
ensures
- returns a point_transform_affine object, T, such that for all valid i:
length(T(from_points[i]) - to_points[i])
is minimized as often as possible. That is, this function finds the affine
transform that maps points in from_points to points in to_points. If no
affine transform exists which performs this mapping exactly then the one
which minimizes the mean squared error is selected. Additionally, if many
equally good transformations exist, then the transformation with the smallest
squared parameters is selected (i.e. if you wrote the transformation as a
matrix then we say we select the transform with minimum Frobenius norm among
all possible solutions).
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
point_transform_affine find_similarity_transform (
const std::vector<dlib::vector<T,2> >& from_points,
const std::vector<dlib::vector<T,2> >& to_points
);
/*!
requires
- from_points.size() == to_points.size()
- from_points.size() >= 2
ensures
- This function is just like find_affine_transform() except it finds the best
similarity transform instead of a full affine transform. This means that it
optimizes over only the space of rotations, scale changes, and translations.
So for example, if you mapped the 3 vertices of a triangle through a
similarity transform then the output would still be the same triangle.
However, the triangle itself may be larger or smaller, rotated, or at a
different location in the coordinate system. This is not the case for a
general affine transform which can stretch points in ways that cause, for
example, an equilateral triangle to turn into an isosceles triangle.
!*/
// ----------------------------------------------------------------------------------------
class rectangle_transform
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is just a point_transform_affine wrapped up so that it can
transform rectangle objects. It will take a rectangle and transform it
according to an affine transformation.
THREAD SAFETY
It is safe for multiple threads to make concurrent accesses to this object
without synchronization.
!*/
public:
rectangle_transform (
);
/*!
ensures
- This object will perform the identity transform. That is, given a rectangle
as input it will return the same rectangle as output.
!*/
rectangle_transform (
const point_transform_affine& tform
);
/*!
ensures
- #get_tform() == tform
!*/
drectangle operator() (
const drectangle& r
) const;
/*!
ensures
- Applies the transformation get_tform() to r and returns the resulting
rectangle. If the transformation doesn't have any rotation then the
transformation simply maps the corners of the rectangle according to
get_tform() and returns the exact result. However, since
dlib::drectangle can't represent rotated rectangles, if there is any
rotation in the affine transform we will attempt to produce the most
faithful possible outputs by ensuring the output rectangle has the
correct center point and that its area and aspect ratio match the correct
rotated rectangle's as much as possible.
!*/
rectangle operator() (
const rectangle& r
) const;
/*!
ensures
- returns (*this)(drectangle(r))
!*/
const point_transform_affine& get_tform(
) const;
/*!
ensures
- returns the affine transformation this object uses to transform rectangles.
!*/
};
void serialize (const rectangle_transform& item, std::ostream& out);
void deserialize (rectangle_transform& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
class point_transform_projective
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an object that takes 2D points or vectors and
applies a projective transformation to them.
THREAD SAFETY
It is safe for multiple threads to make concurrent accesses to this object
without synchronization.
!*/
public:
point_transform_projective (
);
/*!
ensures
- This object will perform the identity transform. That is, given a point
as input it will return the same point as output.
!*/
point_transform_projective (
const matrix<double,3,3>& m
);
/*!
ensures
- #get_m() == m
!*/
point_transform_projective (
const point_transform_affine& tran
);
/*!
ensures
- This object will perform exactly the same transformation as the given
affine transform.
!*/
const dlib::vector<double,2> operator() (
const dlib::vector<double,2>& p
) const;
/*!
ensures
- Applies the projective transformation defined by this object's constructor
to p and returns the result. To define this precisely:
- let p_h == the point p in homogeneous coordinates. That is:
- p_h.x() == p.x()
- p_h.y() == p.y()
- p_h.z() == 1
- let x == get_m()*p_h
- Then this function returns the value x/x.z()
!*/
const matrix<double,3,3>& get_m(
) const;
/*!
ensures
- returns the transformation matrix used by this object.
!*/
};
void serialize (const point_transform_projective& item, std::ostream& out);
void deserialize (point_transform_projective& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
point_transform_projective operator* (
const point_transform_projective& lhs,
const point_transform_projective& rhs
);
/*!
ensures
- returns a transformation TFORM(x) that is equivalent to lhs(rhs(x)). That
is, for all valid x: TFORM(x) == lhs(rhs(x)).
!*/
// ----------------------------------------------------------------------------------------
point_transform_projective inv (
const point_transform_projective& trans
);
/*!
ensures
- If trans is an invertible transformation then this function returns a new
transformation that is the inverse of trans.
!*/
// ----------------------------------------------------------------------------------------
point_transform_projective find_projective_transform (
const std::vector<dlib::vector<double,2> >& from_points,
const std::vector<dlib::vector<double,2> >& to_points
);
/*!
requires
- from_points.size() == to_points.size()
- from_points.size() >= 4
ensures
- returns a point_transform_projective object, T, such that for all valid i:
length(T(from_points[i]) - to_points[i])
is minimized as often as possible. That is, this function finds the projective
transform that maps points in from_points to points in to_points. If no
projective transform exists which performs this mapping exactly then the one
which minimizes the mean squared error is selected.
!*/
// ----------------------------------------------------------------------------------------
class point_transform
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an object that takes 2D points or vectors and
rotates them around the origin by a given angle and then
translates them.
THREAD SAFETY
It is safe for multiple threads to make concurrent accesses to this object
without synchronization.
!*/
public:
point_transform (
);
/*!
ensures
- This object will perform the identity transform. That is, given a point
as input it will return the same point as output.
!*/
point_transform (
const double& angle,
const dlib::vector<double,2>& translate
)
/*!
ensures
- When (*this)(p) is invoked it will return a point P such that:
- P is the point p rotated counter-clockwise around the origin
angle radians and then shifted by having translate added to it.
(Note that this is counter clockwise with respect to the normal
coordinate system with positive y going up and positive x going
to the right)
!*/
template <typename T>
const dlib::vector<T,2> operator() (
const dlib::vector<T,2>& p
) const;
/*!
ensures
- rotates p, then translates it and returns the result. The output
of this function is therefore equal to get_m()*p + get_b().
!*/
const matrix<double,2,2> get_m(
) const;
/*!
ensures
- returns the transformation matrix used by this object.
!*/
const dlib::vector<double,2> get_b(
) const;
/*!
ensures
- returns the offset vector used by this object.
!*/
};
void serialize (const point_transform& item, std::ostream& out);
void deserialize (point_transform& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
class point_rotator
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an object that takes 2D points or vectors and
rotates them around the origin by a given angle.
THREAD SAFETY
It is safe for multiple threads to make concurrent accesses to this object
without synchronization.
!*/
public:
point_rotator (
);
/*!
ensures
- This object will perform the identity transform. That is, given a point
as input it will return the same point as output.
!*/
point_rotator (
const double& angle
);
/*!
ensures
- When (*this)(p) is invoked it will return a point P such that:
- P is the point p rotated counter-clockwise around the origin
angle radians.
(Note that this is counter clockwise with respect to the normal
coordinate system with positive y going up and positive x going
to the right)
!*/
template <typename T>
const dlib::vector<T,2> operator() (
const dlib::vector<T,2>& p
) const;
/*!
ensures
- rotates p and returns the result. The output of this function is
therefore equal to get_m()*p.
!*/
const matrix<double,2,2> get_m(
) const;
/*!
ensures
- returns the transformation matrix used by this object.
!*/
};
void serialize (const point_rotator& item, std::ostream& out);
void deserialize (point_rotator& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
const dlib::vector<T,2> rotate_point (
const dlib::vector<T,2> center,
const dlib::vector<T,2> p,
double angle
);
/*!
ensures
- returns a point P such that:
- P is the point p rotated counter-clockwise around the given
center point by angle radians.
(Note that this is counter clockwise with respect to the normal
coordinate system with positive y going up and positive x going
to the right)
!*/
// ----------------------------------------------------------------------------------------
matrix<double,2,2> rotation_matrix (
double angle
);
/*!
ensures
- returns a rotation matrix which rotates points around the origin in a
counter-clockwise direction by angle radians.
(Note that this is counter clockwise with respect to the normal
coordinate system with positive y going up and positive x going
to the right)
Or in other words, this function returns a matrix M such that, given a
point P, M*P gives a point which is P rotated by angle radians around
the origin in a counter-clockwise direction.
!*/
// ----------------------------------------------------------------------------------------
class point_transform_affine3d
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an object that takes 3D points or vectors and
applies an affine transformation to them.
THREAD SAFETY
It is safe for multiple threads to make concurrent accesses to this object
without synchronization.
!*/
public:
point_transform_affine3d (
);
/*!
ensures
- This object will perform the identity transform. That is, given a point
as input it will return the same point as output.
!*/
point_transform_affine3d (
const matrix<double,3,3>& m,
const dlib::vector<double,3>& b
);
/*!
ensures
- #get_m() == m
- #get_b() == b
- When (*this)(p) is invoked it will return a point P such that:
- P == m*p + b
!*/
const dlib::vector<double,3> operator() (
const dlib::vector<double,3>& p
) const;
/*!
ensures
- applies the affine transformation defined by this object's constructor
to p and returns the result.
!*/
const matrix<double,3,3>& get_m(
) const;
/*!
ensures
- returns the transformation matrix used by this object.
!*/
const dlib::vector<double,3>& get_b(
) const;
/*!
ensures
- returns the offset vector used by this object.
!*/
};
void serialize (const point_transform_affine3d& item, std::ostream& out);
void deserialize (point_transform_affine3d& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
point_transform_affine3d operator* (
const point_transform_affine3d& lhs,
const point_transform_affine3d& rhs
);
/*!
ensures
- returns a transformation TFORM(x) that is equivalent to lhs(rhs(x)). That
is, for all valid x: TFORM(x) == lhs(rhs(x)).
!*/
// ----------------------------------------------------------------------------------------
point_transform_affine3d operator* (
const point_transform_affine3d& lhs,
const point_transform_affine& rhs
);
/*!
ensures
- returns a transformation TFORM(x) that is equivalent to lhs(rhs(x)). That
is, for all valid x: TFORM(x) == lhs(rhs(x)).
!*/
// ----------------------------------------------------------------------------------------
point_transform_affine3d inv (
const point_transform_affine3d& trans
);
/*!
ensures
- If trans is an invertible transformation then this function returns a new
transformation that is the inverse of trans.
!*/
// ----------------------------------------------------------------------------------------
point_transform_affine3d rotate_around_x (
double angle
);
/*!
ensures
- Returns a transformation that rotates a point around the x axis in a
counter-clockwise direction by angle radians. That is, the rotation appears
counter-clockwise when the x axis points toward the observer, the coordinate
system is right-handed, and the angle is positive.
!*/
// ----------------------------------------------------------------------------------------
point_transform_affine3d rotate_around_y (
double angle
);
/*!
ensures
- Returns a transformation that rotates a point around the y axis in a
counter-clockwise direction by angle radians. That is, the rotation appears
counter-clockwise when the y axis points toward the observer, the coordinate
system is right-handed, and the angle is positive.
!*/
// ----------------------------------------------------------------------------------------
point_transform_affine3d rotate_around_z (
double angle
);
/*!
ensures
- Returns a transformation that rotates a point around the z axis in a
counter-clockwise direction by angle radians. That is, the rotation appears
counter-clockwise when the z axis points toward the observer, the coordinate
system is right-handed, and the angle is positive.
!*/
// ----------------------------------------------------------------------------------------
point_transform_affine3d translate_point (
const vector<double,3>& delta
);
/*!
ensures
- returns a transformation that simply translates points by adding delta to
them. That is, this function returns:
point_transform_affine3d(identity_matrix<double>(3),delta);
!*/
point_transform_affine3d translate_point (
double x,
double y,
double z
);
/*!
ensures
- returns translate_point(vector<double>(x,y,z))
!*/
// ----------------------------------------------------------------------------------------
class camera_transform
{
/*!
WHAT THIS OBJECT REPRESENTS
This object maps 3D points into the image plane of a camera. Therefore,
you can use it to compute 2D representations of 3D data from the point of
view of some camera in 3D space.
THREAD SAFETY
It is safe for multiple threads to make concurrent accesses to this object
without synchronization.
!*/
public:
camera_transform (
);
/*!
ensures
- #get_camera_pos() == vector<double>(1,1,1)
- #get_camera_looking_at() == vector<double>(0,0,0)
- #get_camera_up_direction() == vector<double>(0,0,1)
- #get_camera_field_of_view() == 90
- #get_num_pixels() == 1
!*/
camera_transform (
const vector<double>& camera_pos,
const vector<double>& camera_looking_at,
const vector<double>& camera_up_direction,
const double camera_field_of_view,
const unsigned long num_pixels
);
/*!
requires
- 0 < camera_field_of_view < 180
ensures
- #get_camera_pos() == camera_pos
- #get_camera_looking_at() == camera_looking_at
- #get_camera_up_direction() == camera_up_direction
- #get_camera_field_of_view() == camera_field_of_view
- #get_num_pixels() == num_pixels
!*/
dpoint operator() (
const vector<double>& p
) const;
/*!
ensures
- Maps the given 3D point p into the 2D image plane defined by the camera
parameters given to this object's constructor. The 2D point in the image
plane is returned.
!*/
dpoint operator() (
const vector<double>& p,
double& scale,
double& distance
) const;
/*!
ensures
- Maps the given 3D point p into the 2D image plane defined by the camera
parameters given to this object's constructor. The 2D point in the image
plane is returned.
- #scale == a number that tells you how large things are at the point p.
Objects further from the camera appear smaller, in particular, they
appear #scale times their normal size.
- #distance == how far away the point is from the image plane. Objects in
front of the camera will have a positive distance and those behind a
negative distance.
!*/
vector<double> get_camera_pos(
) const;
/*!
ensures
- returns the position, in 3D space, of the camera. When operator() is
invoked it maps 3D points into the image plane of this camera.
!*/
vector<double> get_camera_looking_at(
) const;
/*!
ensures
- returns the point in 3D space the camera is pointed at.
!*/
vector<double> get_camera_up_direction(
) const;
/*!
ensures
- returns a vector that defines what direction is "up" for the camera.
This means that as you travel from the bottom of the image plane to the
top you will be traveling in the direction of this vector. Note that
get_camera_up_direction() doesn't need to be orthogonal to the camera's
line of sight (i.e. get_camera_looking_at()-get_camera_pos()), it just
needs to not be an exact multiple of the line of sight. Any necessary
orthogonalization will be taken care of internally.
!*/
double get_camera_field_of_view(
) const;
/*!
ensures
- returns the field of view of the camera in degrees.
!*/
unsigned long get_num_pixels(
) const;
/*!
ensures
- 3D points that fall within the field of view of the camera are mapped by
operator() into the pixel coordinates of a get_num_pixels() by
get_num_pixels() image. Therefore, you can use the output of operator()
to index into an image. However, you still need to perform bounds
checking as there might be 3D points outside the field of view of the
camera and those will be mapped to 2D points outside the image.
!*/
};
void serialize (const camera_transform& item, std::ostream& out);
void deserialize (camera_transform& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_POINT_TrANSFORMS_ABSTRACT_Hh_
|