Plane.cpp
Go to the documentation of this file.
1 #include "Plane.h"
2 //#include "pca.h"
3 #include <MiscLib/Performance.h>
4 #include "LevMarFitting.h"
5 #include <GfxTL/VectorXD.h>
7 #include <GfxTL/Plane.h>
8 #include <GfxTL/Mean.h>
9 
11 {
12  m_normal = (p2 - p1).cross(p3 - p2);
14  m_pos = p1;
16 }
17 
19 {
20  m_normal = normal;
21  m_pos = p1;
23 }
24 
26 {
27 }
28 
29 bool Plane::Init(Vec3f p1, Vec3f p2, Vec3f p3)
30 {
31  m_normal = (p2 - p1).cross(p3 - p2);
32  if (m_normal.sqrLength() < 1E-6f)
33  {
34  return false;
35  }
37  m_pos = p1;
39  return true;
40 }
41 
43 {
44  if (samples.size() < 6)
45  {
46  return false;
47  }
48  return Init(samples[0], samples[1], samples[2]);
49 }
50 
52 {
53  if (samples.size() < 1)
54  {
55  return false;
56  }
57  m_normal = Vec3f(0, 0, 0);
58  m_pos = Vec3f(0, 0, 0);
59  size_t c = samples.size() / 2;
61  for (intptr_t i = 0; i < c; ++i)
62  {
63  normals[i] = GfxTL::Vector3Df(samples[i + c]);
64  }
65  GfxTL::Vector3Df meanNormal;
66  GfxTL::MeanOfNormals(normals.begin(), normals.end(), &meanNormal);
67  m_normal = Vec3f(meanNormal.Data());
69  GfxTL::Mean(samples.begin(), samples.begin() + c, &mean);
70  m_pos = Vec3f(mean.Data());
72  return true;
73 }
74 
75 bool Plane::Init(bool binary, std::istream* i)
76 {
77  if (binary)
78  {
79  i->read((char*)&m_normal, sizeof(m_normal));
80  i->read((char*)&m_dist, sizeof(m_dist));
81  i->read((char*)&m_pos, sizeof(m_pos));
82  }
83  else
84  {
85  for (size_t j = 0; j < 3; ++j)
86  {
87  (*i) >> m_normal[j];
88  }
89  (*i) >> m_dist;
90  for (size_t j = 0; j < 3; ++j)
91  {
92  (*i) >> m_pos[j];
93  }
94  }
95  return true;
96 }
97 
98 void Plane::Init(float* array)
99 {
100  for (int i = 0; i < 3; i++)
101  {
102  m_normal[i] = array[i];
103  m_pos[i] = array[i + 4];
104  }
105  m_dist = array[3];
106 }
107 
108 void Plane::Init(FILE* i)
109 {
110  fread(&m_normal, sizeof(m_normal), 1, i);
111  fread(&m_dist, sizeof(m_dist), 1, i);
112  fread(&m_pos, sizeof(m_pos), 1, i);
113 }
114 
115 bool Plane::equals(Plane other)
116 {
117  return ((other.getNormal().dot(getNormal()) > 0.90) &&
118  (getDistance(other.getPosition()) < 0.2));
119 }
120 
121 template< class WeightT >
123  : public WeightT
124 {
125 public:
126  enum { NumParams = 4 };
127  typedef float ScalarType;
128 
130  {}
131  // parametrization:
132  // params[0] - params[2] = normal vector
133  // params[3] = dist to origin
134 
135  template< class IteratorT >
136  ScalarType Chi(const ScalarType* params, IteratorT begin, IteratorT end,
137  ScalarType* values, ScalarType* temp) const
138  {
139  ScalarType chi = 0;
140  int size = end - begin;
141  #pragma omp parallel for schedule(static) reduction(+:chi)
142  for (int idx = 0; idx < size; ++idx)
143  {
144  temp[idx] = params[0] * begin[idx][0] + params[1] * begin[idx][1]
145  + params[2] * begin[idx][2] - params[3];
146  chi += (values[idx] = WeightT::Weigh(temp[idx]))
147  * values[idx];
148  }
149  return chi;
150  }
151 
152  template< class IteratorT >
153  void Derivatives(const ScalarType* params, IteratorT begin, IteratorT end,
154  const ScalarType* values, const ScalarType* temp, ScalarType* matrix) const
155  {
156  int size = end - begin;
157  #pragma omp parallel for schedule(static)
158  for (int idx = 0; idx < size; ++idx)
159  {
160  matrix[idx * NumParams + 0] = begin[idx][0];
161  matrix[idx * NumParams + 1] = begin[idx][1];
162  matrix[idx * NumParams + 2] = begin[idx][2];
163  matrix[idx * NumParams + 3] = -1;
164  WeightT::template DerivWeigh< NumParams >(temp[idx],
165  matrix + idx * NumParams);
166  }
167  }
168 
169  void Normalize(ScalarType* params) const
170  {
171  ScalarType len = std::sqrt(params[0] * params[0]
172  + params[1] * params[1] + params[2] * params[2]);
173  params[0] /= len;
174  params[1] /= len;
175  params[2] /= len;
176  }
177 };
178 
182 {
183  LeastSquaresFit(GfxTL::IndexIterate(begin, pc.begin()),
184  GfxTL::IndexIterate(end, pc.begin()));
185  return true;
186 }
187 
189  const MiscLib::Vector< float >& weights, Plane* ip)
190 {
191  Vec3f normal(0, 0, 0);
192  Vec3f position(0, 0, 0);
193  for (size_t i = 0; i < planes.size(); ++i)
194  {
195  normal += weights[i] * planes[i].getNormal();
196  position += weights[i] * planes[i].getPosition();
197  }
198  normal.normalize();
199  *ip = Plane(position, normal);
200  return true;
201 }
202 
203 void Plane::Serialize(bool binary, std::ostream* o) const
204 {
205  if (binary)
206  {
207  o->write((const char*)&m_normal, sizeof(m_normal));
208  o->write((const char*)&m_dist, sizeof(m_dist));
209  o->write((const char*)&m_pos, sizeof(m_pos));
210  }
211  else
212 #ifdef DIRK_FORMAT
213  {
214  (*o) << std::endl
215  << "n " << m_normal[0] << " " << m_normal[1] << " " << m_normal[2] << std::endl
216  << "d " << m_dist << std::endl
217  << "p ";
218  for (size_t i = 0; i < 3; ++i)
219  {
220  (*o) << m_pos[i] << " ";
221  }
222  (*o) << std::endl;
223  }
224 #else
225  {
226  (*o) << m_normal[0] << " " << m_normal[1] << " " << m_normal[2] << " "
227  << m_dist << " ";
228  for (size_t i = 0; i < 3; ++i)
229  {
230  (*o) << m_pos[i] << " ";
231  }
232  }
233 #endif
234 }
235 
237 {
238  return sizeof(Vec3f)
239  + sizeof(float)
240  + sizeof(Vec3f);
241 }
242 
244 {
245  return 7;
246 }
247 
248 void Plane::Serialize(float* array) const
249 {
250  for (int i = 0; i < 3; i++)
251  {
252  array[i] = m_normal[i];
253  array[i + 4] = m_pos[i];
254  }
255  array[3] = m_dist;
256 }
257 
258 
259 void Plane::Serialize(FILE* o) const
260 {
261  fwrite(&m_normal, sizeof(m_normal), 1, o);
262  fwrite(&m_dist, sizeof(m_dist), 1, o);
263  fwrite(&m_pos, sizeof(m_pos), 1, o);
264 }
265 
266 void Plane::Transform(float scale, const Vec3f& translate)
267 {
268  m_pos *= scale;
269  m_pos += translate;
271 }
272 
273 float Plane::Intersect(const Vec3f& p, const Vec3f& r) const
274 {
275  return -SignedDistance(p) / (m_normal.dot(r));
276 }
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
GfxTL::VectorXD
Definition: MatrixXX.h:21
MiscLib::Vector::begin
T * begin()
Definition: Vector.h:460
Mean.h
Vec3f::normalize
float normalize()
Definition: basic.h:125
LevMarPlane::Chi
ScalarType Chi(const ScalarType *params, IteratorT begin, IteratorT end, ScalarType *values, ScalarType *temp) const
Definition: Plane.cpp:136
Vec3f
Definition: basic.h:16
Performance.h
LevMarPlane::ScalarType
float ScalarType
Definition: Plane.cpp:127
Plane::LeastSquaresFit
bool LeastSquaresFit(const PointCloud &pc, MiscLib::Vector< size_t >::const_iterator begin, MiscLib::Vector< size_t >::const_iterator end)
Definition: Plane.cpp:179
Plane::Interpolate
static bool Interpolate(const MiscLib::Vector< Plane > &planes, const MiscLib::Vector< float > &weights, Plane *ip)
Definition: Plane.cpp:188
LevMarPlane
Definition: Plane.cpp:122
Plane::Transform
void Transform(float scale, const Vec3f &translate)
Definition: Plane.cpp:266
LevMarPlane::Derivatives
void Derivatives(const ScalarType *params, IteratorT begin, IteratorT end, const ScalarType *values, const ScalarType *temp, ScalarType *matrix) const
Definition: Plane.cpp:153
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
Plane::InitAverage
bool InitAverage(const MiscLib::Vector< Vec3f > &samples)
Definition: Plane.cpp:51
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
LevMarPlane::NumParams
@ NumParams
Definition: Plane.cpp:126
LevMarPlane::LevMarPlane
LevMarPlane()
Definition: Plane.cpp:129
Plane.h
MiscLib::Vector
Definition: Vector.h:19
VectorXD.h
LevMarFitting.h
Plane::SerializedSize
static size_t SerializedSize()
Definition: Plane.cpp:236
GfxTL::Vector3Df
VectorXD< 3, float > Vector3Df
Definition: VectorXD.h:676
Plane::Plane
Plane()
Definition: Plane.h:20
MiscLib::Vector::size
size_type size() const
Definition: Vector.h:212
Plane::getPosition
const Vec3f & getPosition() const
Definition: Plane.h:55
GfxTL::Mean
void Mean(PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, PointT *mean)
Definition: Mean.h:14
Plane::equals
bool equals(Plane plane)
Definition: Plane.cpp:115
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1615
GfxTL::IndexIterate
IndexedIterator< IndexIteratorT, IteratorT > IndexIterate(IndexIteratorT idxIt, IteratorT it)
Definition: IndexedIterator.h:154
cross
Point cross(const Point &x, const Point &y)
Definition: point.hpp:33
MiscLib::Vector::end
T * end()
Definition: Vector.h:470
Plane::SerializedFloatSize
static size_t SerializedFloatSize()
Definition: Plane.cpp:243
Plane::m_pos
Vec3f m_pos
Definition: Plane.h:88
IndexedIterator.h
PointCloud
Definition: PointCloud.h:69
Plane::Init
bool Init(Vec3f p1, Vec3f p2, Vec3f p3)
Definition: Plane.cpp:29
Plane::m_normal
Vec3f m_normal
Definition: Plane.h:87
Plane::Intersect
float Intersect(const Vec3f &p, const Vec3f &r) const
Definition: Plane.cpp:273
Vec3f::sqrLength
float sqrLength() const
Definition: basic.h:115
Vec3f::dot
float dot(const Vec3f &v) const
Definition: basic.h:92
Plane::SignedDistance
float SignedDistance(const Vec3f &pos) const
Definition: Plane.h:38
Plane::getNormal
const Vec3f & getNormal() const
Definition: Plane.h:51
Plane::Serialize
void Serialize(bool binary, std::ostream *o) const
Definition: Plane.cpp:203
Plane
Definition: Plane.h:16
LevMarPlane::Normalize
void Normalize(ScalarType *params) const
Definition: Plane.cpp:169
GfxTL::Vec3f
VectorXD< 3, float > Vec3f
Definition: VectorXD.h:691
pc
Introduction Thank you for taking interest in our work and downloading this software This library implements the algorithm described in the paper R R R Klein Efficient RANSAC for Point Cloud Shape in Computer Graphics Blackwell June If you use this software you should cite the aforementioned paper in any resulting publication Please send comments or bug reports to Ruwen Roland BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE Example usage This section shows how to use the library to detect the shapes in a point cloud PointCloud pc
Definition: ReadMe.txt:68
Plane::m_dist
float m_dist
Definition: Plane.h:89
Plane::getDistance
float getDistance(const Vec3f &pos) const
Definition: Plane.h:30
Plane::~Plane
virtual ~Plane()
Definition: Plane.cpp:25
GfxTL::MeanOfNormals
bool MeanOfNormals(NormalsItT begin, NormalsItT end, WeightsItT weights, MeanT *mean)
Definition: Mean.h:60