Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HomgPoint2DCov.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #include "HomgPoint2DCov.hh"
27 #include "HomgPoint2D.hh"
28 //#include <Base/Math/Matrix2x2.hh>
29 #include <Base/Debug/Exception.hh>
30 
31 using namespace BIAS;
32 using namespace std;
33 
35 HomgPoint2DCov() : Matrix3x3<double> ()
36 {}
37 
39 HomgPoint2DCov(const MatrixInitType& i) : Matrix3x3<double>(i) {}
40 
43 {
44  if (!CheckSymmetry()){
45  /*cout << setprecision(20) << (*this)[0][1] <<" == "<< (*this)[1][0] << boolalpha << Equal((*this)[0][1], (*this)[1][0])<<"\t"<<((*this)[0][1]==(*this)[1][0])<<endl;
46  cout << setprecision(20) << (*this)[0][2] <<" == "<< (*this)[2][0] << boolalpha << Equal((*this)[0][2], (*this)[2][0])<<endl;
47  cout << setprecision(20) << (*this)[1][2] <<" == "<< (*this)[2][1] << boolalpha << Equal((*this)[1][2], (*this)[2][1])<<endl; */
48  //BIASERR("Not a covariance matrix, forcing symmetry! " << *this);
49  MakeSymmetric();
50  //BEXCEPTION("Not a covariance matrix, symmetry check failed: " << *this);
51  }
52 }
53 
56  : Matrix3x3<double>()
57 {
58  SetEuclidean(m);
59 }
60 
63  : Matrix3x3<double>(m)
64 {
65  if (!CheckSymmetry()){
66  BEXCEPTION("not a covariance matrix "<<*this);
67  }
68 }
69 
72  : Matrix3x3<double>(m)
73 {
74  if (!CheckSymmetry()){
75  BEXCEPTION("not a covariance matrix "<<*this);
76  }
77 }
78 
81 {}
82 
83 
86 {
87  if (!p.IsAtInfinity()){
88  // The jacobian of homogenization is given by:
89  // 1/w 0 -x/w^2
90  // 0 1/w -y/w^2
91  // 0 0 0
93  J[0][0] = J[1][1] = 1.0/p[2];
94  J[0][2] = -p[0]/p[2]/p[2];
95  J[1][2] = -p[1]/p[2]/p[2];
96  // The covariance transforms as J * cov * J^T
97  *this = HomgPoint2DCov(J * *this * J.Transpose());
98  p.Homogenize();
99  } else {
100  cout << __FILE__<<":"<<__LINE__<<" p " << p << " at infinity, doing nothing." << endl;
101  }
102 }
103 
104 
105 void HomgPoint2DCov::
107 {
108  const double norm = p.NormL2();
109  // The jacobian of normalization is given by:
110  // J = 1/|p|(I - pp^T/p^Tp)
111  // Foerstner 2004, "Uncertainty and Projective Geometry"
113  Matrix3x3<double> tmp = p.OuterProduct(p);
114  tmp /= p.ScalarProduct(p);
115  J -= tmp;
116  J /= norm;
117  // The covariance transforms as J * cov * J^T
118  *this = HomgPoint2DCov(J * *this * J.Transpose());
119  p /= norm;
120 }
121 
122 
124 GetEuclidean() const
125 {
126  if (!IsHomogenized()){
127  BEXCEPTION("Euclidean cov matrices can only be returned when homogenized: " << *this);
128  }
129  Matrix2x2<double> res;
130  res[0][0] = (*this)[0][0];
131  res[0][1] = (*this)[0][1];
132  res[1][0] = (*this)[1][0];
133  res[1][1] = (*this)[1][1];
134  return res;
135 }
136 
137 
138 void HomgPoint2DCov::
140 {
141  SetZero();
142  (*this)[0][0] = m[0][0] ;
143  (*this)[0][1] = m[0][1] ;
144  (*this)[1][0] = m[1][0] ;
145  (*this)[1][1] = m[1][1] ;
146  if (!CheckSymmetry()){
147  BEXCEPTION("Not a covariance matrix, symmetry check failed: "<< *this);
148  }
149 }
150 
151 
152 bool HomgPoint2DCov::
154 {
155  const double eps = 1e-10;
156  return ( Equal((*this)[0][1], (*this)[1][0], eps) &&
157  Equal((*this)[0][2], (*this)[2][0], eps) &&
158  Equal((*this)[1][2], (*this)[2][1], eps) );
159 }
160 
162  (*this)[1][0] = (*this)[0][1] = ((*this)[0][1] + (*this)[1][0])/2.0;
163  (*this)[2][0] = (*this)[0][2] = ((*this)[0][2] + (*this)[2][0])/2.0;
164  (*this)[2][1] = (*this)[1][2] = ((*this)[1][2] + (*this)[2][1])/2.0;
165 }
166 
168 {
169  (*this)[0][0] = m[0][0];
170  (*this)[1][1] = m[1][1];
171  (*this)[2][2] = m[2][2];
172  (*this)[1][0] = (*this)[0][1] = (m[0][1] + m[1][0])/2.0;
173  (*this)[2][0] = (*this)[0][2] = (m[0][2] + m[2][0])/2.0;
174  (*this)[2][1] = (*this)[1][2] = (m[1][2] + m[2][1])/2.0;
175 }
MatrixInitType
can be passed to matrix constructors to init the matrix with the most often used values ...
Definition: Matrix.hh:59
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
Matrix2x2< double > GetEuclidean() const
void ScalarProduct(const Vector3< T > &argvec, T &result) const
scalar product (=inner product) of two vectors, storing the result in result
Definition: Vector3.hh:603
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
void OuterProduct(const Vector3< T > &v, Matrix3x3< T > &res) const
outer product, constructs a matrix.
Definition: Vector3.cpp:151
void SetEuclidean(const Matrix2x2< double > &m)
void Homogenize(HomgPoint2D &p)
homogenizes p and *this
bool IsAtInfinity() const
Definition: HomgPoint2D.hh:165
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
bool CheckSymmetry() const
checks whether matrix is symmetric
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
Matrix3x3< T > Transpose() const
returns transposed matrix tested 12.06.2002
Definition: Matrix3x3.cpp:167
void MakeSymmetric()
averages transposed elements to make symmetric
void SetAndMakeSymmetric(const Matrix3x3< double > &m)
Sets from matrix and enforces symmetry.
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633
double Normalize()
divide this by biggest absolute entry, returns biggest entry
bool IsHomogenized() const