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