Plane.h
Go to the documentation of this file.
1 #ifndef __GfxTL_PLANE_HEADER__
2 #define __GfxTL_PLANE_HEADER__
3 #include <GfxTL/Covariance.h>
4 #include <GfxTL/Jacobi.h>
5 #include <GfxTL/Mean.h>
6 #include <GfxTL/MatrixXX.h>
7 #include <GfxTL/VectorXD.h>
8 #include <GfxTL/MathHelper.h>
9 #include <GfxTL/WeightFunc.h>
10 
11 namespace GfxTL
12 {
13  template< class PointT >
14  class Plane
15  {
16  public:
17  typedef PointT PointType;
18  typedef typename PointType::ScalarType ScalarType;
19 
20  Plane();
21  Plane(const PointType& normal, const PointType& origin);
23  : _normal(n)
24  , _d(d)
25  {}
26  void Set(const PointType& origin, const PointType& normal);
27  template< class PointsForwardIt, class WeightsForwardIt >
28  bool Fit(const PointType& origin, PointsForwardIt begin,
29  PointsForwardIt end, WeightsForwardIt weights);
30  template< class PointsForwardIt >
31  bool Fit(const PointType& origin, PointsForwardIt begin,
32  PointsForwardIt end);
33  template< class PointsForwardIt, class WeightsForwardIt >
34  bool Fit(PointsForwardIt begin, PointsForwardIt end,
35  WeightsForwardIt weights);
36  template< class PointsForwardIt >
37  bool Fit(PointsForwardIt begin, PointsForwardIt end);
38  ScalarType SignedDistance(const PointType& p) const;
39  ScalarType Distance(const PointType& p) const
40  {
42  }
44  {
45  return -_d;
46  }
47  void Orient(const PointType& n);
48  void Project(const PointType& p, PointType* s) const;
49  void Normal(PointType* normal) const;
50  void Normal(const PointType& normal);
51  PointType& Normal();
52  const PointType& Normal() const;
53  void Origin(const PointType& o);
54  ScalarType Intersect(const PointType& p, const PointType& r) const;
56 
57  private:
58  PointType _normal;
59  ScalarType _d;
60  };
61 
62  template< class PointT >
63  template< class PointsForwardIt, class WeightsForwardIt >
64  bool Plane< PointT >::Fit(const PointType& origin, PointsForwardIt begin,
65  PointsForwardIt end, WeightsForwardIt weights)
66  {
68  CovarianceMatrix(origin, begin, end, weights, &c);
70  if (!Jacobi(c, &d, &v))
71  {
72  //std::cout << "Jacobi failed:" << std::endl;
73  //std::cout << "origin = " << origin[0] << "," << origin[1] << "," << origin[2] << std::endl
74  // << "cov:" << std::endl
75  // << c[0][0] << c[1][0] << c[2][0] << std::endl
76  // << c[0][1] << c[1][1] << c[2][1] << std::endl
77  // << c[0][2] << c[1][2] << c[2][2] << std::endl;
78  //std::cout << "recomp origin:" << std::endl;
79  //PointT com;
80  //Mean(begin, end, weights, &com);
81  //std::cout << "origin = " << origin[0] << "," << origin[1] << "," << origin[2] << std::endl;
82  //std::cout << "recomp covariance:" << std::endl;
83  //CovarianceMatrix(com, begin, end, weights, &c);
84  //std::cout << "cov:" << std::endl
85  //<< c[0][0] << c[1][0] << c[2][0] << std::endl
86  //<< c[0][1] << c[1][1] << c[2][1] << std::endl
87  //<< c[0][2] << c[1][2] << c[2][2] << std::endl;
88  //std::cout << "weights and points:" << std::endl;
89  //WeightsForwardIt w = weights;
90  //for(PointsForwardIt i = begin; i != end; ++i, ++w)
91  // std::cout << (*i)[0] << "," << (*i)[1] << "," << (*i)[2]
92  // << " weight=" << (*w) << std::endl;
93  return false;
94  }
95  for (unsigned int i = 0; i < PointType::Dim; ++i)
96  {
97  d[i] = Math< ScalarType >::Abs(d[i]);
98  }
99  EigSortDesc(&d, &v);
100  _normal = PointType(v[PointType::Dim - 1]);
101  _d = -(_normal * origin);
102  return true;
103  }
104 
105  template< class PointT >
106  template< class PointsForwardIt >
107  bool Plane< PointT >::Fit(const PointType& origin, PointsForwardIt begin,
108  PointsForwardIt end)
109  {
110  return Fit(origin, begin, end, UnitWeightIterator());
111  }
112 
113  template< class PointT >
114  template< class PointsForwardIt, class WeightsForwardIt >
115  bool Plane< PointT >::Fit(PointsForwardIt begin,
116  PointsForwardIt end, WeightsForwardIt weights)
117  {
118  // copy weights
119  //std::vector< ScalarType > ww(end - begin);
120  //for(unsigned int i = 0; i < ww.size(); ++i)
121  // ww[i] = weights[i];
122  PointT com;
123  Mean(begin, end, weights, &com);
124  bool retVal = Fit(com, begin, end, weights);
125  // cmp weights
126  //WeightsForwardIt w = weights;
127  //for(unsigned int i = 0; i < ww.size(); ++i, ++w)
128  // if(ww[i] != *w)
129  // std::cout << "WEIGHT CHANGED!!! " << ww[i] << "!=" << *w << std::endl;
130  return retVal;
131  //MatrixXX< PointType::Dim, PointType::Dim, ScalarType > c, v;
132  //PointType origin;
133  //CovarianceMatrix(begin, end, weights, &origin, &c);
134  //VectorXD< PointType::Dim, ScalarType > d;
135  //if(!Jacobi(c, &d, &v))
136  // return false;
137  //for(unsigned int i = 0; i < PointType::Dim; ++i)
138  // d[i] = Math< ScalarType >::Abs(d[i]);
139  //EigSortDesc(&d, &v);
140  //_normal = PointType(v[PointType::Dim - 1]);
141  //_normal.Normalize();
142  //_d = -(_normal * origin);
143  //return true;
144  }
145 
146  template< class PointT >
147  template< class PointsForwardIt >
148  bool Plane< PointT >::Fit(PointsForwardIt begin, PointsForwardIt end)
149  {
150  return Fit(begin, end, UnitWeightIterator());
151  }
152 
153  template< class PointT >
155  {
156  _d += offset;
157  }
158 };
159 
160 #include "Plane.hpp"
161 
162 #endif
163 
GfxTL::VectorXD
Definition: MatrixXX.h:21
GfxTL::Plane::ScalarType
PointType::ScalarType ScalarType
Definition: Plane.h:18
GfxTL::Plane::PointType
PointT PointType
Definition: Plane.h:17
GfxTL::Plane
Definition: Plane.h:14
visionx::armem::pointcloud::PointType
PointType
Definition: constants.h:78
Mean.h
GfxTL::Plane::OffsetInNormalDirection
void OffsetInNormalDirection(ScalarType offset)
Definition: Plane.h:154
GfxTL::Plane::Orient
void Orient(const PointType &n)
Definition: Plane.hpp:30
WeightFunc.h
GfxTL::Plane::Plane
Plane(PointType n, ScalarType d)
Definition: Plane.h:22
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
GfxTL::Plane::Intersect
ScalarType Intersect(const PointType &p, const PointType &r) const
Definition: Plane.hpp:74
GfxTL::Plane::Origin
void Origin(const PointType &o)
Definition: Plane.hpp:68
GfxTL::MatrixXX
Definition: MatrixXX.h:25
GfxTL::Math::Abs
static ScalarT Abs(ScalarT s)
Definition: MathHelper.h:14
VectorXD.h
GfxTL::Plane::SignedDistanceToOrigin
ScalarType SignedDistanceToOrigin() const
Definition: Plane.h:43
GfxTL::EigSortDesc
void EigSortDesc(VectorXD< N, T > *d, MatrixXX< N, N, T > *v)
Definition: Jacobi.h:144
GfxTL::Mean
void Mean(PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, PointT *mean)
Definition: Mean.h:14
GfxTL::Jacobi
bool Jacobi(const MatrixXX< N, N, T > &m, VectorXD< N, T > *d, MatrixXX< N, N, T > *v, int *nrot=NULL)
Definition: Jacobi.h:22
Covariance.h
armarx::PointT
pcl::PointXYZRGBL PointT
Definition: Common.h:28
GfxTL::Plane::Set
void Set(const PointType &origin, const PointType &normal)
Definition: Plane.hpp:16
GfxTL::Plane::Project
void Project(const PointType &p, PointType *s) const
Definition: Plane.hpp:38
GfxTL
Definition: AABox.h:8
GfxTL::Plane::Plane
Plane()
Definition: Plane.hpp:5
MatrixXX.h
GfxTL::Plane::Normal
PointType & Normal()
Definition: Plane.hpp:56
GfxTL::Plane::SignedDistance
ScalarType SignedDistance(const PointType &p) const
Definition: Plane.hpp:23
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
Jacobi.h
GfxTL::CovarianceMatrix
void CovarianceMatrix(const PointT &center, PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, MatrixT *matrix)
Definition: Covariance.h:162
Plane.hpp
GfxTL::Plane::Distance
ScalarType Distance(const PointType &p) const
Definition: Plane.h:39
MathHelper.h
GfxTL::Plane::Fit
bool Fit(const PointType &origin, PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights)
Definition: Plane.h:64
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
GfxTL::UnitWeightIterator
Definition: WeightFunc.h:76