Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ProjectionParametersProjective.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 "Geometry/ProjectionParametersProjective.hh"
27 
28 
29 using namespace std;
30 using namespace BIAS;
31 
32 
33 int ProjectionParametersProjective::
34 ProjectLocal(const Vector3<double>& point, HomgPoint2D &p2d,
35  bool IgnoreDistortion) const
36 {
37  BIASERR("No local projection possible in projective case!!!");
38  BIASABORT;
39  return -1;
40 }
41 
42 
43 HomgPoint3D ProjectionParametersProjective::
44 UnProjectToImagePlane(const HomgPoint2D& pos, const double& depth,
45  bool IgnoreDistortion) const
46 {
47 // Vector3<double> p = UnProjectLocal(pos, IgnoreDistortion);
48 // if(Equal(p[2], 0.0))
49 // return HomgPoint3D(0,0,0,0);
50 // p*= depth / p[2];
51 // if(Equal(p.NormL2(), 0.0))
52 // return HomgPoint3D(0,0,0,0);
53 // HomgPoint3D local(p);
54 // return Pose_.LocalToGlobal(local);
55 
56  BIASERR("not implemented in projective case!!!")
57  BIASABORT
58 
59  HomgPoint3D dummy;
60  return dummy;
61 }
62 
63 bool ProjectionParametersProjective::
64 Distort(BIAS::HomgPoint2D& point2d) const
65 {
66  BIASERR("Cannot distort in projective case!!!");
67  BIASABORT;
68  return false;
69 }
70 
71 
72 bool ProjectionParametersProjective::
73 Undistort(BIAS::HomgPoint2D& point2d) const
74 {
75  BIASERR("Cannot undistort in projective case!!!");
76  BIASABORT;
77  return false;
78 }
79 
80 Matrix3x3<double> ProjectionParametersProjective::
81 UnProjectCovLocal(const HomgPoint2D& /*pos*/, const Matrix3x3<double>& /*cov*/,
82  bool /* IgnoreDistortion */, bool /* Normalize */) const
83 {
84  BIASERR("covariance propagation for projective space not implemented");
86 }
87 
88 #ifdef BIAS_HAVE_XML2
89 
90 int ProjectionParametersProjective::XMLGetClassName(std::string& TopLevelTag,
91  double& Version) const {
92  TopLevelTag = "ProjectionParametersProjective";
93  Version = 1.0;
94  return 0;
95 }
96 
97 int ProjectionParametersProjective::XMLOut(const xmlNodePtr Node,
98  XMLIO& XMLObject) const {
99 
100  xmlNodePtr childNode;
101 
102  childNode = XMLObject.addChildNode(Node, "ImageSize");
103  XMLObject.addAttribute(childNode, "width", (int)width_);
104  XMLObject.addAttribute(childNode, "height", (int)height_);
105 
106 
107 
108  Matrix3x4<double> MatrixCache = Pose_.GetMatrix3x4();
109  stringstream datastream;
110  childNode = XMLObject.addChildNode(Node,"MatrixRows");
111  for (unsigned int i=0; i<12; i++) {
112  datastream << (double) MatrixCache.GetData()[i] <<" ";
113  }
114  XMLObject.addContent(childNode, datastream.str());
115  return 0;
116 }
117 
118 int ProjectionParametersProjective::XMLIn(const xmlNodePtr Node,
119  XMLIO& XMLObject) {
120 
121  xmlNodePtr childNode;
122 
123  if ((childNode = XMLObject.getChild(Node, "ImageSize"))!=NULL) {
124  width_ = XMLObject.getAttributeValueInt(childNode, "width");
125  height_ = XMLObject.getAttributeValueInt(childNode, "height");
126  }
127 
128  Matrix3x4<double> MatrixCache;
129 
130  if ((childNode = XMLObject.getChild(Node, "MatrixRows"))==NULL) {
131  BIASERR("Error in xml structure.");
132  return -1;
133  }
134 
135  // get data fields
136  stringstream ssData(XMLObject.getNodeContentString(childNode));
137  unsigned int CurrentData=0;
138  double buffer;
139  while (ssData.good() && CurrentData<12) {
140  ssData >> buffer;
141  MatrixCache.GetData()[CurrentData++] = buffer;
142  }
143  Pose_.SetFromMatrix3x4(MatrixCache);
144  if (CurrentData<12) {
145  // reached end of stream before expected end of data
146  BIASERR("Error parsing XML code ...");
147  return -1;
148  }
149 
150  return 0;
151 }
152 
153 #endif
154 
155 bool ProjectionParametersProjective::
156 DoesPointProjectIntoImageLocal(const Vector3<double>& localX,
157  HomgPoint2D& x,
158  bool IgnoreDistortion) const
159 {
160 
161  BIASERR("no local projection possible in projective case")
162  BIASABORT
163 
164  return false;
165 }
166 
167 
168 bool ProjectionParametersProjective::
169 DoesPointProjectIntoImage(const Vector3<double>& X,
170  HomgPoint2D& x,
171  bool IgnoreDistortion) const
172 {
173 
174  if(width_ != 0 && height_ !=0){
175  // now compute coordinates
176  x = Project(HomgPoint3D(X), IgnoreDistortion);
177  // check if in image
178  return (x[0]>=-0.5 && x[1]>=-0.5 &&
179  x[0]<=(double)(width_)-0.5 && x[1]<=(double)(height_)-0.5 );
180  } else {
181  BIASERR("Image width and height need to be set for this method!!!")
182  }
183 
184  return false;
185 }
186 
187 
188 Vector3<double> ProjectionParametersProjective::GetC() const{
189 
190  PMatrix P = GetP();
191  SVD svd(P);
192  HomgPoint3D CHomg(svd.GetNullvector());
193  CHomg.Homogenize();
194  Vector3<double> C = Vector3<double>(CHomg[0], CHomg[1], CHomg[2]);
195  return C;
196 }
197 
198 
Vector< double > GetNullvector(const int last_offset=0)
return one of the nullvectors.
Definition: SVD.hh:404
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
Definition: XMLIO.cpp:156
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
xmlNodePtr getChild(const xmlNodePtr ParentNode, const std::string &ChildName)
Get a child of a Parent node by specifying the childs name, NULL is returned if the ParentNode has no...
Definition: XMLIO.cpp:489
void Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint3D.hh:308
int getAttributeValueInt(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:728
void addContent(const xmlNodePtr Node, const std::string &Content)
Add content to a node.
Definition: XMLIO.cpp:254
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
xmlNodePtr addChildNode(const xmlNodePtr ParentNode, const std::string &NewNodeName)
Add a child node to an incoming node with the given name.
Definition: XMLIO.cpp:131
T * GetData()
get the pointer to the data array of the matrix (for faster direct memeory access) ...
Definition: Matrix.hh:185
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: PMatrix.hh:88
std::string getNodeContentString(const xmlNodePtr Node) const
Get the content of a given Node.
Definition: XMLIO.cpp:554