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
11namespace 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);
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
constexpr T c
static ScalarT Abs(ScalarT s)
Definition MathHelper.h:15
PointType::ScalarType ScalarType
Definition Plane.h:18
ScalarType Distance(const PointType &p) const
Definition Plane.h:42
void OffsetInNormalDirection(ScalarType offset)
Definition Plane.h:165
void Orient(const PointType &n)
Definition Plane.hpp:32
Plane(PointType n, ScalarType d)
Definition Plane.h:23
ScalarType SignedDistance(const PointType &p) const
Definition Plane.hpp:25
bool Fit(const PointType &origin, PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights)
Definition Plane.h:71
PointType & Normal()
Definition Plane.hpp:62
PointT PointType
Definition Plane.h:17
void Project(const PointType &p, PointType *s) const
Definition Plane.hpp:41
ScalarType Intersect(const PointType &p, const PointType &r) const
Definition Plane.hpp:83
ScalarType SignedDistanceToOrigin() const
Definition Plane.h:48
void Origin(const PointType &o)
Definition Plane.hpp:76
void Set(const PointType &origin, const PointType &normal)
Definition Plane.hpp:17
Definition AABox.h:10
void CovarianceMatrix(const PointT &center, PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, MatrixT *matrix)
Definition Covariance.h:175
void Mean(PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, PointT *mean)
Definition Mean.h:14
bool Jacobi(const MatrixXX< N, N, T > &m, VectorXD< N, T > *d, MatrixXX< N, N, T > *v, int *nrot=NULL)
Definition Jacobi.h:24
void EigSortDesc(VectorXD< N, T > *d, MatrixXX< N, N, T > *v)
Definition Jacobi.h:143