GSLAM  3.0.0
Point.h
1 // GSLAM - A general SLAM framework and benchmark
2 // Copyright 2018 PILAB Inc. All rights reserved.
3 // https://github.com/zdzhaoyong/GSLAM
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 // used to endorse or promote products derived from this software without
15 // specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: zd5945@126.com (Yong Zhao)
30 
31 #ifndef GSLAM_POINT_H
32 #define GSLAM_POINT_H
33 
34 #include <iostream>
35 #include <sstream>
36 #include <math.h>
37 
38 #ifdef HAS_TOON
39 #include <TooN/TooN.h>
40 #endif
41 
42 namespace GSLAM{
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 ////////////////////////////////////////////////////////////////////////////////
46 
47 template <class Precision>
48 struct Point2_
49 {
50  Point2_():x(0),y(0){}
51  Point2_(Precision x_,Precision y_):x(x_),y(y_){}
52 
53  template <typename Scalar>
54  operator Point2_<Scalar>()
55  {
56  return Point2_<Scalar>(x,y);
57  }
58 
59 #ifdef EIGEN_MATRIX_H
60  template <typename Scalar>
61  operator Eigen::Matrix<Scalar,2,1>()
62  {
63  return Eigen::Matrix<Scalar,2,1>(x,y);
64  }
65 
66  template <typename Scalar>
67  Point2_(const Eigen::Matrix<Scalar,2,1>& eig):x(eig[0]),y(eig[1]){}
68 #endif
69 
70  inline Precision& operator[](int index)const
71  {
72  return ((Precision*)this)[index];
73  }
74 
75  friend Point2_ operator + (const Point2_& a,const Point2_& b)
76  {
77  return Point2_(a.x+b.x,a.y+b.y);
78  }
79 
80  friend Point2_ operator - (const Point2_& a,const Point2_& b)
81  {
82  return Point2_(a.x-b.x,a.y-b.y);
83  }
84 
85  friend Point2_ operator -(const Point2_& a)
86  {
87  return Point2_(-a.x,-a.y);
88  }
89 
90  friend Precision operator * (const Point2_& a,const Point2_& b)
91  {
92  return (a.x*b.x+a.y*b.y);
93  }
94 
95  friend Point2_ operator * (const Precision& a,const Point2_& b)
96  {
97  return Point2_(a*b.x,a*b.y);
98  }
99 
100  friend Point2_ operator * (const Point2_& b,const Precision& a)
101  {
102  return Point2_(a*b.x,a*b.y);
103  }
104 
105  friend Point2_ operator / (const Point2_& a,const Precision& b)
106  {
107  return (1./b)*a;
108  }
109 
110  inline Precision norm()const
111  {
112  return sqrt(x*x+y*y);
113  }
114 
115  inline Point2_<Precision> normalize()const
116  {
117  if(x*x+y*y!=0)
118  return (*this)*(1./norm());
119  else
120  return Point2_<Precision>(0,0);
121  }
122 
123  inline Precision dot(const Point2_& a)const
124  {
125  return x*a.x+y*a.y;
126  }
127 
128  Precision at(int i)const{return (*this)[i];}
129  Precision getX()const{return x;}
130  Precision getY()const{return y;}
131 
132  void setX(Precision X){x=X;}
133  void setY(Precision Y){y=Y;}
134 
135  Point2_<Precision> add(const Point2_<Precision>& r)const{return (*this)+r;}
136  Point2_<Precision> sub(const Point2_<Precision>& r)const{return (*this)-r;}
137  Point2_<Precision> mul(Precision r)const{return (*this)*r;}
138  Point2_<Precision> div(Precision r)const{return (*this)/r;}
139 
140  std::string toString()const{std::stringstream sst;sst<<*this;return sst.str();}
141 
142  friend inline std::ostream& operator <<(std::ostream& os,const Point2_& p)
143  {
144  os<<std::to_string(p.x)<<" "<<std::to_string(p.y);
145  return os;
146  }
147 
148  friend inline std::istream& operator >>(std::istream& is,Point2_& p)
149  {
150  is>>p.x>>p.y;
151  return is;
152  }
153  Precision x,y;
154 };
155 
156 typedef Point2_<double> Point2d;
157 typedef Point2_<float> Point2f;
158 typedef Point2_<int> Point2i;
159 
160 
161 template <class Precision>
162 struct Point3_
163 {
164  Point3_():x(0),y(0),z(0){}
165 
166  Point3_(Precision x_,Precision y_,Precision z_):x(x_),y(y_),z(z_){}
167 
168  inline Precision& operator[](int index)const
169  {
170  return ((Precision*)this)[index];
171  }
172 
173  inline Precision norm()const
174  {
175  return sqrt(x*x+y*y+z*z);
176  }
177 
178  inline Precision dot(const Point3_& a)const
179  {
180  return x*a.x+y*a.y+z*a.z;
181  }
182 
183  inline Point3_<Precision> cross(const Point3_& a)const
184  {
185  return Point3_<Precision>(y*a.z-z*a.y,z*a.x-x*a.z,x*a.y-y*a.x);
186  }
187 
188  inline Point3_<Precision> normalize()const
189  {
190  if(x*x+y*y+z*z!=0)
191  return (*this)*(1./norm());
192  else
193  return Point3_<Precision>(0,0,0);
194  }
195 
196  friend inline std::ostream& operator <<(std::ostream& os,const Point3_& p)
197  {
198  os<<std::to_string(p.x)<<" "<<std::to_string(p.y)<<" "<<std::to_string(p.z);
199  return os;
200  }
201 
202  friend inline std::istream& operator >>(std::istream& is,Point3_& p)
203  {
204  is>>p.x>>p.y>>p.z;
205  return is;
206  }
207 
208  friend Point3_ operator + (const Point3_& a,const Point3_& b)
209  {
210  return Point3_(a.x+b.x,a.y+b.y,a.z+b.z);
211  }
212 
213  friend Point3_ operator - (const Point3_& a,const Point3_& b)
214  {
215  return Point3_(a.x-b.x,a.y-b.y,a.z-b.z);
216  }
217 
218  friend Point3_ operator -(const Point3_& a)
219  {
220  return Point3_(-a.x,-a.y,-a.z);
221  }
222 
223  friend Precision operator * (const Point3_& a,const Point3_& b)
224  {
225  return (a.x*b.x+a.y*b.y+a.z*b.z);
226  }
227 
228  friend Point3_<Precision> operator ^ (const Point3_& a,const Point3_& b)
229  {
230  return Point3_(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);
231  }
232 
233  friend Point3_ operator * (const Precision& a,const Point3_& b)
234  {
235  return Point3_(a*b.x,a*b.y,a*b.z);
236  }
237 
238  friend Point3_ operator * (const Point3_& b,const Precision& a)
239  {
240  return Point3_(a*b.x,a*b.y,a*b.z);
241  }
242 
243  friend Point3_ operator / (const Point3_& a,const Precision& b)
244  {
245  return (1./b)*a;
246  }
247 
248  friend inline bool operator < (const Point3_& a,const Point3_ b)
249  {
250  return a.x<b.x;
251  }
252 
253  template <typename Scalar>
254  operator Point3_<Scalar>()
255  {
256  return Point3_<Scalar>(x,y,z);
257  }
258 
259  Precision at(int i)const{return (*this)[i];}
260  Precision getX()const{return x;}
261  Precision getY()const{return y;}
262  Precision getZ()const{return z;}
263 
264  void setX(Precision X){x=X;}
265  void setY(Precision Y){y=Y;}
266  void setZ(Precision Z){z=Z;}
267 
268  Point3_<Precision> add(const Point3_<Precision>& r)const{return (*this)+r;}
269  Point3_<Precision> sub(const Point3_<Precision>& r)const{return (*this)-r;}
270  Point3_<Precision> mul(Precision r)const{return (*this)*r;}
271  Point3_<Precision> div(Precision r)const{return (*this)/r;}
272 
273  std::string toString()const{std::stringstream sst;sst<<*this;return sst.str();}
274 
275 #ifdef EIGEN_MATRIX_H
276  template <typename Scalar>
277  operator Eigen::Matrix<Scalar,3,1>()
278  {
279  return Eigen::Matrix<Scalar,3,1>(x,y,z);
280  }
281 
282  template <typename Scalar>
283  Point3_(const Eigen::Matrix<Scalar,3,1>& eig):x(eig[0]),y(eig[1]),z(eig[2]){}
284 #endif
285 
286 #ifdef HAS_TOON
287  operator TooN::Vector<3,Precision>& ()
288  {
289  return *((TooN::Vector<3,Precision>*)this);
290  }
291 #endif
292 
293  Precision x,y,z;
294 };
295 
297 typedef Point3_<float> Point3f;
298 typedef Point3_<double> Point3d;
299 typedef Point3_<int> Point3i;
300 }
301 
302 #endif // GSLAM_POINT_H
Definition: Point.h:48
Definition: Point.h:162
Definition: Camera.h:45