Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleTriangulate.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  @example ExampleTriangulate.cpp
27  @relates Triangulation, CovMatrix3x3
28  @brief example demonstrating triangulation
29  @ingroup g_examples
30  @author ischiller 10/2009
31 */
32 
33 
34 #include <Geometry/Triangulation.hh>
35 #include <Geometry/RMatrix.hh>
36 #include <Geometry/PMatrix.hh>
37 #include <Base/Geometry/KMatrix.hh>
38 #include "Geometry/CovMatrix3x3.hh"
39 #include <fstream>
40 #include <iostream>
41 
42 using namespace BIAS;
43 using namespace std;
44 
45 //int main(int argc, char *argv[])
46 int main()
47 {
48  PMatrix pleft,pleftScale,pleftScaleTriang,pright,prightScale,prightScaleTriang;
49  ifstream infileL("left.matrix");
50  ifstream infileR("right.matrix");
51 
52  Vector3<double> eucl3d;
53  HomgPoint2D pl, pr;
54  HomgPoint3D p3, p3s, p32, p32s;
55  CovMatrix3x3 cov;
56  Triangulation triangulator;
57 
58  // triangulator.SetDebugLevel(D_TR_POS|D_TR_TR);
59 
60  RMatrix rleft, rright, Rscale;
61  KMatrix K;
62  Vector3<double> Cleft,CleftScaleShift, Cright, CrightScaleShift;
63  rleft.SetXYZ(0.0, 0.0, 0.0);
64  rright.SetXYZ(0.0, 0.0, 0.0); Rscale.SetXYZ(0.4, 0.0, 0.0);
65  K.SetIdentity();
66  K[0][0]=K[1][1]=480;
67  K[0][2]=640/2.0;
68  K[1][2]=480/2.0;
69  Cleft.Set(0.0, 0.0, 0.0);
70  Cright.Set(2.5, 0.0, -5.0);
71  pleft.Compose(K, rleft, Cleft);
72  pright.Compose(K, rright, Cright);
73 
74  double scale = 10.0;
75  Vector3<double> shift(1.0, 1.0, 1.0);
76  CleftScaleShift=scale*Cleft;
77  CleftScaleShift +=shift;
78  CrightScaleShift=scale*Cright;
79  CrightScaleShift+=shift;
80  pleftScale.Compose(K, Rscale, CleftScaleShift);
81  pleftScaleTriang.Compose(K,Rscale.Transpose()*Rscale,Rscale.Transpose()*(CleftScaleShift-shift)/scale);
82  prightScale.Compose(K, Rscale, CrightScaleShift);
83  prightScaleTriang.Compose(K,Rscale.Transpose()*Rscale,Rscale.Transpose()*(CrightScaleShift-shift)/scale);
84 
86  pleftScale.InvalidateDecomposition();
87  pright.InvalidateDecomposition();
88  if (infileL) infileL>>pleft;
89  if (infileR) infileR>>pright;
90  cout<<"Read:"<<endl<<pleft<< endl <<pleftScale<<endl<<pright<<endl;
91  srand(0);
92  for (int i = 0; i <=5; i++){
93  // 3D point
94  BIAS::HomgPoint3D point3d;
95  for (int k=0; k<3; k++)
96  // point coordinate between 0 and 1000
97  point3d[k]=10.0*rand()/(RAND_MAX+1.0)-5;
98  point3d[2]+=6;
99  point3d[3]=1;
100  // project the point3d to get its image
101  BIAS::HomgPoint2D point2d_im1(pleft*point3d),
102  point2d_im1s(pleftScale*point3d);
103  BIAS::HomgPoint2D point2d_im2(pright*point3d),
104  point2d_im2s(prightScale*point3d);
105  point2d_im1.Homogenize();point2d_im1s.Homogenize();
106  point2d_im2.Homogenize();point2d_im2s.Homogenize();
107  // add up to 1 pixel noise
108  //point2d_im1 = (rand()/(RAND_MAX+1.0)*2.0-1.0) + point2d_im1;
109  //point2d_im2 = (rand()/(RAND_MAX+1.0)*2.0-1.0) + point2d_im2;
110  //point2d_im1[2]=1.0; point2d_im2[2]=1.0;
111  //cerr << "point left: "<< point2d_im1 << "\tpoint right: "<< point2d_im2
112  // << endl;
113  if (triangulator.Triangulate(pleft, pright, point2d_im1, point2d_im2,
114  eucl3d)!=0){
115  cerr << "error triangulating using SVD"<<endl;
116  }
117  p3.Set(eucl3d);
118  cout << "p3 =" << p3;
119  triangulator.Triangulate(pleftScaleTriang, prightScaleTriang,
120  point2d_im1s, point2d_im2s, eucl3d);
121  p3s.Set(eucl3d);
122  Vector3<double> vp3s(p3s[0]/p3s[3], (p3s[1]/p3s[3]), p3s[2]/p3s[3]);
123  vp3s = Rscale*vp3s*scale+shift;
124  cout << "p3 with scaled, rot baseline= " << p3s << ", vp3s = "
125  << vp3s << endl;
126  cout << "error is [" << (vp3s[0]-p3[0])/p3[0] << ", " << (vp3s[1]-p3[1])/p3[1]
127  << ", " <<(vp3s[2]-p3[2])/p3[2] << "] " << endl;
128  /*
129  if (triangulator.Triangulate2D(pleft, pright, point2d_im1, point2d_im2,
130  p32, dist, angle)!=0){
131  cerr << "error triangulating using GetNormRayWorldCoo"<<endl;
132  }
133  cout << "p32 =" << p32;
134 
135  triangulator.Triangulate2D(pleftScale, prightScale, point2d_im1, point2d_im2,
136  p32s, dist, angle);
137  cout << "p32 with scaled rot baseline= " << p32s << endl;
138  p32[1]=p32s[1]-10;
139  p32s = Vector3<double>(Rscale.Transpose()*p32s);
140  cout << "scale is [" << p32s[0]/p32[0] << ", " << p32s[1]/p32[1] << ", "
141  << p32s[2]/p32[2] << "] " << endl;
142  cerr <<"SVD point3d: "<<point3d<<" \tp3:"<<p3<<"\tdifference: "
143  <<(point3d-p3).norm_L2()<<endl;;
144  cerr <<"NormRay point3d: "<<point3d<<" \tp3:"<<p32<<"\tdifference: "
145  <<(point3d-p32).norm_L2()<<endl<<endl;
146  if (triangulator.GetCovarianceAnalytic(pleft, pright,
147  point2d_im1, point2d_im2,
148  p3, cov, 1)!=0) {
149  cerr << "error getCovariance"<<endl;
150  }
151  cerr<<"CovMatrix:<<"<<cov<<endl;
152  */
153  }
154 
155  pl.Set(196, 100);
156  pr.Set(155, 101);
157 
158  cout<<"Now triangulation of:"<<pl<<" "<<pr<<endl;
159 
160  cout<<"result of Triangluation: "<<
161  triangulator.Triangulate(pleft, pright, pl, pr, eucl3d)<<endl;
162  p3.Set(eucl3d);
163  cout<<"p3: "<<p3<<endl;
164  // cout<<"Covariance Matrix: "<<endl<<cov<<endl;
165  // cout<<"distance of rays: "<<dist<<endl;
166 
167  Vector3<double> a1,a2, b1,b2;
168  a1[0]= 0; a2[0]= 0;
169  a1[1]= 0; a2[1]= 0;
170  a1[2]= 0; a2[2]= 1;
171 
172  b1[0]= 0; b2[0]= 1;
173  b1[1]= 1; b2[1]= 1;
174  b1[2]= 0; b2[2]= 0;
175 
176  cout<<"Interscection of: a and b:"<<
177  triangulator.Intersect(a1,a2,b1,b2)<<endl;
178 
179  cout<<"Interscection of: b and a:"<<
180  triangulator.Intersect(b1,b2,a1,a2)<<endl;
181  return 0;
182 }
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
void Set(const T *pv)
copy the array of vectorsize beginning at *T to this-&gt;data_
Definition: Vector3.hh:532
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
void SetXYZ(ROTATION_MATRIX_TYPE PhiX, ROTATION_MATRIX_TYPE PhiY, ROTATION_MATRIX_TYPE PhiZ)
Set Euler angles (in rad) in order XYZ.
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
Class for triangulation of 3Dpoints from 2D matches. Covariance matrix (refering to an uncertainty el...
3D rotation matrix
Definition: RMatrix.hh:49
int Triangulate(PMatrix &P1, PMatrix &P2, const HomgPoint2D &p1, const HomgPoint2D &p2, BIAS::Vector3< double > &point3d)
Triangulation for metric PMatrices (using C and Hinf)
void InvalidateDecomposition()
to re-Decompose_() after filling with data use this.
Definition: PMatrix.hh:499
class for 3x3 covariance matrices
Definition: CovMatrix3x3.hh:50
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
Matrix3x3< T > Transpose() const
returns transposed matrix tested 12.06.2002
Definition: Matrix3x3.cpp:167
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrix.hh:88
void Set(const HOMGPOINT2D_TYPE &x, const HOMGPOINT2D_TYPE &y)
set elementwise with given 2 euclidean scalar values.
Definition: HomgPoint2D.hh:174
void Compose(const Matrix3x3< double > &K, const Matrix3x3< double > &R, const Vector3< double > &C)
composes this from K, R and C using P = [ K R&#39; | -K R&#39; C ] with R&#39; = transpose(R) ...
Definition: PMatrix.cpp:581
int Intersect(Vector3< double > &pA, Vector3< double > &dirA, Vector3< double > &pB, Vector3< double > &dirB, Vector3< double > &res)
analytic correct intersection of two lines, point returned has minimum distance to both lines...
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
Definition: Matrix3x3.hh:429