Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ProjectionParametersSphericalSimple.cpp
1 
2 #include "Geometry/ProjectionParametersSphericalSimple.hh"
3 
4 #include <Base/Debug/Exception.hh>
5 
6 using namespace std;
7 using namespace BIAS;
8 
9 
10 ProjectionParametersSphericalSimple::
11 ProjectionParametersSphericalSimple()
12  : ProjectionParametersBase(), phiOffset_(0.0), thetaOffset_(0.0),
13  dPhi_(0.0), dTheta_(0.0)
14 {}
15 
18  const double thetaOffs,
19  const double dPhi,
20  const double dTheta,
21  const unsigned int width,
22  const unsigned int height)
23  : ProjectionParametersBase(width, height),
24  phiOffset_(phiOffs), thetaOffset_(thetaOffs), dPhi_(dPhi), dTheta_(dTheta)
25 {
26  // deal with periodicy of phi and theta
27  // phi must be in range between -pi and +pi
28  // theta must be in range [0, pi]
29  const double pi = M_PI;
30  const double two_pi = 2. * M_PI;
31  while (phiOffset_<-pi) { phiOffset_ += two_pi; }
32  while (phiOffset_>pi) { phiOffset_ -= two_pi; }
33  while (thetaOffset_<0.) { thetaOffset_ += two_pi; }
34  while (thetaOffset_>two_pi) { thetaOffset_ -= two_pi; }
35 }
36 
37 
38 /** @brief calculates the projection of a point
39  in the local camera coordinate system
40  to a pixel in the image plane of the camera
41 
42  In the simplest case perspective pinhole projection x = K * point
43  where point is transformed of X using point = (R^T | -R^T C) X */
45 ProjectLocal(const Vector3<double>& point, bool IgnoreDistortion) const
46 {
47  HomgPoint2D res;
48  ProjectLocal(point, res, IgnoreDistortion);
49 
50  return res;
51 }
52 
53 
56  bool IgnoreDistortion) const
57 {
58  if (fabs(point.NormL2())<1e-6) { // point coincides with camera center
59  p2d.Set(0., 0., 0.);
60  return -1;
61  }
62  Vector2<double> pixel;
63  Local2Pixel(point, pixel);
64  p2d.Set(pixel[0], pixel[1], 1.0);
65  return 0;
66 }
67 
68 
69 
70 /** @brief calculates the unit size viewing ray from the camera center
71  (in the camera coordinate system) which belongs to the image position
72  pos e.g. pos=principal point results in (0,0,1)=optical axis */
74 UnProjectLocal(const HomgPoint2D& pos, Vector3<double>& pointOnRay,
75  Vector3<double>& direction, bool IgnoreDistortion) const
76 {
77  if (pos.IsAtInfinity()) {
78  BEXCEPTION("cannot unproject point at infinity "<<pos);
79  }
80  Vector2<double> pix(pos[0]/pos[2], pos[1]/pos[2]);
81  Pixel2Local(pix, direction);
82  pointOnRay = Vector3<double>(0.0);
83 }
84 
85 
87 Rescale(double ratio, const double offset)
88 {
89  ProjectionParametersBase::Rescale(ratio, offset);
90  dPhi_ *= ratio;
91  dTheta_ *= ratio;
92 }
93 
94 void ProjectionParametersSphericalSimple::Rescale(unsigned int width, unsigned int height){
95 
96  unsigned int oldwidth = width_;
97  ProjectionParametersBase::Rescale(width, height);
98  dPhi_ *= ((double)oldwidth/(double)width_);
99  dTheta_ *= ((double)oldwidth/(double)width_);
100 }
101 
102 
103 /* map points from image onto unit diameter image plane in 3D. */
105 UnProjectToImagePlane(const HomgPoint2D& pos, const double& depth,
106  bool IgnoreDistortion) const
107 {
108  HomgPoint3D res(0,0,0,0);
109  Vector3<double> localv, p;
110  UnProjectLocal(pos, p, localv, IgnoreDistortion);
111  double r = localv.NormL2();
112  if(Equal(r,0.0))
113  {
114  return res;
115  }
116  localv*= depth/r;
117  if(Equal(localv.NormL2(), 0.0))
118  return res;
119  res = HomgPoint3D(localv);
120  res[3] = 1.0;
121  return Pose_.LocalToGlobal(res);
122 }
123 
124 
127  HomgPoint2D& x, bool IgnoreDistortion) const
128 {
129  x = ProjectLocal(localX, IgnoreDistortion);
130  return((x[0]>=0.0)&&(x[0]+1.0<=width_)&&
131  (x[1]>=0.0)&&(x[1]+1.0<=height_));
132 }
133 
134 
135 #ifdef BIAS_HAVE_XML2
136 
138 XMLGetClassName(std::string& TopLevelTag, double& Version) const
139 {
140  TopLevelTag = "ProjectionParametersSphericalSimple";
141  Version = 1.0;
142  return 0;
143 }
144 
145 
147 XMLOut(const xmlNodePtr Node, XMLIO& XMLObject) const
148 {
149  xmlNodePtr theNode;
150  string TopLevelName;
151  double Version;
152  ProjectionParametersBase::XMLGetClassName(TopLevelName, Version);
153  theNode = XMLObject.addChildNode(Node, TopLevelName);
154  XMLObject.addAttribute(theNode, "Version", Version);
155  ProjectionParametersBase::XMLOut(theNode, XMLObject);
156 
157  XMLObject.addAttribute(Node,"ThetaOffset", thetaOffset_);
158  XMLObject.addAttribute(Node,"PhiOffset", phiOffset_);
159  XMLObject.addAttribute(Node,"DTheta", dTheta_);
160  XMLObject.addAttribute(Node,"DPhi", dPhi_);
161 
162  return 0;
163 }
164 
165 
167 XMLIn(const xmlNodePtr Node, XMLIO& XMLObject)
168 {
169  string TopLevelName;
170  double Version;
171  ProjectionParametersBase::XMLGetClassName(TopLevelName, Version);
172  if (XMLObject.getAttributeByName(Node, "Version")==NULL) {
173  BIASERR("error getting \"Version\" attribute of \n");
174  return -1;
175  }
176  double VersionInFile = XMLObject.getAttributeValueDouble(Node, "Version");
177  if (VersionInFile<Version){
178  BEXCEPTION("Cannot read old version "<<VersionInFile
179  <<" of ProjectionParametersSphericalSimple");
180  }
181 
182  xmlNodePtr childNode;
183  if ((childNode = XMLObject.getChild(Node, TopLevelName))==NULL) {
184  BIASERR("Error in xml, no tag" << TopLevelName);
185  return -1;
186  }
187  if (ProjectionParametersBase::XMLIn(childNode, XMLObject)!=0) return -1;
188 
189 
190  if (XMLObject.getAttributeByName(Node, "ThetaOffset")==NULL)
191  return -1;
192  thetaOffset_ = XMLObject.getAttributeValueDouble(Node, "ThetaOffset");
193 
194  if (XMLObject.getAttributeByName(Node, "PhiOffset")==NULL)
195  return -1;
196  phiOffset_ = XMLObject.getAttributeValueDouble(Node, "PhiOffset");
197 
198  if (XMLObject.getAttributeByName(Node, "DTheta")==NULL)
199  return -1;
200  dTheta_ = XMLObject.getAttributeValueDouble(Node, "DTheta");
201  if (XMLObject.getAttributeByName(Node, "DPhi")==NULL)
202  return -1;
203  dPhi_ = XMLObject.getAttributeValueDouble(Node, "DPhi");
204 
205  // deal with periodicy of phi and theta
206  // phi must be in range between -pi and +pi
207  // theta must be in range [0, pi]
208  const double pi = M_PI;
209  const double two_pi = 2. * M_PI;
210  while (phiOffset_<-pi) { phiOffset_ += two_pi; }
211  while (phiOffset_>pi) { phiOffset_ -= two_pi; }
212  while (thetaOffset_<0.) { thetaOffset_ += two_pi; }
213  while (thetaOffset_>two_pi) { thetaOffset_ -= two_pi; }
214 
215  R_ = GetR();
216 
217  return 0;
218 }
219 
220 #endif // BIAS_HAVE_XML2
221 
222 
225 {
227  R_ = Pose_.GetR();
228 }
229 
230 
233 {
235  R_ = Pose_.GetR();
236 }
237 
238 
241 {
243  R_ = Pose_.GetR();
244 }
245 
246 
248 SetPose(const BIAS::Pose pose)
249 {
251  R_ = Pose_.GetR();
252 }
253 
256 {
258  R_ = R;
259 }
260 
263 {
266 }
267 
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
Definition: XMLIO.cpp:156
BIAS::RMatrixBase GetR() const
Get orientation of local coordinate system as rotation matrix mapping from local coordinates to globa...
virtual bool DoesPointProjectIntoImageLocal(const Vector3< double > &localX, HomgPoint2D &x, bool IgnoreDistort) const
Checks if 3D point projects into specified image and returns belonging 2D image point.
void Local2Pixel(const Vector3< double > &p2d, Vector2< double > &pixel) const
Transform from local camera centric reference system to pixel coo.
virtual void ValidatePose()
Validate currently set pose.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
virtual void SetR(const BIAS::RMatrix &R)
Set orientation from rotation matrix R.
virtual int XMLOut(const xmlNodePtr Node, XMLIO &XMLObject) const
Specialization of XML write function.
BIAS::RMatrixBase R_
a cache for the orientation of the camera
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
virtual void Rescale(double ratio, const double offset=0.0)
Adapt internal parameters to resampled image.
Slim class bundeling pose parametrization and associated covariance matrix.
virtual int XMLIn(const xmlNodePtr Node, XMLIO &XMLObject)
Specialization of XML read function.
virtual void SetPose(const BIAS::Pose pose)
Set pose from pose object.
3D rotation matrix
Definition: RMatrix.hh:49
virtual void SetR(const BIAS::RMatrix &R)
Set orientation from rotation matrix R.
virtual void SetQ(const BIAS::Quaternion< double > &Q)
Set orientation from unit quaternion Q.
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
Definition: Pose.hh:73
virtual void SetQC(const BIAS::Quaternion< double > &Q, const BIAS::Vector3< double > &C)
Set pose from unit quaternion Q and projection center C.
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
virtual int XMLGetClassName(std::string &TopLevelTag, double &Version) const
specialization of XML block name function
bool IsAtInfinity() const
Definition: HomgPoint2D.hh:165
virtual int XMLIn(const xmlNodePtr Node, XMLIO &XMLObject)
specialization of XML read function
unsigned int height_
height of image in pixels
virtual BIAS::RMatrix GetR() const
Get orientation as rotation matrix R.
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
virtual void SetPoseParametrization(const PoseParametrization &pp)
Set pose from pose parametrization.
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_...
xmlAttrPtr getAttributeByName(const xmlNodePtr Node, const std::string &attribute_name)
search for a specific attribute
Definition: XMLIO.cpp:650
unsigned int width_
width of image in pixels
HomgPoint3D LocalToGlobal(const HomgPoint3D &X_L) const
Transform point from local frame to world coordinate frame.
void Pixel2Local(const Vector2< double > &pixel, Vector3< double > &p2d) const
Transform from pixel coo to local reference frame.
double getAttributeValueDouble(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:736
virtual void UnProjectLocal(const HomgPoint2D &pos, Vector3< double > &pointOnRay, Vector3< double > &direction, bool IgnoreDistortion=false) const
calculates the unit size viewing ray from the camera center (in the camera coordinate system) which b...
virtual HomgPoint2D ProjectLocal(const Vector3< double > &point, bool IgnoreDistortion=false) const
calculates the projection of a point in the local camera coordinate system to a pixel in the image pl...
virtual void SetQ(const BIAS::Quaternion< double > &Q)
Set orientation from unit quaternion Q.
virtual void ValidatePose()
Validate currently set pose.
virtual void SetQC(const BIAS::Quaternion< double > &Q, const BIAS::Vector3< double > &C)
Set pose from unit quaternion Q and projection center C.
virtual int XMLGetClassName(std::string &TopLevelTag, double &Version) const
Specialization of XML block name function.
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual void SetPose(const BIAS::Pose pose)
Set pose from pose object.
void Rescale(double ratio, const double offset)
Adapt internal parameters to resampled image.
void Set(const HOMGPOINT2D_TYPE &x, const HOMGPOINT2D_TYPE &y)
set elementwise with given 2 euclidean scalar values.
Definition: HomgPoint2D.hh:174
virtual void SetPoseParametrization(const BIAS::PoseParametrization &pp)
Set pose from pose parametrization.
virtual int XMLOut(const xmlNodePtr Node, XMLIO &XMLObject) const
specialization of XML write function
virtual HomgPoint3D UnProjectToImagePlane(const HomgPoint2D &pos, const double &depth=1.0, bool IgnoreDistortion=false) const
map points from image onto unit diameter image plane in 3D.
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633
class BIASGeometryBase_EXPORT HomgPoint3D