Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Covariance3Dto2DHomg.cpp
1 #include <Geometry/Covariance3Dto2DHomg.hh>
2 
3 #include <Base/Geometry/PMatrixBase.hh>
4 #include <Base/Geometry/PoseParametrization.hh>
5 #include <Base/Geometry/HomgPoint2D.hh>
6 #include <Base/Geometry/HomgPoint2DCov.hh>
7 #include <Base/Geometry/HomgPoint3D.hh>
8 #include <Base/Geometry/HomgPoint3DCov.hh>
9 #include <Base/Debug/Exception.hh>
10 
11 using namespace BIAS;
12 using namespace std;
13 
17  : UnscentedTransform(), useHomogenizedPoints_(false),
18  K_(K), Parametrization_(par)
19 {
20  //AddDebugLevel(D_C3DT2D_COV);
21 }
22 
25 {}
26 
29  const BIAS::HomgPoint3D& X,
30  const BIAS::HomgPoint3DCov& Cov_X,
32  BIAS::HomgPoint2DCov& Cov_x) const
33 {
34  // BIASCDOUT(D_C3DT2D_COV, "X: "<<X<<"\nCov_X: "<<Cov_X<<endl);
35  Vector<double> src, dst, Pose = P.GetCQ();
36  Matrix<double> src_cov, dst_cov, Cov_Pose = P.GetCovarianceMatrix();
37  const int pose_dim = Pose.Size();
38  const int dim = pose_dim + 3;
39 
40  if (!X.IsHomogenized() || !Cov_X.IsHomogenized()){
41  BEXCEPTION("only for homogenized entities (unfinished)");
42  }
43 
44  src.newsize(dim);
45  src_cov.newsize(dim, dim);
46  src_cov.SetZero();
47  for (int c=0; c<3; c++){
48  src[c] = X[c];
49  for (int r=0; r<3; r++){
50  src_cov[r][c] = Cov_X[r][c];
51  }
52  }
53 
54  for (int c=0; c<pose_dim; c++){
55  src[c+3] = Pose[c];
56  for (int r=0; r<pose_dim; r++){
57  src_cov[r+3][c+3] = Cov_Pose[r][c];
58  }
59  }
60 
61  // TODO: more efficient guaranty in lines abovce that cov is symmetric/posdef
62  src_cov.MakeSymmetric();
63  const int num_cols = src_cov.num_cols();
64  for (int i=0; i<num_cols; i++) {
65  if (src_cov[i][i] <= 0.0) src_cov[i][i] = 0.0;
66  }
67 
68 
69  BIASCDOUT(D_C3DT2D_COV, "src: "<<src<<"\nsrc_cov: "<<src_cov<<endl);
70  if (Transform(src, src_cov, dst, dst_cov)!=0){
71  return -1;
72  }
73  BIASCDOUT(D_C3DT2D_COV, "dst: "<<dst<<"\ndst_cov: "<<dst_cov<<endl);
74  x = dst;
75  Cov_x = HomgPoint2DCov(dst_cov);
76  return 0;
77 }
78 
80 Transform_(const Vector<double>& src, Vector<double>& dst) const
81 {
82  PMatrixBase P;
83  HomgPoint3D X;
84 
85  const int pose_size = src.Size() - 3;
86  Vector<double> param(pose_size);
87  for (int i=0; i<pose_size; i++){
88  param[i] = src[i+3];
89  }
90  BIASCDOUT(D_C3DT2D_COV, "K: "<<K_<<"\tparam: "<<param<<endl);
91  P.Compose(K_, param, Parametrization_);
92 
93  X.Set(src[0], src[1], src[2], 1.0);
94  dst = P * X;
96  // homogenize
97  if (fabs(dst[2]) < 1e-7) return -1;
98  dst /= dst[2];
99  } else {
100  const double dst_norm = dst.NormL2();
101  if (dst_norm < 1e-7) return -1;
102  dst /= dst_norm;
103  }
104  BIASCDOUT(D_C3DT2D_COV, "Pc32: "<<P<<"\tXc32: "<<X<<"\txx32: "<<dst<<endl);
105 
106  return 0;
107 }
108 
109 
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
class representing the covariance matrix of a homogenous point 2D
Subscript num_cols() const
Definition: cmat.h:320
class BIASGeometryBase_EXPORT HomgPoint2DCov
virtual int Transform_(const BIAS::Vector< double > &src, BIAS::Vector< double > &dst) const
src is a vector consisting of [ 3d point, camera center, orientation ] The orientation must be repres...
bool useHomogenizedPoints_
normalize or homogenize ???
int Project(const BIAS::PoseParametrization &Pose, const BIAS::HomgPoint3D &X, const BIAS::HomgPoint3DCov &Cov_X, BIAS::HomgPoint2D &x, BIAS::HomgPoint2DCov &Cov_x) const
compute
double NormL2() const
Return the L2 norm: sqrt(a^1 + a^2 + ...)
Definition: Vector.hh:416
void Set(const HOMGPOINT3D_TYPE &x, const HOMGPOINT3D_TYPE &y, const HOMGPOINT3D_TYPE &z)
set elementwise with given 3 euclidean scalar values.
Definition: HomgPoint3D.hh:321
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
Slim class bundeling pose parametrization and associated covariance matrix.
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
E_ParametrizationType
description of supported parametrization types
Definition: PMatrixBase.hh:166
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
Definition: Pose.hh:73
enum BIAS::PMatrixBase::E_ParametrizationType Parametrization_
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
const Matrix< PP_TYPE > & GetCovarianceMatrix() const
returns the covariance matrix associated with the CQ vector
int Transform(const Vector< double > &src_mean, const Matrix< double > &src_cov, Vector< double > &dst_mean, Matrix< double > &dst_cov) const
computes the second order approximation of the transformations of the mean and the associated covaria...
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrixBase.hh:74
class representing the covariance matrix of a homogenous point 3D
const Vector< PP_TYPE > & GetCQ() const
returns a vector of dimension 7 where first 3 entries are the position and the last 4 entries are the...
uses the unscented transformation to map a normal distribututed random variable using a nonlinear tra...
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
void Compose(const Matrix3x3< PMATRIX_TYPE > &K, const Matrix3x3< PMATRIX_TYPE > &R, const Vector3< PMATRIX_TYPE > &C)
composes this from K, R and C using P = [ K R&#39; | -K R&#39; C ] with R&#39; = transpose(R) ...
Definition: PMatrixBase.cpp:34
bool IsHomogenized() const
void MakeSymmetric()
componentwise: this = 0.5(this + this^T) yields symmetric matrix only allowed for square shaped matri...
Definition: Matrix.hh:522
bool IsHomogenized() const
Definition: HomgPoint3D.hh:147
Covariance3Dto2DHomg(const BIAS::KMatrix &K, const enum BIAS::PMatrixBase::E_ParametrizationType par)