1 // This is oxl/mvl/ImageMetric.h
2 #ifndef ImageMetric_h_
3 #define ImageMetric_h_
4 //:
5 // \file
6 // \brief Converting between image and metric coordinates
7 //
8 //    ImageMetric is the baseclass for classes that define how points in image
9 //    coordinates are converted to conditioned frames.  For simple systems this
10 //    will represent the mapping of the image plane to the unit square, but for
11 //    a fully calibrated camera, for example, this might include corrections for
12 //    radial lens distortion and other nonlinear effects.
13 //
14 //    The default implementation in this baseclass simply assumes an identity
15 //    mapping between the two coordinate systems.
16 //
17 //    A general convention that is sometimes useful is that points in homogeneous
18 //    coordinates have been conditioned, while nonhomogeneous primitives remain in
19 //    image coordinates.
20 //
21 // \author
22 //     Andrew W. Fitzgibbon, Oxford RRG, 17 Aug 96
23 //
24 // \verbatim
25 //  Modifications
26 //   22 Oct 2002 - Peter Vanroose - added vgl_homg_point_2d interface
27 // \endverbatim
28 //
29 //-----------------------------------------------------------------------------
30 
31 #include <iostream>
32 #include <iosfwd>
33 #include <vnl/vnl_fwd.h>
34 #include <vgl/vgl_fwd.h>
35 #ifdef _MSC_VER
36 #  include <vcl_msvc_warnings.h>
37 #endif
38 class HomgPoint2D;
39 class HomgLineSeg2D;
40 class HomgLine2D;
41 class FMatrix;
42 
43 class ImageMetric
44 {
45  public:
46   // Constructors/Destructors--------------------------------------------------
47   ImageMetric() = default;
48   virtual ~ImageMetric() = default;
49   //ImageMetric(ImageMetric const& that); - use default
50   //ImageMetric& operator=(ImageMetric const& that); - use default
51 
52   virtual vgl_homg_point_2d<double> homg_to_imagehomg(vgl_homg_point_2d<double> const&) const;
53   virtual vgl_homg_point_2d<double> imagehomg_to_homg(vgl_homg_point_2d<double> const&) const;
54 
55   virtual HomgPoint2D homg_to_imagehomg(const HomgPoint2D&) const;
56   virtual HomgPoint2D imagehomg_to_homg(const HomgPoint2D&) const;
57 
58   // The following virtuals may be overridden if desired.
59   // By default they are implemented in terms of the previous two
60   virtual vgl_point_2d<double> homg_to_image(vgl_homg_point_2d<double> const&) const;
61   virtual vnl_double_2 homg_to_image(const HomgPoint2D&) const;
62 
63   virtual vgl_homg_point_2d<double> image_to_homg(vgl_point_2d<double> const&) const;
64   virtual HomgPoint2D image_to_homg(const vnl_double_2&) const;
65   virtual HomgPoint2D image_to_homg(double x, double y) const;
66 
67   virtual vgl_homg_line_2d<double> homg_to_image_line(vgl_homg_line_2d<double> const&) const;
68   virtual vgl_homg_line_2d<double> image_to_homg_line(vgl_homg_line_2d<double> const&) const;
69 
70   virtual HomgLine2D homg_to_image_line(const HomgLine2D&) const;
71   virtual HomgLine2D image_to_homg_line(const HomgLine2D&) const;
72 
73   virtual HomgLineSeg2D image_to_homg_line(const HomgLineSeg2D&) const;
74   virtual vgl_line_segment_2d<double> image_to_homg_line(vgl_line_segment_2d<double> const& l) const;
75   virtual HomgLineSeg2D homg_line_to_image(const HomgLineSeg2D&) const;
76   virtual vgl_line_segment_2d<double> homg_line_to_image(vgl_line_segment_2d<double> const& l) const;
77 
78   virtual double perp_dist_squared(const HomgPoint2D&, const HomgLine2D&) const;
79   virtual double perp_dist_squared(vgl_homg_point_2d<double> const&,
80                                    vgl_homg_line_2d<double> const&) const;
81   virtual HomgPoint2D perp_projection(const HomgLine2D & l, const HomgPoint2D & p) const;
82   virtual vgl_homg_point_2d<double> perp_projection(vgl_homg_line_2d<double> const& l,
83                                                     vgl_homg_point_2d<double> const& p) const;
84   virtual double distance_squared(const HomgPoint2D&, const HomgPoint2D&) const;
85   virtual double distance_squared(const HomgLineSeg2D& segment, const HomgLine2D& line) const;// ca_distance_squared_lineseg_to_line
86   virtual double distance_squared(const vgl_homg_point_2d<double>&, const vgl_homg_point_2d<double>&) const;
87   virtual double distance_squared(vgl_line_segment_2d<double> const& segment,
88                                   vgl_homg_line_2d<double> const& line) const;
89 
90   virtual bool is_within_distance(const HomgPoint2D&, const HomgPoint2D&, double distance) const;
91   virtual bool is_within_distance(vgl_homg_point_2d<double> const&,
92                                   vgl_homg_point_2d<double> const&,
93                                   double distance) const;
94 
95   // Data Access---------------------------------------------------------------
96   virtual vnl_double_3x3 get_C() const;
97   virtual vnl_double_3x3 get_C_inverse() const;
98 
99   virtual bool is_linear() const;
100   virtual bool can_invert_distance() const;
101   virtual double image_to_homg_distance(double image_distance) const;
102   virtual double homg_to_image_distance(double image_distance) const;
103 
104   virtual std::ostream& print(std::ostream& s) const;
105 
106   // Data Control--------------------------------------------------------------
107 
108   // Static functions to condition/decondition image relations
109   static FMatrix decondition(const FMatrix& F, const ImageMetric* c1, const ImageMetric* c2);
110 
111   inline friend std::ostream& operator<<(std::ostream& o, const ImageMetric& m) { return m.print(o); }
112 };
113 
114 #endif // ImageMetric_h_
115