Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CPOINT_H
00029 #define CPOINT_H
00030
00031 #include <mrpt/poses/CPoseOrPoint.h>
00032
00033 namespace mrpt
00034 {
00035 namespace poses
00036 {
00037
00038
00039
00040
00041
00042
00043 template <class DERIVEDCLASS>
00044 class CPoint : public CPoseOrPoint<DERIVEDCLASS>
00045 {
00046 public:
00047
00048
00049
00050
00051
00052
00053 template <class OTHERCLASS>
00054 inline void AddComponents(const OTHERCLASS &b)
00055 {
00056 const int dims = std::min( size_t(DERIVEDCLASS::static_size), size_t(OTHERCLASS::is3DPoseOrPoint() ? 3:2));
00057 for (int i=0;i<dims;i++)
00058 static_cast<DERIVEDCLASS*>(this)->m_coords[i]+= static_cast<const OTHERCLASS*>(&b)->m_coords[i];
00059 }
00060
00061
00062 inline void operator *=(const double s)
00063 {
00064 for (int i=0;i<DERIVEDCLASS::static_size;i++)
00065 static_cast<DERIVEDCLASS*>(this)->m_coords[i] *= s;
00066 }
00067
00068
00069 inline void getAsVector(vector_double &v) const
00070 {
00071 v.resize(DERIVEDCLASS::static_size);
00072 for (int i=0;i<DERIVEDCLASS::static_size;i++)
00073 v[i] = static_cast<const DERIVEDCLASS*>(this)->m_coords[i];
00074 }
00075
00076 inline vector_double getAsVector() const { vector_double v; getAsVector(v); return v; }
00077
00078
00079
00080
00081 void getHomogeneousMatrix(CMatrixDouble44 & out_HM ) const
00082 {
00083 out_HM.unit(4,1.0);
00084 out_HM.get_unsafe(0,3)= static_cast<const DERIVEDCLASS*>(this)->x();
00085 out_HM.get_unsafe(1,3)= static_cast<const DERIVEDCLASS*>(this)->y();
00086 if (DERIVEDCLASS::is3DPoseOrPoint())
00087 out_HM.get_unsafe(2,3)= static_cast<const DERIVEDCLASS*>(this)->m_coords[2];
00088 }
00089
00090
00091
00092
00093 void asString(std::string &s) const
00094 {
00095 s = (DERIVEDCLASS::is3DPoseOrPoint()) ?
00096 mrpt::format("[%f %f]", static_cast<const DERIVEDCLASS*>(this)->x(), static_cast<const DERIVEDCLASS*>(this)->y()) :
00097 mrpt::format("[%f %f %f]",static_cast<const DERIVEDCLASS*>(this)->x(), static_cast<const DERIVEDCLASS*>(this)->y(), static_cast<const DERIVEDCLASS*>(this)->m_coords[2]);
00098 }
00099 inline std::string asString() const { std::string s; asString(s); return s; }
00100
00101
00102
00103
00104
00105 void fromString(const std::string &s)
00106 {
00107 CMatrixDouble m;
00108 if (!m.fromMatlabStringFormat(s)) THROW_EXCEPTION("Malformed expression in ::fromString");
00109 ASSERT_EQUAL_(mrpt::math::size(m,1),1)
00110 ASSERT_EQUAL_(mrpt::math::size(m,2),DERIVEDCLASS::static_size)
00111 for (int i=0;i<DERIVEDCLASS::static_size;i++)
00112 static_cast<DERIVEDCLASS*>(this)->m_coords[i] = m.get_unsafe(0,i);
00113 }
00114
00115 inline const double &operator[](unsigned int i) const { return static_cast<const DERIVEDCLASS*>(this)->m_coords[i]; }
00116 inline double &operator[](unsigned int i) { return static_cast<DERIVEDCLASS*>(this)->m_coords[i]; }
00117
00118
00119
00120 };
00121
00122
00123 template <class DERIVEDCLASS>
00124 std::ostream &operator << (std::ostream& o, const CPoint<DERIVEDCLASS>& p)
00125 {
00126 o << "(" << p[0] << "," << p[1];
00127 if (p.is3DPoseOrPoint()) o << "," << p[2];
00128 o <<")";
00129 return o;
00130 }
00131
00132
00133 template <class DERIVEDCLASS>
00134 bool operator < (const CPoint<DERIVEDCLASS> &a, const CPoint<DERIVEDCLASS> &b)
00135 {
00136 if (a.x()<b.x()) return true;
00137 else
00138 {
00139 if (!a.is3DPoseOrPoint())
00140 return a.y()<b.y();
00141 else if (a.y()<b.y())
00142 return true;
00143 else return a[2]<b[2];
00144 }
00145 }
00146
00147 template <class DERIVEDCLASS>
00148 bool operator==(const CPoint<DERIVEDCLASS> &p1,const CPoint<DERIVEDCLASS> &p2)
00149 {
00150 for (int i=0;i<DERIVEDCLASS::static_size;i++)
00151 if (p1[i]!=p2[i]) return false;
00152 return true;
00153 }
00154
00155 template <class DERIVEDCLASS>
00156 bool operator!=(const CPoint<DERIVEDCLASS> &p1,const CPoint<DERIVEDCLASS> &p2)
00157 {
00158 for (int i=0;i<DERIVEDCLASS::static_size;i++)
00159 if (p1[i]!=p2[i]) return true;
00160 return false;
00161 }
00162
00163
00164
00165 }
00166 }
00167
00168 #endif